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->hasRight(
'expedition',
'lire')) {
68 throw new RestException(403);
71 $result = $this->shipment->fetch($id);
73 throw new RestException(404,
'Shipment not found');
77 throw new RestException(403,
'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 =
'')
104 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
105 throw new RestException(403);
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
115 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
121 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
122 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
124 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
127 if ($search_sale && $search_sale !=
'-1') {
128 if ($search_sale == -2) {
129 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
130 } elseif ($search_sale > 0) {
131 $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).
")";
139 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
143 $sql .= $this->db->order($sortfield, $sortorder);
148 $offset = $limit * $page;
150 $sql .= $this->db->plimit($limit + 1, $offset);
154 $result = $this->db->query($sql);
157 $num = $this->db->num_rows($result);
158 $min = min($num, ($limit <= 0 ? $num : $limit));
161 $obj = $this->db->fetch_object($result);
163 if ($shipment_static->fetch($obj->rowid)) {
169 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
181 public function post($request_data =
null)
183 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
184 throw new RestException(403,
"Insuffisant rights");
187 $result = $this->
_validate($request_data);
189 foreach ($request_data as $field => $value) {
190 if ($field ===
'caller') {
192 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
196 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
198 if (isset($request_data[
"lines"])) {
200 foreach ($request_data[
"lines"] as $line) {
203 $shipmentline->entrepot_id = $line[
'entrepot_id'];
204 $shipmentline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
205 $shipmentline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
206 $shipmentline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
207 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
208 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
209 $shipmentline->qty = $line[
'qty'];
210 $shipmentline->rang = $line[
'rang'];
211 $shipmentline->array_options = $line[
'array_options'];
212 $shipmentline->detail_batch = $line[
'detail_batch'];
214 $lines[] = $shipmentline;
216 $this->shipment->lines = $lines;
219 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
223 return $this->shipment->id;
407 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
408 throw new RestException(403);
411 $result = $this->shipment->fetch($id);
413 throw new RestException(404,
'Shipment not found');
417 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
422 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
423 if ($updateRes > 0) {
427 'message' =>
'line ' .$lineid.
' deleted'
431 throw new RestException(405, $this->shipment->error);
442 public function put($id, $request_data =
null)
444 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
445 throw new RestException(403);
448 $result = $this->shipment->fetch($id);
450 throw new RestException(404,
'Shipment not found');
454 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 foreach ($request_data as $field => $value) {
457 if ($field ==
'id') {
460 if ($field ===
'caller') {
462 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
466 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
469 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
470 return $this->
get($id);
472 throw new RestException(500, $this->shipment->error);
483 public function delete($id)
485 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
486 throw new RestException(403);
488 $result = $this->shipment->fetch($id);
490 throw new RestException(404,
'Shipment not found');
494 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
498 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
504 'message' =>
'Shipment deleted'
530 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
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 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
544 throw new RestException(304,
'Error nothing done. May be object is already validated');
547 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
551 $result = $this->shipment->fetch($id);
553 $this->shipment->fetchObjectLinked();
650 public function close($id, $notrigger = 0)
652 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
653 throw new RestException(403);
656 $result = $this->shipment->fetch($id);
658 throw new RestException(404,
'Shipment not found');
662 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 $result = $this->shipment->setClosed();
667 throw new RestException(304,
'Error nothing done. May be object is already closed');
670 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
674 $result = $this->shipment->fetch($id);
676 $this->shipment->fetchObjectLinked();
700 unset(
$object->barcode_type_code);
701 unset(
$object->barcode_type_label);
702 unset(
$object->barcode_type_coder);
705 foreach (
$object->lines as $line) {
706 if (is_array($line->detail_batch)) {
707 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
708 unset($line->detail_batch[$keytmp2]->db);
711 unset($line->canvas);
713 unset($line->tva_tx);
714 unset($line->vat_src_code);
715 unset($line->total_ht);
716 unset($line->total_ttc);
717 unset($line->total_tva);
718 unset($line->total_localtax1);
719 unset($line->total_localtax2);
720 unset($line->remise_percent);
737 foreach (Shipments::$FIELDS as $field) {
738 if (!isset($data[$field])) {
739 throw new RestException(400,
"$field field missing");
741 $shipment[$field] = $data[$field];
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 shipments.
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='')
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.