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 =
'')
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
118 $sql =
"SELECT t.rowid";
119 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
120 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as t";
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
124 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
125 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
126 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
127 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
129 if ($search_sale > 0)
131 $sql .=
" AND sc.fk_user = ".$search_sale;
138 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
140 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
141 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
144 $sql .= $this->
db->order($sortfield, $sortorder);
150 $offset = $limit * $page;
152 $sql .= $this->
db->plimit($limit + 1, $offset);
156 $result = $this->
db->query($sql);
160 $num = $this->
db->num_rows($result);
161 $min = min($num, ($limit <= 0 ? $num : $limit));
165 $obj = $this->
db->fetch_object($result);
166 $contrat_static =
new Contrat($this->
db);
167 if ($contrat_static->fetch($obj->rowid)) {
173 throw new RestException(503,
'Error when retrieve contrat list : '.$this->
db->lasterror());
175 if (!count($obj_ret)) {
176 throw new RestException(404,
'No contract found');
187 public function post($request_data = null)
189 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
190 throw new RestException(401,
"Insufficient rights");
193 $result = $this->
_validate($request_data);
195 foreach ($request_data as $field => $value) {
196 $this->contract->$field = $value;
205 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
206 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
209 return $this->contract->id;
223 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
224 throw new RestException(401);
227 $result = $this->contract->fetch($id);
229 throw new RestException(404,
'Contract not found');
233 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
235 $this->contract->getLinesArray();
237 foreach ($this->contract->lines as $line) {
253 public function postLine($id, $request_data = null)
255 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
256 throw new RestException(401);
259 $result = $this->contract->fetch($id);
261 throw new RestException(404,
'Contract not found');
265 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
267 $request_data = (object) $request_data;
268 $updateRes = $this->contract->addline(
270 $request_data->subprice,
272 $request_data->tva_tx,
273 $request_data->localtax1_tx,
274 $request_data->localtax2_tx,
275 $request_data->fk_product,
276 $request_data->remise_percent,
277 $request_data->date_start,
278 $request_data->date_end,
280 $request_data->subprice_excl_tax,
281 $request_data->info_bits,
282 $request_data->fk_fournprice,
283 $request_data->pa_ht,
284 $request_data->array_options,
285 $request_data->fk_unit,
289 if ($updateRes > 0) {
306 public function putLine($id, $lineid, $request_data = null)
308 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
309 throw new RestException(401);
312 $result = $this->contract->fetch($id);
314 throw new RestException(404,
'Contrat not found');
318 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
321 $request_data = (object) $request_data;
323 $updateRes = $this->contract->updateline(
326 $request_data->subprice,
328 $request_data->remise_percent,
329 $request_data->date_ouveture_prevue,
330 $request_data->date_fin_validite,
331 $request_data->tva_tx,
332 $request_data->localtax1_tx,
333 $request_data->localtax2_tx,
334 $request_data->date_ouverture,
335 $request_data->date_cloture,
337 $request_data->info_bits,
338 $request_data->fk_fourn_price,
339 $request_data->pa_ht,
340 $request_data->array_options,
341 $request_data->fk_unit
344 if ($updateRes > 0) {
345 $result = $this->
get($id);
346 unset($result->line);
366 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
368 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
369 throw new RestException(401);
372 $result = $this->contract->fetch($id);
374 throw new RestException(404,
'Contrat not found');
378 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
381 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
383 if ($updateRes > 0) {
384 $result = $this->
get($id);
385 unset($result->line);
406 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
407 throw new RestException(401);
410 $result = $this->contract->fetch($id);
412 throw new RestException(404,
'Contrat not found');
416 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
421 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
423 if ($updateRes > 0) {
424 $result = $this->
get($id);
425 unset($result->line);
448 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
449 throw new RestException(401);
452 $result = $this->contract->fetch($id);
454 throw new RestException(404,
'Contrat not found');
458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
463 $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
464 if ($updateRes > 0) {
465 return $this->
get($id);
467 throw new RestException(405, $this->contract->error);
479 public function put($id, $request_data = null)
481 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
482 throw new RestException(401);
485 $result = $this->contract->fetch($id);
487 throw new RestException(404,
'Contrat not found');
491 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
493 foreach ($request_data as $field => $value) {
494 if ($field ==
'id')
continue;
495 $this->contract->$field = $value;
498 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
499 return $this->
get($id);
501 throw new RestException(500, $this->contract->error);
512 public function delete($id)
514 if (!DolibarrApiAccess::$user->rights->contrat->supprimer) {
515 throw new RestException(401);
517 $result = $this->contract->fetch($id);
519 throw new RestException(404,
'Contract not found');
523 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
526 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
527 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
533 'message' =>
'Contract deleted'
556 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
557 throw new RestException(401);
559 $result = $this->contract->fetch($id);
561 throw new RestException(404,
'Contract not found');
565 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
568 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
570 throw new RestException(304,
'Error nothing done. May be object is already validated');
573 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
579 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
600 public function close($id, $notrigger = 0)
602 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
603 throw new RestException(401);
605 $result = $this->contract->fetch($id);
607 throw new RestException(404,
'Contract not found');
611 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
614 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
616 throw new RestException(304,
'Error nothing done. May be object is already close');
619 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
625 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
642 $object = parent::_cleanObjectDatas($object);
644 unset($object->address);
646 unset($object->date_ouverture_prevue);
647 unset($object->date_ouverture);
648 unset($object->date_fin_validite);
649 unset($object->date_cloture);
650 unset($object->date_debut_prevue);
651 unset($object->date_debut_reel);
652 unset($object->date_fin_prevue);
653 unset($object->date_fin_reel);
654 unset($object->civility_id);
669 foreach (Contracts::$FIELDS as $field) {
670 if (!isset($data[$field]))
671 throw new RestException(400,
"$field field missing");
672 $contrat[$field] = $data[$field];
_cleanObjectDatas($object)
Clean sensible object datas.
close($id, $notrigger=0)
Close all services of a contract.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
Class to manage contracts.
validate($id, $notrigger=0)
Validate a contract.
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
getLines($id)
Get lines of a contract.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
post($request_data=null)
Create contract object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
postLine($id, $request_data=null)
Add a line to given contract.
deleteLine($id, $lineid)
Delete a line to given contract.
activateLine($id, $lineid, $datestart, $dateend=null, $comment=null)
Activate a service line of a given contract.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List contracts.
unactivateLine($id, $lineid, $datestart, $comment=null)
Unactivate a service line of a given contract.
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)