dolibarr 19.0.3
mod_contract_serpis.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
24require_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
25
30{
35 public $version = 'dolibarr';
36
37 public $prefix = 'CT';
38
42 public $error = '';
43
49 public $nom = 'Serpis';
50
54 public $name = 'Serpis';
55
59 public $code_auto = 1;
60
61
68 public function info($langs)
69 {
70 global $langs;
71 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
72 }
73
74
80 public function getExample()
81 {
82 return $this->prefix."0501-0001";
83 }
84
85
92 public function canBeActivated($object)
93 {
94 global $conf, $langs, $db;
95
96 $coyymm = '';
97 $max = '';
98
99 $posindice = strlen($this->prefix) + 6;
100 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
101 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
102 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
103 $sql .= " AND entity = ".$conf->entity;
104
105 $resql = $db->query($sql);
106 if ($resql) {
107 $row = $db->fetch_row($resql);
108 if ($row) {
109 $coyymm = substr($row[0], 0, 6);
110 $max = $row[0];
111 }
112 }
113 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
114 $langs->load("errors");
115 $this->error = $langs->trans('ErrorNumRefModel', $max);
116 return false;
117 }
118
119 return true;
120 }
121
129 public function getNextValue($objsoc, $contract)
130 {
131 global $db, $conf;
132
133 $posindice = strlen($this->prefix) + 6;
134 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
135 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
136 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
137 $sql .= " AND entity = ".$conf->entity;
138
139 $resql = $db->query($sql);
140 if ($resql) {
141 $obj = $db->fetch_object($resql);
142 if ($obj) {
143 $max = intval($obj->max);
144 } else {
145 $max = 0;
146 }
147 } else {
148 dol_syslog("mod_contract_serpis::getNextValue", LOG_DEBUG);
149 return -1;
150 }
151
152 $date = $contract->date_contrat;
153 $yymm = dol_print_date($date, "%y%m");
154
155 if ($max >= (pow(10, 4) - 1)) {
156 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
157 } else {
158 $num = sprintf("%04s", $max + 1);
159 }
160
161 dol_syslog("mod_contract_serpis::getNextValue return ".$this->prefix.$yymm."-".$num);
162 return $this->prefix.$yymm."-".$num;
163 }
164
165
166 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
174 public function contract_get_num($objsoc, $objforref)
175 {
176 // phpcs:enable
177 return $this->getNextValue($objsoc, $objforref);
178 }
179}
Parent class for all contract numbering modules.
Class to manage contract numbering rules Serpis.
info($langs)
Return default description of numbering model.
contract_get_num($objsoc, $objforref)
Return next value.
getExample()
Return numbering example.
getNextValue($objsoc, $contract)
Return next value.
canBeActivated($object)
Test if existing numbers make problems with numbering.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
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.