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();
165 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
167 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
168 throw new RestException(403);
174 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
178 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
179 $search_sale = DolibarrApiAccess::$user->id;
182 $sql =
"SELECT t.rowid";
183 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t";
184 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
185 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
187 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
190 if ($search_sale && $search_sale !=
'-1') {
191 if ($search_sale == -2) {
192 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
193 } elseif ($search_sale > 0) {
194 $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).
")";
202 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
207 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
209 $sql .= $this->db->order($sortfield, $sortorder);
214 $offset = $limit * $page;
216 $sql .= $this->db->plimit($limit + 1, $offset);
220 $result = $this->db->query($sql);
223 $num = $this->db->num_rows($result);
224 $min = min($num, ($limit <= 0 ? $num : $limit));
227 $obj = $this->db->fetch_object($result);
228 $proposal_static =
new Propal($this->db);
229 if ($proposal_static->fetch($obj->rowid)) {
231 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
232 if (is_array($tmparray)) {
233 $proposal_static->contacts_ids = $tmparray;
240 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
244 if ($pagination_data) {
245 $totalsResult = $this->db->query($sqlTotals);
246 $total = $this->db->fetch_object($totalsResult)->total;
251 $obj_ret[
'data'] = $tmp;
252 $obj_ret[
'pagination'] = [
253 'total' => (int) $total,
255 'page_count' => ceil((
int) $total / $limit),
269 public function post($request_data =
null)
271 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
272 throw new RestException(403,
"Insuffisant rights");
275 $result = $this->
_validate($request_data);
277 foreach ($request_data as $field => $value) {
278 if ($field ===
'caller') {
280 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
284 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
293 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
294 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
297 return ((
int) $this->propal->id);
312 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
313 throw new RestException(403);
316 $result = $this->propal->fetch(
$id);
318 throw new RestException(404,
'Commercial Proposal not found');
322 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
326 if (!empty($sqlfilters)) {
330 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
334 $this->propal->getLinesArray($sql);
336 foreach ($this->propal->lines as $line) {
354 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
355 throw new RestException(403);
358 $result = $this->propal->fetch(
$id);
360 throw new RestException(404,
'Commercial Proposal not found');
364 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
367 $request_data = (object) $request_data;
369 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
370 $request_data->label =
sanitizeVal($request_data->label);
372 $updateRes = $this->propal->addline(
374 $request_data->subprice,
376 $request_data->tva_tx,
377 $request_data->localtax1_tx,
378 $request_data->localtax2_tx,
379 $request_data->fk_product,
380 $request_data->remise_percent,
381 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
382 $request_data->subprice,
383 $request_data->info_bits,
384 $request_data->product_type,
386 $request_data->special_code,
387 $request_data->fk_parent_line,
388 $request_data->fk_fournprice,
389 $request_data->pa_ht,
390 $request_data->label,
391 $request_data->date_start,
392 $request_data->date_end,
393 $request_data->array_options,
394 $request_data->fk_unit,
395 $request_data->origin,
396 $request_data->origin_id,
397 $request_data->multicurrency_subprice,
398 $request_data->fk_remise_except
401 if ($updateRes > 0) {
404 throw new RestException(400, $this->propal->error);
420 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
421 throw new RestException(403);
424 $result = $this->propal->fetch(
$id);
426 throw new RestException(404,
'Commercial Proposal not found');
430 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
437 foreach ($request_data as $TData) {
438 if (empty($TData[0])) {
439 $TData = array($TData);
442 foreach ($TData as $lineData) {
443 $line = (object) $lineData;
445 $updateRes = $this->propal->addline(
453 $line->remise_percent,
460 $line->fk_parent_line,
461 $line->fk_fournprice,
466 $line->array_options,
470 $line->multicurrency_subprice,
471 $line->fk_remise_except
474 if ($updateRes < 0) {
475 $errors[
'lineLabel'] = $line->label;
476 $errors[
'msg'] = $this->propal->errors;
480 if (empty($errors)) {
484 $this->db->rollback();
485 throw new RestException(400, implode(
", ", $errors));
501 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
502 throw new RestException(403);
505 $result = $this->propal->fetch(
$id);
507 throw new RestException(404,
'Proposal not found');
511 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
514 $request_data = (object) $request_data;
516 if (isset($request_data->desc)) {
517 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
519 if (isset($request_data->label)) {
520 $request_data->label =
sanitizeVal($request_data->label);
524 $result = $propalline->fetch($lineid);
526 throw new RestException(404,
'Proposal line not found');
529 $updateRes = $this->propal->updateline(
531 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
532 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
533 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
534 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
535 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
536 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
537 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
538 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
539 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
540 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
541 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
543 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
544 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
545 isset($request_data->label) ? $request_data->label : $propalline->label,
546 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
547 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
548 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
549 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
550 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
551 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
553 isset($request_data->rang) ? $request_data->rang : $propalline->rang
556 if ($updateRes > 0) {
557 $result = $this->
get(
$id);
558 unset($result->line);
579 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
580 throw new RestException(403);
583 $result = $this->propal->fetch(
$id);
585 throw new RestException(404,
'Proposal not found');
589 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
592 $updateRes = $this->propal->deleteLine($lineid,
$id);
593 if ($updateRes > 0) {
594 return $this->
get(
$id);
596 throw new RestException(405, $this->propal->error);
616 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
617 throw new RestException(403);
620 $result = $this->propal->fetch(
$id);
623 throw new RestException(404,
'Proposal not found');
626 if (!in_array($source, array(
'internal',
'external'),
true)) {
627 throw new RestException(500,
'Availables sources: internal OR external');
630 if ($source ==
'external' && !in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
631 throw new RestException(500,
'Availables external types: BILLING, SHIPPING OR CUSTOMER');
634 if ($source ==
'internal' && !in_array($type, array(
'SALESREPFOLL'),
true)) {
635 throw new RestException(500,
'Availables internal types: SALESREPFOLL');
639 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 $result = $this->propal->add_contact($contactid, $type, $source);
645 throw new RestException(500,
'Error when added the contact');
651 'message' =>
'Contact linked to the proposal'
672 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
673 throw new RestException(403);
676 $result = $this->propal->fetch(
$id);
679 throw new RestException(404,
'Proposal not found');
683 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
686 $contacts = $this->propal->liste_contact();
688 foreach ($contacts as $contact) {
689 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
690 $result = $this->propal->delete_contact($contact[
'rowid']);
693 throw new RestException(500,
'Error when deleted the contact');
708 public function put(
$id, $request_data =
null)
710 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
711 throw new RestException(403);
714 $result = $this->propal->fetch(
$id);
716 throw new RestException(404,
'Proposal not found');
720 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
722 foreach ($request_data as $field => $value) {
723 if ($field ==
'id') {
726 if ($field ===
'caller') {
728 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
731 if ($field ==
'array_options' && is_array($value)) {
732 foreach ($value as $index => $val) {
733 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
738 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
742 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
743 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
745 if (!empty($this->propal->fin_validite)) {
746 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
747 throw new RestException(500, $this->propal->error);
751 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
752 return $this->
get(
$id);
754 throw new RestException(500, $this->propal->error);
764 public function delete(
$id)
766 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
767 throw new RestException(403);
769 $result = $this->propal->fetch(
$id);
771 throw new RestException(404,
'Commercial Proposal not found');
775 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
779 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
785 'message' =>
'Commercial Proposal deleted'
800 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
801 throw new RestException(403);
803 $result = $this->propal->fetch(
$id);
805 throw new RestException(404,
'Proposal not found');
809 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
812 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
814 throw new RestException(304,
'Nothing done. May be object is already draft');
817 throw new RestException(500,
'Error : '.$this->propal->error);
820 $result = $this->propal->fetch(
$id);
822 throw new RestException(404,
'Proposal not found');
826 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
829 $this->propal->fetchObjectLinked();
856 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
857 throw new RestException(403);
859 $result = $this->propal->fetch(
$id);
861 throw new RestException(404,
'Commercial Proposal not found');
865 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
868 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
870 throw new RestException(304,
'Error nothing done. May be object is already validated');
873 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
876 $result = $this->propal->fetch(
$id);
878 throw new RestException(404,
'Commercial Proposal not found');
882 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
885 $this->propal->fetchObjectLinked();
902 public function close(
$id, $status, $note_private =
'', $notrigger = 0, $note_public =
'')
904 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
905 throw new RestException(403);
907 $result = $this->propal->fetch(
$id);
909 throw new RestException(404,
'Commercial Proposal not found');
913 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
916 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger, $note_public);
918 throw new RestException(304,
'Error nothing done. May be object is already closed');
921 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
924 $result = $this->propal->fetch(
$id);
926 throw new RestException(404,
'Proposal not found');
930 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
933 $this->propal->fetchObjectLinked();
948 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
949 throw new RestException(403);
951 $result = $this->propal->fetch(
$id);
953 throw new RestException(404,
'Commercial Proposal not found');
957 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
960 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
962 throw new RestException(500,
'Error : '.$this->propal->error);
965 $result = $this->propal->fetch(
$id);
967 throw new RestException(404,
'Proposal not found');
971 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
974 $this->propal->fetchObjectLinked();
991 foreach (Proposals::$FIELDS as $field) {
992 if (!isset($data[$field])) {
993 throw new RestException(400,
"$field field missing");
995 $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.
setinvoiced($id)
Set a commercial proposal billed.
post($request_data=null)
Create commercial proposal object.
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.