dolibarr  9.0.0
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 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 <http://www.gnu.org/licenses/>.
18  */
19 
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  function __construct()
52  {
53  global $db, $conf;
54  $this->db = $db;
55  $this->contract = new Contrat($this->db);
56  }
57 
68  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 
104  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
105  {
106  global $db, $conf;
107 
108  $obj_ret = array();
109 
110  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
111  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
112 
113  // If the internal user must only see his customers, force searching by him
114  $search_sale = 0;
115  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
116 
117  $sql = "SELECT t.rowid";
118  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)
119  $sql.= " FROM ".MAIN_DB_PREFIX."contrat as t";
120 
121  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
122 
123  $sql.= ' WHERE t.entity IN ('.getEntity('contrat').')';
124  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
125  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
126  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
127  // Insert sale filter
128  if ($search_sale > 0)
129  {
130  $sql .= " AND sc.fk_user = ".$search_sale;
131  }
132  // Add sql filters
133  if ($sqlfilters)
134  {
135  if (! DolibarrApi::_checkFilters($sqlfilters))
136  {
137  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
138  }
139  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
140  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
141  }
142 
143  $sql.= $db->order($sortfield, $sortorder);
144  if ($limit) {
145  if ($page < 0)
146  {
147  $page = 0;
148  }
149  $offset = $limit * $page;
150 
151  $sql.= $db->plimit($limit + 1, $offset);
152  }
153 
154  dol_syslog("API Rest request");
155  $result = $db->query($sql);
156 
157  if ($result)
158  {
159  $num = $db->num_rows($result);
160  $min = min($num, ($limit <= 0 ? $num : $limit));
161  $i=0;
162  while ($i < $min)
163  {
164  $obj = $db->fetch_object($result);
165  $contrat_static = new Contrat($db);
166  if($contrat_static->fetch($obj->rowid)) {
167  $obj_ret[] = $this->_cleanObjectDatas($contrat_static);
168  }
169  $i++;
170  }
171  }
172  else {
173  throw new RestException(503, 'Error when retrieve contrat list : '.$db->lasterror());
174  }
175  if( ! count($obj_ret)) {
176  throw new RestException(404, 'No contract found');
177  }
178  return $obj_ret;
179  }
180 
187  function post($request_data = null)
188  {
189  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
190  throw new RestException(401, "Insuffisant 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  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  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  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  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  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 
445  function deleteLine($id, $lineid)
446  {
447  if (! DolibarrApiAccess::$user->rights->contrat->creer) {
448  throw new RestException(401);
449  }
450 
451  $result = $this->contract->fetch($id);
452  if (! $result) {
453  throw new RestException(404, 'Contrat not found');
454  }
455 
456  if (! DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
457  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
458  }
459 
460  // TODO Check the lineid $lineid is a line of object
461 
462  $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
463  if ($updateRes > 0) {
464  return $this->get($id);
465  }
466  else
467  {
468  throw new RestException(405, $this->contract->error);
469  }
470  }
471 
480  function put($id, $request_data = null)
481  {
482  if (! DolibarrApiAccess::$user->rights->contrat->creer) {
483  throw new RestException(401);
484  }
485 
486  $result = $this->contract->fetch($id);
487  if (! $result) {
488  throw new RestException(404, 'Contrat not found');
489  }
490 
491  if (! DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
492  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
493  }
494  foreach($request_data as $field => $value) {
495  if ($field == 'id') continue;
496  $this->contract->$field = $value;
497  }
498 
499  if ($this->contract->update(DolibarrApiAccess::$user) > 0)
500  {
501  return $this->get($id);
502  }
503  else
504  {
505  throw new RestException(500, $this->contract->error);
506  }
507  }
508 
516  function delete($id)
517  {
518  if (! DolibarrApiAccess::$user->rights->contrat->supprimer) {
519  throw new RestException(401);
520  }
521  $result = $this->contract->fetch($id);
522  if (! $result) {
523  throw new RestException(404, 'Contract not found');
524  }
525 
526  if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
527  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
528  }
529 
530  if (! $this->contract->delete(DolibarrApiAccess::$user)) {
531  throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
532  }
533 
534  return array(
535  'success' => array(
536  'code' => 200,
537  'message' => 'Contract deleted'
538  )
539  );
540  }
541 
558  function validate($id, $notrigger=0)
559  {
560  if (! DolibarrApiAccess::$user->rights->contrat->creer) {
561  throw new RestException(401);
562  }
563  $result = $this->contract->fetch($id);
564  if (! $result) {
565  throw new RestException(404, 'Contract not found');
566  }
567 
568  if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
569  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
570  }
571 
572  $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
573  if ($result == 0) {
574  throw new RestException(304, 'Error nothing done. May be object is already validated');
575  }
576  if ($result < 0) {
577  throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
578  }
579 
580  return array(
581  'success' => array(
582  'code' => 200,
583  'message' => 'Contract validated (Ref='.$this->contract->ref.')'
584  )
585  );
586  }
587 
604  function close($id, $notrigger=0)
605  {
606  if (! DolibarrApiAccess::$user->rights->contrat->creer) {
607  throw new RestException(401);
608  }
609  $result = $this->contract->fetch($id);
610  if (! $result) {
611  throw new RestException(404, 'Contract not found');
612  }
613 
614  if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
615  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
616  }
617 
618  $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
619  if ($result == 0) {
620  throw new RestException(304, 'Error nothing done. May be object is already close');
621  }
622  if ($result < 0) {
623  throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
624  }
625 
626  return array(
627  'success' => array(
628  'code' => 200,
629  'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
630  )
631  );
632  }
633 
634 
635 
642  function _cleanObjectDatas($object)
643  {
644 
645  $object = parent::_cleanObjectDatas($object);
646 
647  unset($object->address);
648 
649  unset($object->date_ouverture_prevue);
650  unset($object->date_ouverture);
651  unset($object->date_fin_validite);
652  unset($object->date_cloture);
653  unset($object->date_debut_prevue);
654  unset($object->date_debut_reel);
655  unset($object->date_fin_prevue);
656  unset($object->date_fin_reel);
657  unset($object->civility_id);
658 
659  return $object;
660  }
661 
669  function _validate($data)
670  {
671  $contrat = array();
672  foreach (Contracts::$FIELDS as $field) {
673  if (!isset($data[$field]))
674  throw new RestException(400, "$field field missing");
675  $contrat[$field] = $data[$field];
676  }
677  return $contrat;
678  }
679 }
_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.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getLines($id)
Get lines of a contract.
post($request_data=null)
Create contract object.
postLine($id, $request_data=null)
Add a line to given contract.
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:225
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.
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)