forked from Qloapps/QloApps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.php
6408 lines (5585 loc) · 241 KB
/
Product.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2017 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @deprecated 1.5.0.1
*/
define('_CUSTOMIZE_FILE_', 0);
/**
* @deprecated 1.5.0.1
*/
define('_CUSTOMIZE_TEXTFIELD_', 1);
class ProductCore extends ObjectModel
{
/** @var string Tax name */
public $tax_name;
/** @var string Tax rate */
public $tax_rate;
/** @var int Manufacturer id */
public $id_manufacturer;
/** @var int Supplier id */
public $id_supplier;
/** @var int default Category id */
public $id_category_default;
/** @var int default Shop id */
public $id_shop_default;
/** @var string Manufacturer name */
public $manufacturer_name;
/** @var string Supplier name */
public $supplier_name;
/** @var string Name */
public $name;
/** @var string Long description */
public $description;
/** @var string Short description */
public $description_short;
/** @var int Quantity available */
public $quantity = 0;
/** @var int Minimal quantity for add to cart */
public $minimal_quantity = 1;
/** @var bool allow order for multiple quantities */
public $allow_multiple_quantity;
/** @var bool max allowed quantities */
public $max_quantity;
/** @var int price calculation method */
public $price_calculation_method;
/** @var string available_now */
public $available_now;
/** @var string available_later */
public $available_later;
/** @var float Price in euros */
public $price = 0;
public $specificPrice = 0;
/** @var float Additional shipping cost */
public $additional_shipping_cost = 0;
/** @var float Wholesale Price in euros */
public $wholesale_price = 0;
/** @var bool on_sale */
public $on_sale = false;
/** @var bool online_only */
public $online_only = false;
/** @var string unity */
public $unity = null;
/** @var float price for product's unity */
public $unit_price;
/** @var float price for product's unity ratio */
public $unit_price_ratio = 0;
/** @var float Ecotax */
public $ecotax = 0;
/** @var string Reference */
public $reference;
/** @var string Supplier Reference */
public $supplier_reference;
/** @var string Location */
public $location;
/** @var string Width in default width unit */
public $width = 0;
/** @var string Height in default height unit */
public $height = 0;
/** @var string Depth in default depth unit */
public $depth = 0;
/** @var string Weight in default weight unit */
public $weight = 0;
/** @var string Ean-13 barcode */
public $ean13;
/** @var string Upc barcode */
public $upc;
/** @var string Friendly URL */
public $link_rewrite;
/** @var string Meta tag description */
public $meta_description;
/** @var string Meta tag keywords */
public $meta_keywords;
/** @var string Meta tag title */
public $meta_title;
/** @var bool Product statuts */
public $quantity_discount = 0;
/** @var bool Product customization */
public $customizable;
/** @var bool Product is new */
public $new = null;
/** @var int Number of uploadable files (concerning customizable products) */
public $uploadable_files;
/** @var int Number of text fields */
public $text_fields;
/** @var bool Product statuts */
public $active = true;
/** @var bool Product statuts */
public $redirect_type = '';
/** @var bool Product statuts */
public $id_product_redirected = 0;
/** @var bool Product available for order */
public $available_for_order = true;
// Room type required to buy
public $service_product_type;
// add product to cart automatically
public $auto_add_to_cart = false;
public $price_addition_type;
public $show_at_front;
/** @var string Object available order date */
public $available_date = '0000-00-00';
/** @var string Enumerated (enum) product condition (new, used, refurbished) */
public $condition;
/** @var bool Show price of Product */
public $show_price = true;
/** @var bool is the product indexed in the search index? */
public $indexed = 0;
/** @var string ENUM('both', 'catalog', 'search', 'none') front office visibility */
public $visibility;
/** @var string Object creation date */
public $date_add;
/** @var string Object last modification date */
public $date_upd;
/*** @var array Tags */
public $tags;
/**
* @var float Base price of the product
* @deprecated 1.6.0.13
*/
public $base_price;
public $id_tax_rules_group = 1;
/**
* We keep this variable for retrocompatibility for themes
* @deprecated 1.5.0
*/
public $id_color_default = 0;
/**
* @since 1.5.0
* @var bool Tells if the product uses the advanced stock management
*/
public $advanced_stock_management = 0;
public $out_of_stock;
public $depends_on_stock;
public $isFullyLoaded = false;
public $cache_is_pack;
public $cache_has_attachments;
public $is_virtual;
public $booking_product;
public $id_pack_product_attribute;
public $cache_default_attribute;
/**
* @var string If product is populated, this property contain the rewrite link of the default category
*/
public $category;
/**
* @var int tell the type of stock management to apply on the pack
*/
public $pack_stock_type = 3;
public static $_taxCalculationMethod = null;
protected static $_prices = array();
protected static $_pricesLevel2 = array();
protected static $_incat = array();
/**
* @since 1.5.6.1
* @var array $_cart_quantity is deprecated since 1.5.6.1
*/
protected static $_cart_quantity = array();
protected static $_tax_rules_group = array();
protected static $_cacheFeatures = array();
protected static $_frontFeaturesCache = array();
protected static $producPropertiesCache = array();
/** @var array cache stock data in getStock() method */
protected static $cacheStock = array();
public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
/* Classic fields */
'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'supplier_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'location' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64),
'width' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'height' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'depth' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'weight' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'quantity_discount' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),
'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
'cache_is_pack' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'cache_has_attachments' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'is_virtual' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'booking_product' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'service_product_type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
/* Shop fields */
'id_category_default' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'id_tax_rules_group' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'on_sale' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'online_only' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'ecotax' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'minimal_quantity' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'allow_multiple_quantity' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'max_quantity' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'price_calculation_method' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'required' => true),
'wholesale_price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'unity' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'),
'unit_price_ratio' => array('type' => self::TYPE_FLOAT, 'shop' => true),
'additional_shipping_cost' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'customizable' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'text_fields' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'uploadable_files' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'active' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'redirect_type' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'),
'id_product_redirected' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'available_for_order' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'available_date' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
'condition' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isGenericName', 'values' => array('new', 'used', 'refurbished'), 'default' => 'new'),
'auto_add_to_cart' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'price_addition_type' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'show_at_front' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'show_price' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'indexed' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'visibility' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isProductVisibility', 'values' => array('both', 'catalog', 'search', 'none'), 'default' => 'both'),
'cache_default_attribute' => array('type' => self::TYPE_INT, 'shop' => true),
'advanced_stock_management' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDate'),
'pack_stock_type' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
/* Lang fields */
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
'link_rewrite' => array(
'type' => self::TYPE_STRING,
'lang' => true,
'validate' => 'isLinkRewrite',
'required' => true,
'size' => 128,
'ws_modifier' => array(
'http_method' => WebserviceRequest::HTTP_POST,
'modifier' => 'modifierWsLinkRewrite'
)
),
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'description_short' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'available_now' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'available_later' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'IsGenericName', 'size' => 255),
),
'associations' => array(
'manufacturer' => array('type' => self::HAS_ONE),
'supplier' => array('type' => self::HAS_ONE),
'default_category' => array('type' => self::HAS_ONE, 'field' => 'id_category_default', 'object' => 'Category'),
'tax_rules_group' => array('type' => self::HAS_ONE),
'categories' => array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),
'stock_availables' => array('type' => self::HAS_MANY, 'field' => 'id_stock_available', 'object' => 'StockAvailable', 'association' => 'stock_availables'),
),
);
protected $webserviceParameters = array(
'objectMethods' => array(
'add' => 'addWs',
'update' => 'updateWs',
'delete' => 'deleteWs'
),
'objectsNodeName' => 'room_types',
'objectNodeName' => 'room_type',
'fields' => array(
'id_hotel' => array(
'getter' => 'getWsHotel',
'setter' => 'setWsHotel',
'xlink_resource' => 'hotels'
),
'adults' => array(
'getter' => 'getWsAdults',
'setter' => 'setWsAdults',
),
'children' => array(
'getter' => 'getWsChildren',
'setter' => 'setWsChildren',
),
'id_manufacturer' => array(
'xlink_resource' => 'manufacturers'
),
'id_supplier' => array(
'xlink_resource' => 'suppliers'
),
'id_category_default' => array(
'xlink_resource' => 'categories'
),
'new' => array(),
'cache_default_attribute' => array(),
'id_default_image' => array(
'getter' => 'getCoverWs',
'setter' => 'setCoverWs',
'xlink_resource' => array(
'resourceName' => 'images',
'subResourceName' => 'room_types'
)
),
// 'id_default_combination' => array(
// 'getter' => 'getWsDefaultCombination',
// 'setter' => 'setWsDefaultCombination',
// 'xlink_resource' => array(
// 'resourceName' => 'combinations'
// )
// ),
'id_tax_rules_group' => array(
'xlink_resource' => array(
'resourceName' => 'tax_rule_groups'
)
),
'position_in_category' => array(
'getter' => 'getWsPositionInCategory',
'setter' => 'setWsPositionInCategory'
),
// 'manufacturer_name' => array(
// 'getter' => 'getWsManufacturerName',
// 'setter' => false
// ),
'quantity' => array(
'getter' => false,
'setter' => false
),
'type' => array(
'getter' => 'getWsType',
'setter' => 'setWsType',
),
'id_advance_paypent' => array(
'xlink_resource' => array(
'resourceName' => 'advance_payments'
),
'getter' => 'getWsRoomTypeAdvancePayment',
'setter' => false
),
),
'associations' => array(
'categories' => array(
'resource' => 'category',
'fields' => array(
'id' => array('required' => true),
)
),
'images' => array(
'resource' => 'image',
'fields' => array('id' => array())
),
'room_type_features' => array(
'resource' => 'room_type_feature',
'fields' => array(
'id' => array('required' => true),
)
),
'tags' => array('resource' => 'tag',
'fields' => array(
'id' => array('required' => true),
)
),
'hotel_rooms' => array(
'setter' => false,
'resource' => 'hotel_room',
'fields' => array('id' => array('required' => true))
),
'feature_prices' => array(
'setter' => false,
'resource' => 'feature_price',
'fields' => array(
'id' => array('required' => true),
)
),
'extra_demands' => array(
'setter' => false,
'resource' => 'extra_demand',
'fields' => array(
'id' => array('required' => true),
)
),
),
);
const CUSTOMIZE_FILE = 0;
const CUSTOMIZE_TEXTFIELD = 1;
/**
* Note: prefix is "PTYPE" because TYPE_ is used in ObjectModel (definition)
*/
const PTYPE_SIMPLE = 0;
const PTYPE_PACK = 1;
const PTYPE_VIRTUAL = 2;
const PRICE_DISPALY_COMBINE = 1;
const PRICE_DISPALY_INDIVISUAL = 2;
const SERVICE_PRODUCT_WITH_ROOMTYPE = 1;
const SERVICE_PRODUCT_WITHOUT_ROOMTYPE = 2;
const PRICE_ADDITION_TYPE_WITH_ROOM = 1;
const PRICE_ADDITION_TYPE_INDEPENDENT = 2;
const PRICE_CALCULATION_METHOD_PER_BOOKING = 1;
const PRICE_CALCULATION_METHOD_PER_DAY = 2;
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
parent::__construct($id_product, $id_lang, $id_shop);
if ($full && $this->id) {
if (!$context) {
$context = Context::getContext();
}
$this->isFullyLoaded = $full;
$this->tax_name = 'deprecated'; // The applicable tax may be BOTH the product one AND the state one (moreover this variable is some deadcode)
$this->manufacturer_name = Manufacturer::getNameById((int)$this->id_manufacturer);
$this->supplier_name = Supplier::getNameById((int)$this->id_supplier);
$address = null;
$id_address = Cart::getIdAddressForTaxCalculation($this->id);
$this->tax_rate = $this->getTaxesRate(new Address($id_address));
$this->new = $this->isNew();
// Keep base price
$this->base_price = $this->price;
$this->price = Product::getPriceStatic((int)$this->id, false, null, 6, null, false, true, 1, false, null, null, null, $this->specificPrice);
$this->unit_price = ($this->unit_price_ratio != 0 ? $this->price / $this->unit_price_ratio : 0);
if ($this->id) {
$this->tags = Tag::getProductTags((int)$this->id);
}
$this->loadStockData();
}
if ($this->id_category_default) {
$this->category = Category::getLinkRewrite((int)$this->id_category_default, (int)$id_lang);
}
}
/**
* @see ObjectModel::getFieldsShop()
* @return array
*/
public function getFieldsShop()
{
$fields = parent::getFieldsShop();
if (is_null($this->update_fields) || (!empty($this->update_fields['price']) && !empty($this->update_fields['unit_price']))) {
$fields['unit_price_ratio'] = (float)$this->unit_price > 0 ? $this->price / $this->unit_price : 0;
}
$fields['unity'] = pSQL($this->unity);
return $fields;
}
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
$id_shop_list = Shop::getContextListShopID();
if ($this->getType() == Product::PTYPE_VIRTUAL) {
foreach ($id_shop_list as $value) {
StockAvailable::setProductOutOfStock((int)$this->id, 1, $value);
}
if ($this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
}
} else {
foreach ($id_shop_list as $value) {
StockAvailable::setProductOutOfStock((int)$this->id, 2, $value);
}
}
$this->setGroupReduction();
Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
return true;
}
public function update($null_values = false)
{
$return = parent::update($null_values);
$this->setGroupReduction();
// Sync stock Reference, EAN13 and UPC
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) {
Db::getInstance()->update('stock', array(
'reference' => pSQL($this->reference),
'ean13' => pSQL($this->ean13),
'upc' => pSQL($this->upc),
), 'id_product = '.(int)$this->id.' AND id_product_attribute = 0');
}
Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));
if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
}
return $return;
}
public static function initPricesComputation($id_customer = null)
{
if ($id_customer) {
$customer = new Customer((int)$id_customer);
if (!Validate::isLoadedObject($customer)) {
die(Tools::displayError());
}
self::$_taxCalculationMethod = Group::getPriceDisplayMethod((int)$customer->id_default_group);
$cur_cart = Context::getContext()->cart;
$id_address = 0;
if (Validate::isLoadedObject($cur_cart)) {
$id_address = (int)$cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
}
$address_infos = Address::getCountryAndState($id_address);
} else {
self::$_taxCalculationMethod = Group::getPriceDisplayMethod(Group::getCurrent()->id);
}
}
public static function getTaxCalculationMethod($id_customer = null)
{
if (self::$_taxCalculationMethod === null || $id_customer !== null) {
Product::initPricesComputation($id_customer);
}
return (int)self::$_taxCalculationMethod;
}
/**
* Move a product inside its category
* @param bool $way Up (1) or Down (0)
* @param int $position
* return boolean Update result
*/
public function updatePosition($way, $position)
{
if (!$res = Db::getInstance()->executeS('
SELECT cp.`id_product`, cp.`position`, cp.`id_category`
FROM `'._DB_PREFIX_.'category_product` cp
WHERE cp.`id_category` = '.(int)Tools::getValue('id_category', 1).'
ORDER BY cp.`position` ASC')
) {
return false;
}
foreach ($res as $product) {
if ((int)$product['id_product'] == (int)$this->id) {
$moved_product = $product;
}
}
if (!isset($moved_product) || !isset($position)) {
return false;
}
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$result = (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category_product` cp
INNER JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = cp.`id_product`)
'.Shop::addSqlAssociation('product', 'p').'
SET cp.`position`= `position` '.($way ? '- 1' : '+ 1').',
p.`date_upd` = "'.date('Y-m-d H:i:s').'", product_shop.`date_upd` = "'.date('Y-m-d H:i:s').'"
WHERE cp.`position`
'.($way
? '> '.(int)$moved_product['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_product['position'].' AND `position` >= '.(int)$position).'
AND `id_category`='.(int)$moved_product['id_category'])
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category_product` cp
INNER JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = cp.`id_product`)
'.Shop::addSqlAssociation('product', 'p').'
SET cp.`position` = '.(int)$position.',
p.`date_upd` = "'.date('Y-m-d H:i:s').'", product_shop.`date_upd` = "'.date('Y-m-d H:i:s').'"
WHERE cp.`id_product` = '.(int)$moved_product['id_product'].'
AND cp.`id_category`='.(int)$moved_product['id_category'])
);
Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));
return $result;
}
/*
* Reorder product position in category $id_category.
* Call it after deleting a product from a category.
*
* @param int $id_category
*/
public static function cleanPositions($id_category, $position = 0)
{
$return = true;
if (!(int)$position) {
$result = Db::getInstance()->executeS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int)$id_category.'
ORDER BY `position`
');
$total = count($result);
for ($i = 0; $i < $total; $i++) {
$return &= Db::getInstance()->update(
'category_product',
array('position' => $i),
'`id_category` = '.(int)$id_category.' AND `id_product` = '.(int)$result[$i]['id_product']
);
$return &= Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'product` p'.Shop::addSqlAssociation('product', 'p').'
SET p.`date_upd` = "'.date('Y-m-d H:i:s').'", product_shop.`date_upd` = "'.date('Y-m-d H:i:s').'"
WHERE p.`id_product` = '.(int)$result[$i]['id_product']
);
}
} else {
$result = Db::getInstance()->executeS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int)$id_category.' AND `position` > '.(int)$position.'
ORDER BY `position`
');
$total = count($result);
$return &= Db::getInstance()->update(
'category_product',
array('position' => array('type' => 'sql', 'value' => '`position`-1')),
'`id_category` = '.(int)$id_category.' AND `position` > '.(int)$position
);
for ($i = 0; $i < $total; $i++) {
$return &= Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'product` p'.Shop::addSqlAssociation('product', 'p').'
SET p.`date_upd` = "'.date('Y-m-d H:i:s').'", product_shop.`date_upd` = "'.date('Y-m-d H:i:s').'"
WHERE p.`id_product` = '.(int)$result[$i]['id_product']
);
}
}
return $return;
}
/**
* Get the default attribute for a product
*
* @return int Attributes list
*/
public static function getDefaultAttribute($id_product, $minimum_quantity = 0, $reset = false)
{
static $combinations = array();
if (!Combination::isFeatureActive()) {
return 0;
}
if ($reset && isset($combinations[$id_product])) {
unset($combinations[$id_product]);
}
if (!isset($combinations[$id_product])) {
$combinations[$id_product] = array();
}
if (isset($combinations[$id_product][$minimum_quantity])) {
return $combinations[$id_product][$minimum_quantity];
}
$sql = 'SELECT product_attribute_shop.id_product_attribute
FROM '._DB_PREFIX_.'product_attribute pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
WHERE pa.id_product = '.(int)$id_product;
$result_no_filter = Db::getInstance()->getValue($sql);
if (!$result_no_filter) {
$combinations[$id_product][$minimum_quantity] = 0;
return 0;
}
$sql = 'SELECT product_attribute_shop.id_product_attribute
FROM '._DB_PREFIX_.'product_attribute pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
'.($minimum_quantity > 0 ? Product::sqlStock('pa', 'pa') : '').
' WHERE product_attribute_shop.default_on = 1 '
.($minimum_quantity > 0 ? ' AND IFNULL(stock.quantity, 0) >= '.(int)$minimum_quantity : '').
' AND pa.id_product = '.(int)$id_product;
$result = Db::getInstance()->getValue($sql);
if (!$result) {
$sql = 'SELECT product_attribute_shop.id_product_attribute
FROM '._DB_PREFIX_.'product_attribute pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
'.($minimum_quantity > 0 ? Product::sqlStock('pa', 'pa') : '').
' WHERE pa.id_product = '.(int)$id_product
.($minimum_quantity > 0 ? ' AND IFNULL(stock.quantity, 0) >= '.(int)$minimum_quantity : '');
$result = Db::getInstance()->getValue($sql);
}
if (!$result) {
$sql = 'SELECT product_attribute_shop.id_product_attribute
FROM '._DB_PREFIX_.'product_attribute pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
WHERE product_attribute_shop.`default_on` = 1
AND pa.id_product = '.(int)$id_product;
$result = Db::getInstance()->getValue($sql);
}
if (!$result) {
$result = $result_no_filter;
}
$combinations[$id_product][$minimum_quantity] = $result;
return $result;
}
public function setAvailableDate($available_date = '0000-00-00')
{
if (Validate::isDateFormat($available_date) && $this->available_date != $available_date) {
$this->available_date = $available_date;
return $this->update();
}
return false;
}
/**
* For a given id_product and id_product_attribute, return available date
*
* @param int $id_product
* @param int $id_product_attribute Optional
* @return string/null
*/
public static function getAvailableDate($id_product, $id_product_attribute = null)
{
$sql = 'SELECT';
if ($id_product_attribute === null) {
$sql .= ' p.`available_date`';
} else {
$sql .= ' IF(pa.`available_date` = "0000-00-00", p.`available_date`, pa.`available_date`) AS available_date';
}
$sql .= ' FROM `'._DB_PREFIX_.'product` p';
if ($id_product_attribute !== null) {
$sql .= ' LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product` = p.`id_product`)';
}
$sql .= Shop::addSqlAssociation('product', 'p');
if ($id_product_attribute !== null) {
$sql .= Shop::addSqlAssociation('product_attribute', 'pa');
}
$sql .= ' WHERE p.`id_product` = '.(int)$id_product;
if ($id_product_attribute !== null) {
$sql .= ' AND pa.`id_product` = '.(int)$id_product.' AND pa.`id_product_attribute` = '.(int)$id_product_attribute;
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
if ($result == '0000-00-00') {
$result = null;
}
return $result;
}
public static function updateIsVirtual($id_product, $is_virtual = true)
{
Db::getInstance()->update('product', array(
'is_virtual' => (bool)$is_virtual,
), 'id_product = '.(int)$id_product);
}
public static function getProductPriceCalculation($id_product) {
return Db::getInstance()->getValue(
'SELECT product_shop.`price_calculation_method`
FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').'
WHERE p.`id_product` = '.(int)$id_product
);
}
/**
* @see ObjectModel::validateField()
*/
public function validateField($field, $value, $id_lang = null, $skip = array(), $human_errors = false)
{
if ($field == 'description_short') {
$limit = (int)Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT');
if ($limit <= 0) {
$limit = 800;
}
$size_without_html = Tools::strlen(strip_tags($value));
$size_with_html = Tools::strlen($value);
$this->def['fields']['description_short']['size'] = $limit + $size_with_html - $size_without_html;
}
return parent::validateField($field, $value, $id_lang, $skip, $human_errors);
}
public function toggleStatus()
{
//test if the product is active and if redirect_type is empty string and set default value to id_product_redirected & redirect_type
// /!\ after parent::toggleStatus() active will be false, that why we set 404 by default :p
if ($this->active) {
//case where active will be false after parent::toggleStatus()
$this->id_product_redirected = 0;
$this->redirect_type = '404';
} else {
//case where active will be true after parent::toggleStatus()
$this->id_product_redirected = 0;
$this->redirect_type = '';
}
return parent::toggleStatus();
}
public static function isBookingProduct($id_product)
{
return Db::getInstance()->getValue('
SELECT `booking_product` FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_product` = '.(int)$id_product
);
}
public function delete()
{
/*
* @since 1.5.0
* It is NOT possible to delete a product if there are currently:
* - physical stock for this product
* - supply order(s) for this product
*/
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $this->advanced_stock_management) {
$stock_manager = StockManagerFactory::getManager();
$physical_quantity = $stock_manager->getProductPhysicalQuantities($this->id, 0);
$real_quantity = $stock_manager->getProductRealQuantities($this->id, 0);
if ($physical_quantity > 0) {
return false;
}
if ($real_quantity > $physical_quantity) {
return false;
}
$warehouse_product_locations = Adapter_ServiceLocator::get('Core_Foundation_Database_EntityManager')->getRepository('WarehouseProductLocation')->findByIdProduct($this->id);
foreach ($warehouse_product_locations as $warehouse_product_location) {
$warehouse_product_location->delete();
}
$stocks = Adapter_ServiceLocator::get('Core_Foundation_Database_EntityManager')->getRepository('Stock')->findByIdProduct($this->id);
foreach ($stocks as $stock) {
$stock->delete();
}
}
$result = parent::delete();
// Removes the product from StockAvailable, for the current shop
StockAvailable::removeProductFromStockAvailable($this->id);
$result &= ($this->deleteProductAttributes() && $this->deleteImages() && $this->deleteSceneProducts());
// If there are still entries in product_shop, don't remove completely the product
if ($this->hasMultishopEntries()) {
return true;
}
if (!$this->booking_product) {
if (!$this->deleteServiceInfo()) {
return false;
}
}
Hook::exec('actionProductDelete', array('id_product' => (int)$this->id, 'product' => $this));
if (!$result ||
!GroupReduction::deleteProductReduction($this->id) ||
!$this->deleteCategories(true) ||
!$this->deleteProductFeatures() ||
!$this->deleteTags() ||
!$this->deleteAttributesImpacts() ||
!$this->deleteAttachments(false) ||
!$this->deleteCustomization() ||
!SpecificPrice::deleteByProductId((int)$this->id) ||
!$this->deletePack() ||
!$this->deleteProductSale() ||
!$this->deleteSearchIndexes() ||
!$this->deleteAccessories() ||
!$this->deleteFromAccessories() ||
!$this->deleteFromSupplier() ||