dolibarr 25.0.0-alpha
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-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\RestException;
22
23require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
24require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
25require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
26
34{
38 public static $FIELDS = array(
39 'socid',
40 'date_contrat'
41 );
42
46 public $contract;
47
51 public function __construct()
52 {
53 global $db;
54 $this->db = $db;
55 $this->contract = new Contrat($this->db);
56 }
57
72 public function get($id, $properties = '', $withLines = true)
73 {
74 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
75 throw new RestException(403);
76 }
77 if ($id == 0) {
78 throw new RestException(400, 'No contract with id=0 can exist');
79 }
80 $result = $this->contract->fetch($id);
81 if (!$result) {
82 throw new RestException(404, 'Contract not found');
83 }
84
85 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
86 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
87 }
88
89 $this->contract->fetchObjectLinked();
90
91 if (!$withLines) {
92 unset($this->contract->lines);
93 }
94
95 return $this->_filterObjectProperties($this->_cleanObjectDatas($this->contract), $properties);
96 }
97
120 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false, $withLines = true)
121 {
122 global $db, $conf;
123
124 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
125 throw new RestException(403);
126 }
127
128 $obj_ret = array();
129
130 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
131 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
132
133 // If the internal user must only see his customers, force searching by him
134 $search_sale = 0;
135 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
136 $search_sale = DolibarrApiAccess::$user->id;
137 }
138
139 $sql = "SELECT t.rowid";
140 $sql .= " FROM ".MAIN_DB_PREFIX."contrat AS t";
141 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = t.fk_soc)";
142 $sql .= " 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
143 $sql .= ' WHERE t.entity IN ('.getEntity('contrat').')';
144 if ($socids) {
145 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
146 }
147 // Search on sale representative
148 if ($search_sale && $search_sale != '-1') {
149 if ($search_sale == -2) {
150 $sql .= " AND ".getSalesRepresentativeSqlFilter('t.fk_soc', 0, 1);
151 } elseif ($search_sale > 0) {
152 $sql .= " AND ".getSalesRepresentativeSqlFilter('t.fk_soc', (int) $search_sale);
153 }
154 }
155 // Add sql filters
156 if ($sqlfilters) {
157 $errormessage = '';
158 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
159 if ($errormessage) {
160 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
161 }
162 }
163
164 //this query will return total orders with the filters given
165 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
166
167 $sql .= $this->db->order($sortfield, $sortorder);
168 if ($limit) {
169 if ($page < 0) {
170 $page = 0;
171 }
172 $offset = $limit * $page;
173
174 $sql .= $this->db->plimit($limit + 1, $offset);
175 }
176
177 $result = $this->db->query($sql);
178
179 if ($result) {
180 $num = $this->db->num_rows($result);
181 $min = min($num, ($limit <= 0 ? $num : $limit));
182 $i = 0;
183 while ($i < $min) {
184 $obj = $this->db->fetch_object($result);
185 $contrat_static = new Contrat($this->db);
186 if ($contrat_static->fetch($obj->rowid)) {
187 if (!$withLines) {
188 unset($contrat_static->lines);
189 }
190 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($contrat_static), $properties);
191 }
192 $i++;
193 }
194 } else {
195 throw new RestException(503, 'Error when retrieve contrat list : '.$this->db->lasterror());
196 }
197
198 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
199 if ($pagination_data) {
200 $totalsResult = $this->db->query($sqlTotals);
201 $total = $this->db->fetch_object($totalsResult)->total;
202
203 $tmp = $obj_ret;
204 $obj_ret = [];
205
206 $obj_ret['data'] = $tmp;
207 $obj_ret['pagination'] = [
208 'total' => (int) $total,
209 'page' => $page, //count starts from 0
210 'page_count' => ceil((int) $total / $limit),
211 'limit' => $limit
212 ];
213 }
214
215 return $obj_ret;
216 }
217
231 public function post($request_data = null)
232 {
233 global $conf;
234 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
235 throw new RestException(403, "Missing permission: Create/modify contracts/subscriptions");
236 }
237
238 // Check mandatory fields
239 $this->_validate($request_data);
240
241 // Check thirdparty validity
242 $socid = (int) $request_data['socid'];
243 $thirdpartytmp = new Societe($this->db);
244 $thirdparty_result = $thirdpartytmp->fetch($socid);
245 if ($thirdparty_result < 1) {
246 throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed');
247 }
248 if (!DolibarrApi::_checkAccessToResource('societe', $thirdpartytmp->id)) {
249 throw new RestException(404, 'Thirdparty with id='.$thirdpartytmp->id.' not found or not allowed');
250 }
251
252 foreach ($request_data as $field => $value) {
253 if ($field === 'caller') {
254 // 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 with the caller
255 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
256 continue;
257 }
258 if ($field == 'id') {
259 throw new RestException(400, 'Creating with id field is forbidden');
260 }
261 if ($field == 'entity' && ((int) $value) != ((int) $conf->entity)) {
262 throw new RestException(403, 'Creating with entity='.((int) $value).' MUST be the same entity='.((int) $conf->entity).' as your API user/key belongs to');
263 }
264
265 if ($field == 'socid') {
266 $thirdparty = new Societe($this->db);
267 $result = $thirdparty->fetch((int) $value);
268 if ($result < 1) {
269 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
270 }
271 }
272
273 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
274 }
275 /*if (isset($request_data["lines"])) {
276 $lines = array();
277 foreach ($request_data["lines"] as $line) {
278 array_push($lines, (object) $line);
279 }
280 $this->contract->lines = $lines;
281 }*/
282 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
284 }
285
286 return $this->contract->id;
287 }
288
309 public function getLines($id, $sortfield = "d.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
310 {
311 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
312 throw new RestException(403);
313 }
314
315 $result = $this->contract->fetch($id);
316 if (!$result) {
317 throw new RestException(404, 'Contract not found');
318 }
319
320 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
321 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
322 }
323
324 $obj_ret = [];
325
326 $sql = "SELECT d.rowid";
327 $sql .= " FROM ".$this->db->prefix()."contratdet AS d";
328 $sql .= " LEFT JOIN ".$this->db->prefix()."contrat AS c ON (c.rowid = d.fk_contrat)";
329 $sql .= " LEFT JOIN ".$this->db->prefix()."contratdet_extrafields AS ef ON (ef.fk_object = d.rowid)";
330 $sql .= " WHERE d.fk_contrat = ".((int) $id);
331 $sql .= ' AND c.entity IN ('.getEntity('contrat').')';
332
333 if ($sqlfilters) {
334 $errormessage = '';
335 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
336 if ($errormessage) {
337 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
338 }
339 }
340
341 $sqlTotals = str_replace('SELECT d.rowid', 'SELECT count(d.rowid) as total', $sql);
342
343 $sql .= $this->db->order($sortfield, $sortorder);
344 if ($limit) {
345 if ($page < 0) {
346 $page = 0;
347 }
348 $offset = $limit * $page;
349
350 $sql .= $this->db->plimit($limit + 1, $offset);
351 }
352
353 $result = $this->db->query($sql);
354 if ($result) {
355 $num = $this->db->num_rows($result);
356 $min = min($num, ($limit <= 0 ? $num : $limit));
357 $i = 0;
358 while ($i < $min) {
359 $obj = $this->db->fetch_object($result);
360 $contratdet_static = new ContratLigne($this->db);
361 if ($contratdet_static->fetch($obj->rowid)) {
362 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($contratdet_static), $properties);
363 }
364 $i++;
365 }
366 } else {
367 throw new RestException(503, 'Error when retrieve contratdet list : '.$this->db->lasterror());
368 }
369
370 if ($pagination_data) {
371 $totalsResult = $this->db->query($sqlTotals);
372 $total = $this->db->fetch_object($totalsResult)->total;
373
374 $tmp = $obj_ret;
375 $obj_ret = [];
376 $obj_ret['data'] = $tmp;
377 $obj_ret['pagination'] = [
378 'total' => (int) $total,
379 'page' => $page,
380 'page_count' => ceil((int) $total / $limit),
381 'limit' => $limit
382 ];
383 }
384
385 return $obj_ret;
386 }
387
400 public function postLine($id, $request_data = null)
401 {
402 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
403 throw new RestException(403);
404 }
405
406 $result = $this->contract->fetch($id);
407 if (!$result) {
408 throw new RestException(404, 'Contract not found');
409 }
410
411 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
412 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
413 }
414
415 $request_data = (object) $request_data;
416
417 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
418 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
419
420 $updateRes = $this->contract->addline(
421 $request_data->desc,
422 $request_data->subprice,
423 $request_data->qty,
424 $request_data->tva_tx,
425 $request_data->localtax1_tx,
426 $request_data->localtax2_tx,
427 $request_data->fk_product,
428 $request_data->remise_percent,
429 $request_data->date_start,
430 $request_data->date_end,
431 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
432 $request_data->subprice_excl_tax,
433 $request_data->info_bits,
434 $request_data->fk_fournprice,
435 $request_data->pa_ht,
436 $request_data->array_options,
437 $request_data->fk_unit,
438 $request_data->rang
439 );
440
441 if ($updateRes > 0) {
442 return $updateRes;
443 }
444 return false;
445 }
446
460 public function putLine($id, $lineid, $request_data = null)
461 {
462 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
463 throw new RestException(403);
464 }
465
466 $result = $this->contract->fetch($id);
467 if (!$result) {
468 throw new RestException(404, 'Contrat not found');
469 }
470
471 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
472 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
473 }
474
475 $contractline = new ContratLigne($this->db);
476 if ($contractline->fetch($lineid) <= 0) {
477 throw new RestException(404, 'Contract line not found');
478 }
479 if ($contractline->fk_contrat != $this->contract->id) {
480 throw new RestException(403, 'Line does not belong to this contract');
481 }
482
483 $request_data = (object) $request_data;
484
485 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
486 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
487
488 $updateRes = $this->contract->updateline(
489 $lineid,
490 $request_data->desc,
491 $request_data->subprice,
492 $request_data->qty,
493 $request_data->remise_percent,
494 $request_data->date_start,
495 $request_data->date_end,
496 $request_data->tva_tx,
497 $request_data->localtax1_tx,
498 $request_data->localtax2_tx,
499 $request_data->date_start_real,
500 $request_data->date_end_real,
501 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
502 $request_data->info_bits,
503 $request_data->fk_fourn_price,
504 $request_data->pa_ht,
505 $request_data->array_options,
506 $request_data->fk_unit
507 );
508
509 if ($updateRes > 0) {
510 if (getDolGlobalInt('API_CONTRAT_PUTLINE_OUTPUT_LINE_ONLY')) {
511 $result = new ContratLigne($this->db);
512 $result->fetch($lineid);
513 foreach (array(
514 'array_languages',
515 'contacts_ids',
516 'linked_objects',
517 'linkedObjectsIds',
518 'actiontypecode',
519 'module',
520 'canvas',
521 'user',
522 'origin',
523 'origin_id',
524 'ref_ext',
525 'status',
526 'country_id',
527 'country_code',
528 'state_id',
529 'region_id',
530 'barcode_type',
531 'barcode_type_coder',
532 'mode_reglement_id',
533 'cond_reglement_id',
534 'demand_reason_id',
535 'transport_mode_id',
536 'shipping_method',
537 'shipping_method_id',
538 'model_pdf',
539 'last_main_doc',
540 'fk_bank',
541 'fk_account',
542 'lines',
543 'name',
544 'firstname',
545 'lastname',
546 'date_creation',
547 'date_validation',
548 'date_modification',
549 'date_cloture',
550 'user_author',
551 'user_creation',
552 'user_creation_id',
553 'user_valid',
554 'user_validation',
555 'user_validation_id',
556 'user_modification',
557 'user_modification_id',
558 'cond_reglement_supplier_id',
559 'deposit_percent',
560 'retained_warranty_fk_cond_reglement',
561 'date_commande',
562 'fk_user_creat',
563 'fk_user_modif',
564 'specimen',
565 'fk_unit',
566 'date_debut_prevue',
567 'date_debut_reel',
568 'date_fin_prevue',
569 'date_fin_reel',
570 'weight',
571 'weight_units',
572 'width',
573 'width_units',
574 'length',
575 'length_units',
576 'height',
577 'height_units',
578 'surface',
579 'surface_units',
580 'volume',
581 'volume_units',
582 'multilangs',
583 'desc',
584 'product',
585 'fk_product_type',
586 'warehouse_id',
587 'totalpaid',
588 'type',
589 'libelle'
590 ) as $fieldToUnset) {
591 unset($result->{$fieldToUnset});
592 }
593 } else {
594 $result = $this->get($id);
595 unset($result->line);
596 }
597 return $this->_cleanObjectDatas($result);
598 } else {
599 throw new RestException(500, implode(';', $this->contract->errors));
600 }
601 }
602
619 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
620 {
621 if (!DolibarrApiAccess::$user->hasRight('contrat', 'activer')) {
622 throw new RestException(403);
623 }
624
625 $result = $this->contract->fetch($id);
626 if (!$result) {
627 throw new RestException(404, 'Contrat not found');
628 }
629
630 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
631 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
632 }
633
634 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $dateend, $comment);
635
636 if ($updateRes > 0) {
637 $result = $this->get($id);
638 unset($result->line);
639 return $this->_cleanObjectDatas($result);
640 }
641
642 return false;
643 }
644
660 public function unactivateLine($id, $lineid, $datestart, $comment = null)
661 {
662 if (!DolibarrApiAccess::$user->hasRight('contrat', 'desactiver')) {
663 throw new RestException(403);
664 }
665
666 $result = $this->contract->fetch($id);
667 if (!$result) {
668 throw new RestException(404, 'Contrat not found');
669 }
670
671 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
672 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
673 }
674
675 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $comment);
676
677 if ($updateRes > 0) {
678 $result = $this->get($id);
679 unset($result->line);
680 return $this->_cleanObjectDatas($result);
681 }
682
683 return false;
684 }
685
700 public function deleteLine($id, $lineid)
701 {
702 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
703 throw new RestException(403);
704 }
705
706 $result = $this->contract->fetch($id);
707 if (!$result) {
708 throw new RestException(404, 'Contrat not found');
709 }
710
711 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
712 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
713 }
714
715 $contractline = new ContratLigne($this->db);
716 if ($contractline->fetch($lineid) <= 0) {
717 throw new RestException(404, 'Contract line not found');
718 }
719 if ($contractline->fk_contrat != $this->contract->id) {
720 throw new RestException(403, 'Line does not belong to this contract');
721 }
722
723 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
724 if ($updateRes > 0) {
725 return $this->get($id);
726 } else {
727 throw new RestException(405, $this->contract->errorsToString());
728 }
729 }
730
745 public function put($id, $request_data = null)
746 {
747 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
748 throw new RestException(403);
749 }
750 if ($id == 0) {
751 throw new RestException(400, 'No contract with id=0 can exist');
752 }
753
754 $result = $this->contract->fetch($id);
755 if (!$result) {
756 throw new RestException(404, 'Contrat not found');
757 }
758
759 $old_socid = $this->contract->socid;
760 $oldthirdpartytmp = new Societe($this->db);
761 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
762 if ($old_thirdparty_result < 1) {
763 throw new RestException(404, 'Thirdparty with id='.$old_socid.' not found or not allowed');
764 }
765 if (!DolibarrApi::_checkAccessToResource('societe', $old_socid)) {
766 throw new RestException(403, 'Access to old thirdparty='.$old_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
767 }
768
769 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
770 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
771 }
772 foreach ($request_data as $field => $value) {
773 if ($field == 'id') {
774 throw new RestException(400, 'Updating with id field is forbidden');
775 }
776 if ($field == 'entity' && $value != $this->contract->entity) {
777 throw new RestException(400, 'Changing entity of a contract using the APIs is not possible');
778 }
779 if ($field === 'caller') {
780 // 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 with the caller
781 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
782 continue;
783 }
784
785 if ($field == 'socid') {
786 $thirdparty = new Societe($this->db);
787 $result = $thirdparty->fetch((int) $value);
788 if ($result < 1) {
789 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
790 }
791 }
792
793 if ($field == 'array_options' && is_array($value)) {
794 foreach ($value as $index => $val) {
795 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
796 }
797 continue;
798 }
799
800 if ($field == 'socid') {
801 $new_socid = (int) $value;
802 $loopthirdpartytmp = new Societe($this->db);
803 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
804 if ($new_thirdparty_result < 1) {
805 throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed');
806 }
807 if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) {
808 throw new RestException(403, 'Access to new thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
809 }
810 }
811
812 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
813 }
814
815 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
816 return $this->get($id);
817 } else {
818 throw new RestException(500, $this->contract->errorsToString());
819 }
820 }
821
836 public function delete($id)
837 {
838 if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) {
839 throw new RestException(403, 'Missing permission: Delete contracts/subscriptions');
840 }
841 if ($id == 0) {
842 throw new RestException(400, 'No contract with id=0 can exist');
843 }
844 $result = $this->contract->fetch($id);
845 if (!$result) {
846 throw new RestException(404, 'Contract not found');
847 }
848
849 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
850 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
851 }
852
853 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
854 throw new RestException(500, 'Error when delete contract : '.$this->contract->errorsToString());
855 }
856
857 return array(
858 'success' => array(
859 'code' => 200,
860 'message' => 'Contract deleted'
861 )
862 );
863 }
864
889 public function validate($id, $notrigger = 0)
890 {
891 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
892 throw new RestException(403);
893 }
894 if ($id == 0) {
895 throw new RestException(400, 'No contract with id=0 can exist');
896 }
897 $result = $this->contract->fetch($id);
898 if (!$result) {
899 throw new RestException(404, 'Contract not found');
900 }
901
902 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
903 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
904 }
905
906 $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
907 if ($result == 0) {
908 throw new RestException(304, 'Error nothing done. May be object is already validated');
909 }
910 if ($result < 0) {
911 throw new RestException(500, 'Error when validating Contract: '.$this->contract->errorsToString());
912 }
913
914 return array(
915 'success' => array(
916 'code' => 200,
917 'message' => 'Contract validated (Ref='.$this->contract->ref.')'
918 )
919 );
920 }
921
946 public function close($id, $notrigger = 0)
947 {
948 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
949 throw new RestException(403);
950 }
951 if ($id == 0) {
952 throw new RestException(400, 'No contract with id=0 can exist');
953 }
954 $result = $this->contract->fetch($id);
955 if (!$result) {
956 throw new RestException(404, 'Contract not found');
957 }
958
959 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
960 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
961 }
962
963 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
964 if ($result == 0) {
965 throw new RestException(304, 'Error nothing done. May be object is already close');
966 }
967 if ($result < 0) {
968 throw new RestException(500, 'Error when closing Contract: '.$this->contract->errorsToString());
969 }
970
971 return array(
972 'success' => array(
973 'code' => 200,
974 'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
975 )
976 );
977 }
978
979
980
981 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
991 protected function _cleanObjectDatas($object)
992 {
993 // phpcs:enable
994 $object = parent::_cleanObjectDatas($object);
995
996 unset($object->address);
997 unset($object->civility_id);
998
999 return $object;
1000 }
1001
1010 private function _validate($data)
1011 {
1012 if ($data === null) {
1013 $data = array();
1014 }
1015 $contrat = array();
1016 foreach (Contracts::$FIELDS as $field) {
1017 if (!isset($data[$field])) {
1018 throw new RestException(400, "$field field missing");
1019 }
1020 $contrat[$field] = $data[$field];
1021 }
1022 return $contrat;
1023 }
1024}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false, $withLines=true)
List contracts.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
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.
getLines($id, $sortfield="d.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
Get lines of a contract.
postLine($id, $request_data=null)
Add a line to given contract.
Class to manage lines of contracts.
Class for API REST v1.
Definition api.class.php:35
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage third parties objects (customers, suppliers, prospects...)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.