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