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).
")";
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').
')';
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);
475 if ($contractline->fetch($lineid) <= 0) {
476 throw new RestException(404,
'Contract line not found');
478 if ($contractline->fk_contrat != $this->contract->id) {
479 throw new RestException(403,
'Line does not belong to this contract');
482 $request_data = (object) $request_data;
484 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
485 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
487 $updateRes = $this->contract->updateline(
490 $request_data->subprice,
492 $request_data->remise_percent,
493 $request_data->date_start,
494 $request_data->date_end,
495 $request_data->tva_tx,
496 $request_data->localtax1_tx,
497 $request_data->localtax2_tx,
498 $request_data->date_start_real,
499 $request_data->date_end_real,
500 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
501 $request_data->info_bits,
502 $request_data->fk_fourn_price,
503 $request_data->pa_ht,
504 $request_data->array_options,
505 $request_data->fk_unit
508 if ($updateRes > 0) {
511 $result->fetch($lineid);
530 'barcode_type_coder',
536 'shipping_method_id',
554 'user_validation_id',
556 'user_modification_id',
557 'cond_reglement_supplier_id',
559 'retained_warranty_fk_cond_reglement',
589 ) as $fieldToUnset) {
590 unset($result->{$fieldToUnset});
593 $result = $this->
get(
$id);
594 unset($result->line);
598 throw new RestException(500, implode(
';', $this->contract->errors));
618 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
620 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'activer')) {
621 throw new RestException(403);
624 $result = $this->contract->fetch(
$id);
626 throw new RestException(404,
'Contrat not found');
630 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
633 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $dateend, $comment);
635 if ($updateRes > 0) {
636 $result = $this->
get(
$id);
637 unset($result->line);
638 return $this->_cleanObjectDatas($result);
661 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'desactiver')) {
662 throw new RestException(403);
665 $result = $this->contract->fetch(
$id);
667 throw new RestException(404,
'Contrat not found');
671 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
674 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $comment);
676 if ($updateRes > 0) {
677 $result = $this->
get(
$id);
678 unset($result->line);
679 return $this->_cleanObjectDatas($result);
701 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
702 throw new RestException(403);
705 $result = $this->contract->fetch(
$id);
707 throw new RestException(404,
'Contrat not found');
711 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
715 if ($contractline->fetch($lineid) <= 0) {
716 throw new RestException(404,
'Contract line not found');
718 if ($contractline->fk_contrat != $this->contract->id) {
719 throw new RestException(403,
'Line does not belong to this contract');
722 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
723 if ($updateRes > 0) {
724 return $this->
get(
$id);
726 throw new RestException(405, $this->contract->error);
744 public function put(
$id, $request_data =
null)
746 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
747 throw new RestException(403);
750 throw new RestException(400,
'No contract with id=0 can exist');
753 $result = $this->contract->fetch(
$id);
755 throw new RestException(404,
'Contrat not found');
758 $old_socid = $this->contract->socid;
759 $oldthirdpartytmp =
new Societe($this->db);
760 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
761 if ($old_thirdparty_result < 1) {
762 throw new RestException(404,
'Thirdparty with id='.$old_socid.
' not found or not allowed');
765 throw new RestException(403,
'Access to old thirdparty='.$old_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
769 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
771 foreach ($request_data as $field => $value) {
772 if ($field ==
'id') {
773 throw new RestException(400,
'Updating with id field is forbidden');
775 if ($field ==
'entity' && $value != $this->contract->entity) {
776 throw new RestException(400,
'Changing entity of a contract using the APIs is not possible');
778 if ($field ===
'caller') {
780 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
784 if ($field ==
'socid') {
785 $thirdparty =
new Societe($this->db);
786 $result = $thirdparty->fetch((
int) $value);
788 throw new RestException(404,
'Thirdparty with id='.((
int) $value).
' not found');
792 if ($field ==
'array_options' && is_array($value)) {
793 foreach ($value as $index => $val) {
794 $this->contract->array_options[$index] = $this->_checkValForAPI($field, $val, $this->contract);
799 if ($field ==
'socid') {
800 $new_socid = (int) $value;
801 $loopthirdpartytmp =
new Societe($this->db);
802 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
803 if ($new_thirdparty_result < 1) {
804 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
807 throw new RestException(403,
'Access to new thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
811 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
814 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
815 return $this->
get(
$id);
817 throw new RestException(500, $this->contract->error);
835 public function delete(
$id)
837 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
838 throw new RestException(403,
'Missing permission: Delete contracts/subscriptions');
841 throw new RestException(400,
'No contract with id=0 can exist');
843 $result = $this->contract->fetch(
$id);
845 throw new RestException(404,
'Contract not found');
849 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
852 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
853 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
859 'message' =>
'Contract deleted'
890 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
891 throw new RestException(403);
894 throw new RestException(400,
'No contract with id=0 can exist');
896 $result = $this->contract->fetch(
$id);
898 throw new RestException(404,
'Contract not found');
902 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
905 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
907 throw new RestException(304,
'Error nothing done. May be object is already validated');
910 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
916 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
947 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
948 throw new RestException(403);
951 throw new RestException(400,
'No contract with id=0 can exist');
953 $result = $this->contract->fetch(
$id);
955 throw new RestException(404,
'Contract not found');
959 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
962 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
964 throw new RestException(304,
'Error nothing done. May be object is already close');
967 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
973 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
1011 if ($data ===
null) {
1015 foreach (Contracts::$FIELDS as $field) {
1016 if (!isset($data[$field])) {
1017 throw new RestException(400,
"$field field missing");
1019 $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...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.