23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
38 public static $FIELDS = array(
69 public function get(
$id)
71 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
72 throw new RestException(403);
75 $result = $this->shipment->fetch(
$id);
77 throw new RestException(404,
'Shipment not found');
81 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
84 $this->shipment->fetchObjectLinked();
109 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
111 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
112 throw new RestException(403);
120 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
124 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
130 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
131 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
134 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
137 if ($search_sale && $search_sale !=
'-1') {
138 if ($search_sale == -2) {
139 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
140 } elseif ($search_sale > 0) {
141 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
146 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'shipments',
'apimethod' =>
'index');
149 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters,
$object, $action);
151 $sql = $hookmanager->resPrint;
152 } elseif ($reshook == 0) {
153 $sql .= $hookmanager->resPrint;
158 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
163 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
165 $sql .= $this->db->order($sortfield, $sortorder);
170 $offset = $limit * $page;
172 $sql .= $this->db->plimit($limit + 1, $offset);
176 $result = $this->db->query($sql);
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $obj = $this->db->fetch_object($result);
185 if ($shipment_static->fetch($obj->rowid)) {
191 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
195 if ($pagination_data) {
196 $totalsResult = $this->db->query($sqlTotals);
197 $total = $this->db->fetch_object($totalsResult)->total;
202 $obj_ret[
'data'] = $tmp;
203 $obj_ret[
'pagination'] = [
204 'total' => (int) $total,
206 'page_count' => ceil((
int) $total / $limit),
222 public function post($request_data =
null)
224 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
225 throw new RestException(403,
"Insufficiant rights");
228 $result = $this->
_validate($request_data);
230 foreach ($request_data as $field => $value) {
231 if ($field ===
'caller') {
233 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
237 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
239 if (isset($request_data[
"lines"])) {
240 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
243 foreach ($request_data[
"lines"] as $line) {
246 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
247 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
248 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
249 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
250 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
251 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
252 $shipmentline->qty = (float) $line[
'qty'];
253 $shipmentline->rang = (int) $line[
'rang'];
254 $array_options = $line[
'array_options'];
255 if (is_array($array_options)) {
256 $shipmentline->array_options = $array_options;
258 $detail_batch = $line[
'detail_batch'];
259 if (is_array($detail_batch) || is_object($detail_batch)) {
260 $shipmentline->detail_batch = $detail_batch;
266 if ($orderline->fetch($shipmentline->fk_elementdet) > 0 && (empty($orderline->product_type) ||
getDolGlobalString(
'STOCK_SUPPORTS_SERVICES') ||
getDolGlobalString(
'SHIPMENT_SUPPORTS_SERVICES'))) {
267 $shipmentline->fk_product = (int) $orderline->fk_product;
270 $lines[] = $shipmentline;
272 $this->shipment->lines = $lines;
275 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
276 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
279 return $this->shipment->id;
467 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
468 throw new RestException(403);
471 $result = $this->shipment->fetch(
$id);
473 throw new RestException(404,
'Shipment not found');
477 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
482 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
483 if ($updateRes > 0) {
487 'message' =>
'line ' .$lineid.
' deleted'
491 throw new RestException(405, $this->shipment->error);
504 public function put(
$id, $request_data =
null)
506 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
507 throw new RestException(403);
510 $result = $this->shipment->fetch(
$id);
512 throw new RestException(404,
'Shipment not found');
516 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
518 foreach ($request_data as $field => $value) {
519 if ($field ==
'id') {
522 if ($field ===
'caller') {
524 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
528 if ($field ==
'array_options' && is_array($value)) {
529 foreach ($value as $index => $val) {
530 $this->shipment->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->shipment);
534 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
537 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
538 return $this->
get(
$id);
540 throw new RestException(500, $this->shipment->error);
553 public function delete(
$id)
555 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
556 throw new RestException(403);
558 $result = $this->shipment->fetch(
$id);
560 throw new RestException(404,
'Shipment not found');
564 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
567 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
568 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
574 'message' =>
'Shipment deleted'
600 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
601 throw new RestException(403);
603 $result = $this->shipment->fetch(
$id);
605 throw new RestException(404,
'Shipment not found');
609 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
614 throw new RestException(304,
'Error nothing done. May be object is already validated');
617 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
621 $result = $this->shipment->fetch(
$id);
623 $this->shipment->fetchObjectLinked();
722 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
723 throw new RestException(403);
726 $result = $this->shipment->fetch(
$id);
728 throw new RestException(404,
'Shipment not found');
732 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
735 $result = $this->shipment->setClosed();
737 throw new RestException(304,
'Error nothing done. May be object is already closed');
740 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
744 $result = $this->shipment->fetch(
$id);
746 $this->shipment->fetchObjectLinked();
773 unset(
$object->barcode_type_code);
774 unset(
$object->barcode_type_label);
775 unset(
$object->barcode_type_coder);
778 foreach (
$object->lines as $line) {
779 if (is_array($line->detail_batch)) {
780 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
781 unset($line->detail_batch[$keytmp2]->db);
784 unset($line->canvas);
786 unset($line->tva_tx);
787 unset($line->vat_src_code);
788 unset($line->total_ht);
789 unset($line->total_ttc);
790 unset($line->total_tva);
791 unset($line->total_localtax1);
792 unset($line->total_localtax2);
793 unset($line->remise_percent);
809 if ($data ===
null) {
813 foreach (Shipments::$FIELDS as $field) {
814 if (!isset($data[$field])) {
815 throw new RestException(400,
"$field field missing");
817 $shipment[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage lines of shipment.
Class to manage order lines.
close($id, $notrigger=0)
Classify the shipment as invoiced.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update shipment general fields (won't touch lines of 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.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
deleteLine($id, $lineid)
Get lines of an shipment.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.