1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iris\Mapping; |
4
|
|
|
|
5
|
|
|
class OrderItem extends Base |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* {@inheritdoc} |
9
|
|
|
*/ |
10
|
2 |
|
public function assign(array $externalData) |
11
|
|
|
{ |
12
|
2 |
|
if (!$externalData) { |
|
|
|
|
13
|
1 |
|
throw new \Exception('No order items sent.'); |
14
|
|
|
} |
15
|
|
|
|
16
|
1 |
|
$itemCollection = new \Iris\Transfer\Sales\Order\ItemCollection(); |
17
|
|
|
|
18
|
1 |
|
foreach ($externalData['order_items'] as $orderItem) { |
19
|
1 |
|
$itemCollection[] = $this->createSalesOrderItem($orderItem, $externalData); |
20
|
1 |
|
} |
21
|
|
|
|
22
|
1 |
|
return $itemCollection; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
1 |
|
public function map($internalData) |
29
|
|
|
{ |
30
|
|
|
$externalData = array( |
31
|
1 |
|
'id' => $internalData->getIdSalesOrderItem(), |
32
|
1 |
|
'sku' => $internalData->getSku(), |
33
|
1 |
|
'sku_supplier' => $internalData->getSkuSupplierSimple(), |
34
|
1 |
|
'quantity' => $internalData->getQuantity(), |
35
|
1 |
|
'price' => $internalData->getPaidPrice(), |
36
|
1 |
|
'taxable_price' => $internalData->getTaxablePrice() |
37
|
1 |
|
); |
38
|
|
|
|
39
|
1 |
|
return $externalData; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
private function createSalesOrderItem($orderItem, $externalData) |
43
|
|
|
{ |
44
|
1 |
|
$shippingAmount = ($externalData['freight_amount'] > 0) |
45
|
1 |
|
? ($externalData['freight_amount'] / count($externalData['order_items'])) |
46
|
1 |
|
: $externalData['freight_amount']; |
47
|
|
|
|
48
|
|
|
$data = array( |
49
|
1 |
|
'sku' => $orderItem['sku'], |
50
|
1 |
|
'unit_price' => $orderItem['price'], |
51
|
1 |
|
'paid_price' => $orderItem['price'], |
52
|
1 |
|
'carrier_id' => $orderItem['carrier_id'], |
53
|
1 |
|
'shipment_carrier_delivery_time' => $orderItem['carrier_delivery_time'], |
54
|
1 |
|
'shipment_warehouse_processing_time' => $orderItem['warehouse_time'], |
55
|
1 |
|
'shipment_total_delivery_time' => $orderItem['freight_time'], |
56
|
|
|
'shipping_amount' => $shippingAmount |
57
|
1 |
|
); |
58
|
|
|
|
59
|
1 |
|
return new \Iris\Transfer\Sales\Order\Item($data); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.