20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
36 static $FIELDS = array(
39 'commercial_signature_id',
55 $this->contract =
new Contrat($this->db);
67 public function get($id)
69 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
70 throw new RestException(401);
73 $result = $this->contract->fetch($id);
75 throw new RestException(404,
'Contract not found');
79 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
82 $this->contract->fetchObjectLinked();
104 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
108 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
109 throw new RestException(401);
115 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
119 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
120 $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
125 $sql .=
", sc.fk_soc, sc.fk_user";
127 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
129 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
130 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
133 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
134 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
135 $sql .=
" AND t.fk_soc = sc.fk_soc";
138 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
140 if ($search_sale > 0) {
141 $sql .=
" AND t.rowid = sc.fk_soc";
144 if ($search_sale > 0) {
145 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
152 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $sql .= $this->db->order($sortfield, $sortorder);
161 $offset = $limit * $page;
163 $sql .= $this->db->plimit($limit + 1, $offset);
167 $result = $this->db->query($sql);
170 $num = $this->db->num_rows($result);
171 $min = min($num, ($limit <= 0 ? $num : $limit));
174 $obj = $this->db->fetch_object($result);
175 $contrat_static =
new Contrat($this->db);
176 if ($contrat_static->fetch($obj->rowid)) {
182 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
184 if (!count($obj_ret)) {
185 throw new RestException(404,
'No contract found');
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
199 throw new RestException(401,
"Insufficient rights");
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 $this->contract->$field = $value;
214 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
215 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
218 return $this->contract->id;
232 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
233 throw new RestException(401);
236 $result = $this->contract->fetch($id);
238 throw new RestException(404,
'Contract not found');
242 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
244 $this->contract->getLinesArray();
246 foreach ($this->contract->lines as $line) {
262 public function postLine($id, $request_data =
null)
264 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
265 throw new RestException(401);
268 $result = $this->contract->fetch($id);
270 throw new RestException(404,
'Contract not found');
274 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
277 $request_data = (object) $request_data;
279 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
280 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
282 $updateRes = $this->contract->addline(
284 $request_data->subprice,
286 $request_data->tva_tx,
287 $request_data->localtax1_tx,
288 $request_data->localtax2_tx,
289 $request_data->fk_product,
290 $request_data->remise_percent,
291 $request_data->date_start,
292 $request_data->date_end,
293 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
294 $request_data->subprice_excl_tax,
295 $request_data->info_bits,
296 $request_data->fk_fournprice,
297 $request_data->pa_ht,
298 $request_data->array_options,
299 $request_data->fk_unit,
303 if ($updateRes > 0) {
320 public function putLine($id, $lineid, $request_data =
null)
322 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
323 throw new RestException(401);
326 $result = $this->contract->fetch($id);
328 throw new RestException(404,
'Contrat not found');
332 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
335 $request_data = (object) $request_data;
337 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
338 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
340 $updateRes = $this->contract->updateline(
343 $request_data->subprice,
345 $request_data->remise_percent,
346 $request_data->date_start,
347 $request_data->date_end,
348 $request_data->tva_tx,
349 $request_data->localtax1_tx,
350 $request_data->localtax2_tx,
351 $request_data->date_start_real,
352 $request_data->date_end_real,
353 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
354 $request_data->info_bits,
355 $request_data->fk_fourn_price,
356 $request_data->pa_ht,
357 $request_data->array_options,
358 $request_data->fk_unit
361 if ($updateRes > 0) {
362 $result = $this->
get($id);
363 unset($result->line);
383 public function activateLine($id, $lineid, $datestart, $dateend =
null, $comment =
null)
385 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
386 throw new RestException(401);
389 $result = $this->contract->fetch($id);
391 throw new RestException(404,
'Contrat not found');
395 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
398 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
400 if ($updateRes > 0) {
401 $result = $this->
get($id);
402 unset($result->line);
423 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
424 throw new RestException(401);
427 $result = $this->contract->fetch($id);
429 throw new RestException(404,
'Contrat not found');
433 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
438 if ($updateRes > 0) {
439 $result = $this->
get($id);
440 unset($result->line);
463 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
464 throw new RestException(401);
467 $result = $this->contract->fetch($id);
469 throw new RestException(404,
'Contrat not found');
473 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
478 $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
479 if ($updateRes > 0) {
480 return $this->
get($id);
482 throw new RestException(405, $this->contract->error);
494 public function put($id, $request_data =
null)
496 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
497 throw new RestException(401);
500 $result = $this->contract->fetch($id);
502 throw new RestException(404,
'Contrat not found');
506 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 foreach ($request_data as $field => $value) {
509 if ($field ==
'id') {
512 $this->contract->$field = $value;
515 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
516 return $this->
get($id);
518 throw new RestException(500, $this->contract->error);
529 public function delete($id)
531 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
532 throw new RestException(401);
534 $result = $this->contract->fetch($id);
536 throw new RestException(404,
'Contract not found');
540 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
543 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
544 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
550 'message' =>
'Contract deleted'
573 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
574 throw new RestException(401);
576 $result = $this->contract->fetch($id);
578 throw new RestException(404,
'Contract not found');
582 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
585 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
587 throw new RestException(304,
'Error nothing done. May be object is already validated');
590 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
596 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
617 public function close($id, $notrigger = 0)
619 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
620 throw new RestException(401);
622 $result = $this->contract->fetch($id);
624 throw new RestException(404,
'Contract not found');
628 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
631 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
633 throw new RestException(304,
'Error nothing done. May be object is already close');
636 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
642 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
659 $object = parent::_cleanObjectDatas($object);
661 unset($object->address);
662 unset($object->civility_id);
677 foreach (Contracts::$FIELDS as $field) {
678 if (!isset($data[$field])) {
679 throw new RestException(400,
"$field field missing");
681 $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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.