166 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,';
167 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
168 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,';
169 $sql .=
' cd.fk_unit,';
170 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
171 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
172 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
173 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
174 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
175 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
176 $result = $this->db->query($sql);
178 $objp = $this->db->fetch_object($result);
181 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
185 $this->
rowid = $objp->rowid;
186 $this->
id = $objp->rowid;
187 $this->fk_commande = $objp->fk_commande;
188 $this->fk_parent_line = $objp->fk_parent_line;
189 $this->label = $objp->custom_label;
190 $this->desc = $objp->description;
191 $this->qty = $objp->qty;
192 $this->
price = $objp->price;
193 $this->subprice = $objp->subprice;
194 $this->ref_ext = $objp->ref_ext;
195 $this->vat_src_code = $objp->vat_src_code;
196 $this->tva_tx = $objp->tva_tx;
197 $this->localtax1_tx = $objp->localtax1_tx;
198 $this->localtax2_tx = $objp->localtax2_tx;
199 $this->remise = $objp->remise;
200 $this->remise_percent = $objp->remise_percent;
201 $this->fk_remise_except = $objp->fk_remise_except;
202 $this->fk_product = $objp->fk_product;
203 $this->product_type = $objp->product_type;
204 $this->info_bits = $objp->info_bits;
205 $this->special_code = $objp->special_code;
206 $this->total_ht = $objp->total_ht;
207 $this->total_tva = $objp->total_tva;
208 $this->total_localtax1 = $objp->total_localtax1;
209 $this->total_localtax2 = $objp->total_localtax2;
210 $this->total_ttc = $objp->total_ttc;
211 $this->fk_fournprice = $objp->fk_fournprice;
212 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
213 $this->pa_ht = $marginInfos[0];
214 $this->marge_tx = $marginInfos[1];
215 $this->marque_tx = $marginInfos[2];
216 $this->special_code = $objp->special_code;
217 $this->rang = $objp->rang;
219 $this->
ref = $objp->product_ref;
221 $this->product_ref = $objp->product_ref;
222 $this->product_label = $objp->product_label;
223 $this->product_desc = $objp->product_desc;
224 $this->product_tobatch = $objp->product_tobatch;
225 $this->fk_unit = $objp->fk_unit;
227 $this->date_start = $this->db->jdate($objp->date_start);
228 $this->date_end = $this->db->jdate($objp->date_end);
230 $this->fk_multicurrency = $objp->fk_multicurrency;
231 $this->multicurrency_code = $objp->multicurrency_code;
232 $this->multicurrency_subprice = $objp->multicurrency_subprice;
233 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
234 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
235 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
239 $this->db->free($result);
243 $this->error = $this->db->lasterror();
255 public function delete(
User $user, $notrigger = 0)
257 global $conf, $langs;
261 if (empty($this->
id) && !empty($this->
rowid)) {
262 $this->
id = $this->rowid;
266 $sqlCheckShipmentLine =
"SELECT";
267 $sqlCheckShipmentLine .=
" ed.rowid";
268 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
269 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
271 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
272 if (!$resqlCheckShipmentLine) {
274 $this->error = $this->db->lasterror();
275 $this->errors[] = $this->error;
277 $langs->load(
'errors');
278 $num = $this->db->num_rows($resqlCheckShipmentLine);
281 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
282 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
283 $this->errors[] = $this->error;
285 $this->db->free($resqlCheckShipmentLine);
288 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
296 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
304 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
307 $resql = $this->db->query($sql);
309 $this->error = $this->db->lasterror();
319 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
328 foreach ($this->errors as $errmsg) {
329 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
330 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
332 $this->db->rollback();
343 public function insert($user =
null, $notrigger = 0)
347 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
349 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
352 if (empty($this->tva_tx)) {
355 if (empty($this->localtax1_tx)) {
356 $this->localtax1_tx = 0;
358 if (empty($this->localtax2_tx)) {
359 $this->localtax2_tx = 0;
361 if (empty($this->localtax1_type)) {
362 $this->localtax1_type = 0;
364 if (empty($this->localtax2_type)) {
365 $this->localtax2_type = 0;
367 if (empty($this->total_localtax1)) {
368 $this->total_localtax1 = 0;
370 if (empty($this->total_localtax2)) {
371 $this->total_localtax2 = 0;
373 if (empty($this->rang)) {
376 if (empty($this->remise_percent)) {
377 $this->remise_percent = 0;
379 if (empty($this->info_bits)) {
380 $this->info_bits = 0;
382 if (empty($this->special_code)) {
383 $this->special_code = 0;
385 if (empty($this->fk_parent_line)) {
386 $this->fk_parent_line = 0;
388 if (empty($this->pa_ht)) {
391 if (empty($this->ref_ext)) {
396 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
397 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
401 $this->pa_ht = $result;
406 if ($this->product_type < 0) {
413 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
414 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
415 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
416 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
417 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
418 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
420 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
422 $sql .=
" VALUES (".$this->fk_commande.
",";
423 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
424 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
425 $sql .=
" '".$this->db->escape($this->desc).
"',";
426 $sql .=
" '".price2num($this->qty).
"',";
427 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
428 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
429 $sql .=
" '".price2num($this->tva_tx).
"',";
430 $sql .=
" '".price2num($this->localtax1_tx).
"',";
431 $sql .=
" '".price2num($this->localtax2_tx).
"',";
432 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
433 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
434 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
435 $sql .=
" '".$this->db->escape($this->product_type).
"',";
436 $sql .=
" '".price2num($this->remise_percent).
"',";
437 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
438 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
439 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
440 $sql .=
' '.((int) $this->special_code).
',';
441 $sql .=
' '.((int) $this->rang).
',';
442 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
443 $sql .=
' '.price2num($this->pa_ht).
',';
444 $sql .=
" ".((int) $this->info_bits).
",";
445 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
446 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
447 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
448 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
449 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
450 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
451 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
452 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
453 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
454 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
455 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
456 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
457 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
458 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
461 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
462 $resql = $this->db->query($sql);
464 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
465 $this->
rowid = $this->id;
474 if (!$error && !$notrigger) {
476 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
488 foreach ($this->errors as $errmsg) {
489 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
490 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
492 $this->db->rollback();
495 $this->error = $this->db->error();
496 $this->db->rollback();
512 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
515 if (empty($this->tva_tx)) {
518 if (empty($this->localtax1_tx)) {
519 $this->localtax1_tx = 0;
521 if (empty($this->localtax2_tx)) {
522 $this->localtax2_tx = 0;
524 if (empty($this->localtax1_type)) {
525 $this->localtax1_type = 0;
527 if (empty($this->localtax2_type)) {
528 $this->localtax2_type = 0;
530 if (empty($this->qty)) {
533 if (empty($this->total_localtax1)) {
534 $this->total_localtax1 = 0;
536 if (empty($this->total_localtax2)) {
537 $this->total_localtax2 = 0;
539 if (empty($this->marque_tx)) {
540 $this->marque_tx = 0;
542 if (empty($this->marge_tx)) {
545 if (empty($this->remise_percent)) {
546 $this->remise_percent = 0;
548 if (empty($this->remise)) {
551 if (empty($this->info_bits)) {
552 $this->info_bits = 0;
554 if (empty($this->special_code)) {
555 $this->special_code = 0;
557 if (empty($this->product_type)) {
558 $this->product_type = 0;
560 if (empty($this->fk_parent_line)) {
561 $this->fk_parent_line = 0;
563 if (empty($this->pa_ht)) {
566 if (empty($this->ref_ext)) {
571 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
572 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
576 $this->pa_ht = $result;
583 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
584 $sql .=
" description='".$this->db->escape($this->desc).
"'";
585 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
586 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
587 $sql .=
" , tva_tx=".price2num($this->tva_tx);
588 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
589 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
590 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
591 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
592 $sql .=
" , qty=".price2num($this->qty);
593 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
594 $sql .=
" , subprice=".price2num($this->subprice);
595 $sql .=
" , remise_percent=".price2num($this->remise_percent);
596 $sql .=
" , price=".price2num($this->
price);
597 $sql .=
" , remise=".price2num($this->remise);
598 if (empty($this->skip_update_total)) {
599 $sql .=
" , total_ht=".price2num($this->total_ht);
600 $sql .=
" , total_tva=".price2num($this->total_tva);
601 $sql .=
" , total_ttc=".price2num($this->total_ttc);
602 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
603 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
605 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
606 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
607 $sql .=
" , info_bits=".((int) $this->info_bits);
608 $sql .=
" , special_code=".((int) $this->special_code);
609 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
610 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
611 $sql .=
" , product_type=".$this->product_type;
612 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
613 if (!empty($this->rang)) {
614 $sql .=
", rang=".((int) $this->rang);
616 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
619 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
620 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
621 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
622 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
624 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
626 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
627 $resql = $this->db->query($sql);
630 $this->
id = $this->rowid;
637 if (!$error && !$notrigger) {
639 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
651 foreach ($this->errors as $errmsg) {
652 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
653 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
655 $this->db->rollback();
658 $this->error = $this->db->error();
659 $this->db->rollback();
677 if (empty($this->total_localtax1)) {
678 $this->total_localtax1 = 0;
680 if (empty($this->total_localtax2)) {
681 $this->total_localtax2 = 0;
685 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
686 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
687 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
688 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
689 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
690 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
691 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
693 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
695 $resql = $this->db->query($sql);
700 $this->error = $this->db->error();
701 $this->db->rollback();