210 $langs->loadLangs(array(
'other',
'admin'));
212 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
215 $deltadateforclient = ((int) $_SESSION[
'dol_tz'] + (
int) $_SESSION[
'dol_dst']);
217 $deltadateforuser = round($deltadateforclient - $deltadateforserver);
221 print
'<table class="border tableforfield centpercent">';
225 if (!empty($object->import_key)) {
227 print
'<tr><td class="titlefield">';
229 print $langs->trans(
"ImportedWithSet");
235 print $object->import_key;
244 if (!empty($object->user_creation) || !empty($object->user_creation_id)) {
246 print
'<tr><td class="titlefield">';
248 print $langs->trans(
"CreatedBy");
254 if (is_object($object->user_creation)) {
255 if ($object->user_creation->id) {
256 print $object->user_creation->getNomUrl(-1,
'', 0, 0, 0);
258 print $langs->trans(
"Unknown");
261 $userstatic =
new User($db);
262 $userstatic->fetch($object->user_creation_id ? $object->user_creation_id : $object->user_creation);
263 if ($userstatic->id) {
264 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
266 print $langs->trans(
"Unknown");
277 if (!empty($object->date_creation)) {
279 print
'<tr><td class="titlefield">';
281 print $langs->trans(
"DateCreation");
287 print
dol_print_date($object->date_creation,
'dayhour',
'tzserver');
288 if ($deltadateforuser) {
289 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_creation,
"dayhour",
"tzuserrel").
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
299 if (!empty($object->user_modification) || !empty($object->user_modification_id)) {
301 print
'<tr><td class="titlefield">';
303 print $langs->trans(
"ModifiedBy");
309 if (is_object($object->user_modification)) {
310 if ($object->user_modification->id) {
311 print $object->user_modification->getNomUrl(-1,
'', 0, 0, 0);
313 print $langs->trans(
"Unknown");
316 $userstatic =
new User($db);
317 $userstatic->fetch($object->user_modification_id ? $object->user_modification_id : $object->user_modification);
318 if ($userstatic->id) {
319 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
321 print $langs->trans(
"Unknown");
332 if (!empty($object->date_modification)) {
334 print
'<tr><td class="titlefield">';
336 print $langs->trans(
"DateLastModification");
342 print
dol_print_date($object->date_modification,
'dayhour',
'tzserver');
343 if ($deltadateforuser) {
344 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_modification,
"dayhour",
"tzuserrel").
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
354 if (!empty($object->user_validation) || !empty($object->user_validation_id)) {
356 print
'<tr><td class="titlefield">';
358 print $langs->trans(
"ValidatedBy");
364 if (is_object($object->user_validation)) {
365 if ($object->user_validation->id) {
366 print $object->user_validation->getNomUrl(-1,
'', 0, 0, 0);
368 print $langs->trans(
"Unknown");
371 $userstatic =
new User($db);
372 $userstatic->fetch($object->user_validation_id ? $object->user_validation_id : $object->user_validation);
373 if ($userstatic->id) {
374 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
376 print $langs->trans(
"Unknown");
387 if (!empty($object->date_validation)) {
389 print
'<tr><td class="titlefield">';
391 print $langs->trans(
"DateValidation");
397 print
dol_print_date($object->date_validation,
'dayhour',
'tzserver');
398 if ($deltadateforuser) {
399 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_validation,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
409 if (!empty($object->user_approve) || !empty($object->user_approve_id)) {
411 print
'<tr><td class="titlefield">';
413 print $langs->trans(
"ApprovedBy");
419 if (!empty($object->user_approve) && is_object($object->user_approve)) {
420 if ($object->user_approve->id) {
421 print $object->user_approve->getNomUrl(-1,
'', 0, 0, 0);
423 print $langs->trans(
"Unknown");
426 $userstatic =
new User($db);
427 $userstatic->fetch($object->user_approve_id ? $object->user_approve_id : $object->user_approve);
428 if ($userstatic->id) {
429 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
431 print $langs->trans(
"Unknown");
442 if (!empty($object->date_approve) || !empty($object->date_approval)) {
444 print
'<tr><td class="titlefield">';
446 print $langs->trans(
"DateApprove");
452 print
dol_print_date($object->date_approve ? $object->date_approve : $object->date_approval,
'dayhour',
'tzserver');
453 if ($deltadateforuser) {
454 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_approve,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
464 if (!empty($object->user_approve_id2)) {
466 print
'<tr><td class="titlefield">';
468 print $langs->trans(
"ApprovedBy");
474 $userstatic =
new User($db);
475 $userstatic->fetch($object->user_approve_id2);
476 if ($userstatic->id) {
477 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
479 print $langs->trans(
"Unknown");
489 if (!empty($object->date_approve2)) {
491 print
'<tr><td class="titlefield">';
493 print $langs->trans(
"DateApprove2");
499 print
dol_print_date($object->date_approve2,
'dayhour',
'tzserver');
500 if ($deltadateforuser) {
501 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_approve2,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
511 if (!empty($object->user_signature) || !empty($object->user_signature_id)) {
513 print
'<tr><td class="titlefield">';
515 print $langs->trans(
'SignedBy');
521 if (is_object($object->user_signature)) {
522 if ($object->user_signature->id) {
523 print $object->user_signature->getNomUrl(-1,
'', 0, 0, 0);
525 print $langs->trans(
'Unknown');
528 $userstatic =
new User($db);
529 $userstatic->fetch($object->user_signature_id ? $object->user_signature_id : $object->user_signature);
530 if ($userstatic->id) {
531 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
533 print $langs->trans(
'Unknown');
544 if (!empty($object->date_signature)) {
546 print
'<tr><td class="titlefield">';
548 print $langs->trans(
'DateSigning');
555 if ($deltadateforuser) {
556 print
' <span class="opacitymedium">'.$langs->trans(
'CurrentHour').
'</span> / '.
dol_print_date($object->date_signature,
'dayhour',
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
'ClientHour').
'</span>';
566 if (!empty($object->user_cloture) || !empty($object->user_closing) || !empty($object->user_closing_id)) {
567 if (isset($object->user_cloture) && !empty($object->user_cloture)) {
568 $object->user_closing = $object->user_cloture;
571 print
'<tr><td class="titlefield">';
573 print $langs->trans(
"ClosedBy");
579 if (is_object($object->user_closing)) {
580 if ($object->user_closing->id) {
581 print $object->user_closing->getNomUrl(-1,
'', 0, 0, 0);
583 print $langs->trans(
"Unknown");
586 $userstatic =
new User($db);
587 $userstatic->fetch($object->user_closing_id ? $object->user_closing_id : $object->user_closing);
588 if ($userstatic->id) {
589 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
591 print $langs->trans(
"Unknown");
602 if (!empty($object->date_cloture) || !empty($object->date_closing)) {
603 if (isset($object->date_cloture) && !empty($object->date_cloture)) {
604 $object->date_closing = $object->date_cloture;
607 print
'<tr><td class="titlefield">';
609 print $langs->trans(
"DateClosing");
615 print
dol_print_date($object->date_closing,
'dayhour',
'tzserver');
616 if ($deltadateforuser) {
617 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_closing,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
627 if (!empty($object->user_rappro) || !empty($object->user_rappro_id)) {
629 print
'<tr><td class="titlefield">';
631 print $langs->trans(
"ReconciledBy");
637 if (is_object($object->user_rappro)) {
638 if ($object->user_rappro->id) {
639 print $object->user_rappro->getNomUrl(-1,
'', 0, 0, 0);
641 print $langs->trans(
"Unknown");
644 $userstatic =
new User($db);
645 $userstatic->fetch($object->user_rappro_id ? $object->user_rappro_id : $object->user_rappro);
646 if ($userstatic->id) {
647 print $userstatic->getNomUrl(1,
'', 0, 0, 0);
649 print $langs->trans(
"Unknown");
660 if (!empty($object->date_rappro)) {
662 print
'<tr><td class="titlefield">';
664 print $langs->trans(
"DateConciliating");
670 print
dol_print_date($object->date_rappro,
'dayhour',
'tzserver');
671 if ($deltadateforuser) {
672 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_rappro,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
682 if (!empty($object->date_envoi)) {
684 print
'<tr><td class="titlefield">';
686 print $langs->trans(
"DateLastSend");
693 if ($deltadateforuser) {
694 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_envoi,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
970function get_next_value($db, $mask, $table, $field, $where =
'', $objsoc =
'', $date =
'', $mode =
'next', $bentityon =
true, $objuser =
null, $forceentity =
null)
974 if (!is_object($objsoc)) {
975 $valueforccc = $objsoc;
976 } elseif ($table ==
"commande_fournisseur" || $table ==
"facture_fourn" || $table ==
"paiementfourn") {
982 $sharetable = $table;
983 if ($table ==
'facture' || $table ==
'invoice') {
984 $sharetable =
'invoicenumber';
1000 $hasglobalcounter =
false;
1003 if (preg_match(
'/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i', $mask, $reg)) {
1004 $masktri = $reg[1].(!empty($reg[2]) ? $reg[2] :
'').(!empty($reg[3]) ? $reg[3] :
'');
1005 $maskcounter = $reg[1];
1006 $hasglobalcounter =
true;
1010 $maskcounter =
'00000';
1015 $resetEveryMonth =
false;
1016 if (
dol_strlen($maskcounter) < 3 && empty($conf->global->MAIN_COUNTER_WITH_LESS_3_DIGITS)) {
1017 return 'ErrorCounterMustHaveMoreThan3Digits';
1021 $regClientRef = array();
1022 if (preg_match(
'/\{(c+)(0*)\}/i', $mask, $regClientRef)) {
1023 $maskrefclient = $regClientRef[1].$regClientRef[2];
1024 $maskrefclient_maskclientcode = $regClientRef[1];
1025 $maskrefclient_maskcounter = $regClientRef[2];
1026 $maskrefclient_maskoffset = 0;
1027 $maskrefclient_clientcode = substr($valueforccc, 0,
dol_strlen($maskrefclient_maskclientcode));
1028 $maskrefclient_clientcode = str_pad($maskrefclient_clientcode,
dol_strlen($maskrefclient_maskclientcode),
"#", STR_PAD_RIGHT);
1030 if (
dol_strlen($maskrefclient_maskcounter) > 0 &&
dol_strlen($maskrefclient_maskcounter) < 3) {
1031 return 'ErrorCounterMustHaveMoreThan3Digits';
1034 $maskrefclient =
'';
1038 if (!$hasglobalcounter && ($maskrefclient_maskcounter ==
'')) {
1039 return 'ErrorBadMask';
1044 if (preg_match(
'/\{(t+)\}/i', $mask, $regType)) {
1045 $masktype = $regType[1];
1046 $masktype_value =
dol_substr(preg_replace(
'/^TE_/',
'', $objsoc->typent_code), 0,
dol_strlen($regType[1]));
1047 $masktype_value = str_pad($masktype_value,
dol_strlen($regType[1]),
"#", STR_PAD_RIGHT);
1050 $masktype_value =
'';
1055 if (preg_match(
'/\{(u+)\}/i', $mask, $regType)) {
1056 $lastname =
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
1057 if (is_object($objuser)) {
1058 $lastname = $objuser->lastname;
1061 $maskuser = $regType[1];
1062 $maskuser_value = substr($lastname, 0,
dol_strlen($regType[1]));
1063 $maskuser_value = str_pad($maskuser_value,
dol_strlen($regType[1]),
"#", STR_PAD_RIGHT);
1066 $maskuser_value =
'';
1070 $maskperso = array();
1071 $maskpersonew = array();
1074 while (preg_match(
'/\{([A-Z]+)\-([1-9])\}/', $tmpmask, $regKey)) {
1075 $maskperso[$regKey[1]] =
'{'.$regKey[1].
'-'.$regKey[2].
'}';
1076 $maskpersonew[$regKey[1]] = str_pad(
'', $regKey[2],
'_', STR_PAD_RIGHT);
1077 $tmpmask = preg_replace(
'/\{'.$regKey[1].
'\-'.$regKey[2].
'\}/i', $maskpersonew[$regKey[1]], $tmpmask);
1080 if (strstr($mask,
'user_extra_')) {
1081 $start =
"{user_extra_";
1084 if (!empty($user->array_options[
'options_'.$extra])) {
1085 $mask = preg_replace(
'#('.$start.
')(.*?)('.$end.
')#si', $user->array_options[
'options_'.$extra], $mask);
1088 $maskwithonlyymcode = $mask;
1089 $maskwithonlyymcode = preg_replace(
'/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i', $maskcounter, $maskwithonlyymcode);
1090 $maskwithonlyymcode = preg_replace(
'/\{dd\}/i',
'dd', $maskwithonlyymcode);
1091 $maskwithonlyymcode = preg_replace(
'/\{(c+)(0*)\}/i', $maskrefclient, $maskwithonlyymcode);
1092 $maskwithonlyymcode = preg_replace(
'/\{(t+)\}/i', $masktype_value, $maskwithonlyymcode);
1093 $maskwithonlyymcode = preg_replace(
'/\{(u+)\}/i', $maskuser_value, $maskwithonlyymcode);
1094 foreach ($maskperso as $key => $val) {
1095 $maskwithonlyymcode = preg_replace(
'/'.preg_quote($val,
'/').
'/i', $maskpersonew[$key], $maskwithonlyymcode);
1097 $maskwithnocode = $maskwithonlyymcode;
1098 $maskwithnocode = preg_replace(
'/\{yyyy\}/i',
'yyyy', $maskwithnocode);
1099 $maskwithnocode = preg_replace(
'/\{yy\}/i',
'yy', $maskwithnocode);
1100 $maskwithnocode = preg_replace(
'/\{y\}/i',
'y', $maskwithnocode);
1101 $maskwithnocode = preg_replace(
'/\{mm\}/i',
'mm', $maskwithnocode);
1108 if (!empty($reg[2]) && preg_match(
'/^\+/', $reg[2])) {
1109 $maskoffset = preg_replace(
'/^\+/',
'', $reg[2]);
1111 if (!empty($reg[3]) && preg_match(
'/^\+/', $reg[3])) {
1112 $maskoffset = preg_replace(
'/^\+/',
'', $reg[3]);
1118 $yearoffsettype =
false;
1121 if (!empty($reg[2]) && preg_match(
'/^@/', $reg[2])) {
1122 $yearoffsettype = preg_replace(
'/^@/',
'', $reg[2]);
1124 if (!empty($reg[3]) && preg_match(
'/^@/', $reg[3])) {
1125 $yearoffsettype = preg_replace(
'/^@/',
'', $reg[3]);
1129 if (is_numeric($yearoffsettype) && $yearoffsettype >= 1) {
1130 $maskraz = $yearoffsettype;
1131 } elseif ($yearoffsettype ===
'0' || (!empty($yearoffsettype) && !is_numeric($yearoffsettype) && $conf->global->SOCIETE_FISCAL_MONTH_START > 1)) {
1132 $maskraz = $conf->global->SOCIETE_FISCAL_MONTH_START;
1137 if ($maskraz == 99) {
1138 $maskraz = date(
'm', $date);
1139 $resetEveryMonth =
true;
1141 if ($maskraz > 12) {
1142 return 'ErrorBadMaskBadRazMonth';
1147 if (preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1150 } elseif (preg_match(
'/^(.*)\{(m+)\}\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1154 return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
1158 return 'ErrorCantUseRazWithYearOnOneDigit';
1162 if (preg_match(
'/^(.*)\{(m+)\}\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1165 } elseif (preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1168 } elseif (preg_match(
'/^(.*)\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1172 return 'ErrorCantUseRazIfNoYearInMask';
1176 $yearlen = $posy ?
dol_strlen($reg[$posy]) : 0;
1177 $monthlen = $posm ?
dol_strlen($reg[$posm]) : 0;
1180 $monthpos = ($yearpos + $yearlen);
1181 if ($posy == 3 && $posm == 2) {
1183 $yearpos = ($monthpos + $monthlen);
1188 $monthcomp = $maskraz;
1191 if (!empty($yearoffsettype) && !is_numeric($yearoffsettype) && $yearoffsettype !=
'=') {
1192 $currentyear = date(
"Y", $date);
1193 $fiscaldate =
dol_mktime(
'0',
'0',
'0', $maskraz,
'1', $currentyear);
1194 $newyeardate =
dol_mktime(
'0',
'0',
'0',
'1',
'1', $currentyear);
1195 $nextnewyeardate =
dol_mktime(
'0',
'0',
'0',
'1',
'1', $currentyear + 1);
1199 if ($date >= $fiscaldate) {
1201 if ($date < $nextnewyeardate && $yearoffsettype ==
'+') {
1204 } elseif ($date >= $newyeardate && $yearoffsettype ==
'-') {
1208 } elseif (date(
"m", $date) < $maskraz && empty($resetEveryMonth)) {
1213 if ($yearlen == 4) {
1214 $yearcomp = sprintf(
"%04d", date(
"Y", $date) + $yearoffset);
1215 } elseif ($yearlen == 2) {
1216 $yearcomp = sprintf(
"%02d", date(
"y", $date) + $yearoffset);
1217 } elseif ($yearlen == 1) {
1218 $yearcomp = substr(date(
'y', $date), 1, 1) + $yearoffset;
1220 if ($monthcomp > 1 && empty($resetEveryMonth)) {
1221 if ($yearlen == 4) {
1222 $yearcomp1 = sprintf(
"%04d", date(
"Y", $date) + $yearoffset + 1);
1223 } elseif ($yearlen == 2) {
1224 $yearcomp1 = sprintf(
"%02d", date(
"y", $date) + $yearoffset + 1);
1228 $sqlwhere .=
" (SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"'";
1229 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") >= '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"')";
1231 $sqlwhere .=
" (SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp1).
"'";
1232 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") < '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"') ";
1234 } elseif ($resetEveryMonth) {
1235 $sqlwhere .=
"(SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"'";
1236 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") = '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"')";
1238 $sqlwhere .=
"(SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"')";
1245 if (function_exists(
'mb_strrpos')) {
1246 $posnumstart = mb_strrpos($maskwithnocode, $maskcounter, 0,
'UTF-8');
1248 $posnumstart = strrpos($maskwithnocode, $maskcounter);
1250 if ($posnumstart < 0) {
1251 return 'ErrorBadMaskFailedToLocatePosOfSequence';
1253 $sqlstring =
"SUBSTRING(".$field.
", ".($posnumstart + 1).
", ".
dol_strlen($maskcounter).
")";
1257 $maskLike = str_replace(
"%",
"_", $maskLike);
1260 $maskLike = preg_replace(
'/\{yyyy\}/i',
'____', $maskLike);
1261 $maskLike = preg_replace(
'/\{yy\}/i',
'__', $maskLike);
1262 $maskLike = preg_replace(
'/\{y\}/i',
'_', $maskLike);
1263 $maskLike = preg_replace(
'/\{mm\}/i',
'__', $maskLike);
1264 $maskLike = preg_replace(
'/\{dd\}/i',
'__', $maskLike);
1266 if ($maskrefclient) {
1275 foreach ($maskperso as $key => $val) {
1276 $maskLike = str_replace(
dol_string_nospecial($maskperso[$key]), $maskpersonew[$key], $maskLike);
1281 $sql =
"SELECT MAX(".$sqlstring.
") as val";
1282 $sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1283 $sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskLike) . (!empty($conf->global->SEARCH_FOR_NEXT_VAL_ON_START_ONLY) ?
"%" :
"") .
"'";
1284 $sql .=
" AND ".$field.
" NOT LIKE '(PROV%)'";
1288 $sql .=
" AND ". $db->regexpsql($sqlstring,
'^[0-9]+$',
true);
1292 $sql .=
" AND entity IN (".getEntity($sharetable).
")";
1293 } elseif (!empty($forceentity)) {
1294 $sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1300 $sql .=
" AND ".$sqlwhere;
1304 dol_syslog(
"functions2::get_next_value mode=".$mode, LOG_DEBUG);
1305 $resql = $db->query($sql);
1307 $obj = $db->fetch_object($resql);
1308 $counter = $obj->val;
1314 if (empty($counter)) {
1315 $counter = $maskoffset;
1316 } elseif (preg_match(
'/[^0-9]/i', $counter)) {
1317 dol_syslog(
"Error, the last counter found is '".$counter.
"' so is not a numeric value. We will restart to 1.", LOG_ERR);
1319 } elseif ($counter < $maskoffset && empty($conf->global->MAIN_NUMBERING_OFFSET_ONLY_FOR_FIRST)) {
1320 $counter = $maskoffset;
1323 if ($mode ==
'last') {
1324 $counterpadded = str_pad($counter,
dol_strlen($maskcounter),
"0", STR_PAD_LEFT);
1328 $maskLike = str_replace(
"%",
"_", $maskLike);
1330 $maskLike = preg_replace(
'/\{yyyy\}/i',
'____', $maskLike);
1331 $maskLike = preg_replace(
'/\{yy\}/i',
'__', $maskLike);
1332 $maskLike = preg_replace(
'/\{y\}/i',
'_', $maskLike);
1333 $maskLike = preg_replace(
'/\{mm\}/i',
'__', $maskLike);
1334 $maskLike = preg_replace(
'/\{dd\}/i',
'__', $maskLike);
1336 if ($maskrefclient) {
1347 $sql =
"SELECT ".$field.
" as ref";
1348 $sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1349 $sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskLike) . (!empty($conf->global->SEARCH_FOR_NEXT_VAL_ON_START_ONLY) ?
"%" :
"") .
"'";
1350 $sql .=
" AND ".$field.
" NOT LIKE '%PROV%'";
1352 $sql .=
" AND entity IN (".getEntity($sharetable).
")";
1353 } elseif (!empty($forceentity)) {
1354 $sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1360 $sql .=
" AND ".$sqlwhere;
1363 dol_syslog(
"functions2::get_next_value mode=".$mode, LOG_DEBUG);
1364 $resql = $db->query($sql);
1366 $obj = $db->fetch_object($resql);
1375 } elseif ($mode ==
'next') {
1377 $maskrefclient_counter = 0;
1380 if ($counter >= pow(10,
dol_strlen($maskcounter))) {
1381 $counter =
'ErrorMaxNumberReachForThisMask';
1384 if (!empty($maskrefclient_maskcounter)) {
1388 $maskrefclient_posnumstart = strpos($maskwithnocode, $maskrefclient_maskcounter, strpos($maskwithnocode, $maskrefclient));
1389 if ($maskrefclient_posnumstart <= 0) {
1390 return 'ErrorBadMask';
1392 $maskrefclient_sqlstring =
'SUBSTRING('.$field.
', '.($maskrefclient_posnumstart + 1).
', '.
dol_strlen($maskrefclient_maskcounter).
')';
1397 $maskrefclient_maskLike = str_replace(
"%",
"_", $maskrefclient_maskLike);
1399 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{yyyy}'),
'____', $maskrefclient_maskLike);
1400 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{yy}'),
'__', $maskrefclient_maskLike);
1401 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{y}'),
'_', $maskrefclient_maskLike);
1402 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{mm}'),
'__', $maskrefclient_maskLike);
1403 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{dd}'),
'__', $maskrefclient_maskLike);
1404 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{'.$masktri.
'}'), str_pad(
"",
dol_strlen($maskcounter),
"_"), $maskrefclient_maskLike);
1405 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{'.$maskrefclient.
'}'), $maskrefclient_clientcode.str_pad(
"",
dol_strlen($maskrefclient_maskcounter),
"_"), $maskrefclient_maskLike);
1408 $maskrefclient_sql =
"SELECT MAX(".$maskrefclient_sqlstring.
") as val";
1409 $maskrefclient_sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1411 $maskrefclient_sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskrefclient_maskLike) . (!empty($conf->global->SEARCH_FOR_NEXT_VAL_ON_START_ONLY) ?
"%" :
"") .
"'";
1413 $maskrefclient_sql .=
" AND entity IN (".getEntity($sharetable).
")";
1414 } elseif (!empty($forceentity)) {
1415 $sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1418 $maskrefclient_sql .= $where;
1421 $maskrefclient_sql .=
' AND '.$sqlwhere;
1423 $maskrefclient_sql .=
" AND (SUBSTRING(".$field.
", ".(strpos($maskwithnocode, $maskrefclient) + 1).
", ".
dol_strlen($maskrefclient_maskclientcode).
") = '".$db->escape($maskrefclient_clientcode).
"')";
1425 dol_syslog(
"functions2::get_next_value maskrefclient", LOG_DEBUG);
1426 $maskrefclient_resql = $db->query($maskrefclient_sql);
1427 if ($maskrefclient_resql) {
1428 $maskrefclient_obj = $db->fetch_object($maskrefclient_resql);
1429 $maskrefclient_counter = $maskrefclient_obj->val;
1434 if (empty($maskrefclient_counter) || preg_match(
'/[^0-9]/i', $maskrefclient_counter)) {
1435 $maskrefclient_counter = $maskrefclient_maskoffset;
1437 $maskrefclient_counter++;
1444 if (!empty($yearoffsettype) && !is_numeric($yearoffsettype) && $yearoffsettype !=
'=') {
1445 $numFinal = preg_replace(
'/\{yyyy\}/i', date(
"Y", $date) + $yearoffset, $numFinal);
1446 $numFinal = preg_replace(
'/\{yy\}/i', date(
"y", $date) + $yearoffset, $numFinal);
1447 $numFinal = preg_replace(
'/\{y\}/i', substr(date(
"y", $date), 1, 1) + $yearoffset, $numFinal);
1450 $numFinal = preg_replace(
'/\{yyyy\}/i', date(
"Y", $date), $numFinal);
1451 $numFinal = preg_replace(
'/\{yy\}/i', date(
"y", $date), $numFinal);
1452 $numFinal = preg_replace(
'/\{y\}/i', substr(date(
"y", $date), 1, 1), $numFinal);
1454 $numFinal = preg_replace(
'/\{mm\}/i', date(
"m", $date), $numFinal);
1455 $numFinal = preg_replace(
'/\{dd\}/i', date(
"d", $date), $numFinal);
1458 $maskbefore =
'{'.$masktri.
'}';
1459 $maskafter = str_pad($counter,
dol_strlen($maskcounter),
"0", STR_PAD_LEFT);
1461 $numFinal = str_replace($maskbefore, $maskafter, $numFinal);
1464 if ($maskrefclient) {
1466 $maskrefclient_maskbefore =
'{'.$maskrefclient.
'}';
1467 $maskrefclient_maskafter = $maskrefclient_clientcode;
1468 if (
dol_strlen($maskrefclient_maskcounter) > 0) {
1469 $maskrefclient_maskafter .= str_pad($maskrefclient_counter,
dol_strlen($maskrefclient_maskcounter),
"0", STR_PAD_LEFT);
1471 $numFinal = str_replace($maskrefclient_maskbefore, $maskrefclient_maskafter, $numFinal);
1476 $masktype_maskbefore =
'{'.$masktype.
'}';
1477 $masktype_maskafter = $masktype_value;
1478 $numFinal = str_replace($masktype_maskbefore, $masktype_maskafter, $numFinal);
1483 $maskuser_maskbefore =
'{'.$maskuser.
'}';
1484 $maskuser_maskafter = $maskuser_value;
1485 $numFinal = str_replace($maskuser_maskbefore, $maskuser_maskafter, $numFinal);
1489 dol_syslog(
"functions2::get_next_value return ".$numFinal, LOG_DEBUG);
1524 $hasglobalcounter =
false;
1527 if (preg_match(
'/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
1528 $masktri = $reg[1].(isset($reg[2]) ? $reg[2] :
'').(isset($reg[3]) ? $reg[3] :
'');
1529 $maskcounter = $reg[1];
1530 $hasglobalcounter =
true;
1534 $maskcounter =
'00000';
1539 return 'ErrorCounterMustHaveMoreThan3Digits';
1543 $regClientRef = array();
1544 if (preg_match(
'/\{(c+)(0*)\}/i', $mask, $regClientRef)) {
1545 $maskrefclient = $regClientRef[1].$regClientRef[2];
1546 $maskrefclient_maskclientcode = $regClientRef[1];
1547 $maskrefclient_maskcounter = $regClientRef[2];
1548 $maskrefclient_maskoffset = 0;
1549 $maskrefclient_clientcode = substr(
'', 0,
dol_strlen($maskrefclient_maskclientcode));
1550 $maskrefclient_clientcode = str_pad($maskrefclient_clientcode,
dol_strlen($maskrefclient_maskclientcode),
"#", STR_PAD_RIGHT);
1552 if (
dol_strlen($maskrefclient_maskcounter) > 0 &&
dol_strlen($maskrefclient_maskcounter) < 3) {
1553 return 'ErrorCounterMustHaveMoreThan3Digits';
1556 $maskrefclient =
'';
1560 if (!$hasglobalcounter && ($maskrefclient_maskcounter ==
'')) {
1561 return 'ErrorBadMask';
1564 $maskwithonlyymcode = $mask;
1565 $maskwithonlyymcode = preg_replace(
'/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $maskcounter, $maskwithonlyymcode);
1566 $maskwithonlyymcode = preg_replace(
'/\{dd\}/i',
'dd', $maskwithonlyymcode);
1567 $maskwithonlyymcode = preg_replace(
'/\{(c+)(0*)\}/i', $maskrefclient, $maskwithonlyymcode);
1568 $maskwithnocode = $maskwithonlyymcode;
1569 $maskwithnocode = preg_replace(
'/\{yyyy\}/i',
'yyyy', $maskwithnocode);
1570 $maskwithnocode = preg_replace(
'/\{yy\}/i',
'yy', $maskwithnocode);
1571 $maskwithnocode = preg_replace(
'/\{y\}/i',
'y', $maskwithnocode);
1572 $maskwithnocode = preg_replace(
'/\{mm\}/i',
'mm', $maskwithnocode);
1578 if (!empty($reg[2]) && preg_match(
'/^\+/', $reg[2])) {
1579 $maskoffset = preg_replace(
'/^\+/',
'', $reg[2]);
1581 if (!empty($reg[3]) && preg_match(
'/^\+/', $reg[3])) {
1582 $maskoffset = preg_replace(
'/^\+/',
'', $reg[3]);
1588 if (!empty($reg[2]) && preg_match(
'/^@/', $reg[2])) {
1589 $maskraz = preg_replace(
'/^@/',
'', $reg[2]);
1591 if (!empty($reg[3]) && preg_match(
'/^@/', $reg[3])) {
1592 $maskraz = preg_replace(
'/^@/',
'', $reg[3]);
1594 if ($maskraz >= 0) {
1595 if ($maskraz == 99) {
1596 $maskraz = date(
'm');
1597 $resetEveryMonth =
true;
1599 if ($maskraz > 12) {
1600 return 'ErrorBadMaskBadRazMonth';
1604 if ($maskraz > 1 && !preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1605 return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
1607 if ($maskraz <= 1 && !preg_match(
'/^(.*)\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1608 return 'ErrorCantUseRazIfNoYearInMask';
1614 if (function_exists(
'mb_strrpos')) {
1615 $posnumstart = mb_strrpos($maskwithnocode, $maskcounter, 0,
'UTF-8');
1617 $posnumstart = strrpos($maskwithnocode, $maskcounter);
1619 if ($posnumstart < 0) {
1620 return 'ErrorBadMaskFailedToLocatePosOfSequence';
1632 dol_syslog(
"functions2::check_value result=".$result, LOG_DEBUG);
1686 $stime = strftime(
'%Y-%m-%d', $time);
1688 if (preg_match(
'/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/i', $stime, $reg)) {
1703 if (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) == 0) {
1704 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) - 3 * 24 * 60 * 60;
1705 } elseif (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) < 4) {
1706 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) + (4 - date(
"w", mktime(12, 0, 0, $mois, $jour, $annee))) * 24 * 60 * 60;
1707 } elseif (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) > 4) {
1708 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) - (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) - 4) * 24 * 60 * 60;
1710 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee);
1714 if (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) == 0) {
1715 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine)) + 4 * 24 * 60 * 60;
1716 } elseif (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) < 4) {
1717 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine)) + (4 - date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine)))) * 24 * 60 * 60;
1718 } elseif (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) > 4) {
1719 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine)) + (7 - (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) - 4)) * 24 * 60 * 60;
1722 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine));
1728 date(
"z", mktime(12, 0, 0, date(
"m", $jeudiSemaine), date(
"d", $jeudiSemaine), date(
"Y", $jeudiSemaine)))
1730 date(
"z", mktime(12, 0, 0, date(
"m", $premierJeudiAnnee), date(
"d", $premierJeudiAnnee), date(
"Y", $premierJeudiAnnee)))
1735 if ($numeroSemaine == 53) {
1737 if (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) == 4 || (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) == 3 && date(
"z", mktime(12, 0, 0, 12, 31, date(
"Y", $jeudiSemaine))) == 365)) {
1738 $numeroSemaine = 53;
1746 return sprintf(
"%02d", $numeroSemaine);