dolibarr  7.0.0-beta
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
22 
29 class Contracts extends DolibarrApi
30 {
31 
35  static $FIELDS = array(
36  'socid',
37  'date_contrat',
38  'commercial_signature_id',
39  'commercial_suivi_id'
40  );
41 
45  public $contract;
46 
50  function __construct()
51  {
52  global $db, $conf;
53  $this->db = $db;
54  $this->contract = new Contrat($this->db);
55  }
56 
67  function get($id)
68  {
69  if(! DolibarrApiAccess::$user->rights->contrat->lire) {
70  throw new RestException(401);
71  }
72 
73  $result = $this->contract->fetch($id);
74  if( ! $result ) {
75  throw new RestException(404, 'Contract not found');
76  }
77 
78  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
79  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
80  }
81 
82  $this->contract->fetchObjectLinked();
83  return $this->_cleanObjectDatas($this->contract);
84  }
85 
86 
87 
103  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') {
104  global $db, $conf;
105 
106  $obj_ret = array();
107 
108  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
109  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
110 
111  // If the internal user must only see his customers, force searching by him
112  $search_sale = 0;
113  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
114 
115  $sql = "SELECT t.rowid";
116  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)
117  $sql.= " FROM ".MAIN_DB_PREFIX."contrat as t";
118 
119  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
120 
121  $sql.= ' WHERE t.entity IN ('.getEntity('contrat').')';
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
123  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
124  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
125  // Insert sale filter
126  if ($search_sale > 0)
127  {
128  $sql .= " AND sc.fk_user = ".$search_sale;
129  }
130  // Add sql filters
131  if ($sqlfilters)
132  {
133  if (! DolibarrApi::_checkFilters($sqlfilters))
134  {
135  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
136  }
137  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
138  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
139  }
140 
141  $sql.= $db->order($sortfield, $sortorder);
142  if ($limit) {
143  if ($page < 0)
144  {
145  $page = 0;
146  }
147  $offset = $limit * $page;
148 
149  $sql.= $db->plimit($limit + 1, $offset);
150  }
151 
152  dol_syslog("API Rest request");
153  $result = $db->query($sql);
154 
155  if ($result)
156  {
157  $num = $db->num_rows($result);
158  $min = min($num, ($limit <= 0 ? $num : $limit));
159  $i=0;
160  while ($i < $min)
161  {
162  $obj = $db->fetch_object($result);
163  $contrat_static = new Contrat($db);
164  if($contrat_static->fetch($obj->rowid)) {
165  $obj_ret[] = $this->_cleanObjectDatas($contrat_static);
166  }
167  $i++;
168  }
169  }
170  else {
171  throw new RestException(503, 'Error when retrieve contrat list : '.$db->lasterror());
172  }
173  if( ! count($obj_ret)) {
174  throw new RestException(404, 'No contract found');
175  }
176  return $obj_ret;
177  }
178 
185  function post($request_data = NULL)
186  {
187  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
188  throw new RestException(401, "Insuffisant rights");
189  }
190  // Check mandatory fields
191  $result = $this->_validate($request_data);
192 
193  foreach($request_data as $field => $value) {
194  $this->contract->$field = $value;
195  }
196  /*if (isset($request_data["lines"])) {
197  $lines = array();
198  foreach ($request_data["lines"] as $line) {
199  array_push($lines, (object) $line);
200  }
201  $this->contract->lines = $lines;
202  }*/
203  if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
204  throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
205  }
206 
207  return $this->contract->id;
208  }
209 
219  function getLines($id) {
220  if(! DolibarrApiAccess::$user->rights->contrat->lire) {
221  throw new RestException(401);
222  }
223 
224  $result = $this->contract->fetch($id);
225  if( ! $result ) {
226  throw new RestException(404, 'Contract not found');
227  }
228 
229  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
230  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
231  }
232  $this->contract->getLinesArray();
233  $result = array();
234  foreach ($this->contract->lines as $line) {
235  array_push($result,$this->_cleanObjectDatas($line));
236  }
237  return $result;
238  }
239 
250  function postLine($id, $request_data = NULL) {
251  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
252  throw new RestException(401);
253  }
254 
255  $result = $this->contract->fetch($id);
256  if( ! $result ) {
257  throw new RestException(404, 'Contract not found');
258  }
259 
260  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
261  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
262  }
263  $request_data = (object) $request_data;
264  $updateRes = $this->contract->addline(
265  $request_data->desc,
266  $request_data->subprice,
267  $request_data->qty,
268  $request_data->tva_tx,
269  $request_data->localtax1_tx,
270  $request_data->localtax2_tx,
271  $request_data->fk_product,
272  $request_data->remise_percent,
273  $request_data->date_start, // date_start = date planned start, date ouverture = date_start_real
274  $request_data->date_end, // date_end = date planned end, date_cloture = date_end_real
275  $request_data->HT,
276  $request_data->subprice_excl_tax,
277  $request_data->info_bits,
278  $request_data->fk_fournprice,
279  $request_data->pa_ht,
280  $request_data->array_options,
281  $request_data->fk_unit,
282  $request_data->rang
283  );
284 
285  if ($updateRes > 0) {
286  return $updateRes;
287 
288  }
289  return false;
290  }
291 
303  function putLine($id, $lineid, $request_data = NULL) {
304  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
305  throw new RestException(401);
306  }
307 
308  $result = $this->contract->fetch($id);
309  if( ! $result ) {
310  throw new RestException(404, 'Contrat not found');
311  }
312 
313  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
314  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
315  }
316 
317  $request_data = (object) $request_data;
318 
319  $updateRes = $this->contract->updateline(
320  $lineid,
321  $request_data->desc,
322  $request_data->subprice,
323  $request_data->qty,
324  $request_data->remise_percent,
325  $request_data->date_ouveture_prevue,
326  $request_data->date_fin_validite,
327  $request_data->tva_tx,
328  $request_data->localtax1_tx,
329  $request_data->localtax2_tx,
330  $request_data->date_ouverture,
331  $request_data->date_cloture,
332  'HT',
333  $request_data->info_bits,
334  $request_data->fk_fourn_price,
335  $request_data->pa_ht,
336  $request_data->array_options,
337  $request_data->fk_unit
338  );
339 
340  if ($updateRes > 0) {
341  $result = $this->get($id);
342  unset($result->line);
343  return $this->_cleanObjectDatas($result);
344  }
345 
346  return false;
347  }
348 
362  function activateLine($id, $lineid, $datestart, $dateend = NULL, $comment = NULL) {
363  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
364  throw new RestException(401);
365  }
366 
367  $result = $this->contract->fetch($id);
368  if( ! $result ) {
369  throw new RestException(404, 'Contrat not found');
370  }
371 
372  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
373  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
374  }
375 
376  $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
377 
378  if ($updateRes > 0) {
379  $result = $this->get($id);
380  unset($result->line);
381  return $this->_cleanObjectDatas($result);
382  }
383 
384  return false;
385  }
386 
399  function unactivateLine($id, $lineid, $datestart, $comment = NULL) {
400  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
401  throw new RestException(401);
402  }
403 
404  $result = $this->contract->fetch($id);
405  if( ! $result ) {
406  throw new RestException(404, 'Contrat not found');
407  }
408 
409  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
410  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
411  }
412 
413  $request_data = (object) $request_data;
414 
415  $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
416 
417  if ($updateRes > 0) {
418  $result = $this->get($id);
419  unset($result->line);
420  return $this->_cleanObjectDatas($result);
421  }
422 
423  return false;
424  }
425 
439  function deleteLine($id, $lineid) {
440  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
441  throw new RestException(401);
442  }
443 
444  $result = $this->contract->fetch($id);
445  if( ! $result ) {
446  throw new RestException(404, 'Contrat not found');
447  }
448 
449  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
450  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
451  }
452 
453  // TODO Check the lineid $lineid is a line of ojbect
454 
455  $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
456  if ($updateRes > 0) {
457  return $this->get($id);
458  }
459  else
460  {
461  throw new RestException(405, $this->contract->error);
462  }
463  }
464 
473  function put($id, $request_data = NULL) {
474  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
475  throw new RestException(401);
476  }
477 
478  $result = $this->contract->fetch($id);
479  if( ! $result ) {
480  throw new RestException(404, 'Contrat not found');
481  }
482 
483  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
484  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
485  }
486  foreach($request_data as $field => $value) {
487  if ($field == 'id') continue;
488  $this->contract->$field = $value;
489  }
490 
491  if ($this->contract->update(DolibarrApiAccess::$user) > 0)
492  {
493  return $this->get($id);
494  }
495  else
496  {
497  throw new RestException(500, $this->contract->error);
498  }
499  }
500 
508  function delete($id)
509  {
510  if(! DolibarrApiAccess::$user->rights->contrat->supprimer) {
511  throw new RestException(401);
512  }
513  $result = $this->contract->fetch($id);
514  if( ! $result ) {
515  throw new RestException(404, 'Contract not found');
516  }
517 
518  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
519  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
520  }
521 
522  if( ! $this->contract->delete(DolibarrApiAccess::$user)) {
523  throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
524  }
525 
526  return array(
527  'success' => array(
528  'code' => 200,
529  'message' => 'Contract deleted'
530  )
531  );
532 
533  }
534 
551  function validate($id, $notrigger=0)
552  {
553  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
554  throw new RestException(401);
555  }
556  $result = $this->contract->fetch($id);
557  if( ! $result ) {
558  throw new RestException(404, 'Contract not found');
559  }
560 
561  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
562  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
563  }
564 
565  $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
566  if ($result == 0) {
567  throw new RestException(304, 'Error nothing done. May be object is already validated');
568  }
569  if ($result < 0) {
570  throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
571  }
572 
573  return array(
574  'success' => array(
575  'code' => 200,
576  'message' => 'Contract validated (Ref='.$this->contract->ref.')'
577  )
578  );
579  }
580 
597  function close($id, $notrigger=0)
598  {
599  if(! DolibarrApiAccess::$user->rights->contrat->creer) {
600  throw new RestException(401);
601  }
602  $result = $this->contract->fetch($id);
603  if( ! $result ) {
604  throw new RestException(404, 'Contract not found');
605  }
606 
607  if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) {
608  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
609  }
610 
611  $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
612  if ($result == 0) {
613  throw new RestException(304, 'Error nothing done. May be object is already close');
614  }
615  if ($result < 0) {
616  throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
617  }
618 
619  return array(
620  'success' => array(
621  'code' => 200,
622  'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
623  )
624  );
625  }
626 
627 
628 
635  function _cleanObjectDatas($object) {
636 
637  $object = parent::_cleanObjectDatas($object);
638 
639  unset($object->address);
640 
641  unset($object->date_ouverture_prevue);
642  unset($object->date_ouverture);
643  unset($object->date_fin_validite);
644  unset($object->date_cloture);
645  unset($object->date_debut_prevue);
646  unset($object->date_debut_reel);
647  unset($object->date_fin_prevue);
648  unset($object->date_fin_reel);
649  unset($object->civility_id);
650 
651  return $object;
652  }
653 
661  function _validate($data)
662  {
663  $contrat = array();
664  foreach (Contracts::$FIELDS as $field) {
665  if (!isset($data[$field]))
666  throw new RestException(400, "$field field missing");
667  $contrat[$field] = $data[$field];
668 
669  }
670  return $contrat;
671  }
672 }
_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.
putLine($id, $lineid, $request_data=NULL)
Update a line to given contract.
__construct()
Constructor.
Class to manage contracts.
validate($id, $notrigger=0)
Validate an contract.
activateLine($id, $lineid, $datestart, $dateend=NULL, $comment=NULL)
Activate a service line of a given contract.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
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.
put($id, $request_data=NULL)
Update contract general fields (won't touch lines of contract)
postLine($id, $request_data=NULL)
Add a line to given contract.
unactivateLine($id, $lineid, $datestart, $comment=NULL)
Unactivate a service line of a given contract.
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:221
deleteLine($id, $lineid)
Delete a line to given contract.