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 $updateRes = $this->propal->updateline(
587 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
588 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
589 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
590 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
591 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
592 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
593 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
594 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
595 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
596 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
597 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
599 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
600 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
601 isset($request_data->label) ? $request_data->label : $propalline->label,
602 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
603 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
604 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
605 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
606 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
607 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
609 isset($request_data->rang) ? $request_data->rang : $propalline->rang
612 if ($updateRes > 0) {
613 $result = $this->
get(
$id);
614 unset($result->line);
636 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
637 throw new RestException(403);
640 $result = $this->propal->fetch(
$id);
642 throw new RestException(404,
'Proposal not found');
646 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
649 $updateRes = $this->propal->deleteLine($lineid,
$id);
650 if ($updateRes > 0) {
651 return $this->
get(
$id);
653 throw new RestException(405, $this->propal->error);
677 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
678 throw new RestException(403);
681 $result = $this->propal->fetch(
$id);
684 throw new RestException(404,
'Proposal not found');
687 if (!in_array($source, array(
'internal',
'external'),
true)) {
688 throw new RestException(500,
'Availables sources: internal OR external');
691 if ($source ==
'external' && !in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
692 throw new RestException(500,
'Availables external types: BILLING, SHIPPING OR CUSTOMER');
695 if ($source ==
'internal' && !in_array($type, array(
'SALESREPFOLL'),
true)) {
696 throw new RestException(500,
'Availables internal types: SALESREPFOLL');
700 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
703 $result = $this->propal->add_contact($contactid, $type, $source);
706 throw new RestException(500,
'Error when added the contact');
712 'message' =>
'Contact linked to the proposal'
735 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
736 throw new RestException(403);
739 $result = $this->propal->fetch(
$id);
742 throw new RestException(404,
'Proposal not found');
746 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
749 $contacts = $this->propal->liste_contact();
751 foreach ($contacts as $contact) {
752 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
753 $result = $this->propal->delete_contact($contact[
'rowid']);
756 throw new RestException(500,
'Error when deleted the contact');
777 public function put(
$id, $request_data =
null)
779 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
780 throw new RestException(403);
783 $result = $this->propal->fetch(
$id);
785 throw new RestException(404,
'Proposal not found');
789 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
791 foreach ($request_data as $field => $value) {
792 if ($field ==
'id') {
795 if ($field ===
'caller') {
797 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
800 if ($field ==
'array_options' && is_array($value)) {
801 foreach ($value as $index => $val) {
802 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
807 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
811 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
812 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
814 if (!empty($this->propal->fin_validite)) {
815 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
816 throw new RestException(500, $this->propal->error);
820 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
821 return $this->
get(
$id);
823 throw new RestException(500, $this->propal->error);
839 public function delete(
$id)
841 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
842 throw new RestException(403);
844 $result = $this->propal->fetch(
$id);
846 throw new RestException(404,
'Commercial Proposal not found');
850 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
853 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
854 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
860 'message' =>
'Commercial Proposal deleted'
879 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
880 throw new RestException(403);
882 $result = $this->propal->fetch(
$id);
884 throw new RestException(404,
'Proposal not found');
888 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
891 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
893 throw new RestException(304,
'Nothing done. May be object is already draft');
896 throw new RestException(500,
'Error : '.$this->propal->error);
899 $result = $this->propal->fetch(
$id);
901 throw new RestException(404,
'Proposal not found');
905 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
908 $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->valid(DolibarrApiAccess::$user, $notrigger);
951 throw new RestException(304,
'Error nothing done. May be object is already validated');
954 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
957 $result = $this->propal->fetch(
$id);
959 throw new RestException(404,
'Commercial Proposal not found');
963 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
966 $this->propal->fetchObjectLinked();
987 public function close(
$id, $status, $note_private =
'', $notrigger = 0, $note_public =
'')
989 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
990 throw new RestException(403);
992 $result = $this->propal->fetch(
$id);
994 throw new RestException(404,
'Commercial Proposal not found');
998 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1001 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
1003 throw new RestException(304,
'Error nothing done. May be object is already closed');
1006 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
1009 $result = $this->propal->fetch(
$id);
1011 throw new RestException(404,
'Proposal not found');
1015 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1018 $this->propal->fetchObjectLinked();
1037 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
1038 throw new RestException(403);
1040 $result = $this->propal->fetch(
$id);
1042 throw new RestException(404,
'Commercial Proposal not found');
1046 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1049 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
1051 throw new RestException(500,
'Error : '.$this->propal->error);
1054 $result = $this->propal->fetch(
$id);
1056 throw new RestException(404,
'Proposal not found');
1060 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1063 $this->propal->fetchObjectLinked();
1079 if ($data ===
null) {
1083 foreach (Proposals::$FIELDS as $field) {
1084 if (!isset($data[$field])) {
1085 throw new RestException(400,
"$field field missing");
1087 $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.