Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCP-4811] feat: add new properties to order and orderItem models #195

Merged
merged 8 commits into from
Dec 10, 2024
19 changes: 18 additions & 1 deletion src/Factory/Xml/Order/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class OrderFactory
'ItemsCount',
'ExtraAttributes',
'Statuses',
'GrandTotal',
'ProductTotal',
'TaxAmount',
'ShippingFeeTotal',
'ShippingTax',
'Voucher',
];

public static function make(SimpleXMLElement $element): Order
Expand Down Expand Up @@ -110,7 +116,18 @@ public static function make(SimpleXMLElement $element): Order
$businessInvoiceRequired,
$shippingType,
$operatorCode,
$extraBillingAttributes
$extraBillingAttributes,
self::stringToFloat((string) $element->GrandTotal),
self::stringToFloat((string) $element->ProductTotal),
self::stringToFloat((string) $element->TaxAmount),
self::stringToFloat((string) $element->ShippingFeeTotal),
self::stringToFloat((string) $element->ShippingTax),
self::stringToFloat((string) $element->Voucher)
);
}

public static function stringToFloat(string $value): float
{
return (float) str_replace(',', '', $value);
}
}
2 changes: 2 additions & 0 deletions src/Factory/Xml/Order/OrderItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OrderItemFactory
'CreatedAt',
'UpdatedAt',
'ReturnStatus',
'ShippingTax',
];
private const REQUIRED_FIELDS_FROM_STATUS = [
'PurchaseOrderId',
Expand Down Expand Up @@ -117,6 +118,7 @@ public static function make(SimpleXMLElement $element): OrderItem
$createdAt,
$updatedAt,
(string) $element->ReturnStatus,
(float) $element->ShippingTax,
(string) $element->SalesType ?? null,
(string) $element->Imei ?? null
);
Expand Down
90 changes: 89 additions & 1 deletion src/Model/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ class Order implements JsonSerializable
* @param string[] $statuses
* @param string|int $orderNumber
*/

/**
* @var float|null
*/
protected $grandTotal;

/**
* @var float|null
*/
protected $productTotal;

/**
* @var float|null
*/
protected $taxAmount;

/**
* @var float|null
*/
protected $shippingFeeTotal;

/**
* @var float|null
*/
protected $shippingTax;

/**
* @var float|null
*/
protected $voucher;

