21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
37 public static $FIELDS = array(
66 public function get(
$id)
68 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
69 throw new RestException(403);
72 $result = $this->order->fetch(
$id);
74 throw new RestException(404,
'Supplier order not found');
78 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->order->fetchObjectLinked();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false)
109 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
110 throw new RestException(403);
116 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
120 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !empty($socids)) {
121 $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 if (!empty($product_ids)) {
128 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
130 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
131 if (!empty($product_ids)) {
132 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
135 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
138 if ($status ==
'draft') {
139 $sql .=
" AND t.fk_statut IN (0)";
141 if ($status ==
'validated') {
142 $sql .=
" AND t.fk_statut IN (1)";
144 if ($status ==
'approved') {
145 $sql .=
" AND t.fk_statut IN (2)";
147 if ($status ==
'running') {
148 $sql .=
" AND t.fk_statut IN (3)";
150 if ($status ==
'received_start') {
151 $sql .=
" AND t.fk_statut IN (4)";
153 if ($status ==
'received_end') {
154 $sql .=
" AND t.fk_statut IN (5)";
156 if ($status ==
'cancelled') {
157 $sql .=
" AND t.fk_statut IN (6,7)";
159 if ($status ==
'refused') {
160 $sql .=
" AND t.fk_statut IN (9)";
163 if ($search_sale && $search_sale !=
'-1') {
164 if ($search_sale == -2) {
165 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
166 } elseif ($search_sale > 0) {
167 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
175 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
179 if ($sqlfilterlines) {
181 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
185 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
190 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
192 $sql .= $this->db->order($sortfield, $sortorder);
197 $offset = $limit * $page;
199 $sql .= $this->db->plimit($limit + 1, $offset);
202 $result = $this->db->query($sql);
205 $num = $this->db->num_rows($result);
206 $min = min($num, ($limit <= 0 ? $num : $limit));
208 $obj = $this->db->fetch_object($result);
210 if ($order_static->fetch($obj->rowid)) {
216 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
220 if ($pagination_data) {
221 $totalsResult = $this->db->query($sqlTotals);
222 $total = $this->db->fetch_object($totalsResult)->total;
227 $obj_ret[
'data'] = $tmp;
228 $obj_ret[
'pagination'] = [
229 'total' => (int) $total,
231 'page_count' => (
int) ceil((
int) $total / $limit),
249 public function post($request_data =
null)
251 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
252 throw new RestException(403,
"Insufficiant rights");
255 if (!is_array($request_data)) {
256 $request_data = array();
262 foreach ($request_data as $field => $value) {
263 if ($field ===
'caller') {
265 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
269 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
271 if (!array_keys($request_data,
'date')) {
272 $this->order->date =
dol_now();
283 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
284 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
286 return $this->order->id;
298 public function put(
$id, $request_data =
null)
300 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
301 throw new RestException(403);
304 $result = $this->order->fetch(
$id);
306 throw new RestException(404,
'Supplier order not found');
310 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
313 foreach ($request_data as $field => $value) {
314 if ($field ==
'id') {
317 if ($field ===
'caller') {
319 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
322 if ($field ==
'array_options' && is_array($value)) {
323 foreach ($value as $index => $val) {
329 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
332 if ($this->order->update(DolibarrApiAccess::$user)) {
333 return $this->
get(
$id);
353 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'creer')) {
354 throw new RestException(403);
357 $result = $this->order->fetch(
$id);
359 throw new RestException(404,
'Supplier order not found');
363 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 $request_data = (object) $request_data;
368 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
370 $updateRes = $this->order->addline(
372 $request_data->subprice,
374 $request_data->tva_tx,
375 $request_data->localtax1_tx,
376 $request_data->localtax2_tx,
377 $request_data->fk_product,
378 $request_data->fk_prod_fourn_price,
379 $request_data->ref_fourn,
380 $request_data->remise_percent,
381 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
382 $request_data->pu_ttc,
383 $request_data->product_type,
384 $request_data->info_bits,
385 $request_data->notrigger,
386 $request_data->date_start,
387 $request_data->date_end,
388 $request_data->array_options,
389 $request_data->fk_unit,
390 $request_data->multicurrency_subprice,
391 $request_data->origin,
392 $request_data->origin_id,
394 $request_data->special_code
397 if ($updateRes > 0) {
400 throw new RestException(400, $this->order->error);
420 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
421 throw new RestException(403);
424 $result = $this->order->fetch(
$id);
426 throw new RestException(404,
'Supplier order not found');
430 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
434 if ($source ==
'all' || $source ==
'external') {
435 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
436 $contacts = array_merge($contacts, $tmpContacts);
439 if ($source ==
'all' || $source ==
'internal') {
440 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
441 $contacts = array_merge($contacts, $tmpContacts);
465 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
466 throw new RestException(403);
469 $result = $this->order->fetch(
$id);
471 throw new RestException(404,
'Supplier order not found');
475 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
478 $result = $this->order->add_contact($contactid, $type, $source);
481 throw new RestException(500,
'Error when added the contact');
485 throw new RestException(304,
'contact already added');
491 'message' =>
'Contact linked to the order'
516 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
517 throw new RestException(403);
520 $result = $this->order->fetch(
$id);
522 throw new RestException(404,
'Supplier order not found');
526 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
529 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
531 $contactToUnlink = 0;
532 foreach ($contacts as $contact) {
533 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
534 $contactToUnlink = $contact[
'rowid'];
539 if ($contactToUnlink == 0) {
540 throw new RestException(404,
'Linked contact not found');
543 $result = $this->order->delete_contact($contactToUnlink);
546 throw new RestException(500,
'Error when deleting the contact');
552 'message' =>
'Contact unlinked from supplier order'
565 public function delete(
$id)
567 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
568 throw new RestException(403);
570 $result = $this->order->fetch(
$id);
572 throw new RestException(404,
'Supplier order not found');
576 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
579 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
580 throw new RestException(500,
'Error when deleting order');
586 'message' =>
'Supplier order deleted'
615 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
616 throw new RestException(403);
618 $result = $this->order->fetch(
$id);
620 throw new RestException(404,
'Order not found');
624 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
627 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
629 throw new RestException(304,
'Error nothing done. May be object is already validated');
632 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
638 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
664 public function approve(
$id, $idwarehouse = 0, $secondlevel = 0)
666 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
667 throw new RestException(403);
669 $result = $this->order->fetch(
$id);
671 throw new RestException(404,
'Order not found');
675 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
678 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
680 throw new RestException(304,
'Error nothing done. May be object is already approved');
683 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
689 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
720 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
721 throw new RestException(403);
723 $result = $this->order->fetch(
$id);
725 throw new RestException(404,
'Order not found');
729 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
732 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
734 throw new RestException(304,
'Error nothing done. May be object is already sent');
737 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
743 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
787 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
788 throw new RestException(403);
790 $result = $this->order->fetch(
$id);
792 throw new RestException(404,
'Order not found');
796 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
799 foreach ($lines as $line) {
800 $lineObj = (object) $line;
802 $result = $this->order->dispatchProduct(
803 DolibarrApiAccess::$user,
804 $lineObj->fk_product,
817 throw new RestException(500,
'Error dispatch order line '.$lineObj->id.
': '.$this->order->error);
821 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
824 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
827 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
833 'message' =>
'Order received (Ref='.$this->order->ref.
')'
855 unset(
$object->barcode_type_code);
856 unset(
$object->barcode_type_label);
857 unset(
$object->barcode_type_coder);
872 if ($data ===
null) {
876 foreach (SupplierOrders::$FIELDS as $field) {
877 if (!isset($data[$field])) {
878 throw new RestException(400,
"$field field missing");
880 $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.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.