21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
36 public static $FIELDS = array(
65 public function get(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
68 throw new RestException(403);
71 $result = $this->order->fetch(
$id);
73 throw new RestException(404,
'Supplier order not found');
77 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
80 $this->order->fetchObjectLinked();
106 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false)
108 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
109 throw new RestException(403);
115 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
119 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !empty($socids)) {
120 $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t";
125 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
126 if (!empty($product_ids)) {
127 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
129 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
130 if (!empty($product_ids)) {
131 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
134 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
137 if ($status ==
'draft') {
138 $sql .=
" AND t.fk_statut IN (0)";
140 if ($status ==
'validated') {
141 $sql .=
" AND t.fk_statut IN (1)";
143 if ($status ==
'approved') {
144 $sql .=
" AND t.fk_statut IN (2)";
146 if ($status ==
'running') {
147 $sql .=
" AND t.fk_statut IN (3)";
149 if ($status ==
'received_start') {
150 $sql .=
" AND t.fk_statut IN (4)";
152 if ($status ==
'received_end') {
153 $sql .=
" AND t.fk_statut IN (5)";
155 if ($status ==
'cancelled') {
156 $sql .=
" AND t.fk_statut IN (6,7)";
158 if ($status ==
'refused') {
159 $sql .=
" AND t.fk_statut IN (9)";
162 if ($search_sale && $search_sale !=
'-1') {
163 if ($search_sale == -2) {
164 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
165 } elseif ($search_sale > 0) {
166 $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).
")";
174 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
178 if ($sqlfilterlines) {
180 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
184 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
189 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
191 $sql .= $this->db->order($sortfield, $sortorder);
196 $offset = $limit * $page;
198 $sql .= $this->db->plimit($limit + 1, $offset);
201 $result = $this->db->query($sql);
204 $num = $this->db->num_rows($result);
205 $min = min($num, ($limit <= 0 ? $num : $limit));
207 $obj = $this->db->fetch_object($result);
209 if ($order_static->fetch($obj->rowid)) {
215 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
219 if ($pagination_data) {
220 $totalsResult = $this->db->query($sqlTotals);
221 $total = $this->db->fetch_object($totalsResult)->total;
226 $obj_ret[
'data'] = $tmp;
227 $obj_ret[
'pagination'] = [
228 'total' => (int) $total,
230 'page_count' => (
int) ceil((
int) $total / $limit),
248 public function post($request_data =
null)
250 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
251 throw new RestException(403,
"Insufficiant rights");
254 if (!is_array($request_data)) {
255 $request_data = array();
261 foreach ($request_data as $field => $value) {
262 if ($field ===
'caller') {
264 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
268 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
270 if (!array_keys($request_data,
'date')) {
271 $this->order->date =
dol_now();
282 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
285 return $this->order->id;
297 public function put(
$id, $request_data =
null)
299 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
300 throw new RestException(403);
303 $result = $this->order->fetch(
$id);
305 throw new RestException(404,
'Supplier order not found');
309 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
312 foreach ($request_data as $field => $value) {
313 if ($field ==
'id') {
316 if ($field ===
'caller') {
318 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
321 if ($field ==
'array_options' && is_array($value)) {
322 foreach ($value as $index => $val) {
323 $this->order->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->order);
328 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
331 if ($this->order->update(DolibarrApiAccess::$user)) {
332 return $this->
get(
$id);
352 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'creer')) {
353 throw new RestException(403);
356 $result = $this->order->fetch(
$id);
358 throw new RestException(404,
'Supplier order not found');
362 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
365 $request_data = (object) $request_data;
367 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
369 $updateRes = $this->order->addline(
371 $request_data->subprice,
373 $request_data->tva_tx,
374 $request_data->localtax1_tx,
375 $request_data->localtax2_tx,
376 $request_data->fk_product,
377 $request_data->fk_prod_fourn_price,
378 $request_data->ref_fourn,
379 $request_data->remise_percent,
380 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
381 $request_data->pu_ttc,
382 $request_data->product_type,
383 $request_data->info_bits,
384 $request_data->notrigger,
385 $request_data->date_start,
386 $request_data->date_end,
387 $request_data->array_options,
388 $request_data->fk_unit,
389 $request_data->multicurrency_subprice,
390 $request_data->origin,
391 $request_data->origin_id,
393 $request_data->special_code
396 if ($updateRes > 0) {
399 throw new RestException(400, $this->order->error);
419 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
420 throw new RestException(403);
423 $result = $this->order->fetch(
$id);
425 throw new RestException(404,
'Supplier order not found');
429 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
433 if ($source ==
'all' || $source ==
'external') {
434 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
435 $contacts = array_merge($contacts, $tmpContacts);
438 if ($source ==
'all' || $source ==
'internal') {
439 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
440 $contacts = array_merge($contacts, $tmpContacts);
464 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
465 throw new RestException(403);
468 $result = $this->order->fetch(
$id);
470 throw new RestException(404,
'Supplier order not found');
474 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
477 $result = $this->order->add_contact($contactid, $type, $source);
480 throw new RestException(500,
'Error when added the contact');
484 throw new RestException(304,
'contact already added');
490 'message' =>
'Contact linked to the order'
515 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
516 throw new RestException(403);
519 $result = $this->order->fetch(
$id);
521 throw new RestException(404,
'Supplier order not found');
525 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
530 $contactToUnlink = 0;
531 foreach ($contacts as $contact) {
532 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
533 $contactToUnlink = $contact[
'rowid'];
538 if ($contactToUnlink == 0) {
539 throw new RestException(404,
'Linked contact not found');
542 $result = $this->order->delete_contact($contact[
'rowid']);
545 throw new RestException(500,
'Error when deleted the contact');
551 'message' =>
'Contact unlinked from supplier order'
564 public function delete(
$id)
566 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
567 throw new RestException(403);
569 $result = $this->order->fetch(
$id);
571 throw new RestException(404,
'Supplier order not found');
575 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
578 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
579 throw new RestException(500,
'Error when deleting order');
585 'message' =>
'Supplier order deleted'
614 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
615 throw new RestException(403);
617 $result = $this->order->fetch(
$id);
619 throw new RestException(404,
'Order not found');
623 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
626 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
628 throw new RestException(304,
'Error nothing done. May be object is already validated');
631 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
637 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
663 public function approve(
$id, $idwarehouse = 0, $secondlevel = 0)
665 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
666 throw new RestException(403);
668 $result = $this->order->fetch(
$id);
670 throw new RestException(404,
'Order not found');
674 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
677 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
679 throw new RestException(304,
'Error nothing done. May be object is already approved');
682 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
688 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
719 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
720 throw new RestException(403);
722 $result = $this->order->fetch(
$id);
724 throw new RestException(404,
'Order not found');
728 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
731 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
733 throw new RestException(304,
'Error nothing done. May be object is already sent');
736 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
742 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
786 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
787 throw new RestException(403);
789 $result = $this->order->fetch(
$id);
791 throw new RestException(404,
'Order not found');
795 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
798 foreach ($lines as $line) {
799 $lineObj = (object) $line;
801 $result = $this->order->dispatchProduct(
802 DolibarrApiAccess::$user,
803 $lineObj->fk_product,
816 throw new RestException(500,
'Error dispatch order line '.$lineObj->id.
': '.$this->order->error);
820 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
823 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
826 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
832 'message' =>
'Order received (Ref='.$this->order->ref.
')'
854 unset(
$object->barcode_type_code);
855 unset(
$object->barcode_type_label);
856 unset(
$object->barcode_type_coder);
871 if ($data ===
null) {
875 foreach (SupplierOrders::$FIELDS as $field) {
876 if (!isset($data[$field])) {
877 throw new RestException(400,
"$field field missing");
879 $order[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage predefined suppliers products.
_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.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $product_ids='', $status='', $sqlfilters='', $sqlfilterlines='', $properties='', $pagination_data=false)
List orders.
__construct()
Constructor.
put($id, $request_data=null)
Update supplier order.
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
postLine($id, $request_data=null)
Add a line to a given supplier order.
deleteContact($id, $contactid, $type, $source)
Unlink a contact type of given supplier order.
makeOrder($id, $date, $method, $comment='')
Sends an order to the vendor.
postContact($id, $contactid, $type, $source)
Add a contact type of given supplier order.
getContacts($id, $source, $type='')
Get contacts of given supplier order.
_validate($data)
Validate fields before create or update object.
approve($id, $idwarehouse=0, $secondlevel=0)
Approve an order.
receiveOrder($id, $closeopenorder, $comment, $lines)
Receives the order, dispatches products.
dol_now($mode='gmt')
Return date for now.
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.