Skip to content

Commit

Permalink
MC-35554: Orders with Zero Payment Information required are Closed af…
Browse files Browse the repository at this point in the history
…ter being invoiced
  • Loading branch information
rostyslav-hymon committed Jul 21, 2020
1 parent 4b428b6 commit c710c9b
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions app/code/Magento/Sales/Model/Order/Invoice/Total/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
*/
namespace Magento\Sales\Model\Order\Invoice\Total;

use Magento\Sales\Model\Order\Invoice;

/**
* Discount invoice
*/
class Discount extends AbstractTotal
{
/**
* @param \Magento\Sales\Model\Order\Invoice $invoice
* Collect invoice
*
* @param Invoice $invoice
* @return $this
*/
public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
public function collect(Invoice $invoice)
{
$invoice->setDiscountAmount(0);
$invoice->setBaseDiscountAmount(0);
Expand All @@ -24,14 +31,7 @@ public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
* So basically if we have invoice with positive discount and it
* was not canceled we don't add shipping discount to this one.
*/
$addShippingDiscount = true;
foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
if ($previousInvoice->getDiscountAmount()) {
$addShippingDiscount = false;
}
}

if ($addShippingDiscount) {
if ($this->isShippingDiscount($invoice)) {
$totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount();
$baseTotalDiscountAmount = $baseTotalDiscountAmount +
$invoice->getOrder()->getBaseShippingDiscountAmount();
Expand Down Expand Up @@ -71,8 +71,29 @@ public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
$invoice->setDiscountAmount(-$totalDiscountAmount);
$invoice->setBaseDiscountAmount(-$baseTotalDiscountAmount);

$invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
$invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
$grandTotal = $invoice->getGrandTotal() - $totalDiscountAmount < 0.0001
? 0 : $invoice->getGrandTotal() - $totalDiscountAmount;
$baseGrandTotal = $invoice->getBaseGrandTotal() - $baseTotalDiscountAmount < 0.0001
? 0 : $invoice->getBaseGrandTotal() - $baseTotalDiscountAmount;
$invoice->setGrandTotal($grandTotal);
$invoice->setBaseGrandTotal($baseGrandTotal);
return $this;
}

/**
* Checking if shipping discount was added in previous invoices.
*
* @param Invoice $invoice
* @return bool
*/
private function isShippingDiscount(Invoice $invoice): bool
{
$addShippingDiscount = true;
foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
if ($previousInvoice->getDiscountAmount()) {
$addShippingDiscount = false;
}
}
return $addShippingDiscount;
}
}

0 comments on commit c710c9b

Please sign in to comment.