25require_once DOL_DOCUMENT_ROOT.
'/core/modules/societe/modules_societe.class.php';
38 public $name =
'Minga';
49 public $version =
'dolibarr';
54 public $position = 40;
65 public $ranges = array();
75 const METHODS = array(
'code',
'next',
'random',
'fixed');
88 $this->ranges = array(
100 $this->method = in_array($method, self::METHODS) ? $method :
'code';
103 $this->nbdigits = max(0,
getDolGlobalInt(
'COMPANY_MINGA_CODE_LAST_DIGITS'));
115 $value = preg_replace(
'/\D/',
'', $value);
116 return ($value ===
'' ? $default : (int) $value);
130 $langs->loadLangs(array(
"admin",
"companies"));
132 $texte = $langs->trans(
"ModuleCompanyCode".$this->
name).
"<br>\n";
133 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
134 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
135 $texte .=
'<input type="hidden" name="page_y" value="">';
136 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
137 $texte .=
'<input type="hidden" name="param1" value="COMPANY_MINGA_CUSTOMER_START">';
138 $texte .=
'<input type="hidden" name="param2" value="COMPANY_MINGA_CUSTOMER_END">';
139 $texte .=
'<input type="hidden" name="param3" value="COMPANY_MINGA_SUPPLIER_START">';
140 $texte .=
'<input type="hidden" name="param4" value="COMPANY_MINGA_SUPPLIER_END">';
141 $texte .=
'<input type="hidden" name="param5" value="COMPANY_MINGA_METHOD">';
142 $texte .=
'<table class="nobordernopadding centpercent">';
143 $texte .=
'<tr><td>';
144 $s1 =
'<input type="text" class="flat width75" name="value1" value="'.$this->ranges[
'customer'][
'start'].
'">';
145 $s2 =
'<input type="text" class="flat width75" name="value2" value="'.$this->ranges[
'customer'][
'end'].
'">';
146 $s3 =
'<input type="text" class="flat width75" name="value3" value="'.$this->ranges[
'supplier'][
'start'].
'">';
147 $s4 =
'<input type="text" class="flat width75" name="value4" value="'.$this->ranges[
'supplier'][
'end'].
'">';
149 $texte .= $langs->trans(
"ModuleCompanyCodeCustomer".$this->
name,
'{s1}',
'{s2}').
"<br>\n";
150 $texte .= $langs->trans(
"ModuleCompanyCodeSupplier".$this->
name,
'{s3}',
'{s4}').
"<br>\n";
151 $texte = str_replace(array(
'{s1}',
'{s2}',
'{s3}',
'{s4}'), array($s1, $s2, $s3, $s4), $texte);
153 $arrayofmethods = array();
154 foreach (self::METHODS as $method) {
155 $arrayofmethods[$method] = $langs->trans(
"AccountancyCodeMingaMethod".ucfirst($method));
157 $texte .= $form->textwithpicto($langs->trans(
"AccountancyCodeMingaMethod"), $langs->trans(
"AccountancyCodeMingaMethodHelp"), 1,
'help',
'', 1).
' ';
158 $texte .= $form->selectarray(
'value5', $arrayofmethods, $this->method, 0, 0, 0,
'', 0, 0, 0,
'',
'minwidth300');
159 if ($this->nbdigits > 0) {
160 $texte .=
"<br>\n".$langs->trans(
'COMPANY_MINGA_CODE_LAST_DIGITS').
' = '.$this->nbdigits.
"\n";
163 $texte .=
'<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans(
"Modify").
'"></td>';
164 $texte .=
'</tr></table>';
178 public function getExample($langs =
null, $objsoc =
'', $type = -1)
181 foreach (array(
'customer' =>
'CU2607-0435',
'supplier' =>
'SU2607-0012') as $typetouse => $codeexample) {
182 $start = $this->ranges[$typetouse][
'start'];
183 $end = $this->ranges[$typetouse][
'end'];
184 if ($this->method ==
'code') {
185 $examples[] = $codeexample.
' → '.($start + (int) $this->
getNumberFromCode($codeexample));
186 } elseif ($this->method ==
'next') {
187 $examples[] = $start.
', '.($start + 1).
', ...';
188 } elseif ($this->method ==
'random') {
189 $examples[] = $start.
' ... '.$end;
191 $examples[] = (string) $start;
195 return implode(
"<br>\n", $examples);
208 public function get_code($db, $societe, $type =
'')
216 if ($type !=
'customer' && $type !=
'supplier') {
217 $this->error =
'Bad value for parameter type';
221 $start = $this->ranges[$type][
'start'];
222 $end = $this->ranges[$type][
'end'];
224 dol_syslog(__METHOD__.
" search code for type=".$type.
" method=".$this->method.
" range=".$start.
"-".$end.
" company=".(!empty($societe->name) ? $societe->name :
''));
227 if ($this->method ==
'fixed') {
228 $this->code = (string) $start;
229 dol_syslog(__METHOD__.
" found code=".$this->code);
234 if ($start <= $end) {
236 if (!is_array($used)) {
240 if ($this->method ==
'code') {
241 $number = $this->
getNumberFromCode(is_object($societe) ? (
string) ($type ==
'customer' ? $societe->code_client : $societe->code_fournisseur) :
'');
242 if ($number !==
'' && $start + (
int) $number <= $end && !isset($used[$start + (
int) $number])) {
243 $code = $start + (int) $number;
248 } elseif ($this->method ==
'random') {
249 for ($i = 0; $i < self::RANDOM_TRIES && $code < 0; $i++) {
250 $candidate = random_int($start, $end);
251 if (!isset($used[$candidate])) {
256 $code = $this->
findFreeNumber($used, $start, $end, random_int($start, $end), 1);
265 $langs->load(
"errors");
266 $this->error = $langs->trans(
"ErrorNoFreeAccountancyCodeInRange", $start, $end);
267 dol_syslog(__METHOD__.
" ".$this->error, LOG_WARNING);
271 $this->code = (string) $code;
272 dol_syslog(__METHOD__.
" found code=".$this->code);
286 if (preg_match(
'/(\d+)\D*$/', $code, $reg)) {
287 return (
string) ((int) ($this->nbdigits > 0 ? substr($reg[1], -$this->nbdigits) : $reg[1]));
304 $table =
'societe_perentity';
305 $fields = array(
'accountancy_code_customer',
'accountancy_code_supplier');
308 $fields = array(
'code_compta',
'code_compta_fournisseur');
312 foreach ($fields as $field) {
313 $sql =
"SELECT ".$db->sanitize($field).
" as code FROM ".MAIN_DB_PREFIX.$table;
314 $sql .=
" WHERE entity IN (".getEntity(
'societe').
")";
315 if (strlen((
string) $start) == strlen((
string) $end)) {
317 $sql .=
" AND ".$db->sanitize($field).
" >= '".$db->escape((
string) $start).
"'";
318 $sql .=
" AND ".$db->sanitize($field).
" <= '".$db->escape((
string) $end).
"'";
320 $sql .=
" AND ".$db->sanitize($field).
" IS NOT NULL";
323 $resql = $db->query($sql);
325 $this->error = $db->lasterror();
328 while ($obj = $db->fetch_object($resql)) {
329 $code = trim((
string) $obj->code);
330 if (ctype_digit($code) && (
int) $code >= $start && (
int) $code <= $end) {
331 $used[(int) $code] = 1;
355 $nbtries = min(count($used), $max - $min) + 1;
356 for ($i = 0; $i < $nbtries; $i++) {
357 if (!isset($used[$candidate])) {
361 if ($candidate > $max) {
363 } elseif ($candidate < $min) {
Parent class for third parties accountancy code generators.
Class to manage accountancy code of thirdparties with Minga rules.
const RANDOM_TRIES
Max number of random picks before scanning the range for a free number.
getUsedNumbers($db, $start, $end)
Load the numbers of the range already used as customer or supplier accountancy code.
getNumberFromCode($code)
Return the last group of digits of a thirdparty code ('CU2607-0435' gives '435'), limited to its last...
__construct()
Constructor.
getExample($langs=null, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
get_code($db, $societe, $type='')
Set accountancy account code for a third party into this->code.
cleanNumber($value, $default)
Return the number made of the digits of a string, or the default value if there is no digit.
info($langs)
Return description of module.
findFreeNumber($used, $min, $max, $from, $step)
Return the first free number of the range, starting at $from and moving in direction $step.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.