dolibarr 25.0.0-alpha
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
27require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
28dol_include_once('/mymodule/core/modules/mymodule/modules_myobject.php');
29
30
35{
40 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41
45 public $prefix = 'MYOBJECT';
46
50 public $error = '';
51
55 public $name = 'standard';
56
60 public $position = 40;
61
62
69 public function info($langs)
70 {
71 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
72 }
73
74
80 public function getExample()
81 {
82 return $this->prefix."0501-0001";
83 }
84
85
93 public function canBeActivated($object)
94 {
95 global $conf, $langs, $db;
96
97 $coyymm = '';
98 $max = '';
99
100 $posindice = dol_strlen($this->prefix) + 6;
101 $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".((int) $posindice).") AS SIGNED)) as max";
102 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
103 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
104 if ($object->ismultientitymanaged == 1) {
105 $sql .= " AND t.entity = ".((int) $conf->entity);
106 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
107 $tmparray = explode('@', (string) $object->ismultientitymanaged);
108 $sql .= " LEFT JOIN ".$db->prefix().$db->sanitize($tmparray[1])." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
109 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
110 }
111
112 $resql = $db->query($sql);
113 if ($resql) {
114 $row = $db->fetch_row($resql);
115 if ($row) {
116 $coyymm = dol_substr($row[0], 0, 6);
117 $max = $row[0];
118 }
119 }
120 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
121 $langs->load("errors");
122 $this->error = $langs->trans('ErrorNumRefModel', $max);
123 return false;
124 }
125
126 return true;
127 }
128
135 public function getNextValue($object)
136 {
137 global $db, $conf;
138
139 // first we get the max value
140 $posindice = dol_strlen($this->prefix) + 6;
141 $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".((int) $posindice).") AS SIGNED)) as max";
142 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
143 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
144 if ($object->ismultientitymanaged == 1) {
145 $sql .= " AND t.entity = ".((int) $conf->entity);
146 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
147 $tmparray = explode('@', (string) $object->ismultientitymanaged);
148 $sql .= " LEFT JOIN ".$db->prefix().$db->sanitize($tmparray[1])." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
149 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
150 }
151
152 $resql = $db->query($sql);
153 if ($resql) {
154 $obj = $db->fetch_object($resql);
155 if ($obj) {
156 $max = intval($obj->max);
157 } else {
158 $max = 0;
159 }
160 } else {
161 dol_syslog("mod_myobject_standard::getNextValue", LOG_DEBUG);
162 return -1;
163 }
164
165 //$date=time();
166 $date = $object->date_creation;
167 $yymm = dol_print_date($date, "%y%m");
168
169 if ($max >= (pow(10, 4) - 1)) {
170 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
171 } else {
172 $num = sprintf("%04u", $max + 1);
173 }
174
175 dol_syslog("mod_myobject_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
176 return $this->prefix.$yymm."-".$num;
177 }
178}
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(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
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.