24use Luracast\Restler\RestException;
26require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
40 public static $FIELDS = array(
56 $this->propal =
new Propal($this->db);
70 public function get(
$id, $contact_list = 1)
72 return $this->
_fetch(
$id,
'',
'', $contact_list);
88 public function getByRef($ref, $contact_list = 1)
90 return $this->
_fetch(0, $ref,
'', $contact_list);
108 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
124 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
126 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
127 throw new RestException(403);
130 $result = $this->propal->fetch(
$id, $ref, $ref_ext);
132 throw new RestException(404,
'Commercial Proposal not found');
136 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
140 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
141 if (is_array($tmparray)) {
142 $this->propal->contacts_ids = $tmparray;
145 $this->propal->fetchObjectLinked();
166 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
168 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
169 throw new RestException(403);
175 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
179 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
183 $sql =
"SELECT t.rowid";
184 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t";
185 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
186 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
188 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
191 if ($search_sale && $search_sale !=
'-1') {
192 if ($search_sale == -2) {
193 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
194 } elseif ($search_sale > 0) {
195 $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).
")";
203 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
208 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
210 $sql .= $this->db->order($sortfield, $sortorder);
215 $offset = $limit * $page;
217 $sql .= $this->db->plimit($limit + 1, $offset);
221 $result = $this->db->query($sql);
224 $num = $this->db->num_rows($result);
225 $min = min($num, ($limit <= 0 ? $num : $limit));
228 $obj = $this->db->fetch_object($result);
229 $proposal_static =
new Propal($this->db);
230 if ($proposal_static->fetch($obj->rowid) > 0) {
232 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
233 if (is_array($tmparray)) {
234 $proposal_static->contacts_ids = $tmparray;
237 if ($loadlinkedobjects) {
239 $proposal_static->fetchObjectLinked();
247 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
251 if ($pagination_data) {
252 $totalsResult = $this->db->query($sqlTotals);
253 $total = $this->db->fetch_object($totalsResult)->total;
258 $obj_ret[
'data'] = $tmp;
259 $obj_ret[
'pagination'] = [
260 'total' => (int) $total,
262 'page_count' => ceil((
int) $total / $limit),
276 public function post($request_data =
null)
278 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
279 throw new RestException(403,
"Insuffisant rights");
282 $result = $this->
_validate($request_data);
284 foreach ($request_data as $field => $value) {
285 if ($field ===
'caller') {
287 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
291 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
300 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
301 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
304 return ((
int) $this->propal->id);
319 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
320 throw new RestException(403);
323 $result = $this->propal->fetch(
$id);
325 throw new RestException(404,
'Commercial Proposal not found');
329 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
333 if (!empty($sqlfilters)) {
337 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
341 $this->propal->getLinesArray($sql);
343 foreach ($this->propal->lines as $line) {
361 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
362 throw new RestException(403);
365 $result = $this->propal->fetch(
$id);
367 throw new RestException(404,
'Commercial Proposal not found');
371 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
374 $request_data = (object) $request_data;
376 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
377 $request_data->label =
sanitizeVal($request_data->label);
379 $updateRes = $this->propal->addline(
381 $request_data->subprice,
383 $request_data->tva_tx,
384 $request_data->localtax1_tx,
385 $request_data->localtax2_tx,
386 $request_data->fk_product,
387 $request_data->remise_percent,
388 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
389 $request_data->subprice,
390 $request_data->info_bits,
391 $request_data->product_type,
393 $request_data->special_code,
394 $request_data->fk_parent_line,
395 $request_data->fk_fournprice,
396 $request_data->pa_ht,
397 $request_data->label,
398 $request_data->date_start,
399 $request_data->date_end,
400 $request_data->array_options,
401 $request_data->fk_unit,
402 $request_data->origin,
403 $request_data->origin_id,
404 $request_data->multicurrency_subprice,
405 $request_data->fk_remise_except
408 if ($updateRes > 0) {
411 throw new RestException(400, $this->propal->error);
427 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
428 throw new RestException(403);
431 $result = $this->propal->fetch(
$id);
433 throw new RestException(404,
'Commercial Proposal not found');
437 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
444 foreach ($request_data as $TData) {
445 if (empty($TData[0])) {
446 $TData = array($TData);
449 foreach ($TData as $lineData) {
450 $line = (object) $lineData;
452 $updateRes = $this->propal->addline(
460 $line->remise_percent,
467 $line->fk_parent_line,
468 $line->fk_fournprice,
473 $line->array_options,
477 $line->multicurrency_subprice,
478 $line->fk_remise_except
481 if ($updateRes < 0) {
482 $errors[
'lineLabel'] = $line->label;
483 $errors[
'msg'] = $this->propal->errors;
487 if (empty($errors)) {
491 $this->db->rollback();
492 throw new RestException(400, implode(
", ", $errors));
508 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
509 throw new RestException(403);
512 $result = $this->propal->fetch(
$id);
514 throw new RestException(404,
'Proposal not found');
518 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
521 $request_data = (object) $request_data;
523 if (isset($request_data->desc)) {
524 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
526 if (isset($request_data->label)) {
527 $request_data->label =
sanitizeVal($request_data->label);
531 $result = $propalline->fetch($lineid);
533 throw new RestException(404,
'Proposal line not found');
536 $updateRes = $this->propal->updateline(
538 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
539 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
540 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
541 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
542 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
543 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
544 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
545 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
546 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
547 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
548 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
550 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
551 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
552 isset($request_data->label) ? $request_data->label : $propalline->label,
553 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
554 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
555 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
556 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
557 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
558 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
560 isset($request_data->rang) ? $request_data->rang : $propalline->rang
563 if ($updateRes > 0) {
564 $result = $this->
get(
$id);
565 unset($result->line);
586 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
587 throw new RestException(403);
590 $result = $this->propal->fetch(
$id);
592 throw new RestException(404,
'Proposal not found');
596 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
599 $updateRes = $this->propal->deleteLine($lineid,
$id);
600 if ($updateRes > 0) {
601 return $this->
get(
$id);
603 throw new RestException(405, $this->propal->error);
623 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
624 throw new RestException(403);
627 $result = $this->propal->fetch(
$id);
630 throw new RestException(404,
'Proposal not found');
633 if (!in_array($source, array(
'internal',
'external'),
true)) {
634 throw new RestException(500,
'Availables sources: internal OR external');
637 if ($source ==
'external' && !in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
638 throw new RestException(500,
'Availables external types: BILLING, SHIPPING OR CUSTOMER');
641 if ($source ==
'internal' && !in_array($type, array(
'SALESREPFOLL'),
true)) {
642 throw new RestException(500,
'Availables internal types: SALESREPFOLL');
646 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
649 $result = $this->propal->add_contact($contactid, $type, $source);
652 throw new RestException(500,
'Error when added the contact');
658 'message' =>
'Contact linked to the proposal'
679 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
680 throw new RestException(403);
683 $result = $this->propal->fetch(
$id);
686 throw new RestException(404,
'Proposal not found');
690 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 $contacts = $this->propal->liste_contact();
695 foreach ($contacts as $contact) {
696 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
697 $result = $this->propal->delete_contact($contact[
'rowid']);
700 throw new RestException(500,
'Error when deleted the contact');
715 public function put(
$id, $request_data =
null)
717 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
718 throw new RestException(403);
721 $result = $this->propal->fetch(
$id);
723 throw new RestException(404,
'Proposal not found');
727 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
729 foreach ($request_data as $field => $value) {
730 if ($field ==
'id') {
733 if ($field ===
'caller') {
735 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
738 if ($field ==
'array_options' && is_array($value)) {
739 foreach ($value as $index => $val) {
740 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
745 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
749 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
750 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
752 if (!empty($this->propal->fin_validite)) {
753 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
754 throw new RestException(500, $this->propal->error);
758 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
759 return $this->
get(
$id);
761 throw new RestException(500, $this->propal->error);
771 public function delete(
$id)
773 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
774 throw new RestException(403);
776 $result = $this->propal->fetch(
$id);
778 throw new RestException(404,
'Commercial Proposal not found');
782 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
785 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
786 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
792 'message' =>
'Commercial Proposal deleted'
807 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
808 throw new RestException(403);
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 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
821 throw new RestException(304,
'Nothing done. May be object is already draft');
824 throw new RestException(500,
'Error : '.$this->propal->error);
827 $result = $this->propal->fetch(
$id);
829 throw new RestException(404,
'Proposal not found');
833 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
836 $this->propal->fetchObjectLinked();
863 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
864 throw new RestException(403);
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 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
877 throw new RestException(304,
'Error nothing done. May be object is already validated');
880 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
883 $result = $this->propal->fetch(
$id);
885 throw new RestException(404,
'Commercial Proposal not found');
889 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
892 $this->propal->fetchObjectLinked();
909 public function close(
$id, $status, $note_private =
'', $notrigger = 0, $note_public =
'')
911 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
912 throw new RestException(403);
914 $result = $this->propal->fetch(
$id);
916 throw new RestException(404,
'Commercial Proposal not found');
920 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
923 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
925 throw new RestException(304,
'Error nothing done. May be object is already closed');
928 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
931 $result = $this->propal->fetch(
$id);
933 throw new RestException(404,
'Proposal not found');
937 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
940 $this->propal->fetchObjectLinked();
955 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
956 throw new RestException(403);
958 $result = $this->propal->fetch(
$id);
960 throw new RestException(404,
'Commercial Proposal not found');
964 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
967 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
969 throw new RestException(500,
'Error : '.$this->propal->error);
972 $result = $this->propal->fetch(
$id);
974 throw new RestException(404,
'Proposal not found');
978 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
981 $this->propal->fetchObjectLinked();
998 foreach (Proposals::$FIELDS as $field) {
999 if (!isset($data[$field])) {
1000 throw new RestException(400,
"$field field missing");
1002 $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)
setinvoiced($id)
Set a commercial proposal billed.
post($request_data=null)
Create commercial proposal object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List commercial proposals.
postContact($id, $contactid, $type, $source='external')
Add a contact type of given commercial proposal.
close($id, $status, $note_private='', $notrigger=0, $note_public='')
Close (Accept or refuse) a quote / 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.