Skip to content

Commit af4acbf

Browse files
committed
Merge branch 'MAGETWO-71371' into MPI-PR
2 parents 60d9ca9 + d955773 commit af4acbf

File tree

8 files changed

+544
-326
lines changed

8 files changed

+544
-326
lines changed

app/code/Magento/Sales/Model/Order/Invoice.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,18 @@ public function pay()
362362

363363
$this->setState(self::STATE_PAID);
364364

365-
$this->getOrder()->getPayment()->pay($this);
366-
$this->getOrder()->setTotalPaid($this->getOrder()->getTotalPaid() + $this->getGrandTotal());
367-
$this->getOrder()->setBaseTotalPaid($this->getOrder()->getBaseTotalPaid() + $this->getBaseGrandTotal());
365+
$order = $this->getOrder();
366+
$order->getPayment()->pay($this);
367+
$totalPaid = $this->getGrandTotal();
368+
$baseTotalPaid = $this->getBaseGrandTotal();
369+
$invoiceList = $order->getInvoiceCollection();
370+
// calculate all totals
371+
if (count($invoiceList->getItems()) > 1) {
372+
$totalPaid += $order->getTotalPaid();
373+
$baseTotalPaid += $order->getBaseTotalPaid();
374+
}
375+
$order->setTotalPaid($totalPaid);
376+
$order->setBaseTotalPaid($baseTotalPaid);
368377
$this->_eventManager->dispatch('sales_order_invoice_pay', [$this->_eventObject => $this]);
369378
return $this;
370379
}

app/code/Magento/Sales/Model/Order/Payment.php

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ protected function _construct()
198198
public function setOrder(Order $order)
199199
{
200200
$this->_order = $order;
201+
201202
return $this;
202203
}
203204

@@ -225,6 +226,7 @@ public function getOrder()
225226
public function setTransactionId($transactionId)
226227
{
227228
$this->setData('transaction_id', $transactionId);
229+
228230
return $this;
229231
}
230232

@@ -247,6 +249,7 @@ public function getTransactionId()
247249
public function setIsTransactionClosed($isClosed)
248250
{
249251
$this->setData('is_transaction_closed', (bool)$isClosed);
252+
250253
return $this;
251254
}
252255

@@ -293,6 +296,7 @@ public function canCapture()
293296
return false;
294297
}
295298
}
299+
296300
return true;
297301
}
298302

@@ -499,15 +503,16 @@ public function registerAuthorizationNotification($amount)
499503
*/
500504
public function pay($invoice)
501505
{
502-
$this->_updateTotals(
503-
[
504-
'amount_paid' => $invoice->getGrandTotal(),
505-
'base_amount_paid' => $invoice->getBaseGrandTotal(),
506-
'shipping_captured' => $invoice->getShippingAmount(),
507-
'base_shipping_captured' => $invoice->getBaseShippingAmount(),
508-
]
506+
$totals = $this->collectTotalAmounts(
507+
$this->getOrder(),
508+
['grand_total', 'base_grand_total', 'shipping_amount', 'base_shipping_amount']
509509
);
510+
$this->setAmountPaid($totals['grand_total']);
511+
$this->setBaseAmountPaid($totals['base_grand_total']);
512+
$this->setShippingCaptured($totals['shipping_amount']);
513+
$this->setBaseShippingCaptured($totals['base_shipping_amount']);
510514
$this->_eventManager->dispatch('sales_order_payment_pay', ['payment' => $this, 'invoice' => $invoice]);
515+
511516
return $this;
512517
}
513518

@@ -550,6 +555,7 @@ protected function _invoice()
550555
}
551556

552557
$this->getOrder()->addRelatedObject($invoice);
558+
553559
return $invoice;
554560
}
555561

@@ -567,6 +573,7 @@ public function canVoid()
567573
$this->_canVoidLookup = (bool)$authTransaction && !(int)$authTransaction->getIsClosed();
568574
}
569575
}
576+
570577
return $this->_canVoidLookup;
571578
}
572579

