|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2016 Marcos García <[email protected]> |
|
4
|
|
|
* Copyright (C) 2018 Juanjo Menent <[email protected]> |
|
5
|
|
|
* Copyright (C) 2022 Open-Dsi <[email protected]> |
|
6
|
|
|
* Copyright (C) 2024 MDW <[email protected]> |
|
7
|
|
|
* Copyright (C) 2024 Frédéric France <[email protected]> |
|
8
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU General Public License as published by |
|
12
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
13
|
|
|
* (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU General Public License |
|
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Dolibarr\Code\Variants\Classes; |
|
25
|
|
|
|
|
26
|
|
|
use DoliDB; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* \file htdocs/variants/class/ProductCombination.class.php |
|
30
|
|
|
* \ingroup variants |
|
31
|
|
|
* \brief File of the ProductCombination class |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Class ProductCombinationLevel |
|
36
|
|
|
* Used to represent a product combination Level |
|
37
|
|
|
*/ |
|
38
|
|
|
class ProductCombinationLevel |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Database handler |
|
42
|
|
|
* @var DoliDB |
|
43
|
|
|
*/ |
|
44
|
|
|
public $db; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string Name of table without prefix where object is stored |
|
48
|
|
|
*/ |
|
49
|
|
|
public $table_element = 'product_attribute_combination_price_level'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Rowid of combination |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
public $id; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Rowid of parent product combination |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
public $fk_product_attribute_combination; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Combination price level |
|
65
|
|
|
* @var int |
|
66
|
|
|
*/ |
|
67
|
|
|
public $fk_price_level; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Price variation |
|
71
|
|
|
* @var float |
|
72
|
|
|
*/ |
|
73
|
|
|
public $variation_price; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Is the price variation a relative variation? |
|
77
|
|
|
* @var bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public $variation_price_percentage = false; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var string error |
|
83
|
|
|
*/ |
|
84
|
|
|
public $error; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var string[] array of errors |
|
88
|
|
|
*/ |
|
89
|
|
|
public $errors = array(); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Constructor |
|
93
|
|
|
* |
|
94
|
|
|
* @param DoliDB $db Database handler |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct(DoliDB $db) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->db = $db; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Retrieves a combination level by its rowid |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $rowid Row id |
|
105
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
106
|
|
|
*/ |
|
107
|
|
|
public function fetch($rowid) |
|
108
|
|
|
{ |
|
109
|
|
|
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"; |
|
110
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element; |
|
111
|
|
|
$sql .= " WHERE rowid = " . (int) $rowid; |
|
112
|
|
|
|
|
113
|
|
|
$resql = $this->db->query($sql); |
|
114
|
|
|
if ($resql) { |
|
115
|
|
|
$obj = $this->db->fetch_object($resql); |
|
116
|
|
|
if ($obj) { |
|
117
|
|
|
return $this->fetchFormObj($obj); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return -1; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Retrieves combination price levels |
|
127
|
|
|
* |
|
128
|
|
|
* @param int $fk_product_attribute_combination Id of product combination |
|
129
|
|
|
* @param int $fk_price_level The price level to fetch, use 0 for all |
|
130
|
|
|
* @return mixed self[] | -1 on KO |
|
131
|
|
|
*/ |
|
132
|
|
|
public function fetchAll($fk_product_attribute_combination, $fk_price_level = 0) |
|
133
|
|
|
{ |
|
134
|
|
|
$result = array(); |
|
135
|
|
|
|
|
136
|
|
|
$sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"; |
|
137
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element; |
|
138
|
|
|
$sql .= " WHERE fk_product_attribute_combination = " . intval($fk_product_attribute_combination); |
|
139
|
|
|
if (!empty($fk_price_level)) { |
|
140
|
|
|
$sql .= ' AND fk_price_level = ' . intval($fk_price_level); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$res = $this->db->query($sql); |
|
144
|
|
|
if ($res) { |
|
145
|
|
|
if ($this->db->num_rows($res) > 0) { |
|
146
|
|
|
while ($obj = $this->db->fetch_object($res)) { |
|
147
|
|
|
$productCombinationLevel = new ProductCombinationLevel($this->db); |
|
148
|
|
|
$productCombinationLevel->fetchFormObj($obj); |
|
149
|
|
|
$result[$obj->fk_price_level] = $productCombinationLevel; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} else { |
|
153
|
|
|
return -1; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $result; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Assign vars form an stdclass like sql obj |
|
161
|
|
|
* |
|
162
|
|
|
* @param Object $obj Object resultset |
|
163
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
164
|
|
|
*/ |
|
165
|
|
|
public function fetchFormObj($obj) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$obj) { |
|
168
|
|
|
return -1; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->id = $obj->rowid; |
|
172
|
|
|
$this->fk_product_attribute_combination = (int) $obj->fk_product_attribute_combination; |
|
173
|
|
|
$this->fk_price_level = intval($obj->fk_price_level); |
|
174
|
|
|
$this->variation_price = (float) $obj->variation_price; |
|
175
|
|
|
$this->variation_price_percentage = (bool) $obj->variation_price_percentage; |
|
176
|
|
|
|
|
177
|
|
|
return 1; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Save a price impact of a product combination for a price level |
|
183
|
|
|
* |
|
184
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
185
|
|
|
*/ |
|
186
|
|
|
public function save() |
|
187
|
|
|
{ |
|
188
|
|
|
if (($this->id > 0 && empty($this->fk_product_attribute_combination)) || empty($this->fk_price_level)) { |
|
189
|
|
|
return -1; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Check if level exist in DB before add |
|
193
|
|
|
if ($this->fk_product_attribute_combination > 0 && empty($this->id)) { |
|
194
|
|
|
$sql = "SELECT rowid id"; |
|
195
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element; |
|
196
|
|
|
$sql .= " WHERE fk_product_attribute_combination = " . (int) $this->fk_product_attribute_combination; |
|
197
|
|
|
$sql .= ' AND fk_price_level = ' . ((int) $this->fk_price_level); |
|
198
|
|
|
|
|
199
|
|
|
$resql = $this->db->query($sql); |
|
200
|
|
|
if ($resql) { |
|
201
|
|
|
$obj = $this->db->fetch_object($resql); |
|
202
|
|
|
if ($obj) { |
|
203
|
|
|
$this->id = $obj->id; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
// Update |
|
209
|
|
|
if (!empty($this->id)) { |
|
210
|
|
|
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element; |
|
211
|
|
|
$sql .= ' SET variation_price = ' . (float) $this->variation_price . ' , variation_price_percentage = ' . intval($this->variation_price_percentage); |
|
212
|
|
|
$sql .= ' WHERE rowid = ' . ((int) $this->id); |
|
213
|
|
|
|
|
214
|
|
|
$res = $this->db->query($sql); |
|
215
|
|
|
if ($res > 0) { |
|
216
|
|
|
return $this->id; |
|
217
|
|
|
} else { |
|
218
|
|
|
$this->error = $this->db->error(); |
|
219
|
|
|
$this->errors[] = $this->error; |
|
220
|
|
|
return -1; |
|
221
|
|
|
} |
|
222
|
|
|
} else { |
|
223
|
|
|
// Add |
|
224
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " ("; |
|
225
|
|
|
$sql .= "fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage"; |
|
226
|
|
|
$sql .= ") VALUES ("; |
|
227
|
|
|
$sql .= (int) $this->fk_product_attribute_combination; |
|
228
|
|
|
$sql .= ", " . intval($this->fk_price_level); |
|
229
|
|
|
$sql .= ", " . (float) $this->variation_price; |
|
230
|
|
|
$sql .= ", " . intval($this->variation_price_percentage); |
|
231
|
|
|
$sql .= ")"; |
|
232
|
|
|
|
|
233
|
|
|
$res = $this->db->query($sql); |
|
234
|
|
|
if ($res) { |
|
235
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
|
236
|
|
|
} else { |
|
237
|
|
|
$this->error = $this->db->error(); |
|
238
|
|
|
$this->errors[] = $this->error; |
|
239
|
|
|
return -1; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $this->id; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* delete |
|
249
|
|
|
* |
|
250
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
251
|
|
|
*/ |
|
252
|
|
|
public function delete() |
|
253
|
|
|
{ |
|
254
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE rowid = " . (int) $this->id; |
|
255
|
|
|
$res = $this->db->query($sql); |
|
256
|
|
|
|
|
257
|
|
|
return $res ? 1 : -1; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* delete all for a combination |
|
263
|
|
|
* |
|
264
|
|
|
* @param int $fk_product_attribute_combination Id of combination |
|
265
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
266
|
|
|
*/ |
|
267
|
|
|
public function deleteAllForCombination($fk_product_attribute_combination) |
|
268
|
|
|
{ |
|
269
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE fk_product_attribute_combination = " . (int) $fk_product_attribute_combination; |
|
270
|
|
|
$res = $this->db->query($sql); |
|
271
|
|
|
|
|
272
|
|
|
return $res ? 1 : -1; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Clean not needed price levels for a combination |
|
278
|
|
|
* |
|
279
|
|
|
* @param int $fk_product_attribute_combination Id of combination |
|
280
|
|
|
* @return int Return integer <0 KO, >0 OK |
|
281
|
|
|
*/ |
|
282
|
|
|
public function clean($fk_product_attribute_combination) |
|
283
|
|
|
{ |
|
284
|
|
|
global $conf; |
|
285
|
|
|
|
|
286
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element; |
|
287
|
|
|
$sql .= " WHERE fk_product_attribute_combination = " . (int) $fk_product_attribute_combination; |
|
288
|
|
|
$sql .= " AND fk_price_level > " . (int) getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT'); |
|
289
|
|
|
$res = $this->db->query($sql); |
|
290
|
|
|
|
|
291
|
|
|
return $res ? 1 : -1; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Create new Product Combination Price level from Parent |
|
297
|
|
|
* |
|
298
|
|
|
* @param DoliDB $db Database handler |
|
299
|
|
|
* @param ProductCombination $productCombination Product combination |
|
300
|
|
|
* @param int $fkPriceLevel Price level |
|
301
|
|
|
* @return ProductCombinationLevel |
|
302
|
|
|
*/ |
|
303
|
|
|
public static function createFromParent(DoliDB $db, ProductCombination $productCombination, $fkPriceLevel) |
|
304
|
|
|
{ |
|
305
|
|
|
$productCombinationLevel = new self($db); |
|
306
|
|
|
$productCombinationLevel->fk_price_level = $fkPriceLevel; |
|
307
|
|
|
$productCombinationLevel->fk_product_attribute_combination = $productCombination->id; |
|
308
|
|
|
$productCombinationLevel->variation_price = $productCombination->variation_price; |
|
309
|
|
|
$productCombinationLevel->variation_price_percentage = (bool) $productCombination->variation_price_percentage; |
|
310
|
|
|
|
|
311
|
|
|
return $productCombinationLevel; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|