From 036c1c228fcee93a024b76e27f280d0781a4a0b2 Mon Sep 17 00:00:00 2001 From: Jim O'Halloran Date: Mon, 4 Dec 2017 17:05:52 +1030 Subject: [PATCH] Data fixtures to create Australian tax rules. --- .../Data/ORM/LoadAuTaxConfigurationData.php | 75 ++++++ .../Data/ORM/LoadAuTaxTableRatesData.php | 220 ++++++++++++++++++ .../Data/ORM/data/tax_table_rates.php | 56 +++++ 3 files changed, 351 insertions(+) create mode 100644 src/Migrations/Data/ORM/LoadAuTaxConfigurationData.php create mode 100644 src/Migrations/Data/ORM/LoadAuTaxTableRatesData.php create mode 100644 src/Migrations/Data/ORM/data/tax_table_rates.php diff --git a/src/Migrations/Data/ORM/LoadAuTaxConfigurationData.php b/src/Migrations/Data/ORM/LoadAuTaxConfigurationData.php new file mode 100644 index 0000000..a67231f --- /dev/null +++ b/src/Migrations/Data/ORM/LoadAuTaxConfigurationData.php @@ -0,0 +1,75 @@ + [ + 'country' => 'AU', + 'region' => 'AU-SA', #South Australia + 'region_text' => null, + 'postal_code' => '5000' #Adelaide + ], + 'use_as_base_by_default' => TaxationSettingsProvider::USE_AS_BASE_DESTINATION, + 'address_resolver_granularity' => 'country', + 'shipping_tax_code' => ['GST'], + ]; + + /** + * {@inheritdoc} + */ + public function getDependencies() + { + return [ + 'Aligent\AustraliaBundle\Migrations\Data\ORM\LoadAuTaxTableRatesData' + ]; + } + + /** + * {@inheritdoc} + * + * @param ObjectManager $manager + */ + public function load(ObjectManager $manager) + { + $productTaxCodeRepo = $this->container->get('oro_tax.repository.product_tax_code'); + $taxCodes = $productTaxCodeRepo->findBy(['code' => self::$configurations['shipping_tax_code']]); + + self::$configurations['shipping_tax_code'] = array_map( + function (AbstractTaxCode $taxCode) { + return $taxCode->getId(); + }, + $taxCodes + ); + + $configManager = $this->container->get('oro_config.global'); + + foreach (self::$configurations as $option => $value) { + if ($option == 'shipping_tax_code') { + $foo='bar'; + } + $configManager->set( + OroTaxExtension::ALIAS . ConfigManager::SECTION_MODEL_SEPARATOR . $option, + $value + ); + } + + $configManager->flush(); + } +} diff --git a/src/Migrations/Data/ORM/LoadAuTaxTableRatesData.php b/src/Migrations/Data/ORM/LoadAuTaxTableRatesData.php new file mode 100644 index 0000000..e343199 --- /dev/null +++ b/src/Migrations/Data/ORM/LoadAuTaxTableRatesData.php @@ -0,0 +1,220 @@ +entitiesFactory = new TaxEntitiesFactory(); + } + + /** + * {@inheritdoc} + * + * @param ObjectManager $manager + */ + public function load(ObjectManager $manager) + { + $locator = $this->container->get('file_locator'); + $data = require $locator->locate('@AligentAustraliaBundle/Migrations/Data/ORM/data/tax_table_rates.php'); + + $this->loadCustomerTaxCodes($manager, $data['customer_tax_codes']); + $this->loadProductTaxCodes($manager, $data['product_tax_codes']); + $this->loadTaxes($manager, $data['taxes']); + $this->loadTaxJurisdictions($manager, $data['tax_jurisdictions']); + $this->loadTaxRules($manager, $data['tax_rules']); + + $manager->flush(); + } + + /** + * @param ObjectManager $manager + * @param array $customerTaxCodes + * + * @return $this + */ + private function loadCustomerTaxCodes(ObjectManager $manager, $customerTaxCodes) + { + $owner = $this->getAdminUser($manager); + foreach ($customerTaxCodes as $code => $data) { + $taxCode = $this->entitiesFactory->createCustomerTaxCode($code, $data['description'], $manager, $this); + $taxCode->setOwner($owner); + $taxCode->setOrganization($owner->getOrganization()); + if (isset($data['customer_groups'])) { + foreach ($data['customer_groups'] as $groupName) { + $group = $manager->getRepository('OroCustomerBundle:CustomerGroup')->findOneByName($groupName); + if (null !== $group) { + $group->setTaxCode($taxCode); + } + } + } + } + + return $this; + } + + /** + * @param ObjectManager $manager + * @param array $productTaxCodes + * + * @return $this + */ + private function loadProductTaxCodes(ObjectManager $manager, $productTaxCodes) + { + foreach ($productTaxCodes as $code => $data) { + $taxCode = $this->entitiesFactory->createProductTaxCode($code, $data['description'], $manager, $this); + } + + return $this; + } + + /** + * @param ObjectManager $manager + * @param array $taxes + * + * @return $this + */ + private function loadTaxes(ObjectManager $manager, $taxes) + { + foreach ($taxes as $code => $data) { + $this->entitiesFactory->createTax($code, $data['rate'], $data['description'], $manager, $this); + } + + return $this; + } + + /** + * @param ObjectManager $manager + * @param array $taxJurisdictions + * + * @return $this + */ + private function loadTaxJurisdictions(ObjectManager $manager, $taxJurisdictions) + { + foreach ($taxJurisdictions as $code => $data) { + $country = $this->getCountryByIso2Code($manager, $data['country']); + $region = $this->getRegionByCountryAndCode($manager, $country, $data['state']); + + $jurisdiction = new TaxJurisdiction(); + $jurisdiction->setCode($code); + $jurisdiction->setDescription($data['description']); + if ($country) { + $jurisdiction->setCountry($country); + if ($region) { + $jurisdiction->setRegion($region); + } + } + + $manager->persist($jurisdiction); + $this->addReference($code, $jurisdiction); + } + + return $this; + } + + /** + * @param ObjectManager $manager + * @param array $taxRules + * + * @return $this + */ + private function loadTaxRules(ObjectManager $manager, $taxRules) + { + foreach ($taxRules as $rule) { + /** @var \Oro\Bundle\TaxBundle\Entity\CustomerTaxCode $customerTaxCode */ + $customerTaxCode = $this->getReference($rule['customer_tax_code']); + + /** @var \Oro\Bundle\TaxBundle\Entity\ProductTaxCode $productTaxCode */ + $productTaxCode = $this->getReference($rule['product_tax_code']); + + /** @var \Oro\Bundle\TaxBundle\Entity\TaxJurisdiction $taxJurisdiction */ + $taxJurisdiction = $this->getReference($rule['tax_jurisdiction']); + + /** @var \Oro\Bundle\TaxBundle\Entity\Tax $tax */ + $tax = $this->getReference($rule['tax']); + + $this->entitiesFactory->createTaxRule( + $customerTaxCode, + $productTaxCode, + $taxJurisdiction, + $tax, + isset($rule['description']) ? $rule['description'] : '', + $manager + ); + } + + return $this; + } + + //region Helper methods for the methods that the corresponding repositories do not have + /** + * @param ObjectManager $manager + * @param string $iso2Code + * + * @return Country|null + */ + private function getCountryByIso2Code(ObjectManager $manager, $iso2Code) + { + return $manager->getRepository('OroAddressBundle:Country')->findOneBy(['iso2Code' => $iso2Code]); + } + + /** + * @param ObjectManager $manager + * @param Country $country + * @param string $code + * + * @return Region|null + */ + private function getRegionByCountryAndCode(ObjectManager $manager, Country $country, $code) + { + return $manager->getRepository('OroAddressBundle:Region')->findOneBy(['country' => $country, 'code' => $code]); + } + //endregion + + /** + * @param ObjectManager $manager + * @return User + * @throws \InvalidArgumentException + */ + private function getAdminUser(ObjectManager $manager) + { + $repository = $manager->getRepository(Role::class); + $role = $repository->findOneBy(['role' => User::ROLE_ADMINISTRATOR]); + + if (!$role) { + throw new \InvalidArgumentException('Administrator role should exist.'); + } + + $user = $repository->getFirstMatchedUser($role); + + if (!$user) { + throw new \InvalidArgumentException( + 'Administrator user should exist to load tax codes demo data.' + ); + } + + return $user; + } +} diff --git a/src/Migrations/Data/ORM/data/tax_table_rates.php b/src/Migrations/Data/ORM/data/tax_table_rates.php new file mode 100644 index 0000000..1b774be --- /dev/null +++ b/src/Migrations/Data/ORM/data/tax_table_rates.php @@ -0,0 +1,56 @@ + [ + 'description' => 'GST Exempt Customers', + ], + 'TAXABLE' => [ + 'description' => 'Taxable Customers', + 'customer_groups' => [ + 'Non-Authenticated Visitors' + ] + ], +]; + +$productTaxCodes = [ + 'GST' => [ + 'description' => 'Taxable Items', + ], + 'GST_FREE' => [ + 'description' => 'GST Free (Non-Taxable) Items', + ], +]; + +$taxes = [ + 'AU_GST' => ['rate' => 0.10, 'description' => 'Australian GST'], +]; + +$taxJurisdictions = [ + 'AUSTRALIA' => [ + 'country' => 'AU', + 'state' => '', + 'zip_codes' => [], + 'description' => 'Australia', + ], +]; + +$taxRules = [ + [ + 'customer_tax_code' => 'TAXABLE', + 'product_tax_code' => 'GST', + 'tax_jurisdiction' => 'AUSTRALIA', + 'tax' => 'AU_GST' + ], +]; + +return [ + 'customer_tax_codes' => $customerTaxCodes, + 'product_tax_codes' => $productTaxCodes, + 'taxes' => $taxes, + 'tax_jurisdictions' => $taxJurisdictions, + 'tax_rules' => $taxRules, +];