1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2002-2004 Rodolphe Quiedeville <[email protected]> |
4
|
|
|
* Copyright (C) 2004 Eric Seigne <[email protected]> |
5
|
|
|
* Copyright (C) 2004-2011 Laurent Destailleur <[email protected]> |
6
|
|
|
* Copyright (C) 2005 Marc Barilley <[email protected]> |
7
|
|
|
* Copyright (C) 2005-2013 Regis Houssin <[email protected]> |
8
|
|
|
* Copyright (C) 2006 Andre Cianfarani <[email protected]> |
9
|
|
|
* Copyright (C) 2008 Raphael Bertrand <[email protected]> |
10
|
|
|
* Copyright (C) 2010-2020 Juanjo Menent <[email protected]> |
11
|
|
|
* Copyright (C) 2010-2018 Philippe Grand <[email protected]> |
12
|
|
|
* Copyright (C) 2012-2014 Christophe Battarel <[email protected]> |
13
|
|
|
* Copyright (C) 2013 Florian Henry <[email protected]> |
14
|
|
|
* Copyright (C) 2014 Marcos García <[email protected]> |
15
|
|
|
* Copyright (C) 2016 Ferran Marcet <[email protected]> |
16
|
|
|
* Copyright (C) 2018 Nicolas ZABOURI <[email protected]> |
17
|
|
|
* Copyright (C) 2019-2024 Frédéric France <[email protected]> |
18
|
|
|
* Copyright (C) 2020 Tobias Sekan <[email protected]> |
19
|
|
|
* Copyright (C) 2022 Gauthier VERDOL <[email protected]> |
20
|
|
|
* Copyright (C) 2024 MDW <[email protected]> |
21
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
22
|
|
|
* |
23
|
|
|
* This program is free software; you can redistribute it and/or modify |
24
|
|
|
* it under the terms of the GNU General Public License as published by |
25
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
26
|
|
|
* (at your option) any later version. |
27
|
|
|
* |
28
|
|
|
* This program is distributed in the hope that it will be useful, |
29
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
30
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31
|
|
|
* GNU General Public License for more details. |
32
|
|
|
* |
33
|
|
|
* You should have received a copy of the GNU General Public License |
34
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
namespace Dolibarr\Code\SupplierProposal\Classes; |
38
|
|
|
|
39
|
|
|
use Dolibarr\Core\Base\CommonObjectLine; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* \file htdocs/supplier_proposal/class/supplier_proposal.class.php |
43
|
|
|
* \brief File of class to manage supplier proposals |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/fourn/class/fournisseur.product.class.php'; |
47
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/product/class/product.class.php'; |
48
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/contact/class/contact.class.php'; |
49
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/margin/lib/margins.lib.php'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class to manage supplier_proposal lines |
53
|
|
|
*/ |
54
|
|
|
class SupplierProposalLine extends CommonObjectLine |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var DoliDB Database handler. |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public $db; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string Error code (or message) |
63
|
|
|
*/ |
64
|
|
|
public $error = ''; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string ID to identify managed object |
68
|
|
|
*/ |
69
|
|
|
public $element = 'supplier_proposaldet'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string Name of table without prefix where object is stored |
73
|
|
|
*/ |
74
|
|
|
public $table_element = 'supplier_proposaldet'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see CommonObjectLine |
78
|
|
|
*/ |
79
|
|
|
public $parent_element = 'supplier_proposal'; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see CommonObjectLine |
83
|
|
|
*/ |
84
|
|
|
public $fk_parent_attribute = 'fk_supplier_proposal'; |
85
|
|
|
|
86
|
|
|
public $oldline; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var int ID |
90
|
|
|
*/ |
91
|
|
|
public $id; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var int ID |
95
|
|
|
*/ |
96
|
|
|
public $fk_supplier_proposal; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var int ID |
100
|
|
|
*/ |
101
|
|
|
public $fk_parent_line; |
102
|
|
|
|
103
|
|
|
public $desc; // Description ligne |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var int ID |
107
|
|
|
*/ |
108
|
|
|
public $fk_product; // Id produit predefini |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @deprecated |
112
|
|
|
* @see $product_type |
113
|
|
|
*/ |
114
|
|
|
public $fk_product_type; |
115
|
|
|
/** |
116
|
|
|
* Product type |
117
|
|
|
* @var int |
118
|
|
|
* @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE |
119
|
|
|
*/ |
120
|
|
|
public $product_type = Product::TYPE_PRODUCT; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var float Quantity |
124
|
|
|
*/ |
125
|
|
|
public $qty; |
126
|
|
|
public $tva_tx; |
127
|
|
|
public $vat_src_code; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Unit price before taxes |
131
|
|
|
* @var float |
132
|
|
|
*/ |
133
|
|
|
public $subprice; |
134
|
|
|
public $remise_percent; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var int ID |
138
|
|
|
*/ |
139
|
|
|
public $fk_remise_except; |
140
|
|
|
|
141
|
|
|
public $rang = 0; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var int ID |
145
|
|
|
*/ |
146
|
|
|
public $fk_fournprice; |
147
|
|
|
|
148
|
|
|
public $pa_ht; |
149
|
|
|
public $marge_tx; |
150
|
|
|
public $marque_tx; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @var int special code |
154
|
|
|
*/ |
155
|
|
|
public $special_code; // Tag for special lines (exclusive tags) |
156
|
|
|
// 1: frais de port |
157
|
|
|
// 2: ecotaxe |
158
|
|
|
// 3: option line (when qty = 0) |
159
|
|
|
|
160
|
|
|
public $info_bits = 0; // Liste d'options cumulables: |
161
|
|
|
// Bit 0: 0 si TVA normal - 1 if TVA NPR |
162
|
|
|
// Bit 1: 0 ligne normal - 1 if fixed reduction |
163
|
|
|
|
164
|
|
|
public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne |
165
|
|
|
public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne |
166
|
|
|
public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne |
167
|
|
|
|
168
|
|
|
public $date_start; |
169
|
|
|
public $date_end; |
170
|
|
|
|
171
|
|
|
// From llx_product |
172
|
|
|
/** |
173
|
|
|
* @deprecated |
174
|
|
|
* @see $product_ref |
175
|
|
|
*/ |
176
|
|
|
public $ref; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Product reference |
180
|
|
|
* @var string |
181
|
|
|
*/ |
182
|
|
|
public $product_ref; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @deprecated |
186
|
|
|
* @see $product_label |
187
|
|
|
*/ |
188
|
|
|
public $libelle; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Product label |
192
|
|
|
* @var string |
193
|
|
|
*/ |
194
|
|
|
public $product_label; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Custom label |
198
|
|
|
* @var string |
199
|
|
|
*/ |
200
|
|
|
public $label; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Product description |
204
|
|
|
* @var string |
205
|
|
|
*/ |
206
|
|
|
public $product_desc; |
207
|
|
|
|
208
|
|
|
public $localtax1_tx; // Local tax 1 |
209
|
|
|
public $localtax2_tx; // Local tax 2 |
210
|
|
|
public $localtax1_type; // Local tax 1 type |
211
|
|
|
public $localtax2_type; // Local tax 2 type |
212
|
|
|
public $total_localtax1; // Line total local tax 1 |
213
|
|
|
public $total_localtax2; // Line total local tax 2 |
214
|
|
|
|
215
|
|
|
public $skip_update_total; // Skip update price total for special lines |
216
|
|
|
|
217
|
|
|
public $ref_fourn; |
218
|
|
|
public $ref_supplier; |
219
|
|
|
|
220
|
|
|
// Multicurrency |
221
|
|
|
/** |
222
|
|
|
* @var int ID |
223
|
|
|
*/ |
224
|
|
|
public $fk_multicurrency; |
225
|
|
|
|
226
|
|
|
public $multicurrency_code; |
227
|
|
|
public $multicurrency_subprice; |
228
|
|
|
public $multicurrency_total_ht; |
229
|
|
|
public $multicurrency_total_tva; |
230
|
|
|
public $multicurrency_total_ttc; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Class line Constructor |
234
|
|
|
* |
235
|
|
|
* @param DoliDB $db Database handler |
236
|
|
|
*/ |
237
|
|
|
public function __construct($db) |
238
|
|
|
{ |
239
|
|
|
$this->db = $db; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Retrieve the propal line object |
244
|
|
|
* |
245
|
|
|
* @param int $rowid Propal line id |
246
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
247
|
|
|
*/ |
248
|
|
|
public function fetch($rowid) |
249
|
|
|
{ |
250
|
|
|
$sql = 'SELECT pd.rowid, pd.fk_supplier_proposal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.tva_tx,'; |
251
|
|
|
$sql .= ' pd.date_start, pd.date_end,'; |
252
|
|
|
$sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,'; |
253
|
|
|
$sql .= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,'; |
254
|
|
|
$sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,'; |
255
|
|
|
$sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,'; |
256
|
|
|
$sql .= ' pd.product_type, pd.ref_fourn as ref_produit_fourn,'; |
257
|
|
|
$sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc, pd.fk_unit'; |
258
|
|
|
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'supplier_proposaldet as pd'; |
259
|
|
|
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON pd.fk_product = p.rowid'; |
260
|
|
|
$sql .= ' WHERE pd.rowid = ' . ((int) $rowid); |
261
|
|
|
|
262
|
|
|
$result = $this->db->query($sql); |
263
|
|
|
if ($result) { |
264
|
|
|
$objp = $this->db->fetch_object($result); |
265
|
|
|
|
266
|
|
|
$this->id = $objp->rowid; |
267
|
|
|
$this->fk_supplier_proposal = $objp->fk_supplier_proposal; |
268
|
|
|
$this->fk_parent_line = $objp->fk_parent_line; |
269
|
|
|
$this->label = $objp->custom_label; |
270
|
|
|
$this->desc = $objp->description; |
271
|
|
|
$this->qty = $objp->qty; |
272
|
|
|
$this->subprice = $objp->subprice; |
273
|
|
|
$this->tva_tx = $objp->tva_tx; |
274
|
|
|
$this->remise_percent = $objp->remise_percent; |
275
|
|
|
$this->fk_remise_except = $objp->fk_remise_except; |
276
|
|
|
$this->fk_product = $objp->fk_product; |
277
|
|
|
$this->info_bits = $objp->info_bits; |
278
|
|
|
$this->date_start = $this->db->jdate($objp->date_start); |
279
|
|
|
$this->date_end = $this->db->jdate($objp->date_end); |
280
|
|
|
|
281
|
|
|
$this->total_ht = $objp->total_ht; |
282
|
|
|
$this->total_tva = $objp->total_tva; |
283
|
|
|
$this->total_ttc = $objp->total_ttc; |
284
|
|
|
|
285
|
|
|
$this->fk_fournprice = $objp->fk_fournprice; |
286
|
|
|
|
287
|
|
|
$marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht); |
288
|
|
|
$this->pa_ht = $marginInfos[0]; |
289
|
|
|
$this->marge_tx = $marginInfos[1]; |
290
|
|
|
$this->marque_tx = $marginInfos[2]; |
291
|
|
|
|
292
|
|
|
$this->special_code = $objp->special_code; |
293
|
|
|
$this->product_type = $objp->product_type; |
294
|
|
|
$this->rang = $objp->rang; |
295
|
|
|
|
296
|
|
|
$this->ref = $objp->product_ref; // deprecated |
297
|
|
|
$this->product_ref = $objp->product_ref; |
298
|
|
|
$this->libelle = $objp->product_label; // deprecated |
299
|
|
|
$this->product_label = $objp->product_label; |
300
|
|
|
$this->product_desc = $objp->product_desc; |
301
|
|
|
|
302
|
|
|
$this->ref_fourn = $objp->ref_produit_fourn; |
303
|
|
|
|
304
|
|
|
// Multicurrency |
305
|
|
|
$this->fk_multicurrency = $objp->fk_multicurrency; |
306
|
|
|
$this->multicurrency_code = $objp->multicurrency_code; |
307
|
|
|
$this->multicurrency_subprice = $objp->multicurrency_subprice; |
308
|
|
|
$this->multicurrency_total_ht = $objp->multicurrency_total_ht; |
309
|
|
|
$this->multicurrency_total_tva = $objp->multicurrency_total_tva; |
310
|
|
|
$this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; |
311
|
|
|
$this->fk_unit = $objp->fk_unit; |
312
|
|
|
|
313
|
|
|
$this->db->free($result); |
314
|
|
|
return 1; |
315
|
|
|
} else { |
316
|
|
|
dol_print_error($this->db); |
317
|
|
|
return -1; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Insert object line propal in database |
323
|
|
|
* |
324
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
325
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
326
|
|
|
*/ |
327
|
|
|
public function insert($notrigger = 0) |
328
|
|
|
{ |
329
|
|
|
global $conf, $langs, $user; |
330
|
|
|
|
331
|
|
|
$error = 0; |
332
|
|
|
|
333
|
|
|
dol_syslog(get_class($this) . "::insert rang=" . $this->rang); |
334
|
|
|
|
335
|
|
|
// Clean parameters |
336
|
|
|
if (empty($this->tva_tx)) { |
337
|
|
|
$this->tva_tx = 0; |
338
|
|
|
} |
339
|
|
|
if (empty($this->vat_src_code)) { |
340
|
|
|
$this->vat_src_code = ''; |
341
|
|
|
} |
342
|
|
|
if (empty($this->localtax1_tx)) { |
343
|
|
|
$this->localtax1_tx = 0; |
344
|
|
|
} |
345
|
|
|
if (empty($this->localtax2_tx)) { |
346
|
|
|
$this->localtax2_tx = 0; |
347
|
|
|
} |
348
|
|
|
if (empty($this->localtax1_type)) { |
349
|
|
|
$this->localtax1_type = 0; |
350
|
|
|
} |
351
|
|
|
if (empty($this->localtax2_type)) { |
352
|
|
|
$this->localtax2_type = 0; |
353
|
|
|
} |
354
|
|
|
if (empty($this->total_localtax1)) { |
355
|
|
|
$this->total_localtax1 = 0; |
356
|
|
|
} |
357
|
|
|
if (empty($this->total_localtax2)) { |
358
|
|
|
$this->total_localtax2 = 0; |
359
|
|
|
} |
360
|
|
|
if (empty($this->rang)) { |
361
|
|
|
$this->rang = 0; |
362
|
|
|
} |
363
|
|
|
if (empty($this->remise_percent)) { |
364
|
|
|
$this->remise_percent = 0; |
365
|
|
|
} |
366
|
|
|
if (empty($this->info_bits)) { |
367
|
|
|
$this->info_bits = 0; |
368
|
|
|
} |
369
|
|
|
if (empty($this->special_code)) { |
370
|
|
|
$this->special_code = 0; |
371
|
|
|
} |
372
|
|
|
if (empty($this->fk_parent_line)) { |
373
|
|
|
$this->fk_parent_line = 0; |
374
|
|
|
} |
375
|
|
|
if (empty($this->fk_fournprice)) { |
376
|
|
|
$this->fk_fournprice = 0; |
377
|
|
|
} |
378
|
|
|
if (empty($this->fk_unit)) { |
379
|
|
|
$this->fk_unit = 0; |
380
|
|
|
} |
381
|
|
|
if (empty($this->subprice)) { |
382
|
|
|
$this->subprice = 0; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
if (empty($this->pa_ht)) { |
386
|
|
|
$this->pa_ht = 0; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
// if buy price not defined, define buyprice as configured in margin admin |
390
|
|
|
if ($this->pa_ht == 0) { |
391
|
|
|
$result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product); |
392
|
|
|
if ($result < 0) { |
393
|
|
|
return $result; |
394
|
|
|
} else { |
395
|
|
|
$this->pa_ht = $result; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
// Check parameters |
400
|
|
|
if ($this->product_type < 0) { |
401
|
|
|
return -1; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$this->db->begin(); |
405
|
|
|
|
406
|
|
|
// Insert line into database |
407
|
|
|
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'supplier_proposaldet'; |
408
|
|
|
$sql .= ' (fk_supplier_proposal, fk_parent_line, label, description, fk_product, product_type,'; |
409
|
|
|
$sql .= ' date_start, date_end,'; |
410
|
|
|
$sql .= ' fk_remise_except, qty, tva_tx, vat_src_code, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,'; |
411
|
|
|
$sql .= ' subprice, remise_percent, '; |
412
|
|
|
$sql .= ' info_bits, '; |
413
|
|
|
$sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,'; |
414
|
|
|
$sql .= ' ref_fourn,'; |
415
|
|
|
$sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc, fk_unit)'; |
416
|
|
|
$sql .= " VALUES (" . $this->fk_supplier_proposal . ","; |
417
|
|
|
$sql .= " " . ($this->fk_parent_line > 0 ? ((int) $this->fk_parent_line) : "null") . ","; |
418
|
|
|
$sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ","; |
419
|
|
|
$sql .= " '" . $this->db->escape($this->desc) . "',"; |
420
|
|
|
$sql .= " " . ($this->fk_product ? ((int) $this->fk_product) : "null") . ","; |
421
|
|
|
$sql .= " '" . $this->db->escape($this->product_type) . "',"; |
422
|
|
|
$sql .= " " . ($this->date_start ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ","; |
423
|
|
|
$sql .= " " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ","; |
424
|
|
|
$sql .= " " . ($this->fk_remise_except ? ((int) $this->fk_remise_except) : "null") . ","; |
425
|
|
|
$sql .= " " . price2num($this->qty, 'MS') . ","; |
426
|
|
|
$sql .= " " . price2num($this->tva_tx) . ","; |
427
|
|
|
$sql .= " '" . $this->db->escape($this->vat_src_code) . "',"; |
428
|
|
|
$sql .= " " . price2num($this->localtax1_tx) . ","; |
429
|
|
|
$sql .= " " . price2num($this->localtax2_tx) . ","; |
430
|
|
|
$sql .= " '" . $this->db->escape($this->localtax1_type) . "',"; |
431
|
|
|
$sql .= " '" . $this->db->escape($this->localtax2_type) . "',"; |
432
|
|
|
$sql .= " " . price2num($this->subprice, 'MU') . ","; |
433
|
|
|
$sql .= " " . ((float) $this->remise_percent) . ","; |
434
|
|
|
$sql .= " " . (isset($this->info_bits) ? ((int) $this->info_bits) : "null") . ","; |
435
|
|
|
$sql .= " " . price2num($this->total_ht, 'MT') . ","; |
436
|
|
|
$sql .= " " . price2num($this->total_tva, 'MT') . ","; |
437
|
|
|
$sql .= " " . price2num($this->total_localtax1, 'MT') . ","; |
438
|
|
|
$sql .= " " . price2num($this->total_localtax2, 'MT') . ","; |
439
|
|
|
$sql .= " " . price2num($this->total_ttc, 'MT') . ","; |
440
|
|
|
$sql .= " " . (!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null") . ","; |
441
|
|
|
$sql .= " " . (isset($this->pa_ht) ? price2num($this->pa_ht, 'MU') : "null") . ","; |
442
|
|
|
$sql .= ' ' . ((int) $this->special_code) . ','; |
443
|
|
|
$sql .= ' ' . ((int) $this->rang) . ','; |
444
|
|
|
$sql .= " '" . $this->db->escape($this->ref_fourn) . "'"; |
445
|
|
|
$sql .= ", " . ($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null'); |
446
|
|
|
$sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'"; |
447
|
|
|
$sql .= ", " . price2num($this->multicurrency_subprice, 'CU'); |
448
|
|
|
$sql .= ", " . price2num($this->multicurrency_total_ht, 'CT'); |
449
|
|
|
$sql .= ", " . price2num($this->multicurrency_total_tva, 'CT'); |
450
|
|
|
$sql .= ", " . price2num($this->multicurrency_total_ttc, 'CT'); |
451
|
|
|
$sql .= ", " . ($this->fk_unit ? ((int) $this->fk_unit) : 'null'); |
452
|
|
|
$sql .= ')'; |
453
|
|
|
|
454
|
|
|
dol_syslog(get_class($this) . '::insert', LOG_DEBUG); |
455
|
|
|
$resql = $this->db->query($sql); |
456
|
|
|
if ($resql) { |
457
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'supplier_proposaldet'); |
458
|
|
|
|
459
|
|
|
if (!$error) { |
460
|
|
|
$result = $this->insertExtraFields(); |
461
|
|
|
if ($result < 0) { |
462
|
|
|
$error++; |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
if (!$error && !$notrigger) { |
467
|
|
|
// Call trigger |
468
|
|
|
$result = $this->call_trigger('LINESUPPLIER_PROPOSAL_INSERT', $user); |
469
|
|
|
if ($result < 0) { |
470
|
|
|
$this->db->rollback(); |
471
|
|
|
return -1; |
472
|
|
|
} |
473
|
|
|
// End call triggers |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
$this->db->commit(); |
477
|
|
|
return 1; |
478
|
|
|
} else { |
479
|
|
|
$this->error = $this->db->error() . " sql=" . $sql; |
480
|
|
|
$this->db->rollback(); |
481
|
|
|
return -1; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Delete line in database |
487
|
|
|
* |
488
|
|
|
* @param User $user User making the deletion |
489
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
490
|
|
|
*/ |
491
|
|
|
public function delete($user) |
492
|
|
|
{ |
493
|
|
|
$error = 0; |
494
|
|
|
|
495
|
|
|
$this->db->begin(); |
496
|
|
|
|
497
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "supplier_proposaldet"; |
498
|
|
|
$sql .= " WHERE rowid = " . ((int) $this->id); |
499
|
|
|
|
500
|
|
|
if ($this->db->query($sql)) { |
501
|
|
|
// Remove extrafields |
502
|
|
|
if (!$error) { |
503
|
|
|
$result = $this->deleteExtraFields(); |
504
|
|
|
if ($result < 0) { |
505
|
|
|
$error++; |
506
|
|
|
dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR); |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
// Call trigger |
511
|
|
|
$result = $this->call_trigger('LINESUPPLIER_PROPOSAL_DELETE', $user); |
512
|
|
|
if ($result < 0) { |
513
|
|
|
$this->db->rollback(); |
514
|
|
|
return -1; |
515
|
|
|
} |
516
|
|
|
// End call triggers |
517
|
|
|
|
518
|
|
|
$this->db->commit(); |
519
|
|
|
|
520
|
|
|
return 1; |
521
|
|
|
} else { |
522
|
|
|
$this->error = $this->db->error() . " sql=" . $sql; |
523
|
|
|
$this->db->rollback(); |
524
|
|
|
return -1; |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Update propal line object into DB |
530
|
|
|
* |
531
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
532
|
|
|
* @return int Return integer <0 if ko, >0 if ok |
533
|
|
|
*/ |
534
|
|
|
public function update($notrigger = 0) |
535
|
|
|
{ |
536
|
|
|
global $conf, $langs, $user; |
537
|
|
|
|
538
|
|
|
$error = 0; |
539
|
|
|
|
540
|
|
|
// Clean parameters |
541
|
|
|
if (empty($this->tva_tx)) { |
542
|
|
|
$this->tva_tx = 0; |
543
|
|
|
} |
544
|
|
|
if (empty($this->localtax1_tx)) { |
545
|
|
|
$this->localtax1_tx = 0; |
546
|
|
|
} |
547
|
|
|
if (empty($this->localtax2_tx)) { |
548
|
|
|
$this->localtax2_tx = 0; |
549
|
|
|
} |
550
|
|
|
if (empty($this->total_localtax1)) { |
551
|
|
|
$this->total_localtax1 = 0; |
552
|
|
|
} |
553
|
|
|
if (empty($this->total_localtax2)) { |
554
|
|
|
$this->total_localtax2 = 0; |
555
|
|
|
} |
556
|
|
|
if (empty($this->localtax1_type)) { |
557
|
|
|
$this->localtax1_type = 0; |
558
|
|
|
} |
559
|
|
|
if (empty($this->localtax2_type)) { |
560
|
|
|
$this->localtax2_type = 0; |
561
|
|
|
} |
562
|
|
|
if (empty($this->marque_tx)) { |
563
|
|
|
$this->marque_tx = 0; |
564
|
|
|
} |
565
|
|
|
if (empty($this->marge_tx)) { |
566
|
|
|
$this->marge_tx = 0; |
567
|
|
|
} |
568
|
|
|
if (empty($this->remise_percent)) { |
569
|
|
|
$this->remise_percent = 0; |
570
|
|
|
} |
571
|
|
|
if (empty($this->info_bits)) { |
572
|
|
|
$this->info_bits = 0; |
573
|
|
|
} |
574
|
|
|
if (empty($this->special_code)) { |
575
|
|
|
$this->special_code = 0; |
576
|
|
|
} |
577
|
|
|
if (empty($this->fk_parent_line)) { |
578
|
|
|
$this->fk_parent_line = 0; |
579
|
|
|
} |
580
|
|
|
if (empty($this->fk_fournprice)) { |
581
|
|
|
$this->fk_fournprice = 0; |
582
|
|
|
} |
583
|
|
|
if (empty($this->fk_unit)) { |
584
|
|
|
$this->fk_unit = 0; |
585
|
|
|
} |
586
|
|
|
if (empty($this->subprice)) { |
587
|
|
|
$this->subprice = 0; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
if (empty($this->pa_ht)) { |
591
|
|
|
$this->pa_ht = 0; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
// if buy price not defined, define buyprice as configured in margin admin |
595
|
|
|
if ($this->pa_ht == 0) { |
596
|
|
|
$result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product); |
597
|
|
|
if ($result < 0) { |
598
|
|
|
return $result; |
599
|
|
|
} else { |
600
|
|
|
$this->pa_ht = $result; |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
$this->db->begin(); |
605
|
|
|
|
606
|
|
|
// Mise a jour ligne en base |
607
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "supplier_proposaldet SET"; |
608
|
|
|
$sql .= " description='" . $this->db->escape($this->desc) . "'"; |
609
|
|
|
$sql .= " , label=" . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null"); |
610
|
|
|
$sql .= " , product_type=" . ((int) $this->product_type); |
611
|
|
|
$sql .= " , date_start=" . ($this->date_start ? "'" . $this->db->idate($this->date_start) . "'" : "null"); |
612
|
|
|
$sql .= " , date_end=" . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null"); |
613
|
|
|
$sql .= " , tva_tx='" . price2num($this->tva_tx) . "'"; |
614
|
|
|
$sql .= " , localtax1_tx=" . price2num($this->localtax1_tx); |
615
|
|
|
$sql .= " , localtax2_tx=" . price2num($this->localtax2_tx); |
616
|
|
|
$sql .= " , localtax1_type='" . $this->db->escape($this->localtax1_type) . "'"; |
617
|
|
|
$sql .= " , localtax2_type='" . $this->db->escape($this->localtax2_type) . "'"; |
618
|
|
|
$sql .= " , qty='" . price2num($this->qty) . "'"; |
619
|
|
|
$sql .= " , subprice=" . price2num($this->subprice); |
620
|
|
|
$sql .= " , remise_percent=" . price2num($this->remise_percent); |
621
|
|
|
$sql .= " , info_bits='" . $this->db->escape($this->info_bits) . "'"; |
622
|
|
|
if (empty($this->skip_update_total)) { |
623
|
|
|
$sql .= " , total_ht=" . price2num($this->total_ht); |
624
|
|
|
$sql .= " , total_tva=" . price2num($this->total_tva); |
625
|
|
|
$sql .= " , total_ttc=" . price2num($this->total_ttc); |
626
|
|
|
$sql .= " , total_localtax1=" . price2num($this->total_localtax1); |
627
|
|
|
$sql .= " , total_localtax2=" . price2num($this->total_localtax2); |
628
|
|
|
} |
629
|
|
|
$sql .= " , fk_product_fournisseur_price=" . (!empty($this->fk_fournprice) ? "'" . $this->db->escape($this->fk_fournprice) . "'" : "null"); |
630
|
|
|
$sql .= " , buy_price_ht=" . price2num($this->pa_ht); |
631
|
|
|
$sql .= " , special_code=" . ((int) $this->special_code); |
632
|
|
|
$sql .= " , fk_parent_line=" . ($this->fk_parent_line > 0 ? $this->fk_parent_line : "null"); |
633
|
|
|
if (!empty($this->rang)) { |
634
|
|
|
$sql .= ", rang=" . ((int) $this->rang); |
635
|
|
|
} |
636
|
|
|
$sql .= " , ref_fourn=" . (!empty($this->ref_fourn) ? "'" . $this->db->escape($this->ref_fourn) . "'" : "null"); |
637
|
|
|
$sql .= " , fk_unit=" . ($this->fk_unit ? $this->fk_unit : 'null'); |
638
|
|
|
|
639
|
|
|
// Multicurrency |
640
|
|
|
$sql .= " , multicurrency_subprice=" . price2num($this->multicurrency_subprice); |
641
|
|
|
$sql .= " , multicurrency_total_ht=" . price2num($this->multicurrency_total_ht); |
642
|
|
|
$sql .= " , multicurrency_total_tva=" . price2num($this->multicurrency_total_tva); |
643
|
|
|
$sql .= " , multicurrency_total_ttc=" . price2num($this->multicurrency_total_ttc); |
644
|
|
|
|
645
|
|
|
$sql .= " WHERE rowid = " . ((int) $this->id); |
646
|
|
|
|
647
|
|
|
dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
648
|
|
|
$resql = $this->db->query($sql); |
649
|
|
|
if ($resql) { |
650
|
|
|
if (!$error) { |
651
|
|
|
$result = $this->insertExtraFields(); |
652
|
|
|
if ($result < 0) { |
653
|
|
|
$error++; |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
if (!$error && !$notrigger) { |
658
|
|
|
// Call trigger |
659
|
|
|
$result = $this->call_trigger('LINESUPPLIER_PROPOSAL_MODIFY', $user); |
660
|
|
|
if ($result < 0) { |
661
|
|
|
$this->db->rollback(); |
662
|
|
|
return -1; |
663
|
|
|
} |
664
|
|
|
// End call triggers |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
$this->db->commit(); |
668
|
|
|
return 1; |
669
|
|
|
} else { |
670
|
|
|
$this->error = $this->db->error(); |
671
|
|
|
$this->db->rollback(); |
672
|
|
|
return -2; |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
677
|
|
|
/** |
678
|
|
|
* Update DB line fields total_xxx |
679
|
|
|
* Used by migration |
680
|
|
|
* |
681
|
|
|
* @return int Return integer <0 if ko, >0 if ok |
682
|
|
|
*/ |
683
|
|
|
public function update_total() |
684
|
|
|
{ |
685
|
|
|
// phpcs:enable |
686
|
|
|
$this->db->begin(); |
687
|
|
|
|
688
|
|
|
// Mise a jour ligne en base |
689
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "supplier_proposaldet SET"; |
690
|
|
|
$sql .= " total_ht=" . price2num($this->total_ht, 'MT'); |
691
|
|
|
$sql .= ",total_tva=" . price2num($this->total_tva, 'MT'); |
692
|
|
|
$sql .= ",total_ttc=" . price2num($this->total_ttc, 'MT'); |
693
|
|
|
$sql .= " WHERE rowid = " . ((int) $this->id); |
694
|
|
|
|
695
|
|
|
dol_syslog("SupplierProposalLine::update_total", LOG_DEBUG); |
696
|
|
|
|
697
|
|
|
$resql = $this->db->query($sql); |
698
|
|
|
if ($resql) { |
699
|
|
|
$this->db->commit(); |
700
|
|
|
return 1; |
701
|
|
|
} else { |
702
|
|
|
$this->error = $this->db->error(); |
703
|
|
|
$this->db->rollback(); |
704
|
|
|
return -2; |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
} |
708
|
|
|
|