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();
103 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
107 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
108 throw new RestException(403);
114 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
118 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
119 $search_sale = DolibarrApiAccess::$user->id;
122 $sql =
"SELECT t.rowid";
123 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
124 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
126 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
129 if ($search_sale && $search_sale !=
'-1') {
130 if ($search_sale == -2) {
131 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
132 } elseif ($search_sale > 0) {
133 $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).
")";
141 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
146 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
148 $sql .= $this->db->order($sortfield, $sortorder);
153 $offset = $limit * $page;
155 $sql .= $this->db->plimit($limit + 1, $offset);
159 $result = $this->db->query($sql);
162 $num = $this->db->num_rows($result);
163 $min = min($num, ($limit <= 0 ? $num : $limit));
166 $obj = $this->db->fetch_object($result);
167 $contrat_static =
new Contrat($this->db);
168 if ($contrat_static->fetch($obj->rowid)) {
174 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
178 if ($pagination_data) {
179 $totalsResult = $this->db->query($sqlTotals);
180 $total = $this->db->fetch_object($totalsResult)->total;
185 $obj_ret[
'data'] = $tmp;
186 $obj_ret[
'pagination'] = [
187 'total' => (int) $total,
189 'page_count' => ceil((
int) $total / $limit),
203 public function post($request_data =
null)
205 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
206 throw new RestException(403,
"Insufficient rights");
209 $result = $this->
_validate($request_data);
211 foreach ($request_data as $field => $value) {
212 if ($field ===
'caller') {
214 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
218 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
227 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
228 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
231 return $this->contract->id;
245 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
246 throw new RestException(403);
249 $result = $this->contract->fetch(
$id);
251 throw new RestException(404,
'Contract not found');
255 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
257 $this->contract->getLinesArray();
259 foreach ($this->contract->lines as $line) {
277 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
278 throw new RestException(403);
281 $result = $this->contract->fetch(
$id);
283 throw new RestException(404,
'Contract not found');
287 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
290 $request_data = (object) $request_data;
292 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
293 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
295 $updateRes = $this->contract->addline(
297 $request_data->subprice,
299 $request_data->tva_tx,
300 $request_data->localtax1_tx,
301 $request_data->localtax2_tx,
302 $request_data->fk_product,
303 $request_data->remise_percent,
304 $request_data->date_start,
305 $request_data->date_end,
306 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
307 $request_data->subprice_excl_tax,
308 $request_data->info_bits,
309 $request_data->fk_fournprice,
310 $request_data->pa_ht,
311 $request_data->array_options,
312 $request_data->fk_unit,
316 if ($updateRes > 0) {
335 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
336 throw new RestException(403);
339 $result = $this->contract->fetch(
$id);
341 throw new RestException(404,
'Contrat not found');
345 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
348 $request_data = (object) $request_data;
350 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
351 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
353 $updateRes = $this->contract->updateline(
356 $request_data->subprice,
358 $request_data->remise_percent,
359 $request_data->date_start,
360 $request_data->date_end,
361 $request_data->tva_tx,
362 $request_data->localtax1_tx,
363 $request_data->localtax2_tx,
364 $request_data->date_start_real,
365 $request_data->date_end_real,
366 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
367 $request_data->info_bits,
368 $request_data->fk_fourn_price,
369 $request_data->pa_ht,
370 $request_data->array_options,
371 $request_data->fk_unit
374 if ($updateRes > 0) {
375 $result = $this->
get(
$id);
376 unset($result->line);
396 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
398 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
399 throw new RestException(403);
402 $result = $this->contract->fetch(
$id);
404 throw new RestException(404,
'Contrat not found');
408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
411 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
413 if ($updateRes > 0) {
414 $result = $this->
get(
$id);
415 unset($result->line);
436 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
437 throw new RestException(403);
440 $result = $this->contract->fetch(
$id);
442 throw new RestException(404,
'Contrat not found');
446 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
449 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
451 if ($updateRes > 0) {
452 $result = $this->
get(
$id);
453 unset($result->line);
476 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
477 throw new RestException(403);
480 $result = $this->contract->fetch(
$id);
482 throw new RestException(404,
'Contrat not found');
486 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
492 if ($updateRes > 0) {
493 return $this->
get(
$id);
495 throw new RestException(405, $this->contract->error);
506 public function put(
$id, $request_data =
null)
508 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
509 throw new RestException(403);
512 $result = $this->contract->fetch(
$id);
514 throw new RestException(404,
'Contrat not found');
518 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 foreach ($request_data as $field => $value) {
521 if ($field ==
'id') {
524 if ($field ===
'caller') {
526 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
529 if ($field ==
'array_options' && is_array($value)) {
530 foreach ($value as $index => $val) {
531 $this->contract->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contract);;
536 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
539 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
540 return $this->
get(
$id);
542 throw new RestException(500, $this->contract->error);
553 public function delete(
$id)
555 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
556 throw new RestException(403);
558 $result = $this->contract->fetch(
$id);
560 throw new RestException(404,
'Contract not found');
564 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
567 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
568 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
574 'message' =>
'Contract deleted'
597 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
598 throw new RestException(403);
600 $result = $this->contract->fetch(
$id);
602 throw new RestException(404,
'Contract not found');
606 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
609 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
611 throw new RestException(304,
'Error nothing done. May be object is already validated');
614 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
620 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
643 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
644 throw new RestException(403);
646 $result = $this->contract->fetch(
$id);
648 throw new RestException(404,
'Contract not found');
652 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
655 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
657 throw new RestException(304,
'Error nothing done. May be object is already close');
660 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
666 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
701 foreach (Contracts::$FIELDS as $field) {
702 if (!isset($data[$field])) {
703 throw new RestException(400,
"$field field missing");
705 $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='', $pagination_data=false)
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.
_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.