invoice: increase number, customer specific counter (#1836)
This commit is contained in:
@@ -62,16 +62,26 @@ final class ConfigurableNumberGenerator implements NumberGeneratorInterface
|
|||||||
|
|
||||||
preg_match_all('/{[^}]*?}/', $format, $matches);
|
preg_match_all('/{[^}]*?}/', $format, $matches);
|
||||||
foreach ($matches[0] as $part) {
|
foreach ($matches[0] as $part) {
|
||||||
$formatter = null;
|
$formatterLength = null;
|
||||||
|
$increaseBy = 1;
|
||||||
|
|
||||||
$tmp = str_replace(['{', '}'], '', $part);
|
$tmp = str_replace(['{', '}'], '', $part);
|
||||||
|
|
||||||
// number format
|
$parts = preg_split('/[,]+/', $tmp);
|
||||||
if (substr_count($tmp, ',') !== 0) {
|
|
||||||
$parts = explode(',', $tmp);
|
|
||||||
$tmp = $parts[0];
|
$tmp = $parts[0];
|
||||||
$formatter = \intval($parts[1]);
|
if (\count($parts) === 2) {
|
||||||
if ((string) $formatter !== $parts[1]) {
|
$formatterLength = \intval($parts[1]);
|
||||||
$formatter = null;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,28 +114,46 @@ final class ConfigurableNumberGenerator implements NumberGeneratorInterface
|
|||||||
$partialResult = $invoiceDate->format('ymd');
|
$partialResult = $invoiceDate->format('ymd');
|
||||||
break;
|
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':
|
case 'c':
|
||||||
$partialResult = $this->repository->getCounterForAllTime($invoiceDate) + 1;
|
$partialResult = $this->repository->getCounterForAllTime($invoiceDate) + $increaseBy;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'cy':
|
case 'cy':
|
||||||
$partialResult = $this->repository->getCounterForYear($invoiceDate) + 1;
|
$partialResult = $this->repository->getCounterForYear($invoiceDate) + $increaseBy;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'cm':
|
case 'cm':
|
||||||
$partialResult = $this->repository->getCounterForMonth($invoiceDate) + 1;
|
$partialResult = $this->repository->getCounterForMonth($invoiceDate) + $increaseBy;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'cd':
|
case 'cd':
|
||||||
$partialResult = $this->repository->getCounterForDay($invoiceDate) + 1;
|
$partialResult = $this->repository->getCounterForDay($invoiceDate) + $increaseBy;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$partialResult = $part;
|
$partialResult = $part;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $formatter) {
|
if (null !== $formatterLength) {
|
||||||
$partialResult = str_pad($partialResult, $formatter, '0', STR_PAD_LEFT);
|
$partialResult = str_pad($partialResult, $formatterLength, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = str_replace($part, $partialResult, $result);
|
$result = str_replace($part, $partialResult, $result);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
namespace App\Repository;
|
namespace App\Repository;
|
||||||
|
|
||||||
|
use App\Entity\Customer;
|
||||||
use App\Entity\Invoice;
|
use App\Entity\Invoice;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Repository\Loader\InvoiceLoader;
|
use App\Repository\Loader\InvoiceLoader;
|
||||||
@@ -35,7 +36,7 @@ class InvoiceRepository extends EntityRepository
|
|||||||
$entityManager->flush();
|
$entityManager->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCounterFor(\DateTime $start, \DateTime $end): int
|
private function getCounterFor(\DateTime $start, \DateTime $end, ?Customer $customer = null): int
|
||||||
{
|
{
|
||||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||||
$qb->select('count(i.createdAt) as counter')
|
$qb->select('count(i.createdAt) as counter')
|
||||||
@@ -46,6 +47,13 @@ class InvoiceRepository extends EntityRepository
|
|||||||
->setParameter('end', $end)
|
->setParameter('end', $end)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
if (null !== $customer) {
|
||||||
|
$qb
|
||||||
|
->andWhere($qb->expr()->eq('i.customer', ':customer'))
|
||||||
|
->setParameter('customer', $customer)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
$result = $qb->getQuery()->getOneOrNullResult();
|
$result = $qb->getQuery()->getOneOrNullResult();
|
||||||
|
|
||||||
if ($result === null) {
|
if ($result === null) {
|
||||||
@@ -55,32 +63,36 @@ class InvoiceRepository extends EntityRepository
|
|||||||
return $result['counter'];
|
return $result['counter'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCounterForDay(\DateTime $date): int
|
public function getCounterForDay(\DateTime $date, ?Customer $customer = null): int
|
||||||
{
|
{
|
||||||
$start = (clone $date)->setTime(0, 0, 0);
|
$start = (clone $date)->setTime(0, 0, 0);
|
||||||
$end = (clone $date)->setTime(23, 59, 59);
|
$end = (clone $date)->setTime(23, 59, 59);
|
||||||
|
|
||||||
return $this->getCounterFor($start, $end);
|
return $this->getCounterFor($start, $end, $customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCounterForMonth(\DateTime $date): int
|
public function getCounterForMonth(\DateTime $date, ?Customer $customer = null): int
|
||||||
{
|
{
|
||||||
$start = (clone $date)->setDate((int) $date->format('Y'), (int) $date->format('n'), 1)->setTime(0, 0, 0);
|
$start = (clone $date)->setDate((int) $date->format('Y'), (int) $date->format('n'), 1)->setTime(0, 0, 0);
|
||||||
$end = (clone $date)->setDate((int) $date->format('Y'), (int) $date->format('n'), (int) $date->format('t'))->setTime(23, 59, 59);
|
$end = (clone $date)->setDate((int) $date->format('Y'), (int) $date->format('n'), (int) $date->format('t'))->setTime(23, 59, 59);
|
||||||
|
|
||||||
return $this->getCounterFor($start, $end);
|
return $this->getCounterFor($start, $end, $customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCounterForYear(\DateTime $date): int
|
public function getCounterForYear(\DateTime $date, ?Customer $customer = null): int
|
||||||
{
|
{
|
||||||
$start = (clone $date)->setDate((int) $date->format('Y'), 1, 1)->setTime(0, 0, 0);
|
$start = (clone $date)->setDate((int) $date->format('Y'), 1, 1)->setTime(0, 0, 0);
|
||||||
$end = (clone $date)->setDate((int) $date->format('Y'), 12, 31)->setTime(23, 59, 59);
|
$end = (clone $date)->setDate((int) $date->format('Y'), 12, 31)->setTime(23, 59, 59);
|
||||||
|
|
||||||
return $this->getCounterFor($start, $end);
|
return $this->getCounterFor($start, $end, $customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCounterForAllTime(\DateTime $date): int
|
public function getCounterForAllTime(\DateTime $date, ?Customer $customer = null): int
|
||||||
{
|
{
|
||||||
|
if (null !== $customer) {
|
||||||
|
return $this->count(['customer' => $customer]);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->count([]);
|
return $this->count([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
namespace App\Tests\Invoice\NumberGenerator;
|
namespace App\Tests\Invoice\NumberGenerator;
|
||||||
|
|
||||||
use App\Configuration\SystemConfiguration;
|
use App\Configuration\SystemConfiguration;
|
||||||
|
use App\Entity\Customer;
|
||||||
use App\Invoice\InvoiceModel;
|
use App\Invoice\InvoiceModel;
|
||||||
use App\Invoice\NumberGenerator\ConfigurableNumberGenerator;
|
use App\Invoice\NumberGenerator\ConfigurableNumberGenerator;
|
||||||
use App\Repository\InvoiceRepository;
|
use App\Repository\InvoiceRepository;
|
||||||
@@ -55,6 +56,7 @@ class ConfigurableNumberGeneratorTest extends TestCase
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
// simple tests for single calls
|
// simple tests for single calls
|
||||||
|
['my-{date} is+cool,really', 'my-' . $invoiceDate->format('ymd') . ' is+cool,really', $invoiceDate],
|
||||||
['{date}', $invoiceDate->format('ymd'), $invoiceDate],
|
['{date}', $invoiceDate->format('ymd'), $invoiceDate],
|
||||||
['{Y}', $invoiceDate->format('Y'), $invoiceDate],
|
['{Y}', $invoiceDate->format('Y'), $invoiceDate],
|
||||||
['{y}', $invoiceDate->format('y'), $invoiceDate],
|
['{y}', $invoiceDate->format('y'), $invoiceDate],
|
||||||
@@ -73,10 +75,27 @@ class ConfigurableNumberGeneratorTest extends TestCase
|
|||||||
['{M,3}', '0' . $invoiceDate->format('m'), $invoiceDate],
|
['{M,3}', '0' . $invoiceDate->format('m'), $invoiceDate],
|
||||||
['{M,#}', $invoiceDate->format('m'), $invoiceDate], // invalid formatter length
|
['{M,#}', $invoiceDate->format('m'), $invoiceDate], // invalid formatter length
|
||||||
['{D,3}', '0' . $invoiceDate->format('d'), $invoiceDate],
|
['{D,3}', '0' . $invoiceDate->format('d'), $invoiceDate],
|
||||||
|
// counter across all invoices
|
||||||
['{c,2}', '02', $invoiceDate],
|
['{c,2}', '02', $invoiceDate],
|
||||||
['{cy,2}', '02', $invoiceDate],
|
['{cy,2}', '02', $invoiceDate],
|
||||||
['{cm,2}', '02', $invoiceDate],
|
['{cm,2}', '02', $invoiceDate],
|
||||||
['{cd,2}', '02', $invoiceDate],
|
['{cd,2}', '02', $invoiceDate],
|
||||||
|
// customer specific
|
||||||
|
['{cc,2}', '02', $invoiceDate],
|
||||||
|
['{ccy,2}', '02', $invoiceDate],
|
||||||
|
['{ccm,2}', '02', $invoiceDate],
|
||||||
|
['{ccd,2}', '02', $invoiceDate],
|
||||||
|
// with incrementing counter
|
||||||
|
['{c+13,2}', '14', $invoiceDate],
|
||||||
|
['{ccy+1,2}', '02', $invoiceDate],
|
||||||
|
['{cm+-1,2}', '02', $invoiceDate], // negative is not allowed and set to 1
|
||||||
|
['{cm+0,2}', '02', $invoiceDate], // zero is not allowed and set to 1
|
||||||
|
['{cd+111,2}', '112', $invoiceDate],
|
||||||
|
['{c+13}', '14', $invoiceDate],
|
||||||
|
['{cy+1}', '2', $invoiceDate],
|
||||||
|
['{cm+-1}', '2', $invoiceDate], // negative is not allowed and set to 1
|
||||||
|
['{cm+0}', '2', $invoiceDate], // zero is not allowed and set to 1
|
||||||
|
['{cd+111}', '112', $invoiceDate],
|
||||||
// mixing identifiers
|
// mixing identifiers
|
||||||
['{Y}{cy}', $invoiceDate->format('Y') . '2', $invoiceDate],
|
['{Y}{cy}', $invoiceDate->format('Y') . '2', $invoiceDate],
|
||||||
['{Y}{cy}{m}', $invoiceDate->format('Y') . '2' . $invoiceDate->format('n'), $invoiceDate],
|
['{Y}{cy}{m}', $invoiceDate->format('Y') . '2' . $invoiceDate->format('n'), $invoiceDate],
|
||||||
@@ -95,6 +114,7 @@ class ConfigurableNumberGeneratorTest extends TestCase
|
|||||||
$sut = $this->getSut($format);
|
$sut = $this->getSut($format);
|
||||||
$model = new InvoiceModel(new DebugFormatter());
|
$model = new InvoiceModel(new DebugFormatter());
|
||||||
$model->setInvoiceDate($invoiceDate);
|
$model->setInvoiceDate($invoiceDate);
|
||||||
|
$model->setCustomer(new Customer());
|
||||||
$sut->setModel($model);
|
$sut->setModel($model);
|
||||||
|
|
||||||
$this->assertEquals($expectedInvoiceNumber, $sut->getInvoiceNumber());
|
$this->assertEquals($expectedInvoiceNumber, $sut->getInvoiceNumber());
|
||||||
|
|||||||
Reference in New Issue
Block a user