dolibarr 21.0.0-beta
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 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24use Luracast\Restler\RestException;
25
26require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
27
28
36{
40 public static $FIELDS = array(
41 'socid'
42 );
43
47 public $propal;
48
52 public function __construct()
53 {
54 global $db;
55 $this->db = $db;
56 $this->propal = new Propal($this->db);
57 }
58
70 public function get($id, $contact_list = 1)
71 {
72 return $this->_fetch($id, '', '', $contact_list);
73 }
74
88 public function getByRef($ref, $contact_list = 1)
89 {
90 return $this->_fetch(0, $ref, '', $contact_list);
91 }
92
106 public function getByRefExt($ref_ext, $contact_list = 1)
107 {
108 return $this->_fetch(0, '', $ref_ext, $contact_list);
109 }
110
124 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
125 {
126 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
127 throw new RestException(403);
128 }
129
130 $result = $this->propal->fetch($id, $ref, $ref_ext);
131 if (!$result) {
132 throw new RestException(404, 'Commercial Proposal not found');
133 }
134
135 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
136 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 }
138
139 // Add external contacts ids.
140 $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
141 if (is_array($tmparray)) {
142 $this->propal->contacts_ids = $tmparray;
143 }
144
145 $this->propal->fetchObjectLinked();
146
147 return $this->_cleanObjectDatas($this->propal);
148 }
149
166 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
167 {
168 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
169 throw new RestException(403);
170 }
171
172 $obj_ret = array();
173
174 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
175 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
176
177 // If the internal user must only see his customers, force searching by him
178 $search_sale = 0;
179 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
181 }
182
183 $sql = "SELECT t.rowid";
184 $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t";
185 $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
186 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
187 if ($socids) {
188 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
189 }
190 // Search on sale representative
191 if ($search_sale && $search_sale != '-1') {
192 if ($search_sale == -2) {
193 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
194 } elseif ($search_sale > 0) {
195 $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).")";
196 }
197 }
198 // Add sql filters
199 if ($sqlfilters) {
200 $errormessage = '';
201 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
202 if ($errormessage) {
203 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
204 }
205 }
206
207 //this query will return total proposals with the filters given
208 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
209
210 $sql .= $this->db->order($sortfield, $sortorder);
211 if ($limit) {
212 if ($page < 0) {
213 $page = 0;
214 }
215 $offset = $limit * $page;
216
217 $sql .= $this->db->plimit($limit + 1, $offset);
218 }
219
220 dol_syslog("API Rest request");
221 $result = $this->db->query($sql);
222
223 if ($result) {
224 $num = $this->db->num_rows($result);
225 $min = min($num, ($limit <= 0 ? $num : $limit));
226 $i = 0;
227 while ($i < $min) {
228 $obj = $this->db->fetch_object($result);
229 $proposal_static = new Propal($this->db);
230 if ($proposal_static->fetch($obj->rowid) > 0) {
231 // Add external contacts ids
232 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
233 if (is_array($tmparray)) {
234 $proposal_static->contacts_ids = $tmparray;
235 }
236
237 if ($loadlinkedobjects) {
238 // retrieve linked objects
239 $proposal_static->fetchObjectLinked();
240 }
241
242 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($proposal_static), $properties);
243 }
244 $i++;
245 }
246 } else {
247 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
248 }
249
250 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
251 if ($pagination_data) {
252 $totalsResult = $this->db->query($sqlTotals);
253 $total = $this->db->fetch_object($totalsResult)->total;
254
255 $tmp = $obj_ret;
256 $obj_ret = [];
257
258 $obj_ret['data'] = $tmp;
259 $obj_ret['pagination'] = [
260 'total' => (int) $total,
261 'page' => $page, //count starts from 0
262 'page_count' => ceil((int) $total / $limit),
263 'limit' => $limit
264 ];
265 }
266
267 return $obj_ret;
268 }
269
276 public function post($request_data = null)
277 {
278 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
279 throw new RestException(403, "Insuffisant rights");
280 }
281 // Check mandatory fields
282 $result = $this->_validate($request_data);
283
284 foreach ($request_data as $field => $value) {
285 if ($field === 'caller') {
286 // 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
287 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
288 continue;
289 }
290
291 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
292 }
293 /*if (isset($request_data["lines"])) {
294 $lines = array();
295 foreach ($request_data["lines"] as $line) {
296 array_push($lines, (object) $line);
297 }
298 $this->propal->lines = $lines;
299 }*/
300 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
301 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
302 }
303
304 return ((int) $this->propal->id);
305 }
306
317 public function getLines($id, $sqlfilters = '')
318 {
319 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
320 throw new RestException(403);
321 }
322
323 $result = $this->propal->fetch($id);
324 if (!$result) {
325 throw new RestException(404, 'Commercial Proposal not found');
326 }
327
328 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
329 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
330 }
331
332 $sql = '';
333 if (!empty($sqlfilters)) {
334 $errormessage = '';
335 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
336 if ($errormessage) {
337 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
338 }
339 }
340
341 $this->propal->getLinesArray($sql);
342 $result = array();
343 foreach ($this->propal->lines as $line) {
344 array_push($result, $this->_cleanObjectDatas($line));
345 }
346 return $result;
347 }
348
359 public function postLine($id, $request_data = null)
360 {
361 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
362 throw new RestException(403);
363 }
364
365 $result = $this->propal->fetch($id);
366 if (!$result) {
367 throw new RestException(404, 'Commercial Proposal not found');
368 }
369
370 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
371 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
372 }
373
374 $request_data = (object) $request_data;
375
376 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
377 $request_data->label = sanitizeVal($request_data->label);
378
379 $updateRes = $this->propal->addline(
380 $request_data->desc,
381 $request_data->subprice,
382 $request_data->qty,
383 $request_data->tva_tx,
384 $request_data->localtax1_tx,
385 $request_data->localtax2_tx,
386 $request_data->fk_product,
387 $request_data->remise_percent,
388 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
389 $request_data->subprice,
390 $request_data->info_bits,
391 $request_data->product_type,
392 $request_data->rang,
393 $request_data->special_code,
394 $request_data->fk_parent_line,
395 $request_data->fk_fournprice,
396 $request_data->pa_ht,
397 $request_data->label,
398 $request_data->date_start,
399 $request_data->date_end,
400 $request_data->array_options,
401 $request_data->fk_unit,
402 $request_data->origin,
403 $request_data->origin_id,
404 $request_data->multicurrency_subprice,
405 $request_data->fk_remise_except
406 );
407
408 if ($updateRes > 0) {
409 return $updateRes;
410 } else {
411 throw new RestException(400, $this->propal->error);
412 }
413 }
414
425 public function postLines($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 $errors = [];
441 $updateRes = 0;
442 $this->db->begin();
443
444 foreach ($request_data as $TData) {
445 if (empty($TData[0])) {
446 $TData = array($TData);
447 }
448
449 foreach ($TData as $lineData) {
450 $line = (object) $lineData;
451
452 $updateRes = $this->propal->addline(
453 $line->desc,
454 $line->subprice,
455 $line->qty,
456 $line->tva_tx,
457 $line->localtax1_tx,
458 $line->localtax2_tx,
459 $line->fk_product,
460 $line->remise_percent,
461 'HT',
462 0,
463 $line->info_bits,
464 $line->product_type,
465 $line->rang,
466 $line->special_code,
467 $line->fk_parent_line,
468 $line->fk_fournprice,
469 $line->pa_ht,
470 $line->label,
471 $line->date_start,
472 $line->date_end,
473 $line->array_options,
474 $line->fk_unit,
475 $line->origin,
476 $line->origin_id,
477 $line->multicurrency_subprice,
478 $line->fk_remise_except
479 );
480
481 if ($updateRes < 0) {
482 $errors['lineLabel'] = $line->label;
483 $errors['msg'] = $this->propal->errors;
484 }
485 }
486 }
487 if (empty($errors)) {
488 $this->db->commit();
489 return $updateRes;
490 } else {
491 $this->db->rollback();
492 throw new RestException(400, implode(", ", $errors));
493 }
494 }
495
506 public function putLine($id, $lineid, $request_data = null)
507 {
508 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
509 throw new RestException(403);
510 }
511
512 $result = $this->propal->fetch($id);
513 if ($result <= 0) {
514 throw new RestException(404, 'Proposal not found');
515 }
516
517 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
518 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
519 }
520
521 $request_data = (object) $request_data;
522
523 if (isset($request_data->desc)) {
524 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
525 }
526 if (isset($request_data->label)) {
527 $request_data->label = sanitizeVal($request_data->label);
528 }
529
530 $propalline = new PropaleLigne($this->db);
531 $result = $propalline->fetch($lineid);
532 if ($result <= 0) {
533 throw new RestException(404, 'Proposal line not found');
534 }
535
536 $updateRes = $this->propal->updateline(
537 $lineid,
538 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
539 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
540 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
541 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
542 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
543 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
544 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
545 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
546 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
547 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
548 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
549 0,
550 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
551 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
552 isset($request_data->label) ? $request_data->label : $propalline->label,
553 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
554 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
555 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
556 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
557 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
558 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
559 0,
560 isset($request_data->rang) ? $request_data->rang : $propalline->rang
561 );
562
563 if ($updateRes > 0) {
564 $result = $this->get($id);
565 unset($result->line);
566 return $this->_cleanObjectDatas($result);
567 }
568 return false;
569 }
570
584 public function deleteLine($id, $lineid)
585 {
586 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
587 throw new RestException(403);
588 }
589
590 $result = $this->propal->fetch($id);
591 if (!$result) {
592 throw new RestException(404, 'Proposal not found');
593 }
594
595 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
596 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
597 }
598
599 $updateRes = $this->propal->deleteLine($lineid, $id);
600 if ($updateRes > 0) {
601 return $this->get($id);
602 } else {
603 throw new RestException(405, $this->propal->error);
604 }
605 }
606
621 public function postContact($id, $contactid, $type, $source = 'external')
622 {
623 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
624 throw new RestException(403);
625 }
626
627 $result = $this->propal->fetch($id);
628
629 if (!$result) {
630 throw new RestException(404, 'Proposal not found');
631 }
632
633 if (!in_array($source, array('internal', 'external'), true)) {
634 throw new RestException(500, 'Availables sources: internal OR external');
635 }
636
637 if ($source == 'external' && !in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
638 throw new RestException(500, 'Availables external types: BILLING, SHIPPING OR CUSTOMER');
639 }
640
641 if ($source == 'internal' && !in_array($type, array('SALESREPFOLL'), true)) {
642 throw new RestException(500, 'Availables internal types: SALESREPFOLL');
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 $result = $this->propal->add_contact($contactid, $type, $source);
650
651 if (!$result) {
652 throw new RestException(500, 'Error when added the contact');
653 }
654
655 return array(
656 'success' => array(
657 'code' => 200,
658 'message' => 'Contact linked to the proposal'
659 )
660 );
661 }
662
677 public function deleteContact($id, $contactid, $type)
678 {
679 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
680 throw new RestException(403);
681 }
682
683 $result = $this->propal->fetch($id);
684
685 if (!$result) {
686 throw new RestException(404, 'Proposal not found');
687 }
688
689 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
690 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
691 }
692
693 $contacts = $this->propal->liste_contact();
694
695 foreach ($contacts as $contact) {
696 if ($contact['id'] == $contactid && $contact['code'] == $type) {
697 $result = $this->propal->delete_contact($contact['rowid']);
698
699 if (!$result) {
700 throw new RestException(500, 'Error when deleted the contact');
701 }
702 }
703 }
704
705 return $this->_cleanObjectDatas($this->propal);
706 }
707
715 public function put($id, $request_data = null)
716 {
717 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
718 throw new RestException(403);
719 }
720
721 $result = $this->propal->fetch($id);
722 if (!$result) {
723 throw new RestException(404, 'Proposal not found');
724 }
725
726 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
727 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 }
729 foreach ($request_data as $field => $value) {
730 if ($field == 'id') {
731 continue;
732 }
733 if ($field === 'caller') {
734 // 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
735 $this->propal->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
736 continue;
737 }
738 if ($field == 'array_options' && is_array($value)) {
739 foreach ($value as $index => $val) {
740 $this->propal->array_options[$index] = $this->_checkValForAPI($field, $val, $this->propal);
741 }
742 continue;
743 }
744
745 $this->propal->$field = $this->_checkValForAPI($field, $value, $this->propal);
746 }
747
748 // update end of validity date
749 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
750 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
751 }
752 if (!empty($this->propal->fin_validite)) {
753 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
754 throw new RestException(500, $this->propal->error);
755 }
756 }
757
758 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
759 return $this->get($id);
760 } else {
761 throw new RestException(500, $this->propal->error);
762 }
763 }
764
771 public function delete($id)
772 {
773 if (!DolibarrApiAccess::$user->hasRight('propal', 'supprimer')) {
774 throw new RestException(403);
775 }
776 $result = $this->propal->fetch($id);
777 if (!$result) {
778 throw new RestException(404, 'Commercial Proposal not found');
779 }
780
781 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
782 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
783 }
784
785 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
786 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
787 }
788
789 return array(
790 'success' => array(
791 'code' => 200,
792 'message' => 'Commercial Proposal deleted'
793 )
794 );
795 }
796
805 public function settodraft($id)
806 {
807 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
808 throw new RestException(403);
809 }
810 $result = $this->propal->fetch($id);
811 if (!$result) {
812 throw new RestException(404, 'Proposal not found');
813 }
814
815 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
816 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
817 }
818
819 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
820 if ($result == 0) {
821 throw new RestException(304, 'Nothing done. May be object is already draft');
822 }
823 if ($result < 0) {
824 throw new RestException(500, 'Error : '.$this->propal->error);
825 }
826
827 $result = $this->propal->fetch($id);
828 if (!$result) {
829 throw new RestException(404, 'Proposal not found');
830 }
831
832 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
833 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
834 }
835
836 $this->propal->fetchObjectLinked();
837
838 return $this->_cleanObjectDatas($this->propal);
839 }
840
841
861 public function validate($id, $notrigger = 0)
862 {
863 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
864 throw new RestException(403);
865 }
866 $result = $this->propal->fetch($id);
867 if (!$result) {
868 throw new RestException(404, 'Commercial Proposal not found');
869 }
870
871 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
872 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
873 }
874
875 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
876 if ($result == 0) {
877 throw new RestException(304, 'Error nothing done. May be object is already validated');
878 }
879 if ($result < 0) {
880 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
881 }
882
883 $result = $this->propal->fetch($id);
884 if (!$result) {
885 throw new RestException(404, 'Commercial Proposal not found');
886 }
887
888 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
889 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
890 }
891
892 $this->propal->fetchObjectLinked();
893
894 return $this->_cleanObjectDatas($this->propal);
895 }
896
909 public function close($id, $status, $note_private = '', $notrigger = 0, $note_public = '')
910 {
911 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
912 throw new RestException(403);
913 }
914 $result = $this->propal->fetch($id);
915 if (!$result) {
916 throw new RestException(404, 'Commercial Proposal not found');
917 }
918
919 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
920 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 }
922
923 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
924 if ($result == 0) {
925 throw new RestException(304, 'Error nothing done. May be object is already closed');
926 }
927 if ($result < 0) {
928 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
929 }
930
931 $result = $this->propal->fetch($id);
932 if (!$result) {
933 throw new RestException(404, 'Proposal not found');
934 }
935
936 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
937 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
938 }
939
940 $this->propal->fetchObjectLinked();
941
942 return $this->_cleanObjectDatas($this->propal);
943 }
944
953 public function setinvoiced($id)
954 {
955 if (!DolibarrApiAccess::$user->hasRight('propal', 'creer')) {
956 throw new RestException(403);
957 }
958 $result = $this->propal->fetch($id);
959 if (!$result) {
960 throw new RestException(404, 'Commercial Proposal not found');
961 }
962
963 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
964 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
965 }
966
967 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
968 if ($result < 0) {
969 throw new RestException(500, 'Error : '.$this->propal->error);
970 }
971
972 $result = $this->propal->fetch($id);
973 if (!$result) {
974 throw new RestException(404, 'Proposal not found');
975 }
976
977 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
978 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
979 }
980
981 $this->propal->fetchObjectLinked();
982
983 return $this->_cleanObjectDatas($this->propal);
984 }
985
986
995 private function _validate($data)
996 {
997 $propal = array();
998 foreach (Proposals::$FIELDS as $field) {
999 if (!isset($data[$field])) {
1000 throw new RestException(400, "$field field missing");
1001 }
1002 $propal[$field] = $data[$field];
1003 }
1004 return $propal;
1005 }
1006
1007
1008 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1015 protected function _cleanObjectDatas($object)
1016 {
1017 // phpcs:enable
1018 $object = parent::_cleanObjectDatas($object);
1019
1020 unset($object->note);
1021 unset($object->name);
1022 unset($object->lastname);
1023 unset($object->firstname);
1024 unset($object->civility_id);
1025 unset($object->address);
1026
1027 return $object;
1028 }
1029}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class for API REST v1.
Definition api.class.php:30
_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:82
Class to manage proposals.
Class to manage commercial proposal lines.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an proposal object.
getLines($id, $sqlfilters='')
Get lines of a commercial proposal.
settodraft($id)
Set a proposal to draft.
put($id, $request_data=null)
Update commercial proposal general fields (won't touch lines of commercial proposal)
setinvoiced($id)
Set a commercial proposal billed.
post($request_data=null)
Create commercial proposal object.
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 a contact type of given commercial proposal.
close($id, $status, $note_private='', $notrigger=0, $note_public='')
Close (Accept or refuse) a quote / commercial proposal.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an proposal object by ref_ext.
postLine($id, $request_data=null)
Add a line to given commercial proposal.
_cleanObjectDatas($object)
Clean sensible object datas.
postLines($id, $request_data=null)
Add lines to given commercial proposal.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of given commercial proposal.
deleteContact($id, $contactid, $type)
Delete a contact type of given commercial proposal.
validate($id, $notrigger=0)
Validate a commercial proposal.
__construct()
Constructor.
getByRef($ref, $contact_list=1)
Get properties of an proposal object by ref.
putLine($id, $lineid, $request_data=null)
Update a line of given commercial proposal.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.