dolibarr 18.0.6
api_proposals.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
5 * Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
6 * Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
24require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
25
26
34{
38 static $FIELDS = array(
39 'socid'
40 );
41
45 public $propal;
46
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->propal = new Propal($this->db);
55 }
56
68 public function get($id, $contact_list = 1)
69 {
70 return $this->_fetch($id, '', '', $contact_list);
71 }
72
86 public function getByRef($ref, $contact_list = 1)
87 {
88 return $this->_fetch('', $ref, '', $contact_list);
89 }
90
104 public function getByRefExt($ref_ext, $contact_list = 1)
105 {
106 return $this->_fetch('', '', $ref_ext, $contact_list);
107 }
108
122 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
123 {
124 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
125 throw new RestException(401);
126 }
127
128 $result = $this->propal->fetch($id, $ref, $ref_ext);
129 if (!$result) {
130 throw new RestException(404, 'Commercial Proposal not found');
131 }
132
133 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
134 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
135 }
136
137 // Add external contacts ids.
138 $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
139 if (is_array($tmparray)) {
140 $this->propal->contacts_ids = $tmparray;
141 }
142
143 $this->propal->fetchObjectLinked();
144
145 return $this->_cleanObjectDatas($this->propal);
146 }
147
161 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
162 {
163 global $db, $conf;
164
165 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
166 throw new RestException(401);
167 }
168
169 $obj_ret = array();
170
171 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
172 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
173
174 // If the internal user must only see his customers, force searching by him
175 $search_sale = 0;
176 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
177 $search_sale = DolibarrApiAccess::$user->id;
178 }
179
180 $sql = "SELECT t.rowid";
181 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
182 $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)
183 }
184 $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t LEFT JOIN ".MAIN_DB_PREFIX."propal_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
185
186
187 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
188 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
189 }
190
191 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
192 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
193 $sql .= " AND t.fk_soc = sc.fk_soc";
194 }
195 if ($socids) {
196 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
197 }
198 if ($search_sale > 0) {
199 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
200 }
201 // Insert sale filter
202 if ($search_sale > 0) {
203 $sql .= " AND sc.fk_user = ".((int) $search_sale);
204 }
205 // Add sql filters
206 if ($sqlfilters) {
207 $errormessage = '';
208 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
209 if ($errormessage) {
210 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
211 }
212 }
213
214 $sql .= $this->db->order($sortfield, $sortorder);
215 if ($limit) {
216 if ($page < 0) {
217 $page = 0;
218 }
219 $offset = $limit * $page;
220
221 $sql .= $this->db->plimit($limit + 1, $offset);
222 }
223
224 dol_syslog("API Rest request");
225 $result = $this->db->query($sql);
226
227 if ($result) {
228 $num = $this->db->num_rows($result);
229 $min = min($num, ($limit <= 0 ? $num : $limit));
230 $i = 0;
231 while ($i < $min) {
232 $obj = $this->db->fetch_object($result);
233 $proposal_static = new Propal($this->db);
234 if ($proposal_static->fetch($obj->rowid)) {
235 // Add external contacts ids
236 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
237 if (is_array($tmparray)) {
238 $proposal_static->contacts_ids = $tmparray;
239 }
240 $obj_ret[] = $this->_cleanObjectDatas($proposal_static);
241 }
242 $i++;
243 }
244 } else {
245 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
246 }
247 if (!count($obj_ret)) {
248 throw new RestException(404, 'No proposal found');
249 }
250 return $obj_ret;
251 }
252
259 public function post($request_data = null)
260 {
261 if (!DolibarrApiAccess::$user->rights->propal->creer) {
262 throw new RestException(401, "Insuffisant rights");
263 }
264 // Check mandatory fields
265 $result = $this->_validate($request_data);
266
267 foreach ($request_data as $field => $value) {
268 $this->propal->$field = $value;
269 }
270 /*if (isset($request_data["lines"])) {
271 $lines = array();
272 foreach ($request_data["lines"] as $line) {
273 array_push($lines, (object) $line);
274 }
275 $this->propal->lines = $lines;
276 }*/
277 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
279 }
280
281 return ((int) $this->propal->id);
282 }
283
294 public function getLines($id, $sqlfilters = '')
295 {
296 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
297 throw new RestException(401);
298 }
299
300 $result = $this->propal->fetch($id);
301 if (!$result) {
302 throw new RestException(404, 'Commercial Proposal not found');
303 }
304
305 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
306 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
307 }
308
309 $sql = '';
310 if (!empty($sqlfilters)) {
311 $errormessage = '';
312 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
313 if ($errormessage) {
314 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
315 }
316 }
317
318 $this->propal->getLinesArray($sql);
319 $result = array();
320 foreach ($this->propal->lines as $line) {
321 array_push($result, $this->_cleanObjectDatas($line));
322 }
323 return $result;
324 }
325
336 public function postLine($id, $request_data = null)
337 {
338 if (!DolibarrApiAccess::$user->rights->propal->creer) {
339 throw new RestException(401);
340 }
341
342 $result = $this->propal->fetch($id);
343 if (!$result) {
344 throw new RestException(404, 'Commercial Proposal not found');
345 }
346
347 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
348 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
349 }
350
351 $request_data = (object) $request_data;
352
353 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
354 $request_data->label = sanitizeVal($request_data->label);
355
356 $updateRes = $this->propal->addline(
357 $request_data->desc,
358 $request_data->subprice,
359 $request_data->qty,
360 $request_data->tva_tx,
361 $request_data->localtax1_tx,
362 $request_data->localtax2_tx,
363 $request_data->fk_product,
364 $request_data->remise_percent,
365 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
366 $request_data->subprice,
367 $request_data->info_bits,
368 $request_data->product_type,
369 $request_data->rang,
370 $request_data->special_code,
371 $request_data->fk_parent_line,
372 $request_data->fk_fournprice,
373 $request_data->pa_ht,
374 $request_data->label,
375 $request_data->date_start,
376 $request_data->date_end,
377 $request_data->array_options,
378 $request_data->fk_unit,
379 $request_data->origin,
380 $request_data->origin_id,
381 $request_data->multicurrency_subprice,
382 $request_data->fk_remise_except
383 );
384
385 if ($updateRes > 0) {
386 return $updateRes;
387 } else {
388 throw new RestException(400, $this->propal->error);
389 }
390 }
391
402 public function postLines($id, $request_data = null)
403 {
404 if (!DolibarrApiAccess::$user->rights->propal->creer) {
405 throw new RestException(401);
406 }
407
408 $result = $this->propal->fetch($id);
409 if (!$result) {
410 throw new RestException(404, 'Commercial Proposal not found');
411 }
412
413 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
414 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
415 }
416
417 $errors = [];
418 $this->db->begin();
419
420 foreach ($request_data as $TData) {
421 if (empty($TData[0])) $TData = array($TData);
422
423 foreach ($TData as $lineData) {
424 $line = (object) $lineData;
425
426 $updateRes = $this->propal->addline(
427 $line->desc,
428 $line->subprice,
429 $line->qty,
430 $line->tva_tx,
431 $line->localtax1_tx,
432 $line->localtax2_tx,
433 $line->fk_product,
434 $line->remise_percent,
435 'HT',
436 0,
437 $line->info_bits,
438 $line->product_type,
439 $line->rang,
440 $line->special_code,
441 $line->fk_parent_line,
442 $line->fk_fournprice,
443 $line->pa_ht,
444 $line->label,
445 $line->date_start,
446 $line->date_end,
447 $line->array_options,
448 $line->fk_unit,
449 $line->origin,
450 $line->origin_id,
451 $line->multicurrency_subprice,
452 $line->fk_remise_except
453 );
454
455 if ($updateRes < 0) {
456 $errors['lineLabel'] = $line->label;
457 $errors['msg'] = $this->propal->errors;
458 }
459 }
460 }
461 if (empty($errors)) {
462 $this->db->commit();
463 return $updateRes;
464 } else {
465 $this->db->rollback();
466 throw new RestException(400, implode(", ", $errors));
467 }
468 }
469
480 public function putLine($id, $lineid, $request_data = null)
481 {
482 if (!DolibarrApiAccess::$user->rights->propal->creer) {
483 throw new RestException(401);
484 }
485
486 $result = $this->propal->fetch($id);
487 if ($result <= 0) {
488 throw new RestException(404, 'Proposal not found');
489 }
490
491 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
492 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
493 }
494
495 $request_data = (object) $request_data;
496
497 if (isset($request_data->desc)) {
498 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
499 }
500 if (isset($request_data->label)) {
501 $request_data->label = sanitizeVal($request_data->label);
502 }
503
504 $propalline = new PropaleLigne($this->db);
505 $result = $propalline->fetch($lineid);
506 if ($result <= 0) {
507 throw new RestException(404, 'Proposal line not found');
508 }
509
510 $updateRes = $this->propal->updateline(
511 $lineid,
512 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
513 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
514 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
515 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
516 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
517 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
518 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
519 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
520 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
521 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
522 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
523 0,
524 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
525 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
526 isset($request_data->label) ? $request_data->label : $propalline->label,
527 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
528 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
529 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
530 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
531 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
532 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
533 0,
534 isset($request_data->rang) ? $request_data->rang : $propalline->rang
535 );
536
537 if ($updateRes > 0) {
538 $result = $this->get($id);
539 unset($result->line);
540 return $this->_cleanObjectDatas($result);
541 }
542 return false;
543 }
544
558 public function deleteLine($id, $lineid)
559 {
560 if (!DolibarrApiAccess::$user->rights->propal->creer) {
561 throw new RestException(401);
562 }
563
564 $result = $this->propal->fetch($id);
565 if (!$result) {
566 throw new RestException(404, 'Proposal not found');
567 }
568
569 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
570 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
571 }
572
573 $updateRes = $this->propal->deleteline($lineid, $id);
574 if ($updateRes > 0) {
575 return $this->get($id);
576 } else {
577 throw new RestException(405, $this->propal->error);
578 }
579 }
580
594 public function postContact($id, $contactid, $type)
595 {
596 if (!DolibarrApiAccess::$user->rights->propal->creer) {
597 throw new RestException(401);
598 }
599
600 $result = $this->propal->fetch($id);
601
602 if (!$result) {
603 throw new RestException(404, 'Proposal not found');
604 }
605
606 if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
607 throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
608 }
609
610 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
611 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 }
613
614 $result = $this->propal->add_contact($contactid, $type, 'external');
615
616 if (!$result) {
617 throw new RestException(500, 'Error when added the contact');
618 }
619
620 return array(
621 'success' => array(
622 'code' => 200,
623 'message' => 'Contact linked to the proposal'
624 )
625 );
626 }
627
642 public function deleteContact($id, $contactid, $type)
643 {
644 if (!DolibarrApiAccess::$user->rights->propal->creer) {
645 throw new RestException(401);
646 }
647
648 $result = $this->propal->fetch($id);
649
650 if (!$result) {
651 throw new RestException(404, 'Proposal not found');
652 }
653
654 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
655 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
656 }
657
658 $contacts = $this->propal->liste_contact();
659
660 foreach ($contacts as $contact) {
661 if ($contact['id'] == $contactid && $contact['code'] == $type) {
662 $result = $this->propal->delete_contact($contact['rowid']);
663
664 if (!$result) {
665 throw new RestException(500, 'Error when deleted the contact');
666 }
667 }
668 }
669
670 return $this->_cleanObjectDatas($this->propal);
671 }
672
680 public function put($id, $request_data = null)
681 {
682 if (!DolibarrApiAccess::$user->rights->propal->creer) {
683 throw new RestException(401);
684 }
685
686 $result = $this->propal->fetch($id);
687 if (!$result) {
688 throw new RestException(404, 'Proposal not found');
689 }
690
691 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
692 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 }
694 foreach ($request_data as $field => $value) {
695 if ($field == 'id') {
696 continue;
697 }
698 if ($field == 'array_options' && is_array($value)) {
699 foreach ($value as $index => $val) {
700 $this->propal->array_options[$index] = $this->_checkValForAPI($field, $val, $this->propal);
701 }
702 continue;
703 }
704 $this->propal->$field = $value;
705 }
706
707 // update end of validity date
708 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
709 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
710 }
711 if (!empty($this->propal->fin_validite)) {
712 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
713 throw new RestException(500, $this->propal->error);
714 }
715 }
716
717 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
718 return $this->get($id);
719 } else {
720 throw new RestException(500, $this->propal->error);
721 }
722 }
723
730 public function delete($id)
731 {
732 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
733 throw new RestException(401);
734 }
735 $result = $this->propal->fetch($id);
736 if (!$result) {
737 throw new RestException(404, 'Commercial Proposal not found');
738 }
739
740 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
741 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
742 }
743
744 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
745 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
746 }
747
748 return array(
749 'success' => array(
750 'code' => 200,
751 'message' => 'Commercial Proposal deleted'
752 )
753 );
754 }
755
764 public function settodraft($id)
765 {
766 if (!DolibarrApiAccess::$user->rights->propal->creer) {
767 throw new RestException(401);
768 }
769 $result = $this->propal->fetch($id);
770 if (!$result) {
771 throw new RestException(404, 'Proposal not found');
772 }
773
774 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
775 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
776 }
777
778 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
779 if ($result == 0) {
780 throw new RestException(304, 'Nothing done. May be object is already draft');
781 }
782 if ($result < 0) {
783 throw new RestException(500, 'Error : '.$this->propal->error);
784 }
785
786 $result = $this->propal->fetch($id);
787 if (!$result) {
788 throw new RestException(404, 'Proposal not found');
789 }
790
791 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
792 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
793 }
794
795 $this->propal->fetchObjectLinked();
796
797 return $this->_cleanObjectDatas($this->propal);
798 }
799
800
820 public function validate($id, $notrigger = 0)
821 {
822 if (!DolibarrApiAccess::$user->rights->propal->creer) {
823 throw new RestException(401);
824 }
825 $result = $this->propal->fetch($id);
826 if (!$result) {
827 throw new RestException(404, 'Commercial Proposal not found');
828 }
829
830 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
831 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 }
833
834 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
835 if ($result == 0) {
836 throw new RestException(304, 'Error nothing done. May be object is already validated');
837 }
838 if ($result < 0) {
839 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
840 }
841
842 $result = $this->propal->fetch($id);
843 if (!$result) {
844 throw new RestException(404, 'Commercial Proposal not found');
845 }
846
847 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
848 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
849 }
850
851 $this->propal->fetchObjectLinked();
852
853 return $this->_cleanObjectDatas($this->propal);
854 }
855
867 public function close($id, $status, $note_private = '', $notrigger = 0)
868 {
869 if (!DolibarrApiAccess::$user->rights->propal->creer) {
870 throw new RestException(401);
871 }
872 $result = $this->propal->fetch($id);
873 if (!$result) {
874 throw new RestException(404, 'Commercial Proposal not found');
875 }
876
877 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
878 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
879 }
880
881 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
882 if ($result == 0) {
883 throw new RestException(304, 'Error nothing done. May be object is already closed');
884 }
885 if ($result < 0) {
886 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
887 }
888
889 $result = $this->propal->fetch($id);
890 if (!$result) {
891 throw new RestException(404, 'Proposal not found');
892 }
893
894 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
895 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
896 }
897
898 $this->propal->fetchObjectLinked();
899
900 return $this->_cleanObjectDatas($this->propal);
901 }
902
911 public function setinvoiced($id)
912 {
913 if (!DolibarrApiAccess::$user->rights->propal->creer) {
914 throw new RestException(401);
915 }
916 $result = $this->propal->fetch($id);
917 if (!$result) {
918 throw new RestException(404, 'Commercial Proposal not found');
919 }
920
921 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
922 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
923 }
924
925 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
926 if ($result < 0) {
927 throw new RestException(500, 'Error : '.$this->propal->error);
928 }
929
930 $result = $this->propal->fetch($id);
931 if (!$result) {
932 throw new RestException(404, 'Proposal not found');
933 }
934
935 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
936 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
937 }
938
939 $this->propal->fetchObjectLinked();
940
941 return $this->_cleanObjectDatas($this->propal);
942 }
943
944
953 private function _validate($data)
954 {
955 $propal = array();
956 foreach (Proposals::$FIELDS as $field) {
957 if (!isset($data[$field])) {
958 throw new RestException(400, "$field field missing");
959 }
960 $propal[$field] = $data[$field];
961 }
962 return $propal;
963 }
964
965
966 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
973 protected function _cleanObjectDatas($object)
974 {
975 // phpcs:enable
976 $object = parent::_cleanObjectDatas($object);
977
978 unset($object->note);
979 unset($object->name);
980 unset($object->lastname);
981 unset($object->firstname);
982 unset($object->civility_id);
983 unset($object->address);
984
985 return $object;
986 }
987}
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 proposals.
Class to manage commercial proposal lines.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an proposal object.
getLines($id, $sqlfilters='')
Get lines of a commercial proposal.
settodraft($id)
Set a proposal to draft.
put($id, $request_data=null)
Update commercial proposal general fields (won't touch lines of commercial proposal)
close($id, $status, $note_private='', $notrigger=0)
Close (Accept or refuse) a quote / commercial proposal.
setinvoiced($id)
Set a commercial proposal billed.
post($request_data=null)
Create commercial proposal object.
postContact($id, $contactid, $type)
Add a contact type of given commercial proposal.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an proposal object by ref_ext.
postLine($id, $request_data=null)
Add a line to given commercial proposal.
_cleanObjectDatas($object)
Clean sensible object datas.
postLines($id, $request_data=null)
Add lines to given commercial proposal.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of given commercial proposal.
deleteContact($id, $contactid, $type)
Delete a contact type of given commercial proposal.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List commercial proposals.
validate($id, $notrigger=0)
Validate a commercial proposal.
__construct()
Constructor.
getByRef($ref, $contact_list=1)
Get properties of an proposal object by ref.
putLine($id, $lineid, $request_data=null)
Update a line of given commercial proposal.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.