dolibarr  13.0.2
mod_facture_fournisseur_cactus.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
5  * Copyright (C) 2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  * or see https://www.gnu.org/
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
29 
30 
35 {
40  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41 
45  public $error = '';
46 
52  public $nom = 'Cactus';
53 
57  public $name = 'Cactus';
58 
59  public $prefixinvoice = 'SI';
60 
61  public $prefixcreditnote = 'SA';
62 
63  public $prefixdeposit = 'SD';
64 
65 
71  public function info()
72  {
73  global $langs;
74  $langs->load("bills");
75  return $langs->trans("CactusNumRefModelDesc1", $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
76  }
77 
78 
84  public function getExample()
85  {
86  return $this->prefixinvoice."1301-0001";
87  }
88 
89 
95  public function canBeActivated()
96  {
97  global $conf, $langs, $db;
98 
99  $langs->load("bills");
100 
101  // Check invoice num
102  $siyymm = ''; $max = '';
103 
104  $posindice = strlen($this->prefixinvoice) + 6;
105  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
106  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
107  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
108  $sql .= " AND entity = ".$conf->entity;
109  $resql = $db->query($sql);
110  if ($resql)
111  {
112  $row = $db->fetch_row($resql);
113  if ($row) { $siyymm = substr($row[0], 0, 6); $max = $row[0]; }
114  }
115  if ($siyymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $siyymm))
116  {
117  $langs->load("errors");
118  $this->error = $langs->trans('ErrorNumRefModel', $max);
119  return false;
120  }
121 
122  // Check credit note num
123  $siyymm = '';
124 
125  $posindice = strlen($this->prefixcreditnote) + 6;
126  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
127  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
128  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
129  $sql .= " AND entity = ".$conf->entity;
130 
131  $resql = $db->query($sql);
132  if ($resql)
133  {
134  $row = $db->fetch_row($resql);
135  if ($row) { $siyymm = substr($row[0], 0, 6); $max = $row[0]; }
136  }
137  if ($siyymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $siyymm))
138  {
139  $this->error = $langs->trans('ErrorNumRefModel', $max);
140  return false;
141  }
142 
143  // Check deposit num
144  $siyymm = '';
145 
146  $posindice = strlen($this->prefixdeposit) + 6;
147  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
148  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
149  $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
150  $sql .= " AND entity = ".$conf->entity;
151 
152  $resql = $db->query($sql);
153  if ($resql)
154  {
155  $row = $db->fetch_row($resql);
156  if ($row) { $siyymm = substr($row[0], 0, 6); $max = $row[0]; }
157  }
158  if ($siyymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $siyymm))
159  {
160  $this->error = $langs->trans('ErrorNumRefModel', $max);
161  return false;
162  }
163  }
164 
173  public function getNextValue($objsoc, $object, $mode = 'next')
174  {
175  global $db, $conf;
176 
177  $prefix = $this->prefixinvoice;
178  if ($object->type == 2) $prefix = $this->prefixcreditnote;
179  elseif ($object->type == 3) $prefix = $this->prefixdeposit;
180 
181  // First, we get the max value
182  $posindice = strlen($prefix) + 6;
183  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
184  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
185  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
186  $sql .= " AND entity = ".$conf->entity;
187 
188  $resql = $db->query($sql);
189  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
190  if ($resql)
191  {
192  $obj = $db->fetch_object($resql);
193  if ($obj) $max = intval($obj->max);
194  else $max = 0;
195  } else {
196  return -1;
197  }
198 
199  if ($mode == 'last')
200  {
201  if ($max >= (pow(10, 4) - 1)) $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
202  else $num = sprintf("%04s", $max);
203 
204  $ref = '';
205  $sql = "SELECT ref as ref";
206  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn";
207  $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
208  $sql .= " AND entity = ".$conf->entity;
209 
210  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
211  $resql = $db->query($sql);
212  if ($resql)
213  {
214  $obj = $db->fetch_object($resql);
215  if ($obj) $ref = $obj->ref;
216  } else dol_print_error($db);
217 
218  return $ref;
219  } elseif ($mode == 'next')
220  {
221  $date = $object->date; // This is invoice date (not creation date)
222  $yymm = strftime("%y%m", $date);
223 
224  if ($max >= (pow(10, 4) - 1)) $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
225  else $num = sprintf("%04s", $max + 1);
226 
227  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
228  return $prefix.$yymm."-".$num;
229  } else dol_print_error('', 'Bad parameter for getNextValue');
230  }
231 
232 
241  public function getNumRef($objsoc, $objforref, $mode = 'next')
242  {
243  return $this->getNextValue($objsoc, $objforref, $mode);
244  }
245 }
getNumRef($objsoc, $objforref, $mode= 'next')
Return next free value.
getExample()
Returns a numbering example.
info()
Return description of numbering model.
Parent Class of numbering models of suppliers invoices references.
getNextValue($objsoc, $object, $mode= 'next')
Return next value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Cactus Class of numbering models of suppliers invoices references.
canBeActivated()
Tests if the numbers already in force in the database do not cause conflicts that would prevent this ...