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');
486 if ($field ==
'array_options' && is_array($value)) {
487 foreach ($value as $index => $val) {
488 $this->shipment->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->shipment);
492 $this->shipment->$field = $this->
_checkValForAPI($field, $value, $this->shipment);
495 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
496 return $this->
get(
$id);
498 throw new RestException(500, $this->shipment->error);
509 public function delete(
$id)
511 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'supprimer')) {
512 throw new RestException(403);
514 $result = $this->shipment->fetch(
$id);
516 throw new RestException(404,
'Shipment not found');
520 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
523 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
524 throw new RestException(500,
'Error when deleting shipment : '.$this->shipment->error);
530 'message' =>
'Shipment deleted'
556 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
557 throw new RestException(403);
559 $result = $this->shipment->fetch(
$id);
561 throw new RestException(404,
'Shipment not found');
565 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
568 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
570 throw new RestException(304,
'Error nothing done. May be object is already validated');
573 throw new RestException(500,
'Error when validating Shipment: '.$this->shipment->error);
577 $result = $this->shipment->fetch(
$id);
579 $this->shipment->fetchObjectLinked();
678 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
679 throw new RestException(403);
682 $result = $this->shipment->fetch(
$id);
684 throw new RestException(404,
'Shipment not found');
688 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
691 $result = $this->shipment->setClosed();
693 throw new RestException(304,
'Error nothing done. May be object is already closed');
696 throw new RestException(500,
'Error when closing Order: '.$this->shipment->error);
700 $result = $this->shipment->fetch(
$id);
702 $this->shipment->fetchObjectLinked();
726 unset(
$object->barcode_type_code);
727 unset(
$object->barcode_type_label);
728 unset(
$object->barcode_type_coder);
731 foreach (
$object->lines as $line) {
732 if (is_array($line->detail_batch)) {
733 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
734 unset($line->detail_batch[$keytmp2]->db);
737 unset($line->canvas);
739 unset($line->tva_tx);
740 unset($line->vat_src_code);
741 unset($line->total_ht);
742 unset($line->total_ttc);
743 unset($line->total_tva);
744 unset($line->total_localtax1);
745 unset($line->total_localtax2);
746 unset($line->remise_percent);
763 foreach (Shipments::$FIELDS as $field) {
764 if (!isset($data[$field])) {
765 throw new RestException(400,
"$field field missing");
767 $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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...