dolibarr 19.0.3
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 public 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
162 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
163 {
164 global $db, $conf;
165
166 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
167 throw new RestException(401);
168 }
169
170 $obj_ret = array();
171
172 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
173 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
174
175 // If the internal user must only see his customers, force searching by him
176 $search_sale = 0;
177 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
178 $search_sale = DolibarrApiAccess::$user->id;
179 }
180
181 $sql = "SELECT t.rowid";
182 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
183 $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)
184 }
185 $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
186
187
188 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
189 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
190 }
191
192 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
193 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
194 $sql .= " AND t.fk_soc = sc.fk_soc";
195 }
196 if ($socids) {
197 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
198 }
199 if ($search_sale > 0) {
200 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
201 }
202 // Insert sale filter
203 if ($search_sale > 0) {
204 $sql .= " AND sc.fk_user = ".((int) $search_sale);
205 }
206 // Add sql filters
207 if ($sqlfilters) {
208 $errormessage = '';
209 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
210 if ($errormessage) {
211 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
212 }
213 }
214
215 $sql .= $this->db->order($sortfield, $sortorder);
216 if ($limit) {
217 if ($page < 0) {
218 $page = 0;
219 }
220 $offset = $limit * $page;
221
222 $sql .= $this->db->plimit($limit + 1, $offset);
223 }
224
225 dol_syslog("API Rest request");
226 $result = $this->db->query($sql);
227
228 if ($result) {
229 $num = $this->db->num_rows($result);
230 $min = min($num, ($limit <= 0 ? $num : $limit));
231 $i = 0;
232 while ($i < $min) {
233 $obj = $this->db->fetch_object($result);
234 $proposal_static = new Propal($this->db);
235 if ($proposal_static->fetch($obj->rowid)) {
236 // Add external contacts ids
237 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
238 if (is_array($tmparray)) {
239 $proposal_static->contacts_ids = $tmparray;
240 }
241 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($proposal_static), $properties);
242 }
243 $i++;
244 }
245 } else {
246 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
247 }
248
249 return $obj_ret;
250 }
251
258 public function post($request_data = null)
259 {
260 if (!DolibarrApiAccess::$user->rights->propal->creer) {
261 throw new RestException(401, "Insuffisant rights");
262 }
263 // Check mandatory fields
264 $result = $this->_validate($request_data);
265
266 foreach ($request_data as $field => $value) {
267 if ($field === 'caller') {
268 // 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
269 $this->propal->context['caller'] = $request_data['caller'];
270 continue;
271 }
272
273 $this->propal->$field = $value;
274 }
275 /*if (isset($request_data["lines"])) {
276 $lines = array();
277 foreach ($request_data["lines"] as $line) {
278 array_push($lines, (object) $line);
279 }
280 $this->propal->lines = $lines;
281 }*/
282 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
284 }
285
286 return ((int) $this->propal->id);
287 }
288
299 public function getLines($id, $sqlfilters = '')
300 {
301 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
302 throw new RestException(401);
303 }
304
305 $result = $this->propal->fetch($id);
306 if (!$result) {
307 throw new RestException(404, 'Commercial Proposal not found');
308 }
309
310 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
311 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
312 }
313
314 $sql = '';
315 if (!empty($sqlfilters)) {
316 $errormessage = '';
317 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
318 if ($errormessage) {
319 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
320 }
321 }
322
323 $this->propal->getLinesArray($sql);
324 $result = array();
325 foreach ($this->propal->lines as $line) {
326 array_push($result, $this->_cleanObjectDatas($line));
327 }
328 return $result;
329 }
330
341 public function postLine($id, $request_data = null)
342 {
343 if (!DolibarrApiAccess::$user->rights->propal->creer) {
344 throw new RestException(401);
345 }
346
347 $result = $this->propal->fetch($id);
348 if (!$result) {
349 throw new RestException(404, 'Commercial Proposal not found');
350 }
351
352 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
353 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
354 }
355
356 $request_data = (object) $request_data;
357
358 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
359 $request_data->label = sanitizeVal($request_data->label);
360
361 $updateRes = $this->propal->addline(
362 $request_data->desc,
363 $request_data->subprice,
364 $request_data->qty,
365 $request_data->tva_tx,
366 $request_data->localtax1_tx,
367 $request_data->localtax2_tx,
368 $request_data->fk_product,
369 $request_data->remise_percent,
370 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
371 $request_data->subprice,
372 $request_data->info_bits,
373 $request_data->product_type,
374 $request_data->rang,
375 $request_data->special_code,
376 $request_data->fk_parent_line,
377 $request_data->fk_fournprice,
378 $request_data->pa_ht,
379 $request_data->label,
380 $request_data->date_start,
381 $request_data->date_end,
382 $request_data->array_options,
383 $request_data->fk_unit,
384 $request_data->origin,
385 $request_data->origin_id,
386 $request_data->multicurrency_subprice,
387 $request_data->fk_remise_except
388 );
389
390 if ($updateRes > 0) {
391 return $updateRes;
392 } else {
393 throw new RestException(400, $this->propal->error);
394 }
395 }
396
407 public function postLines($id, $request_data = null)
408 {
409 if (!DolibarrApiAccess::$user->rights->propal->creer) {
410 throw new RestException(401);
411 }
412
413 $result = $this->propal->fetch($id);
414 if (!$result) {
415 throw new RestException(404, 'Commercial Proposal not found');
416 }
417
418 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
419 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 }
421
422 $errors = [];
423 $this->db->begin();
424
425 foreach ($request_data as $TData) {
426 if (empty($TData[0])) {
427 $TData = array($TData);
428 }
429
430 foreach ($TData as $lineData) {
431 $line = (object) $lineData;
432
433 $updateRes = $this->propal->addline(
434 $line->desc,
435 $line->subprice,
436 $line->qty,
437 $line->tva_tx,
438 $line->localtax1_tx,
439 $line->localtax2_tx,
440 $line->fk_product,
441 $line->remise_percent,
442 'HT',
443 0,
444 $line->info_bits,
445 $line->product_type,
446 $line->rang,
447 $line->special_code,
448 $line->fk_parent_line,
449 $line->fk_fournprice,
450 $line->pa_ht,
451 $line->label,
452 $line->date_start,
453 $line->date_end,
454 $line->array_options,
455 $line->fk_unit,
456 $line->origin,
457 $line->origin_id,
458 $line->multicurrency_subprice,
459 $line->fk_remise_except
460 );
461
462 if ($updateRes < 0) {
463 $errors['lineLabel'] = $line->label;
464 $errors['msg'] = $this->propal->errors;
465 }
466 }
467 }
468 if (empty($errors)) {
469 $this->db->commit();
470 return $updateRes;
471 } else {
472 $this->db->rollback();
473 throw new RestException(400, implode(", ", $errors));
474 }
475 }
476
487 public function putLine($id, $lineid, $request_data = null)
488 {
489 if (!DolibarrApiAccess::$user->rights->propal->creer) {
490 throw new RestException(401);
491 }
492
493 $result = $this->propal->fetch($id);
494 if ($result <= 0) {
495 throw new RestException(404, 'Proposal not found');
496 }
497
498 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
499 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
500 }
501
502 $request_data = (object) $request_data;
503
504 if (isset($request_data->desc)) {
505 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
506 }
507 if (isset($request_data->label)) {
508 $request_data->label = sanitizeVal($request_data->label);
509 }
510
511 $propalline = new PropaleLigne($this->db);
512 $result = $propalline->fetch($lineid);
513 if ($result <= 0) {
514 throw new RestException(404, 'Proposal line not found');
515 }
516
517 $updateRes = $this->propal->updateline(
518 $lineid,
519 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
520 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
521 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
522 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
523 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
524 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
525 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
526 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
527 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
528 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
529 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
530 0,
531 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
532 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
533 isset($request_data->label) ? $request_data->label : $propalline->label,
534 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
535 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
536 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
537 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
538 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
539 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
540 0,
541 isset($request_data->rang) ? $request_data->rang : $propalline->rang
542 );
543
544 if ($updateRes > 0) {
545 $result = $this->get($id);
546 unset($result->line);
547 return $this->_cleanObjectDatas($result);
548 }
549 return false;
550 }
551
565 public function deleteLine($id, $lineid)
566 {
567 if (!DolibarrApiAccess::$user->rights->propal->creer) {
568 throw new RestException(401);
569 }
570
571 $result = $this->propal->fetch($id);
572 if (!$result) {
573 throw new RestException(404, 'Proposal not found');
574 }
575
576 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
577 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
578 }
579
580 $updateRes = $this->propal->deleteline($lineid, $id);
581 if ($updateRes > 0) {
582 return $this->get($id);
583 } else {
584 throw new RestException(405, $this->propal->error);
585 }
586 }
587
601 public function postContact($id, $contactid, $type)
602 {
603 if (!DolibarrApiAccess::$user->rights->propal->creer) {
604 throw new RestException(401);
605 }
606
607 $result = $this->propal->fetch($id);
608
609 if (!$result) {
610 throw new RestException(404, 'Proposal not found');
611 }
612
613 if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
614 throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
615 }
616
617 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
618 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
619 }
620
621 $result = $this->propal->add_contact($contactid, $type, 'external');
622
623 if (!$result) {
624 throw new RestException(500, 'Error when added the contact');
625 }
626
627 return array(
628 'success' => array(
629 'code' => 200,
630 'message' => 'Contact linked to the proposal'
631 )
632 );
633 }
634
649 public function deleteContact($id, $contactid, $type)
650 {
651 if (!DolibarrApiAccess::$user->rights->propal->creer) {
652 throw new RestException(401);
653 }
654
655 $result = $this->propal->fetch($id);
656
657 if (!$result) {
658 throw new RestException(404, 'Proposal not found');
659 }
660
661 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
662 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 }
664
665 $contacts = $this->propal->liste_contact();
666
667 foreach ($contacts as $contact) {
668 if ($contact['id'] == $contactid && $contact['code'] == $type) {
669 $result = $this->propal->delete_contact($contact['rowid']);
670
671 if (!$result) {
672 throw new RestException(500, 'Error when deleted the contact');
673 }
674 }
675 }
676
677 return $this->_cleanObjectDatas($this->propal);
678 }
679
687 public function put($id, $request_data = null)
688 {
689 if (!DolibarrApiAccess::$user->rights->propal->creer) {
690 throw new RestException(401);
691 }
692
693 $result = $this->propal->fetch($id);
694 if (!$result) {
695 throw new RestException(404, 'Proposal not found');
696 }
697
698 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
699 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
700 }
701 foreach ($request_data as $field => $value) {
702 if ($field == 'id') {
703 continue;
704 }
705 if ($field === 'caller') {
706 // 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
707 $this->propal->context['caller'] = $request_data['caller'];
708 continue;
709 }
710
711 $this->propal->$field = $value;
712 }
713
714 // update end of validity date
715 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
716 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
717 }
718 if (!empty($this->propal->fin_validite)) {
719 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
720 throw new RestException(500, $this->propal->error);
721 }
722 }
723
724 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
725 return $this->get($id);
726 } else {
727 throw new RestException(500, $this->propal->error);
728 }
729 }
730
737 public function delete($id)
738 {
739 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
740 throw new RestException(401);
741 }
742 $result = $this->propal->fetch($id);
743 if (!$result) {
744 throw new RestException(404, 'Commercial Proposal not found');
745 }
746
747 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
748 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
749 }
750
751 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
752 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
753 }
754
755 return array(
756 'success' => array(
757 'code' => 200,
758 'message' => 'Commercial Proposal deleted'
759 )
760 );
761 }
762
771 public function settodraft($id)
772 {
773 if (!DolibarrApiAccess::$user->rights->propal->creer) {
774 throw new RestException(401);
775 }
776 $result = $this->propal->fetch($id);
777 if (!$result) {
778 throw new RestException(404, 'Proposal not found');
779 }
780
781 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
782 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
783 }
784
785 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
786 if ($result == 0) {
787 throw new RestException(304, 'Nothing done. May be object is already draft');
788 }
789 if ($result < 0) {
790 throw new RestException(500, 'Error : '.$this->propal->error);
791 }
792
793 $result = $this->propal->fetch($id);
794 if (!$result) {
795 throw new RestException(404, 'Proposal not found');
796 }
797
798 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
799 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
800 }
801
802 $this->propal->fetchObjectLinked();
803
804 return $this->_cleanObjectDatas($this->propal);
805 }
806
807
827 public function validate($id, $notrigger = 0)
828 {
829 if (!DolibarrApiAccess::$user->rights->propal->creer) {
830 throw new RestException(401);
831 }
832 $result = $this->propal->fetch($id);
833 if (!$result) {
834 throw new RestException(404, 'Commercial Proposal not found');
835 }
836
837 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
838 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
839 }
840
841 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
842 if ($result == 0) {
843 throw new RestException(304, 'Error nothing done. May be object is already validated');
844 }
845 if ($result < 0) {
846 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
847 }
848
849 $result = $this->propal->fetch($id);
850 if (!$result) {
851 throw new RestException(404, 'Commercial Proposal not found');
852 }
853
854 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
855 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
856 }
857
858 $this->propal->fetchObjectLinked();
859
860 return $this->_cleanObjectDatas($this->propal);
861 }
862
874 public function close($id, $status, $note_private = '', $notrigger = 0)
875 {
876 if (!DolibarrApiAccess::$user->rights->propal->creer) {
877 throw new RestException(401);
878 }
879 $result = $this->propal->fetch($id);
880 if (!$result) {
881 throw new RestException(404, 'Commercial Proposal not found');
882 }
883
884 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
885 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
886 }
887
888 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
889 if ($result == 0) {
890 throw new RestException(304, 'Error nothing done. May be object is already closed');
891 }
892 if ($result < 0) {
893 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
894 }
895
896 $result = $this->propal->fetch($id);
897 if (!$result) {
898 throw new RestException(404, 'Proposal not found');
899 }
900
901 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
902 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
903 }
904
905 $this->propal->fetchObjectLinked();
906
907 return $this->_cleanObjectDatas($this->propal);
908 }
909
918 public function setinvoiced($id)
919 {
920 if (!DolibarrApiAccess::$user->rights->propal->creer) {
921 throw new RestException(401);
922 }
923 $result = $this->propal->fetch($id);
924 if (!$result) {
925 throw new RestException(404, 'Commercial Proposal not found');
926 }
927
928 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
929 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
930 }
931
932 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
933 if ($result < 0) {
934 throw new RestException(500, 'Error : '.$this->propal->error);
935 }
936
937 $result = $this->propal->fetch($id);
938 if (!$result) {
939 throw new RestException(404, 'Proposal not found');
940 }
941
942 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
943 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
944 }
945
946 $this->propal->fetchObjectLinked();
947
948 return $this->_cleanObjectDatas($this->propal);
949 }
950
951
960 private function _validate($data)
961 {
962 $propal = array();
963 foreach (Proposals::$FIELDS as $field) {
964 if (!isset($data[$field])) {
965 throw new RestException(400, "$field field missing");
966 }
967 $propal[$field] = $data[$field];
968 }
969 return $propal;
970 }
971
972
973 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
980 protected function _cleanObjectDatas($object)
981 {
982 // phpcs:enable
983 $object = parent::_cleanObjectDatas($object);
984
985 unset($object->note);
986 unset($object->name);
987 unset($object->lastname);
988 unset($object->firstname);
989 unset($object->civility_id);
990 unset($object->address);
991
992 return $object;
993 }
994}
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 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.
validate($id, $notrigger=0)
Validate a commercial proposal.
__construct()
Constructor.
getByRef($ref, $contact_list=1)
Get properties of an proposal object by ref.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List commercial proposals.
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.