19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
35 static $FIELDS = array(
66 public function get($id)
68 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
69 throw new RestException(401);
72 $result = $this->shipment->fetch($id);
74 throw new RestException(404,
'Shipment not found');
78 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->shipment->fetchObjectLinked();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
109 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
115 $sql =
"SELECT t.rowid";
116 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as t";
119 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
121 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
123 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
124 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
126 if ($search_sale > 0)
128 $sql .=
" AND sc.fk_user = ".$search_sale;
135 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
137 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
138 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
141 $sql .= $this->
db->order($sortfield, $sortorder);
147 $offset = $limit * $page;
149 $sql .= $this->
db->plimit($limit + 1, $offset);
153 $result = $this->
db->query($sql);
157 $num = $this->
db->num_rows($result);
158 $min = min($num, ($limit <= 0 ? $num : $limit));
162 $obj = $this->
db->fetch_object($result);
164 if ($shipment_static->fetch($obj->rowid)) {
170 throw new RestException(503,
'Error when retrieve commande list : '.$this->
db->lasterror());
172 if (!count($obj_ret)) {
173 throw new RestException(404,
'No shipment found');
184 public function post($request_data = null)
186 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
187 throw new RestException(401,
"Insuffisant rights");
190 $result = $this->
_validate($request_data);
192 foreach ($request_data as $field => $value) {
193 $this->shipment->$field = $value;
195 if (isset($request_data[
"lines"])) {
197 foreach ($request_data[
"lines"] as $line) {
198 array_push($lines, (
object) $line);
200 $this->shipment->lines = $lines;
203 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
204 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
207 return $this->shipment->id;
381 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
382 throw new RestException(401);
385 $result = $this->shipment->fetch($id);
387 throw new RestException(404,
'Shipment not found');
391 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
396 $request_data = (object) $request_data;
397 $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
398 if ($updateRes > 0) {
399 return $this->
get($id);
401 throw new RestException(405, $this->shipment->error);
413 public function put($id, $request_data = null)
415 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
416 throw new RestException(401);
419 $result = $this->shipment->fetch($id);
421 throw new RestException(404,
'Shipment not found');
425 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
427 foreach ($request_data as $field => $value) {
428 if ($field ==
'id')
continue;
429 $this->shipment->$field = $value;
432 if ($this->shipment->update(DolibarrApiAccess::$user) > 0)
434 return $this->
get($id);
436 throw new RestException(500, $this->shipment->error);
447 public function delete($id)
449 if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
450 throw new RestException(401);
452 $result = $this->shipment->fetch($id);
454 throw new RestException(404,
'Shipment not found');
458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
462 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
468 'message' =>
'Shipment deleted'
494 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
495 throw new RestException(401);
497 $result = $this->shipment->fetch($id);
499 throw new RestException(404,
'Shipment not found');
503 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
506 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
508 throw new RestException(304,
'Error nothing done. May be object is already validated');
511 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
513 $result = $this->shipment->fetch($id);
515 throw new RestException(404,
'Shipment not found');
519 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 $this->shipment->fetchObjectLinked();
619 $object = parent::_cleanObjectDatas($object);
621 unset($object->thirdparty);
623 unset($object->note);
624 unset($object->address);
625 unset($object->barcode_type);
626 unset($object->barcode_type_code);
627 unset($object->barcode_type_label);
628 unset($object->barcode_type_coder);
630 if (!empty($object->lines) && is_array($object->lines))
632 foreach ($object->lines as $line)
634 unset($line->tva_tx);
635 unset($line->vat_src_code);
636 unset($line->total_ht);
637 unset($line->total_ttc);
638 unset($line->total_tva);
639 unset($line->total_localtax1);
640 unset($line->total_localtax2);
641 unset($line->remise_percent);
658 foreach (Shipments::$FIELDS as $field) {
659 if (!isset($data[$field]))
660 throw new RestException(400,
"$field field missing");
661 $shipment[$field] = $data[$field];
_cleanObjectDatas($object)
Classify the shipment as invoiced.
deleteLine($id, $lineid)
Get lines of an shipment.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
__construct()
Constructor.
Class to manage shipments.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
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= '')
List shipments.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create shipment object.
validate($id, $notrigger=0)
Validate a shipment.