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 if ($propalline->fk_propal != $id) {
586 throw new RestException(403, 'Line does not belong to this proposal');
587 }
588
589 $updateRes = $this->propal->updateline(
590 $lineid,
591 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
592 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
593 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
594 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
595 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
596 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
597 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
598 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
599 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
600 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
601 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
602 0,
603 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
604 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
605 isset($request_data->label) ? $request_data->label : $propalline->label,
606 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
607 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
608 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
609 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
610 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
611 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
612 0,
613 isset($request_data->rang) ? $request_data->rang : $propalline->rang
614 );
615
616 if ($updateRes > 0) {
617 $result = $this->get($id);
618 unset($result->line);
619 return $this->_cleanObjectDatas($result);
620 }
621 return false;
622 }
623
638 public function deleteLine($id, $lineid)
639 {
640 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
641 throw new RestException(403);
642 }
643
644 $result = $this->propal->fetch($id);
645 if (!$result) {
646 throw new RestException(404, 'Proposal not found');
647 }
648
649 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
650 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 }
652
653 $updateRes = $this->propal->deleteLine($lineid, $id);
654 if ($updateRes > 0) {
655 return $this->get($id);
656 } else {
657 throw new RestException(405, $this->propal->error);
658 }
659 }
660
679 public function postContact($id, $contactid, $type, $source = 'external')
680 {
681 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
682 throw new RestException(403);
683 }
684
685 $result = $this->propal->fetch($id);
686
687 if (!$result) {
688 throw new RestException(404, 'Proposal not found');
689 }
690
691 if (!in_array($source, array('internal', 'external'), true)) {
692 throw new RestException(500, 'Availables sources: internal OR external');
693 }
694
695 if ($source == 'external' && !in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
696 throw new RestException(500, 'Availables external types: BILLING, SHIPPING OR CUSTOMER');
697 }
698
699 if ($source == 'internal' && !in_array($type, array('SALESREPFOLL'), true)) {
700 throw new RestException(500, 'Availables internal types: SALESREPFOLL');
701 }
702
703 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
704 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
705 }
706
707 $result = $this->propal->add_contact($contactid, $type, $source);
708
709 if (!$result) {
710 throw new RestException(500, 'Error when added the contact');
711 }
712
713 return array(
714 'success' => array(
715 'code' => 200,
716 'message' => 'Contact linked to the proposal'
717 )
718 );
719 }
720
737 public function deleteContact($id, $contactid, $type)
738 {
739 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
740 throw new RestException(403);
741 }
742
743 $result = $this->propal->fetch($id);
744
745 if (!$result) {
746 throw new RestException(404, 'Proposal not found');
747 }
748
749 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
750 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
751 }
752
753 $contacts = $this->propal->liste_contact();
754
755 foreach ($contacts as $contact) {
756 if ($contact['id'] == $contactid && $contact['code'] == $type) {
757 $result = $this->propal->delete_contact($contact['rowid']);
758
759 if (!$result) {
760 throw new RestException(500, 'Error when deleted the contact');
761 }
762 }
763 }
764
765 return $this->_cleanObjectDatas($this->propal);
766 }
767
781 public function put($id, $request_data = null)
782 {
783 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
784 throw new RestException(403);
785 }
786
787 $result = $this->propal->fetch($id);
788 if (!$result) {
789 throw new RestException(404, 'Proposal not found');
790 }
791
792 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
793 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
794 }
795 foreach ($request_data as $field => $value) {
796 if ($field == 'id') {
797 continue;
798 }
799 if ($field === 'caller') {
800 // 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
801 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
802 continue;
803 }
804 if ($field == 'array_options' && is_array($value)) {
805 foreach ($value as $index => $val) {
806 $this->propal->array_options[$index] = $this->_checkValForAPI($field, $val, $this->propal);
807 }
808 continue;
809 }
810
811 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
812 }
813
814 // update end of validity date
815 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
816 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
817 }
818 if (!empty($this->propal->fin_validite)) {
819 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
820 throw new RestException(500, $this->propal->error);
821 }
822 }
823
824 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
825 return $this->get($id);
826 } else {
827 throw new RestException(500, $this->propal->error);
828 }
829 }
830
843 public function delete($id)
844 {
845 if (!DolibarrApiAccess::$user->hasRight('propal', 'supprimer')) {
846 throw new RestException(403);
847 }
848 $result = $this->propal->fetch($id);
849 if (!$result) {
850 throw new RestException(404, 'Commercial Proposal not found');
851 }
852
853 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
854 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
855 }
856
857 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
858 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
859 }
860
861 return array(
862 'success' => array(
863 'code' => 200,
864 'message' => 'Commercial Proposal deleted'
865 )
866 );
867 }
868
881 public function settodraft($id)
882 {
883 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
884 throw new RestException(403);
885 }
886 $result = $this->propal->fetch($id);
887 if (!$result) {
888 throw new RestException(404, 'Proposal not found');
889 }
890
891 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
892 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 }
894
895 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
896 if ($result == 0) {
897 throw new RestException(304, 'Nothing done. May be object is already draft');
898 }
899 if ($result < 0) {
900 throw new RestException(500, 'Error : '.$this->propal->error);
901 }
902
903 $result = $this->propal->fetch($id);
904 if (!$result) {
905 throw new RestException(404, 'Proposal not found');
906 }
907
908 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
909 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
910 }
911
912 $this->propal->fetchObjectLinked();
913
914 return $this->_cleanObjectDatas($this->propal);
915 }
916
917
939 public function validate($id, $notrigger = 0)
940 {
941 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
942 throw new RestException(403);
943 }
944 $result = $this->propal->fetch($id);
945 if (!$result) {
946 throw new RestException(404, 'Commercial Proposal not found');
947 }
948
949 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
950 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 }
952
953 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
954 if ($result == 0) {
955 throw new RestException(304, 'Error nothing done. May be object is already validated');
956 }
957 if ($result < 0) {
958 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
959 }
960
961 $result = $this->propal->fetch($id);
962 if (!$result) {
963 throw new RestException(404, 'Commercial Proposal not found');
964 }
965
966 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
967 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
968 }
969
970 $this->propal->fetchObjectLinked();
971
972 return $this->_cleanObjectDatas($this->propal);
973 }
974
991 public function close($id, $status, $note_private = '', $notrigger = 0, $note_public = '')
992 {
993 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
994 throw new RestException(403);
995 }
996 $result = $this->propal->fetch($id);
997 if (!$result) {
998 throw new RestException(404, 'Commercial Proposal not found');
999 }
1000
1001 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1002 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1003 }
1004
1005 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
1006 if ($result == 0) {
1007 throw new RestException(304, 'Error nothing done. May be object is already closed');
1008 }
1009 if ($result < 0) {
1010 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
1011 }
1012
1013 $result = $this->propal->fetch($id);
1014 if (!$result) {
1015 throw new RestException(404, 'Proposal not found');
1016 }
1017
1018 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1019 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1020 }
1021
1022 $this->propal->fetchObjectLinked();
1023
1024 return $this->_cleanObjectDatas($this->propal);
1025 }
1026
1039 public function setinvoiced($id)
1040 {
1041 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1042 throw new RestException(403);
1043 }
1044 $result = $this->propal->fetch($id);
1045 if (!$result) {
1046 throw new RestException(404, 'Commercial Proposal not found');
1047 }
1048
1049 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1050 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1051 }
1052
1053 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
1054 if ($result < 0) {
1055 throw new RestException(500, 'Error : '.$this->propal->error);
1056 }
1057
1058 $result = $this->propal->fetch($id);
1059 if (!$result) {
1060 throw new RestException(404, 'Proposal not found');
1061 }
1062
1063 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1064 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1065 }
1066
1067 $this->propal->fetchObjectLinked();
1068
1069 return $this->_cleanObjectDatas($this->propal);
1070 }
1071
1072
1081 private function _validate($data)
1082 {
1083 if ($data === null) {
1084 $data = array();
1085 }
1086 $propal = array();
1087 foreach (Proposals::$FIELDS as $field) {
1088 if (!isset($data[$field])) {
1089 throw new RestException(400, "$field field missing");
1090 }
1091 $propal[$field] = $data[$field];
1092 }
1093 return $propal;
1094 }
1095
1096
1097 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1104 protected function _cleanObjectDatas($object)
1105 {
1106 // phpcs:enable
1107 $object = parent::_cleanObjectDatas($object);
1108
1109 unset($object->note);
1110 unset($object->name);
1111 unset($object->lastname);
1112 unset($object->firstname);
1113 unset($object->civility_id);
1114 unset($object->address);
1115
1116 return $object;
1117 }
1118}
$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.