dolibarr  19.0.0-dev
api_receptions.class.php
1 <?php
2 /* Copyright (C) 2022 Quatadah Nasdami <quatadah.nasdami@gmail.com>
3  * Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
22 
29 class Receptions extends DolibarrApi
30 {
31 
35  public static $FIELDS = array(
36  'socid',
37  'origin_id',
38  'origin_type',
39  );
40 
44  public $reception;
45 
49  public function __construct()
50  {
51  global $db, $conf;
52  $this->db = $db;
53  $this->reception = new Reception($this->db);
54  }
55 
65  public function get($id)
66  {
67  if (!DolibarrApiAccess::$user->rights->reception->lire) {
68  throw new RestException(401);
69  }
70 
71  $result = $this->reception->fetch($id);
72  if (!$result) {
73  throw new RestException(404, 'Reception not found');
74  }
75 
76  if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
77  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78  }
79 
80  $this->reception->fetchObjectLinked();
81  return $this->_cleanObjectDatas($this->reception);
82  }
83 
84 
85 
101  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
102  {
103  global $db, $conf;
104 
105  if (!DolibarrApiAccess::$user->rights->reception->lire) {
106  throw new RestException(401);
107  }
108 
109  $obj_ret = array();
110 
111  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
112  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113 
114  // If the internal user must only see his customers, force searching by him
115  $search_sale = 0;
116  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
117  $search_sale = DolibarrApiAccess::$user->id;
118  }
119 
120  $sql = "SELECT t.rowid";
121  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
122  $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
123  }
124  $sql .= " FROM ".MAIN_DB_PREFIX."reception AS t LEFT JOIN ".MAIN_DB_PREFIX."reception_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
125 
126  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
127  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
128  }
129 
130  $sql .= ' WHERE t.entity IN ('.getEntity('reception').')';
131  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
132  $sql .= " AND t.fk_soc = sc.fk_soc";
133  }
134  if ($socids) {
135  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
136  }
137  if ($search_sale > 0) {
138  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
139  }
140  // Insert sale filter
141  if ($search_sale > 0) {
142  $sql .= " AND sc.fk_user = ".((int) $search_sale);
143  }
144  // Add sql filters
145  if ($sqlfilters) {
146  $errormessage = '';
147  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
148  if ($errormessage) {
149  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
150  }
151  }
152 
153  $sql .= $this->db->order($sortfield, $sortorder);
154  if ($limit) {
155  if ($page < 0) {
156  $page = 0;
157  }
158  $offset = $limit * $page;
159 
160  $sql .= $this->db->plimit($limit + 1, $offset);
161  }
162 
163  dol_syslog("API Rest request");
164  $result = $this->db->query($sql);
165 
166  if ($result) {
167  $num = $this->db->num_rows($result);
168  $min = min($num, ($limit <= 0 ? $num : $limit));
169  $i = 0;
170  while ($i < $min) {
171  $obj = $this->db->fetch_object($result);
172  $reception_static = new Reception($this->db);
173  if ($reception_static->fetch($obj->rowid)) {
174  $obj_ret[] = $this->_cleanObjectDatas($reception_static);
175  }
176  $i++;
177  }
178  } else {
179  throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
180  }
181  if (!count($obj_ret)) {
182  throw new RestException(404, 'No reception found');
183  }
184  return $obj_ret;
185  }
186 
193  public function post($request_data = null)
194  {
195  if (!DolibarrApiAccess::$user->rights->reception->creer) {
196  throw new RestException(401, "Insuffisant rights");
197  }
198  // Check mandatory fields
199  $result = $this->_validate($request_data);
200 
201  foreach ($request_data as $field => $value) {
202  $this->reception->$field = $value;
203  }
204  if (isset($request_data["lines"])) {
205  $lines = array();
206  foreach ($request_data["lines"] as $line) {
207  array_push($lines, (object) $line);
208  }
209  $this->reception->lines = $lines;
210  }
211 
212  if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
213  throw new RestException(500, "Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
214  }
215 
216  return $this->reception->id;
217  }
218 
219  // /**
220  // * Get lines of an reception
221  // *
222  // * @param int $id Id of reception
223  // *
224  // * @url GET {id}/lines
225  // *
226  // * @return int
227  // */
228  /*
229  public function getLines($id)
230  {
231  if(! DolibarrApiAccess::$user->rights->reception->lire) {
232  throw new RestException(401);
233  }
234 
235  $result = $this->reception->fetch($id);
236  if( ! $result ) {
237  throw new RestException(404, 'Reception not found');
238  }
239 
240  if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
241  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
242  }
243  $this->reception->getLinesArray();
244  $result = array();
245  foreach ($this->reception->lines as $line) {
246  array_push($result,$this->_cleanObjectDatas($line));
247  }
248  return $result;
249  }
250  */
251 
252  // /**
253  // * Add a line to given reception
254  // *
255  // * @param int $id Id of reception to update
256  // * @param array $request_data ShipmentLine data
257  // *
258  // * @url POST {id}/lines
259  // *
260  // * @return int
261  // */
262  /*
263  public function postLine($id, $request_data = null)
264  {
265  if(! DolibarrApiAccess::$user->rights->reception->creer) {
266  throw new RestException(401);
267  }
268 
269  $result = $this->reception->fetch($id);
270  if ( ! $result ) {
271  throw new RestException(404, 'Reception not found');
272  }
273 
274  if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
275  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
276  }
277 
278  $request_data = (object) $request_data;
279 
280  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
281  $request_data->label = sanitizeVal($request_data->label);
282 
283  $updateRes = $this->reception->addline(
284  $request_data->desc,
285  $request_data->subprice,
286  $request_data->qty,
287  $request_data->tva_tx,
288  $request_data->localtax1_tx,
289  $request_data->localtax2_tx,
290  $request_data->fk_product,
291  $request_data->remise_percent,
292  $request_data->info_bits,
293  $request_data->fk_remise_except,
294  'HT',
295  0,
296  $request_data->date_start,
297  $request_data->date_end,
298  $request_data->product_type,
299  $request_data->rang,
300  $request_data->special_code,
301  $fk_parent_line,
302  $request_data->fk_fournprice,
303  $request_data->pa_ht,
304  $request_data->label,
305  $request_data->array_options,
306  $request_data->fk_unit,
307  $request_data->origin,
308  $request_data->origin_id,
309  $request_data->multicurrency_subprice
310  );
311 
312  if ($updateRes > 0) {
313  return $updateRes;
314 
315  }
316  return false;
317  }*/
318 
319  // /**
320  // * Update a line to given reception
321  // *
322  // * @param int $id Id of reception to update
323  // * @param int $lineid Id of line to update
324  // * @param array $request_data ShipmentLine data
325  // *
326  // * @url PUT {id}/lines/{lineid}
327  // *
328  // * @return object
329  // */
330  /*
331  public function putLine($id, $lineid, $request_data = null)
332  {
333  if (! DolibarrApiAccess::$user->rights->reception->creer) {
334  throw new RestException(401);
335  }
336 
337  $result = $this->reception->fetch($id);
338  if ( ! $result ) {
339  throw new RestException(404, 'Reception not found');
340  }
341 
342  if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
343  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
344  }
345 
346  $request_data = (object) $request_data;
347 
348  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
349  $request_data->label = sanitizeVal($request_data->label);
350 
351  $updateRes = $this->reception->updateline(
352  $lineid,
353  $request_data->desc,
354  $request_data->subprice,
355  $request_data->qty,
356  $request_data->remise_percent,
357  $request_data->tva_tx,
358  $request_data->localtax1_tx,
359  $request_data->localtax2_tx,
360  'HT',
361  $request_data->info_bits,
362  $request_data->date_start,
363  $request_data->date_end,
364  $request_data->product_type,
365  $request_data->fk_parent_line,
366  0,
367  $request_data->fk_fournprice,
368  $request_data->pa_ht,
369  $request_data->label,
370  $request_data->special_code,
371  $request_data->array_options,
372  $request_data->fk_unit,
373  $request_data->multicurrency_subprice
374  );
375 
376  if ($updateRes > 0) {
377  $result = $this->get($id);
378  unset($result->line);
379  return $this->_cleanObjectDatas($result);
380  }
381  return false;
382  }*/
383 
396  public function deleteLine($id, $lineid)
397  {
398  if (!DolibarrApiAccess::$user->rights->reception->creer) {
399  throw new RestException(401);
400  }
401 
402  $result = $this->reception->fetch($id);
403  if (!$result) {
404  throw new RestException(404, 'Reception not found');
405  }
406 
407  if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
408  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
409  }
410 
411  // TODO Check the lineid $lineid is a line of object
412 
413  $updateRes = $this->reception->deleteline(DolibarrApiAccess::$user, $lineid);
414  if ($updateRes < 0) {
415  throw new RestException(405, $this->reception->error);
416  }
417 
418  return array(
419  'success' => array(
420  'code' => 200,
421  'message' => 'Line deleted'
422  )
423  );
424  }
425 
433  public function put($id, $request_data = null)
434  {
435  if (!DolibarrApiAccess::$user->rights->reception->creer) {
436  throw new RestException(401);
437  }
438 
439  $result = $this->reception->fetch($id);
440  if (!$result) {
441  throw new RestException(404, 'Reception not found');
442  }
443 
444  if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
445  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
446  }
447  foreach ($request_data as $field => $value) {
448  if ($field == 'id') {
449  continue;
450  }
451  $this->reception->$field = $value;
452  }
453 
454  if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
455  return $this->get($id);
456  } else {
457  throw new RestException(500, $this->reception->error);
458  }
459  }
460 
467  public function delete($id)
468  {
469  if (!DolibarrApiAccess::$user->rights->reception->supprimer) {
470  throw new RestException(401);
471  }
472  $result = $this->reception->fetch($id);
473  if (!$result) {
474  throw new RestException(404, 'Reception not found');
475  }
476 
477  if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
478  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
479  }
480 
481  if (!$this->reception->delete(DolibarrApiAccess::$user)) {
482  throw new RestException(500, 'Error when deleting reception : '.$this->reception->error);
483  }
484 
485  return array(
486  'success' => array(
487  'code' => 200,
488  'message' => 'Reception deleted'
489  )
490  );
491  }
492 
512  public function validate($id, $notrigger = 0)
513  {
514  if (!DolibarrApiAccess::$user->rights->reception->creer) {
515  throw new RestException(401);
516  }
517  $result = $this->reception->fetch($id);
518  if (!$result) {
519  throw new RestException(404, 'Reception not found');
520  }
521 
522  if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
523  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
524  }
525 
526  $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
527  if ($result == 0) {
528  throw new RestException(304, 'Error nothing done. May be object is already validated');
529  }
530  if ($result < 0) {
531  throw new RestException(500, 'Error when validating Reception: '.$this->reception->error);
532  }
533 
534  // Reload reception
535  $result = $this->reception->fetch($id);
536 
537  $this->reception->fetchObjectLinked();
538  return $this->_cleanObjectDatas($this->reception);
539  }
540 
541 
542  // /**
543  // * Classify the reception as invoiced
544  // *
545  // * @param int $id Id of the reception
546  // *
547  // * @url POST {id}/setinvoiced
548  // *
549  // * @return int
550  // *
551  // * @throws RestException 400
552  // * @throws RestException 401
553  // * @throws RestException 404
554  // * @throws RestException 405
555  // */
556  /*
557  public function setinvoiced($id)
558  {
559 
560  if(! DolibarrApiAccess::$user->rights->reception->creer) {
561  throw new RestException(401);
562  }
563  if(empty($id)) {
564  throw new RestException(400, 'Reception ID is mandatory');
565  }
566  $result = $this->reception->fetch($id);
567  if( ! $result ) {
568  throw new RestException(404, 'Reception not found');
569  }
570 
571  $result = $this->reception->classifyBilled(DolibarrApiAccess::$user);
572  if( $result < 0) {
573  throw new RestException(400, $this->reception->error);
574  }
575  return $result;
576  }
577  */
578 
579 
580  // /**
581  // * Create a reception using an existing order.
582  // *
583  // * @param int $orderid Id of the order
584  // *
585  // * @url POST /createfromorder/{orderid}
586  // *
587  // * @return int
588  // * @throws RestException 400
589  // * @throws RestException 401
590  // * @throws RestException 404
591  // * @throws RestException 405
592  // */
593  /*
594  public function createShipmentFromOrder($orderid)
595  {
596 
597  require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
598 
599  if(! DolibarrApiAccess::$user->rights->reception->lire) {
600  throw new RestException(401);
601  }
602  if(! DolibarrApiAccess::$user->rights->reception->creer) {
603  throw new RestException(401);
604  }
605  if(empty($proposalid)) {
606  throw new RestException(400, 'Order ID is mandatory');
607  }
608 
609  $order = new Commande($this->db);
610  $result = $order->fetch($proposalid);
611  if( ! $result ) {
612  throw new RestException(404, 'Order not found');
613  }
614 
615  $result = $this->reception->createFromOrder($order, DolibarrApiAccess::$user);
616  if( $result < 0) {
617  throw new RestException(405, $this->reception->error);
618  }
619  $this->reception->fetchObjectLinked();
620  return $this->_cleanObjectDatas($this->reception);
621  }
622  */
623 
634  public function close($id, $notrigger = 0)
635  {
636  if (!DolibarrApiAccess::$user->rights->reception->creer) {
637  throw new RestException(401);
638  }
639 
640  $result = $this->reception->fetch($id);
641  if (!$result) {
642  throw new RestException(404, 'Reception not found');
643  }
644 
645  if (!DolibarrApi::_checkAccessToResource('reception', $this->commande->id)) {
646  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
647  }
648 
649  $result = $this->reception->setClosed();
650  if ($result == 0) {
651  throw new RestException(304, 'Error nothing done. May be object is already closed');
652  }
653  if ($result < 0) {
654  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
655  }
656 
657  // Reload reception
658  $result = $this->reception->fetch($id);
659 
660  $this->reception->fetchObjectLinked();
661 
662  return $this->_cleanObjectDatas($this->reception);
663  }
664 
665  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
672  protected function _cleanObjectDatas($object)
673  {
674  // phpcs:enable
675  $object = parent::_cleanObjectDatas($object);
676 
677  unset($object->thirdparty); // id already returned
678 
679  unset($object->note);
680  unset($object->address);
681  unset($object->barcode_type);
682  unset($object->barcode_type_code);
683  unset($object->barcode_type_label);
684  unset($object->barcode_type_coder);
685 
686  if (!empty($object->lines) && is_array($object->lines)) {
687  foreach ($object->lines as $line) {
688  unset($line->tva_tx);
689  unset($line->vat_src_code);
690  unset($line->total_ht);
691  unset($line->total_ttc);
692  unset($line->total_tva);
693  unset($line->total_localtax1);
694  unset($line->total_localtax2);
695  unset($line->remise_percent);
696  }
697  }
698 
699  return $object;
700  }
701 
709  private function _validate($data)
710  {
711  $reception = array();
712  foreach (Receptions::$FIELDS as $field) {
713  if (!isset($data[$field])) {
714  throw new RestException(400, "$field field missing");
715  }
716  $reception[$field] = $data[$field];
717  }
718  return $reception;
719  }
720 }
Class for API REST v1.
Definition: api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:282
Class to manage receptions.
close($id, $notrigger=0)
Close a reception (Classify it as "Delivered")
validate($id, $notrigger=0)
Validate a reception.
post($request_data=null)
Create reception object.
deleteLine($id, $lineid)
Delete a line to given reception.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update reception general fields (won't touch lines of reception)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List receptions.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.