use configured language for non-twig invoice templates (#1924)
This commit is contained in:
@@ -9,24 +9,19 @@
|
||||
|
||||
namespace App\Invoice;
|
||||
|
||||
use App\Twig\DateExtensions;
|
||||
use App\Twig\LocaleExtensions;
|
||||
use App\Configuration\LanguageFormattings;
|
||||
use App\Utils\LocaleFormatter;
|
||||
|
||||
final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
{
|
||||
/**
|
||||
* @var DateExtensions
|
||||
* @var LocaleFormatter
|
||||
*/
|
||||
private $dateExtension;
|
||||
/**
|
||||
* @var LocaleExtensions
|
||||
*/
|
||||
private $extension;
|
||||
private $formatter;
|
||||
|
||||
public function __construct(DateExtensions $dateExtension, LocaleExtensions $extensions)
|
||||
public function __construct(LanguageFormattings $formats, string $locale)
|
||||
{
|
||||
$this->dateExtension = $dateExtension;
|
||||
$this->extension = $extensions;
|
||||
$this->formatter = new LocaleFormatter($formats, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +30,7 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedDateTime(\DateTime $date)
|
||||
{
|
||||
return $this->dateExtension->dateShort($date);
|
||||
return $this->formatter->dateShort($date);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +39,7 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedTime(\DateTime $date)
|
||||
{
|
||||
return $this->dateExtension->time($date);
|
||||
return $this->formatter->time($date);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +48,7 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedMonthName(\DateTime $date)
|
||||
{
|
||||
return $this->dateExtension->monthName($date);
|
||||
return $this->formatter->monthName($date);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +59,7 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true)
|
||||
{
|
||||
return $this->extension->money($amount, $currency, $withCurrency);
|
||||
return $this->formatter->money($amount, $currency, $withCurrency);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +68,7 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedDuration($seconds)
|
||||
{
|
||||
return $this->extension->duration($seconds);
|
||||
return $this->formatter->duration($seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,11 +77,11 @@ final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedDecimalDuration($seconds)
|
||||
{
|
||||
return $this->extension->durationDecimal($seconds);
|
||||
return $this->formatter->durationDecimal($seconds);
|
||||
}
|
||||
|
||||
public function getCurrencySymbol(string $currency): string
|
||||
{
|
||||
return $this->extension->currency($currency);
|
||||
return $this->formatter->currency($currency);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class InvoiceModelDefaultHydrator implements InvoiceModelHydrator
|
||||
$subtotal = $model->getCalculator()->getSubtotal();
|
||||
$formatter = $model->getFormatter();
|
||||
|
||||
$values = [
|
||||
return [
|
||||
'invoice.due_date' => $formatter->getFormattedDateTime($model->getDueDate()),
|
||||
'invoice.date' => $formatter->getFormattedDateTime($model->getInvoiceDate()),
|
||||
'invoice.number' => $model->getInvoiceNumber(),
|
||||
@@ -67,7 +67,5 @@ class InvoiceModelDefaultHydrator implements InvoiceModelHydrator
|
||||
'query.end_month_number' => $model->getQuery()->getEnd()->format('m'), // since 1.9
|
||||
'query.end_year' => $model->getQuery()->getEnd()->format('Y'), // since 1.9
|
||||
];
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,22 +9,24 @@
|
||||
|
||||
namespace App\Invoice;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* @internal this is subject to change
|
||||
*/
|
||||
interface InvoiceFormatter
|
||||
{
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @param DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFormattedDateTime(\DateTime $date);
|
||||
public function getFormattedDateTime(DateTime $date);
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @param DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFormattedTime(\DateTime $date);
|
||||
public function getFormattedTime(DateTime $date);
|
||||
|
||||
/**
|
||||
* @param int|float $amount
|
||||
@@ -35,10 +37,10 @@ interface InvoiceFormatter
|
||||
public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true);
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @param DateTime $date
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFormattedMonthName(\DateTime $date);
|
||||
public function getFormattedMonthName(DateTime $date);
|
||||
|
||||
/**
|
||||
* @param int $seconds
|
||||
@@ -52,5 +54,11 @@ interface InvoiceFormatter
|
||||
*/
|
||||
public function getFormattedDecimalDuration($seconds);
|
||||
|
||||
/**
|
||||
* Returns the currency symbol for the given currency by name.
|
||||
*
|
||||
* @param string $currency
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrencySymbol(string $currency): string;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
namespace App\Invoice;
|
||||
|
||||
use App\Configuration\LanguageFormattings;
|
||||
use App\Constants;
|
||||
use App\Entity\Invoice;
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Event\InvoiceCreatedEvent;
|
||||
@@ -57,7 +59,7 @@ final class ServiceInvoice
|
||||
*/
|
||||
private $dateTimeFactory;
|
||||
/**
|
||||
* @var InvoiceFormatter
|
||||
* @var LanguageFormattings
|
||||
*/
|
||||
private $formatter;
|
||||
/**
|
||||
@@ -65,7 +67,7 @@ final class ServiceInvoice
|
||||
*/
|
||||
private $invoiceRepository;
|
||||
|
||||
public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, UserDateTimeFactory $dateTimeFactory, InvoiceFormatter $formatter)
|
||||
public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, UserDateTimeFactory $dateTimeFactory, LanguageFormattings $formatter)
|
||||
{
|
||||
$this->documents = $repository;
|
||||
$this->fileHelper = $fileHelper;
|
||||
@@ -417,8 +419,20 @@ final class ServiceInvoice
|
||||
*/
|
||||
public function createModel(InvoiceQuery $query): InvoiceModel
|
||||
{
|
||||
$model = new InvoiceModel($this->formatter);
|
||||
$template = $query->getTemplate();
|
||||
|
||||
if (null === $template) {
|
||||
throw new \Exception('Cannot create invoice model without template');
|
||||
}
|
||||
|
||||
if (null === $template->getLanguage()) {
|
||||
$template->setLanguage(Constants::DEFAULT_LOCALE);
|
||||
@trigger_error('Using invoice templates without a language is is deprecated and trigger and will throw an exception with 2.0', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$model = new InvoiceModel(new DefaultInvoiceFormatter($this->formatter, $template->getLanguage()));
|
||||
$model
|
||||
->setTemplate($template)
|
||||
->setInvoiceDate($this->dateTimeFactory->createDateTime())
|
||||
->setQuery($query)
|
||||
;
|
||||
@@ -431,22 +445,19 @@ final class ServiceInvoice
|
||||
$model->setCustomer($query->getCustomers()[0]);
|
||||
}
|
||||
|
||||
if ($query->getTemplate() !== null) {
|
||||
$generator = $this->getNumberGeneratorByName($query->getTemplate()->getNumberGenerator());
|
||||
if (null === $generator) {
|
||||
throw new \Exception('Unknown number generator: ' . $query->getTemplate()->getNumberGenerator());
|
||||
}
|
||||
|
||||
$calculator = $this->getCalculatorByName($query->getTemplate()->getCalculator());
|
||||
if (null === $calculator) {
|
||||
throw new \Exception('Unknown invoice calculator: ' . $query->getTemplate()->getCalculator());
|
||||
}
|
||||
|
||||
$model->setTemplate($query->getTemplate());
|
||||
$model->setCalculator($calculator);
|
||||
$model->setNumberGenerator($generator);
|
||||
$generator = $this->getNumberGeneratorByName($query->getTemplate()->getNumberGenerator());
|
||||
if (null === $generator) {
|
||||
throw new \Exception('Unknown number generator: ' . $query->getTemplate()->getNumberGenerator());
|
||||
}
|
||||
|
||||
$calculator = $this->getCalculatorByName($query->getTemplate()->getCalculator());
|
||||
if (null === $calculator) {
|
||||
throw new \Exception('Unknown invoice calculator: ' . $query->getTemplate()->getCalculator());
|
||||
}
|
||||
|
||||
$model->setCalculator($calculator);
|
||||
$model->setNumberGenerator($generator);
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user