23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
39 public static $FIELDS = array(
55 $this->propal =
new Propal($this->db);
69 public function get($id, $contact_list = 1)
71 return $this->
_fetch($id,
'',
'', $contact_list);
87 public function getByRef($ref, $contact_list = 1)
89 return $this->
_fetch(
'', $ref,
'', $contact_list);
107 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
123 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
125 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
126 throw new RestException(403);
129 $result = $this->propal->fetch($id, $ref, $ref_ext);
131 throw new RestException(404,
'Commercial Proposal not found');
135 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
139 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
140 if (is_array($tmparray)) {
141 $this->propal->contacts_ids = $tmparray;
144 $this->propal->fetchObjectLinked();
164 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
166 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
167 throw new RestException(403);
173 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
177 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
178 $search_sale = DolibarrApiAccess::$user->id;
181 $sql =
"SELECT t.rowid";
182 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t";
183 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
184 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
186 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
189 if ($search_sale && $search_sale !=
'-1') {
190 if ($search_sale == -2) {
191 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
192 } elseif ($search_sale > 0) {
193 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
201 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
206 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
208 $sql .= $this->db->order($sortfield, $sortorder);
213 $offset = $limit * $page;
215 $sql .= $this->db->plimit($limit + 1, $offset);
219 $result = $this->db->query($sql);
222 $num = $this->db->num_rows($result);
223 $min = min($num, ($limit <= 0 ? $num : $limit));
226 $obj = $this->db->fetch_object($result);
227 $proposal_static =
new Propal($this->db);
228 if ($proposal_static->fetch($obj->rowid)) {
230 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
231 if (is_array($tmparray)) {
232 $proposal_static->contacts_ids = $tmparray;
239 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
243 if ($pagination_data) {
244 $totalsResult = $this->db->query($sqlTotals);
245 $total = $this->db->fetch_object($totalsResult)->total;
250 $obj_ret[
'data'] = $tmp;
251 $obj_ret[
'pagination'] = [
252 'total' => (int) $total,
254 'page_count' => ceil((
int) $total / $limit),
268 public function post($request_data =
null)
270 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
271 throw new RestException(403,
"Insuffisant rights");
274 $result = $this->
_validate($request_data);
276 foreach ($request_data as $field => $value) {
277 if ($field ===
'caller') {
279 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
283 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
292 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
293 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
296 return ((
int) $this->propal->id);
311 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
312 throw new RestException(403);
315 $result = $this->propal->fetch($id);
317 throw new RestException(404,
'Commercial Proposal not found');
321 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
325 if (!empty($sqlfilters)) {
329 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
333 $this->propal->getLinesArray($sql);
335 foreach ($this->propal->lines as $line) {
351 public function postLine($id, $request_data =
null)
353 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
354 throw new RestException(403);
357 $result = $this->propal->fetch($id);
359 throw new RestException(404,
'Commercial Proposal not found');
363 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 $request_data = (object) $request_data;
368 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
369 $request_data->label =
sanitizeVal($request_data->label);
371 $updateRes = $this->propal->addline(
373 $request_data->subprice,
375 $request_data->tva_tx,
376 $request_data->localtax1_tx,
377 $request_data->localtax2_tx,
378 $request_data->fk_product,
379 $request_data->remise_percent,
380 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
381 $request_data->subprice,
382 $request_data->info_bits,
383 $request_data->product_type,
385 $request_data->special_code,
386 $request_data->fk_parent_line,
387 $request_data->fk_fournprice,
388 $request_data->pa_ht,
389 $request_data->label,
390 $request_data->date_start,
391 $request_data->date_end,
392 $request_data->array_options,
393 $request_data->fk_unit,
394 $request_data->origin,
395 $request_data->origin_id,
396 $request_data->multicurrency_subprice,
397 $request_data->fk_remise_except
400 if ($updateRes > 0) {
403 throw new RestException(400, $this->propal->error);
419 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
420 throw new RestException(403);
423 $result = $this->propal->fetch($id);
425 throw new RestException(404,
'Commercial Proposal not found');
429 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 foreach ($request_data as $TData) {
437 if (empty($TData[0])) {
438 $TData = array($TData);
441 foreach ($TData as $lineData) {
442 $line = (object) $lineData;
444 $updateRes = $this->propal->addline(
452 $line->remise_percent,
459 $line->fk_parent_line,
460 $line->fk_fournprice,
465 $line->array_options,
469 $line->multicurrency_subprice,
470 $line->fk_remise_except
473 if ($updateRes < 0) {
474 $errors[
'lineLabel'] = $line->label;
475 $errors[
'msg'] = $this->propal->errors;
479 if (empty($errors)) {
483 $this->db->rollback();
484 throw new RestException(400, implode(
", ", $errors));
498 public function putLine($id, $lineid, $request_data =
null)
500 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
501 throw new RestException(403);
504 $result = $this->propal->fetch($id);
506 throw new RestException(404,
'Proposal not found');
510 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
513 $request_data = (object) $request_data;
515 if (isset($request_data->desc)) {
516 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
518 if (isset($request_data->label)) {
519 $request_data->label =
sanitizeVal($request_data->label);
523 $result = $propalline->fetch($lineid);
525 throw new RestException(404,
'Proposal line not found');
528 $updateRes = $this->propal->updateline(
530 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
531 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
532 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
533 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
534 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
535 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
536 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
537 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
538 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
539 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
540 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
542 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
543 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
544 isset($request_data->label) ? $request_data->label : $propalline->label,
545 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
546 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
547 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
548 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
549 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
550 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
552 isset($request_data->rang) ? $request_data->rang : $propalline->rang
555 if ($updateRes > 0) {
556 $result = $this->
get($id);
557 unset($result->line);
578 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
579 throw new RestException(403);
582 $result = $this->propal->fetch($id);
584 throw new RestException(404,
'Proposal not found');
588 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
591 $updateRes = $this->propal->deleteLine($lineid, $id);
592 if ($updateRes > 0) {
593 return $this->
get($id);
595 throw new RestException(405, $this->propal->error);
614 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
615 throw new RestException(403);
618 $result = $this->propal->fetch($id);
621 throw new RestException(404,
'Proposal not found');
624 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
625 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
629 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
632 $result = $this->propal->add_contact($contactid, $type,
'external');
635 throw new RestException(500,
'Error when added the contact');
641 'message' =>
'Contact linked to the proposal'
662 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
663 throw new RestException(403);
666 $result = $this->propal->fetch($id);
669 throw new RestException(404,
'Proposal not found');
673 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 $contacts = $this->propal->liste_contact();
678 foreach ($contacts as $contact) {
679 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
680 $result = $this->propal->delete_contact($contact[
'rowid']);
683 throw new RestException(500,
'Error when deleted the contact');
698 public function put($id, $request_data =
null)
700 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
701 throw new RestException(403);
704 $result = $this->propal->fetch($id);
706 throw new RestException(404,
'Proposal not found');
710 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
712 foreach ($request_data as $field => $value) {
713 if ($field ==
'id') {
716 if ($field ===
'caller') {
718 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
721 if ($field ==
'array_options' && is_array($value)) {
722 foreach ($value as $index => $val) {
723 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
728 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
732 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
733 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
735 if (!empty($this->propal->fin_validite)) {
736 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
737 throw new RestException(500, $this->propal->error);
741 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
742 return $this->
get($id);
744 throw new RestException(500, $this->propal->error);
754 public function delete($id)
756 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
757 throw new RestException(403);
759 $result = $this->propal->fetch($id);
761 throw new RestException(404,
'Commercial Proposal not found');
765 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
768 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
769 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
775 'message' =>
'Commercial Proposal deleted'
790 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
791 throw new RestException(403);
793 $result = $this->propal->fetch($id);
795 throw new RestException(404,
'Proposal not found');
799 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
804 throw new RestException(304,
'Nothing done. May be object is already draft');
807 throw new RestException(500,
'Error : '.$this->propal->error);
810 $result = $this->propal->fetch($id);
812 throw new RestException(404,
'Proposal not found');
816 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
819 $this->propal->fetchObjectLinked();
846 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
847 throw new RestException(403);
849 $result = $this->propal->fetch($id);
851 throw new RestException(404,
'Commercial Proposal not found');
855 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
858 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
860 throw new RestException(304,
'Error nothing done. May be object is already validated');
863 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
866 $result = $this->propal->fetch($id);
868 throw new RestException(404,
'Commercial Proposal not found');
872 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
875 $this->propal->fetchObjectLinked();
891 public function close($id, $status, $note_private =
'', $notrigger = 0)
893 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
894 throw new RestException(403);
896 $result = $this->propal->fetch($id);
898 throw new RestException(404,
'Commercial Proposal not found');
902 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
905 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
907 throw new RestException(304,
'Error nothing done. May be object is already closed');
910 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
913 $result = $this->propal->fetch($id);
915 throw new RestException(404,
'Proposal not found');
919 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
922 $this->propal->fetchObjectLinked();
937 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
938 throw new RestException(403);
940 $result = $this->propal->fetch($id);
942 throw new RestException(404,
'Commercial Proposal not found');
946 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
949 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
951 throw new RestException(500,
'Error : '.$this->propal->error);
954 $result = $this->propal->fetch($id);
956 throw new RestException(404,
'Proposal not found');
960 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
963 $this->propal->fetchObjectLinked();
980 foreach (Proposals::$FIELDS as $field) {
981 if (!isset($data[$field])) {
982 throw new RestException(400,
"$field field missing");
984 $propal[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List commercial proposals.
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.
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.