23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
39 public static $FIELDS = array(
55 $this->propal =
new Propal($this->db);
69 public function get($id, $contact_list = 1)
71 return $this->
_fetch($id,
'',
'', $contact_list);
87 public function getByRef($ref, $contact_list = 1)
89 return $this->
_fetch(
'', $ref,
'', $contact_list);
107 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
123 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
125 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
126 throw new RestException(403);
129 $result = $this->propal->fetch($id, $ref, $ref_ext);
131 throw new RestException(404,
'Commercial Proposal not found');
135 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
139 $tmparray = $this->propal->liste_contact(-1,
'external', $contact_list);
140 if (is_array($tmparray)) {
141 $this->propal->contacts_ids = $tmparray;
144 $this->propal->fetchObjectLinked();
163 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
165 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
166 throw new RestException(403);
172 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
176 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
177 $search_sale = DolibarrApiAccess::$user->id;
180 $sql =
"SELECT t.rowid";
181 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal AS t";
182 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"propal_extrafields AS ef ON (ef.fk_object = t.rowid)";
183 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
185 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
188 if ($search_sale && $search_sale !=
'-1') {
189 if ($search_sale == -2) {
190 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
191 } elseif ($search_sale > 0) {
192 $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).
")";
200 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
204 $sql .= $this->db->order($sortfield, $sortorder);
209 $offset = $limit * $page;
211 $sql .= $this->db->plimit($limit + 1, $offset);
215 $result = $this->db->query($sql);
218 $num = $this->db->num_rows($result);
219 $min = min($num, ($limit <= 0 ? $num : $limit));
222 $obj = $this->db->fetch_object($result);
223 $proposal_static =
new Propal($this->db);
224 if ($proposal_static->fetch($obj->rowid)) {
226 $tmparray = $proposal_static->liste_contact(-1,
'external', 1);
227 if (is_array($tmparray)) {
228 $proposal_static->contacts_ids = $tmparray;
235 throw new RestException(503,
'Error when retrieve propal list : '.$this->db->lasterror());
247 public function post($request_data =
null)
249 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
250 throw new RestException(403,
"Insuffisant rights");
253 $result = $this->
_validate($request_data);
255 foreach ($request_data as $field => $value) {
256 if ($field ===
'caller') {
258 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
262 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
271 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
272 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
275 return ((
int) $this->propal->id);
290 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
291 throw new RestException(403);
294 $result = $this->propal->fetch($id);
296 throw new RestException(404,
'Commercial Proposal not found');
300 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
304 if (!empty($sqlfilters)) {
308 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
312 $this->propal->getLinesArray($sql);
314 foreach ($this->propal->lines as $line) {
330 public function postLine($id, $request_data =
null)
332 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
333 throw new RestException(403);
336 $result = $this->propal->fetch($id);
338 throw new RestException(404,
'Commercial Proposal not found');
342 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
345 $request_data = (object) $request_data;
347 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
348 $request_data->label =
sanitizeVal($request_data->label);
350 $updateRes = $this->propal->addline(
352 $request_data->subprice,
354 $request_data->tva_tx,
355 $request_data->localtax1_tx,
356 $request_data->localtax2_tx,
357 $request_data->fk_product,
358 $request_data->remise_percent,
359 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
360 $request_data->subprice,
361 $request_data->info_bits,
362 $request_data->product_type,
364 $request_data->special_code,
365 $request_data->fk_parent_line,
366 $request_data->fk_fournprice,
367 $request_data->pa_ht,
368 $request_data->label,
369 $request_data->date_start,
370 $request_data->date_end,
371 $request_data->array_options,
372 $request_data->fk_unit,
373 $request_data->origin,
374 $request_data->origin_id,
375 $request_data->multicurrency_subprice,
376 $request_data->fk_remise_except
379 if ($updateRes > 0) {
382 throw new RestException(400, $this->propal->error);
398 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
399 throw new RestException(403);
402 $result = $this->propal->fetch($id);
404 throw new RestException(404,
'Commercial Proposal not found');
408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
415 foreach ($request_data as $TData) {
416 if (empty($TData[0])) {
417 $TData = array($TData);
420 foreach ($TData as $lineData) {
421 $line = (object) $lineData;
423 $updateRes = $this->propal->addline(
431 $line->remise_percent,
438 $line->fk_parent_line,
439 $line->fk_fournprice,
444 $line->array_options,
448 $line->multicurrency_subprice,
449 $line->fk_remise_except
452 if ($updateRes < 0) {
453 $errors[
'lineLabel'] = $line->label;
454 $errors[
'msg'] = $this->propal->errors;
458 if (empty($errors)) {
462 $this->db->rollback();
463 throw new RestException(400, implode(
", ", $errors));
477 public function putLine($id, $lineid, $request_data =
null)
479 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
480 throw new RestException(403);
483 $result = $this->propal->fetch($id);
485 throw new RestException(404,
'Proposal not found');
489 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
492 $request_data = (object) $request_data;
494 if (isset($request_data->desc)) {
495 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
497 if (isset($request_data->label)) {
498 $request_data->label =
sanitizeVal($request_data->label);
502 $result = $propalline->fetch($lineid);
504 throw new RestException(404,
'Proposal line not found');
507 $updateRes = $this->propal->updateline(
509 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
510 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
511 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
512 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
513 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
514 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
515 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
516 isset($request_data->price_base_type) ? $request_data->price_base_type :
'HT',
517 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
518 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
519 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
521 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
522 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
523 isset($request_data->label) ? $request_data->label : $propalline->label,
524 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
525 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
526 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
527 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
528 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
529 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
531 isset($request_data->rang) ? $request_data->rang : $propalline->rang
534 if ($updateRes > 0) {
535 $result = $this->
get($id);
536 unset($result->line);
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 $updateRes = $this->propal->deleteLine($lineid, $id);
571 if ($updateRes > 0) {
572 return $this->
get($id);
574 throw new RestException(405, $this->propal->error);
592 public function postContact($id, $contactid, $type, $source =
'external')
594 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
595 throw new RestException(403);
598 $result = $this->propal->fetch($id);
601 throw new RestException(404,
'Proposal not found');
604 if (!in_array($source, array(
'internal',
'external'),
true)) {
605 throw new RestException(500,
'Availables sources: internal OR external');
608 if ($source ==
'external' && !in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
609 throw new RestException(500,
'Availables external types: BILLING, SHIPPING OR CUSTOMER');
612 if ($source ==
'internal' && !in_array($type, array(
'SALESREPFOLL'),
true)) {
613 throw new RestException(500,
'Availables internal types: SALESREPFOLL');
617 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
620 $result = $this->propal->add_contact($contactid, $type, $source);
623 throw new RestException(500,
'Error when added the contact');
629 'message' =>
'Contact linked to the proposal'
650 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
651 throw new RestException(403);
654 $result = $this->propal->fetch($id);
657 throw new RestException(404,
'Proposal not found');
661 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
664 $contacts = $this->propal->liste_contact();
666 foreach ($contacts as $contact) {
667 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
668 $result = $this->propal->delete_contact($contact[
'rowid']);
671 throw new RestException(500,
'Error when deleted the contact');
686 public function put($id, $request_data =
null)
688 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
689 throw new RestException(403);
692 $result = $this->propal->fetch($id);
694 throw new RestException(404,
'Proposal not found');
698 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
700 foreach ($request_data as $field => $value) {
701 if ($field ==
'id') {
704 if ($field ===
'caller') {
706 $this->propal->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
709 if ($field ==
'array_options' && is_array($value)) {
710 foreach ($value as $index => $val) {
711 $this->propal->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->propal);
716 $this->propal->$field = $this->
_checkValForAPI($field, $value, $this->propal);
720 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
721 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
723 if (!empty($this->propal->fin_validite)) {
724 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
725 throw new RestException(500, $this->propal->error);
729 if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
730 return $this->
get($id);
732 throw new RestException(500, $this->propal->error);
742 public function delete($id)
744 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'supprimer')) {
745 throw new RestException(403);
747 $result = $this->propal->fetch($id);
749 throw new RestException(404,
'Commercial Proposal not found');
753 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
756 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
757 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
763 'message' =>
'Commercial Proposal deleted'
778 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
779 throw new RestException(403);
781 $result = $this->propal->fetch($id);
783 throw new RestException(404,
'Proposal not found');
787 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
792 throw new RestException(304,
'Nothing done. May be object is already draft');
795 throw new RestException(500,
'Error : '.$this->propal->error);
798 $result = $this->propal->fetch($id);
800 throw new RestException(404,
'Proposal not found');
804 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
807 $this->propal->fetchObjectLinked();
834 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
835 throw new RestException(403);
837 $result = $this->propal->fetch($id);
839 throw new RestException(404,
'Commercial Proposal not found');
843 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
848 throw new RestException(304,
'Error nothing done. May be object is already validated');
851 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
854 $result = $this->propal->fetch($id);
856 throw new RestException(404,
'Commercial Proposal not found');
860 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
863 $this->propal->fetchObjectLinked();
879 public function close($id, $status, $note_private =
'', $notrigger = 0)
881 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
882 throw new RestException(403);
884 $result = $this->propal->fetch($id);
886 throw new RestException(404,
'Commercial Proposal not found');
890 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
895 throw new RestException(304,
'Error nothing done. May be object is already closed');
898 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
901 $result = $this->propal->fetch($id);
903 throw new RestException(404,
'Proposal not found');
907 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
910 $this->propal->fetchObjectLinked();
925 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'creer')) {
926 throw new RestException(403);
928 $result = $this->propal->fetch($id);
930 throw new RestException(404,
'Commercial Proposal not found');
934 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
937 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
939 throw new RestException(500,
'Error : '.$this->propal->error);
942 $result = $this->propal->fetch($id);
944 throw new RestException(404,
'Proposal not found');
948 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 $this->propal->fetchObjectLinked();
968 foreach (Proposals::$FIELDS as $field) {
969 if (!isset($data[$field])) {
970 throw new RestException(400,
"$field field missing");
972 $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)
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, $source='external')
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.