dolibarr 24.0.1
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-2026 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2025 William Mead <william@m34d.com>
10 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26use Luracast\Restler\RestException;
27
28require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
29
30
40{
44 public static $FIELDS = array(
45 'socid'
46 );
47
51 public $propal;
52
56 public function __construct()
57 {
58 global $db;
59 $this->db = $db;
60 $this->propal = new Propal($this->db);
61 }
62
76 public function get($id, $contact_list = 1)
77 {
78 return $this->_fetch($id, '', '', $contact_list);
79 }
80
96 public function getByRef($ref, $contact_list = 1)
97 {
98 return $this->_fetch(0, $ref, '', $contact_list);
99 }
100
116 public function getByRefExt($ref_ext, $contact_list = 1)
117 {
118 return $this->_fetch(0, '', $ref_ext, $contact_list);
119 }
120
134 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
135 {
136 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
137 throw new RestException(403);
138 }
139 if (empty($id) && empty($ref) && empty($ref_ext)) {
140 throw new RestException(400, 'No ID or Ref provided');
141 }
142 $result = $this->propal->fetch($id, $ref, $ref_ext);
143 if (!$result) {
144 throw new RestException(404, 'Commercial Proposal not found');
145 }
146
147 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
148 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
149 }
150
151 if ($contact_list > -1) {
152 // Add external contacts ids.
153 $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
154 if (is_array($tmparray)) {
155 $this->propal->contacts_ids = $tmparray;
156 }
157 $tmparray = $this->propal->liste_contact(-1, 'internal', $contact_list);
158 if (is_array($tmparray)) {
159 $this->propal->contacts_ids_internal = $tmparray;
160 }
161 }
162
163 $this->propal->fetchObjectLinked();
164
165 return $this->_cleanObjectDatas($this->propal);
166 }
167
190 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
191 {
192 global $hookmanager;
193
194 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
195 throw new RestException(403);
196 }
197
198 $obj_ret = array();
199
200 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
201 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
202
203 // If the internal user must only see his customers, force searching by him
204 $search_sale = 0;
205 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
206 $search_sale = DolibarrApiAccess::$user->id;
207 }
208 $sql = "SELECT t.rowid";
209 $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t";
210 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = t.fk_soc)";
211 $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
212 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
213 if ($socids) {
214 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
215 }
216 // Search on sale representative
217 if ($search_sale && $search_sale != '-1') {
218 if ($search_sale == -2) {
219 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
220 } elseif ($search_sale > 0) {
221 $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).")";
222 }
223 }
224 $parameters = array();
225 $hookmanager->executeHooks('printFieldListWhere', $parameters, $this->propal); // Note that $action and $object may have been modified by hook
226 $sql .= $hookmanager->resPrint;
227 // Add sql filters
228 if ($sqlfilters) {
229 $errormessage = '';
230 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
231 if ($errormessage) {
232 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
233 }
234 }
235
236 //this query will return total proposals with the filters given
237 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
238
239 $sql .= $this->db->order($sortfield, $sortorder);
240 if ($limit) {
241 if ($page < 0) {
242 $page = 0;
243 }
244 $offset = $limit * $page;
245
246 $sql .= $this->db->plimit($limit + 1, $offset);
247 }
248
249 dol_syslog("API Rest request");
250 $result = $this->db->query($sql);
251
252 if ($result) {
253 $num = $this->db->num_rows($result);
254 $min = min($num, ($limit <= 0 ? $num : $limit));
255 $i = 0;
256 while ($i < $min) {
257 $obj = $this->db->fetch_object($result);
258 $proposal_static = new Propal($this->db);
259 if ($proposal_static->fetch($obj->rowid) > 0) {
260 // Add external contacts ids
261 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
262 if (is_array($tmparray)) {
263 $proposal_static->contacts_ids = $tmparray;
264 }
265
266 if ($loadlinkedobjects) {
267 // retrieve linked objects
268 $proposal_static->fetchObjectLinked();
269 }
270
271 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($proposal_static), $properties);
272 }
273 $i++;
274 }
275 } else {
276 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
277 }
278
279 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
280 if ($pagination_data) {
281 $totalsResult = $this->db->query($sqlTotals);
282 $total = $this->db->fetch_object($totalsResult)->total;
283
284 $tmp = $obj_ret;
285 $obj_ret = [];
286
287 $obj_ret['data'] = $tmp;
288 $obj_ret['pagination'] = [
289 'total' => (int) $total,
290 'page' => $page, //count starts from 0
291 'page_count' => ceil((int) $total / $limit),
292 'limit' => $limit
293 ];
294 }
295
296 return $obj_ret;
297 }
298
311 public function post($request_data = null)
312 {
313 global $conf;
314 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
315 throw new RestException(403, "Insufficiant rights");
316 }
317
318 // Check mandatory fields
319 $this->_validate($request_data);
320
321 // Check thirdparty validity
322 $socid = (int) $request_data['socid'];
323 $thirdpartytmp = new Societe($this->db);
324 $thirdparty_result = $thirdpartytmp->fetch($socid);
325 if ($thirdparty_result < 1) {
326 throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed');
327 }
328 if (!DolibarrApi::_checkAccessToResource('societe', $thirdpartytmp->id)) {
329 throw new RestException(404, 'Thirdparty with id='.$thirdpartytmp->id.' not found or not allowed');
330 }
331
332 foreach ($request_data as $field => $value) {
333 if ($field === 'caller') {
334 // 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
335 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
336 continue;
337 }
338 if ($field == 'id') {
339 throw new RestException(400, 'Creating with id field is forbidden');
340 }
341 if ($field == 'entity' && ((int) $value) != ((int) $conf->entity)) {
342 throw new RestException(403, 'Creating with entity='.((int) $value).' MUST be the same entity='.((int) $conf->entity).' as your API user/key belongs to');
343 }
344
345 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
346 }
347 /*if (isset($request_data["lines"])) {
348 $lines = array();
349 foreach ($request_data["lines"] as $line) {
350 array_push($lines, (object) $line);
351 }
352 $this->propal->lines = $lines;
353 }*/
354 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
355 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
356 }
357
358 return ((int) $this->propal->id);
359 }
360
377 public function getLines($id, $sqlfilters = '')
378 {
379 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
380 throw new RestException(403);
381 }
382
383 $result = $this->propal->fetch($id);
384 if (!$result) {
385 throw new RestException(404, 'Commercial Proposal not found');
386 }
387
388 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
389 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 }
391
392 $sql = '';
393 if (!empty($sqlfilters)) {
394 $errormessage = '';
395 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
396 if ($errormessage) {
397 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
398 }
399 }
400
401 $this->propal->getLinesArray($sql);
402 $result = array();
403 foreach ($this->propal->lines as $line) {
404 array_push($result, $this->_cleanObjectDatas($line));
405 }
406 return $result;
407 }
408
425 public function postLine($id, $request_data = null)
426 {
427 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
428 throw new RestException(403);
429 }
430
431 $result = $this->propal->fetch($id);
432 if (!$result) {
433 throw new RestException(404, 'Commercial Proposal not found');
434 }
435
436 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
437 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
438 }
439
440 $request_data = (object) $request_data;
441
442 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
443 $request_data->label = sanitizeVal($request_data->label);
444
445 $updateRes = $this->propal->addline(
446 $request_data->desc,
447 $request_data->subprice,
448 $request_data->qty,
449 $request_data->tva_tx,
450 $request_data->localtax1_tx,
451 $request_data->localtax2_tx,
452 $request_data->fk_product,
453 $request_data->remise_percent,
454 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
455 $request_data->subprice,
456 $request_data->info_bits,
457 $request_data->product_type,
458 $request_data->rang,
459 $request_data->special_code,
460 $request_data->fk_parent_line,
461 $request_data->fk_fournprice,
462 $request_data->pa_ht,
463 $request_data->label,
464 $request_data->date_start,
465 $request_data->date_end,
466 $request_data->array_options,
467 $request_data->fk_unit,
468 $request_data->origin,
469 $request_data->origin_id,
470 $request_data->multicurrency_subprice,
471 $request_data->fk_remise_except
472 );
473
474 if ($updateRes > 0) {
475 return $updateRes;
476 } else {
477 throw new RestException(400, $this->propal->error);
478 }
479 }
480
495 public function postLines($id, $request_data = null)
496 {
497 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
498 throw new RestException(403);
499 }
500
501 $result = $this->propal->fetch($id);
502 if (!$result) {
503 throw new RestException(404, 'Commercial Proposal not found');
504 }
505
506 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
507 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 }
509
510 $errors = [];
511 $updateRes = 0;
512 $this->db->begin();
513
514 foreach ($request_data as $TData) {
515 if (empty($TData[0])) {
516 $TData = array($TData);
517 }
518
519 foreach ($TData as $lineData) {
520 $line = (object) $lineData;
521
522 $updateRes = $this->propal->addline(
523 $line->desc,
524 $line->subprice,
525 $line->qty,
526 $line->tva_tx,
527 $line->localtax1_tx,
528 $line->localtax2_tx,
529 $line->fk_product,
530 $line->remise_percent,
531 'HT',
532 0,
533 $line->info_bits,
534 $line->product_type,
535 $line->rang,
536 $line->special_code,
537 $line->fk_parent_line,
538 $line->fk_fournprice,
539 $line->pa_ht,
540 $line->label,
541 $line->date_start,
542 $line->date_end,
543 $line->array_options,
544 $line->fk_unit,
545 $line->origin,
546 $line->origin_id,
547 $line->multicurrency_subprice,
548 $line->fk_remise_except
549 );
550
551 if ($updateRes < 0) {
552 $errors['lineLabel'] = $line->label;
553 $errors['msg'] = $this->propal->errors;
554 }
555 }
556 }
557 if (empty($errors)) {
558 $this->db->commit();
559 return $updateRes;
560 } else {
561 $this->db->rollback();
562 throw new RestException(400, implode(", ", $errors));
563 }
564 }
565
582 public function putLine($id, $lineid, $request_data = null)
583 {
584 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
585 throw new RestException(403);
586 }
587
588 $result = $this->propal->fetch($id);
589 if ($result <= 0) {
590 throw new RestException(404, 'Proposal not found');
591 }
592
593 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
594 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
595 }
596
597 $request_data = (object) $request_data;
598
599 if (isset($request_data->desc)) {
600 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
601 }
602 if (isset($request_data->label)) {
603 $request_data->label = sanitizeVal($request_data->label);
604 }
605
606 $propalline = new PropaleLigne($this->db);
607 $result = $propalline->fetch($lineid);
608 if ($result <= 0) {
609 throw new RestException(404, 'Proposal line not found');
610 }
611
612 if ($propalline->fk_propal != $id) {
613 throw new RestException(403, 'Line does not belong to this proposal');
614 }
615
616 $updateRes = $this->propal->updateline(
617 $lineid,
618 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
619 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
620 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
621 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
622 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
623 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
624 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
625 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
626 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
627 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
628 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
629 0,
630 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
631 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
632 isset($request_data->label) ? $request_data->label : $propalline->label,
633 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
634 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
635 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
636 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
637 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
638 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
639 0,
640 isset($request_data->rang) ? $request_data->rang : $propalline->rang
641 );
642
643 if ($updateRes > 0) {
644 $result = $this->get($id);
645 unset($result->line);
646 return $this->_cleanObjectDatas($result);
647 }
648 return false;
649 }
650
665 public function deleteLine($id, $lineid)
666 {
667 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
668 throw new RestException(403);
669 }
670
671 $result = $this->propal->fetch($id);
672 if (!$result) {
673 throw new RestException(404, 'Proposal not found');
674 }
675
676 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
677 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
678 }
679
680 $updateRes = $this->propal->deleteLine($lineid, $id);
681 if ($updateRes > 0) {
682 return $this->get($id);
683 } else {
684 throw new RestException(405, $this->propal->error);
685 }
686 }
700 public function getContacts($id, $type = '')
701 {
702 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
703 throw new RestException(403);
704 }
705
706 $result = $this->propal->fetch($id);
707 if (!$result) {
708 throw new RestException(404, 'Proposal not found');
709 }
710
711 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
712 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 }
714
715 $contacts = $this->propal->liste_contact(-1, 'external', 0, $type);
716 $socpeoples = $this->propal->liste_contact(-1, 'internal', 0, $type);
717
718 $contacts = array_merge($contacts, $socpeoples);
719
720 return $contacts;
721 }
722
744 public function postContact($id, $contactid, $type, $source = 'external', $notrigger = 0)
745 {
746 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
747 throw new RestException(403);
748 }
749
750 // test source
751 if (empty($source)) {
752 throw new RestException(400, 'Source can not be empty');
753 }
754 $sql_distinct_source = "SELECT DISTINCT source";
755 $sql_distinct_source .= " FROM ".MAIN_DB_PREFIX."c_type_contact";
756 $sql_distinct_source .= " WHERE element LIKE 'propal'";
757 $sql_distinct_source .= " AND source is NOT NULL";
758 $sql_distinct_source .= " AND active != 0";
759 $source_result = $this->db->query($sql_distinct_source);
760 $source_array = array();
761
762 if ($source_result) {
763 $num = $this->db->num_rows($source_result);
764 $i = 0;
765 while ($i < $num) {
766 $obj = $this->db->fetch_object($source_result);
767 $source_kind = (string) $obj->source;
768 array_push($source_array, $source_kind);
769 dol_syslog("source_kind=".$source_kind);
770 $i++;
771 }
772 } else {
773 throw new RestException(503, 'Error when retrieving a list of propal contact sources: '.$this->db->lasterror());
774 }
775 if (!in_array($source, (array) $source_array, true)) {
776 throw new RestException(400, 'Combo of Source='.$source.' and Type='.$type.' not found in dictionary with active proposal contact types');
777 }
778
779 // test type
780 if (empty($type)) {
781 throw new RestException(400, 'type can not be empty');
782 }
783 // variable called type here, but code in dictionary and database
784 $sql_distinct_type = "SELECT DISTINCT code";
785 $sql_distinct_type .= " FROM ".MAIN_DB_PREFIX."c_type_contact";
786 $sql_distinct_type .= " WHERE element LIKE 'propal'";
787 $sql_distinct_type .= " AND source='".$this->db->escape($source)."'";
788 $sql_distinct_type .= " AND code is NOT NULL";
789 $sql_distinct_type .= " AND active != 0";
790 $type_result = $this->db->query($sql_distinct_type);
791 $type_array = array();
792
793 if ($type_result) {
794 $num = $this->db->num_rows($type_result);
795 $i = 0;
796 while ($i < $num) {
797 $obj = $this->db->fetch_object($type_result);
798 // variable called type here, but code in dictionary and database
799 $type_kind = (string) $obj->code;
800 array_push($type_array, $type_kind);
801 dol_syslog("type_kind=".$type_kind);
802 $i++;
803 }
804 } else {
805 throw new RestException(503, 'Error when retrieving a list of propal contact types: '.$this->db->lasterror());
806 }
807 if (!in_array($type, (array) $type_array, true)) {
808 throw new RestException(400, 'Combo of Type='.$type.' and Source='.$source.' not found in dictionary with active proposal contact types');
809 }
810
811 // tests done, let's get it
812 $result = $this->propal->fetch($id);
813 if (!$result) {
814 throw new RestException(404, 'Proposal not found');
815 }
816 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
817 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
818 }
819
820 $result = $this->propal->add_contact($contactid, $type, $source, $notrigger);
821
822 if ($result == 0) {
823 throw new RestException(400, 'Already exists: Contact='.$contactid.' is already linked to the proposal='.$id.' as source='.$source.' and type='.$type);
824 } elseif ($result == -1) {
825 throw new RestException(400, 'Wrong contact='.$contactid);
826 } elseif ($result == -2) {
827 throw new RestException(400, 'Wrong type='.$type);
828 } elseif ($result == -3) {
829 throw new RestException(400, 'Not allowed contacts');
830 } elseif ($result == -4) {
831 throw new RestException(400, 'ErrorCommercialNotAllowedForThirdparty');
832 } elseif ($result == -5) {
833 throw new RestException(400, 'Trigger failed');
834 } elseif ($result == -6) {
835 throw new RestException(400, 'DB_ERROR_RECORD_ALREADY_EXISTS');
836 } elseif ($result == -7) {
837 throw new RestException(400, 'Some other error');
838 }
839
840 if (!$result) {
841 throw new RestException(500, 'Error when added the contact');
842 }
843
844 return array(
845 'success' => array(
846 'code' => 200,
847 'message' => 'Contact='.$contactid.' linked to the proposal='.$id.' as '.$source.' '.$type
848 )
849 );
850 }
851
868 public function deleteContact($id, $contactid, $type)
869 {
870 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
871 throw new RestException(403);
872 }
873
874 $result = $this->propal->fetch($id);
875
876 if (!$result) {
877 throw new RestException(404, 'Proposal not found');
878 }
879
880 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
881 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
882 }
883 foreach (array('internal', 'external') as $source) {
884 $contacts = $this->propal->liste_contact(-1, $source);
885 foreach ($contacts as $contact) {
886 if ($contact['id'] == $contactid && $contact['code'] == $type) {
887 $result = $this->propal->delete_contact($contact['rowid']);
888
889 if (!$result) {
890 throw new RestException(500, 'Error when deleted the contact');
891 }
892 }
893 }
894 }
895 return $this->_cleanObjectDatas($this->propal);
896 }
897
911 public function put($id, $request_data = null)
912 {
913 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
914 throw new RestException(403);
915 }
916 if ($id == 0) {
917 throw new RestException(400, 'No proposal with id=0 can exist');
918 }
919 $result = $this->propal->fetch($id);
920 if (!$result) {
921 throw new RestException(404, 'Proposal not found');
922 }
923
924 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
925 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
926 }
927 foreach ($request_data as $field => $value) {
928 if ($field == 'id') {
929 continue;
930 }
931 if ($field === 'caller') {
932 // 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
933 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
934 continue;
935 }
936 if ($field == 'array_options' && is_array($value)) {
937 foreach ($value as $index => $val) {
938 $this->propal->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->propal);
939 }
940 continue;
941 }
942
943 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
944 }
945
946 // update end of validity date
947 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
948 $this->propal->fin_validite = $this->propal->date_creation + (int) ($this->propal->duree_validite * 24 * 3600);
949 }
950 if (!empty($this->propal->fin_validite)) {
951 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
952 throw new RestException(500, $this->propal->error);
953 }
954 }
955
956 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
957 return $this->get($id);
958 } else {
959 throw new RestException(500, $this->propal->error);
960 }
961 }
962
975 public function delete($id)
976 {
977 if (!DolibarrApiAccess::$user->hasRight('propal', 'supprimer')) {
978 throw new RestException(403);
979 }
980 if ($id == 0) {
981 throw new RestException(400, 'No proposal with id=0 can exist');
982 }
983
984 $result = $this->propal->fetch($id);
985 if (!$result) {
986 throw new RestException(404, 'Commercial Proposal not found');
987 }
988
989 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
990 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 }
992
993 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
994 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
995 }
996
997 return array(
998 'success' => array(
999 'code' => 200,
1000 'message' => 'Commercial Proposal deleted'
1001 )
1002 );
1003 }
1004
1017 public function settodraft($id)
1018 {
1019 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1020 throw new RestException(403);
1021 }
1022 $result = $this->propal->fetch($id);
1023 if (!$result) {
1024 throw new RestException(404, 'Proposal not found');
1025 }
1026
1027 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1028 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1029 }
1030
1031 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
1032 if ($result == 0) {
1033 throw new RestException(304, 'Nothing done. May be object is already draft');
1034 }
1035 if ($result < 0) {
1036 throw new RestException(500, 'Error : '.$this->propal->error);
1037 }
1038
1039 $result = $this->propal->fetch($id);
1040 if (!$result) {
1041 throw new RestException(404, 'Proposal not found');
1042 }
1043
1044 // test already done
1045 // if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1046 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 // }
1048
1049 $this->propal->fetchObjectLinked();
1050
1051 return $this->_cleanObjectDatas($this->propal);
1052 }
1053
1054
1076 public function validate($id, $notrigger = 0)
1077 {
1078 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1079 throw new RestException(403);
1080 }
1081 $result = $this->propal->fetch($id);
1082 if (!$result) {
1083 throw new RestException(404, 'Commercial Proposal not found');
1084 }
1085
1086 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1087 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1088 }
1089
1090 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
1091 if ($result == 0) {
1092 throw new RestException(304, 'Error nothing done. May be object is already validated');
1093 }
1094 if ($result < 0) {
1095 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
1096 }
1097
1098 $result = $this->propal->fetch($id);
1099 if (!$result) {
1100 throw new RestException(404, 'Commercial Proposal not found');
1101 }
1102
1103 // test already done
1104 // if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1105 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1106 // }
1107
1108 $this->propal->fetchObjectLinked();
1109
1110 return $this->_cleanObjectDatas($this->propal);
1111 }
1112
1129 public function close($id, $status, $note_private = '', $notrigger = 0, $note_public = '')
1130 {
1131 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1132 throw new RestException(403);
1133 }
1134 $result = $this->propal->fetch($id);
1135 if (!$result) {
1136 throw new RestException(404, 'Commercial Proposal not found');
1137 }
1138
1139 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1140 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1141 }
1142
1143 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
1144 if ($result == 0) {
1145 throw new RestException(304, 'Error nothing done. May be object is already closed');
1146 }
1147 if ($result < 0) {
1148 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
1149 }
1150
1151 $result = $this->propal->fetch($id);
1152 if (!$result) {
1153 throw new RestException(404, 'Proposal not found');
1154 }
1155
1156 // test already done
1157 // if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1158 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1159 // }
1160
1161 $this->propal->fetchObjectLinked();
1162
1163 return $this->_cleanObjectDatas($this->propal);
1164 }
1165
1178 public function setinvoiced($id)
1179 {
1180 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
1181 throw new RestException(403);
1182 }
1183 $result = $this->propal->fetch($id);
1184 if (!$result) {
1185 throw new RestException(404, 'Commercial Proposal not found');
1186 }
1187
1188 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1189 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1190 }
1191
1192 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
1193 if ($result < 0) {
1194 throw new RestException(500, 'Error : '.$this->propal->error);
1195 }
1196
1197 $result = $this->propal->fetch($id);
1198 if (!$result) {
1199 throw new RestException(404, 'Proposal not found');
1200 }
1201
1202 // test already done
1203 // if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
1204 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1205 // }
1206
1207 $this->propal->fetchObjectLinked();
1208
1209 return $this->_cleanObjectDatas($this->propal);
1210 }
1211
1212
1221 private function _validate($data)
1222 {
1223 if ($data === null) {
1224 $data = array();
1225 }
1226 $propal = array();
1227 foreach (Proposals::$FIELDS as $field) {
1228 if (!isset($data[$field])) {
1229 throw new RestException(400, "$field field missing");
1230 }
1231 $propal[$field] = $data[$field];
1232 }
1233 return $propal;
1234 }
1235
1236
1237 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1247 protected function _cleanObjectDatas($object)
1248 {
1249 // phpcs:enable
1250 $object = parent::_cleanObjectDatas($object);
1251
1252 unset($object->note);
1253 unset($object->name);
1254 unset($object->lastname);
1255 unset($object->firstname);
1256 unset($object->civility_id);
1257 unset($object->address);
1258
1259 return $object;
1260 }
1261}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
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 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', $notrigger=0)
Add (link) a contact to a commercial proposal.
getContacts($id, $type='')
Get contacts of given 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 @phpstan-template T.
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.
Class to manage third parties objects (customers, suppliers, prospects...)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php