-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.php
743 lines (629 loc) · 32.4 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<?php
/*
Utilitats genèriques API prestashop
*/
require_once("configuration.php");
require_once("DBMySQL.php");
require_once('PSWebServiceLibrary.php');
/**
* @package PrestaShopWebservice
*/
class Utils
{
/**
* VARIABLES I CONSTANTS
*/
/* Variables base de dades */
public $myDB;
public $myDBPS;
/* Variables de classe */
protected $webService;
protected $idProducte;
protected $idFabricant;
/* Mode debut */
protected $debug = False;
/**
* MÈTODES
*/
function __construct($debug = False)
{
$this->debug = $debug;
if(!isset($this->myDB))
{
$this->myDB = new MySQL();
$this->webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, $this->debug);
}
if(!isset($this->myDBPS))
{
$this->myDBPS = new MySQLPS();
}
}
/* Obtenir productes a tractar */
public function nousProductes($timestampACercar){
$consulta = "SELECT * FROM icgps.icg_ps_producte WHERE flag_actualitzat = 1";
return $this->myDB->consulta($consulta);
}
/* Obtenir productes a tractar */
public function nousStocks($timestampACercar){
$consulta = "SELECT * FROM icgps.icg_ps_stocks WHERE flag_actualitzat = 1 AND ps_producte_atribut > 0 ";
return $this->myDB->consulta($consulta);
}
/* Obtenir productes a tractar */
public function nousPreus($timestampACercar){
$consulta = "SELECT * FROM icgps.icg_ps_preus WHERE flag_actualitzat = 1 AND ps_producte_atribut > 0 limit 200";
return $this->myDB->consulta($consulta);
}
/* Obtenir productes a tractar */
public function totsProductesPS(){
$consulta = "SELECT DISTINCT ps_producte FROM icgps.icg_ps_producte WHERE ps_producte != 0";
return $this->myDB->consulta($consulta);
}
/* Obtenir combinacions a tractar */
public function totsProductesPSSenseAtribut(){
$consulta = "SELECT ps_producte, icg_producte, icg_color, icg_talla, ean13 FROM icgps.icg_ps_producte WHERE ps_producte_atribut = -1";
return $this->myDB->consulta($consulta);
}
/**
BLOC FABRICANT
*/
/* Consultar si existeix fabricant a PS */
public function existeixFabricant($idFabricantICG){
$consulta = "SELECT * FROM icgps.icg_ps_fabricant WHERE icg_fabricant = $idFabricantICG";
$result_ps_fabricant = $this->myDB->consulta($consulta);
if( $this->myDB->num_rows($result_ps_fabricant) > 0 ){
//echo "El fabricant existex, no cal crear-lo <br>\n";
$row_fabricant = $this->myDB->fetch_array($result_ps_fabricant);
return $row_fabricant['ps_fabricant'];
}else{
return false;
}
}
/* Crear fabricant */
public function crearFabricant($idFabricantICG, $nomFabricantICG){
$idFabricantPS = self::existeixFabricant($idFabricantICG);
if(!$idFabricantPS){
$optCreate = array('resource' => 'manufacturers');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/manufacturers?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
$resourceCreate->name = $this->encodeToUtf8($nomFabricantICG);
$resourceCreate->active = 1;
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
$idFabricantPS = $xmlResponse->manufacturer->id;
if($this->myDB->consulta("INSERT INTO icgps.icg_ps_fabricant (ps_fabricant, icg_fabricant) VALUES ($idFabricantPS,$idFabricantICG)")){
//echo "Fabricant $idFabricantICG:$nomFabricantICG creat correctament al Prestashop amb num $idFabricantPS <br>\n";
}
}
return $idFabricantPS;
}
/**
BLOC PRODUCTE
*/
/* Consultar si existeix producte a PS mirant la nostra taula */
public function existeixProducte($idProducteICG){
$consulta = "SELECT * FROM icgps.icg_ps_producte WHERE icg_producte = $idProducteICG";
$result_ps_producte = $this->myDB->consulta($consulta);
if( $this->myDB->num_rows($result_ps_producte) > 0 ){
$row_producte = $this->myDB->fetch_array($result_ps_producte);
//echo "El producte ".$row_producte['ps_producte']." existex, no cal crear-lo <br>\n";
return $row_producte['ps_producte'];
}else{
return false;
}
}
/* Consultar si existeix un producte a PS mirant remotament a PS */
public function existeixRealmentProducte($idProductPS){
$opt['resource'] = 'products';
$opt['id'] = $idProductPS;
//echo "Producte PS: ".$idProductPS;
try{
$xml = $this->webService->head($opt);
return True;
}catch (Exception $e){
return False;
}
}
public function crearProducte($row_producte,$idFabricantPS){
$idProductePS;
$optCreate = array('resource' => 'products');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/products?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
$resourceCreate->name->language[0] = $this->encodeToUtf8($row_producte['descripcio']);
$resourceCreate->name->language[1] = $this->encodeToUtf8($row_producte['descripcio']);
$resourceCreate->name->language[2] = $this->encodeToUtf8($row_producte['descripcio']);
if(isset($idFabricantPS)){
$resourceCreate->id_manufacturer = $idFabricantPS;
}
$resourceCreate->price = 0;
$resourceCreate->id_category_default = ICG_CATEGORY;
$resourceCreate->associations->categories->category->id = ICG_CATEGORY;
$resourceCreate->active = 1;
$resourceCreate->id_shop_default = 1;
$resourceCreate->available_for_order = 1;
$resourceCreate->show_price = 1;
$resourceCreate->id_tax_rules_group = 1;
$resourceCreate->minimal_quantity = 1;
$resourceCreate->state = 1;
if($row_producte['icg_reference']){
$resourceCreate->reference = $row_producte['icg_reference'];
}else{//Legacy
$resourceCreate->reference = $row_producte['icg_producte'];
}
//$resourceCreate->advanced_stock_management = 1;
//TODO: Convert àéï...
$stringURL = $this->creaUrlLink($row_producte['descripcio']); // Converts spaces to dashes
$resourceCreate->link_rewrite->language[0] = $stringURL;
$resourceCreate->link_rewrite->language[1] = $stringURL;
$resourceCreate->link_rewrite->language[2] = $stringURL;
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
$idProductePS = $xmlResponse->product->id;
if($this->myDB->consulta("UPDATE icgps.icg_ps_producte SET ps_producte = ".$idProductePS.",timestamp = '".date("Y-m-d H:i:s")."' WHERE icg_producte = ".$row_producte['icg_producte'])){
//echo "Producte ".$row_producte['icg_producte']." creat correctament a Prestashop amb num $idProductePS i el idFabricant $idFabricantPS <br>\n";
}
return $idProductePS;
}
/**
BLOC GRUPS TALLA/COLOR
*/
public function crearGrupTallaColor($row_producte, $idProductePS, $tipus){
$idGrupTallaColor;
$optCreate = array('resource' => 'product_options');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/product_options?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
$resourceCreate->name->language[0] = $idProductePS."_".$tipus;
$resourceCreate->name->language[1] = $idProductePS."_".$tipus;
$resourceCreate->name->language[2] = $idProductePS."_".$tipus;
$resourceCreate->public_name->language[0] = $this->encodeToUtf8($row_producte['descripcio'])." ".$tipus;
$resourceCreate->public_name->language[1] = $this->encodeToUtf8($row_producte['descripcio'])." ".$tipus;
$resourceCreate->public_name->language[2] = $this->encodeToUtf8($row_producte['descripcio'])." ".$tipus;
$resourceCreate->group_type = "select";
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
$idGrupTalla = $xmlResponse->product_option->id;
//echo "El grup talla $idGrupTalla del producte PS $idProductePS s'ha creat correctament <br>\n";
return $idGrupTalla;
}
/* Desar grups Talla Color d'un producte */
public function desarProducteTallaColor($idProductePS,$idGrupTalla,$idGrupColor){
try{
$resultat = $this->myDB->consulta("INSERT INTO icgps.ps_producte_t_c (ps_producte, ps_grup_talla, ps_grup_color) VALUES ($idProductePS,$idGrupTalla,$idGrupColor)");
} catch (Exception $e){
echo "Error: No s'ha pogut crear entrada ProducteTallaColor per: $idProductePS,$idGrupTalla,$idGrupColor <br>\n";
}
}
/*
A partir d'un ID d'atribut, consulta el nom en llengua ICG = 4
*/
public function obtenirNomAtribut($id){
//echo "obtenirNomAtribut <br>\n";
$opt['resource'] = 'product_option_values';
$opt['id'] = $id;
$xml = $this->webService->get($opt);
$resources = $xml->product_option_value->name;
foreach($resources->language as $language){
if($language['id'] == PS_LANG_ICG){
//echo "Obtenim el valor: ".$language." <br>\n";
return $language[0];
}
}
}
/* Consultar si existeix un atribut dins un grup d'atributs */
public function existeixAtribut($idGrup,$nomAtribut){
//echo "Dins grup $idGrup existeix atribut: $nomAtribut <br>\n";
$opt['resource'] = 'product_options';
$opt['id'] = $idGrup;
$xml = $this->webService->get($opt);
foreach($xml->product_option->associations->product_option_values->product_option_value as $valor){
$nomActual = self::obtenirNomAtribut($valor->id);
//echo "comparcio ($nomActual) amb ($nomAtribut)";
if($nomActual == $nomAtribut){
return $valor->id;
}
}
return false;
}
/* Inserir talla */
public function inserirAtribut($row_producte, $idGrupAtribut, $nomAtribut){
//echo "Dins inserir atribut: $idGrupAtribut, $nomAtribut <br>\n";
$idAtribut = self::existeixAtribut($idGrupAtribut,$nomAtribut);
//echo "Existeix atribuit? ".$idAtribut."<br>";
if(!$idAtribut){
//echo $idGrupAtribut." - ".$nomAtribut." no existeixen, anem a inserir-los";
$optCreate = array('resource' => 'product_option_values');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/product_option_values?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
$resourceCreate->id_attribute_group = $idGrupAtribut;
$resourceCreate->name->language[0] = $this->encodeToUtf8($nomAtribut);
$resourceCreate->name->language[1] = $this->encodeToUtf8($nomAtribut);
$resourceCreate->name->language[2] = $this->encodeToUtf8($nomAtribut);
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
$idAtribut = $xmlResponse->product_option_value->id;
//echo "El atribut $idAtribut amb nom $nomAtribut s'ha creat correctament en el grup $idGrupAtribut <br>\n";
}else{
//echo "El atribut $idAtribut amb nom $nomAtribut ja estava creada correctament en el grup $idGrupAtribut <br>\n";
}
return $idAtribut;
}
/* Obtenir productes a tractar */
public function getGrup($idProducte, $tipus){
$consulta = "SELECT * FROM icgps.ps_producte_t_c WHERE ps_producte = ".$idProducte;
//echo "Consulta $consulta <br>\n";
$result = $this->myDB->consulta($consulta);
$row_producte = $this->myDB->fetch_array($result);
return $row_producte["$tipus"];
}
public function canviVisibilitatProducte($ps_producte, $ps_producte_atribut, $new_active){
//echo "obtenirNomAtribut <br>\n";
$optUpdate['resource'] = 'products';
$optUpdate['id'] = $ps_producte;
$xmlUpdate = 0;
try{
$xmlUpdate = $this->webService->get($optUpdate);
}catch (Exception $e){
//La combinació no existeix a Prestashop, tenim dades incorrectes a la BD d'integració
echo $e->getMessage();
print("<pre>".print_r($optUpdate,true)."</pre>");
print("<pre>".print_r($producte,true)."</pre>");
return false;
}
$active = $xmlUpdate->product->active;
if($active == $new_active){
echo "No cal fer res a PS \n";
if(!$new_active){
$new_ps_producte = intval($ps_producte) * -1;
$this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
}
return true;
} else{
$resourceUpdate = $xmlUpdate->children()->children();
//$new_active 0 is inactve, 1 is active
$resourceUpdate->active = $new_active;
unset($resourceUpdate->manufacturer_name);
unset($resourceUpdate->quantity);
//Enviar XML
$optUpdate['putXml'] = $xmlUpdate->asXML();
$optUpdate['id'] = $ps_producte;
try{
$xmlResponse = $this->webService->edit($optUpdate);
}catch (Exception $e){
echo $e->getMessage();
print("<pre>".print_r($optUpdate,true)."</pre>");
print("<pre>".print_r($producte,true)."</pre>");
return false;
}
$new_ps_producte = intval($ps_producte) * -1;
$this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$new_ps_producte.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte);
return true;
}
}
public function actualitzarReferencia($ps_producte, $icg_reference){
//echo "obtenirNomAtribut <br>\n";
$optUpdate['resource'] = 'products';
$optUpdate['id'] = $ps_producte;
$xmlUpdate = 0;
try{
$xmlUpdate = $this->webService->get($optUpdate);
}catch (Exception $e){
//La combinació no existeix a Prestashop, tenim dades incorrectes a la BD d'integració
echo $e->getMessage();
print("<pre>".print_r($optUpdate,true)."</pre>");
print("<pre>".print_r($producte,true)."</pre>");
return false;
}
$reference = $xmlUpdate->product->reference;
if($reference <> $icg_reference){
$resourceUpdate = $xmlUpdate->children()->children();
echo $resourceUpdate->reference;
echo " - ";
echo $icg_reference;
//$new_active 0 is inactve, 1 is active
$resourceUpdate->reference = $icg_reference;
echo $resourceUpdate->reference;
unset($resourceUpdate->manufacturer_name);
unset($resourceUpdate->quantity);
//Enviar XML
$optUpdate['putXml'] = $xmlUpdate->asXML();
$optUpdate['id'] = $ps_producte;
$xmlResponse = $this->webService->edit($optUpdate);
}
return true;
}
public function actualitzarMarca($ps_producte, $codi_marca, $nom_marca){
//echo "obtenirNomAtribut <br>\n";
$optUpdate['resource'] = 'products';
$optUpdate['id'] = $ps_producte;
$xmlUpdate = 0;
try{
$xmlUpdate = $this->webService->get($optUpdate);
}catch (Exception $e){
//La combinació no existeix a Prestashop, tenim dades incorrectes a la BD d'integració
echo $e->getMessage();
print("<pre>".print_r($optUpdate,true)."</pre>");
print("<pre>".print_r($producte,true)."</pre>");
return false;
}
$id_manufacturer = $xmlUpdate->product->id_manufacturer;
$codi_marca = self::crearFabricant($codi_marca, $nom_marca);
if($id_manufacturer <> $codi_marca){
$resourceUpdate = $xmlUpdate->children()->children();
echo $resourceUpdate->id_manufacturer;
echo " - ";
echo $icg_reference;
//$new_active 0 is inactve, 1 is active
$resourceUpdate->id_manufacturer = $codi_marca;
echo $resourceUpdate->id_manufacturer;
unset($resourceUpdate->manufacturer_name);
unset($resourceUpdate->quantity);
//Enviar XML
$optUpdate['putXml'] = $xmlUpdate->asXML();
$optUpdate['id'] = $ps_producte;
$xmlResponse = $this->webService->edit($optUpdate);
}
return true;
}
/**
BLOC COMBINACIONS icgps.ps_producte_atribut
*/
/* Inserir combinacio */
public function inserirCombinacio($idProductePS, $idTalla, $idColor, $row_producte){
$optCreate = array('resource' => 'combinations');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/combinations?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
$resourceCreate->id_product = $idProductePS;
$resourceCreate->minimal_quantity = 1;
$resourceCreate->ean13 = $row_producte['ean13'];
$resourceCreate->associations->product_option_values->product_option_value->id = $idTalla;
$product_option_value = $resourceCreate->associations->product_option_values->addChild('product_option_value');
$product_option_value->addChild('id',$idColor);
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
$idCombinacio = $xmlResponse->combination->id;
if($this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$idProductePS.", ps_producte_atribut = ".$idCombinacio." WHERE icg_producte = ".$row_producte['icg_producte']." AND icg_color = '".$this->encodeToUtf8($row_producte['icg_color'])."' AND icg_talla = '".$this->encodeToUtf8($row_producte['icg_talla'])."'")){
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$idProductePS.", ps_producte_atribut = ".$idCombinacio.", flag_actualitzat = 1 WHERE icg_producte = ".$row_producte['icg_producte']." AND icg_color = '".$this->encodeToUtf8($row_producte['icg_color'])."' AND icg_talla = '".$this->encodeToUtf8($row_producte['icg_talla'])."'");
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = ".$idProductePS.", ps_producte_atribut = ".$idCombinacio.", flag_actualitzat = 1 WHERE icg_producte = ".$row_producte['icg_producte']." AND icg_color = '".$this->encodeToUtf8($row_producte['icg_color'])."' AND icg_talla = '".$this->encodeToUtf8($row_producte['icg_talla'])."'");
//echo "inserirCombinacio: La combinacio $idCombinacio ($idTalla,$idColor) s'ha creat correctament <br>\n";
}
return $idCombinacio;
}
public function existeixCombinacioIntegracio($idCombinacio){
$sql = "SELECT * FROM `icg_ps_producte` where `ps_producte_atribut` = ".$idCombinacio;
if($this->myDB->num_rows($this->myDB->consulta($sql)) > 0){
return True;
}
return False;
}
/* Consultar si existeix una combinació dins d'un producte */
public function existeixCombinacio($idProductePS,$nomTalla, $nomColor){
//echo "Dins grup $idPro existeix atribut: $nomAtribut <br>\n";
$opt['resource'] = 'products';
$opt['id'] = $idProductePS;
$xml = $this->webService->get($opt);
#echo "* PRODUCTE ".$idProductePS." TALLA ".$nomTalla." COLOR ".$nomColor."\n";
foreach($xml->product->associations->combinations->combination as $idCombinacio){
if(self::existeixCombinacioIntegracio($idCombinacio->id)){
#echo "Ens hem estalviat consulta ".$idCombinacio->id."\n";
continue;
}else{
#echo "Somhi".$idCombinacio->id."\n";
}
$opt2['resource'] = 'combinations';
$opt2['id'] = $idCombinacio->id;
#echo "Provem la combinació: ".$idCombinacio->id."\n";
$xml2 = $this->webService->get($opt2);
$colorTrobat = false;
$tallaTrobada = false;
foreach($xml2->combination->associations->product_option_values->product_option_value as $idProductOption){
$nomActual = self::obtenirNomAtribut($idProductOption->id);
#echo "- Provem: ".$nomActual."\n";
if($nomActual == $nomTalla){
$tallaTrobada = True;
#echo "- - Hem trobat talla: ".$nomActual."\n";
}
if($nomActual == $nomColor){
$colorTrobat = True;
#echo "- - Hem trobat color: ".$nomActual."\n";
}
}
if($tallaTrobada & $colorTrobat){
$colorTrobat = false;
$tallaTrobada = false;
return $idCombinacio->id;
}
}
return false;
}
public function eliminarCombinacio($ps_producte, $ps_producte_atribut){
//$row_producte['ps_producte'],$row_producte['ps_producte_atribut']);
$optUpdate = array('resource' => 'combinations');
$optUpdate['id'] = $ps_producte_atribut;
$xmlUpdate = 0;
try{
$xmlUpdate = $this->webService->get($optUpdate);
}catch (Exception $e){
//La combinació no existeix a Prestashop, tenim dades incorrectes a la BD d'integració
echo $e->getMessage();
print("<pre>".print_r($optUpdate,true)."</pre>");
print("<pre>".print_r($ps_producte,true)."</pre>");
print("<pre>".print_r($ps_producte_atribut,true)."</pre>");
return false;
}
$url = "https://casapiera.com/versio17/api/combinations/".$ps_producte_atribut."?ps_method=DELETE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
try{
curl_exec($ch);
}catch (Exception $e){
echo $e->getMessage();
return false;
}
$new_ps_producte_atribut = intval($ps_producte_atribut) * -1;
$this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte_atribut = ".$new_ps_producte_atribut.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte." AND ps_producte_atribut =".$ps_producte_atribut);
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte_atribut = ".$new_ps_producte_atribut.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte." AND ps_producte_atribut =".$ps_producte_atribut);
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte_atribut = ".$new_ps_producte_atribut.", flag_actualitzat = 0 WHERE ps_producte = ".$ps_producte." AND ps_producte_atribut =".$ps_producte_atribut);
}
public function flagActualitzatProducte($row_producte){
$this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', flag_actualitzat = 0 WHERE id = ".$row_producte['id']);
}
/**
BLOC STOCK
*/
/* Canviar stock */
public function actualitzarStock($idProductePS, $idAtributPS, $stock){
$result = $this->myDBPS->consulta("UPDATE ".DB_NAME_PS.".ps_stock_available SET quantity = ".$stock." WHERE id_product = ".$idProductePS." AND id_product_attribute =".$idAtributPS);
//Per a cada nou stock
if($result)
{
//echo "El stock del producte ($idProductePS _ $idAtributPS) s'ha actualitzat correctament a $stock";
}else{
echo "No s'ha pogut actualitzar stock: ".idProductePS." - ".idAtributPS;
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', flag_actualitzat = 0 WHERE ps_producte = ".$idProductePS."ps_producte_atribut = ".$idAtributPS);
}
}
public function flagActualitzatStock($row_producte){
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', flag_actualitzat = 0 WHERE id = ".$row_producte['id']);
}
/**
BLOC PREUS
*/
/* Actualitzar preu */
public function actualitzarPreus($producte){
//Obtenir XML
$optUpdate = array('resource' => 'combinations');
$optUpdate['id'] = $producte['ps_producte_atribut'];
$xmlUpdate = 0;
try{
$xmlUpdate = $this->webService->get($optUpdate);
}catch (Exception $e){
//La combinació no existeix a Prestashop, tenim dades incorrectes a la BD d'integració
//echo $e->getMessage();
//print("<pre>".print_r($optUpdate,true)."</pre>");
//print("<pre>".print_r($producte,true)."</pre>");
echo $producte['ps_producte_atribut'].",";
return false;
}
$resourceUpdate = $xmlUpdate->children()->children();
//Modificar XML
$resourceUpdate->price = $producte['pvp_siva'];
//Enviar XML
$optUpdate['putXml'] = $xmlUpdate->asXML();
$optUpdate['id'] = $producte['ps_producte_atribut'];
$xmlResponse = $this->webService->edit($optUpdate);
return true;
}
public function existeixDescompte($producte){
//echo "dins existeix descompte <br>\n";
$result = $this->myDB->consulta("SELECT * FROM icgps.ps_producte_oferta WHERE ps_producte = ".$producte['ps_producte']);
if($this->myDB->num_rows($result) > 0 ){
$result_ps_producte = $this->myDB->fetch_array($result);
//echo "Hem trobat el preu del producte ".$producte['ps_producte']." es el ".$result_ps_producte['specific_price']." <br>\n";
return $result_ps_producte['specific_price'];
}else{
//echo "NO hem trobat el preu del producte ".$producte['ps_producte']." <br>\n";
return false;
}
}
/* Afegir descompte */
public function afegirDescompte($producte){
//echo "dins afegir descompte <br>\n";
$idDescompte = self::existeixDescompte($producte);
if($idDescompte){
//echo "Actualitzem el $idDescompte <br>\n";
//Obtenir XML
$optUpdate = array('resource' => 'specific_prices');
$optUpdate['id'] = $idDescompte;
$xmlUpdate = $this->webService->get($optUpdate);
$resourceUpdate = $xmlUpdate->children()->children();
//Omplir camps
$resourceUpdate->reduction = $producte['dto_percent']/100;
//Enviar XML
$optUpdate['putXml'] = $xmlUpdate->asXML();
$optUpdate['id'] = $idDescompte;
$xmlResponse = $this->webService->edit($optUpdate);
}else{
//echo "NO Actualitzem el $idDescompte. L'afegim de nou <br>\n";
//Obtenir XML
$optCreate = array('resource' => 'specific_prices');
$xmlCreate = $this->webService->get(array('url' => PS_SHOP_PATH.'/api/specific_prices?schema=blank'));
$resourceCreate = $xmlCreate->children()->children();
//Omplir camps
$resourceCreate->id_product = $producte['ps_producte'];
$resourceCreate->id_product_attribute = $producte['ps_producte_atribut'];
$resourceCreate->id_shop = 0;
$resourceCreate->id_cart = 0;
$resourceCreate->id_currency = 0;
$resourceCreate->id_country = 0;
$resourceCreate->id_group = 0;
$resourceCreate->id_customer = 0;
$resourceCreate->price = 0;
$resourceCreate->reduction_tax = 0;
$resourceCreate->from = '0000-00-00 00:00:00';
$resourceCreate->to = '0000-00-00 00:00:00';
$resourceCreate->reduction = $producte['dto_percent']/100;
$resourceCreate->reduction_type = 'percentage';
$resourceCreate->from_quantity = 1;
//Enviar XML
$optCreate['postXml'] = $xmlCreate->asXML();
$xmlResponse = $this->webService->add($optCreate);
//Desar codi descompte del producte
$idDescompte = $xmlResponse->specific_price->id;
$this->myDB->consulta("INSERT INTO icgps.ps_producte_oferta (ps_producte, specific_price) VALUES (".$producte['ps_producte'].",$idDescompte)");
}
}
public function flagActualitzatPreus($row_producte){
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET flag_actualitzat = 0 WHERE id = ".$row_producte['id']);
}
public function flagNoActualitzatPreus($row_producte){
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET flag_actualitzat = 2 WHERE id = ".$row_producte['id']);
}
public function parseja($response){
return $this->webService->parseXML($response);
}
/* Utils genèrics */
public function encodeToUtf8($string) {
$string = preg_replace("/{|}/","",$string);
$string = preg_replace("/'/","´",$string);
return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
}
public function creaUrlLink($s) {
$s = preg_replace("/á|à|â|ã|ª|ä/","a",$s);
$s = preg_replace("/Á|À|Â|Ã|Ä/","A",$s);
$s = preg_replace("/é|è|ê|ë/","e",$s);
$s = preg_replace("/É|È|Ê|Ë/","E",$s);
$s = preg_replace("/í|ì|î|ï/","i",$s);
$s = preg_replace("/Í|Ì|Î|Ï/","I",$s);
$s = preg_replace("/ó|ò|ô|õ|º|ö/","o",$s);
$s = preg_replace("/Ó|Ò|Ô|Õ|Ö/","O",$s);
$s = preg_replace("/ú|ù|û|ü/","u",$s);
$s = preg_replace("/Ú|Ù|Û|Ü/","U",$s);
$s = str_replace("ñ","n",$s);
$s = str_replace("Ñ","N",$s);
$s = str_replace("ç","c",$s);
$s = str_replace("Ç","C",$s);
$s = strtolower($s);
$s = str_replace("'", "" , $s);
$s = str_replace('%20', ' ', $s);//$s = str_replace(" ","_",$s);
$s = preg_replace('~[^\pL0-9_]+~u', '-', $s);
$s = trim($s, "-");
$s = iconv("utf-8", "us-ascii//TRANSLIT", $s);
$s = preg_replace('~[^-a-z0-9_]+~', "", $s);
return $s;
}
public function netejaIntegracioPS($idProductePS){
if($this->myDB->consulta("UPDATE icgps.icg_ps_producte SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = 0, ps_producte_atribut = 0, flag_actualitzat = 1 WHERE ps_producte = ".$idProductePS)){
$this->myDB->consulta("UPDATE icgps.icg_ps_stocks SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = 0, ps_producte_atribut = 0, flag_actualitzat = 1 WHERE ps_producte = ".$idProductePS);
$this->myDB->consulta("UPDATE icgps.icg_ps_preus SET timestamp = '".date("Y-m-d H:i:s")."', ps_producte = 0, ps_producte_atribut = 0, flag_actualitzat = 1 WHERE ps_producte = ".$idProductePS);
$this->myDB->consulta("DELETE FROM icgps.ps_producte_t_c WHERE ps_producte = ".$idProductePS);
$this->myDB->consulta("DELETE FROM icgps.ps_producte_oferta WHERE ps_producte = ".$idProductePS);
echo "El producte PS ".$idProductePS." no exisitia i l'hem marcat per recrearse<br>\n";
}
}
}
?>