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