21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
38 public static $FIELDS = array(
55 $this->contract =
new Contrat($this->db);
72 public function get(
$id, $properties =
'', $withLines =
true)
74 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
75 throw new RestException(403);
78 throw new RestException(400,
'No contract with id=0 can exist');
80 $result = $this->contract->fetch(
$id);
82 throw new RestException(404,
'Contract not found');
86 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
89 $this->contract->fetchObjectLinked();
92 unset($this->contract->lines);
120 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $withLines =
true)
124 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
125 throw new RestException(403);
131 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
135 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
136 $search_sale = DolibarrApiAccess::$user->id;
139 $sql =
"SELECT t.rowid";
140 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t";
141 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
142 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
143 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
145 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
148 if ($search_sale && $search_sale !=
'-1') {
149 if ($search_sale == -2) {
150 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
151 } elseif ($search_sale > 0) {
152 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
160 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
165 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
167 $sql .= $this->db->order($sortfield, $sortorder);
172 $offset = $limit * $page;
174 $sql .= $this->db->plimit($limit + 1, $offset);
177 $result = $this->db->query($sql);
180 $num = $this->db->num_rows($result);
181 $min = min($num, ($limit <= 0 ? $num : $limit));
184 $obj = $this->db->fetch_object($result);
185 $contrat_static =
new Contrat($this->db);
186 if ($contrat_static->fetch($obj->rowid)) {
188 unset($contrat_static->lines);
195 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
199 if ($pagination_data) {
200 $totalsResult = $this->db->query($sqlTotals);
201 $total = $this->db->fetch_object($totalsResult)->total;
206 $obj_ret[
'data'] = $tmp;
207 $obj_ret[
'pagination'] = [
208 'total' => (int) $total,
210 'page_count' => ceil((
int) $total / $limit),
231 public function post($request_data =
null)
234 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
235 throw new RestException(403,
"Missing permission: Create/modify contracts/subscriptions");
242 $socid = (int) $request_data[
'socid'];
243 $thirdpartytmp =
new Societe($this->db);
244 $thirdparty_result = $thirdpartytmp->fetch($socid);
245 if ($thirdparty_result < 1) {
246 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
249 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
252 foreach ($request_data as $field => $value) {
253 if ($field ===
'caller') {
255 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
258 if ($field ==
'id') {
259 throw new RestException(400,
'Creating with id field is forbidden');
261 if ($field ==
'entity' && ((
int) $value) != ((
int)
$conf->entity)) {
262 throw new RestException(403,
'Creating with entity='.((
int) $value).
' MUST be the same entity='.((
int)
$conf->entity).
' as your API user/key belongs to');
265 if ($field ==
'socid') {
266 $thirdparty =
new Societe($this->db);
267 $result = $thirdparty->fetch((
int) $value);
269 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
273 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
282 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
286 return $this->contract->id;
309 public function getLines(
$id, $sortfield =
"d.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
311 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
312 throw new RestException(403);
315 $result = $this->contract->fetch(
$id);
317 throw new RestException(404,
'Contract not found');
321 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
326 $sql =
"SELECT d.rowid";
327 $sql .=
" FROM ".$this->db->prefix().
"contratdet AS d";
328 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contrat AS c ON (c.rowid = d.fk_contrat)";
329 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contratdet_extrafields AS ef ON (ef.fk_object = d.rowid)";
330 $sql .=
" WHERE d.fk_contrat = ".((int)
$id);
331 $sql .=
' AND c.entity IN ('.getEntity(
'contrat').
')';
337 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
341 $sqlTotals = str_replace(
'SELECT d.rowid',
'SELECT count(d.rowid) as total', $sql);
343 $sql .= $this->db->order($sortfield, $sortorder);
348 $offset = $limit * $page;
350 $sql .= $this->db->plimit($limit + 1, $offset);
353 $result = $this->db->query($sql);
355 $num = $this->db->num_rows($result);
356 $min = min($num, ($limit <= 0 ? $num : $limit));
359 $obj = $this->db->fetch_object($result);
361 if ($contratdet_static->fetch($obj->rowid)) {
367 throw new RestException(503,
'Error when retrieve contratdet list : '.$this->db->lasterror());
370 if ($pagination_data) {
371 $totalsResult = $this->db->query($sqlTotals);
372 $total = $this->db->fetch_object($totalsResult)->total;
376 $obj_ret[
'data'] = $tmp;
377 $obj_ret[
'pagination'] = [
378 'total' => (int) $total,
380 'page_count' => ceil((
int) $total / $limit),
402 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
403 throw new RestException(403);
406 $result = $this->contract->fetch(
$id);
408 throw new RestException(404,
'Contract not found');
412 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
415 $request_data = (object) $request_data;
417 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
418 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
420 $updateRes = $this->contract->addline(
422 $request_data->subprice,
424 $request_data->tva_tx,
425 $request_data->localtax1_tx,
426 $request_data->localtax2_tx,
427 $request_data->fk_product,
428 $request_data->remise_percent,
429 $request_data->date_start,
430 $request_data->date_end,
431 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
432 $request_data->subprice_excl_tax,
433 $request_data->info_bits,
434 $request_data->fk_fournprice,
435 $request_data->pa_ht,
436 $request_data->array_options,
437 $request_data->fk_unit,
441 if ($updateRes > 0) {
462 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
463 throw new RestException(403);
466 $result = $this->contract->fetch(
$id);
468 throw new RestException(404,
'Contrat not found');
472 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
476 if ($contractline->fetch($lineid) <= 0) {
477 throw new RestException(404,
'Contract line not found');
479 if ($contractline->fk_contrat != $this->contract->id) {
480 throw new RestException(403,
'Line does not belong to this contract');
483 $request_data = (object) $request_data;
485 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
486 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
488 $updateRes = $this->contract->updateline(
491 $request_data->subprice,
493 $request_data->remise_percent,
494 $request_data->date_start,
495 $request_data->date_end,
496 $request_data->tva_tx,
497 $request_data->localtax1_tx,
498 $request_data->localtax2_tx,
499 $request_data->date_start_real,
500 $request_data->date_end_real,
501 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
502 $request_data->info_bits,
503 $request_data->fk_fourn_price,
504 $request_data->pa_ht,
505 $request_data->array_options,
506 $request_data->fk_unit
509 if ($updateRes > 0) {
512 $result->fetch($lineid);
531 'barcode_type_coder',
537 'shipping_method_id',
555 'user_validation_id',
557 'user_modification_id',
558 'cond_reglement_supplier_id',
560 'retained_warranty_fk_cond_reglement',
590 ) as $fieldToUnset) {
591 unset($result->{$fieldToUnset});
594 $result = $this->
get(
$id);
595 unset($result->line);
599 throw new RestException(500, implode(
';', $this->contract->errors));
619 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
621 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'activer')) {
622 throw new RestException(403);
625 $result = $this->contract->fetch(
$id);
627 throw new RestException(404,
'Contrat not found');
631 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
634 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $dateend, $comment);
636 if ($updateRes > 0) {
637 $result = $this->
get(
$id);
638 unset($result->line);
639 return $this->_cleanObjectDatas($result);
662 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'desactiver')) {
663 throw new RestException(403);
666 $result = $this->contract->fetch(
$id);
668 throw new RestException(404,
'Contrat not found');
672 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
675 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $comment);
677 if ($updateRes > 0) {
678 $result = $this->
get(
$id);
679 unset($result->line);
680 return $this->_cleanObjectDatas($result);
702 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
703 throw new RestException(403);
706 $result = $this->contract->fetch(
$id);
708 throw new RestException(404,
'Contrat not found');
712 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
716 if ($contractline->fetch($lineid) <= 0) {
717 throw new RestException(404,
'Contract line not found');
719 if ($contractline->fk_contrat != $this->contract->id) {
720 throw new RestException(403,
'Line does not belong to this contract');
723 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
724 if ($updateRes > 0) {
725 return $this->
get(
$id);
727 throw new RestException(405, $this->contract->errorsToString());
745 public function put(
$id, $request_data =
null)
747 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
748 throw new RestException(403);
751 throw new RestException(400,
'No contract with id=0 can exist');
754 $result = $this->contract->fetch(
$id);
756 throw new RestException(404,
'Contrat not found');
759 $old_socid = $this->contract->socid;
760 $oldthirdpartytmp =
new Societe($this->db);
761 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
762 if ($old_thirdparty_result < 1) {
763 throw new RestException(404,
'Thirdparty with id='.$old_socid.
' not found or not allowed');
766 throw new RestException(403,
'Access to old thirdparty='.$old_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
770 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
772 foreach ($request_data as $field => $value) {
773 if ($field ==
'id') {
774 throw new RestException(400,
'Updating with id field is forbidden');
776 if ($field ==
'entity' && $value != $this->contract->entity) {
777 throw new RestException(400,
'Changing entity of a contract using the APIs is not possible');
779 if ($field ===
'caller') {
781 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
785 if ($field ==
'socid') {
786 $thirdparty =
new Societe($this->db);
787 $result = $thirdparty->fetch((
int) $value);
789 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
793 if ($field ==
'array_options' && is_array($value)) {
794 foreach ($value as $index => $val) {
795 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
800 if ($field ==
'socid') {
801 $new_socid = (int) $value;
802 $loopthirdpartytmp =
new Societe($this->db);
803 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
804 if ($new_thirdparty_result < 1) {
805 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
808 throw new RestException(403,
'Access to new thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
812 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
815 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
816 return $this->
get(
$id);
818 throw new RestException(500, $this->contract->errorsToString());
836 public function delete(
$id)
838 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
839 throw new RestException(403,
'Missing permission: Delete contracts/subscriptions');
842 throw new RestException(400,
'No contract with id=0 can exist');
844 $result = $this->contract->fetch(
$id);
846 throw new RestException(404,
'Contract not found');
850 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
853 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
854 throw new RestException(500,
'Error when delete contract : '.$this->contract->errorsToString());
860 'message' =>
'Contract deleted'
891 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
892 throw new RestException(403);
895 throw new RestException(400,
'No contract with id=0 can exist');
897 $result = $this->contract->fetch(
$id);
899 throw new RestException(404,
'Contract not found');
903 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
906 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
908 throw new RestException(304,
'Error nothing done. May be object is already validated');
911 throw new RestException(500,
'Error when validating Contract: '.$this->contract->errorsToString());
917 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
948 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
949 throw new RestException(403);
952 throw new RestException(400,
'No contract with id=0 can exist');
954 $result = $this->contract->fetch(
$id);
956 throw new RestException(404,
'Contract not found');
960 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
963 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
965 throw new RestException(304,
'Error nothing done. May be object is already close');
968 throw new RestException(500,
'Error when closing Contract: '.$this->contract->errorsToString());
974 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
1012 if ($data ===
null) {
1016 foreach (Contracts::$FIELDS as $field) {
1017 if (!isset($data[$field])) {
1018 throw new RestException(400,
"$field field missing");
1020 $contrat[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)
deleteLine($id, $lineid)
Delete a line to given contract.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false, $withLines=true)
List contracts.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
activateLine($id, $lineid, $datestart, $dateend=null, $comment=null)
Activate a service line of a given contract.
validate($id, $notrigger=0)
Validate a contract.
post($request_data=null)
Create contract object.
unactivateLine($id, $lineid, $datestart, $comment=null)
Unactivate a service line of a given contract.
__construct()
Constructor.
close($id, $notrigger=0)
Close all services of a contract.
getLines($id, $sortfield="d.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
Get lines of a contract.
postLine($id, $request_data=null)
Add a line to given contract.
Class to manage lines of contracts.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage third parties objects (customers, suppliers, prospects...)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.