21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
37 public static $FIELDS = array(
40 'commercial_signature_id',
56 $this->contract =
new Contrat($this->db);
68 public function get(
$id)
70 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
71 throw new RestException(403);
74 $result = $this->contract->fetch(
$id);
76 throw new RestException(404,
'Contract not found');
80 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
83 $this->contract->fetchObjectLinked();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
111 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
112 throw new RestException(403);
118 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
122 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
123 $search_sale = DolibarrApiAccess::$user->id;
126 $sql =
"SELECT t.rowid";
127 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat AS t LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)";
128 $sql .=
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
130 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
133 if ($search_sale && $search_sale !=
'-1') {
134 if ($search_sale == -2) {
135 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
136 } elseif ($search_sale > 0) {
137 $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).
")";
145 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
150 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
152 $sql .= $this->db->order($sortfield, $sortorder);
157 $offset = $limit * $page;
159 $sql .= $this->db->plimit($limit + 1, $offset);
162 $result = $this->db->query($sql);
165 $num = $this->db->num_rows($result);
166 $min = min($num, ($limit <= 0 ? $num : $limit));
169 $obj = $this->db->fetch_object($result);
170 $contrat_static =
new Contrat($this->db);
171 if ($contrat_static->fetch($obj->rowid)) {
177 throw new RestException(503,
'Error when retrieve contrat list : '.$this->db->lasterror());
181 if ($pagination_data) {
182 $totalsResult = $this->db->query($sqlTotals);
183 $total = $this->db->fetch_object($totalsResult)->total;
188 $obj_ret[
'data'] = $tmp;
189 $obj_ret[
'pagination'] = [
190 'total' => (int) $total,
192 'page_count' => ceil((
int) $total / $limit),
208 public function post($request_data =
null)
210 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
211 throw new RestException(403,
"Missing permission: Create/modify contracts/subscriptions");
214 $socid = (int) $request_data[
'socid'];
215 $thirdpartytmp =
new Societe($this->db);
216 $thirdparty_result = $thirdpartytmp->fetch($socid);
217 if ($thirdparty_result < 1) {
218 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
221 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
225 $result = $this->
_validate($request_data);
227 foreach ($request_data as $field => $value) {
228 if ($field ===
'caller') {
230 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
234 $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
243 if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
244 throw new RestException(500,
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
247 return $this->contract->id;
270 public function getLines(
$id, $sortfield =
"d.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
272 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
273 throw new RestException(403);
276 $result = $this->contract->fetch(
$id);
278 throw new RestException(404,
'Contract not found');
282 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
287 $sql =
"SELECT d.rowid";
288 $sql .=
" FROM ".$this->db->prefix().
"contratdet AS d";
289 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contrat AS c ON (c.rowid = d.fk_contrat)";
290 $sql .=
" LEFT JOIN ".$this->db->prefix().
"contratdet_extrafields AS ef ON (ef.fk_object = d.rowid)";
291 $sql .=
" WHERE d.fk_contrat = ".((int)
$id);
292 $sql .=
' AND c.entity IN ('.getEntity(
'contrat').
')';
298 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
302 $sqlTotals = str_replace(
'SELECT d.rowid',
'SELECT count(d.rowid) as total', $sql);
304 $sql .= $this->db->order($sortfield, $sortorder);
309 $offset = $limit * $page;
311 $sql .= $this->db->plimit($limit + 1, $offset);
314 $result = $this->db->query($sql);
316 $num = $this->db->num_rows($result);
317 $min = min($num, ($limit <= 0 ? $num : $limit));
320 $obj = $this->db->fetch_object($result);
322 if ($contratdet_static->fetch($obj->rowid)) {
328 throw new RestException(503,
'Error when retrieve contratdet list : '.$this->db->lasterror());
331 if ($pagination_data) {
332 $totalsResult = $this->db->query($sqlTotals);
333 $total = $this->db->fetch_object($totalsResult)->total;
337 $obj_ret[
'data'] = $tmp;
338 $obj_ret[
'pagination'] = [
339 'total' => (int) $total,
341 'page_count' => ceil((
int) $total / $limit),
363 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
364 throw new RestException(403);
367 $result = $this->contract->fetch(
$id);
369 throw new RestException(404,
'Contract not found');
373 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
376 $request_data = (object) $request_data;
378 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
379 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
381 $updateRes = $this->contract->addline(
383 $request_data->subprice,
385 $request_data->tva_tx,
386 $request_data->localtax1_tx,
387 $request_data->localtax2_tx,
388 $request_data->fk_product,
389 $request_data->remise_percent,
390 $request_data->date_start,
391 $request_data->date_end,
392 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
393 $request_data->subprice_excl_tax,
394 $request_data->info_bits,
395 $request_data->fk_fournprice,
396 $request_data->pa_ht,
397 $request_data->array_options,
398 $request_data->fk_unit,
402 if ($updateRes > 0) {
423 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
424 throw new RestException(403);
427 $result = $this->contract->fetch(
$id);
429 throw new RestException(404,
'Contrat not found');
433 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
436 $request_data = (object) $request_data;
438 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
439 $request_data->price_base_type =
sanitizeVal($request_data->price_base_type);
441 $updateRes = $this->contract->updateline(
444 $request_data->subprice,
446 $request_data->remise_percent,
447 $request_data->date_start,
448 $request_data->date_end,
449 $request_data->tva_tx,
450 $request_data->localtax1_tx,
451 $request_data->localtax2_tx,
452 $request_data->date_start_real,
453 $request_data->date_end_real,
454 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
455 $request_data->info_bits,
456 $request_data->fk_fourn_price,
457 $request_data->pa_ht,
458 $request_data->array_options,
459 $request_data->fk_unit
462 if ($updateRes > 0) {
465 $result->fetch($lineid);
484 'barcode_type_coder',
490 'shipping_method_id',
508 'user_validation_id',
510 'user_modification_id',
511 'cond_reglement_supplier_id',
513 'retained_warranty_fk_cond_reglement',
543 ) as $fieldToUnset) {
544 unset($result->{$fieldToUnset});
547 $result = $this->
get(
$id);
548 unset($result->line);
569 public function activateLine(
$id, $lineid, $datestart, $dateend =
null, $comment =
null)
571 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
572 throw new RestException(403);
575 $result = $this->contract->fetch(
$id);
577 throw new RestException(404,
'Contrat not found');
581 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
584 $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $dateend, $comment);
586 if ($updateRes > 0) {
587 $result = $this->
get(
$id);
588 unset($result->line);
589 return $this->_cleanObjectDatas($result);
609 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
610 throw new RestException(403);
613 $result = $this->contract->fetch(
$id);
615 throw new RestException(404,
'Contrat not found');
619 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
622 $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (
int) $datestart, $comment);
624 if ($updateRes > 0) {
625 $result = $this->
get(
$id);
626 unset($result->line);
627 return $this->_cleanObjectDatas($result);
649 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
650 throw new RestException(403);
653 $result = $this->contract->fetch(
$id);
655 throw new RestException(404,
'Contrat not found');
659 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
664 $updateRes = $this->contract->deleteLine($lineid, DolibarrApiAccess::$user);
665 if ($updateRes > 0) {
666 return $this->
get(
$id);
668 throw new RestException(405, $this->contract->error);
681 public function put(
$id, $request_data =
null)
683 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
684 throw new RestException(403);
686 $result = $this->contract->fetch(
$id);
688 throw new RestException(404,
'Contrat not found');
691 $old_socid = $this->contract->socid;
692 $oldthirdpartytmp =
new Societe($this->db);
693 $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid);
694 if ($old_thirdparty_result < 1) {
695 throw new RestException(404,
'Thirdparty with id='.$old_socid.
' not found or not allowed');
698 throw new RestException(403,
'Access to old thirdparty='.$old_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
702 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
704 foreach ($request_data as $field => $value) {
705 if ($field ==
'id') {
708 if ($field ===
'caller') {
710 $this->contract->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
713 if ($field ==
'array_options' && is_array($value)) {
714 foreach ($value as $index => $val) {
715 $this->contract->array_options[$index] = $this->_checkValForAPI($field, $val, $this->contract);
720 if ($field ==
'socid') {
721 $new_socid = (int) $value;
722 $loopthirdpartytmp =
new Societe($this->db);
723 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
724 if ($new_thirdparty_result < 1) {
725 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
728 throw new RestException(403,
'Access to new thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
732 $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract);
735 if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
736 return $this->
get(
$id);
738 throw new RestException(500, $this->contract->error);
751 public function delete(
$id)
753 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'supprimer')) {
754 throw new RestException(403,
'Missing permission: Delete contracts/subscriptions');
756 $result = $this->contract->fetch(
$id);
758 throw new RestException(404,
'Contract not found');
762 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
765 if (!$this->contract->delete(DolibarrApiAccess::$user)) {
766 throw new RestException(500,
'Error when delete contract : '.$this->contract->error);
772 'message' =>
'Contract deleted'
798 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
799 throw new RestException(403);
801 $result = $this->contract->fetch(
$id);
803 throw new RestException(404,
'Contract not found');
807 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
810 $result = $this->contract->validate(DolibarrApiAccess::$user,
'', $notrigger);
812 throw new RestException(304,
'Error nothing done. May be object is already validated');
815 throw new RestException(500,
'Error when validating Contract: '.$this->contract->error);
821 'message' =>
'Contract validated (Ref='.$this->contract->ref.
')'
847 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'creer')) {
848 throw new RestException(403);
850 $result = $this->contract->fetch(
$id);
852 throw new RestException(404,
'Contract not found');
856 throw new RestException(403,
'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login);
859 $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
861 throw new RestException(304,
'Error nothing done. May be object is already close');
864 throw new RestException(500,
'Error when closing Contract: '.$this->contract->error);
870 'message' =>
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.'
904 if ($data ===
null) {
908 foreach (Contracts::$FIELDS as $field) {
909 if (!isset($data[$field])) {
910 throw new RestException(400,
"$field field missing");
912 $contrat[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List contracts.
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)
deleteLine($id, $lineid)
Delete a line to given contract.
_cleanObjectDatas($object)
Clean sensible object datas.
activateLine($id, $lineid, $datestart, $dateend=null, $comment=null)
Activate a service line of a given contract.
validate($id, $notrigger=0)
Validate a contract.
post($request_data=null)
Create contract object.
unactivateLine($id, $lineid, $datestart, $comment=null)
Unactivate a service line of a given contract.
__construct()
Constructor.
close($id, $notrigger=0)
Close all services of a contract.
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.
_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...)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...