dolibarr 22.0.5
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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2025 William Mead <william@m34d.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25use Luracast\Restler\RestException;
26
27require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
28
29
39{
43 public static $FIELDS = array(
44 'socid'
45 );
46
50 public $propal;
51
55 public function __construct()
56 {
57 global $db;
58 $this->db = $db;
59 $this->propal = new Propal($this->db);
60 }
61
75 public function get($id, $contact_list = 1)
76 {
77 return $this->_fetch($id, '', '', $contact_list);
78 }
79
95 public function getByRef($ref, $contact_list = 1)
96 {
97 return $this->_fetch(0, $ref, '', $contact_list);
98 }
99
115 public function getByRefExt($ref_ext, $contact_list = 1)
116 {
117 return $this->_fetch(0, '', $ref_ext, $contact_list);
118 }
119
133 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
134 {
135 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
136 throw new RestException(403);
137 }
138
139 $result = $this->propal->fetch($id, $ref, $ref_ext);
140 if (!$result) {
141 throw new RestException(404, 'Commercial Proposal not found');
142 }
143
144 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
145 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
146 }
147
148 if ($contact_list > -1) {
149 // Add external contacts ids.
150 $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
151 if (is_array($tmparray)) {
152 $this->propal->contacts_ids = $tmparray;
153 }
154 $tmparray = $this->propal->liste_contact(-1, 'internal', $contact_list);
155 if (is_array($tmparray)) {
156 $this->propal->contacts_ids_internal = $tmparray;
157 }
158 }
159
160 $this->propal->fetchObjectLinked();
161
162 return $this->_cleanObjectDatas($this->propal);
163 }
164
187 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
188 {
189 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
190 throw new RestException(403);
191 }
192
193 $obj_ret = array();
194
195 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
196 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
197
198 // If the internal user must only see his customers, force searching by him
199 $search_sale = 0;
200 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
201 $search_sale = DolibarrApiAccess::$user->id;
202 }
203
204 $sql = "SELECT t.rowid";
205 $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t";
206 $sql .= " 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
207 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
208 if ($socids) {
209 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
210 }
211 // Search on sale representative
212 if ($search_sale && $search_sale != '-1') {
213 if ($search_sale == -2) {
214 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
215 } elseif ($search_sale > 0) {
216 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
217 }
218 }
219 // Add sql filters
220 if ($sqlfilters) {
221 $errormessage = '';
222 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
223 if ($errormessage) {
224 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
225 }
226 }
227
228 //this query will return total proposals with the filters given
229 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
230
231 $sql .= $this->db->order($sortfield, $sortorder);
232 if ($limit) {
233 if ($page < 0) {
234 $page = 0;
235 }
236 $offset = $limit * $page;
237
238 $sql .= $this->db->plimit($limit + 1, $offset);
239 }
240
241 dol_syslog("API Rest request");
242 $result = $this->db->query($sql);
243
244 if ($result) {
245 $num = $this->db->num_rows($result);
246 $min = min($num, ($limit <= 0 ? $num : $limit));
247 $i = 0;
248 while ($i < $min) {
249 $obj = $this->db->fetch_object($result);
250 $proposal_static = new Propal($this->db);
251 if ($proposal_static->fetch($obj->rowid) > 0) {
252 // Add external contacts ids
253 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
254 if (is_array($tmparray)) {
255 $proposal_static->contacts_ids = $tmparray;
256 }
257
258 if ($loadlinkedobjects) {
259 // retrieve linked objects
260 $proposal_static->fetchObjectLinked();
261 }
262
263 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($proposal_static), $properties);
264 }
265 $i++;
266 }
267 } else {
268 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
269 }
270
271 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
272 if ($pagination_data) {
273 $totalsResult = $this->db->query($sqlTotals);
274 $total = $this->db->fetch_object($totalsResult)->total;
275
276 $tmp = $obj_ret;
277 $obj_ret = [];
278
279 $obj_ret['data'] = $tmp;
280 $obj_ret['pagination'] = [
281 'total' => (int) $total,
282 'page' => $page, //count starts from 0
283 'page_count' => ceil((int) $total / $limit),
284 'limit' => $limit
285 ];
286 }
287
288 return $obj_ret;
289 }
290
303 public function post($request_data = null)
304 {
305 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
306 throw new RestException(403, "Insuffisant rights");
307 }
308 // Check mandatory fields
309 $result = $this->_validate($request_data);
310
311 foreach ($request_data as $field => $value) {
312 if ($field === 'caller') {
313 // 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 with the caller
314 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
315 continue;
316 }
317
318 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
319 }
320 /*if (isset($request_data["lines"])) {
321 $lines = array();
322 foreach ($request_data["lines"] as $line) {
323 array_push($lines, (object) $line);
324 }
325 $this->propal->lines = $lines;
326 }*/
327 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
328 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
329 }
330
331 return ((int) $this->propal->id);
332 }
333
350 public function getLines($id, $sqlfilters = '')
351 {
352 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
353 throw new RestException(403);
354 }
355
356 $result = $this->propal->fetch($id);
357 if (!$result) {
358 throw new RestException(404, 'Commercial Proposal not found');
359 }
360
361 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
362 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
363 }
364
365 $sql = '';
366 if (!empty($sqlfilters)) {
367 $errormessage = '';
368 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
369 if ($errormessage) {
370 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
371 }
372 }
373
374 $this->propal->getLinesArray($sql);
375 $result = array();
376 foreach ($this->propal->lines as $line) {
377 array_push($result, $this->_cleanObjectDatas($line));
378 }
379 return $result;
380 }
381
398 public function postLine($id, $request_data = null)
399 {
400 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
401 throw new RestException(403);
402 }
403
404 $result = $this->propal->fetch($id);
405 if (!$result) {
406 throw new RestException(404, 'Commercial Proposal not found');
407 }
408
409 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
410 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
411 }
412
413 $request_data = (object) $request_data;
414
415 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
416 $request_data->label = sanitizeVal($request_data->label);
417
418 $updateRes = $this->propal->addline(
419 $request_data->desc,
420 $request_data->subprice,
421 $request_data->qty,
422 $request_data->tva_tx,
423 $request_data->localtax1_tx,
424 $request_data->localtax2_tx,
425 $request_data->fk_product,
426 $request_data->remise_percent,
427 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
428 $request_data->subprice,
429 $request_data->info_bits,
430 $request_data->product_type,
431 $request_data->rang,
432 $request_data->special_code,
433 $request_data->fk_parent_line,
434 $request_data->fk_fournprice,
435 $request_data->pa_ht,
436 $request_data->label,
437 $request_data->date_start,
438 $request_data->date_end,
439 $request_data->array_options,
440 $request_data->fk_unit,
441 $request_data->origin,
442 $request_data->origin_id,
443 $request_data->multicurrency_subprice,
444 $request_data->fk_remise_except
445 );
446
447 if ($updateRes > 0) {
448 return $updateRes;
449 } else {
450 throw new RestException(400, $this->propal->error);
451 }
452 }
453
468 public function postLines($id, $request_data = null)
469 {
470 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
471 throw new RestException(403);
472 }
473
474 $result = $this->propal->fetch($id);
475 if (!$result) {
476 throw new RestException(404, 'Commercial Proposal not found');
477 }
478
479 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
480 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
481 }
482
483 $errors = [];
484 $updateRes = 0;
485 $this->db->begin();
486
487 foreach ($request_data as $TData) {
488 if (empty($TData[0])) {
489 $TData = array($TData);
490 }
491
492 foreach ($TData as $lineData) {
493 $line = (object) $lineData;
494
495 $updateRes = $this->propal->addline(
496 $line->desc,
497 $line->subprice,
498 $line->qty,
499 $line->tva_tx,
500 $line->localtax1_tx,
501 $line->localtax2_tx,
502 $line->fk_product,
503 $line->remise_percent,
504 'HT',
505 0,
506 $line->info_bits,
507 $line->product_type,
508 $line->rang,
509 $line->special_code,
510 $line->fk_parent_line,
511 $line->fk_fournprice,
512 $line->pa_ht,
513 $line->label,
514 $line->date_start,
515 $line->date_end,
516 $line->array_options,
517 $line->fk_unit,
518 $line->origin,
519 $line->origin_id,
520 $line->multicurrency_subprice,
521 $line->fk_remise_except
522 );
523
524 if ($updateRes < 0) {
525 $errors['lineLabel'] = $line->label;
526 $errors['msg'] = $this->propal->errors;
527 }
528 }
529 }
530 if (empty($errors)) {
531 $this->db->commit();
532 return $updateRes;
533 } else {
534 $this->db->rollback();
535 throw new RestException(400, implode(", ", $errors));
536 }
537 }
538
555 public function putLine($id, $lineid, $request_data = null)
556 {
557 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
558 throw new RestException(403);
559 }
560
561 $result = $this->propal->fetch($id);
562 if ($result <= 0) {
563 throw new RestException(404, 'Proposal not found');
564 }
565
566 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
567 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
568 }
569
570 $request_data = (object) $request_data;
571
572 if (isset($request_data->desc)) {
573 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
574 }
575 if (isset($request_data->label)) {
576 $request_data->label = sanitizeVal($request_data->label);
577 }
578
579 $propalline = new PropaleLigne($this->db);
580 $result = $propalline->fetch($lineid);
581 if ($result <= 0) {
582 throw new RestException(404, 'Proposal line not found');
583 }
584
585 $updateRes = $this->propal->updateline(
586 $lineid,
587 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
588 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
589 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
590 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
591 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
592 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
593 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
594 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
595 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
596 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
597 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
598 0,
599 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
600 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
601 isset($request_data->label) ? $request_data->label : $propalline->label,
602 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
603 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
604 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
605 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
606 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
607 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
608 0,
609 isset($request_data->rang) ? $request_data->rang : $propalline->rang
610 );
611
612 if ($updateRes > 0) {
613 $result = $this->get($id);
614 unset($result->line);
615 return $this->_cleanObjectDatas($result);
616 }
617 return false;
618 }
619
634 public function deleteLine($id, $lineid)
635 {
636 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
637 throw new RestException(403);
638 }
639
640 $result = $this->propal->fetch($id);
641 if (!$result) {
642 throw new RestException(404, 'Proposal not found');
643 }
644
645 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
646 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
647 }
648
649 $updateRes = $this->propal->deleteLine($lineid, $id);
650 if ($updateRes > 0) {
651 return $this->get($id);
652 } else {
653 throw new RestException(405, $this->propal->error);
654 }
655 }
656
675 public function postContact($id, $contactid, $type, $source = 'external')
676 {
677 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
678 throw new RestException(403);
679 }
680
681 $result = $this->propal->fetch($id);
682
683 if (!$result) {
684 throw new RestException(404, 'Proposal not found');
685 }
686
687 if (!in_array($source, array('internal', 'external'), true)) {
688 throw new RestException(500, 'Availables sources: internal OR external');
689 }
690
691 if ($source == 'external' && !in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
692 throw new RestException(500, 'Availables external types: BILLING, SHIPPING OR CUSTOMER');
693 }
694
695 if ($source == 'internal' && !in_array($type, array('SALESREPFOLL'), true)) {
696 throw new RestException(500, 'Availables internal types: SALESREPFOLL');
697 }
698
699 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
700 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
701 }
702
703 $result = $this->propal->add_contact($contactid, $type, $source);
704
705 if (!$result) {
706 throw new RestException(500, 'Error when added the contact');
707 }
708
709 return array(
710 'success' => array(
711 'code' => 200,
712 'message' => 'Contact linked to the proposal'
713 )
714 );
715 }
716
733 public function deleteContact($id, $contactid, $type)
734 {
735 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
736 throw new RestException(403);
737 }
738
739 $result = $this->propal->fetch($id);
740
741 if (!$result) {
742 throw new RestException(404, 'Proposal not found');
743 }
744
745 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
746 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
747 }
748
749 $contacts = $this->propal->liste_contact();
750
751 foreach ($contacts as $contact) {
752 if ($contact['id'] == $contactid && $contact['code'] == $type) {
753 $result = $this->propal->delete_contact($contact['rowid']);
754
755 if (!$result) {
756 throw new RestException(500, 'Error when deleted the contact');
757 }
758 }
759 }
760
761 return $this->_cleanObjectDatas($this->propal);
762 }
763
777 public function put($id, $request_data = null)
778 {
779 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
780 throw new RestException(403);
781 }
782
783 $result = $this->propal->fetch($id);
784 if (!$result) {
785 throw new RestException(404, 'Proposal not found');
786 }
787
788 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
789 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 }
791 foreach ($request_data as $field => $value) {
792 if ($field == 'id') {
793 continue;
794 }
795 if ($field === 'caller') {
796 // 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 with the caller
797 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
798 continue;
799 }
800 if ($field == 'array_options' && is_array($value)) {
801 foreach ($value as $index => $val) {
802 $this->propal->array_options[$index] = $this->_checkValForAPI($field, $val, $this->propal);
803 }
804 continue;
805 }
806
807 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
808 }
809
810 // update end of validity date
811 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
812 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
813 }
814 if (!empty($this->propal->fin_validite)) {
815 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
816 throw new RestException(500, $this->propal->error);
817 }
818 }
819
820 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
821 return $this->get($id);
822 } else {
823 throw new RestException(500, $this->propal->error);
824 }
825 }
826
839 public function delete($id)
840 {
841 if (!DolibarrApiAccess::$user->hasRight('propal', 'supprimer')) {
842 throw new RestException(403);
843 }
844 $result = $this->propal->fetch($id);
845 if (!$result) {
846 throw new RestException(404, 'Commercial Proposal not found');
847 }
848
849 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
850 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
851 }
852
853 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
854 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
855 }
856
857 return array(
858 'success' => array(
859 'code' => 200,
860 'message' => 'Commercial Proposal deleted'
861 )
862 );
863 }
864
877 public function settodraft($id)
878 {
879 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
880 throw new RestException(403);
881 }
882 $result = $this->propal->fetch($id);
883 if (!$result) {
884 throw new RestException(404, 'Proposal not found');
885 }
886
887 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
888 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
889 }
890
891 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
892 if ($result == 0) {
893 throw new RestException(304, 'Nothing done. May be object is already draft');
894 }
895 if ($result < 0) {
896 throw new RestException(500, 'Error : '.$this->propal->error);
897 }
898
899 $result = $this->propal->fetch($id);
900 if (!$result) {
901 throw new RestException(404, 'Proposal not found');
902 }
903
904 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
905 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
906 }
907
908 $this->propal->fetchObjectLinked();
909
910 return $this->_cleanObjectDatas($this->propal);
911 }
912
913
935 public function validate($id, $notrigger = 0)
936 {
937 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
938 throw new RestException(403);
939 }
940 $result = $this->propal->fetch($id);
941 if (!$result) {
942 throw new RestException(404, 'Commercial Proposal not found');
943 }
944
945 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
946 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
947 }
948
949 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
950 if ($result == 0) {
951 throw new RestException(304, 'Error nothing done. May be object is already validated');
952 }
953 if ($result < 0) {
954 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
955 }
956
957 $result = $this->propal->fetch($id);
958 if (!$result) {
959 throw new RestException(404, 'Commercial Proposal not found');
960 }
961
962 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
963 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
964 }
965
966 $this->propal->fetchObjectLinked();
967
968 return $this->_cleanObjectDatas($this->propal);
969 }
970
987 public function close($id, $status, $note_private = '', $notrigger = 0, $note_public = '')
988 {
989 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
990 throw new RestException(403);
991 }
992 $result = $this->propal->fetch($id);
993 if (!$result) {
994 throw new RestException(404, 'Commercial Proposal not found');
995 }
996
997 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
998 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
999 }
1000
1001 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
1002 if ($result == 0) {
1003 throw new RestException(304, 'Error nothing done. May be object is already closed');
1004 }
1005 if ($result < 0) {
1006 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
1007 }
1008
1009 $result = $this->propal->fetch($id);
1010 if (!$result) {
1011 throw new RestException(404, 'Proposal not found');
1012 }
1013
1014 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1015 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1016 }
1017
1018 $this->propal->fetchObjectLinked();
1019
1020 return $this->_cleanObjectDatas($this->propal);
1021 }
1022
1035 public function setinvoiced($id)
1036 {
1037 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1038 throw new RestException(403);
1039 }
1040 $result = $this->propal->fetch($id);
1041 if (!$result) {
1042 throw new RestException(404, 'Commercial Proposal not found');
1043 }
1044
1045 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1046 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 }
1048
1049 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
1050 if ($result < 0) {
1051 throw new RestException(500, 'Error : '.$this->propal->error);
1052 }
1053
1054 $result = $this->propal->fetch($id);
1055 if (!$result) {
1056 throw new RestException(404, 'Proposal not found');
1057 }
1058
1059 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1060 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1061 }
1062
1063 $this->propal->fetchObjectLinked();
1064
1065 return $this->_cleanObjectDatas($this->propal);
1066 }
1067
1068
1077 private function _validate($data)
1078 {
1079 if ($data === null) {
1080 $data = array();
1081 }
1082 $propal = array();
1083 foreach (Proposals::$FIELDS as $field) {
1084 if (!isset($data[$field])) {
1085 throw new RestException(400, "$field field missing");
1086 }
1087 $propal[$field] = $data[$field];
1088 }
1089 return $propal;
1090 }
1091
1092
1093 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1100 protected function _cleanObjectDatas($object)
1101 {
1102 // phpcs:enable
1103 $object = parent::_cleanObjectDatas($object);
1104
1105 unset($object->note);
1106 unset($object->name);
1107 unset($object->lastname);
1108 unset($object->firstname);
1109 unset($object->civility_id);
1110 unset($object->address);
1111
1112 return $object;
1113 }
1114}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
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 commercial proposal to draft.
put($id, $request_data=null)
Update a commercial proposal general fields (won't change lines of commercial proposal)
setinvoiced($id)
Set a commercial proposal to billed.
post($request_data=null)
Create a commercial proposal.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List commercial proposals.
postContact($id, $contactid, $type, $source='external')
Add (link) a contact to a commercial proposal.
close($id, $status, $note_private='', $notrigger=0, $note_public='')
Close (accept or refuse) a commercial proposal.
getByRefExt($ref_ext, $contact_list=1)
Get a commercial proposal by ref_ext.
postLine($id, $request_data=null)
Add a line to a commercial proposal.
_cleanObjectDatas($object)
Clean sensible object datas.
postLines($id, $request_data=null)
Add lines to a commercial proposal.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of a commercial proposal.
deleteContact($id, $contactid, $type)
Remove (unlink) a contact from commercial proposal.
validate($id, $notrigger=0)
Validate a commercial proposal.
__construct()
Constructor.
getByRef($ref, $contact_list=1)
Get a commercial proposal by ref.
putLine($id, $lineid, $request_data=null)
Update a line of a 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.