20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
35 public static $FIELDS = array(
38 'commercial_signature_id',
54 $this->contract =
new Contrat($this->db);
66 public function get($id)
68 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
69 throw new RestException(403);
72 $result = $this->contract->fetch($id);
74 throw new RestException(404,
'Contract not found');
78 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->contract->fetchObjectLinked();
104 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
108 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
109 throw new RestException(403);
115 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
119 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
120 $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
125 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
127 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
130 if ($search_sale && $search_sale !=
'-1') {
131 if ($search_sale == -2) {
132 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
133 } elseif ($search_sale > 0) {
134 $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).
")";
142 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
146 $sql .= $this->db->order($sortfield, $sortorder);
151 $offset = $limit * $page;
153 $sql .= $this->db->plimit($limit + 1, $offset);
157 $result = $this->db->query($sql);
160 $num = $this->db->num_rows($result);
161 $min = min($num, ($limit <= 0 ? $num : $limit));
164 $obj = $this->db->fetch_object($result);
165 $contrat_static =
new Contrat($this->db);
166 if ($contrat_static->fetch($obj->rowid)) {
172 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
184 public function post($request_data =
null)
186 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
187 throw new RestException(403,
"Insufficient rights");
190 $result = $this->
_validate($request_data);
192 foreach ($request_data as $field => $value) {
193 if ($field ===
'caller') {
195 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
199 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
208 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
209 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
212 return $this->contract->id;
226 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
227 throw new RestException(403);
230 $result = $this->contract->fetch($id);
232 throw new RestException(404,
'Contract not found');
236 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
238 $this->contract->getLinesArray();
240 foreach ($this->contract->lines as $line) {
256 public function postLine($id, $request_data =
null)
258 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
259 throw new RestException(403);
262 $result = $this->contract->fetch($id);
264 throw new RestException(404,
'Contract not found');
268 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
271 $request_data = (object) $request_data;
273 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
274 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
276 $updateRes = $this->contract->addline(
278 $request_data->subprice,
280 $request_data->tva_tx,
281 $request_data->localtax1_tx,
282 $request_data->localtax2_tx,
283 $request_data->fk_product,
284 $request_data->remise_percent,
285 $request_data->date_start,
286 $request_data->date_end,
287 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
288 $request_data->subprice_excl_tax,
289 $request_data->info_bits,
290 $request_data->fk_fournprice,
291 $request_data->pa_ht,
292 $request_data->array_options,
293 $request_data->fk_unit,
297 if ($updateRes > 0) {
314 public function putLine($id, $lineid, $request_data =
null)
316 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
317 throw new RestException(403);
320 $result = $this->contract->fetch($id);
322 throw new RestException(404,
'Contrat not found');
326 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 $request_data = (object) $request_data;
331 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
332 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
334 $updateRes = $this->contract->updateline(
337 $request_data->subprice,
339 $request_data->remise_percent,
340 $request_data->date_start,
341 $request_data->date_end,
342 $request_data->tva_tx,
343 $request_data->localtax1_tx,
344 $request_data->localtax2_tx,
345 $request_data->date_start_real,
346 $request_data->date_end_real,
347 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
348 $request_data->info_bits,
349 $request_data->fk_fourn_price,
350 $request_data->pa_ht,
351 $request_data->array_options,
352 $request_data->fk_unit
355 if ($updateRes > 0) {
356 $result = $this->
get($id);
357 unset($result->line);
377 public function activateLine($id, $lineid, $datestart, $dateend =
null, $comment =
null)
379 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
380 throw new RestException(403);
383 $result = $this->contract->fetch($id);
385 throw new RestException(404,
'Contrat not found');
389 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
392 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
394 if ($updateRes > 0) {
395 $result = $this->
get($id);
396 unset($result->line);
417 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
418 throw new RestException(403);
421 $result = $this->contract->fetch($id);
423 throw new RestException(404,
'Contrat not found');
427 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
430 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
432 if ($updateRes > 0) {
433 $result = $this->
get($id);
434 unset($result->line);
457 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
458 throw new RestException(403);
461 $result = $this->contract->fetch($id);
463 throw new RestException(404,
'Contrat not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
472 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
473 if ($updateRes > 0) {
474 return $this->
get($id);
476 throw new RestException(405, $this->contract->error);
487 public function put($id, $request_data =
null)
489 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
490 throw new RestException(403);
493 $result = $this->contract->fetch($id);
495 throw new RestException(404,
'Contrat not found');
499 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
501 foreach ($request_data as $field => $value) {
502 if ($field ==
'id') {
505 if ($field ===
'caller') {
507 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
510 if ($field ==
'array_options' && is_array($value)) {
511 foreach ($value as $index => $val) {
512 $this->contract->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contract);;
517 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
520 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
521 return $this->
get($id);
523 throw new RestException(500, $this->contract->error);
534 public function delete($id)
536 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
537 throw new RestException(403);
539 $result = $this->contract->fetch($id);
541 throw new RestException(404,
'Contract not found');
545 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
549 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
555 'message' =>
'Contract deleted'
578 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
579 throw new RestException(403);
581 $result = $this->contract->fetch($id);
583 throw new RestException(404,
'Contract not found');
587 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
592 throw new RestException(304,
'Error nothing done. May be object is already validated');
595 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
601 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
622 public function close($id, $notrigger = 0)
624 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
625 throw new RestException(403);
627 $result = $this->contract->fetch($id);
629 throw new RestException(404,
'Contract not found');
633 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
636 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
638 throw new RestException(304,
'Error nothing done. May be object is already close');
641 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
647 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
682 foreach (Contracts::$FIELDS as $field) {
683 if (!isset($data[$field])) {
684 throw new RestException(400,
"$field field missing");
686 $contrat[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List contracts.
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.
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.
_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.
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.