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();
103 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
105 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
106 throw new RestException(403);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
121 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition AS t";
122 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"expedition_extrafields AS ef ON (ef.fk_object = t.rowid)";
123 $sql .=
' WHERE t.entity IN ('.getEntity(
'expedition').
')';
125 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
128 if ($search_sale && $search_sale !=
'-1') {
129 if ($search_sale == -2) {
130 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
131 } elseif ($search_sale > 0) {
132 $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).
")";
140 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
145 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
147 $sql .= $this->db->order($sortfield, $sortorder);
152 $offset = $limit * $page;
154 $sql .= $this->db->plimit($limit + 1, $offset);
158 $result = $this->db->query($sql);
161 $num = $this->db->num_rows($result);
162 $min = min($num, ($limit <= 0 ? $num : $limit));
165 $obj = $this->db->fetch_object($result);
167 if ($shipment_static->fetch($obj->rowid)) {
173 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
177 if ($pagination_data) {
178 $totalsResult = $this->db->query($sqlTotals);
179 $total = $this->db->fetch_object($totalsResult)->total;
184 $obj_ret[
'data'] = $tmp;
185 $obj_ret[
'pagination'] = [
186 'total' => (int) $total,
188 'page_count' => ceil((
int) $total / $limit),
202 public function post($request_data =
null)
204 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
205 throw new RestException(403,
"Insuffisant rights");
208 $result = $this->
_validate($request_data);
210 foreach ($request_data as $field => $value) {
211 if ($field ===
'caller') {
213 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
217 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
219 if (isset($request_data[
"lines"])) {
221 foreach ($request_data[
"lines"] as $line) {
224 $shipmentline->entrepot_id = $line[
'entrepot_id'];
225 $shipmentline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
226 $shipmentline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
227 $shipmentline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
228 $shipmentline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
229 $shipmentline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
230 $shipmentline->qty = $line[
'qty'];
231 $shipmentline->rang = $line[
'rang'];
232 $shipmentline->array_options = $line[
'array_options'];
233 $shipmentline->detail_batch = $line[
'detail_batch'];
235 $lines[] = $shipmentline;
237 $this->shipment->lines = $lines;
240 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
241 throw new RestException(500,
"Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
244 return $this->shipment->id;
428 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
429 throw new RestException(403);
432 $result = $this->shipment->fetch(
$id);
434 throw new RestException(404,
'Shipment not found');
438 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
443 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
444 if ($updateRes > 0) {
448 'message' =>
'line ' .$lineid.
' deleted'
452 throw new RestException(405, $this->shipment->error);
463 public function put(
$id, $request_data =
null)
465 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
466 throw new RestException(403);
469 $result = $this->shipment->fetch(
$id);
471 throw new RestException(404,
'Shipment not found');
475 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
477 foreach ($request_data as $field => $value) {
478 if ($field ==
'id') {
481 if ($field ===
'caller') {
483 $this->shipment->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
487 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
490 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
491 return $this->
get(
$id);
493 throw new RestException(500, $this->shipment->error);
504 public function delete(
$id)
506 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
507 throw new RestException(403);
509 $result = $this->shipment->fetch(
$id);
511 throw new RestException(404,
'Shipment not found');
515 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
518 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
519 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
525 'message' =>
'Shipment deleted'
551 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
552 throw new RestException(403);
554 $result = $this->shipment->fetch(
$id);
556 throw new RestException(404,
'Shipment not found');
560 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
565 throw new RestException(304,
'Error nothing done. May be object is already validated');
568 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
572 $result = $this->shipment->fetch(
$id);
574 $this->shipment->fetchObjectLinked();
673 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
674 throw new RestException(403);
677 $result = $this->shipment->fetch(
$id);
679 throw new RestException(404,
'Shipment not found');
683 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
686 $result = $this->shipment->setClosed();
688 throw new RestException(304,
'Error nothing done. May be object is already closed');
691 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
695 $result = $this->shipment->fetch(
$id);
697 $this->shipment->fetchObjectLinked();
721 unset(
$object->barcode_type_code);
722 unset(
$object->barcode_type_label);
723 unset(
$object->barcode_type_coder);
726 foreach (
$object->lines as $line) {
727 if (is_array($line->detail_batch)) {
728 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
729 unset($line->detail_batch[$keytmp2]->db);
732 unset($line->canvas);
734 unset($line->tva_tx);
735 unset($line->vat_src_code);
736 unset($line->total_ht);
737 unset($line->total_ttc);
738 unset($line->total_tva);
739 unset($line->total_localtax1);
740 unset($line->total_localtax2);
741 unset($line->remise_percent);
758 foreach (Shipments::$FIELDS as $field) {
759 if (!isset($data[$field])) {
760 throw new RestException(400,
"$field field missing");
762 $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 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.