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,
394 isset($request_data->rang) ? $request_data->rang : -1,
395 $request_data->special_code
398 if ($updateRes > 0) {
401 throw new RestException(400, $this->order->error);
421 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
422 throw new RestException(403);
425 $result = $this->order->fetch(
$id);
427 throw new RestException(404,
'Supplier order not found');
431 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 if ($source ==
'all' || $source ==
'external') {
436 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
437 $contacts = array_merge($contacts, $tmpContacts);
440 if ($source ==
'all' || $source ==
'internal') {
441 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
442 $contacts = array_merge($contacts, $tmpContacts);
466 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
467 throw new RestException(403);
470 $result = $this->order->fetch(
$id);
472 throw new RestException(404,
'Supplier order not found');
476 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 $result = $this->order->add_contact($contactid, $type, $source);
482 throw new RestException(500,
'Error when added the contact');
486 throw new RestException(304,
'contact already added');
492 'message' =>
'Contact linked to the order'
517 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
518 throw new RestException(403);
521 $result = $this->order->fetch(
$id);
523 throw new RestException(404,
'Supplier order not found');
527 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
530 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
532 $contactToUnlink = 0;
533 foreach ($contacts as $contact) {
534 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
535 $contactToUnlink = $contact[
'rowid'];
540 if ($contactToUnlink == 0) {
541 throw new RestException(404,
'Linked contact not found');
544 $result = $this->order->delete_contact($contact[
'rowid']);
547 throw new RestException(500,
'Error when deleted the contact');
553 'message' =>
'Contact unlinked from supplier order'
566 public function delete(
$id)
568 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
569 throw new RestException(403);
571 $result = $this->order->fetch(
$id);
573 throw new RestException(404,
'Supplier order not found');
577 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
580 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
581 throw new RestException(500,
'Error when deleting order');
587 'message' =>
'Supplier order deleted'
616 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
617 throw new RestException(403);
619 $result = $this->order->fetch(
$id);
621 throw new RestException(404,
'Order not found');
625 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
630 throw new RestException(304,
'Error nothing done. May be object is already validated');
633 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
639 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
665 public function approve(
$id, $idwarehouse = 0, $secondlevel = 0)
667 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
668 throw new RestException(403);
670 $result = $this->order->fetch(
$id);
672 throw new RestException(404,
'Order not found');
676 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
681 throw new RestException(304,
'Error nothing done. May be object is already approved');
684 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
690 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
721 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
722 throw new RestException(403);
724 $result = $this->order->fetch(
$id);
726 throw new RestException(404,
'Order not found');
730 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
733 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
735 throw new RestException(304,
'Error nothing done. May be object is already sent');
738 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
744 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
788 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
789 throw new RestException(403);
791 $result = $this->order->fetch(
$id);
793 throw new RestException(404,
'Order not found');
797 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
800 foreach ($lines as $line) {
801 $lineObj = (object) $line;
803 $result = $this->order->dispatchProduct(
804 DolibarrApiAccess::$user,
805 $lineObj->fk_product,
818 throw new RestException(500,
'Error dispatch order line '.$lineObj->id.
': '.$this->order->error);
822 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
825 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
828 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
834 'message' =>
'Order received (Ref='.$this->order->ref.
')'
856 unset(
$object->barcode_type_code);
857 unset(
$object->barcode_type_label);
858 unset(
$object->barcode_type_coder);
873 if ($data ===
null) {
877 foreach (SupplierOrders::$FIELDS as $field) {
878 if (!isset($data[$field])) {
879 throw new RestException(400,
"$field field missing");
881 $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.