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 $this->reception->$field = $value;
462 }
463
464 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
465 return $this->get($id);
466 } else {
467 throw new RestException(500, $this->reception->error);
468 }
469 }
470
477 public function delete($id)
478 {
479 if (!DolibarrApiAccess::$user->rights->reception->supprimer) {
480 throw new RestException(401);
481 }
482 $result = $this->reception->fetch($id);
483 if (!$result) {
484 throw new RestException(404, 'Reception not found');
485 }
486
487 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
488 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 }
490
491 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
492 throw new RestException(500, 'Error when deleting reception : '.$this->reception->error);
493 }
494
495 return array(
496 'success' => array(
497 'code' => 200,
498 'message' => 'Reception deleted'
499 )
500 );
501 }
502
522 public function validate($id, $notrigger = 0)
523 {
524 if (!DolibarrApiAccess::$user->rights->reception->creer) {
525 throw new RestException(401);
526 }
527 $result = $this->reception->fetch($id);
528 if (!$result) {
529 throw new RestException(404, 'Reception not found');
530 }
531
532 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
533 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
534 }
535
536 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
537 if ($result == 0) {
538 throw new RestException(304, 'Error nothing done. May be object is already validated');
539 }
540 if ($result < 0) {
541 throw new RestException(500, 'Error when validating Reception: '.$this->reception->error);
542 }
543
544 // Reload reception
545 $result = $this->reception->fetch($id);
546
547 $this->reception->fetchObjectLinked();
548 return $this->_cleanObjectDatas($this->reception);
549 }
550
551
552 // /**
553 // * Classify the reception as invoiced
554 // *
555 // * @param int $id Id of the reception
556 // *
557 // * @url POST {id}/setinvoiced
558 // *
559 // * @return int
560 // *
561 // * @throws RestException 400
562 // * @throws RestException 401
563 // * @throws RestException 404
564 // * @throws RestException 405
565 // */
566 /*
567 public function setinvoiced($id)
568 {
569
570 if(! DolibarrApiAccess::$user->rights->reception->creer) {
571 throw new RestException(401);
572 }
573 if(empty($id)) {
574 throw new RestException(400, 'Reception ID is mandatory');
575 }
576 $result = $this->reception->fetch($id);
577 if( ! $result ) {
578 throw new RestException(404, 'Reception not found');
579 }
580
581 $result = $this->reception->classifyBilled(DolibarrApiAccess::$user);
582 if( $result < 0) {
583 throw new RestException(400, $this->reception->error);
584 }
585 return $result;
586 }
587 */
588
589
590 // /**
591 // * Create a reception using an existing order.
592 // *
593 // * @param int $orderid Id of the order
594 // *
595 // * @url POST /createfromorder/{orderid}
596 // *
597 // * @return int
598 // * @throws RestException 400
599 // * @throws RestException 401
600 // * @throws RestException 404
601 // * @throws RestException 405
602 // */
603 /*
604 public function createShipmentFromOrder($orderid)
605 {
606
607 require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
608
609 if(! DolibarrApiAccess::$user->rights->reception->lire) {
610 throw new RestException(401);
611 }
612 if(! DolibarrApiAccess::$user->rights->reception->creer) {
613 throw new RestException(401);
614 }
615 if(empty($proposalid)) {
616 throw new RestException(400, 'Order ID is mandatory');
617 }
618
619 $order = new Commande($this->db);
620 $result = $order->fetch($proposalid);
621 if( ! $result ) {
622 throw new RestException(404, 'Order not found');
623 }
624
625 $result = $this->reception->createFromOrder($order, DolibarrApiAccess::$user);
626 if( $result < 0) {
627 throw new RestException(405, $this->reception->error);
628 }
629 $this->reception->fetchObjectLinked();
630 return $this->_cleanObjectDatas($this->reception);
631 }
632 */
633
644 public function close($id, $notrigger = 0)
645 {
646 if (!DolibarrApiAccess::$user->rights->reception->creer) {
647 throw new RestException(401);
648 }
649
650 $result = $this->reception->fetch($id);
651 if (!$result) {
652 throw new RestException(404, 'Reception not found');
653 }
654
655 if (!DolibarrApi::_checkAccessToResource('reception', $this->reception->id)) {
656 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
657 }
658
659 $result = $this->reception->setClosed();
660 if ($result == 0) {
661 throw new RestException(304, 'Error nothing done. May be object is already closed');
662 }
663 if ($result < 0) {
664 throw new RestException(500, 'Error when closing Reception: '.$this->reception->error);
665 }
666
667 // Reload reception
668 $result = $this->reception->fetch($id);
669
670 $this->reception->fetchObjectLinked();
671
672 return $this->_cleanObjectDatas($this->reception);
673 }
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
682 protected function _cleanObjectDatas($object)
683 {
684 // phpcs:enable
685 $object = parent::_cleanObjectDatas($object);
686
687 unset($object->thirdparty); // id already returned
688
689 unset($object->note);
690 unset($object->address);
691 unset($object->barcode_type);
692 unset($object->barcode_type_code);
693 unset($object->barcode_type_label);
694 unset($object->barcode_type_coder);
695
696 if (!empty($object->lines) && is_array($object->lines)) {
697 foreach ($object->lines as $line) {
698 unset($line->tva_tx);
699 unset($line->vat_src_code);
700 unset($line->total_ht);
701 unset($line->total_ttc);
702 unset($line->total_tva);
703 unset($line->total_localtax1);
704 unset($line->total_localtax2);
705 unset($line->remise_percent);
706 }
707 }
708
709 return $object;
710 }
711
719 private function _validate($data)
720 {
721 $reception = array();
722 foreach (Receptions::$FIELDS as $field) {
723 if (!isset($data[$field])) {
724 throw new RestException(400, "$field field missing");
725 }
726 $reception[$field] = $data[$field];
727 }
728 return $reception;
729 }
730}
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.
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.