Skip to content
8 changes: 4 additions & 4 deletions Model/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function initCheckout($reloadIfCurrencyChanged = true)
$billingAddress->save();
$shippingAddress->save();

$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
$this->totalsCollector->collectQuoteTotals($quote);
// $this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
// $this->totalsCollector->collectQuoteTotals($quote);

$quote->collectTotals();
$this->quoteRepository->save($quote);
Expand Down Expand Up @@ -746,8 +746,8 @@ public function placeOrder(GetPaymentResponse $dibsPayment, Quote $quote, $weHan
$quote->collectTotals();
}

//- do not recollect totals
$quote->setTotalsCollectedFlag(true);
//- recollect totals - original price
$quote->collectTotals();

//!
// Now we create the order from the quote
Expand Down
73 changes: 63 additions & 10 deletions Model/Dibs/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,24 @@ class Items
protected $_itemsArray = [];

protected $addCustomOptionsToItemName = null;
/**
* @var \Magento\SalesRule\Api\RuleRepositoryInterface
*/
private $ruleRepository;

public function __construct(
\Dibs\EasyCheckout\Helper\Data $helper,
\Magento\Catalog\Helper\Product\Configuration $productConfig,
\Magento\Tax\Model\Calculation $calculationTool
\Magento\Tax\Model\Calculation $calculationTool,
\Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository
) {
$this->_helper = $helper;
$this->_productConfig = $productConfig;
$this->calculationTool = $calculationTool;

// resets all values
$this->init();
$this->ruleRepository = $ruleRepository;
}

public function init($store = null)
Expand Down Expand Up @@ -252,11 +258,11 @@ public function addItems($items)
->setName($itemName)
->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
->setTaxRate((int) $vat)
->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
->setGrossTotalAmount((int) ($this->addZeroes($item->getRowTotalInclTax()))); // incl. tax

// add to array
$this->_cart[$sku] = $orderItem;
Expand Down Expand Up @@ -329,7 +335,7 @@ public function addShipping($address)
->setName((string)__('Shipping Fee (%1)', $address->getShippingDescription()))
->setUnit("st") // TODO! We need to map these somehow!
->setQuantity(1)
->setTaxRate($this->addZeroes($vat)) // the tax rate i.e 25% (2500)
->setTaxRate((int) $vat)
->setTaxAmount($this->addZeroes($taxAmount)) // total tax amount
->setUnitPrice($this->addZeroes($exclTax)) // excl. tax price per item
->setNetTotalAmount($this->addZeroes($exclTax)) // excl. tax
Expand Down Expand Up @@ -428,13 +434,13 @@ public function generateInvoiceFeeItem($invoiceLabel, $invoiceFee, $vatIncluded)
$feeItem
->setName($invoiceLabel)
->setReference(strtolower(str_replace(" ", "_", $invoiceLabel)))
->setTaxRate($this->addZeroes($taxRate))
->setTaxRate((int) $taxRate)
->setGrossTotalAmount($this->addZeroes($invoiceFeeInclTax)) // incl tax
->setNetTotalAmount($this->addZeroes($invoiceFeeExclTax)) // // excl. tax
->setUnit("st")
->setQuantity(1)
->setUnitPrice($this->addZeroes($invoiceFeeExclTax)) // // excl. tax
->setTaxAmount($this->addZeroes($taxAmount)); // tax amount
->setTaxAmount((int) $taxAmount); // tax amount

return $feeItem;
}
Expand Down Expand Up @@ -466,8 +472,8 @@ public function addDiscounts($couponCode)
->setName($couponCode ? (string)__('Discount (%1)', $couponCode) : (string)__('Discount'))
->setUnit("st")
->setQuantity(1)
->setTaxRate($this->addZeroes($vat)) // the tax rate i.e 25% (2500)
->setTaxAmount($taxAmount) // total tax amount
->setTaxRate((int) $vat)
->setTaxAmount((int) $taxAmount) // total tax amount
->setUnitPrice(0) // excl. tax price per item
->setNetTotalAmount(-$amountExclTax) // excl. tax
->setGrossTotalAmount(-$amountInclTax); // incl. tax
Expand All @@ -478,6 +484,48 @@ public function addDiscounts($couponCode)
return $this;
}

/**
* Check if discount was applied for whole cart
*
* @param Quote $quote
*/
private function addDiscountByCartRule(Quote $quote) : void
{
$ruleIds = $quote->getAppliedRuleIds();
if (!$ruleIds) {
return;
}

foreach (explode(',', $ruleIds) as $ruleId) {
try {
$rule = $this->ruleRepository->getById($ruleId);
} catch (\Exception $e) {
continue;
}

if ($rule->getSimpleAction() != 'cart_fixed') {
continue;
}

$discount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount();
$discount = $this->addZeroes($discount);
$orderItem = new OrderItem();
$reference = $rule->getName();
$orderItem
->setReference($reference)
->setName($quote->getCouponCode() ? (string)__('Discount (%1)', $quote->getCouponCode()) : (string)__('Discount'))
->setUnit("st")
->setQuantity(1)
->setTaxRate($this->addZeroes(0)) // the tax rate i.e 25% (2500)
->setTaxAmount(0) // total tax amount
->setUnitPrice(-$discount) // excl. tax price per item
->setNetTotalAmount(-$discount) // excl. tax
->setGrossTotalAmount(-$discount); // incl. tax

$this->_cart[$reference] = $orderItem;
}
}

public function validateTotals($grandTotal)
{
//calculate Dibs total
Expand Down Expand Up @@ -591,7 +639,12 @@ public function generateOrderItemsFromQuote(Quote $quote)
$this->addShipping($shippingAddress);
}

$this->addDiscounts($quote->getCouponCode());
// If there is no discount per items, but code does exist
// it means, that discount was applied on whole cart
// @TODO: Refactor this to external model
count($this->_discounts)
? $this->addDiscounts($quote->getCouponCode())
: $this->addDiscountByCartRule($quote);

try {
$this->validateTotals($quote->getGrandTotal());
Expand Down