tax_classes.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  7. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  8. $customerTaxClass1 = $objectManager->create(
  9. \Magento\Tax\Model\ClassModel::class
  10. )->setClassName(
  11. 'CustomerTaxClass1'
  12. )->setClassType(
  13. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER
  14. )->save();
  15. $customerTaxClass2 = $objectManager->create(
  16. \Magento\Tax\Model\ClassModel::class
  17. )->setClassName(
  18. 'CustomerTaxClass2'
  19. )->setClassType(
  20. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER
  21. )->save();
  22. $productTaxClass1 = $objectManager->create(
  23. \Magento\Tax\Model\ClassModel::class
  24. )->setClassName(
  25. 'ProductTaxClass1'
  26. )->setClassType(
  27. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT
  28. )->save();
  29. $productTaxClass2 = $objectManager->create(
  30. \Magento\Tax\Model\ClassModel::class
  31. )->setClassName(
  32. 'ProductTaxClass2'
  33. )->setClassType(
  34. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT
  35. )->save();
  36. // Tax class created but not used in the rule to ensure that unused tax classes are handled properly
  37. $productTaxClass3 = $objectManager->create(
  38. \Magento\Tax\Model\ClassModel::class
  39. )->setClassName(
  40. 'ProductTaxClass3'
  41. )->setClassType(
  42. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT
  43. )->save();
  44. $taxRate = [
  45. 'tax_country_id' => 'US',
  46. 'tax_region_id' => '12',
  47. 'tax_postcode' => '*',
  48. 'code' => '*',
  49. 'rate' => '7.5',
  50. ];
  51. $rate = $objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)->setData($taxRate)->save();
  52. /** @var Magento\Framework\Registry $registry */
  53. $registry = $objectManager->get(\Magento\Framework\Registry::class);
  54. $registry->unregister('_fixture/Magento_Tax_Model_Calculation_Rate');
  55. $registry->register('_fixture/Magento_Tax_Model_Calculation_Rate', $rate);
  56. $ruleData = [
  57. 'code' => 'Test Rule',
  58. 'priority' => '0',
  59. 'position' => '0',
  60. 'customer_tax_class_ids' => [$customerTaxClass1->getId(), $customerTaxClass2->getId()],
  61. 'product_tax_class_ids' => [$productTaxClass1->getId(), $productTaxClass2->getId()],
  62. 'tax_rate_ids' => [$rate->getId()],
  63. 'tax_rates_codes' => [$rate->getId() => $rate->getCode()],
  64. ];
  65. $taxRule = $objectManager->create(\Magento\Tax\Model\Calculation\Rule::class)->setData($ruleData)->save();
  66. $registry->unregister('_fixture/Magento_Tax_Model_Calculation_Rule');
  67. $registry->register('_fixture/Magento_Tax_Model_Calculation_Rule', $taxRule);
  68. $ruleData['code'] = 'Test Rule Duplicate';
  69. $objectManager->create(\Magento\Tax\Model\Calculation\Rule::class)->setData($ruleData)->save();