1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jh\DataImportMagento\Writer; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Exception\WriterException; |
6
|
|
|
use Ddeboer\DataImport\Writer\AbstractWriter; |
7
|
|
|
use Guzzle\Common\Exception\InvalidArgumentException; |
8
|
|
|
use Jh\DataImportMagento\Exception\MagentoSaveException; |
9
|
|
|
use Jh\DataImportMagento\Options\OptionsParseTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class InventoryUpdateWriter |
13
|
|
|
* @author Aydin Hassan <[email protected]> |
14
|
|
|
* @package Jh\DataImportMagento\Writer |
15
|
|
|
*/ |
16
|
|
|
class InventoryUpdateWriter extends AbstractWriter |
17
|
|
|
{ |
18
|
|
|
use OptionsParseTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Whether to add qty to existing qty |
22
|
|
|
*/ |
23
|
|
|
const STOCK_UPDATE_TYPE_ADD = 'add'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Whether to set qty and override current |
27
|
|
|
*/ |
28
|
|
|
const STOCK_UPDATE_TYPE_SET = 'set'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Mage_Catalog_Model_Product |
32
|
|
|
*/ |
33
|
|
|
private $productModel; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $options = [ |
39
|
|
|
'productIdField' => 'sku', |
40
|
|
|
'stockUpdateType' => self::STOCK_UPDATE_TYPE_SET, |
41
|
|
|
'updateStockStatusIfInStock' => true |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \Mage_Catalog_Model_Product $productModel |
46
|
|
|
* @param array $options |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
\Mage_Catalog_Model_Product $productModel, |
50
|
|
|
array $options = array() |
51
|
|
|
) { |
52
|
|
|
$this->productModel = $productModel; |
53
|
|
|
$this->setOptions($options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $options |
58
|
|
|
*/ |
59
|
|
|
public function setOptions(array $options) |
60
|
|
|
{ |
61
|
|
|
if (isset($options['stockUpdateType'])) { |
62
|
|
|
//check the type passed in, is actually type we support |
63
|
|
|
if ($options['stockUpdateType'] !== self::STOCK_UPDATE_TYPE_SET |
64
|
|
|
&& $options['stockUpdateType'] !== self::STOCK_UPDATE_TYPE_ADD |
65
|
|
|
) { |
66
|
|
|
throw new \InvalidArgumentException( |
67
|
|
|
sprintf("'%s' is not a valid value for 'stockUpdateType'", $options['stockUpdateType']) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
$this->options = $this->parseOptions($this->options, $options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return \Ddeboer\DataImport\Writer\WriterInterface |
76
|
|
|
*/ |
77
|
|
|
public function prepare() |
78
|
|
|
{ |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $item |
84
|
|
|
* @return \Ddeboer\DataImport\Writer\WriterInterface |
85
|
|
|
* @throws \Ddeboer\DataImport\Exception\WriterException |
86
|
|
|
* @throws \Jh\DataImportMagento\Exception\MagentoSaveException |
87
|
|
|
*/ |
88
|
|
|
public function writeItem(array $item) |
89
|
|
|
{ |
90
|
|
|
if (!isset($item['product_id'])) { |
91
|
|
|
throw new WriterException("No product Id Found"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$id = $item['product_id']; |
95
|
|
|
|
96
|
|
|
if (!isset($item['qty'])) { |
97
|
|
|
throw new WriterException( |
98
|
|
|
sprintf('No Quantity found for Product: "%s". Using field "qty"', $id) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//If Given a sku as the Product ID field, we need to get the product ID |
103
|
|
|
//from the actual product |
104
|
|
|
$product = clone $this->productModel; |
105
|
|
|
switch ($this->options['productIdField']) { |
106
|
|
|
case 'sku': |
107
|
|
|
$productId = $product->getIdBySku($id); |
108
|
|
|
if (!$productId) { |
109
|
|
|
throw new WriterException( |
110
|
|
|
sprintf('Product not found with SKU: "%s"', $id) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
break; |
114
|
|
|
case 'id': |
115
|
|
|
default: |
116
|
|
|
//default to assume just using product_id |
117
|
|
|
$productId = $id; |
118
|
|
|
break; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$product->load($productId); |
122
|
|
|
$stockItem = $product->getData('stock_item'); |
123
|
|
|
|
124
|
|
|
switch ($this->options['stockUpdateType']) { |
125
|
|
|
case self::STOCK_UPDATE_TYPE_ADD: |
126
|
|
|
$stockItem->setData('qty', $stockItem->getData('qty') + $item['qty']); |
127
|
|
|
break; |
128
|
|
|
case self::STOCK_UPDATE_TYPE_SET: |
129
|
|
|
$stockItem->setData('qty', $item['qty']); |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->options['updateStockStatusIfInStock']) { |
134
|
|
|
// set item to in stock if the new qty matches or is greater than min qty in the config |
135
|
|
|
if ($item['qty'] >= $stockItem->getMinQty()) { |
136
|
|
|
$stockItem->setData('is_in_stock', 1); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
try { |
141
|
|
|
$stockItem->save(); |
142
|
|
|
} catch (\Exception $e) { |
143
|
|
|
throw new MagentoSaveException($e); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|