/** * {@inheritdoc} */ public function isAssignedToObjects() { $searchCriteria = $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('tax_class_id')->setValue($this->getId())->create()])->create(); $result = $this->groupService->searchGroups($searchCriteria); $items = $result->getItems(); return !empty($items); }
/** * Invoke InvoiceCommentsList service * * @param int $id * @return \Magento\Sales\Service\V1\Data\CommentSearchResults */ public function invoke($id) { $this->criteriaBuilder->addFilter(['eq' => $this->filterBuilder->setField('parent_id')->setValue($id)->create()]); $criteria = $this->criteriaBuilder->create(); $comments = []; foreach ($this->commentRepository->find($criteria) as $comment) { $comments[] = $this->commentMapper->extractDto($comment); } return $this->searchResultsBuilder->setItems($comments)->setSearchCriteria($criteria)->setTotalCount(count($comments))->create(); }
/** * Retrieve all product tax class options. * * @param bool $withEmpty * @return array */ public function getAllOptions($withEmpty = false) { if (!$this->_options) { $filter = $this->_filterBuilder->setField(TaxClass::KEY_TYPE)->setValue(\Magento\Tax\Service\V1\TaxClassServiceInterface::TYPE_PRODUCT)->create(); $searchCriteria = $this->_searchCriteriaBuilder->addFilter([$filter])->create(); $searchResults = $this->_taxClassService->searchTaxClass($searchCriteria); foreach ($searchResults->getItems() as $taxClass) { $this->_options[] = array('value' => $taxClass->getClassId(), 'label' => $taxClass->getClassName()); } } if ($withEmpty) { return array_merge(array(array('value' => '0', 'label' => __('None'))), $this->_options); } return $this->_options; }
/** * Load search results * * @return $this */ public function load() { $result = []; if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) { $this->setResults($result); return $this; } $this->_searchCriteriaBuilder->setCurrentPage($this->getStart()); $this->_searchCriteriaBuilder->setPageSize($this->getLimit()); $searchFields = ['firstname', 'lastname', 'company']; $filters = []; foreach ($searchFields as $field) { $filters[] = $this->_filterBuilder->setField($field)->setConditionType('like')->setValue($this->getQuery() . '%')->create(); } $this->_searchCriteriaBuilder->addFilter($filters); $searchCriteria = $this->_searchCriteriaBuilder->create(); $searchResults = $this->_customerService->searchCustomers($searchCriteria); /** @var \Magento\Customer\Service\V1\Data\CustomerDetails $customerDetails */ foreach ($searchResults->getItems() as $customerDetails) { $customerData = $customerDetails->getCustomer(); $customerAddresses = $customerDetails->getAddresses(); /** Look for a company name defined in default billing address */ $company = null; foreach ($customerAddresses as $customerAddress) { if ($customerAddress->isDefaultBilling()) { $company = $customerAddress->getCompany(); break; } } $result[] = array('id' => 'customer/1/' . $customerData->getId(), 'type' => __('Customer'), 'name' => $this->_customerViewHelper->getCustomerName($customerData), 'description' => $company, 'url' => $this->_adminhtmlData->getUrl('customer/index/edit', array('id' => $customerData->getId()))); } $this->setResults($result); return $this; }
/** * Creates a search criteria DTO based on the array of field filters. * * @return SearchCriteria */ protected function getSearchCriteria() { foreach ($this->fieldFilters as $filter) { // array of fields, put filters in array to use 'or' group /** @var Filter[] $filterGroup */ $filterGroup = array(); if (!is_array($filter['field'])) { // just one field $filterGroup = [$this->createFilterData($filter['field'], $filter['condition'])]; } else { foreach ($filter['field'] as $index => $field) { $filterGroup[] = $this->createFilterData($field, $filter['condition'][$index]); } } $this->searchCriteriaBuilder->addFilter($filterGroup); } foreach ($this->_orders as $field => $direction) { /** @var \Magento\Framework\Service\V1\Data\SortOrder $sortOrder */ /** @var string $direction */ $direction = $direction == 'ASC' ? SearchCriteria::SORT_ASC : SearchCriteria::SORT_DESC; $sortOrder = $this->sortOrderBuilder->setField($field)->setDirection($direction)->create(); $this->searchCriteriaBuilder->addSortOrder($sortOrder); } $this->searchCriteriaBuilder->setCurrentPage($this->_curPage); $this->searchCriteriaBuilder->setPageSize($this->_pageSize); return $this->searchCriteriaBuilder->create(); }
/** * load entity * * @param int $id * @return Transaction * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\InputException */ public function get($id) { if (!$id) { throw new \Magento\Framework\Exception\InputException('ID required'); } if (!isset($this->registry[$id])) { $filter = $this->filterBuilder->setField('transaction_id')->setValue($id)->setConditionType('eq')->create(); $this->searchCriteriaBuilder->addFilter([$filter]); $this->find($this->searchCriteriaBuilder->create()); if (!isset($this->registry[$id])) { throw new \Magento\Framework\Exception\NoSuchEntityException('Requested entity doesn\'t exist'); } } return $this->registry[$id]; }
/** * {@inheritdoc} */ public function getTaxClassId($taxClassKey, $taxClassType = TaxClassServiceInterface::TYPE_PRODUCT) { if (!empty($taxClassKey)) { switch ($taxClassKey->getType()) { case TaxClassKey::TYPE_ID: return $taxClassKey->getValue(); case TaxClassKey::TYPE_NAME: $searchCriteria = $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(TaxClass::KEY_TYPE)->setValue($taxClassType)->create()])->addFilter([$this->filterBuilder->setField(TaxClass::KEY_NAME)->setValue($taxClassKey->getValue())->create()])->create(); $taxClasses = $this->searchTaxClass($searchCriteria)->getItems(); $taxClass = array_shift($taxClasses); return null == $taxClass ? null : $taxClass->getClassId(); default: } } return null; }
/** * {@inheritdoc} */ public function getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId) { $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(TaxRule::CUSTOMER_TAX_CLASS_IDS)->setValue([$customerTaxClassId])->create()]); $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(TaxRule::PRODUCT_TAX_CLASS_IDS)->setValue([$productTaxClassId])->create()]); $searchResults = $this->searchTaxRules($this->searchCriteriaBuilder->create()); $taxRules = $searchResults->getItems(); $rates = []; foreach ($taxRules as $taxRule) { $rateIds = $taxRule->getTaxRateIds(); if (!empty($rateIds)) { foreach ($rateIds as $rateId) { $rates[] = $this->taxRateService->getTaxRate($rateId); } } } return $rates; }
public function testSearchTaxRate() { $collectionMock = $this->getMockBuilder('Magento\\Tax\\Model\\Resource\\Calculation\\Rate\\Collection')->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getSize', 'load', 'getIterator', 'joinRegionTable'])->getMock(); $this->mockReturnValue($collectionMock, ['getSize' => 1, 'getIterator' => new \ArrayIterator([$this->rateModelMock])]); $this->rateFactoryMock->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->rateModelMock)); $this->mockReturnValue($this->rateModelMock, ['load' => $this->returnSelf(), 'getCollection' => $collectionMock]); $taxRate = $this->taxRateBuilder->setId(2)->setCode('Rate-Code')->setCountryId('US')->setPercentageRate(0.1)->setPostcode('55555')->setRegionId(self::REGION_ID)->create(); $this->converterMock->expects($this->once())->method('createTaxRateDataObjectFromModel')->with($this->rateModelMock)->will($this->returnValue($taxRate)); $filterBuilder = $this->objectManager->getObject('\\Magento\\Framework\\Service\\V1\\Data\\FilterBuilder'); $filter = $filterBuilder->setField(TaxRate::KEY_REGION_ID)->setValue(self::REGION_ID)->create(); $this->searchCriteriaBuilder->addFilter([$filter])->addSortOrder('id', \Magento\Framework\Service\V1\Data\SearchCriteria::SORT_ASC); $this->createService(); $searchCriteria = $this->searchCriteriaBuilder->create(); $searchResults = $this->taxRateService->searchTaxRates($searchCriteria); $items = $searchResults->getItems(); $this->assertNotNull($searchResults); $this->assertSame($searchCriteria, $searchResults->getSearchCriteria()); $this->assertEquals(1, $searchResults->getTotalCount()); $this->assertNotNull($items); $this->assertFalse(empty($items)); $this->assertEquals(self::REGION_ID, $items[0]->getRegionId()); }
public function testSearch() { $metadata = array(); $attributeCodes = ['price', 'id', 'sku']; foreach ($attributeCodes as $code) { $attributeMetadataMock = $this->getMockBuilder('\\Magento\\Catalog\\Service\\V1\\Data\\Eav\\AttributeMetadata')->disableOriginalConstructor()->getMock(); $attributeMetadataMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue($code)); $metadata[] = $attributeMetadataMock; } $this->metadataServiceMock->expects($this->any())->method('getProductAttributesMetadata')->will($this->returnValue($metadata)); $collection = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection')->disableOriginalConstructor()->getMock(); $collection->expects($this->any())->method('addAttributeToSelect'); $collection->expects($this->any())->method('joinAttribute'); $collection->expects($this->any())->method('addOrder')->with($this->equalTo('price'), $this->equalTo('ASC')); $collection->expects($this->once())->method('setCurPage')->with($this->equalTo(1)); $collection->expects($this->once())->method('setPageSize')->with($this->equalTo(10)); $collection->expects($this->once())->method('getSize')->will($this->returnValue(5)); $this->productCollection->expects($this->once())->method('create')->will($this->returnValue($collection)); $this->_mockReturnValue($collection, array('getSize' => 1, '_getItems' => array($this->_productMock), 'getIterator' => new \ArrayIterator(array($this->_productMock)))); $productDataBuilder = $this->getMockBuilder('\\Magento\\Catalog\\Service\\V1\\Data\\ProductBuilder')->disableOriginalConstructor()->getMock(); $productDataBuilder->setPrice('10.000'); $productDataBuilder->setSku('test'); $productDataBuilder->setStoreId(10); $this->converterMock->expects($this->once())->method('createProductBuilderFromModel')->will($this->returnValue($productDataBuilder)); $this->searchResultsBuilderMock->expects($this->once())->method('setItems')->with($this->equalTo(array($productDataBuilder->create()))); $this->searchResultsBuilderMock->expects($this->once())->method('create')->will($this->returnValue(array($productDataBuilder->create()))); $productService = $this->_objectManager->getObject('Magento\\Catalog\\Service\\V1\\ProductService', ['productLoader' => $this->_productLoaderMock, 'productCollection' => $this->productCollection, 'searchResultsBuilder' => $this->searchResultsBuilderMock, 'metadataService' => $this->metadataServiceMock, 'converter' => $this->converterMock]); $helper = new \Magento\TestFramework\Helper\ObjectManager($this); $filterBuilder = $helper->getObject('\\Magento\\Framework\\Service\\V1\\Data\\FilterBuilder'); $filter = $filterBuilder->setField('price')->setValue('10.000')->setConditionType('eq')->create(); $this->_searchBuilder->addFilter([$filter]); $sortOrderBuilder = $helper->getObject('\\Magento\\Framework\\Service\\V1\\Data\\SortOrderBuilder'); $sortOrder = $sortOrderBuilder->setField('price')->setDirection(SearchCriteria::SORT_ASC)->create(); $this->_searchBuilder->addSortOrder($sortOrder); $this->_searchBuilder->setCurrentPage(1); $this->_searchBuilder->setPageSize(10); $productService->search($this->_searchBuilder->create()); }
public function testSearchCustomers() { $collectionMock = $this->getMockBuilder('\\Magento\\Customer\\Model\\Resource\\Customer\\Collection')->disableOriginalConstructor()->setMethods(['addNameToSelect', 'addFieldToFilter', 'getSize', 'load', 'getItems', 'getIterator', 'joinAttribute'])->getMock(); $collectionMock->expects($this->any())->method('joinAttribute')->will($this->returnSelf()); $this->_mockReturnValue($collectionMock, array('getSize' => 1, '_getItems' => array($this->_customerModelMock), 'getIterator' => new \ArrayIterator(array($this->_customerModelMock)))); $this->_customerFactoryMock->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_customerModelMock)); $this->_mockReturnValue($this->_customerModelMock, array('load' => $this->returnSelf(), 'getId' => self::ID, 'getEmail' => self::EMAIL, 'getCollection' => $collectionMock, 'getAttributes' => array())); $this->_customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_customerModelMock)); $this->_customerAddressServiceMock->expects($this->once())->method('getAddresses')->will($this->returnValue(array())); $this->_customerMetadataService->expects($this->any())->method('getAllCustomerAttributeMetadata')->will($this->returnValue(array())); $customerService = $this->_createService(); $filterBuilder = $this->_objectManager->getObject('\\Magento\\Framework\\Service\\V1\\Data\\FilterBuilder'); $filter = $filterBuilder->setField('email')->setValue(self::EMAIL)->create(); $this->_searchBuilder->addFilter([$filter]); $searchResults = $customerService->searchCustomers($this->_searchBuilder->create()); $this->assertEquals(1, $searchResults->getTotalCount()); $this->assertEquals(self::EMAIL, $searchResults->getItems()[0]->getCustomer()->getEmail()); }
/** * Retrieve EAV attribute metadata of category * * @param int $attributeSetId * @return AttributeMetadata[] */ public function getCategoryAttributesMetadata($attributeSetId = MetadataServiceInterface::DEFAULT_ATTRIBUTE_SET_ID) { $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('attribute_set_id')->setValue($attributeSetId)->create()]); return $this->metadataService->getAllAttributeMetadata(MetadataServiceInterface::ENTITY_TYPE, $this->searchCriteriaBuilder->create())->getItems(); }
/** * Retrieve EAV attribute metadata of product * * @param int $attributeSetId * @return AttributeMetadata[] */ public function getProductAttributesMetadata($attributeSetId = self::DEFAULT_ATTRIBUTE_SET_ID) { /** @var \Magento\Framework\Service\V1\Data\SearchCriteriaBuilder $searchCriteria */ $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField('attribute_set_id')->setValue($attributeSetId)->create()]); return $this->metadataService->getAllAttributeMetadata(MetadataServiceInterface::ENTITY_TYPE, $this->searchCriteriaBuilder->create())->getItems(); }