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