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);
475 $request_data = (object) $request_data;
477 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
478 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
480 $updateRes = $this->contract->updateline(
483 $request_data->subprice,
485 $request_data->remise_percent,
486 $request_data->date_start,
487 $request_data->date_end,
488 $request_data->tva_tx,
489 $request_data->localtax1_tx,
490 $request_data->localtax2_tx,
491 $request_data->date_start_real,
492 $request_data->date_end_real,
493 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
494 $request_data->info_bits,
495 $request_data->fk_fourn_price,
496 $request_data->pa_ht,
497 $request_data->array_options,
498 $request_data->fk_unit
501 if ($updateRes > 0) {
504 $result->fetch($lineid);
523 'barcode_type_coder',
529 'shipping_method_id',
547 'user_validation_id',
549 'user_modification_id',
550 'cond_reglement_supplier_id',
552 'retained_warranty_fk_cond_reglement',
582 ) as $fieldToUnset) {
583 unset($result->{$fieldToUnset});
586 $result = $this->
get(
$id);
587 unset($result->line);
591 throw new RestException(500, implode(
';', $this->contract->errors));
611 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
613 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'activer')) {
614 throw new RestException(403);
617 $result = $this->contract->fetch(
$id);
619 throw new RestException(404,
'Contrat not found');
623 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
626 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $dateend, $comment);
628 if ($updateRes > 0) {
629 $result = $this->
get(
$id);
630 unset($result->line);
631 return $this->_cleanObjectDatas($result);
654 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'desactiver')) {
655 throw new RestException(403);
658 $result = $this->contract->fetch(
$id);
660 throw new RestException(404,
'Contrat not found');
664 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
667 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $comment);
669 if ($updateRes > 0) {
670 $result = $this->
get(
$id);
671 unset($result->line);
672 return $this->_cleanObjectDatas($result);
694 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
695 throw new RestException(403);
698 $result = $this->contract->fetch(
$id);
700 throw new RestException(404,
'Contrat not found');
704 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
709 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
710 if ($updateRes > 0) {
711 return $this->
get(
$id);
713 throw new RestException(405, $this->contract->error);
731 public function put(
$id, $request_data =
null)
733 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
734 throw new RestException(403);
737 throw new RestException(400,
'No contract with id=0 can exist');
740 $result = $this->contract->fetch(
$id);
742 throw new RestException(404,
'Contrat not found');
745 $old_socid = $this->contract->socid;
746 $oldthirdpartytmp =
new Societe($this->db);
747 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
748 if ($old_thirdparty_result < 1) {
749 throw new RestException(404,
'Thirdparty with id='.$old_socid.
' not found or not allowed');
752 throw new RestException(403,
'Access to old thirdparty='.$old_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
756 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
758 foreach ($request_data as $field => $value) {
759 if ($field ==
'id') {
760 throw new RestException(400,
'Updating with id field is forbidden');
762 if ($field ==
'entity' && $value != $this->contract->entity) {
763 throw new RestException(400,
'Changing entity of a contract using the APIs is not possible');
765 if ($field ===
'caller') {
767 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
771 if ($field ==
'socid') {
772 $thirdparty =
new Societe($this->db);
773 $result = $thirdparty->fetch((
int) $value);
775 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
779 if ($field ==
'array_options' && is_array($value)) {
780 foreach ($value as $index => $val) {
781 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
786 if ($field ==
'socid') {
787 $new_socid = (int) $value;
788 $loopthirdpartytmp =
new Societe($this->db);
789 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
790 if ($new_thirdparty_result < 1) {
791 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
794 throw new RestException(403,
'Access to new thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
798 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
801 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
802 return $this->
get(
$id);
804 throw new RestException(500, $this->contract->error);
822 public function delete(
$id)
824 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
825 throw new RestException(403,
'Missing permission: Delete contracts/subscriptions');
828 throw new RestException(400,
'No contract with id=0 can exist');
830 $result = $this->contract->fetch(
$id);
832 throw new RestException(404,
'Contract not found');
836 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
839 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
840 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
846 'message' =>
'Contract deleted'
877 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
878 throw new RestException(403);
881 throw new RestException(400,
'No contract with id=0 can exist');
883 $result = $this->contract->fetch(
$id);
885 throw new RestException(404,
'Contract not found');
889 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
892 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
894 throw new RestException(304,
'Error nothing done. May be object is already validated');
897 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
903 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
934 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
935 throw new RestException(403);
938 throw new RestException(400,
'No contract with id=0 can exist');
940 $result = $this->contract->fetch(
$id);
942 throw new RestException(404,
'Contract not found');
946 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
949 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
951 throw new RestException(304,
'Error nothing done. May be object is already close');
954 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
960 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
998 if ($data ===
null) {
1002 foreach (Contracts::$FIELDS as $field) {
1003 if (!isset($data[$field])) {
1004 throw new RestException(400,
"$field field missing");
1006 $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.