22use Luracast\Restler\RestException;
24require_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->hasRight(
'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->hasRight(
'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 LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
187 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
188 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
191 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
192 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
193 $sql .=
" AND t.fk_soc = sc.fk_soc";
196 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
198 if ($search_sale > 0) {
199 $sql .=
" AND t.rowid = sc.fk_soc";
202 if ($search_sale > 0) {
203 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
210 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
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 ((
int) $this->propal->id);
296 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
297 throw new RestException(401);
300 $result = $this->propal->fetch($id);
302 throw new RestException(404,
'Commercial Proposal not found');
306 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
310 if (!empty($sqlfilters)) {
314 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
318 $this->propal->getLinesArray($sql);
320 foreach ($this->propal->lines as $line) {
336 public function postLine($id, $request_data =
null)
338 if (!DolibarrApiAccess::$user->rights->propal->creer) {
339 throw new RestException(401);
342 $result = $this->propal->fetch($id);
344 throw new RestException(404,
'Commercial Proposal not found');
348 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
351 $request_data = (object) $request_data;
353 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
354 $request_data->label =
sanitizeVal($request_data->label);
356 $updateRes = $this->propal->addline(
358 $request_data->subprice,
360 $request_data->tva_tx,
361 $request_data->localtax1_tx,
362 $request_data->localtax2_tx,
363 $request_data->fk_product,
364 $request_data->remise_percent,
365 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
366 $request_data->subprice,
367 $request_data->info_bits,
368 $request_data->product_type,
370 $request_data->special_code,
371 $request_data->fk_parent_line,
372 $request_data->fk_fournprice,
373 $request_data->pa_ht,
374 $request_data->label,
375 $request_data->date_start,
376 $request_data->date_end,
377 $request_data->array_options,
378 $request_data->fk_unit,
379 $request_data->origin,
380 $request_data->origin_id,
381 $request_data->multicurrency_subprice,
382 $request_data->fk_remise_except
385 if ($updateRes > 0) {
388 throw new RestException(400, $this->propal->error);
404 if (!DolibarrApiAccess::$user->rights->propal->creer) {
405 throw new RestException(401);
408 $result = $this->propal->fetch($id);
410 throw new RestException(404,
'Commercial Proposal not found');
414 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 foreach ($request_data as $TData) {
421 if (empty($TData[0])) $TData = array($TData);
423 foreach ($TData as $lineData) {
424 $line = (object) $lineData;
426 $updateRes = $this->propal->addline(
434 $line->remise_percent,
441 $line->fk_parent_line,
442 $line->fk_fournprice,
447 $line->array_options,
451 $line->multicurrency_subprice,
452 $line->fk_remise_except
455 if ($updateRes < 0) {
456 $errors[
'lineLabel'] = $line->label;
457 $errors[
'msg'] = $this->propal->errors;
461 if (empty($errors)) {
465 $this->db->rollback();
466 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);
560 if (!DolibarrApiAccess::$user->rights->propal->creer) {
561 throw new RestException(401);
564 $result = $this->propal->fetch($id);
566 throw new RestException(404,
'Proposal not found');
570 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
573 $updateRes = $this->propal->deleteline($lineid, $id);
574 if ($updateRes > 0) {
575 return $this->
get($id);
577 throw new RestException(405, $this->propal->error);
596 if (!DolibarrApiAccess::$user->rights->propal->creer) {
597 throw new RestException(401);
600 $result = $this->propal->fetch($id);
603 throw new RestException(404,
'Proposal not found');
606 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
607 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
611 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
614 $result = $this->propal->add_contact($contactid, $type,
'external');
617 throw new RestException(500,
'Error when added the contact');
623 'message' =>
'Contact linked to the proposal'
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');
680 public function put($id, $request_data =
null)
682 if (!DolibarrApiAccess::$user->rights->propal->creer) {
683 throw new RestException(401);
686 $result = $this->propal->fetch($id);
688 throw new RestException(404,
'Proposal not found');
692 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
694 foreach ($request_data as $field => $value) {
695 if ($field ==
'id') {
698 $this->propal->$field = $value;
702 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
703 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
705 if (!empty($this->propal->fin_validite)) {
706 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
707 throw new RestException(500, $this->propal->error);
711 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
712 return $this->
get($id);
714 throw new RestException(500, $this->propal->error);
724 public function delete($id)
726 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
727 throw new RestException(401);
729 $result = $this->propal->fetch($id);
731 throw new RestException(404,
'Commercial Proposal not found');
735 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
738 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
739 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
745 'message' =>
'Commercial Proposal deleted'
760 if (!DolibarrApiAccess::$user->rights->propal->creer) {
761 throw new RestException(401);
763 $result = $this->propal->fetch($id);
765 throw new RestException(404,
'Proposal not found');
769 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
774 throw new RestException(304,
'Nothing done. May be object is already draft');
777 throw new RestException(500,
'Error : '.$this->propal->error);
780 $result = $this->propal->fetch($id);
782 throw new RestException(404,
'Proposal not found');
786 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
789 $this->propal->fetchObjectLinked();
816 if (!DolibarrApiAccess::$user->rights->propal->creer) {
817 throw new RestException(401);
819 $result = $this->propal->fetch($id);
821 throw new RestException(404,
'Commercial Proposal not found');
825 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
828 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
830 throw new RestException(304,
'Error nothing done. May be object is already validated');
833 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
836 $result = $this->propal->fetch($id);
838 throw new RestException(404,
'Commercial Proposal not found');
842 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 $this->propal->fetchObjectLinked();
861 public function close($id, $status, $note_private =
'', $notrigger = 0)
863 if (!DolibarrApiAccess::$user->rights->propal->creer) {
864 throw new RestException(401);
866 $result = $this->propal->fetch($id);
868 throw new RestException(404,
'Commercial Proposal not found');
872 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
875 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
877 throw new RestException(304,
'Error nothing done. May be object is already closed');
880 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
883 $result = $this->propal->fetch($id);
885 throw new RestException(404,
'Proposal not found');
889 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
892 $this->propal->fetchObjectLinked();
907 if (!DolibarrApiAccess::$user->rights->propal->creer) {
908 throw new RestException(401);
910 $result = $this->propal->fetch($id);
912 throw new RestException(404,
'Commercial Proposal not found');
916 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
919 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
921 throw new RestException(500,
'Error : '.$this->propal->error);
924 $result = $this->propal->fetch($id);
926 throw new RestException(404,
'Proposal not found');
930 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
933 $this->propal->fetchObjectLinked();
950 foreach (Proposals::$FIELDS as $field) {
951 if (!isset($data[$field])) {
952 throw new RestException(400,
"$field field missing");
954 $propal[$field] = $data[$field];
970 $object = parent::_cleanObjectDatas($object);
972 unset($object->note);
973 unset($object->name);
974 unset($object->lastname);
975 unset($object->firstname);
976 unset($object->civility_id);
977 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, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
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.