Skip to content

Commit 5c8daac

Browse files
author
Prabhu Ram
committed
MC-20637: MyAccount :: Order Details :: Invoice Details by Order Number
- invoice updated impl and added new tests
1 parent f7e86b6 commit 5c8daac

File tree

5 files changed

+874
-157
lines changed

5 files changed

+874
-157
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/InvoiceItems.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,34 @@ private function getInvoiceItemData(OrderInterface $order, InvoiceItemInterface
122122
],
123123
'quantity_invoiced' => $invoiceItem->getQty(),
124124
'model' => $invoiceItem,
125-
'product_type' => $orderItem['product_type']
125+
'product_type' => $orderItem['product_type'],
126+
'order_item' => $orderItem,
127+
'discounts' => $this->getDiscountDetails($order, $invoiceItem)
126128
];
127129
}
130+
131+
/**
132+
* Returns information about an applied discount
133+
*
134+
* @param OrderInterface $associatedOrder
135+
* @param InvoiceItemInterface $invoiceItem
136+
* @return array
137+
*/
138+
private function getDiscountDetails(OrderInterface $associatedOrder, InvoiceItemInterface $invoiceItem) : array
139+
{
140+
if ($associatedOrder->getDiscountDescription() === null && $invoiceItem->getDiscountAmount() == 0
141+
&& $associatedOrder->getDiscountAmount() == 0
142+
) {
143+
$discounts = [];
144+
} else {
145+
$discounts [] = [
146+
'label' => $associatedOrder->getDiscountDescription() ?? _('Discount'),
147+
'amount' => [
148+
'value' => $invoiceItem->getDiscountAmount() ?? 0,
149+
'currency' => $associatedOrder->getOrderCurrencyCode()
150+
]
151+
];
152+
}
153+
return $discounts;
154+
}
128155
}

app/code/Magento/SalesGraphQl/Model/Resolver/InvoiceTotal.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,45 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Sales\Api\Data\InvoiceInterface;
1515
use Magento\Sales\Api\Data\OrderInterface;
16+
use Magento\Tax\Api\OrderTaxManagementInterface;
17+
use Magento\SalesGraphQl\Model\SalesItem\ShippingTaxHelper;
18+
use Magento\Tax\Helper\Data as TaxHelper;
1619

