170 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
171 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
172 $sql .=
' cd.remise, cd.remise_percent, cd.subprice,';
173 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
174 $sql .=
' cd.total_localtax1, cd.total_localtax2,';
175 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
176 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit, cd.extraparams,';
177 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
178 $sql .=
' c.fk_soc as socid';
179 $sql .=
' FROM '.$this->db->prefix().
'commande_fournisseur as c, '.$this->db->prefix().
'commande_fournisseurdet as cd';
180 $sql .=
' LEFT JOIN '.$this->db->prefix().
'product as p ON cd.fk_product = p.rowid';
181 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
183 $result = $this->db->query($sql);
185 $objp = $this->db->fetch_object($result);
188 $this->rowid = $objp->rowid;
189 $this->
id = $objp->rowid;
190 $this->fk_commande = $objp->fk_commande;
191 $this->desc = $objp->description;
192 $this->qty = $objp->qty;
193 $this->ref_fourn = $objp->ref_supplier;
194 $this->ref_supplier = $objp->ref_supplier;
195 $this->subprice = $objp->subprice;
196 $this->tva_tx = $objp->tva_tx;
197 $this->localtax1_tx = $objp->localtax1_tx;
198 $this->localtax2_tx = $objp->localtax2_tx;
199 $this->localtax1_type = $objp->localtax1_type;
200 $this->localtax2_type = $objp->localtax2_type;
201 $this->remise = $objp->remise;
202 $this->remise_percent = $objp->remise_percent;
203 $this->fk_product = $objp->fk_product;
204 $this->info_bits = $objp->info_bits;
205 $this->total_ht = $objp->total_ht;
206 $this->total_tva = $objp->total_tva;
207 $this->total_localtax1 = $objp->total_localtax1;
208 $this->total_localtax2 = $objp->total_localtax2;
209 $this->total_ttc = $objp->total_ttc;
210 $this->product_type = $objp->product_type;
211 $this->special_code = $objp->special_code;
213 $this->
ref = $objp->product_ref;
215 $this->product_ref = $objp->product_ref;
216 $this->product_label = $objp->product_label;
217 $this->product_desc = $objp->product_desc;
224 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.$this->db->prefix().
"product_fournisseur_price";
225 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
226 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
227 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
228 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
229 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
230 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
231 $sqlsearchpackage .=
" ORDER BY packaging ASC";
232 $sqlsearchpackage .=
" LIMIT 1";
234 $resqlsearchpackage = $this->db->query($sqlsearchpackage);
235 if ($resqlsearchpackage) {
236 $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
237 if ($objsearchpackage) {
238 $this->fk_fournprice = $objsearchpackage->rowid;
239 $this->packaging = (float) $objsearchpackage->packaging;
242 $this->error = $this->db->lasterror();
247 $this->date_start = $this->db->jdate($objp->date_start);
248 $this->date_end = $this->db->jdate($objp->date_end);
249 $this->fk_unit = $objp->fk_unit;
251 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
253 $this->multicurrency_subprice = $objp->multicurrency_subprice;
254 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
255 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
256 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
260 $this->db->free($result);
263 $this->error =
'Supplier order line with id='.$rowid.
' not found';
264 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
285 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
288 if (empty($this->tva_tx)) {
291 if (empty($this->localtax1_tx)) {
292 $this->localtax1_tx = 0;
294 if (empty($this->localtax2_tx)) {
295 $this->localtax2_tx = 0;
297 if (empty($this->localtax1_type)) {
298 $this->localtax1_type =
'0';
300 if (empty($this->localtax2_type)) {
301 $this->localtax2_type =
'0';
303 if (empty($this->total_localtax1)) {
304 $this->total_localtax1 = 0;
306 if (empty($this->total_localtax2)) {
307 $this->total_localtax2 = 0;
309 if (empty($this->rang)) {
312 if (empty($this->remise_percent)) {
313 $this->remise_percent = 0;
315 if (empty($this->info_bits)) {
316 $this->info_bits = 0;
318 if (empty($this->special_code)) {
319 $this->special_code = 0;
321 if (empty($this->fk_parent_line)) {
322 $this->fk_parent_line = 0;
324 if (empty($this->pa_ht)) {
329 if (!empty($this->multicurrency_code)) {
332 if (empty($this->fk_multicurrency)) {
333 $this->multicurrency_code =
$conf->currency;
334 $this->fk_multicurrency = 0;
335 $this->multicurrency_tx = 1;
339 if ($this->product_type < 0) {
346 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element;
347 $sql .=
" (fk_commande, label, description, date_start, date_end,";
348 $sql .=
" fk_product, product_type, special_code, rang,";
349 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
350 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
351 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
352 $sql .=
" fk_parent_line)";
353 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->db->escape($this->label).
"','".$this->db->escape($this->desc).
"',";
354 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
355 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
356 if ($this->fk_product) {
357 $sql .= $this->fk_product.
",";
361 $sql .= ((int) $this->product_type).
",";
362 $sql .= ((int) $this->special_code).
",";
363 $sql .=
"'".$this->db->escape((
string) $this->rang).
"',";
364 $sql .=
"'".$this->db->escape((
string) $this->qty).
"', ";
365 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape((
string) $this->vat_src_code).
"'").
",";
366 $sql .=
" ".price2num($this->tva_tx).
", ";
367 $sql .=
" ".price2num($this->localtax1_tx).
",";
368 $sql .=
" ".price2num($this->localtax2_tx).
",";
369 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
370 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
371 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", '".$this->db->escape($this->ref_supplier).
"',";
372 $sql .=
" ".price2num($this->total_ht).
",";
373 $sql .=
" ".price2num($this->total_tva).
",";
374 $sql .=
" ".price2num($this->total_localtax1).
",";
375 $sql .=
" ".price2num($this->total_localtax2).
",";
376 $sql .=
" ".price2num($this->total_ttc).
",";
377 $sql .= ($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
378 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
379 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
380 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
381 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
382 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
383 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
384 $sql .=
", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line :
'null');
387 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
388 $resql = $this->db->query($sql);
390 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
391 $this->rowid = $this->id;
400 if (!$error && !$notrigger) {
402 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
414 foreach ($this->errors as $errmsg) {
415 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
416 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
418 $this->db->rollback();
421 $this->errors[] = $this->db->error();
422 $this->db->rollback();
440 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
441 $sql .=
" description='".$this->db->escape($this->desc).
"'";
442 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
443 $sql .=
", subprice = ".((float)
price2num($this->subprice));
444 $sql .=
", subprice_ttc = ".((float)
price2num($this->subprice_ttc));
445 $sql .=
", remise_percent = ".((float)
price2num($this->remise_percent));
446 $sql .=
", vat_src_code = '".$this->db->escape((
string) $this->vat_src_code).
"'";
447 $sql .=
", tva_tx = ".((float)
price2num($this->tva_tx));
448 $sql .=
", localtax1_tx = ".((float)
price2num($this->localtax1_tx));
449 $sql .=
", localtax2_tx = ".((float)
price2num($this->localtax2_tx));
450 $sql .=
", localtax1_type = '".$this->db->escape($this->localtax1_type).
"'";
451 $sql .=
", localtax2_type = '".$this->db->escape($this->localtax2_type).
"'";
452 $sql .=
", qty = ".((float)
price2num($this->qty));
453 $sql .=
", date_start = ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
454 $sql .=
", date_end = ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
455 $sql .=
", info_bits = ".((int) $this->info_bits);
456 $sql .=
", total_ht = ".((float)
price2num($this->total_ht));
457 $sql .=
", total_tva = ".((float)
price2num($this->total_tva));
458 $sql .=
", total_localtax1 = ".((float)
price2num($this->total_localtax1));
459 $sql .=
", total_localtax2 = ".((float)
price2num($this->total_localtax2));
460 $sql .=
", total_ttc = ".((float)
price2num($this->total_ttc));
461 $sql .=
", product_type = ".((int) $this->product_type);
462 $sql .=
", special_code = ".((int) $this->special_code);
463 $sql .=
", fk_unit = ".($this->fk_unit ? ((int) $this->fk_unit) :
"null");
464 if (!empty($this->rang)) {
465 $sql .=
", rang=".((int) $this->rang);
468 $sql .=
", multicurrency_subprice = ".((float)
price2num($this->multicurrency_subprice));
469 $sql .=
", multicurrency_subprice_ttc = ".((float)
price2num($this->multicurrency_subprice_ttc));
470 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht));
471 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva));
472 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc));
474 $sql .=
" WHERE rowid = ".((int) $this->
id);
476 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
477 $resql = $this->db->query($sql);
486 if (!$error && !$notrigger) {
488 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
490 $this->db->rollback();
500 $this->db->rollback();
504 $this->error = $this->db->lasterror();
505 $this->db->rollback();