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 $request_data = (object) $request_data;
476
477 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
478 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
479
480 $updateRes = $this->contract->updateline(
481 $lineid,
482 $request_data->desc,
483 $request_data->subprice,
484 $request_data->qty,
485 $request_data->remise_percent,
486 $request_data->date_start,
487 $request_data->date_end,
488 $request_data->tva_tx,
489 $request_data->localtax1_tx,
490 $request_data->localtax2_tx,
491 $request_data->date_start_real,
492 $request_data->date_end_real,
493 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
494 $request_data->info_bits,
495 $request_data->fk_fourn_price,
496 $request_data->pa_ht,
497 $request_data->array_options,
498 $request_data->fk_unit
499 );
500
501 if ($updateRes > 0) {
502 if (getDolGlobalInt('API_CONTRAT_PUTLINE_OUTPUT_LINE_ONLY')) {
503 $result = new ContratLigne($this->db);
504 $result->fetch($lineid);
505 foreach (array(
506 'array_languages',
507 'contacts_ids',
508 'linked_objects',
509 'linkedObjectsIds',
510 'actiontypecode',
511 'module',
512 'canvas',
513 'user',
514 'origin',
515 'origin_id',
516 'ref_ext',
517 'status',
518 'country_id',
519 'country_code',
520 'state_id',
521 'region_id',
522 'barcode_type',
523 'barcode_type_coder',
524 'mode_reglement_id',
525 'cond_reglement_id',
526 'demand_reason_id',
527 'transport_mode_id',
528 'shipping_method',
529 'shipping_method_id',
530 'model_pdf',
531 'last_main_doc',
532 'fk_bank',
533 'fk_account',
534 'lines',
535 'name',
536 'firstname',
537 'lastname',
538 'date_creation',
539 'date_validation',
540 'date_modification',
541 'date_cloture',
542 'user_author',
543 'user_creation',
544 'user_creation_id',
545 'user_valid',
546 'user_validation',
547 'user_validation_id',
548 'user_modification',
549 'user_modification_id',
550 'cond_reglement_supplier_id',
551 'deposit_percent',
552 'retained_warranty_fk_cond_reglement',
553 'date_commande',
554 'fk_user_creat',
555 'fk_user_modif',
556 'specimen',
557 'fk_unit',
558 'date_debut_prevue',
559 'date_debut_reel',
560 'date_fin_prevue',
561 'date_fin_reel',
562 'weight',
563 'weight_units',
564 'width',
565 'width_units',
566 'length',
567 'length_units',
568 'height',
569 'height_units',
570 'surface',
571 'surface_units',
572 'volume',
573 'volume_units',
574 'multilangs',
575 'desc',
576 'product',
577 'fk_product_type',
578 'warehouse_id',
579 'totalpaid',
580 'type',
581 'libelle'
582 ) as $fieldToUnset) {
583 unset($result->{$fieldToUnset});
584 }
585 } else {
586 $result = $this->get($id);
587 unset($result->line);
588 }
589 return $this->_cleanObjectDatas($result);
590 } else {
591 throw new RestException(500, implode(';', $this->contract->errors));
592 }
593 }
594
611 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
612 {
613 if (!DolibarrApiAccess::$user->hasRight('contrat', 'activer')) {
614 throw new RestException(403);
615 }
616
617 $result = $this->contract->fetch($id);
618 if (!$result) {
619 throw new RestException(404, 'Contrat not found');
620 }
621
622 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
623 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
624 }
625
626 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $dateend, $comment);
627
628 if ($updateRes > 0) {
629 $result = $this->get($id);
630 unset($result->line);
631 return $this->_cleanObjectDatas($result);
632 }
633
634 return false;
635 }
636
652 public function unactivateLine($id, $lineid, $datestart, $comment = null)
653 {
654 if (!DolibarrApiAccess::$user->hasRight('contrat', 'desactiver')) {
655 throw new RestException(403);
656 }
657
658 $result = $this->contract->fetch($id);
659 if (!$result) {
660 throw new RestException(404, 'Contrat not found');
661 }
662
663 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
664 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
665 }
666
667 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $comment);
668
669 if ($updateRes > 0) {
670 $result = $this->get($id);
671 unset($result->line);
672 return $this->_cleanObjectDatas($result);
673 }
674
675 return false;
676 }
677
692 public function deleteLine($id, $lineid)
693 {
694 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
695 throw new RestException(403);
696 }
697
698 $result = $this->contract->fetch($id);
699 if (!$result) {
700 throw new RestException(404, 'Contrat not found');
701 }
702
703 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
704 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
705 }
706
707 // TODO Check the lineid $lineid is a line of object
708
709 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
710 if ($updateRes > 0) {
711 return $this->get($id);
712 } else {
713 throw new RestException(405, $this->contract->error);
714 }
715 }
716
731 public function put($id, $request_data = null)
732 {
733 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
734 throw new RestException(403);
735 }
736 if ($id == 0) {
737 throw new RestException(400, 'No contract with id=0 can exist');
738 }
739
740 $result = $this->contract->fetch($id);
741 if (!$result) {
742 throw new RestException(404, 'Contrat not found');
743 }
744
745 $old_socid = $this->contract->socid;
746 $oldthirdpartytmp = new Societe($this->db);
747 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
748 if ($old_thirdparty_result < 1) {
749 throw new RestException(404, 'Thirdparty with id='.$old_socid.' not found or not allowed');
750 }
751 if (!DolibarrApi::_checkAccessToResource('societe', $old_socid)) {
752 throw new RestException(403, 'Access to old thirdparty='.$old_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
753 }
754
755 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
756 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
757 }
758 foreach ($request_data as $field => $value) {
759 if ($field == 'id') {
760 throw new RestException(400, 'Updating with id field is forbidden');
761 }
762 if ($field == 'entity' && $value != $this->contract->entity) {
763 throw new RestException(400, 'Changing entity of a contract using the APIs is not possible');
764 }
765 if ($field === 'caller') {
766 // 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
767 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
768 continue;
769 }
770
771 if ($field == 'socid') {
772 $thirdparty = new Societe($this->db);
773 $result = $thirdparty->fetch((int) $value);
774 if ($result < 1) {
775 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
776 }
777 }
778
779 if ($field == 'array_options' && is_array($value)) {
780 foreach ($value as $index => $val) {
781 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
782 }
783 continue;
784 }
785
786 if ($field == 'socid') {
787 $new_socid = (int) $value;
788 $loopthirdpartytmp = new Societe($this->db);
789 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
790 if ($new_thirdparty_result < 1) {
791 throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed');
792 }
793 if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) {
794 throw new RestException(403, 'Access to new thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
795 }
796 }
797
798 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
799 }
800
801 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
802 return $this->get($id);
803 } else {
804 throw new RestException(500, $this->contract->error);
805 }
806 }
807
822 public function delete($id)
823 {
824 if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) {
825 throw new RestException(403, 'Missing permission: Delete contracts/subscriptions');
826 }
827 if ($id == 0) {
828 throw new RestException(400, 'No contract with id=0 can exist');
829 }
830 $result = $this->contract->fetch($id);
831 if (!$result) {
832 throw new RestException(404, 'Contract not found');
833 }
834
835 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
836 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
837 }
838
839 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
840 throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
841 }
842
843 return array(
844 'success' => array(
845 'code' => 200,
846 'message' => 'Contract deleted'
847 )
848 );
849 }
850
875 public function validate($id, $notrigger = 0)
876 {
877 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
878 throw new RestException(403);
879 }
880 if ($id == 0) {
881 throw new RestException(400, 'No contract with id=0 can exist');
882 }
883 $result = $this->contract->fetch($id);
884 if (!$result) {
885 throw new RestException(404, 'Contract not found');
886 }
887
888 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
889 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
890 }
891
892 $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
893 if ($result == 0) {
894 throw new RestException(304, 'Error nothing done. May be object is already validated');
895 }
896 if ($result < 0) {
897 throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
898 }
899
900 return array(
901 'success' => array(
902 'code' => 200,
903 'message' => 'Contract validated (Ref='.$this->contract->ref.')'
904 )
905 );
906 }
907
932 public function close($id, $notrigger = 0)
933 {
934 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
935 throw new RestException(403);
936 }
937 if ($id == 0) {
938 throw new RestException(400, 'No contract with id=0 can exist');
939 }
940 $result = $this->contract->fetch($id);
941 if (!$result) {
942 throw new RestException(404, 'Contract not found');
943 }
944
945 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
946 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
947 }
948
949 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
950 if ($result == 0) {
951 throw new RestException(304, 'Error nothing done. May be object is already close');
952 }
953 if ($result < 0) {
954 throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
955 }
956
957 return array(
958 'success' => array(
959 'code' => 200,
960 'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
961 )
962 );
963 }
964
965
966
967 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
977 protected function _cleanObjectDatas($object)
978 {
979 // phpcs:enable
980 $object = parent::_cleanObjectDatas($object);
981
982 unset($object->address);
983 unset($object->civility_id);
984
985 return $object;
986 }
987
996 private function _validate($data)
997 {
998 if ($data === null) {
999 $data = array();
1000 }
1001 $contrat = array();
1002 foreach (Contracts::$FIELDS as $field) {
1003 if (!isset($data[$field])) {
1004 throw new RestException(400, "$field field missing");
1005 }
1006 $contrat[$field] = $data[$field];
1007 }
1008 return $contrat;
1009 }
1010}
$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:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.