22 use Luracast\Restler\RestException;
24 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
38 static $FIELDS = array(
54 $this->propal =
new Propal($this->
db);
68 public function get($id, $contact_list = 1)
70 return $this->
_fetch($id,
'',
'', $contact_list);
86 public function getByRef($ref, $contact_list = 1)
88 return $this->
_fetch(
'', $ref,
'', $contact_list);
106 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
122 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
124 if (!DolibarrApiAccess::$user->rights->propal->lire) {
125 throw new RestException(401);
128 $result = $this->propal->fetch($id, $ref, $ref_ext);
130 throw new RestException(404,
'Commercial Proposal not found');
134 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
138 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
139 if (is_array($tmparray)) {
140 $this->propal->contacts_ids = $tmparray;
143 $this->propal->fetchObjectLinked();
161 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
165 if (!DolibarrApiAccess::$user->rights->propal->lire) {
166 throw new RestException(401);
172 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
176 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
177 $search_sale = DolibarrApiAccess::$user->id;
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";
184 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as t";
186 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
187 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
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";
195 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
197 if ($search_sale > 0) {
198 $sql .=
" AND t.rowid = sc.fk_soc";
201 if ($search_sale > 0) {
202 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
208 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
210 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
211 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
214 $sql .= $this->
db->order($sortfield, $sortorder);
219 $offset = $limit * $page;
221 $sql .= $this->
db->plimit($limit + 1, $offset);
225 $result = $this->
db->query($sql);
228 $num = $this->
db->num_rows($result);
229 $min = min($num, ($limit <= 0 ? $num : $limit));
232 $obj = $this->
db->fetch_object($result);
233 $proposal_static =
new Propal($this->
db);
234 if ($proposal_static->fetch($obj->rowid)) {
236 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
237 if (is_array($tmparray)) {
238 $proposal_static->contacts_ids = $tmparray;
245 throw new RestException(503,
'Error when retrieve propal list : '.$this->
db->lasterror());
247 if (!count($obj_ret)) {
248 throw new RestException(404,
'No proposal found');
259 public function post($request_data =
null)
261 if (!DolibarrApiAccess::$user->rights->propal->creer) {
262 throw new RestException(401,
"Insuffisant rights");
265 $result = $this->
_validate($request_data);
267 foreach ($request_data as $field => $value) {
268 $this->propal->$field = $value;
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));
281 return $this->propal->id;
298 if (!DolibarrApiAccess::$user->rights->propal->lire) {
299 throw new RestException(401);
302 $result = $this->propal->fetch($id);
304 throw new RestException(404,
'Commercial Proposal not found');
308 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
311 if (!empty($sqlfilters)) {
313 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
315 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
316 $filters =
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
319 $this->propal->getLinesArray($filters);
321 foreach ($this->propal->lines as $line) {
337 public function postLine($id, $request_data =
null)
339 if (!DolibarrApiAccess::$user->rights->propal->creer) {
340 throw new RestException(401);
343 $result = $this->propal->fetch($id);
345 throw new RestException(404,
'Commercial Proposal not found');
349 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
352 $request_data = (object) $request_data;
354 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
355 $request_data->label =
sanitizeVal($request_data->label);
357 $updateRes = $this->propal->addline(
359 $request_data->subprice,
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,
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
386 if ($updateRes > 0) {
389 throw new RestException(400, $this->propal->error);
405 if (!DolibarrApiAccess::$user->rights->propal->creer) {
406 throw new RestException(401);
409 $result = $this->propal->fetch($id);
411 throw new RestException(404,
'Commercial Proposal not found');
415 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
421 foreach ($request_data as $TData) {
422 if (empty($TData[0])) $TData = array($TData);
424 foreach ($TData as $lineData) {
425 $line = (object) $lineData;
427 $updateRes = $this->propal->addline(
435 $line->remise_percent,
442 $line->fk_parent_line,
443 $line->fk_fournprice,
448 $line->array_options,
452 $line->multicurrency_subprice,
453 $line->fk_remise_except
456 if ($updateRes < 0) {
457 $errors[
'lineLabel'] = $line->label;
458 $errors[
'msg'] = $this->propal->errors;
462 if (empty($errors)) {
464 return count($request_data);
466 $this->
db->rollback();
467 throw new RestException(400, implode(
", ", $errors));
482 public function putLine($id, $lineid, $request_data =
null)
484 if (!DolibarrApiAccess::$user->rights->propal->creer) {
485 throw new RestException(401);
488 $result = $this->propal->fetch($id);
490 throw new RestException(404,
'Proposal not found');
494 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 $request_data = (object) $request_data;
499 if (isset($request_data->desc)) {
500 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
502 if (isset($request_data->label)) {
503 $request_data->label =
sanitizeVal($request_data->label);
507 $result = $propalline->fetch($lineid);
509 throw new RestException(404,
'Proposal line not found');
512 $updateRes = $this->propal->updateline(
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,
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,
536 isset($request_data->rang) ? $request_data->rang : $propalline->rang
539 if ($updateRes > 0) {
540 $result = $this->
get($id);
541 unset($result->line);
563 if (!DolibarrApiAccess::$user->rights->propal->creer) {
564 throw new RestException(401);
567 $result = $this->propal->fetch($id);
569 throw new RestException(404,
'Proposal not found');
573 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
578 $updateRes = $this->propal->deleteline($lineid);
579 if ($updateRes > 0) {
580 return $this->
get($id);
582 throw new RestException(405, $this->propal->error);
602 if (!DolibarrApiAccess::$user->rights->propal->creer) {
603 throw new RestException(401);
606 $result = $this->propal->fetch($id);
609 throw new RestException(404,
'Proposal not found');
612 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
613 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
617 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
620 $result = $this->propal->add_contact($contactid, $type,
'external');
623 throw new RestException(500,
'Error when added the contact');
626 return $this->propal;
646 if (!DolibarrApiAccess::$user->rights->propal->creer) {
647 throw new RestException(401);
650 $result = $this->propal->fetch($id);
653 throw new RestException(404,
'Proposal not found');
657 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
660 $contacts = $this->propal->liste_contact();
662 foreach ($contacts as $contact) {
663 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
664 $result = $this->propal->delete_contact($contact[
'rowid']);
667 throw new RestException(500,
'Error when deleted the contact');
683 public function put($id, $request_data =
null)
685 if (!DolibarrApiAccess::$user->rights->propal->creer) {
686 throw new RestException(401);
689 $result = $this->propal->fetch($id);
691 throw new RestException(404,
'Proposal not found');
695 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
697 foreach ($request_data as $field => $value) {
698 if ($field ==
'id') {
701 $this->propal->$field = $value;
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);
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);
714 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
715 return $this->
get($id);
717 throw new RestException(500, $this->propal->error);
728 public function delete($id)
730 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
731 throw new RestException(401);
733 $result = $this->propal->fetch($id);
735 throw new RestException(404,
'Commercial Proposal not found');
739 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
742 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
743 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
749 'message' =>
'Commercial Proposal deleted'
765 if (!DolibarrApiAccess::$user->rights->propal->creer) {
766 throw new RestException(401);
768 $result = $this->propal->fetch($id);
770 throw new RestException(404,
'Proposal not found');
774 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
777 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
779 throw new RestException(304,
'Nothing done. May be object is already draft');
782 throw new RestException(500,
'Error : '.$this->propal->error);
785 $result = $this->propal->fetch($id);
787 throw new RestException(404,
'Proposal not found');
791 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
794 $this->propal->fetchObjectLinked();
822 if (!DolibarrApiAccess::$user->rights->propal->creer) {
823 throw new RestException(401);
825 $result = $this->propal->fetch($id);
827 throw new RestException(404,
'Commercial Proposal not found');
831 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
834 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
836 throw new RestException(304,
'Error nothing done. May be object is already validated');
839 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
842 $result = $this->propal->fetch($id);
844 throw new RestException(404,
'Commercial Proposal not found');
848 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
851 $this->propal->fetchObjectLinked();
868 public function close($id, $status, $note_private =
'', $notrigger = 0)
870 if (!DolibarrApiAccess::$user->rights->propal->creer) {
871 throw new RestException(401);
873 $result = $this->propal->fetch($id);
875 throw new RestException(404,
'Commercial Proposal not found');
879 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
882 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
884 throw new RestException(304,
'Error nothing done. May be object is already closed');
887 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
890 $result = $this->propal->fetch($id);
892 throw new RestException(404,
'Proposal not found');
896 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
899 $this->propal->fetchObjectLinked();
915 if (!DolibarrApiAccess::$user->rights->propal->creer) {
916 throw new RestException(401);
918 $result = $this->propal->fetch($id);
920 throw new RestException(404,
'Commercial Proposal not found');
924 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
927 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
929 throw new RestException(500,
'Error : '.$this->propal->error);
932 $result = $this->propal->fetch($id);
934 throw new RestException(404,
'Proposal not found');
938 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
941 $this->propal->fetchObjectLinked();
957 foreach (Proposals::$FIELDS as $field) {
958 if (!isset($data[$field])) {
959 throw new RestException(400,
"$field field missing");
961 $propal[$field] = $data[$field];
977 $object = parent::_cleanObjectDatas($object);
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);