1720
/**
1821
* Resolver for Invoice total
1922
*/
2023
class InvoiceTotal implements ResolverInterface
2124
{
25+
/**
26+
* @var TaxHelper
27+
*/
28+
private $taxHelper;
29+
30+
/**
31+
* @var OrderTaxManagementInterface
32+
*/
33+
private $orderTaxManagement;
34+
35+
/**
36+
* @var ShippingTaxHelper
37+
*/
38+
private $shippingTaxHelper;
39+
40+
/**
41+
* @param OrderTaxManagementInterface $orderTaxManagement
42+
* @param TaxHelper $taxHelper
43+
* @param ShippingTaxHelper $shippingTaxHelper
44+
*/
45+
public function __construct(
46+
OrderTaxManagementInterface $orderTaxManagement,
47+
TaxHelper $taxHelper,
48+
ShippingTaxHelper $shippingTaxHelper
49+
) {
50+
$this->taxHelper = $taxHelper;
51+
$this->orderTaxManagement = $orderTaxManagement;
52+
$this->shippingTaxHelper = $shippingTaxHelper;
53+
}
54+
2255
/**
2356
* @inheritDoc
2457
*/
@@ -48,6 +81,11 @@ public function resolve(
4881
'subtotal' => ['value' => $invoiceModel->getSubtotal(), 'currency' => $currency],
4982
'total_tax' => ['value' => $invoiceModel->getTaxAmount(), 'currency' => $currency],
5083
'total_shipping' => ['value' => $invoiceModel->getShippingAmount(), 'currency' => $currency],
84+
'discounts' => $this->getDiscountDetails($invoiceModel),
85+
'taxes' => $this->formatTaxes(
86+
$orderModel,
87+
$this->taxHelper->getCalculatedTaxes($invoiceModel),
88+
),
5189
'shipping_handling' => [
5290
'amount_excluding_tax' => [
5391
'value' => $invoiceModel->getShippingAmount(),
@@ -61,7 +99,80 @@ public function resolve(
6199
'value' => $invoiceModel->getShippingAmount(),
62100
'currency' => $currency
63101
],
102+
'discounts' => $this->getShippingDiscountDetails($invoiceModel),
103+
'taxes' => $this->formatTaxes(
104+
$orderModel,
105+
$this->shippingTaxHelper->calculateShippingTaxes($orderModel, $invoiceModel),
106+
)
64107
]
65108
];
66109
}
110+
111+
/**
112+
* Return information about an applied discount on shipping
113+
*
114+
* @param InvoiceInterface $invoice
115+
* @return array
116+
*/
117+
private function getShippingDiscountDetails(InvoiceInterface $invoice)
118+
{
119+
$shippingDiscounts = [];
120+
if (!($invoice->getDiscountDescription() === null
121+
&& $invoice->getShippingDiscountTaxCompensationAmount() == 0)) {
122+
$shippingDiscounts[] =
123+
[
124+
'label' => $invoice->getDiscountDescription() ?? __('Discount'),
125+
'amount' => [
126+
'value' => abs($invoice->getShippingDiscountTaxCompensationAmount()),
127+
'currency' => $invoice->getOrderCurrencyCode()
128+
]
129+
];
130+
}
131+
return $shippingDiscounts;
132+
}
133+
134+
/**
135+
* Return information about an applied discount
136+
*
137+
* @param InvoiceInterface $invoice
138+
* @return array
139+
*/
140+
private function getDiscountDetails(InvoiceInterface $invoice)
141+
{
142+
$discounts = [];
143+
if (!($invoice->getDiscountDescription() === null && $invoice->getDiscountAmount() == 0)) {
144+
$discounts[] = [
145+
'label' => $invoice->getDiscountDescription() ?? __('Discount'),
146+
'amount' => [
147+
'value' => abs($invoice->getDiscountAmount()),
148+
'currency' => $invoice->getOrderCurrencyCode()
149+
]
150+
];
151+
}
152+
return $discounts;
153+
}
154+
155+
/**
156+
* Format applied taxes
157+
*
158+
* @param OrderInterface $order
159+
* @param array $appliedTaxes
160+
* @return array
161+
*/
162+
private function formatTaxes(OrderInterface $order, array $appliedTaxes)
163+
{
164+
$taxes = [];
165+
foreach ($appliedTaxes as $appliedTax) {
166+
$appliedTaxesArray = [
167+
'rate' => $appliedTax['percent'] ?? 0,
168+
'title' => $appliedTax['title'] ?? null,
169+
'amount' => [
170+
'value' => $appliedTax['tax_amount'] ?? 0,
171+
'currency' => $order->getOrderCurrencyCode()
172+
]
173+
];
174+
$taxes[] = $appliedTaxesArray;
175+
}
176+
return $taxes;
177+
}
67178
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Magento\SalesGraphQl\Model\SalesItem;
4+
5+
use Magento\Sales\Api\Data\OrderInterface;
6+
use Magento\Sales\Model\EntityInterface;
7+
use Magento\Tax\Api\Data\OrderTaxDetailsItemInterface;
8+
use Magento\Tax\Api\OrderTaxManagementInterface;
9+
10+
class ShippingTaxHelper
11+
{
12+
/**
13+
* @var OrderTaxManagementInterface
14+
*/
15+
private $orderTaxManagement;
16+
17+
/**
18+
* @param OrderTaxManagementInterface $orderTaxManagement
19+
*/
20+
public function __construct(
21+
OrderTaxManagementInterface $orderTaxManagement
22+
) {
23+
$this->orderTaxManagement = $orderTaxManagement;
24+
}
25+
26+
/**
27+
* Calculate shipping taxes for sales item
28+
*
29+
* @param OrderInterface $order
30+
* @param EntityInterface $salesItem
31+
* @return array
32+
* @throws \Magento\Framework\Exception\NoSuchEntityException
33+
*/
34+
public function calculateShippingTaxes(
35+
OrderInterface $order,
36+
EntityInterface $salesItem
37+
) {
38+
$orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getId());
39+
$taxClassAmount = [];
40+
// Apply any taxes for shipping
41+
$shippingTaxAmount = $salesItem->getShippingTaxAmount();
42+
$originalShippingTaxAmount = $order->getShippingTaxAmount();
43+
if ($shippingTaxAmount && $originalShippingTaxAmount &&
44+
$shippingTaxAmount != 0 && (float)$originalShippingTaxAmount
45+
) {
46+
//An invoice or credit memo can have a different qty than its order
47+
$shippingRatio = $shippingTaxAmount / $originalShippingTaxAmount;
48+
$itemTaxDetails = $orderTaxDetails->getItems();
49+
foreach ($itemTaxDetails as $itemTaxDetail) {
50+
//Aggregate taxable items associated with shipping
51+
if ($itemTaxDetail->getType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
52+
$taxClassAmount = $this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $shippingRatio);
53+
}
54+
}
55+
}
56+
57+
return $taxClassAmount;
58+
}
59+
60+
/**
61+
* Accumulates the pre-calculated taxes for each tax class
62+
*
63+
* This method accepts and returns the 'taxClassAmount' array with format:
64+
* array(
65+
* $index => array(
66+
* 'tax_amount' => $taxAmount,
67+
* 'base_tax_amount' => $baseTaxAmount,
68+
* 'title' => $title,
69+
* 'percent' => $percent
70+
* )
71+
* )
72+
*
73+
* @param array $taxClassAmount
74+
* @param OrderTaxDetailsItemInterface $itemTaxDetail
75+
* @param float $ratio
76+
* @return array
77+
*/
78+
private function _aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $itemTaxDetail, $ratio)
79+
{
80+
$itemAppliedTaxes = $itemTaxDetail->getAppliedTaxes();
81+
foreach ($itemAppliedTaxes as $itemAppliedTax) {
82+
$taxAmount = $itemAppliedTax->getAmount() * $ratio;
83+
$baseTaxAmount = $itemAppliedTax->getBaseAmount() * $ratio;
84+
if (0 == $taxAmount && 0 == $baseTaxAmount) {
85+
continue;
86+
}
87+
$taxCode = $itemAppliedTax->getCode();
88+
if (!isset($taxClassAmount[$taxCode])) {
89+
$taxClassAmount[$taxCode]['title'] = $itemAppliedTax->getTitle();
90+
$taxClassAmount[$taxCode]['percent'] = $itemAppliedTax->getPercent();
91+
$taxClassAmount[$taxCode]['tax_amount'] = $taxAmount;
92+
$taxClassAmount[$taxCode]['base_tax_amount'] = $baseTaxAmount;
93+
} else {
94+
$taxClassAmount[$taxCode]['tax_amount'] += $taxAmount;
95+
$taxClassAmount[$taxCode]['base_tax_amount'] += $baseTaxAmount;
96+
}
97+
}
98+
return $taxClassAmount;
99+
}
100+
}

app/code/Magento/SalesGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"magento/module-sales": "*",
99
"magento/module-store": "*",
1010
"magento/module-catalog": "*",
11+
"magento/module-tax": "*",
1112
"magento/module-graph-ql": "*"
1213
},
1314
"suggest": {

0 commit comments

Comments
 (0)