dolibarr 25.0.0-alpha
mod_contract_serpis.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26require_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
27
32{
33 // variables inherited from ModelNumRefContracts class
34 public $name = 'Serpis';
35
36 public $version = 'dolibarr';
37
41 public $position = 10;
42
43 // variables not inherited
44
48 public $prefix = 'CT';
49
53 public function __construct()
54 {
55 $this->code_auto = 1;
56 }
57
64 public function info($langs)
65 {
66 global $langs;
67 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
68 }
69
70
76 public function getExample()
77 {
78 return $this->prefix."0501-0001";
79 }
80
81
88 public function canBeActivated($object)
89 {
90 global $conf, $langs, $db;
91
92 $coyymm = '';
93 $max = '';
94
95 $posindice = strlen($this->prefix) + 6;
96 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".((int) $posindice).") AS SIGNED)) as max";
97 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
98 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
99 $sql .= " AND entity = ".((int) $conf->entity);
100
101 $resql = $db->query($sql);
102 if ($resql) {
103 $row = $db->fetch_row($resql);
104 if ($row) {
105 $coyymm = substr($row[0], 0, 6);
106 $max = $row[0];
107 }
108 }
109 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
110 $langs->load("errors");
111 $this->error = $langs->trans('ErrorNumRefModel', $max);
112 return false;
113 }
114
115 return true;
116 }
117
125 public function getNextValue($objsoc, $contract)
126 {
127 global $db, $conf;
128
129 $posindice = strlen($this->prefix) + 6;
130 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".((int) $posindice).") AS SIGNED)) as max";
131 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
132 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
133 $sql .= " AND entity = ".((int) $conf->entity);
134
135 $resql = $db->query($sql);
136 if ($resql) {
137 $obj = $db->fetch_object($resql);
138 if ($obj) {
139 $max = intval($obj->max);
140 } else {
141 $max = 0;
142 }
143 } else {
144 dol_syslog("mod_contract_serpis::getNextValue", LOG_DEBUG);
145 return -1;
146 }
147
148 $date = $contract->date_contrat;
149 $yymm = dol_print_date($date, "%y%m");
150
151 if ($max >= (pow(10, 4) - 1)) {
152 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
153 } else {
154 $num = sprintf("%04d", $max + 1);
155 }
156
157 dol_syslog("mod_contract_serpis::getNextValue return ".$this->prefix.$yymm."-".$num);
158 return $this->prefix.$yymm."-".$num;
159 }
160}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class for all contract numbering modules.
Class to manage contract numbering rules Serpis.
info($langs)
Return default description of numbering model.
getExample()
Return numbering example.
getNextValue($objsoc, $contract)
Return next value.
canBeActivated($object)
Test if existing numbers make problems with numbering.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.