dolibarr 19.0.4
mod_reception_beryl.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Quentin Vial-Gouteyron <quentin.vial-gouteyron@atm-consulting.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
24require_once DOL_DOCUMENT_ROOT.'/core/modules/reception/modules_reception.php';
25
30{
31 public $version = 'dolibarr';
32 public $prefix = 'RCP';
33 public $error = '';
34 public $nom = 'Beryl';
35
36
43 public function info($langs)
44 {
45 global $langs;
46 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
47 }
48
49
55 public function getExample()
56 {
57 return $this->prefix."0501-0001";
58 }
59
60
67 public function canBeActivated($object)
68 {
69 global $conf, $langs, $db;
70
71 $coyymm = '';
72 $max = '';
73
74 $posindice = strlen($this->prefix) + 6;
75 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
76 $sql .= " FROM ".MAIN_DB_PREFIX."reception";
77 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
78 $sql .= " AND entity = ".$conf->entity;
79
80 $resql = $db->query($sql);
81 if ($resql) {
82 $row = $db->fetch_row($resql);
83 if ($row) {
84 $coyymm = substr($row[0], 0, 6);
85 $max = $row[0];
86 }
87 }
88 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
89 $langs->load("errors");
90 $this->error = $langs->trans('ErrorNumRefModel', $max);
91 return false;
92 }
93
94 return true;
95 }
96
104 public function getNextValue($objsoc, $reception)
105 {
106 global $db, $conf;
107
108 $posindice = strlen($this->prefix) + 6;
109 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
110 $sql .= " FROM ".MAIN_DB_PREFIX."reception";
111 $sql .= " WHERE ref like '".$db->escape($this->prefix)."____-%'";
112 $sql .= " AND entity = ".$conf->entity;
113
114 $resql = $db->query($sql);
115 if ($resql) {
116 $obj = $db->fetch_object($resql);
117 if ($obj) {
118 $max = intval($obj->max);
119 } else {
120 $max = 0;
121 }
122 } else {
123 dol_syslog("mod_reception_beryl::getNextValue", LOG_DEBUG);
124 return -1;
125 }
126
127 $date = time();
128 $yymm = dol_print_date($date, "%y%m");
129
130 if ($max >= (pow(10, 4) - 1)) {
131 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
132 } else {
133 $num = sprintf("%04s", $max + 1);
134 }
135
136 dol_syslog("mod_reception_beryl::getNextValue return ".$this->prefix.$yymm."-".$num);
137 return $this->prefix.$yymm."-".$num;
138 }
139
140 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
148 public function reception_get_num($objsoc, $objforref)
149 {
150 // phpcs:enable
151 return $this->getNextValue($objsoc, $objforref);
152 }
153}
Parent Class of numbering models of sending receipts references.
Class to manage reception numbering rules Beryl.
getExample()
Return numbering example.
getNextValue($objsoc, $reception)
Return next value.
canBeActivated($object)
Test if existing numbers make problems with numbering.
info($langs)
Return default description of numbering model.
reception_get_num($objsoc, $objforref)
Return next free value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $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.