dolibarr  17.0.3
mod_codeproduct_elephant.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2007-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
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/product/modules_product.class.php';
29 
30 
36 {
42  public $nom = 'Elephant';
43 
47  public $name = 'Elephant';
48 
49  public $code_modifiable; // Code modifiable
50 
51  public $code_modifiable_invalide; // Code modifiable si il est invalide
52 
53  public $code_modifiable_null; // Code modifiables si il est null
54 
55  public $code_null; // Code facultatif
56 
61  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
62 
66  public $code_auto;
67 
68  public $searchcode; // String de recherche
69 
70  public $numbitcounter; // Nombre de chiffres du compteur
71 
72  public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
73 
74 
78  public function __construct()
79  {
80  $this->code_null = 0;
81  $this->code_modifiable = 1;
82  $this->code_modifiable_invalide = 1;
83  $this->code_modifiable_null = 1;
84  $this->code_auto = 1;
85  $this->prefixIsRequired = 0;
86  }
87 
88 
95  public function info($langs)
96  {
97  global $conf, $mc;
98  global $form;
99 
100  $langs->load("products");
101 
102  $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
103 
104  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
105  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
106  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
107  $texte .= '<input type="hidden" name="page_y" value="">';
108  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
109  $texte .= '<input type="hidden" name="param1" value="PRODUCT_ELEPHANT_MASK_PRODUCT">';
110  $texte .= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
111  $texte .= '<table class="nobordernopadding" width="100%">';
112 
113  $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("Product"), $langs->transnoentities("Product"));
114  $tooltip .= $langs->trans("GenericMaskCodes3");
115  $tooltip .= $langs->trans("GenericMaskCodes4c");
116  $tooltip .= $langs->trans("GenericMaskCodes5");
117 
118  // Parametrage du prefix customers
119  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
120  $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.(!empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT) ? $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
121 
122  $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button small reposition" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
123 
124  $texte .= '</tr>';
125 
126  // Parametrage du prefix suppliers
127  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
128  $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.(!empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE) ? $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
129  $texte .= '</tr>';
130 
131  $texte .= '</table>';
132  $texte .= '</form>';
133 
134  return $texte;
135  }
136 
137 
146  public function getExample($langs, $objproduct = 0, $type = -1)
147  {
148  $exampleproduct = $exampleservice = '';
149 
150  if ($type == 0 || $type == -1) {
151  $exampleproduct = $this->getNextValue($objproduct, 0);
152  if (!$exampleproduct) {
153  $exampleproduct = $langs->trans('NotConfigured');
154  }
155  if ($exampleproduct == "ErrorBadMask") {
156  $langs->load("errors");
157  $exampleproduct = $langs->trans($exampleproduct);
158  }
159  }
160  if ($type == 1 || $type == -1) {
161  $exampleservice = $this->getNextValue($objproduct, 1);
162  if (!$exampleservice) {
163  $exampleservice = $langs->trans('NotConfigured');
164  }
165  if ($exampleservice == "ErrorBadMask") {
166  $langs->load("errors");
167  $exampleservice = $langs->trans($exampleservice);
168  }
169  }
170 
171  if ($type == 0) {
172  return $exampleproduct;
173  }
174  if ($type == 1) {
175  return $exampleservice;
176  }
177  return $exampleproduct.'<br>'.$exampleservice;
178  }
179 
187  public function getNextValue($objproduct = 0, $type = -1)
188  {
189  global $db, $conf;
190 
191  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
192 
193  // Get Mask value
194  $mask = '';
195  if ($type == 0 && !empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT)) {
196  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
197  } elseif ($type == 1 && !empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE)) {
198  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
199  }
200 
201  if (empty($mask)) {
202  $this->error = 'NotConfigured';
203  return '';
204  }
205 
206  $field = '';
207  $where = '';
208  if ($type == 0) {
209  $field = 'ref';
210  //$where = ' AND client in (1,2)';
211  } elseif ($type == 1) {
212  $field = 'ref';
213  //$where = ' AND fournisseur = 1';
214  } else {
215  return -1;
216  }
217 
218  $now = dol_now();
219 
220  if (!empty($conf->global->PRODUCT_ELEPHANT_ADD_WHERE)) {
221  $where = ' AND ('.dol_string_nospecial(dol_string_unaccent($conf->global->PRODUCT_ELEPHANT_ADD_WHERE), '_', array(',', '@', '"', "|", ";", ":")).')';
222  }
223 
224  $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
225 
226  return $numFinal;
227  }
228 
229 
230  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
236  public function verif_prefixIsUsed()
237  {
238  // phpcs:enable
239  global $conf;
240 
241  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
242  if (preg_match('/\{pre\}/i', $mask)) {
243  return 1;
244  }
245 
246  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
247  if (preg_match('/\{pre\}/i', $mask)) {
248  return 1;
249  }
250 
251  return 0;
252  }
253 
254 
269  public function verif($db, &$code, $product, $type)
270  {
271  global $conf;
272 
273  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
274 
275  $result = 0;
276  $code = strtoupper(trim($code));
277 
278  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) {
279  $result = 0;
280  } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))) {
281  $result = -2;
282  } else {
283  // Get Mask value
284  $mask = '';
285  if ($type == 0) {
286  $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT) ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
287  }
288  if ($type == 1) {
289  $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE) ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
290  }
291  if (!$mask) {
292  $this->error = 'NotConfigured';
293  return -5;
294  }
295 
296  $result = check_value($mask, $code);
297  if (is_string($result)) {
298  $this->error = $result;
299  return -5;
300  }
301  }
302 
303  dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
304  return $result;
305  }
306 
307 
308  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
317  public function verif_dispo($db, $code, $product)
318  {
319  // phpcs:enable
320  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
321  $sql .= " WHERE ref = '".$db->escape($code)."'";
322  if ($product->id > 0) {
323  $sql .= " AND rowid <> ".$product->id;
324  }
325 
326  $resql = $db->query($sql);
327  if ($resql) {
328  if ($db->num_rows($resql) == 0) {
329  return 0;
330  } else {
331  return -1;
332  }
333  } else {
334  return -2;
335  }
336  }
337 }
mod_codeproduct_elephant\getExample
getExample($langs, $objproduct=0, $type=-1)
Return an example of result returned by getNextValue.
Definition: mod_codeproduct_elephant.php:146
ModeleProductCode
Class template for classes of numbering product.
Definition: modules_product.class.php:100
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
mod_codeproduct_elephant\info
info($langs)
Return description of module.
Definition: mod_codeproduct_elephant.php:95
mod_codeproduct_elephant\getNextValue
getNextValue($objproduct=0, $type=-1)
Return next value.
Definition: mod_codeproduct_elephant.php:187
mod_codeproduct_elephant\__construct
__construct()
Constructor.
Definition: mod_codeproduct_elephant.php:78
mod_codeproduct_elephant\verif
verif($db, &$code, $product, $type)
Check validity of code according to its rules.
Definition: mod_codeproduct_elephant.php:269
mod_codeproduct_elephant\verif_dispo
verif_dispo($db, $code, $product)
Renvoi si un code est pris ou non (par autre tiers)
Definition: mod_codeproduct_elephant.php:317
dol_string_unaccent
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
Definition: functions.lib.php:1326
mod_codeproduct_elephant\verif_prefixIsUsed
verif_prefixIsUsed()
Check if mask/numbering use prefix.
Definition: mod_codeproduct_elephant.php:236
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
get_next_value
get_next_value($db, $mask, $table, $field, $where='', $objsoc='', $date='', $mode='next', $bentityon=true, $objuser=null, $forceentity=null)
Return last or next value for a mask (according to area we should not reset)
Definition: functions2.lib.php:970
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
check_value
check_value($mask, $value)
Check value.
Definition: functions2.lib.php:1520
mod_codeproduct_elephant
Class to manage product code with elephant rule.
Definition: mod_codeproduct_elephant.php:35