23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
39 public static $FIELDS = array(
70 public function get(
$id)
72 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
73 throw new RestException(403);
76 $result = $this->shipment->fetch(
$id);
78 throw new RestException(404,
'Shipment not found');
82 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
85 $this->shipment->fetchObjectLinked();
110 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
112 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
113 throw new RestException(403);
121 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
125 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
126 $search_sale = DolibarrApiAccess::$user->id;
129 $sql =
"SELECT t.rowid";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
131 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
132 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
133 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
135 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
138 if ($search_sale && $search_sale !=
'-1') {
139 if ($search_sale == -2) {
140 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
141 } elseif ($search_sale > 0) {
142 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
147 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'shipments',
'apimethod' =>
'index');
150 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters,
$object, $action);
152 $sql = $hookmanager->resPrint;
153 } elseif ($reshook == 0) {
154 $sql .= $hookmanager->resPrint;
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
164 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
166 $sql .= $this->db->order($sortfield, $sortorder);
171 $offset = $limit * $page;
173 $sql .= $this->db->plimit($limit + 1, $offset);
177 $result = $this->db->query($sql);
180 $num = $this->db->num_rows($result);
181 $min = min($num, ($limit <= 0 ? $num : $limit));
184 $obj = $this->db->fetch_object($result);
186 if ($shipment_static->fetch($obj->rowid)) {
192 throw new RestException(503,
'Error when retrieving shipments list : '.$this->db->lasterror());
196 if ($pagination_data) {
197 $totalsResult = $this->db->query($sqlTotals);
198 $total = $this->db->fetch_object($totalsResult)->total;
203 $obj_ret[
'data'] = $tmp;
204 $obj_ret[
'pagination'] = [
205 'total' => (int) $total,
207 'page_count' => ceil((
int) $total / $limit),
223 public function post($request_data =
null)
225 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
226 throw new RestException(403,
"Insufficiant rights");
229 $result = $this->
_validate($request_data);
231 foreach ($request_data as $field => $value) {
232 if ($field ===
'caller') {
234 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
238 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
243 if (
getDolGlobalString(
'SHIPMENT_STANDALONE') && !isset($request_data[
'origin_id']) && !isset($request_data[
'origin_type'])) {
247 if (isset($request_data[
"lines"])) {
249 foreach ($request_data[
"lines"] as $line) {
252 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
253 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
254 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
255 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
256 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
257 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
258 $shipmentline->qty = (float) $line[
'qty'];
259 $shipmentline->rang = (int) $line[
'rang'];
260 $array_options = $line[
'array_options'];
261 if (is_array($array_options)) {
262 $shipmentline->array_options = $array_options;
264 $detail_batch = $line[
'detail_batch'];
265 if (is_array($detail_batch) || is_object($detail_batch)) {
266 $shipmentline->detail_batch = $detail_batch;
268 $lines[] = $shipmentline;
270 $this->shipment->lines = $lines;
273 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
274 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
277 return $this->shipment->id;
289 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
291 if (!is_array($lines)) {
292 throw new RestException(400,
'Field lines must be an array');
294 $this->shipment->lines = array();
298 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
299 $this->db->rollback();
300 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
303 foreach ($lines as $line) {
304 if (!is_array($line)) {
305 $this->db->rollback();
306 throw new RestException(400,
'Each line must be an object');
308 $fk_product = (int) ($line[
'fk_product'] ?? 0);
309 $description =
sanitizeVal((
string) ($line[
'description'] ?? ($line[
'desc'] ??
'')),
'restricthtml');
310 $qty = (float)
price2num($line[
'qty'] ??
'',
'MS');
311 $fk_unit = isset($line[
'fk_unit']) ? (int) $line[
'fk_unit'] :
null;
314 if (!isset($line[
'qty']) || $line[
'qty'] ===
'') {
315 $error =
'Field qty is mandatory';
316 } elseif ($qty < 0) {
317 $error =
'Field qty cannot be negative';
319 $error =
'Field fk_product is mandatory (MAIN_DISABLE_FREE_LINES is on)';
320 } elseif ($fk_product <= 0 && $description ===
'') {
321 $error =
'A line needs a fk_product or a description';
322 } elseif ($fk_product > 0) {
323 $product =
new Product($this->db);
324 if ($product->fetch($fk_product) <= 0) {
325 $error =
'Product '.$fk_product.
' not found';
326 } elseif ($fk_unit ===
null) {
327 $fk_unit = $product->fk_unit;
331 $this->db->rollback();
332 throw new RestException(400, $error);
335 $array_options = (isset($line[
'array_options']) && is_array($line[
'array_options'])) ? $line[
'array_options'] : array();
336 $rang = (int) ($line[
'rang'] ?? 0);
338 if ($this->shipment->addlinefree($qty,
'shipping', $fk_product, $fk_unit, $rang > 0 ? $rang : -1, $description, 0, $array_options) <= 0) {
339 $this->db->rollback();
340 throw new RestException(500,
"Error creating shipment line", array_merge(array($this->shipment->error), $this->shipment->errors));
346 return $this->shipment->id;
362 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
363 throw new RestException(403);
366 $result = $this->shipment->fetch(
$id);
368 throw new RestException(404,
'Shipment not found');
372 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
374 $this->shipment->getLinesArray();
376 foreach ($this->shipment->lines as $line) {
410 public function postLine(
$id, $fk_origin_line = 0, $qty = 0, $warehouse_id = 0, $fk_product = 0, $description =
'')
412 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
413 throw new RestException(403);
416 $result = $this->shipment->fetch(
$id);
418 throw new RestException(404,
'Shipment not found');
422 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
427 throw new RestException(405,
'Lines can only be added to a draft shipment');
430 $fk_origin_line = (int) $fk_origin_line;
433 if ($fk_origin_line <= 0 && empty($this->shipment->origin_id) &&
getDolGlobalString(
'SHIPMENT_STANDALONE')) {
435 $fk_product = (int) $fk_product;
436 $description =
sanitizeVal((
string) $description,
'restricthtml');
440 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
442 if ($fk_product > 0) {
443 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
444 $product =
new Product($this->db);
445 if ($product->fetch($fk_product) <= 0) {
446 throw new RestException(400,
'Product '.$fk_product.
' not found');
448 $fk_unit = $product->fk_unit;
450 throw new RestException(400,
'Field fk_product is mandatory (MAIN_DISABLE_FREE_LINES is on)');
451 } elseif ($description ===
'') {
452 throw new RestException(400,
'A line needs a fk_product or a description');
455 $lineId = $this->shipment->addlinefree($qty,
'shipping', $fk_product, $fk_unit, -1, $description, 0, array(), (
int) $warehouse_id);
457 $msg = $this->shipment->errorsToString() ? $this->shipment->errorsToString() :
'Error while adding shipment line';
459 throw new RestException(in_array($lineId, array(-1, -3, -4),
true) ? 400 : 500, $msg);
465 if ($fk_origin_line <= 0) {
466 throw new RestException(400,
'Field fk_origin_line is mandatory');
471 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
474 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
476 $resorderline = $orderline->fetch($fk_origin_line);
477 if ($resorderline <= 0) {
478 throw new RestException(404,
'Origin order line '.$fk_origin_line.
' not found');
480 if (
isModEnabled(
'productbatch') && !empty($orderline->product_tobatch)) {
481 throw new RestException(405,
'Adding a batch/lot managed shipment line is not supported through this endpoint. Use the dedicated dispatch flow instead.');
486 $addResult = $this->shipment->addline((
int) $warehouse_id, $fk_origin_line, $qty, array(), (
int) $fk_product);
487 if ($addResult <= 0) {
488 $msg = $this->shipment->errorsToString() ? $this->shipment->errorsToString() :
'Error while adding shipment line';
490 if (in_array((
int) $addResult, array(-1, -3, -4),
true)) {
491 throw new RestException(400, $msg);
493 throw new RestException(500, $msg);
497 $line = end($this->shipment->lines);
498 $line->fk_expedition = $this->shipment->id;
500 $insertRes = $line->insert(DolibarrApiAccess::$user);
501 if ($insertRes > 0) {
505 throw new RestException(500, $line->errorsToString());
532 public function putLine(
$id, $lineid, $qty = 0, $warehouse_id = 0)
534 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
535 throw new RestException(403);
538 $result = $this->shipment->fetch(
$id);
540 throw new RestException(404,
'Shipment not found');
544 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 throw new RestException(405,
'Only draft shipment lines can be updated');
554 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
558 $resline = $line->fetch($lineid);
560 throw new RestException(404,
'Shipment line not found');
562 if ((
int) $line->fk_expedition != (
int) $this->shipment->id) {
563 throw new RestException(404,
'Line '.((
int) $lineid).
' is not a line of shipment '.((
int)
$id));
566 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expeditionlinebatch.class.php';
568 $lots = $linebatch->fetchAll($line->id);
569 if (is_array($lots) && count($lots) > 0) {
570 throw new RestException(405,
'Update of a batch/lot managed shipment line is not supported. Delete and recreate the line instead.');
574 if ($warehouse_id > 0) {
575 $line->entrepot_id = (int) $warehouse_id;
578 $updateRes = $line->update(DolibarrApiAccess::$user);
579 if ($updateRes > 0) {
580 return $this->
get(
$id);
583 throw new RestException(500, $line->errorsToString() ? $line->errorsToString() : $this->shipment->errorsToString());
607 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
608 throw new RestException(403);
611 $result = $this->shipment->fetch(
$id);
613 throw new RestException(404,
'Shipment not found');
617 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
622 throw new RestException(405,
'Only draft shipment lines can be deleted');
626 $resline = $line->fetch($lineid);
628 throw new RestException(404,
'Shipment line not found');
630 if ((
int) $line->fk_expedition != (
int) $this->shipment->id) {
631 throw new RestException(404,
'Line '.((
int) $lineid).
' is not a line of shipment '.((
int)
$id));
634 $deleteRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
635 if ($deleteRes > 0) {
636 return $this->
get(
$id);
639 throw new RestException(500, $this->shipment->errorsToString() ? $this->shipment->errorsToString() :
'Error while deleting shipment line');
651 public function put(
$id, $request_data =
null)
653 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
654 throw new RestException(403);
657 $result = $this->shipment->fetch(
$id);
659 throw new RestException(404,
'Shipment not found');
663 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 foreach ($request_data as $field => $value) {
666 if ($field ==
'id') {
669 if ($field ===
'caller') {
671 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
675 if ($field ==
'array_options' && is_array($value)) {
676 foreach ($value as $index => $val) {
681 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
684 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
685 return $this->
get(
$id);
687 throw new RestException(500, $this->shipment->errorsToString());
700 public function delete(
$id)
702 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
703 throw new RestException(403);
705 $result = $this->shipment->fetch(
$id);
707 throw new RestException(404,
'Shipment not found');
711 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
714 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
715 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->errorsToString());
721 'message' =>
'Shipment deleted'
747 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
748 throw new RestException(403);
750 $result = $this->shipment->fetch(
$id);
752 throw new RestException(404,
'Shipment not found');
756 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
759 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
761 throw new RestException(304,
'Error nothing done. May be object is already validated');
764 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->errorsToString());
768 $result = $this->shipment->fetch(
$id);
770 $this->shipment->fetchObjectLinked();
869 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
870 throw new RestException(403);
873 $result = $this->shipment->fetch(
$id);
875 throw new RestException(404,
'Shipment not found');
879 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
882 $result = $this->shipment->setClosed();
884 throw new RestException(304,
'Error nothing done. May be object is already closed');
887 throw new RestException(500,
'Error when closing Order: '.$this->shipment->errorsToString());
891 $result = $this->shipment->fetch(
$id);
893 $this->shipment->fetchObjectLinked();
917 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
918 throw new RestException(403);
921 $result = $this->shipment->fetch(
$id);
923 throw new RestException(404,
'Shipment not found');
927 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
930 $result = $this->shipment->setDraft(DolibarrApiAccess::$user);
932 throw new RestException(304,
'Error nothing done. May be object is already draft');
935 throw new RestException(500,
'Error when setting shipment back to draft: '.$this->shipment->errorsToString());
939 $result = $this->shipment->fetch(
$id);
941 $this->shipment->fetchObjectLinked();
968 unset(
$object->barcode_type_code);
969 unset(
$object->barcode_type_label);
970 unset(
$object->barcode_type_coder);
973 foreach (
$object->lines as $line) {
974 if (is_array($line->detail_batch)) {
975 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
976 unset($line->detail_batch[$keytmp2]->db);
979 unset($line->canvas);
981 unset($line->tva_tx);
982 unset($line->vat_src_code);
983 unset($line->total_ht);
984 unset($line->total_ttc);
985 unset($line->total_tva);
986 unset($line->total_localtax1);
987 unset($line->total_localtax2);
988 unset($line->remise_percent);
1004 if ($data ===
null) {
1007 $shipment = array();
1011 $mandatory = Shipments::$FIELDS;
1012 if (
getDolGlobalString(
'SHIPMENT_STANDALONE') && !isset($data[
'origin_id']) && !isset($data[
'origin_type'])) {
1013 $mandatory = array_values(array_diff($mandatory, array(
'origin_id',
'origin_type')));
1015 foreach ($mandatory as $field) {
1016 if (!isset($data[$field])) {
1017 throw new RestException(400,
"$field field missing");
1019 $shipment[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
const STATUS_DRAFT
Draft status.
Class to manage lines of shipment.
CRUD class for batch number management within shipment.
Class to manage order lines.
Class to manage products or services.
close($id, $notrigger=0)
Classify the shipment as invoiced.
_validate($data)
Validate fields before create or update object.
postLine($id, $fk_origin_line=0, $qty=0, $warehouse_id=0, $fk_product=0, $description='')
Add a line to a given shipment.
setToDraft($id)
Set a shipment back to draft status.
createStandalone($lines)
Create a standalone shipment (no source order) and its free lines.
put($id, $request_data=null)
Update shipment general fields (won't touch lines of shipment)
putLine($id, $lineid, $qty=0, $warehouse_id=0)
Update a line of a given shipment.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List shipments.
validate($id, $notrigger=0)
Validate a shipment.
__construct()
Constructor.
post($request_data=null)
Create shipment object.
getLines($id)
Get lines of a shipment.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
deleteLine($id, $lineid)
Delete a line of a given shipment.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.