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)) {
466 $this->
db->rollback();
467 throw new RestException(400, implode(
", ", $errors));
481 public function putLine($id, $lineid, $request_data =
null)
483 if (!DolibarrApiAccess::$user->rights->propal->creer) {
484 throw new RestException(401);
487 $result = $this->propal->fetch($id);
489 throw new RestException(404,
'Proposal not found');
493 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
496 $request_data = (object) $request_data;
498 if (isset($request_data->desc)) {
499 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
501 if (isset($request_data->label)) {
502 $request_data->label =
sanitizeVal($request_data->label);
506 $result = $propalline->fetch($lineid);
508 throw new RestException(404,
'Proposal line not found');
511 $updateRes = $this->propal->updateline(
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,
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,
535 isset($request_data->rang) ? $request_data->rang : $propalline->rang
538 if ($updateRes > 0) {
539 $result = $this->
get($id);
540 unset($result->line);
561 if (!DolibarrApiAccess::$user->rights->propal->creer) {
562 throw new RestException(401);
565 $result = $this->propal->fetch($id);
567 throw new RestException(404,
'Proposal not found');
571 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
574 $updateRes = $this->propal->deleteline($lineid, $id);
575 if ($updateRes > 0) {
576 return $this->
get($id);
578 throw new RestException(405, $this->propal->error);
597 if (!DolibarrApiAccess::$user->rights->propal->creer) {
598 throw new RestException(401);
601 $result = $this->propal->fetch($id);
604 throw new RestException(404,
'Proposal not found');
607 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
608 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
612 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 $result = $this->propal->add_contact($contactid, $type,
'external');
618 throw new RestException(500,
'Error when added the contact');
624 'message' =>
'Contact linked to the proposal'
645 if (!DolibarrApiAccess::$user->rights->propal->creer) {
646 throw new RestException(401);
649 $result = $this->propal->fetch($id);
652 throw new RestException(404,
'Proposal not found');
656 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
659 $contacts = $this->propal->liste_contact();
661 foreach ($contacts as $contact) {
662 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
663 $result = $this->propal->delete_contact($contact[
'rowid']);
666 throw new RestException(500,
'Error when deleted the contact');
681 public function put($id, $request_data =
null)
683 if (!DolibarrApiAccess::$user->rights->propal->creer) {
684 throw new RestException(401);
687 $result = $this->propal->fetch($id);
689 throw new RestException(404,
'Proposal not found');
693 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
695 foreach ($request_data as $field => $value) {
696 if ($field ==
'id') {
699 $this->propal->$field = $value;
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);
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);
712 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
713 return $this->
get($id);
715 throw new RestException(500, $this->propal->error);
725 public function delete($id)
727 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
728 throw new RestException(401);
730 $result = $this->propal->fetch($id);
732 throw new RestException(404,
'Commercial Proposal not found');
736 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
739 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
740 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
746 'message' =>
'Commercial Proposal deleted'
761 if (!DolibarrApiAccess::$user->rights->propal->creer) {
762 throw new RestException(401);
764 $result = $this->propal->fetch($id);
766 throw new RestException(404,
'Proposal not found');
770 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
773 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
775 throw new RestException(304,
'Nothing done. May be object is already draft');
778 throw new RestException(500,
'Error : '.$this->propal->error);
781 $result = $this->propal->fetch($id);
783 throw new RestException(404,
'Proposal not found');
787 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 $this->propal->fetchObjectLinked();
817 if (!DolibarrApiAccess::$user->rights->propal->creer) {
818 throw new RestException(401);
820 $result = $this->propal->fetch($id);
822 throw new RestException(404,
'Commercial Proposal not found');
826 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
829 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
831 throw new RestException(304,
'Error nothing done. May be object is already validated');
834 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
837 $result = $this->propal->fetch($id);
839 throw new RestException(404,
'Commercial Proposal not found');
843 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 $this->propal->fetchObjectLinked();
862 public function close($id, $status, $note_private =
'', $notrigger = 0)
864 if (!DolibarrApiAccess::$user->rights->propal->creer) {
865 throw new RestException(401);
867 $result = $this->propal->fetch($id);
869 throw new RestException(404,
'Commercial Proposal not found');
873 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
876 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
878 throw new RestException(304,
'Error nothing done. May be object is already closed');
881 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
884 $result = $this->propal->fetch($id);
886 throw new RestException(404,
'Proposal not found');
890 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 $this->propal->fetchObjectLinked();
908 if (!DolibarrApiAccess::$user->rights->propal->creer) {
909 throw new RestException(401);
911 $result = $this->propal->fetch($id);
913 throw new RestException(404,
'Commercial Proposal not found');
917 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
920 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
922 throw new RestException(500,
'Error : '.$this->propal->error);
925 $result = $this->propal->fetch($id);
927 throw new RestException(404,
'Proposal not found');
931 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
934 $this->propal->fetchObjectLinked();
951 foreach (Proposals::$FIELDS as $field) {
952 if (!isset($data[$field])) {
953 throw new RestException(400,
"$field field missing");
955 $propal[$field] = $data[$field];
971 $object = parent::_cleanObjectDatas($object);
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);
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.