19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
34 public static $FIELDS = array(
65 public function get($id)
67 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
68 throw new RestException(401);
71 $result = $this->shipment->fetch($id);
73 throw new RestException(404,
'Shipment not found');
77 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
80 $this->shipment->fetchObjectLinked();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
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());
192 public function post($request_data =
null)
194 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
195 throw new RestException(401,
"Insuffisant rights");
198 $result = $this->
_validate($request_data);
200 foreach ($request_data as $field => $value) {
201 if ($field ===
'caller') {
203 $this->shipment->context[
'caller'] = $request_data[
'caller'];
207 $this->shipment->$field = $value;
209 if (isset($request_data[
"lines"])) {
211 foreach ($request_data[
"lines"] as $line) {
212 array_push($lines, (
object) $line);
214 $this->shipment->lines = $lines;
217 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
221 return $this->shipment->id;
405 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
406 throw new RestException(401);
409 $result = $this->shipment->fetch($id);
411 throw new RestException(404,
'Shipment not found');
415 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
421 if ($updateRes > 0) {
422 return $this->
get($id);
424 throw new RestException(405, $this->shipment->error);
436 public function put($id, $request_data =
null)
438 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
439 throw new RestException(401);
442 $result = $this->shipment->fetch($id);
444 throw new RestException(404,
'Shipment not found');
448 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
450 foreach ($request_data as $field => $value) {
451 if ($field ==
'id') {
454 if ($field ===
'caller') {
456 $this->shipment->context[
'caller'] = $request_data[
'caller'];
460 $this->shipment->$field = $value;
463 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
464 return $this->
get($id);
466 throw new RestException(500, $this->shipment->error);
477 public function delete($id)
479 if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
480 throw new RestException(401);
482 $result = $this->shipment->fetch($id);
484 throw new RestException(404,
'Shipment not found');
488 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
492 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
498 'message' =>
'Shipment deleted'
524 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
525 throw new RestException(401);
527 $result = $this->shipment->fetch($id);
529 throw new RestException(404,
'Shipment not found');
533 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
536 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
538 throw new RestException(304,
'Error nothing done. May be object is already validated');
541 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
545 $result = $this->shipment->fetch($id);
547 $this->shipment->fetchObjectLinked();
644 public function close($id, $notrigger = 0)
646 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
647 throw new RestException(401);
650 $result = $this->shipment->fetch($id);
652 throw new RestException(404,
'Shipment not found');
656 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
659 $result = $this->shipment->setClosed();
661 throw new RestException(304,
'Error nothing done. May be object is already closed');
664 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
668 $result = $this->shipment->fetch($id);
670 $this->shipment->fetchObjectLinked();
685 $object = parent::_cleanObjectDatas($object);
687 unset($object->thirdparty);
689 unset($object->note);
690 unset($object->address);
691 unset($object->barcode_type);
692 unset($object->barcode_type_code);
693 unset($object->barcode_type_label);
694 unset($object->barcode_type_coder);
696 if (!empty($object->lines) && is_array($object->lines)) {
697 foreach ($object->lines as $line) {
698 if (is_array($line->detail_batch)) {
699 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
700 unset($line->detail_batch[$keytmp2]->db);
703 unset($line->tva_tx);
704 unset($line->vat_src_code);
705 unset($line->total_ht);
706 unset($line->total_ttc);
707 unset($line->total_tva);
708 unset($line->total_localtax1);
709 unset($line->total_localtax2);
710 unset($line->remise_percent);
727 foreach (Shipments::$FIELDS as $field) {
728 if (!isset($data[$field])) {
729 throw new RestException(400,
"$field field missing");
731 $shipment[$field] = $data[$field];
_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.
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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.