Skip to content

Commit

Permalink
Merge pull request #1 from aligent/create_tax_fixtures
Browse files Browse the repository at this point in the history
Data fixtures to create Australian tax rules.
  • Loading branch information
AdamJHall committed Dec 4, 2017
2 parents 2b9e31d + 036c1c2 commit e0030f3
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Migrations/Data/ORM/LoadAuTaxConfigurationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Aligent\AustraliaBundle\Migrations\Data\ORM;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\TaxBundle\DependencyInjection\OroTaxExtension;
use Oro\Bundle\TaxBundle\Provider\TaxationSettingsProvider;
use Oro\Bundle\TaxBundle\Entity\AbstractTaxCode;

class LoadAuTaxConfigurationData extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
{
use ContainerAwareTrait;

/** @var array */
private static $configurations = [
'origin_address' => [
'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();
}
}
220 changes: 220 additions & 0 deletions src/Migrations/Data/ORM/LoadAuTaxTableRatesData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php

namespace Aligent\AustraliaBundle\Migrations\Data\ORM;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Oro\Bundle\AddressBundle\Entity\Country;
use Oro\Bundle\AddressBundle\Entity\Region;
use Oro\Bundle\UserBundle\Entity\Role;
use Oro\Bundle\UserBundle\Entity\User;
use Oro\Bundle\TaxBundle\Migrations\TaxEntitiesFactory;
use Oro\Bundle\TaxBundle\Entity\TaxJurisdiction;

class LoadAuTaxTableRatesData extends AbstractFixture implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* @var TaxEntitiesFactory
*/
private $entitiesFactory;

public function __construct()
{
$this->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;
}
}
56 changes: 56 additions & 0 deletions src/Migrations/Data/ORM/data/tax_table_rates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

// The categories of organizations and/or items below that are excluded from sales tax collection in California
// are selected for demo and testing purposes only. More examples can be found in
// Sales and Use Tax: Exemptions and Exclusions (Publication 61) at http://www.boe.ca.gov/pdf/pub61.pdf

$customerTaxCodes = [
'EXEMPT' => [
'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,
];

0 comments on commit e0030f3

Please sign in to comment.