151 $langs->loadLangs(array(
'other',
'admin'));
153 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
156 $deltadateforclient = ((int) $_SESSION[
'dol_tz'] + (
int) $_SESSION[
'dol_dst']);
158 $deltadateforuser = round($deltadateforclient - $deltadateforserver);
162 print
'<table class="border tableforfield centpercent">';
166 if (!empty($object->import_key)) {
168 print
'<tr><td class="titlefield">';
170 print $langs->trans(
"ImportedWithSet");
176 print $object->import_key;
185 if (!empty($object->user_creation) || !empty($object->user_creation_id)) {
187 print
'<tr><td class="titlefield">';
189 print $langs->trans(
"CreatedBy");
195 if (! empty($object->user_creation) && is_object($object->user_creation)) {
196 if ($object->user_creation->id) {
197 print $object->user_creation->getNomUrl(-1,
'', 0, 0, 0);
199 print $langs->trans(
"Unknown");
202 $userstatic =
new User($db);
203 $userstatic->fetch($object->user_creation_id);
204 if ($userstatic->id) {
205 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
207 print $langs->trans(
"Unknown");
218 if (!empty($object->date_creation)) {
220 print
'<tr><td class="titlefield">';
222 print $langs->trans(
"DateCreation");
228 print
dol_print_date($object->date_creation,
'dayhour',
'tzserver');
229 if ($deltadateforuser) {
230 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_creation,
"dayhour",
"tzuserrel").
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
240 if (!empty($object->user_modification) || !empty($object->user_modification_id)) {
242 print
'<tr><td class="titlefield">';
244 print $langs->trans(
"ModifiedBy");
250 if (is_object($object->user_modification)) {
251 if ($object->user_modification->id) {
252 print $object->user_modification->getNomUrl(-1,
'', 0, 0, 0);
254 print $langs->trans(
"Unknown");
257 $userstatic =
new User($db);
258 $userstatic->fetch($object->user_modification_id);
259 if ($userstatic->id) {
260 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
262 print $langs->trans(
"Unknown");
273 if (!empty($object->date_modification)) {
275 print
'<tr><td class="titlefield">';
277 print $langs->trans(
"DateLastModification");
283 print
dol_print_date($object->date_modification,
'dayhour',
'tzserver');
284 if ($deltadateforuser) {
285 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_modification,
"dayhour",
"tzuserrel").
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
295 if (!empty($object->user_validation) || !empty($object->user_validation_id)) {
297 print
'<tr><td class="titlefield">';
299 print $langs->trans(
"ValidatedBy");
305 if (is_object($object->user_validation)) {
306 if ($object->user_validation->id) {
307 print $object->user_validation->getNomUrl(-1,
'', 0, 0, 0);
309 print $langs->trans(
"Unknown");
312 $userstatic =
new User($db);
313 $userstatic->fetch($object->user_validation_id ? $object->user_validation_id : $object->user_validation);
314 if ($userstatic->id) {
315 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
317 print $langs->trans(
"Unknown");
328 if (!empty($object->date_validation)) {
330 print
'<tr><td class="titlefield">';
332 print $langs->trans(
"DateValidation");
338 print
dol_print_date($object->date_validation,
'dayhour',
'tzserver');
339 if ($deltadateforuser) {
340 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_validation,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
350 if (!empty($object->user_approve) || !empty($object->user_approve_id)) {
352 print
'<tr><td class="titlefield">';
354 print $langs->trans(
"ApprovedBy");
360 if (!empty($object->user_approve) && is_object($object->user_approve)) {
361 if ($object->user_approve->id) {
362 print $object->user_approve->getNomUrl(-1,
'', 0, 0, 0);
364 print $langs->trans(
"Unknown");
367 $userstatic =
new User($db);
368 $userstatic->fetch($object->user_approve_id ? $object->user_approve_id : $object->user_approve);
369 if ($userstatic->id) {
370 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
372 print $langs->trans(
"Unknown");
383 if (!empty($object->date_approve) || !empty($object->date_approval)) {
385 print
'<tr><td class="titlefield">';
387 print $langs->trans(
"DateApprove");
393 print
dol_print_date($object->date_approve ? $object->date_approve : $object->date_approval,
'dayhour',
'tzserver');
394 if ($deltadateforuser) {
395 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_approve,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
405 if (!empty($object->user_approve_id2)) {
407 print
'<tr><td class="titlefield">';
409 print $langs->trans(
"ApprovedBy");
415 $userstatic =
new User($db);
416 $userstatic->fetch($object->user_approve_id2);
417 if ($userstatic->id) {
418 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
420 print $langs->trans(
"Unknown");
430 if (!empty($object->date_approve2)) {
432 print
'<tr><td class="titlefield">';
434 print $langs->trans(
"DateApprove2");
440 print
dol_print_date($object->date_approve2,
'dayhour',
'tzserver');
441 if ($deltadateforuser) {
442 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_approve2,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
452 if (!empty($object->user_signature) || !empty($object->user_signature_id)) {
454 print
'<tr><td class="titlefield">';
456 print $langs->trans(
'SignedBy');
462 if (is_object($object->user_signature)) {
463 if ($object->user_signature->id) {
464 print $object->user_signature->getNomUrl(-1,
'', 0, 0, 0);
466 print $langs->trans(
'Unknown');
469 $userstatic =
new User($db);
470 $userstatic->fetch($object->user_signature_id ? $object->user_signature_id : $object->user_signature);
471 if ($userstatic->id) {
472 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
474 print $langs->trans(
'Unknown');
485 if (!empty($object->date_signature)) {
487 print
'<tr><td class="titlefield">';
489 print $langs->trans(
'DateSigning');
496 if ($deltadateforuser) {
497 print
' <span class="opacitymedium">'.$langs->trans(
'CurrentHour').
'</span> / '.
dol_print_date($object->date_signature,
'dayhour',
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
'ClientHour').
'</span>';
507 if (!empty($object->user_closing_id)) {
509 print
'<tr><td class="titlefield">';
511 print $langs->trans(
"ClosedBy");
517 $userstatic =
new User($db);
518 $userstatic->fetch($object->user_closing_id);
519 if ($userstatic->id) {
520 print $userstatic->getNomUrl(-1,
'', 0, 0, 0);
522 print $langs->trans(
"Unknown");
532 if (!empty($object->date_cloture) || !empty($object->date_closing)) {
533 if (isset($object->date_cloture) && !empty($object->date_cloture)) {
534 $object->date_closing = $object->date_cloture;
537 print
'<tr><td class="titlefield">';
539 print $langs->trans(
"DateClosing");
545 print
dol_print_date($object->date_closing,
'dayhour',
'tzserver');
546 if ($deltadateforuser) {
547 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_closing,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
557 if (!empty($object->user_rappro) || !empty($object->user_rappro_id)) {
559 print
'<tr><td class="titlefield">';
561 print $langs->trans(
"ReconciledBy");
567 if (is_object($object->user_rappro)) {
568 if ($object->user_rappro->id) {
569 print $object->user_rappro->getNomUrl(-1,
'', 0, 0, 0);
571 print $langs->trans(
"Unknown");
574 $userstatic =
new User($db);
575 $userstatic->fetch($object->user_rappro_id ? $object->user_rappro_id : $object->user_rappro);
576 if ($userstatic->id) {
577 print $userstatic->getNomUrl(1,
'', 0, 0, 0);
579 print $langs->trans(
"Unknown");
590 if (!empty($object->date_rappro)) {
592 print
'<tr><td class="titlefield">';
594 print $langs->trans(
"DateConciliating");
600 print
dol_print_date($object->date_rappro,
'dayhour',
'tzserver');
601 if ($deltadateforuser) {
602 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_rappro,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
612 if (!empty($object->date_envoi)) {
614 print
'<tr><td class="titlefield">';
616 print $langs->trans(
"DateLastSend");
623 if ($deltadateforuser) {
624 print
' <span class="opacitymedium">'.$langs->trans(
"CurrentHour").
'</span> / '.
dol_print_date($object->date_envoi,
"dayhour",
'tzuserrel').
' <span class="opacitymedium">'.$langs->trans(
"ClientHour").
'</span>';
900function get_next_value($db, $mask, $table, $field, $where =
'', $objsoc =
'', $date =
'', $mode =
'next', $bentityon =
true, $objuser =
null, $forceentity =
null)
904 if (!is_object($objsoc)) {
905 $valueforccc = $objsoc;
906 } elseif ($table ==
"commande_fournisseur" || $table ==
"facture_fourn" || $table ==
"paiementfourn") {
912 $sharetable = $table;
913 if ($table ==
'facture' || $table ==
'invoice') {
914 $sharetable =
'invoicenumber';
930 $hasglobalcounter =
false;
933 if (preg_match(
'/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i', $mask, $reg)) {
934 $masktri = $reg[1].(!empty($reg[2]) ? $reg[2] :
'').(!empty($reg[3]) ? $reg[3] :
'');
935 $maskcounter = $reg[1];
936 $hasglobalcounter =
true;
940 $maskcounter =
'00000';
945 $resetEveryMonth =
false;
947 return 'ErrorCounterMustHaveMoreThan3Digits';
951 $regClientRef = array();
952 if (preg_match(
'/\{(c+)(0*)\}/i', $mask, $regClientRef)) {
953 $maskrefclient = $regClientRef[1].$regClientRef[2];
954 $maskrefclient_maskclientcode = $regClientRef[1];
955 $maskrefclient_maskcounter = $regClientRef[2];
956 $maskrefclient_maskoffset = 0;
957 $maskrefclient_clientcode = substr($valueforccc, 0,
dol_strlen($maskrefclient_maskclientcode));
958 $maskrefclient_clientcode = str_pad($maskrefclient_clientcode,
dol_strlen($maskrefclient_maskclientcode),
"#", STR_PAD_RIGHT);
961 return 'ErrorCounterMustHaveMoreThan3Digits';
968 if (!$hasglobalcounter && ($maskrefclient_maskcounter ==
'')) {
969 return 'ErrorBadMask';
974 if (preg_match(
'/\{(t+)\}/i', $mask, $regType)) {
975 $masktype = $regType[1];
976 $masktype_value =
dol_substr(preg_replace(
'/^TE_/',
'', $objsoc->typent_code), 0,
dol_strlen($regType[1]));
977 $masktype_value = str_pad($masktype_value,
dol_strlen($regType[1]),
"#", STR_PAD_RIGHT);
980 $masktype_value =
'';
985 if (preg_match(
'/\{(u+)\}/i', $mask, $regType)) {
986 $lastname =
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
987 if (is_object($objuser)) {
988 $lastname = $objuser->lastname;
991 $maskuser = $regType[1];
992 $maskuser_value = substr($lastname, 0,
dol_strlen($regType[1]));
993 $maskuser_value = str_pad($maskuser_value,
dol_strlen($regType[1]),
"#", STR_PAD_RIGHT);
996 $maskuser_value =
'';
1000 $maskperso = array();
1001 $maskpersonew = array();
1004 while (preg_match(
'/\{([A-Z]+)\-([1-9])\}/', $tmpmask, $regKey)) {
1005 $maskperso[$regKey[1]] =
'{'.$regKey[1].
'-'.$regKey[2].
'}';
1006 $maskpersonew[$regKey[1]] = str_pad(
'', $regKey[2],
'_', STR_PAD_RIGHT);
1007 $tmpmask = preg_replace(
'/\{'.$regKey[1].
'\-'.$regKey[2].
'\}/i', $maskpersonew[$regKey[1]], $tmpmask);
1010 if (strstr($mask,
'user_extra_')) {
1011 $start =
"{user_extra_";
1014 if (!empty($user->array_options[
'options_'.$extra])) {
1015 $mask = preg_replace(
'#('.$start.
')(.*?)('.$end.
')#si', $user->array_options[
'options_'.$extra], $mask);
1018 $maskwithonlyymcode = $mask;
1019 $maskwithonlyymcode = preg_replace(
'/\{(0+)([@\+][0-9\-\+\=]+)?([@\+][0-9\-\+\=]+)?\}/i', $maskcounter, $maskwithonlyymcode);
1020 $maskwithonlyymcode = preg_replace(
'/\{dd\}/i',
'dd', $maskwithonlyymcode);
1021 $maskwithonlyymcode = preg_replace(
'/\{(c+)(0*)\}/i', $maskrefclient, $maskwithonlyymcode);
1022 $maskwithonlyymcode = preg_replace(
'/\{(t+)\}/i', $masktype_value, $maskwithonlyymcode);
1023 $maskwithonlyymcode = preg_replace(
'/\{(u+)\}/i', $maskuser_value, $maskwithonlyymcode);
1024 foreach ($maskperso as $key => $val) {
1025 $maskwithonlyymcode = preg_replace(
'/'.preg_quote($val,
'/').
'/i', $maskpersonew[$key], $maskwithonlyymcode);
1027 $maskwithnocode = $maskwithonlyymcode;
1028 $maskwithnocode = preg_replace(
'/\{yyyy\}/i',
'yyyy', $maskwithnocode);
1029 $maskwithnocode = preg_replace(
'/\{yy\}/i',
'yy', $maskwithnocode);
1030 $maskwithnocode = preg_replace(
'/\{y\}/i',
'y', $maskwithnocode);
1031 $maskwithnocode = preg_replace(
'/\{mm\}/i',
'mm', $maskwithnocode);
1038 if (!empty($reg[2]) && preg_match(
'/^\+/', $reg[2])) {
1039 $maskoffset = preg_replace(
'/^\+/',
'', $reg[2]);
1041 if (!empty($reg[3]) && preg_match(
'/^\+/', $reg[3])) {
1042 $maskoffset = preg_replace(
'/^\+/',
'', $reg[3]);
1048 $yearoffsettype =
false;
1051 if (!empty($reg[2]) && preg_match(
'/^@/', $reg[2])) {
1052 $yearoffsettype = preg_replace(
'/^@/',
'', $reg[2]);
1054 if (!empty($reg[3]) && preg_match(
'/^@/', $reg[3])) {
1055 $yearoffsettype = preg_replace(
'/^@/',
'', $reg[3]);
1059 if (is_numeric($yearoffsettype) && $yearoffsettype >= 1) {
1060 $maskraz = $yearoffsettype;
1061 } elseif ($yearoffsettype ===
'0' || (!empty($yearoffsettype) && !is_numeric($yearoffsettype) &&
getDolGlobalInt(
'SOCIETE_FISCAL_MONTH_START') > 1)) {
1062 $maskraz = $conf->global->SOCIETE_FISCAL_MONTH_START;
1067 if ($maskraz == 99) {
1068 $maskraz = date(
'm', $date);
1069 $resetEveryMonth =
true;
1071 if ($maskraz > 12) {
1072 return 'ErrorBadMaskBadRazMonth';
1077 if (preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1080 } elseif (preg_match(
'/^(.*)\{(m+)\}\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1084 return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
1088 return 'ErrorCantUseRazWithYearOnOneDigit';
1091 if (preg_match(
'/^(.*)\{(m+)\}\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1094 } elseif (preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1097 } elseif (preg_match(
'/^(.*)\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1101 return 'ErrorCantUseRazIfNoYearInMask';
1105 $yearlen = $posy ?
dol_strlen($reg[$posy]) : 0;
1106 $monthlen = $posm ?
dol_strlen($reg[$posm]) : 0;
1109 $monthpos = ($yearpos + $yearlen);
1110 if ($posy == 3 && $posm == 2) {
1112 $yearpos = ($monthpos + $monthlen);
1117 $monthcomp = $maskraz;
1120 if (!empty($yearoffsettype) && !is_numeric($yearoffsettype) && $yearoffsettype !=
'=') {
1121 $currentyear = date(
"Y", $date);
1122 $fiscaldate =
dol_mktime(
'0',
'0',
'0', $maskraz,
'1', $currentyear);
1123 $newyeardate =
dol_mktime(
'0',
'0',
'0',
'1',
'1', $currentyear);
1124 $nextnewyeardate =
dol_mktime(
'0',
'0',
'0',
'1',
'1', $currentyear + 1);
1128 if ($date >= $fiscaldate) {
1130 if ($date < $nextnewyeardate && $yearoffsettype ==
'+') {
1133 } elseif ($date >= $newyeardate && $yearoffsettype ==
'-') {
1137 } elseif (date(
"m", $date) < $maskraz && empty($resetEveryMonth)) {
1142 if ($yearlen == 4) {
1143 $yearcomp = sprintf(
"%04d", date(
"Y", $date) + $yearoffset);
1144 } elseif ($yearlen == 2) {
1145 $yearcomp = sprintf(
"%02d", date(
"y", $date) + $yearoffset);
1146 } elseif ($yearlen == 1) {
1147 $yearcomp = substr(date(
'y', $date), 1, 1) + $yearoffset;
1149 if ($monthcomp > 1 && empty($resetEveryMonth)) {
1150 if ($yearlen == 4) {
1151 $yearcomp1 = sprintf(
"%04d", date(
"Y", $date) + $yearoffset + 1);
1152 } elseif ($yearlen == 2) {
1153 $yearcomp1 = sprintf(
"%02d", date(
"y", $date) + $yearoffset + 1);
1157 $sqlwhere .=
" (SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"'";
1158 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") >= '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"')";
1160 $sqlwhere .=
" (SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp1).
"'";
1161 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") < '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"') ";
1163 } elseif ($resetEveryMonth) {
1164 $sqlwhere .=
"(SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"'";
1165 $sqlwhere .=
" AND SUBSTRING(".$field.
", ".$monthpos.
", ".$monthlen.
") = '".str_pad($monthcomp, $monthlen,
'0', STR_PAD_LEFT).
"')";
1167 $sqlwhere .=
"(SUBSTRING(".$field.
", ".$yearpos.
", ".$yearlen.
") = '".$db->escape($yearcomp).
"')";
1174 if (function_exists(
'mb_strrpos')) {
1175 $posnumstart = mb_strrpos($maskwithnocode, $maskcounter, 0,
'UTF-8');
1177 $posnumstart = strrpos($maskwithnocode, $maskcounter);
1179 if ($posnumstart < 0) {
1180 return 'ErrorBadMaskFailedToLocatePosOfSequence';
1182 $sqlstring =
"SUBSTRING(".$field.
", ".($posnumstart + 1).
", ".
dol_strlen($maskcounter).
")";
1186 $maskLike = str_replace(
"%",
"_", $maskLike);
1189 $maskLike = preg_replace(
'/\{yyyy\}/i',
'____', $maskLike);
1190 $maskLike = preg_replace(
'/\{yy\}/i',
'__', $maskLike);
1191 $maskLike = preg_replace(
'/\{y\}/i',
'_', $maskLike);
1192 $maskLike = preg_replace(
'/\{mm\}/i',
'__', $maskLike);
1193 $maskLike = preg_replace(
'/\{dd\}/i',
'__', $maskLike);
1195 if ($maskrefclient) {
1204 foreach ($maskperso as $key => $val) {
1205 $maskLike = str_replace(
dol_string_nospecial($maskperso[$key]), $maskpersonew[$key], $maskLike);
1210 $sql =
"SELECT MAX(".$sqlstring.
") as val";
1211 $sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1212 $sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskLike) . (
getDolGlobalString(
'SEARCH_FOR_NEXT_VAL_ON_START_ONLY') ?
"%" :
"") .
"'";
1213 $sql .=
" AND ".$field.
" NOT LIKE '(PROV%)'";
1218 $sql .=
" AND ". $db->regexpsql($sqlstring,
'^[0-9]+$', 1);
1222 $sql .=
" AND entity IN (".getEntity($sharetable).
")";
1223 } elseif (!empty($forceentity)) {
1224 $sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1230 $sql .=
" AND ".$sqlwhere;
1234 dol_syslog(
"functions2::get_next_value mode=".$mode, LOG_DEBUG);
1235 $resql = $db->query($sql);
1237 $obj = $db->fetch_object($resql);
1238 $counter = $obj->val;
1244 if (empty($counter)) {
1245 $counter = $maskoffset;
1246 } elseif (preg_match(
'/[^0-9]/i', $counter)) {
1247 dol_syslog(
"Error, the last counter found is '".$counter.
"' so is not a numeric value. We will restart to 1.", LOG_ERR);
1249 } elseif ($counter < $maskoffset && !
getDolGlobalString(
'MAIN_NUMBERING_OFFSET_ONLY_FOR_FIRST')) {
1250 $counter = $maskoffset;
1253 if ($mode ==
'last') {
1254 $counterpadded = str_pad($counter,
dol_strlen($maskcounter),
"0", STR_PAD_LEFT);
1258 $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) {
1277 $sql =
"SELECT ".$field.
" as ref";
1278 $sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1279 $sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskLike) . (
getDolGlobalString(
'SEARCH_FOR_NEXT_VAL_ON_START_ONLY') ?
"%" :
"") .
"'";
1280 $sql .=
" AND ".$field.
" NOT LIKE '%PROV%'";
1282 $sql .=
" AND entity IN (".getEntity($sharetable).
")";
1283 } elseif (!empty($forceentity)) {
1284 $sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1290 $sql .=
" AND ".$sqlwhere;
1293 dol_syslog(
"functions2::get_next_value mode=".$mode, LOG_DEBUG);
1294 $resql = $db->query($sql);
1296 $obj = $db->fetch_object($resql);
1305 } elseif ($mode ==
'next') {
1307 $maskrefclient_counter = 0;
1310 if ($counter >= pow(10,
dol_strlen($maskcounter))) {
1311 $counter =
'ErrorMaxNumberReachForThisMask';
1314 if (!empty($maskrefclient_maskcounter)) {
1318 $maskrefclient_posnumstart = strpos($maskwithnocode, $maskrefclient_maskcounter, strpos($maskwithnocode, $maskrefclient));
1319 if ($maskrefclient_posnumstart <= 0) {
1320 return 'ErrorBadMask';
1322 $maskrefclient_sqlstring =
'SUBSTRING('.$field.
', '.($maskrefclient_posnumstart + 1).
', '.
dol_strlen($maskrefclient_maskcounter).
')';
1327 $maskrefclient_maskLike = str_replace(
"%",
"_", $maskrefclient_maskLike);
1329 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{yyyy}'),
'____', $maskrefclient_maskLike);
1330 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{yy}'),
'__', $maskrefclient_maskLike);
1331 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{y}'),
'_', $maskrefclient_maskLike);
1332 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{mm}'),
'__', $maskrefclient_maskLike);
1333 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{dd}'),
'__', $maskrefclient_maskLike);
1334 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{'.$masktri.
'}'), str_pad(
"",
dol_strlen($maskcounter),
"_"), $maskrefclient_maskLike);
1335 $maskrefclient_maskLike = str_replace(
dol_string_nospecial(
'{'.$maskrefclient.
'}'), $maskrefclient_clientcode.str_pad(
"",
dol_strlen($maskrefclient_maskcounter),
"_"), $maskrefclient_maskLike);
1338 $maskrefclient_sql =
"SELECT MAX(".$maskrefclient_sqlstring.
") as val";
1339 $maskrefclient_sql .=
" FROM ".MAIN_DB_PREFIX.$table;
1341 $maskrefclient_sql .=
" WHERE ".$field.
" LIKE '".$db->escape($maskrefclient_maskLike) . (
getDolGlobalString(
'SEARCH_FOR_NEXT_VAL_ON_START_ONLY') ?
"%" :
"") .
"'";
1343 $maskrefclient_sql .=
" AND entity IN (".getEntity($sharetable).
")";
1344 } elseif (!empty($forceentity)) {
1345 $maskrefclient_sql .=
" AND entity IN (".$db->sanitize($forceentity).
")";
1348 $maskrefclient_sql .= $where;
1351 $maskrefclient_sql .=
' AND '.$sqlwhere;
1353 $maskrefclient_sql .=
" AND (SUBSTRING(".$field.
", ".(strpos($maskwithnocode, $maskrefclient) + 1).
", ".
dol_strlen($maskrefclient_maskclientcode).
") = '".$db->escape($maskrefclient_clientcode).
"')";
1355 dol_syslog(
"functions2::get_next_value maskrefclient", LOG_DEBUG);
1356 $maskrefclient_resql = $db->query($maskrefclient_sql);
1357 if ($maskrefclient_resql) {
1358 $maskrefclient_obj = $db->fetch_object($maskrefclient_resql);
1359 $maskrefclient_counter = $maskrefclient_obj->val;
1364 if (empty($maskrefclient_counter) || preg_match(
'/[^0-9]/i', $maskrefclient_counter)) {
1365 $maskrefclient_counter = $maskrefclient_maskoffset;
1367 $maskrefclient_counter++;
1374 if (!empty($yearoffsettype) && !is_numeric($yearoffsettype) && $yearoffsettype !=
'=') {
1375 $numFinal = preg_replace(
'/\{yyyy\}/i', date(
"Y", $date) + $yearoffset, $numFinal);
1376 $numFinal = preg_replace(
'/\{yy\}/i', date(
"y", $date) + $yearoffset, $numFinal);
1377 $numFinal = preg_replace(
'/\{y\}/i', substr(date(
"y", $date), 1, 1) + $yearoffset, $numFinal);
1379 $numFinal = preg_replace(
'/\{yyyy\}/i', date(
"Y", $date), $numFinal);
1380 $numFinal = preg_replace(
'/\{yy\}/i', date(
"y", $date), $numFinal);
1381 $numFinal = preg_replace(
'/\{y\}/i', substr(date(
"y", $date), 1, 1), $numFinal);
1383 $numFinal = preg_replace(
'/\{mm\}/i', date(
"m", $date), $numFinal);
1384 $numFinal = preg_replace(
'/\{dd\}/i', date(
"d", $date), $numFinal);
1387 $maskbefore =
'{'.$masktri.
'}';
1388 $maskafter = str_pad($counter,
dol_strlen($maskcounter),
"0", STR_PAD_LEFT);
1390 $numFinal = str_replace($maskbefore, $maskafter, $numFinal);
1393 if ($maskrefclient) {
1395 $maskrefclient_maskbefore =
'{'.$maskrefclient.
'}';
1396 $maskrefclient_maskafter = $maskrefclient_clientcode;
1397 if (
dol_strlen($maskrefclient_maskcounter) > 0) {
1398 $maskrefclient_maskafter .= str_pad($maskrefclient_counter,
dol_strlen($maskrefclient_maskcounter),
"0", STR_PAD_LEFT);
1400 $numFinal = str_replace($maskrefclient_maskbefore, $maskrefclient_maskafter, $numFinal);
1405 $masktype_maskbefore =
'{'.$masktype.
'}';
1406 $masktype_maskafter = $masktype_value;
1407 $numFinal = str_replace($masktype_maskbefore, $masktype_maskafter, $numFinal);
1412 $maskuser_maskbefore =
'{'.$maskuser.
'}';
1413 $maskuser_maskafter = $maskuser_value;
1414 $numFinal = str_replace($maskuser_maskbefore, $maskuser_maskafter, $numFinal);
1418 dol_syslog(
"functions2::get_next_value return ".$numFinal, LOG_DEBUG);
1453 $hasglobalcounter =
false;
1456 if (preg_match(
'/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
1457 $masktri = $reg[1].(isset($reg[2]) ? $reg[2] :
'').(isset($reg[3]) ? $reg[3] :
'');
1458 $maskcounter = $reg[1];
1459 $hasglobalcounter =
true;
1463 $maskcounter =
'00000';
1468 return 'ErrorCounterMustHaveMoreThan3Digits';
1472 $regClientRef = array();
1473 if (preg_match(
'/\{(c+)(0*)\}/i', $mask, $regClientRef)) {
1474 $maskrefclient = $regClientRef[1].$regClientRef[2];
1475 $maskrefclient_maskclientcode = $regClientRef[1];
1476 $maskrefclient_maskcounter = $regClientRef[2];
1477 $maskrefclient_maskoffset = 0;
1478 $maskrefclient_clientcode = substr(
'', 0,
dol_strlen($maskrefclient_maskclientcode));
1479 $maskrefclient_clientcode = str_pad($maskrefclient_clientcode,
dol_strlen($maskrefclient_maskclientcode),
"#", STR_PAD_RIGHT);
1481 if (
dol_strlen($maskrefclient_maskcounter) > 0 &&
dol_strlen($maskrefclient_maskcounter) < 3) {
1482 return 'ErrorCounterMustHaveMoreThan3Digits';
1485 $maskrefclient =
'';
1489 if (!$hasglobalcounter && ($maskrefclient_maskcounter ==
'')) {
1490 return 'ErrorBadMask';
1493 $maskwithonlyymcode = $mask;
1494 $maskwithonlyymcode = preg_replace(
'/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $maskcounter, $maskwithonlyymcode);
1495 $maskwithonlyymcode = preg_replace(
'/\{dd\}/i',
'dd', $maskwithonlyymcode);
1496 $maskwithonlyymcode = preg_replace(
'/\{(c+)(0*)\}/i', $maskrefclient, $maskwithonlyymcode);
1497 $maskwithnocode = $maskwithonlyymcode;
1498 $maskwithnocode = preg_replace(
'/\{yyyy\}/i',
'yyyy', $maskwithnocode);
1499 $maskwithnocode = preg_replace(
'/\{yy\}/i',
'yy', $maskwithnocode);
1500 $maskwithnocode = preg_replace(
'/\{y\}/i',
'y', $maskwithnocode);
1501 $maskwithnocode = preg_replace(
'/\{mm\}/i',
'mm', $maskwithnocode);
1507 if (!empty($reg[2]) && preg_match(
'/^\+/', $reg[2])) {
1508 $maskoffset = preg_replace(
'/^\+/',
'', $reg[2]);
1510 if (!empty($reg[3]) && preg_match(
'/^\+/', $reg[3])) {
1511 $maskoffset = preg_replace(
'/^\+/',
'', $reg[3]);
1517 if (!empty($reg[2]) && preg_match(
'/^@/', $reg[2])) {
1518 $maskraz = preg_replace(
'/^@/',
'', $reg[2]);
1520 if (!empty($reg[3]) && preg_match(
'/^@/', $reg[3])) {
1521 $maskraz = preg_replace(
'/^@/',
'', $reg[3]);
1523 if ($maskraz >= 0) {
1524 if ($maskraz == 99) {
1525 $maskraz = date(
'm');
1526 $resetEveryMonth =
true;
1528 if ($maskraz > 12) {
1529 return 'ErrorBadMaskBadRazMonth';
1533 if ($maskraz > 1 && !preg_match(
'/^(.*)\{(y+)\}\{(m+)\}/i', $maskwithonlyymcode, $reg)) {
1534 return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask';
1536 if ($maskraz <= 1 && !preg_match(
'/^(.*)\{(y+)\}/i', $maskwithonlyymcode, $reg)) {
1537 return 'ErrorCantUseRazIfNoYearInMask';
1543 if (function_exists(
'mb_strrpos')) {
1544 $posnumstart = mb_strrpos($maskwithnocode, $maskcounter, 0,
'UTF-8');
1546 $posnumstart = strrpos($maskwithnocode, $maskcounter);
1548 if ($posnumstart < 0) {
1549 return 'ErrorBadMaskFailedToLocatePosOfSequence';
1561 dol_syslog(
"functions2::check_value result=".$result, LOG_DEBUG);
1615 $stime = strftime(
'%Y-%m-%d', $time);
1617 if (preg_match(
'/^([0-9]+)\-([0-9]+)\-([0-9]+)\s?([0-9]+)?:?([0-9]+)?/i', $stime, $reg)) {
1632 if (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) == 0) {
1633 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) - 3 * 24 * 60 * 60;
1634 } elseif (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) < 4) {
1635 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) + (4 - date(
"w", mktime(12, 0, 0, $mois, $jour, $annee))) * 24 * 60 * 60;
1636 } elseif (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) > 4) {
1637 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee) - (date(
"w", mktime(12, 0, 0, $mois, $jour, $annee)) - 4) * 24 * 60 * 60;
1639 $jeudiSemaine = mktime(12, 0, 0, $mois, $jour, $annee);
1643 if (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) == 0) {
1644 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine)) + 4 * 24 * 60 * 60;
1645 } elseif (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) < 4) {
1646 $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;
1647 } elseif (date(
"w", mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine))) > 4) {
1648 $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;
1650 $premierJeudiAnnee = mktime(12, 0, 0, 1, 1, date(
"Y", $jeudiSemaine));
1656 date(
"z", mktime(12, 0, 0, date(
"m", $jeudiSemaine), date(
"d", $jeudiSemaine), date(
"Y", $jeudiSemaine)))
1658 date(
"z", mktime(12, 0, 0, date(
"m", $premierJeudiAnnee), date(
"d", $premierJeudiAnnee), date(
"Y", $premierJeudiAnnee)))
1663 if ($numeroSemaine == 53) {
1665 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)) {
1666 $numeroSemaine = 53;
1674 return sprintf(
"%02d", $numeroSemaine);