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);
209 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
213 $sql .= $this->
db->order($sortfield, $sortorder);
218 $offset = $limit * $page;
220 $sql .= $this->
db->plimit($limit + 1, $offset);
224 $result = $this->
db->query($sql);
227 $num = $this->
db->num_rows($result);
228 $min = min($num, ($limit <= 0 ? $num : $limit));
231 $obj = $this->
db->fetch_object($result);
232 $proposal_static =
new Propal($this->
db);
233 if ($proposal_static->fetch($obj->rowid)) {
235 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
236 if (is_array($tmparray)) {
237 $proposal_static->contacts_ids = $tmparray;
244 throw new RestException(503,
'Error when retrieve propal list : '.$this->
db->lasterror());
246 if (!count($obj_ret)) {
247 throw new RestException(404,
'No proposal found');
258 public function post($request_data =
null)
260 if (!DolibarrApiAccess::$user->rights->propal->creer) {
261 throw new RestException(401,
"Insuffisant rights");
264 $result = $this->
_validate($request_data);
266 foreach ($request_data as $field => $value) {
267 $this->propal->$field = $value;
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));
280 return $this->propal->id;
295 if (!DolibarrApiAccess::$user->rights->propal->lire) {
296 throw new RestException(401);
299 $result = $this->propal->fetch($id);
301 throw new RestException(404,
'Commercial Proposal not found');
305 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
309 if (!empty($sqlfilters)) {
313 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
317 $this->propal->getLinesArray($sql);
319 foreach ($this->propal->lines as $line) {
335 public function postLine($id, $request_data =
null)
337 if (!DolibarrApiAccess::$user->rights->propal->creer) {
338 throw new RestException(401);
341 $result = $this->propal->fetch($id);
343 throw new RestException(404,
'Commercial Proposal not found');
347 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
350 $request_data = (object) $request_data;
352 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
353 $request_data->label =
sanitizeVal($request_data->label);
355 $updateRes = $this->propal->addline(
357 $request_data->subprice,
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,
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
384 if ($updateRes > 0) {
387 throw new RestException(400, $this->propal->error);
403 if (!DolibarrApiAccess::$user->rights->propal->creer) {
404 throw new RestException(401);
407 $result = $this->propal->fetch($id);
409 throw new RestException(404,
'Commercial Proposal not found');
413 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
419 foreach ($request_data as $TData) {
420 if (empty($TData[0])) $TData = array($TData);
422 foreach ($TData as $lineData) {
423 $line = (object) $lineData;
425 $updateRes = $this->propal->addline(
433 $line->remise_percent,
440 $line->fk_parent_line,
441 $line->fk_fournprice,
446 $line->array_options,
450 $line->multicurrency_subprice,
451 $line->fk_remise_except
454 if ($updateRes < 0) {
455 $errors[
'lineLabel'] = $line->label;
456 $errors[
'msg'] = $this->propal->errors;
460 if (empty($errors)) {
462 return count($request_data);
464 $this->
db->rollback();
465 throw new RestException(400, implode(
", ", $errors));
480 public function putLine($id, $lineid, $request_data =
null)
482 if (!DolibarrApiAccess::$user->rights->propal->creer) {
483 throw new RestException(401);
486 $result = $this->propal->fetch($id);
488 throw new RestException(404,
'Proposal not found');
492 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $request_data = (object) $request_data;
497 if (isset($request_data->desc)) {
498 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
500 if (isset($request_data->label)) {
501 $request_data->label =
sanitizeVal($request_data->label);
505 $result = $propalline->fetch($lineid);
507 throw new RestException(404,
'Proposal line not found');
510 $updateRes = $this->propal->updateline(
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,
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,
534 isset($request_data->rang) ? $request_data->rang : $propalline->rang
537 if ($updateRes > 0) {
538 $result = $this->
get($id);
539 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);
576 $updateRes = $this->propal->deleteline($lineid);
577 if ($updateRes > 0) {
578 return $this->
get($id);
580 throw new RestException(405, $this->propal->error);
600 if (!DolibarrApiAccess::$user->rights->propal->creer) {
601 throw new RestException(401);
604 $result = $this->propal->fetch($id);
607 throw new RestException(404,
'Proposal not found');
610 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
611 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
615 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
618 $result = $this->propal->add_contact($contactid, $type,
'external');
621 throw new RestException(500,
'Error when added the contact');
624 return $this->propal;
644 if (!DolibarrApiAccess::$user->rights->propal->creer) {
645 throw new RestException(401);
648 $result = $this->propal->fetch($id);
651 throw new RestException(404,
'Proposal not found');
655 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
658 $contacts = $this->propal->liste_contact();
660 foreach ($contacts as $contact) {
661 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
662 $result = $this->propal->delete_contact($contact[
'rowid']);
665 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);
726 public function delete($id)
728 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
729 throw new RestException(401);
731 $result = $this->propal->fetch($id);
733 throw new RestException(404,
'Commercial Proposal not found');
737 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
740 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
741 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
747 'message' =>
'Commercial Proposal deleted'
763 if (!DolibarrApiAccess::$user->rights->propal->creer) {
764 throw new RestException(401);
766 $result = $this->propal->fetch($id);
768 throw new RestException(404,
'Proposal not found');
772 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
775 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
777 throw new RestException(304,
'Nothing done. May be object is already draft');
780 throw new RestException(500,
'Error : '.$this->propal->error);
783 $result = $this->propal->fetch($id);
785 throw new RestException(404,
'Proposal not found');
789 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
792 $this->propal->fetchObjectLinked();
820 if (!DolibarrApiAccess::$user->rights->propal->creer) {
821 throw new RestException(401);
823 $result = $this->propal->fetch($id);
825 throw new RestException(404,
'Commercial Proposal not found');
829 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
834 throw new RestException(304,
'Error nothing done. May be object is already validated');
837 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
840 $result = $this->propal->fetch($id);
842 throw new RestException(404,
'Commercial Proposal not found');
846 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
849 $this->propal->fetchObjectLinked();
866 public function close($id, $status, $note_private =
'', $notrigger = 0)
868 if (!DolibarrApiAccess::$user->rights->propal->creer) {
869 throw new RestException(401);
871 $result = $this->propal->fetch($id);
873 throw new RestException(404,
'Commercial Proposal not found');
877 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
880 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
882 throw new RestException(304,
'Error nothing done. May be object is already closed');
885 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
888 $result = $this->propal->fetch($id);
890 throw new RestException(404,
'Proposal not found');
894 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
897 $this->propal->fetchObjectLinked();
913 if (!DolibarrApiAccess::$user->rights->propal->creer) {
914 throw new RestException(401);
916 $result = $this->propal->fetch($id);
918 throw new RestException(404,
'Commercial Proposal not found');
922 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
925 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
927 throw new RestException(500,
'Error : '.$this->propal->error);
930 $result = $this->propal->fetch($id);
932 throw new RestException(404,
'Proposal not found');
936 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
939 $this->propal->fetchObjectLinked();
955 foreach (Proposals::$FIELDS as $field) {
956 if (!isset($data[$field])) {
957 throw new RestException(400,
"$field field missing");
959 $propal[$field] = $data[$field];
975 $object = parent::_cleanObjectDatas($object);
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);
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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.