dolibarr 24.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 * 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';
25
33{
37 public static $FIELDS = array(
38 'socid',
39 'date_contrat'
40 );
41
45 public $contract;
46
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->contract = new Contrat($this->db);
55 }
56
71 public function get($id, $properties = '', $withLines = true)
72 {
73 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
74 throw new RestException(403);
75 }
76 if ($id == 0) {
77 throw new RestException(400, 'No contract with id=0 can exist');
78 }
79 $result = $this->contract->fetch($id);
80 if (!$result) {
81 throw new RestException(404, 'Contract not found');
82 }
83
84 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
85 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
86 }
87
88 $this->contract->fetchObjectLinked();
89
90 if (!$withLines) {
91 unset($this->contract->lines);
92 }
93
94 return $this->_filterObjectProperties($this->_cleanObjectDatas($this->contract), $properties);
95 }
96
119 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false, $withLines = true)
120 {
121 global $db, $conf;
122
123 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
124 throw new RestException(403);
125 }
126
127 $obj_ret = array();
128
129 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
130 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
131
132 // If the internal user must only see his customers, force searching by him
133 $search_sale = 0;
134 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
135 $search_sale = DolibarrApiAccess::$user->id;
136 }
137
138 $sql = "SELECT t.rowid";
139 $sql .= " FROM ".MAIN_DB_PREFIX."contrat AS t";
140 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = t.fk_soc)";
141 $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
142 $sql .= ' WHERE t.entity IN ('.getEntity('contrat').')';
143 if ($socids) {
144 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
145 }
146 // Search on sale representative
147 if ($search_sale && $search_sale != '-1') {
148 if ($search_sale == -2) {
149 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
150 } elseif ($search_sale > 0) {
151 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
152 }
153 }
154 // Add sql filters
155 if ($sqlfilters) {
156 $errormessage = '';
157 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
158 if ($errormessage) {
159 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
160 }
161 }
162
163 //this query will return total orders with the filters given
164 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
165
166 $sql .= $this->db->order($sortfield, $sortorder);
167 if ($limit) {
168 if ($page < 0) {
169 $page = 0;
170 }
171 $offset = $limit * $page;
172
173 $sql .= $this->db->plimit($limit + 1, $offset);
174 }
175
176 $result = $this->db->query($sql);
177
178 if ($result) {
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
181 $i = 0;
182 while ($i < $min) {
183 $obj = $this->db->fetch_object($result);
184 $contrat_static = new Contrat($this->db);
185 if ($contrat_static->fetch($obj->rowid)) {
186 if (!$withLines) {
187 unset($contrat_static->lines);
188 }
189 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($contrat_static), $properties);
190 }
191 $i++;
192 }
193 } else {
194 throw new RestException(503, 'Error when retrieve contrat list : '.$this->db->lasterror());
195 }
196
197 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
198 if ($pagination_data) {
199 $totalsResult = $this->db->query($sqlTotals);
200 $total = $this->db->fetch_object($totalsResult)->total;
201
202 $tmp = $obj_ret;
203 $obj_ret = [];
204
205 $obj_ret['data'] = $tmp;
206 $obj_ret['pagination'] = [
207 'total' => (int) $total,
208 'page' => $page, //count starts from 0
209 'page_count' => ceil((int) $total / $limit),
210 'limit' => $limit
211 ];
212 }
213
214 return $obj_ret;
215 }
216
230 public function post($request_data = null)
231 {
232 global $conf;
233 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
234 throw new RestException(403, "Missing permission: Create/modify contracts/subscriptions");
235 }
236
237 // Check mandatory fields
238 $this->_validate($request_data);
239
240 // Check thirdparty validity
241 $socid = (int) $request_data['socid'];
242 $thirdpartytmp = new Societe($this->db);
243 $thirdparty_result = $thirdpartytmp->fetch($socid);
244 if ($thirdparty_result < 1) {
245 throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed');
246 }
247 if (!DolibarrApi::_checkAccessToResource('societe', $thirdpartytmp->id)) {
248 throw new RestException(404, 'Thirdparty with id='.$thirdpartytmp->id.' not found or not allowed');
249 }
250
251 foreach ($request_data as $field => $value) {
252 if ($field === 'caller') {
253 // 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
254 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
255 continue;
256 }
257 if ($field == 'id') {
258 throw new RestException(400, 'Creating with id field is forbidden');
259 }
260 if ($field == 'entity' && ((int) $value) != ((int) $conf->entity)) {
261 throw new RestException(403, 'Creating with entity='.((int) $value).' MUST be the same entity='.((int) $conf->entity).' as your API user/key belongs to');
262 }
263
264 if ($field == 'socid') {
265 $thirdparty = new Societe($this->db);
266 $result = $thirdparty->fetch((int) $value);
267 if ($result < 1) {
268 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
269 }
270 }
271
272 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
273 }
274 /*if (isset($request_data["lines"])) {
275 $lines = array();
276 foreach ($request_data["lines"] as $line) {
277 array_push($lines, (object) $line);
278 }
279 $this->contract->lines = $lines;
280 }*/
281 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
282 throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
283 }
284
285 return $this->contract->id;
286 }
287
308 public function getLines($id, $sortfield = "d.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
309 {
310 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
311 throw new RestException(403);
312 }
313
314 $result = $this->contract->fetch($id);
315 if (!$result) {
316 throw new RestException(404, 'Contract not found');
317 }
318
319 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
320 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
321 }
322
323 $obj_ret = [];
324
325 $sql = "SELECT d.rowid";
326 $sql .= " FROM ".$this->db->prefix()."contratdet AS d";
327 $sql .= " LEFT JOIN ".$this->db->prefix()."contrat AS c ON (c.rowid = d.fk_contrat)";
328 $sql .= " LEFT JOIN ".$this->db->prefix()."contratdet_extrafields AS ef ON (ef.fk_object = d.rowid)";
329 $sql .= " WHERE d.fk_contrat = ".((int) $id);
330 $sql .= ' AND c.entity IN ('.getEntity('contrat').')';
331
332 if ($sqlfilters) {
333 $errormessage = '';
334 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
335 if ($errormessage) {
336 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
337 }
338 }
339
340 $sqlTotals = str_replace('SELECT d.rowid', 'SELECT count(d.rowid) as total', $sql);
341
342 $sql .= $this->db->order($sortfield, $sortorder);
343 if ($limit) {
344 if ($page < 0) {
345 $page = 0;
346 }
347 $offset = $limit * $page;
348
349 $sql .= $this->db->plimit($limit + 1, $offset);
350 }
351
352 $result = $this->db->query($sql);
353 if ($result) {
354 $num = $this->db->num_rows($result);
355 $min = min($num, ($limit <= 0 ? $num : $limit));
356 $i = 0;
357 while ($i < $min) {
358 $obj = $this->db->fetch_object($result);
359 $contratdet_static = new ContratLigne($this->db);
360 if ($contratdet_static->fetch($obj->rowid)) {
361 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($contratdet_static), $properties);
362 }
363 $i++;
364 }
365 } else {
366 throw new RestException(503, 'Error when retrieve contratdet list : '.$this->db->lasterror());
367 }
368
369 if ($pagination_data) {
370 $totalsResult = $this->db->query($sqlTotals);
371 $total = $this->db->fetch_object($totalsResult)->total;
372
373 $tmp = $obj_ret;
374 $obj_ret = [];
375 $obj_ret['data'] = $tmp;
376 $obj_ret['pagination'] = [
377 'total' => (int) $total,
378 'page' => $page,
379 'page_count' => ceil((int) $total / $limit),
380 'limit' => $limit
381 ];
382 }
383
384 return $obj_ret;
385 }
386
399 public function postLine($id, $request_data = null)
400 {
401 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
402 throw new RestException(403);
403 }
404
405 $result = $this->contract->fetch($id);
406 if (!$result) {
407 throw new RestException(404, 'Contract not found');
408 }
409
410 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
411 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
412 }
413
414 $request_data = (object) $request_data;
415
416 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
417 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
418
419 $updateRes = $this->contract->addline(
420 $request_data->desc,
421 $request_data->subprice,
422 $request_data->qty,
423 $request_data->tva_tx,
424 $request_data->localtax1_tx,
425 $request_data->localtax2_tx,
426 $request_data->fk_product,
427 $request_data->remise_percent,
428 $request_data->date_start,
429 $request_data->date_end,
430 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
431 $request_data->subprice_excl_tax,
432 $request_data->info_bits,
433 $request_data->fk_fournprice,
434 $request_data->pa_ht,
435 $request_data->array_options,
436 $request_data->fk_unit,
437 $request_data->rang
438 );
439
440 if ($updateRes > 0) {
441 return $updateRes;
442 }
443 return false;
444 }
445
459 public function putLine($id, $lineid, $request_data = null)
460 {
461 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
462 throw new RestException(403);
463 }
464
465 $result = $this->contract->fetch($id);
466 if (!$result) {
467 throw new RestException(404, 'Contrat not found');
468 }
469
470 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
471 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
472 }
473
474 $request_data = (object) $request_data;
475
476 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
477 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
478
479 $updateRes = $this->contract->updateline(
480 $lineid,
481 $request_data->desc,
482 $request_data->subprice,
483 $request_data->qty,
484 $request_data->remise_percent,
485 $request_data->date_start,
486 $request_data->date_end,
487 $request_data->tva_tx,
488 $request_data->localtax1_tx,
489 $request_data->localtax2_tx,
490 $request_data->date_start_real,
491 $request_data->date_end_real,
492 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
493 $request_data->info_bits,
494 $request_data->fk_fourn_price,
495 $request_data->pa_ht,
496 $request_data->array_options,
497 $request_data->fk_unit
498 );
499
500 if ($updateRes > 0) {
501 if (getDolGlobalInt('API_CONTRAT_PUTLINE_OUTPUT_LINE_ONLY')) {
502 $result = new ContratLigne($this->db);
503 $result->fetch($lineid);
504 foreach (array(
505 'array_languages',
506 'contacts_ids',
507 'linked_objects',
508 'linkedObjectsIds',
509 'actiontypecode',
510 'module',
511 'canvas',
512 'user',
513 'origin',
514 'origin_id',
515 'ref_ext',
516 'status',
517 'country_id',
518 'country_code',
519 'state_id',
520 'region_id',
521 'barcode_type',
522 'barcode_type_coder',
523 'mode_reglement_id',
524 'cond_reglement_id',
525 'demand_reason_id',
526 'transport_mode_id',
527 'shipping_method',
528 'shipping_method_id',
529 'model_pdf',
530 'last_main_doc',
531 'fk_bank',
532 'fk_account',
533 'lines',
534 'name',
535 'firstname',
536 'lastname',
537 'date_creation',
538 'date_validation',
539 'date_modification',
540 'date_cloture',
541 'user_author',
542 'user_creation',
543 'user_creation_id',
544 'user_valid',
545 'user_validation',
546 'user_validation_id',
547 'user_modification',
548 'user_modification_id',
549 'cond_reglement_supplier_id',
550 'deposit_percent',
551 'retained_warranty_fk_cond_reglement',
552 'date_commande',
553 'fk_user_creat',
554 'fk_user_modif',
555 'specimen',
556 'fk_unit',
557 'date_debut_prevue',
558 'date_debut_reel',
559 'date_fin_prevue',
560 'date_fin_reel',
561 'weight',
562 'weight_units',
563 'width',
564 'width_units',
565 'length',
566 'length_units',
567 'height',
568 'height_units',
569 'surface',
570 'surface_units',
571 'volume',
572 'volume_units',
573 'multilangs',
574 'desc',
575 'product',
576 'fk_product_type',
577 'warehouse_id',
578 'totalpaid',
579 'type',
580 'libelle'
581 ) as $fieldToUnset) {
582 unset($result->{$fieldToUnset});
583 }
584 } else {
585 $result = $this->get($id);
586 unset($result->line);
587 }
588 return $this->_cleanObjectDatas($result);
589 } else {
590 throw new RestException(500, implode(';', $this->contract->errors));
591 }
592 }
593
610 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
611 {
612 if (!DolibarrApiAccess::$user->hasRight('contrat', 'activer')) {
613 throw new RestException(403);
614 }
615
616 $result = $this->contract->fetch($id);
617 if (!$result) {
618 throw new RestException(404, 'Contrat not found');
619 }
620
621 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
622 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
623 }
624
625 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $dateend, $comment);
626
627 if ($updateRes > 0) {
628 $result = $this->get($id);
629 unset($result->line);
630 return $this->_cleanObjectDatas($result);
631 }
632
633 return false;
634 }
635
651 public function unactivateLine($id, $lineid, $datestart, $comment = null)
652 {
653 if (!DolibarrApiAccess::$user->hasRight('contrat', 'desactiver')) {
654 throw new RestException(403);
655 }
656
657 $result = $this->contract->fetch($id);
658 if (!$result) {
659 throw new RestException(404, 'Contrat not found');
660 }
661
662 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
663 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
664 }
665
666 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $comment);
667
668 if ($updateRes > 0) {
669 $result = $this->get($id);
670 unset($result->line);
671 return $this->_cleanObjectDatas($result);
672 }
673
674 return false;
675 }
676
691 public function deleteLine($id, $lineid)
692 {
693 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
694 throw new RestException(403);
695 }
696
697 $result = $this->contract->fetch($id);
698 if (!$result) {
699 throw new RestException(404, 'Contrat not found');
700 }
701
702 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
703 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
704 }
705
706 // TODO Check the lineid $lineid is a line of object
707
708 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
709 if ($updateRes > 0) {
710 return $this->get($id);
711 } else {
712 throw new RestException(405, $this->contract->error);
713 }
714 }
715
730 public function put($id, $request_data = null)
731 {
732 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
733 throw new RestException(403);
734 }
735 if ($id == 0) {
736 throw new RestException(400, 'No contract with id=0 can exist');
737 }
738
739 $result = $this->contract->fetch($id);
740 if (!$result) {
741 throw new RestException(404, 'Contrat not found');
742 }
743
744 $old_socid = $this->contract->socid;
745 $oldthirdpartytmp = new Societe($this->db);
746 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
747 if ($old_thirdparty_result < 1) {
748 throw new RestException(404, 'Thirdparty with id='.$old_socid.' not found or not allowed');
749 }
750 if (!DolibarrApi::_checkAccessToResource('societe', $old_socid)) {
751 throw new RestException(403, 'Access to old thirdparty='.$old_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
752 }
753
754 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
755 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
756 }
757 foreach ($request_data as $field => $value) {
758 if ($field == 'id') {
759 throw new RestException(400, 'Updating with id field is forbidden');
760 }
761 if ($field == 'entity' && $value != $this->contract->entity) {
762 throw new RestException(400, 'Changing entity of a contract using the APIs is not possible');
763 }
764 if ($field === 'caller') {
765 // 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
766 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
767 continue;
768 }
769
770 if ($field == 'socid') {
771 $thirdparty = new Societe($this->db);
772 $result = $thirdparty->fetch((int) $value);
773 if ($result < 1) {
774 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
775 }
776 }
777
778 if ($field == 'array_options' && is_array($value)) {
779 foreach ($value as $index => $val) {
780 $this->contract->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->contract);
781 }
782 continue;
783 }
784
785 if ($field == 'socid') {
786 $new_socid = (int) $value;
787 $loopthirdpartytmp = new Societe($this->db);
788 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
789 if ($new_thirdparty_result < 1) {
790 throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed');
791 }
792 if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) {
793 throw new RestException(403, 'Access to new thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
794 }
795 }
796
797 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
798 }
799
800 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
801 return $this->get($id);
802 } else {
803 throw new RestException(500, $this->contract->error);
804 }
805 }
806
821 public function delete($id)
822 {
823 if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) {
824 throw new RestException(403, 'Missing permission: Delete contracts/subscriptions');
825 }
826 if ($id == 0) {
827 throw new RestException(400, 'No contract with id=0 can exist');
828 }
829 $result = $this->contract->fetch($id);
830 if (!$result) {
831 throw new RestException(404, 'Contract not found');
832 }
833
834 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
835 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
836 }
837
838 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
839 throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
840 }
841
842 return array(
843 'success' => array(
844 'code' => 200,
845 'message' => 'Contract deleted'
846 )
847 );
848 }
849
874 public function validate($id, $notrigger = 0)
875 {
876 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
877 throw new RestException(403);
878 }
879 if ($id == 0) {
880 throw new RestException(400, 'No contract with id=0 can exist');
881 }
882 $result = $this->contract->fetch($id);
883 if (!$result) {
884 throw new RestException(404, 'Contract not found');
885 }
886
887 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
888 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
889 }
890
891 $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
892 if ($result == 0) {
893 throw new RestException(304, 'Error nothing done. May be object is already validated');
894 }
895 if ($result < 0) {
896 throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
897 }
898
899 return array(
900 'success' => array(
901 'code' => 200,
902 'message' => 'Contract validated (Ref='.$this->contract->ref.')'
903 )
904 );
905 }
906
931 public function close($id, $notrigger = 0)
932 {
933 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
934 throw new RestException(403);
935 }
936 if ($id == 0) {
937 throw new RestException(400, 'No contract with id=0 can exist');
938 }
939 $result = $this->contract->fetch($id);
940 if (!$result) {
941 throw new RestException(404, 'Contract not found');
942 }
943
944 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
945 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
946 }
947
948 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
949 if ($result == 0) {
950 throw new RestException(304, 'Error nothing done. May be object is already close');
951 }
952 if ($result < 0) {
953 throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
954 }
955
956 return array(
957 'success' => array(
958 'code' => 200,
959 'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
960 )
961 );
962 }
963
964
965
966 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
976 protected function _cleanObjectDatas($object)
977 {
978 // phpcs:enable
979 $object = parent::_cleanObjectDatas($object);
980
981 unset($object->address);
982 unset($object->civility_id);
983
984 return $object;
985 }
986
995 private function _validate($data)
996 {
997 if ($data === null) {
998 $data = array();
999 }
1000 $contrat = array();
1001 foreach (Contracts::$FIELDS as $field) {
1002 if (!isset($data[$field])) {
1003 throw new RestException(400, "$field field missing");
1004 }
1005 $contrat[$field] = $data[$field];
1006 }
1007 return $contrat;
1008 }
1009}
$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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.