1
|
|
|
<?php |
2
|
|
|
namespace Jh\DataImportMagento\Writer; |
3
|
|
|
|
4
|
|
|
use Ddeboer\DataImport\Exception\WriterException; |
5
|
|
|
use Ddeboer\DataImport\Writer\AbstractWriter; |
6
|
|
|
use Jh\DataImportMagento\Exception\MagentoSaveException; |
7
|
|
|
use Jh\DataImportMagento\Options\OptionsParseTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ShipmentWriter |
11
|
|
|
* @package Jh\DataImportMagento\Writer |
12
|
|
|
* @author Aydin Hassan <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ShipmentWriter extends AbstractWriter |
15
|
|
|
{ |
16
|
|
|
use OptionsParseTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Mage_Core_Model_Resource_Transaction |
20
|
|
|
*/ |
21
|
|
|
protected $transactionResourceModel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Mage_Sales_Model_Order |
25
|
|
|
*/ |
26
|
|
|
protected $orderModel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Mage_Sales_Model_Order_Shipment_Track |
30
|
|
|
*/ |
31
|
|
|
protected $trackingModel; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $options = [ |
37
|
|
|
'send_shipment_email' => false |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Mage_Sales_Model_Order $order |
42
|
|
|
* @param \Mage_Core_Model_Resource_Transaction $transactionResourceModel |
43
|
|
|
* @param \Mage_Sales_Model_Order_Shipment_Track $trackingModel |
44
|
|
|
* @param array $options |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
\Mage_Sales_Model_Order $order, |
48
|
|
|
\Mage_Core_Model_Resource_Transaction $transactionResourceModel, |
49
|
|
|
\Mage_Sales_Model_Order_Shipment_Track $trackingModel, |
50
|
|
|
array $options |
51
|
|
|
) { |
52
|
|
|
$this->orderModel = $order; |
53
|
|
|
$this->transactionResourceModel = $transactionResourceModel; |
54
|
|
|
$this->trackingModel = $trackingModel; |
55
|
|
|
$this->setOptions($options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $options |
60
|
|
|
*/ |
61
|
|
|
public function setOptions(array $options) |
62
|
|
|
{ |
63
|
|
|
$this->options = $this->parseOptions($this->options, $options); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $item |
68
|
|
|
* @return \Ddeboer\DataImport\Writer\WriterInterface|void |
69
|
|
|
* @throws \Ddeboer\DataImport\Exception\WriterException |
70
|
|
|
* @throws \Jh\DataImportMagento\Exception\MagentoSaveException |
71
|
|
|
*/ |
72
|
|
|
public function writeItem(array $item) |
73
|
|
|
{ |
74
|
|
|
if (!isset($item['order_id'])) { |
75
|
|
|
throw new WriterException('order_id must be set'); |
76
|
|
|
} |
77
|
|
|
$order = clone $this->orderModel; |
78
|
|
|
$order->loadByIncrementId($item['order_id']); |
79
|
|
|
if (!$order->getId()) { |
80
|
|
|
throw new WriterException(sprintf('Order with ID: "%s" cannot be found', $item['order_id'])); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$shipment = $order->prepareShipment(); |
85
|
|
|
$shipment->register(); |
86
|
|
|
$shipment->getOrder()->setData('is_in_process', true); |
87
|
|
|
|
88
|
|
|
$transactionSave = clone $this->transactionResourceModel; |
89
|
|
|
$transactionSave |
90
|
|
|
->addObject($shipment) |
91
|
|
|
->addObject($shipment->getOrder()) |
92
|
|
|
->save(); |
93
|
|
|
|
94
|
|
|
if (array_key_exists('tracks', $item) && is_array($item['tracks'])) { |
95
|
|
|
foreach ($item['tracks'] as $currentTrack) { |
96
|
|
|
$tracking = clone $this->trackingModel; |
97
|
|
|
$tracking->setShipment($shipment); |
98
|
|
|
$tracking->setData( |
99
|
|
|
[ |
100
|
|
|
'title' => $currentTrack['carrier'], |
101
|
|
|
'number' => $currentTrack['tracking_number'], |
102
|
|
|
'carrier_code' => 'custom', |
103
|
|
|
'order_id' => $order->getId() |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
$tracking->save(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
throw new MagentoSaveException($e->getMessage()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->options['send_shipment_email']) { |
114
|
|
|
$shipment->sendEmail(true); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|