CollectionPointManagement.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\CollectionPoint;
  6. use Magento\Framework\Api\SearchCriteriaBuilder;
  7. use Magento\Framework\Exception\CouldNotDeleteException;
  8. use Magento\Framework\Exception\CouldNotSaveException;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Temando\Shipping\Api\Data\CollectionPoint\CollectionPointSearchResultInterface;
  11. use Temando\Shipping\Api\Data\CollectionPoint\QuoteCollectionPointInterface;
  12. use Temando\Shipping\Api\Data\CollectionPoint\SearchRequestInterface;
  13. use Temando\Shipping\Api\Data\CollectionPoint\SearchRequestInterfaceFactory;
  14. use Temando\Shipping\Model\ResourceModel\Repository\CollectionPointSearchRepositoryInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  16. /**
  17. * Manage Collection Point Access
  18. *
  19. * @deprecated since 1.4.0
  20. * @see \Temando\Shipping\Model\Delivery\CollectionPointManagement
  21. *
  22. * @package Temando\Shipping\Model
  23. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  24. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  25. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. * @link https://www.temando.com/
  27. */
  28. class CollectionPointManagement
  29. {
  30. /**
  31. * @var CollectionPointSearchRepositoryInterface
  32. */
  33. private $searchRequestRepository;
  34. /**
  35. * @var SearchRequestInterfaceFactory
  36. */
  37. private $searchRequestFactory;
  38. /**
  39. * @var QuoteCollectionPointRepositoryInterface
  40. */
  41. private $collectionPointRepository;
  42. /**
  43. * @var SearchCriteriaBuilder
  44. */
  45. private $searchCriteriaBuilder;
  46. /**
  47. * CollectionPointManagement constructor.
  48. *
  49. * @param CollectionPointSearchRepositoryInterface $searchRequestRepository
  50. * @param SearchRequestInterfaceFactory $searchRequestFactory
  51. * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
  52. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  53. */
  54. public function __construct(
  55. CollectionPointSearchRepositoryInterface $searchRequestRepository,
  56. SearchRequestInterfaceFactory $searchRequestFactory,
  57. QuoteCollectionPointRepositoryInterface $collectionPointRepository,
  58. SearchCriteriaBuilder $searchCriteriaBuilder
  59. ) {
  60. $this->searchRequestRepository = $searchRequestRepository;
  61. $this->searchRequestFactory = $searchRequestFactory;
  62. $this->collectionPointRepository = $collectionPointRepository;
  63. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  64. }
  65. /**
  66. * Save new search parameters, delete previous search results.
  67. *
  68. * @param int $addressId
  69. * @param string $countryId
  70. * @param string $postcode
  71. * @param bool $pending
  72. * @return \Temando\Shipping\Api\Data\CollectionPoint\SearchRequestInterface
  73. * @throws CouldNotSaveException
  74. */
  75. public function saveSearchRequest($addressId, $countryId, $postcode, $pending = false)
  76. {
  77. $data = [
  78. SearchRequestInterface::SHIPPING_ADDRESS_ID => $addressId,
  79. SearchRequestInterface::PENDING => $pending,
  80. ];
  81. if ($countryId && $postcode) {
  82. $data[SearchRequestInterface::COUNTRY_ID] = $countryId;
  83. $data[SearchRequestInterface::POSTCODE] = $postcode;
  84. }
  85. $searchRequest = $this->searchRequestFactory->create(['data' => $data]);
  86. try {
  87. $this->searchRequestRepository->save($searchRequest);
  88. $this->deleteCollectionPoints($addressId);
  89. } catch (LocalizedException $exception) {
  90. throw new CouldNotSaveException(__('Unable to save search parameters.'), $exception);
  91. }
  92. return $searchRequest;
  93. }
  94. /**
  95. * Delete search parameters, delete previous search results.
  96. *
  97. * @param int $addressId
  98. * @return bool
  99. * @throws CouldNotDeleteException
  100. */
  101. public function deleteSearchRequest($addressId)
  102. {
  103. try {
  104. $this->searchRequestRepository->delete($addressId);
  105. $this->deleteCollectionPoints($addressId);
  106. } catch (LocalizedException $exception) {
  107. throw new CouldNotDeleteException(__('Unable to delete search parameters.'), $exception);
  108. }
  109. return true;
  110. }
  111. /**
  112. * Load all collection point search results for a given shipping address id.
  113. *
  114. * @param int $addressId
  115. * @return \Temando\Shipping\Api\Data\CollectionPoint\QuoteCollectionPointInterface[]
  116. */
  117. public function getCollectionPoints($addressId)
  118. {
  119. $this->searchCriteriaBuilder->addFilter(
  120. QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID,
  121. $addressId
  122. );
  123. $criteria = $this->searchCriteriaBuilder->create();
  124. $searchResult = $this->collectionPointRepository->getList($criteria);
  125. return $searchResult->getItems();
  126. }
  127. /**
  128. * Delete all collection point search results for a given shipping address id.
  129. *
  130. * @param int $addressId
  131. * @return CollectionPointSearchResultInterface
  132. * @throws CouldNotDeleteException
  133. */
  134. public function deleteCollectionPoints($addressId)
  135. {
  136. $this->searchCriteriaBuilder->addFilter(
  137. QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID,
  138. $addressId
  139. );
  140. $criteria = $this->searchCriteriaBuilder->create();
  141. try {
  142. $searchResult = $this->collectionPointRepository->getList($criteria);
  143. $collectionPoints = $searchResult->getItems();
  144. array_walk($collectionPoints, function (QuoteCollectionPointInterface $collectionPoint) {
  145. $this->collectionPointRepository->delete($collectionPoint);
  146. });
  147. } catch (LocalizedException $exception) {
  148. throw new CouldNotDeleteException(__('Unable to delete collection points.'), $exception);
  149. }
  150. return $searchResult;
  151. }
  152. /**
  153. * Mark a collection point search result as selected for a given shipping address id.
  154. *
  155. * @param int $addressId
  156. * @param int $entityId
  157. * @return bool
  158. * @throws CouldNotSaveException
  159. */
  160. public function selectCollectionPoint($addressId, $entityId)
  161. {
  162. $collectionPoints = $this->getCollectionPoints($addressId);
  163. try {
  164. array_walk($collectionPoints, function (QuoteCollectionPointInterface $collectionPoint) use ($entityId) {
  165. $isSelected = ($entityId == $collectionPoint->getEntityId());
  166. /** @var $collectionPoint QuoteCollectionPoint */
  167. $collectionPoint->setData(QuoteCollectionPointInterface::SELECTED, $isSelected);
  168. $this->collectionPointRepository->save($collectionPoint);
  169. });
  170. } catch (\Exception $exception) {
  171. throw new CouldNotSaveException(__('Unable to select collection point.'), $exception);
  172. }
  173. return true;
  174. }
  175. }