dolibarr 20.0.0
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
25dol_include_once('/mymodule/core/modules/mymodule/modules_myobject.php');
26
27
32{
37 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
39 public $prefix = 'MYOBJECT';
40
44 public $error = '';
45
49 public $name = 'standard';
50
51
58 public function info($langs)
59 {
60 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
61 }
62
63
69 public function getExample()
70 {
71 return $this->prefix."0501-0001";
72 }
73
74
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."mymodule_myobject";
92 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
93 if ($object->ismultientitymanaged == 1) {
94 $sql .= " AND entity = ".$conf->entity;
95 } elseif ($object->ismultientitymanaged == 2) {
96 // TODO
97 }
98
99 $resql = $db->query($sql);
100 if ($resql) {
101 $row = $db->fetch_row($resql);
102 if ($row) {
103 $coyymm = substr($row[0], 0, 6);
104 $max = $row[0];
105 }
106 }
107 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
108 $langs->load("errors");
109 $this->error = $langs->trans('ErrorNumRefModel', $max);
110 return false;
111 }
112
113 return true;
114 }
115
122 public function getNextValue($object)
123 {
124 global $db, $conf;
125
126 // first we get the max value
127 $posindice = strlen($this->prefix) + 6;
128 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
129 $sql .= " FROM ".MAIN_DB_PREFIX."mymodule_myobject";
130 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
131 if ($object->ismultientitymanaged == 1) {
132 $sql .= " AND entity = ".$conf->entity;
133 } elseif ($object->ismultientitymanaged == 2) {
134 // TODO
135 }
136
137 $resql = $db->query($sql);
138 if ($resql) {
139 $obj = $db->fetch_object($resql);
140 if ($obj) {
141 $max = intval($obj->max);
142 } else {
143 $max = 0;
144 }
145 } else {
146 dol_syslog("mod_myobject_standard::getNextValue", LOG_DEBUG);
147 return -1;
148 }
149
150 //$date=time();
151 $date = $object->date_creation;
152 $yymm = dol_print_date($date, "%y%m");
153
154 if ($max >= (pow(10, 4) - 1)) {
155 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
156 } else {
157 $num = sprintf("%04s", $max + 1);
158 }
159
160 dol_syslog("mod_myobject_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
161 return $this->prefix.$yymm."-".$num;
162 }
163}
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.