allow to decrement invoice counter (#1947)

* added increment for cc, ccy, ccn, ccd
This commit is contained in:
Kevin Papst
2020-09-17 02:53:38 +02:00
committed by GitHub
parent 9ef32e75c5
commit c02a882eff
2 changed files with 202 additions and 111 deletions

View File

@@ -62,103 +62,135 @@ final class ConfigurableNumberGenerator implements NumberGeneratorInterface
preg_match_all('/{[^}]*?}/', $format, $matches);
foreach ($matches[0] as $part) {
$formatterLength = null;
$increaseBy = 1;
$tmp = str_replace(['{', '}'], '', $part);
$parts = preg_split('/[,]+/', $tmp);
$tmp = $parts[0];
if (\count($parts) === 2) {
$formatterLength = \intval($parts[1]);
if ((string) $formatterLength !== $parts[1]) {
$formatterLength = null;
}
}
$parts = preg_split("/[\+]+/", $tmp);
$tmp = $parts[0];
if (\count($parts) === 2) {
$increaseBy = \intval($parts[1]);
if ($increaseBy <= 0) {
$increaseBy = 1;
}
}
switch ($tmp) {
case 'Y':
$partialResult = $invoiceDate->format('Y');
break;
case 'y':
$partialResult = $invoiceDate->format('y');
break;
case 'M':
$partialResult = $invoiceDate->format('m');
break;
case 'm':
$partialResult = $invoiceDate->format('n');
break;
case 'D':
$partialResult = $invoiceDate->format('d');
break;
case 'd':
$partialResult = $invoiceDate->format('j');
break;
case 'date':
$partialResult = $invoiceDate->format('ymd');
break;
// for customer
case 'cc':
$partialResult = $this->repository->getCounterForAllTime($invoiceDate, $this->model->getCustomer()) + 1;
break;
case 'ccy':
$partialResult = $this->repository->getCounterForYear($invoiceDate, $this->model->getCustomer()) + 1;
break;
case 'ccm':
$partialResult = $this->repository->getCounterForMonth($invoiceDate, $this->model->getCustomer()) + 1;
break;
case 'ccd':
$partialResult = $this->repository->getCounterForDay($invoiceDate, $this->model->getCustomer()) + 1;
break;
// across all invoices
case 'c':
$partialResult = $this->repository->getCounterForAllTime($invoiceDate) + $increaseBy;
break;
case 'cy':
$partialResult = $this->repository->getCounterForYear($invoiceDate) + $increaseBy;
break;
case 'cm':
$partialResult = $this->repository->getCounterForMonth($invoiceDate) + $increaseBy;
break;
case 'cd':
$partialResult = $this->repository->getCounterForDay($invoiceDate) + $increaseBy;
break;
default:
$partialResult = $part;
}
if (null !== $formatterLength) {
$partialResult = str_pad($partialResult, $formatterLength, '0', STR_PAD_LEFT);
}
$partialResult = $this->parseReplacer($invoiceDate, $part);
$result = str_replace($part, $partialResult, $result);
}
return (string) $result;
}
private function parseReplacer(\DateTime $invoiceDate, string $originalFormat): string
{
$formatterLength = null;
$increaseBy = 0;
$formatPattern = str_replace(['{', '}'], '', $originalFormat);
$parts = preg_split('/([+\-,])+/', $formatPattern, -1, PREG_SPLIT_DELIM_CAPTURE);
$format = array_shift($parts);
if (\count($parts) % 2 !== 0) {
throw new \InvalidArgumentException('Invalid configuration found');
}
while (null !== ($tmp = array_shift($parts))) {
switch ($tmp) {
case '+':
$local = array_shift($parts);
if (!is_numeric($local)) {
throw new \InvalidArgumentException('Unknown increment found');
}
$increaseBy = $increaseBy + \intval($local);
break;
case '-':
$local = array_shift($parts);
if (!is_numeric($local)) {
throw new \InvalidArgumentException('Unknown decrement found');
}
$increaseBy = $increaseBy - \intval($local);
break;
case ',':
$local = array_shift($parts);
if (!is_numeric($local)) {
throw new \InvalidArgumentException('Unknown format length found');
}
$formatterLength = \intval($local);
if ((string) $formatterLength !== $local) {
throw new \InvalidArgumentException('Unknown format length found');
}
break;
default:
throw new \InvalidArgumentException('Unknown pattern found');
}
}
if ($increaseBy === 0) {
$increaseBy = 1;
}
switch ($format) {
case 'Y':
$partialResult = $invoiceDate->format('Y');
break;
case 'y':
$partialResult = $invoiceDate->format('y');
break;
case 'M':
$partialResult = $invoiceDate->format('m');
break;
case 'm':
$partialResult = $invoiceDate->format('n');
break;
case 'D':
$partialResult = $invoiceDate->format('d');
break;
case 'd':
$partialResult = $invoiceDate->format('j');
break;
case 'date':
$partialResult = $invoiceDate->format('ymd');
break;
// for customer
case 'cc':
$partialResult = $this->repository->getCounterForAllTime($invoiceDate, $this->model->getCustomer()) + $increaseBy;
break;
case 'ccy':
$partialResult = $this->repository->getCounterForYear($invoiceDate, $this->model->getCustomer()) + $increaseBy;
break;
case 'ccm':
$partialResult = $this->repository->getCounterForMonth($invoiceDate, $this->model->getCustomer()) + $increaseBy;
break;
case 'ccd':
$partialResult = $this->repository->getCounterForDay($invoiceDate, $this->model->getCustomer()) + $increaseBy;
break;
// across all invoices
case 'c':
$partialResult = $this->repository->getCounterForAllTime($invoiceDate) + $increaseBy;
break;
case 'cy':
$partialResult = $this->repository->getCounterForYear($invoiceDate) + $increaseBy;
break;
case 'cm':
$partialResult = $this->repository->getCounterForMonth($invoiceDate) + $increaseBy;
break;
case 'cd':
$partialResult = $this->repository->getCounterForDay($invoiceDate) + $increaseBy;
break;
default:
$partialResult = $originalFormat;
}
if (null !== $formatterLength) {
$partialResult = str_pad($partialResult, $formatterLength, '0', STR_PAD_LEFT);
}
return $partialResult;
}
}