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();
96 public $user_author_id;
101 public $fk_user_author;
115 public $ref_customer;
127 public $tracking_number;
132 public $tracking_url;
141 public $weight_units;
145 public $height_units;
154 public $date_delivery;
172 public $date_shipping;
177 public $date_creation;
200 public $lines = array();
206 public $fk_multicurrency;
211 public $multicurrency_code;
212 public $multicurrency_tx;
213 public $multicurrency_total_ht;
214 public $multicurrency_total_tva;
215 public $multicurrency_total_ttc;
250 $this->statuts = array();
251 $this->statuts[-1] =
'StatusSendingCanceled';
252 $this->statuts[0] =
'StatusSendingDraft';
253 $this->statuts[1] =
'StatusSendingValidated';
254 $this->statuts[2] =
'StatusSendingProcessed';
257 $this->statuts_short = array();
258 $this->statuts_short[-1] =
'StatusSendingCanceledShort';
259 $this->statuts_short[0] =
'StatusSendingDraftShort';
260 $this->statuts_short[1] =
'StatusSendingValidatedShort';
261 $this->statuts_short[2] =
'StatusSendingProcessedShort';
272 global $langs, $conf;
273 $langs->load(
"sendings");
275 if (!empty($conf->global->EXPEDITION_ADDON_NUMBER)) {
278 $file = $conf->global->EXPEDITION_ADDON_NUMBER.
".php";
279 $classname = $conf->global->EXPEDITION_ADDON_NUMBER;
282 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
284 foreach ($dirmodels as $reldir) {
288 $mybool |= @include_once $dir.$file;
296 $obj =
new $classname();
298 $numref = $obj->getNextValue($soc, $this);
303 dol_print_error($this->db, get_class($this).
"::getNextNumRef ".$obj->error);
307 print $langs->trans(
"Error").
" ".$langs->trans(
"Error_EXPEDITION_ADDON_NUMBER_NotDefined");
319 public function create($user, $notrigger = 0)
321 global $conf, $hookmanager;
325 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
329 $this->brouillon = 1;
331 if (empty($this->fk_project)) {
332 $this->fk_project = 0;
340 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expedition (";
343 $sql .=
", ref_customer";
345 $sql .=
", date_creation";
346 $sql .=
", fk_user_author";
347 $sql .=
", date_expedition";
348 $sql .=
", date_delivery";
350 $sql .=
", fk_projet";
351 $sql .=
", fk_address";
352 $sql .=
", fk_shipping_method";
353 $sql .=
", tracking_number";
358 $sql .=
", weight_units";
359 $sql .=
", size_units";
360 $sql .=
", note_private";
361 $sql .=
", note_public";
362 $sql .=
", model_pdf";
363 $sql .=
", fk_incoterms, location_incoterms";
364 $sql .=
") VALUES (";
366 $sql .=
", ".((int) $conf->entity);
367 $sql .=
", ".($this->ref_customer ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null");
368 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
369 $sql .=
", '".$this->db->idate($now).
"'";
370 $sql .=
", ".((int) $user->id);
371 $sql .=
", ".($this->date_expedition > 0 ?
"'".$this->db->idate($this->date_expedition).
"'" :
"null");
372 $sql .=
", ".($this->date_delivery > 0 ?
"'".$this->db->idate($this->date_delivery).
"'" :
"null");
373 $sql .=
", ".($this->socid > 0 ? ((int) $this->socid) :
"null");
374 $sql .=
", ".($this->fk_project > 0 ? ((int) $this->fk_project) :
"null");
375 $sql .=
", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address :
"null");
376 $sql .=
", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) :
"null");
377 $sql .=
", '".$this->db->escape($this->tracking_number).
"'";
378 $sql .=
", ".(is_numeric($this->weight) ? $this->weight :
'NULL');
379 $sql .=
", ".(is_numeric($this->sizeS) ? $this->sizeS :
'NULL');
380 $sql .=
", ".(is_numeric($this->sizeW) ? $this->sizeW :
'NULL');
381 $sql .=
", ".(is_numeric($this->sizeH) ? $this->sizeH :
'NULL');
382 $sql .=
", ".($this->weight_units !=
'' ? (int) $this->weight_units :
'NULL');
383 $sql .=
", ".($this->size_units !=
'' ? (int) $this->size_units :
'NULL');
384 $sql .=
", ".(!empty($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
385 $sql .=
", ".(!empty($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
386 $sql .=
", ".(!empty($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
387 $sql .=
", ".(int) $this->fk_incoterms;
388 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
391 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
392 $resql = $this->db->query(
$sql);
394 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expedition");
396 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition";
397 $sql .=
" SET ref = '(PROV".$this->id.
")'";
398 $sql .=
" WHERE rowid = ".((int) $this->
id);
400 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
401 if ($this->db->query(
$sql)) {
403 $num = count($this->lines);
404 for ($i = 0; $i < $num; $i++) {
405 if (empty($this->lines[$i]->product_type) || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
406 if (!isset($this->lines[$i]->detail_batch)) {
407 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) {
411 if ($this->
create_line_batch($this->lines[$i], $this->lines[$i]->array_options) <= 0) {
418 if (!$error && $this->
id && $this->origin_id) {
433 if (!$error && !$notrigger) {
435 $result = $this->
call_trigger(
'SHIPPING_CREATE', $user);
445 foreach ($this->errors as $errmsg) {
446 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
447 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
449 $this->db->rollback();
454 $this->db->rollback();
459 $this->error = $this->db->lasterror().
" - sql=$sql";
460 $this->db->rollback();
465 $this->error = $this->db->error().
" - sql=$sql";
466 $this->db->rollback();
482 public function create_line($entrepot_id, $origin_line_id, $qty, $rang = 0, $array_options =
null)
488 $expeditionline->fk_expedition = $this->id;
489 $expeditionline->entrepot_id = $entrepot_id;
490 $expeditionline->fk_origin_line = $origin_line_id;
491 $expeditionline->qty = $qty;
492 $expeditionline->rang = $rang;
493 $expeditionline->array_options = $array_options;
495 if (($lineId = $expeditionline->insert($user)) < 0) {
496 $this->errors[] = $expeditionline->error;
514 $stockLocationQty = array();
516 $tab = $line_ext->detail_batch;
518 foreach ($tab as $detbatch) {
519 if (!empty($detbatch->entrepot_id)) {
520 if (empty($stockLocationQty[$detbatch->entrepot_id])) {
521 $stockLocationQty[$detbatch->entrepot_id] = 0;
523 $stockLocationQty[$detbatch->entrepot_id] += $detbatch->qty;
527 foreach ($stockLocationQty as $stockLocation => $qty) {
528 $line_id = $this->
create_line($stockLocation, $line_ext->origin_line_id, $qty, $line_ext->rang, $array_options);
533 foreach ($tab as $detbatch) {
534 if ($detbatch->entrepot_id == $stockLocation) {
535 if (!($detbatch->create($line_id) > 0)) {
536 $this->errors = $detbatch->errors;
560 public function fetch($id, $ref =
'', $ref_ext =
'', $notused =
'')
565 if (empty($id) && empty($ref) && empty($ref_ext)) {
569 $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";
570 $sql .=
", e.date_valid";
571 $sql .=
", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
572 $sql .=
", e.date_expedition as date_expedition, e.model_pdf, e.fk_address, e.date_delivery";
573 $sql .=
", e.fk_shipping_method, e.tracking_number";
574 $sql .=
", e.note_private, e.note_public";
575 $sql .=
', e.fk_incoterms, e.location_incoterms';
576 $sql .=
', i.libelle as label_incoterms';
577 $sql .=
', s.libelle as shipping_method';
578 $sql .=
", el.fk_source as origin_id, el.sourcetype as origin";
579 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
580 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"element_element as el ON el.fk_target = e.rowid AND el.targettype = '".$this->db->escape($this->element).
"'";
581 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON e.fk_incoterms = i.rowid';
582 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_shipment_mode as s ON e.fk_shipping_method = s.rowid';
583 $sql .=
" WHERE e.entity IN (".getEntity(
'expedition').
")";
585 $sql .=
" AND e.rowid = ".((int) $id);
588 $sql .=
" AND e.ref='".$this->db->escape($ref).
"'";
591 $sql .=
" AND e.ref_ext='".$this->db->escape($ref_ext).
"'";
594 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
595 $result = $this->db->query(
$sql);
597 if ($this->db->num_rows($result)) {
598 $obj = $this->db->fetch_object($result);
600 $this->
id = $obj->rowid;
601 $this->entity = $obj->entity;
602 $this->
ref = $obj->ref;
603 $this->socid = $obj->socid;
604 $this->ref_customer = $obj->ref_customer;
605 $this->ref_ext = $obj->ref_ext;
606 $this->status = $obj->fk_statut;
607 $this->statut = $this->status;
608 $this->user_author_id = $obj->fk_user_author;
609 $this->fk_user_author = $obj->fk_user_author;
610 $this->date_creation = $this->db->jdate($obj->date_creation);
611 $this->date_valid = $this->db->jdate($obj->date_valid);
612 $this->date = $this->db->jdate($obj->date_expedition);
613 $this->date_expedition = $this->db->jdate($obj->date_expedition);
614 $this->date_shipping = $this->db->jdate($obj->date_expedition);
615 $this->date_delivery = $this->db->jdate($obj->date_delivery);
616 $this->fk_delivery_address = $obj->fk_address;
617 $this->model_pdf = $obj->model_pdf;
618 $this->modelpdf = $obj->model_pdf;
619 $this->shipping_method_id = $obj->fk_shipping_method;
620 $this->shipping_method = $obj->shipping_method;
621 $this->tracking_number = $obj->tracking_number;
622 $this->origin = ($obj->origin ? $obj->origin :
'commande');
623 $this->origin_id = $obj->origin_id;
624 $this->billed = $obj->billed;
625 $this->fk_project = $obj->fk_project;
627 $this->trueWeight = $obj->weight;
628 $this->weight_units = $obj->weight_units;
630 $this->trueWidth = $obj->width;
631 $this->width_units = $obj->size_units;
632 $this->trueHeight = $obj->height;
633 $this->height_units = $obj->size_units;
634 $this->trueDepth = $obj->size;
635 $this->depth_units = $obj->size_units;
637 $this->note_public = $obj->note_public;
638 $this->note_private = $obj->note_private;
641 $this->trueSize = $obj->size.
"x".$obj->width.
"x".$obj->height;
642 $this->size_units = $obj->size_units;
645 $this->fk_incoterms = $obj->fk_incoterms;
646 $this->location_incoterms = $obj->location_incoterms;
647 $this->label_incoterms = $obj->label_incoterms;
649 $this->db->free($result);
651 if ($this->statut == self::STATUS_DRAFT) {
652 $this->brouillon = 1;
666 if (!empty($this->multicurrency_code)) {
667 $this->multicurrency_code = $this->thirdparty->multicurrency_code;
669 if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($this->thirdparty->multicurrency_tx)) {
670 $this->multicurrency_tx = $this->thirdparty->multicurrency_tx;
684 dol_syslog(get_class($this).
'::Fetch no expedition found', LOG_ERR);
685 $this->error =
'Delivery with id '.$id.
' not found';
689 $this->error = $this->db->error();
701 public function valid($user, $notrigger = 0)
703 global $conf, $langs;
705 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
711 dol_syslog(get_class($this).
"::valid not in draft status", LOG_WARNING);
715 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->creer))
716 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->expedition->shipping_advance->validate)))) {
717 $this->error =
'Permission denied';
718 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
728 $soc->fetch($this->socid);
731 $result = $soc->set_as_client();
734 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
737 $numref =
"EXP".$this->id;
744 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition SET";
745 $sql .=
" ref='".$this->db->escape($numref).
"'";
746 $sql .=
", fk_statut = 1";
747 $sql .=
", date_valid = '".$this->db->idate($now).
"'";
748 $sql .=
", fk_user_valid = ".$user->id;
749 $sql .=
" WHERE rowid = ".((int) $this->
id);
751 dol_syslog(get_class($this).
"::valid update expedition", LOG_DEBUG);
752 $resql = $this->db->query(
$sql);
754 $this->error = $this->db->lasterror();
759 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
772 if (!$error && !$notrigger) {
774 $result = $this->
call_trigger(
'SHIPPING_VALIDATE', $user);
782 $this->oldref = $this->ref;
785 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
787 $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).
"'";
788 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'expedition/sending/".$this->db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
789 $resql = $this->db->query(
$sql);
791 $error++; $this->error = $this->db->lasterror();
797 $dirsource = $conf->expedition->dir_output.
'/sending/'.$oldref;
798 $dirdest = $conf->expedition->dir_output.
'/sending/'.$newref;
799 if (!$error && file_exists($dirsource)) {
800 dol_syslog(get_class($this).
"::valid rename dir ".$dirsource.
" into ".$dirdest);
802 if (@rename($dirsource, $dirdest)) {
805 $listoffiles =
dol_dir_list($conf->expedition->dir_output.
'/sending/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
806 foreach ($listoffiles as $fileentry) {
807 $dirsource = $fileentry[
'name'];
808 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
809 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
810 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
811 @rename($dirsource, $dirdest);
820 $this->
ref = $numref;
828 $this->db->rollback();
847 if ($this->statut == self::STATUS_VALIDATED || $this->statut == self::STATUS_CLOSED) {
849 include_once DOL_DOCUMENT_ROOT.
'/delivery/class/delivery.class.php';
850 $delivery =
new Delivery($this->db);
851 $result = $delivery->create_from_sending($user, $this->
id);
855 $this->error = $delivery->error;
878 public function addline($entrepot_id, $id, $qty, $array_options = 0)
880 global $conf, $langs;
882 $num = count($this->lines);
885 $line->entrepot_id = $entrepot_id;
886 $line->origin_line_id = $id;
887 $line->fk_origin_line = $id;
891 $orderline->fetch($id);
894 $line->rang = $orderline->rang;
895 $line->product_type = $orderline->product_type;
897 if (
isModEnabled(
'stock') && !empty($orderline->fk_product)) {
898 $fk_product = $orderline->fk_product;
900 if (!($entrepot_id > 0) && empty($conf->global->STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS)) {
901 $langs->load(
"errors");
902 $this->error = $langs->trans(
"ErrorWarehouseRequiredIntoShipmentLine");
906 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT)) {
907 $product =
new Product($this->db);
908 $product->fetch($fk_product);
911 if ($entrepot_id > 0) {
912 $product->load_stock(
'warehouseopen');
913 $product_stock = $product->stock_warehouse[$entrepot_id]->real;
915 $product_stock = $product->stock_reel;
918 $product_type = $product->type;
919 if ($product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
920 $isavirtualproduct = ($product->hasFatherOrChild(1) > 0);
922 if (!$isavirtualproduct || empty($conf->global->PRODUIT_SOUSPRODUITS) || ($isavirtualproduct && empty($conf->global->STOCK_EXCLUDE_VIRTUAL_PRODUCTS))) {
923 if ($product_stock < $qty) {
924 $langs->load(
"errors");
925 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnShipment', $product->ref);
926 $this->errorhidden =
'ErrorStockIsNotEnoughToAddProductOnShipment';
928 $this->db->rollback();
938 if (
isModEnabled(
'productbatch') && !empty($orderline->fk_product) && !empty($orderline->product_tobatch)) {
939 $this->error =
'ADDLINE_WAS_CALLED_INSTEAD_OF_ADDLINEBATCH '.$orderline->id.
' '.$orderline->fk_product;
944 if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) {
945 $line->array_options = $array_options;
948 $this->lines[$num] = $line;
964 global $conf, $langs;
966 $num = count($this->lines);
967 if ($dbatch[
'qty'] > 0 || ($dbatch[
'qty'] == 0 &&
getDolGlobalString(
'SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
970 foreach ($dbatch[
'detail'] as $key => $value) {
971 if ($value[
'q'] > 0 || ($value[
'q'] == 0 &&
getDolGlobalString(
'SHIPMENT_GETS_ALL_ORDER_PRODUCTS'))) {
977 $ret = $linebatch->fetchFromStock($value[
'id_batch']);
979 $this->error = $linebatch->error;
982 $linebatch->qty = $value[
'q'];
984 $linebatch->batch =
null;
989 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
991 $prod_batch->fetch($value[
'id_batch']);
993 if ($prod_batch->qty < $linebatch->qty) {
994 $langs->load(
"errors");
995 $this->errors[] = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnShipment', $prod_batch->fk_product);
996 dol_syslog(get_class($this).
"::addline_batch error=Product ".$prod_batch->batch.
": ".$this->errorsToString(), LOG_ERR);
997 $this->db->rollback();
1005 $line->entrepot_id = $linebatch->entrepot_id;
1006 $line->origin_line_id = $dbatch[
'ix_l'];
1007 $line->fk_origin_line = $dbatch[
'ix_l'];
1008 $line->qty = $dbatch[
'qty'];
1009 $line->detail_batch = $tab;
1012 if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options) > 0) {
1013 $line->array_options = $array_options;
1017 $this->lines[$num] = $line;
1029 public function update($user =
null, $notrigger = 0)
1036 if (isset($this->
ref)) {
1037 $this->
ref = trim($this->
ref);
1039 if (isset($this->entity)) {
1040 $this->entity = (int) $this->entity;
1042 if (isset($this->ref_customer)) {
1043 $this->ref_customer = trim($this->ref_customer);
1045 if (isset($this->socid)) {
1046 $this->socid = (int) $this->socid;
1048 if (isset($this->fk_user_author)) {
1049 $this->fk_user_author = (int) $this->fk_user_author;
1051 if (isset($this->fk_user_valid)) {
1052 $this->fk_user_valid = (int) $this->fk_user_valid;
1054 if (isset($this->fk_delivery_address)) {
1055 $this->fk_delivery_address = (int) $this->fk_delivery_address;
1057 if (isset($this->shipping_method_id)) {
1058 $this->shipping_method_id = (int) $this->shipping_method_id;
1060 if (isset($this->tracking_number)) {
1061 $this->tracking_number = trim($this->tracking_number);
1063 if (isset($this->statut)) {
1064 $this->statut = (int) $this->statut;
1066 if (isset($this->trueDepth)) {
1067 $this->trueDepth = trim($this->trueDepth);
1069 if (isset($this->trueWidth)) {
1070 $this->trueWidth = trim($this->trueWidth);
1072 if (isset($this->trueHeight)) {
1073 $this->trueHeight = trim($this->trueHeight);
1075 if (isset($this->size_units)) {
1076 $this->size_units = trim($this->size_units);
1078 if (isset($this->weight_units)) {
1079 $this->weight_units = trim($this->weight_units);
1081 if (isset($this->trueWeight)) {
1082 $this->weight = trim($this->trueWeight);
1084 if (isset($this->note_private)) {
1085 $this->note_private = trim($this->note_private);
1087 if (isset($this->note_public)) {
1088 $this->note_public = trim($this->note_public);
1090 if (isset($this->model_pdf)) {
1091 $this->model_pdf = trim($this->model_pdf);
1100 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition SET";
1102 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1103 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1104 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1105 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1106 $sql .=
" date_creation=".(dol_strlen($this->date_creation) != 0 ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
1107 $sql .=
" fk_user_author=".(isset($this->fk_user_author) ? $this->fk_user_author :
"null").
",";
1108 $sql .=
" date_valid=".(dol_strlen($this->date_valid) != 0 ?
"'".$this->db->idate($this->date_valid).
"'" :
'null').
",";
1109 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->fk_user_valid :
"null").
",";
1110 $sql .=
" date_expedition=".(dol_strlen($this->date_expedition) != 0 ?
"'".$this->db->idate($this->date_expedition).
"'" :
'null').
",";
1111 $sql .=
" date_delivery=".(dol_strlen($this->date_delivery) != 0 ?
"'".$this->db->idate($this->date_delivery).
"'" :
'null').
",";
1112 $sql .=
" fk_address=".(isset($this->fk_delivery_address) ? $this->fk_delivery_address :
"null").
",";
1113 $sql .=
" fk_shipping_method=".((isset($this->shipping_method_id) && $this->shipping_method_id > 0) ? $this->shipping_method_id :
"null").
",";
1114 $sql .=
" tracking_number=".(isset($this->tracking_number) ?
"'".$this->db->escape($this->tracking_number).
"'" :
"null").
",";
1115 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1116 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1117 $sql .=
" height=".(($this->trueHeight !=
'') ? $this->trueHeight :
"null").
",";
1118 $sql .=
" width=".(($this->trueWidth !=
'') ? $this->trueWidth :
"null").
",";
1119 $sql .=
" size_units=".(isset($this->size_units) ? $this->size_units :
"null").
",";
1120 $sql .=
" size=".(($this->trueDepth !=
'') ? $this->trueDepth :
"null").
",";
1121 $sql .=
" weight_units=".(isset($this->weight_units) ? $this->weight_units :
"null").
",";
1122 $sql .=
" weight=".(($this->trueWeight !=
'') ? $this->trueWeight :
"null").
",";
1123 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1124 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1125 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1126 $sql .=
" entity=".$conf->entity;
1128 $sql .=
" WHERE rowid=".((int) $this->
id);
1132 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1133 $resql = $this->db->query(
$sql);
1135 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1138 if (!$error && !$notrigger) {
1140 $result = $this->
call_trigger(
'SHIPPING_MODIFY', $user);
1149 foreach ($this->errors as $errmsg) {
1150 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1151 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1153 $this->db->rollback();
1156 $this->db->commit();
1169 public function cancel($notrigger = 0, $also_update_stock =
false)
1171 global $conf, $langs, $user;
1173 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1182 if (count($this->linkedObjectsIds) > 0) {
1183 $this->error =
'ErrorThereIsSomeDeliveries';
1187 if (!$error && !$notrigger) {
1189 $result = $this->
call_trigger(
'SHIPPING_CANCEL', $user);
1198 (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1199 ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1200 require_once DOL_DOCUMENT_ROOT.
"/product/stock/class/mouvementstock.class.php";
1202 $langs->load(
"agenda");
1205 $sql =
"SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1206 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
1207 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
1208 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1209 $sql .=
" AND cd.rowid = ed.fk_origin_line";
1211 dol_syslog(get_class($this).
"::delete select details", LOG_DEBUG);
1212 $resql = $this->db->query(
$sql);
1214 $cpt = $this->db->num_rows($resql);
1218 for ($i = 0; $i < $cpt; $i++) {
1219 dol_syslog(get_class($this).
"::delete movement index ".$i);
1220 $obj = $this->db->fetch_object($resql);
1224 $mouvS->origin =
null;
1228 $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1229 if (!is_array($lotArray)) {
1231 $this->errors[] =
"Error ".$this->db->lasterror();
1235 if (empty($lotArray)) {
1239 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans(
"ShipmentCanceledInDolibarr", $this->ref));
1242 $this->errors = array_merge($this->errors, $mouvS->errors);
1248 foreach ($lotArray as $lot) {
1249 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans(
"ShipmentCanceledInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch);
1252 $this->errors = array_merge($this->errors, $mouvS->errors);
1262 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1269 if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1270 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1276 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
1277 $sql .=
" WHERE fk_expedition = ".((int) $this->
id);
1279 if ($this->db->query(
$sql)) {
1288 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"expedition";
1289 $sql .=
" WHERE rowid = ".((int) $this->
id);
1291 if ($this->db->query(
$sql)) {
1292 if (!empty($this->origin) && $this->origin_id > 0) {
1294 $origin = $this->origin;
1297 $this->$origin->loadExpeditions();
1299 if (count($this->$origin->expeditions) <= 0) {
1306 $this->db->commit();
1310 if (!empty($conf->expedition->dir_output)) {
1311 $dir = $conf->expedition->dir_output.
'/sending/'.$ref;
1312 $file = $dir.
'/'.$ref.
'.pdf';
1313 if (file_exists($file)) {
1318 if (file_exists($dir)) {
1320 $this->error = $langs->trans(
"ErrorCanNotDeleteDir", $dir);
1328 $this->db->rollback();
1332 $this->error = $this->db->lasterror().
" - sql=$sql";
1333 $this->db->rollback();
1337 $this->error = $this->db->lasterror().
" - sql=$sql";
1338 $this->db->rollback();
1342 $this->error = $this->db->lasterror().
" - sql=$sql";
1343 $this->db->rollback();
1347 $this->db->rollback();
1360 public function delete($notrigger = 0, $also_update_stock =
false)
1362 global $conf, $langs, $user;
1364 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1373 if (count($this->linkedObjectsIds) > 0) {
1374 $this->error =
'ErrorThereIsSomeDeliveries';
1378 if (!$error && !$notrigger) {
1380 $result = $this->
call_trigger(
'SHIPPING_DELETE', $user);
1389 (($conf->global->STOCK_CALCULATE_ON_SHIPMENT && $this->statut > self::STATUS_DRAFT) ||
1390 ($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE && $this->statut == self::STATUS_CLOSED && $also_update_stock))) {
1391 require_once DOL_DOCUMENT_ROOT.
"/product/stock/class/mouvementstock.class.php";
1393 $langs->load(
"agenda");
1399 $sql =
"SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id";
1400 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
1401 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
1402 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1403 $sql .=
" AND cd.rowid = ed.fk_origin_line";
1405 dol_syslog(get_class($this).
"::delete select details", LOG_DEBUG);
1406 $resql = $this->db->query(
$sql);
1408 $cpt = $this->db->num_rows($resql);
1409 for ($i = 0; $i < $cpt; $i++) {
1410 dol_syslog(get_class($this).
"::delete movement index ".$i);
1411 $obj = $this->db->fetch_object($resql);
1415 $mouvS->origin =
null;
1417 $lotArray = $shipmentlinebatch->fetchAll($obj->expeditiondet_id);
1418 if (!is_array($lotArray)) {
1419 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1421 if (empty($lotArray)) {
1425 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty, 0, $langs->trans(
"ShipmentDeletedInDolibarr", $this->ref));
1428 $this->errors = array_merge($this->errors, $mouvS->errors);
1434 foreach ($lotArray as $lot) {
1435 $result = $mouvS->reception($user, $obj->fk_product, $obj->fk_entrepot, $lot->qty, 0, $langs->trans(
"ShipmentDeletedInDolibarr", $this->ref), $lot->eatby, $lot->sellby, $lot->batch);
1438 $this->errors = array_merge($this->errors, $mouvS->errors);
1448 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1455 if ($shipmentlinebatch->deleteFromShipment($this->id) < 0) {
1456 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1461 $main = MAIN_DB_PREFIX.
'expeditiondet';
1462 $ef = $main.
"_extrafields";
1463 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_expedition = ".((int) $this->
id).
")";
1465 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
1466 $sql .=
" WHERE fk_expedition = ".((int) $this->
id);
1468 if ($this->db->query($sqlef) && $this->db->query(
$sql)) {
1482 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expedition";
1483 $sql .=
" WHERE rowid = ".((int) $this->
id);
1485 if ($this->db->query(
$sql)) {
1486 if (!empty($this->origin) && $this->origin_id > 0) {
1488 $origin = $this->origin;
1491 $this->$origin->loadExpeditions();
1493 if (count($this->$origin->expeditions) <= 0) {
1500 $this->db->commit();
1507 if (!empty($conf->expedition->dir_output)) {
1508 $dir = $conf->expedition->dir_output.
'/sending/'.$ref;
1509 $file = $dir.
'/'.$ref.
'.pdf';
1510 if (file_exists($file)) {
1515 if (file_exists($dir)) {
1517 $this->error = $langs->trans(
"ErrorCanNotDeleteDir", $dir);
1525 $this->db->rollback();
1529 $this->error = $this->db->lasterror().
" - sql=$sql";
1530 $this->db->rollback();
1534 $this->error = $this->db->lasterror().
" - sql=$sql";
1535 $this->db->rollback();
1539 $this->error = $this->db->lasterror().
" - sql=$sql";
1540 $this->db->rollback();
1544 $this->db->rollback();
1558 global $conf, $mysoc;
1560 $this->lines = array();
1565 $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";
1566 $sql .=
", cd.total_ht, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.total_tva";
1567 $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";
1568 $sql .=
", cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc, cd.rang";
1569 $sql .=
", ed.rowid as line_id, ed.qty as qty_shipped, ed.fk_origin_line, ed.fk_entrepot";
1570 $sql .=
", p.ref as product_ref, p.label as product_label, p.fk_product_type";
1571 $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";
1572 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet as ed, ".MAIN_DB_PREFIX.
"commandedet as cd";
1573 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = cd.fk_product";
1574 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
1575 $sql .=
" AND ed.fk_origin_line = cd.rowid";
1576 $sql .=
" ORDER BY cd.rang, ed.fk_origin_line";
1578 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1579 $resql = $this->db->query(
$sql);
1581 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1583 $num = $this->db->num_rows($resql);
1588 $this->total_ht = 0;
1589 $this->total_tva = 0;
1590 $this->total_ttc = 0;
1591 $this->total_localtax1 = 0;
1592 $this->total_localtax2 = 0;
1597 $obj = $this->db->fetch_object($resql);
1599 if ($originline > 0 && $originline == $obj->fk_origin_line) {
1600 $line->entrepot_id = 0;
1601 $line->qty_shipped += $obj->qty_shipped;
1604 $line->entrepot_id = $obj->fk_entrepot;
1605 $line->qty_shipped = $obj->qty_shipped;
1608 $detail_entrepot =
new stdClass();
1609 $detail_entrepot->entrepot_id = $obj->fk_entrepot;
1610 $detail_entrepot->qty_shipped = $obj->qty_shipped;
1611 $detail_entrepot->line_id = $obj->line_id;
1612 $line->details_entrepot[] = $detail_entrepot;
1614 $line->line_id = $obj->line_id;
1615 $line->rowid = $obj->line_id;
1616 $line->id = $obj->line_id;
1618 $line->fk_origin =
'orderline';
1619 $line->fk_origin_line = $obj->fk_origin_line;
1620 $line->origin_line_id = $obj->fk_origin_line;
1622 $line->fk_expedition = $this->id;
1624 $line->product_type = $obj->product_type;
1625 $line->fk_product = $obj->fk_product;
1626 $line->fk_product_type = $obj->fk_product_type;
1627 $line->ref = $obj->product_ref;
1628 $line->product_ref = $obj->product_ref;
1629 $line->product_label = $obj->product_label;
1630 $line->libelle = $obj->product_label;
1631 $line->product_tosell = $obj->product_tosell;
1632 $line->product_tobuy = $obj->product_tobuy;
1633 $line->product_tobatch = $obj->product_tobatch;
1634 $line->label = $obj->custom_label;
1635 $line->description = $obj->description;
1636 $line->qty_asked = $obj->qty_asked;
1637 $line->rang = $obj->rang;
1638 $line->weight = $obj->weight;
1639 $line->weight_units = $obj->weight_units;
1640 $line->length = $obj->length;
1641 $line->length_units = $obj->length_units;
1642 $line->surface = $obj->surface;
1643 $line->surface_units = $obj->surface_units;
1644 $line->volume = $obj->volume;
1645 $line->volume_units = $obj->volume_units;
1646 $line->fk_unit = $obj->fk_unit;
1648 $line->pa_ht = $obj->pa_ht;
1651 $localtax_array = array(0=>$obj->localtax1_type, 1=>$obj->localtax1_tx, 2=>$obj->localtax2_type, 3=>$obj->localtax2_tx);
1652 $localtax1_tx =
get_localtax($obj->tva_tx, 1, $this->thirdparty);
1653 $localtax2_tx =
get_localtax($obj->tva_tx, 2, $this->thirdparty);
1656 $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);
1657 $line->desc = $obj->description;
1658 $line->qty = $line->qty_shipped;
1659 $line->total_ht = $tabprice[0];
1660 $line->total_localtax1 = $tabprice[9];
1661 $line->total_localtax2 = $tabprice[10];
1662 $line->total_ttc = $tabprice[2];
1663 $line->total_tva = $tabprice[1];
1664 $line->vat_src_code = $obj->vat_src_code;
1665 $line->tva_tx = $obj->tva_tx;
1666 $line->localtax1_tx = $obj->localtax1_tx;
1667 $line->localtax2_tx = $obj->localtax2_tx;
1668 $line->info_bits = $obj->info_bits;
1669 $line->price = $obj->price;
1670 $line->subprice = $obj->subprice;
1671 $line->remise_percent = $obj->remise_percent;
1673 $this->total_ht += $tabprice[0];
1674 $this->total_tva += $tabprice[1];
1675 $this->total_ttc += $tabprice[2];
1676 $this->total_localtax1 += $tabprice[9];
1677 $this->total_localtax2 += $tabprice[10];
1680 $this->fk_multicurrency = $obj->fk_multicurrency;
1681 $this->multicurrency_code = $obj->multicurrency_code;
1682 $this->multicurrency_subprice = $obj->multicurrency_subprice;
1683 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1684 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1685 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1687 if ($originline != $obj->fk_origin_line) {
1688 $line->detail_batch = array();
1692 if (
isModEnabled(
'productbatch') && $obj->line_id > 0 && $obj->product_tobatch > 0) {
1693 $newdetailbatch = $shipmentlinebatch->fetchAll($obj->line_id, $obj->fk_product);
1695 if (is_array($newdetailbatch)) {
1696 if ($originline != $obj->fk_origin_line) {
1697 $line->detail_batch = $newdetailbatch;
1699 $line->detail_batch = array_merge($line->detail_batch, $newdetailbatch);
1704 $line->fetch_optionals();
1706 if ($originline != $obj->fk_origin_line) {
1707 $this->lines[$lineindex] = $line;
1710 $line->total_ht += $tabprice[0];
1711 $line->total_localtax1 += $tabprice[9];
1712 $line->total_localtax2 += $tabprice[10];
1713 $line->total_ttc += $tabprice[2];
1714 $line->total_tva += $tabprice[1];
1718 $originline = $obj->fk_origin_line;
1720 $this->db->free($resql);
1723 $this->error = $this->db->error();
1739 if ($this->statut == self::STATUS_DRAFT) {
1745 $line->fetch($lineid);
1747 if ($line->delete($user) > 0) {
1750 $this->db->commit();
1753 $this->db->rollback();
1757 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1772 global $conf, $langs;
1774 $langs->load(
'sendings');
1776 $nofetch = !empty($params[
'nofetch']);
1779 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Shipment").
'</u>';
1780 if (isset($this->statut)) {
1781 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
1783 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1784 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.($this->ref_customer ? $this->ref_customer : $this->ref_client);
1786 $langs->load(
'companies');
1787 if (empty($this->thirdparty)) {
1790 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
1807 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1)
1809 global $langs, $conf, $hookmanager;
1814 'objecttype' => $this->element,
1815 'option' => $option,
1818 $classfortooltip =
'classfortooltip';
1821 $classfortooltip =
'classforajaxtooltip';
1822 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1828 $url = DOL_URL_ROOT.
'/expedition/card.php?id='.$this->id;
1834 if ($option !==
'nolink') {
1836 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1837 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1838 $add_save_lastsearch_values = 1;
1840 if ($add_save_lastsearch_values) {
1841 $url .=
'&save_lastsearch_values=1';
1846 if (empty($notooltip)) {
1847 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1848 $label = $langs->trans(
"Shipment");
1849 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1851 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1852 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
1855 $linkstart =
'<a href="'.$url.
'"';
1856 $linkstart .= $linkclose.
'>';
1859 $result .= $linkstart;
1861 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
1863 if ($withpicto != 2) {
1864 $result .= $this->ref;
1866 $result .= $linkend;
1868 $hookmanager->initHooks(array($this->element .
'dao'));
1869 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1870 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1872 $result = $hookmanager->resPrint;
1874 $result .= $hookmanager->resPrint;
1887 return $this->
LibStatut($this->statut, $mode);
1903 $labelStatus = $langs->transnoentitiesnoconv($this->statuts[$status]);
1904 $labelStatusShort = $langs->transnoentitiesnoconv($this->statuts_short[$status]);
1906 $statusType =
'status'.$status;
1907 if ($status == self::STATUS_VALIDATED) {
1908 $statusType =
'status4';
1910 if ($status == self::STATUS_CLOSED) {
1911 $statusType =
'status6';
1913 if ($status == self::STATUS_CANCELED) {
1914 $statusType =
'status9';
1917 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
1933 dol_syslog(get_class($this).
"::initAsSpecimen");
1936 $order->initAsSpecimen();
1940 $this->
ref =
'SPECIMEN';
1941 $this->specimen = 1;
1943 $this->livraison_id = 0;
1945 $this->date_creation = $now;
1946 $this->date_valid = $now;
1947 $this->date_delivery = $now + 24 * 3600;
1948 $this->date_expedition = $now + 24 * 3600;
1950 $this->entrepot_id = 0;
1951 $this->fk_delivery_address = 0;
1954 $this->commande_id = 0;
1955 $this->commande = $order;
1957 $this->origin_id = 1;
1958 $this->origin =
'commande';
1960 $this->note_private =
'Private note';
1961 $this->note_public =
'Public note';
1965 while ($xnbp < $nbp) {
1967 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
1968 $line->libelle = $langs->trans(
"Description").
" ".$xnbp;
1969 $line->label = $langs->trans(
"Description").
" ".$xnbp;
1971 $line->qty_asked = 5;
1972 $line->qty_shipped = 4;
1973 $line->fk_product = $this->commande->lines[$xnbp]->fk_product;
1975 $this->lines[] = $line;
2004 if ($user->rights->expedition->creer) {
2005 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"expedition";
2006 $sql .=
" SET date_delivery = ".($delivery_date ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2007 $sql .=
" WHERE rowid = ".((int) $this->
id);
2009 dol_syslog(get_class($this).
"::setDeliveryDate", LOG_DEBUG);
2010 $resql = $this->db->query(
$sql);
2012 $this->date_delivery = $delivery_date;
2015 $this->error = $this->db->error();
2033 $this->meths = array();
2035 $sql =
"SELECT em.rowid, em.code, em.libelle as label";
2036 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2037 $sql .=
" WHERE em.active = 1";
2038 $sql .=
" ORDER BY em.libelle ASC";
2040 $resql = $this->db->query(
$sql);
2042 while ($obj = $this->db->fetch_object($resql)) {
2043 $label = $langs->trans(
'SendingMethod'.$obj->code);
2044 $this->meths[$obj->rowid] = ($label !=
'SendingMethod'.$obj->code ? $label : $obj->label);
2061 $this->listmeths = array();
2064 $sql =
"SELECT em.rowid, em.code, em.libelle as label, em.description, em.tracking, em.active";
2065 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2067 $sql .=
" WHERE em.rowid=".((int) $id);
2070 $resql = $this->db->query(
$sql);
2072 while ($obj = $this->db->fetch_object($resql)) {
2073 $this->listmeths[$i][
'rowid'] = $obj->rowid;
2074 $this->listmeths[$i][
'code'] = $obj->code;
2075 $label = $langs->trans(
'SendingMethod'.$obj->code);
2076 $this->listmeths[$i][
'libelle'] = ($label !=
'SendingMethod'.$obj->code ? $label : $obj->label);
2077 $this->listmeths[$i][
'description'] = $obj->description;
2078 $this->listmeths[$i][
'tracking'] = $obj->tracking;
2079 $this->listmeths[$i][
'active'] = $obj->active;
2093 if (!empty($this->shipping_method_id)) {
2094 $sql =
"SELECT em.code, em.tracking";
2095 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_shipment_mode as em";
2096 $sql .=
" WHERE em.rowid = ".((int) $this->shipping_method_id);
2098 $resql = $this->db->query(
$sql);
2100 if ($obj = $this->db->fetch_object($resql)) {
2101 $tracking = $obj->tracking;
2106 if (!empty($tracking) && !empty($value)) {
2107 $url = str_replace(
'{TRACKID}', $value, $tracking);
2108 $this->tracking_url = sprintf(
'<a target="_blank" rel="noopener noreferrer" href="%s">'.($value ? $value :
'url').
'</a>', $url, $url);
2110 $this->tracking_url = $value;
2121 global $conf, $langs, $user;
2126 if ($this->statut == self::STATUS_CLOSED) {
2133 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut > 0";
2135 $resql = $this->db->query(
$sql);
2138 if ($this->origin ==
'commande' && $this->origin_id > 0) {
2140 $order->fetch($this->origin_id);
2142 $order->loadExpeditions(self::STATUS_CLOSED);
2144 $shipments_match_order = 1;
2145 foreach ($order->lines as $line) {
2146 $lineid = $line->id;
2148 if (($line->product_type == 0 || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) && $order->expeditions[$lineid] != $qty) {
2149 $shipments_match_order = 0;
2150 $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';
2155 if ($shipments_match_order) {
2156 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');
2158 $order->cloture($user);
2166 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2175 $result = $this->
call_trigger(
'SHIPPING_CLOSED', $user);
2186 $this->db->commit();
2192 $this->db->rollback();
2210 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/mouvementstock.class.php';
2212 $langs->load(
"agenda");
2215 $sql =
"SELECT cd.fk_product, cd.subprice,";
2216 $sql .=
" ed.rowid, ed.qty, ed.fk_entrepot,";
2218 $sql .=
" edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock,";
2219 $sql .=
" cd.rowid as cdid, ed.rowid as edid";
2220 $sql .=
" FROM " . MAIN_DB_PREFIX .
"commandedet as cd,";
2221 $sql .=
" " . MAIN_DB_PREFIX .
"expeditiondet as ed";
2222 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2223 $sql .=
" INNER JOIN " . MAIN_DB_PREFIX .
"expedition as e ON ed.fk_expedition = e.rowid";
2224 $sql .=
" WHERE ed.fk_expedition = " . ((int) $this->
id);
2225 $sql .=
" AND cd.rowid = ed.fk_origin_line";
2227 dol_syslog(get_class($this) .
"::valid select details", LOG_DEBUG);
2228 $resql = $this->db->query(
$sql);
2230 $cpt = $this->db->num_rows($resql);
2231 for ($i = 0; $i < $cpt; $i++) {
2232 $obj = $this->db->fetch_object($resql);
2233 if (empty($obj->edbrowid)) {
2236 $qty = $obj->edbqty;
2238 if ($qty <= 0 || ($qty < 0 && !
getDolGlobalInt(
'SHIPMENT_ALLOW_NEGATIVE_QTY'))) {
2241 dol_syslog(get_class($this) .
"::valid movement index " . $i .
" ed.rowid=" . $obj->rowid .
" edb.rowid=" . $obj->edbrowid);
2244 $mouvS->origin = &$this;
2245 $mouvS->setOrigin($this->element, $this->
id, $obj->cdid, $obj->edid);
2247 if (empty($obj->edbrowid)) {
2251 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $qty, $obj->subprice, $langs->trans(
"ShipmentClassifyClosedInDolibarr", $obj->ref));
2253 $this->error = $mouvS->error;
2254 $this->errors = $mouvS->errors;
2262 $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);
2264 $this->error = $mouvS->error;
2265 $this->errors = $mouvS->errors;
2273 $sqldelete =
"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)";
2274 $resqldelete = $this->db->query($sqldelete);
2278 $this->error = $this->db->lasterror();
2279 $this->errors[] = $this->db->lasterror();
2298 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'expedition SET fk_statut=2, billed=1';
2299 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > 0';
2301 $resql = $this->db->query(
$sql);
2307 $result = $this->
call_trigger(
'SHIPPING_BILLED', $user);
2313 $this->errors[] = $this->db->lasterror;
2316 if (empty($error)) {
2317 $this->db->commit();
2322 $this->db->rollback();
2337 if ($this->statut <= self::STATUS_DRAFT) {
2341 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'SHIPMENT_UNVALIDATE');
2351 global $conf, $langs, $user;
2356 if ($this->statut == self::STATUS_VALIDATED) {
2362 $oldbilled = $this->billed;
2364 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'expedition SET fk_statut = 1';
2365 $sql .=
" WHERE rowid = ".((int) $this->
id).
' AND fk_statut > 0';
2367 $resql = $this->db->query(
$sql);
2373 if (!$error &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
2374 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2376 $langs->load(
"agenda");
2380 $sql =
"SELECT cd.fk_product, cd.subprice,";
2381 $sql .=
" ed.rowid, ed.qty, ed.fk_entrepot,";
2382 $sql .=
" edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock";
2383 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commandedet as cd,";
2384 $sql .=
" ".MAIN_DB_PREFIX.
"expeditiondet as ed";
2385 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid";
2386 $sql .=
" WHERE ed.fk_expedition = ".((int) $this->
id);
2387 $sql .=
" AND cd.rowid = ed.fk_origin_line";
2389 dol_syslog(get_class($this).
"::valid select details", LOG_DEBUG);
2390 $resql = $this->db->query(
$sql);
2392 $cpt = $this->db->num_rows($resql);
2393 for ($i = 0; $i < $cpt; $i++) {
2394 $obj = $this->db->fetch_object($resql);
2395 if (empty($obj->edbrowid)) {
2398 $qty = $obj->edbqty;
2403 dol_syslog(get_class($this).
"::reopen expedition movement index ".$i.
" ed.rowid=".$obj->rowid.
" edb.rowid=".$obj->edbrowid);
2407 $mouvS->origin = &$this;
2408 $mouvS->setOrigin($this->element, $this->
id);
2410 if (empty($obj->edbrowid)) {
2414 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans(
"ShipmentUnClassifyCloseddInDolibarr", $this->ref));
2416 $this->error = $mouvS->error;
2417 $this->errors = $mouvS->errors;
2425 $result = $mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, -$qty, $obj->subprice, $langs->trans(
"ShipmentUnClassifyCloseddInDolibarr", $this->ref),
'', $this->db->jdate($obj->eatby), $this->db->jdate($obj->sellby), $obj->batch, $obj->fk_origin_stock);
2427 $this->error = $mouvS->error;
2428 $this->errors = $mouvS->errors;
2435 $this->error = $this->db->lasterror();
2442 $result = $this->
call_trigger(
'SHIPPING_REOPEN', $user);
2449 $this->errors[] = $this->db->lasterror();
2453 $this->db->commit();
2457 $this->billed = $oldbilled;
2458 $this->db->rollback();
2474 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2478 $outputlangs->load(
"products");
2483 if (!empty($this->model_pdf)) {
2484 $modele = $this->model_pdf;
2485 } elseif (!empty($this->modelpdf)) {
2486 $modele = $this->modelpdf;
2487 } elseif (!empty($conf->global->EXPEDITION_ADDON_PDF)) {
2488 $modele = $conf->global->EXPEDITION_ADDON_PDF;
2492 $modelpath =
"core/modules/expedition/doc/";
2496 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2526 public $element =
'expeditiondet';
2531 public $table_element =
'expeditiondet';
2558 public $fk_origin_line;
2563 public $fk_expedition;
2578 public $qty_shipped;
2587 public $detail_batch;
2591 public $details_entrepot;
2597 public $entrepot_id;
2614 public $product_ref;
2625 public $product_label;
2637 public $product_desc;
2643 public $product_type = 0;
2654 public $weight_units;
2660 public $length_units;
2666 public $surface_units;
2672 public $volume_units;
2675 public $remise_percent;
2696 public $total_localtax1;
2701 public $total_localtax2;
2722 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang';
2723 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
2724 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
2725 $result = $this->db->query(
$sql);
2727 $objp = $this->db->fetch_object($result);
2728 $this->
id = $objp->rowid;
2729 $this->fk_expedition = $objp->fk_expedition;
2730 $this->entrepot_id = $objp->fk_entrepot;
2731 $this->fk_origin_line = $objp->fk_origin_line;
2732 $this->qty = $objp->qty;
2733 $this->rang = $objp->rang;
2735 $this->db->free($result);
2739 $this->errors[] = $this->db->lasterror();
2740 $this->error = $this->db->lasterror();
2752 public function insert($user, $notrigger = 0)
2754 global $langs, $conf;
2759 if (empty($this->fk_expedition) || empty($this->fk_origin_line) || !is_numeric($this->qty)) {
2760 $this->error =
'ErrorMandatoryParametersNotProvided';
2766 if (empty($this->rang)) {
2771 $ranktouse = $this->rang;
2772 if ($ranktouse == -1) {
2773 $rangmax = $this->
line_max($this->fk_expedition);
2774 $ranktouse = $rangmax + 1;
2777 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
2778 $sql .=
"fk_expedition";
2779 $sql .=
", fk_entrepot";
2780 $sql .=
", fk_origin_line";
2783 $sql .=
") VALUES (";
2784 $sql .= $this->fk_expedition;
2785 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : $this->entrepot_id);
2786 $sql .=
", ".((int) $this->fk_origin_line);
2787 $sql .=
", ".price2num($this->qty,
'MS');
2788 $sql .=
", ".((int) $ranktouse);
2791 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
2792 $resql = $this->db->query(
$sql);
2794 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
2803 if (!$error && !$notrigger) {
2805 $result = $this->
call_trigger(
'LINESHIPPING_INSERT', $user);
2813 foreach ($this->errors as $errmsg) {
2814 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
2815 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2823 $this->db->rollback();
2826 $this->db->commit();
2838 public function delete($user =
null, $notrigger = 0)
2848 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
2849 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
2851 if (!$this->db->query(
$sql)) {
2852 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
2857 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
2858 $sql .=
" WHERE rowid = ".((int) $this->
id);
2860 if (!$error && $this->db->query(
$sql)) {
2865 $this->errors[] = $this->error;
2869 if (!$error && !$notrigger) {
2871 $result = $this->
call_trigger(
'LINESHIPPING_DELETE', $user);
2873 $this->errors[] = $this->error;
2879 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
2884 $this->db->commit();
2887 foreach ($this->errors as $errmsg) {
2888 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
2889 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2891 $this->db->rollback();
2903 public function update($user =
null, $notrigger = 0)
2909 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
2914 if (empty($this->qty)) {
2921 $expedition_batch_id =
null;
2922 if (is_array($this->detail_batch)) {
2923 if (count($this->detail_batch) > 1) {
2924 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
2925 $this->errors[] =
'ErrorBadParameters';
2928 $batch = $this->detail_batch[0]->batch;
2929 $batch_id = $this->detail_batch[0]->fk_origin_stock;
2930 $expedition_batch_id = $this->detail_batch[0]->id;
2931 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
2932 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
2933 $this->errors[] =
'ErrorBadParameters';
2936 $qty =
price2num($this->detail_batch[0]->qty);
2938 } elseif (!empty($this->detail_batch)) {
2939 $batch = $this->detail_batch->batch;
2940 $batch_id = $this->detail_batch->fk_origin_stock;
2941 $expedition_batch_id = $this->detail_batch->id;
2942 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
2943 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
2944 $this->errors[] =
'ErrorBadParameters';
2947 $qty =
price2num($this->detail_batch->qty);
2951 if (!isset($this->
id) || !isset($this->entrepot_id)) {
2952 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
2953 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
2961 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch");
2963 if (empty($batch_id) || empty($this->fk_product)) {
2964 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
2965 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
2972 if (!$error && ($lotArray = $shipmentlinebatch->fetchAll($this->id)) < 0) {
2973 $this->errors[] = $this->db->lasterror().
" - ExpeditionLineBatch::fetchAll";
2977 foreach ($lotArray as $lot) {
2978 if ($expedition_batch_id != $lot->id) {
2979 $remainingQty += $lot->qty;
2982 $qty += $remainingQty;
2987 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
2989 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
2990 $this->errors[] = $lot->errors;
2993 if (!$error && !empty($expedition_batch_id)) {
2995 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
2996 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
2997 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
2999 if (!$this->db->query(
$sql)) {
3000 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
3004 if (!$error && $this->detail_batch->qty > 0) {
3006 if (isset($lot->id)) {
3008 $shipmentLot->batch = $lot->batch;
3009 $shipmentLot->eatby = $lot->eatby;
3010 $shipmentLot->sellby = $lot->sellby;
3011 $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id;
3012 $shipmentLot->qty = $this->detail_batch->qty;
3013 $shipmentLot->fk_origin_stock = $batch_id;
3014 if ($shipmentLot->create($this->id) < 0) {
3015 $this->errors = $shipmentLot->errors;
3024 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
3025 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id :
'null');
3027 $sql .=
" WHERE rowid = ".((int) $this->
id);
3029 if (!$this->db->query(
$sql)) {
3030 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
3039 $this->errors[] = $this->error;
3045 if (!$error && !$notrigger) {
3047 $result = $this->
call_trigger(
'LINESHIPPING_MODIFY', $user);
3049 $this->errors[] = $this->error;
3055 $this->db->commit();
3058 foreach ($this->errors as $errmsg) {
3059 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
3060 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3062 $this->db->rollback();