-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
fournisseur.product.class.php
511 lines (457 loc) · 18.4 KB
/
fournisseur.product.class.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
<?php
/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/fourn/class/fournisseur.product.class.php
* \ingroup produit
* \brief File of class to manage predefined suppliers products
*/
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
/**
* Class to manage predefined suppliers products
*/
class ProductFournisseur extends Product
{
var $db;
var $error;
var $product_fourn_price_id; // id of ligne product-supplier
var $id; // product id
var $fourn_ref; // ref supplier
var $fourn_qty; // quantity for price
var $fourn_price; // price for quantity
var $fourn_remise_percent; // discount for quantity (percent)
var $fourn_remise; // discount for quantity (amount)
var $product_fourn_id; // supplier id
var $fk_availability; // availability delay
var $fourn_unitprice;
var $fourn_tva_npr;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
}
/**
* Remove all prices for this couple supplier-product
*
* @param int $id_fourn Supplier Id
* @return int < 0 if error, > 0 if ok
*/
function remove_fournisseur($id_fourn)
{
$ok=1;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn;
dol_syslog(get_class($this)."::remove_fournisseur sql=".$sql);
$resql2=$this->db->query($sql);
if (! $resql2)
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::remove_fournisseur ".$this->error, LOG_ERR);
$ok=0;
}
if ($ok)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Remove a price for a couple supplier-product
*
* @param int $rowid Line id of price
* @return int <0 if KO, >0 if OK
*/
function remove_product_fournisseur_price($rowid)
{
global $conf;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::remove_product_fournisseur_price sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::remove_product_fournisseur_price ".$this->error,LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Modify the purchase price for a supplier
*
* @param int $qty Min quantity for which price is valid
* @param float $buyprice Purchase price for the quantity min
* @param User $user Object user user made changes
* @param string $price_base_type HT or TTC
* @param Societe $fourn Supplier
* @param int $availability Product availability
* @param string $ref_fourn Supplier ref
* @param float $tva_tx VAT rate
* @param string $charges costs affering to product
* @param float $remise_percent Discount regarding qty (percent)
* @param float $remise Discount regarding qty (amount)
* @param int $newnpr Set NPR or not
* @return int <0 if KO, >=0 if OK
*/
function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0)
{
global $conf,$mysoc;
// Clean parameter
if (empty($qty)) $qty=0;
if (empty($buyprice)) $buyprice=0;
if (empty($charges)) $charges=0;
if (empty($availability)) $availability=0;
if (empty($remise_percent)) $remise_percent=0;
if ($price_base_type == 'TTC')
{
//$ttx = get_default_tva($fourn,$mysoc,$this->id); // We must use the VAT rate defined by user and not calculate it
$ttx = $tva_tx;
$buyprice = $buyprice/(1+($ttx/100));
}
$buyprice=price2num($buyprice,'MU');
$charges=price2num($charges,'MU');
$qty=price2num($qty);
$error=0;
$unitBuyPrice = price2num($buyprice/$qty,'MU');
$unitCharges = price2num($charges/$qty,'MU');
$now=dol_now();
$this->db->begin();
if ($this->product_fourn_price_id)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " SET fk_user = " . $user->id." ,";
$sql.= " ref_fourn = \"" . $this->db->escape($ref_fourn) . "\",";
$sql.= " price = ".price2num($buyprice).",";
$sql.= " quantity = ".$qty.",";
$sql.= " remise_percent = ".$remise_percent.",";
$sql.= " remise = ".$remise.",";
$sql.= " unitprice = ".$unitBuyPrice.",";
$sql.= " unitcharges = ".$unitCharges.",";
$sql.= " tva_tx = ".$tva_tx.",";
$sql.= " fk_availability = ".$availability.",";
$sql.= " entity = ".$conf->entity.",";
$sql.= " info_bits = ".$newnpr.",";
$sql.= " charges = ".$charges;
$sql.= " WHERE rowid = ".$this->product_fourn_price_id;
// TODO Add price_base_type and price_ttc
dol_syslog(get_class($this).'::update_buyprice sql='.$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 0;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -2;
}
}
else
{
// Delete price for this quantity
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE fk_soc = ".$fourn->id." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".$qty." AND entity = ".$conf->entity;
dol_syslog(get_class($this).'::update_buyprice sql='.$sql);
$resql=$this->db->query($sql);
if ($resql)
{
// Add price for this quantity to supplier
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
$sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, entity, info_bits)";
$sql.= " values('".$this->db->idate($now)."',";
$sql.= " ".$this->id.",";
$sql.= " ".$fourn->id.",";
$sql.= " '".$this->db->escape($ref_fourn)."',";
$sql.= " ".$user->id.",";
$sql.= " ".$buyprice.",";
$sql.= " ".$qty.",";
$sql.= " ".$remise_percent.",";
$sql.= " ".$remise.",";
$sql.= " ".$unitBuyPrice.",";
$sql.= " ".$tva_tx.",";
$sql.= " ".$charges.",";
$sql.= " ".$unitCharges.",";
$sql.= " ".$availability.",";
$sql.= " ".$newnpr.",";
$sql.= $conf->entity;
$sql.=")";
dol_syslog(get_class($this)."::update_buyprice sql=".$sql);
if (! $this->db->query($sql))
{
$error++;
}
/*if (! $error)
{
// Ajoute modif dans table log
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
$sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)";
$sql.= "values('".$this->db->idate($now)."',";
$sql.= " ".$this->product_fourn_id.",";
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty;
$sql.=")";
$resql=$this->db->query($sql);
if (! $resql)
{
$error++;
}
}
*/
if (! $error)
{
$this->db->commit();
return 0;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -1;
}
}
}
/**
* Loads the price information of a provider
*
* @param int $rowid Line id
* @return int < 0 if KO, 0 if OK but not found, > 0 if OK
*/
function fetch_product_fournisseur_price($rowid)
{
$sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability,";
$sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges"; // , pfp.recuperableonly as fourn_tva_npr"; FIXME this field not exist in llx_product_fournisseur_price
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE pfp.rowid = ".$rowid;
dol_syslog(get_class($this)."::fetch_product_fournisseur_price sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
if ($obj)
{
$this->product_fourn_price_id = $rowid;
$this->fourn_ref = $obj->ref_fourn;
$this->fourn_price = $obj->price;
$this->fourn_charges = $obj->charges;
$this->fourn_qty = $obj->quantity;
$this->fourn_remise_percent = $obj->remise_percent;
$this->fourn_remise = $obj->remise;
$this->fourn_unitprice = $obj->unitprice;
$this->fourn_unitcharges = $obj->unitcharges;
$this->tva_tx = $obj->tva_tx;
$this->product_id = $obj->fk_product; // deprecated
$this->fk_product = $obj->fk_product;
$this->fk_availability = $obj->fk_availability;
//$this->fourn_tva_npr = $obj->fourn_tva_npr; // FIXME this field not exist in llx_product_fournisseur_price
return 1;
}
else
{
return 0;
}
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::fetch_product_fournisseur_price error=".$this->error, LOG_ERR);
return -1;
}
}
/**
* List all supplier prices of a product
*
* @param int $prodid Id of product
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @return array Array of Products with new properties to define supplier price
*/
function list_product_fournisseur_price($prodid, $sortfield='', $sortorder='')
{
global $conf;
$sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
$sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn, pfp.fk_product as product_fourn_id,";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.unitcharges, pfp.info_bits";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= ", ".MAIN_DB_PREFIX."societe as s";
$sql.= " WHERE pfp.entity IN (".getEntity('product', 1).")";
$sql.= " AND pfp.fk_soc = s.rowid";
$sql.= " AND pfp.fk_product = ".$prodid;
if (empty($sortfield)) $sql.= " ORDER BY s.nom, pfp.quantity, pfp.price";
else $sql.= $this->db->order($sortfield,$sortorder);
dol_syslog(get_class($this)."::list_product_fournisseur_price sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$retarray = array();
while ($record = $this->db->fetch_array($resql))
{
//define base attribute
$prodfourn = new ProductFournisseur($this->db);
$prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
$prodfourn->product_fourn_id = $record["product_fourn_id"];
$prodfourn->fourn_ref = $record["ref_fourn"];
$prodfourn->fourn_price = $record["price"];
$prodfourn->fourn_qty = $record["quantity"];
$prodfourn->fourn_remise_percent = $record["remise_percent"];
$prodfourn->fourn_remise = $record["remise"];
$prodfourn->fourn_unitprice = $record["unitprice"];
$prodfourn->fourn_charges = $record["charges"];
$prodfourn->fourn_unitcharges = $record["unitcharges"];
$prodfourn->fourn_tva_tx = $record["tva_tx"];
$prodfourn->fourn_id = $record["fourn_id"];
$prodfourn->fourn_name = $record["supplier_name"];
$prodfourn->fk_availability = $record["fk_availability"];
$prodfourn->id = $prodid;
$prodfourn->fourn_tva_npr = $record["info_bits"];
if (!isset($prodfourn->fourn_unitprice))
{
if ($prodfourn->fourn_qty!=0)
{
$prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price/$prodfourn->fourn_qty,'MU');
}
else
{
$prodfourn->fourn_unitprice="";
}
}
$retarray[]=$prodfourn;
}
$this->db->free($resql);
return $retarray;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::list_product_fournisseur_price error=".$this->error, LOG_ERR);
return -1;
}
}
/**
* Load properties for minimum price
*
* @param int $prodid Product id
* @return int <0 if KO, >0 if OK
*/
function find_min_price_product_fournisseur($prodid)
{
global $conf;
$this->product_fourn_price_id = '';
$this->product_fourn_id = '';
$this->fourn_ref = '';
$this->fourn_price = '';
$this->fourn_qty = '';
$this->fourn_remise_percent = '';
$this->fourn_remise = '';
$this->fourn_unitprice = '';
$this->fourn_id = '';
$this->fourn_name = '';
$this->id = '';
$sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
$sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges, pfp.unitcharges";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
$sql.= " AND pfp.fk_product = ".$prodid;
$sql.= " AND pfp.fk_soc = s.rowid";
$sql.= " ORDER BY pfp.unitprice";
$sql.= $this->db->plimit(1);
dol_syslog(get_class($this)."::find_min_price_product_fournisseur sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$record = $this->db->fetch_array($resql);
$this->product_fourn_price_id = $record["product_fourn_price_id"];
$this->fourn_ref = $record["ref_fourn"];
$this->fourn_price = $record["price"];
$this->fourn_qty = $record["quantity"];
$this->fourn_remise_percent = $record["remise_percent"];
$this->fourn_remise = $record["remise"];
$this->fourn_unitprice = $record["unitprice"];
$this->fourn_charges = $record["charges"];
$this->fourn_unitcharges = $record["unitcharges"];
$this->fourn_tva_tx = $record["tva_tx"];
$this->fourn_id = $record["fourn_id"];
$this->fourn_name = $record["supplier_name"];
$this->id = $prodid;
$this->db->free($resql);
return 1;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::find_min_price_product_fournisseur error=".$this->error, LOG_ERR);
return -1;
}
}
/**
* Display supplier of product
*
* @param int $withpicto Add picto
* @param string $option Target of link ('', 'customer', 'prospect', 'supplier')
* @return string String with supplier price
* TODO Remove this method. Use getNomUrl directly.
*/
function getSocNomUrl($withpicto=0,$option='supplier')
{
$cust = new Fournisseur($this->db);
$cust->fetch($this->fourn_id);
return $cust->getNomUrl($withpicto,$option);
}
/**
* Display price of product
*
* @return string String with supplier price
*/
function display_price_product_fournisseur()
{
global $langs;
$langs->load("suppliers");
$out=price($this->fourn_unitprice).' '.$langs->trans("HT").' ('.$langs->trans("Supplier").': '.$this->getSocNomUrl(1).' / '.$langs->trans("SupplierRef").': '.$this->fourn_ref.')';
return $out;
}
}
?>