resource = $resource; $this->sourceCustomerFactory = $sourceCustomerFactory; $this->sourceCustomerCollectionFactory = $sourceCustomerCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->collectionProcessor = $collectionProcessor; $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory; $this->sourceRepository = $sourceRepository; } /** * @inheritDoc */ public function save( SourceCustomerInterface $sourceCustomer ) { try { $this->sourceRepository->get($sourceCustomer->getSourceCode()); if (!$sourceCustomer->getEntityId() && $sourceCustomer->getCustomerId()) { $collection = $this->sourceCustomerCollectionFactory->create() ->setItemObjectClass(SourceCustomer::class) ->addFilter(SourceCustomerInterface::CUSTOMER_ID, $sourceCustomer->getCustomerId()); $temp = $collection->getFirstItem(); if ($temp->getEntityId() && !$sourceCustomer->getEntityId()) { $sourceCustomer->setEntityId($temp->getEntityId()); } } $this->resource->save($sourceCustomer); } catch (\Exception $exception) { throw new CouldNotSaveException(__( 'Could not save the inventorySourceCustomer: %1', $exception->getMessage() )); } return $sourceCustomer; } public function getFromCustomerId(int $customerId): ?SourceCustomerInterface { $inventorySourceCustomer = $this->sourceCustomerFactory->create(); $this->resource->load($inventorySourceCustomer, $customerId, 'customer_id'); if (!$inventorySourceCustomer->getId()) { throw new NoSuchEntityException(__('SourceCustomer with customer id "%1" does not exist.', $customerId)); } return $inventorySourceCustomer; } /** * @inheritDoc */ public function get($id) { $inventorySourceCustomer = $this->sourceCustomerFactory->create(); $this->resource->load($inventorySourceCustomer, $id); if (!$inventorySourceCustomer->getId()) { throw new NoSuchEntityException(__('SourceCustomer with id "%1" does not exist.', $id)); } return $inventorySourceCustomer; } /** * @inheritDoc */ public function getList( string $customerId ) { $criteria = $this->searchCriteriaBuilderFactory->create() ->addFilter(SourceInterface::ENABLED, true) ->addFilter('is_pickup_location_active', 1) ->create(); $searchResults = $this->sourceRepository->getList($criteria); $items = []; $index = 0; /* @var $model SourceInterface */ $sourceCustomer = $this->sourceCustomerFactory->create(); $this->resource->load($sourceCustomer, $customerId, 'customer_id'); foreach ($searchResults->getItems() as $model) { if ($sourceCustomer->getSourceCode() == $model->getSourceCode()) { $items[0] = $sourceCustomer->setSource($model); } else { $index += 1; $items[$index] = $this->sourceCustomerFactory->create()->setSource($model); } } ksort($items); $searchResults->setItems(array_values($items)); $searchResults->setTotalCount($searchResults->getTotalCount()); return $searchResults; } /** * @inheritDoc */ public function delete( SourceCustomerInterface $inventorySourceCustomer ) { try { $inventorySourceCustomerModel = $this->sourceCustomerFactory->create(); $this->resource->load($inventorySourceCustomerModel, $inventorySourceCustomer->getInventorysourcecustomerId()); $this->resource->delete($inventorySourceCustomerModel); } catch (\Exception $exception) { throw new CouldNotDeleteException(__( 'Could not delete the SourceCustomer: %1', $exception->getMessage() )); } return true; } /** * @inheritDoc */ public function deleteById(int $entityId) { return $this->delete($this->get($entityId)); } }