20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
36 static $FIELDS = array(
52 $this->propal =
new Propal($this->
db);
66 public function get($id, $contact_list = 1)
68 return $this->
_fetch($id,
'',
'', $contact_list);
84 public function getByRef($ref, $contact_list = 1)
86 return $this->
_fetch(
'', $ref,
'', $contact_list);
104 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
120 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
122 if (!DolibarrApiAccess::$user->rights->propal->lire) {
123 throw new RestException(401);
126 $result = $this->propal->fetch($id, $ref, $ref_ext);
128 throw new RestException(404,
'Commercial Proposal not found');
132 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
136 $this->propal->contacts_ids = $this->propal->liste_contact(-1,
'external', $contact_list);
137 $this->propal->fetchObjectLinked();
154 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
161 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
165 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
167 $sql =
"SELECT t.rowid";
168 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
169 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as t";
171 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
173 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
174 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
175 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
176 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
178 if ($search_sale > 0)
180 $sql .=
" AND sc.fk_user = ".$search_sale;
187 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
189 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
190 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
193 $sql .= $this->
db->order($sortfield, $sortorder);
199 $offset = $limit * $page;
201 $sql .= $this->
db->plimit($limit + 1, $offset);
205 $result = $this->
db->query($sql);
209 $num = $this->
db->num_rows($result);
210 $min = min($num, ($limit <= 0 ? $num : $limit));
214 $obj = $this->
db->fetch_object($result);
215 $proposal_static =
new Propal($this->
db);
216 if ($proposal_static->fetch($obj->rowid)) {
218 $proposal_static->contacts_ids = $proposal_static->liste_contact(-1,
'external', 1);
224 throw new RestException(503,
'Error when retrieve propal list : '.$this->
db->lasterror());
226 if (!count($obj_ret)) {
227 throw new RestException(404,
'No proposal found');
238 public function post($request_data = null)
240 if (!DolibarrApiAccess::$user->rights->propal->creer) {
241 throw new RestException(401,
"Insuffisant rights");
244 $result = $this->
_validate($request_data);
246 foreach ($request_data as $field => $value) {
247 $this->propal->$field = $value;
256 if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
257 throw new RestException(500,
"Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
260 return $this->propal->id;
274 if (!DolibarrApiAccess::$user->rights->propal->lire) {
275 throw new RestException(401);
278 $result = $this->propal->fetch($id);
280 throw new RestException(404,
'Commercial Proposal not found');
284 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
286 $this->propal->getLinesArray();
288 foreach ($this->propal->lines as $line) {
304 public function postLine($id, $request_data = null)
306 if (!DolibarrApiAccess::$user->rights->propal->creer) {
307 throw new RestException(401);
310 $result = $this->propal->fetch($id);
312 throw new RestException(404,
'Commercial Proposal not found');
316 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
319 $request_data = (object) $request_data;
321 $updateRes = $this->propal->addline(
323 $request_data->subprice,
325 $request_data->tva_tx,
326 $request_data->localtax1_tx,
327 $request_data->localtax2_tx,
328 $request_data->fk_product,
329 $request_data->remise_percent,
332 $request_data->info_bits,
333 $request_data->product_type,
335 $request_data->special_code,
336 $request_data->fk_parent_line,
337 $request_data->fk_fournprice,
338 $request_data->pa_ht,
339 $request_data->label,
340 $request_data->date_start,
341 $request_data->date_end,
342 $request_data->array_options,
343 $request_data->fk_unit,
344 $request_data->origin,
345 $request_data->origin_id,
346 $request_data->multicurrency_subprice,
347 $request_data->fk_remise_except
350 if ($updateRes > 0) {
353 throw new RestException(400, $this->propal->error);
368 public function putLine($id, $lineid, $request_data = null)
370 if (!DolibarrApiAccess::$user->rights->propal->creer) {
371 throw new RestException(401);
374 $result = $this->propal->fetch($id);
376 throw new RestException(404,
'Proposal not found');
380 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 $request_data = (object) $request_data;
386 $result = $propalline->fetch($lineid);
388 throw new RestException(404,
'Proposal line not found');
391 $updateRes = $this->propal->updateline(
393 isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
394 isset($request_data->qty) ? $request_data->qty : $propalline->qty,
395 isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
396 isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
397 isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
398 isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
399 isset($request_data->desc) ? $request_data->desc : $propalline->desc,
401 isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
402 isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
403 isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
405 isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
406 isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
407 isset($request_data->label) ? $request_data->label : $propalline->label,
408 isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
409 isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
410 isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
411 isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
412 isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
413 isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice
416 if ($updateRes > 0) {
417 $result = $this->
get($id);
418 unset($result->line);
440 if (!DolibarrApiAccess::$user->rights->propal->creer) {
441 throw new RestException(401);
444 $result = $this->propal->fetch($id);
446 throw new RestException(404,
'Proposal not found');
450 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 $updateRes = $this->propal->deleteline($lineid);
456 if ($updateRes > 0) {
457 return $this->
get($id);
459 throw new RestException(405, $this->propal->error);
479 if (!DolibarrApiAccess::$user->rights->propal->creer) {
480 throw new RestException(401);
483 $result = $this->propal->fetch($id);
486 throw new RestException(404,
'Proposal not found');
489 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
490 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
494 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 $result = $this->propal->add_contact($contactid, $type,
'external');
500 throw new RestException(500,
'Error when added the contact');
503 return $this->propal;
523 if (!DolibarrApiAccess::$user->rights->propal->creer) {
524 throw new RestException(401);
527 $result = $this->propal->fetch($id);
530 throw new RestException(404,
'Proposal not found');
534 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
537 $contacts = $this->invoice->liste_contact();
539 foreach ($contacts as $contact) {
540 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
541 $result = $this->propal->delete_contact($contact[
'rowid']);
544 throw new RestException(500,
'Error when deleted the contact');
560 public function put($id, $request_data = null)
562 if (!DolibarrApiAccess::$user->rights->propal->creer) {
563 throw new RestException(401);
566 $result = $this->propal->fetch($id);
568 throw new RestException(404,
'Proposal not found');
572 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
574 foreach ($request_data as $field => $value) {
575 if ($field ==
'id')
continue;
576 $this->propal->$field = $value;
580 if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation))
582 $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
584 if (!empty($this->propal->fin_validite))
586 if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0)
588 throw new RestException(500, $this->propal->error);
592 if ($this->propal->update(DolibarrApiAccess::$user) > 0)
594 return $this->
get($id);
596 throw new RestException(500, $this->propal->error);
607 public function delete($id)
609 if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
610 throw new RestException(401);
612 $result = $this->propal->fetch($id);
614 throw new RestException(404,
'Commercial Proposal not found');
618 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
621 if (!$this->propal->delete(DolibarrApiAccess::$user)) {
622 throw new RestException(500,
'Error when delete Commercial Proposal : '.$this->propal->error);
628 'message' =>
'Commercial Proposal deleted'
644 if (!DolibarrApiAccess::$user->rights->propal->creer) {
645 throw new RestException(401);
647 $result = $this->propal->fetch($id);
649 throw new RestException(404,
'Proposal not found');
653 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
656 $result = $this->propal->setDraft(DolibarrApiAccess::$user);
658 throw new RestException(304,
'Nothing done. May be object is already draft');
661 throw new RestException(500,
'Error : '.$this->propal->error);
664 $result = $this->propal->fetch($id);
666 throw new RestException(404,
'Proposal not found');
670 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
673 $this->propal->fetchObjectLinked();
701 if (!DolibarrApiAccess::$user->rights->propal->creer) {
702 throw new RestException(401);
704 $result = $this->propal->fetch($id);
706 throw new RestException(404,
'Commercial Proposal not found');
710 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
715 throw new RestException(304,
'Error nothing done. May be object is already validated');
718 throw new RestException(500,
'Error when validating Commercial Proposal: '.$this->propal->error);
721 $result = $this->propal->fetch($id);
723 throw new RestException(404,
'Commercial Proposal not found');
727 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 $this->propal->fetchObjectLinked();
747 public function close($id, $status, $note_private =
'', $notrigger = 0)
749 if (!DolibarrApiAccess::$user->rights->propal->creer) {
750 throw new RestException(401);
752 $result = $this->propal->fetch($id);
754 throw new RestException(404,
'Commercial Proposal not found');
758 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
761 $result = $this->propal->cloture(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
763 throw new RestException(304,
'Error nothing done. May be object is already closed');
766 throw new RestException(500,
'Error when closing Commercial Proposal: '.$this->propal->error);
769 $result = $this->propal->fetch($id);
771 throw new RestException(404,
'Proposal not found');
775 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 $this->propal->fetchObjectLinked();
794 if (!DolibarrApiAccess::$user->rights->propal->creer) {
795 throw new RestException(401);
797 $result = $this->propal->fetch($id);
799 throw new RestException(404,
'Commercial Proposal not found');
803 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
806 $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
808 throw new RestException(500,
'Error : '.$this->propal->error);
811 $result = $this->propal->fetch($id);
813 throw new RestException(404,
'Proposal not found');
817 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
820 $this->propal->fetchObjectLinked();
836 foreach (Proposals::$FIELDS as $field) {
837 if (!isset($data[$field]))
838 throw new RestException(400,
"$field field missing");
839 $propal[$field] = $data[$field];
855 $object = parent::_cleanObjectDatas($object);
857 unset($object->note);
858 unset($object->name);
859 unset($object->lastname);
860 unset($object->firstname);
861 unset($object->civility_id);
862 unset($object->address);
_validate($data)
Validate fields before create or update object.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an proposal object by ref_ext.
_fetch($id, $ref= '', $ref_ext= '', $contact_list=1)
Get properties of an proposal object.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List commercial proposals.
setinvoiced($id)
Set a commercial proposal billed.
getLines($id)
Get lines of a commercial proposal.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
deleteLine($id, $lineid)
Delete a line of given commercial proposal.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
postContact($id, $contactid, $type)
Add a contact type of given commercial proposal.
postLine($id, $request_data=null)
Add a line to given commercial proposal.
Class to manage commercial proposal lines.
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.
close($id, $status, $note_private= '', $notrigger=0)
Close (Accept or refuse) a quote / commercial proposal.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
post($request_data=null)
Create commercial proposal object.
deleteContact($id, $contactid, $type)
Delete a contact type of given commercial proposal.
put($id, $request_data=null)
Update commercial proposal general fields (won't touch lines of commercial proposal) ...
_cleanObjectDatas($object)
Clean sensible object datas.
validate($id, $notrigger=0)
Validate a commercial proposal.
Class to manage proposals.
__construct()
Constructor.
settodraft($id)
Set a proposal to draft.