dolibarr 21.0.0-alpha
mod_partnership_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 *
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
26require_once DOL_DOCUMENT_ROOT.'/partnership/core/modules/partnership/modules_partnership.php';
27
28
33{
38 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
39
40 public $prefix = 'PSHIP';
41
45 public $error = '';
46
50 public $name = 'standard';
51
52
59 public function info($langs)
60 {
61 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
62 }
63
64
70 public function getExample()
71 {
72 return $this->prefix."0501-0001";
73 }
74
75
83 public function canBeActivated($object)
84 {
85 global $conf, $langs, $db;
86
87 $coyymm = '';
88 $max = '';
89
90 $posindice = strlen($this->prefix) + 6;
91 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
92 $sql .= " FROM ".MAIN_DB_PREFIX."partnership";
93 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
94 if ($object->ismultientitymanaged == 1) {
95 $sql .= " AND entity = ".$conf->entity;
96 } elseif ($object->ismultientitymanaged == 2) {
97 // TODO
98 }
99
100 $resql = $db->query($sql);
101 if ($resql) {
102 $row = $db->fetch_row($resql);
103 if ($row) {
104 $coyymm = substr($row[0], 0, 6);
105 $max = $row[0];
106 }
107 }
108 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
109 $langs->load("errors");
110 $this->error = $langs->trans('ErrorNumRefModel', $max);
111 return false;
112 }
113
114 return true;
115 }
116
123 public function getNextValue($object)
124 {
125 global $db, $conf;
126
127 // first we get the max value
128 $posindice = strlen($this->prefix) + 6;
129 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
130 $sql .= " FROM ".MAIN_DB_PREFIX."partnership";
131 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
132 if ($object->ismultientitymanaged == 1) {
133 $sql .= " AND entity = ".$conf->entity;
134 } elseif ($object->ismultientitymanaged == 2) {
135 // TODO
136 }
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_partnership_standard::getNextValue", LOG_DEBUG);
148 return -1;
149 }
150
151 //$date=time();
152 $date = $object->date_creation;
153 $yymm = dol_print_date($date, "%y%m");
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_partnership_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
162 return $this->prefix.$yymm."-".$num;
163 }
164}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class to manage numbering of Partnership.
Class to manage the Standard numbering rule for Partnership.
info($langs)
Return description of numbering module.
getNextValue($object)
Return next free value.
getExample()
Return an example of numbering.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
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.