dolibarr 19.0.3
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
31{
35 public static $FIELDS = array(
36 'socid',
37 'date_contrat',
38 'commercial_signature_id',
39 'commercial_suivi_id'
40 );
41
45 public $contract;
46
50 public function __construct()
51 {
52 global $db, $conf;
53 $this->db = $db;
54 $this->contract = new Contrat($this->db);
55 }
56
66 public function get($id)
67 {
68 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
69 throw new RestException(401);
70 }
71
72 $result = $this->contract->fetch($id);
73 if (!$result) {
74 throw new RestException(404, 'Contract not found');
75 }
76
77 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
78 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 }
80
81 $this->contract->fetchObjectLinked();
82 return $this->_cleanObjectDatas($this->contract);
83 }
84
85
86
104 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
105 {
106 global $db, $conf;
107
108 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
109 throw new RestException(401);
110 }
111
112 $obj_ret = array();
113
114 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
115 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116
117 // If the internal user must only see his customers, force searching by him
118 $search_sale = 0;
119 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
120 $search_sale = DolibarrApiAccess::$user->id;
121 }
122
123 $sql = "SELECT t.rowid";
124 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
125 $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)
126 }
127 $sql .= " FROM ".MAIN_DB_PREFIX."contrat AS t LEFT JOIN ".MAIN_DB_PREFIX."contrat_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
128
129 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
130 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
131 }
132
133 $sql .= ' WHERE t.entity IN ('.getEntity('contrat').')';
134 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
135 $sql .= " AND t.fk_soc = sc.fk_soc";
136 }
137 if ($socids) {
138 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
139 }
140 if ($search_sale > 0) {
141 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
142 }
143 // Insert sale filter
144 if ($search_sale > 0) {
145 $sql .= " AND sc.fk_user = ".((int) $search_sale);
146 }
147 // Add sql filters
148 if ($sqlfilters) {
149 $errormessage = '';
150 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
151 if ($errormessage) {
152 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
153 }
154 }
155
156 $sql .= $this->db->order($sortfield, $sortorder);
157 if ($limit) {
158 if ($page < 0) {
159 $page = 0;
160 }
161 $offset = $limit * $page;
162
163 $sql .= $this->db->plimit($limit + 1, $offset);
164 }
165
166 dol_syslog("API Rest request");
167 $result = $this->db->query($sql);
168
169 if ($result) {
170 $num = $this->db->num_rows($result);
171 $min = min($num, ($limit <= 0 ? $num : $limit));
172 $i = 0;
173 while ($i < $min) {
174 $obj = $this->db->fetch_object($result);
175 $contrat_static = new Contrat($this->db);
176 if ($contrat_static->fetch($obj->rowid)) {
177 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($contrat_static), $properties);
178 }
179 $i++;
180 }
181 } else {
182 throw new RestException(503, 'Error when retrieve contrat list : '.$this->db->lasterror());
183 }
184
185 return $obj_ret;
186 }
187
194 public function post($request_data = null)
195 {
196 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
197 throw new RestException(401, "Insufficient rights");
198 }
199 // Check mandatory fields
200 $result = $this->_validate($request_data);
201
202 foreach ($request_data as $field => $value) {
203 if ($field === 'caller') {
204 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
205 $this->contract->context['caller'] = $request_data['caller'];
206 continue;
207 }
208
209 $this->contract->$field = $value;
210 }
211 /*if (isset($request_data["lines"])) {
212 $lines = array();
213 foreach ($request_data["lines"] as $line) {
214 array_push($lines, (object) $line);
215 }
216 $this->contract->lines = $lines;
217 }*/
218 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
220 }
221
222 return $this->contract->id;
223 }
224
234 public function getLines($id)
235 {
236 if (!DolibarrApiAccess::$user->rights->contrat->lire) {
237 throw new RestException(401);
238 }
239
240 $result = $this->contract->fetch($id);
241 if (!$result) {
242 throw new RestException(404, 'Contract not found');
243 }
244
245 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
246 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
247 }
248 $this->contract->getLinesArray();
249 $result = array();
250 foreach ($this->contract->lines as $line) {
251 array_push($result, $this->_cleanObjectDatas($line));
252 }
253 return $result;
254 }
255
266 public function postLine($id, $request_data = null)
267 {
268 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
269 throw new RestException(401);
270 }
271
272 $result = $this->contract->fetch($id);
273 if (!$result) {
274 throw new RestException(404, 'Contract not found');
275 }
276
277 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
278 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
279 }
280
281 $request_data = (object) $request_data;
282
283 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
284 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
285
286 $updateRes = $this->contract->addline(
287 $request_data->desc,
288 $request_data->subprice,
289 $request_data->qty,
290 $request_data->tva_tx,
291 $request_data->localtax1_tx,
292 $request_data->localtax2_tx,
293 $request_data->fk_product,
294 $request_data->remise_percent,
295 $request_data->date_start,
296 $request_data->date_end,
297 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
298 $request_data->subprice_excl_tax,
299 $request_data->info_bits,
300 $request_data->fk_fournprice,
301 $request_data->pa_ht,
302 $request_data->array_options,
303 $request_data->fk_unit,
304 $request_data->rang
305 );
306
307 if ($updateRes > 0) {
308 return $updateRes;
309 }
310 return false;
311 }
312
324 public function putLine($id, $lineid, $request_data = null)
325 {
326 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
327 throw new RestException(401);
328 }
329
330 $result = $this->contract->fetch($id);
331 if (!$result) {
332 throw new RestException(404, 'Contrat not found');
333 }
334
335 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
336 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
337 }
338
339 $request_data = (object) $request_data;
340
341 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
342 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
343
344 $updateRes = $this->contract->updateline(
345 $lineid,
346 $request_data->desc,
347 $request_data->subprice,
348 $request_data->qty,
349 $request_data->remise_percent,
350 $request_data->date_start,
351 $request_data->date_end,
352 $request_data->tva_tx,
353 $request_data->localtax1_tx,
354 $request_data->localtax2_tx,
355 $request_data->date_start_real,
356 $request_data->date_end_real,
357 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
358 $request_data->info_bits,
359 $request_data->fk_fourn_price,
360 $request_data->pa_ht,
361 $request_data->array_options,
362 $request_data->fk_unit
363 );
364
365 if ($updateRes > 0) {
366 $result = $this->get($id);
367 unset($result->line);
368 return $this->_cleanObjectDatas($result);
369 }
370
371 return false;
372 }
373
387 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
388 {
389 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
390 throw new RestException(401);
391 }
392
393 $result = $this->contract->fetch($id);
394 if (!$result) {
395 throw new RestException(404, 'Contrat not found');
396 }
397
398 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
399 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
400 }
401
402 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
403
404 if ($updateRes > 0) {
405 $result = $this->get($id);
406 unset($result->line);
407 return $this->_cleanObjectDatas($result);
408 }
409
410 return false;
411 }
412
425 public function unactivateLine($id, $lineid, $datestart, $comment = null)
426 {
427 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
428 throw new RestException(401);
429 }
430
431 $result = $this->contract->fetch($id);
432 if (!$result) {
433 throw new RestException(404, 'Contrat not found');
434 }
435
436 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
437 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
438 }
439
440 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
441
442 if ($updateRes > 0) {
443 $result = $this->get($id);
444 unset($result->line);
445 return $this->_cleanObjectDatas($result);
446 }
447
448 return false;
449 }
450
465 public function deleteLine($id, $lineid)
466 {
467 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
468 throw new RestException(401);
469 }
470
471 $result = $this->contract->fetch($id);
472 if (!$result) {
473 throw new RestException(404, 'Contrat not found');
474 }
475
476 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
477 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
478 }
479
480 // TODO Check the lineid $lineid is a line of object
481
482 $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
483 if ($updateRes > 0) {
484 return $this->get($id);
485 } else {
486 throw new RestException(405, $this->contract->error);
487 }
488 }
489
498 public function put($id, $request_data = null)
499 {
500 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
501 throw new RestException(401);
502 }
503
504 $result = $this->contract->fetch($id);
505 if (!$result) {
506 throw new RestException(404, 'Contrat not found');
507 }
508
509 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
510 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
511 }
512 foreach ($request_data as $field => $value) {
513 if ($field == 'id') {
514 continue;
515 }
516 if ($field === 'caller') {
517 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
518 $this->contract->context['caller'] = $request_data['caller'];
519 continue;
520 }
521
522 $this->contract->$field = $value;
523 }
524
525 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
526 return $this->get($id);
527 } else {
528 throw new RestException(500, $this->contract->error);
529 }
530 }
531
539 public function delete($id)
540 {
541 if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) {
542 throw new RestException(401);
543 }
544 $result = $this->contract->fetch($id);
545 if (!$result) {
546 throw new RestException(404, 'Contract not found');
547 }
548
549 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
550 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
551 }
552
553 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
554 throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
555 }
556
557 return array(
558 'success' => array(
559 'code' => 200,
560 'message' => 'Contract deleted'
561 )
562 );
563 }
564
581 public function validate($id, $notrigger = 0)
582 {
583 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
584 throw new RestException(401);
585 }
586 $result = $this->contract->fetch($id);
587 if (!$result) {
588 throw new RestException(404, 'Contract not found');
589 }
590
591 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
592 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
593 }
594
595 $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
596 if ($result == 0) {
597 throw new RestException(304, 'Error nothing done. May be object is already validated');
598 }
599 if ($result < 0) {
600 throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
601 }
602
603 return array(
604 'success' => array(
605 'code' => 200,
606 'message' => 'Contract validated (Ref='.$this->contract->ref.')'
607 )
608 );
609 }
610
627 public function close($id, $notrigger = 0)
628 {
629 if (!DolibarrApiAccess::$user->rights->contrat->creer) {
630 throw new RestException(401);
631 }
632 $result = $this->contract->fetch($id);
633 if (!$result) {
634 throw new RestException(404, 'Contract not found');
635 }
636
637 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
638 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
639 }
640
641 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
642 if ($result == 0) {
643 throw new RestException(304, 'Error nothing done. May be object is already close');
644 }
645 if ($result < 0) {
646 throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
647 }
648
649 return array(
650 'success' => array(
651 'code' => 200,
652 'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
653 )
654 );
655 }
656
657
658
659 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
666 protected function _cleanObjectDatas($object)
667 {
668 // phpcs:enable
669 $object = parent::_cleanObjectDatas($object);
670
671 unset($object->address);
672 unset($object->civility_id);
673
674 return $object;
675 }
676
684 private function _validate($data)
685 {
686 $contrat = array();
687 foreach (Contracts::$FIELDS as $field) {
688 if (!isset($data[$field])) {
689 throw new RestException(400, "$field field missing");
690 }
691 $contrat[$field] = $data[$field];
692 }
693 return $contrat;
694 }
695}
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List contracts.
getLines($id)
Get lines of a contract.
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)
deleteLine($id, $lineid)
Delete a line to given contract.
_cleanObjectDatas($object)
Clean sensible object datas.
activateLine($id, $lineid, $datestart, $dateend=null, $comment=null)
Activate a service line of a given contract.
validate($id, $notrigger=0)
Validate a contract.
post($request_data=null)
Create contract object.
unactivateLine($id, $lineid, $datestart, $comment=null)
Unactivate a service line of a given contract.
__construct()
Constructor.
close($id, $notrigger=0)
Close all services of a contract.
postLine($id, $request_data=null)
Add a line to given contract.
Class to manage contracts.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.