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,';
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 = $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->multicurrency_subprice = $objp->multicurrency_subprice;
252 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
253 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
254 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
258 $this->db->free($result);
261 $this->error =
'Supplier order line with id='.$rowid.
' not found';
262 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
283 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
286 if (empty($this->tva_tx)) {
289 if (empty($this->localtax1_tx)) {
290 $this->localtax1_tx = 0;
292 if (empty($this->localtax2_tx)) {
293 $this->localtax2_tx = 0;
295 if (empty($this->localtax1_type)) {
296 $this->localtax1_type =
'0';
298 if (empty($this->localtax2_type)) {
299 $this->localtax2_type =
'0';
301 if (empty($this->total_localtax1)) {
302 $this->total_localtax1 = 0;
304 if (empty($this->total_localtax2)) {
305 $this->total_localtax2 = 0;
307 if (empty($this->rang)) {
310 if (empty($this->remise_percent)) {
311 $this->remise_percent = 0;
313 if (empty($this->info_bits)) {
314 $this->info_bits = 0;
316 if (empty($this->special_code)) {
317 $this->special_code = 0;
319 if (empty($this->fk_parent_line)) {
320 $this->fk_parent_line = 0;
322 if (empty($this->pa_ht)) {
327 if (!empty($this->multicurrency_code)) {
330 if (empty($this->fk_multicurrency)) {
331 $this->multicurrency_code = $conf->currency;
332 $this->fk_multicurrency = 0;
333 $this->multicurrency_tx = 1;
337 if ($this->product_type < 0) {
344 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element;
345 $sql .=
" (fk_commande, label, description, date_start, date_end,";
346 $sql .=
" fk_product, product_type, special_code, rang,";
347 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
348 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
349 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
350 $sql .=
" fk_parent_line)";
351 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->db->escape($this->label).
"','".$this->db->escape($this->desc).
"',";
352 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
353 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
354 if ($this->fk_product) {
355 $sql .= $this->fk_product.
",";
359 $sql .=
"'".$this->db->escape($this->product_type).
"',";
360 $sql .= (int) $this->special_code .
",";
361 $sql .=
"'".$this->db->escape($this->rang).
"',";
362 $sql .=
"'".$this->db->escape($this->qty).
"', ";
363 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
364 $sql .=
" ".price2num($this->tva_tx).
", ";
365 $sql .=
" ".price2num($this->localtax1_tx).
",";
366 $sql .=
" ".price2num($this->localtax2_tx).
",";
367 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
368 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
369 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", '".$this->db->escape($this->ref_supplier).
"',";
370 $sql .=
" ".price2num($this->total_ht).
",";
371 $sql .=
" ".price2num($this->total_tva).
",";
372 $sql .=
" ".price2num($this->total_localtax1).
",";
373 $sql .=
" ".price2num($this->total_localtax2).
",";
374 $sql .=
" ".price2num($this->total_ttc).
",";
375 $sql .= ($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
376 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
377 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
378 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
379 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
380 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
381 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
382 $sql .=
", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line :
'null');
385 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
386 $resql = $this->db->query($sql);
388 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
389 $this->rowid = $this->id;
398 if (!$error && !$notrigger) {
400 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
412 foreach ($this->errors as $errmsg) {
413 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
414 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
416 $this->db->rollback();
419 $this->errors[] = $this->db->error();
420 $this->db->rollback();
438 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
439 $sql .=
" description='".$this->db->escape($this->desc).
"'";
440 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
441 $sql .=
", subprice='".price2num($this->subprice).
"'";
443 $sql .=
", remise_percent='".price2num($this->remise_percent).
"'";
445 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
446 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
447 $sql .=
", localtax1_tx='".price2num($this->localtax1_tx).
"'";
448 $sql .=
", localtax2_tx='".price2num($this->localtax2_tx).
"'";
449 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
450 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
451 $sql .=
", qty='".price2num($this->qty).
"'";
452 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
453 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
454 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
455 $sql .=
", total_ht='".price2num($this->total_ht).
"'";
456 $sql .=
", total_tva='".price2num($this->total_tva).
"'";
457 $sql .=
", total_localtax1='".price2num($this->total_localtax1).
"'";
458 $sql .=
", total_localtax2='".price2num($this->total_localtax2).
"'";
459 $sql .=
", total_ttc='".price2num($this->total_ttc).
"'";
460 $sql .=
", product_type=".$this->product_type;
461 $sql .=
", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
462 $sql .= ($this->fk_unit ?
", fk_unit='".$this->db->escape($this->fk_unit).
"'" :
", fk_unit=null");
465 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
466 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
467 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
468 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
470 $sql .=
" WHERE rowid = ".((int) $this->
id);
472 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
473 $resql = $this->db->query($sql);
482 if (!$error && !$notrigger) {
484 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
486 $this->db->rollback();
496 $this->db->rollback();
500 $this->error = $this->db->lasterror();
501 $this->db->rollback();