From cd1d57d0543d490ed8b2f6dac27d37d9cba85fba Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Wed, 11 Sep 2024 16:45:10 +0200 Subject: [PATCH] fix already paid and fee, remove grand-total.js --- Model/Total/Quote/BuckarooAlreadyPay.php | 44 ++++++- Model/Total/Quote/BuckarooFee.php | 11 +- Model/Total/Quote/BuckarooFeeHyva.php | 62 ++++++++++ view/frontend/layout/checkout_index_index.xml | 9 -- .../js/view/checkout/summary/grand-total.js | 116 ------------------ view/frontend/web/js/view/summary/totals.js | 48 -------- 6 files changed, 110 insertions(+), 180 deletions(-) delete mode 100644 view/frontend/web/js/view/checkout/summary/grand-total.js diff --git a/Model/Total/Quote/BuckarooAlreadyPay.php b/Model/Total/Quote/BuckarooAlreadyPay.php index 2dea6e756..d9f0024e5 100644 --- a/Model/Total/Quote/BuckarooAlreadyPay.php +++ b/Model/Total/Quote/BuckarooAlreadyPay.php @@ -47,6 +47,46 @@ public function __construct( $this->giftcardCollection = $giftcardCollection; } + /** + * Collect grand total address amount + * + * @param \Magento\Quote\Model\Quote $quote + * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment + * @param \Magento\Quote\Model\Quote\Address\Total $total + * @return $this + * + * @throws \LogicException + */ + public function collect( + \Magento\Quote\Model\Quote $quote, + \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, + \Magento\Quote\Model\Quote\Address\Total $total + ) { + parent::collect($quote, $shippingAssignment, $total); + + // Ensure that shipping assignment has items, otherwise skip processing. + if (!$shippingAssignment->getItems()) { + return $this; + } + + $orderId = $quote->getReservedOrderId(); + $alreadyPaidAmount = $this->groupTransaction->getAlreadyPaid($orderId); + + // Ensure totals are properly initialized. + $total->setTotalAmount($this->getCode(), 0); + $total->setBaseTotalAmount($this->getCode(), 0); + + // Adjust the grand total only if the already paid amount is greater than zero. + if ($alreadyPaidAmount > 0) { + $total->addTotalAmount($this->getCode(), -$alreadyPaidAmount); + $total->addBaseTotalAmount($this->getCode(), -$alreadyPaidAmount); + + } + + return $this; + } + + /** * Add buckaroo fee information to address * @@ -61,7 +101,7 @@ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Qu $customTitle = []; if ($orderId) { $items = $this->groupTransaction->getGroupTransactionItemsNotRefunded($orderId); - + foreach ($items as $giftcard) { $foundGiftcard = $this->giftcardCollection->getItemByColumnValue( 'servicecode', @@ -70,7 +110,7 @@ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Qu if ($foundGiftcard !== null || $giftcard['servicecode'] === 'buckaroovoucher') { - + if ($giftcard['servicecode'] === 'buckaroovoucher') { $label = __('Voucher'); } else { diff --git a/Model/Total/Quote/BuckarooFee.php b/Model/Total/Quote/BuckarooFee.php index cd26ff588..b35d576c5 100644 --- a/Model/Total/Quote/BuckarooFee.php +++ b/Model/Total/Quote/BuckarooFee.php @@ -112,7 +112,11 @@ public function collect( \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total ) { + parent::collect($quote, $shippingAssignment, $total); + if (!$shippingAssignment->getItems()) { + return $this; + } /** * @noinspection PhpUndefinedMethodInspection */ @@ -123,11 +127,8 @@ public function collect( $total->setBaseBuckarooFee(0); $orderId = $quote->getReservedOrderId(); - if ($this->groupTransaction->getAlreadyPaid($orderId) > 0) { - return $this; - } - if (!$shippingAssignment->getItems()) { + if ($this->groupTransaction->getAlreadyPaid($orderId) > 0) { return $this; } @@ -175,7 +176,7 @@ public function collect( /** * @noinspection PhpUndefinedMethodInspection */ - $total->setGrandTotal($total->getGrandTotal() + $paymentFee); +// $total->setGrandTotal($total->getGrandTotal() + $paymentFee); return $this; } diff --git a/Model/Total/Quote/BuckarooFeeHyva.php b/Model/Total/Quote/BuckarooFeeHyva.php index 4de318092..aabc3ab7b 100644 --- a/Model/Total/Quote/BuckarooFeeHyva.php +++ b/Model/Total/Quote/BuckarooFeeHyva.php @@ -99,6 +99,68 @@ public function __construct( $this->taxCalculation = $taxCalculation; } + /** + * Collect grand total address amount + * + * @param \Magento\Quote\Model\Quote $quote + * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment + * @param \Magento\Quote\Model\Quote\Address\Total $total + * @return $this + * + * @throws \LogicException + */ + public function collect( + \Magento\Quote\Model\Quote $quote, + \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, + \Magento\Quote\Model\Quote\Address\Total $total + ) { + parent::collect($quote, $shippingAssignment, $total); + + // Ensure that shipping assignment has items, otherwise skip processing. + if (!$shippingAssignment->getItems()) { + return $this; + } + + $orderId = $quote->getReservedOrderId(); + + // Check if already paid amount is affecting the calculation + if ($this->groupTransaction->getAlreadyPaid($orderId) > 0) { + // Optionally, you could log or debug here to ensure no early returns affect the fee calculation. + $this->logging->addDebug('Already paid detected, but continuing to add fee.'); + } + + // Ensure payment method is set correctly + $paymentMethod = $quote->getPayment()->getMethod(); + if (!$paymentMethod || strpos($paymentMethod, 'buckaroo_magento2_') !== 0) { + return $this; + } + + $methodInstance = $quote->getPayment()->getMethodInstance(); + if (!$methodInstance instanceof \Buckaroo\Magento2\Model\Method\AbstractMethod) { + return $this; + } + + // Calculate the base payment fee using the getBaseFee method + $basePaymentFee = $this->getBaseFee($methodInstance, $quote); + if ($basePaymentFee < 0.01) { + return $this; + } + + // Convert the fee to the store's currency + $paymentFee = $this->priceCurrency->convert($basePaymentFee, $quote->getStore()); + + // Add fee amounts using addTotalAmount to ensure proper accumulation with other totals + $total->addTotalAmount('buckaroo_fee_hyva', $paymentFee); + $total->addBaseTotalAmount('buckaroo_fee_hyva', $basePaymentFee); + + // Set the fee on the total object for further calculations + $total->setBuckarooFee($paymentFee); + $total->setBaseBuckarooFee($basePaymentFee); + $total->setGrandTotal($total->getGrandTotal() + $paymentFee); + + return $this; + } + /** * Add buckaroo fee information to address * diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index de5ecd060..7f1b64222 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -51,15 +51,6 @@ - - Buckaroo_Magento2/js/view/checkout/summary/grand-total - - Order Total Excl. Tax - Order Total Incl. Tax - You will be charged for - Order Total - - diff --git a/view/frontend/web/js/view/checkout/summary/grand-total.js b/view/frontend/web/js/view/checkout/summary/grand-total.js deleted file mode 100644 index 078ed2898..000000000 --- a/view/frontend/web/js/view/checkout/summary/grand-total.js +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -/** - * @api - */ - -define([ - 'Magento_Checkout/js/view/summary/abstract-total', - 'Magento_Checkout/js/model/quote', - 'Magento_Catalog/js/price-utils', - 'Magento_Checkout/js/model/totals' -], function (Component, quote, priceUtils, totals) { - 'use strict'; - - return Component.extend({ - defaults: { - isFullTaxSummaryDisplayed: window.checkoutConfig.isFullTaxSummaryDisplayed || false, - template: 'Buckaroo_Magento2/checkout/summary/grand-total' - }, - totals: quote.getTotals(), - isTaxDisplayedInGrandTotal: window.checkoutConfig.includeTaxInGrandTotal || false, - - /** - * @return {*} - */ - isDisplayed: function () { - return this.isFullMode(); - }, - - /** - * @return {*|String} - */ - getValue: function () { - var price = 0; - - if (this.totals()) { - price = totals.getSegment('grand_total').value; - - if(!isNaN(parseFloat(this.getAlreadyPayTotal()))){ - price = parseFloat(price) - parseFloat(this.getAlreadyPayTotal()); - } - } - - return this.getFormattedPrice(price); - }, - - /** - * @return {*|String} - */ - getBaseValue: function () { - var price = 0; - - if (this.totals()) { - price = this.totals()['base_grand_total']; - } - - return priceUtils.formatPrice(price, quote.getBasePriceFormat()); - }, - - /** - * @return {*} - */ - getGrandTotalExclTax: function () { - var total = this.totals(), - amount; - - if (!total) { - return 0; - } - - amount = total['grand_total'] - total['tax_amount']; - - if (amount < 0) { - amount = 0; - } - - return this.getFormattedPrice(amount); - }, - - /** - * @return {Boolean} - */ - isBaseGrandTotalDisplayNeeded: function () { - var total = this.totals(); - - if (!total) { - return false; - } - - return total['base_currency_code'] != total['quote_currency_code']; //eslint-disable-line eqeqeq - }, - - getAlreadyPayTotal : function () { - var buckarooFeeSegment = totals.getSegment('buckaroo_already_paid'); - try { - if (buckarooFeeSegment.title) { - var items = JSON.parse(buckarooFeeSegment.title); - var total = 0; - if ((typeof items === 'object') && (items.length > 0)) { - for (var i = 0; i < items.length; i++) { - total = parseFloat(total) + parseFloat(items[i].serviceamount); - } - return parseFloat(total).toFixed(2); - } - } - } catch (e) { - // console.log(e); - } - - return parseFloat(buckarooFeeSegment.value).toFixed(2); - } - }); -}); diff --git a/view/frontend/web/js/view/summary/totals.js b/view/frontend/web/js/view/summary/totals.js index ff06a4a2e..189240767 100644 --- a/view/frontend/web/js/view/summary/totals.js +++ b/view/frontend/web/js/view/summary/totals.js @@ -165,54 +165,6 @@ define( this.getFormattedPrice(buckarooFeeSegment.value) : false; }, - - removeGiftcard: function (transaction_id, servicecode, amount) { - self = this; - if (confirm('Are you sure you want to remove?')) { - - $.ajax({ - url: url.build("buckaroo/checkout/giftcard"), - type: 'POST', - dataType: 'json', - showLoader: true, //use for display loader - data: { - refund: transaction_id, - card: servicecode, - amount: amount, - } - }).done(function (data) { - if(data.error){ - alert({ - title: $t('Error'), - content: $t(data.error), - actions: {always: function(){} } - }); - }else{ - alert({ - title: $t('Success'), - content: $t(data.message), - actions: {always: function(){} }, - buttons: [{ - text: $t(data.message), - class: 'action primary accept', - click: function () { - this.closeModal(true); - } - }] - }); - } - - var deferred = $.Deferred(); - getTotalsAction([], deferred); - // $('.buckaroo_magento2_'+self.currentGiftcard+' input[name="payment[method]"]').click(); - - }); - - } else { - console.log('no'); - } - } - } ); }