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 retrieve commande 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);
240 if (isset($request_data[
"lines"])) {
242 foreach ($request_data[
"lines"] as $line) {
245 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
246 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
247 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
248 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
249 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
250 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
251 $shipmentline->qty = (float) $line[
'qty'];
252 $shipmentline->rang = (int) $line[
'rang'];
253 $array_options = $line[
'array_options'];
254 if (is_array($array_options)) {
255 $shipmentline->array_options = $array_options;
257 $detail_batch = $line[
'detail_batch'];
258 if (is_array($detail_batch) || is_object($detail_batch)) {
259 $shipmentline->detail_batch = $detail_batch;
261 $lines[] = $shipmentline;
263 $this->shipment->lines = $lines;
266 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
270 return $this->shipment->id;
286 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
287 throw new RestException(403);
290 $result = $this->shipment->fetch(
$id);
292 throw new RestException(404,
'Shipment not found');
296 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
298 $this->shipment->getLinesArray();
300 foreach ($this->shipment->lines as $line) {
331 public function postLine(
$id, $fk_origin_line = 0, $qty = 0, $warehouse_id = 0, $fk_product = 0)
333 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
334 throw new RestException(403);
337 $result = $this->shipment->fetch(
$id);
339 throw new RestException(404,
'Shipment not found');
343 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
348 throw new RestException(405,
'Lines can only be added to a draft shipment');
351 $fk_origin_line = (int) $fk_origin_line;
352 if ($fk_origin_line <= 0) {
353 throw new RestException(400,
'Field fk_origin_line is mandatory');
358 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
361 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
363 $resorderline = $orderline->fetch($fk_origin_line);
364 if ($resorderline <= 0) {
365 throw new RestException(404,
'Origin order line '.$fk_origin_line.
' not found');
367 if (
isModEnabled(
'productbatch') && !empty($orderline->product_tobatch)) {
368 throw new RestException(405,
'Adding a batch/lot managed shipment line is not supported through this endpoint. Use the dedicated dispatch flow instead.');
373 $addResult = $this->shipment->addline((
int) $warehouse_id, $fk_origin_line, $qty, array(), (
int) $fk_product);
374 if ($addResult <= 0) {
375 $msg = $this->shipment->errorsToString() ? $this->shipment->errorsToString() :
'Error while adding shipment line';
377 if (in_array((
int) $addResult, array(-1, -3, -4),
true)) {
378 throw new RestException(400, $msg);
380 throw new RestException(500, $msg);
384 $line = end($this->shipment->lines);
385 $line->fk_expedition = $this->shipment->id;
387 $insertRes = $line->insert(DolibarrApiAccess::$user);
388 if ($insertRes > 0) {
392 throw new RestException(500, $line->errorsToString());
419 public function putLine(
$id, $lineid, $qty = 0, $warehouse_id = 0)
421 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
422 throw new RestException(403);
425 $result = $this->shipment->fetch(
$id);
427 throw new RestException(404,
'Shipment not found');
431 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 throw new RestException(405,
'Only draft shipment lines can be updated');
441 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
445 $resline = $line->fetch($lineid);
447 throw new RestException(404,
'Shipment line not found');
449 if ((
int) $line->fk_expedition != (
int) $this->shipment->id) {
450 throw new RestException(404,
'Line '.((
int) $lineid).
' is not a line of shipment '.((
int)
$id));
453 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expeditionlinebatch.class.php';
455 $lots = $linebatch->fetchAll($line->id);
456 if (is_array($lots) && count($lots) > 0) {
457 throw new RestException(405,
'Update of a batch/lot managed shipment line is not supported. Delete and recreate the line instead.');
461 if ($warehouse_id > 0) {
462 $line->entrepot_id = (int) $warehouse_id;
465 $updateRes = $line->update(DolibarrApiAccess::$user);
466 if ($updateRes > 0) {
467 return $this->
get(
$id);
470 throw new RestException(500, $line->errorsToString() ? $line->errorsToString() : $this->shipment->errorsToString());
494 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
495 throw new RestException(403);
498 $result = $this->shipment->fetch(
$id);
500 throw new RestException(404,
'Shipment not found');
504 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
509 throw new RestException(405,
'Only draft shipment lines can be deleted');
513 $resline = $line->fetch($lineid);
515 throw new RestException(404,
'Shipment line not found');
517 if ((
int) $line->fk_expedition != (
int) $this->shipment->id) {
518 throw new RestException(404,
'Line '.((
int) $lineid).
' is not a line of shipment '.((
int)
$id));
521 $deleteRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
522 if ($deleteRes > 0) {
523 return $this->
get(
$id);
526 throw new RestException(500, $this->shipment->errorsToString() ? $this->shipment->errorsToString() :
'Error while deleting shipment line');
538 public function put(
$id, $request_data =
null)
540 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
541 throw new RestException(403);
544 $result = $this->shipment->fetch(
$id);
546 throw new RestException(404,
'Shipment not found');
550 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
552 foreach ($request_data as $field => $value) {
553 if ($field ==
'id') {
556 if ($field ===
'caller') {
558 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
562 if ($field ==
'array_options' && is_array($value)) {
563 foreach ($value as $index => $val) {
568 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
571 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
572 return $this->
get(
$id);
574 throw new RestException(500, $this->shipment->errorsToString());
587 public function delete(
$id)
589 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
590 throw new RestException(403);
592 $result = $this->shipment->fetch(
$id);
594 throw new RestException(404,
'Shipment not found');
598 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
601 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
602 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->errorsToString());
608 'message' =>
'Shipment deleted'
634 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
635 throw new RestException(403);
637 $result = $this->shipment->fetch(
$id);
639 throw new RestException(404,
'Shipment not found');
643 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
646 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
648 throw new RestException(304,
'Error nothing done. May be object is already validated');
651 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->errorsToString());
655 $result = $this->shipment->fetch(
$id);
657 $this->shipment->fetchObjectLinked();
756 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
757 throw new RestException(403);
760 $result = $this->shipment->fetch(
$id);
762 throw new RestException(404,
'Shipment not found');
766 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 $result = $this->shipment->setClosed();
771 throw new RestException(304,
'Error nothing done. May be object is already closed');
774 throw new RestException(500,
'Error when closing Order: '.$this->shipment->errorsToString());
778 $result = $this->shipment->fetch(
$id);
780 $this->shipment->fetchObjectLinked();
804 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
805 throw new RestException(403);
808 $result = $this->shipment->fetch(
$id);
810 throw new RestException(404,
'Shipment not found');
814 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
817 $result = $this->shipment->setDraft(DolibarrApiAccess::$user);
819 throw new RestException(304,
'Error nothing done. May be object is already draft');
822 throw new RestException(500,
'Error when setting shipment back to draft: '.$this->shipment->errorsToString());
826 $result = $this->shipment->fetch(
$id);
828 $this->shipment->fetchObjectLinked();
855 unset(
$object->barcode_type_code);
856 unset(
$object->barcode_type_label);
857 unset(
$object->barcode_type_coder);
860 foreach (
$object->lines as $line) {
861 if (is_array($line->detail_batch)) {
862 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
863 unset($line->detail_batch[$keytmp2]->db);
866 unset($line->canvas);
868 unset($line->tva_tx);
869 unset($line->vat_src_code);
870 unset($line->total_ht);
871 unset($line->total_ttc);
872 unset($line->total_tva);
873 unset($line->total_localtax1);
874 unset($line->total_localtax2);
875 unset($line->remise_percent);
891 if ($data ===
null) {
898 $mandatory = Shipments::$FIELDS;
899 if (
getDolGlobalString(
'SHIPMENT_STANDALONE') && !isset($data[
'origin_id']) && !isset($data[
'origin_type'])) {
900 $mandatory = array_values(array_diff($mandatory, array(
'origin_id',
'origin_type')));
902 foreach ($mandatory as $field) {
903 if (!isset($data[$field])) {
904 throw new RestException(400,
"$field field missing");
906 $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.
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)
Add a line to a given shipment.
setToDraft($id)
Set a shipment back to draft status.
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.
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.