dolibarr  17.0.4
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  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
208  if ($errormessage) {
209  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
210  }
211  }
212 
213  $sql .= $this->db->order($sortfield, $sortorder);
214  if ($limit) {
215  if ($page < 0) {
216  $page = 0;
217  }
218  $offset = $limit * $page;
219 
220  $sql .= $this->db->plimit($limit + 1, $offset);
221  }
222 
223  dol_syslog("API Rest request");
224  $result = $this->db->query($sql);
225 
226  if ($result) {
227  $num = $this->db->num_rows($result);
228  $min = min($num, ($limit <= 0 ? $num : $limit));
229  $i = 0;
230  while ($i < $min) {
231  $obj = $this->db->fetch_object($result);
232  $proposal_static = new Propal($this->db);
233  if ($proposal_static->fetch($obj->rowid)) {
234  // Add external contacts ids
235  $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
236  if (is_array($tmparray)) {
237  $proposal_static->contacts_ids = $tmparray;
238  }
239  $obj_ret[] = $this->_cleanObjectDatas($proposal_static);
240  }
241  $i++;
242  }
243  } else {
244  throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
245  }
246  if (!count($obj_ret)) {
247  throw new RestException(404, 'No proposal found');
248  }
249  return $obj_ret;
250  }
251 
258  public function post($request_data = null)
259  {
260  if (!DolibarrApiAccess::$user->rights->propal->creer) {
261  throw new RestException(401, "Insuffisant rights");
262  }
263  // Check mandatory fields
264  $result = $this->_validate($request_data);
265 
266  foreach ($request_data as $field => $value) {
267  $this->propal->$field = $value;
268  }
269  /*if (isset($request_data["lines"])) {
270  $lines = array();
271  foreach ($request_data["lines"] as $line) {
272  array_push($lines, (object) $line);
273  }
274  $this->propal->lines = $lines;
275  }*/
276  if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
277  throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
278  }
279 
280  return $this->propal->id;
281  }
282 
293  public function getLines($id, $sqlfilters = '')
294  {
295  if (!DolibarrApiAccess::$user->rights->propal->lire) {
296  throw new RestException(401);
297  }
298 
299  $result = $this->propal->fetch($id);
300  if (!$result) {
301  throw new RestException(404, 'Commercial Proposal not found');
302  }
303 
304  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
305  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
306  }
307 
308  $sql = '';
309  if (!empty($sqlfilters)) {
310  $errormessage = '';
311  $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
312  if ($errormessage) {
313  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
314  }
315  }
316 
317  $this->propal->getLinesArray($sql);
318  $result = array();
319  foreach ($this->propal->lines as $line) {
320  array_push($result, $this->_cleanObjectDatas($line));
321  }
322  return $result;
323  }
324 
335  public function postLine($id, $request_data = null)
336  {
337  if (!DolibarrApiAccess::$user->rights->propal->creer) {
338  throw new RestException(401);
339  }
340 
341  $result = $this->propal->fetch($id);
342  if (!$result) {
343  throw new RestException(404, 'Commercial Proposal not found');
344  }
345 
346  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
347  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
348  }
349 
350  $request_data = (object) $request_data;
351 
352  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
353  $request_data->label = sanitizeVal($request_data->label);
354 
355  $updateRes = $this->propal->addline(
356  $request_data->desc,
357  $request_data->subprice,
358  $request_data->qty,
359  $request_data->tva_tx,
360  $request_data->localtax1_tx,
361  $request_data->localtax2_tx,
362  $request_data->fk_product,
363  $request_data->remise_percent,
364  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
365  $request_data->subprice,
366  $request_data->info_bits,
367  $request_data->product_type,
368  $request_data->rang,
369  $request_data->special_code,
370  $request_data->fk_parent_line,
371  $request_data->fk_fournprice,
372  $request_data->pa_ht,
373  $request_data->label,
374  $request_data->date_start,
375  $request_data->date_end,
376  $request_data->array_options,
377  $request_data->fk_unit,
378  $request_data->origin,
379  $request_data->origin_id,
380  $request_data->multicurrency_subprice,
381  $request_data->fk_remise_except
382  );
383 
384  if ($updateRes > 0) {
385  return $updateRes;
386  } else {
387  throw new RestException(400, $this->propal->error);
388  }
389  }
390 
401  public function postLines($id, $request_data = null)
402  {
403  if (!DolibarrApiAccess::$user->rights->propal->creer) {
404  throw new RestException(401);
405  }
406 
407  $result = $this->propal->fetch($id);
408  if (!$result) {
409  throw new RestException(404, 'Commercial Proposal not found');
410  }
411 
412  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
413  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
414  }
415 
416  $errors = [];
417  $this->db->begin();
418 
419  foreach ($request_data as $TData) {
420  if (empty($TData[0])) $TData = array($TData);
421 
422  foreach ($TData as $lineData) {
423  $line = (object) $lineData;
424 
425  $updateRes = $this->propal->addline(
426  $line->desc,
427  $line->subprice,
428  $line->qty,
429  $line->tva_tx,
430  $line->localtax1_tx,
431  $line->localtax2_tx,
432  $line->fk_product,
433  $line->remise_percent,
434  'HT',
435  0,
436  $line->info_bits,
437  $line->product_type,
438  $line->rang,
439  $line->special_code,
440  $line->fk_parent_line,
441  $line->fk_fournprice,
442  $line->pa_ht,
443  $line->label,
444  $line->date_start,
445  $line->date_end,
446  $line->array_options,
447  $line->fk_unit,
448  $line->origin,
449  $line->origin_id,
450  $line->multicurrency_subprice,
451  $line->fk_remise_except
452  );
453 
454  if ($updateRes < 0) {
455  $errors['lineLabel'] = $line->label;
456  $errors['msg'] = $this->propal->errors;
457  }
458  }
459  }
460  if (empty($errors)) {
461  $this->db->commit();
462  return count($request_data);
463  } else {
464  $this->db->rollback();
465  throw new RestException(400, implode(", ", $errors));
466  }
467  }
468 
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 
559  public function deleteLine($id, $lineid)
560  {
561  if (!DolibarrApiAccess::$user->rights->propal->creer) {
562  throw new RestException(401);
563  }
564 
565  $result = $this->propal->fetch($id);
566  if (!$result) {
567  throw new RestException(404, 'Proposal not found');
568  }
569 
570  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
571  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
572  }
573 
574  // TODO Check the lineid $lineid is a line of ojbect
575 
576  $updateRes = $this->propal->deleteline($lineid);
577  if ($updateRes > 0) {
578  return $this->get($id);
579  } else {
580  throw new RestException(405, $this->propal->error);
581  }
582  }
583 
598  public function postContact($id, $contactid, $type)
599  {
600  if (!DolibarrApiAccess::$user->rights->propal->creer) {
601  throw new RestException(401);
602  }
603 
604  $result = $this->propal->fetch($id);
605 
606  if (!$result) {
607  throw new RestException(404, 'Proposal not found');
608  }
609 
610  if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
611  throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
612  }
613 
614  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
615  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
616  }
617 
618  $result = $this->propal->add_contact($contactid, $type, 'external');
619 
620  if (!$result) {
621  throw new RestException(500, 'Error when added the contact');
622  }
623 
624  return $this->propal;
625  }
626 
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 
681  public function put($id, $request_data = null)
682  {
683  if (!DolibarrApiAccess::$user->rights->propal->creer) {
684  throw new RestException(401);
685  }
686 
687  $result = $this->propal->fetch($id);
688  if (!$result) {
689  throw new RestException(404, 'Proposal not found');
690  }
691 
692  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
693  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
694  }
695  foreach ($request_data as $field => $value) {
696  if ($field == 'id') {
697  continue;
698  }
699  $this->propal->$field = $value;
700  }
701 
702  // update end of validity date
703  if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
704  $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
705  }
706  if (!empty($this->propal->fin_validite)) {
707  if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
708  throw new RestException(500, $this->propal->error);
709  }
710  }
711 
712  if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
713  return $this->get($id);
714  } else {
715  throw new RestException(500, $this->propal->error);
716  }
717  }
718 
726  public function delete($id)
727  {
728  if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
729  throw new RestException(401);
730  }
731  $result = $this->propal->fetch($id);
732  if (!$result) {
733  throw new RestException(404, 'Commercial Proposal not found');
734  }
735 
736  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
737  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
738  }
739 
740  if (!$this->propal->delete(DolibarrApiAccess::$user)) {
741  throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
742  }
743 
744  return array(
745  'success' => array(
746  'code' => 200,
747  'message' => 'Commercial Proposal deleted'
748  )
749  );
750  }
751 
761  public function settodraft($id)
762  {
763  if (!DolibarrApiAccess::$user->rights->propal->creer) {
764  throw new RestException(401);
765  }
766  $result = $this->propal->fetch($id);
767  if (!$result) {
768  throw new RestException(404, 'Proposal not found');
769  }
770 
771  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
772  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
773  }
774 
775  $result = $this->propal->setDraft(DolibarrApiAccess::$user);
776  if ($result == 0) {
777  throw new RestException(304, 'Nothing done. May be object is already draft');
778  }
779  if ($result < 0) {
780  throw new RestException(500, 'Error : '.$this->propal->error);
781  }
782 
783  $result = $this->propal->fetch($id);
784  if (!$result) {
785  throw new RestException(404, 'Proposal not found');
786  }
787 
788  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
789  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
790  }
791 
792  $this->propal->fetchObjectLinked();
793 
794  return $this->_cleanObjectDatas($this->propal);
795  }
796 
797 
818  public function validate($id, $notrigger = 0)
819  {
820  if (!DolibarrApiAccess::$user->rights->propal->creer) {
821  throw new RestException(401);
822  }
823  $result = $this->propal->fetch($id);
824  if (!$result) {
825  throw new RestException(404, 'Commercial Proposal not found');
826  }
827 
828  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
829  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
830  }
831 
832  $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
833  if ($result == 0) {
834  throw new RestException(304, 'Error nothing done. May be object is already validated');
835  }
836  if ($result < 0) {
837  throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
838  }
839 
840  $result = $this->propal->fetch($id);
841  if (!$result) {
842  throw new RestException(404, 'Commercial Proposal not found');
843  }
844 
845  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
846  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
847  }
848 
849  $this->propal->fetchObjectLinked();
850 
851  return $this->_cleanObjectDatas($this->propal);
852  }
853 
866  public function close($id, $status, $note_private = '', $notrigger = 0)
867  {
868  if (!DolibarrApiAccess::$user->rights->propal->creer) {
869  throw new RestException(401);
870  }
871  $result = $this->propal->fetch($id);
872  if (!$result) {
873  throw new RestException(404, 'Commercial Proposal not found');
874  }
875 
876  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
877  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
878  }
879 
880  $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
881  if ($result == 0) {
882  throw new RestException(304, 'Error nothing done. May be object is already closed');
883  }
884  if ($result < 0) {
885  throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
886  }
887 
888  $result = $this->propal->fetch($id);
889  if (!$result) {
890  throw new RestException(404, 'Proposal not found');
891  }
892 
893  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
894  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
895  }
896 
897  $this->propal->fetchObjectLinked();
898 
899  return $this->_cleanObjectDatas($this->propal);
900  }
901 
911  public function setinvoiced($id)
912  {
913  if (!DolibarrApiAccess::$user->rights->propal->creer) {
914  throw new RestException(401);
915  }
916  $result = $this->propal->fetch($id);
917  if (!$result) {
918  throw new RestException(404, 'Commercial Proposal not found');
919  }
920 
921  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
922  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
923  }
924 
925  $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
926  if ($result < 0) {
927  throw new RestException(500, 'Error : '.$this->propal->error);
928  }
929 
930  $result = $this->propal->fetch($id);
931  if (!$result) {
932  throw new RestException(404, 'Proposal not found');
933  }
934 
935  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
936  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
937  }
938 
939  $this->propal->fetchObjectLinked();
940 
941  return $this->_cleanObjectDatas($this->propal);
942  }
943 
944 
952  private function _validate($data)
953  {
954  $propal = array();
955  foreach (Proposals::$FIELDS as $field) {
956  if (!isset($data[$field])) {
957  throw new RestException(400, "$field field missing");
958  }
959  $propal[$field] = $data[$field];
960  }
961  return $propal;
962  }
963 
964 
965  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
972  protected function _cleanObjectDatas($object)
973  {
974  // phpcs:enable
975  $object = parent::_cleanObjectDatas($object);
976 
977  unset($object->note);
978  unset($object->name);
979  unset($object->lastname);
980  unset($object->firstname);
981  unset($object->civility_id);
982  unset($object->address);
983 
984  return $object;
985  }
986 }
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.
Definition: api.class.php:283
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, &$error='')
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.
$conf db
API class for accounts.
Definition: inc.php:41