172 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
173 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
174 $sql .=
' cd.remise, cd.remise_percent, cd.subprice,';
175 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
176 $sql .=
' cd.total_localtax1, cd.total_localtax2,';
177 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
178 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit, cd.extraparams,';
179 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
180 $sql .=
' c.fk_soc as socid';
181 $sql .=
' FROM '.$this->db->prefix().
'commande_fournisseur as c, '.$this->db->prefix().
'commande_fournisseurdet as cd';
182 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON cd.fk_product = p.rowid';
183 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
185 $result = $this->db->query($sql);
187 $objp = $this->db->fetch_object($result);
190 $this->rowid = $objp->rowid;
191 $this->
id = $objp->rowid;
192 $this->fk_commande = $objp->fk_commande;
193 $this->desc = $objp->description;
194 $this->qty = $objp->qty;
195 $this->ref_fourn = $objp->ref_supplier;
196 $this->ref_supplier = $objp->ref_supplier;
197 $this->subprice = $objp->subprice;
198 $this->subprice_ttc = $objp->subprice_ttc;
199 $this->tva_tx = $objp->tva_tx;
200 $this->localtax1_tx = $objp->localtax1_tx;
201 $this->localtax2_tx = $objp->localtax2_tx;
202 $this->localtax1_type = $objp->localtax1_type;
203 $this->localtax2_type = $objp->localtax2_type;
204 $this->remise = $objp->remise;
205 $this->remise_percent = $objp->remise_percent;
206 $this->fk_product = $objp->fk_product;
207 $this->info_bits = $objp->info_bits;
208 $this->total_ht = $objp->total_ht;
209 $this->total_tva = $objp->total_tva;
210 $this->total_localtax1 = $objp->total_localtax1;
211 $this->total_localtax2 = $objp->total_localtax2;
212 $this->total_ttc = $objp->total_ttc;
213 $this->product_type = $objp->product_type;
214 $this->special_code = $objp->special_code;
216 $this->
ref = $objp->product_ref;
218 $this->product_ref = $objp->product_ref;
219 $this->product_label = $objp->product_label;
220 $this->product_desc = $objp->product_desc;
227 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.$this->db->prefix().
"product_fournisseur_price";
228 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'productsupplierprice').
")";
229 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
230 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
231 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
232 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
233 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
234 $sqlsearchpackage .=
" ORDER BY packaging ASC";
235 $sqlsearchpackage .=
" LIMIT 1";
237 $resqlsearchpackage = $this->db->query($sqlsearchpackage);
238 if ($resqlsearchpackage) {
239 $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
240 if ($objsearchpackage) {
241 $this->fk_fournprice = $objsearchpackage->rowid;
242 $this->packaging = (float) $objsearchpackage->packaging;
245 $this->error = $this->db->lasterror();
250 $this->date_start = $this->db->jdate($objp->date_start);
251 $this->date_end = $this->db->jdate($objp->date_end);
252 $this->fk_unit = $objp->fk_unit;
254 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
256 $this->multicurrency_subprice = $objp->multicurrency_subprice;
257 $this->multicurrency_subprice_ttc = $objp->multicurrency_subprice_ttc;
258 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
259 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
260 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
264 $this->db->free($result);
267 $this->error =
'Supplier order line with id='.$rowid.
' not found';
268 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
289 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
292 if (empty($this->tva_tx)) {
295 if (empty($this->localtax1_tx)) {
296 $this->localtax1_tx = 0;
298 if (empty($this->localtax2_tx)) {
299 $this->localtax2_tx = 0;
301 if (empty($this->localtax1_type)) {
302 $this->localtax1_type =
'0';
304 if (empty($this->localtax2_type)) {
305 $this->localtax2_type =
'0';
307 if (empty($this->total_localtax1)) {
308 $this->total_localtax1 = 0;
310 if (empty($this->total_localtax2)) {
311 $this->total_localtax2 = 0;
313 if (empty($this->rang)) {
316 if (empty($this->remise_percent)) {
317 $this->remise_percent = 0;
319 if (empty($this->info_bits)) {
320 $this->info_bits = 0;
322 if (empty($this->special_code)) {
323 $this->special_code = 0;
325 if (empty($this->fk_parent_line)) {
326 $this->fk_parent_line = 0;
328 if (empty($this->pa_ht)) {
333 if (!empty($this->multicurrency_code)) {
336 if (empty($this->fk_multicurrency)) {
337 $this->multicurrency_code = $conf->currency;
338 $this->fk_multicurrency = 0;
339 $this->multicurrency_tx = 1;
343 if ($this->product_type < 0) {
350 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element;
351 $sql .=
" (fk_commande, label, description, date_start, date_end,";
352 $sql .=
" fk_product, product_type, special_code, rang,";
353 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc, ref,";
354 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
355 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
356 $sql .=
" fk_parent_line)";
357 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->db->escape($this->label).
"','".$this->db->escape($this->desc).
"',";
358 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
359 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
360 if ($this->fk_product) {
361 $sql .= $this->fk_product.
",";
365 $sql .= ((int) $this->product_type).
",";
366 $sql .= ((int) $this->special_code).
",";
367 $sql .=
"'".$this->db->escape((
string) $this->rang).
"',";
368 $sql .=
"'".$this->db->escape((
string) $this->qty).
"', ";
369 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape((
string) $this->vat_src_code).
"'").
",";
370 $sql .=
" ".price2num($this->tva_tx).
", ";
371 $sql .=
" ".price2num($this->localtax1_tx).
",";
372 $sql .=
" ".price2num($this->localtax2_tx).
",";
373 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
374 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
375 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", ".
price2num($this->subprice_ttc,
'MU').
", '".$this->db->escape($this->ref_supplier).
"',";
376 $sql .=
" ".price2num($this->total_ht).
",";
377 $sql .=
" ".price2num($this->total_tva).
",";
378 $sql .=
" ".price2num($this->total_localtax1).
",";
379 $sql .=
" ".price2num($this->total_localtax2).
",";
380 $sql .=
" ".price2num($this->total_ttc).
",";
381 $sql .= ($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
382 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
383 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
384 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
385 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
386 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
387 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
388 $sql .=
", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line :
'null');
391 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
392 $resql = $this->db->query($sql);
394 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
395 $this->rowid = $this->id;
404 if (!$error && !$notrigger) {
406 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
418 foreach ($this->errors as $errmsg) {
419 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
420 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
422 $this->db->rollback();
425 $this->errors[] = $this->db->error();
426 $this->db->rollback();
444 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
445 $sql .=
" description='".$this->db->escape($this->desc).
"'";
446 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
447 $sql .=
", subprice = ".((float)
price2num($this->subprice));
448 $sql .=
", subprice_ttc = ".((float)
price2num($this->subprice_ttc));
449 $sql .=
", remise_percent = ".((float)
price2num($this->remise_percent));
450 $sql .=
", vat_src_code = '".$this->db->escape((
string) $this->vat_src_code).
"'";
451 $sql .=
", tva_tx = ".((float)
price2num($this->tva_tx));
452 $sql .=
", localtax1_tx = ".((float)
price2num($this->localtax1_tx));
453 $sql .=
", localtax2_tx = ".((float)
price2num($this->localtax2_tx));
454 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
455 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
456 $sql .=
", qty = ".((float)
price2num($this->qty));
457 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
458 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
459 $sql .=
", info_bits = ".((int) $this->info_bits);
460 $sql .=
", total_ht = ".((float)
price2num($this->total_ht));
461 $sql .=
", total_tva = ".((float)
price2num($this->total_tva));
462 $sql .=
", total_localtax1 = ".((float)
price2num($this->total_localtax1));
463 $sql .=
", total_localtax2 = ".((float)
price2num($this->total_localtax2));
464 $sql .=
", total_ttc = ".((float)
price2num($this->total_ttc));
465 $sql .=
", product_type = ".((int) $this->product_type);
466 $sql .=
", special_code = ".((int) $this->special_code);
467 $sql .=
", fk_unit = ".($this->fk_unit ? ((int) $this->fk_unit) :
"null");
468 if (!empty($this->rang)) {
469 $sql .=
", rang=".((int) $this->rang);
472 $sql .=
", multicurrency_subprice = ".((float)
price2num($this->multicurrency_subprice));
473 $sql .=
", multicurrency_subprice_ttc = ".((float)
price2num($this->multicurrency_subprice_ttc));
474 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht));
475 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva));
476 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc));
478 $sql .=
" WHERE rowid = ".((int) $this->
id);
480 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
481 $resql = $this->db->query($sql);
490 if (!$error && !$notrigger) {
492 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
494 $this->db->rollback();
504 $this->db->rollback();
508 $this->error = $this->db->lasterror();
509 $this->db->rollback();