dolibarr 21.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
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(ref FROM ".$posindice.") AS SIGNED)) as max";
96 $sql .= " FROM ".MAIN_DB_PREFIX."mymodule_myobject";
97 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
98 if ($object->ismultientitymanaged == 1) {
99 $sql .= " AND entity = ".$conf->entity;
100 } elseif ($object->ismultientitymanaged == 2) { // @phan-suppress-current-line PhanPluginEmptyStatementIf
101 // TODO
102 }
103
104 $resql = $db->query($sql);
105 if ($resql) {
106 $row = $db->fetch_row($resql);
107 if ($row) {
108 $coyymm = substr($row[0], 0, 6);
109 $max = $row[0];
110 }
111 }
112 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
113 $langs->load("errors");
114 $this->error = $langs->trans('ErrorNumRefModel', $max);
115 return false;
116 }
117
118 return true;
119 }
120
127 public function getNextValue($object)
128 {
129 global $db, $conf;
130
131 // first we get the max value
132 $posindice = strlen($this->prefix) + 6;
133 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
134 $sql .= " FROM ".MAIN_DB_PREFIX."mymodule_myobject";
135 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
136 if ($object->ismultientitymanaged == 1) {
137 $sql .= " AND entity = ".$conf->entity;
138 } elseif ($object->ismultientitymanaged == 2) {
139 // TODO
140 }
141
142 $resql = $db->query($sql);
143 if ($resql) {
144 $obj = $db->fetch_object($resql);
145 if ($obj) {
146 $max = intval($obj->max);
147 } else {
148 $max = 0;
149 }
150 } else {
151 dol_syslog("mod_myobject_standard::getNextValue", LOG_DEBUG);
152 return -1;
153 }
154
155 //$date=time();
156 $date = $object->date_creation;
157 $yymm = dol_print_date($date, "%y%m");
158
159 if ($max >= (pow(10, 4) - 1)) {
160 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
161 } else {
162 $num = sprintf("%04u", $max + 1);
163 }
164
165 dol_syslog("mod_myobject_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
166 return $this->prefix.$yymm."-".$num;
167 }
168}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
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).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.