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