20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
35 public static $FIELDS = array(
66 public function get(
$id)
68 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
69 throw new RestException(403);
72 $result = $this->shipment->fetch(
$id);
74 throw new RestException(404,
'Shipment not found');
78 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->shipment->fetchObjectLinked();
106 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
108 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
109 throw new RestException(403);
115 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
119 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
120 $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
125 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
126 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
128 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
131 if ($search_sale && $search_sale !=
'-1') {
132 if ($search_sale == -2) {
133 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
134 } elseif ($search_sale > 0) {
135 $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).
")";
143 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
148 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
150 $sql .= $this->db->order($sortfield, $sortorder);
155 $offset = $limit * $page;
157 $sql .= $this->db->plimit($limit + 1, $offset);
161 $result = $this->db->query($sql);
164 $num = $this->db->num_rows($result);
165 $min = min($num, ($limit <= 0 ? $num : $limit));
168 $obj = $this->db->fetch_object($result);
170 if ($shipment_static->fetch($obj->rowid)) {
176 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
180 if ($pagination_data) {
181 $totalsResult = $this->db->query($sqlTotals);
182 $total = $this->db->fetch_object($totalsResult)->total;
187 $obj_ret[
'data'] = $tmp;
188 $obj_ret[
'pagination'] = [
189 'total' => (int) $total,
191 'page_count' => ceil((
int) $total / $limit),
207 public function post($request_data =
null)
209 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
210 throw new RestException(403,
"Insuffisant rights");
213 $result = $this->
_validate($request_data);
215 foreach ($request_data as $field => $value) {
216 if ($field ===
'caller') {
218 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
222 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
224 if (isset($request_data[
"lines"])) {
226 foreach ($request_data[
"lines"] as $line) {
229 $shipmentline->entrepot_id = (int) $line[
'entrepot_id'];
230 $shipmentline->fk_element = (int) ($line[
'fk_element'] ?? $line[
'origin_id']);
231 $shipmentline->origin_line_id = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
232 $shipmentline->fk_elementdet = (int) ($line[
'fk_elementdet'] ?? $line[
'origin_line_id']);
233 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
234 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
235 $shipmentline->qty = (float) $line[
'qty'];
236 $shipmentline->rang = (int) $line[
'rang'];
237 $array_options = $line[
'array_options'];
238 if (is_array($array_options)) {
239 $shipmentline->array_options = $array_options;
241 $detail_batch = $line[
'detail_batch'];
242 if (is_array($detail_batch) || is_object($detail_batch)) {
243 $shipmentline->detail_batch = $detail_batch;
245 $lines[] = $shipmentline;
247 $this->shipment->lines = $lines;
250 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
251 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
254 return $this->shipment->id;
442 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
443 throw new RestException(403);
446 $result = $this->shipment->fetch(
$id);
448 throw new RestException(404,
'Shipment not found');
452 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
457 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
458 if ($updateRes > 0) {
462 'message' =>
'line ' .$lineid.
' deleted'
466 throw new RestException(405, $this->shipment->error);
479 public function put(
$id, $request_data =
null)
481 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
482 throw new RestException(403);
485 $result = $this->shipment->fetch(
$id);
487 throw new RestException(404,
'Shipment not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
493 foreach ($request_data as $field => $value) {
494 if ($field ==
'id') {
497 if ($field ===
'caller') {
499 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
503 if ($field ==
'array_options' && is_array($value)) {
504 foreach ($value as $index => $val) {
505 $this->shipment->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->shipment);
509 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
512 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
513 return $this->
get(
$id);
515 throw new RestException(500, $this->shipment->error);
528 public function delete(
$id)
530 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
531 throw new RestException(403);
533 $result = $this->shipment->fetch(
$id);
535 throw new RestException(404,
'Shipment not found');
539 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
542 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
543 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
549 'message' =>
'Shipment deleted'
575 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
576 throw new RestException(403);
578 $result = $this->shipment->fetch(
$id);
580 throw new RestException(404,
'Shipment not found');
584 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
589 throw new RestException(304,
'Error nothing done. May be object is already validated');
592 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
596 $result = $this->shipment->fetch(
$id);
598 $this->shipment->fetchObjectLinked();
697 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
698 throw new RestException(403);
701 $result = $this->shipment->fetch(
$id);
703 throw new RestException(404,
'Shipment not found');
707 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
710 $result = $this->shipment->setClosed();
712 throw new RestException(304,
'Error nothing done. May be object is already closed');
715 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
719 $result = $this->shipment->fetch(
$id);
721 $this->shipment->fetchObjectLinked();
745 unset(
$object->barcode_type_code);
746 unset(
$object->barcode_type_label);
747 unset(
$object->barcode_type_coder);
750 foreach (
$object->lines as $line) {
751 if (is_array($line->detail_batch)) {
752 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
753 unset($line->detail_batch[$keytmp2]->db);
756 unset($line->canvas);
758 unset($line->tva_tx);
759 unset($line->vat_src_code);
760 unset($line->total_ht);
761 unset($line->total_ttc);
762 unset($line->total_tva);
763 unset($line->total_localtax1);
764 unset($line->total_localtax2);
765 unset($line->remise_percent);
781 if ($data ===
null) {
785 foreach (Shipments::$FIELDS as $field) {
786 if (!isset($data[$field])) {
787 throw new RestException(400,
"$field field missing");
789 $shipment[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...