@@ -581,6 +588,7 @@ public function void(\Magento\Framework\DataObject $document)
581588
{
582589
$this->_void(true);
583590
$this->_eventManager->dispatch('sales_order_payment_void', ['payment' => $this, 'invoice' => $document]);
591+
584592
return $this;
585593
}
586594

@@ -596,6 +604,7 @@ public function registerVoidNotification($amount = null)
596604
if (!$this->hasMessage()) {
597605
$this->setMessage(__('Registered a Void notification.'));
598606
}
607+
599608
return $this->_void(false, $amount);
600609
}
601610

@@ -608,6 +617,7 @@ public function registerVoidNotification($amount = null)
608617
public function setCreditmemo(Creditmemo $creditmemo)
609618
{
610619
$this->setData('creditmemo', $creditmemo);
620+
611621
return $this;
612622
}
613623

@@ -724,6 +734,7 @@ public function refund($creditmemo)
724734
'sales_order_payment_refund',
725735
['payment' => $this, 'creditmemo' => $creditmemo]
726736
);
737+
727738
return $this;
728739
}
729740

@@ -778,6 +789,7 @@ public function registerRefundNotification($amount)
778789
),
779790
false
780791
);
792+
781793
return $this;
782794
}
783795

@@ -807,6 +819,7 @@ public function registerRefundNotification($amount)
807819
);
808820
$message = $this->_appendTransactionToMessage($transaction, $message);
809821
$this->setOrderStateProcessing($message);
822+
810823
return $this;
811824
}
812825

@@ -830,6 +843,7 @@ public function cancelCreditmemo($creditmemo)
830843
'sales_order_payment_cancel_creditmemo',
831844
['payment' => $this, 'creditmemo' => $creditmemo]
832845
);
846+
833847
return $this;
834848
}
835849

@@ -904,6 +918,7 @@ public function accept()
904918
);
905919
$this->setOrderStatePaymentReview($message, $transactionId);
906920
}
921+
907922
return $this;
908923
}
909924

@@ -944,6 +959,7 @@ public function deny($isOnline = true)
944959
);
945960
$this->setOrderStatePaymentReview($message, $transactionId);
946961
}
962+
947963
return $this;
948964
}
949965

@@ -998,7 +1014,8 @@ protected function updateBaseAmountPaidOnlineTotal($invoice)
9981014
{
9991015
if ($invoice instanceof Invoice) {
10001016
$invoice->pay();
1001-
$this->_updateTotals(['base_amount_paid_online' => $invoice->getBaseGrandTotal()]);
1017+
$totals = $this->collectTotalAmounts($this->getOrder(), ['base_grand_total']);
1018+
$this->setBaseAmountPaidOnline($totals['base_grand_total']);
10021019
$this->getOrder()->addRelatedObject($invoice);
10031020
}
10041021
}
@@ -1147,6 +1164,7 @@ protected function _void($isOnline, $amount = null, $gatewayCallback = 'void')
11471164
$message = $this->_appendTransactionToMessage($transaction, $message);
11481165
$this->setOrderStateProcessing($message);
11491166
$order->setDataChanges(true);
1167+
11501168
return $this;
11511169
}
11521170

@@ -1168,6 +1186,7 @@ public function addTransaction($type, $salesDocument = null, $failSafe = false)
11681186
if ($salesDocument) {
11691187
$builder->setSalesDocument($salesDocument);
11701188
}
1189+
11711190
return $builder->build($type);
11721191
}
11731192

@@ -1202,6 +1221,7 @@ public function importTransactionInfo(Transaction $transactionTo)
12021221
$data
12031222
);
12041223
}
1224+
12051225
return $this;
12061226
}
12071227

@@ -1235,6 +1255,7 @@ protected function _appendTransactionToMessage($transaction, $message)
12351255
$txnId = is_object($transaction) ? $transaction->getHtmlTxnId() : $transaction;
12361256
$message .= ' ' . __('Transaction ID: "%1"', $txnId);
12371257
}
1258+
12381259
return $message;
12391260
}
12401261

