dolibarr 18.0.6
api_proposals.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
5 * Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
6 * Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
24require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
25
26
34{
38 static $FIELDS = array(
39 'socid'
40 );
41
45 public $propal;
46
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->propal = new Propal($this->db);
55 }
56
68 public function get($id, $contact_list = 1)
69 {
70 return $this->_fetch($id, '', '', $contact_list);
71 }
72
86 public function getByRef($ref, $contact_list = 1)
87 {
88 return $this->_fetch('', $ref, '', $contact_list);
89 }
90
104 public function getByRefExt($ref_ext, $contact_list = 1)
105 {
106 return $this->_fetch('', '', $ref_ext, $contact_list);
107 }
108
122 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
123 {
124 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
125 throw new RestException(401);
126 }
127
128 $result = $this->propal->fetch($id, $ref, $ref_ext);
129 if (!$result) {
130 throw new RestException(404, 'Commercial Proposal not found');
131 }
132
133 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
134 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
135 }
136
137 // Add external contacts ids.
138 $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
139 if (is_array($tmparray)) {
140 $this->propal->contacts_ids = $tmparray;
141 }
142
143 $this->propal->fetchObjectLinked();
144
145 return $this->_cleanObjectDatas($this->propal);
146 }
147
161 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
162 {
163 global $db, $conf;
164
165 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
166 throw new RestException(401);
167 }
168
169 $obj_ret = array();
170
171 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
172 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
173
174 // If the internal user must only see his customers, force searching by him
175 $search_sale = 0;
176 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
177 $search_sale = DolibarrApiAccess::$user->id;
178 }
179
180 $sql = "SELECT t.rowid";
181 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
182 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
183 }
184 $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t LEFT JOIN ".MAIN_DB_PREFIX."propal_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
185
186
187 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
188 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
189 }
190
191 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
192 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
193 $sql .= " AND t.fk_soc = sc.fk_soc";
194 }
195 if ($socids) {
196 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
197 }
198 if ($search_sale > 0) {
199 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
200 }
201 // Insert sale filter
202 if ($search_sale > 0) {
203 $sql .= " AND sc.fk_user = ".((int) $search_sale);
204 }
205 // Add sql filters
206 if ($sqlfilters) {
207 $errormessage = '';
208 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
209 if ($errormessage) {
210 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
211 }
212 }
213
214 $sql .= $this->db->order($sortfield, $sortorder);
215 if ($limit) {
216 if ($page < 0) {
217 $page = 0;
218 }
219 $offset = $limit * $page;
220
221 $sql .= $this->db->plimit($limit + 1, $offset);
222 }
223
224 dol_syslog("API Rest request");
225 $result = $this->db->query($sql);
226
227 if ($result) {
228 $num = $this->db->num_rows($result);
229 $min = min($num, ($limit <= 0 ? $num : $limit));
230 $i = 0;
231 while ($i < $min) {
232 $obj = $this->db->fetch_object($result);
233 $proposal_static = new Propal($this->db);
234 if ($proposal_static->fetch($obj->rowid)) {
235 // Add external contacts ids
236 $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
237 if (is_array($tmparray)) {
238 $proposal_static->contacts_ids = $tmparray;
239 }
240 $obj_ret[] = $this->_cleanObjectDatas($proposal_static);
241 }
242 $i++;
243 }
244 } else {
245 throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
246 }
247 if (!count($obj_ret)) {
248 throw new RestException(404, 'No proposal found');
249 }
250 return $obj_ret;
251 }
252
259 public function post($request_data = null)
260 {
261 if (!DolibarrApiAccess::$user->rights->propal->creer) {
262 throw new RestException(401, "Insuffisant rights");
263 }
264 // Check mandatory fields
265 $result = $this->_validate($request_data);
266
267 foreach ($request_data as $field => $value) {
268 $this->propal->$field = $value;
269 }
270 /*if (isset($request_data["lines"])) {
271 $lines = array();
272 foreach ($request_data["lines"] as $line) {
273 array_push($lines, (object) $line);
274 }
275 $this->propal->lines = $lines;
276 }*/
277 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
279 }
280
281 return ((int) $this->propal->id);
282 }
283
294 public function getLines($id, $sqlfilters = '')
295 {
296 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
297 throw new RestException(401);
298 }
299
300 $result = $this->propal->fetch($id);
301 if (!$result) {
302 throw new RestException(404, 'Commercial Proposal not found');
303 }
304
305 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
306 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
307 }
308
309 $sql = '';
310 if (!empty($sqlfilters)) {
311 $errormessage = '';
312 $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
313 if ($errormessage) {
314 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
315 }
316 }
317
318 $this->propal->getLinesArray($sql);
319 $result = array();
320 foreach ($this->propal->lines as $line) {
321 array_push($result, $this->_cleanObjectDatas($line));
322 }
323 return $result;
324 }
325
336 public function postLine($id, $request_data = null)
337 {
338 if (!DolibarrApiAccess::$user->rights->propal->creer) {
339 throw new RestException(401);
340 }
341
342 $result = $this->propal->fetch($id);
343 if (!$result) {
344 throw new RestException(404, 'Commercial Proposal not found');
345 }
346
347 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
348 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
349 }
350
351 $request_data = (object) $request_data;
352
353 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
354 $request_data->label = sanitizeVal($request_data->label);
355
356 $updateRes = $this->propal->addline(
357 $request_data->desc,
358 $request_data->subprice,
359 $request_data->qty,
360 $request_data->tva_tx,
361 $request_data->localtax1_tx,
362 $request_data->localtax2_tx,
363 $request_data->fk_product,
364 $request_data->remise_percent,
365 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
366 $request_data->subprice,
367 $request_data->info_bits,
368 $request_data->product_type,
369 $request_data->rang,
370 $request_data->special_code,
371 $request_data->fk_parent_line,
372 $request_data->fk_fournprice,
373 $request_data->pa_ht,
374 $request_data->label,
375 $request_data->date_start,
376 $request_data->date_end,
377 $request_data->array_options,
378 $request_data->fk_unit,
379 $request_data->origin,
380 $request_data->origin_id,
381 $request_data->multicurrency_subprice,
382 $request_data->fk_remise_except
383 );
384
385 if ($updateRes > 0) {
386 return $updateRes;
387 } else {
388 throw new RestException(400, $this->propal->error);
389 }
390 }
391
402 public function postLines($id, $request_data = null)
403 {
404 if (!DolibarrApiAccess::$user->rights->propal->creer) {
405 throw new RestException(401);
406 }
407
408 $result = $this->propal->fetch($id);
409 if (!$result) {
410 throw new RestException(404, 'Commercial Proposal not found');
411 }
412
413 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
414 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
415 }
416
417 $errors = [];
418 $this->db->begin();
419
420 foreach ($request_data as $TData) {
421 if (empty($TData[0])) $TData = array($TData);
422
423 foreach ($TData as $lineData) {
424 $line = (object) $lineData;
425
426 $updateRes = $this->propal->addline(
427 $line->desc,
428 $line->subprice,
429 $line->qty,
430 $line->tva_tx,
431 $line->localtax1_tx,
432 $line->localtax2_tx,
433 $line->fk_product,
434 $line->remise_percent,
435 'HT',
436 0,
437 $line->info_bits,
438 $line->product_type,
439 $line->rang,
440 $line->special_code,
441 $line->fk_parent_line,
442 $line->fk_fournprice,
443 $line->pa_ht,
444 $line->label,
445 $line->date_start,
446 $line->date_end,
447 $line->array_options,
448 $line->fk_unit,
449 $line->origin,
450 $line->origin_id,
451 $line->multicurrency_subprice,
452 $line->fk_remise_except
453 );
454
455 if ($updateRes < 0) {
456 $errors['lineLabel'] = $line->label;
457 $errors['msg'] = $this->propal->errors;
458 }
459 }
460 }
461 if (empty($errors)) {
462 $this->db->commit();
463 return $updateRes;
464 } else {
465 $this->db->rollback();
466 throw new RestException(400, implode(", ", $errors));
467 }
468 }
469
480 public function putLine($id, $lineid, $request_data = null)
481 {
482 if (!DolibarrApiAccess::$user->rights->propal->creer) {
483 throw new RestException(401);
484 }
485
486 $result = $this->propal->fetch($id);
487 if ($result <= 0) {
488 throw new RestException(404, 'Proposal not found');
489 }
490
491 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
492 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
493 }
494
495 $request_data = (object) $request_data;
496
497 if (isset($request_data->desc)) {
498 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
499 }
500 if (isset($request_data->label)) {
501 $request_data->label = sanitizeVal($request_data->label);
502 }
503
504 $propalline = new PropaleLigne($this->db);
505 $result = $propalline->fetch($lineid);
506 if ($result <= 0) {
507 throw new RestException(404, 'Proposal line not found');
508 }
509
510 $updateRes = $this->propal->updateline(
511 $lineid,
512 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
513 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
514 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
515 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
516 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
517 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
518 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
519 isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
520 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
521 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
522 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
523 0,
524 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
525 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
526 isset($request_data->label) ? $request_data->label : $propalline->label,
527 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
528 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
529 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
530 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
531 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
532 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
533 0,
534 isset($request_data->rang) ? $request_data->rang : $propalline->rang
535 );
536
537 if ($updateRes > 0) {
538 $result = $this->get($id);
539 unset($result->line);
540 return $this->_cleanObjectDatas($result);
541 }
542 return false;
543 }
544
558 public function deleteLine($id, $lineid)
559 {
560 if (!DolibarrApiAccess::$user->rights->propal->creer) {
561 throw new RestException(401);
562 }
563
564 $result = $this->propal->fetch($id);
565 if (!$result) {
566 throw new RestException(404, 'Proposal not found');
567 }
568
569 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
570 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
571 }
572
573 $updateRes = $this->propal->deleteline($lineid, $id);
574 if ($updateRes > 0) {
575 return $this->get($id);
576 } else {
577 throw new RestException(405, $this->propal->error);
578 }
579 }
580
594 public function postContact($id, $contactid, $type)
595 {
596 if (!DolibarrApiAccess::$user->rights->propal->creer) {
597 throw new RestException(401);
598 }
599
600 $result = $this->propal->fetch($id);
601
602 if (!$result) {
603 throw new RestException(404, 'Proposal not found');
604 }
605
606 if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
607 throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
608 }
609
610 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
611 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 }
613
614 $result = $this->propal->add_contact($contactid, $type, 'external');
615
616 if (!$result) {
617 throw new RestException(500, 'Error when added the contact');
618 }
619
620 return array(
621 'success' => array(
622 'code' => 200,
623 'message' => 'Contact linked to the proposal'
624 )
625 );
626 }
627
642 public function deleteContact($id, $contactid, $type)
643 {
644 if (!DolibarrApiAccess::$user->rights->propal->creer) {
645 throw new RestException(401);
646 }
647
648 $result = $this->propal->fetch($id);
649
650 if (!$result) {
651 throw new RestException(404, 'Proposal not found');
652 }
653
654 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
655 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
656 }
657
658 $contacts = $this->propal->liste_contact();
659
660 foreach ($contacts as $contact) {
661 if ($contact['id'] == $contactid && $contact['code'] == $type) {
662 $result = $this->propal->delete_contact($contact['rowid']);
663
664 if (!$result) {
665 throw new RestException(500, 'Error when deleted the contact');
666 }
667 }
668 }
669
670 return $this->_cleanObjectDatas($this->propal);
671 }
672
680 public function put($id, $request_data = null)
681 {
682 if (!DolibarrApiAccess::$user->rights->propal->creer) {
683 throw new RestException(401);
684 }
685
686 $result = $this->propal->fetch($id);
687 if (!$result) {
688 throw new RestException(404, 'Proposal not found');
689 }
690
691 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
692 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 }
694 foreach ($request_data as $field => $value) {
695 if ($field == 'id') {
696 continue;
697 }
698 $this->propal->$field = $value;
699 }
700
701 // update end of validity date
702 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
703 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
704 }
705 if (!empty($this->propal->fin_validite)) {
706 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
707 throw new RestException(500, $this->propal->error);
708 }
709 }
710
711 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
712 return $this->get($id);
713 } else {
714 throw new RestException(500, $this->propal->error);
715 }
716 }
717
724 public function delete($id)
725 {
726 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
727 throw new RestException(401);
728 }
729 $result = $this->propal->fetch($id);
730 if (!$result) {
731 throw new RestException(404, 'Commercial Proposal not found');
732 }
733
734 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
735 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
736 }
737
738 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
739 throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
740 }
741
742 return array(
743 'success' => array(
744 'code' => 200,
745 'message' => 'Commercial Proposal deleted'
746 )
747 );
748 }
749
758 public function settodraft($id)
759 {
760 if (!DolibarrApiAccess::$user->rights->propal->creer) {
761 throw new RestException(401);
762 }
763 $result = $this->propal->fetch($id);
764 if (!$result) {
765 throw new RestException(404, 'Proposal not found');
766 }
767
768 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
769 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
770 }
771
772 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
773 if ($result == 0) {
774 throw new RestException(304, 'Nothing done. May be object is already draft');
775 }
776 if ($result < 0) {
777 throw new RestException(500, 'Error : '.$this->propal->error);
778 }
779
780 $result = $this->propal->fetch($id);
781 if (!$result) {
782 throw new RestException(404, 'Proposal not found');
783 }
784
785 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
786 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
787 }
788
789 $this->propal->fetchObjectLinked();
790
791 return $this->_cleanObjectDatas($this->propal);
792 }
793
794
814 public function validate($id, $notrigger = 0)
815 {
816 if (!DolibarrApiAccess::$user->rights->propal->creer) {
817 throw new RestException(401);
818 }
819 $result = $this->propal->fetch($id);
820 if (!$result) {
821 throw new RestException(404, 'Commercial Proposal not found');
822 }
823
824 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
825 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
826 }
827
828 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
829 if ($result == 0) {
830 throw new RestException(304, 'Error nothing done. May be object is already validated');
831 }
832 if ($result < 0) {
833 throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
834 }
835
836 $result = $this->propal->fetch($id);
837 if (!$result) {
838 throw new RestException(404, 'Commercial Proposal not found');
839 }
840
841 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
842 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 }
844
845 $this->propal->fetchObjectLinked();
846
847 return $this->_cleanObjectDatas($this->propal);
848 }
849
861 public function close($id, $status, $note_private = '', $notrigger = 0)
862 {
863 if (!DolibarrApiAccess::$user->rights->propal->creer) {
864 throw new RestException(401);
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(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
873 }
874
875 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
876 if ($result == 0) {
877 throw new RestException(304, 'Error nothing done. May be object is already closed');
878 }
879 if ($result < 0) {
880 throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
881 }
882
883 $result = $this->propal->fetch($id);
884 if (!$result) {
885 throw new RestException(404, 'Proposal not found');
886 }
887
888 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
889 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
890 }
891
892 $this->propal->fetchObjectLinked();
893
894 return $this->_cleanObjectDatas($this->propal);
895 }
896
905 public function setinvoiced($id)
906 {
907 if (!DolibarrApiAccess::$user->rights->propal->creer) {
908 throw new RestException(401);
909 }
910 $result = $this->propal->fetch($id);
911 if (!$result) {
912 throw new RestException(404, 'Commercial Proposal not found');
913 }
914
915 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
916 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
917 }
918
919 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
920 if ($result < 0) {
921 throw new RestException(500, 'Error : '.$this->propal->error);
922 }
923
924 $result = $this->propal->fetch($id);
925 if (!$result) {
926 throw new RestException(404, 'Proposal not found');
927 }
928
929 if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
930 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
931 }
932
933 $this->propal->fetchObjectLinked();
934
935 return $this->_cleanObjectDatas($this->propal);
936 }
937
938
947 private function _validate($data)
948 {
949 $propal = array();
950 foreach (Proposals::$FIELDS as $field) {
951 if (!isset($data[$field])) {
952 throw new RestException(400, "$field field missing");
953 }
954 $propal[$field] = $data[$field];
955 }
956 return $propal;
957 }
958
959
960 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
967 protected function _cleanObjectDatas($object)
968 {
969 // phpcs:enable
970 $object = parent::_cleanObjectDatas($object);
971
972 unset($object->note);
973 unset($object->name);
974 unset($object->lastname);
975 unset($object->firstname);
976 unset($object->civility_id);
977 unset($object->address);
978
979 return $object;
980 }
981}
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage proposals.
Class to manage commercial proposal lines.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an proposal object.
getLines($id, $sqlfilters='')
Get lines of a commercial proposal.
settodraft($id)
Set a proposal to draft.
put($id, $request_data=null)
Update commercial proposal general fields (won't touch lines of commercial proposal)
close($id, $status, $note_private='', $notrigger=0)
Close (Accept or refuse) a quote / commercial proposal.
setinvoiced($id)
Set a commercial proposal billed.
post($request_data=null)
Create commercial proposal object.
postContact($id, $contactid, $type)
Add a contact type of given commercial proposal.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an proposal object by ref_ext.
postLine($id, $request_data=null)
Add a line to given commercial proposal.
_cleanObjectDatas($object)
Clean sensible object datas.
postLines($id, $request_data=null)
Add lines to given commercial proposal.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of given commercial proposal.
deleteContact($id, $contactid, $type)
Delete a contact type of given commercial proposal.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List commercial proposals.
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.