Stockbase - Version 1.1.1

Version Notes

Fixes:

- Image sync running perpetually when not permitted.
- Image sync single API call for all EANs to sync instead of 1 call per EAN
- Some code refactoring.

Download this release

Release Info

Developer Divide BV
Extension Stockbase
Version 1.1.1
Comparing to
See all releases


Code changes from version 1.0.3 to 1.1.1

app/code/local/Divide/Stockbase/Block/Config.php CHANGED
@@ -1,15 +1,15 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Block_Config extends Mage_Adminhtml_Block_Template
4
- {
5
- /**
6
- * Config Block constructor
7
- * Constructs the page and loads the phtml-template
8
- * See app/design/default/default/layout & /template for these files.
9
- */
10
- public function __construct()
11
- {
12
- parent::__construct();
13
- $this->setTemplate('stockbase/config.phtml');
14
- }
15
- }
1
+ <?php
2
+
3
+ class Divide_Stockbase_Block_Config extends Mage_Adminhtml_Block_Template
4
+ {
5
+ /**
6
+ * Config Block constructor
7
+ * Constructs the page and loads the phtml-template
8
+ * See app/design/default/default/layout & /template for these files.
9
+ */
10
+ public function __construct()
11
+ {
12
+ parent::__construct();
13
+ $this->setTemplate('stockbase/config.phtml');
14
+ }
15
+ }
app/code/local/Divide/Stockbase/Helper/Data.php CHANGED
@@ -1,29 +1,72 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Helper_Data extends Mage_Core_Helper_Abstract
4
- {
5
- /**
6
- * Returns array with all product eans from this shop. Uses
7
- * the configured field in Stockbase settings to retrieve the ean values.
8
- *
9
- * @return array
10
- */
11
- public function getAllEans()
12
- {
13
- $attribute = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
14
- $collection = Mage::getModel('catalog/product')->getCollection();
15
- $storeId = Mage::app()->getStore()->getStoreId();
16
-
17
- $eans = [];
18
- foreach ($collection as $product) {
19
- $value = Mage::getResourceModel('catalog/product')
20
- ->getAttributeRawValue($product->getId(), $attribute, $storeId);
21
-
22
- if ($value) {
23
- $eans[] = $value;
24
- }
25
- }
26
-
27
- return $eans;
28
- }
29
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @property Divide_Stockbase_Helper_HTTP _requestHelper
5
+ */
6
+ class Divide_Stockbase_Helper_Data extends Mage_Core_Helper_Abstract
7
+ {
8
+
9
+ /**
10
+ * Holds the request helper HTTP for calls to Stockbase
11
+ *
12
+ * @var Divide_Stockbase_Helper_HTTP
13
+ */
14
+ protected $_requestHelper;
15
+
16
+ /**
17
+ * Divide_Stockbase_Helper_Data constructor.
18
+ */
19
+ public function __construct()
20
+ {
21
+ $this->_requestHelper = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
22
+ }
23
+
24
+ /**
25
+ * Returns array with all product eans from this shop. Uses
26
+ * the configured field in Stockbase settings to retrieve the ean values.
27
+ *
28
+ * @return array
29
+ */
30
+ public function getAllEans()
31
+ {
32
+ $attribute = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
33
+ $collection = Mage::getModel('catalog/product')->getCollection();
34
+ $storeId = Mage::app()->getStore()->getStoreId();
35
+
36
+ $eans = array();
37
+ foreach ($collection as $product) {
38
+ $value = Mage::getResourceModel('catalog/product')
39
+ ->getAttributeRawValue($product->getId(), $attribute, $storeId);
40
+
41
+ if ($value) {
42
+ $eans[] = $value;
43
+ }
44
+ }
45
+
46
+ return $eans;
47
+ }
48
+
49
+ /**
50
+ * Collect all EANS from user Stockbase account in a array.
51
+ *
52
+ * @return type
53
+ */
54
+ public function getAllEansFromStockbase()
55
+ {
56
+ $stockbaseStock = $this->getStock();
57
+ $eans = array();
58
+
59
+ foreach ($stockbaseStock->{'Groups'} as $group) {
60
+ foreach ($group->{'Items'} as $brand) {
61
+ $eans[] = $brand->EAN;
62
+ }
63
+ }
64
+
65
+ return $eans;
66
+ }
67
+
68
+ public function getStock()
69
+ {
70
+ return $this->_requestHelper->getStock();
71
+ }
72
+ }
app/code/local/Divide/Stockbase/Helper/HTTP.php CHANGED
@@ -1,474 +1,458 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Helper_HTTP extends Mage_Core_Helper_Abstract
4
- {
5
-
6
- /**
7
- * Webservice ENDPOINTS, & ANSWERS
8
- * Live: https://iqservice.divide.nl/
9
- * Dev: http://server.divide.nl/divide.api/
10
- * Docs: http://server.divide.nl/divide.api/docs
11
- */
12
- const ENDPOINT_LOGIN = 'services/login';
13
- const ENDPOINT_AUTH = 'authenticate';
14
- const ENDPOINT_STOCKBASE_STOCK = 'stockbase/stock';
15
- const ENDPOINT_STOCKBASE_ORDER = 'stockbase/orderRequest';
16
- const ENDPOINT_STOCKBASE_IMAGES = 'stockbase/images';
17
- const ANSWER_INVALID_CREDENTIALS = 'LoginCredentialsNotValid';
18
- const ANSWER_EMPTY_CREDENTIALS = 'CredentialsEmpty';
19
- const ANSWER_SUCCESS = 'Success';
20
- const ANSWER_UNKNOWN = 'UnknownError';
21
- const ANSWER_TOKEN_EXPIRED = 'TokenExpired';
22
-
23
- protected $authToken;
24
- protected $authTokenExpiration;
25
- protected $refreshToken;
26
- protected $accessToken;
27
- protected $awnser;
28
- protected $client;
29
- protected $env;
30
-
31
- /**
32
- * Divide_Stockbase_Helper_HTTP constructor.
33
- */
34
- public function __construct()
35
- {
36
- // Set mage storeview to default admin
37
- Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
38
- // Using UTC standards
39
- $now = new DateTime('NOW', new DateTimeZone('UTC'));
40
- // Varien HTTP Client for making request
41
- $this->client = new Varien_Http_Client();
42
- // Varien File IO for write/read actions to filesystem
43
- $this->file = new Varien_Io_File();
44
-
45
- // Server configured to use (production or development)
46
- $this->env = Mage::getStoreConfig('stockbase_options/login/stockbase_enviroment');
47
- // Client uses the REST Interface, set to be application/json for all calls.
48
- $this->client->setHeaders('Content-Type', 'application/json');
49
- $this->accessToken = Mage::getStoreConfig('stockbase_options/token/access');
50
- $this->authToken = Mage::getStoreConfig('stockbase_options/token/auth');
51
- $this->authTokenExpiration = Mage::getStoreConfig('stockbase_options/token/auth_expiration_date');
52
- $this->accessTokenExpiration = Mage::getStoreConfig('stockbase_options/token/access_expiration_date');
53
- $this->refreshToken = Mage::getStoreConfig('stockbase_options/token/refresh');
54
- $this->username = Mage::getStoreConfig('stockbase_options/login/username');
55
- $this->password = Mage::getStoreConfig('stockbase_options/login/password');
56
-
57
- // Check at instantiation for legit & non-expired tokens, otherwise refresh.
58
- if (strtotime($this->authTokenExpiration) < $now->getTimestamp()) {
59
- $this->authenticate(true);
60
- }
61
- }
62
-
63
- /**
64
- * Authenticate with the authentication token
65
- * for access and refresh tokens. refresh parameter
66
- * forces to refresh tokens, otherwise accesstoken will
67
- * be used by default.
68
- *
69
- * @param bool $refresh
70
- * @return bool
71
- */
72
- public function authenticate($refresh = false)
73
- {
74
- $authClient = new Zend_Http_Client($this->env . self::ENDPOINT_AUTH);
75
- $token = null;
76
-
77
- // if refresh true, use refresh, but if not set, use auth instead
78
- if($refresh){
79
- $token = Mage::getStoreConfig('stockbase_options/token/refresh');
80
- }
81
-
82
- // Possible that we dont have refresh token yet, then fallback on the auth token to present
83
- if (!$token) {
84
- $token = Mage::getStoreConfig('stockbase_options/token/auth');
85
- }
86
-
87
- // If we still dont have token, login first then.
88
- if ($token == null) {
89
- $token = $this->login();
90
- }
91
-
92
- $authClient->setHeaders([
93
- 'Content-Type' => 'application/json',
94
- 'Authentication' => $token
95
- ]);
96
-
97
- $result = json_decode($authClient->request('GET')->getBody());
98
- $response = $result->{'nl.divide.iq'};
99
-
100
- if ($response->answer == self::ANSWER_TOKEN_EXPIRED) {
101
- $this->login($this->username, $this->password);
102
- }
103
-
104
- if ($response->answer == self::ANSWER_SUCCESS) {
105
- if(isset($response->refresh_token)){
106
- Mage::getModel('core/config')
107
- ->saveConfig('stockbase_options/token/refresh', $response->refresh_token);
108
- }
109
- Mage::getModel('core/config')
110
- ->saveConfig('stockbase_options/token/access', $response->access_token);
111
- Mage::getModel('core/config')
112
- ->saveConfig('stockbase_options/token/access_expiration_date', $response->expiration_date);
113
-
114
-
115
- return true;
116
- }
117
-
118
- return false;
119
- }
120
-
121
- /**
122
- * Array with username & password for getting
123
- * the authentication token and expiration date
124
- *
125
- * @param $username
126
- * @param $password
127
- * @return bool
128
- */
129
- public function login($username = false, $password = false)
130
- {
131
- $loginClient = new Zend_Http_Client($this->env . self::ENDPOINT_LOGIN);
132
-
133
- if ($username == false || $password == false) {
134
- $username = Mage::getStoreConfig('stockbase_options/login/username');
135
- $password = Mage::getStoreConfig('stockbase_options/login/password');
136
- }
137
-
138
- $loginClient->setHeaders([
139
- 'Content-Type' => 'application/json',
140
- 'Username' => $username,
141
- 'Password' => $password
142
- ]);
143
-
144
- $result = json_decode($loginClient->request('GET')->getBody());
145
- $response = $result->{'nl.divide.iq'};
146
-
147
- if ($response->answer == self::ANSWER_SUCCESS) {
148
- Mage::getModel('core/config')
149
- ->saveConfig('stockbase_options/token/auth', $response->authentication_token);
150
- Mage::getModel('core/config')
151
- ->saveConfig('stockbase_options/token/auth_expiration_date', $response->expiration_date);
152
-
153
- return $response->authentication_token;
154
- }
155
-
156
- return false;
157
- }
158
-
159
- /**
160
- * Sends order to Stockbase if ordered quantity
161
- * is not met by own stocklevels.
162
- *
163
- * @param Mage_Sales_Model_Order $mageOrder
164
- * @return bool
165
- */
166
- public function sendMageOrder(Mage_Sales_Model_Order $mageOrder)
167
- {
168
- // Configured orderPrefix for keeping ordernumbers unique for merchant.
169
- $orderPrefix = Mage::getStoreConfig('stockbase_options/login/order_prefix');
170
- // Configured EAN field from stockbase configuration
171
- $configuredEan = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
172
- $shippingAddress = $mageOrder->getShippingAddress();
173
- // Parsing street for seperation of street, number, and additional
174
- $parsedAddress = $this->splitStreet($shippingAddress->getStreet1());
175
- // StockbaseEANs from users Stockbase account
176
- $sbEans = $this->getAllEansFromStockbase();
177
- // Using UTC TimeZone as standard
178
- $now = new DateTime('now', new DateTimeZone('UTC'));
179
- // Used for collecting items from order to send
180
- $Orderlines = null;
181
-
182
- // If splitStreet fails, fallback to fullstreet.
183
- if (!$parsedAddress['street'] || !$parsedAddress['number']) {
184
- $parsedAddress['street'] = $shippingAddress->getStreetFull();
185
- $parsedAddress['number'] = '-';
186
- }
187
-
188
- // Parsing Address fields from the Magento Order
189
- $Address = [
190
- 'Street' => $parsedAddress['street'],
191
- 'StreetNumber' => (int) $parsedAddress['number'],
192
- 'StreetNumberAddition' => $parsedAddress['numberAddition'],
193
- 'ZipCode' => $shippingAddress->getPostcode(),
194
- 'City' => $shippingAddress->getCity(),
195
- 'CountryCode' => $shippingAddress->getCountryModel()->getIso3Code(),
196
- ];
197
-
198
- // Filling Person details
199
- $Person = [
200
- 'Gender' => $mageOrder->getCustomerGender() ? 'Female' : 'Male',
201
- 'Initials' => strtoupper($mageOrder->getCustomerFirstname()[0]),
202
- 'FirstName' => $mageOrder->getCustomerFirstname(),
203
- 'SurnamePrefix' => $mageOrder->getCustomerPrefix() ? : ' ',
204
- 'Surname' => $mageOrder->getCustomerLastname(),
205
- 'Company' => $mageOrder->getShippingAddress()->getCompany() ? : " ",
206
- ];
207
-
208
- // Put the person and Adres in OrderDelivery Array
209
- $OrderDelivery = [
210
- 'Person' => $Person,
211
- 'Address' => $Address,
212
- ];
213
-
214
- // Loop over ordered items and check if there is a shortage on own stock
215
- foreach ($mageOrder->getAllItems() as $key => $item) {
216
- // Get stockitem from the orderedProduct to check with.
217
- $stock = $item->getProduct()->getStockItem()->getQty();
218
-
219
- // If out of stock && known by Stockbase,
220
- // TypeCasting it into integer cuz Magento uses decimals for qty.
221
- if ($stock < (int) $item->getQtyOrdered()) {
222
- // Find the configured EAN for the product so we can identify it.
223
- $ean = $item->getProduct()->getData($configuredEan);
224
- // Now check if the ordered shortage exsists in your stockbase account.
225
- if (in_array($ean, $sbEans)) {
226
- // Finally fill up the orderlines to be send to stockbase.
227
- $Orderlines[] = [
228
- 'Number' => $key + 1, // orderLineNumber starting from 1
229
- 'EAN' => $ean,
230
- 'Amount' => (int) $item->getQtyOrdered(),
231
- ];
232
- }
233
- }
234
- }
235
-
236
- // Compose the OrderHeader with the filled up data
237
- $OrderHeader = [
238
- 'OrderNumber' => $orderPrefix . '#' . $mageOrder->getRealOrderId(),
239
- 'TimeStamp' => $now->format('Y-m-d h:i:s'),
240
- 'Attention' => $mageOrder->getCustomerNote() ? $mageOrder->getCustomerNote() : ' ',
241
- ];
242
-
243
- // Compose the OrderRequest in final form for Stockbase
244
- $OrderRequest = [
245
- 'OrderHeader' => $OrderHeader,
246
- 'OrderLines' => $Orderlines,
247
- 'OrderDelivery' => $OrderDelivery,
248
- ];
249
-
250
- // Only send the order if we have collected items to be ordered.
251
- if (count($Orderlines) > 1) {
252
- $posted = $this->post(self::ENDPOINT_STOCKBASE_ORDER, $OrderRequest)->{'nl.divide.iq'};
253
- }
254
-
255
- // If failure, we log request and response for debugging.
256
- if (!$posted->response->content->StatusCode == 1) {
257
- // We log errors to stockbase-failure.txt in the /var/log/ folder of Magento
258
- Mage::log(var_export($posted, true), false, 'stockbase-failure.txt');
259
-
260
- return false;
261
- }
262
-
263
- return true;
264
- }
265
-
266
- /**
267
- * Magento street comes with housenumber attached to it, this
268
- * method tries to seperate the Street, housnumber, and additions.
269
- *
270
- * @param type $street
271
- * @return type
272
- */
273
- protected function splitStreet($street)
274
- {
275
- $aMatch = [];
276
- $pattern = '#^([\w[:punct:] ]+) ([0-9]{1,5})([\w[:punct:]\-/]*)$#';
277
- $matchResult = preg_match($pattern, $street, $aMatch);
278
-
279
- $street = (isset($aMatch[1])) ? $aMatch[1] : '';
280
- $number = (isset($aMatch[2])) ? $aMatch[2] : '';
281
- $numberAddition = (isset($aMatch[3])) ? $aMatch[3] : '';
282
-
283
- return [
284
- 'street' => $street,
285
- 'number' => $number,
286
- 'numberAddition' => $numberAddition,
287
- ];
288
- }
289
-
290
- /**
291
- * Collect all EANS from user Stockbase account in a array.
292
- *
293
- * @return type
294
- */
295
- protected function getAllEansFromStockbase()
296
- {
297
- $stockbaseStock = $this->getStock();
298
- $eans = [];
299
-
300
- foreach ($stockbaseStock->Groups as $group) {
301
- foreach ($group->Items as $brand) {
302
- $eans[] = $brand->EAN;
303
- }
304
- }
305
-
306
- return $eans;
307
- }
308
-
309
- /**
310
- * Returns simpleXMLElement with Stock (beginning from group)
311
- * Debug param defaults to false, gives debug info about request if true.
312
- *
313
- * @param bool $debug
314
- * @return SimpleXMLElement
315
- */
316
- public function getStock($debug = false)
317
- {
318
- return $this->call(self::ENDPOINT_STOCKBASE_STOCK, $debug);
319
- }
320
-
321
- /**
322
- * Makes the final call (the request)to the
323
- * given webservice endpoint. Should only be
324
- * used internally by this class.
325
- *
326
- * @param $endpoint
327
- * @param bool $debug
328
- * @return $response (json_decoded body)
329
- */
330
- protected function call($endpoint, $debug = false)
331
- {
332
- if (!$this->hasAccessToken()) {
333
- $this->authenticate(true);
334
- }
335
-
336
- $this->client->setUri($this->env . $endpoint);
337
- $this->client->setHeaders('Authentication', $this->accessToken);
338
-
339
- $result = json_decode($this->client->request()->getBody());
340
-
341
- if ($result->{'nl.divide.iq'}->response->answer == self::ANSWER_SUCCESS) {
342
- if (!$debug) {
343
- return $result->{'nl.divide.iq'}->response->content;
344
- }
345
-
346
- return $result->{'nl.divide.iq'};
347
- }
348
- }
349
-
350
- /**
351
- * Check if accesstoken is set from magento's config.
352
- *
353
- * @return bool
354
- */
355
- protected function hasAccessToken()
356
- {
357
- return (bool)$this->accessToken;
358
- }
359
-
360
- /**
361
- * Post a payload to a endpoint. See
362
- * defined ENDPOINT constants in HTTP Helper.
363
- *
364
- * @param $endpoint
365
- * @param array $payload
366
- * @return mixed
367
- */
368
- protected function post($endpoint, $payload = [])
369
- {
370
- if (!$this->hasAccessToken()) {
371
- $this->authenticate(true);
372
- }
373
-
374
- $this->client->setUri($this->env . $endpoint);
375
- $this->client->setHeaders('Authentication', $this->accessToken);
376
- $result = $this->client->setRawData(json_encode($payload), "application/json;charset=UTF-8")->request('POST');
377
- $response = json_decode($result->getBody());
378
-
379
- if (!$response) {
380
- Mage::log(var_export($result->getBody(), true), false, 'stockbase-curl-failure.txt');
381
-
382
- return $result;
383
- }
384
-
385
- return $response;
386
- }
387
-
388
- /**
389
- * Will return image if available at stockbase for given EAN.
390
- *
391
- * @param string|array $ean
392
- */
393
- public function getImageForEan($ean)
394
- {
395
- $collectionEan = [];
396
-
397
- if (is_array($ean)) {
398
- foreach ($ean as $singleEAN) {
399
- $collectionEan[] = $singleEAN;
400
- }
401
- $collectionEan = implode(',', $collectionEan);
402
- } else {
403
- $collectionEan = $ean;
404
- }
405
-
406
- $imageClient = new Zend_Http_Client($this->env . self::ENDPOINT_STOCKBASE_IMAGES . '?ean='.$collectionEan);
407
-
408
- $imageClient->setHeaders([
409
- 'Content-Type' => 'application/json',
410
- 'Authentication' => Mage::getStoreConfig('stockbase_options/token/access')
411
- ]);
412
-
413
- $result = json_decode($imageClient->request()->getBody())->{'nl.divide.iq'};
414
-
415
- if($result->response->answer == self::ANSWER_SUCCESS){
416
- return $result->response->content->Items;
417
- }
418
-
419
- return false;
420
- }
421
-
422
- /**
423
- * Saves images array from stockbase for given $ean
424
- *
425
- * @param array $images
426
- *
427
- * @return bool
428
- */
429
- public function saveImageForProduct($images)
430
- {
431
- $productModel = Mage::getModel('catalog/product');
432
- $eanField = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
433
- $accessToken = Mage::getStoreConfig('stockbase_options/token/access');
434
- $tempFolder = Mage::getBaseDir('media') . DS . 'tmp';
435
- $io = new Varien_Io_File();
436
- $addedProducts = [];
437
-
438
- foreach ($images as $image) {
439
- $product = $productModel->loadByAttribute($eanField, $image->EAN);
440
- $io->checkAndCreateFolder($tempFolder);
441
- $filePath = $tempFolder . DS . basename($image->Url);
442
-
443
- // Continue looping, if we dont have product, we have nothing.
444
- if(!$product){
445
- continue;
446
- }
447
-
448
- $client = new Varien_Http_Client($image->Url);
449
- $client->setMethod(Varien_Http_Client::GET);
450
- $client->setHeaders('Authentication', $accessToken);
451
- $protectedImage = $client->request()->getBody();
452
-
453
- if($io->isWriteable($tempFolder)){
454
- $io->write($filePath, $protectedImage);
455
- }
456
-
457
- // Verify written file exsist
458
- if ($io->fileExists($filePath)) {
459
- if ($product->getMediaGallery() == null) {
460
- $product->setMediaGallery(['images' => [], 'values' => []]);
461
- }
462
- $product->addImageToMediaGallery(
463
- $filePath,
464
- ['image', 'small_image', 'thumbnail'],
465
- false,
466
- false
467
- );
468
- $product->save();
469
- }
470
- }
471
-
472
- return true;
473
- }
474
- }
1
+ <?php
2
+
3
+ /**
4
+ * Class Divide_Stockbase_Helper_HTTP
5
+ */
6
+ class Divide_Stockbase_Helper_HTTP extends Mage_Core_Helper_Abstract
7
+ {
8
+ /**
9
+ * Webservice ENDPOINTS, & ANSWERS
10
+ * Live: https://iqservice.divide.nl/
11
+ * Dev: http://server.divide.nl/divide.api/
12
+ * Docs: http://server.divide.nl/divide.api/docs
13
+ */
14
+ const ENDPOINT_LOGIN = 'services/login';
15
+ const ENDPOINT_AUTH = 'authenticate';
16
+ const ENDPOINT_STOCKBASE_STOCK = 'stockbase/stock';
17
+ const ENDPOINT_STOCKBASE_ORDER = 'stockbase/orderRequest';
18
+ const ENDPOINT_STOCKBASE_IMAGES = 'stockbase/images';
19
+ const ANSWER_INVALID_CREDENTIALS = 'LoginCredentialsNotValid';
20
+ const ANSWER_EMPTY_CREDENTIALS = 'CredentialsEmpty';
21
+ const ANSWER_SUCCESS = 'Success';
22
+ const ANSWER_UNKNOWN = 'UnknownError';
23
+ const ANSWER_TOKEN_EXPIRED = 'TokenExpired';
24
+
25
+ protected $_authToken;
26
+ protected $_authTokenExpiration;
27
+ protected $_refreshToken;
28
+ protected $_accessToken;
29
+ protected $_client;
30
+ protected $_env;
31
+
32
+ /**
33
+ * Divide_Stockbase_Helper_HTTP constructor.
34
+ */
35
+ public function __construct()
36
+ {
37
+ // Set mage storeview to default admin
38
+ Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
39
+ // Using UTC standards
40
+ $now = new DateTime('NOW', new DateTimeZone('UTC'));
41
+ // Varien HTTP Client for making request
42
+ $this->_client = new Varien_Http_Client();
43
+ // Varien File IO for write/read actions to filesystem
44
+ $this->file = new Varien_Io_File();
45
+
46
+ // Server configured to use (production or development)
47
+ $this->_env = Mage::getStoreConfig('stockbase_options/login/stockbase_enviroment');
48
+ // Client uses the REST Interface, set to be application/json for all calls.
49
+ $this->_client->setHeaders('Content-Type', 'application/json');
50
+ $this->_accessToken = Mage::getStoreConfig('stockbase_options/token/access');
51
+ $this->_authToken = Mage::getStoreConfig('stockbase_options/token/auth');
52
+ $this->_authTokenExpiration = Mage::getStoreConfig('stockbase_options/token/auth_expiration_date');
53
+ $this->accessTokenExpiration = Mage::getStoreConfig('stockbase_options/token/access_expiration_date');
54
+ $this->_refreshToken = Mage::getStoreConfig('stockbase_options/token/refresh');
55
+ $this->username = Mage::getStoreConfig('stockbase_options/login/username');
56
+ $this->password = Mage::getStoreConfig('stockbase_options/login/password');
57
+ }
58
+
59
+ /**
60
+ * Authenticate with the authentication token
61
+ * for access and refresh tokens. refresh parameter
62
+ * forces to refresh tokens, otherwise accesstoken will
63
+ * be used by default.
64
+ *
65
+ * @param bool $refresh
66
+ *
67
+ * @return bool
68
+ */
69
+ public function authenticate($refresh = false)
70
+ {
71
+ $authClient = new Zend_Http_Client($this->_env . self::ENDPOINT_AUTH);
72
+ $token = null;
73
+
74
+ // if refresh true, use refresh, but if not set, use auth instead
75
+ if ($refresh) {
76
+ $token = Mage::getStoreConfig('stockbase_options/token/refresh');
77
+ }
78
+
79
+ // Possible that we dont have refresh token yet, then fallback on the auth token to present
80
+ if (!$token) {
81
+ $token = Mage::getStoreConfig('stockbase_options/token/auth');
82
+ }
83
+
84
+ // If we still dont have token, login first then.
85
+ if ($token == null) {
86
+ $token = $this->login();
87
+ }
88
+
89
+ $headers = array(
90
+ 'Content-Type' => 'application/json',
91
+ 'Authentication' => $token,
92
+ );
93
+ $authClient->setHeaders($headers);
94
+
95
+ $result = json_decode($authClient->request('GET')->getBody());
96
+ $response = $result->{'nl.divide.iq'};
97
+
98
+ if ($response->answer == self::ANSWER_TOKEN_EXPIRED) {
99
+ $this->login($this->username, $this->password);
100
+ }
101
+
102
+ if ($response->answer == self::ANSWER_SUCCESS) {
103
+ if (isset($response->{'refresh_token'})) {
104
+ Mage::getModel('core/config')
105
+ ->saveConfig('stockbase_options/token/refresh', $response->{'refresh_token'});
106
+ }
107
+ Mage::getModel('core/config')
108
+ ->saveConfig('stockbase_options/token/access', $response->{'access_token'});
109
+ Mage::getModel('core/config')
110
+ ->saveConfig('stockbase_options/token/access_expiration_date', $response->{'expiration_date'});
111
+
112
+
113
+ return true;
114
+ }
115
+
116
+ return false;
117
+ }
118
+
119
+ /**
120
+ * Array with username & password for getting
121
+ * the authentication token and expiration date
122
+ *
123
+ * @param $username
124
+ * @param $password
125
+ *
126
+ * @return bool
127
+ */
128
+ public function login($username = false, $password = false)
129
+ {
130
+ $loginClient = new Zend_Http_Client($this->_env . self::ENDPOINT_LOGIN);
131
+
132
+ if ($username == false || $password == false) {
133
+ $username = Mage::getStoreConfig('stockbase_options/login/username');
134
+ $password = Mage::getStoreConfig('stockbase_options/login/password');
135
+ }
136
+
137
+ $headers = array(
138
+ 'Content-Type' => 'application/json',
139
+ 'Username' => $username,
140
+ 'Password' => $password,
141
+ );
142
+ $loginClient->setHeaders($headers);
143
+
144
+ $result = json_decode($loginClient->request('GET')->getBody());
145
+ $response = $result->{'nl.divide.iq'};
146
+
147
+ if ($response->answer == self::ANSWER_SUCCESS) {
148
+ Mage::getModel('core/config')
149
+ ->saveConfig('stockbase_options/token/auth', $response->{'authentication_token'});
150
+ Mage::getModel('core/config')
151
+ ->saveConfig('stockbase_options/token/auth_expiration_date', $response->{'expiration_date'});
152
+
153
+ return $response->{'authentication_token'};
154
+ }
155
+
156
+ return false;
157
+ }
158
+
159
+ /**
160
+ * Sends order to Stockbase if ordered quantity
161
+ * is not met by own stocklevels.
162
+ *
163
+ * @param Mage_Sales_Model_Order $mageOrder
164
+ *
165
+ * @return bool
166
+ */
167
+ public function sendMageOrder(Mage_Sales_Model_Order $mageOrder)
168
+ {
169
+ // Configured orderPrefix for keeping ordernumbers unique for merchant.
170
+ $orderPrefix = Mage::getStoreConfig('stockbase_options/login/order_prefix');
171
+ // Configured EAN field from stockbase configuration
172
+ $configuredEan = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
173
+ $shippingAddress = $mageOrder->getShippingAddress();
174
+ // Parsing street for seperation of street, number, and additional
175
+ $parsedAddress = $this->splitStreet($shippingAddress->getStreet1());
176
+ // StockbaseEANs from users Stockbase account
177
+ $sbEans = Mage::getSingleton('Divide_Stockbase_Helper_Data')->getAllEansFromStockbase();
178
+ // Using UTC TimeZone as standard
179
+ $now = new DateTime('now', new DateTimeZone('UTC'));
180
+ // Used for collecting items from order to send
181
+ $orderLines = array();
182
+
183
+ // If splitStreet fails, fallback to fullstreet.
184
+ if (!$parsedAddress['street'] || !$parsedAddress['number']) {
185
+ $parsedAddress['street'] = $shippingAddress->getStreetFull();
186
+ $parsedAddress['number'] = '-';
187
+ }
188
+
189
+ // Parsing Address fields from the Magento Order
190
+ $address = array(
191
+ 'Street' => $parsedAddress['street'],
192
+ 'StreetNumber' => (int)$parsedAddress['number'],
193
+ 'StreetNumberAddition' => $parsedAddress['numberAddition'],
194
+ 'ZipCode' => $shippingAddress->getPostcode(),
195
+ 'City' => $shippingAddress->getCity(),
196
+ 'CountryCode' => $shippingAddress->getCountryModel()->getIso3Code(),
197
+ );
198
+
199
+ // Filling Person details
200
+ $person = array(
201
+ 'Gender' => $mageOrder->getCustomerGender() ? 'Female' : 'Male',
202
+ 'Initials' => strtoupper($mageOrder->getCustomerFirstname()[0]),
203
+ 'FirstName' => $mageOrder->getCustomerFirstname(),
204
+ 'SurnamePrefix' => $mageOrder->getCustomerPrefix() ?: ' ',
205
+ 'Surname' => $mageOrder->getCustomerLastname(),
206
+ 'Company' => $mageOrder->getShippingAddress()->getCompany() ?: " ",
207
+ );
208
+
209
+ // Put the person and Adres in OrderDelivery Array
210
+ $orderDelivery = array(
211
+ 'Person' => $person,
212
+ 'Address' => $address,
213
+ );
214
+
215
+ // Loop over ordered items and check if there is a shortage on own stock
216
+ foreach ($mageOrder->getAllItems() as $key => $item) {
217
+ // Get stockitem from the orderedProduct to check with.
218
+ $stock = $item->getProduct()->getStockItem()->getQty();
219
+
220
+ // If out of stock && known by Stockbase,
221
+ // TypeCasting it into integer cuz Magento uses decimals for qty.
222
+ if ($stock < (int)$item->getQtyOrdered()) {
223
+ // Find the configured EAN for the product so we can identify it.
224
+ $ean = $item->getProduct()->getData($configuredEan);
225
+ // Now check if the ordered shortage exsists in your stockbase account.
226
+ if (in_array($ean, $sbEans)) {
227
+ // Finally fill up the orderlines to be send to stockbase.
228
+ $orderLines[] = array(
229
+ 'Number' => $key + 1, // orderLineNumber starting from 1
230
+ 'EAN' => $ean,
231
+ 'Amount' => (int)$item->getQtyOrdered(),
232
+ );
233
+ }
234
+ }
235
+ }
236
+
237
+ // Compose the OrderHeader with the filled up data
238
+ $orderHeader = array(
239
+ 'OrderNumber' => $orderPrefix . '#' . $mageOrder->getRealOrderId(),
240
+ 'TimeStamp' => $now->format('Y-m-d h:i:s'),
241
+ 'Attention' => $mageOrder->getCustomerNote() ? $mageOrder->getCustomerNote() : ' ',
242
+ );
243
+
244
+ // Compose the OrderRequest in final form for Stockbase
245
+ $orderRequest = array(
246
+ 'OrderHeader' => $orderHeader,
247
+ 'OrderLines' => $orderLines,
248
+ 'OrderDelivery' => $orderDelivery,
249
+ );
250
+
251
+ // Only send the order if we have collected items to be ordered.
252
+ if (count($orderLines) > 1) {
253
+ $posted = $this->post(self::ENDPOINT_STOCKBASE_ORDER, $orderRequest)->{'nl.divide.iq'};
254
+ }
255
+
256
+ // If failure, we log request and response for debugging.
257
+ if (!$posted->response->content->StatusCode == 1) {
258
+ // We log errors to stockbase-failure.txt in the /var/log/ folder of Magento
259
+ Mage::log(var_export($posted, true), false, 'stockbase-failure.txt');
260
+
261
+ return false;
262
+ }
263
+
264
+ return true;
265
+ }
266
+
267
+ /**
268
+ * Magento street comes with housenumber attached to it, this
269
+ * method tries to seperate the Street, housnumber, and additions.
270
+ *
271
+ * @param string $street
272
+ *
273
+ * @return array
274
+ */
275
+ protected function splitStreet($street)
276
+ {
277
+ $aMatch = array();
278
+ $pattern = '#^([\w[:punct:] ]+) ([0-9]{1,5})([\w[:punct:]\-/]*)$#';
279
+ $matchResult = preg_match($pattern, $street, $aMatch);
280
+
281
+ $street = (isset($aMatch[1])) ? $aMatch[1] : '';
282
+ $number = (isset($aMatch[2])) ? $aMatch[2] : '';
283
+ $numberAddition = (isset($aMatch[3])) ? $aMatch[3] : '';
284
+
285
+ return array(
286
+ 'street' => $street,
287
+ 'number' => $number,
288
+ 'numberAddition' => $numberAddition,
289
+ );
290
+ }
291
+
292
+ /**
293
+ * Post a payload to a endpoint. See
294
+ * defined ENDPOINT constants in HTTP Helper.
295
+ *
296
+ * @param $endpoint
297
+ * @param array $payload
298
+ *
299
+ * @return mixed
300
+ */
301
+ protected function post($endpoint, $payload = array())
302
+ {
303
+ if (!$this->hasAccessToken()) {
304
+ $this->authenticate(true);
305
+ }
306
+
307
+ $this->_client->setUri($this->_env . $endpoint);
308
+ $this->_client->setHeaders('Authentication', $this->_accessToken);
309
+ $result = $this->_client->setRawData(json_encode($payload), "application/json;charset=UTF-8")->request('POST');
310
+ $response = json_decode($result->getBody());
311
+
312
+ if (!$response) {
313
+ Mage::log(var_export($result->getBody(), true), false, 'stockbase-curl-failure.txt');
314
+
315
+ return $result;
316
+ }
317
+
318
+ return $response;
319
+ }
320
+
321
+ /**
322
+ * Check if accesstoken is set from magento's config.
323
+ *
324
+ * @return bool
325
+ */
326
+ protected function hasAccessToken()
327
+ {
328
+ return (bool)$this->_accessToken;
329
+ }
330
+
331
+ /**
332
+ * Returns simpleXMLElement with Stock (beginning from group)
333
+ * Debug param defaults to false, gives debug info about request if true.
334
+ *
335
+ * @param bool $debug
336
+ *
337
+ * @return SimpleXMLElement
338
+ */
339
+ public function getStock($debug = false)
340
+ {
341
+ return $this->call(self::ENDPOINT_STOCKBASE_STOCK, $debug);
342
+ }
343
+
344
+ /**
345
+ * Makes the final call (the request)to the
346
+ * given webservice endpoint. Should only be
347
+ * used internally by this class.
348
+ *
349
+ * @param $endpoint
350
+ * @param bool $debug
351
+ *
352
+ * @return $response (json_decoded body)
353
+ */
354
+ protected function call($endpoint, $debug = false)
355
+ {
356
+ if (!$this->hasAccessToken()) {
357
+ $this->authenticate(true);
358
+ }
359
+
360
+ $this->_client->setUri($this->_env . $endpoint);
361
+ $this->_client->setHeaders('Authentication', $this->_accessToken);
362
+
363
+ $result = json_decode($this->_client->request()->getBody());
364
+
365
+ if ($result->{'nl.divide.iq'}->response->answer == self::ANSWER_SUCCESS) {
366
+ if (!$debug) {
367
+ return $result->{'nl.divide.iq'}->response->content;
368
+ }
369
+
370
+ return $result->{'nl.divide.iq'};
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Will return image if available at stockbase for given EAN.
376
+ *
377
+ * @param string|array $eans
378
+ *
379
+ * @return array
380
+ */
381
+ public function getImageForEan($eans)
382
+ {
383
+ // Make sure `$eans` is an array.
384
+ $eans = is_array($eans) ? $eans : array($eans);
385
+
386
+ // Create the HTTP client.
387
+ $url = $this->_env . self::ENDPOINT_STOCKBASE_IMAGES;
388
+ $imageClient = new Zend_Http_Client("{$url}?ean=" . implode(',', $eans));
389
+
390
+ // Add the required headers.
391
+ $headers = array(
392
+ 'Content-Type' => 'application/json',
393
+ 'Authentication' => Mage::getStoreConfig('stockbase_options/token/access'),
394
+ );
395
+ $imageClient->setHeaders($headers);
396
+
397
+ $result = json_decode($imageClient->request()->getBody())->{'nl.divide.iq'};
398
+
399
+ if ($result->response->answer == self::ANSWER_SUCCESS) {
400
+ return $result->response->content->Items;
401
+ }
402
+
403
+ return array();
404
+ }
405
+
406
+ /**
407
+ * Saves images array from stockbase for given $ean
408
+ *
409
+ * @param array $images
410
+ *
411
+ * @return bool
412
+ */
413
+ public function saveImageForProduct($images)
414
+ {
415
+ $productModel = Mage::getModel('catalog/product');
416
+ $eanField = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
417
+ $accessToken = Mage::getStoreConfig('stockbase_options/token/access');
418
+ $tempFolder = Mage::getBaseDir('media') . DS . 'tmp';
419
+ $io = new Varien_Io_File();
420
+ $addedProducts = array();
421
+
422
+ foreach ($images as $image) {
423
+ $product = $productModel->loadByAttribute($eanField, $image->EAN);
424
+ $io->checkAndCreateFolder($tempFolder);
425
+ $filePath = $tempFolder . Mage::getSingleton('core/url')->parseUrl($image->{'Url'})->getPath();
426
+
427
+ // Continue looping, if we dont have product, we have nothing.
428
+ if (!$product) {
429
+ continue;
430
+ }
431
+
432
+ $client = new Varien_Http_Client($image->{'Url'});
433
+ $client->setMethod(Varien_Http_Client::GET);
434
+ $client->setHeaders('Authentication', $accessToken);
435
+ $protectedImage = $client->request()->getBody();
436
+
437
+ if ($io->isWriteable($tempFolder)) {
438
+ $io->write($filePath, $protectedImage);
439
+ }
440
+
441
+ // Verify written file exsist
442
+ if ($io->fileExists($filePath)) {
443
+ if ($product->getMediaGallery() == null) {
444
+ $product->setMediaGallery(array('images' => array(), 'values' => array()));
445
+ }
446
+ $product->addImageToMediaGallery(
447
+ $filePath,
448
+ array('image', 'small_image', 'thumbnail'),
449
+ false,
450
+ false
451
+ );
452
+ $product->{'save'}();
453
+ }
454
+ }
455
+
456
+ return true;
457
+ }
458
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/local/Divide/Stockbase/Model/Crons.php CHANGED
@@ -1,71 +1,89 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Model_Crons
4
- {
5
- /**
6
- * Method to be called by cron in config.xml
7
- *
8
- * @return boolean
9
- */
10
- public function runNoos()
11
- {
12
- if (Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active', Mage::app()->getStore())) {
13
- $http_helper = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
14
- $data_helper = Mage::getSingleton('Divide_Stockbase_Helper_Data');
15
- $productModel = Mage::getModel('catalog/product');
16
- $stockModel = Mage::getModel('cataloginventory/stock_item');
17
- $noosEnabled = Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active');
18
- $fieldSet = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
19
- Mage::getModel('core/config')->saveConfig('stockbase_options/cron/last_noos', date('Y-m-d h:i:s'));
20
- $http_helper->authenticate(true);
21
- $skus = $data_helper->getAllEans();
22
- $stockbaseEans = [];
23
-
24
- foreach ($http_helper->getStock()->Groups as $group) {
25
- foreach ($group->Items as $brand) {
26
- if (in_array($brand->EAN, $skus)) {
27
- $stockItem = $stockModel->loadByProduct($productModel->loadByAttribute($fieldSet, $brand->EAN));
28
- if ($stockItem && $brand->NOOS && $brand->Amount >= 1 && $noosEnabled) {
29
- $stockItem
30
- ->setData('use_config_backorders', 0)
31
- ->setData('is_in_stock', 1)
32
- ->setData('backorders', 1);
33
- $stockItem->save();
34
- }
35
- }
36
- }
37
- }
38
- Mage::log('done running never out of stock');
39
-
40
- return true;
41
- }
42
- }
43
-
44
- /**
45
- * Sync images with stockbase if configured.
46
- *
47
- * @return void
48
- */
49
- public function runImageImport()
50
- {
51
- if (Mage::getStoreConfig('stockbase_options/login/stockbase_image_import', Mage::app()->getStore())) {
52
- $cron = Mage::getSingleton('Divide_Stockbase_Model_Crons');
53
- $data_helper = Mage::getSingleton('Divide_Stockbase_Helper_Data');
54
- $http_helper = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
55
- $skus = $data_helper->getAllEans();
56
- $processed = Mage::getStoreConfig('stockbase_options/images/processed') ? : [];
57
- $processedEanList = json_decode($processed);
58
- $max = 5;
59
-
60
- foreach ($skus as $sku) {
61
- if (!in_array($sku, $processedEanList) && $max != 0) {
62
- $images = $http_helper->getImageForEan($sku);
63
- $http_helper->saveImageForProduct($images, $sku);
64
- $processedEanList[] = $sku;
65
- Mage::getConfig()->saveConfig('stockbase_options/images/processed', json_encode($processedEanList));
66
- $max--;
67
- }
68
- }
69
- }
70
- }
71
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Divide_Stockbase_Model_Crons
4
+ {
5
+ /**
6
+ * Method to be called by cron in config.xml
7
+ *
8
+ * @return boolean
9
+ */
10
+ public function runNoos()
11
+ {
12
+ if (Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active', Mage::app()->getStore())) {
13
+ $httpHelper = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
14
+ $dataHelper = Mage::getSingleton('Divide_Stockbase_Helper_Data');
15
+ $productModel = Mage::getModel('catalog/product');
16
+ $stockModel = Mage::getModel('cataloginventory/stock_item');
17
+ $noosEnabled = Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active');
18
+ $fieldSet = Mage::getStoreConfig('stockbase_options/login/stockbase_ean_field');
19
+ $lastNoos = Mage::getSingleton('core/date')->{'date'}();
20
+ Mage::getModel('core/config')->saveConfig('stockbase_options/cron/last_noos', $lastNoos);
21
+ $httpHelper->authenticate(true);
22
+ $skus = $dataHelper->getAllEans();
23
+ $stockbaseEans = array();
24
+ foreach ($httpHelper->getStock()->Groups as $group) {
25
+ foreach ($group->{'Items'} as $brand) {
26
+ if (in_array($brand->EAN, $skus)) {
27
+ $product = $productModel->loadByAttribute($fieldSet, $brand->EAN);
28
+
29
+ // Product information adding by custom attributes
30
+ if ($product) {
31
+ $product->setData('stockbase_product', 1);
32
+ $product->setData('stockbase_ean', $brand->EAN);
33
+ $product->setData('stockbase_stock', $brand->{'Amount'});
34
+ $product->setData('stockbase_noos', (bool)$brand->NOOS);
35
+ $product->{'save'}();
36
+ }
37
+
38
+ // Product stock information update
39
+ $stockItem = $stockModel->loadByProduct($product);
40
+ if ($stockItem && ($brand->{'Amount'} >= 1 || $brand->NOOS == true) && $noosEnabled) {
41
+ $stockItem
42
+ ->setData('use_config_backorders', 0)
43
+ ->setData('is_in_stock', 1)
44
+ ->setData('backorders', 1);
45
+ $stockItem->{'save'}();
46
+ }
47
+ }
48
+ }
49
+ }
50
+ Mage::log('done running never out of stock');
51
+
52
+ return true;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Sync images with stockbase if configured.
58
+ *
59
+ * @return void
60
+ */
61
+ public function runImageImport()
62
+ {
63
+ if (Mage::getStoreConfig('stockbase_options/login/stockbase_image_import', Mage::app()->getStore())) {
64
+ $dataHelper = Mage::getSingleton('Divide_Stockbase_Helper_Data');
65
+ $httpHelper = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
66
+ $allEans = $dataHelper->getAllEans();
67
+ $processedEans = json_decode(Mage::getStoreConfig('stockbase_options/images/processed')) ?: array();
68
+
69
+ // Process 100 unprocessed EANs at a time.
70
+ $eans = array_slice(array_diff($allEans, $processedEans), 0, 100);
71
+
72
+ try {
73
+ $images = $httpHelper->getImageForEan($eans);
74
+ } catch (Exception $e) {
75
+ // Error connecting to Stockbase. Stop processing.
76
+ return;
77
+ }
78
+
79
+ // Download and save the images locally.
80
+ $httpHelper->saveImageForProduct($images);
81
+
82
+ // Update the `processed images` configuration.
83
+ $processedEans = array_merge($processedEans, $eans);
84
+ Mage::getConfig()->saveConfig('stockbase_options/images/processed', json_encode($processedEans));
85
+ }
86
+
87
+ return true;
88
+ }
89
+ }
app/code/local/Divide/Stockbase/Model/Enviroment.php CHANGED
@@ -1,26 +1,26 @@
1
- <?php
2
-
3
- /**
4
- * Enviroment options selection for in admin panel.
5
- * Switching production/development server by setting it in the MageAdmin.
6
- */
7
- class Divide_Stockbase_Model_Enviroment
8
- {
9
- const DEVELOPMENT = 'http://server.divide.nl/divide.api/';
10
- const PRODUCTION = 'https://iqservice.divide.nl/';
11
-
12
- /**
13
- * Returns option array for server selection in admin configuration.
14
- *
15
- * @return array
16
- */
17
- public function toOptionArray()
18
- {
19
- $options = [
20
- ['value' => self::PRODUCTION, 'label'=>'Production'],
21
- ['value' => self::DEVELOPMENT, 'label'=>'Development']
22
- ];
23
-
24
- return $options;
25
- }
26
- }
1
+ <?php
2
+
3
+ /**
4
+ * Enviroment options selection for in admin panel.
5
+ * Switching production/development server by setting it in the MageAdmin.
6
+ */
7
+ class Divide_Stockbase_Model_Enviroment
8
+ {
9
+ const DEVELOPMENT = 'http://server.divide.nl/divide.api/';
10
+ const PRODUCTION = 'https://iqservice.divide.nl/';
11
+
12
+ /**
13
+ * Returns option array for server selection in admin configuration.
14
+ *
15
+ * @return array
16
+ */
17
+ public function toOptionArray()
18
+ {
19
+ $options = array(
20
+ array('value' => self::PRODUCTION, 'label' => 'Production'),
21
+ array('value' => self::DEVELOPMENT, 'label' => 'Development'),
22
+ );
23
+
24
+ return $options;
25
+ }
26
+ }
app/code/local/Divide/Stockbase/Model/Observer.php CHANGED
@@ -1,67 +1,67 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Model_Observer
4
- {
5
- /**
6
- * Event catch after sale has successfully been paid for. We check your
7
- * own stock levels against the ordered amount and if your stock has shortage
8
- * we order the products for you at stockbase. Please login to your Stockbase
9
- * account to see these orders.
10
- *
11
- * @param Varien_Event_Observer $observerObserver
12
- */
13
- public function afterPayment(Varien_Event_Observer $observer)
14
- {
15
- /**
16
- * @var $order Mage_Sales_Model_Order
17
- * @var $_product Mage_Sales_Model_Order_Item
18
- * @var $_stock Mage_CatalogInventory_Model_Stock_Item
19
- * @var $oStock Mage_CatalogInventory_Model_Stock_Item
20
- * @var $orderedItem Mage_Sales_Model_Order_Item
21
- * @var $shipment Mage_Sales_Model_Order_Shipment
22
- * @var $observer Varien_Event_Observer
23
- *
24
- * After invoicing and payment this method will be used to
25
- * check if your own stock will be enough to fulfill the requested amount.
26
- * If not, we will take the product and send a order to Stockbase for the
27
- * amount needed, and the order will be placed @Stockbase. This method
28
- * is using the sales_order_payment_pay event and will only be activated
29
- * after payment completes and the order is payed for by the customer.
30
- */
31
- $http = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
32
- $invoice = $observer->getInvoice();
33
- $order = $invoice->getOrder();
34
- $orderedProducts = $order->getAllItems();
35
- $moduleEnabled = Mage::getStoreConfig('stockbase_options/login/stockbase_active');
36
-
37
- if ($order && $moduleEnabled) {
38
- // Always rely on own stock first, so stockbase does not always get triggerd.
39
- $sendItToStockbase = false;
40
- foreach ($orderedProducts as $orderedProduct) {
41
- $baseQty = 0;
42
- $productId = $orderedProduct->getId();
43
- $baseQty = $orderedProduct->getQtyOrdered()
44
- - $orderedProduct->getQtyShipped()
45
- - $orderedProduct->getQtyRefunded()
46
- - $orderedProduct->getQtyCanceled();
47
- $finalQty[$productId] = $baseQty;
48
- $ownStockQty = $orderedProduct->getProduct()->getStockItem()->getQty();
49
-
50
- if ($ownStockQty <= 0 || $ownStockQty <= $baseQty) {
51
- $sendItToStockbase = true;
52
- }
53
- }
54
-
55
- // Only send if own stock is too low
56
- if ($sendItToStockbase == true) {
57
- $send = $http->sendMageOrder($order);
58
-
59
- // Add status history to order to identify Stockbase orders
60
- if ($send) {
61
- $order->addStatusHistoryComment('This order was send to stockbase.');
62
- $order->save();
63
- }
64
- }
65
- }
66
- }
67
- }
1
+ <?php
2
+
3
+ class Divide_Stockbase_Model_Observer
4
+ {
5
+ /**
6
+ * Event catch after sale has successfully been paid for. We check your
7
+ * own stock levels against the ordered amount and if your stock has shortage
8
+ * we order the products for you at stockbase. Please login to your Stockbase
9
+ * account to see these orders.
10
+ *
11
+ * @param Varien_Event_Observer $observerObserver
12
+ */
13
+ public function afterPayment(Varien_Event_Observer $observer)
14
+ {
15
+ /**
16
+ * @var $order Mage_Sales_Model_Order
17
+ * @var $_product Mage_Sales_Model_Order_Item
18
+ * @var $_stock Mage_CatalogInventory_Model_Stock_Item
19
+ * @var $oStock Mage_CatalogInventory_Model_Stock_Item
20
+ * @var $orderedItem Mage_Sales_Model_Order_Item
21
+ * @var $shipment Mage_Sales_Model_Order_Shipment
22
+ * @var $observer Varien_Event_Observer
23
+ *
24
+ * After invoicing and payment this method will be used to
25
+ * check if your own stock will be enough to fulfill the requested amount.
26
+ * If not, we will take the product and send a order to Stockbase for the
27
+ * amount needed, and the order will be placed @Stockbase. This method
28
+ * is using the sales_order_payment_pay event and will only be activated
29
+ * after payment completes and the order is payed for by the customer.
30
+ */
31
+ $http = Mage::getSingleton('Divide_Stockbase_Helper_HTTP');
32
+ $invoice = $observer->getInvoice();
33
+ $order = $invoice->getOrder();
34
+ $orderedProducts = $order->getAllItems();
35
+ $moduleEnabled = Mage::getStoreConfig('stockbase_options/login/stockbase_active');
36
+
37
+ if ($order && $moduleEnabled) {
38
+ // Always rely on own stock first, so stockbase does not always get triggerd.
39
+ $sendItToStockbase = false;
40
+ foreach ($orderedProducts as $orderedProduct) {
41
+ $baseQty = 0;
42
+ $productId = $orderedProduct->getId();
43
+ $baseQty = $orderedProduct->getQtyOrdered()
44
+ - $orderedProduct->getQtyShipped()
45
+ - $orderedProduct->getQtyRefunded()
46
+ - $orderedProduct->getQtyCanceled();
47
+ $finalQty[$productId] = $baseQty;
48
+ $ownStockQty = $orderedProduct->getProduct()->getStockItem()->getQty();
49
+
50
+ if ($ownStockQty <= 0 || $ownStockQty <= $baseQty) {
51
+ $sendItToStockbase = true;
52
+ }
53
+ }
54
+
55
+ // Only send if own stock is too low
56
+ if ($sendItToStockbase == true) {
57
+ $send = $http->sendMageOrder($order);
58
+
59
+ // Add status history to order to identify Stockbase orders
60
+ if ($send) {
61
+ $order->addStatusHistoryComment('This order was send to stockbase.');
62
+ $order->save();
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
app/code/local/Divide/Stockbase/Model/Options.php CHANGED
@@ -1,33 +1,33 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Model_Options
4
- {
5
- /**
6
- * Return options for EAN selection from all the created
7
- * attributes in magento. When user adds new fields to productattributes
8
- * this will fetch them and display them to be used as custom EAN field.
9
- * We default to 'sku' field
10
- *
11
- * @return array
12
- */
13
- public function toOptionArray()
14
- {
15
- $productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
16
- $options = [];
17
-
18
- foreach ($productAttrs as $productAttr) {
19
- $code = $productAttr->getAttributeCode();
20
- $label = $productAttr->getFrontendLabel();
21
- if (!$label) {
22
- $label = $productAttr->getAttributeCode();
23
- }
24
-
25
- $options[] = [
26
- 'value' => $code,
27
- 'label' => $label
28
- ];
29
- }
30
-
31
- return $options;
32
- }
33
- }
1
+ <?php
2
+
3
+ class Divide_Stockbase_Model_Options
4
+ {
5
+ /**
6
+ * Return options for EAN selection from all the created
7
+ * attributes in magento. When user adds new fields to productattributes
8
+ * this will fetch them and display them to be used as custom EAN field.
9
+ * We default to 'sku' field
10
+ *
11
+ * @return array
12
+ */
13
+ public function toOptionArray()
14
+ {
15
+ $productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
16
+ $options = array();
17
+
18
+ foreach ($productAttrs as $productAttr) {
19
+ $code = $productAttr->getAttributeCode();
20
+ $label = $productAttr->getFrontendLabel();
21
+ if (!$label) {
22
+ $label = $productAttr->getAttributeCode();
23
+ }
24
+
25
+ $options[] = array(
26
+ 'value' => $code,
27
+ 'label' => $label,
28
+ );
29
+ }
30
+
31
+ return $options;
32
+ }
33
+ }
app/code/local/Divide/Stockbase/controllers/Adminhtml/ConfigController.php CHANGED
@@ -1,66 +1,66 @@
1
- <?php
2
-
3
- class Divide_Stockbase_Adminhtml_ConfigController extends Mage_Adminhtml_Controller_Action
4
- {
5
- /**
6
- * indexAction for config page of Stockbase
7
- *
8
- * First loads a block and then adds the block with data to the layout.
9
- * Loads the given stockbase phtml template and fills it up
10
- * with the configured configuration found in the mage core config.
11
- *
12
- * @return void
13
- */
14
- public function indexAction()
15
- {
16
- // First start loading the default adminhtml layout
17
- $this->loadLayout();
18
-
19
- // Fetch array with key-value pair Stockbase configuration
20
- $configuration = $this->getCurrentConfigurationForStockbase();
21
-
22
- // Make the block with the retrieved configuration for rendering
23
- $configBlock = $this->getLayout()->createBlock('Mage_Core_Block_Template')
24
- ->setTemplate('stockbase/config.phtml')
25
- ->setStockbaseConfiguration($configuration);
26
-
27
- // Using the Mage_Core_Block_Template now get the
28
- // content Block and append it with our block.
29
- $this->getLayout()->getBlock('content')->append($configBlock);
30
-
31
- // Render the total Mage_Core_Block_Template with our given config.
32
- $this->renderLayout();
33
- }
34
-
35
- /**
36
- * Returns array with set configuration for stockbase.
37
- *
38
- * @return array
39
- */
40
- private function getCurrentConfigurationForStockbase()
41
- {
42
- $configuration = [];
43
- $processedImages = Mage::getStoreConfig('stockbase_options/images/processed') ?: [];
44
- $configuration['module_version'] = Mage::getConfig()->getModuleConfig("Divide_Stockbase")->version;
45
- $configuration['enabled_module'] = Mage::getStoreConfig('stockbase_options/login/stockbase_active');
46
- $configuration['enabled_noos'] = Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active');
47
- $configuration['enabled_image'] = Mage::getStoreConfig('stockbase_options/login/stockbase_image_import');
48
- $configuration['processed_images'] = count(json_decode($processedImages));
49
- $configuration['order_prefix'] = Mage::getStoreConfig('stockbase_options/login/order_prefix');
50
- $configuration['username'] = Mage::getStoreConfig('stockbase_options/login/username');
51
- $configuration['enviroment'] = Mage::getStoreConfig('stockbase_options/login/stockbase_enviroment');
52
- $configuration['last_cron'] = Mage::getStoreConfig('stockbase_options/cron/last_noos');
53
-
54
- return $configuration;
55
- }
56
-
57
- /**
58
- * Mage ACL rules for adminhtml
59
- *
60
- * @return boolean
61
- */
62
- protected function _isAllowed()
63
- {
64
- return true;
65
- }
66
- }
1
+ <?php
2
+
3
+ class Divide_Stockbase_Adminhtml_ConfigController extends Mage_Adminhtml_Controller_Action
4
+ {
5
+ /**
6
+ * indexAction for config page of Stockbase
7
+ *
8
+ * First loads a block and then adds the block with data to the layout.
9
+ * Loads the given stockbase phtml template and fills it up
10
+ * with the configured configuration found in the mage core config.
11
+ *
12
+ * @return void
13
+ */
14
+ public function indexAction()
15
+ {
16
+ // First start loading the default adminhtml layout
17
+ $this->loadLayout();
18
+
19
+ // Fetch array with key-value pair Stockbase configuration
20
+ $configuration = $this->getCurrentConfigurationForStockbase();
21
+
22
+ // Make the block with the retrieved configuration for rendering
23
+ $configBlock = $this->getLayout()->createBlock('Mage_Core_Block_Template')
24
+ ->setTemplate('stockbase/config.phtml')
25
+ ->setStockbaseConfiguration($configuration);
26
+
27
+ // Using the Mage_Core_Block_Template now get the
28
+ // content Block and append it with our block.
29
+ $this->getLayout()->getBlock('content')->append($configBlock);
30
+
31
+ // Render the total Mage_Core_Block_Template with our given config.
32
+ $this->renderLayout();
33
+ }
34
+
35
+ /**
36
+ * Returns array with set configuration for stockbase.
37
+ *
38
+ * @return array
39
+ */
40
+ protected function getCurrentConfigurationForStockbase()
41
+ {
42
+ $configuration = array();
43
+ $processedImages = Mage::getStoreConfig('stockbase_options/images/processed') ?: array();
44
+ $configuration['module_version'] = Mage::getConfig()->getModuleConfig("Divide_Stockbase")->version;
45
+ $configuration['enabled_module'] = Mage::getStoreConfig('stockbase_options/login/stockbase_active');
46
+ $configuration['enabled_noos'] = Mage::getStoreConfig('stockbase_options/login/stockbase_noos_active');
47
+ $configuration['enabled_image'] = Mage::getStoreConfig('stockbase_options/login/stockbase_image_import');
48
+ $configuration['processed_images'] = count(json_decode($processedImages));
49
+ $configuration['order_prefix'] = Mage::getStoreConfig('stockbase_options/login/order_prefix');
50
+ $configuration['username'] = Mage::getStoreConfig('stockbase_options/login/username');
51
+ $configuration['enviroment'] = Mage::getStoreConfig('stockbase_options/login/stockbase_enviroment');
52
+ $configuration['last_cron'] = Mage::getStoreConfig('stockbase_options/cron/last_noos');
53
+
54
+ return $configuration;
55
+ }
56
+
57
+ /**
58
+ * Mage ACL rules for adminhtml
59
+ *
60
+ * @return boolean
61
+ */
62
+ protected function _isAllowed()
63
+ {
64
+ return true;
65
+ }
66
+ }
app/code/local/Divide/Stockbase/etc/config.xml CHANGED
@@ -1,149 +1,149 @@
1
- <?xml version="1.0"?>
2
- <config>
3
- <modules>
4
- <Divide_Stockbase>
5
- <version>1.0.3</version>
6
- </Divide_Stockbase>
7
- </modules>
8
- <global>
9
- <helpers>
10
- <Divide_Stockbase>
11
- <class>Divide_Stockbase_Helper</class>
12
- </Divide_Stockbase>
13
- </helpers>
14
- <models>
15
- <stock>
16
- <class>Divide_Stockbase_Model</class>
17
- <resourceModel>stock_mysql4</resourceModel>
18
- </stock>
19
- <stock_mysql4>
20
- <class>Divide_Stockbase_Model_Mysql4</class>
21
- <entities>
22
- <stock>
23
- <table>stock</table>
24
- </stock>
25
- </entities>
26
- </stock_mysql4>
27
- </models>
28
- <resources>
29
- <stock_setup>
30
- <setup>
31
- <module>Divide_Stockbase</module>
32
- </setup>
33
- <connection>
34
- <use>core_setup</use>
35
- </connection>
36
- </stock_setup>
37
- <stock_write>
38
- <connection>
39
- <use>core_write</use>
40
- </connection>
41
- </stock_write>
42
- <stock_read>
43
- <connection>
44
- <use>core_read</use>
45
- </connection>
46
- </stock_read>
47
- </resources>
48
- <blocks>
49
- <stockbase>
50
- <class>Divide_Stockbase_Block</class>
51
- </stockbase>
52
- </blocks>
53
- <events>
54
- <sales_order_payment_pay>
55
- <observers>
56
- <Divide_Stockbase_Model_Observer>
57
- <type>singleton</type>
58
- <class>Divide_Stockbase_Model_Observer</class>
59
- <method>afterPayment</method>
60
- </Divide_Stockbase_Model_Observer>
61
- </observers>
62
- </sales_order_payment_pay>
63
- </events>
64
- </global>
65
- <admin>
66
- <routers>
67
- <adminhtml>
68
- <args>
69
- <modules>
70
- <Divide_Stockbase before="Mage_Adminhtml">Divide_Stockbase_Adminhtml</Divide_Stockbase>
71
- </modules>
72
- </args>
73
- </adminhtml>
74
- </routers>
75
- </admin>
76
- <adminhtml>
77
- <menu>
78
- <stockbase module="Divide_Stockbase" translate="title">
79
- <title>Stockbase</title>
80
- <sort_order>200</sort_order>
81
- <children>
82
- <stock>
83
- <title>Module Status</title>
84
- <action>adminhtml/config/index</action>
85
- </stock>
86
- <configuration>
87
- <title>Configuration</title>
88
- <action>adminhtml/system_config/edit/section/stockbase_options</action>
89
- </configuration>
90
- </children>
91
- </stockbase>
92
- </menu>
93
- <acl>
94
- <resources>
95
- <admin>
96
- <children>
97
- <system>
98
- <children>
99
- <config>
100
- <children>
101
- <stockbase_options>
102
- <title>Stockbase Module Section</title>
103
- </stockbase_options>
104
- </children>
105
- </config>
106
- </children>
107
- </system>
108
- </children>
109
- </admin>
110
- </resources>
111
- </acl>
112
- <translate>
113
- <modules>
114
- <translations>
115
- <files>
116
- <default>Divide_Stockbase.csv</default>
117
- </files>
118
- </translations>
119
- </modules>
120
- </translate>
121
- <layout>
122
- <updates>
123
- <stockbase>
124
- <file>stockbase.xml</file>
125
- </stockbase>
126
- </updates>
127
- </layout>
128
- </adminhtml>
129
- <crontab>
130
- <jobs>
131
- <stockbase_noos_cron>
132
- <schedule>
133
- <cron_expr>0 * * * *</cron_expr>
134
- </schedule>
135
- <run>
136
- <model>stock/crons::runNoos</model>
137
- </run>
138
- </stockbase_noos_cron>
139
- <stockbase_image_cron>
140
- <schedule>
141
- <cron_expr>*/30 * * * *</cron_expr>
142
- </schedule>
143
- <run>
144
- <model>stock/crons::runImageImport</model>
145
- </run>
146
- </stockbase_image_cron>
147
- </jobs>
148
- </crontab>
149
- </config>
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Divide_Stockbase>
5
+ <version>1.1.0</version>
6
+ </Divide_Stockbase>
7
+ </modules>
8
+ <global>
9
+ <helpers>
10
+ <Divide_Stockbase>
11
+ <class>Divide_Stockbase_Helper</class>
12
+ </Divide_Stockbase>
13
+ </helpers>
14
+ <models>
15
+ <stock>
16
+ <class>Divide_Stockbase_Model</class>
17
+ <resourceModel>stock_mysql4</resourceModel>
18
+ </stock>
19
+ <stock_mysql4>
20
+ <class>Divide_Stockbase_Model_Mysql4</class>
21
+ <entities>
22
+ <stock>
23
+ <table>stock</table>
24
+ </stock>
25
+ </entities>
26
+ </stock_mysql4>
27
+ </models>
28
+ <resources>
29
+ <stock_setup>
30
+ <setup>
31
+ <module>Divide_Stockbase</module>
32
+ </setup>
33
+ <connection>
34
+ <use>core_setup</use>
35
+ </connection>
36
+ </stock_setup>
37
+ <stock_write>
38
+ <connection>
39
+ <use>core_write</use>
40
+ </connection>
41
+ </stock_write>
42
+ <stock_read>
43
+ <connection>
44
+ <use>core_read</use>
45
+ </connection>
46
+ </stock_read>
47
+ </resources>
48
+ <blocks>
49
+ <stockbase>
50
+ <class>Divide_Stockbase_Block</class>
51
+ </stockbase>
52
+ </blocks>
53
+ <events>
54
+ <sales_order_payment_pay>
55
+ <observers>
56
+ <Divide_Stockbase_Model_Observer>
57
+ <type>singleton</type>
58
+ <class>Divide_Stockbase_Model_Observer</class>
59
+ <method>afterPayment</method>
60
+ </Divide_Stockbase_Model_Observer>
61
+ </observers>
62
+ </sales_order_payment_pay>
63
+ </events>
64
+ </global>
65
+ <admin>
66
+ <routers>
67
+ <adminhtml>
68
+ <args>
69
+ <modules>
70
+ <Divide_Stockbase before="Mage_Adminhtml">Divide_Stockbase_Adminhtml</Divide_Stockbase>
71
+ </modules>
72
+ </args>
73
+ </adminhtml>
74
+ </routers>
75
+ </admin>
76
+ <adminhtml>
77
+ <menu>
78
+ <stockbase module="Divide_Stockbase" translate="title">
79
+ <title>Stockbase</title>
80
+ <sort_order>200</sort_order>
81
+ <children>
82
+ <stock>
83
+ <title>Module Status</title>
84
+ <action>adminhtml/config/index</action>
85
+ </stock>
86
+ <configuration>
87
+ <title>Configuration</title>
88
+ <action>adminhtml/system_config/edit/section/stockbase_options</action>
89
+ </configuration>
90
+ </children>
91
+ </stockbase>
92
+ </menu>
93
+ <acl>
94
+ <resources>
95
+ <admin>
96
+ <children>
97
+ <system>
98
+ <children>
99
+ <config>
100
+ <children>
101
+ <stockbase_options>
102
+ <title>Stockbase Module Section</title>
103
+ </stockbase_options>
104
+ </children>
105
+ </config>
106
+ </children>
107
+ </system>
108
+ </children>
109
+ </admin>
110
+ </resources>
111
+ </acl>
112
+ <translate>
113
+ <modules>
114
+ <translations>
115
+ <files>
116
+ <default>Divide_Stockbase.csv</default>
117
+ </files>
118
+ </translations>
119
+ </modules>
120
+ </translate>
121
+ <layout>
122
+ <updates>
123
+ <stockbase>
124
+ <file>stockbase.xml</file>
125
+ </stockbase>
126
+ </updates>
127
+ </layout>
128
+ </adminhtml>
129
+ <crontab>
130
+ <jobs>
131
+ <stockbase_noos_cron>
132
+ <schedule>
133
+ <cron_expr>0 * * * *</cron_expr>
134
+ </schedule>
135
+ <run>
136
+ <model>stock/crons::runNoos</model>
137
+ </run>
138
+ </stockbase_noos_cron>
139
+ <stockbase_image_cron>
140
+ <schedule>
141
+ <cron_expr>*/30 * * * *</cron_expr>
142
+ </schedule>
143
+ <run>
144
+ <model>stock/crons::runImageImport</model>
145
+ </run>
146
+ </stockbase_image_cron>
147
+ </jobs>
148
+ </crontab>
149
+ </config>
app/code/local/Divide/Stockbase/etc/system.xml CHANGED
@@ -109,4 +109,4 @@
109
  </groups>
110
  </stockbase_options>
111
  </sections>
112
- </config>
109
  </groups>
110
  </stockbase_options>
111
  </sections>
112
+ </config>
app/code/local/Divide/Stockbase/sql/stock_setup/mysql4-upgrade-1.0.4-1.1.0.php ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: nav.appaiya
5
+ * Date: 14-10-2016
6
+ * Time: 16:55
7
+ */
8
+ // Set data:
9
+ $attributeGroup = 'General';
10
+ $attributeSetIds = array(4);
11
+
12
+ $attributeName = 'Stockbase Product'; // Is Stockbase product => stockbase_product
13
+ $attributeCode = 'stockbase_product'; // Label stockbase product
14
+
15
+ $attributeNoosName = 'Stockbase NOOS'; // Is NOOS => stockbase_noos
16
+ $attributeNoosCode = 'stockbase_noos'; // Noos Label
17
+
18
+ $attributeStockName = 'Stockbase Stock'; // Stocklevel stockbase => stockbase_stock
19
+ $attributeStockCode = 'stockbase_stock'; // label stocklevel
20
+
21
+ $attributeEANName = 'Stockbase EAN'; // Stocklevel stockbase => stockbase_stock
22
+ $attributeEANCode = 'stockbase_ean'; // label stocklevel
23
+
24
+ // Configuration for stockbase_product:
25
+ $data = array(
26
+ 'type' => 'int', // Attribute type
27
+ 'input' => 'boolean', // Input type
28
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
29
+ 'required' => false, // Is this attribute required?
30
+ 'user_defined' => false,
31
+ 'searchable' => false,
32
+ 'filterable' => false,
33
+ 'comparable' => false,
34
+ 'visible_on_front' => false,
35
+ 'unique' => false,
36
+ 'used_in_product_listing' => true,
37
+ // Filled from above:
38
+ 'label' => $attributeName,
39
+ );
40
+
41
+ // Configuration for stockbase noos
42
+ $dataNoos = array(
43
+ 'type' => 'int', // Attribute type
44
+ 'input' => 'boolean', // Input type
45
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
46
+ 'required' => false, // Is this attribute required?
47
+ 'user_defined' => false,
48
+ 'searchable' => false,
49
+ 'filterable' => false,
50
+ 'comparable' => false,
51
+ 'visible_on_front' => false,
52
+ 'unique' => false,
53
+ 'used_in_product_listing' => true,
54
+ // Filled from above:
55
+ 'label' => $attributeNoosName,
56
+ );
57
+
58
+ // Configuration for stockbase stock
59
+ $dataStock = array(
60
+ 'type' => 'text', // Attribute type
61
+ 'input' => 'text', // Input type
62
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
63
+ 'required' => false, // Is this attribute required?
64
+ 'user_defined' => false,
65
+ 'searchable' => false,
66
+ 'filterable' => false,
67
+ 'comparable' => false,
68
+ 'visible_on_front' => false,
69
+ 'unique' => false,
70
+ 'used_in_product_listing' => true,
71
+ // Filled from above:
72
+ 'label' => $attributeStockName,
73
+ );
74
+
75
+ // Configuration for stockbase stock
76
+ $dataEAN = array(
77
+ 'type' => 'text', // Attribute type
78
+ 'input' => 'text', // Input type
79
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
80
+ 'required' => false, // Is this attribute required?
81
+ 'user_defined' => false,
82
+ 'searchable' => false,
83
+ 'filterable' => false,
84
+ 'comparable' => false,
85
+ 'visible_on_front' => false,
86
+ 'unique' => false,
87
+ 'used_in_product_listing' => true,
88
+ // Filled from above:
89
+ 'label' => $attributeEANName,
90
+ );
91
+
92
+ $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
93
+ $installer->startSetup();
94
+ $installer->addAttribute('catalog_product', $attributeCode, $data);
95
+ $installer->addAttribute('catalog_product', $attributeNoosCode, $dataNoos);
96
+ $installer->addAttribute('catalog_product', $attributeStockCode, $dataStock);
97
+ $installer->addAttribute('catalog_product', $attributeEANCode, $dataEAN);
98
+
99
+ // Add the attribute to the proper sets/groups:
100
+ foreach ($attributeSetIds as $attributeSetId) {
101
+ $installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeGroup, $attributeCode);
102
+ $installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeGroup, $attributeNoosCode);
103
+ $installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeGroup, $attributeStockCode);
104
+ $installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeGroup, $attributeEANCode);
105
+ }
106
+
107
+ // Done:
108
+ $installer->endSetup();
app/design/adminhtml/default/default/layout/stockbase.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0"?>
2
- <layout version="0.1.6">
3
- <stockbase_config_index>
4
- <reference name="content">
5
- <block type="stockbase/config" name="config" />
6
- </reference>
7
- </stockbase_config_index>
8
- </layout>
 
 
 
 
 
 
 
 
package.xml CHANGED
@@ -1,33 +1,32 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Stockbase</name>
4
- <version>1.0.3</version>
5
  <stability>stable</stability>
6
  <license uri="https://opensource.org/licenses/GPL-3.0">GPL-3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>This module allows connecting your Stockbase account with your Magento webshop, allowing it to make use of the extra inventory offered by Stockbase.</summary>
10
  <description>This module requires you to have a Stockbase account with username and password. After installing the module you have to connect it to your Stockbase account using the configuration page in the Magento administration. After configuration you can make use of the following features:&#xD;
11
- &#xD;
12
- Products never-out-of-stock:&#xD;
13
- The connected suppliers and brands in your Stockbase account will be checked when receiving an order with an out-of-stock product. The order will be forwarded to Stockbase when applicable&#xD;
14
- &#xD;
15
- Sending orders to stockbase&#xD;
16
- When the products are out-of-stock, the order will be forwarded to Stockbase's order handling service. Your own stock is not sufficient enough to fullfill the order. Products on your webshop can continue to be ordered, even when they are out of stock. Stockbase will take the order and deliver the desired quantity.&#xD;
17
- &#xD;
18
- Product image import&#xD;
19
- The module will compare your webshop's product inventory to your Stockbase account's inventory, based on EAN. If a match is found, the module will download any images from Stockbase to your webshop and add them to the product.&#xD;
20
  </description>
21
- <notes>Release notes v1.0.3:&#xD;
22
  &#xD;
23
- - Package structure fixed&#xD;
24
- - Minor bugfixes&#xD;
25
- - Added locale dutch &amp; english&#xD;
26
- </notes>
27
  <authors><author><name>Divide BV</name><user>Stockbase</user><email>support@stockbase.nl</email></author></authors>
28
- <date>2016-09-27</date>
29
- <time>12:00:44</time>
30
- <contents><target name="magelocal"><dir name="Divide"><dir name="Stockbase"><dir name="Block"><file name="Config.php" hash="8e7dc3df9eed45c8900436888590e604"/></dir><dir name="Helper"><file name="Data.php" hash="604fdc3a6c55e38092b9273ba57e29b1"/><file name="HTTP.php" hash="32cbc78fc27516815f304a6a073685a0"/></dir><dir name="Model"><file name="Crons.php" hash="6aac78bf53dd319c879e3cc4e4f28076"/><file name="Enviroment.php" hash="03258d9afaa6121144260001f4ad518a"/><file name="Observer.php" hash="6d4aae3551083b731c4df1a3f1923f17"/><file name="Options.php" hash="ec70a8490684ddd27db168cae92d48eb"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="ConfigController.php" hash="b56e1a7f9c5e3438d40f0c86465d3cd2"/></dir></dir><dir name="etc"><file name="config.xml" hash="8df060df60b4401c1307ffb964cf46e5"/><file name="system.xml" hash="e115570eb8fcaef89c1a310289d3cb16"/></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="stockbase"><file name="config.phtml" hash="62a961154f59879c7b5ee84921408db6"/></dir></dir><dir name="layout"><file name="stockbase.xml" hash="4fe5e4e52eab018f93b1415017e5c77e"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Divide_Stockbase.xml" hash="57143df3ed1cd2598bf5c928f625da91"/></dir></target><target name="magelocale"><dir name="en_US"><file name="Divide_Stockbase.csv" hash="148597a20354efcd0e9b4c6e2c951467"/></dir><dir name="nl_NL"><file name="Divide_Stockbase.csv" hash="c5fdf87abc7957bb5145df6432cda7fc"/></dir></target></contents>
31
  <compatible/>
32
  <dependencies><required><php><min>5.4.0</min><max>6.0.0</max></php></required></dependencies>
33
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Stockbase</name>
4
+ <version>1.1.1</version>
5
  <stability>stable</stability>
6
  <license uri="https://opensource.org/licenses/GPL-3.0">GPL-3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>This module allows connecting your Stockbase account with your Magento webshop, allowing it to make use of the extra inventory offered by Stockbase.</summary>
10
  <description>This module requires you to have a Stockbase account with username and password. After installing the module you have to connect it to your Stockbase account using the configuration page in the Magento administration. After configuration you can make use of the following features:&#xD;
11
+ &#xD;
12
+ Products never-out-of-stock:&#xD;
13
+ The connected suppliers and brands in your Stockbase account will be checked when receiving an order with an out-of-stock product. The order will be forwarded to Stockbase when applicable&#xD;
14
+ &#xD;
15
+ Sending orders to stockbase&#xD;
16
+ When the products are out-of-stock, the order will be forwarded to Stockbase's order handling service. Your own stock is not sufficient enough to fullfill the order. Products on your webshop can continue to be ordered, even when they are out of stock. Stockbase will take the order and deliver the desired quantity.&#xD;
17
+ &#xD;
18
+ Product image import&#xD;
19
+ The module will compare your webshop's product inventory to your Stockbase account's inventory, based on EAN. If a match is found, the module will download any images from Stockbase to your webshop and add them to the product.&#xD;
20
  </description>
21
+ <notes>Fixes:&#xD;
22
  &#xD;
23
+ - Image sync running perpetually when not permitted.&#xD;
24
+ - Image sync single API call for all EANs to sync instead of 1 call per EAN&#xD;
25
+ - Some code refactoring.</notes>
 
26
  <authors><author><name>Divide BV</name><user>Stockbase</user><email>support@stockbase.nl</email></author></authors>
27
+ <date>2016-11-25</date>
28
+ <time>16:21:13</time>
29
+ <contents><target name="magelocal"><dir name="Divide"><dir name="Stockbase"><dir name="Block"><file name="Config.php" hash="c3034e9095c4f81cc0c84e46ad8f1a33"/></dir><dir name="Helper"><file name="Data.php" hash="1b85819a6e5e77047ea1f577149fc464"/><file name="HTTP.php" hash="d4ed6cb86589e99fa339b294085be1c1"/></dir><dir name="Model"><file name="Crons.php" hash="d2aac7af7592c87718c6ca5c3f61ef6c"/><file name="Enviroment.php" hash="ffa4fe690896a16b025073c835ddf21b"/><file name="Observer.php" hash="0ba698231b1be6a96100c29b1973693e"/><file name="Options.php" hash="de03b037088ae67717fa50c7d5c9d853"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="ConfigController.php" hash="9613fab861af4e60420dc09c299fb605"/></dir></dir><dir name="etc"><file name="config.xml" hash="6d49e166859a0bba238b03137b0f1f73"/><file name="system.xml" hash="139d172df653dd92eee3941757638d32"/></dir><dir name="sql"><dir name="stock_setup"><file name="mysql4-upgrade-1.0.4-1.1.0.php" hash="87e0b37603c2906edc6fb905f59d6c4e"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="stockbase"><file name="config.phtml" hash="62a961154f59879c7b5ee84921408db6"/></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Divide_Stockbase.xml" hash="57143df3ed1cd2598bf5c928f625da91"/></dir></target><target name="magelocale"><dir name="en_US"><file name="Divide_Stockbase.csv" hash="148597a20354efcd0e9b4c6e2c951467"/></dir><dir name="nl_NL"><file name="Divide_Stockbase.csv" hash="c5fdf87abc7957bb5145df6432cda7fc"/></dir></target></contents>
30
  <compatible/>
31
  <dependencies><required><php><min>5.4.0</min><max>6.0.0</max></php></required></dependencies>
32
  </package>