20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
35 public static $FIELDS = array(
64 public function get(
$id)
66 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
67 throw new RestException(403);
70 $result = $this->order->fetch(
$id);
72 throw new RestException(404,
'Supplier order not found');
76 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 $this->order->fetchObjectLinked();
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false)
107 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
108 throw new RestException(403);
114 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
118 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !empty($socids)) {
119 $search_sale = DolibarrApiAccess::$user->id;
122 $sql =
"SELECT t.rowid";
123 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t";
124 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
125 if (!empty($product_ids)) {
126 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
128 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
129 if (!empty($product_ids)) {
130 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
133 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
136 if ($status ==
'draft') {
137 $sql .=
" AND t.fk_statut IN (0)";
139 if ($status ==
'validated') {
140 $sql .=
" AND t.fk_statut IN (1)";
142 if ($status ==
'approved') {
143 $sql .=
" AND t.fk_statut IN (2)";
145 if ($status ==
'running') {
146 $sql .=
" AND t.fk_statut IN (3)";
148 if ($status ==
'received_start') {
149 $sql .=
" AND t.fk_statut IN (4)";
151 if ($status ==
'received_end') {
152 $sql .=
" AND t.fk_statut IN (5)";
154 if ($status ==
'cancelled') {
155 $sql .=
" AND t.fk_statut IN (6,7)";
157 if ($status ==
'refused') {
158 $sql .=
" AND t.fk_statut IN (9)";
161 if ($search_sale && $search_sale !=
'-1') {
162 if ($search_sale == -2) {
163 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
164 } elseif ($search_sale > 0) {
165 $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).
")";
173 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
177 if ($sqlfilterlines) {
179 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
183 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
188 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
190 $sql .= $this->db->order($sortfield, $sortorder);
195 $offset = $limit * $page;
197 $sql .= $this->db->plimit($limit + 1, $offset);
200 $result = $this->db->query($sql);
203 $num = $this->db->num_rows($result);
204 $min = min($num, ($limit <= 0 ? $num : $limit));
206 $obj = $this->db->fetch_object($result);
208 if ($order_static->fetch($obj->rowid)) {
214 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
218 if ($pagination_data) {
219 $totalsResult = $this->db->query($sqlTotals);
220 $total = $this->db->fetch_object($totalsResult)->total;
225 $obj_ret[
'data'] = $tmp;
226 $obj_ret[
'pagination'] = [
227 'total' => (int) $total,
229 'page_count' => (
int) ceil((
int) $total / $limit),
247 public function post($request_data =
null)
249 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
250 throw new RestException(403,
"Insuffisant rights");
253 if (!is_array($request_data)) {
254 $request_data = array();
260 foreach ($request_data as $field => $value) {
261 if ($field ===
'caller') {
263 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
267 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
269 if (!array_keys($request_data,
'date')) {
270 $this->order->date =
dol_now();
281 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
282 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
284 return $this->order->id;
296 public function put(
$id, $request_data =
null)
298 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
299 throw new RestException(403);
302 $result = $this->order->fetch(
$id);
304 throw new RestException(404,
'Supplier order not found');
308 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
311 foreach ($request_data as $field => $value) {
312 if ($field ==
'id') {
315 if ($field ===
'caller') {
317 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
320 if ($field ==
'array_options' && is_array($value)) {
321 foreach ($value as $index => $val) {
322 $this->order->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->order);
327 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
330 if ($this->order->update(DolibarrApiAccess::$user)) {
331 return $this->
get(
$id);
353 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
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);
367 if ($source ==
'all' || $source ==
'external') {
368 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
369 $contacts = array_merge($contacts, $tmpContacts);
372 if ($source ==
'all' || $source ==
'internal') {
373 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
374 $contacts = array_merge($contacts, $tmpContacts);
398 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
399 throw new RestException(403);
402 $result = $this->order->fetch(
$id);
404 throw new RestException(404,
'Supplier order not found');
408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
411 $result = $this->order->add_contact($contactid, $type, $source);
414 throw new RestException(500,
'Error when added the contact');
418 throw new RestException(304,
'contact already added');
424 'message' =>
'Contact linked to the order'
449 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
450 throw new RestException(403);
453 $result = $this->order->fetch(
$id);
455 throw new RestException(404,
'Supplier order not found');
459 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
464 $contactToUnlink = 0;
465 foreach ($contacts as $contact) {
466 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
467 $contactToUnlink = $contact[
'rowid'];
472 if ($contactToUnlink == 0) {
473 throw new RestException(404,
'Linked contact not found');
476 $result = $this->order->delete_contact($contact[
'rowid']);
479 throw new RestException(500,
'Error when deleted the contact');
485 'message' =>
'Contact unlinked from supplier order'
498 public function delete(
$id)
500 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
501 throw new RestException(403);
503 $result = $this->order->fetch(
$id);
505 throw new RestException(404,
'Supplier order not found');
509 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
512 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
513 throw new RestException(500,
'Error when deleting order');
519 'message' =>
'Supplier order deleted'
548 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
549 throw new RestException(403);
551 $result = $this->order->fetch(
$id);
553 throw new RestException(404,
'Order not found');
557 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
560 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
562 throw new RestException(304,
'Error nothing done. May be object is already validated');
565 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
571 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
597 public function approve(
$id, $idwarehouse = 0, $secondlevel = 0)
599 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
600 throw new RestException(403);
602 $result = $this->order->fetch(
$id);
604 throw new RestException(404,
'Order not found');
608 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
611 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
613 throw new RestException(304,
'Error nothing done. May be object is already approved');
616 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
622 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
653 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
654 throw new RestException(403);
656 $result = $this->order->fetch(
$id);
658 throw new RestException(404,
'Order not found');
662 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
667 throw new RestException(304,
'Error nothing done. May be object is already sent');
670 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
676 'message' =>
'Order sent (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 foreach ($lines as $line) {
733 $lineObj = (object) $line;
735 $result = $this->order->dispatchProduct(
736 DolibarrApiAccess::$user,
737 $lineObj->fk_product,
750 throw new RestException(500,
'Error dispatch order line '.$lineObj->id.
': '.$this->order->error);
754 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
757 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
760 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
766 'message' =>
'Order received (Ref='.$this->order->ref.
')'
785 unset(
$object->barcode_type_code);
786 unset(
$object->barcode_type_label);
787 unset(
$object->barcode_type_coder);
802 if ($data ===
null) {
806 foreach (SupplierOrders::$FIELDS as $field) {
807 if (!isset($data[$field])) {
808 throw new RestException(400,
"$field field missing");
810 $order[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...