dolibarr  16.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  *
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 
22 use Luracast\Restler\RestException;
23 
24 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
25 
26 
33 class Proposals extends DolibarrApi
34 {
38  static $FIELDS = array(
39  'socid'
40  );
41 
45  public $propal;
46 
50  public function __construct()
51  {
52  global $db, $conf;
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->rights->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->rights->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";
185 
186  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
187  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
188  }
189 
190  $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
191  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
192  $sql .= " AND t.fk_soc = sc.fk_soc";
193  }
194  if ($socids) {
195  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
196  }
197  if ($search_sale > 0) {
198  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
199  }
200  // Insert sale filter
201  if ($search_sale > 0) {
202  $sql .= " AND sc.fk_user = ".((int) $search_sale);
203  }
204  // Add sql filters
205  if ($sqlfilters) {
206  $errormessage = '';
207  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
208  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
209  }
210  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
211  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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 $this->propal->id;
282  }
283 
294  public function getLines($id, $sqlfilters = '')
295  {
296  $filters = "";
297 
298  if (!DolibarrApiAccess::$user->rights->propal->lire) {
299  throw new RestException(401);
300  }
301 
302  $result = $this->propal->fetch($id);
303  if (!$result) {
304  throw new RestException(404, 'Commercial Proposal not found');
305  }
306 
307  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
308  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
309  }
310 
311  if (!empty($sqlfilters)) {
312  if (!DolibarrApi::_checkFilters($sqlfilters)) {
313  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
314  }
315  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
316  $filters = " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
317  }
318 
319  $this->propal->getLinesArray($filters);
320  $result = array();
321  foreach ($this->propal->lines as $line) {
322  array_push($result, $this->_cleanObjectDatas($line));
323  }
324  return $result;
325  }
326 
337  public function postLine($id, $request_data = null)
338  {
339  if (!DolibarrApiAccess::$user->rights->propal->creer) {
340  throw new RestException(401);
341  }
342 
343  $result = $this->propal->fetch($id);
344  if (!$result) {
345  throw new RestException(404, 'Commercial Proposal not found');
346  }
347 
348  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
349  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
350  }
351 
352  $request_data = (object) $request_data;
353 
354  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
355  $request_data->label = sanitizeVal($request_data->label);
356 
357  $updateRes = $this->propal->addline(
358  $request_data->desc,
359  $request_data->subprice,
360  $request_data->qty,
361  $request_data->tva_tx,
362  $request_data->localtax1_tx,
363  $request_data->localtax2_tx,
364  $request_data->fk_product,
365  $request_data->remise_percent,
366  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
367  $request_data->subprice,
368  $request_data->info_bits,
369  $request_data->product_type,
370  $request_data->rang,
371  $request_data->special_code,
372  $request_data->fk_parent_line,
373  $request_data->fk_fournprice,
374  $request_data->pa_ht,
375  $request_data->label,
376  $request_data->date_start,
377  $request_data->date_end,
378  $request_data->array_options,
379  $request_data->fk_unit,
380  $request_data->origin,
381  $request_data->origin_id,
382  $request_data->multicurrency_subprice,
383  $request_data->fk_remise_except
384  );
385 
386  if ($updateRes > 0) {
387  return $updateRes;
388  } else {
389  throw new RestException(400, $this->propal->error);
390  }
391  }
392 
403  public function postLines($id, $request_data = null)
404  {
405  if (!DolibarrApiAccess::$user->rights->propal->creer) {
406  throw new RestException(401);
407  }
408 
409  $result = $this->propal->fetch($id);
410  if (!$result) {
411  throw new RestException(404, 'Commercial Proposal not found');
412  }
413 
414  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
415  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416  }
417 
418  $errors = [];
419  $this->db->begin();
420 
421  foreach ($request_data as $TData) {
422  if (empty($TData[0])) $TData = array($TData);
423 
424  foreach ($TData as $lineData) {
425  $line = (object) $lineData;
426 
427  $updateRes = $this->propal->addline(
428  $line->desc,
429  $line->subprice,
430  $line->qty,
431  $line->tva_tx,
432  $line->localtax1_tx,
433  $line->localtax2_tx,
434  $line->fk_product,
435  $line->remise_percent,
436  'HT',
437  0,
438  $line->info_bits,
439  $line->product_type,
440  $line->rang,
441  $line->special_code,
442  $line->fk_parent_line,
443  $line->fk_fournprice,
444  $line->pa_ht,
445  $line->label,
446  $line->date_start,
447  $line->date_end,
448  $line->array_options,
449  $line->fk_unit,
450  $line->origin,
451  $line->origin_id,
452  $line->multicurrency_subprice,
453  $line->fk_remise_except
454  );
455 
456  if ($updateRes < 0) {
457  $errors['lineLabel'] = $line->label;
458  $errors['msg'] = $this->propal->errors;
459  }
460  }
461  }
462  if (empty($errors)) {
463  $this->db->commit();
464  return count($request_data);
465  } else {
466  $this->db->rollback();
467  throw new RestException(400, implode(", ", $errors));
468  }
469  }
470 
482  public function putLine($id, $lineid, $request_data = null)
483  {
484  if (!DolibarrApiAccess::$user->rights->propal->creer) {
485  throw new RestException(401);
486  }
487 
488  $result = $this->propal->fetch($id);
489  if ($result <= 0) {
490  throw new RestException(404, 'Proposal not found');
491  }
492 
493  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
494  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495  }
496 
497  $request_data = (object) $request_data;
498 
499  if (isset($request_data->desc)) {
500  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
501  }
502  if (isset($request_data->label)) {
503  $request_data->label = sanitizeVal($request_data->label);
504  }
505 
506  $propalline = new PropaleLigne($this->db);
507  $result = $propalline->fetch($lineid);
508  if ($result <= 0) {
509  throw new RestException(404, 'Proposal line not found');
510  }
511 
512  $updateRes = $this->propal->updateline(
513  $lineid,
514  isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
515  isset($request_data->qty) ? $request_data->qty : $propalline->qty,
516  isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
517  isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
518  isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
519  isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
520  isset($request_data->desc) ? $request_data->desc : $propalline->desc,
521  isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
522  isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
523  isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
524  isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
525  0,
526  isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
527  isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
528  isset($request_data->label) ? $request_data->label : $propalline->label,
529  isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
530  isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
531  isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
532  isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
533  isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
534  isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
535  0,
536  isset($request_data->rang) ? $request_data->rang : $propalline->rang
537  );
538 
539  if ($updateRes > 0) {
540  $result = $this->get($id);
541  unset($result->line);
542  return $this->_cleanObjectDatas($result);
543  }
544  return false;
545  }
546 
561  public function deleteLine($id, $lineid)
562  {
563  if (!DolibarrApiAccess::$user->rights->propal->creer) {
564  throw new RestException(401);
565  }
566 
567  $result = $this->propal->fetch($id);
568  if (!$result) {
569  throw new RestException(404, 'Proposal not found');
570  }
571 
572  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
573  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
574  }
575 
576  // TODO Check the lineid $lineid is a line of ojbect
577 
578  $updateRes = $this->propal->deleteline($lineid);
579  if ($updateRes > 0) {
580  return $this->get($id);
581  } else {
582  throw new RestException(405, $this->propal->error);
583  }
584  }
585 
600  public function postContact($id, $contactid, $type)
601  {
602  if (!DolibarrApiAccess::$user->rights->propal->creer) {
603  throw new RestException(401);
604  }
605 
606  $result = $this->propal->fetch($id);
607 
608  if (!$result) {
609  throw new RestException(404, 'Proposal not found');
610  }
611 
612  if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
613  throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
614  }
615 
616  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
617  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
618  }
619 
620  $result = $this->propal->add_contact($contactid, $type, 'external');
621 
622  if (!$result) {
623  throw new RestException(500, 'Error when added the contact');
624  }
625 
626  return $this->propal;
627  }
628 
644  public function deleteContact($id, $contactid, $type)
645  {
646  if (!DolibarrApiAccess::$user->rights->propal->creer) {
647  throw new RestException(401);
648  }
649 
650  $result = $this->propal->fetch($id);
651 
652  if (!$result) {
653  throw new RestException(404, 'Proposal not found');
654  }
655 
656  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
657  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
658  }
659 
660  $contacts = $this->propal->liste_contact();
661 
662  foreach ($contacts as $contact) {
663  if ($contact['id'] == $contactid && $contact['code'] == $type) {
664  $result = $this->propal->delete_contact($contact['rowid']);
665 
666  if (!$result) {
667  throw new RestException(500, 'Error when deleted the contact');
668  }
669  }
670  }
671 
672  return $this->_cleanObjectDatas($this->propal);
673  }
674 
683  public function put($id, $request_data = null)
684  {
685  if (!DolibarrApiAccess::$user->rights->propal->creer) {
686  throw new RestException(401);
687  }
688 
689  $result = $this->propal->fetch($id);
690  if (!$result) {
691  throw new RestException(404, 'Proposal not found');
692  }
693 
694  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
695  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
696  }
697  foreach ($request_data as $field => $value) {
698  if ($field == 'id') {
699  continue;
700  }
701  $this->propal->$field = $value;
702  }
703 
704  // update end of validity date
705  if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
706  $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
707  }
708  if (!empty($this->propal->fin_validite)) {
709  if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
710  throw new RestException(500, $this->propal->error);
711  }
712  }
713 
714  if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
715  return $this->get($id);
716  } else {
717  throw new RestException(500, $this->propal->error);
718  }
719  }
720 
728  public function delete($id)
729  {
730  if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
731  throw new RestException(401);
732  }
733  $result = $this->propal->fetch($id);
734  if (!$result) {
735  throw new RestException(404, 'Commercial Proposal not found');
736  }
737 
738  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
739  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
740  }
741 
742  if (!$this->propal->delete(DolibarrApiAccess::$user)) {
743  throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
744  }
745 
746  return array(
747  'success' => array(
748  'code' => 200,
749  'message' => 'Commercial Proposal deleted'
750  )
751  );
752  }
753 
763  public function settodraft($id)
764  {
765  if (!DolibarrApiAccess::$user->rights->propal->creer) {
766  throw new RestException(401);
767  }
768  $result = $this->propal->fetch($id);
769  if (!$result) {
770  throw new RestException(404, 'Proposal not found');
771  }
772 
773  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
774  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
775  }
776 
777  $result = $this->propal->setDraft(DolibarrApiAccess::$user);
778  if ($result == 0) {
779  throw new RestException(304, 'Nothing done. May be object is already draft');
780  }
781  if ($result < 0) {
782  throw new RestException(500, 'Error : '.$this->propal->error);
783  }
784 
785  $result = $this->propal->fetch($id);
786  if (!$result) {
787  throw new RestException(404, 'Proposal not found');
788  }
789 
790  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
791  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
792  }
793 
794  $this->propal->fetchObjectLinked();
795 
796  return $this->_cleanObjectDatas($this->propal);
797  }
798 
799 
820  public function validate($id, $notrigger = 0)
821  {
822  if (!DolibarrApiAccess::$user->rights->propal->creer) {
823  throw new RestException(401);
824  }
825  $result = $this->propal->fetch($id);
826  if (!$result) {
827  throw new RestException(404, 'Commercial Proposal not found');
828  }
829 
830  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
831  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
832  }
833 
834  $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
835  if ($result == 0) {
836  throw new RestException(304, 'Error nothing done. May be object is already validated');
837  }
838  if ($result < 0) {
839  throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
840  }
841 
842  $result = $this->propal->fetch($id);
843  if (!$result) {
844  throw new RestException(404, 'Commercial Proposal not found');
845  }
846 
847  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
848  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
849  }
850 
851  $this->propal->fetchObjectLinked();
852 
853  return $this->_cleanObjectDatas($this->propal);
854  }
855 
868  public function close($id, $status, $note_private = '', $notrigger = 0)
869  {
870  if (!DolibarrApiAccess::$user->rights->propal->creer) {
871  throw new RestException(401);
872  }
873  $result = $this->propal->fetch($id);
874  if (!$result) {
875  throw new RestException(404, 'Commercial Proposal not found');
876  }
877 
878  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
879  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
880  }
881 
882  $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
883  if ($result == 0) {
884  throw new RestException(304, 'Error nothing done. May be object is already closed');
885  }
886  if ($result < 0) {
887  throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
888  }
889 
890  $result = $this->propal->fetch($id);
891  if (!$result) {
892  throw new RestException(404, 'Proposal not found');
893  }
894 
895  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
896  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
897  }
898 
899  $this->propal->fetchObjectLinked();
900 
901  return $this->_cleanObjectDatas($this->propal);
902  }
903 
913  public function setinvoiced($id)
914  {
915  if (!DolibarrApiAccess::$user->rights->propal->creer) {
916  throw new RestException(401);
917  }
918  $result = $this->propal->fetch($id);
919  if (!$result) {
920  throw new RestException(404, 'Commercial Proposal not found');
921  }
922 
923  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
924  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
925  }
926 
927  $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
928  if ($result < 0) {
929  throw new RestException(500, 'Error : '.$this->propal->error);
930  }
931 
932  $result = $this->propal->fetch($id);
933  if (!$result) {
934  throw new RestException(404, 'Proposal not found');
935  }
936 
937  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
938  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
939  }
940 
941  $this->propal->fetchObjectLinked();
942 
943  return $this->_cleanObjectDatas($this->propal);
944  }
945 
946 
954  private function _validate($data)
955  {
956  $propal = array();
957  foreach (Proposals::$FIELDS as $field) {
958  if (!isset($data[$field])) {
959  throw new RestException(400, "$field field missing");
960  }
961  $propal[$field] = $data[$field];
962  }
963  return $propal;
964  }
965 
966 
967  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
974  protected function _cleanObjectDatas($object)
975  {
976  // phpcs:enable
977  $object = parent::_cleanObjectDatas($object);
978 
979  unset($object->note);
980  unset($object->name);
981  unset($object->lastname);
982  unset($object->firstname);
983  unset($object->civility_id);
984  unset($object->address);
985 
986  return $object;
987  }
988 }
Proposals\deleteContact
deleteContact($id, $contactid, $type)
Delete a contact type of given commercial proposal.
Definition: api_proposals.class.php:644
db
$conf db
API class for accounts.
Definition: inc.php:41
Proposals\getByRef
getByRef($ref, $contact_list=1)
Get properties of an proposal object by ref.
Definition: api_proposals.class.php:86
Proposals\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List commercial proposals.
Definition: api_proposals.class.php:161
Proposals\put
put($id, $request_data=null)
Update commercial proposal general fields (won't touch lines of commercial proposal)
Definition: api_proposals.class.php:683
PropaleLigne
Class to manage commercial proposal lines.
Definition: propal.class.php:3878
Proposals\post
post($request_data=null)
Create commercial proposal object.
Definition: api_proposals.class.php:259
sanitizeVal
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Definition: functions.lib.php:825
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Proposals\__construct
__construct()
Constructor.
Definition: api_proposals.class.php:50
Proposals\postLine
postLine($id, $request_data=null)
Add a line to given commercial proposal.
Definition: api_proposals.class.php:337
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Proposals\close
close($id, $status, $note_private='', $notrigger=0)
Close (Accept or refuse) a quote / commercial proposal.
Definition: api_proposals.class.php:868
Proposals\getByRefExt
getByRefExt($ref_ext, $contact_list=1)
Get properties of an proposal object by ref_ext.
Definition: api_proposals.class.php:104
Proposals\postLines
postLines($id, $request_data=null)
Add lines to given commercial proposal.
Definition: api_proposals.class.php:403
Proposals\_fetch
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an proposal object.
Definition: api_proposals.class.php:122
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Proposals\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_proposals.class.php:974
Proposals\setinvoiced
setinvoiced($id)
Set a commercial proposal billed.
Definition: api_proposals.class.php:913
Proposals\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_proposals.class.php:954
Proposals\postContact
postContact($id, $contactid, $type)
Add a contact type of given commercial proposal.
Definition: api_proposals.class.php:600
Proposals\validate
validate($id, $notrigger=0)
Validate a commercial proposal.
Definition: api_proposals.class.php:820
Proposals
Definition: api_proposals.class.php:33
Proposals\putLine
putLine($id, $lineid, $request_data=null)
Update a line of given commercial proposal.
Definition: api_proposals.class.php:482
Proposals\settodraft
settodraft($id)
Set a proposal to draft.
Definition: api_proposals.class.php:763
Proposals\deleteLine
deleteLine($id, $lineid)
Delete a line of given commercial proposal.
Definition: api_proposals.class.php:561
Propal
Class to manage proposals.
Definition: propal.class.php:52
Proposals\getLines
getLines($id, $sqlfilters='')
Get lines of a commercial proposal.
Definition: api_proposals.class.php:294