dolibarr 20.0.0
mod_payment_cicada.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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
26require_once DOL_DOCUMENT_ROOT.'/core/modules/payment/modules_payment.php';
27
32{
37 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
39 public $prefix = 'PAY';
40
44 public $error = '';
45
51 public $nom = 'Cicada';
52
56 public $name = 'Cicada';
57
58
65 public function info($langs)
66 {
67 global $langs;
68 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
69 }
70
71
77 public function getExample()
78 {
79 return $this->prefix."0501-0001";
80 }
81
82
90 public function canBeActivated($object)
91 {
92 global $conf, $langs, $db;
93
94 $payyymm = '';
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."paiement";
100 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
101 $sql .= " AND entity = ".$conf->entity;
102
103 $resql = $db->query($sql);
104 if ($resql) {
105 $row = $db->fetch_row($resql);
106 if ($row) {
107 $payyymm = substr($row[0], 0, 6);
108 $max = $row[0];
109 }
110 }
111 if ($payyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $payyymm)) {
112 $langs->load("errors");
113 $this->error = $langs->trans('ErrorNumRefModel', $max);
114 return false;
115 }
116
117 return true;
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."paiement";
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(__METHOD__, LOG_DEBUG);
148 return -1;
149 }
150
151 //$date=time();
152 $date = $object->datepaye;
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(__METHOD__." return ".$this->prefix.$yymm."-".$num);
162 return $this->prefix.$yymm."-".$num;
163 }
164
165
166 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
174 public function payment_get_num($objsoc, $objforref)
175 {
176 // phpcs:enable
177 return $this->getNextValue($objsoc, $objforref);
178 }
179}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Payment numbering references mother class.
Class to manage customer payment numbering rules Cicada.
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...
getNextValue($objsoc, $object)
Return next free value.
payment_get_num($objsoc, $objforref)
Return next free value.
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).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.