178 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.vat_src_code, cd.tva_tx, cd.special_code,';
179 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
180 $sql .=
' cd.remise, cd.remise_percent, cd.subprice, cd.subprice_ttc,';
181 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
182 $sql .=
' cd.total_localtax1, cd.total_localtax2, cd.fk_parent_line, cd.rang,';
183 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch, p.barcode as product_barcode, p.stockable_product,';
184 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit, cd.extraparams,';
185 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code,';
186 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_subprice_ttc, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
187 $sql .=
' c.fk_soc as socid';
188 $sql .=
' FROM '.$this->db->prefix().
'commande_fournisseur as c, '.$this->db->prefix().
'commande_fournisseurdet as cd';
189 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON cd.fk_product = p.rowid';
190 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
192 $result = $this->db->query($sql);
194 $objp = $this->db->fetch_object($result);
197 $this->rowid = $objp->rowid;
198 $this->
id = $objp->rowid;
199 $this->fk_commande = $objp->fk_commande;
200 $this->desc = $objp->description;
202 $this->qty = $objp->qty;
203 $this->ref_fourn = $objp->ref_supplier;
204 $this->ref_supplier = $objp->ref_supplier;
205 $this->subprice = $objp->subprice;
206 $this->subprice_ttc = $objp->subprice_ttc;
207 $this->vat_src_code = $objp->vat_src_code;
208 $this->tva_tx = $objp->tva_tx;
209 $this->localtax1_tx = $objp->localtax1_tx;
210 $this->localtax2_tx = $objp->localtax2_tx;
211 $this->localtax1_type = $objp->localtax1_type;
212 $this->localtax2_type = $objp->localtax2_type;
213 $this->remise = $objp->remise;
214 $this->remise_percent = $objp->remise_percent;
215 $this->fk_product = $objp->fk_product;
216 $this->info_bits = $objp->info_bits;
217 $this->total_ht = $objp->total_ht;
218 $this->total_tva = $objp->total_tva;
219 $this->total_localtax1 = $objp->total_localtax1;
220 $this->total_localtax2 = $objp->total_localtax2;
221 $this->total_ttc = $objp->total_ttc;
222 $this->product_type = $objp->product_type;
223 $this->special_code = $objp->special_code;
224 $this->fk_parent_line = $objp->fk_parent_line;
225 $this->rang = $objp->rang;
227 $this->
ref = $objp->product_ref;
229 $this->product_ref = $objp->product_ref;
230 $this->product_label = $objp->product_label;
231 $this->product_desc = $objp->product_desc;
232 $this->product_tobatch = $objp->product_tobatch;
233 $this->product_barcode = $objp->product_barcode;
234 $this->stockable_product = $objp->stockable_product;
241 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.$this->db->prefix().
"product_fournisseur_price";
242 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'productsupplierprice').
")";
243 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
244 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
245 $sqlsearchpackage .=
" AND quantity <= ".abs((
float) $objp->qty);
246 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".abs((
float) $objp->qty).
")";
247 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
248 $sqlsearchpackage .=
" ORDER BY packaging ASC";
249 $sqlsearchpackage .=
" LIMIT 1";
251 $resqlsearchpackage = $this->db->query($sqlsearchpackage);
252 if ($resqlsearchpackage) {
253 $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
254 if ($objsearchpackage) {
255 $this->fk_fournprice = $objsearchpackage->rowid;
256 $this->packaging = (float) $objsearchpackage->packaging;
259 $this->error = $this->db->lasterror();
264 $this->date_start = $this->db->jdate($objp->date_start);
265 $this->date_end = $this->db->jdate($objp->date_end);
266 $this->fk_unit = $objp->fk_unit;
268 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
270 $this->fk_multicurrency = $objp->fk_multicurrency;
271 $this->multicurrency_code = $objp->multicurrency_code;
272 $this->multicurrency_subprice = $objp->multicurrency_subprice;
273 $this->multicurrency_subprice_ttc = $objp->multicurrency_subprice_ttc;
274 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
275 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
276 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
280 $this->db->free($result);
283 $this->error =
'Supplier order line with id='.$rowid.
' not found';
284 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
288 dol_print_error($this->db);
305 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
308 if (empty($this->tva_tx)) {
311 if (empty($this->localtax1_tx)) {
312 $this->localtax1_tx = 0;
314 if (empty($this->localtax2_tx)) {
315 $this->localtax2_tx = 0;
317 if (empty($this->localtax1_type)) {
318 $this->localtax1_type =
'0';
320 if (empty($this->localtax2_type)) {
321 $this->localtax2_type =
'0';
323 if (empty($this->total_localtax1)) {
324 $this->total_localtax1 = 0;
326 if (empty($this->total_localtax2)) {
327 $this->total_localtax2 = 0;
329 if (empty($this->rang)) {
332 if (empty($this->remise_percent)) {
333 $this->remise_percent = 0;
335 if (empty($this->subprice_ttc)) {
336 $this->subprice_ttc = 0;
338 if (empty($this->info_bits)) {
339 $this->info_bits = 0;
341 if (empty($this->special_code)) {
342 $this->special_code = 0;
344 if (empty($this->fk_parent_line)) {
345 $this->fk_parent_line = 0;
347 if (empty($this->pa_ht)) {
352 if (!empty($this->multicurrency_code)) {
355 if (empty($this->fk_multicurrency)) {
356 $this->multicurrency_code =
$conf->currency;
357 $this->fk_multicurrency = 0;
358 $this->multicurrency_tx = 1;
362 if ($this->product_type < 0) {
369 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element;
370 $sql .=
" (fk_commande, label, description, date_start, date_end,";
371 $sql .=
" fk_product, product_type, special_code, rang,";
372 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc, ref,";
373 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
374 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_subprice_ttc, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
375 $sql .=
" fk_parent_line)";
376 $sql .=
" VALUES (".((int) $this->fk_commande).
", '".$this->db->escape($this->label).
"','".$this->db->escape($this->desc).
"',";
377 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
378 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
379 if ($this->fk_product) {
380 $sql .= ((int) $this->fk_product).
",";
384 $sql .= ((int) $this->product_type).
",";
385 $sql .= ((int) $this->special_code).
",";
386 $sql .=
"'".$this->db->escape((
string) $this->rang).
"',";
387 $sql .=
"'".$this->db->escape((
string) $this->qty).
"', ";
388 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape((
string) $this->vat_src_code).
"'").
",";
389 $sql .=
" ".price2num($this->tva_tx).
", ";
390 $sql .=
" ".price2num($this->localtax1_tx).
",";
391 $sql .=
" ".price2num($this->localtax2_tx).
",";
392 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
393 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
394 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", ".
price2num($this->subprice_ttc,
'MU').
", '".$this->db->escape($this->ref_supplier).
"',";
395 $sql .=
" ".price2num($this->total_ht).
",";
396 $sql .=
" ".price2num($this->total_tva).
",";
397 $sql .=
" ".price2num($this->total_localtax1).
",";
398 $sql .=
" ".price2num($this->total_localtax2).
",";
399 $sql .=
" ".price2num($this->total_ttc).
",";
400 $sql .= ($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
401 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
402 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
403 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
404 $sql .=
", ".($this->multicurrency_subprice_ttc ?
price2num($this->multicurrency_subprice_ttc) :
'0');
405 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
406 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
407 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
408 $sql .=
", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? ((
int) $this->fk_parent_line) :
'null');
411 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
412 $resql = $this->db->query($sql);
414 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
415 $this->rowid = $this->id;
424 if (!$error && !$notrigger) {
426 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
438 foreach ($this->errors as $errmsg) {
439 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
440 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
442 $this->db->rollback();
445 $this->errors[] = $this->db->error();
446 $this->db->rollback();
464 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
465 $sql .=
" description='".$this->db->escape($this->desc).
"'";
466 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
467 $sql .=
", subprice = ".((float)
price2num($this->subprice));
468 $sql .=
", subprice_ttc = ".((float)
price2num($this->subprice_ttc));
469 $sql .=
", remise_percent = ".((float)
price2num($this->remise_percent));
470 $sql .=
", vat_src_code = '".$this->db->escape((
string) $this->vat_src_code).
"'";
471 $sql .=
", tva_tx = ".((float)
price2num($this->tva_tx));
472 $sql .=
", localtax1_tx = ".((float)
price2num($this->localtax1_tx));
473 $sql .=
", localtax2_tx = ".((float)
price2num($this->localtax2_tx));
474 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
475 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
476 $sql .=
", qty = ".((float)
price2num($this->qty));
477 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
478 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
479 $sql .=
", info_bits = ".((int) $this->info_bits);
480 $sql .=
", total_ht = ".((float)
price2num($this->total_ht));
481 $sql .=
", total_tva = ".((float)
price2num($this->total_tva));
482 $sql .=
", total_localtax1 = ".((float)
price2num($this->total_localtax1));
483 $sql .=
", total_localtax2 = ".((float)
price2num($this->total_localtax2));
484 $sql .=
", total_ttc = ".((float)
price2num($this->total_ttc));
485 $sql .=
", product_type = ".((int) $this->product_type);
486 $sql .=
", special_code = ".((int) $this->special_code);
487 $sql .=
", fk_unit = ".($this->fk_unit ? ((int) $this->fk_unit) :
"null");
488 if (!empty($this->rang)) {
489 $sql .=
", rang=".((int) $this->rang);
492 $sql .=
", multicurrency_subprice = ".((float)
price2num($this->multicurrency_subprice));
493 $sql .=
", multicurrency_subprice_ttc = ".((float)
price2num($this->multicurrency_subprice_ttc));
494 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht));
495 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva));
496 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc));
498 $sql .=
" WHERE rowid = ".((int) $this->
id);
500 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
501 $resql = $this->db->query($sql);
510 if (!$error && !$notrigger) {
512 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
514 $this->db->rollback();
524 $this->db->rollback();
528 $this->error = $this->db->lasterror();
529 $this->db->rollback();