21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
37 public static $FIELDS = array(
54 $this->contract =
new Contrat($this->db);
71 public function get(
$id, $properties =
'', $withLines =
true)
73 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
74 throw new RestException(403);
77 throw new RestException(400,
'No contract with id=0 can exist');
79 $result = $this->contract->fetch(
$id);
81 throw new RestException(404,
'Contract not found');
85 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
88 $this->contract->fetchObjectLinked();
91 unset($this->contract->lines);
119 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $withLines =
true)
123 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
124 throw new RestException(403);
130 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
134 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
135 $search_sale = DolibarrApiAccess::$user->id;
138 $sql =
"SELECT t.rowid";
139 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t";
140 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
141 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
142 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
144 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
147 if ($search_sale && $search_sale !=
'-1') {
148 if ($search_sale == -2) {
149 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
150 } elseif ($search_sale > 0) {
151 $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).
")";
157 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
164 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
166 $sql .= $this->db->order($sortfield, $sortorder);
171 $offset = $limit * $page;
173 $sql .= $this->db->plimit($limit + 1, $offset);
176 $result = $this->db->query($sql);
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $obj = $this->db->fetch_object($result);
184 $contrat_static =
new Contrat($this->db);
185 if ($contrat_static->fetch($obj->rowid)) {
187 unset($contrat_static->lines);
194 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
198 if ($pagination_data) {
199 $totalsResult = $this->db->query($sqlTotals);
200 $total = $this->db->fetch_object($totalsResult)->total;
205 $obj_ret[
'data'] = $tmp;
206 $obj_ret[
'pagination'] = [
207 'total' => (int) $total,
209 'page_count' => ceil((
int) $total / $limit),
230 public function post($request_data =
null)
233 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
234 throw new RestException(403,
"Missing permission: Create/modify contracts/subscriptions");
241 $socid = (int) $request_data[
'socid'];
242 $thirdpartytmp =
new Societe($this->db);
243 $thirdparty_result = $thirdpartytmp->fetch($socid);
244 if ($thirdparty_result < 1) {
245 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
248 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
251 foreach ($request_data as $field => $value) {
252 if ($field ===
'caller') {
254 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
257 if ($field ==
'id') {
258 throw new RestException(400,
'Creating with id field is forbidden');
260 if ($field ==
'entity' && ((
int) $value) != ((
int)
$conf->entity)) {
261 throw new RestException(403,
'Creating with entity='.((
int) $value).
' MUST be the same entity='.((
int)
$conf->entity).
' as your API user/key belongs to');
264 if ($field ==
'socid') {
265 $thirdparty =
new Societe($this->db);
266 $result = $thirdparty->fetch((
int) $value);
268 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
272 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
281 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
282 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
285 return $this->contract->id;
308 public function getLines(
$id, $sortfield =
"d.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
310 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
311 throw new RestException(403);
314 $result = $this->contract->fetch(
$id);
316 throw new RestException(404,
'Contract not found');
320 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
325 $sql =
"SELECT d.rowid";
326 $sql .=
" FROM ".$this->db->prefix().
"contratdet AS d";
327 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contrat AS c ON (c.rowid = d.fk_contrat)";
328 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contratdet_extrafields AS ef ON (ef.fk_object = d.rowid)";
329 $sql .=
" WHERE d.fk_contrat = ".((int)
$id);
330 $sql .=
' AND c.entity IN ('.getEntity(
'contrat').
')';
334 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
336 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
340 $sqlTotals = str_replace(
'SELECT d.rowid',
'SELECT count(d.rowid) as total', $sql);
342 $sql .= $this->db->order($sortfield, $sortorder);
347 $offset = $limit * $page;
349 $sql .= $this->db->plimit($limit + 1, $offset);
352 $result = $this->db->query($sql);
354 $num = $this->db->num_rows($result);
355 $min = min($num, ($limit <= 0 ? $num : $limit));
358 $obj = $this->db->fetch_object($result);
360 if ($contratdet_static->fetch($obj->rowid)) {
366 throw new RestException(503,
'Error when retrieve contratdet list : '.$this->db->lasterror());
369 if ($pagination_data) {
370 $totalsResult = $this->db->query($sqlTotals);
371 $total = $this->db->fetch_object($totalsResult)->total;
375 $obj_ret[
'data'] = $tmp;
376 $obj_ret[
'pagination'] = [
377 'total' => (int) $total,
379 'page_count' => ceil((
int) $total / $limit),
401 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
402 throw new RestException(403);
405 $result = $this->contract->fetch(
$id);
407 throw new RestException(404,
'Contract not found');
411 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
414 $request_data = (object) $request_data;
416 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
417 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
419 $updateRes = $this->contract->addline(
421 $request_data->subprice,
423 $request_data->tva_tx,
424 $request_data->localtax1_tx,
425 $request_data->localtax2_tx,
426 $request_data->fk_product,
427 $request_data->remise_percent,
428 $request_data->date_start,
429 $request_data->date_end,
430 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
431 $request_data->subprice_excl_tax,
432 $request_data->info_bits,
433 $request_data->fk_fournprice,
434 $request_data->pa_ht,
435 $request_data->array_options,
436 $request_data->fk_unit,
440 if ($updateRes > 0) {
461 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
462 throw new RestException(403);
465 $result = $this->contract->fetch(
$id);
467 throw new RestException(404,
'Contrat not found');
471 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
474 $request_data = (object) $request_data;
476 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
477 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
479 $updateRes = $this->contract->updateline(
482 $request_data->subprice,
484 $request_data->remise_percent,
485 $request_data->date_start,
486 $request_data->date_end,
487 $request_data->tva_tx,
488 $request_data->localtax1_tx,
489 $request_data->localtax2_tx,
490 $request_data->date_start_real,
491 $request_data->date_end_real,
492 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
493 $request_data->info_bits,
494 $request_data->fk_fourn_price,
495 $request_data->pa_ht,
496 $request_data->array_options,
497 $request_data->fk_unit
500 if ($updateRes > 0) {
503 $result->fetch($lineid);
522 'barcode_type_coder',
528 'shipping_method_id',
546 'user_validation_id',
548 'user_modification_id',
549 'cond_reglement_supplier_id',
551 'retained_warranty_fk_cond_reglement',
581 ) as $fieldToUnset) {
582 unset($result->{$fieldToUnset});
585 $result = $this->
get(
$id);
586 unset($result->line);
590 throw new RestException(500, implode(
';', $this->contract->errors));
610 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
612 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'activer')) {
613 throw new RestException(403);
616 $result = $this->contract->fetch(
$id);
618 throw new RestException(404,
'Contrat not found');
622 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
625 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $dateend, $comment);
627 if ($updateRes > 0) {
628 $result = $this->
get(
$id);
629 unset($result->line);
630 return $this->_cleanObjectDatas($result);
653 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'desactiver')) {
654 throw new RestException(403);
657 $result = $this->contract->fetch(
$id);
659 throw new RestException(404,
'Contrat not found');
663 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
666 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $comment);
668 if ($updateRes > 0) {
669 $result = $this->
get(
$id);
670 unset($result->line);
671 return $this->_cleanObjectDatas($result);
693 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
694 throw new RestException(403);
697 $result = $this->contract->fetch(
$id);
699 throw new RestException(404,
'Contrat not found');
703 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
708 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
709 if ($updateRes > 0) {
710 return $this->
get(
$id);
712 throw new RestException(405, $this->contract->error);
730 public function put(
$id, $request_data =
null)
732 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
733 throw new RestException(403);
736 throw new RestException(400,
'No contract with id=0 can exist');
739 $result = $this->contract->fetch(
$id);
741 throw new RestException(404,
'Contrat not found');
744 $old_socid = $this->contract->socid;
745 $oldthirdpartytmp =
new Societe($this->db);
746 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
747 if ($old_thirdparty_result < 1) {
748 throw new RestException(404,
'Thirdparty with id='.$old_socid.
' not found or not allowed');
751 throw new RestException(403,
'Access to old thirdparty='.$old_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
755 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
757 foreach ($request_data as $field => $value) {
758 if ($field ==
'id') {
759 throw new RestException(400,
'Updating with id field is forbidden');
761 if ($field ==
'entity' && $value != $this->contract->entity) {
762 throw new RestException(400,
'Changing entity of a contract using the APIs is not possible');
764 if ($field ===
'caller') {
766 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
770 if ($field ==
'socid') {
771 $thirdparty =
new Societe($this->db);
772 $result = $thirdparty->fetch((
int) $value);
774 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
778 if ($field ==
'array_options' && is_array($value)) {
779 foreach ($value as $index => $val) {
780 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
785 if ($field ==
'socid') {
786 $new_socid = (int) $value;
787 $loopthirdpartytmp =
new Societe($this->db);
788 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
789 if ($new_thirdparty_result < 1) {
790 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
793 throw new RestException(403,
'Access to new thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
797 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
800 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
801 return $this->
get(
$id);
803 throw new RestException(500, $this->contract->error);
821 public function delete(
$id)
823 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
824 throw new RestException(403,
'Missing permission: Delete contracts/subscriptions');
827 throw new RestException(400,
'No contract with id=0 can exist');
829 $result = $this->contract->fetch(
$id);
831 throw new RestException(404,
'Contract not found');
835 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
838 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
839 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
845 'message' =>
'Contract deleted'
876 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
877 throw new RestException(403);
880 throw new RestException(400,
'No contract with id=0 can exist');
882 $result = $this->contract->fetch(
$id);
884 throw new RestException(404,
'Contract not found');
888 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
891 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
893 throw new RestException(304,
'Error nothing done. May be object is already validated');
896 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
902 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
933 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
934 throw new RestException(403);
937 throw new RestException(400,
'No contract with id=0 can exist');
939 $result = $this->contract->fetch(
$id);
941 throw new RestException(404,
'Contract not found');
945 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
948 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
950 throw new RestException(304,
'Error nothing done. May be object is already close');
953 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
959 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
997 if ($data ===
null) {
1001 foreach (Contracts::$FIELDS as $field) {
1002 if (!isset($data[$field])) {
1003 throw new RestException(400,
"$field field missing");
1005 $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.
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 third parties objects (customers, suppliers, prospects...)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.