Skip to content

Commit

Permalink
Vat rate for payment fee is the highest vat rate in the order
Browse files Browse the repository at this point in the history
  • Loading branch information
andy committed Jan 8, 2019
1 parent 7ffa213 commit 27f3224
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
6 changes: 4 additions & 2 deletions paynl_paymentmethods/controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function initContent()

$apiStart->setEnduser($arrEnduser);

// producten toevoegen

/**
* @var $cart CartCore
*/
Expand Down Expand Up @@ -198,7 +198,9 @@ public function initContent()
}

if ($extraFee != 0) {
$apiStart->addProduct('PAYMENTFEE', 'Betaalkosten', round($extraFee * 100), 1, 'H');
$vatRate = $this->module->getHighestVatRate($cart);
$vatClass = Pay_Helper::nearestTaxClass($vatRate);
$apiStart->addProduct('PAYMENTFEE', 'Betaalkosten', round($extraFee * 100), 1, $vatClass);
}


Expand Down
21 changes: 13 additions & 8 deletions paynl_paymentmethods/includes/classes/Pay/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,24 @@ public static function splitAddress($strAddress) {
*/
public static function calculateTaxClass($amountInclTax, $taxAmount)
{
$taxClasses = array(
0 => 'N',
9 => 'L',
21 => 'H'
);
// return 0 if amount or tax is 0
if ($taxAmount == 0 || $amountInclTax == 0) {
return $taxClasses[0];
return self::nearestTaxClass(0);
}
$amountExclTax = $amountInclTax - $taxAmount;
$taxRate = ($taxAmount / $amountExclTax) * 100;
$nearestTaxRate = self::nearest($taxRate, array_keys($taxClasses));
return ($taxClasses[$nearestTaxRate]);
return self::nearestTaxClass($taxRate);
}

public static function nearestTaxClass($taxRate){
$taxClasses = array(
0 => 'N',
9 => 'L',
21 => 'H'
);

$nearest = self::nearest($taxRate, array_keys($taxClasses));
return $taxClasses[$nearest];
}
/**
* Get the nearest number
Expand Down
31 changes: 29 additions & 2 deletions paynl_paymentmethods/paynl_paymentmethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct()
{
$this->name = 'paynl_paymentmethods';
$this->tab = 'payments_gateways';
$this->version = '3.5.2';
$this->version = '3.5.3';
$this->_postErrors = array();
$this->module_key = '6c2f48f238008e8f68271f5e4763d308';

Expand All @@ -34,6 +34,27 @@ public function __construct()
}
}

public function getHighestVatRate(Cart $cart){
$highestVatRate = 0;
$products = $cart->getProducts();
foreach ($products as $product) {
$vatAmount = $product['price_wt'] - $product['price'];
$vatRate = ($vatAmount/$product['price'])*100;
if($vatRate > $highestVatRate) $highestVatRate = $vatRate;
}

//verzendkosten toevoegen
$shippingCost = $cart->getTotalShippingCost(null,true);
$shippingCost_no_tax = $cart->getTotalShippingCost(null,false);
if($shippingCost != 0){
$shippingVat = $shippingCost - $shippingCost_no_tax;
$vatRate = ($shippingVat/$shippingCost_no_tax)*100;
if($vatRate > $highestVatRate) $highestVatRate = $vatRate;
}

return $highestVatRate;
}

public function validateOrderPay(
$id_cart,
$id_order_state,
Expand All @@ -50,6 +71,8 @@ public function validateOrderPay(
$statusPending = Configuration::get('PAYNL_WAIT');
$statusPaid = Configuration::get('PAYNL_SUCCESS');

$cart = new Cart($id_cart);

// Als er nog geen order van dit cartid is, de order valideren.
$orderId = Order::getOrderByCartId($id_cart);
if ($orderId == false) {
Expand Down Expand Up @@ -77,13 +100,17 @@ public function validateOrderPay(
$shippingCost = $order->total_shipping;

$newShippingCosts = $shippingCost + $extraCosts;
$extraCostsExcl = round($extraCosts / (1 + (21 / 100)), 2);
$highestVatRate = $this->getHighestVatRate($cart);

$extraCostsExcl = round($extraCosts / (1 + ($highestVatRate / 100)), 2);

if ($extraCosts != 0) {
//als de order extra kosten heeft, moeten deze worden toegevoegd.
$order->total_shipping = $newShippingCosts;
$order->total_shipping_tax_excl = $order->total_shipping_tax_excl + $extraCostsExcl;
$order->total_shipping_tax_incl = $newShippingCosts;
$order->carrier_tax_rate = $highestVatRate;


$order->total_paid_tax_excl = $order->total_paid_tax_excl + $extraCostsExcl;

Expand Down

0 comments on commit 27f3224

Please sign in to comment.