Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 31, 2015
1 parent f0e51b0 commit b5e35a0
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 212 deletions.
27 changes: 15 additions & 12 deletions code/XeroExtension.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<?php

class XeroExtension_Customer extends DataExtension {
class XeroExtension_Customer extends DataExtension
{

private static $db = array(
'XeroContactID' => 'Varchar(255)'
);
private static $db = array(
'XeroContactID' => 'Varchar(255)'
);
}

class XeroExtension_Order extends DataExtension {
class XeroExtension_Order extends DataExtension
{

private static $db = array(
'XeroInvoiceID' => 'Varchar(255)'
);
private static $db = array(
'XeroInvoiceID' => 'Varchar(255)'
);
}

class XeroExtension_Payment extends DataExtension {
class XeroExtension_Payment extends DataExtension
{

private static $db = array(
'XeroPaymentID' => 'Varchar(255)'
);
private static $db = array(
'XeroPaymentID' => 'Varchar(255)'
);
}
168 changes: 85 additions & 83 deletions code/XeroTaxCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,93 @@
*
* @see http://doc.silverstripe.org/framework/en/reference/injector
*/
abstract class XeroTaxCalculator {

/**
* Apply a tax rate to the Item
*
* @param Item $item
*/
abstract public function applyItemTaxRate(Item $item);

/**
* Apply a tax rate to the Order Modification
*
* @param Modification $mod
*/
abstract public function applyModificationTaxRate(Modification $mod);

/**
* Calculate the tax component based on tax rates for the items and modifications in the order
*
* @param Order $order
* @return Price The tax amount for the order
*/
public function calculate(Order $order) {

$taxAmount = 0;
$shopConfig = ShopConfig::current_shop_config();

$items = $order->Items();
if ($items && $items->exists()) foreach ($items as $item) {
$taxAmount += $item->Total()->getAmount() * ($item->XeroTaxRate / 100);
}

$mods = $order->Modifications();
if ($mods && $mods->exists()) foreach ($mods as $mod) {
$taxAmount += $mod->Amount()->getAmount() * ($mod->XeroTaxRate / 100);
}

$amount = new Price();
$amount->setAmount($taxAmount);
$amount->setCurrency($shopConfig->BaseCurrency);
$amount->setSymbol($shopConfig->BaseCurrencySymbol);
return $amount;
}
abstract class XeroTaxCalculator
{

/**
* Apply a tax rate to the Item
*
* @param Item $item
*/
abstract public function applyItemTaxRate(Item $item);

/**
* Apply a tax rate to the Order Modification
*
* @param Modification $mod
*/
abstract public function applyModificationTaxRate(Modification $mod);

/**
* Calculate the tax component based on tax rates for the items and modifications in the order
*
* @param Order $order
* @return Price The tax amount for the order
*/
public function calculate(Order $order)
{
$taxAmount = 0;
$shopConfig = ShopConfig::current_shop_config();

$items = $order->Items();
if ($items && $items->exists()) {
foreach ($items as $item) {
$taxAmount += $item->Total()->getAmount() * ($item->XeroTaxRate / 100);
}
}

$mods = $order->Modifications();
if ($mods && $mods->exists()) {
foreach ($mods as $mod) {
$taxAmount += $mod->Amount()->getAmount() * ($mod->XeroTaxRate / 100);
}
}

$amount = new Price();
$amount->setAmount($taxAmount);
$amount->setCurrency($shopConfig->BaseCurrency);
$amount->setSymbol($shopConfig->BaseCurrencySymbol);
return $amount;
}
}

/**
* Apply NZ tax rates to Orders being sent to NZ and no tax to orders shipped elsewhere.
*/
class XeroTaxCalculator_NZ extends XeroTaxCalculator {

public function applyItemTaxRate(Item $item) {

$order = $item->Order();

// Orders shipped within New Zealand have tax applied
if ($order && $order->exists() && $order->ShippingCountryCode == 'NZ') {
$item->XeroTaxType = 'OUTPUT2';
$item->XeroTaxRate = 15.00;
}
else {
$item->XeroTaxType = 'NONE';
$item->XeroTaxRate = 0.00;
}

$item->write();
}

public function applyModificationTaxRate(Modification $mod) {

if ($mod->SubTotalModifier) {

$order = $mod->Order();

// Orders shipped within New Zealand have tax applied
if ($order && $order->exists() && $order->ShippingCountryCode == 'NZ') {
$mod->XeroTaxType = 'OUTPUT2';
$mod->XeroTaxRate = 15.00;
}
else {
$mod->XeroTaxType = 'NONE';
$mod->XeroTaxRate = 0.00;
}

$mod->write();
}
}

}
class XeroTaxCalculator_NZ extends XeroTaxCalculator
{

public function applyItemTaxRate(Item $item)
{
$order = $item->Order();

// Orders shipped within New Zealand have tax applied
if ($order && $order->exists() && $order->ShippingCountryCode == 'NZ') {
$item->XeroTaxType = 'OUTPUT2';
$item->XeroTaxRate = 15.00;
} else {
$item->XeroTaxType = 'NONE';
$item->XeroTaxRate = 0.00;
}

$item->write();
}

public function applyModificationTaxRate(Modification $mod)
{
if ($mod->SubTotalModifier) {
$order = $mod->Order();

// Orders shipped within New Zealand have tax applied
if ($order && $order->exists() && $order->ShippingCountryCode == 'NZ') {
$mod->XeroTaxType = 'OUTPUT2';
$mod->XeroTaxRate = 15.00;
} else {
$mod->XeroTaxType = 'NONE';
$mod->XeroTaxRate = 0.00;
}

$mod->write();
}
}
}
24 changes: 13 additions & 11 deletions code/XeroTaxExtension.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

class XeroTaxExtension_Item extends DataExtension {
class XeroTaxExtension_Item extends DataExtension
{

private static $db = array(
'XeroTaxType' => 'Varchar(255)',
'XeroTaxRate' => 'Decimal(19,4)'
);
private static $db = array(
'XeroTaxType' => 'Varchar(255)',
'XeroTaxRate' => 'Decimal(19,4)'
);
}

class XeroTaxExtension_Modification extends DataExtension {
class XeroTaxExtension_Modification extends DataExtension
{

private static $db = array(
'XeroTaxType' => 'Varchar(255)',
'XeroTaxRate' => 'Decimal(19,4)'
);
}
private static $db = array(
'XeroTaxType' => 'Varchar(255)',
'XeroTaxRate' => 'Decimal(19,4)'
);
}
129 changes: 68 additions & 61 deletions code/XeroTaxModification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,84 @@
* dependencies:
* taxCalculator: %$XeroTaxCalculator_NZ
*/
class XeroTaxModification extends Modification {
class XeroTaxModification extends Modification
{

private static $defaults = array(
'SubTotalModifier' => false,
'SortOrder' => 150
);
private static $defaults = array(
'SubTotalModifier' => false,
'SortOrder' => 150
);

public $taxCalculator;
/** @config */
private static $dependencies = array(
'taxCalculator' => '%$XeroTaxCalculator'
);
public $taxCalculator;
/** @config */
private static $dependencies = array(
'taxCalculator' => '%$XeroTaxCalculator'
);

/**
* Add a XeroTaxModification to the order by applying tax rates to Items and Modifications in the order using
* the taxCalculator.
*
* @param Order $order
* @param Mixed $value Unused
*/
public function add($order, $value = null) {
/**
* Add a XeroTaxModification to the order by applying tax rates to Items and Modifications in the order using
* the taxCalculator.
*
* @param Order $order
* @param Mixed $value Unused
*/
public function add($order, $value = null)
{

// Go through the items and modifications in the order and apply a tax rate to them
// Calculate tax based on rates for each item and modification

$items = $order->Items();
if ($items && $items->exists()) foreach ($items as $item) {
$this->taxCalculator->applyItemTaxRate($item);
}
// Go through the items and modifications in the order and apply a tax rate to them
// Calculate tax based on rates for each item and modification

$mods = $order->Modifications();
if ($mods && $mods->exists()) foreach ($mods as $mod) {
$this->taxCalculator->applyModificationTaxRate($mod);
}
$items = $order->Items();
if ($items && $items->exists()) {
foreach ($items as $item) {
$this->taxCalculator->applyItemTaxRate($item);
}
}

// Create new modification using rates for each item and modification in the order
$taxAmount = $this->taxCalculator->calculate($order);
$mods = $order->Modifications();
if ($mods && $mods->exists()) {
foreach ($mods as $mod) {
$this->taxCalculator->applyModificationTaxRate($mod);
}
}

$mod = new XeroTaxModification();
$mod->Price = $taxAmount->getAmount();
$mod->Description = _t('Xero.TAX', 'Tax');
$mod->OrderID = $order->ID;
$mod->Value = null;
$mod->write();
}
// Create new modification using rates for each item and modification in the order
$taxAmount = $this->taxCalculator->calculate($order);

/**
* Get the form fields for the OrderForm.
*
* @return FieldList List of fields
*/
public function getFormFields() {
$mod = new XeroTaxModification();
$mod->Price = $taxAmount->getAmount();
$mod->Description = _t('Xero.TAX', 'Tax');
$mod->OrderID = $order->ID;
$mod->Value = null;
$mod->write();
}

$fields = new FieldList();
/**
* Get the form fields for the OrderForm.
*
* @return FieldList List of fields
*/
public function getFormFields()
{
$fields = new FieldList();

$field = new XeroTaxModifierField(
$this,
_t('Xero.TAX', 'Tax')
);
$field = new XeroTaxModifierField(
$this,
_t('Xero.TAX', 'Tax')
);

$shopConfig = ShopConfig::current_shop_config();
$amount = new Price();
$amount->setAmount($this->Price);
$amount->setCurrency($shopConfig->BaseCurrency);
$amount->setSymbol($shopConfig->BaseCurrencySymbol);
$shopConfig = ShopConfig::current_shop_config();
$amount = new Price();
$amount->setAmount($this->Price);
$amount->setCurrency($shopConfig->BaseCurrency);
$amount->setSymbol($shopConfig->BaseCurrencySymbol);

$field->setAmount($amount);
$fields->push($field);
$field->setAmount($amount);
$fields->push($field);

if (!$fields->exists()) Requirements::javascript('swipestripe-flatfeetax/javascript/FlatFeeTaxModifierField.js');
return $fields;
}

}
if (!$fields->exists()) {
Requirements::javascript('swipestripe-flatfeetax/javascript/FlatFeeTaxModifierField.js');
}
return $fields;
}
}
Loading

0 comments on commit b5e35a0

Please sign in to comment.