@@ -1260,9 +1281,11 @@ public function prependMessage($messagePrependTo)
12601281
) {
12611282
$comment = $preparedMessage->getComment() . ' ' . $messagePrependTo;
12621283
$preparedMessage->setComment($comment);
1284+
12631285
return $comment;
12641286
}
12651287
}
1288+
12661289
return $messagePrependTo;
12671290
}
12681291

@@ -1276,6 +1299,7 @@ public function prependMessage($messagePrependTo)
12761299
public function formatAmount($amount, $asFloat = false)
12771300
{
12781301
$amount = $this->priceCurrency->round($amount);
1302+
12791303
return !$asFloat ? (string)$amount : $amount;
12801304
}
12811305

@@ -1311,6 +1335,7 @@ public function getAuthorizationTransaction()
13111335
public function isCaptureFinal($amountToCapture)
13121336
{
13131337
$total = $this->getOrder()->getBaseTotalDue();
1338+
13141339
return $this->formatAmount($total, true) == $this->formatAmount($amountToCapture, true);
13151340
}
13161341

@@ -1354,6 +1379,7 @@ public function getTransactionAdditionalInfo()
13541379
public function resetTransactionAdditionalInfo()
13551380
{
13561381
$this->transactionAdditionalInfo = [];
1382+
13571383
return $this;
13581384
}
13591385

@@ -1410,6 +1436,7 @@ protected function _getInvoiceForTransactionId($transactionId)
14101436
foreach ($this->getOrder()->getInvoiceCollection() as $invoice) {
14111437
if ($invoice->getTransactionId() == $transactionId) {
14121438
$invoice->load($invoice->getId());
1439+
14131440
// to make sure all data will properly load (maybe not required)
14141441
return $invoice;
14151442
}
@@ -1420,9 +1447,11 @@ protected function _getInvoiceForTransactionId($transactionId)
14201447
)
14211448
) {
14221449
$invoice->setTransactionId($transactionId);
1450+
14231451
return $invoice;
14241452
}
14251453
}
1454+
14261455
return false;
14271456
}
14281457

@@ -1436,7 +1465,7 @@ private function getOrderStateResolver()
14361465
$this->orderStateResolver = ObjectManager::getInstance()->get(OrderStateResolverInterface::class);
14371466
}
14381467

1439-
return$this->orderStateResolver;
1468+
return $this->orderStateResolver;
14401469
}
14411470

14421471
//@codeCoverageIgnoreStart
@@ -2413,6 +2442,7 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\OrderPaymentExten
24132442
public function setIsTransactionPending($flag)
24142443
{
24152444
$this->setData('is_transaction_pending', (bool)$flag);
2445+
24162446
return $this;
24172447
}
24182448

@@ -2436,6 +2466,7 @@ public function getIsTransactionPending()
24362466
public function setIsFraudDetected($flag)
24372467
{
24382468
$this->setData('is_fraud_detected', (bool)$flag);
2469+
24392470
return $this;
24402471
}
24412472

@@ -2459,6 +2490,7 @@ public function getIsFraudDetected()
24592490
public function setShouldCloseParentTransaction($flag)
24602491
{
24612492
$this->setData('should_close_parent_transaction', (bool)$flag);
2493+
24622494
return $this;
24632495
}
24642496

@@ -2493,4 +2525,26 @@ private function setTransactionIdsForRefund(Transaction $transaction)
24932525
}
24942526

24952527
//@codeCoverageIgnoreEnd
2528+
2529+
/**
2530+
* Collects order invoices totals by provided keys.
2531+
* Returns result as {key: amount}.
2532+
*
2533+
* @param Order $order
2534+
* @param array $keys
2535+
* @return array
2536+
*/
2537+
private function collectTotalAmounts(Order $order, array $keys)
2538+
{
2539+
$result = array_fill_keys($keys, 0.00);
2540+
$invoiceCollection = $order->getInvoiceCollection();
2541+
/** @var Invoice $invoice */
2542+
foreach ($invoiceCollection as $invoice) {
2543+
foreach ($keys as $key) {
2544+
$result[$key] += $invoice->getData($key);
2545+
}
2546+
}
2547+
2548+
return $result;
2549+
}
24962550
}

0 commit comments

Comments
 (0)