178 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, 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,';
183 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
184 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit, cd.extraparams,';
185 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_subprice_ttc, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
186 $sql .=
' c.fk_soc as socid';
187 $sql .=
' FROM '.$this->db->prefix().
'commande_fournisseur as c, '.$this->db->prefix().
'commande_fournisseurdet as cd';
188 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON cd.fk_product = p.rowid';
189 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
191 $result = $this->db->query($sql);
193 $objp = $this->db->fetch_object($result);
196 $this->rowid = $objp->rowid;
197 $this->
id = $objp->rowid;
198 $this->fk_commande = $objp->fk_commande;
199 $this->desc = $objp->description;
200 $this->qty = $objp->qty;
201 $this->ref_fourn = $objp->ref_supplier;
202 $this->ref_supplier = $objp->ref_supplier;
203 $this->subprice = $objp->subprice;
204 $this->subprice_ttc = $objp->subprice_ttc;
205 $this->tva_tx = $objp->tva_tx;
206 $this->localtax1_tx = $objp->localtax1_tx;
207 $this->localtax2_tx = $objp->localtax2_tx;
208 $this->localtax1_type = $objp->localtax1_type;
209 $this->localtax2_type = $objp->localtax2_type;
210 $this->remise = $objp->remise;
211 $this->remise_percent = $objp->remise_percent;
212 $this->fk_product = $objp->fk_product;
213 $this->info_bits = $objp->info_bits;
214 $this->total_ht = $objp->total_ht;
215 $this->total_tva = $objp->total_tva;
216 $this->total_localtax1 = $objp->total_localtax1;
217 $this->total_localtax2 = $objp->total_localtax2;
218 $this->total_ttc = $objp->total_ttc;
219 $this->product_type = $objp->product_type;
220 $this->special_code = $objp->special_code;
222 $this->
ref = $objp->product_ref;
224 $this->product_ref = $objp->product_ref;
225 $this->product_label = $objp->product_label;
226 $this->product_desc = $objp->product_desc;
233 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.$this->db->prefix().
"product_fournisseur_price";
234 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'productsupplierprice').
")";
235 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
236 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
237 $sqlsearchpackage .=
" AND quantity <= ".abs((
float) $objp->qty);
238 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".abs((
float) $objp->qty).
")";
239 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
240 $sqlsearchpackage .=
" ORDER BY packaging ASC";
241 $sqlsearchpackage .=
" LIMIT 1";
243 $resqlsearchpackage = $this->db->query($sqlsearchpackage);
244 if ($resqlsearchpackage) {
245 $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
246 if ($objsearchpackage) {
247 $this->fk_fournprice = $objsearchpackage->rowid;
248 $this->packaging = (float) $objsearchpackage->packaging;
251 $this->error = $this->db->lasterror();
256 $this->date_start = $this->db->jdate($objp->date_start);
257 $this->date_end = $this->db->jdate($objp->date_end);
258 $this->fk_unit = $objp->fk_unit;
260 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
262 $this->multicurrency_subprice = $objp->multicurrency_subprice;
263 $this->multicurrency_subprice_ttc = $objp->multicurrency_subprice_ttc;
264 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
265 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
266 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
270 $this->db->free($result);
273 $this->error =
'Supplier order line with id='.$rowid.
' not found';
274 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
295 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
298 if (empty($this->tva_tx)) {
301 if (empty($this->localtax1_tx)) {
302 $this->localtax1_tx = 0;
304 if (empty($this->localtax2_tx)) {
305 $this->localtax2_tx = 0;
307 if (empty($this->localtax1_type)) {
308 $this->localtax1_type =
'0';
310 if (empty($this->localtax2_type)) {
311 $this->localtax2_type =
'0';
313 if (empty($this->total_localtax1)) {
314 $this->total_localtax1 = 0;
316 if (empty($this->total_localtax2)) {
317 $this->total_localtax2 = 0;
319 if (empty($this->rang)) {
322 if (empty($this->remise_percent)) {
323 $this->remise_percent = 0;
325 if (empty($this->info_bits)) {
326 $this->info_bits = 0;
328 if (empty($this->special_code)) {
329 $this->special_code = 0;
331 if (empty($this->fk_parent_line)) {
332 $this->fk_parent_line = 0;
334 if (empty($this->pa_ht)) {
339 if (!empty($this->multicurrency_code)) {
342 if (empty($this->fk_multicurrency)) {
343 $this->multicurrency_code =
$conf->currency;
344 $this->fk_multicurrency = 0;
345 $this->multicurrency_tx = 1;
349 if ($this->product_type < 0) {
356 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element;
357 $sql .=
" (fk_commande, label, description, date_start, date_end,";
358 $sql .=
" fk_product, product_type, special_code, rang,";
359 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc, ref,";
360 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
361 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
362 $sql .=
" fk_parent_line)";
363 $sql .=
" VALUES (".((int) $this->fk_commande).
", '".$this->db->escape($this->label).
"','".$this->db->escape($this->desc).
"',";
364 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
365 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
366 if ($this->fk_product) {
367 $sql .= ((int) $this->fk_product).
",";
371 $sql .= ((int) $this->product_type).
",";
372 $sql .= ((int) $this->special_code).
",";
373 $sql .=
"'".$this->db->escape((
string) $this->rang).
"',";
374 $sql .=
"'".$this->db->escape((
string) $this->qty).
"', ";
375 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape((
string) $this->vat_src_code).
"'").
",";
376 $sql .=
" ".price2num($this->tva_tx).
", ";
377 $sql .=
" ".price2num($this->localtax1_tx).
",";
378 $sql .=
" ".price2num($this->localtax2_tx).
",";
379 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
380 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
381 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", ".
price2num($this->subprice_ttc,
'MU').
", '".$this->db->escape($this->ref_supplier).
"',";
382 $sql .=
" ".price2num($this->total_ht).
",";
383 $sql .=
" ".price2num($this->total_tva).
",";
384 $sql .=
" ".price2num($this->total_localtax1).
",";
385 $sql .=
" ".price2num($this->total_localtax2).
",";
386 $sql .=
" ".price2num($this->total_ttc).
",";
387 $sql .= ($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
388 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
389 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
390 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
391 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
392 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
393 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
394 $sql .=
", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? ((
int) $this->fk_parent_line) :
'null');
397 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
398 $resql = $this->db->query($sql);
400 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
401 $this->rowid = $this->id;
410 if (!$error && !$notrigger) {
412 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
424 foreach ($this->errors as $errmsg) {
425 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
426 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
428 $this->db->rollback();
431 $this->errors[] = $this->db->error();
432 $this->db->rollback();
450 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
451 $sql .=
" description='".$this->db->escape($this->desc).
"'";
452 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
453 $sql .=
", subprice = ".((float)
price2num($this->subprice));
454 $sql .=
", subprice_ttc = ".((float)
price2num($this->subprice_ttc));
455 $sql .=
", remise_percent = ".((float)
price2num($this->remise_percent));
456 $sql .=
", vat_src_code = '".$this->db->escape((
string) $this->vat_src_code).
"'";
457 $sql .=
", tva_tx = ".((float)
price2num($this->tva_tx));
458 $sql .=
", localtax1_tx = ".((float)
price2num($this->localtax1_tx));
459 $sql .=
", localtax2_tx = ".((float)
price2num($this->localtax2_tx));
460 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
461 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
462 $sql .=
", qty = ".((float)
price2num($this->qty));
463 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
464 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
465 $sql .=
", info_bits = ".((int) $this->info_bits);
466 $sql .=
", total_ht = ".((float)
price2num($this->total_ht));
467 $sql .=
", total_tva = ".((float)
price2num($this->total_tva));
468 $sql .=
", total_localtax1 = ".((float)
price2num($this->total_localtax1));
469 $sql .=
", total_localtax2 = ".((float)
price2num($this->total_localtax2));
470 $sql .=
", total_ttc = ".((float)
price2num($this->total_ttc));
471 $sql .=
", product_type = ".((int) $this->product_type);
472 $sql .=
", special_code = ".((int) $this->special_code);
473 $sql .=
", fk_unit = ".($this->fk_unit ? ((int) $this->fk_unit) :
"null");
474 if (!empty($this->rang)) {
475 $sql .=
", rang=".((int) $this->rang);
478 $sql .=
", multicurrency_subprice = ".((float)
price2num($this->multicurrency_subprice));
479 $sql .=
", multicurrency_subprice_ttc = ".((float)
price2num($this->multicurrency_subprice_ttc));
480 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht));
481 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva));
482 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc));
484 $sql .=
" WHERE rowid = ".((int) $this->
id);
486 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
487 $resql = $this->db->query($sql);
496 if (!$error && !$notrigger) {
498 $result = $this->call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
500 $this->db->rollback();
510 $this->db->rollback();
514 $this->error = $this->db->lasterror();
515 $this->db->rollback();