22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
37 public static $FIELDS = array(
68 public function get(
$id)
70 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
71 throw new RestException(403);
74 $result = $this->shipment->fetch(
$id);
76 throw new RestException(404,
'Shipment not found');
80 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 $this->shipment->fetchObjectLinked();
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
110 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
111 throw new RestException(403);
119 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
123 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
124 $search_sale = DolibarrApiAccess::$user->id;
127 $sql =
"SELECT t.rowid";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
129 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
130 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
133 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
136 if ($search_sale && $search_sale !=
'-1') {
137 if ($search_sale == -2) {
138 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
139 } elseif ($search_sale > 0) {
140 $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).
")";
145 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'shipments',
'apimethod' =>
'index');
148 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters,
$object, $action);
150 $sql = $hookmanager->resPrint;
151 } elseif ($reshook == 0) {
152 $sql .= $hookmanager->resPrint;
157 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
162 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
164 $sql .= $this->db->order($sortfield, $sortorder);
169 $offset = $limit * $page;
171 $sql .= $this->db->plimit($limit + 1, $offset);
175 $result = $this->db->query($sql);
178 $num = $this->db->num_rows($result);
179 $min = min($num, ($limit <= 0 ? $num : $limit));
182 $obj = $this->db->fetch_object($result);
184 if ($shipment_static->fetch($obj->rowid)) {
190 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
194 if ($pagination_data) {
195 $totalsResult = $this->db->query($sqlTotals);
196 $total = $this->db->fetch_object($totalsResult)->total;
201 $obj_ret[
'data'] = $tmp;
202 $obj_ret[
'pagination'] = [
203 'total' => (int) $total,
205 'page_count' => ceil((
int) $total / $limit),
221 public function post($request_data =
null)
223 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
224 throw new RestException(403,
"Insufficiant rights");
227 $result = $this->
_validate($request_data);
229 foreach ($request_data as $field => $value) {
230 if ($field ===
'caller') {
232 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
236 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
238 if (isset($request_data[
"lines"])) {
240 foreach ($request_data[
"lines"] as $line) {
243 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
244 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
245 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
246 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
247 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
248 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
249 $shipmentline->qty = (float) $line[
'qty'];
250 $shipmentline->rang = (int) $line[
'rang'];
251 $array_options = $line[
'array_options'];
252 if (is_array($array_options)) {
253 $shipmentline->array_options = $array_options;
255 $detail_batch = $line[
'detail_batch'];
256 if (is_array($detail_batch) || is_object($detail_batch)) {
257 $shipmentline->detail_batch = $detail_batch;
259 $lines[] = $shipmentline;
261 $this->shipment->lines = $lines;
264 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
265 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
268 return $this->shipment->id;
456 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
457 throw new RestException(403);
460 $result = $this->shipment->fetch(
$id);
462 throw new RestException(404,
'Shipment not found');
466 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
471 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
472 if ($updateRes > 0) {
476 'message' =>
'line ' .$lineid.
' deleted'
480 throw new RestException(405, $this->shipment->error);
493 public function put(
$id, $request_data =
null)
495 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
496 throw new RestException(403);
499 $result = $this->shipment->fetch(
$id);
501 throw new RestException(404,
'Shipment not found');
505 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
507 foreach ($request_data as $field => $value) {
508 if ($field ==
'id') {
511 if ($field ===
'caller') {
513 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
517 if ($field ==
'array_options' && is_array($value)) {
518 foreach ($value as $index => $val) {
519 $this->shipment->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->shipment);
523 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
526 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
527 return $this->
get(
$id);
529 throw new RestException(500, $this->shipment->error);
542 public function delete(
$id)
544 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
545 throw new RestException(403);
547 $result = $this->shipment->fetch(
$id);
549 throw new RestException(404,
'Shipment not found');
553 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
556 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
557 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
563 'message' =>
'Shipment deleted'
589 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
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 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
603 throw new RestException(304,
'Error nothing done. May be object is already validated');
606 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
610 $result = $this->shipment->fetch(
$id);
612 $this->shipment->fetchObjectLinked();
711 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
712 throw new RestException(403);
715 $result = $this->shipment->fetch(
$id);
717 throw new RestException(404,
'Shipment not found');
721 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
724 $result = $this->shipment->setClosed();
726 throw new RestException(304,
'Error nothing done. May be object is already closed');
729 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
733 $result = $this->shipment->fetch(
$id);
735 $this->shipment->fetchObjectLinked();
762 unset(
$object->barcode_type_code);
763 unset(
$object->barcode_type_label);
764 unset(
$object->barcode_type_coder);
767 foreach (
$object->lines as $line) {
768 if (is_array($line->detail_batch)) {
769 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
770 unset($line->detail_batch[$keytmp2]->db);
773 unset($line->canvas);
775 unset($line->tva_tx);
776 unset($line->vat_src_code);
777 unset($line->total_ht);
778 unset($line->total_ttc);
779 unset($line->total_tva);
780 unset($line->total_localtax1);
781 unset($line->total_localtax2);
782 unset($line->remise_percent);
798 if ($data ===
null) {
802 foreach (Shipments::$FIELDS as $field) {
803 if (!isset($data[$field])) {
804 throw new RestException(400,
"$field field missing");
806 $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.
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
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.