dolibarr 23.0.4
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 $contractline = new ContratLigne($this->db);
475 if ($contractline->fetch($lineid) <= 0) {
476 throw new RestException(404, 'Contract line not found');
477 }
478 if ($contractline->fk_contrat != $this->contract->id) {
479 throw new RestException(403, 'Line does not belong to this contract');
480 }
481
482 $request_data = (object) $request_data;
483
484 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
485 $request_data->price_base_type = sanitizeVal($request_data->price_base_type);
486
487 $updateRes = $this->contract->updateline(
488 $lineid,
489 $request_data->desc,
490 $request_data->subprice,
491 $request_data->qty,
492 $request_data->remise_percent,
493 $request_data->date_start,
494 $request_data->date_end,
495 $request_data->tva_tx,
496 $request_data->localtax1_tx,
497 $request_data->localtax2_tx,
498 $request_data->date_start_real,
499 $request_data->date_end_real,
500 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
501 $request_data->info_bits,
502 $request_data->fk_fourn_price,
503 $request_data->pa_ht,
504 $request_data->array_options,
505 $request_data->fk_unit
506 );
507
508 if ($updateRes > 0) {
509 if (getDolGlobalInt('API_CONTRAT_PUTLINE_OUTPUT_LINE_ONLY')) {
510 $result = new ContratLigne($this->db);
511 $result->fetch($lineid);
512 foreach (array(
513 'array_languages',
514 'contacts_ids',
515 'linked_objects',
516 'linkedObjectsIds',
517 'actiontypecode',
518 'module',
519 'canvas',
520 'user',
521 'origin',
522 'origin_id',
523 'ref_ext',
524 'status',
525 'country_id',
526 'country_code',
527 'state_id',
528 'region_id',
529 'barcode_type',
530 'barcode_type_coder',
531 'mode_reglement_id',
532 'cond_reglement_id',
533 'demand_reason_id',
534 'transport_mode_id',
535 'shipping_method',
536 'shipping_method_id',
537 'model_pdf',
538 'last_main_doc',
539 'fk_bank',
540 'fk_account',
541 'lines',
542 'name',
543 'firstname',
544 'lastname',
545 'date_creation',
546 'date_validation',
547 'date_modification',
548 'date_cloture',
549 'user_author',
550 'user_creation',
551 'user_creation_id',
552 'user_valid',
553 'user_validation',
554 'user_validation_id',
555 'user_modification',
556 'user_modification_id',
557 'cond_reglement_supplier_id',
558 'deposit_percent',
559 'retained_warranty_fk_cond_reglement',
560 'date_commande',
561 'fk_user_creat',
562 'fk_user_modif',
563 'specimen',
564 'fk_unit',
565 'date_debut_prevue',
566 'date_debut_reel',
567 'date_fin_prevue',
568 'date_fin_reel',
569 'weight',
570 'weight_units',
571 'width',
572 'width_units',
573 'length',
574 'length_units',
575 'height',
576 'height_units',
577 'surface',
578 'surface_units',
579 'volume',
580 'volume_units',
581 'multilangs',
582 'desc',
583 'product',
584 'fk_product_type',
585 'warehouse_id',
586 'totalpaid',
587 'type',
588 'libelle'
589 ) as $fieldToUnset) {
590 unset($result->{$fieldToUnset});
591 }
592 } else {
593 $result = $this->get($id);
594 unset($result->line);
595 }
596 return $this->_cleanObjectDatas($result);
597 } else {
598 throw new RestException(500, implode(';', $this->contract->errors));
599 }
600 }
601
618 public function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null)
619 {
620 if (!DolibarrApiAccess::$user->hasRight('contrat', 'activer')) {
621 throw new RestException(403);
622 }
623
624 $result = $this->contract->fetch($id);
625 if (!$result) {
626 throw new RestException(404, 'Contrat not found');
627 }
628
629 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
630 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
631 }
632
633 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $dateend, $comment);
634
635 if ($updateRes > 0) {
636 $result = $this->get($id);
637 unset($result->line);
638 return $this->_cleanObjectDatas($result);
639 }
640
641 return false;
642 }
643
659 public function unactivateLine($id, $lineid, $datestart, $comment = null)
660 {
661 if (!DolibarrApiAccess::$user->hasRight('contrat', 'desactiver')) {
662 throw new RestException(403);
663 }
664
665 $result = $this->contract->fetch($id);
666 if (!$result) {
667 throw new RestException(404, 'Contrat not found');
668 }
669
670 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
671 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
672 }
673
674 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $comment);
675
676 if ($updateRes > 0) {
677 $result = $this->get($id);
678 unset($result->line);
679 return $this->_cleanObjectDatas($result);
680 }
681
682 return false;
683 }
684
699 public function deleteLine($id, $lineid)
700 {
701 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
702 throw new RestException(403);
703 }
704
705 $result = $this->contract->fetch($id);
706 if (!$result) {
707 throw new RestException(404, 'Contrat not found');
708 }
709
710 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
711 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
712 }
713
714 $contractline = new ContratLigne($this->db);
715 if ($contractline->fetch($lineid) <= 0) {
716 throw new RestException(404, 'Contract line not found');
717 }
718 if ($contractline->fk_contrat != $this->contract->id) {
719 throw new RestException(403, 'Line does not belong to this contract');
720 }
721
722 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
723 if ($updateRes > 0) {
724 return $this->get($id);
725 } else {
726 throw new RestException(405, $this->contract->error);
727 }
728 }
729
744 public function put($id, $request_data = null)
745 {
746 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
747 throw new RestException(403);
748 }
749 if ($id == 0) {
750 throw new RestException(400, 'No contract with id=0 can exist');
751 }
752
753 $result = $this->contract->fetch($id);
754 if (!$result) {
755 throw new RestException(404, 'Contrat not found');
756 }
757
758 $old_socid = $this->contract->socid;
759 $oldthirdpartytmp = new Societe($this->db);
760 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
761 if ($old_thirdparty_result < 1) {
762 throw new RestException(404, 'Thirdparty with id='.$old_socid.' not found or not allowed');
763 }
764 if (!DolibarrApi::_checkAccessToResource('societe', $old_socid)) {
765 throw new RestException(403, 'Access to old thirdparty='.$old_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
766 }
767
768 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
769 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
770 }
771 foreach ($request_data as $field => $value) {
772 if ($field == 'id') {
773 throw new RestException(400, 'Updating with id field is forbidden');
774 }
775 if ($field == 'entity' && $value != $this->contract->entity) {
776 throw new RestException(400, 'Changing entity of a contract using the APIs is not possible');
777 }
778 if ($field === 'caller') {
779 // 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
780 $this->contract->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
781 continue;
782 }
783
784 if ($field == 'socid') {
785 $thirdparty = new Societe($this->db);
786 $result = $thirdparty->fetch((int) $value);
787 if ($result < 1) {
788 throw new RestException(404, 'Thirdparty with id='.((int) $value).' not found');
789 }
790 }
791
792 if ($field == 'array_options' && is_array($value)) {
793 foreach ($value as $index => $val) {
794 $this->contract->array_options[$index] = $this->_checkValForAPI($field, $val, $this->contract);
795 }
796 continue;
797 }
798
799 if ($field == 'socid') {
800 $new_socid = (int) $value;
801 $loopthirdpartytmp = new Societe($this->db);
802 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
803 if ($new_thirdparty_result < 1) {
804 throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed');
805 }
806 if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) {
807 throw new RestException(403, 'Access to new thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login);
808 }
809 }
810
811 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
812 }
813
814 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
815 return $this->get($id);
816 } else {
817 throw new RestException(500, $this->contract->error);
818 }
819 }
820
835 public function delete($id)
836 {
837 if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) {
838 throw new RestException(403, 'Missing permission: Delete contracts/subscriptions');
839 }
840 if ($id == 0) {
841 throw new RestException(400, 'No contract with id=0 can exist');
842 }
843 $result = $this->contract->fetch($id);
844 if (!$result) {
845 throw new RestException(404, 'Contract not found');
846 }
847
848 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
849 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
850 }
851
852 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
853 throw new RestException(500, 'Error when delete contract : '.$this->contract->error);
854 }
855
856 return array(
857 'success' => array(
858 'code' => 200,
859 'message' => 'Contract deleted'
860 )
861 );
862 }
863
888 public function validate($id, $notrigger = 0)
889 {
890 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
891 throw new RestException(403);
892 }
893 if ($id == 0) {
894 throw new RestException(400, 'No contract with id=0 can exist');
895 }
896 $result = $this->contract->fetch($id);
897 if (!$result) {
898 throw new RestException(404, 'Contract not found');
899 }
900
901 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
902 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
903 }
904
905 $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger);
906 if ($result == 0) {
907 throw new RestException(304, 'Error nothing done. May be object is already validated');
908 }
909 if ($result < 0) {
910 throw new RestException(500, 'Error when validating Contract: '.$this->contract->error);
911 }
912
913 return array(
914 'success' => array(
915 'code' => 200,
916 'message' => 'Contract validated (Ref='.$this->contract->ref.')'
917 )
918 );
919 }
920
945 public function close($id, $notrigger = 0)
946 {
947 if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) {
948 throw new RestException(403);
949 }
950 if ($id == 0) {
951 throw new RestException(400, 'No contract with id=0 can exist');
952 }
953 $result = $this->contract->fetch($id);
954 if (!$result) {
955 throw new RestException(404, 'Contract not found');
956 }
957
958 if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
959 throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
960 }
961
962 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
963 if ($result == 0) {
964 throw new RestException(304, 'Error nothing done. May be object is already close');
965 }
966 if ($result < 0) {
967 throw new RestException(500, 'Error when closing Contract: '.$this->contract->error);
968 }
969
970 return array(
971 'success' => array(
972 'code' => 200,
973 'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.'
974 )
975 );
976 }
977
978
979
980 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
990 protected function _cleanObjectDatas($object)
991 {
992 // phpcs:enable
993 $object = parent::_cleanObjectDatas($object);
994
995 unset($object->address);
996 unset($object->civility_id);
997
998 return $object;
999 }
1000
1009 private function _validate($data)
1010 {
1011 if ($data === null) {
1012 $data = array();
1013 }
1014 $contrat = array();
1015 foreach (Contracts::$FIELDS as $field) {
1016 if (!isset($data[$field])) {
1017 throw new RestException(400, "$field field missing");
1018 }
1019 $contrat[$field] = $data[$field];
1020 }
1021 return $contrat;
1022 }
1023}
$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:33
_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.
Definition api.class.php:98
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.