20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
36 static $FIELDS = array(
39 'commercial_signature_id',
68 public function get($id)
70 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
71 throw new RestException(401);
74 $result = $this->contract->fetch($id);
76 throw new RestException(404,
'Contract not found');
80 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 $this->contract->fetchObjectLinked();
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
109 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
110 throw new RestException(401);
116 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
120 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
121 $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
126 $sql .=
", sc.fk_soc, sc.fk_user";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as t";
130 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
131 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
134 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
135 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
136 $sql .=
" AND t.fk_soc = sc.fk_soc";
139 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
141 if ($search_sale > 0) {
142 $sql .=
" AND t.rowid = sc.fk_soc";
145 if ($search_sale > 0) {
146 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
152 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
154 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
155 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
158 $sql .= $this->
db->order($sortfield, $sortorder);
163 $offset = $limit * $page;
165 $sql .= $this->
db->plimit($limit + 1, $offset);
169 $result = $this->
db->query($sql);
172 $num = $this->
db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
176 $obj = $this->
db->fetch_object($result);
177 $contrat_static =
new Contrat($this->
db);
178 if ($contrat_static->fetch($obj->rowid)) {
184 throw new RestException(503,
'Error when retrieve contrat list : '.$this->
db->lasterror());
186 if (!count($obj_ret)) {
187 throw new RestException(404,
'No contract found');
198 public function post($request_data =
null)
200 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
201 throw new RestException(401,
"Insufficient rights");
204 $result = $this->
_validate($request_data);
206 foreach ($request_data as $field => $value) {
207 $this->contract->$field = $value;
216 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
220 return $this->contract->id;
234 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
235 throw new RestException(401);
238 $result = $this->contract->fetch($id);
240 throw new RestException(404,
'Contract not found');
244 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
246 $this->contract->getLinesArray();
248 foreach ($this->contract->lines as $line) {
264 public function postLine($id, $request_data =
null)
266 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
267 throw new RestException(401);
270 $result = $this->contract->fetch($id);
272 throw new RestException(404,
'Contract not found');
276 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
279 $request_data = (object) $request_data;
281 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
282 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
284 $updateRes = $this->contract->addline(
286 $request_data->subprice,
288 $request_data->tva_tx,
289 $request_data->localtax1_tx,
290 $request_data->localtax2_tx,
291 $request_data->fk_product,
292 $request_data->remise_percent,
293 $request_data->date_start,
294 $request_data->date_end,
295 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
296 $request_data->subprice_excl_tax,
297 $request_data->info_bits,
298 $request_data->fk_fournprice,
299 $request_data->pa_ht,
300 $request_data->array_options,
301 $request_data->fk_unit,
305 if ($updateRes > 0) {
322 public function putLine($id, $lineid, $request_data =
null)
324 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
325 throw new RestException(401);
328 $result = $this->contract->fetch($id);
330 throw new RestException(404,
'Contrat not found');
334 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
337 $request_data = (object) $request_data;
339 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
340 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
342 $updateRes = $this->contract->updateline(
345 $request_data->subprice,
347 $request_data->remise_percent,
348 $request_data->date_start,
349 $request_data->date_end,
350 $request_data->tva_tx,
351 $request_data->localtax1_tx,
352 $request_data->localtax2_tx,
353 $request_data->date_start_real,
354 $request_data->date_end_real,
355 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
356 $request_data->info_bits,
357 $request_data->fk_fourn_price,
358 $request_data->pa_ht,
359 $request_data->array_options,
360 $request_data->fk_unit
363 if ($updateRes > 0) {
364 $result = $this->
get($id);
365 unset($result->line);
385 public function activateLine($id, $lineid, $datestart, $dateend =
null, $comment =
null)
387 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
388 throw new RestException(401);
391 $result = $this->contract->fetch($id);
393 throw new RestException(404,
'Contrat not found');
397 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
400 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
402 if ($updateRes > 0) {
403 $result = $this->
get($id);
404 unset($result->line);
425 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
426 throw new RestException(401);
429 $result = $this->contract->fetch($id);
431 throw new RestException(404,
'Contrat not found');
435 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
438 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
440 if ($updateRes > 0) {
441 $result = $this->
get($id);
442 unset($result->line);
465 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
466 throw new RestException(401);
469 $result = $this->contract->fetch($id);
471 throw new RestException(404,
'Contrat not found');
475 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
480 $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
481 if ($updateRes > 0) {
482 return $this->
get($id);
484 throw new RestException(405, $this->contract->error);
496 public function put($id, $request_data =
null)
498 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
499 throw new RestException(401);
502 $result = $this->contract->fetch($id);
504 throw new RestException(404,
'Contrat not found');
508 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
510 foreach ($request_data as $field => $value) {
511 if ($field ==
'id') {
514 $this->contract->$field = $value;
517 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
518 return $this->
get($id);
520 throw new RestException(500, $this->contract->error);
531 public function delete($id)
533 if (!DolibarrApiAccess::$user->rights->contrat->supprimer) {
534 throw new RestException(401);
536 $result = $this->contract->fetch($id);
538 throw new RestException(404,
'Contract not found');
542 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
545 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
546 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
552 'message' =>
'Contract deleted'
575 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
576 throw new RestException(401);
578 $result = $this->contract->fetch($id);
580 throw new RestException(404,
'Contract not found');
584 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
589 throw new RestException(304,
'Error nothing done. May be object is already validated');
592 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
598 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
619 public function close($id, $notrigger = 0)
621 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
622 throw new RestException(401);
624 $result = $this->contract->fetch($id);
626 throw new RestException(404,
'Contract not found');
630 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
633 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
635 throw new RestException(304,
'Error nothing done. May be object is already close');
638 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
644 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
661 $object = parent::_cleanObjectDatas($object);
663 unset($object->address);
665 unset($object->date_start);
666 unset($object->date_start_real);
667 unset($object->date_end);
668 unset($object->date_end_real);
669 unset($object->civility_id);
684 foreach (Contracts::$FIELDS as $field) {
685 if (!isset($data[$field])) {
686 throw new RestException(400,
"$field field missing");
688 $contrat[$field] = $data[$field];
getLines($id)
Get lines of a contract.
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List contracts.
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)
deleteLine($id, $lineid)
Delete a line to given contract.
_cleanObjectDatas($object)
Clean sensible object datas.
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.
postLine($id, $request_data=null)
Add a line to given contract.
Class to manage contracts.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.