dolibarr 24.0.0-beta
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
59 public $position = 40;
60
61
68 public function info($langs)
69 {
70 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
71 }
72
73
79 public function getExample()
80 {
81 return $this->prefix."0501-0001";
82 }
83
84
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(t.ref FROM ".$posindice.") AS SIGNED)) as max";
101 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
102 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
103 if ($object->ismultientitymanaged == 1) {
104 $sql .= " AND t.entity = ".((int) $conf->entity);
105 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
106 $tmparray = explode('@', (string) $object->ismultientitymanaged);
107 $sql .= " LEFT JOIN ".$db->prefix().$tmparray[1]." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
108 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
109 }
110
111 $resql = $db->query($sql);
112 if ($resql) {
113 $row = $db->fetch_row($resql);
114 if ($row) {
115 $coyymm = substr($row[0], 0, 6);
116 $max = $row[0];
117 }
118 }
119 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
120 $langs->load("errors");
121 $this->error = $langs->trans('ErrorNumRefModel', $max);
122 return false;
123 }
124
125 return true;
126 }
127
134 public function getNextValue($object)
135 {
136 global $db, $conf;
137
138 // first we get the max value
139 $posindice = strlen($this->prefix) + 6;
140 $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".$posindice.") AS SIGNED)) as max";
141 $sql .= " FROM ".$db->prefix()."mymodule_myobject as t";
142 $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'";
143 if ($object->ismultientitymanaged == 1) {
144 $sql .= " AND t.entity = ".((int) $conf->entity);
145 } elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) {
146 $tmparray = explode('@', (string) $object->ismultientitymanaged);
147 $sql .= " LEFT JOIN ".$db->prefix().$tmparray[1]." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid";
148 $sql .= " WHERE pt.entity IN (".getEntity($object->element).")";
149 }
150
151 $resql = $db->query($sql);
152 if ($resql) {
153 $obj = $db->fetch_object($resql);
154 if ($obj) {
155 $max = intval($obj->max);
156 } else {
157 $max = 0;
158 }
159 } else {
160 dol_syslog("mod_myobject_standard::getNextValue", LOG_DEBUG);
161 return -1;
162 }
163
164 //$date=time();
165 $date = $object->date_creation;
166 $yymm = dol_print_date($date, "%y%m");
167
168 if ($max >= (pow(10, 4) - 1)) {
169 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
170 } else {
171 $num = sprintf("%04u", $max + 1);
172 }
173
174 dol_syslog("mod_myobject_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
175 return $this->prefix.$yymm."-".$num;
176 }
177}
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:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.