dolibarr 24.0.0-beta
mod_member_advanced.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_member.class.php';
28
29
34{
35 // variables inherited from ModeleNumRefMembers class
36 public $name = 'Advanced';
37 public $version = 'dolibarr';
38
42 public $position = 40;
43
44 // variables not inherited
45
49 public $prefix = 'MEM';
50
54 public function __construct()
55 {
56 $this->code_auto = 1;
57 }
58
65 public function info($langs)
66 {
67 global $langs;
68 return $langs->trans("AdvancedNumRefModelDesc", $this->prefix);
69 }
70
71
77 public function getExample()
78 {
79 return $this->prefix."2301-0001";
80 }
81
82
90 public function canBeActivated($object)
91 {
92 global $conf, $langs, $db;
93
94 $coyymm = '';
95 $max = '';
96
97 $posindice = strlen($this->prefix) + 6;
98 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
99 $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
100 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
101 $sql .= " AND entity = ".$conf->entity;
102 $resql = $db->query($sql);
103 if ($resql) {
104 $row = $db->fetch_row($resql);
105 if ($row) {
106 $coyymm = substr($row[0], 0, 6);
107 $max = $row[0];
108 }
109 }
110 if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
111 return true;
112 } else {
113 $langs->load("errors");
114 $this->error = $langs->trans('ErrorNumRefModel', $max);
115 return false;
116 }
117 }
118
119
127 public function getNextValue($objsoc, $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."adherent";
135 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
136 $sql .= " AND entity = ".$conf->entity;
137
138 $resql = $db->query($sql);
139 if ($resql) {
140 $obj = $db->fetch_object($resql);
141 if ($obj) {
142 $max = intval($obj->max);
143 } else {
144 $max = 0;
145 }
146 } else {
147 dol_syslog("mod_member_advanced::getNextValue", LOG_DEBUG);
148 return -1;
149 }
150
151 $date = empty($object->datec) ? dol_now() : $object->datec;
152
153 $yymm = dol_print_date($date, '%y%m', 'gmt');
154
155 if ($max >= (pow(10, 4) - 1)) {
156 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
157 } else {
158 $num = sprintf("%04d", $max + 1);
159 }
160
161 dol_syslog("mod_member_advanced::getNextValue return ".$this->prefix.$yymm."-".$num, LOG_INFO);
162 return $this->prefix.$yymm."-".$num;
163 }
164}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class mere des modeles de numerotation des references de members.
Class to manage the numbering module Advanced for member references.
info($langs)
Return description of numbering module.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getExample()
Return an example of numbering module values.
getNextValue($objsoc, $object)
Return next value.
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.
dol_now($mode='gmt')
Return date for now.
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.