37 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
38 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobjectline.class.php";
39 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonincoterm.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
44 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
46 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expeditionlinebatch.class.php';
59 public $element =
"shipping";
64 public $fk_element =
"fk_expedition";
69 public $table_element =
"expedition";
74 public $table_element_line =
"expeditiondet";
80 public $ismultientitymanaged = 1;
85 public $picto =
'dolly';
91 public $fields = array();
106 public $ref_customer;
118 public $tracking_number;
123 public $tracking_url;
132 public $weight_units;
136 public $height_units;
145 public $date_delivery;
163 public $date_shipping;
168 public $date_creation;
178 public $lines = array();
214 $this->statuts = array();
215 $this->statuts[-1] =
'StatusSendingCanceled';
216 $this->statuts[0] =
'StatusSendingDraft';
217 $this->statuts[1] =
'StatusSendingValidated';
218 $this->statuts[2] =
'StatusSendingProcessed';
221 $this->statuts_short = array();
222 $this->statuts_short[-1] =
'StatusSendingCanceledShort';
223 $this->statuts_short[0] =
'StatusSendingDraftShort';
224 $this->statuts_short[1] =
'StatusSendingValidatedShort';
225 $this->statuts_short[2] =
'StatusSendingProcessedShort';
236 global $langs, $conf;
237 $langs->load(
"sendings");
239 if (!empty($conf->global->EXPEDITION_ADDON_NUMBER)) {
242 $file = $conf->global->EXPEDITION_ADDON_NUMBER.
".php";
243 $classname = $conf->global->EXPEDITION_ADDON_NUMBER;
246 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
248 foreach ($dirmodels as $reldir) {
252 $mybool |= @include_once $dir.$file;
260 $obj =
new $classname();
262 $numref = $obj->getNextValue($soc, $this);
271 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_EXPEDITION_ADDON_NUMBER_NotDefined");
283 public function create($user, $notrigger = 0)
285 global $conf, $hookmanager;
289 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
293 $this->brouillon = 1;
295 if (empty($this->fk_project)) {
296 $this->fk_project = 0;
304 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expedition (";
307 $sql .=
", ref_customer";
309 $sql .=
", date_creation";
310 $sql .=
", fk_user_author";
311 $sql .=
", date_expedition";
312 $sql .=
", date_delivery";
314 $sql .=
", fk_projet";
315 $sql .=
", fk_address";
316 $sql .=
", fk_shipping_method";
317 $sql .=
", tracking_number";
322 $sql .=
", weight_units";
323 $sql .=
", size_units";
324 $sql .=
", note_private";
325 $sql .=
", note_public";
326 $sql .=
", model_pdf";
327 $sql .=
", fk_incoterms, location_incoterms";
328 $sql .=
") VALUES (";
330 $sql .=
", ".((int) $conf->entity);
331 $sql .=
", ".($this->ref_customer ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null");
332 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
333 $sql .=
", '".$this->db->idate($now).
"'";
334 $sql .=
", ".((int) $user->id);
335 $sql .=
", ".($this->date_expedition > 0 ?
"'".$this->db->idate($this->date_expedition).
"'" :
"null");
336 $sql .=
", ".($this->date_delivery > 0 ?
"'".$this->db->idate($this->date_delivery).
"'" :
"null");
337 $sql .=
", ".($this->socid > 0 ? ((int) $this->socid) :
"null");
338 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
339 $sql .=
", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address :
"null");
340 $sql .=
", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
"null");
341 $sql .=
", '".$this->db->escape($this->tracking_number).
"'";
342 $sql .=
", ".(is_numeric($this->weight) ? $this->weight :
'NULL');
343 $sql .=
", ".(is_numeric($this->sizeS) ? $this->sizeS :
'NULL');
344 $sql .=
", ".(is_numeric($this->sizeW) ? $this->sizeW :
'NULL');
345 $sql .=
", ".(is_numeric($this->sizeH) ? $this->sizeH :
'NULL');
346 $sql .=
", ".($this->weight_units !=
'' ? (int) $this->weight_units :
'NULL');
347 $sql .=
", ".($this->size_units !=
'' ? (int) $this->size_units :
'NULL');
348 $sql .=
", ".(!empty($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
349 $sql .=
", ".(!empty($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
350 $sql .=
", ".(!empty($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
351 $sql .=
", ".(int) $this->fk_incoterms;
352 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
355 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
358 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"expedition");
360 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition";
361 $sql .=
" SET ref = '(PROV".$this->id.
")'";
362 $sql .=
" WHERE rowid = ".((int) $this->
id);
364 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
365 if ($this->
db->query($sql)) {
367 $num = count($this->lines);
368 for ($i = 0; $i < $num; $i++) {
369 if (empty($this->lines[$i]->product_type) || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
370 if (!isset($this->lines[$i]->detail_batch)) {
371 if ($this->
create_line($this->lines[$i]->entrepot_id, $this->lines[$i]->origin_line_id, $this->lines[$i]->qty, $this->lines[$i]->rang, $this->lines[$i]->array_options) <= 0) {
375 if ($this->
create_line_batch($this->lines[$i], $this->lines[$i]->array_options) <= 0) {
382 if (!$error && $this->
id && $this->origin_id) {
397 if (!$error && !$notrigger) {
399 $result = $this->
call_trigger(
'SHIPPING_CREATE', $user);
409 foreach ($this->errors as $errmsg) {
410 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
411 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
413 $this->
db->rollback();
418 $this->
db->rollback();
423 $this->error = $this->
db->lasterror().
" - sql=$sql";
424 $this->
db->rollback();
429 $this->error = $this->
db->error().
" - sql=$sql";
430 $this->
db->rollback();
446 public function create_line($entrepot_id, $origin_line_id, $qty, $rang = 0, $array_options =
null)
452 $expeditionline->fk_expedition = $this->id;
453 $expeditionline->entrepot_id = $entrepot_id;
454 $expeditionline->fk_origin_line = $origin_line_id;
455 $expeditionline->qty = $qty;
456 $expeditionline->rang = $rang;
457 $expeditionline->array_options = $array_options;
459 if (($lineId = $expeditionline->insert($user)) < 0) {
460 $this->errors[] = $expeditionline->error;
478 $stockLocationQty = array();
480 $tab = $line_ext->detail_batch;
482 foreach ($tab as $detbatch) {
483 if ($detbatch->entrepot_id) {
484 $stockLocationQty[$detbatch->entrepot_id] += $detbatch->qty;
488 foreach ($stockLocationQty as $stockLocation => $qty) {
489 $line_id = $this->
create_line($stockLocation, $line_ext->origin_line_id, $qty, $line_ext->rang, $array_options);
494 foreach ($tab as $detbatch) {
495 if ($detbatch->entrepot_id == $stockLocation) {
496 if (!($detbatch->create($line_id) > 0)) {
520 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
525 if (empty($id) && empty($ref) && empty($ref_ext)) {
529 $sql =
"SELECT e.rowid, e.entity, e.ref, e.fk_soc as socid, e.date_creation, e.ref_customer, e.ref_ext, e.fk_user_author, e.fk_statut, e.fk_projet as fk_project, e.billed";
530 $sql .=
", e.date_valid";
531 $sql .=
", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
532 $sql .=
", e.date_expedition as date_expedition, e.model_pdf, e.fk_address, e.date_delivery";
533 $sql .=
", e.fk_shipping_method, e.tracking_number";
534 $sql .=
", e.note_private, e.note_public";
535 $sql .=
', e.fk_incoterms, e.location_incoterms';
536 $sql .=
', i.libelle as label_incoterms';
537 $sql .=
', s.libelle as shipping_method';
538 $sql .=
", el.fk_source as origin_id, el.sourcetype as origin";
539 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
540 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"element_element as el ON el.fk_target = e.rowid AND el.targettype = '".$this->
db->escape($this->element).
"'";
541 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON e.fk_incoterms = i.rowid';
542 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_shipment_mode as s ON e.fk_shipping_method = s.rowid';
543 $sql .=
" WHERE e.entity IN (".getEntity(
'expedition').
")";
545 $sql .=
" AND e.rowid = ".((int) $id);
548 $sql .=
" AND e.ref='".$this->db->escape($ref).
"'";
551 $sql .=
" AND e.ref_ext='".$this->db->escape($ref_ext).
"'";
554 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
555 $result = $this->
db->query($sql);
557 if ($this->
db->num_rows($result)) {
558 $obj = $this->
db->fetch_object($result);
560 $this->
id = $obj->rowid;
561 $this->entity = $obj->entity;
562 $this->
ref = $obj->ref;
563 $this->socid = $obj->socid;
564 $this->ref_customer = $obj->ref_customer;
565 $this->ref_ext = $obj->ref_ext;
566 $this->status = $obj->fk_statut;
567 $this->statut = $this->status;
568 $this->user_author_id = $obj->fk_user_author;
569 $this->date_creation = $this->
db->jdate($obj->date_creation);
570 $this->date_valid = $this->
db->jdate($obj->date_valid);
571 $this->date = $this->
db->jdate($obj->date_expedition);
572 $this->date_expedition = $this->
db->jdate($obj->date_expedition);
573 $this->date_shipping = $this->
db->jdate($obj->date_expedition);
574 $this->date_delivery = $this->
db->jdate($obj->date_delivery);
575 $this->fk_delivery_address = $obj->fk_address;
576 $this->model_pdf = $obj->model_pdf;
577 $this->modelpdf = $obj->model_pdf;
578 $this->shipping_method_id = $obj->fk_shipping_method;
579 $this->shipping_method = $obj->shipping_method;
580 $this->tracking_number = $obj->tracking_number;
581 $this->origin = ($obj->origin ? $obj->origin :
'commande');
582 $this->origin_id = $obj->origin_id;
583 $this->billed = $obj->billed;
584 $this->fk_project = $obj->fk_project;
586 $this->trueWeight = $obj->weight;
587 $this->weight_units = $obj->weight_units;
589 $this->trueWidth = $obj->width;
590 $this->width_units = $obj->size_units;
591 $this->trueHeight = $obj->height;
592 $this->height_units = $obj->size_units;
593 $this->trueDepth = $obj->size;
594 $this->depth_units = $obj->size_units;
596 $this->note_public = $obj->note_public;
597 $this->note_private = $obj->note_private;
600 $this->trueSize = $obj->size.
"x".$obj->width.
"x".$obj->height;
601 $this->size_units = $obj->size_units;
604 $this->fk_incoterms = $obj->fk_incoterms;
605 $this->location_incoterms = $obj->location_incoterms;
606 $this->label_incoterms = $obj->label_incoterms;
608 $this->
db->free($result);
610 if ($this->statut == self::STATUS_DRAFT) {
611 $this->brouillon = 1;
625 if (!empty($this->multicurrency_code)) {
626 $this->multicurrency_code = $this->thirdparty->multicurrency_code;
628 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($this->thirdparty->multicurrency_tx)) {
629 $this->multicurrency_tx = $this->thirdparty->multicurrency_tx;
643 dol_syslog(get_class($this).
'::Fetch no expedition found', LOG_ERR);
644 $this->error =
'Delivery with id '.$id.
' not found';
648 $this->error = $this->
db->error();
660 public function valid($user, $notrigger = 0)
662 global $conf, $langs;
664 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
670 dol_syslog(get_class($this).
"::valid not in draft status", LOG_WARNING);
674 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->creer))
675 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->shipping_advance->validate)))) {
676 $this->error =
'Permission denied';
677 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
687 $soc->fetch($this->socid);
690 $result = $soc->set_as_client();
693 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
696 $numref =
"EXP".$this->id;
703 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition SET";
704 $sql .=
" ref='".$this->db->escape($numref).
"'";
705 $sql .=
", fk_statut = 1";
706 $sql .=
", date_valid = '".$this->db->idate($now).
"'";
707 $sql .=
", fk_user_valid = ".$user->id;
708 $sql .=
" WHERE rowid = ".((int) $this->
id);
710 dol_syslog(get_class($this).
"::valid update expedition", LOG_DEBUG);
713 $this->error = $this->
db->lasterror();
718 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
719 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
721 $langs->load(
"agenda");
724 $sql =
"SELECT cd.fk_product, cd.subprice,";
725 $sql .=
" ed.rowid, ed.qty, ed.fk_entrepot,";
726 $sql .=
" edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
727 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
728 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
729 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
730 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
731 $sql .=
" AND cd.rowid = ed.fk_origin_line";
733 dol_syslog(get_class($this).
"::valid select details", LOG_DEBUG);
737 for ($i = 0; $i < $cpt; $i++) {
738 $obj = $this->
db->fetch_object(
$resql);
739 if (empty($obj->edbrowid)) {
745 if ($qty == 0 || ($qty < 0 && !
getDolGlobalInt(
'SHIPMENT_ALLOW_NEGATIVE_QTY'))) {
748 dol_syslog(get_class($this).
"::valid movement index ".$i.
" ed.rowid=".$obj->rowid.
" edb.rowid=".$obj->edbrowid);
753 $mouvS->setOrigin($this->element, $this->
id);
755 if (empty($obj->edbrowid)) {
759 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans(
"ShipmentValidatedInDolibarr", $numref),
'',
'',
'',
'', 0,
'', 1);
763 $this->error = $mouvS->error;
764 $this->errors = array_merge($this->errors, $mouvS->errors);
772 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans(
"ShipmentValidatedInDolibarr", $numref),
'', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock,
'', 1);
775 $this->error = $mouvS->error;
776 $this->errors = array_merge($this->errors, $mouvS->errors);
784 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_stock WHERE reel = 0 AND rowid NOT IN (SELECT fk_product_stock FROM ".MAIN_DB_PREFIX.
"product_batch as pb)";
788 $this->
db->rollback();
789 $this->error = $this->
db->error();
800 if (!$error && !$notrigger) {
802 $result = $this->
call_trigger(
'SHIPPING_VALIDATE', $user);
810 $this->oldref = $this->ref;
813 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
815 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'expedition/sending/".$this->
db->escape($this->newref).
"'";
816 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'expedition/sending/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
819 $error++; $this->error = $this->
db->lasterror();
825 $dirsource = $conf->expedition->dir_output.
'/sending/'.$oldref;
826 $dirdest = $conf->expedition->dir_output.
'/sending/'.$newref;
827 if (!$error && file_exists($dirsource)) {
828 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
830 if (@rename($dirsource, $dirdest)) {
833 $listoffiles =
dol_dir_list($conf->expedition->dir_output.
'/sending/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
834 foreach ($listoffiles as $fileentry) {
835 $dirsource = $fileentry[
'name'];
836 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
837 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
838 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
839 @rename($dirsource, $dirdest);
848 $this->
ref = $numref;
856 $this->
db->rollback();
874 if ($conf->delivery_note->enabled) {
875 if ($this->statut == self::STATUS_VALIDATED || $this->statut == self::STATUS_CLOSED) {
877 include_once DOL_DOCUMENT_ROOT.
'/delivery/class/delivery.class.php';
879 $result = $delivery->create_from_sending($user, $this->
id);
883 $this->error = $delivery->error;
906 public function addline($entrepot_id, $id, $qty, $array_options = 0)
908 global $conf, $langs;
910 $num = count($this->lines);
913 $line->entrepot_id = $entrepot_id;
914 $line->origin_line_id = $id;
915 $line->fk_origin_line = $id;
919 $orderline->fetch($id);
922 $line->rang = $orderline->rang;
923 $line->product_type = $orderline->product_type;
925 if (
isModEnabled(
'stock') && !empty($orderline->fk_product)) {
926 $fk_product = $orderline->fk_product;
928 if (!($entrepot_id > 0) && empty($conf->global->STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS)) {
929 $langs->load(
"errors");
930 $this->error = $langs->trans(
"ErrorWarehouseRequiredIntoShipmentLine");
934 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT)) {
936 $product->fetch($fk_product);
939 if ($entrepot_id > 0) {
940 $product->load_stock(
'warehouseopen');
941 $product_stock = $product->stock_warehouse[$entrepot_id]->real;
943 $product_stock = $product->stock_reel;
946 $product_type = $product->type;
947 if ($product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
948 $isavirtualproduct = ($product->hasFatherOrChild(1) > 0);
950 if (!$isavirtualproduct || empty($conf->global->PRODUIT_SOUSPRODUITS) || ($isavirtualproduct && empty($conf->global->STOCK_EXCLUDE_VIRTUAL_PRODUCTS))) {
951 if ($product_stock < $qty) {
952 $langs->load(
"errors");
953 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnShipment', $product->ref);
954 $this->errorhidden =
'ErrorStockIsNotEnoughToAddProductOnShipment';
956 $this->
db->rollback();
966 if (
isModEnabled(
'productbatch') && !empty($orderline->fk_product) && !empty($orderline->product_tobatch)) {
967 $this->error =
'ADDLINE_WAS_CALLED_INSTEAD_OF_ADDLINEBATCH '.$orderline->id.
' '.$orderline->fk_product;
972 if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) {
973 $line->array_options = $array_options;
976 $this->lines[$num] = $line;
992 global $conf, $langs;
994 $num = count($this->lines);
995 if ($dbatch[
'qty'] > 0) {
998 foreach ($dbatch[
'detail'] as $key => $value) {
999 if ($value[
'q'] > 0) {
1005 $ret = $linebatch->fetchFromStock($value[
'id_batch']);
1007 $this->error = $linebatch->error;
1010 $linebatch->qty = $value[
'q'];
1011 $tab[] = $linebatch;
1013 if ($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT) {
1014 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
1016 $prod_batch->fetch($value[
'id_batch']);
1018 if ($prod_batch->qty < $linebatch->qty) {
1019 $langs->load(
"errors");
1020 $this->errors[] = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnShipment', $prod_batch->fk_product);
1021 dol_syslog(get_class($this).
"::addline_batch error=Product ".$prod_batch->batch.
": ".$this->errorsToString(), LOG_ERR);
1022 $this->
db->rollback();
1030 $line->entrepot_id = $linebatch->entrepot_id;
1031 $line->origin_line_id = $dbatch[
'ix_l'];
1032 $line->fk_origin_line = $dbatch[
'ix_l'];
1033 $line->qty = $dbatch[
'qty'];
1034 $line->detail_batch = $tab;
1037 if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) {
1038 $line->array_options = $array_options;
1042 $this->lines[$num] = $line;
1054 public function update($user =
null, $notrigger = 0)
1061 if (isset($this->
ref)) {
1062 $this->
ref = trim($this->
ref);
1064 if (isset($this->entity)) {
1065 $this->entity = (int) $this->entity;
1067 if (isset($this->ref_customer)) {
1068 $this->ref_customer = trim($this->ref_customer);
1070 if (isset($this->socid)) {
1071 $this->socid = (int) $this->socid;
1073 if (isset($this->fk_user_author)) {
1074 $this->fk_user_author = (int) $this->fk_user_author;
1076 if (isset($this->fk_user_valid)) {
1077 $this->fk_user_valid = (int) $this->fk_user_valid;
1079 if (isset($this->fk_delivery_address)) {
1080 $this->fk_delivery_address = (int) $this->fk_delivery_address;
1082 if (isset($this->shipping_method_id)) {
1083 $this->shipping_method_id = (int) $this->shipping_method_id;
1085 if (isset($this->tracking_number)) {
1086 $this->tracking_number = trim($this->tracking_number);
1088 if (isset($this->statut)) {
1089 $this->statut = (int) $this->statut;
1091 if (isset($this->trueDepth)) {
1092 $this->trueDepth = trim($this->trueDepth);
1094 if (isset($this->trueWidth)) {
1095 $this->trueWidth = trim($this->trueWidth);
1097 if (isset($this->trueHeight)) {
1098 $this->trueHeight = trim($this->trueHeight);
1100 if (isset($this->size_units)) {
1101 $this->size_units = trim($this->size_units);
1103 if (isset($this->weight_units)) {
1104 $this->weight_units = trim($this->weight_units);
1106 if (isset($this->trueWeight)) {
1107 $this->weight = trim($this->trueWeight);
1109 if (isset($this->note_private)) {
1110 $this->note_private = trim($this->note_private);
1112 if (isset($this->note_public)) {
1113 $this->note_public = trim($this->note_public);
1115 if (isset($this->model_pdf)) {
1116 $this->model_pdf = trim($this->model_pdf);
1125 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition SET";
1127 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1128 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1129 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1130 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1131 $sql .=
" date_creation=".(dol_strlen($this->date_creation) != 0 ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
1132 $sql .=
" fk_user_author=".(isset($this->fk_user_author) ? $this->fk_user_author :
"null").
",";
1133 $sql .=
" date_valid=".(dol_strlen($this->date_valid) != 0 ?
"'".$this->db->idate($this->date_valid).
"'" :
'null').
",";
1134 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid :
"null").
",";
1135 $sql .=
" date_expedition=".(dol_strlen($this->date_expedition) != 0 ?
"'".$this->db->idate($this->date_expedition).
"'" :
'null').
",";
1136 $sql .=
" date_delivery=".(dol_strlen($this->date_delivery) != 0 ?
"'".$this->db->idate($this->date_delivery).
"'" :
'null').
",";
1137 $sql .=
" fk_address=".(isset($this->fk_delivery_address) ? $this->fk_delivery_address :
"null").
",";
1138 $sql .=
" fk_shipping_method=".((isset($this->shipping_method_id) && $this->shipping_method_id > 0) ? $this->shipping_method_id :
"null").
",";
1139 $sql .=
" tracking_number=".(isset($this->tracking_number) ?
"'".$this->db->escape($this->tracking_number).
"'" :
"null").
",";
1140 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1141 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1142 $sql .=
" height=".(($this->trueHeight !=
'') ? $this->trueHeight :
"null").
",";
1143 $sql .=
" width=".(($this->trueWidth !=
'') ? $this->trueWidth :
"null").
",";
1144 $sql .=
" size_units=".(isset($this->size_units) ? $this->size_units :
"null").
",";
1145 $sql .=
" size=".(($this->trueDepth !=
'') ? $this->trueDepth :
"null").
",";
1146 $sql .=
" weight_units=".(isset($this->weight_units) ? $this->weight_units :
"null").
",";
1147 $sql .=
" weight=".(($this->trueWeight !=
'') ? $this->trueWeight :
"null").
",";
1148 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1149 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1150 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1151 $sql .=
" entity=".$conf->entity;
1153 $sql .=
" WHERE rowid=".((int) $this->
id);
1157 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1160 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1163 if (!$error && !$notrigger) {
1165 $result = $this->
call_trigger(
'SHIPPING_MODIFY', $user);
1174 foreach ($this->errors as $errmsg) {
1175 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1176 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1178 $this->
db->rollback();
1181 $this->
db->commit();
1194 public function cancel($notrigger = 0, $also_update_stock =
false)
1196 global $conf, $langs, $user;
1198 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1207 if (count($this->linkedObjectsIds) > 0) {
1208 $this->error =
'ErrorThereIsSomeDeliveries';
1212 if (!$error && !$notrigger) {
1214 $result = $this->
call_trigger(
'SHIPPING_CANCEL', $user);
1223 (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1224 ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1225 require_once DOL_DOCUMENT_ROOT.
"/product/stock/class/mouvementstock.class.php";
1227 $langs->load(
"agenda");
1230 $sql =
"SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1231 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
1232 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
1233 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1234 $sql .=
" AND cd.rowid = ed.fk_origin_line";
1236 dol_syslog(get_class($this).
"::delete select details", LOG_DEBUG);
1239 $cpt = $this->
db->num_rows(
$resql);
1243 for ($i = 0; $i < $cpt; $i++) {
1244 dol_syslog(get_class($this).
"::delete movement index ".$i);
1245 $obj = $this->
db->fetch_object(
$resql);
1249 $mouvS->origin =
null;
1253 $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1254 if (!is_array($lotArray)) {
1256 $this->errors[] =
"Error ".$this->db->lasterror();
1260 if (empty($lotArray)) {
1264 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans(
"ShipmentCanceledInDolibarr", $this->ref));
1267 $this->errors = array_merge($this->errors, $mouvS->errors);
1273 foreach ($lotArray as $lot) {
1274 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans(
"ShipmentCanceledInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch);
1277 $this->errors = array_merge($this->errors, $mouvS->errors);
1287 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1294 if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1295 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1301 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
1302 $sql .=
" WHERE fk_expedition = ".((int) $this->
id);
1304 if ($this->
db->query($sql)) {
1313 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"expedition";
1314 $sql .=
" WHERE rowid = ".((int) $this->
id);
1316 if ($this->
db->query($sql)) {
1317 if (!empty($this->origin) && $this->origin_id > 0) {
1319 $origin = $this->origin;
1322 $this->$origin->loadExpeditions();
1324 if (count($this->$origin->expeditions) <= 0) {
1331 $this->
db->commit();
1335 if (!empty($conf->expedition->dir_output)) {
1336 $dir = $conf->expedition->dir_output.
'/sending/'.$ref;
1337 $file = $dir.
'/'.$ref.
'.pdf';
1338 if (file_exists($file)) {
1343 if (file_exists($dir)) {
1345 $this->error = $langs->trans(
"ErrorCanNotDeleteDir", $dir);
1353 $this->
db->rollback();
1357 $this->error = $this->
db->lasterror().
" - sql=$sql";
1358 $this->
db->rollback();
1362 $this->error = $this->
db->lasterror().
" - sql=$sql";
1363 $this->
db->rollback();
1367 $this->error = $this->
db->lasterror().
" - sql=$sql";
1368 $this->
db->rollback();
1372 $this->
db->rollback();
1385 public function delete($notrigger = 0, $also_update_stock =
false)
1387 global $conf, $langs, $user;
1389 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1398 if (count($this->linkedObjectsIds) > 0) {
1399 $this->error =
'ErrorThereIsSomeDeliveries';
1403 if (!$error && !$notrigger) {
1405 $result = $this->
call_trigger(
'SHIPPING_DELETE', $user);
1414 (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1415 ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1416 require_once DOL_DOCUMENT_ROOT.
"/product/stock/class/mouvementstock.class.php";
1418 $langs->load(
"agenda");
1424 $sql =
"SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1425 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
1426 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
1427 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1428 $sql .=
" AND cd.rowid = ed.fk_origin_line";
1430 dol_syslog(get_class($this).
"::delete select details", LOG_DEBUG);
1433 $cpt = $this->
db->num_rows(
$resql);
1434 for ($i = 0; $i < $cpt; $i++) {
1435 dol_syslog(get_class($this).
"::delete movement index ".$i);
1436 $obj = $this->
db->fetch_object(
$resql);
1440 $mouvS->origin =
null;
1442 $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1443 if (!is_array($lotArray)) {
1444 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1446 if (empty($lotArray)) {
1450 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans(
"ShipmentDeletedInDolibarr", $this->ref));
1453 $this->errors = array_merge($this->errors, $mouvS->errors);
1459 foreach ($lotArray as $lot) {
1460 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans(
"ShipmentDeletedInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch);
1463 $this->errors = array_merge($this->errors, $mouvS->errors);
1473 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1480 if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1481 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1486 $main = MAIN_DB_PREFIX.
'expeditiondet';
1487 $ef = $main.
"_extrafields";
1488 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_expedition = ".((int) $this->
id).
")";
1490 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
1491 $sql .=
" WHERE fk_expedition = ".((int) $this->
id);
1493 if ($this->
db->query($sqlef) && $this->
db->query($sql)) {
1507 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expedition";
1508 $sql .=
" WHERE rowid = ".((int) $this->
id);
1510 if ($this->
db->query($sql)) {
1511 if (!empty($this->origin) && $this->origin_id > 0) {
1513 $origin = $this->origin;
1516 $this->$origin->loadExpeditions();
1518 if (count($this->$origin->expeditions) <= 0) {
1525 $this->
db->commit();
1532 if (!empty($conf->expedition->dir_output)) {
1533 $dir = $conf->expedition->dir_output.
'/sending/'.$ref;
1534 $file = $dir.
'/'.$ref.
'.pdf';
1535 if (file_exists($file)) {
1540 if (file_exists($dir)) {
1542 $this->error = $langs->trans(
"ErrorCanNotDeleteDir", $dir);
1550 $this->
db->rollback();
1554 $this->error = $this->
db->lasterror().
" - sql=$sql";
1555 $this->
db->rollback();
1559 $this->error = $this->
db->lasterror().
" - sql=$sql";
1560 $this->
db->rollback();
1564 $this->error = $this->
db->lasterror().
" - sql=$sql";
1565 $this->
db->rollback();
1569 $this->
db->rollback();
1583 global $conf, $mysoc;
1585 $this->lines = array();
1590 $sql =
"SELECT cd.rowid, cd.fk_product, cd.label as custom_label, cd.description, cd.qty as qty_asked, cd.product_type, cd.fk_unit";
1591 $sql .=
", cd.total_ht, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.total_tva";
1592 $sql .=
", cd.vat_src_code, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.info_bits, cd.price, cd.subprice, cd.remise_percent,cd.buy_price_ht as pa_ht";
1593 $sql .=
", cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc, cd.rang";
1594 $sql .=
", ed.rowid as line_id, ed.qty as qty_shipped, ed.fk_origin_line, ed.fk_entrepot";
1595 $sql .=
", p.ref as product_ref, p.label as product_label, p.fk_product_type";
1596 $sql .=
", p.weight, p.weight_units, p.length, p.length_units, p.surface, p.surface_units, p.volume, p.volume_units, p.tosell as product_tosell, p.tobuy as product_tobuy, p.tobatch as product_tobatch";
1597 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet as ed, ".MAIN_DB_PREFIX.
"commandedet as cd";
1598 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = cd.fk_product";
1599 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1600 $sql .=
" AND ed.fk_origin_line = cd.rowid";
1601 $sql .=
" ORDER BY cd.rang, ed.fk_origin_line";
1603 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1606 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1608 $num = $this->
db->num_rows(
$resql);
1613 $this->total_ht = 0;
1614 $this->total_tva = 0;
1615 $this->total_ttc = 0;
1616 $this->total_localtax1 = 0;
1617 $this->total_localtax2 = 0;
1622 $obj = $this->
db->fetch_object(
$resql);
1624 if ($originline > 0 && $originline == $obj->fk_origin_line) {
1625 $line->entrepot_id = 0;
1626 $line->qty_shipped += $obj->qty_shipped;
1629 $line->entrepot_id = $obj->fk_entrepot;
1630 $line->qty_shipped = $obj->qty_shipped;
1633 $detail_entrepot =
new stdClass();
1634 $detail_entrepot->entrepot_id = $obj->fk_entrepot;
1635 $detail_entrepot->qty_shipped = $obj->qty_shipped;
1636 $detail_entrepot->line_id = $obj->line_id;
1637 $line->details_entrepot[] = $detail_entrepot;
1639 $line->line_id = $obj->line_id;
1640 $line->rowid = $obj->line_id;
1641 $line->id = $obj->line_id;
1643 $line->fk_origin =
'orderline';
1644 $line->fk_origin_line = $obj->fk_origin_line;
1645 $line->origin_line_id = $obj->fk_origin_line;
1647 $line->fk_expedition = $this->id;
1649 $line->product_type = $obj->product_type;
1650 $line->fk_product = $obj->fk_product;
1651 $line->fk_product_type = $obj->fk_product_type;
1652 $line->ref = $obj->product_ref;
1653 $line->product_ref = $obj->product_ref;
1654 $line->product_label = $obj->product_label;
1655 $line->libelle = $obj->product_label;
1656 $line->product_tosell = $obj->product_tosell;
1657 $line->product_tobuy = $obj->product_tobuy;
1658 $line->product_tobatch = $obj->product_tobatch;
1659 $line->label = $obj->custom_label;
1660 $line->description = $obj->description;
1661 $line->qty_asked = $obj->qty_asked;
1662 $line->rang = $obj->rang;
1663 $line->weight = $obj->weight;
1664 $line->weight_units = $obj->weight_units;
1665 $line->length = $obj->length;
1666 $line->length_units = $obj->length_units;
1667 $line->surface = $obj->surface;
1668 $line->surface_units = $obj->surface_units;
1669 $line->volume = $obj->volume;
1670 $line->volume_units = $obj->volume_units;
1671 $line->fk_unit = $obj->fk_unit;
1673 $line->pa_ht = $obj->pa_ht;
1676 $localtax_array = array(0=>$obj->localtax1_type, 1=>$obj->localtax1_tx, 2=>$obj->localtax2_type, 3=>$obj->localtax2_tx);
1677 $localtax1_tx =
get_localtax($obj->tva_tx, 1, $this->thirdparty);
1678 $localtax2_tx =
get_localtax($obj->tva_tx, 2, $this->thirdparty);
1681 $tabprice =
calcul_price_total($obj->qty_shipped, $obj->subprice, $obj->remise_percent, $obj->tva_tx, $localtax1_tx, $localtax2_tx, 0,
'HT', $obj->info_bits, $obj->fk_product_type, $mysoc, $localtax_array);
1682 $line->desc = $obj->description;
1683 $line->qty = $line->qty_shipped;
1684 $line->total_ht = $tabprice[0];
1685 $line->total_localtax1 = $tabprice[9];
1686 $line->total_localtax2 = $tabprice[10];
1687 $line->total_ttc = $tabprice[2];
1688 $line->total_tva = $tabprice[1];
1689 $line->vat_src_code = $obj->vat_src_code;
1690 $line->tva_tx = $obj->tva_tx;
1691 $line->localtax1_tx = $obj->localtax1_tx;
1692 $line->localtax2_tx = $obj->localtax2_tx;
1693 $line->info_bits = $obj->info_bits;
1694 $line->price = $obj->price;
1695 $line->subprice = $obj->subprice;
1696 $line->remise_percent = $obj->remise_percent;
1698 $this->total_ht += $tabprice[0];
1699 $this->total_tva += $tabprice[1];
1700 $this->total_ttc += $tabprice[2];
1701 $this->total_localtax1 += $tabprice[9];
1702 $this->total_localtax2 += $tabprice[10];
1705 $this->fk_multicurrency = $obj->fk_multicurrency;
1706 $this->multicurrency_code = $obj->multicurrency_code;
1707 $this->multicurrency_subprice = $obj->multicurrency_subprice;
1708 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1709 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1710 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1712 if ($originline != $obj->fk_origin_line) {
1713 $line->detail_batch = array();
1717 if (
isModEnabled(
'productbatch') && $obj->line_id > 0 && $obj->product_tobatch > 0) {
1718 $newdetailbatch = $shipmentlinebatch->fetchAll($obj->line_id, $obj->fk_product);
1720 if (is_array($newdetailbatch)) {
1721 if ($originline != $obj->fk_origin_line) {
1722 $line->detail_batch = $newdetailbatch;
1724 $line->detail_batch = array_merge($line->detail_batch, $newdetailbatch);
1729 $line->fetch_optionals();
1731 if ($originline != $obj->fk_origin_line) {
1732 $this->lines[$lineindex] = $line;
1735 $line->total_ht += $tabprice[0];
1736 $line->total_localtax1 += $tabprice[9];
1737 $line->total_localtax2 += $tabprice[10];
1738 $line->total_ttc += $tabprice[2];
1739 $line->total_tva += $tabprice[1];
1743 $originline = $obj->fk_origin_line;
1748 $this->error = $this->
db->error();
1764 if ($this->statut == self::STATUS_DRAFT) {
1770 $line->fetch($lineid);
1772 if ($line->delete($user) > 0) {
1775 $this->
db->commit();
1778 $this->
db->rollback();
1782 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1799 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1)
1801 global $langs, $conf, $hookmanager;
1804 $label =
'<u>'.$langs->trans(
"Shipment").
'</u>';
1805 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1806 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client);
1808 $url = DOL_URL_ROOT.
'/expedition/card.php?id='.$this->id;
1814 if ($option !==
'nolink') {
1816 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1817 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1818 $add_save_lastsearch_values = 1;
1820 if ($add_save_lastsearch_values) {
1821 $url .=
'&save_lastsearch_values=1';
1826 if (empty($notooltip)) {
1827 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1828 $label = $langs->trans(
"Shipment");
1829 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1831 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1832 $linkclose .=
' class="classfortooltip"';
1835 $linkstart =
'<a href="'.$url.
'"';
1836 $linkstart .= $linkclose.
'>';
1839 $result .= $linkstart;
1841 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1843 if ($withpicto != 2) {
1844 $result .= $this->ref;
1846 $result .= $linkend;
1848 $hookmanager->initHooks(array($this->element .
'dao'));
1849 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1850 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1852 $result = $hookmanager->resPrint;
1854 $result .= $hookmanager->resPrint;
1867 return $this->
LibStatut($this->statut, $mode);
1883 $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
1884 $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
1886 $statusType =
'status'.$status;
1887 if ($status == self::STATUS_VALIDATED) {
1888 $statusType =
'status4';
1890 if ($status == self::STATUS_CLOSED) {
1891 $statusType =
'status6';
1893 if ($status == self::STATUS_CANCELED) {
1894 $statusType =
'status9';
1897 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
1913 dol_syslog(get_class($this).
"::initAsSpecimen");
1918 $sql =
"SELECT rowid";
1919 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
1920 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
1923 $num_prods = $this->
db->num_rows(
$resql);
1925 while ($i < $num_prods) {
1927 $row = $this->
db->fetch_row(
$resql);
1928 $prodids[$i] = $row[0];
1933 $order->initAsSpecimen();
1937 $this->
ref =
'SPECIMEN';
1938 $this->specimen = 1;
1940 $this->livraison_id = 0;
1942 $this->date_creation = $now;
1943 $this->date_valid = $now;
1944 $this->date_delivery = $now;
1945 $this->date_expedition = $now + 24 * 3600;
1947 $this->entrepot_id = 0;
1948 $this->fk_delivery_address = 0;
1951 $this->commande_id = 0;
1952 $this->commande = $order;
1954 $this->origin_id = 1;
1955 $this->origin =
'commande';
1957 $this->note_private =
'Private note';
1958 $this->note_public =
'Public note';
1962 while ($xnbp < $nbp) {
1964 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
1965 $line->libelle = $langs->trans(
"Description").
" ".$xnbp;
1966 $line->label = $langs->trans(
"Description").
" ".$xnbp;
1968 $line->qty_asked = 5;
1969 $line->qty_shipped = 4;
1970 $line->fk_product = $this->commande->lines[$xnbp]->fk_product;
1972 $this->lines[] = $line;
2001 if ($user->rights->expedition->creer) {
2002 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition";
2003 $sql .=
" SET date_delivery = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2004 $sql .=
" WHERE rowid = ".((int) $this->
id);
2006 dol_syslog(get_class($this).
"::setDeliveryDate", LOG_DEBUG);
2009 $this->date_delivery = $delivery_date;
2012 $this->error = $this->
db->error();
2030 $this->meths = array();
2032 $sql =
"SELECT em.rowid, em.code, em.libelle as label";
2033 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2034 $sql .=
" WHERE em.active = 1";
2035 $sql .=
" ORDER BY em.libelle ASC";
2039 while ($obj = $this->
db->fetch_object(
$resql)) {
2040 $label = $langs->trans(
'SendingMethod'.$obj->code);
2041 $this->meths[$obj->rowid] = ($label !=
'SendingMethod'.$obj->code ? $label : $obj->label);
2058 $this->listmeths = array();
2061 $sql =
"SELECT em.rowid, em.code, em.libelle as label, em.description, em.tracking, em.active";
2062 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2064 $sql .=
" WHERE em.rowid=".((int) $id);
2069 while ($obj = $this->
db->fetch_object(
$resql)) {
2070 $this->listmeths[$i][
'rowid'] = $obj->rowid;
2071 $this->listmeths[$i][
'code'] = $obj->code;
2072 $label = $langs->trans(
'SendingMethod'.$obj->code);
2073 $this->listmeths[$i][
'libelle'] = ($label !=
'SendingMethod'.$obj->code ? $label : $obj->label);
2074 $this->listmeths[$i][
'description'] = $obj->description;
2075 $this->listmeths[$i][
'tracking'] = $obj->tracking;
2076 $this->listmeths[$i][
'active'] = $obj->active;
2090 if (!empty($this->shipping_method_id)) {
2091 $sql =
"SELECT em.code, em.tracking";
2092 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2093 $sql .=
" WHERE em.rowid = ".((int) $this->shipping_method_id);
2097 if ($obj = $this->
db->fetch_object(
$resql)) {
2098 $tracking = $obj->tracking;
2103 if (!empty($tracking) && !empty($value)) {
2104 $url = str_replace(
'{TRACKID}', $value, $tracking);
2105 $this->tracking_url = sprintf(
'<a target="_blank" rel="noopener noreferrer" href="%s">'.($value ? $value :
'url').
'</a>', $url, $url);
2107 $this->tracking_url = $value;
2118 global $conf, $langs, $user;
2123 if ($this->statut == self::STATUS_CLOSED) {
2130 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut > 0";
2135 if ($this->origin ==
'commande' && $this->origin_id > 0) {
2137 $order->fetch($this->origin_id);
2139 $order->loadExpeditions(self::STATUS_CLOSED);
2141 $shipments_match_order = 1;
2142 foreach ($order->lines as $line) {
2143 $lineid = $line->id;
2145 if (($line->product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) && $order->expeditions[$lineid] != $qty) {
2146 $shipments_match_order = 0;
2147 $text =
'Qty for order line id '.$lineid.
' is '.$qty.
'. However in the shipments with status Expedition::STATUS_CLOSED='.self::STATUS_CLOSED.
' we have qty = '.$order->expeditions[$lineid].
', so we can t close order';
2152 if ($shipments_match_order) {
2153 dol_syslog(
"Qty for the ".count($order->lines).
" lines of the origin order is same than qty for lines in the shipment we close (shipments_match_order is true), with new status Expedition::STATUS_CLOSED=".self::STATUS_CLOSED.
', so we close order');
2155 $order->cloture($user);
2163 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2164 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2166 $langs->load(
"agenda");
2170 $sql =
"SELECT cd.fk_product, cd.subprice,";
2171 $sql .=
" ed.rowid, ed.qty, ed.fk_entrepot,";
2173 $sql .=
" edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2174 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
2175 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
2176 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2177 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"expedition as e ON ed.fk_expedition = e.rowid";
2178 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
2179 $sql .=
" AND cd.rowid = ed.fk_origin_line";
2181 dol_syslog(get_class($this).
"::valid select details", LOG_DEBUG);
2184 $cpt = $this->
db->num_rows(
$resql);
2185 for ($i = 0; $i < $cpt; $i++) {
2186 $obj = $this->
db->fetch_object(
$resql);
2187 if (empty($obj->edbrowid)) {
2190 $qty = $obj->edbqty;
2195 dol_syslog(get_class($this).
"::valid movement index ".$i.
" ed.rowid=".$obj->rowid.
" edb.rowid=".$obj->edbrowid);
2198 $mouvS->origin = &$this;
2199 $mouvS->setOrigin($this->element, $this->
id);
2201 if (empty($obj->edbrowid)) {
2205 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans(
"ShipmentClassifyClosedInDolibarr", $obj->ref));
2207 $this->error = $mouvS->error;
2208 $this->errors = $mouvS->errors;
2216 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans(
"ShipmentClassifyClosedInDolibarr", $obj->ref),
'', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2218 $this->error = $mouvS->error;
2219 $this->errors = $mouvS->errors;
2226 $this->error = $this->
db->lasterror();
2233 $result = $this->
call_trigger(
'SHIPPING_CLOSED', $user);
2244 $this->
db->commit();
2250 $this->
db->rollback();
2267 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'expedition SET fk_statut=2, billed=1';
2268 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > 0';
2276 $result = $this->
call_trigger(
'SHIPPING_BILLED', $user);
2282 $this->errors[] = $this->
db->lasterror;
2285 if (empty($error)) {
2286 $this->
db->commit();
2291 $this->
db->rollback();
2303 global $conf, $langs, $user;
2308 if ($this->statut == self::STATUS_VALIDATED) {
2314 $oldbilled = $this->billed;
2316 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'expedition SET fk_statut=1';
2317 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > 0';
2325 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2326 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2328 $langs->load(
"agenda");
2332 $sql =
"SELECT cd.fk_product, cd.subprice,";
2333 $sql .=
" ed.rowid, ed.qty, ed.fk_entrepot,";
2334 $sql .=
" edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2335 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
2336 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
2337 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2338 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
2339 $sql .=
" AND cd.rowid = ed.fk_origin_line";
2341 dol_syslog(get_class($this).
"::valid select details", LOG_DEBUG);
2344 $cpt = $this->
db->num_rows(
$resql);
2345 for ($i = 0; $i < $cpt; $i++) {
2346 $obj = $this->
db->fetch_object(
$resql);
2347 if (empty($obj->edbrowid)) {
2350 $qty = $obj->edbqty;
2355 dol_syslog(get_class($this).
"::reopen expedition movement index ".$i.
" ed.rowid=".$obj->rowid.
" edb.rowid=".$obj->edbrowid);
2359 $mouvS->origin = &$this;
2360 $mouvS->setOrigin($this->element, $this->
id);
2362 if (empty($obj->edbrowid)) {
2366 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans(
"ShipmentUnClassifyCloseddInDolibarr", $numref));
2368 $this->error = $mouvS->error;
2369 $this->errors = $mouvS->errors;
2377 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans(
"ShipmentUnClassifyCloseddInDolibarr", $numref),
'', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2379 $this->error = $mouvS->error;
2380 $this->errors = $mouvS->errors;
2387 $this->error = $this->
db->lasterror();
2394 $result = $this->
call_trigger(
'SHIPPING_REOPEN', $user);
2401 $this->errors[] = $this->
db->lasterror();
2405 $this->
db->commit();
2409 $this->billed = $oldbilled;
2410 $this->
db->rollback();
2426 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2430 $outputlangs->load(
"products");
2435 if (!empty($this->model_pdf)) {
2436 $modele = $this->model_pdf;
2437 } elseif (!empty($this->modelpdf)) {
2438 $modele = $this->modelpdf;
2439 } elseif (!empty($conf->global->EXPEDITION_ADDON_PDF)) {
2440 $modele = $conf->global->EXPEDITION_ADDON_PDF;
2444 $modelpath =
"core/modules/expedition/doc/";
2448 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2478 public $element =
'expeditiondet';
2483 public $table_element =
'expeditiondet';
2510 public $fk_origin_line;
2515 public $fk_expedition;
2530 public $qty_shipped;
2539 public $detail_batch;
2543 public $details_entrepot;
2549 public $entrepot_id;
2566 public $product_ref;
2577 public $product_label;
2589 public $product_desc;
2595 public $product_type = 0;
2606 public $weight_units;
2612 public $length_units;
2618 public $surface_units;
2624 public $volume_units;
2627 public $remise_percent;
2648 public $total_localtax1;
2653 public $total_localtax2;
2674 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang';
2675 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
2676 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
2677 $result = $this->
db->query($sql);
2679 $objp = $this->
db->fetch_object($result);
2680 $this->
id = $objp->rowid;
2681 $this->fk_expedition = $objp->fk_expedition;
2682 $this->entrepot_id = $objp->fk_entrepot;
2683 $this->fk_origin_line = $objp->fk_origin_line;
2684 $this->qty = $objp->qty;
2685 $this->rang = $objp->rang;
2687 $this->
db->free($result);
2691 $this->errors[] = $this->
db->lasterror();
2692 $this->error = $this->
db->lasterror();
2704 public function insert($user, $notrigger = 0)
2706 global $langs, $conf;
2711 if (empty($this->fk_expedition) || empty($this->fk_origin_line) || !is_numeric($this->qty)) {
2712 $this->error =
'ErrorMandatoryParametersNotProvided';
2718 if (empty($this->rang)) {
2723 $ranktouse = $this->rang;
2724 if ($ranktouse == -1) {
2725 $rangmax = $this->
line_max($this->fk_expedition);
2726 $ranktouse = $rangmax + 1;
2729 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
2730 $sql .=
"fk_expedition";
2731 $sql .=
", fk_entrepot";
2732 $sql .=
", fk_origin_line";
2735 $sql .=
") VALUES (";
2736 $sql .= $this->fk_expedition;
2737 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : $this->entrepot_id);
2738 $sql .=
", ".((int) $this->fk_origin_line);
2739 $sql .=
", ".price2num($this->qty,
'MS');
2740 $sql .=
", ".((int) $ranktouse);
2743 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
2746 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
2755 if (!$error && !$notrigger) {
2757 $result = $this->
call_trigger(
'LINESHIPPING_INSERT', $user);
2765 foreach ($this->errors as $errmsg) {
2766 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
2767 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2775 $this->
db->rollback();
2778 $this->
db->commit();
2790 public function delete($user =
null, $notrigger = 0)
2800 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
2801 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
2803 if (!$this->
db->query($sql)) {
2804 $this->errors[] = $this->
db->lasterror().
" - sql=$sql";
2809 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
2810 $sql .=
" WHERE rowid = ".((int) $this->
id);
2812 if (!$error && $this->
db->query($sql)) {
2817 $this->errors[] = $this->error;
2821 if (!$error && !$notrigger) {
2823 $result = $this->
call_trigger(
'LINESHIPPING_DELETE', $user);
2825 $this->errors[] = $this->error;
2831 $this->errors[] = $this->
db->lasterror().
" - sql=$sql";
2836 $this->
db->commit();
2839 foreach ($this->errors as $errmsg) {
2840 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
2841 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2843 $this->
db->rollback();
2855 public function update($user =
null, $notrigger = 0)
2861 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
2866 if (empty($this->qty)) {
2873 $expedition_batch_id =
null;
2874 if (is_array($this->detail_batch)) {
2875 if (count($this->detail_batch) > 1) {
2876 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
2877 $this->errors[] =
'ErrorBadParameters';
2880 $batch = $this->detail_batch[0]->batch;
2881 $batch_id = $this->detail_batch[0]->fk_origin_stock;
2882 $expedition_batch_id = $this->detail_batch[0]->id;
2883 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
2884 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
2885 $this->errors[] =
'ErrorBadParameters';
2888 $qty =
price2num($this->detail_batch[0]->qty);
2890 } elseif (!empty($this->detail_batch)) {
2891 $batch = $this->detail_batch->batch;
2892 $batch_id = $this->detail_batch->fk_origin_stock;
2893 $expedition_batch_id = $this->detail_batch->id;
2894 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
2895 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
2896 $this->errors[] =
'ErrorBadParameters';
2899 $qty =
price2num($this->detail_batch->qty);
2903 if (!isset($this->
id) || !isset($this->entrepot_id)) {
2904 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
2905 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
2913 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch");
2915 if (empty($batch_id) || empty($this->fk_product)) {
2916 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
2917 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
2924 if (!$error && ($lotArray = $shipmentlinebatch->fetchAll($this->id)) < 0) {
2925 $this->errors[] = $this->
db->lasterror().
" - ExpeditionLineBatch::fetchAll";
2929 foreach ($lotArray as $lot) {
2930 if ($expedition_batch_id != $lot->id) {
2931 $remainingQty += $lot->qty;
2934 $qty += $remainingQty;
2939 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
2941 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
2942 $this->errors[] = $lot->errors;
2945 if (!$error && !empty($expedition_batch_id)) {
2947 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
2948 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
2949 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
2951 if (!$this->
db->query($sql)) {
2952 $this->errors[] = $this->
db->lasterror().
" - sql=$sql";
2956 if (!$error && $this->detail_batch->qty > 0) {
2958 if (isset($lot->id)) {
2960 $shipmentLot->batch = $lot->batch;
2961 $shipmentLot->eatby = $lot->eatby;
2962 $shipmentLot->sellby = $lot->sellby;
2963 $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id;
2964 $shipmentLot->qty = $this->detail_batch->qty;
2965 $shipmentLot->fk_origin_stock = $batch_id;
2966 if ($shipmentLot->create($this->id) < 0) {
2967 $this->errors[] = $shipmentLot->errors;
2976 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
2977 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id :
'null');
2978 $sql .=
" , qty = ".((float)
price2num($qty,
'MS'));
2979 $sql .=
" WHERE rowid = ".((int) $this->
id);
2981 if (!$this->
db->query($sql)) {
2982 $this->errors[] = $this->
db->lasterror().
" - sql=$sql";
2991 $this->errors[] = $this->error;
2997 if (!$error && !$notrigger) {
2999 $result = $this->
call_trigger(
'LINESHIPPING_MODIFY', $user);
3001 $this->errors[] = $this->error;
3007 $this->
db->commit();
3010 foreach ($this->errors as $errmsg) {
3011 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3012 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3014 $this->
db->rollback();