dolibarr 23.0.3
mod_myobject_standard.php
1<?php
2/* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) ---Replace with your own copyright and developer email---
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 * or see https://www.gnu.org/
20 */
21
27dol_include_once('/mymodule/core/modules/mymodule/modules_myobject.php');
28
29
34{
39 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40
44 public $prefix = 'MYOBJECT';
45
49 public $error = '';
50
54 public $name = 'standard';
55
56
63 public function info($langs)
64 {
65 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
66 }
67
68
74 public function getExample()
75 {
76 return $this->prefix."0501-0001";
77 }
78
79
87 public function canBeActivated($object)
88 {
89 global $conf, $langs, $db;
90
91 $coyymm = '';
92 $max = '';
93
94 $posindice = strlen($this->prefix) + 6;
95 $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".$posindice.") AS SIGNED)) as max";
96 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
97 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
98 if ($object->ismultientitymanaged == 1) {
99 $sql .= " AND t.entity = ".((int) $conf->entity);
100 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
101 $tmparray = explode('@', (string) $object->ismultientitymanaged);
102 $sql .= " LEFT JOIN ".$db->prefix().$tmparray[1]." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
103 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
104 }
105
106 $resql = $db->query($sql);
107 if ($resql) {
108 $row = $db->fetch_row($resql);
109 if ($row) {
110 $coyymm = substr($row[0], 0, 6);
111 $max = $row[0];
112 }
113 }
114 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
115 $langs->load("errors");
116 $this->error = $langs->trans('ErrorNumRefModel', $max);
117 return false;
118 }
119
120 return true;
121 }
122
129 public function getNextValue($object)
130 {
131 global $db, $conf;
132
133 // first we get the max value
134 $posindice = strlen($this->prefix) + 6;
135 $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".$posindice.") AS SIGNED)) as max";
136 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
137 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
138 if ($object->ismultientitymanaged == 1) {
139 $sql .= " AND t.entity = ".((int) $conf->entity);
140 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
141 $tmparray = explode('@', (string) $object->ismultientitymanaged);
142 $sql .= " LEFT JOIN ".$db->prefix().$tmparray[1]." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
143 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
144 }
145
146 $resql = $db->query($sql);
147 if ($resql) {
148 $obj = $db->fetch_object($resql);
149 if ($obj) {
150 $max = intval($obj->max);
151 } else {
152 $max = 0;
153 }
154 } else {
155 dol_syslog("mod_myobject_standard::getNextValue", LOG_DEBUG);
156 return -1;
157 }
158
159 //$date=time();
160 $date = $object->date_creation;
161 $yymm = dol_print_date($date, "%y%m");
162
163 if ($max >= (pow(10, 4) - 1)) {
164 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
165 } else {
166 $num = sprintf("%04u", $max + 1);
167 }
168
169 dol_syslog("mod_myobject_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
170 return $this->prefix.$yymm."-".$num;
171 }
172}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class to manage numbering of MyObject.
Class to manage the Standard numbering rule for MyObject.
getNextValue($object)
Return next free value.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
info($langs)
Return description of numbering module.
getExample()
Return an example of numbering.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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.