37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonorder.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
39 if (!empty($conf->productbatch->enabled)) {
40 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
42 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
52 public $element =
'order_supplier';
57 public $table_element =
'commande_fournisseur';
62 public $table_element_line =
'commande_fournisseurdet';
67 public $fk_element =
'fk_commande';
72 public $picto =
'supplier_order';
78 public $ismultientitymanaged = 1;
84 public $restrictiononfksoc = 1;
102 public $ref_supplier;
116 public $date_creation;
118 public $date_approve;
119 public $date_approve2;
120 public $date_commande;
126 public $date_livraison;
131 public $delivery_date;
135 public $total_localtax1;
136 public $total_localtax2;
145 public $cond_reglement_id;
146 public $cond_reglement_code;
147 public $cond_reglement_label;
148 public $cond_reglement_doc;
155 public $mode_reglement_id;
156 public $mode_reglement_code;
157 public $user_author_id;
158 public $user_valid_id;
159 public $user_approve_id;
160 public $user_approve_id2;
164 public $extraparams = array();
169 public $lines = array();
174 public $linked_objects = array();
180 public $fk_multicurrency;
182 public $multicurrency_code;
183 public $multicurrency_tx;
184 public $multicurrency_total_ht;
185 public $multicurrency_total_tva;
186 public $multicurrency_total_ttc;
217 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>10),
218 'ref' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'showoncombobox'=>1,
'position'=>25,
'searchall'=>1),
219 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>35),
220 'ref_supplier' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefOrderSupplierShort',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'searchall'=>1),
221 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>45),
222 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
223 'date_approve' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove',
'enabled'=>1,
'visible'=>-1,
'position'=>62),
224 'date_approve2' =>array(
'type'=>
'datetime',
'label'=>
'DateApprove2',
'enabled'=>1,
'visible'=>3,
'position'=>64),
225 'date_commande' =>array(
'type'=>
'date',
'label'=>
'OrderDateShort',
'enabled'=>1,
'visible'=>1,
'position'=>70),
226 'date_livraison' =>array(
'type'=>
'datetime',
'label'=>
'DeliveryDate',
'enabled'=>
'empty($conf->global->ORDER_DISABLE_DELIVERY_DATE)',
'visible'=>1,
'position'=>74),
227 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>3,
'position'=>75),
228 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>3,
'notnull'=>-1,
'position'=>80),
229 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>3,
'position'=>85),
230 'fk_user_approve' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval',
'enabled'=>1,
'visible'=>3,
'position'=>90),
231 'fk_user_approve2' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserApproval2',
'enabled'=>1,
'visible'=>3,
'position'=>95),
232 'source' =>array(
'type'=>
'smallint(6)',
'label'=>
'Source',
'enabled'=>1,
'visible'=>3,
'notnull'=>1,
'position'=>100),
233 'billed' =>array(
'type'=>
'smallint(6)',
'label'=>
'Billed',
'enabled'=>1,
'visible'=>1,
'position'=>110),
234 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>1,
'position'=>130,
'isameasure'=>1),
235 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>3,
'position'=>135,
'isameasure'=>1),
236 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>3,
'position'=>140,
'isameasure'=>1),
237 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>1,
'position'=>145,
'isameasure'=>1),
238 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>150,
'isameasure'=>1),
239 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>155,
'searchall'=>1),
240 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>160,
'searchall'=>1),
241 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'ModelPDF',
'enabled'=>1,
'visible'=>0,
'position'=>165),
242 'fk_input_method' =>array(
'type'=>
'integer',
'label'=>
'OrderMode',
'enabled'=>1,
'visible'=>3,
'position'=>170),
243 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>3,
'position'=>175),
244 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>3,
'position'=>180),
245 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>0,
'position'=>190),
246 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>3,
'position'=>200),
247 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>1,
'visible'=>3,
'position'=>205),
248 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLocation',
'enabled'=>1,
'visible'=>3,
'position'=>210),
249 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>0,
'position'=>215),
250 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Currency',
'enabled'=>
'!empty($conf->multicurrency->enabled)',
'visible'=>-1,
'position'=>220),
251 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'CurrencyRate',
'enabled'=>
'!empty($conf->multicurrency->enabled)',
'visible'=>-1,
'position'=>225),
252 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'!empty($conf->multicurrency->enabled)',
'visible'=>-1,
'position'=>230),
253 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'!empty($conf->multicurrency->enabled)',
'visible'=>-1,
'position'=>235),
254 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'!empty($conf->multicurrency->enabled)',
'visible'=>-1,
'position'=>240),
255 'date_creation' =>array(
'type'=>
'datetime',
'label'=>
'Date creation',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
256 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>1,
'notnull'=>1,
'position'=>46),
257 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>1000,
'index'=>1),
258 'tms'=>array(
'type'=>
'datetime',
'label'=>
"DateModificationShort",
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>501),
259 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>0,
'position'=>700),
260 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'position'=>701),
261 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>0,
'position'=>900),
336 public function fetch($id, $ref =
'')
341 if (empty($id) && empty($ref)) {
345 $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,";
346 $sql .=
" c.localtax1, c.localtax2, ";
347 $sql .=
" c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
348 $sql .=
" c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
349 $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,";
350 $sql .=
" c.fk_account,";
351 $sql .=
" c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
352 $sql .=
" c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
353 $sql .=
" cm.libelle as methode_commande,";
354 $sql .=
" cr.code as cond_reglement_code, cr.libelle as cond_reglement_label, cr.libelle_facture as cond_reglement_doc,";
355 $sql .=
" p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
356 $sql .=
', c.fk_incoterms, c.location_incoterms';
357 $sql .=
', i.libelle as label_incoterms';
358 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
359 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
360 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as p ON c.fk_mode_reglement = p.id";
361 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_input_method as cm ON cm.rowid = c.fk_input_method";
362 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON c.fk_incoterms = i.rowid';
365 $sql .=
" WHERE c.entity IN (".getEntity(
'supplier_order').
")";
367 $sql .=
" WHERE c.rowid=".((int) $id);
371 $sql .=
" AND c.ref='".$this->db->escape($ref).
"'";
374 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
377 $obj = $this->
db->fetch_object(
$resql);
379 $this->error =
'Bill with id '.$id.
' not found';
380 dol_syslog(get_class($this).
'::fetch '.$this->error);
384 $this->
id = $obj->rowid;
385 $this->entity = $obj->entity;
387 $this->
ref = $obj->ref;
388 $this->ref_supplier = $obj->ref_supplier;
389 $this->socid = $obj->fk_soc;
390 $this->fourn_id = $obj->fk_soc;
391 $this->statut = $obj->fk_statut;
392 $this->status = $obj->fk_statut;
393 $this->billed = $obj->billed;
394 $this->user_author_id = $obj->fk_user_author;
395 $this->user_valid_id = $obj->fk_user_valid;
396 $this->user_approve_id = $obj->fk_user_approve;
397 $this->user_approve_id2 = $obj->fk_user_approve2;
398 $this->total_ht = $obj->total_ht;
399 $this->total_tva = $obj->total_tva;
400 $this->total_localtax1 = $obj->localtax1;
401 $this->total_localtax2 = $obj->localtax2;
402 $this->total_ttc = $obj->total_ttc;
403 $this->date_creation = $this->
db->jdate($obj->date_creation);
404 $this->date_valid = $this->
db->jdate($obj->date_valid);
405 $this->date_approve = $this->
db->jdate($obj->date_approve);
406 $this->date_approve2 = $this->
db->jdate($obj->date_approve2);
407 $this->date_commande = $this->
db->jdate($obj->date_commande);
408 if (isset($obj->date_commande)) {
409 $this->date = $this->date_commande;
411 $this->date = $this->date_creation;
413 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
414 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
415 $this->remise_percent = $obj->remise_percent;
416 $this->methode_commande_id = $obj->fk_input_method;
417 $this->methode_commande = $obj->methode_commande;
419 $this->source = $obj->source;
420 $this->fk_project = $obj->fk_project;
421 $this->cond_reglement_id = $obj->fk_cond_reglement;
422 $this->cond_reglement_code = $obj->cond_reglement_code;
423 $this->cond_reglement = $obj->cond_reglement_label;
424 $this->cond_reglement_label = $obj->cond_reglement_label;
425 $this->cond_reglement_doc = $obj->cond_reglement_doc;
426 $this->fk_account = $obj->fk_account;
427 $this->mode_reglement_id = $obj->fk_mode_reglement;
428 $this->mode_reglement_code = $obj->mode_reglement_code;
429 $this->mode_reglement = $obj->mode_reglement_libelle;
430 $this->note = $obj->note_private;
431 $this->note_private = $obj->note_private;
432 $this->note_public = $obj->note_public;
433 $this->model_pdf = $obj->model_pdf;
434 $this->modelpdf = $obj->model_pdf;
437 $this->fk_incoterms = $obj->fk_incoterms;
438 $this->location_incoterms = $obj->location_incoterms;
439 $this->label_incoterms = $obj->label_incoterms;
442 $this->fk_multicurrency = $obj->fk_multicurrency;
443 $this->multicurrency_code = $obj->multicurrency_code;
444 $this->multicurrency_tx = $obj->multicurrency_tx;
445 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
446 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
447 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
449 $this->extraparams = (array) json_decode($obj->extraparams,
true);
457 if ($this->statut == 0) {
458 $this->brouillon = 1;
472 $this->error = $this->
db->error().
" sql=".$sql;
489 $this->lines = array();
491 $sql =
"SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
492 $sql .=
" l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
493 $sql .=
" l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
494 $sql .=
" l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
495 $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,";
496 $sql .=
" l.fk_unit,";
497 $sql .=
" l.date_start, l.date_end,";
498 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
499 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet as l";
500 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
501 $sql .=
" WHERE l.fk_commande = ".((int) $this->
id);
503 $sql .=
' AND p.fk_product_type = 0';
505 $sql .=
" ORDER BY l.rang, l.rowid";
508 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
510 $result = $this->
db->query($sql);
512 $num = $this->
db->num_rows($result);
516 $objp = $this->
db->fetch_object($result);
520 $line->id = $objp->rowid;
521 $line->desc = $objp->description;
522 $line->description = $objp->description;
523 $line->qty = $objp->qty;
524 $line->tva_tx = $objp->tva_tx;
525 $line->localtax1_tx = $objp->localtax1_tx;
526 $line->localtax2_tx = $objp->localtax2_tx;
527 $line->localtax1_type = $objp->localtax1_type;
528 $line->localtax2_type = $objp->localtax2_type;
529 $line->subprice = $objp->subprice;
530 $line->pu_ht = $objp->subprice;
531 $line->remise_percent = $objp->remise_percent;
533 $line->vat_src_code = $objp->vat_src_code;
534 $line->total_ht = $objp->total_ht;
535 $line->total_tva = $objp->total_tva;
536 $line->total_localtax1 = $objp->total_localtax1;
537 $line->total_localtax2 = $objp->total_localtax2;
538 $line->total_ttc = $objp->total_ttc;
539 $line->product_type = $objp->product_type;
541 $line->fk_product = $objp->fk_product;
543 $line->libelle = $objp->product_label;
544 $line->product_label = $objp->product_label;
545 $line->product_desc = $objp->product_desc;
546 $line->product_tobatch = $objp->product_tobatch;
547 $line->product_barcode = $objp->product_barcode;
549 $line->ref = $objp->product_ref;
550 $line->product_ref = $objp->product_ref;
551 $line->ref_fourn = $objp->ref_supplier;
552 $line->ref_supplier = $objp->ref_supplier;
554 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
559 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
560 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
561 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
562 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
563 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
564 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
565 $sqlsearchpackage .=
" AND fk_soc = ".((int) $this->socid);
566 $sqlsearchpackage .=
" ORDER BY packaging ASC";
567 $sqlsearchpackage .=
" LIMIT 1";
569 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
570 if ($resqlsearchpackage) {
571 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
572 if ($objsearchpackage) {
573 $line->fk_fournprice = $objsearchpackage->rowid;
574 $line->packaging = $objsearchpackage->packaging;
577 $this->error = $this->
db->lasterror();
582 $line->date_start = $this->
db->jdate($objp->date_start);
583 $line->date_end = $this->
db->jdate($objp->date_end);
584 $line->fk_unit = $objp->fk_unit;
587 $line->fk_multicurrency = $objp->fk_multicurrency;
588 $line->multicurrency_code = $objp->multicurrency_code;
589 $line->multicurrency_subprice = $objp->multicurrency_subprice;
590 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
591 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
592 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
594 $line->special_code = $objp->special_code;
595 $line->fk_parent_line = $objp->fk_parent_line;
597 $line->rang = $objp->rang;
601 $line->fetch_optionals();
603 $this->lines[$i] = $line;
607 $this->
db->free($result);
611 $this->error = $this->
db->error().
" sql=".$sql;
624 public function valid($user, $idwarehouse = 0, $notrigger = 0)
626 global $langs, $conf;
627 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
633 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && (!empty($user->rights->fournisseur->commande->creer) || !empty($user->rights->supplier_order->creer)))
634 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->fournisseur->supplier_order_advance->validate))) {
639 $soc->fetch($this->fourn_id);
642 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
649 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"commande_fournisseur";
650 $sql .=
" SET ref='".$this->db->escape($num).
"',";
651 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
",";
652 $sql .=
" date_valid='".$this->db->idate(
dol_now()).
"',";
653 $sql .=
" fk_user_valid = ".((int) $user->id);
654 $sql .=
" WHERE rowid = ".((int) $this->
id);
655 $sql .=
" AND fk_statut = ".self::STATUS_DRAFT;
663 if (!$error && !$notrigger) {
665 $result = $this->
call_trigger(
'ORDER_SUPPLIER_VALIDATE', $user);
673 $this->oldref = $this->ref;
676 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
678 $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).
"'";
679 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'fournisseur/commande/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
682 $error++; $this->error = $this->
db->lasterror();
688 $dirsource = $conf->fournisseur->commande->dir_output.
'/'.$oldref;
689 $dirdest = $conf->fournisseur->commande->dir_output.
'/'.$newref;
690 if (!$error && file_exists($dirsource)) {
691 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
693 if (@rename($dirsource, $dirdest)) {
696 $listoffiles =
dol_dir_list($conf->fournisseur->commande->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
697 foreach ($listoffiles as $fileentry) {
698 $dirsource = $fileentry[
'name'];
699 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
700 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
701 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
702 @rename($dirsource, $dirdest);
719 $this->
db->rollback();
723 $this->error =
'NotAuthorized';
724 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
737 return $this->
LibStatut($this->statut, $mode, $this->billed);
749 public function LibStatut($status, $mode = 0, $billed = 0)
752 global $conf, $langs, $hookmanager;
754 if (empty($this->statuts) || empty($this->statutshort)) {
755 $langs->load(
'orders');
757 $this->statuts[0] =
'StatusSupplierOrderDraft';
758 $this->statuts[1] =
'StatusSupplierOrderValidated';
759 $this->statuts[2] =
'StatusSupplierOrderApproved';
760 if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
761 $this->statuts[3] =
'StatusSupplierOrderOnProcess';
763 $this->statuts[3] =
'StatusSupplierOrderOnProcessWithValidation';
765 $this->statuts[4] =
'StatusSupplierOrderReceivedPartially';
766 $this->statuts[5] =
'StatusSupplierOrderReceivedAll';
767 $this->statuts[6] =
'StatusSupplierOrderCanceled';
768 $this->statuts[7] =
'StatusSupplierOrderCanceled';
769 $this->statuts[9] =
'StatusSupplierOrderRefused';
772 $this->statutshort[0] =
'StatusSupplierOrderDraftShort';
773 $this->statutshort[1] =
'StatusSupplierOrderValidatedShort';
774 $this->statutshort[2] =
'StatusSupplierOrderApprovedShort';
775 $this->statutshort[3] =
'StatusSupplierOrderOnProcessShort';
776 $this->statutshort[4] =
'StatusSupplierOrderReceivedPartiallyShort';
777 $this->statutshort[5] =
'StatusSupplierOrderReceivedAllShort';
778 $this->statutshort[6] =
'StatusSupplierOrderCanceledShort';
779 $this->statutshort[7] =
'StatusSupplierOrderCanceledShort';
780 $this->statutshort[9] =
'StatusSupplierOrderRefusedShort';
783 $statustrans = array(
795 $statusClass =
'status0';
796 if (!empty($statustrans[$status])) {
797 $statusClass = $statustrans[$status];
802 $billedtext =
' - '.$langs->trans(
"Billed");
804 if ($status == 5 && $billed) {
805 $statusClass =
'status6';
808 $statusLong = $langs->transnoentitiesnoconv($this->statuts[$status]).$billedtext;
809 $statusShort = $langs->transnoentitiesnoconv($this->statutshort[$status]);
811 $parameters = array(
'status' => $status,
'mode' => $mode,
'billed' => $billed);
812 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
814 return $hookmanager->resPrint;
817 return dolGetStatus($statusLong, $statusShort,
'', $statusClass, $mode);
831 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
833 global $langs, $conf, $user, $hookmanager;
839 if ($user->rights->fournisseur->commande->lire) {
840 $label =
'<u class="paddingrightonly">'.$langs->trans(
"SupplierOrder").
'</u>';
841 if (isset($this->statut)) {
842 $label .=
' '.$this->getLibStatut(5);
844 if (!empty($this->
ref)) {
845 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
847 if (!empty($this->ref_supplier)) {
848 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
850 if (!empty($this->total_ht)) {
851 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
853 if (!empty($this->total_tva)) {
854 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
856 if (!empty($this->total_ttc)) {
857 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
859 if (!empty($this->date)) {
860 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
862 if (!empty($this->delivery_date)) {
863 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
868 $url = DOL_URL_ROOT.
'/fourn/commande/card.php?id='.$this->id;
870 if ($option !==
'nolink') {
872 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
873 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
874 $add_save_lastsearch_values = 1;
876 if ($add_save_lastsearch_values) {
877 $url .=
'&save_lastsearch_values=1';
882 if (empty($notooltip)) {
883 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
884 $label = $langs->trans(
"ShowOrder");
885 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
887 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
888 $linkclose .=
' class="classfortooltip"';
891 $linkstart =
'<a href="'.$url.
'"';
892 $linkstart .= $linkclose.
'>';
895 $result .= $linkstart;
897 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
899 if ($withpicto != 2) {
900 $result .= $this->ref;
904 if ($addlinktonotes) {
905 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
908 $result .=
' <span class="note inline-block">';
909 $result .=
'<a href="'.DOL_URL_ROOT.
'/fourn/commande/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow).
'">';
914 $result .=
'</span>';
919 $hookmanager->initHooks(array($this->element .
'dao'));
920 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
921 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
923 $result = $hookmanager->resPrint;
925 $result .= $hookmanager->resPrint;
940 global $db, $langs, $conf;
941 $langs->load(
"orders");
943 if (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER)) {
946 $file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.
'.php';
947 $classname = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
950 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
952 foreach ($dirmodels as $reldir) {
953 $dir =
dol_buildpath($reldir.
"core/modules/supplier_order/");
956 $mybool |= @include_once $dir.$file;
959 if ($mybool ===
false) {
964 $obj =
new $classname();
965 $numref = $obj->getNextValue($soc, $this);
970 $this->error = $obj->error;
974 $this->error =
"Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
994 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur SET billed = 1';
997 if ($this->
db->query($sql)) {
1000 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CLASSIFY_BILLED', $user);
1010 $this->
db->commit();
1013 $this->
db->rollback();
1019 $this->
db->rollback();
1032 public function approve($user, $idwarehouse = 0, $secondlevel = 0)
1034 global $langs, $conf;
1035 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1041 if ($user->rights->fournisseur->commande->approuver) {
1048 $soc->fetch($this->fourn_id);
1051 if (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
1059 $movetoapprovestatus =
true;
1062 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1063 $sql .=
" SET ref='".$this->db->escape($num).
"',";
1064 if (empty($secondlevel)) {
1065 $sql .=
" date_approve='".$this->db->idate($now).
"',";
1066 $sql .=
" fk_user_approve = ".$user->id;
1067 if (!empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) {
1068 if (empty($this->user_approve_id2)) {
1069 $movetoapprovestatus =
false;
1070 $comment =
' (first level)';
1075 $sql .=
" date_approve2='".$this->db->idate($now).
"',";
1076 $sql .=
" fk_user_approve2 = ".((int) $user->id);
1077 if (empty($this->user_approve_id)) {
1078 $movetoapprovestatus =
false;
1080 $comment =
' (second level)';
1083 if ($movetoapprovestatus) {
1084 $sql .=
", fk_statut = ".self::STATUS_ACCEPTED;
1086 $sql .=
", fk_statut = ".self::STATUS_VALIDATED;
1088 $sql .=
" WHERE rowid = ".((int) $this->
id);
1089 $sql .=
" AND fk_statut = ".self::STATUS_VALIDATED;
1091 if ($this->
db->query($sql)) {
1092 if (!empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT)) {
1093 $result = $this->
add_contact($user->id,
'SALESREPFOLL',
'internal', 1);
1094 if ($result < 0 && $result != -2) {
1100 if (!$error && $movetoapprovestatus && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
1101 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
1102 $langs->load(
"agenda");
1104 $cpt = count($this->lines);
1105 for ($i = 0; $i < $cpt; $i++) {
1107 if ($this->lines[$i]->fk_product > 0) {
1108 $this->line = $this->lines[$i];
1110 $mouvP->origin = &$this;
1111 $mouvP->setOrigin($this->element, $this->
id);
1113 $up_ht_disc = $this->lines[$i]->subprice;
1114 if (!empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) {
1115 $up_ht_disc =
price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100,
'MU');
1117 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans(
"OrderApprovedInDolibarr", $this->ref));
1128 $result = $this->
call_trigger(
'ORDER_SUPPLIER_APPROVE', $user);
1136 $this->
ref = $this->newref;
1138 if ($movetoapprovestatus) {
1143 if (empty($secondlevel)) {
1144 $this->date_approve = $now;
1145 $this->user_approve_id = $user->id;
1148 $this->date_approve2 = $now;
1149 $this->user_approve_id2 = $user->id;
1152 $this->
db->commit();
1155 $this->
db->rollback();
1159 $this->
db->rollback();
1160 $this->error = $this->
db->lasterror();
1164 dol_syslog(get_class($this).
"::approve Not Authorized", LOG_ERR);
1177 global $conf, $langs;
1183 if ($user->rights->fournisseur->commande->approuver) {
1187 $sql .=
" WHERE rowid = ".((int) $this->
id);
1189 if ($this->
db->query($sql)) {
1194 $result = $this->
call_trigger(
'ORDER_SUPPLIER_REFUSE', $user);
1197 $this->
db->rollback();
1199 $this->
db->commit();
1204 $this->
db->rollback();
1205 $this->error = $this->
db->lasterror();
1206 dol_syslog(get_class($this).
"::refuse Error -1");
1210 dol_syslog(get_class($this).
"::refuse Not Authorized");
1224 public function Cancel($user, $idwarehouse = -1)
1227 global $langs, $conf;
1233 if ($user->rights->fournisseur->commande->commander) {
1238 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur SET fk_statut = ".((int) $statut);
1239 $sql .=
" WHERE rowid = ".((int) $this->
id);
1240 dol_syslog(get_class($this).
"::cancel", LOG_DEBUG);
1241 if ($this->
db->query($sql)) {
1245 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CANCEL', $user);
1252 $this->
db->commit();
1255 $this->
db->rollback();
1259 $this->
db->rollback();
1260 $this->error = $this->
db->lasterror();
1261 dol_syslog(get_class($this).
"::cancel ".$this->error);
1265 dol_syslog(get_class($this).
"::cancel Not Authorized");
1279 public function commande($user, $date, $methode, $comment =
'')
1284 if ($user->rights->fournisseur->commande->commander) {
1287 $newnoteprivate = $this->note_private;
1289 $newnoteprivate =
dol_concatdesc($newnoteprivate, $langs->trans(
"Comment").
': '.$comment);
1292 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1293 $sql .=
" SET fk_statut=".self::STATUS_ORDERSENT.
", fk_input_method=".$methode.
", date_commande='".$this->
db->idate($date).
"', ";
1294 $sql .=
" note_private='".$this->db->escape($newnoteprivate).
"'";
1295 $sql .=
" WHERE rowid=".((int) $this->
id);
1297 dol_syslog(get_class($this).
"::commande", LOG_DEBUG);
1298 if ($this->
db->query($sql)) {
1300 $this->methode_commande_id = $methode;
1301 $this->date_commande = $date;
1302 $this->context = array(
'comments' => $comment);
1305 $result = $this->
call_trigger(
'ORDER_SUPPLIER_SUBMIT', $user);
1312 $this->error = $this->
db->lasterror();
1313 $this->errors[] = $this->
db->lasterror();
1317 $this->
db->commit();
1319 $this->
db->rollback();
1323 $this->error = $langs->trans(
'NotAuthorized');
1324 $this->errors[] = $langs->trans(
'NotAuthorized');
1325 dol_syslog(get_class($this).
"::commande User not Authorized", LOG_WARNING);
1328 return ($error ? -1 : 1);
1338 public function create($user, $notrigger = 0)
1340 global $langs, $conf, $hookmanager;
1348 $date = ($this->date_commande ? $this->date_commande : $this->date);
1352 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1355 if (empty($this->source)) {
1360 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1365 if (empty($this->fk_multicurrency)) {
1366 $this->multicurrency_code = $conf->currency;
1367 $this->fk_multicurrency = 0;
1368 $this->multicurrency_tx = 1;
1372 $this->brouillon = 1;
1374 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur (";
1376 $sql .=
", ref_supplier";
1377 $sql .=
", note_private";
1378 $sql .=
", note_public";
1381 $sql .=
", fk_projet";
1382 $sql .=
", date_creation";
1383 $sql .=
", date_livraison";
1384 $sql .=
", fk_user_author";
1385 $sql .=
", fk_statut";
1387 $sql .=
", model_pdf";
1388 $sql .=
", fk_mode_reglement";
1389 $sql .=
", fk_cond_reglement";
1390 $sql .=
", fk_account";
1391 $sql .=
", fk_incoterms, location_incoterms";
1392 $sql .=
", fk_multicurrency";
1393 $sql .=
", multicurrency_code";
1394 $sql .=
", multicurrency_tx";
1396 $sql .=
" VALUES (";
1398 $sql .=
", '".$this->db->escape($this->ref_supplier).
"'";
1399 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1400 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1401 $sql .=
", ".((int) $conf->entity);
1402 $sql .=
", ".((int) $this->socid);
1403 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
1404 $sql .=
", '".$this->db->idate($date).
"'";
1405 $sql .=
", ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
"null");
1406 $sql .=
", ".((int) $user->id);
1407 $sql .=
", ".self::STATUS_DRAFT;
1408 $sql .=
", ".((int) $this->source);
1409 $sql .=
", '".$this->db->escape($conf->global->COMMANDE_SUPPLIER_ADDON_PDF).
"'";
1410 $sql .=
", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id :
'null');
1411 $sql .=
", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id :
'null');
1412 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
1413 $sql .=
", ".(int) $this->fk_incoterms;
1414 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1415 $sql .=
", ".(int) $this->fk_multicurrency;
1416 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1417 $sql .=
", ".(double) $this->multicurrency_tx;
1420 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1421 if ($this->
db->query($sql)) {
1422 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"commande_fournisseur");
1425 $num = count($this->lines);
1428 for ($i = 0; $i < $num; $i++) {
1429 $line = $this->lines[$i];
1430 if (!is_object($line)) {
1431 $line = (object) $line;
1435 $this->special_code = $line->special_code;
1443 $line->localtax1_tx,
1444 $line->localtax2_tx,
1448 $line->remise_percent,
1451 $line->product_type,
1456 $line->array_options,
1460 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1461 $this->
db->rollback();
1466 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
1467 $sql .=
" SET ref='(PROV".$this->id.
")'";
1468 $sql .=
" WHERE rowid=".((int) $this->
id);
1469 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1470 if ($this->
db->query($sql)) {
1473 $this->
ref =
"(PROV".$this->id.
")";
1475 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1476 $this->linked_objects = $this->linkedObjectsIds;
1480 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1481 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1482 if (is_array($tmp_origin_id)) {
1483 foreach ($tmp_origin_id as $origin_id) {
1492 $origin_id = $tmp_origin_id;
1510 if (!$error && !$notrigger) {
1512 $result = $this->
call_trigger(
'ORDER_SUPPLIER_CREATE', $user);
1514 $this->
db->rollback();
1520 $this->
db->commit();
1523 $this->error = $this->
db->lasterror();
1524 $this->
db->rollback();
1529 $this->error = $this->
db->lasterror();
1530 $this->
db->rollback();
1549 if (isset($this->
ref)) {
1550 $this->
ref = trim($this->
ref);
1552 if (isset($this->ref_supplier)) {
1553 $this->ref_supplier = trim($this->ref_supplier);
1555 if (isset($this->note_private)) {
1556 $this->note_private = trim($this->note_private);
1558 if (isset($this->note_public)) {
1559 $this->note_public = trim($this->note_public);
1561 if (isset($this->model_pdf)) {
1562 $this->model_pdf = trim($this->model_pdf);
1564 if (isset($this->import_key)) {
1565 $this->import_key = trim($this->import_key);
1569 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
1571 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1572 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1573 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1574 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1575 $sql .=
" date_commande=".(strval($this->date_commande) !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
'null').
",";
1576 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
1577 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1578 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
1579 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
1580 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1581 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1582 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1583 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
1584 $sql .=
" fk_user_valid=".(isset($this->user_valid) && $this->user_valid > 0 ? $this->user_valid :
"null").
",";
1585 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1586 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1587 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
1588 $sql .=
" date_livraison=".(strval($this->delivery_date) !=
'' ?
"'".$this->db->idate($this->delivery_date).
"'" :
'null').
",";
1590 $sql .=
" fk_account=".($this->fk_account > 0 ? $this->fk_account :
"null").
",";
1592 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1593 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1594 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1595 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
1597 $sql .=
" WHERE rowid=".((int) $this->
id);
1601 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1605 $this->errors[] =
"Error ".$this->db->lasterror();
1615 if (!$error && !$notrigger) {
1617 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
1626 foreach ($this->errors as $errmsg) {
1627 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1628 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1630 $this->
db->rollback();
1633 $this->
db->commit();
1648 global $conf, $user, $hookmanager;
1655 foreach ($this->lines as $line) {
1656 $line->fetch_optionals();
1660 $objFrom = clone $this;
1663 if (!empty($socid) && $socid != $this->socid) {
1666 if ($objsoc->fetch($socid) > 0) {
1667 $this->socid = $objsoc->id;
1668 $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1669 $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1670 $this->fk_project = 0;
1671 $this->fk_delivery_address = 0;
1681 $this->user_author_id = $user->id;
1682 $this->user_valid = 0;
1683 $this->date_creation =
'';
1684 $this->date_validation =
'';
1685 $this->ref_supplier =
'';
1686 $this->user_approve_id =
'';
1687 $this->user_approve_id2 =
'';
1688 $this->date_approve =
'';
1689 $this->date_approve2 =
'';
1692 $this->context[
'createfromclone'] =
'createfromclone';
1693 $result = $this->
create($user, $notrigger);
1700 if (is_object($hookmanager)) {
1701 $parameters = array(
'objFrom'=>$objFrom);
1703 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1705 $this->errors += $hookmanager->errors;
1706 $this->error = $hookmanager->error;
1712 unset($this->context[
'createfromclone']);
1716 $this->
db->commit();
1719 $this->
db->rollback();
1752 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)
1754 global $langs, $mysoc, $conf;
1756 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");
1757 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1759 if ($this->statut == self::STATUS_DRAFT) {
1760 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1769 if (empty($txtva)) {
1775 if (empty($txlocaltax1)) {
1778 if (empty($txlocaltax2)) {
1781 if (empty($remise_percent)) {
1782 $remise_percent = 0;
1785 $remise_percent =
price2num($remise_percent);
1788 $pu_ht_devise =
price2num($pu_ht_devise);
1790 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1795 if ($price_base_type ==
'HT') {
1800 $desc = trim($desc);
1803 if ($qty < 0 && !$fk_product) {
1804 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Product"));
1810 if ($date_start && $date_end && $date_start > $date_end) {
1811 $langs->load(
"errors");
1812 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1819 $product_type = $type;
1822 if ($fk_product > 0) {
1823 if (!empty($conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY)) {
1825 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);
1827 if ($prod->fetch($fk_product) > 0) {
1828 $product_type = $prod->type;
1829 $label = $prod->label;
1833 $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', (isset($this->fk_soc) ? $this->fk_soc : $this->socid));
1837 if ($result > 0 && ($origin ==
'commande' || $pu ===
'')) {
1838 $pu = $prod->fourn_pu;
1839 $ref_supplier = $prod->ref_supplier;
1841 if ($remise_percent == 0 && $prod->remise_percent != 0) {
1842 $remise_percent = $prod->remise_percent;
1846 $langs->load(
"errors");
1847 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1848 $this->
db->rollback();
1849 dol_syslog(get_class($this).
"::addline we did not found supplier price, so we can't guess unit price");
1854 if ($result == -1) {
1855 $langs->load(
"errors");
1856 $this->error =
"Ref ".$prod->ref.
" ".$langs->trans(
"ErrorQtyTooLowForThisSupplier");
1857 $this->
db->rollback();
1858 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_DEBUG);
1862 $this->error = $prod->error;
1863 $this->
db->rollback();
1864 dol_syslog(get_class($this).
"::addline result=".$result.
" - ".$this->error, LOG_ERR);
1868 $this->error = $prod->error;
1869 $this->
db->rollback();
1875 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
1877 $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product,
'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
1879 if ($qty < $prod->packaging) {
1880 $qty = $prod->packaging;
1882 if (!empty($prod->packaging) && ($qty % $prod->packaging) > 0) {
1883 $coeff = intval($qty / $prod->packaging) + 1;
1884 $qty = $prod->packaging * $coeff;
1885 setEventMessage($langs->trans(
'QtyRecalculatedWithPackaging'),
'mesgs');
1891 if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
1899 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1900 $vat_src_code = $reg[1];
1901 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1909 $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);
1911 $total_ht = $tabprice[0];
1912 $total_tva = $tabprice[1];
1913 $total_ttc = $tabprice[2];
1914 $total_localtax1 = $tabprice[9];
1915 $total_localtax2 = $tabprice[10];
1916 $pu = $pu_ht = $tabprice[3];
1919 $multicurrency_total_ht = $tabprice[16];
1920 $multicurrency_total_tva = $tabprice[17];
1921 $multicurrency_total_ttc = $tabprice[18];
1922 $pu_ht_devise = $tabprice[19];
1924 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
1925 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
1929 $rang = $rangmax + 1;
1935 $this->line->context = $this->context;
1937 $this->line->fk_commande = $this->id;
1938 $this->line->label = $label;
1939 $this->line->ref_fourn = $ref_supplier;
1940 $this->line->ref_supplier = $ref_supplier;
1941 $this->line->desc = $desc;
1942 $this->line->qty = $qty;
1943 $this->line->tva_tx = $txtva;
1944 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
1945 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
1946 $this->line->localtax1_type = $localtax1_type;
1947 $this->line->localtax2_type = $localtax2_type;
1948 $this->line->fk_product = $fk_product;
1949 $this->line->product_type = $product_type;
1950 $this->line->remise_percent = $remise_percent;
1951 $this->line->subprice = $pu_ht;
1952 $this->line->rang = $rang;
1953 $this->line->info_bits = $info_bits;
1955 $this->line->vat_src_code = $vat_src_code;
1956 $this->line->total_ht = $total_ht;
1957 $this->line->total_tva = $total_tva;
1958 $this->line->total_localtax1 = $total_localtax1;
1959 $this->line->total_localtax2 = $total_localtax2;
1960 $this->line->total_ttc = $total_ttc;
1961 $this->line->product_type = $type;
1962 $this->line->special_code = $this->special_code;
1963 $this->line->origin = $origin;
1964 $this->line->origin_id = $origin_id;
1965 $this->line->fk_unit = $fk_unit;
1967 $this->line->date_start = $date_start;
1968 $this->line->date_end = $date_end;
1971 $this->line->fk_multicurrency = $this->fk_multicurrency;
1972 $this->line->multicurrency_code = $this->multicurrency_code;
1973 $this->line->multicurrency_subprice = $pu_ht_devise;
1974 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
1975 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
1976 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
1978 $this->line->subprice = $pu_ht;
1979 $this->line->price = $this->line->subprice;
1981 $this->line->remise_percent = $remise_percent;
1983 if (is_array($array_options) && count($array_options) > 0) {
1984 $this->line->array_options = $array_options;
1987 $result = $this->line->insert($notrigger);
1990 if (!empty($fk_parent_line)) {
1992 } elseif ($rang > 0 && $rang <= count($this->lines)) {
1993 $linecount = count($this->lines);
1994 for ($ii = $rang; $ii <= $linecount; $ii++) {
2000 $result = $this->
update_price(1,
'auto', 0, $this->thirdparty);
2002 $this->
db->commit();
2003 return $this->line->id;
2005 $this->
db->rollback();
2009 $this->error = $this->line->error;
2010 $this->errors = $this->line->errors;
2011 dol_syslog(get_class($this).
"::addline error=".$this->error, LOG_ERR);
2012 $this->
db->rollback();
2035 public function dispatchProduct($user, $product, $qty, $entrepot, $price = 0, $comment =
'', $eatby =
'', $sellby =
'', $batch =
'', $fk_commandefourndet = 0, $notrigger = 0)
2037 global $conf, $langs;
2040 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2043 if ($entrepot <= 0) {
2044 $this->error =
'ErrorBadValueForParameterWarehouse';
2048 $this->error =
'ErrorBadValueForParameterQty';
2052 $dispatchstatus = 1;
2053 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2054 $dispatchstatus = 0;
2061 if (($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY || $this->statut == self::STATUS_RECEIVED_COMPLETELY)) {
2064 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch";
2065 $sql .=
" (fk_commande, fk_product, qty, fk_entrepot, fk_user, datec, fk_commandefourndet, status, comment, eatby, sellby, batch) VALUES";
2066 $sql .=
" ('".$this->id.
"','".$product.
"','".$qty.
"',".($entrepot > 0 ?
"'".$entrepot.
"'" :
"null").
",'".$user->id.
"','".$this->db->idate($now).
"','".$fk_commandefourndet.
"', ".$dispatchstatus.
", '".$this->
db->escape($comment).
"', ";
2067 $sql .= ($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ".($sellby ?
"'".$this->
db->idate($sellby).
"'" :
"null").
", ".($batch ?
"'".$this->
db->escape($batch).
"'" :
"null");
2070 dol_syslog(get_class($this).
"::dispatchProduct", LOG_DEBUG);
2074 global $conf, $langs, $user;
2076 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DISPATCH', $user);
2083 $this->error = $this->
db->lasterror();
2088 if (!$error && $entrepot > 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
2092 $mouv->origin = &$this;
2093 $mouv->setOrigin($this->element, $this->
id);
2096 if (!empty($conf->global->SUPPLIER_ORDER_ALLOW_NEGATIVE_QTY_FOR_SUPPLIER_ORDER_RETURN) && $qty < 0) {
2097 $result = $mouv->livraison($user, $product, $entrepot, $qty*(-1), $price, $comment, $now, $eatby, $sellby, $batch, 0, $inventorycode);
2099 $result = $mouv->reception($user, $product, $entrepot, $qty, $price, $comment, $eatby, $sellby, $batch,
'', 0, $inventorycode);
2103 $this->error = $mouv->error;
2104 $this->errors = $mouv->errors;
2105 dol_syslog(get_class($this).
"::dispatchProduct ".$this->error.
" ".join(
',', $this->errors), LOG_ERR);
2112 $this->
db->commit();
2115 $this->
db->rollback();
2119 $this->error =
'BadStatusForObject';
2133 if ($this->statut == 0) {
2136 if ($line->fetch($idline) <= 0) {
2140 if ($line->delete($notrigger) > 0) {
2144 $this->error = $line->error;
2145 $this->errors = $line->errors;
2160 public function delete(
User $user, $notrigger = 0)
2162 global $langs, $conf;
2163 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2169 if (empty($notrigger)) {
2171 $result = $this->
call_trigger(
'ORDER_SUPPLIER_DELETE', $user);
2173 $this->errors[] =
'ErrorWhenRunningTrigger';
2174 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2175 $this->
db->rollback();
2183 if (!empty($this->linkedObjects) && array_key_exists(
'reception', $this->linkedObjects)) {
2184 foreach ($this->linkedObjects[
'reception'] as $element) {
2185 if ($element->statut >= 0) {
2186 $this->errors[] = $langs->trans(
'ReceptionExist');
2193 $main = MAIN_DB_PREFIX.
'commande_fournisseurdet';
2194 $ef = $main.
"_extrafields";
2195 $sql =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".((int) $this->
id).
")";
2196 dol_syslog(get_class($this).
"::delete extrafields lines", LOG_DEBUG);
2197 if (!$this->
db->query($sql)) {
2198 $this->error = $this->
db->lasterror();
2199 $this->errors[] = $this->
db->lasterror();
2203 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE fk_commande =".((int) $this->
id);
2204 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2205 if (!$this->
db->query($sql)) {
2206 $this->error = $this->
db->lasterror();
2207 $this->errors[] = $this->
db->lasterror();
2211 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"commande_fournisseur WHERE rowid =".((int) $this->
id);
2212 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
2213 if (
$resql = $this->
db->query($sql)) {
2214 if ($this->
db->affected_rows(
$resql) < 1) {
2215 $this->error = $this->
db->lasterror();
2216 $this->errors[] = $this->
db->lasterror();
2220 $this->error = $this->
db->lasterror();
2221 $this->errors[] = $this->
db->lasterror();
2229 $this->error =
'FailToDeleteExtraFields';
2230 $this->errors[] =
'FailToDeleteExtraFields';
2232 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
2239 $this->error =
'FailToDeleteObjectLinked';
2240 $this->errors[] =
'FailToDeleteObjectLinked';
2250 if ($conf->fournisseur->commande->dir_output) {
2251 $dir = $conf->fournisseur->commande->dir_output.
"/".$ref;
2252 $file = $dir.
"/".$ref.
".pdf";
2253 if (file_exists($file)) {
2255 $this->error =
'ErrorFailToDeleteFile';
2256 $this->errors[] =
'ErrorFailToDeleteFile';
2260 if (file_exists($dir)) {
2263 $this->error =
'ErrorFailToDeleteDir';
2264 $this->errors[] =
'ErrorFailToDeleteDir';
2272 dol_syslog(get_class($this).
"::delete $this->id by $user->id", LOG_DEBUG);
2273 $this->
db->commit();
2276 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
2277 $this->
db->rollback();
2291 $sql =
"SELECT rowid, libelle";
2292 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_input_method";
2293 $sql .=
" WHERE active = 1";
2298 $num = $this->
db->num_rows(
$resql);
2299 $this->methodes_commande = array();
2301 $row = $this->
db->fetch_row(
$resql);
2303 $this->methodes_commande[$row[0]] = $row[1];
2326 $sql =
"SELECT p.ref, p.label,";
2327 $sql .=
" e.rowid as warehouse_id, e.ref as entrepot,";
2328 $sql .=
" cfd.rowid as dispatchedlineid, cfd.fk_product, cfd.qty, cfd.eatby, cfd.sellby, cfd.batch, cfd.comment, cfd.status";
2329 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product as p,";
2330 $sql .=
" ".MAIN_DB_PREFIX.
"commande_fournisseur_dispatch as cfd";
2331 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot as e ON cfd.fk_entrepot = e.rowid";
2332 $sql .=
" WHERE cfd.fk_commande = ".((int) $this->
id);
2333 $sql .=
" AND cfd.fk_product = p.rowid";
2335 $sql .=
" AND cfd.status = ".((int) $status);
2337 $sql .=
" ORDER BY cfd.rowid ASC";
2341 $num = $this->
db->num_rows(
$resql);
2345 $objp = $this->
db->fetch_object(
$resql);
2348 'id' => $objp->dispatchedlineid,
2349 'productid' => $objp->fk_product,
2350 'warehouseid' => $objp->warehouse_id,
2351 'qty' => $objp->qty,
2358 dol_print_error($this->
db,
'Failed to execute request to get dispatched lines');
2377 global $conf, $langs;
2384 $usercanreceive = 0;
2385 if (empty($conf->reception->enabled)) {
2386 $usercanreceive = $user->rights->fournisseur->commande->receptionner;
2388 $usercanreceive = $user->rights->reception->creer;
2391 if ($usercanreceive) {
2393 if ($type ==
'par') {
2395 } elseif ($type ==
'tot') {
2397 } elseif ($type ==
'nev') {
2399 } elseif ($type ==
'can') {
2403 dol_syslog(get_class($this).
"::Livraison Error -2", LOG_ERR);
2408 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
2410 if (!$error && ($type ==
'tot')) {
2412 if (count($dispatchedlinearray) > 0) {
2415 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove';
2416 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionToApprove', LOG_DEBUG);
2419 if (!$error && !empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS_NEED_APPROVE) && ($type ==
'tot')) {
2421 if (count($dispatchedlinearray) > 0) {
2424 $this->errors[] =
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied';
2425 dol_syslog(
'ErrorCantSetReceptionToTotalDoneWithReceptionDenied', LOG_DEBUG);
2432 if (empty($error)) {
2435 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2436 $sql .=
" SET fk_statut = ".((int) $statut);
2437 $sql .=
" WHERE rowid = ".((int) $this->
id);
2438 $sql .=
" AND fk_statut IN (".self::STATUS_ORDERSENT.
",".self::STATUS_RECEIVED_PARTIALLY.
")";
2440 dol_syslog(get_class($this).
"::Livraison", LOG_DEBUG);
2444 $old_statut = $this->statut;
2445 $this->statut = $statut;
2446 $this->actionmsg2 = $comment;
2449 $result_trigger = $this->
call_trigger(
'ORDER_SUPPLIER_RECEIVE', $user);
2450 if ($result_trigger < 0) {
2455 if (empty($error)) {
2456 $this->
db->commit();
2458 $this->statut = $old_statut;
2459 $this->
db->rollback();
2460 $this->error = $this->
db->lasterror();
2464 $this->
db->rollback();
2465 $this->error = $this->
db->lasterror();
2470 $this->error = $langs->trans(
'NotAuthorized');
2471 $this->errors[] = $langs->trans(
'NotAuthorized');
2472 dol_syslog(get_class($this).
"::Livraison Not Authorized");
2504 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2509 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2510 $sql .=
" SET date_livraison = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2511 $sql .=
" WHERE rowid = ".((int) $this->
id);
2516 $this->errors[] = $this->
db->error();
2521 $this->oldcopy = clone $this;
2522 $this->date_livraison = $delivery_date;
2523 $this->delivery_date = $delivery_date;
2526 if (!$notrigger && empty($error)) {
2528 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2536 $this->
db->commit();
2539 foreach ($this->errors as $errmsg) {
2540 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2541 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2543 $this->
db->rollback();
2563 if ($user->rights->fournisseur->commande->creer || $user->rights->supplier_order->creer) {
2568 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commande_fournisseur";
2569 $sql .=
" SET fk_projet = ".($id_projet > 0 ? (int) $id_projet :
'null');
2570 $sql .=
" WHERE rowid = ".((int) $this->
id);
2575 $this->errors[] = $this->
db->error();
2580 $this->oldcopy = clone $this;
2581 $this->fk_projet = $id_projet;
2582 $this->fk_project = $id_projet;
2585 if (!$notrigger && empty($error)) {
2587 $result = $this->
call_trigger(
'ORDER_SUPPLIER_MODIFY', $user);
2595 $this->
db->commit();
2598 foreach ($this->errors as $errmsg) {
2599 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2600 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2602 $this->
db->rollback();
2621 $comclient->fetch($comclientid);
2625 $this->lines = array();
2627 $num = count($comclient->lines);
2628 for ($i = 0; $i < $num; $i++) {
2632 if ($prod->fetch($comclient->lines[$i]->fk_product) > 0) {
2633 $label = $prod->label;
2637 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"commande_fournisseurdet";
2638 $sql .=
" (fk_commande, label, description, fk_product, price, qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref)";
2639 $sql .=
" VALUES (".((int) $idc).
", '".$this->
db->escape($label).
"', '".$this->
db->escape($comclient->lines[$i]->desc).
"'";
2640 $sql .=
",".$comclient->lines[$i]->fk_product.
", ".
price2num($comclient->lines[$i]->price,
'MU');
2641 $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);
2642 $sql .=
", '".price2num($comclient->lines[$i]->subprice,
'MT').
"','0', '".$this->
db->escape($ref).
"');";
2643 if ($this->
db->query($sql)) {
2660 global $conf, $langs;
2665 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'commande_fournisseur';
2666 $sql .=
" SET fk_statut = ".$status;
2667 $sql .=
" WHERE rowid = ".((int) $this->
id);
2669 dol_syslog(get_class($this).
"::setStatus", LOG_DEBUG);
2673 $triggerName = array();
2674 $triggerName[0] =
'DRAFT';
2675 $triggerName[1] =
'VALIDATED';
2676 $triggerName[2] =
'APPROVED';
2677 $triggerName[3] =
'ORDERED';
2678 $triggerName[4] =
'RECEIVED_PARTIALLY';
2679 $triggerName[5] =
'RECEIVED_COMPLETELY';
2680 $triggerName[6] =
'CANCELED';
2681 $triggerName[7] =
'CANCELED';
2682 $triggerName[9] =
'REFUSED';
2685 $result = $this->
call_trigger(
"ORDER_SUPPLIER_STATUS_".$triggerName[$status], $user);
2692 $this->error = $this->
db->lasterror();
2693 dol_syslog(get_class($this).
"::setStatus ".$this->error);
2697 $this->statut = $status;
2698 $this->
db->commit();
2701 $this->
db->rollback();
2729 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 =
'')
2731 global $mysoc, $conf, $langs;
2732 dol_syslog(get_class($this).
"::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
2733 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2737 if ($this->brouillon) {
2742 if (empty($info_bits)) {
2745 if (empty($txtva)) {
2748 if (empty($txlocaltax1)) {
2751 if (empty($txlocaltax2)) {
2754 if (empty($remise)) {
2757 if (empty($remise_percent)) {
2758 $remise_percent = 0;
2761 $remise_percent =
price2num($remise_percent);
2767 $pu_ht_devise =
price2num($pu_ht_devise);
2768 if (!preg_match(
'/\((.*)\)/', $txtva)) {
2778 if ($date_start && $date_end && $date_start > $date_end) {
2779 $langs->load(
"errors");
2780 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
2796 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
2797 $vat_src_code = $reg[1];
2798 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
2801 $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);
2802 $total_ht = $tabprice[0];
2803 $total_tva = $tabprice[1];
2804 $total_ttc = $tabprice[2];
2805 $total_localtax1 = $tabprice[9];
2806 $total_localtax2 = $tabprice[10];
2807 $pu_ht = $tabprice[3];
2808 $pu_tva = $tabprice[4];
2809 $pu_ttc = $tabprice[5];
2812 $multicurrency_total_ht = $tabprice[16];
2813 $multicurrency_total_tva = $tabprice[17];
2814 $multicurrency_total_ttc = $tabprice[18];
2815 $pu_ht_devise = $tabprice[19];
2817 $localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2818 $localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2822 $this->line->fetch($rowid);
2824 $oldline = clone $this->line;
2825 $this->line->oldline = $oldline;
2827 $this->line->context = $this->context;
2829 $this->line->fk_commande = $this->id;
2831 $this->line->desc = $desc;
2834 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2835 if ($qty < $this->line->packaging) {
2836 $qty = $this->line->packaging;
2838 if (! empty($this->line->packaging) && ($qty % $this->line->packaging) > 0) {
2839 $coeff = intval($qty / $this->line->packaging) + 1;
2840 $qty = $this->line->packaging * $coeff;
2841 setEventMessage($langs->trans(
'QtyRecalculatedWithPackaging'),
'mesgs');
2846 $this->line->qty = $qty;
2847 $this->line->ref_supplier = $ref_supplier;
2849 $this->line->vat_src_code = $vat_src_code;
2850 $this->line->tva_tx = $txtva;
2851 $this->line->localtax1_tx = $txlocaltax1;
2852 $this->line->localtax2_tx = $txlocaltax2;
2853 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
2854 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
2855 $this->line->remise_percent = $remise_percent;
2856 $this->line->subprice = $pu_ht;
2857 $this->line->rang = $this->rang;
2858 $this->line->info_bits = $info_bits;
2859 $this->line->total_ht = $total_ht;
2860 $this->line->total_tva = $total_tva;
2861 $this->line->total_localtax1 = $total_localtax1;
2862 $this->line->total_localtax2 = $total_localtax2;
2863 $this->line->total_ttc = $total_ttc;
2864 $this->line->product_type = $type;
2865 $this->line->special_code = $this->special_code;
2866 $this->line->origin = $this->origin;
2867 $this->line->fk_unit = $fk_unit;
2869 $this->line->date_start = $date_start;
2870 $this->line->date_end = $date_end;
2873 $this->line->fk_multicurrency = $this->fk_multicurrency;
2874 $this->line->multicurrency_code = $this->multicurrency_code;
2875 $this->line->multicurrency_subprice = $pu_ht_devise;
2876 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
2877 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
2878 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
2880 $this->line->subprice = $pu_ht;
2881 $this->line->price = $this->line->subprice;
2883 $this->line->remise_percent = $remise_percent;
2885 if (is_array($array_options) && count($array_options) > 0) {
2887 foreach ($array_options as $key => $value) {
2888 $this->line->array_options[$key] = $array_options[$key];
2892 $result = $this->line->update($notrigger);
2898 $this->
db->commit();
2901 $this->error = $this->
db->lasterror();
2902 $this->
db->rollback();
2906 $this->error =
"Order status makes operation forbidden";
2907 dol_syslog(get_class($this).
"::updateline ".$this->error, LOG_ERR);
2922 global $user, $langs, $conf;
2924 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
2926 dol_syslog(get_class($this).
"::initAsSpecimen");
2933 $sql =
"SELECT rowid";
2934 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2935 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2936 $sql .= $this->
db->order(
"rowid",
"ASC");
2937 $sql .= $this->
db->plimit(1);
2940 $obj = $this->
db->fetch_object(
$resql);
2941 $prodid = $obj->rowid;
2946 $this->
ref =
'SPECIMEN';
2947 $this->specimen = 1;
2950 $this->date_commande = $now;
2951 $this->date_lim_reglement = $this->date + 3600 * 24 * 30;
2952 $this->cond_reglement_code =
'RECEP';
2953 $this->mode_reglement_code =
'CHQ';
2955 $this->note_public =
'This is a comment (public)';
2956 $this->note_private =
'This is a comment (private)';
2958 $this->multicurrency_tx = 1;
2959 $this->multicurrency_code = $conf->currency;
2966 while ($xnbp < $nbp) {
2968 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2970 $line->subprice = 100;
2972 $line->tva_tx = 19.6;
2973 $line->localtax1_tx = 0;
2974 $line->localtax2_tx = 0;
2976 $line->total_ht = 50;
2977 $line->total_ttc = 59.8;
2978 $line->total_tva = 9.8;
2979 $line->remise_percent = 50;
2981 $line->total_ht = 100;
2982 $line->total_ttc = 119.6;
2983 $line->total_tva = 19.6;
2984 $line->remise_percent = 00;
2986 $line->fk_product = $prodid;
2988 $this->lines[$xnbp] = $line;
2990 $this->total_ht += $line->total_ht;
2991 $this->total_tva += $line->total_tva;
2992 $this->total_ttc += $line->total_ttc;
3006 $sql =
'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
3007 $sql .=
' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
3008 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c';
3009 $sql .=
' WHERE c.rowid = '.((int) $id);
3011 $result = $this->
db->query($sql);
3013 if ($this->
db->num_rows($result)) {
3014 $obj = $this->
db->fetch_object($result);
3015 $this->
id = $obj->rowid;
3016 if ($obj->fk_user_author) {
3017 $this->user_creation_id = $obj->fk_user_author;
3019 if ($obj->fk_user_valid) {
3020 $this->user_validation_id = $obj->fk_user_valid;
3022 if ($obj->fk_user_modif) {
3023 $this->user_modification_id = $obj->fk_user_modif;
3025 if ($obj->fk_user_approve) {
3026 $this->user_approve_id = $obj->fk_user_approve;
3028 if ($obj->fk_user_approve2) {
3029 $this->user_approve_id2 = $obj->fk_user_approve2;
3032 $this->date_creation = $this->
db->jdate($obj->datec);
3033 $this->date_modification = $this->
db->jdate($obj->datem);
3034 $this->date_approve = $this->
db->jdate($obj->datea);
3035 $this->date_approve2 = $this->
db->jdate($obj->datea2);
3036 $this->date_validation = $this->
db->jdate($obj->date_validation);
3038 $this->
db->free($result);
3053 global $conf, $user;
3055 $this->nb = array();
3058 $sql =
"SELECT count(co.rowid) as nb";
3059 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as co";
3060 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON co.fk_soc = s.rowid";
3061 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3062 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3063 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3066 $sql .=
" ".$clause.
" co.entity IN (".
getEntity(
'supplier_order').
")";
3070 while ($obj = $this->
db->fetch_object(
$resql)) {
3071 $this->nb[
"supplier_orders"] = $obj->nb;
3077 $this->error = $this->
db->error();
3093 global $conf, $langs;
3095 $sql =
"SELECT c.rowid, c.date_creation as datec, c.date_commande, c.fk_statut, c.date_livraison as delivery_date, c.total_ht";
3096 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as c";
3097 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3098 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3100 $sql .=
" WHERE c.entity = ".$conf->entity;
3101 if ($mode ===
'awaiting') {
3102 $sql .=
" AND c.fk_statut IN (".self::STATUS_ORDERSENT.
", ".self::STATUS_RECEIVED_PARTIALLY.
")";
3104 $sql .=
" AND c.fk_statut IN (".self::STATUS_VALIDATED.
", ".self::STATUS_ACCEPTED.
")";
3107 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
3115 $response->warning_delay = $conf->commande->fournisseur->warning_delay / 60 / 60 / 24;
3116 $response->label = $langs->trans(
"SuppliersOrdersToProcess");
3117 $response->labelShort = $langs->trans(
"Opened");
3118 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=1,2&mainmenu=commercial&leftmenu=orders_suppliers';
3121 if ($mode ===
'awaiting') {
3122 $response->label = $langs->trans(
"SuppliersOrdersAwaitingReception");
3123 $response->labelShort = $langs->trans(
"AwaitingReception");
3124 $response->url = DOL_URL_ROOT.
'/fourn/commande/list.php?search_status=3,4&mainmenu=commercial&leftmenu=orders_suppliers';
3127 while ($obj = $this->
db->fetch_object(
$resql)) {
3128 $commandestatic->delivery_date = $this->
db->jdate($obj->delivery_date);
3129 $commandestatic->date_commande = $this->
db->jdate($obj->date_commande);
3130 $commandestatic->statut = $obj->fk_statut;
3132 $response->nbtodo++;
3133 $response->total += $obj->total_ht;
3135 if ($commandestatic->hasDelay()) {
3136 $response->nbtodolate++;
3142 $this->error = $this->
db->error();
3157 if ($this->methode_commande_id > 0) {
3158 $sql =
"SELECT rowid, code, libelle as label";
3159 $sql .=
" FROM ".MAIN_DB_PREFIX.
'c_input_method';
3160 $sql .=
" WHERE active=1 AND rowid = ".((int) $this->methode_commande_id);
3165 $obj = $this->
db->fetch_object(
$resql);
3167 $string = $langs->trans($obj->code);
3168 if ($string == $obj->code) {
3169 $string = $obj->label !=
'-' ? $obj->label :
'';
3192 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3194 global $conf, $langs;
3196 $langs->load(
"suppliers");
3197 $outputlangs->load(
"products");
3201 if ($this->model_pdf) {
3202 $modele = $this->model_pdf;
3203 } elseif (!empty($conf->global->COMMANDE_SUPPLIER_ADDON_PDF)) {
3204 $modele = $conf->global->COMMANDE_SUPPLIER_ADDON_PDF;
3207 if (empty($modele)) {
3210 $modelpath =
"core/modules/supplier_order/doc/";
3211 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3223 if (empty($this->lines)) {
3230 foreach ($this->lines as $line) {
3231 if ($line->fk_product > 0) {
3232 $idp = $obj->find_min_price_product_fournisseur($line->fk_product, $line->qty);
3235 if ($obj->delivery_time_days > $nb) {
3236 $nb = $obj->delivery_time_days;
3245 return $nb.
' '.$langs->trans(
'Days');
3257 return $user->rights->fournisseur->commande;
3272 'commande_fournisseur'
3289 'commande_fournisseurdet'
3306 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3307 $this->delivery_date = $this->date_livraison;
3310 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3312 if (!empty($this->delivery_date)) {
3313 $date_to_test = $this->delivery_date;
3314 return $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3322 $date_to_test = $this->date_commande;
3324 return ($this->statut > 0 && $this->statut < 5) && $date_to_test && $date_to_test < ($now - $conf->commande->fournisseur->warning_delay);
3337 global $conf, $langs;
3339 if (empty($this->delivery_date) && !empty($this->date_livraison)) {
3340 $this->delivery_date = $this->date_livraison;
3345 if ($this->statut == self::STATUS_ORDERSENT || $this->statut == self::STATUS_RECEIVED_PARTIALLY) {
3346 if (!empty($this->delivery_date)) {
3347 $text = $langs->trans(
"DeliveryDate").
' '.
dol_print_date($this->delivery_date,
'day');
3349 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3352 $text = $langs->trans(
"OrderDate").
' '.
dol_print_date($this->date_commande,
'day');
3355 $text .=
' '.($conf->commande->fournisseur->warning_delay > 0 ?
'+' :
'-').
' '.round(abs($conf->commande->fournisseur->warning_delay) / 3600 / 24, 1).
' '.$langs->trans(
"days").
' < '.$langs->trans(
"Today");
3372 global $conf, $langs;
3374 if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled)) {
3375 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.dispatch.class.php';
3377 $qtydelivered = array();
3378 $qtywished = array();
3381 $filter = array(
't.fk_commande'=>$this->
id);
3382 if (!empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) {
3383 $filter[
't.status'] = 1;
3386 $ret = $supplierorderdispatch->fetchAll(
'',
'', 0, 0, $filter);
3388 $this->error = $supplierorderdispatch->error; $this->errors = $supplierorderdispatch->errors;
3391 if (is_array($supplierorderdispatch->lines) && count($supplierorderdispatch->lines) > 0) {
3392 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3396 foreach ($supplierorderdispatch->lines as $line) {
3397 $qtydelivered[$line->fk_product] += $line->qty;
3399 foreach ($this->lines as $line) {
3401 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $line->product_type > 0) {
3404 $qtywished[$line->fk_product] += $line->qty;
3408 $diff_array = array_diff_assoc($qtydelivered, $qtywished);
3409 $keysinwishednotindelivered = array_diff(array_keys($qtywished), array_keys($qtydelivered));
3410 $keysindeliverednotinwished = array_diff(array_keys($qtydelivered), array_keys($qtywished));
3418 if (count($diff_array) == 0 && count($keysinwishednotindelivered) == 0 && count($keysindeliverednotinwished) == 0) {
3419 if ($closeopenorder) {
3421 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3429 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3435 } elseif (!empty($conf->global->SUPPLIER_ORDER_MORE_THAN_WISHED)) {
3440 if (count($diff_array) > 0) {
3444 foreach ($diff_array as $key => $value) {
3446 if ($qtydelivered[$key] >= $qtywished[$key]) {
3453 if ($close == count($diff_array)) {
3455 if ($closeopenorder) {
3456 $ret = $this->
Livraison($user, $date_liv,
'tot', $comment);
3463 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3471 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3479 $ret = $this->
Livraison($user, $date_liv,
'par', $comment);
3501 $this->receptions = array();
3503 dol_syslog(get_class($this).
"::loadReceptions", LOG_DEBUG);
3505 $sql =
'SELECT cd.rowid, cd.fk_product,';
3506 $sql .=
' sum(cfd.qty) as qty';
3507 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur_dispatch as cfd,';
3508 if ($filtre_statut >= 0) {
3509 $sql .=
' '.MAIN_DB_PREFIX.
'reception as e,';
3511 $sql .=
' '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3513 if ($filtre_statut >= 0) {
3514 $sql .=
' cfd.fk_reception = e.rowid AND';
3516 $sql .=
' cfd.fk_commandefourndet = cd.rowid';
3517 $sql .=
' AND cd.fk_commande ='.((int) $this->
id);
3518 if ($this->fk_product > 0) {
3519 $sql .=
' AND cd.fk_product = '.((int) $this->fk_product);
3521 if ($filtre_statut >= 0) {
3522 $sql .=
' AND e.fk_statut >= '.((int) $filtre_statut);
3524 $sql .=
' GROUP BY cd.rowid, cd.fk_product';
3528 $num = $this->
db->num_rows(
$resql);
3531 $obj = $this->
db->fetch_object(
$resql);
3532 empty($this->receptions[$obj->rowid]) ? $this->receptions[$obj->rowid] = $obj->qty : $this->receptions[$obj->rowid] += $obj->qty;
3539 $this->error = $this->
db->lasterror();
3555 public $element =
'commande_fournisseurdet';
3560 public $table_element =
'commande_fournisseurdet';
3568 public $fk_commande;
3574 public $fk_parent_line;
3582 public $special_code = 0;
3599 public $ref_supplier;
3623 $sql =
'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
3624 $sql .=
' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
3625 $sql .=
' cd.remise, cd.remise_percent, cd.subprice,';
3626 $sql .=
' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
3627 $sql .=
' cd.total_localtax1, cd.total_localtax2,';
3628 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
3629 $sql .=
' cd.date_start, cd.date_end, cd.fk_unit,';
3630 $sql .=
' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
3631 $sql .=
' c.fk_soc as socid';
3632 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commande_fournisseur as c, '.MAIN_DB_PREFIX.
'commande_fournisseurdet as cd';
3633 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
3634 $sql .=
' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
3636 $result = $this->
db->query($sql);
3638 $objp = $this->
db->fetch_object($result);
3640 if (!empty($objp)) {
3641 $this->
rowid = $objp->rowid;
3642 $this->
id = $objp->rowid;
3643 $this->fk_commande = $objp->fk_commande;
3644 $this->desc = $objp->description;
3645 $this->qty = $objp->qty;
3646 $this->ref_fourn = $objp->ref_supplier;
3647 $this->ref_supplier = $objp->ref_supplier;
3648 $this->subprice = $objp->subprice;
3649 $this->tva_tx = $objp->tva_tx;
3650 $this->localtax1_tx = $objp->localtax1_tx;
3651 $this->localtax2_tx = $objp->localtax2_tx;
3652 $this->localtax1_type = $objp->localtax1_type;
3653 $this->localtax2_type = $objp->localtax2_type;
3654 $this->remise = $objp->remise;
3655 $this->remise_percent = $objp->remise_percent;
3656 $this->fk_product = $objp->fk_product;
3657 $this->info_bits = $objp->info_bits;
3658 $this->total_ht = $objp->total_ht;
3659 $this->total_tva = $objp->total_tva;
3660 $this->total_localtax1 = $objp->total_localtax1;
3661 $this->total_localtax2 = $objp->total_localtax2;
3662 $this->total_ttc = $objp->total_ttc;
3663 $this->product_type = $objp->product_type;
3664 $this->special_code = $objp->special_code;
3666 $this->
ref = $objp->product_ref;
3668 $this->product_ref = $objp->product_ref;
3669 $this->product_label = $objp->product_label;
3670 $this->product_desc = $objp->product_desc;
3672 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
3677 $sqlsearchpackage =
'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX.
"product_fournisseur_price";
3678 $sqlsearchpackage .=
' WHERE entity IN ('.getEntity(
'product_fournisseur_price').
")";
3679 $sqlsearchpackage .=
" AND fk_product = ".((int) $objp->fk_product);
3680 $sqlsearchpackage .=
" AND ref_fourn = '".$this->db->escape($objp->ref_supplier).
"'";
3681 $sqlsearchpackage .=
" AND quantity <= ".((float) $objp->qty);
3682 $sqlsearchpackage .=
" AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).
")";
3683 $sqlsearchpackage .=
" AND fk_soc = ".((int) $objp->socid);
3684 $sqlsearchpackage .=
" ORDER BY packaging ASC";
3685 $sqlsearchpackage .=
" LIMIT 1";
3687 $resqlsearchpackage = $this->
db->query($sqlsearchpackage);
3688 if ($resqlsearchpackage) {
3689 $objsearchpackage = $this->
db->fetch_object($resqlsearchpackage);
3690 if ($objsearchpackage) {
3691 $this->fk_fournprice = $objsearchpackage->rowid;
3692 $this->packaging = $objsearchpackage->packaging;
3695 $this->error = $this->
db->lasterror();
3700 $this->date_start = $this->
db->jdate($objp->date_start);
3701 $this->date_end = $this->
db->jdate($objp->date_end);
3702 $this->fk_unit = $objp->fk_unit;
3704 $this->multicurrency_subprice = $objp->multicurrency_subprice;
3705 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
3706 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
3707 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
3711 $this->
db->free($result);
3714 $this->error =
'Supplier order line with id='.$rowid.
' not found';
3715 dol_syslog(get_class($this).
"::fetch Error ".$this->error, LOG_ERR);
3732 global $conf, $user;
3736 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
3739 if (empty($this->tva_tx)) {
3742 if (empty($this->localtax1_tx)) {
3743 $this->localtax1_tx = 0;
3745 if (empty($this->localtax2_tx)) {
3746 $this->localtax2_tx = 0;
3748 if (empty($this->localtax1_type)) {
3749 $this->localtax1_type =
'0';
3751 if (empty($this->localtax2_type)) {
3752 $this->localtax2_type =
'0';
3754 if (empty($this->total_localtax1)) {
3755 $this->total_localtax1 = 0;
3757 if (empty($this->total_localtax2)) {
3758 $this->total_localtax2 = 0;
3760 if (empty($this->rang)) {
3763 if (empty($this->remise_percent)) {
3764 $this->remise_percent = 0;
3766 if (empty($this->info_bits)) {
3767 $this->info_bits = 0;
3769 if (empty($this->special_code)) {
3770 $this->special_code = 0;
3772 if (empty($this->fk_parent_line)) {
3773 $this->fk_parent_line = 0;
3775 if (empty($this->pa_ht)) {
3780 if (!empty($this->multicurrency_code)) {
3783 if (empty($this->fk_multicurrency)) {
3784 $this->multicurrency_code = $conf->currency;
3785 $this->fk_multicurrency = 0;
3786 $this->multicurrency_tx = 1;
3790 if ($this->product_type < 0) {
3797 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
3798 $sql .=
" (fk_commande, label, description, date_start, date_end,";
3799 $sql .=
" fk_product, product_type, special_code, rang,";
3800 $sql .=
" qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
3801 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
3802 $sql .=
" fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
3804 $sql .=
" VALUES (".$this->fk_commande.
", '".$this->
db->escape($this->label).
"','".$this->
db->escape($this->desc).
"',";
3805 $sql .=
" ".($this->date_start ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3806 $sql .=
" ".($this->date_end ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3807 if ($this->fk_product) {
3808 $sql .= $this->fk_product.
",";
3812 $sql .=
"'".$this->db->escape($this->product_type).
"',";
3813 $sql .=
"'".$this->db->escape($this->special_code).
"',";
3814 $sql .=
"'".$this->db->escape($this->rang).
"',";
3815 $sql .=
"'".$this->db->escape($this->qty).
"', ";
3816 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
3817 $sql .=
" ".price2num($this->tva_tx).
", ";
3818 $sql .=
" ".price2num($this->localtax1_tx).
",";
3819 $sql .=
" ".price2num($this->localtax2_tx).
",";
3820 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3821 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3822 $sql .=
" ".((float) $this->remise_percent).
", ".
price2num($this->subprice,
'MU').
", '".$this->
db->escape($this->ref_supplier).
"',";
3823 $sql .=
" ".price2num($this->total_ht).
",";
3824 $sql .=
" ".price2num($this->total_tva).
",";
3825 $sql .=
" ".price2num($this->total_localtax1).
",";
3826 $sql .=
" ".price2num($this->total_localtax2).
",";
3827 $sql .=
" ".price2num($this->total_ttc).
",";
3828 $sql .= ($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
3829 $sql .=
", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) :
"null");
3830 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
3831 $sql .=
", ".($this->multicurrency_subprice ?
price2num($this->multicurrency_subprice) :
'0');
3832 $sql .=
", ".($this->multicurrency_total_ht ?
price2num($this->multicurrency_total_ht) :
'0');
3833 $sql .=
", ".($this->multicurrency_total_tva ?
price2num($this->multicurrency_total_tva) :
'0');
3834 $sql .=
", ".($this->multicurrency_total_ttc ?
price2num($this->multicurrency_total_ttc) :
'0');
3837 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3840 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
3841 $this->
rowid = $this->id;
3850 if (!$error && !$notrigger) {
3852 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_CREATE', $user);
3860 $this->
db->commit();
3864 foreach ($this->errors as $errmsg) {
3865 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
3866 $this->errors[] = ($this->errors ?
', '.$errmsg : $errmsg);
3868 $this->
db->rollback();
3871 $this->errors[] = $this->
db->error();
3872 $this->
db->rollback();
3884 global $conf, $user;
3891 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3892 $sql .=
" description='".$this->db->escape($this->desc).
"'";
3893 $sql .=
", ref='".$this->db->escape($this->ref_supplier).
"'";
3894 $sql .=
", subprice='".price2num($this->subprice).
"'";
3896 $sql .=
", remise_percent='".price2num($this->remise_percent).
"'";
3898 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
3899 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
3900 $sql .=
", localtax1_tx='".price2num($this->localtax1_tx).
"'";
3901 $sql .=
", localtax2_tx='".price2num($this->localtax2_tx).
"'";
3902 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
3903 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
3904 $sql .=
", qty='".price2num($this->qty).
"'";
3905 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3906 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3907 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
3908 $sql .=
", total_ht='".price2num($this->total_ht).
"'";
3909 $sql .=
", total_tva='".price2num($this->total_tva).
"'";
3910 $sql .=
", total_localtax1='".price2num($this->total_localtax1).
"'";
3911 $sql .=
", total_localtax2='".price2num($this->total_localtax2).
"'";
3912 $sql .=
", total_ttc='".price2num($this->total_ttc).
"'";
3913 $sql .=
", product_type=".$this->product_type;
3914 $sql .=
", special_code=".(!empty($this->special_code) ? $this->special_code : 0);
3915 $sql .= ($this->fk_unit ?
", fk_unit='".$this->db->escape($this->fk_unit).
"'" :
", fk_unit=null");
3918 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
3919 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
3920 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
3921 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
3923 $sql .=
" WHERE rowid = ".((int) $this->
id);
3925 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
3926 $result = $this->
db->query($sql);
3935 if (!$error && !$notrigger) {
3938 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_MODIFY', $user);
3940 $this->
db->rollback();
3947 $this->
db->commit();
3950 $this->
db->rollback();
3954 $this->error = $this->
db->lasterror();
3955 $this->
db->rollback();
3966 public function delete($notrigger = 0)
3977 $this->
db->rollback();
3981 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"commande_fournisseurdet WHERE rowid=".((int) $this->
id);
3988 $result = $this->
call_trigger(
'LINEORDER_SUPPLIER_DELETE', $user);
3996 $this->
db->commit();
4000 foreach ($this->errors as $errmsg) {
4001 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
4002 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4004 $this->
db->rollback();
4007 $this->error = $this->
db->lasterror();