25use Luracast\Restler\RestException;
27require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
43 public static $FIELDS = array(
59 $this->propal =
new Propal($this->db);
75 public function get(
$id, $contact_list = 1)
77 return $this->
_fetch(
$id,
'',
'', $contact_list);
95 public function getByRef($ref, $contact_list = 1)
97 return $this->
_fetch(0, $ref,
'', $contact_list);
117 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
133 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
135 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
136 throw new RestException(403);
139 $result = $this->propal->fetch(
$id, $ref, $ref_ext);
141 throw new RestException(404,
'Commercial Proposal not found');
145 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
148 if ($contact_list > -1) {
150 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
151 if (is_array($tmparray)) {
152 $this->propal->contacts_ids = $tmparray;
154 $tmparray = $this->propal->liste_contact(-1,
'internal', $contact_list);
155 if (is_array($tmparray)) {
156 $this->propal->contacts_ids_internal = $tmparray;
160 $this->propal->fetchObjectLinked();
187 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
189 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
190 throw new RestException(403);
196 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
200 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
201 $search_sale = DolibarrApiAccess::$user->id;
204 $sql =
"SELECT t.rowid";
205 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t";
206 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
207 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
209 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
212 if ($search_sale && $search_sale !=
'-1') {
213 if ($search_sale == -2) {
214 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
215 } elseif ($search_sale > 0) {
216 $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).
")";
224 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
229 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
231 $sql .= $this->db->order($sortfield, $sortorder);
236 $offset = $limit * $page;
238 $sql .= $this->db->plimit($limit + 1, $offset);
242 $result = $this->db->query($sql);
245 $num = $this->db->num_rows($result);
246 $min = min($num, ($limit <= 0 ? $num : $limit));
249 $obj = $this->db->fetch_object($result);
250 $proposal_static =
new Propal($this->db);
251 if ($proposal_static->fetch($obj->rowid) > 0) {
253 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
254 if (is_array($tmparray)) {
255 $proposal_static->contacts_ids = $tmparray;
258 if ($loadlinkedobjects) {
260 $proposal_static->fetchObjectLinked();
268 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
272 if ($pagination_data) {
273 $totalsResult = $this->db->query($sqlTotals);
274 $total = $this->db->fetch_object($totalsResult)->total;
279 $obj_ret[
'data'] = $tmp;
280 $obj_ret[
'pagination'] = [
281 'total' => (int) $total,
283 'page_count' => ceil((
int) $total / $limit),
303 public function post($request_data =
null)
305 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
306 throw new RestException(403,
"Insuffisant rights");
309 $result = $this->
_validate($request_data);
311 foreach ($request_data as $field => $value) {
312 if ($field ===
'caller') {
314 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
318 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
327 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
328 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
331 return ((
int) $this->propal->id);
352 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
353 throw new RestException(403);
356 $result = $this->propal->fetch(
$id);
358 throw new RestException(404,
'Commercial Proposal not found');
362 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 if (!empty($sqlfilters)) {
370 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
374 $this->propal->getLinesArray($sql);
376 foreach ($this->propal->lines as $line) {
400 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
401 throw new RestException(403);
404 $result = $this->propal->fetch(
$id);
406 throw new RestException(404,
'Commercial Proposal not found');
410 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
413 $request_data = (object) $request_data;
415 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
416 $request_data->label =
sanitizeVal($request_data->label);
418 $updateRes = $this->propal->addline(
420 $request_data->subprice,
422 $request_data->tva_tx,
423 $request_data->localtax1_tx,
424 $request_data->localtax2_tx,
425 $request_data->fk_product,
426 $request_data->remise_percent,
427 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
428 $request_data->subprice,
429 $request_data->info_bits,
430 $request_data->product_type,
432 $request_data->special_code,
433 $request_data->fk_parent_line,
434 $request_data->fk_fournprice,
435 $request_data->pa_ht,
436 $request_data->label,
437 $request_data->date_start,
438 $request_data->date_end,
439 $request_data->array_options,
440 $request_data->fk_unit,
441 $request_data->origin,
442 $request_data->origin_id,
443 $request_data->multicurrency_subprice,
444 $request_data->fk_remise_except
447 if ($updateRes > 0) {
450 throw new RestException(400, $this->propal->error);
470 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
471 throw new RestException(403);
474 $result = $this->propal->fetch(
$id);
476 throw new RestException(404,
'Commercial Proposal not found');
480 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
487 foreach ($request_data as $TData) {
488 if (empty($TData[0])) {
489 $TData = array($TData);
492 foreach ($TData as $lineData) {
493 $line = (object) $lineData;
495 $updateRes = $this->propal->addline(
503 $line->remise_percent,
510 $line->fk_parent_line,
511 $line->fk_fournprice,
516 $line->array_options,
520 $line->multicurrency_subprice,
521 $line->fk_remise_except
524 if ($updateRes < 0) {
525 $errors[
'lineLabel'] = $line->label;
526 $errors[
'msg'] = $this->propal->errors;
530 if (empty($errors)) {
534 $this->db->rollback();
535 throw new RestException(400, implode(
", ", $errors));
557 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
558 throw new RestException(403);
561 $result = $this->propal->fetch(
$id);
563 throw new RestException(404,
'Proposal not found');
567 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
570 $request_data = (object) $request_data;
572 if (isset($request_data->desc)) {
573 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
575 if (isset($request_data->label)) {
576 $request_data->label =
sanitizeVal($request_data->label);
580 $result = $propalline->fetch($lineid);
582 throw new RestException(404,
'Proposal line not found');
585 if ($propalline->fk_propal !=
$id) {
586 throw new RestException(403,
'Line does not belong to this proposal');
589 $updateRes = $this->propal->updateline(
591 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
592 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
593 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
594 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
595 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
596 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
597 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
598 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
599 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
600 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
601 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
603 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
604 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
605 isset($request_data->label) ? $request_data->label : $propalline->label,
606 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
607 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
608 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
609 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
610 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
611 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
613 isset($request_data->rang) ? $request_data->rang : $propalline->rang
616 if ($updateRes > 0) {
617 $result = $this->
get(
$id);
618 unset($result->line);
640 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
641 throw new RestException(403);
644 $result = $this->propal->fetch(
$id);
646 throw new RestException(404,
'Proposal not found');
650 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
653 $updateRes = $this->propal->deleteLine($lineid,
$id);
654 if ($updateRes > 0) {
655 return $this->
get(
$id);
657 throw new RestException(405, $this->propal->error);
681 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
682 throw new RestException(403);
685 $result = $this->propal->fetch(
$id);
688 throw new RestException(404,
'Proposal not found');
691 if (!in_array($source, array(
'internal',
'external'),
true)) {
692 throw new RestException(500,
'Availables sources: internal OR external');
695 if ($source ==
'external' && !in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
696 throw new RestException(500,
'Availables external types: BILLING, SHIPPING OR CUSTOMER');
699 if ($source ==
'internal' && !in_array($type, array(
'SALESREPFOLL'),
true)) {
700 throw new RestException(500,
'Availables internal types: SALESREPFOLL');
704 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
707 $result = $this->propal->add_contact($contactid, $type, $source);
710 throw new RestException(500,
'Error when added the contact');
716 'message' =>
'Contact linked to the proposal'
739 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
740 throw new RestException(403);
743 $result = $this->propal->fetch(
$id);
746 throw new RestException(404,
'Proposal not found');
750 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
753 $contacts = $this->propal->liste_contact();
755 foreach ($contacts as $contact) {
756 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
757 $result = $this->propal->delete_contact($contact[
'rowid']);
760 throw new RestException(500,
'Error when deleted the contact');
781 public function put(
$id, $request_data =
null)
783 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
784 throw new RestException(403);
787 $result = $this->propal->fetch(
$id);
789 throw new RestException(404,
'Proposal not found');
793 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
795 foreach ($request_data as $field => $value) {
796 if ($field ==
'id') {
799 if ($field ===
'caller') {
801 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
804 if ($field ==
'array_options' && is_array($value)) {
805 foreach ($value as $index => $val) {
806 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
811 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
815 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
816 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
818 if (!empty($this->propal->fin_validite)) {
819 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
820 throw new RestException(500, $this->propal->error);
824 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
825 return $this->
get(
$id);
827 throw new RestException(500, $this->propal->error);
843 public function delete(
$id)
845 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
846 throw new RestException(403);
848 $result = $this->propal->fetch(
$id);
850 throw new RestException(404,
'Commercial Proposal not found');
854 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
857 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
858 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
864 'message' =>
'Commercial Proposal deleted'
883 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
884 throw new RestException(403);
886 $result = $this->propal->fetch(
$id);
888 throw new RestException(404,
'Proposal not found');
892 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
895 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
897 throw new RestException(304,
'Nothing done. May be object is already draft');
900 throw new RestException(500,
'Error : '.$this->propal->error);
903 $result = $this->propal->fetch(
$id);
905 throw new RestException(404,
'Proposal not found');
909 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
912 $this->propal->fetchObjectLinked();
941 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
942 throw new RestException(403);
944 $result = $this->propal->fetch(
$id);
946 throw new RestException(404,
'Commercial Proposal not found');
950 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
953 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
955 throw new RestException(304,
'Error nothing done. May be object is already validated');
958 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
961 $result = $this->propal->fetch(
$id);
963 throw new RestException(404,
'Commercial Proposal not found');
967 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
970 $this->propal->fetchObjectLinked();
991 public function close(
$id, $status, $note_private =
'', $notrigger = 0, $note_public =
'')
993 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
994 throw new RestException(403);
996 $result = $this->propal->fetch(
$id);
998 throw new RestException(404,
'Commercial Proposal not found');
1002 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1005 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
1007 throw new RestException(304,
'Error nothing done. May be object is already closed');
1010 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
1013 $result = $this->propal->fetch(
$id);
1015 throw new RestException(404,
'Proposal not found');
1019 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1022 $this->propal->fetchObjectLinked();
1041 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
1042 throw new RestException(403);
1044 $result = $this->propal->fetch(
$id);
1046 throw new RestException(404,
'Commercial Proposal not found');
1050 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1053 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
1055 throw new RestException(500,
'Error : '.$this->propal->error);
1058 $result = $this->propal->fetch(
$id);
1060 throw new RestException(404,
'Proposal not found');
1064 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1067 $this->propal->fetchObjectLinked();
1083 if ($data ===
null) {
1087 foreach (Proposals::$FIELDS as $field) {
1088 if (!isset($data[$field])) {
1089 throw new RestException(400,
"$field field missing");
1091 $propal[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
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 commercial proposal to draft.
put($id, $request_data=null)
Update a commercial proposal general fields (won't change lines of commercial proposal)
setinvoiced($id)
Set a commercial proposal to billed.
post($request_data=null)
Create a commercial proposal.
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 (link) a contact to a commercial proposal.
close($id, $status, $note_private='', $notrigger=0, $note_public='')
Close (accept or refuse) a commercial proposal.
getByRefExt($ref_ext, $contact_list=1)
Get a commercial proposal by ref_ext.
postLine($id, $request_data=null)
Add a line to a commercial proposal.
_cleanObjectDatas($object)
Clean sensible object datas.
postLines($id, $request_data=null)
Add lines to a commercial proposal.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of a commercial proposal.
deleteContact($id, $contactid, $type)
Remove (unlink) a contact from commercial proposal.
validate($id, $notrigger=0)
Validate a commercial proposal.
__construct()
Constructor.
getByRef($ref, $contact_list=1)
Get a commercial proposal by ref.
putLine($id, $lineid, $request_data=null)
Update a line of a 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.