dolibarr 21.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
39 // variables not inherited
40
44 public $prefix = 'MEM';
45
49 public function __construct()
50 {
51 $this->code_auto = 1;
52 }
53
60 public function info($langs)
61 {
62 global $langs;
63 return $langs->trans("AdvancedNumRefModelDesc", $this->prefix);
64 }
65
66
72 public function getExample()
73 {
74 return $this->prefix."2301-0001";
75 }
76
77
85 public function canBeActivated($object)
86 {
87 global $conf, $langs, $db;
88
89 $coyymm = '';
90 $max = '';
91
92 $posindice = strlen($this->prefix) + 6;
93 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
94 $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
95 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
96 $sql .= " AND entity = ".$conf->entity;
97 $resql = $db->query($sql);
98 if ($resql) {
99 $row = $db->fetch_row($resql);
100 if ($row) {
101 $coyymm = substr($row[0], 0, 6);
102 $max = $row[0];
103 }
104 }
105 if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
106 return true;
107 } else {
108 $langs->load("errors");
109 $this->error = $langs->trans('ErrorNumRefModel', $max);
110 return false;
111 }
112 }
113
114
122 public function getNextValue($objsoc, $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."adherent";
130 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
131 $sql .= " AND entity = ".$conf->entity;
132
133 $resql = $db->query($sql);
134 if ($resql) {
135 $obj = $db->fetch_object($resql);
136 if ($obj) {
137 $max = intval($obj->max);
138 } else {
139 $max = 0;
140 }
141 } else {
142 dol_syslog("mod_member_advanced::getNextValue", LOG_DEBUG);
143 return -1;
144 }
145
146 $date = empty($object->datec) ? dol_now() : $object->datec;
147
148 $yymm = dol_print_date($date, '%y%m', 'gmt');
149
150 if ($max >= (pow(10, 4) - 1)) {
151 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
152 } else {
153 $num = sprintf("%04d", $max + 1);
154 }
155
156 dol_syslog("mod_member_advanced::getNextValue return ".$this->prefix.$yymm."-".$num, LOG_INFO);
157 return $this->prefix.$yymm."-".$num;
158 }
159}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
dol_now($mode='auto')
Return date for now.
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).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79