1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Model\ResourceModel; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class StockItem |
8
|
|
|
*/ |
9
|
|
|
class StockItem extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb |
10
|
|
|
{ |
11
|
|
|
const BULK_INSERT_CHUNK_SIZE = 100; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \Magento\Framework\EntityManager\EntityManager |
15
|
|
|
*/ |
16
|
|
|
protected $entityManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Gets the amount of items available on stock. |
20
|
|
|
* |
21
|
|
|
* @param string|string[] $eans |
22
|
|
|
* @return array|string |
23
|
|
|
*/ |
24
|
2 |
|
public function getNotReservedStockAmount($eans) |
25
|
|
|
{ |
26
|
2 |
|
$connection = $this->getConnection(); |
27
|
|
|
|
28
|
2 |
|
$query = $connection->select() |
29
|
2 |
|
->from( |
30
|
2 |
|
['s' => $this->getMainTable()], |
31
|
2 |
|
['s.ean', 'amount' => new \Zend_Db_Expr('IF(s.noos = 1, 1000000000, s.amount - COALESCE(SUM(r.amount), 0))')] |
32
|
|
|
) |
33
|
2 |
|
->joinLeft( |
34
|
2 |
|
['r' => $this->getTable('stockbase_stock_reserve')], |
35
|
2 |
|
'r.ean = s.ean', |
36
|
2 |
|
null |
37
|
|
|
) |
38
|
2 |
|
->where('s.ean in (?)', $eans) |
39
|
2 |
|
->group('s.ean'); |
40
|
|
|
|
41
|
2 |
|
$pairs = $connection->fetchPairs($query); |
42
|
|
|
|
43
|
2 |
|
return is_array($eans) ? $pairs : reset($pairs); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Updates the amount of items available on stock for given EAN. |
48
|
|
|
* |
49
|
|
|
* @param string $ean |
50
|
|
|
* @param float $amount |
51
|
|
|
* @param string $operation |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
4 |
|
public function updateStockAmount($ean, $amount, $operation = '-') |
55
|
|
|
{ |
56
|
4 |
|
$amount = (float) $amount; |
57
|
4 |
|
$operation = $operation == '-' ? '-' : '+'; |
58
|
|
|
|
59
|
4 |
|
$connection = $this->getConnection(); |
60
|
|
|
|
61
|
4 |
|
$result = $connection->update( |
62
|
4 |
|
$this->getMainTable(), |
63
|
4 |
|
['amount' => new \Zend_Db_Expr(sprintf('amount %s %s', $operation, $amount))], |
64
|
4 |
|
['ean = ?' => $ean] |
65
|
|
|
); |
66
|
4 |
|
if ($result == 1) { |
67
|
3 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the modification date of the last modified item in the stock. |
75
|
|
|
* |
76
|
|
|
* @return \DateTime|null |
77
|
|
|
*/ |
78
|
3 |
|
public function getLastModifiedItemDate() |
79
|
|
|
{ |
80
|
3 |
|
$connection = $this->getConnection(); |
81
|
|
|
|
82
|
3 |
|
$query = $connection->select() |
83
|
3 |
|
->from($this->getMainTable(), 'timestamp') |
84
|
3 |
|
->order('timestamp DESC') |
85
|
3 |
|
->limit(1); |
86
|
|
|
|
87
|
3 |
|
$result = $connection->fetchCol($query); |
88
|
|
|
|
89
|
3 |
|
return !empty($result[0]) ? new \DateTime($result[0]) : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Updates the local stock based on given Stockbase API response. |
94
|
|
|
* |
95
|
|
|
* @param \stdClass $stock |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
2 |
|
public function updateFromStockObject(\stdClass $stock) |
99
|
|
|
{ |
100
|
2 |
|
$data = []; |
101
|
2 |
|
$total = 0; |
102
|
2 |
|
foreach ($stock->{'Groups'} as $group) { |
103
|
1 |
|
foreach ($group->{'Items'} as $item) { |
104
|
1 |
|
$total++; |
105
|
1 |
|
$data[] = [ |
106
|
1 |
|
'ean' => $item->{'EAN'}, |
107
|
1 |
|
'brand' => !empty($group->{'Brand'}) ? $group->{'Brand'} : null, |
108
|
1 |
|
'code' => !empty($group->{'Code'}) ? $group->{'Code'} : null, |
109
|
1 |
|
'supplier_code' => !empty($group->{'SupplierCode'}) ? $group->{'SupplierCode'} : null, |
110
|
1 |
|
'supplier_gln' => !empty($group->{'SupplierGLN'}) ? $group->{'SupplierGLN'} : null, |
111
|
1 |
|
'amount' => $item->{'Amount'}, |
112
|
1 |
|
'noos' => $item->{'NOOS'}, |
113
|
1 |
|
'timestamp' => date('Y-m-d H:i:s', $item->{'Timestamp'}), |
114
|
|
|
]; |
115
|
1 |
|
if (count($data) >= self::BULK_INSERT_CHUNK_SIZE) { |
116
|
|
|
$this->bulkUpdate($data); |
117
|
1 |
|
$data = []; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
2 |
|
if (count($data) > 0) { |
122
|
1 |
|
$this->bulkUpdate($data); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $total; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
11 |
|
protected function _construct() |
132
|
|
|
{ |
133
|
11 |
|
$this->_init('stockbase_stock', 'ean'); |
134
|
11 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $data |
138
|
|
|
* @throws \Exception |
139
|
|
|
*/ |
140
|
1 |
|
protected function bulkUpdate(array $data) |
141
|
|
|
{ |
142
|
1 |
|
$connection = $this->getConnection(); |
143
|
|
|
|
144
|
1 |
|
$connection->beginTransaction(); |
145
|
|
|
try { |
146
|
1 |
|
$connection->insertOnDuplicate($this->getMainTable(), $data); |
147
|
|
|
|
148
|
1 |
|
$connection->commit(); |
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
$connection->rollBack(); |
151
|
|
|
throw $e; |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
} |
155
|
|
|
|