/**
  * @magentoDbIsolation enabled
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Updating classType is not allowed.
  */
 public function testUpdateTaxClassWithChangingClassType()
 {
     $taxClassName = 'New Class Name';
     $taxClassDataObject = $this->taxClassBuilder->setClassName($taxClassName)->setClassType(TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)->create();
     $taxClassId = $this->taxClassService->createTaxClass($taxClassDataObject);
     $this->assertEquals($taxClassName, $this->taxClassModel->load($taxClassId)->getClassName());
     $updatedTaxClassName = 'Updated Class Name';
     $taxClassDataObject = $this->taxClassBuilder->setClassName($updatedTaxClassName)->setClassType(TaxClassModel::TAX_CLASS_TYPE_PRODUCT)->create();
     $this->taxClassService->updateTaxClass($taxClassId, $taxClassDataObject);
 }
Example #2
0
 /**
  * @magentoDbIsolation enabled
  */
 public function testGetTaxClassId()
 {
     $taxClassName = 'Get Me';
     $taxClassDataObject = $this->taxClassBuilder->setClassName($taxClassName)->setClassType(TaxClassServiceInterface::TYPE_CUSTOMER)->create();
     $taxClassId = $this->taxClassService->createTaxClass($taxClassDataObject);
     /** @var \Magento\Tax\Service\V1\Data\TaxClassKeyBuilder $taxClassKeyBuilder */
     $taxClassKeyBuilder = $this->objectManager->create('Magento\\Tax\\Service\\V1\\Data\\TaxClassKeyBuilder');
     $taxClassKeyTypeId = $taxClassKeyBuilder->populateWithArray([TaxClassKey::KEY_TYPE => TaxClassKey::TYPE_ID, TaxClassKey::KEY_VALUE => $taxClassId])->create();
     $this->assertEquals($taxClassId, $this->taxClassService->getTaxClassId($taxClassKeyTypeId, TaxClassServiceInterface::TYPE_CUSTOMER));
     $taxClassKeyTypeName = $taxClassKeyBuilder->populateWithArray([TaxClassKey::KEY_TYPE => TaxClassKey::TYPE_NAME, TaxClassKey::KEY_VALUE => $taxClassName])->create();
     $this->assertEquals($taxClassId, $this->taxClassService->getTaxClassId($taxClassKeyTypeId, TaxClassServiceInterface::TYPE_CUSTOMER));
     $this->assertNull($this->taxClassService->getTaxClassId(null));
     $this->assertEquals(null, $this->taxClassService->getTaxClassId($taxClassKeyTypeName, TaxClassServiceInterface::TYPE_PRODUCT));
 }
 public function testSearch()
 {
     $collectionSize = 3;
     $currentPage = 1;
     $pageSize = 10;
     $searchCriteria = $this->createSearchCriteria();
     $this->searchResultBuilder->expects($this->once())->method('setSearchCriteria')->with($searchCriteria);
     /** @var \PHPUnit_Framework_MockObject_MockObject $collectionMock */
     $collectionMock = $this->getMockBuilder('Magento\\Tax\\Model\\Resource\\TaxClass\\Collection')->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getSize', 'setCurPage', 'setPageSize', 'getItems', 'addOrder'])->getMock();
     $this->taxClassCollectionFactory->expects($this->once())->method('create')->will($this->returnValue($collectionMock));
     $collectionMock->expects($this->exactly(2))->method('addFieldToFilter');
     $collectionMock->expects($this->any())->method('getSize')->will($this->returnValue($collectionSize));
     $collectionMock->expects($this->once())->method('setCurPage')->with($currentPage);
     $collectionMock->expects($this->once())->method('setPageSize')->with($pageSize);
     $collectionMock->expects($this->once())->method('addOrder')->with('class_name', 'ASC');
     /** @var \PHPUnit_Framework_MockObject_MockObject $taxClassModelMock */
     $taxClassModelMock = $this->getMockBuilder('Magento\\Tax\\Model\\ClassModel')->disableOriginalConstructor()->getMock();
     $collectionMock->expects($this->once())->method('getItems')->will($this->returnValue([$taxClassModelMock]));
     /** @var \PHPUnit_Framework_MockObject_MockObject $taxMock */
     $taxClassMock = $this->getMockBuilder('Magento\\Tax\\Service\\V1\\Data\\TaxClass')->disableOriginalConstructor()->getMock();
     $this->converterMock->expects($this->once())->method('createTaxClassData')->with($taxClassModelMock)->will($this->returnValue($taxClassMock));
     $this->searchResultBuilder->expects($this->once())->method('setItems')->will($this->returnValue([$taxClassMock]));
     $this->taxClassService->searchTaxClass($searchCriteria);
 }
