This commit is contained in:
loviuz 2020-01-03 17:09:49 +01:00
parent 9efeba383b
commit a9a1cbe9e4
2 changed files with 15 additions and 11 deletions

View File

@ -719,7 +719,7 @@ class Fattura extends Document
$ultimo = Generator::getPreviousFrom($maschera, 'co_documenti', 'numero_esterno', [ $ultimo = Generator::getPreviousFrom($maschera, 'co_documenti', 'numero_esterno', [
'YEAR(data) = '.prepare(date('Y', strtotime($data))), 'YEAR(data) = '.prepare(date('Y', strtotime($data))),
'id_segment = '.prepare($id_segment), 'id_segment = '.prepare($id_segment),
]); ], $data);
$numero = Generator::generate($maschera, $ultimo, 1, Generator::dateToPattern($data)); $numero = Generator::generate($maschera, $ultimo, 1, Generator::dateToPattern($data));
return $numero; return $numero;

View File

@ -46,10 +46,10 @@ class Generator
* *
* @return string * @return string
*/ */
public static function generate($pattern, $last = null, $quantity = 1, $values = []) public static function generate($pattern, $last = null, $quantity = 1, $values = [], $date = null)
{ {
// Costruzione del pattern // Costruzione del pattern
$result = self::complete($pattern, $values); $result = self::complete($pattern, $values, $date);
$length = substr_count($result, '#'); $length = substr_count($result, '#');
// Individuazione dei valori precedenti // Individuazione dei valori precedenti
@ -70,10 +70,10 @@ class Generator
* *
* @return string * @return string
*/ */
public static function complete($pattern, $values = []) public static function complete($pattern, $values = [], $date = null)
{ {
// Costruzione del pattern // Costruzione del pattern
$replaces = array_merge(self::getReplaces(), $values); $replaces = array_merge(self::getReplaces($date), $values);
$keys = array_keys($replaces); $keys = array_keys($replaces);
$values = array_column($replaces, 'value'); $values = array_column($replaces, 'value');
@ -88,10 +88,10 @@ class Generator
* *
* @return array * @return array
*/ */
public static function read($pattern, $string) public static function read($pattern, $string, $date = null)
{ {
// Costruzione del pattern // Costruzione del pattern
$replaces = self::getReplaces(); $replaces = self::getReplaces($date);
$replaces['#'] = [ $replaces['#'] = [
'regex' => '(?<number>[0-9]+)', 'regex' => '(?<number>[0-9]+)',
@ -114,14 +114,18 @@ class Generator
* *
* @return array * @return array
*/ */
public static function getReplaces() public static function getReplaces($date = null)
{ {
$replaces = self::$replaces; $replaces = self::$replaces;
if ($date == null) {
$date = date('Y-m-d');
}
foreach ($replaces as $key => $value) { foreach ($replaces as $key => $value) {
if (!isset($replaces[$key]['value'])) { if (!isset($replaces[$key]['value'])) {
if (isset($replaces[$key]['date'])) { if (isset($replaces[$key]['date'])) {
$replaces[$key]['value'] = date($replaces[$key]['date']); $replaces[$key]['value'] = date($replaces[$key]['date'], strtotime($date));
} else { } else {
$replaces[$key]['value'] = $key; $replaces[$key]['value'] = $key;
} }
@ -174,11 +178,11 @@ class Generator
return $maschera['pattern']; return $maschera['pattern'];
} }
public static function getPreviousFrom($maschera, $table, $field, $where = []) public static function getPreviousFrom($maschera, $table, $field, $where = [], $date = null)
{ {
$order = static::getMascheraOrder($maschera, $field); $order = static::getMascheraOrder($maschera, $field);
$maschera = Generator::complete($maschera); $maschera = Generator::complete($maschera, [], $date);
$maschera = str_replace('#', '%', $maschera); $maschera = str_replace('#', '%', $maschera);
$query = Manager::table($table)->select($field)->where($field, 'like', $maschera)->orderByRaw($order); $query = Manager::table($table)->select($field)->where($field, 'like', $maschera)->orderByRaw($order);