dolibarr 21.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 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 public $version = 'dolibarr';
36
37 // variables not inherited
38
42 public $prefix = 'CT';
43
47 public function __construct()
48 {
49 $this->code_auto = 1;
50 }
51
58 public function info($langs)
59 {
60 global $langs;
61 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
62 }
63
64
70 public function getExample()
71 {
72 return $this->prefix."0501-0001";
73 }
74
75
82 public function canBeActivated($object)
83 {
84 global $conf, $langs, $db;
85
86 $coyymm = '';
87 $max = '';
88
89 $posindice = strlen($this->prefix) + 6;
90 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
91 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
92 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
93 $sql .= " AND entity = ".$conf->entity;
94
95 $resql = $db->query($sql);
96 if ($resql) {
97 $row = $db->fetch_row($resql);
98 if ($row) {
99 $coyymm = substr($row[0], 0, 6);
100 $max = $row[0];
101 }
102 }
103 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
104 $langs->load("errors");
105 $this->error = $langs->trans('ErrorNumRefModel', $max);
106 return false;
107 }
108
109 return true;
110 }
111
119 public function getNextValue($objsoc, $contract)
120 {
121 global $db, $conf;
122
123 $posindice = strlen($this->prefix) + 6;
124 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
125 $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
126 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
127 $sql .= " AND entity = ".$conf->entity;
128
129 $resql = $db->query($sql);
130 if ($resql) {
131 $obj = $db->fetch_object($resql);
132 if ($obj) {
133 $max = intval($obj->max);
134 } else {
135 $max = 0;
136 }
137 } else {
138 dol_syslog("mod_contract_serpis::getNextValue", LOG_DEBUG);
139 return -1;
140 }
141
142 $date = $contract->date_contrat;
143 $yymm = dol_print_date($date, "%y%m");
144
145 if ($max >= (pow(10, 4) - 1)) {
146 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
147 } else {
148 $num = sprintf("%04d", $max + 1);
149 }
150
151 dol_syslog("mod_contract_serpis::getNextValue return ".$this->prefix.$yymm."-".$num);
152 return $this->prefix.$yymm."-".$num;
153 }
154}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $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.