Example #4
0
 public function testGetTaxClassIdEmptyTaxClassKey()
 {
     $this->assertNull($this->taxClassService->getTaxClassId(null));
 }
 /**
  * {@inheritdoc}
  */
 public function calculateTax(QuoteDetails $quoteDetails, $storeId = null)
 {
     if (is_null($storeId)) {
         $storeId = $this->storeManager->getStore()->getStoreId();
     }
     // initial TaxDetails data
     $taxDetailsData = [TaxDetails::KEY_SUBTOTAL => 0.0, TaxDetails::KEY_TAX_AMOUNT => 0.0, TaxDetails::KEY_DISCOUNT_TAX_COMPENSATION_AMOUNT => 0.0, TaxDetails::KEY_APPLIED_TAXES => [], TaxDetails::KEY_ITEMS => []];
     $items = $quoteDetails->getItems();
     if (empty($items)) {
         return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->create();
     }
     $this->computeRelationships($items);
     $calculator = $this->calculatorFactory->create($this->config->getAlgorithm($storeId), $storeId, $quoteDetails->getBillingAddress(), $quoteDetails->getShippingAddress(), $this->taxClassService->getTaxClassId($quoteDetails->getCustomerTaxClassKey(), 'customer'), $quoteDetails->getCustomerId());
     $processedItems = [];
     /** @var QuoteDetailsItem $item */
     foreach ($this->keyedItems as $item) {
         if (isset($this->parentToChildren[$item->getCode()])) {
             $processedChildren = [];
             foreach ($this->parentToChildren[$item->getCode()] as $child) {
                 $processedItem = $this->processItem($child, $calculator);
                 $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
                 $processedItems[$processedItem->getCode()] = $processedItem;
                 $processedChildren[] = $processedItem;
             }
             $processedItemBuilder = $this->calculateParent($processedChildren, $item->getQuantity());
             $processedItemBuilder->setCode($item->getCode());
             $processedItemBuilder->setType($item->getType());
             $processedItem = $processedItemBuilder->create();
         } else {
             $processedItem = $this->processItem($item, $calculator);
             $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
         }
         $processedItems[$processedItem->getCode()] = $processedItem;
     }
     return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->setItems($processedItems)->create();
 }
Example #6
0
 /**
  * Validate tax rule
  *
  * @param TaxRule $rule
  * @return void
  * @throws InputException
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 private function validate(TaxRule $rule)
 {
     $exception = new InputException();
     // SortOrder is required and must be 0 or greater
     if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::SORT_ORDER]);
     }
     if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'GreaterThan', [-1])) {
         $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::SORT_ORDER, 'value' => $rule->getSortOrder(), 'minValue' => 0]);
     }
     // Priority is required and must be 0 or greater
     if (!\Zend_Validate::is(trim($rule->getPriority()), 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRIORITY]);
     }
     if (!\Zend_Validate::is(trim($rule->getPriority()), 'GreaterThan', [-1])) {
         $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::PRIORITY, 'value' => $rule->getPriority(), 'minValue' => 0]);
     }
     // Code is required
     if (!\Zend_Validate::is(trim($rule->getCode()), 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CODE]);
     }
     // customer tax class ids is required
     if ($rule->getCustomerTaxClassIds() === null || !$rule->getCustomerTaxClassIds()) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS]);
     } else {
         // see if the customer tax class ids exist
         $customerTaxClassIds = $rule->getCustomerTaxClassIds();
         foreach ($customerTaxClassIds as $customerTaxClassId) {
             try {
                 $taxClass = $this->taxClassService->getTaxClass($customerTaxClassId);
                 if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)) {
                     $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS, 'value' => $customerTaxClassId]);
                 }
             } catch (NoSuchEntityException $e) {
                 $exception->addError($e->getRawMessage(), $e->getParameters());
             }
         }
     }
     // product tax class ids is required
     if ($rule->getProductTaxClassIds() === null || !$rule->getProductTaxClassIds()) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS]);
     } else {
         // see if the product tax class ids exist
         $productTaxClassIds = $rule->getProductTaxClassIds();
         foreach ($productTaxClassIds as $productTaxClassId) {
             try {
                 $taxClass = $this->taxClassService->getTaxClass($productTaxClassId);
                 if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_PRODUCT)) {
                     $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS, 'value' => $productTaxClassId]);
                 }
             } catch (NoSuchEntityException $e) {
                 $exception->addError($e->getRawMessage(), $e->getParameters());
             }
         }
     }
     // tax rate ids is required
     if ($rule->getTaxRateIds() === null || !$rule->getTaxRateIds()) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::TAX_RATE_IDS]);
     }
     // throw exception if errors were found
     if ($exception->wasErrorAdded()) {
         throw $exception;
     }
 }