/**
* @param string|int $orderNumber
* @param string[] $statuses
*/
public static function fromData(
int $orderId,
$orderNumber,
Expand All @@ -169,7 +204,13 @@ public static function fromData(
?bool $businessInvoiceRequired,
?string $shippingType,
?string $operatorCode = null,
?ExtraBillingAttributes $extraBillingAttributes = null
?ExtraBillingAttributes $extraBillingAttributes = null,
float $grandTotal,
float $productTotal,
float $taxAmount,
float $shippingFeeTotal,
float $shippingTax,
float $voucher
): Order {
$order = new self();

Expand Down Expand Up @@ -198,6 +239,12 @@ public static function fromData(
$order->shippingType = $shippingType;
$order->extraBillingAttributes = $extraBillingAttributes;
$order->operatorCode = $operatorCode;
$order->grandTotal = $grandTotal;
$order->productTotal = $productTotal;
$order->taxAmount = $taxAmount;
$order->shippingFeeTotal = $shippingFeeTotal;
$order->shippingTax = $shippingTax;
$order->voucher = $voucher;

return $order;
}
Expand Down Expand Up @@ -352,11 +399,46 @@ public function getExtraBillingAttributes(): ?ExtraBillingAttributes
return $this->extraBillingAttributes;
}

public function getGrandTotal(): float
{
return $this->grandTotal;
}

public function getProductTotal(): float
{
return $this->productTotal;
}

public function getTaxAmount(): float
{
return $this->taxAmount;
}

public function getShippingFeeTotal(): float
{
return $this->shippingFeeTotal;
}

public function getShippingTax(): float
{
return $this->shippingTax;
}

public function getVoucher(): float
{
return $this->voucher;
}

public function setOrderItems(OrderItems $orderItems): void
{
$this->orderItems = $orderItems;
}

public function stringToFloat(string $value): float
{
return (float) str_replace(',', '', $value);
}

public function jsonSerialize(): stdClass
{
$serialized = new stdClass();
Expand Down Expand Up @@ -386,6 +468,12 @@ public function jsonSerialize(): stdClass
$serialized->shippingType = $this->shippingType;
$serialized->extraBillingAttributes = $this->extraBillingAttributes;
$serialized->operatorCode = $this->operatorCode;
$serialized->grandTotal = $this->grandTotal;
$serialized->productTotal = $this->productTotal;
$serialized->taxAmount = $this->taxAmount;
$serialized->shippingFeeTotal = $this->shippingFeeTotal;
$serialized->shippingTax = $this->shippingTax;
$serialized->voucher = $this->voucher;

return $serialized;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Model/Order/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ class OrderItem implements JsonSerializable
*/
protected $message;

/**
* @var float|null
*/
protected $shippingTax;

/**
* @param mixed[]|null $extraAttributes
*/
Expand Down Expand Up @@ -245,6 +250,7 @@ final public static function fromOrderItem(
?DateTimeImmutable $createdAt,
?DateTimeImmutable $updatedAt,
?string $returnStatus,
?float $shippingTax = null,
?string $salesType = null,
?string $imei = null
): OrderItem {
Expand Down Expand Up @@ -286,6 +292,7 @@ final public static function fromOrderItem(
$orderItem->createdAt = $createdAt;
$orderItem->updatedAt = $updatedAt;
$orderItem->returnStatus = $returnStatus;
$orderItem->shippingTax = $shippingTax;
$orderItem->salesType = $salesType;
$orderItem->imei = $imei;

Expand Down Expand Up @@ -522,6 +529,11 @@ public function getImei(): ?string
return !empty($this->imei) ? $this->imei : null;
}

public function getShippingTax(): ?float
{
return $this->shippingTax;
}

public function setImei(?string $imei): void
{
$this->imei = $imei;
Expand Down Expand Up @@ -566,6 +578,7 @@ public function jsonSerialize(): stdClass
$serialized->createdAt = $this->createdAt;
$serialized->updatedAt = $this->updatedAt;
$serialized->returnStatus = $this->returnStatus;
$serialized->shippingTax = $this->shippingTax;
$serialized->salesType = $this->salesType;
$serialized->imei = $this->imei;

Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/Order/OrderItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class OrderItemTest extends LinioTestCase
protected $packageId = '1000414030800';
protected $shippingProviderType = 'express';
protected $returnStatus = 'approved';
protected $shippingTax = 0.00;

public function testItReturnsTheValueWithEachAccessor(): void
{
Expand Down Expand Up @@ -90,6 +91,7 @@ public function testItReturnsTheValueWithEachAccessor(): void
$this->assertEquals($orderItem->getUpdatedAt(), DateTimeImmutable::createFromFormat('Y-m-d H:i:s', (string) $simpleXml->UpdatedAt));
$this->assertEquals($orderItem->getReturnStatus(), (string) $simpleXml->ReturnStatus);
$this->assertEquals($orderItem->getSalesType(), (string) $simpleXml->SalesType);
$this->assertEquals($orderItem->getShippingTax(), (float) $simpleXml->ShippingTax);
}

public function testItReturnsTheNullWithoutAnExtraAttributeTag(): void
Expand Down Expand Up @@ -221,6 +223,7 @@ public function testItReturnsAJsonRepresentation(): void
$expectedJson['createdAt'] = $orderItem->getCreatedAt();
$expectedJson['updatedAt'] = $orderItem->getUpdatedAt();
$expectedJson['returnStatus'] = $this->returnStatus;
$expectedJson['shippingTax'] = $orderItem->getShippingTax();

$this->assertJsonStringEqualsJsonString(Json::encode($expectedJson), Json::encode($orderItem));
}
Expand Down Expand Up @@ -303,7 +306,8 @@ public function createXmlStringForOrderItems(string $schema = 'Order/OrderItem.x
$this->purchaseOrderNumber,
$this->packageId,
$this->shippingProviderType,
$this->returnStatus
$this->returnStatus,
$this->shippingTax
);
}
}
26 changes: 25 additions & 1 deletion tests/Unit/Order/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class OrderTest extends LinioTestCase
protected $operatorCode = 'facl';
protected $shippingType = 'Dropshipping';
protected $businessInvoiceRequired = true;
protected $grandTotal = 95800;
protected $productTotal = 95800;
protected $taxAmount = 15295;
protected $shippingFeeTotal = 0;
protected $shippingTax = 0;
protected $voucher = 0;

public function testItReturnsValidOrder(): Order
{
Expand Down Expand Up @@ -93,6 +99,12 @@ public function testItReturnsValidOrder(): Order
$this->assertEquals((string) $simpleXml->ExtraAttributes, $order->getExtraAttributes());
$this->assertSame((string) $simpleXml->Statuses->Status[0], $order->getStatuses()[0]);
$this->assertEquals((string) $simpleXml->OperatorCode, $order->getOperatorCode());
$this->assertEquals((string) $simpleXml->GrandTotal, $order->getGrandTotal());
$this->assertEquals((string) $simpleXml->ProductTotal, $order->getProductTotal());
$this->assertEquals((string) $simpleXml->TaxAmount, $order->getTaxAmount());
$this->assertEquals((string) $simpleXml->ShippingFeeTotal, $order->getShippingFeeTotal());
$this->assertEquals((string) $simpleXml->ShippingTax, $order->getShippingTax());
$this->assertEquals((string) $simpleXml->Voucher, $order->getVoucher());

return $order;
}
Expand Down Expand Up @@ -197,6 +209,12 @@ public function testItReturnsAJsonRepresentation(string $property): void
$expectedJson['businessInvoiceRequired'] = $this->businessInvoiceRequired;
$expectedJson['shippingType'] = $this->shippingType;
$expectedJson['operatorCode'] = $this->operatorCode;
$expectedJson['grandTotal'] = $this->grandTotal;
$expectedJson['productTotal'] = $this->productTotal;
$expectedJson['taxAmount'] = $this->taxAmount;
$expectedJson['shippingFeeTotal'] = $this->shippingFeeTotal;
$expectedJson['shippingTax'] = $this->shippingTax;
$expectedJson['voucher'] = $this->voucher;

$this->assertJsonStringEqualsJsonString(Json::encode($expectedJson), Json::encode($order));
}
Expand Down Expand Up @@ -259,7 +277,13 @@ public function createXmlStringForAOrder(string $schema = 'Order/Order.xml'): st
$this->extraAttributes,
$this->statuses[0],
$this->statuses[1],
$this->operatorCode
$this->operatorCode,
$this->grandTotal,
$this->productTotal,
$this->taxAmount,
$this->shippingFeeTotal,
$this->shippingTax,
$this->voucher
);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/_schemas/Order/Order.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@
},
"businessInvoiceRequired": null,
"shippingType": null,
"orderItems": null
"orderItems": null,
"grandTotal": "%d",
"productTotal": "%d",
"taxAmount": "%d",
"shippingFeeTotal": "%d",
"shippingTax": "%d",
"voucher": "%d"
}
6 changes: 6 additions & 0 deletions tests/_schemas/Order/Order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@
<InvoiceRequired>true</InvoiceRequired>
<ShippingType>Dropshipping</ShippingType>
<OperatorCode>%s</OperatorCode>
<GrandTotal>%s</GrandTotal>
<ProductTotal>%s</ProductTotal>
<TaxAmount>%s</TaxAmount>
<ShippingFeeTotal>%s</ShippingFeeTotal>
<ShippingTax>%s</ShippingTax>
<Voucher>%s</Voucher>
</Order>
1 change: 1 addition & 0 deletions tests/_schemas/Order/OrderItem.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"createdAt": "%s",
"updatedAt": "%s",
"returnStatus": "%s",
"shippingTax": "%s",
"salesType": "PURE_MARKETPLACE",
"imei": "qwe12eqw165qw4-qw7e89q654"
}
2 changes: 1 addition & 1 deletion tests/_schemas/Order/OrderItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<CreatedAt>2018-10-13 23:08:34</CreatedAt>
<UpdatedAt>2018-10-14 13:30:50</UpdatedAt>
<ReturnStatus>%s</ReturnStatus>
<SalesType>PURE_MARKETPLACE</SalesType>
<ShippingTax>%s</ShippingTax>
<SalesType>PURE_MARKETPLACE</SalesType>
<Imei>qwe12eqw165qw4-qw7e89q654</Imei>
</OrderItem>
2 changes: 2 additions & 0 deletions tests/_schemas/Order/OrderItemsResponse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<CodCollectableAmount/>
<ShippingAmount>0.00</ShippingAmount>
<ShippingServiceCost>7000.00</ShippingServiceCost>
<ShippingTax>1330</ShippingTax>
<VoucherAmount>0</VoucherAmount>
<VoucherCode/>
<Status>pending</Status>
Expand All @@ -45,6 +46,7 @@
<CreatedAt>2018-10-13 23:08:34</CreatedAt>
<UpdatedAt>2018-10-14 13:30:50</UpdatedAt>
<ReturnStatus/>
<ShippingTax>0.0</ShippingTax>
<Imei>weqweqwe-123123</Imei>
</OrderItem>
</OrderItems>
Expand Down
6 changes: 6 additions & 0 deletions tests/_schemas/Order/OrderResponse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
<InvoiceRequired>true</InvoiceRequired>
<ShippingType>Dropshipping</ShippingType>
<OperatorCode>FACL</OperatorCode>
<GrandTotal>69,190.00</GrandTotal>
<ProductTotal>46,200.00</ProductTotal>
<TaxAmount>0.00</TaxAmount>
<ShippingFeeTotal>22,990.00</ShippingFeeTotal>
<ShippingTax>0.00</ShippingTax>
<Voucher>0.00</Voucher>
</Order>
</Orders>
</Body>
Expand Down
Loading
Loading