dolibarr 19.0.4
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
30{
34 public static $FIELDS = array(
35 'socid',
36 'origin_id',
37 'origin_type',
38 );
39
43 public $reception;
44
48 public function __construct()
49 {
50 global $db, $conf;
51 $this->db = $db;
52 $this->reception = new Reception($this->db);
53 }
54
64 public function get($id)
65 {
66 if (!DolibarrApiAccess::$user->rights->reception->lire) {
67 throw new RestException(401);
68 }
69
70 $result = $this->reception->fetch($id);
71 if (!$result) {
72 throw new RestException(404, 'Reception not found');
73 }
74
75 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
76 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77 }
78
79 $this->reception->fetchObjectLinked();
80 return $this->_cleanObjectDatas($this->reception);
81 }
82
83
84
101 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
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->_filterObjectProperties($this->_cleanObjectDatas($reception_static), $properties);
175 }
176 $i++;
177 }
178 } else {
179 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
180 }
181
182 return $obj_ret;
183 }
184
191 public function post($request_data = null)
192 {
193 if (!DolibarrApiAccess::$user->rights->reception->creer) {
194 throw new RestException(401, "Insuffisant rights");
195 }
196 // Check mandatory fields
197 $result = $this->_validate($request_data);
198
199 foreach ($request_data as $field => $value) {
200 if ($field === 'caller') {
201 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
202 $this->reception->context['caller'] = $request_data['caller'];
203 continue;
204 }
205
206 $this->reception->$field = $value;
207 }
208 if (isset($request_data["lines"])) {
209 $lines = array();
210 foreach ($request_data["lines"] as $line) {
211 array_push($lines, (object) $line);
212 }
213 $this->reception->lines = $lines;
214 }
215
216 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500, "Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
218 }
219
220 return $this->reception->id;
221 }
222
223 // /**
224 // * Get lines of an reception
225 // *
226 // * @param int $id Id of reception
227 // *
228 // * @url GET {id}/lines
229 // *
230 // * @return int
231 // */
232 /*
233 public function getLines($id)
234 {
235 if(! DolibarrApiAccess::$user->rights->reception->lire) {
236 throw new RestException(401);
237 }
238
239 $result = $this->reception->fetch($id);
240 if( ! $result ) {
241 throw new RestException(404, 'Reception not found');
242 }
243
244 if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
245 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
246 }
247 $this->reception->getLinesArray();
248 $result = array();
249 foreach ($this->reception->lines as $line) {
250 array_push($result,$this->_cleanObjectDatas($line));
251 }
252 return $result;
253 }
254 */
255
256 // /**
257 // * Add a line to given reception
258 // *
259 // * @param int $id Id of reception to update
260 // * @param array $request_data ShipmentLine data
261 // *
262 // * @url POST {id}/lines
263 // *
264 // * @return int
265 // */
266 /*
267 public function postLine($id, $request_data = null)
268 {
269 if(! DolibarrApiAccess::$user->rights->reception->creer) {
270 throw new RestException(401);
271 }
272
273 $result = $this->reception->fetch($id);
274 if ( ! $result ) {
275 throw new RestException(404, 'Reception not found');
276 }
277
278 if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
279 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
280 }
281
282 $request_data = (object) $request_data;
283
284 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
285 $request_data->label = sanitizeVal($request_data->label);
286
287 $updateRes = $this->reception->addline(
288 $request_data->desc,
289 $request_data->subprice,
290 $request_data->qty,
291 $request_data->tva_tx,
292 $request_data->localtax1_tx,
293 $request_data->localtax2_tx,
294 $request_data->fk_product,
295 $request_data->remise_percent,
296 $request_data->info_bits,
297 $request_data->fk_remise_except,
298 'HT',
299 0,
300 $request_data->date_start,
301 $request_data->date_end,
302 $request_data->product_type,
303 $request_data->rang,
304 $request_data->special_code,
305 $fk_parent_line,
306 $request_data->fk_fournprice,
307 $request_data->pa_ht,
308 $request_data->label,
309 $request_data->array_options,
310 $request_data->fk_unit,
311 $request_data->origin,
312 $request_data->origin_id,
313 $request_data->multicurrency_subprice
314 );
315
316 if ($updateRes > 0) {
317 return $updateRes;
318
319 }
320 return false;
321 }*/
322
323 // /**
324 // * Update a line to given reception
325 // *
326 // * @param int $id Id of reception to update
327 // * @param int $lineid Id of line to update
328 // * @param array $request_data ShipmentLine data
329 // *
330 // * @url PUT {id}/lines/{lineid}
331 // *
332 // * @return object
333 // */
334 /*
335 public function putLine($id, $lineid, $request_data = null)
336 {
337 if (! DolibarrApiAccess::$user->rights->reception->creer) {
338 throw new RestException(401);
339 }
340
341 $result = $this->reception->fetch($id);
342 if ( ! $result ) {
343 throw new RestException(404, 'Reception not found');
344 }
345
346 if( ! DolibarrApi::_checkAccessToResource('reception',$this->reception->id)) {
347 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
348 }
349
350 $request_data = (object) $request_data;
351
352 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
353 $request_data->label = sanitizeVal($request_data->label);
354
355 $updateRes = $this->reception->updateline(
356 $lineid,
357 $request_data->desc,
358 $request_data->subprice,
359 $request_data->qty,
360 $request_data->remise_percent,
361 $request_data->tva_tx,
362 $request_data->localtax1_tx,
363 $request_data->localtax2_tx,
364 'HT',
365 $request_data->info_bits,
366 $request_data->date_start,
367 $request_data->date_end,
368 $request_data->product_type,
369 $request_data->fk_parent_line,
370 0,
371 $request_data->fk_fournprice,
372 $request_data->pa_ht,
373 $request_data->label,
374 $request_data->special_code,
375 $request_data->array_options,
376 $request_data->fk_unit,
377 $request_data->multicurrency_subprice
378 );
379
380 if ($updateRes > 0) {
381 $result = $this->get($id);
382 unset($result->line);
383 return $this->_cleanObjectDatas($result);
384 }
385 return false;
386 }*/
387
400 public function deleteLine($id, $lineid)
401 {
402 if (!DolibarrApiAccess::$user->rights->reception->creer) {
403 throw new RestException(401);
404 }
405
406 $result = $this->reception->fetch($id);
407 if (!$result) {
408 throw new RestException(404, 'Reception not found');
409 }
410
411 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
412 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
413 }
414
415 // TODO Check the lineid $lineid is a line of object
416
417 $updateRes = $this->reception->deleteline(DolibarrApiAccess::$user, $lineid);
418 if ($updateRes < 0) {
419 throw new RestException(405, $this->reception->error);
420 }
421
422 return array(
423 'success' => array(
424 'code' => 200,
425 'message' => 'Line deleted'
426 )
427 );
428 }
429
437 public function put($id, $request_data = null)
438 {
439 if (!DolibarrApiAccess::$user->rights->reception->creer) {
440 throw new RestException(401);
441 }
442
443 $result = $this->reception->fetch($id);
444 if (!$result) {
445 throw new RestException(404, 'Reception not found');
446 }
447
448 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
449 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
450 }
451 foreach ($request_data as $field => $value) {
452 if ($field == 'id') {
453 continue;
454 }
455 if ($field === 'caller') {
456 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
457 $this->reception->context['caller'] = $request_data['caller'];
458 continue;
459 }
460
461 if ($field == 'array_options' && is_array($value)) {
462 foreach ($value as $index => $val) {
463 $this->reception->array_options[$index] = $this->_checkValForAPI($field, $val, $this->reception);
464 }
465 continue;
466 }
467 $this->reception->$field = $this->_checkValForAPI($field, $value, $this->reception);
468 }
469
470 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
471 return $this->get($id);
472 } else {
473 throw new RestException(500, $this->reception->error);
474 }
475 }
476
483 public function delete($id)
484 {
485 if (!DolibarrApiAccess::$user->rights->reception->supprimer) {
486 throw new RestException(401);
487 }
488 $result = $this->reception->fetch($id);
489 if (!$result) {
490 throw new RestException(404, 'Reception not found');
491 }
492
493 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
494 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 }
496
497 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
498 throw new RestException(500, 'Error when deleting reception : '.$this->reception->error);
499 }
500
501 return array(
502 'success' => array(
503 'code' => 200,
504 'message' => 'Reception deleted'
505 )
506 );
507 }
508
528 public function validate($id, $notrigger = 0)
529 {
530 if (!DolibarrApiAccess::$user->rights->reception->creer) {
531 throw new RestException(401);
532 }
533 $result = $this->reception->fetch($id);
534 if (!$result) {
535 throw new RestException(404, 'Reception not found');
536 }
537
538 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
539 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 }
541
542 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
543 if ($result == 0) {
544 throw new RestException(304, 'Error nothing done. May be object is already validated');
545 }
546 if ($result < 0) {
547 throw new RestException(500, 'Error when validating Reception: '.$this->reception->error);
548 }
549
550 // Reload reception
551 $result = $this->reception->fetch($id);
552
553 $this->reception->fetchObjectLinked();
554 return $this->_cleanObjectDatas($this->reception);
555 }
556
557
558 // /**
559 // * Classify the reception as invoiced
560 // *
561 // * @param int $id Id of the reception
562 // *
563 // * @url POST {id}/setinvoiced
564 // *
565 // * @return int
566 // *
567 // * @throws RestException 400
568 // * @throws RestException 401
569 // * @throws RestException 404
570 // * @throws RestException 405
571 // */
572 /*
573 public function setinvoiced($id)
574 {
575
576 if(! DolibarrApiAccess::$user->rights->reception->creer) {
577 throw new RestException(401);
578 }
579 if(empty($id)) {
580 throw new RestException(400, 'Reception ID is mandatory');
581 }
582 $result = $this->reception->fetch($id);
583 if( ! $result ) {
584 throw new RestException(404, 'Reception not found');
585 }
586
587 $result = $this->reception->classifyBilled(DolibarrApiAccess::$user);
588 if( $result < 0) {
589 throw new RestException(400, $this->reception->error);
590 }
591 return $result;
592 }
593 */
594
595
596 // /**
597 // * Create a reception using an existing order.
598 // *
599 // * @param int $orderid Id of the order
600 // *
601 // * @url POST /createfromorder/{orderid}
602 // *
603 // * @return int
604 // * @throws RestException 400
605 // * @throws RestException 401
606 // * @throws RestException 404
607 // * @throws RestException 405
608 // */
609 /*
610 public function createShipmentFromOrder($orderid)
611 {
612
613 require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
614
615 if(! DolibarrApiAccess::$user->rights->reception->lire) {
616 throw new RestException(401);
617 }
618 if(! DolibarrApiAccess::$user->rights->reception->creer) {
619 throw new RestException(401);
620 }
621 if(empty($proposalid)) {
622 throw new RestException(400, 'Order ID is mandatory');
623 }
624
625 $order = new Commande($this->db);
626 $result = $order->fetch($proposalid);
627 if( ! $result ) {
628 throw new RestException(404, 'Order not found');
629 }
630
631 $result = $this->reception->createFromOrder($order, DolibarrApiAccess::$user);
632 if( $result < 0) {
633 throw new RestException(405, $this->reception->error);
634 }
635 $this->reception->fetchObjectLinked();
636 return $this->_cleanObjectDatas($this->reception);
637 }
638 */
639
650 public function close($id, $notrigger = 0)
651 {
652 if (!DolibarrApiAccess::$user->rights->reception->creer) {
653 throw new RestException(401);
654 }
655
656 $result = $this->reception->fetch($id);
657 if (!$result) {
658 throw new RestException(404, 'Reception not found');
659 }
660
661 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
662 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 }
664
665 $result = $this->reception->setClosed();
666 if ($result == 0) {
667 throw new RestException(304, 'Error nothing done. May be object is already closed');
668 }
669 if ($result < 0) {
670 throw new RestException(500, 'Error when closing Reception: '.$this->reception->error);
671 }
672
673 // Reload reception
674 $result = $this->reception->fetch($id);
675
676 $this->reception->fetchObjectLinked();
677
678 return $this->_cleanObjectDatas($this->reception);
679 }
680
681 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
688 protected function _cleanObjectDatas($object)
689 {
690 // phpcs:enable
691 $object = parent::_cleanObjectDatas($object);
692
693 unset($object->thirdparty); // id already returned
694
695 unset($object->note);
696 unset($object->address);
697 unset($object->barcode_type);
698 unset($object->barcode_type_code);
699 unset($object->barcode_type_label);
700 unset($object->barcode_type_coder);
701
702 if (!empty($object->lines) && is_array($object->lines)) {
703 foreach ($object->lines as $line) {
704 unset($line->tva_tx);
705 unset($line->vat_src_code);
706 unset($line->total_ht);
707 unset($line->total_ttc);
708 unset($line->total_tva);
709 unset($line->total_localtax1);
710 unset($line->total_localtax2);
711 unset($line->remise_percent);
712 }
713 }
714
715 return $object;
716 }
717
725 private function _validate($data)
726 {
727 $reception = array();
728 foreach (Receptions::$FIELDS as $field) {
729 if (!isset($data[$field])) {
730 throw new RestException(400, "$field field missing");
731 }
732 $reception[$field] = $data[$field];
733 }
734 return $reception;
735 }
736}
Class for API REST v1.
Definition api.class.php:31
_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.
Definition api.class.php:85
Class to manage receptions.
close($id, $notrigger=0)
Classify the reception as invoiced.
validate($id, $notrigger=0)
Validate a reception.
post($request_data=null)
Create reception object.
deleteLine($id, $lineid)
Get lines of an reception.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List receptions.
put($id, $request_data=null)
Update reception general fields (won't touch lines of reception)
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.