19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
35 public 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 =
'')
106 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
107 throw new RestException(401);
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
117 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
118 $search_sale = DolibarrApiAccess::$user->id;
121 $sql =
"SELECT t.rowid";
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
123 $sql .=
", sc.fk_soc, sc.fk_user";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
131 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
132 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133 $sql .=
" AND t.fk_soc = sc.fk_soc";
136 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
138 if ($search_sale > 0) {
139 $sql .=
" AND t.rowid = sc.fk_soc";
142 if ($search_sale > 0) {
143 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
150 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
154 $sql .= $this->db->order($sortfield, $sortorder);
159 $offset = $limit * $page;
161 $sql .= $this->db->plimit($limit + 1, $offset);
165 $result = $this->db->query($sql);
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
172 $obj = $this->db->fetch_object($result);
174 if ($shipment_static->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
182 if (!count($obj_ret)) {
183 throw new RestException(404,
'No shipment found');
194 public function post($request_data =
null)
196 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
197 throw new RestException(401,
"Insuffisant rights");
200 $result = $this->
_validate($request_data);
202 foreach ($request_data as $field => $value) {
203 $this->shipment->$field = $value;
205 if (isset($request_data[
"lines"])) {
207 foreach ($request_data[
"lines"] as $line) {
208 array_push($lines, (
object) $line);
210 $this->shipment->lines = $lines;
213 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
214 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
217 return $this->shipment->id;
401 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
402 throw new RestException(401);
405 $result = $this->shipment->fetch($id);
407 throw new RestException(404,
'Shipment not found');
411 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
417 if ($updateRes > 0) {
418 return $this->
get($id);
420 throw new RestException(405, $this->shipment->error);
432 public function put($id, $request_data =
null)
434 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
435 throw new RestException(401);
438 $result = $this->shipment->fetch($id);
440 throw new RestException(404,
'Shipment not found');
444 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
446 foreach ($request_data as $field => $value) {
447 if ($field ==
'id') {
450 $this->shipment->$field = $value;
453 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
454 return $this->
get($id);
456 throw new RestException(500, $this->shipment->error);
467 public function delete($id)
469 if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
470 throw new RestException(401);
472 $result = $this->shipment->fetch($id);
474 throw new RestException(404,
'Shipment not found');
478 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
481 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
482 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
488 'message' =>
'Shipment deleted'
514 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
515 throw new RestException(401);
517 $result = $this->shipment->fetch($id);
519 throw new RestException(404,
'Shipment not found');
523 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
526 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
528 throw new RestException(304,
'Error nothing done. May be object is already validated');
531 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
535 $result = $this->shipment->fetch($id);
537 $this->shipment->fetchObjectLinked();
634 public function close($id, $notrigger = 0)
636 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
637 throw new RestException(401);
640 $result = $this->shipment->fetch($id);
642 throw new RestException(404,
'Shipment not found');
646 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
649 $result = $this->shipment->setClosed();
651 throw new RestException(304,
'Error nothing done. May be object is already closed');
654 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
658 $result = $this->shipment->fetch($id);
660 $this->shipment->fetchObjectLinked();
675 $object = parent::_cleanObjectDatas($object);
677 unset($object->thirdparty);
679 unset($object->note);
680 unset($object->address);
681 unset($object->barcode_type);
682 unset($object->barcode_type_code);
683 unset($object->barcode_type_label);
684 unset($object->barcode_type_coder);
686 if (!empty($object->lines) && is_array($object->lines)) {
687 foreach ($object->lines as $line) {
688 if (is_array($line->detail_batch)) {
689 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
690 unset($line->detail_batch[$keytmp2]->db);
693 unset($line->tva_tx);
694 unset($line->vat_src_code);
695 unset($line->total_ht);
696 unset($line->total_ttc);
697 unset($line->total_tva);
698 unset($line->total_localtax1);
699 unset($line->total_localtax2);
700 unset($line->remise_percent);
717 foreach (Shipments::$FIELDS as $field) {
718 if (!isset($data[$field])) {
719 throw new RestException(400,
"$field field missing");
721 $shipment[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage shipments.
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)
validate($id, $notrigger=0)
Validate a shipment.
__construct()
Constructor.
post($request_data=null)
Create shipment object.
_cleanObjectDatas($object)
Clean sensible object datas.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List shipments.
deleteLine($id, $lineid)
Get lines of an shipment.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.