22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
38 public static $FIELDS = array(
54 $this->propal =
new Propal($this->db);
68 public function get($id, $contact_list = 1)
70 return $this->
_fetch($id,
'',
'', $contact_list);
86 public function getByRef($ref, $contact_list = 1)
88 return $this->
_fetch(
'', $ref,
'', $contact_list);
106 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
122 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
124 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
125 throw new RestException(401);
128 $result = $this->propal->fetch($id, $ref, $ref_ext);
130 throw new RestException(404,
'Commercial Proposal not found');
134 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
138 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
139 if (is_array($tmparray)) {
140 $this->propal->contacts_ids = $tmparray;
143 $this->propal->fetchObjectLinked();
162 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
166 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
167 throw new RestException(401);
173 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
177 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
178 $search_sale = DolibarrApiAccess::$user->id;
181 $sql =
"SELECT t.rowid";
182 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
183 $sql .=
", sc.fk_soc, sc.fk_user";
185 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
188 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
189 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
192 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
193 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
194 $sql .=
" AND t.fk_soc = sc.fk_soc";
197 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
199 if ($search_sale > 0) {
200 $sql .=
" AND t.rowid = sc.fk_soc";
203 if ($search_sale > 0) {
204 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
211 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
215 $sql .= $this->db->order($sortfield, $sortorder);
220 $offset = $limit * $page;
222 $sql .= $this->db->plimit($limit + 1, $offset);
226 $result = $this->db->query($sql);
229 $num = $this->db->num_rows($result);
230 $min = min($num, ($limit <= 0 ? $num : $limit));
233 $obj = $this->db->fetch_object($result);
234 $proposal_static =
new Propal($this->db);
235 if ($proposal_static->fetch($obj->rowid)) {
237 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
238 if (is_array($tmparray)) {
239 $proposal_static->contacts_ids = $tmparray;
246 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
258 public function post($request_data =
null)
260 if (!DolibarrApiAccess::$user->rights->propal->creer) {
261 throw new RestException(401,
"Insuffisant rights");
264 $result = $this->
_validate($request_data);
266 foreach ($request_data as $field => $value) {
267 if ($field ===
'caller') {
269 $this->propal->context[
'caller'] = $request_data[
'caller'];
273 $this->propal->$field = $value;
282 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
286 return ((
int) $this->propal->id);
301 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
302 throw new RestException(401);
305 $result = $this->propal->fetch($id);
307 throw new RestException(404,
'Commercial Proposal not found');
311 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
315 if (!empty($sqlfilters)) {
319 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
323 $this->propal->getLinesArray($sql);
325 foreach ($this->propal->lines as $line) {
341 public function postLine($id, $request_data =
null)
343 if (!DolibarrApiAccess::$user->rights->propal->creer) {
344 throw new RestException(401);
347 $result = $this->propal->fetch($id);
349 throw new RestException(404,
'Commercial Proposal not found');
353 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
356 $request_data = (object) $request_data;
358 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
359 $request_data->label =
sanitizeVal($request_data->label);
361 $updateRes = $this->propal->addline(
363 $request_data->subprice,
365 $request_data->tva_tx,
366 $request_data->localtax1_tx,
367 $request_data->localtax2_tx,
368 $request_data->fk_product,
369 $request_data->remise_percent,
370 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
371 $request_data->subprice,
372 $request_data->info_bits,
373 $request_data->product_type,
375 $request_data->special_code,
376 $request_data->fk_parent_line,
377 $request_data->fk_fournprice,
378 $request_data->pa_ht,
379 $request_data->label,
380 $request_data->date_start,
381 $request_data->date_end,
382 $request_data->array_options,
383 $request_data->fk_unit,
384 $request_data->origin,
385 $request_data->origin_id,
386 $request_data->multicurrency_subprice,
387 $request_data->fk_remise_except
390 if ($updateRes > 0) {
393 throw new RestException(400, $this->propal->error);
409 if (!DolibarrApiAccess::$user->rights->propal->creer) {
410 throw new RestException(401);
413 $result = $this->propal->fetch($id);
415 throw new RestException(404,
'Commercial Proposal not found');
419 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 foreach ($request_data as $TData) {
426 if (empty($TData[0])) {
427 $TData = array($TData);
430 foreach ($TData as $lineData) {
431 $line = (object) $lineData;
433 $updateRes = $this->propal->addline(
441 $line->remise_percent,
448 $line->fk_parent_line,
449 $line->fk_fournprice,
454 $line->array_options,
458 $line->multicurrency_subprice,
459 $line->fk_remise_except
462 if ($updateRes < 0) {
463 $errors[
'lineLabel'] = $line->label;
464 $errors[
'msg'] = $this->propal->errors;
468 if (empty($errors)) {
472 $this->db->rollback();
473 throw new RestException(400, implode(
", ", $errors));
487 public function putLine($id, $lineid, $request_data =
null)
489 if (!DolibarrApiAccess::$user->rights->propal->creer) {
490 throw new RestException(401);
493 $result = $this->propal->fetch($id);
495 throw new RestException(404,
'Proposal not found');
499 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
502 $request_data = (object) $request_data;
504 if (isset($request_data->desc)) {
505 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
507 if (isset($request_data->label)) {
508 $request_data->label =
sanitizeVal($request_data->label);
512 $result = $propalline->fetch($lineid);
514 throw new RestException(404,
'Proposal line not found');
517 $updateRes = $this->propal->updateline(
519 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
520 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
521 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
522 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
523 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
524 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
525 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
526 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
527 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
528 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
529 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
531 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
532 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
533 isset($request_data->label) ? $request_data->label : $propalline->label,
534 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
535 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
536 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
537 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
538 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
539 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
541 isset($request_data->rang) ? $request_data->rang : $propalline->rang
544 if ($updateRes > 0) {
545 $result = $this->
get($id);
546 unset($result->line);
567 if (!DolibarrApiAccess::$user->rights->propal->creer) {
568 throw new RestException(401);
571 $result = $this->propal->fetch($id);
573 throw new RestException(404,
'Proposal not found');
577 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
580 $updateRes = $this->propal->deleteline($lineid, $id);
581 if ($updateRes > 0) {
582 return $this->
get($id);
584 throw new RestException(405, $this->propal->error);
603 if (!DolibarrApiAccess::$user->rights->propal->creer) {
604 throw new RestException(401);
607 $result = $this->propal->fetch($id);
610 throw new RestException(404,
'Proposal not found');
613 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
614 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
618 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
621 $result = $this->propal->add_contact($contactid, $type,
'external');
624 throw new RestException(500,
'Error when added the contact');
630 'message' =>
'Contact linked to the proposal'
651 if (!DolibarrApiAccess::$user->rights->propal->creer) {
652 throw new RestException(401);
655 $result = $this->propal->fetch($id);
658 throw new RestException(404,
'Proposal not found');
662 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 $contacts = $this->propal->liste_contact();
667 foreach ($contacts as $contact) {
668 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
669 $result = $this->propal->delete_contact($contact[
'rowid']);
672 throw new RestException(500,
'Error when deleted the contact');
687 public function put($id, $request_data =
null)
689 if (!DolibarrApiAccess::$user->rights->propal->creer) {
690 throw new RestException(401);
693 $result = $this->propal->fetch($id);
695 throw new RestException(404,
'Proposal not found');
699 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
701 foreach ($request_data as $field => $value) {
702 if ($field ==
'id') {
705 if ($field ===
'caller') {
707 $this->propal->context[
'caller'] = $request_data[
'caller'];
711 $this->propal->$field = $value;
715 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
716 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
718 if (!empty($this->propal->fin_validite)) {
719 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
720 throw new RestException(500, $this->propal->error);
724 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
725 return $this->
get($id);
727 throw new RestException(500, $this->propal->error);
737 public function delete($id)
739 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
740 throw new RestException(401);
742 $result = $this->propal->fetch($id);
744 throw new RestException(404,
'Commercial Proposal not found');
748 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
751 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
752 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
758 'message' =>
'Commercial Proposal deleted'
773 if (!DolibarrApiAccess::$user->rights->propal->creer) {
774 throw new RestException(401);
776 $result = $this->propal->fetch($id);
778 throw new RestException(404,
'Proposal not found');
782 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
785 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
787 throw new RestException(304,
'Nothing done. May be object is already draft');
790 throw new RestException(500,
'Error : '.$this->propal->error);
793 $result = $this->propal->fetch($id);
795 throw new RestException(404,
'Proposal not found');
799 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 $this->propal->fetchObjectLinked();
829 if (!DolibarrApiAccess::$user->rights->propal->creer) {
830 throw new RestException(401);
832 $result = $this->propal->fetch($id);
834 throw new RestException(404,
'Commercial Proposal not found');
838 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
841 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
843 throw new RestException(304,
'Error nothing done. May be object is already validated');
846 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
849 $result = $this->propal->fetch($id);
851 throw new RestException(404,
'Commercial Proposal not found');
855 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
858 $this->propal->fetchObjectLinked();
874 public function close($id, $status, $note_private =
'', $notrigger = 0)
876 if (!DolibarrApiAccess::$user->rights->propal->creer) {
877 throw new RestException(401);
879 $result = $this->propal->fetch($id);
881 throw new RestException(404,
'Commercial Proposal not found');
885 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
888 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
890 throw new RestException(304,
'Error nothing done. May be object is already closed');
893 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
896 $result = $this->propal->fetch($id);
898 throw new RestException(404,
'Proposal not found');
902 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
905 $this->propal->fetchObjectLinked();
920 if (!DolibarrApiAccess::$user->rights->propal->creer) {
921 throw new RestException(401);
923 $result = $this->propal->fetch($id);
925 throw new RestException(404,
'Commercial Proposal not found');
929 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
932 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
934 throw new RestException(500,
'Error : '.$this->propal->error);
937 $result = $this->propal->fetch($id);
939 throw new RestException(404,
'Proposal not found');
943 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
946 $this->propal->fetchObjectLinked();
963 foreach (Proposals::$FIELDS as $field) {
964 if (!isset($data[$field])) {
965 throw new RestException(400,
"$field field missing");
967 $propal[$field] = $data[$field];
983 $object = parent::_cleanObjectDatas($object);
985 unset($object->note);
986 unset($object->name);
987 unset($object->lastname);
988 unset($object->firstname);
989 unset($object->civility_id);
990 unset($object->address);
_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.
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)
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List commercial proposals.
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.