22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.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 ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
140 } elseif ($search_sale > 0) {
141 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
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"])) {
241 foreach ($request_data[
"lines"] as $line) {
244 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
245 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
246 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
247 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
248 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
249 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
250 $shipmentline->qty = (float) $line[
'qty'];
251 $shipmentline->rang = (int) $line[
'rang'];
252 $array_options = $line[
'array_options'];
253 if (is_array($array_options)) {
254 $shipmentline->array_options = $array_options;
256 $detail_batch = $line[
'detail_batch'];
257 if (is_array($detail_batch) || is_object($detail_batch)) {
258 $shipmentline->detail_batch = $detail_batch;
260 $lines[] = $shipmentline;
262 $this->shipment->lines = $lines;
265 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
266 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
269 return $this->shipment->id;
457 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
458 throw new RestException(403);
461 $result = $this->shipment->fetch(
$id);
463 throw new RestException(404,
'Shipment not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
472 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
473 if ($updateRes > 0) {
477 'message' =>
'line ' .$lineid.
' deleted'
481 throw new RestException(405, $this->shipment->error);
494 public function put(
$id, $request_data =
null)
496 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
497 throw new RestException(403);
500 $result = $this->shipment->fetch(
$id);
502 throw new RestException(404,
'Shipment not found');
506 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 foreach ($request_data as $field => $value) {
509 if ($field ==
'id') {
512 if ($field ===
'caller') {
514 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
518 if ($field ==
'array_options' && is_array($value)) {
519 foreach ($value as $index => $val) {
524 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
527 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
528 return $this->
get(
$id);
530 throw new RestException(500, $this->shipment->error);
543 public function delete(
$id)
545 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
546 throw new RestException(403);
548 $result = $this->shipment->fetch(
$id);
550 throw new RestException(404,
'Shipment not found');
554 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
557 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
558 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
564 'message' =>
'Shipment deleted'
590 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
591 throw new RestException(403);
593 $result = $this->shipment->fetch(
$id);
595 throw new RestException(404,
'Shipment not found');
599 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
602 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
604 throw new RestException(304,
'Error nothing done. May be object is already validated');
607 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
611 $result = $this->shipment->fetch(
$id);
613 $this->shipment->fetchObjectLinked();
712 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
713 throw new RestException(403);
716 $result = $this->shipment->fetch(
$id);
718 throw new RestException(404,
'Shipment not found');
722 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
725 $result = $this->shipment->setClosed();
727 throw new RestException(304,
'Error nothing done. May be object is already closed');
730 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
734 $result = $this->shipment->fetch(
$id);
736 $this->shipment->fetchObjectLinked();
763 unset(
$object->barcode_type_code);
764 unset(
$object->barcode_type_label);
765 unset(
$object->barcode_type_coder);
768 foreach (
$object->lines as $line) {
769 if (is_array($line->detail_batch)) {
770 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
771 unset($line->detail_batch[$keytmp2]->db);
774 unset($line->canvas);
776 unset($line->tva_tx);
777 unset($line->vat_src_code);
778 unset($line->total_ht);
779 unset($line->total_ttc);
780 unset($line->total_tva);
781 unset($line->total_localtax1);
782 unset($line->total_localtax2);
783 unset($line->remise_percent);
799 if ($data ===
null) {
803 foreach (Shipments::$FIELDS as $field) {
804 if (!isset($data[$field])) {
805 throw new RestException(400,
"$field field missing");
807 $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.
Class to manage lines of shipment.
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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.