dolibarr  x.y.z
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;
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 $updateRes;
465  } else {
466  $this->db->rollback();
467  throw new RestException(400, implode(", ", $errors));
468  }
469  }
470 
481  public function putLine($id, $lineid, $request_data = null)
482  {
483  if (!DolibarrApiAccess::$user->rights->propal->creer) {
484  throw new RestException(401);
485  }
486 
487  $result = $this->propal->fetch($id);
488  if ($result <= 0) {
489  throw new RestException(404, 'Proposal not found');
490  }
491 
492  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
493  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
494  }
495 
496  $request_data = (object) $request_data;
497 
498  if (isset($request_data->desc)) {
499  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
500  }
501  if (isset($request_data->label)) {
502  $request_data->label = sanitizeVal($request_data->label);
503  }
504 
505  $propalline = new PropaleLigne($this->db);
506  $result = $propalline->fetch($lineid);
507  if ($result <= 0) {
508  throw new RestException(404, 'Proposal line not found');
509  }
510 
511  $updateRes = $this->propal->updateline(
512  $lineid,
513  isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
514  isset($request_data->qty) ? $request_data->qty : $propalline->qty,
515  isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
516  isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
517  isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
518  isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
519  isset($request_data->desc) ? $request_data->desc : $propalline->desc,
520  isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
521  isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
522  isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
523  isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
524  0,
525  isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
526  isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
527  isset($request_data->label) ? $request_data->label : $propalline->label,
528  isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
529  isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
530  isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
531  isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
532  isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
533  isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
534  0,
535  isset($request_data->rang) ? $request_data->rang : $propalline->rang
536  );
537 
538  if ($updateRes > 0) {
539  $result = $this->get($id);
540  unset($result->line);
541  return $this->_cleanObjectDatas($result);
542  }
543  return false;
544  }
545 
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  $updateRes = $this->propal->deleteline($lineid, $id);
575  if ($updateRes > 0) {
576  return $this->get($id);
577  } else {
578  throw new RestException(405, $this->propal->error);
579  }
580  }
581 
595  public function postContact($id, $contactid, $type)
596  {
597  if (!DolibarrApiAccess::$user->rights->propal->creer) {
598  throw new RestException(401);
599  }
600 
601  $result = $this->propal->fetch($id);
602 
603  if (!$result) {
604  throw new RestException(404, 'Proposal not found');
605  }
606 
607  if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
608  throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
609  }
610 
611  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
612  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
613  }
614 
615  $result = $this->propal->add_contact($contactid, $type, 'external');
616 
617  if (!$result) {
618  throw new RestException(500, 'Error when added the contact');
619  }
620 
621  return array(
622  'success' => array(
623  'code' => 200,
624  'message' => 'Contact linked to the proposal'
625  )
626  );
627  }
628 
643  public function deleteContact($id, $contactid, $type)
644  {
645  if (!DolibarrApiAccess::$user->rights->propal->creer) {
646  throw new RestException(401);
647  }
648 
649  $result = $this->propal->fetch($id);
650 
651  if (!$result) {
652  throw new RestException(404, 'Proposal not found');
653  }
654 
655  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
656  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
657  }
658 
659  $contacts = $this->propal->liste_contact();
660 
661  foreach ($contacts as $contact) {
662  if ($contact['id'] == $contactid && $contact['code'] == $type) {
663  $result = $this->propal->delete_contact($contact['rowid']);
664 
665  if (!$result) {
666  throw new RestException(500, 'Error when deleted the contact');
667  }
668  }
669  }
670 
671  return $this->_cleanObjectDatas($this->propal);
672  }
673 
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 
725  public function delete($id)
726  {
727  if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
728  throw new RestException(401);
729  }
730  $result = $this->propal->fetch($id);
731  if (!$result) {
732  throw new RestException(404, 'Commercial Proposal not found');
733  }
734 
735  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
736  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
737  }
738 
739  if (!$this->propal->delete(DolibarrApiAccess::$user)) {
740  throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
741  }
742 
743  return array(
744  'success' => array(
745  'code' => 200,
746  'message' => 'Commercial Proposal deleted'
747  )
748  );
749  }
750 
759  public function settodraft($id)
760  {
761  if (!DolibarrApiAccess::$user->rights->propal->creer) {
762  throw new RestException(401);
763  }
764  $result = $this->propal->fetch($id);
765  if (!$result) {
766  throw new RestException(404, 'Proposal not found');
767  }
768 
769  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
770  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
771  }
772 
773  $result = $this->propal->setDraft(DolibarrApiAccess::$user);
774  if ($result == 0) {
775  throw new RestException(304, 'Nothing done. May be object is already draft');
776  }
777  if ($result < 0) {
778  throw new RestException(500, 'Error : '.$this->propal->error);
779  }
780 
781  $result = $this->propal->fetch($id);
782  if (!$result) {
783  throw new RestException(404, 'Proposal not found');
784  }
785 
786  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
787  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
788  }
789 
790  $this->propal->fetchObjectLinked();
791 
792  return $this->_cleanObjectDatas($this->propal);
793  }
794 
795 
815  public function validate($id, $notrigger = 0)
816  {
817  if (!DolibarrApiAccess::$user->rights->propal->creer) {
818  throw new RestException(401);
819  }
820  $result = $this->propal->fetch($id);
821  if (!$result) {
822  throw new RestException(404, 'Commercial Proposal not found');
823  }
824 
825  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
826  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
827  }
828 
829  $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
830  if ($result == 0) {
831  throw new RestException(304, 'Error nothing done. May be object is already validated');
832  }
833  if ($result < 0) {
834  throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
835  }
836 
837  $result = $this->propal->fetch($id);
838  if (!$result) {
839  throw new RestException(404, 'Commercial Proposal not found');
840  }
841 
842  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
843  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
844  }
845 
846  $this->propal->fetchObjectLinked();
847 
848  return $this->_cleanObjectDatas($this->propal);
849  }
850 
862  public function close($id, $status, $note_private = '', $notrigger = 0)
863  {
864  if (!DolibarrApiAccess::$user->rights->propal->creer) {
865  throw new RestException(401);
866  }
867  $result = $this->propal->fetch($id);
868  if (!$result) {
869  throw new RestException(404, 'Commercial Proposal not found');
870  }
871 
872  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
873  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
874  }
875 
876  $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
877  if ($result == 0) {
878  throw new RestException(304, 'Error nothing done. May be object is already closed');
879  }
880  if ($result < 0) {
881  throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
882  }
883 
884  $result = $this->propal->fetch($id);
885  if (!$result) {
886  throw new RestException(404, 'Proposal not found');
887  }
888 
889  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
890  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
891  }
892 
893  $this->propal->fetchObjectLinked();
894 
895  return $this->_cleanObjectDatas($this->propal);
896  }
897 
906  public function setinvoiced($id)
907  {
908  if (!DolibarrApiAccess::$user->rights->propal->creer) {
909  throw new RestException(401);
910  }
911  $result = $this->propal->fetch($id);
912  if (!$result) {
913  throw new RestException(404, 'Commercial Proposal not found');
914  }
915 
916  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
917  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
918  }
919 
920  $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
921  if ($result < 0) {
922  throw new RestException(500, 'Error : '.$this->propal->error);
923  }
924 
925  $result = $this->propal->fetch($id);
926  if (!$result) {
927  throw new RestException(404, 'Proposal not found');
928  }
929 
930  if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
931  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
932  }
933 
934  $this->propal->fetchObjectLinked();
935 
936  return $this->_cleanObjectDatas($this->propal);
937  }
938 
939 
948  private function _validate($data)
949  {
950  $propal = array();
951  foreach (Proposals::$FIELDS as $field) {
952  if (!isset($data[$field])) {
953  throw new RestException(400, "$field field missing");
954  }
955  $propal[$field] = $data[$field];
956  }
957  return $propal;
958  }
959 
960 
961  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
968  protected function _cleanObjectDatas($object)
969  {
970  // phpcs:enable
971  $object = parent::_cleanObjectDatas($object);
972 
973  unset($object->note);
974  unset($object->name);
975  unset($object->lastname);
976  unset($object->firstname);
977  unset($object->civility_id);
978  unset($object->address);
979 
980  return $object;
981  }
982 }
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
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
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.
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