allow increasing invoice numbers per day (#2433)
This commit is contained in:
@@ -20,6 +20,17 @@ abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator imp
|
||||
{
|
||||
abstract protected function calculateSumIdentifier(InvoiceItemInterface $invoiceItem): string;
|
||||
|
||||
protected function calculateIdentifier(InvoiceItemInterface $entry): string
|
||||
{
|
||||
$prefix = $this->calculateSumIdentifier($entry);
|
||||
|
||||
if (null !== $entry->getFixedRate()) {
|
||||
return $prefix . '_fixed_' . (string) $entry->getFixedRate();
|
||||
}
|
||||
|
||||
return $prefix . '_hourly_' . (string) $entry->getHourlyRate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InvoiceItem[]
|
||||
*/
|
||||
@@ -34,13 +45,7 @@ abstract class AbstractSumInvoiceCalculator extends AbstractMergedCalculator imp
|
||||
$invoiceItems = [];
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$id = $this->calculateSumIdentifier($entry);
|
||||
|
||||
if (null !== $entry->getFixedRate()) {
|
||||
$id = $id . '_fixed_' . (string) $entry->getFixedRate();
|
||||
} else {
|
||||
$id = $id . '_hourly_' . (string) $entry->getHourlyRate();
|
||||
}
|
||||
$id = $this->calculateIdentifier($entry);
|
||||
|
||||
if (!isset($invoiceItems[$id])) {
|
||||
$invoiceItems[$id] = new InvoiceItem();
|
||||
|
||||
@@ -11,17 +11,27 @@ namespace App\Invoice\NumberGenerator;
|
||||
|
||||
use App\Invoice\InvoiceModel;
|
||||
use App\Invoice\NumberGeneratorInterface;
|
||||
use App\Repository\InvoiceRepository;
|
||||
|
||||
/**
|
||||
* Class DateNumberGenerator generates the invoice number based on the current day.
|
||||
* It will create duplicate IDs if you create multiple invoices per day.
|
||||
* It will create duplicate IDs if you create more then 99 invoices per day.
|
||||
*/
|
||||
class DateNumberGenerator implements NumberGeneratorInterface
|
||||
final class DateNumberGenerator implements NumberGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var InvoiceModel
|
||||
*/
|
||||
protected $model;
|
||||
private $model;
|
||||
/**
|
||||
* @var InvoiceRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(InvoiceRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
@@ -44,6 +54,17 @@ class DateNumberGenerator implements NumberGeneratorInterface
|
||||
*/
|
||||
public function getInvoiceNumber(): string
|
||||
{
|
||||
return date('ymd', $this->model->getInvoiceDate()->getTimestamp());
|
||||
$loops = 0;
|
||||
$increaseBy = 0;
|
||||
|
||||
$result = date('ymd', $this->model->getInvoiceDate()->getTimestamp());
|
||||
|
||||
// in the case that someone configured a weird format, that should not result in an endless loop
|
||||
while ($this->repository->hasInvoice($result) && $loops++ < 99) {
|
||||
$suffix = str_pad((string) ++$increaseBy, 2, '0', STR_PAD_LEFT);
|
||||
$result = date('ymd', $this->model->getInvoiceDate()->getTimestamp()) . '-' . $suffix;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user