37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
53 public $element =
'order_supplier';
58 public $table_element =
'commande_fournisseur';
63 public $table_element_line =
'commande_fournisseurdet';
68 public $fk_element =
'fk_commande';
73 public $picto =
'supplier_order';
79 public $ismultientitymanaged = 1;
85 public $restrictiononfksoc = 1;
103 public $ref_supplier;
117 public $date_creation;
119 public $date_approve;
120 public $date_approve2;
121 public $date_commande;
127 public $date_livraison;
132 public $delivery_date;
136 public $total_localtax1;
137 public $total_localtax2;
146 public $cond_reglement_id;
147 public $cond_reglement_code;
148 public $cond_reglement_label;
149 public $cond_reglement_doc;
156 public $mode_reglement_id;
157 public $mode_reglement_code;
158 public $user_author_id;
159 public $user_valid_id;
160 public $user_approve_id;
161 public $user_approve_id2;
165 public $extraparams = array();
170 public $lines = array();
175 public $linked_objects = array();
181 public $fk_multicurrency;
183 public $multicurrency_code;
184 public $multicurrency_tx;
185 public $multicurrency_total_ht;
186 public $multicurrency_total_tva;
187 public $multicurrency_total_ttc;
218 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>10),
219 'ref' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'showoncombobox'=>1,
'position'=>25,
'searchall'=>1),
220 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>35),
221 'ref_supplier' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefOrderSupplierShort',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'searchall'=>1),
222 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>45),
223 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
224 'date_approve' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
225 'date_approve2' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove2',
'enabled'=>1,
'visible'=>3,
'position'=>64),
226 'date_commande' =>array(
'type'=>
'date',
'label'=>
'OrderDateShort',
'enabled'=>1,
'visible'=>1,
'position'=>70),
227 'date_livraison' =>array(
'type'=>
'datetime',
'label'=>
'DeliveryDate',
'enabled'=>
'empty($conf->global->ORDER_DISABLE_DELIVERY_DATE)',
'visible'=>1,
'position'=>74),
228 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>3,
'position'=>75),
229 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>3,
'notnull'=>-1,
'position'=>80),
230 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>3,
'position'=>85),
231 'fk_user_approve' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval',
'enabled'=>1,
'visible'=>3,
'position'=>90),
232 'fk_user_approve2' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval2',
'enabled'=>1,
'visible'=>3,
'position'=>95),
233 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>3,
'notnull'=>1,
'position'=>100),
234 'billed' =>array(
'type'=>
'smallint(6)',
'label'=>
'Billed',
'enabled'=>1,
'visible'=>1,
'position'=>110),
235 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>1,
'position'=>130,
'isameasure'=>1),
236 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>3,
'position'=>135,
'isameasure'=>1),
237 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>3,
'position'=>140,
'isameasure'=>1),
238 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>1,
'position'=>145,
'isameasure'=>1),
239 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>150,
'isameasure'=>1),
240 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155,
'searchall'=>1),
241 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>160,
'searchall'=>1),
242 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'ModelPDF',
'enabled'=>1,
'visible'=>0,
'position'=>165),
243 'fk_input_method' =>array(
'type'=>
'integer',
'label'=>
'OrderMode',
'enabled'=>1,
'visible'=>3,
'position'=>170),
244 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>3,
'position'=>175),
245 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>3,
'position'=>180),
246 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>0,
'position'=>190),
247 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>3,
'position'=>200),
248 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>1,
'visible'=>3,
'position'=>205),
249 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLocation',
'enabled'=>1,
'visible'=>3,
'position'=>210),
250 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>0,
'position'=>215),
251 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Currency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>220),
252 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'CurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>225),
253 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>230),
254 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>235),
255 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240),
256 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'Date creation',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
257 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>1,
'notnull'=>1,
'position'=>46),
258 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>1000,
'index'=>1),
259 'tms'=>array(
'type'=>
'datetime',
'label'=>
"DateModificationShort",
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>501),
260 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>0,
'position'=>700),
261 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'position'=>701),
262 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>0,
'position'=>900),
337 public function fetch($id, $ref =
'')
342 if (empty($id) && empty($ref)) {
346 $sql =
"SELECT c.rowid, c.entity, c.ref, ref_supplier, c.fk_soc, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.total_tva,";
347 $sql .=
" c.localtax1, c.localtax2, ";
348 $sql .=
" c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
349 $sql .=
" c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
350 $sql .=
" c.date_commande as date_commande, c.date_livraison as delivery_date, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
351 $sql .=
" c.fk_account,";
352 $sql .=
" c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
353 $sql .=
" c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
354 $sql .=
" cm.libelle as methode_commande,";
355 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
356 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
357 $sql .=
', c.fk_incoterms, c.location_incoterms';
358 $sql .=
', i.libelle as label_incoterms';
359 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
360 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
361 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON c.fk_mode_reglement = p.id";
362 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_input_method as cm ON cm.rowid = c.fk_input_method";
363 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
366 $sql .=
" WHERE c.entity IN (".getEntity(
'supplier_order').
")";
368 $sql .=
" WHERE c.rowid=".((int) $id);
372 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
375 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
378 $obj = $this->
db->fetch_object(
$resql);
380 $this->error =
'Bill with id '.$id.
' not found';
381 dol_syslog(get_class($this).
'::fetch '.$this->error);
385 $this->
id = $obj->rowid;
386 $this->entity = $obj->entity;
388 $this->
ref = $obj->ref;
389 $this->ref_supplier = $obj->ref_supplier;
390 $this->socid = $obj->fk_soc;
391 $this->fourn_id = $obj->fk_soc;
392 $this->statut = $obj->fk_statut;
393 $this->status = $obj->fk_statut;
394 $this->billed = $obj->billed;
395 $this->user_author_id = $obj->fk_user_author;
396 $this->user_valid_id = $obj->fk_user_valid;
397 $this->user_approve_id = $obj->fk_user_approve;
398 $this->user_approve_id2 = $obj->fk_user_approve2;
399 $this->total_ht = $obj->total_ht;
400 $this->total_tva = $obj->total_tva;
401 $this->total_localtax1 = $obj->localtax1;
402 $this->total_localtax2 = $obj->localtax2;
403 $this->total_ttc = $obj->total_ttc;
404 $this->date_creation = $this->
db->jdate($obj->date_creation);
405 $this->date_valid = $this->
db->jdate($obj->date_valid);
406 $this->date_approve = $this->
db->jdate($obj->date_approve);
407 $this->date_approve2 = $this->
db->jdate($obj->date_approve2);
408 $this->date_commande = $this->
db->jdate($obj->date_commande);
409 if (isset($obj->date_commande)) {
410 $this->date = $this->date_commande;
412 $this->date = $this->date_creation;
414 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
415 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
416 $this->remise_percent = $obj->remise_percent;
417 $this->methode_commande_id = $obj->fk_input_method;
418 $this->methode_commande = $obj->methode_commande;
420 $this->source = $obj->source;
421 $this->fk_project = $obj->fk_project;
422 $this->cond_reglement_id = $obj->fk_cond_reglement;
423 $this->cond_reglement_code = $obj->cond_reglement_code;
424 $this->cond_reglement = $obj->cond_reglement_label;
425 $this->cond_reglement_label = $obj->cond_reglement_label;
426 $this->cond_reglement_doc = $obj->cond_reglement_doc;
427 $this->fk_account = $obj->fk_account;
428 $this->mode_reglement_id = $obj->fk_mode_reglement;
429 $this->mode_reglement_code = $obj->mode_reglement_code;
430 $this->mode_reglement = $obj->mode_reglement_libelle;
431 $this->note = $obj->note_private;
432 $this->note_private = $obj->note_private;
433 $this->note_public = $obj->note_public;
434 $this->model_pdf = $obj->model_pdf;
435 $this->modelpdf = $obj->model_pdf;
438 $this->fk_incoterms = $obj->fk_incoterms;
439 $this->location_incoterms = $obj->location_incoterms;
440 $this->label_incoterms = $obj->label_incoterms;
443 $this->fk_multicurrency = $obj->fk_multicurrency;
444 $this->multicurrency_code = $obj->multicurrency_code;
445 $this->multicurrency_tx = $obj->multicurrency_tx;
446 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
447 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
448 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
450 $this->extraparams = (array) json_decode($obj->extraparams,
true);
458 if ($this->statut == 0) {
459 $this->brouillon = 1;
473 $this->error = $this->
db->error().
" sql=".$sql;
490 $this->lines = array();
492 $sql =
"SELECT l.rowid, l.fk_commande, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
493 $sql .=
" l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
494 $sql .=
" l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
495 $sql .=
" l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
496 $sql .=
" p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch, p.barcode as product_barcode,";
497 $sql .=
" l.fk_unit,";
498 $sql .=
" l.date_start, l.date_end,";
499 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
500 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet as l";
501 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
502 $sql .=
" WHERE l.fk_commande = ".((int) $this->
id);
504 $sql .=
' AND p.fk_product_type = 0';
506 $sql .=
" ORDER BY l.rang, l.rowid";
509 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
511 $result = $this->
db->query($sql);
513 $num = $this->
db->num_rows($result);
517 $objp = $this->
db->fetch_object($result);
521 $line->id = $objp->rowid;
522 $line->fk_commande = $objp->fk_commande;
523 $line->desc = $objp->description;
524 $line->description = $objp->description;
525 $line->qty = $objp->qty;
526 $line->tva_tx = $objp->tva_tx;
527 $line->localtax1_tx = $objp->localtax1_tx;
528 $line->localtax2_tx = $objp->localtax2_tx;
529 $line->localtax1_type = $objp->localtax1_type;
530 $line->localtax2_type = $objp->localtax2_type;
531 $line->subprice = $objp->subprice;
532 $line->pu_ht = $objp->subprice;
533 $line->remise_percent = $objp->remise_percent;
535 $line->vat_src_code = $objp->vat_src_code;
536 $line->total_ht = $objp->total_ht;
537 $line->total_tva = $objp->total_tva;
538 $line->total_localtax1 = $objp->total_localtax1;
539 $line->total_localtax2 = $objp->total_localtax2;
540 $line->total_ttc = $objp->total_ttc;
541 $line->product_type = $objp->product_type;
543 $line->fk_product = $objp->fk_product;
545 $line->libelle = $objp->product_label;
546 $line->product_label = $objp->product_label;
547 $line->product_desc = $objp->product_desc;
548 $line->product_tobatch = $objp->product_tobatch;
549 $line->product_barcode = $objp->product_barcode;
551 $line->ref = $objp->product_ref;
552 $line->product_ref = $objp->product_ref;
553 $line->ref_fourn = $objp->ref_supplier;
554 $line->ref_supplier = $objp->ref_supplier;
556 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
561 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
562 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
563 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
564 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
565 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
566 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
567 $sqlsearchpackage .=
" AND fk_soc = ".((int) $this->socid);
568 $sqlsearchpackage .=
" ORDER BY packaging ASC";
569 $sqlsearchpackage .=
" LIMIT 1";
571 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
572 if ($resqlsearchpackage) {
573 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
574 if ($objsearchpackage) {
575 $line->fk_fournprice = $objsearchpackage->rowid;
576 $line->packaging = $objsearchpackage->packaging;
579 $this->error = $this->
db->lasterror();
584 $line->date_start = $this->
db->jdate($objp->date_start);
585 $line->date_end = $this->
db->jdate($objp->date_end);
586 $line->fk_unit = $objp->fk_unit;
589 $line->fk_multicurrency = $objp->fk_multicurrency;
590 $line->multicurrency_code = $objp->multicurrency_code;
591 $line->multicurrency_subprice = $objp->multicurrency_subprice;
592 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
593 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
594 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
596 $line->special_code = $objp->special_code;
597 $line->fk_parent_line = $objp->fk_parent_line;
599 $line->rang = $objp->rang;
603 $line->fetch_optionals();
605 $this->lines[$i] = $line;
609 $this->
db->free($result);
613 $this->error = $this->
db->error().
" sql=".$sql;
626 public function valid($user, $idwarehouse = 0, $notrigger = 0)
628 global $langs, $conf;
629 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
635 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && (!empty($user->rights->fournisseur->commande->creer) || !empty($user->rights->supplier_order->creer)))
636 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->supplier_order_advance->validate))) {
641 $soc->fetch($this->fourn_id);
644 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
651 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande_fournisseur";
652 $sql .=
" SET ref='".$this->db->escape($num).
"',";
653 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
654 $sql .=
" date_valid='".$this->db->idate(
dol_now()).
"',";
655 $sql .=
" fk_user_valid = ".((int) $user->id);
656 $sql .=
" WHERE rowid = ".((int) $this->
id);
657 $sql .=
" AND fk_statut = ".self::STATUS_DRAFT;
665 if (!$error && !$notrigger) {
667 $result = $this->
call_trigger(
'ORDER_SUPPLIER_VALIDATE', $user);
675 $this->oldref = $this->ref;
678 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
680 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'fournisseur/commande/".$this->
db->escape($this->newref).
"'";
681 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'fournisseur/commande/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
684 $error++; $this->error = $this->
db->lasterror();
690 $dirsource = $conf->fournisseur->commande->dir_output.
'/'.$oldref;
691 $dirdest = $conf->fournisseur->commande->dir_output.
'/'.$newref;
692 if (!$error && file_exists($dirsource)) {
693 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
695 if (@rename($dirsource, $dirdest)) {
698 $listoffiles =
dol_dir_list($conf->fournisseur->commande->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
699 foreach ($listoffiles as $fileentry) {
700 $dirsource = $fileentry[
'name'];
701 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
702 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
703 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
704 @rename($dirsource, $dirdest);
721 $this->
db->rollback();
725 $this->error =
'NotAuthorized';
726 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
739 return $this->
LibStatut($this->statut, $mode, $this->billed);
751 public function LibStatut($status, $mode = 0, $billed = 0)
754 global $conf, $langs, $hookmanager;
756 if (empty($this->statuts) || empty($this->statuts_short)) {
757 $langs->load(
'orders');
759 $this->statuts[0] =
'StatusSupplierOrderDraft';
760 $this->statuts[1] =
'StatusSupplierOrderValidated';
761 $this->statuts[2] =
'StatusSupplierOrderApproved';
762 if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
763 $this->statuts[3] =
'StatusSupplierOrderOnProcess';
765 $this->statuts[3] =
'StatusSupplierOrderOnProcessWithValidation';
767 $this->statuts[4] =
'StatusSupplierOrderReceivedPartially';
768 $this->statuts[5] =
'StatusSupplierOrderReceivedAll';
769 $this->statuts[6] =
'StatusSupplierOrderCanceled';
770 $this->statuts[7] =
'StatusSupplierOrderCanceled';
771 $this->statuts[9] =
'StatusSupplierOrderRefused';
774 $this->statuts_short[0] =
'StatusSupplierOrderDraftShort';
775 $this->statuts_short[1] =
'StatusSupplierOrderValidatedShort';
776 $this->statuts_short[2] =
'StatusSupplierOrderApprovedShort';
777 $this->statuts_short[3] =
'StatusSupplierOrderOnProcessShort';
778 $this->statuts_short[4] =
'StatusSupplierOrderReceivedPartiallyShort';
779 $this->statuts_short[5] =
'StatusSupplierOrderReceivedAllShort';
780 $this->statuts_short[6] =
'StatusSupplierOrderCanceledShort';
781 $this->statuts_short[7] =
'StatusSupplierOrderCanceledShort';
782 $this->statuts_short[9] =
'StatusSupplierOrderRefusedShort';
785 $statustrans = array(
797 $statusClass =
'status0';
798 if (!empty($statustrans[$status])) {
799 $statusClass = $statustrans[$status];
804 $billedtext =
' - '.$langs->trans(
"Billed");
806 if ($status == 5 && $billed) {
807 $statusClass =
'status6';
810 $statusLong = $langs->transnoentitiesnoconv($this->statuts[$status]).$billedtext;
811 $statusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
813 $parameters = array(
'status' => $status,
'mode' => $mode,
'billed' => $billed);
814 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
816 return $hookmanager->resPrint;
819 return dolGetStatus($statusLong, $statusShort,
'', $statusClass, $mode);
833 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
835 global $langs, $conf, $user, $hookmanager;
841 if ($user->hasRight(
"fournisseur",
"commande",
"read")) {
842 $label =
'<u class="paddingrightonly">'.$langs->trans(
"SupplierOrder").
'</u>';
843 if (isset($this->statut)) {
844 $label .=
' '.$this->getLibStatut(5);
846 if (!empty($this->
ref)) {
847 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
849 if (!empty($this->ref_supplier)) {
850 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
852 if (!empty($this->total_ht)) {
853 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
855 if (!empty($this->total_tva)) {
856 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
858 if (!empty($this->total_ttc)) {
859 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
861 if (!empty($this->date)) {
862 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
864 if (!empty($this->delivery_date)) {
865 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
870 $url = DOL_URL_ROOT.
'/fourn/commande/card.php?id='.$this->id;
872 if ($option !==
'nolink') {
874 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
875 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
876 $add_save_lastsearch_values = 1;
878 if ($add_save_lastsearch_values) {
879 $url .=
'&save_lastsearch_values=1';
884 if (empty($notooltip)) {
885 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
886 $label = $langs->trans(
"ShowOrder");
887 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
889 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
890 $linkclose .=
' class="classfortooltip"';
893 $linkstart =
'<a href="'.$url.
'"';
894 $linkstart .= $linkclose.
'>';
897 $result .= $linkstart;
899 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
901 if ($withpicto != 2) {
902 $result .= $this->ref;
906 if ($addlinktonotes) {
907 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
910 $result .=
' <span class="note inline-block">';
911 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
916 $result .=
'</span>';
921 $hookmanager->initHooks(array($this->element .
'dao'));
922 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
923 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
925 $result = $hookmanager->resPrint;
927 $result .= $hookmanager->resPrint;
942 global $db, $langs, $conf;
943 $langs->load(
"orders");
945 if (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER)) {
948 $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.
'.php';
949 $classname = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
952 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
954 foreach ($dirmodels as $reldir) {
955 $dir =
dol_buildpath($reldir.
"core/modules/supplier_order/");
958 $mybool |= @include_once $dir.$file;
961 if ($mybool ===
false) {
966 $obj =
new $classname();
967 $numref = $obj->getNextValue($soc, $this);
972 $this->error = $obj->error;
976 $this->error =
"Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
996 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur SET billed = 1';
999 if ($this->
db->query($sql)) {
1002 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
1012 $this->
db->commit();
1015 $this->
db->rollback();
1021 $this->
db->rollback();
1034 public function approve($user, $idwarehouse = 0, $secondlevel = 0)
1036 global $langs, $conf;
1037 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1043 if ($user->rights->fournisseur->commande->approuver) {
1050 $soc->fetch($this->fourn_id);
1053 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1061 $movetoapprovestatus =
true;
1064 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1065 $sql .=
" SET ref='".$this->db->escape($num).
"',";
1066 if (empty($secondlevel)) {
1067 $sql .=
" date_approve='".$this->db->idate($now).
"',";
1068 $sql .=
" fk_user_approve = ".$user->id;
1069 if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) {
1070 if (empty($this->user_approve_id2)) {
1071 $movetoapprovestatus =
false;
1072 $comment =
' (first level)';
1077 $sql .=
" date_approve2='".$this->db->idate($now).
"',";
1078 $sql .=
" fk_user_approve2 = ".((int) $user->id);
1079 if (empty($this->user_approve_id)) {
1080 $movetoapprovestatus =
false;
1082 $comment =
' (second level)';
1085 if ($movetoapprovestatus) {
1086 $sql .=
", fk_statut = ".self::STATUS_ACCEPTED;
1088 $sql .=
", fk_statut = ".self::STATUS_VALIDATED;
1090 $sql .=
" WHERE rowid = ".((int) $this->
id);
1091 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
1093 if ($this->
db->query($sql)) {
1094 if (!empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT)) {
1095 $result = $this->
add_contact($user->id,
'SALESREPFOLL',
'internal', 1);
1096 if ($result < 0 && $result != -2) {
1102 if (!$error && $movetoapprovestatus &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
1103 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1104 $langs->load(
"agenda");
1106 $cpt = count($this->lines);
1107 for ($i = 0; $i < $cpt; $i++) {
1109 if ($this->lines[$i]->fk_product > 0) {
1110 $this->line = $this->lines[$i];
1112 $mouvP->origin = &$this;
1113 $mouvP->setOrigin($this->element, $this->
id);
1115 $up_ht_disc = $this->lines[$i]->subprice;
1116 if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1117 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1119 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"OrderApprovedInDolibarr", $this->ref));
1130 $result = $this->
call_trigger(
'ORDER_SUPPLIER_APPROVE', $user);
1138 $this->
ref = $this->newref;
1140 if ($movetoapprovestatus) {
1145 if (empty($secondlevel)) {
1146 $this->date_approve = $now;
1147 $this->user_approve_id = $user->id;
1150 $this->date_approve2 = $now;
1151 $this->user_approve_id2 = $user->id;
1154 $this->
db->commit();
1157 $this->
db->rollback();
1161 $this->
db->rollback();
1162 $this->error = $this->
db->lasterror();
1166 dol_syslog(get_class($this).
"::approve Not Authorized", LOG_ERR);
1179 global $conf, $langs;
1185 if ($user->rights->fournisseur->commande->approuver) {
1189 $sql .=
" WHERE rowid = ".((int) $this->
id);
1191 if ($this->
db->query($sql)) {
1196 $result = $this->
call_trigger(
'ORDER_SUPPLIER_REFUSE', $user);
1199 $this->
db->rollback();
1201 $this->
db->commit();
1206 $this->
db->rollback();
1207 $this->error = $this->
db->lasterror();
1208 dol_syslog(get_class($this).
"::refuse Error -1");
1212 dol_syslog(get_class($this).
"::refuse Not Authorized");
1226 public function Cancel($user, $idwarehouse = -1)
1229 global $langs, $conf;
1235 if ($user->rights->fournisseur->commande->commander) {
1240 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur SET fk_statut = ".((int) $statut);
1241 $sql .=
" WHERE rowid = ".((int) $this->
id);
1242 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
1243 if ($this->
db->query($sql)) {
1247 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CANCEL', $user);
1254 $this->
db->commit();
1257 $this->
db->rollback();
1261 $this->
db->rollback();
1262 $this->error = $this->
db->lasterror();
1263 dol_syslog(get_class($this).
"::cancel ".$this->error);
1267 dol_syslog(get_class($this).
"::cancel Not Authorized");
1281 public function commande($user, $date, $methode, $comment =
'')
1286 if ($user->rights->fournisseur->commande->commander) {
1289 $newnoteprivate = $this->note_private;
1291 $newnoteprivate =
dol_concatdesc($newnoteprivate, $langs->trans(
"Comment").
': '.$comment);
1294 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1295 $sql .=
" SET fk_statut=".self::STATUS_ORDERSENT.
", fk_input_method=".$methode.
", date_commande='".$this->
db->idate($date).
"', ";
1296 $sql .=
" note_private='".$this->db->escape($newnoteprivate).
"'";
1297 $sql .=
" WHERE rowid=".((int) $this->
id);
1299 dol_syslog(get_class($this).
"::commande", LOG_DEBUG);
1300 if ($this->
db->query($sql)) {
1302 $this->methode_commande_id = $methode;
1303 $this->date_commande = $date;
1304 $this->context = array(
'comments' => $comment);
1307 $result = $this->
call_trigger(
'ORDER_SUPPLIER_SUBMIT', $user);
1314 $this->error = $this->
db->lasterror();
1315 $this->errors[] = $this->
db->lasterror();
1319 $this->
db->commit();
1321 $this->
db->rollback();
1325 $this->error = $langs->trans(
'NotAuthorized');
1326 $this->errors[] = $langs->trans(
'NotAuthorized');
1327 dol_syslog(get_class($this).
"::commande User not Authorized", LOG_WARNING);
1330 return ($error ? -1 : 1);
1340 public function create($user, $notrigger = 0)
1342 global $langs, $conf, $hookmanager;
1350 $date = ($this->date_commande ? $this->date_commande : $this->date);
1354 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1357 if (empty($this->source)) {
1362 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1367 if (empty($this->fk_multicurrency)) {
1368 $this->multicurrency_code = $conf->currency;
1369 $this->fk_multicurrency = 0;
1370 $this->multicurrency_tx = 1;
1374 $this->brouillon = 1;
1376 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur (";
1378 $sql .=
", ref_supplier";
1379 $sql .=
", note_private";
1380 $sql .=
", note_public";
1383 $sql .=
", fk_projet";
1384 $sql .=
", date_creation";
1385 $sql .=
", date_livraison";
1386 $sql .=
", fk_user_author";
1387 $sql .=
", fk_statut";
1389 $sql .=
", model_pdf";
1390 $sql .=
", fk_mode_reglement";
1391 $sql .=
", fk_cond_reglement";
1392 $sql .=
", fk_account";
1393 $sql .=
", fk_incoterms, location_incoterms";
1394 $sql .=
", fk_multicurrency";
1395 $sql .=
", multicurrency_code";
1396 $sql .=
", multicurrency_tx";
1398 $sql .=
" VALUES (";
1400 $sql .=
", '".$this->db->escape($this->ref_supplier).
"'";
1401 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1402 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1403 $sql .=
", ".setEntity($this);
1404 $sql .=
", ".((int) $this->socid);
1405 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1406 $sql .=
", '".$this->db->idate($date).
"'";
1407 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1408 $sql .=
", ".((int) $user->id);
1409 $sql .=
", ".self::STATUS_DRAFT;
1410 $sql .=
", ".((int) $this->source);
1411 $sql .=
", '".$this->db->escape($conf->global->COMMANDE_SUPPLIER_ADDON_PDF).
"'";
1412 $sql .=
", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id :
'null');
1413 $sql .=
", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id :
'null');
1414 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
1415 $sql .=
", ".(int) $this->fk_incoterms;
1416 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1417 $sql .=
", ".(int) $this->fk_multicurrency;
1418 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1419 $sql .=
", ".(double) $this->multicurrency_tx;
1422 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1423 if ($this->
db->query($sql)) {
1424 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"commande_fournisseur");
1427 $num = count($this->lines);
1430 for ($i = 0; $i < $num; $i++) {
1431 $line = $this->lines[$i];
1432 if (!is_object($line)) {
1433 $line = (object) $line;
1445 $line->localtax1_tx,
1446 $line->localtax2_tx,
1450 $line->remise_percent,
1453 $line->product_type,
1458 $line->array_options,
1463 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1464 $this->
db->rollback();
1469 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1470 $sql .=
" SET ref='(PROV".$this->id.
")'";
1471 $sql .=
" WHERE rowid=".((int) $this->
id);
1472 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1473 if ($this->
db->query($sql)) {
1476 $this->
ref =
"(PROV".$this->id.
")";
1478 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1479 $this->linked_objects = $this->linkedObjectsIds;
1483 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1484 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1485 if (is_array($tmp_origin_id)) {
1486 foreach ($tmp_origin_id as $origin_id) {
1495 $origin_id = $tmp_origin_id;
1513 if (!$error && !$notrigger) {
1515 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CREATE', $user);
1517 $this->
db->rollback();
1523 $this->
db->commit();
1526 $this->error = $this->
db->lasterror();
1527 $this->
db->rollback();
1532 $this->error = $this->
db->lasterror();
1533 $this->
db->rollback();
1552 if (isset($this->
ref)) {
1553 $this->
ref = trim($this->
ref);
1555 if (isset($this->ref_supplier)) {
1556 $this->ref_supplier = trim($this->ref_supplier);
1558 if (isset($this->note_private)) {
1559 $this->note_private = trim($this->note_private);
1561 if (isset($this->note_public)) {
1562 $this->note_public = trim($this->note_public);
1564 if (isset($this->model_pdf)) {
1565 $this->model_pdf = trim($this->model_pdf);
1567 if (isset($this->import_key)) {
1568 $this->import_key = trim($this->import_key);
1572 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
1574 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1575 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1576 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1577 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1578 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
1579 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
1580 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1581 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
1582 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
1583 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1584 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1585 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1586 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
1587 $sql .=
" fk_user_valid=".(isset($this->user_valid) && $this->user_valid > 0 ? $this->user_valid :
"null").
",";
1588 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1589 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1590 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
1591 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
1593 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
1595 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1596 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1597 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1598 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
1600 $sql .=
" WHERE rowid=".((int) $this->
id);
1604 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1608 $this->errors[] =
"Error ".$this->db->lasterror();
1618 if (!$error && !$notrigger) {
1620 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
1629 foreach ($this->errors as $errmsg) {
1630 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1631 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1633 $this->
db->rollback();
1636 $this->
db->commit();
1651 global $conf, $user, $hookmanager;
1658 foreach ($this->lines as $line) {
1659 $line->fetch_optionals();
1663 $objFrom = clone $this;
1666 if (!empty($socid) && $socid != $this->socid) {
1669 if ($objsoc->fetch($socid) > 0) {
1670 $this->socid = $objsoc->id;
1671 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1672 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1673 $this->fk_project = 0;
1674 $this->fk_delivery_address = 0;
1684 $this->user_author_id = $user->id;
1685 $this->user_valid = 0;
1686 $this->date_creation =
'';
1687 $this->date_validation =
'';
1688 $this->ref_supplier =
'';
1689 $this->user_approve_id =
'';
1690 $this->user_approve_id2 =
'';
1691 $this->date_approve =
'';
1692 $this->date_approve2 =
'';
1695 $this->context[
'createfromclone'] =
'createfromclone';
1696 $result = $this->
create($user, $notrigger);
1703 if (is_object($hookmanager)) {
1704 $parameters = array(
'objFrom'=>$objFrom);
1706 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1708 $this->errors += $hookmanager->errors;
1709 $this->error = $hookmanager->error;
1715 unset($this->context[
'createfromclone']);
1719 $this->
db->commit();
1722 $this->
db->rollback();
1756 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $fk_prod_fourn_price = 0, $ref_supplier =
'', $remise_percent = 0.0, $price_base_type =
'HT', $pu_ttc = 0.0, $type = 0, $info_bits = 0, $notrigger =
false, $date_start =
null, $date_end =
null, $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $origin =
'', $origin_id = 0, $rang = -1, $special_code = 0)
1758 global $langs, $mysoc, $conf;
1760 dol_syslog(get_class($this).
"::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $info_bits, $notrigger, $date_start, $date_end, $fk_unit, $pu_ht_devise, $origin, $origin_id");
1761 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1763 if ($this->statut == self::STATUS_DRAFT) {
1764 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1773 if (empty($txtva)) {
1779 if (empty($txlocaltax1)) {
1782 if (empty($txlocaltax2)) {
1785 if (empty($remise_percent)) {
1786 $remise_percent = 0;
1789 $remise_percent =
price2num($remise_percent);
1792 $pu_ht_devise =
price2num($pu_ht_devise);
1794 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1799 if ($price_base_type ==
'HT') {
1804 $desc = trim($desc);
1807 if ($qty < 0 && !$fk_product) {
1808 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Product"));
1814 if ($date_start && $date_end && $date_start > $date_end) {
1815 $langs->load(
"errors");
1816 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1823 $product_type = $type;
1826 if ($fk_product > 0) {
1827 if (!empty($conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY)) {
1829 dol_syslog(get_class($this).
"::addline we check supplier prices fk_product=".$fk_product.
" fk_prod_fourn_price=".$fk_prod_fourn_price.
" qty=".$qty.
" ref_supplier=".$ref_supplier);
1831 if ($prod->fetch($fk_product) > 0) {
1832 $product_type = $prod->type;
1833 $label = $prod->label;
1837 $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', (isset($this->fk_soc) ? $this->fk_soc : $this->socid));
1841 if ($result > 0 && ($origin ==
'commande' || $pu ===
'')) {
1842 $pu = $prod->fourn_pu;
1843 $ref_supplier = $prod->ref_supplier;
1845 if ($remise_percent == 0 && $prod->remise_percent != 0) {
1846 $remise_percent = $prod->remise_percent;
1850 $langs->load(
"errors");
1851 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1852 $this->
db->rollback();
1853 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
1858 if ($result == -1) {
1859 $langs->load(
"errors");
1860 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1861 $this->
db->rollback();
1862 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
1866 $this->error = $prod->error;
1867 $this->
db->rollback();
1868 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
1872 $this->error = $prod->error;
1873 $this->
db->rollback();
1879 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
1881 $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
1883 if ($qty < $prod->packaging) {
1884 $qty = $prod->packaging;
1886 if (!empty($prod->packaging) && ($qty % $prod->packaging) > 0) {
1887 $coeff = intval($qty / $prod->packaging) + 1;
1888 $qty = $prod->packaging * $coeff;
1889 setEventMessages($langs->trans(
'QtyRecalculatedWithPackaging'),
null,
'mesgs');
1895 if (
isModEnabled(
"multicurrency") && $pu_ht_devise > 0) {
1904 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1905 $vat_src_code = $reg[1];
1906 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1914 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
1916 $total_ht = $tabprice[0];
1917 $total_tva = $tabprice[1];
1918 $total_ttc = $tabprice[2];
1919 $total_localtax1 = $tabprice[9];
1920 $total_localtax2 = $tabprice[10];
1921 $pu = $pu_ht = $tabprice[3];
1924 $multicurrency_total_ht = $tabprice[16];
1925 $multicurrency_total_tva = $tabprice[17];
1926 $multicurrency_total_ttc = $tabprice[18];
1927 $pu_ht_devise = $tabprice[19];
1929 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1930 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1934 $rang = $rangmax + 1;
1940 $this->line->context = $this->context;
1942 $this->line->fk_commande = $this->id;
1943 $this->line->label = $label;
1944 $this->line->ref_fourn = $ref_supplier;
1945 $this->line->ref_supplier = $ref_supplier;
1946 $this->line->desc = $desc;
1947 $this->line->qty = $qty;
1948 $this->line->tva_tx = $txtva;
1949 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1950 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1951 $this->line->localtax1_type = $localtax1_type;
1952 $this->line->localtax2_type = $localtax2_type;
1953 $this->line->fk_product = $fk_product;
1954 $this->line->product_type = $product_type;
1955 $this->line->remise_percent = $remise_percent;
1956 $this->line->subprice = $pu_ht;
1957 $this->line->rang = $rang;
1958 $this->line->info_bits = $info_bits;
1960 $this->line->vat_src_code = $vat_src_code;
1961 $this->line->total_ht = $total_ht;
1962 $this->line->total_tva = $total_tva;
1963 $this->line->total_localtax1 = $total_localtax1;
1964 $this->line->total_localtax2 = $total_localtax2;
1965 $this->line->total_ttc = $total_ttc;
1966 $this->line->product_type = $type;
1967 $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
1968 $this->line->origin = $origin;
1969 $this->line->origin_id = $origin_id;
1970 $this->line->fk_unit = $fk_unit;
1972 $this->line->date_start = $date_start;
1973 $this->line->date_end = $date_end;
1976 $this->line->fk_multicurrency = $this->fk_multicurrency;
1977 $this->line->multicurrency_code = $this->multicurrency_code;
1978 $this->line->multicurrency_subprice = $pu_ht_devise;
1979 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1980 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1981 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1983 $this->line->subprice = $pu_ht;
1984 $this->line->price = $this->line->subprice;
1986 $this->line->remise_percent = $remise_percent;
1988 if (is_array($array_options) && count($array_options) > 0) {
1989 $this->line->array_options = $array_options;
1992 $result = $this->line->insert($notrigger);
1995 if (!empty($fk_parent_line)) {
1997 } elseif ($rang > 0 && $rang <= count($this->lines)) {
1998 $linecount = count($this->lines);
1999 for ($ii = $rang; $ii <= $linecount; $ii++) {
2005 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2007 $this->
db->commit();
2008 return $this->line->id;
2010 $this->
db->rollback();
2014 $this->error = $this->line->error;
2015 $this->errors = $this->line->errors;
2016 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
2017 $this->
db->rollback();
2040 public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment =
'', $eatby =
'', $sellby =
'', $batch =
'', $fk_commandefourndet = 0, $notrigger = 0)
2042 global $conf, $langs;
2045 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2048 if ($entrepot <= 0) {
2049 $this->error =
'ErrorBadValueForParameterWarehouse';
2053 $this->error =
'ErrorBadValueForParameterQty';
2057 $dispatchstatus = 1;
2058 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2059 $dispatchstatus = 0;
2066 if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY)) {
2069 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch";
2070 $sql .=
" (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
2071 $sql .=
" ('".$this->id.
"','".$product.
"','".$qty.
"',".($entrepot > 0 ?
"'".$entrepot.
"'" :
"null").
",'".$user->id.
"','".$this->db->idate($now).
"','".$fk_commandefourndet.
"', ".$dispatchstatus.
", '".$this->
db->escape($comment).
"', ";
2072 $sql .= ($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ".($sellby ?
"'".$this->
db->idate($sellby).
"'" :
"null").
", ".($batch ?
"'".$this->
db->escape($batch).
"'" :
"null");
2075 dol_syslog(get_class($this).
"::dispatchProduct", LOG_DEBUG);
2079 global $conf, $langs, $user;
2081 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DISPATCH', $user);
2088 $this->error = $this->
db->lasterror();
2093 if (!$error && $entrepot > 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
2097 $mouv->origin = &$this;
2098 $mouv->setOrigin($this->element, $this->
id);
2101 if (!empty($conf->global->SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN) && $qty < 0) {
2102 $result = $mouv->livraison($user, $product, $entrepot, $qty*(-1), $price, $comment, $now, $eatby, $sellby, $batch, 0, $inventorycode);
2104 $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch,
'', 0, $inventorycode);
2108 $this->error = $mouv->error;
2109 $this->errors = $mouv->errors;
2110 dol_syslog(get_class($this).
"::dispatchProduct ".$this->error.
" ".join(
',', $this->errors), LOG_ERR);
2117 $this->
db->commit();
2120 $this->
db->rollback();
2124 $this->error =
'BadStatusForObject';
2138 if ($this->statut == 0) {
2141 if ($line->fetch($idline) <= 0) {
2145 if ($line->delete($notrigger) > 0) {
2149 $this->error = $line->error;
2150 $this->errors = $line->errors;
2165 public function delete(
User $user, $notrigger = 0)
2167 global $langs, $conf;
2168 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2174 if (empty($notrigger)) {
2176 $result = $this->
call_trigger(
'ORDER_SUPPLIER_DELETE', $user);
2178 $this->errors[] =
'ErrorWhenRunningTrigger';
2179 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2180 $this->
db->rollback();
2188 if (!empty($this->linkedObjects) && array_key_exists(
'reception', $this->linkedObjects)) {
2189 foreach ($this->linkedObjects[
'reception'] as $element) {
2190 if ($element->statut >= 0) {
2191 $this->errors[] = $langs->trans(
'ReceptionExist');
2198 $main = MAIN_DB_PREFIX.
'commande_fournisseurdet';
2199 $ef = $main.
"_extrafields";
2200 $sql =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".((int) $this->
id).
")";
2201 dol_syslog(get_class($this).
"::delete extrafields lines", LOG_DEBUG);
2202 if (!$this->
db->query($sql)) {
2203 $this->error = $this->
db->lasterror();
2204 $this->errors[] = $this->
db->lasterror();
2208 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE fk_commande =".((int) $this->
id);
2209 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2210 if (!$this->
db->query($sql)) {
2211 $this->error = $this->
db->lasterror();
2212 $this->errors[] = $this->
db->lasterror();
2216 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseur WHERE rowid =".((int) $this->
id);
2217 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2218 if (
$resql = $this->
db->query($sql)) {
2219 if ($this->
db->affected_rows(
$resql) < 1) {
2220 $this->error = $this->
db->lasterror();
2221 $this->errors[] = $this->
db->lasterror();
2225 $this->error = $this->
db->lasterror();
2226 $this->errors[] = $this->
db->lasterror();
2234 $this->error =
'FailToDeleteExtraFields';
2235 $this->errors[] =
'FailToDeleteExtraFields';
2237 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
2244 $this->error =
'FailToDeleteObjectLinked';
2245 $this->errors[] =
'FailToDeleteObjectLinked';
2255 if ($conf->fournisseur->commande->dir_output) {
2256 $dir = $conf->fournisseur->commande->dir_output.
"/".$ref;
2257 $file = $dir.
"/".$ref.
".pdf";
2258 if (file_exists($file)) {
2260 $this->error =
'ErrorFailToDeleteFile';
2261 $this->errors[] =
'ErrorFailToDeleteFile';
2265 if (file_exists($dir)) {
2268 $this->error =
'ErrorFailToDeleteDir';
2269 $this->errors[] =
'ErrorFailToDeleteDir';
2277 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
2278 $this->
db->commit();
2281 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2282 $this->
db->rollback();
2296 $sql =
"SELECT rowid, libelle";
2297 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_input_method";
2298 $sql .=
" WHERE active = 1";
2303 $num = $this->
db->num_rows(
$resql);
2304 $this->methodes_commande = array();
2306 $row = $this->
db->fetch_row(
$resql);
2308 $this->methodes_commande[$row[0]] = $row[1];
2331 $sql =
"SELECT p.ref, p.label,";
2332 $sql .=
" e.rowid as warehouse_id, e.ref as entrepot,";
2333 $sql .=
" cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
2334 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product as p,";
2335 $sql .=
" ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch as cfd";
2336 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot as e ON cfd.fk_entrepot = e.rowid";
2337 $sql .=
" WHERE cfd.fk_commande = ".((int) $this->
id);
2338 $sql .=
" AND cfd.fk_product = p.rowid";
2340 $sql .=
" AND cfd.status = ".((int) $status);
2342 $sql .=
" ORDER BY cfd.rowid ASC";
2346 $num = $this->
db->num_rows(
$resql);
2350 $objp = $this->
db->fetch_object(
$resql);
2353 'id' => $objp->dispatchedlineid,
2354 'productid' => $objp->fk_product,
2355 'warehouseid' => $objp->warehouse_id,
2356 'qty' => $objp->qty,
2363 dol_print_error($this->
db,
'Failed to execute request to get dispatched lines');
2382 global $conf, $langs;
2389 $usercanreceive = 0;
2391 $usercanreceive = $user->rights->fournisseur->commande->receptionner;
2393 $usercanreceive = $user->rights->reception->creer;
2396 if ($usercanreceive) {
2398 if ($type ==
'par') {
2400 } elseif ($type ==
'tot') {
2402 } elseif ($type ==
'nev') {
2404 } elseif ($type ==
'can') {
2408 dol_syslog(get_class($this).
"::Livraison Error -2", LOG_ERR);
2413 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2415 if (!$error && ($type ==
'tot')) {
2417 if (count($dispatchedlinearray) > 0) {
2420 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2421 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2424 if (!$error && !empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS_NEED_APPROVE) && ($type ==
'tot')) {
2426 if (count($dispatchedlinearray) > 0) {
2429 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2430 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2437 if (empty($error)) {
2440 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2441 $sql .=
" SET fk_statut = ".((int) $statut);
2442 $sql .=
" WHERE rowid = ".((int) $this->
id);
2443 $sql .=
" AND fk_statut IN (".self::STATUS_ORDERSENT.
",".self::STATUS_RECEIVED_PARTIALLY.
")";
2445 dol_syslog(get_class($this).
"::Livraison", LOG_DEBUG);
2449 $old_statut = $this->statut;
2450 $this->statut = $statut;
2451 $this->actionmsg2 = $comment;
2454 $result_trigger = $this->
call_trigger(
'ORDER_SUPPLIER_RECEIVE', $user);
2455 if ($result_trigger < 0) {
2460 if (empty($error)) {
2461 $this->
db->commit();
2463 $this->statut = $old_statut;
2464 $this->
db->rollback();
2465 $this->error = $this->
db->lasterror();
2469 $this->
db->rollback();
2470 $this->error = $this->
db->lasterror();
2475 $this->error = $langs->trans(
'NotAuthorized');
2476 $this->errors[] = $langs->trans(
'NotAuthorized');
2477 dol_syslog(get_class($this).
"::Livraison Not Authorized");
2509 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2514 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2515 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2516 $sql .=
" WHERE rowid = ".((int) $this->
id);
2521 $this->errors[] = $this->
db->error();
2526 $this->oldcopy = clone $this;
2527 $this->date_livraison = $delivery_date;
2528 $this->delivery_date = $delivery_date;
2531 if (!$notrigger && empty($error)) {
2533 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2541 $this->
db->commit();
2544 foreach ($this->errors as $errmsg) {
2545 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2546 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2548 $this->
db->rollback();
2568 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2573 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2574 $sql .=
" SET fk_projet = ".($id_projet > 0 ? (int) $id_projet :
'null');
2575 $sql .=
" WHERE rowid = ".((int) $this->
id);
2580 $this->errors[] = $this->
db->error();
2585 $this->oldcopy = clone $this;
2586 $this->fk_projet = $id_projet;
2587 $this->fk_project = $id_projet;
2590 if (!$notrigger && empty($error)) {
2592 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2600 $this->
db->commit();
2603 foreach ($this->errors as $errmsg) {
2604 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2605 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2607 $this->
db->rollback();
2626 $comclient->fetch($comclientid);
2630 $this->lines = array();
2632 $num = count($comclient->lines);
2633 for ($i = 0; $i < $num; $i++) {
2637 if ($prod->fetch($comclient->lines[$i]->fk_product) > 0) {
2638 $label = $prod->label;
2642 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseurdet";
2643 $sql .=
" (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2644 $sql .=
" VALUES (".((int) $idc).
", '".$this->
db->escape($label).
"', '".$this->
db->escape($comclient->lines[$i]->desc).
"'";
2645 $sql .=
",".$comclient->lines[$i]->fk_product.
", ".
price2num($comclient->lines[$i]->price,
'MU');
2646 $sql .=
", ".price2num($comclient->lines[$i]->qty,
'MS').
", ".
price2num($comclient->lines[$i]->tva_tx, 5).
", ".
price2num($comclient->lines[$i]->localtax1_tx, 5).
", ".
price2num($comclient->lines[$i]->localtax2_tx, 5).
", ".
price2num($comclient->lines[$i]->remise_percent, 3);
2647 $sql .=
", '".price2num($comclient->lines[$i]->subprice,
'MT').
"','0', '".$this->
db->escape($ref).
"');";
2648 if ($this->
db->query($sql)) {
2665 global $conf, $langs;
2670 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur';
2671 $sql .=
" SET fk_statut = ".$status;
2672 $sql .=
" WHERE rowid = ".((int) $this->
id);
2674 dol_syslog(get_class($this).
"::setStatus", LOG_DEBUG);
2678 $triggerName = array();
2679 $triggerName[0] =
'DRAFT';
2680 $triggerName[1] =
'VALIDATED';
2681 $triggerName[2] =
'APPROVED';
2682 $triggerName[3] =
'ORDERED';
2683 $triggerName[4] =
'RECEIVED_PARTIALLY';
2684 $triggerName[5] =
'RECEIVED_COMPLETELY';
2685 $triggerName[6] =
'CANCELED';
2686 $triggerName[7] =
'CANCELED';
2687 $triggerName[9] =
'REFUSED';
2690 $result = $this->
call_trigger(
"ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
2697 $this->error = $this->
db->lasterror();
2698 dol_syslog(get_class($this).
"::setStatus ".$this->error);
2702 $this->statut = $status;
2703 $this->
db->commit();
2706 $this->
db->rollback();
2734 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type =
'HT', $info_bits = 0, $type = 0, $notrigger = 0, $date_start =
'', $date_end =
'', $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $ref_supplier =
'')
2736 global $mysoc, $conf, $langs;
2737 dol_syslog(get_class($this).
"::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2738 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2742 if ($this->brouillon) {
2747 if (empty($info_bits)) {
2750 if (empty($txtva)) {
2753 if (empty($txlocaltax1)) {
2756 if (empty($txlocaltax2)) {
2759 if (empty($remise)) {
2762 if (empty($remise_percent)) {
2763 $remise_percent = 0;
2766 $remise_percent =
price2num($remise_percent);
2772 $pu_ht_devise =
price2num($pu_ht_devise);
2773 if (!preg_match(
'/\((.*)\)/', $txtva)) {
2783 if ($date_start && $date_end && $date_start > $date_end) {
2784 $langs->load(
"errors");
2785 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2801 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2802 $vat_src_code = $reg[1];
2803 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2806 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
2807 $total_ht = $tabprice[0];
2808 $total_tva = $tabprice[1];
2809 $total_ttc = $tabprice[2];
2810 $total_localtax1 = $tabprice[9];
2811 $total_localtax2 = $tabprice[10];
2812 $pu_ht = $tabprice[3];
2813 $pu_tva = $tabprice[4];
2814 $pu_ttc = $tabprice[5];
2817 $multicurrency_total_ht = $tabprice[16];
2818 $multicurrency_total_tva = $tabprice[17];
2819 $multicurrency_total_ttc = $tabprice[18];
2820 $pu_ht_devise = $tabprice[19];
2822 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2823 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2827 $this->line->fetch($rowid);
2829 $oldline = clone $this->line;
2830 $this->line->oldline = $oldline;
2832 $this->line->context = $this->context;
2834 $this->line->fk_commande = $this->id;
2836 $this->line->desc = $desc;
2839 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2840 if ($qty < $this->line->packaging) {
2841 $qty = $this->line->packaging;
2843 if (!empty($this->line->packaging) && ($qty % $this->line->packaging) > 0) {
2844 $coeff = intval($qty / $this->line->packaging) + 1;
2845 $qty = $this->line->packaging * $coeff;
2846 setEventMessage($langs->trans(
'QtyRecalculatedWithPackaging'),
'mesgs');
2851 $this->line->qty = $qty;
2852 $this->line->ref_supplier = $ref_supplier;
2854 $this->line->vat_src_code = $vat_src_code;
2855 $this->line->tva_tx = $txtva;
2856 $this->line->localtax1_tx = $txlocaltax1;
2857 $this->line->localtax2_tx = $txlocaltax2;
2858 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2859 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2860 $this->line->remise_percent = $remise_percent;
2861 $this->line->subprice = $pu_ht;
2862 $this->line->rang = $this->rang;
2863 $this->line->info_bits = $info_bits;
2864 $this->line->total_ht = $total_ht;
2865 $this->line->total_tva = $total_tva;
2866 $this->line->total_localtax1 = $total_localtax1;
2867 $this->line->total_localtax2 = $total_localtax2;
2868 $this->line->total_ttc = $total_ttc;
2869 $this->line->product_type = $type;
2870 $this->line->special_code = (!empty($this->special_code) ? $this->special_code : 0);
2871 $this->line->origin = $this->origin;
2872 $this->line->fk_unit = $fk_unit;
2874 $this->line->date_start = $date_start;
2875 $this->line->date_end = $date_end;
2878 $this->line->fk_multicurrency = $this->fk_multicurrency;
2879 $this->line->multicurrency_code = $this->multicurrency_code;
2880 $this->line->multicurrency_subprice = $pu_ht_devise;
2881 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2882 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2883 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2885 $this->line->subprice = $pu_ht;
2886 $this->line->price = $this->line->subprice;
2888 $this->line->remise_percent = $remise_percent;
2890 if (is_array($array_options) && count($array_options) > 0) {
2892 foreach ($array_options as $key => $value) {
2893 $this->line->array_options[$key] = $array_options[$key];
2897 $result = $this->line->update($notrigger);
2903 $this->
db->commit();
2906 $this->error = $this->
db->lasterror();
2907 $this->
db->rollback();
2911 $this->error =
"Order status makes operation forbidden";
2912 dol_syslog(get_class($this).
"::updateline ".$this->error, LOG_ERR);
2927 global $user, $langs, $conf;
2929 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
2931 dol_syslog(get_class($this).
"::initAsSpecimen");
2938 $sql =
"SELECT rowid";
2939 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2940 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2941 $sql .= $this->
db->order(
"rowid",
"ASC");
2942 $sql .= $this->
db->plimit(1);
2945 $obj = $this->
db->fetch_object(
$resql);
2946 $prodid = $obj->rowid;
2951 $this->
ref =
'SPECIMEN';
2952 $this->specimen = 1;
2955 $this->date_commande = $now;
2956 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2957 $this->cond_reglement_code =
'RECEP';
2958 $this->mode_reglement_code =
'CHQ';
2960 $this->note_public =
'This is a comment (public)';
2961 $this->note_private =
'This is a comment (private)';
2963 $this->multicurrency_tx = 1;
2964 $this->multicurrency_code = $conf->currency;
2971 while ($xnbp < $nbp) {
2973 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2975 $line->subprice = 100;
2977 $line->tva_tx = 19.6;
2978 $line->localtax1_tx = 0;
2979 $line->localtax2_tx = 0;
2981 $line->total_ht = 50;
2982 $line->total_ttc = 59.8;
2983 $line->total_tva = 9.8;
2984 $line->remise_percent = 50;
2986 $line->total_ht = 100;
2987 $line->total_ttc = 119.6;
2988 $line->total_tva = 19.6;
2989 $line->remise_percent = 00;
2991 $line->fk_product = $prodid;
2993 $this->lines[$xnbp] = $line;
2995 $this->total_ht += $line->total_ht;
2996 $this->total_tva += $line->total_tva;
2997 $this->total_ttc += $line->total_ttc;
3011 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
3012 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
3013 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c';
3014 $sql .=
' WHERE c.rowid = '.((int) $id);
3016 $result = $this->
db->query($sql);
3018 if ($this->
db->num_rows($result)) {
3019 $obj = $this->
db->fetch_object($result);
3020 $this->
id = $obj->rowid;
3021 if ($obj->fk_user_author) {
3022 $this->user_creation_id = $obj->fk_user_author;
3024 if ($obj->fk_user_valid) {
3025 $this->user_validation_id = $obj->fk_user_valid;
3027 if ($obj->fk_user_modif) {
3028 $this->user_modification_id = $obj->fk_user_modif;
3030 if ($obj->fk_user_approve) {
3031 $this->user_approve_id = $obj->fk_user_approve;
3033 if ($obj->fk_user_approve2) {
3034 $this->user_approve_id2 = $obj->fk_user_approve2;
3037 $this->date_creation = $this->
db->jdate($obj->datec);
3038 $this->date_modification = $this->
db->jdate($obj->datem);
3039 $this->date_approve = $this->
db->jdate($obj->datea);
3040 $this->date_approve2 = $this->
db->jdate($obj->datea2);
3041 $this->date_validation = $this->
db->jdate($obj->date_validation);
3043 $this->
db->free($result);
3058 global $conf, $user;
3060 $this->nb = array();
3063 $sql =
"SELECT count(co.rowid) as nb";
3064 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as co";
3065 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
3066 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3067 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3068 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3071 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'supplier_order').
")";
3075 while ($obj = $this->
db->fetch_object(
$resql)) {
3076 $this->nb[
"supplier_orders"] = $obj->nb;
3082 $this->error = $this->
db->error();
3098 global $conf, $langs;
3100 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date, c.total_ht";
3101 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
3102 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3103 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3105 $sql .=
" WHERE c.entity = ".$conf->entity;
3106 if ($mode ===
'awaiting') {
3107 $sql .=
" AND c.fk_statut IN (".self::STATUS_ORDERSENT.
", ".self::STATUS_RECEIVED_PARTIALLY.
")";
3109 $sql .=
" AND c.fk_statut IN (".self::STATUS_VALIDATED.
", ".self::STATUS_ACCEPTED.
")";
3112 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3120 $response->warning_delay = $conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
3121 $response->label = $langs->trans(
"SuppliersOrdersToProcess");
3122 $response->labelShort = $langs->trans(
"Opened");
3123 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
3126 if ($mode ===
'awaiting') {
3127 $response->label = $langs->trans(
"SuppliersOrdersAwaitingReception");
3128 $response->labelShort = $langs->trans(
"AwaitingReception");
3129 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
3132 while ($obj = $this->
db->fetch_object(
$resql)) {
3133 $commandestatic->delivery_date = $this->
db->jdate($obj->delivery_date);
3134 $commandestatic->date_commande = $this->
db->jdate($obj->date_commande);
3135 $commandestatic->statut = $obj->fk_statut;
3137 $response->nbtodo++;
3138 $response->total += $obj->total_ht;
3140 if ($commandestatic->hasDelay()) {
3141 $response->nbtodolate++;
3147 $this->error = $this->
db->error();
3162 if ($this->methode_commande_id > 0) {
3163 $sql =
"SELECT rowid, code, libelle as label";
3164 $sql .=
" FROM ".MAIN_DB_PREFIX.
'c_input_method';
3165 $sql .=
" WHERE active=1 AND rowid = ".((int) $this->methode_commande_id);
3170 $obj = $this->
db->fetch_object(
$resql);
3172 $string = $langs->trans($obj->code);
3173 if ($string == $obj->code) {
3174 $string = $obj->label !=
'-' ? $obj->label :
'';
3197 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3199 global $conf, $langs;
3204 if (!empty($this->model_pdf)) {
3205 $modele = $this->model_pdf;
3206 } elseif (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
3207 $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
3211 if (empty($modele)) {
3214 $langs->load(
"suppliers");
3215 $outputlangs->load(
"products");
3217 $modelpath =
"core/modules/supplier_order/doc/";
3218 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3230 if (empty($this->lines)) {
3237 foreach ($this->lines as $line) {
3238 if ($line->fk_product > 0) {
3239 $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
3242 if ($obj->delivery_time_days > $nb) {
3243 $nb = $obj->delivery_time_days;
3252 return $nb.
' '.$langs->trans(
'Days');
3264 return $user->rights->fournisseur->commande;
3279 'commande_fournisseur'
3296 'commande_fournisseurdet'
3313 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3314 $this->delivery_date = $this->date_livraison;
3317 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3319 if (!empty($this->delivery_date)) {
3320 $date_to_test = $this->delivery_date;
3321 return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3329 $date_to_test = $this->date_commande;
3331 return ($this->statut > 0 && $this->statut < 5) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3344 global $conf, $langs;
3346 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3347 $this->delivery_date = $this->date_livraison;
3352 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3353 if (!empty($this->delivery_date)) {
3354 $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
3356 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3359 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3362 $text .=
' '.($conf->commande->fournisseur->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
3379 global $conf, $langs;
3381 if ((
isModEnabled(
"fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) ||
isModEnabled(
"supplier_order")) {
3382 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.dispatch.class.php';
3384 $qtydelivered = array();
3385 $qtywished = array();
3388 $filter = array(
't.fk_commande'=>$this->
id);
3389 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
3390 $filter[
't.status'] = 1;
3393 $ret = $supplierorderdispatch->fetchAll(
'',
'', 0, 0, $filter);
3395 $this->error = $supplierorderdispatch->error; $this->errors = $supplierorderdispatch->errors;
3398 if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3399 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3403 foreach ($supplierorderdispatch->lines as $line) {
3404 $qtydelivered[$line->fk_product] += $line->qty;
3406 foreach ($this->lines as $line) {
3408 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $line->product_type > 0) {
3411 $qtywished[$line->fk_product] += $line->qty;
3415 $diff_array = array_diff_assoc($qtydelivered, $qtywished);
3416 $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered));
3417 $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished));
3425 if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) {
3426 if ($closeopenorder) {
3428 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3436 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3442 } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
3447 if (count($diff_array) > 0) {
3451 foreach ($diff_array as $key => $value) {
3453 if ($qtydelivered[$key] >= $qtywished[$key]) {
3460 if ($close == count($diff_array)) {
3462 if ($closeopenorder) {
3463 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3470 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3478 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3486 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3508 $this->receptions = array();
3510 dol_syslog(get_class($this).
"::loadReceptions", LOG_DEBUG);
3512 $sql =
'SELECT cd.rowid, cd.fk_product,';
3513 $sql .=
' sum(cfd.qty) as qty';
3514 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur_dispatch as cfd,';
3515 if ($filtre_statut >= 0) {
3516 $sql .=
' '.MAIN_DB_PREFIX.
'reception as e,';
3518 $sql .=
' '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3520 if ($filtre_statut >= 0) {
3521 $sql .=
' cfd.fk_reception = e.rowid AND';
3523 $sql .=
' cfd.fk_commandefourndet = cd.rowid';
3524 $sql .=
' AND cd.fk_commande ='.((int) $this->
id);
3525 if ($this->fk_product > 0) {
3526 $sql .=
' AND cd.fk_product = '.((int) $this->fk_product);
3528 if ($filtre_statut >= 0) {
3529 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
3531 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
3535 $num = $this->
db->num_rows(
$resql);
3538 $obj = $this->
db->fetch_object(
$resql);
3539 empty($this->receptions[$obj->rowid]) ? $this->receptions[$obj->rowid] = $obj->qty : $this->receptions[$obj->rowid] += $obj->qty;
3546 $this->error = $this->
db->lasterror();
3562 public $element =
'commande_fournisseurdet';
3567 public $table_element =
'commande_fournisseurdet';
3575 public $fk_commande;
3581 public $fk_parent_line;
3589 public $special_code = 0;
3606 public $ref_supplier;
3630 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
3631 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
3632 $sql .=
' cd.remise, cd.remise_percent, cd.subprice,';
3633 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3634 $sql .=
' cd.total_localtax1, cd.total_localtax2,';
3635 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
3636 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit,';
3637 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
3638 $sql .=
' c.fk_soc as socid';
3639 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c, '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3640 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
3641 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
3643 $result = $this->
db->query($sql);
3645 $objp = $this->
db->fetch_object($result);
3647 if (!empty($objp)) {
3648 $this->
rowid = $objp->rowid;
3649 $this->
id = $objp->rowid;
3650 $this->fk_commande = $objp->fk_commande;
3651 $this->desc = $objp->description;
3652 $this->qty = $objp->qty;
3653 $this->ref_fourn = $objp->ref_supplier;
3654 $this->ref_supplier = $objp->ref_supplier;
3655 $this->subprice = $objp->subprice;
3656 $this->tva_tx = $objp->tva_tx;
3657 $this->localtax1_tx = $objp->localtax1_tx;
3658 $this->localtax2_tx = $objp->localtax2_tx;
3659 $this->localtax1_type = $objp->localtax1_type;
3660 $this->localtax2_type = $objp->localtax2_type;
3661 $this->remise = $objp->remise;
3662 $this->remise_percent = $objp->remise_percent;
3663 $this->fk_product = $objp->fk_product;
3664 $this->info_bits = $objp->info_bits;
3665 $this->total_ht = $objp->total_ht;
3666 $this->total_tva = $objp->total_tva;
3667 $this->total_localtax1 = $objp->total_localtax1;
3668 $this->total_localtax2 = $objp->total_localtax2;
3669 $this->total_ttc = $objp->total_ttc;
3670 $this->product_type = $objp->product_type;
3671 $this->special_code = $objp->special_code;
3673 $this->
ref = $objp->product_ref;
3675 $this->product_ref = $objp->product_ref;
3676 $this->product_label = $objp->product_label;
3677 $this->product_desc = $objp->product_desc;
3679 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
3684 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
3685 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
3686 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
3687 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
3688 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
3689 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
3690 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
3691 $sqlsearchpackage .=
" ORDER BY packaging ASC";
3692 $sqlsearchpackage .=
" LIMIT 1";
3694 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
3695 if ($resqlsearchpackage) {
3696 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
3697 if ($objsearchpackage) {
3698 $this->fk_fournprice = $objsearchpackage->rowid;
3699 $this->packaging = $objsearchpackage->packaging;
3702 $this->error = $this->
db->lasterror();
3707 $this->date_start = $this->
db->jdate($objp->date_start);
3708 $this->date_end = $this->
db->jdate($objp->date_end);
3709 $this->fk_unit = $objp->fk_unit;
3711 $this->multicurrency_subprice = $objp->multicurrency_subprice;
3712 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3713 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3714 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3718 $this->
db->free($result);
3721 $this->error =
'Supplier order line with id='.$rowid.
' not found';
3722 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
3739 global $conf, $user;
3743 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
3746 if (empty($this->tva_tx)) {
3749 if (empty($this->localtax1_tx)) {
3750 $this->localtax1_tx = 0;
3752 if (empty($this->localtax2_tx)) {
3753 $this->localtax2_tx = 0;
3755 if (empty($this->localtax1_type)) {
3756 $this->localtax1_type =
'0';
3758 if (empty($this->localtax2_type)) {
3759 $this->localtax2_type =
'0';
3761 if (empty($this->total_localtax1)) {
3762 $this->total_localtax1 = 0;
3764 if (empty($this->total_localtax2)) {
3765 $this->total_localtax2 = 0;
3767 if (empty($this->rang)) {
3770 if (empty($this->remise_percent)) {
3771 $this->remise_percent = 0;
3773 if (empty($this->info_bits)) {
3774 $this->info_bits = 0;
3776 if (empty($this->special_code)) {
3777 $this->special_code = 0;
3779 if (empty($this->fk_parent_line)) {
3780 $this->fk_parent_line = 0;
3782 if (empty($this->pa_ht)) {
3787 if (!empty($this->multicurrency_code)) {
3790 if (empty($this->fk_multicurrency)) {
3791 $this->multicurrency_code = $conf->currency;
3792 $this->fk_multicurrency = 0;
3793 $this->multicurrency_tx = 1;
3797 if ($this->product_type < 0) {
3804 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3805 $sql .=
" (fk_commande, label, description, date_start, date_end,";
3806 $sql .=
" fk_product, product_type, special_code, rang,";
3807 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3808 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3809 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3811 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->
db->escape($this->label).
"','".$this->
db->escape($this->desc).
"',";
3812 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3813 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3814 if ($this->fk_product) {
3815 $sql .= $this->fk_product.
",";
3819 $sql .=
"'".$this->db->escape($this->product_type).
"',";
3820 $sql .=
"'".$this->db->escape($this->special_code).
"',";
3821 $sql .=
"'".$this->db->escape($this->rang).
"',";
3822 $sql .=
"'".$this->db->escape($this->qty).
"', ";
3823 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
3824 $sql .=
" ".price2num($this->tva_tx).
", ";
3825 $sql .=
" ".price2num($this->localtax1_tx).
",";
3826 $sql .=
" ".price2num($this->localtax2_tx).
",";
3827 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3828 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3829 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", '".$this->
db->escape($this->ref_supplier).
"',";
3830 $sql .=
" ".price2num($this->total_ht).
",";
3831 $sql .=
" ".price2num($this->total_tva).
",";
3832 $sql .=
" ".price2num($this->total_localtax1).
",";
3833 $sql .=
" ".price2num($this->total_localtax2).
",";
3834 $sql .=
" ".price2num($this->total_ttc).
",";
3835 $sql .= ($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
3836 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
3837 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
3838 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
3839 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
3840 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
3841 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
3844 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3847 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3848 $this->
rowid = $this->id;
3857 if (!$error && !$notrigger) {
3859 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
3867 $this->
db->commit();
3871 foreach ($this->errors as $errmsg) {
3872 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
3873 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
3875 $this->
db->rollback();
3878 $this->errors[] = $this->
db->error();
3879 $this->
db->rollback();
3891 global $conf, $user;
3898 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3899 $sql .=
" description='".$this->db->escape($this->desc).
"'";
3900 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
3901 $sql .=
", subprice='".price2num($this->subprice).
"'";
3903 $sql .=
", remise_percent='".price2num($this->remise_percent).
"'";
3905 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
3906 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
3907 $sql .=
", localtax1_tx='".price2num($this->localtax1_tx).
"'";
3908 $sql .=
", localtax2_tx='".price2num($this->localtax2_tx).
"'";
3909 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
3910 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
3911 $sql .=
", qty='".price2num($this->qty).
"'";
3912 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3913 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3914 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
3915 $sql .=
", total_ht='".price2num($this->total_ht).
"'";
3916 $sql .=
", total_tva='".price2num($this->total_tva).
"'";
3917 $sql .=
", total_localtax1='".price2num($this->total_localtax1).
"'";
3918 $sql .=
", total_localtax2='".price2num($this->total_localtax2).
"'";
3919 $sql .=
", total_ttc='".price2num($this->total_ttc).
"'";
3920 $sql .=
", product_type=".$this->product_type;
3921 $sql .=
", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
3922 $sql .= ($this->fk_unit ?
", fk_unit='".$this->db->escape($this->fk_unit).
"'" :
", fk_unit=null");
3925 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
3926 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
3927 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
3928 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
3930 $sql .=
" WHERE rowid = ".((int) $this->
id);
3932 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
3942 if (!$error && !$notrigger) {
3944 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
3946 $this->
db->rollback();
3953 $this->
db->commit();
3956 $this->
db->rollback();
3960 $this->error = $this->
db->lasterror();
3961 $this->
db->rollback();
3972 public function delete($notrigger = 0)
3983 $this->
db->rollback();
3987 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE rowid=".((int) $this->
id);
3994 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DELETE', $user);
4002 $this->
db->commit();
4006 foreach ($this->errors as $errmsg) {
4007 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4008 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4010 $this->
db->rollback();
4013 $this->error = $this->
db->lasterror();