· 6 years ago · Jul 17, 2019, 05:50 AM
1<?php
2class MAT extends Presta15Conn {
3
4 function __construct() {
5
6 $this->name = get_class($this);
7 if (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'imported'")) != 1) {
8 echo DB_HOSTNAME ."/". DB_DATABASE ."/".DB_USERNAME ."/". DB_PASSWORD . "<br><br>DB Tables installed.<br>" ;
9 $this->install();
10 }
11 $this->category_mapping = Settings::get_category_mappings($this->name);
12 $this->settings = Settings::get_import_settings($this->name);
13 }
14
15 public function process_xml($type = "FOR_INSERT") {
16 global $logger, $db;
17 $this->store_products = $this->get_imported($this->name);
18 $this->import_settings = Settings::get_import_settings($this->name);
19
20 foreach(explode("\n", $this->import_settings['DISABLEMARGINFORSKU']) as $tmp) {
21 $DISABLEMARGINFORSKU[trim($tmp)] = true;
22 }
23
24 foreach(explode("\n", $this->import_settings['DONTUPDATEDESC']) as $tmp) {
25 $this->DONTUPDATEDESC[trim($tmp)] = true;
26 }
27
28 foreach(explode("\n", $this->import_settings['DEACTIVATESKU']) as $tmp) {
29 $DEACTIVATESKU[trim($tmp)] = true;
30 }
31
32 foreach(explode("\n", $this->import_settings['FIXPRICE']) as $tmp) {
33 $tmp2 = explode(":", $tmp);
34 @$FIXPRICE[trim($tmp2[0])] = @trim($tmp2[1]);
35 }
36 foreach(explode("\n", $this->import_settings['FIXQTY']) as $tmp) {
37 $tmp2 = explode(":", $tmp);
38 @$FIXQTY[trim($tmp2[0])] = @trim($tmp2[1]);
39 }
40
41// dump($FIXQTY);exit;
42
43
44 if ($type=="FOR_UPDATE") {echo ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; }
45
46 $fulldataset = file_get_contents(FEED_PATH ."/" . $this->name . ".xml");
47 $xml = new SimpleXMLElement($fulldataset);
48 $items_total = 0;
49
50 $StoreCategoryMapping = $this->getStoreCategoryMapping();
51// dump($StoreCategoryMapping);exit;
52
53 foreach($db->query("SELECT * FROM "._DB_PREFIX_."product") as $rr) {
54 $this->CAT2PROD[$rr['id_product']]=$rr['id_category_default'];
55 }
56 /*
57 //$FIXPRICE = array();
58 foreach(explode("\n", file_get_contents(TXT_PRICES)) as $row) {
59
60 $tmp2 = explode(":", $row);
61 $FIXPRICE[trim($tmp2[0])] = trim($tmp2[1]);
62
63 } */
64
65// dump($FIXPRICE);exit;
66// dump($this->CAT2PROD);exit;
67
68 foreach($xml->Products->children() as $a) {
69// dump($a);exit;
70
71 $deliverydate[(string) $a->DELIVERY_DATE] = true;
72
73
74 $items_total++;
75 $item = new stdClass();
76 $item->sku = (string) $a->Code;
77
78 if (isset($DEACTIVATESKU[$item->sku]))
79 continue;
80
81 $store_product_id = @$this->store_products[$item->sku];
82 if (($type=="FOR_INSERT") && $store_product_id) continue;
83 if (($type=="FOR_UPDATE") && !$store_product_id) continue;
84
85 $item->title = strip_tags ( (string) $a->Nazev);
86
87 $item->available_later = "";
88 $item->available_now = "";;
89 $item->quantity = (int) $a->Mnozstvi;
90
91 if (isset($FIXQTY[$item->sku])) {
92 $item->quantity = $FIXQTY[$item->sku];
93// dump($item);exit;
94 }
95 $item->status = 1;
96 $item->active = 1;
97
98 if ($item->status == 1) {
99 $item->available_later = "";
100 }
101
102 if ($item->status == 0) {
103 $item->available_later = "";
104 }
105
106 if (false) {
107 $item->category_XML = ((string) $a->Kategorie);
108
109 if (@$this->category_mapping[$item->category_XML])
110 foreach ($this->category_mapping[$item->category_XML] as $catstore) {
111
112
113 $f = $this->get_category_ancestors($catstore);
114 if (count($f) > 0)
115 foreach(@$f as $catanc)
116 if ($catanc > 2)
117 $item->category_ids[] = $catanc;
118
119
120 $item->category_ids[] = (int) $catstore;
121 }
122
123 if (!isset($item->category_ids)) {
124 continue;
125 }
126
127 unset($item->category_ids);
128 }
129
130 $item->category_ids[] = 7973;
131
132 // if ($type=="FOR_INSERT") {
133 // unset($item->category_ids);
134 // $item->category_ids[] = 3326;
135 // }
136 // if (@count($item->category_ids) == 0){
137 // @$this->unmapped[] = $item->sku;
138 // continue;
139// $item->category_ids[] = 2;
140 // }
141
142 $item->store_product_id = $store_product_id;
143 $item->manufacturer_sku = $item->sku;
144 $item->id_supplier = 1;
145
146 $item->desc_short = ImpLib::limitSentence((string) $a->Popis, 160);
147 $item->desc_long = (string) $a->Popis;
148 if ($item->desc_long == "") {
149 $item->desc_long = $item->desc_short;
150 }
151
152 if ($item->desc_long == "") {
153 $item->desc_long = $item->title;
154 $item->desc_short = $item->title;
155 }
156
157 foreach($a->EANs as $ean) {
158 $item->ean_upc = (string) $ean->EAN;
159 break;
160 }
161
162 $item->id_tax_rules_group = 1;
163// echo $item->id_tax_rules_group;
164 $item->ecotax = 0;
165
166 $vyprodej= "";
167 $item->on_sale=0;
168
169 $margin = 0;
170// $margin = @$this->settings['MARGIN'][$item->category_XML];
171
172// dump($this->CAT2PROD);
173// exit;
174// dump($StoreCategoryMapping);exit;
175
176
177 if (isset($this->CAT2PROD[$item->store_product_id]))
178 $product_Category = $this->CAT2PROD[$item->store_product_id];
179
180 if (isset($product_Category)) {
181 if (isset($StoreCategoryMapping[$product_Category])) {
182 $margin = $StoreCategoryMapping[$product_Category];
183// dump($margin);exit;
184 }
185
186
187 }
188
189 $item->raw_price = (float) $a->doporucenaMOC;
190 $item->price = $item->raw_price;
191
192 $item->price = $item->raw_price / 1.21;
193
194
195 if ($item->price==0) {
196 $logger->LogCron(" - Zero price " . $item->sku);
197 continue;
198 }
199 $item->wholesale_price = (float) $a->VaseCenaBezDPH;
200
201 $item->margin = $margin + $this->settings['GLOBALMARGIN'];
202
203 $item->margin = $item->margin=="" ? 0 : $item->margin;
204 if (isset($DISABLEMARGINFORSKU[$item->sku])) {
205 $item->margin = 0;
206 }
207
208
209// echo $item->margin;exit;
210 $item->price = $item->price * (1 + ($item->margin / 100));
211
212 if ($item->margin < 0){
213 // dump($item);exit;
214 }
215// dump($FIXPRICE);exit;
216 if (isset($FIXPRICE[$item->sku])) {
217 $item->price = $FIXPRICE[$item->sku];
218 $item->price = $item->price / 1.21;
219 $item->margin = " ** Pevně stanovená cena ** ";
220 }
221
222 $item->price = round($item->price, 2);
223 if (@$_REQUEST['MAT-pix?range']=="") {
224 if ($type=="FOR_UPDATE") { // {$item->title} |
225 echo "{$item->sku} | {$item->store_product_id} | FEED_CENA={$item->raw_price} + {$item->margin}% => MOC (bez) =".round($item->price, 2).")
226 qty: {$item->quantity} <br>";
227
228 }
229 }
230 //$article['price_tax'] = round($article['price'] * (1 + $tax / 100), 2);
231// print_r($a->zoom);exit;
232 $item->images[] = (string) $a->ImgURL;
233
234
235 $man = "";
236// $item->id_manufacturer = $this->get_manufacturer_id(addslashes($man));
237 $item->manufacturer = addslashes($man);
238 $attributes = array();
239 $item->supplier_reference = "";
240 $item->shipping_fee_world = "";
241 $item->online_only = "";
242 $item->UNSPSC = "";
243 $item->weight = (float) $a->Weight;
244 $item->length = "";
245 $item->depth = "";
246 $item->width = "";
247 $item->height = "";
248
249 $item->category_ids = @array_unique($item->category_ids);
250 $articles_final[$item->sku] = $item;
251
252 if ($type=="FOR_INSERT") {
253 if (@$RR++ > 70) {
254// dump($articles_final);exit;
255 break;
256 }
257 }
258
259 }
260 if ($type=="FOR_UPDATE") {
261// dump($articles_final);exit;
262 }
263// dump($deliverydate);exit;
264
265 $this->items_total = $items_total;
266 return @$articles_final;
267 }
268
269
270 public function download_feeds() {
271 //return true;
272 global $logger;
273 $file = FEED_PATH . "/" . $this->name . ".xml";
274 if (file_exists($file)) { $fdiff = (date("U") - filemtime($file)) / 3600; $not_exist = false; } else { $not_exist = true;}
275 if ( ( (@filesize($file) < 100) || ($fdiff > 10) ) || $not_exist ) {
276 $url = "http://mat-online.cz/export/3958becef1222baa335aa594bac566fd.xml";
277 ImpLib::download_file($url, $file);
278 $logger->LogCron("DOWNLOADED: $url => $file (".ImpLib::convert( filesize($file)).")");
279 }
280 return true;
281 }
282
283 public function get_category_mapping_tree() {
284 global $db;
285
286 $fulldataset = file_get_contents($file = FEED_PATH . "/" . $this->name . ".xml");
287 $xml = new SimpleXMLElement($fulldataset);
288
289 foreach($xml->Categories->children() as $a) {
290// $category[(int) $a->ParentId][(int) $a->id] = (string) $a->Nazev ;
291 $id_category = (int) $a->id;
292 $id_parent = (int) $a->ParentId;
293 (string) $a->Nazev;
294
295 if (!isset($supplier_categories_temp2[$id_category])) {
296 $supplier_categories_temp2[$id_category] = array(
297 'id_category' => $id_category,
298 'id_parent' => $id_parent,
299 'name' => (string) $a->Nazev
300 );
301 }
302
303 }
304
305 foreach ($supplier_categories_temp2 as $c) {
306 if ($c['id_parent'] != "") {
307 $categs[$c['id_category']] = $this->_get_category($c['id_parent'], $supplier_categories_temp2) . " > " . $c['name'];
308 } else {
309 $categs[$c['id_category']] = $c['name'];
310 }
311 }
312
313
314
315// dump($categs);exit;
316
317 $categs = array_unique($categs);
318 asort($categs);
319
320 foreach($categs as $id_category => $catname) {
321 $sub = "";
322 $exp = explode(" > ", $catname);
323 // echo $exp['2']. "<br>";
324
325 $id_new_category = ($id_category);
326 $category_mapping[$id_new_category] = array
327 (
328 'id' => $id_new_category,
329 'name' => $catname,
330 'fullpath' => $catname,
331 'sub' => $sub
332 );
333 }
334 $this->feed_category_counts = array();
335
336// dump($category_mapping);
337 return $category_mapping;
338 }
339
340 public function _get_category($id_parent, $supplier_categories_temp2) {
341 $c = @$supplier_categories_temp2[$id_parent] ;
342 if ($c!="") {
343 // dump($c);exit;
344 // echo $id_parent; dump($supplier_categories_temp2);
345
346 if ($c['id_parent'] != "0") {
347 return $this->_get_category($c['id_parent'], $supplier_categories_temp2) . " > " . $c['name'];
348 } else {
349 return $c['name'];
350 }
351 }
352 return "Ostatní";
353 }
354
355
356 public function install() {
357
358 mysql_query("
359 CREATE TABLE IF NOT EXISTS `import_settings` (
360 `id` int(11) NOT NULL AUTO_INCREMENT,
361 `supplier_id` varchar(50) NOT NULL,
362 `name` varchar(100) NOT NULL,
363 `value` longtext NOT NULL,
364 `supplier` varchar(100) NOT NULL,
365 PRIMARY KEY (`id`)
366 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
367 ");
368
369 mysql_query("CREATE TABLE IF NOT EXISTS `import_category_settings` (
370 `feed_category_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
371 `store_category_id` int(11) DEFAULT NULL,
372 `margin` int(11) DEFAULT NULL,
373 `supplier_id` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL
374 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
375 ") or die(mysql_error());
376
377 mysql_query("CREATE TABLE IF NOT EXISTS `imported` (
378 `id` int(11) NOT NULL AUTO_INCREMENT,
379 `sku` varchar(200) NOT NULL,
380 `shop_status` varchar(20) NOT NULL,
381 `store_product_id` int(11) NOT NULL,
382 `supplier` varchar(30) NOT NULL,
383 `manufacturer` varchar(30) NOT NULL,
384 `manufacturer_sku` varchar(30) NOT NULL,
385 `ean` varchar(30) NOT NULL,
386 `processed` tinyint(1) NOT NULL,
387 `date_add` datetime NOT NULL,
388 `date_update` datetime NOT NULL,
389 PRIMARY KEY (`id`),
390 UNIQUE `sku` (`sku`),
391 KEY `store_product_id` (`store_product_id`),
392 KEY `shop_status` (`shop_status`)
393 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
394 ") or die(mysql_error());
395
396 mysql_query("CREATE TABLE `import_product_settings` (
397 `sku` varchar(50) NOT NULL,
398 `imported` int(11) NOT NULL,
399 `category_feed_id` varchar(50) NOT NULL,
400 `supplier` varchar(20) NOT NULL,
401 UNIQUE KEY `sku_category_feed_id` (`sku`,`category_feed_id`),
402 KEY `imported` (`imported`),
403 KEY `category_feed_id` (`category_feed_id`)
404 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
405 ") or die(mysql_error());
406
407 mysql_query("CREATE TABLE `imported_attribute` (
408 `id_product_attribute` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
409 `reference` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
410`supplier` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
411 `processed` tinyint(1) DEFAULT '1',
412 PRIMARY KEY (`id_product_attribute`),
413 KEY `reference` (`reference`)
414 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
415 ");
416 mysql_query("CREATE TABLE IF NOT EXISTS `import_icecat` (
417 `id` int(11) NOT NULL AUTO_INCREMENT,
418 `sku` varchar(30) NOT NULL,
419 `store_product_id` varchar(30) NOT NULL,
420 `part_number` varchar(30) NOT NULL,
421 `vendor_id` varchar(200) NOT NULL,
422 `ean_upc` varchar(30) NOT NULL,
423 `icecat_state` varchar(25) NOT NULL,
424 `price` int(11) NOT NULL,
425 `date_add` datetime NOT NULL,
426 `date_update` datetime NOT NULL,
427 `shop_status` varchar(30) NOT NULL,
428 `date_update_shop` datetime NOT NULL,
429 `id_icecat` int(11) NOT NULL,
430 `id_lang` int(11) NOT NULL,
431 `icecat_specs` text NOT NULL,
432 PRIMARY KEY (`id`,`part_number`),
433 KEY `sku` (`sku`),
434 KEY `part_number` (`part_number`),
435 KEY `date_update_shop` (`date_update_shop`),
436 KEY `store_product_id` (`store_product_id`),
437 KEY `date_update` (`date_update`)
438 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
439 ");
440
441 mysql_query("DROP TABLE IF EXISTS `import_icecat_feature`;
442 CREATE TABLE `import_icecat_feature` (
443 `icecat_id_feature` int(11) NOT NULL,
444 `store_id_feature` int(11) NOT NULL,
445 KEY `icecat_id_feature` (`icecat_id_feature`),
446 KEY `store_id_feature` (`store_id_feature`)
447 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
448 ");
449
450 mysql_query("DROP TABLE IF EXISTS `import_icecat_feature`;
451 CREATE TABLE `import_icecat_feature` (
452 `icecat_id_feature` int(11) NOT NULL,
453 `store_id_feature` int(11) NOT NULL,
454 KEY `icecat_id_feature` (`icecat_id_feature`),
455 KEY `store_id_feature` (`store_id_feature`)
456 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
457 ");
458
459
460
461 }
462
463 public function getStoreCategoryMapping() {
464 global $b2b;
465 $settings = Settings::get_import_settings("MAT");
466 $tmp = array();
467 $STORE_CATEGORY_MARGIN = unserialize(@$settings['STORE_CATEGORY_MARGIN']);
468 if ($STORE_CATEGORY_MARGIN) {
469 if (count($STORE_CATEGORY_MARGIN) > 0) {
470 // dump($STORE_CATEGORY_MARGIN);exit;
471 foreach ($STORE_CATEGORY_MARGIN as $m => $v){
472 $tmp[$m] = $v;
473 //$tmp[$m]['store_id'] = $this->_feature_get_id($v['store_name']);
474 }
475 }
476 }
477 return $tmp;
478 }
479
480
481
482
483}