Skip to content

Commit

Permalink
Fixed rounding on grand total as cart items, update module version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriy Kravets committed Sep 9, 2020
1 parent 5365014 commit 71d320c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 17 deletions.
112 changes: 97 additions & 15 deletions Model/Dibs/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Items

protected $addCustomOptionsToItemName = null;

/**
* Items constructor.
*
* @param \Dibs\EasyCheckout\Helper\Data $helper
* @param \Magento\Catalog\Helper\Product\Configuration $productConfig
* @param \Magento\Tax\Model\Calculation $calculationTool
*/
public function __construct(
\Dibs\EasyCheckout\Helper\Data $helper,
\Magento\Catalog\Helper\Product\Configuration $productConfig,
Expand All @@ -52,6 +59,11 @@ public function __construct(
$this->init();
}

/**
* @param null $store
*
* @return $this
*/
public function init($store = null)
{
$this->_store = $store;
Expand All @@ -71,7 +83,7 @@ public function init($store = null)
public function addItems($items)
{
if ($this->addCustomOptionsToItemName === null) {
$shouldAdd = $this->_helper->addCustomOptionsToItemName();;
$shouldAdd = $this->_helper->addCustomOptionsToItemName();
$this->addCustomOptionsToItemName = $shouldAdd ? true : false;
}

Expand Down Expand Up @@ -175,8 +187,6 @@ public function addItems($items)
$addPrices = $isChildrenCalculated;
}
} else {


if ($addComments) {
$comment = [];
//add configurable/children information, as comment
Expand Down Expand Up @@ -234,10 +244,9 @@ public function addItems($items)
$unitPrice = $addPrices ? $this->addZeroes($item->getPriceInclTax()) : 0;
$unitPriceExclTax = $addPrices ? $this->addZeroes($item->getPrice()) : 0;


$itemName = $item->getName();
if ($addComments && $comment) {
$itemName .= ' ' . '('.$comment.')';
$itemName .= ' ' . '(' . $comment . ')';
}

// max length!
Expand All @@ -253,7 +262,7 @@ public function addItems($items)
->setUnit("st")
->setQuantity(round($qty, 0))
->setTaxRate($this->addZeroes($vat)) // the tax rate i.e 25% (2500)
->setTaxAmount($this->addZeroes($this->getTotalTaxAmount($unitPrice * $qty, $vat, false))) // total tax amount
->setTaxAmount((int) $this->getTotalTaxAmount($unitPrice * $qty, $vat, false)) // total tax amount
->setUnitPrice((int) $unitPriceExclTax) // excl. tax price per item
->setNetTotalAmount((int) ($unitPriceExclTax * $qty)) // excl. tax
->setGrossTotalAmount((int) ($unitPrice * $qty)); // incl. tax
Expand Down Expand Up @@ -299,6 +308,11 @@ public function addItems($items)
return $this;
}

/**
* @param $address
*
* @return $this
*/
public function addShipping($address)
{
if ($this->_toInvoice && $address->getBaseShippingAmount() <= $address->getBaseShippingInvoiced()) {
Expand Down Expand Up @@ -440,6 +454,12 @@ public function generateInvoiceFeeItem($invoiceLabel, $invoiceFee, $vatIncluded)
}

// TODO!!

/**
* @param $couponCode
*
* @return $this
*/
public function addDiscounts($couponCode)
{
foreach ($this->_discounts as $vat=> $amountInclTax) {
Expand All @@ -452,10 +472,6 @@ public function addDiscounts($couponCode)
$reference = 'discount-toinvoice';
}

// var_dump($vat);
//var_dump($amount);
//die;

$taxAmount = $this->getTotalTaxAmount($amountInclTax, $vat);
$amountInclTax = $this->addZeroes($amountInclTax);
$amountExclTax = $amountInclTax - $taxAmount;
Expand All @@ -478,6 +494,12 @@ public function addDiscounts($couponCode)
return $this;
}

/**
* @param $grandTotal
*
* @return $this
* @throws CheckoutException
*/
public function validateTotals($grandTotal)
{
//calculate Dibs total
Expand Down Expand Up @@ -577,11 +599,7 @@ public function generateOrderItemsFromQuote(Quote $quote)
$this->init($quote->getStore());

$billingAddress = $quote->getBillingAddress();
if ($quote->isVirtual()) {
$shippingAddress = $billingAddress;
} else {
$shippingAddress = $quote->getShippingAddress();
}
$shippingAddress = $quote->isVirtual() ? $billingAddress : $quote->getShippingAddress();

/*Get all cart items*/
$cartItems = $quote->getAllVisibleItems(); //getItemParentId is null and !isDeleted
Expand All @@ -592,6 +610,7 @@ public function generateOrderItemsFromQuote(Quote $quote)
}

$this->addDiscounts($quote->getCouponCode());
$this->addCustomTotals($quote->getTotals());

try {
$this->validateTotals($quote->getGrandTotal());
Expand All @@ -604,6 +623,13 @@ public function generateOrderItemsFromQuote(Quote $quote)
}

//generate Dibs items from Magento Order

/**
* @param Order $order
*
* @return array
* @throws CheckoutException
*/
public function fromOrder(Order $order)
{
$this->init($order->getStore());
Expand Down Expand Up @@ -677,16 +703,29 @@ public function addDibsItemsByCreditMemo(Order\Creditmemo $creditMemo)
$this->addDiscounts($order->getCouponCode()); //coupon code is not copied to invoice
}

/**
* @return int
*/
public function getMaxVat()
{
return $this->_maxvat;
}

/**
* @return array
*/
public function getCart()
{
return $this->_cart;
}

/**
* @param $price
* @param $vat
* @param bool $addZeroes
*
* @return float|int
*/
public function getTotalTaxAmount($price, $vat, $addZeroes = true)
{
if ($addZeroes) {
Expand All @@ -696,8 +735,51 @@ public function getTotalTaxAmount($price, $vat, $addZeroes = true)
}
}

/**
* @param $amount
*
* @return int
*/
public function addZeroes($amount)
{
return (int) round($amount * 100, 0);
}

/**
* @param array $totals
*/
private function addCustomTotals(array $totals)
{
$customTotals = array_diff(array_keys($totals), [
'subtotal',
'grand_total',
'tax',
'shipping'
]);

foreach ($customTotals as $customTotal) {
/** @var \Magento\Quote\Model\Quote\Address\Total $total */
$total = $totals[$customTotal];
$amountInclTax = $total->getValue();
$vat = 25;

$taxAmount = $this->getTotalTaxAmount($amountInclTax, $vat);
$amountInclTax = $this->addZeroes($amountInclTax);
$amountExclTax = $amountInclTax - $taxAmount;

$orderItem = new OrderItem();
$orderItem
->setReference($total->getCode())
->setName($total->getTitle() ?: $total->getCode())
->setUnit("st")
->setQuantity(1)
->setTaxRate((int) $this->addZeroes($vat)) // the tax rate i.e 25% (2500)
->setTaxAmount((int) $taxAmount) // total tax amount
->setUnitPrice(0) // excl. tax price per item
->setNetTotalAmount((int) $amountExclTax) // excl. tax
->setGrossTotalAmount((int) $amountInclTax); // incl. tax

$this->_cart[$total->getCode()] = $orderItem;
}
}
}
2 changes: 1 addition & 1 deletion Model/Dibs/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public function loadDibsPaymentById($paymentId)
*/
protected function fixPrice($price)
{
return $price * 100;
return (int) round($price * 100, 0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Dibs_EasyCheckout" setup_version="1.0.3">
<module name="Dibs_EasyCheckout" setup_version="1.1.3">
<sequence>
<module name="Magento_Checkout"/>
<module name="Magento_Sales"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ define([
quote.setTotals(response.totals);
paymentService.setPaymentMethods(methodConverter(response['payment_methods']));

// Update DIBS iframe in overlay mode if exists
if (null !== document.getElementById('nets-popup-iframe')) {
document.getElementById('nets-popup-iframe').src += '';
}

var dibsCheckout = uiRegistry.get('nwtdibsCheckout');
if (window.dibs_msuodc_enabled && dibsCheckout) {
dibsCheckout._ajaxSubmit(
Expand Down

0 comments on commit 71d320c

Please sign in to comment.