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);
153 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
157 $sql .= $this->
db->order($sortfield, $sortorder);
162 $offset = $limit * $page;
164 $sql .= $this->
db->plimit($limit + 1, $offset);
168 $result = $this->
db->query($sql);
171 $num = $this->
db->num_rows($result);
172 $min = min($num, ($limit <= 0 ? $num : $limit));
175 $obj = $this->
db->fetch_object($result);
176 $contrat_static =
new Contrat($this->
db);
177 if ($contrat_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve contrat list : '.$this->
db->lasterror());
185 if (!count($obj_ret)) {
186 throw new RestException(404,
'No contract found');
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
200 throw new RestException(401,
"Insufficient rights");
203 $result = $this->
_validate($request_data);
205 foreach ($request_data as $field => $value) {
206 $this->contract->$field = $value;
215 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
216 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
219 return $this->contract->id;
233 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
234 throw new RestException(401);
237 $result = $this->contract->fetch($id);
239 throw new RestException(404,
'Contract not found');
243 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
245 $this->contract->getLinesArray();
247 foreach ($this->contract->lines as $line) {
263 public function postLine($id, $request_data =
null)
265 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
266 throw new RestException(401);
269 $result = $this->contract->fetch($id);
271 throw new RestException(404,
'Contract not found');
275 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
278 $request_data = (object) $request_data;
280 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
281 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
283 $updateRes = $this->contract->addline(
285 $request_data->subprice,
287 $request_data->tva_tx,
288 $request_data->localtax1_tx,
289 $request_data->localtax2_tx,
290 $request_data->fk_product,
291 $request_data->remise_percent,
292 $request_data->date_start,
293 $request_data->date_end,
294 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
295 $request_data->subprice_excl_tax,
296 $request_data->info_bits,
297 $request_data->fk_fournprice,
298 $request_data->pa_ht,
299 $request_data->array_options,
300 $request_data->fk_unit,
304 if ($updateRes > 0) {
321 public function putLine($id, $lineid, $request_data =
null)
323 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
324 throw new RestException(401);
327 $result = $this->contract->fetch($id);
329 throw new RestException(404,
'Contrat not found');
333 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
336 $request_data = (object) $request_data;
338 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
339 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
341 $updateRes = $this->contract->updateline(
344 $request_data->subprice,
346 $request_data->remise_percent,
347 $request_data->date_start,
348 $request_data->date_end,
349 $request_data->tva_tx,
350 $request_data->localtax1_tx,
351 $request_data->localtax2_tx,
352 $request_data->date_start_real,
353 $request_data->date_end_real,
354 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
355 $request_data->info_bits,
356 $request_data->fk_fourn_price,
357 $request_data->pa_ht,
358 $request_data->array_options,
359 $request_data->fk_unit
362 if ($updateRes > 0) {
363 $result = $this->
get($id);
364 unset($result->line);
384 public function activateLine($id, $lineid, $datestart, $dateend =
null, $comment =
null)
386 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
387 throw new RestException(401);
390 $result = $this->contract->fetch($id);
392 throw new RestException(404,
'Contrat not found');
396 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
399 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
401 if ($updateRes > 0) {
402 $result = $this->
get($id);
403 unset($result->line);
424 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
425 throw new RestException(401);
428 $result = $this->contract->fetch($id);
430 throw new RestException(404,
'Contrat not found');
434 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
437 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
439 if ($updateRes > 0) {
440 $result = $this->
get($id);
441 unset($result->line);
464 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
465 throw new RestException(401);
468 $result = $this->contract->fetch($id);
470 throw new RestException(404,
'Contrat not found');
474 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
480 if ($updateRes > 0) {
481 return $this->
get($id);
483 throw new RestException(405, $this->contract->error);
495 public function put($id, $request_data =
null)
497 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
498 throw new RestException(401);
501 $result = $this->contract->fetch($id);
503 throw new RestException(404,
'Contrat not found');
507 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
509 foreach ($request_data as $field => $value) {
510 if ($field ==
'id') {
513 $this->contract->$field = $value;
516 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
517 return $this->
get($id);
519 throw new RestException(500, $this->contract->error);
530 public function delete($id)
532 if (!DolibarrApiAccess::$user->rights->contrat->supprimer) {
533 throw new RestException(401);
535 $result = $this->contract->fetch($id);
537 throw new RestException(404,
'Contract not found');
541 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
544 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
545 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
551 'message' =>
'Contract deleted'
574 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
575 throw new RestException(401);
577 $result = $this->contract->fetch($id);
579 throw new RestException(404,
'Contract not found');
583 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
586 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
588 throw new RestException(304,
'Error nothing done. May be object is already validated');
591 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
597 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
618 public function close($id, $notrigger = 0)
620 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
621 throw new RestException(401);
623 $result = $this->contract->fetch($id);
625 throw new RestException(404,
'Contract not found');
629 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
632 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
634 throw new RestException(304,
'Error nothing done. May be object is already close');
637 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
643 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
660 $object = parent::_cleanObjectDatas($object);
662 unset($object->address);
663 unset($object->civility_id);
678 foreach (Contracts::$FIELDS as $field) {
679 if (!isset($data[$field])) {
680 throw new RestException(400,
"$field field missing");
682 $contrat[$field] = $data[$field];