dolibarr 21.0.0-alpha
mod_expedition_safor.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
3 * Copyright (C) 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
26require_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
27
32{
37 public $version = 'dolibarr';
38
39 public $prefix = 'SH';
40
44 public $error = '';
45
51 public $nom = 'Safor';
52
56 public $name = 'Safor';
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
89 public function canBeActivated($object)
90 {
91 global $conf, $langs, $db;
92
93 $coyymm = '';
94 $max = '';
95
96 $posindice = strlen($this->prefix) + 6;
97 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
98 $sql .= " FROM ".MAIN_DB_PREFIX."expedition";
99 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
100 $sql .= " AND entity = ".$conf->entity;
101
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 $langs->load("errors");
112 $this->error = $langs->trans('ErrorNumRefModel', $max);
113 return false;
114 }
115
116 return true;
117 }
118
126 public function getNextValue($objsoc, $shipment)
127 {
128 global $db, $conf;
129
130 $posindice = strlen($this->prefix) + 6;
131 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
132 $sql .= " FROM ".MAIN_DB_PREFIX."expedition";
133 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
134 $sql .= " AND entity = ".$conf->entity;
135
136 $resql = $db->query($sql);
137 if ($resql) {
138 $obj = $db->fetch_object($resql);
139 if ($obj) {
140 $max = intval($obj->max);
141 } else {
142 $max = 0;
143 }
144 } else {
145 dol_syslog("mod_expedition_safor::getNextValue", LOG_DEBUG);
146 return -1;
147 }
148
149 $date = $shipment->date_creation;
150 $yymm = dol_print_date($date, "%y%m");
151
152 if ($max >= (pow(10, 4) - 1)) {
153 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
154 } else {
155 $num = sprintf("%04d", $max + 1);
156 }
157
158 dol_syslog("mod_expedition_safor::getNextValue return ".$this->prefix.$yymm."-".$num);
159 return $this->prefix.$yymm."-".$num;
160 }
161}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent Class of numbering models of sending receipts references.
Class to manage expedition numbering rules Safor.
canBeActivated($object)
Test if existing numbers make problems with numbering.
info($langs)
Return default description of numbering model.
getExample()
Return numbering example.
getNextValue($objsoc, $shipment)
Return next value.
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.