dolibarr  13.0.2
api_contracts.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20  use Luracast\Restler\RestException;
21 
22  require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
23 
30 class Contracts extends DolibarrApi
31 {
32 
36  static $FIELDS = array(
37  'socid',
38  'date_contrat',
39  'commercial_signature_id',
40  'commercial_suivi_id'
41  );
42 
46  public $contract;
47 
51  public function __construct()
52  {
53  global $db, $conf;
54  $this->db = $db;
55  $this->contract = new Contrat($this->db);
56  }
57 
68  public function get($id)
69  {
70  if (!DolibarrApiAccess::$user->rights->contrat->lire) {
71  throw new RestException(401);
72  }
73 
74  $result = $this->contract->fetch($id);
75  if (!$result) {
76  throw new RestException(404, 'Contract not found');
77  }
78 
79  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
80  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81  }
82 
83  $this->contract->fetchObjectLinked();
84  return $this->_cleanObjectDatas($this->contract);
85  }
86 
87 
88 
105  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
106  {
107  global $db, $conf;
108 
109  $obj_ret = array();
110 
111  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
112  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113 
114  // If the internal user must only see his customers, force searching by him
115  $search_sale = 0;
116  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
117 
118  $sql = "SELECT t.rowid";
119  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
120  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as t";
121 
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
123 
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"; // Join for the needed table to filter by sale
128  // Insert sale filter
129  if ($search_sale > 0)
130  {
131  $sql .= " AND sc.fk_user = ".$search_sale;
132  }
133  // Add sql filters
134  if ($sqlfilters)
135  {
136  if (!DolibarrApi::_checkFilters($sqlfilters))
137  {
138  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
139  }
140  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
141  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
142  }
143 
144  $sql .= $this->db->order($sortfield, $sortorder);
145  if ($limit) {
146  if ($page < 0)
147  {
148  $page = 0;
149  }
150  $offset = $limit * $page;
151 
152  $sql .= $this->db->plimit($limit + 1, $offset);
153  }
154 
155  dol_syslog("API Rest request");
156  $result = $this->db->query($sql);
157 
158  if ($result)
159  {
160  $num = $this->db->num_rows($result);
161  $min = min($num, ($limit <= 0 ? $num : $limit));
162  $i = 0;
163  while ($i < $min)
164  {
165  $obj = $this->db->fetch_object($result);
166  $contrat_static = new Contrat($this->db);
167  if ($contrat_static->fetch($obj->rowid)) {
168  $obj_ret[] = $this->_cleanObjectDatas($contrat_static);
169  }
170  $i++;
171  }
172  } else {
173  throw new RestException(503, 'Error when retrieve contrat list : '.$this->db->lasterror());
174  }
175  if (!count($obj_ret)) {
176  throw new RestException(404, 'No contract found');
177  }
178  return $obj_ret;
179  }
180 
187  public function post($request_data = null)
188  {
189  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
190  throw new RestException(401, "Insufficient rights");
191  }
192  // Check mandatory fields
193  $result = $this->_validate($request_data);
194 
195  foreach ($request_data as $field => $value) {
196  $this->contract->$field = $value;
197  }
198  /*if (isset($request_data["lines"])) {
199  $lines = array();
200  foreach ($request_data["lines"] as $line) {
201  array_push($lines, (object) $line);
202  }
203  $this->contract->lines = $lines;
204  }*/
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));
207  }
208 
209  return $this->contract->id;
210  }
211 
221  public function getLines($id)
222  {
223  if (!DolibarrApiAccess::$user->rights->contrat->lire) {
224  throw new RestException(401);
225  }
226 
227  $result = $this->contract->fetch($id);
228  if (!$result) {
229  throw new RestException(404, 'Contract not found');
230  }
231 
232  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
233  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234  }
235  $this->contract->getLinesArray();
236  $result = array();
237  foreach ($this->contract->lines as $line) {
238  array_push($result, $this->_cleanObjectDatas($line));
239  }
240  return $result;
241  }
242 
253  public function postLine($id, $request_data = null)
254  {
255  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
256  throw new RestException(401);
257  }
258 
259  $result = $this->contract->fetch($id);
260  if (!$result) {
261  throw new RestException(404, 'Contract not found');
262  }
263 
264  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
265  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
266  }
267  $request_data = (object) $request_data;
268  $updateRes = $this->contract->addline(
269  $request_data->desc,
270  $request_data->subprice,
271  $request_data->qty,
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, // date_start = date planned start, date ouverture = date_start_real
278  $request_data->date_end, // date_end = date planned end, date_cloture = date_end_real
279  $request_data->HT,
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,
286  $request_data->rang
287  );
288 
289  if ($updateRes > 0) {
290  return $updateRes;
291  }
292  return false;
293  }
294 
306  public function putLine($id, $lineid, $request_data = null)
307  {
308  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
309  throw new RestException(401);
310  }
311 
312  $result = $this->contract->fetch($id);
313  if (!$result) {
314  throw new RestException(404, 'Contrat not found');
315  }
316 
317  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
318  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
319  }
320 
321  $request_data = (object) $request_data;
322 
323  $updateRes = $this->contract->updateline(
324  $lineid,
325  $request_data->desc,
326  $request_data->subprice,
327  $request_data->qty,
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,
336  'HT',
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
342  );
343 
344  if ($updateRes > 0) {
345  $result = $this->get($id);
346  unset($result->line);
347  return $this->_cleanObjectDatas($result);
348  }
349 
350  return false;
351  }
352 
366  public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
367  {
368  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
369  throw new RestException(401);
370  }
371 
372  $result = $this->contract->fetch($id);
373  if (!$result) {
374  throw new RestException(404, 'Contrat not found');
375  }
376 
377  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
378  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
379  }
380 
381  $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
382 
383  if ($updateRes > 0) {
384  $result = $this->get($id);
385  unset($result->line);
386  return $this->_cleanObjectDatas($result);
387  }
388 
389  return false;
390  }
391 
404  public function unactivateLine($id, $lineid, $datestart, $comment = null)
405  {
406  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
407  throw new RestException(401);
408  }
409 
410  $result = $this->contract->fetch($id);
411  if (!$result) {
412  throw new RestException(404, 'Contrat not found');
413  }
414 
415  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
416  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
417  }
418 
419  // $request_data = (object) $request_data;
420 
421  $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
422 
423  if ($updateRes > 0) {
424  $result = $this->get($id);
425  unset($result->line);
426  return $this->_cleanObjectDatas($result);
427  }
428 
429  return false;
430  }
431 
446  public function deleteLine($id, $lineid)
447  {
448  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
449  throw new RestException(401);
450  }
451 
452  $result = $this->contract->fetch($id);
453  if (!$result) {
454  throw new RestException(404, 'Contrat not found');
455  }
456 
457  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
458  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
459  }
460 
461  // TODO Check the lineid $lineid is a line of object
462 
463  $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
464  if ($updateRes > 0) {
465  return $this->get($id);
466  } else {
467  throw new RestException(405, $this->contract->error);
468  }
469  }
470 
479  public function put($id, $request_data = null)
480  {
481  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
482  throw new RestException(401);
483  }
484 
485  $result = $this->contract->fetch($id);
486  if (!$result) {
487  throw new RestException(404, 'Contrat not found');
488  }
489 
490  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
491  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
492  }
493  foreach ($request_data as $field => $value) {
494  if ($field == 'id') continue;
495  $this->contract->$field = $value;
496  }
497 
498  if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
499  return $this->get($id);
500  } else {
501  throw new RestException(500, $this->contract->error);
502  }
503  }
504 
512  public function delete($id)
513  {
514  if (!DolibarrApiAccess::$user->rights->contrat->supprimer) {
515  throw new RestException(401);
516  }
517  $result = $this->contract->fetch($id);
518  if (!$result) {
519  throw new RestException(404, 'Contract not found');
520  }
521 
522  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
523  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
524  }
525 
526  if (!$this->contract->delete(DolibarrApiAccess::$user)) {
527  throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
528  }
529 
530  return array(
531  'success' => array(
532  'code' => 200,
533  'message' => 'Contract deleted'
534  )
535  );
536  }
537 
554  public function validate($id, $notrigger = 0)
555  {
556  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
557  throw new RestException(401);
558  }
559  $result = $this->contract->fetch($id);
560  if (!$result) {
561  throw new RestException(404, 'Contract not found');
562  }
563 
564  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
565  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
566  }
567 
568  $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
569  if ($result == 0) {
570  throw new RestException(304, 'Error nothing done. May be object is already validated');
571  }
572  if ($result < 0) {
573  throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
574  }
575 
576  return array(
577  'success' => array(
578  'code' => 200,
579  'message' => 'Contract validated (Ref='.$this->contract->ref.')'
580  )
581  );
582  }
583 
600  public function close($id, $notrigger = 0)
601  {
602  if (!DolibarrApiAccess::$user->rights->contrat->creer) {
603  throw new RestException(401);
604  }
605  $result = $this->contract->fetch($id);
606  if (!$result) {
607  throw new RestException(404, 'Contract not found');
608  }
609 
610  if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
611  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612  }
613 
614  $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
615  if ($result == 0) {
616  throw new RestException(304, 'Error nothing done. May be object is already close');
617  }
618  if ($result < 0) {
619  throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
620  }
621 
622  return array(
623  'success' => array(
624  'code' => 200,
625  'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
626  )
627  );
628  }
629 
630 
631 
632  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
639  protected function _cleanObjectDatas($object)
640  {
641  // phpcs:enable
642  $object = parent::_cleanObjectDatas($object);
643 
644  unset($object->address);
645 
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);
655 
656  return $object;
657  }
658 
666  private function _validate($data)
667  {
668  $contrat = array();
669  foreach (Contracts::$FIELDS as $field) {
670  if (!isset($data[$field]))
671  throw new RestException(400, "$field field missing");
672  $contrat[$field] = $data[$field];
673  }
674  return $contrat;
675  }
676 }
_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.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
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.
Definition: api.class.php:252
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&#39;t touch lines of contract)