Skip to content

Commit

Permalink
Added ability to pass custom template parameters
Browse files Browse the repository at this point in the history
to `InvoiceGenerator`

remp/crm#3142
  • Loading branch information
zoldia committed Apr 16, 2024
1 parent c55fa05 commit e2ffb6f
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions src/Models/Generator/InvoiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ class InvoiceGenerator
{
use RedisClientTrait;

/** @var string */
private $templateFile;
private string $templateFile;

/** @var string */
private $tempDir;
private array $templateParams = [];

private string $tempDir;

public function __construct(
private InvoicesRepository $invoicesRepository,
private PaymentsRepository $paymentsRepository,
private ApplicationConfig $applicationConfig,
private PriceHelper $priceHelper,
private Translator $translator,
private InvoiceNumberInterface $invoiceNumber,
private readonly InvoicesRepository $invoicesRepository,
private readonly PaymentsRepository $paymentsRepository,
private readonly ApplicationConfig $applicationConfig,
private readonly PriceHelper $priceHelper,
private readonly Translator $translator,
private readonly InvoiceNumberInterface $invoiceNumber,
RedisClientFactory $redisClientFactory,
private AddressesRepository $addressesRepository
private readonly AddressesRepository $addressesRepository
) {
$this->redisClientFactory = $redisClientFactory;
}

public function setTempDir(string $tempDir)
public function setTempDir(string $tempDir): void
{
if (!is_dir($tempDir)) {
Debugger::log("Providid temp dir {$tempDir} is not directory. System temp directory will be used.", Debugger::ERROR);
Debugger::log("Provided temp dir {$tempDir} is not directory. System temp directory will be used.", Debugger::ERROR);
return;
}
$this->tempDir = $tempDir;
Expand All @@ -60,7 +60,7 @@ public function getTempDir(): string
return $this->tempDir;
}

public function setTemplateFile(string $templateFile)
public function setTemplateFile(string $templateFile): void
{
if (!file_exists($templateFile)) {
Debugger::log("Unable to find provided invoice template file {$templateFile}. Default template will be used.", Debugger::ERROR);
Expand All @@ -78,6 +78,11 @@ public function getTemplateFile(): string
return $this->templateFile;
}

public function setTemplateParams(array $params): void
{
$this->templateParams = $params;
}

/**
* @throws PaymentNotInvoiceableException
* @throws InvoiceGenerationException
Expand Down Expand Up @@ -138,17 +143,26 @@ public function generate(ActiveRow $user, ActiveRow $payment): ?PdfResponse
return $this->renderInvoicePDF($user, $payment);
}

public function renderInvoicePDF($user, $payment)
/**
* @param $user
* @param $payment
* @return PdfResponse|null
* @throws InvoiceGenerationException
*/
public function renderInvoicePDF($user, $payment): ?PdfResponse
{
if ($payment->user->id == $user->id) {
if ($payment->user->id === $user->id) {
return $this->renderInvoice($payment);
}
return null;
}

public function renderInvoicePDFToFile($filePath, $user, $payment)
/**
* @throws InvoiceGenerationException|\Mpdf\MpdfException
*/
public function renderInvoicePDFToFile($filePath, $user, $payment): ?PdfResponse
{
if ($payment->user->id == $user->id) {
if ($payment->user->id === $user->id) {
$pdf = $this->renderInvoice($payment);
file_put_contents($filePath, $pdf->toString());
return $pdf;
Expand All @@ -158,12 +172,9 @@ public function renderInvoicePDFToFile($filePath, $user, $payment)


/**
* @param ActiveRow $payment
*
* @return PdfResponse
* @throws InvoiceGenerationException
*/
private function renderInvoice(ActiveRow $payment)
private function renderInvoice(ActiveRow $payment): PdfResponse
{
$invoice = $this->invoicesRepository->find($payment->invoice_id);
$engine = new Engine();
Expand All @@ -175,6 +186,7 @@ private function renderInvoice(ActiveRow $payment)
[
'invoice' => $invoice,
'config' => $this->applicationConfig,
'params' => $this->templateParams,
]
);

Expand All @@ -196,44 +208,43 @@ private function renderInvoice(ActiveRow $payment)
return $pdf;
}


/**
* Generates invoice PDF file as attachment.
*
* If invoice isn't generated for payment and user allowed invoicing, invoice will be generated and linked to payment.
*
* @param ActiveRow $payment
*
* @return array|bool Returns false if user disabled invoicing.
* @return array
* @throws PaymentNotInvoiceableException
* @throws InvoiceGenerationException
* @throws InvoiceGenerationException|RedisClientTraitException|\Mpdf\MpdfException
*/
public function renderInvoiceMailAttachment(ActiveRow $payment)
public function renderInvoiceMailAttachment(ActiveRow $payment): array
{
if (!$payment->invoice_id) {
$this->generate($payment->user, $payment);
$payment = $this->paymentsRepository->find($payment->id); // refresh the instance to get invoice ID
}

$attachment = [
return [
'file' => $payment->variable_symbol . '.pdf',
'content' => $this->generateInvoiceAsString($payment),
'mime_type' => 'application/pdf',
];

return $attachment;
}

/**
* Generates invoice PDF file and returns contents as string. Invoice must be already generated and linked to payment.
*
* @throws InvoiceGenerationException
* @throws \Mpdf\MpdfException
*/
public function generateInvoiceAsString(ActiveRow $payment): string
{
if ($payment->invoice_id === null) {
throw new InvoiceGenerationException("No linked invoice for payment VS {$payment->variable_symbol}. Cannot generate PDF attachment.");
}

$pdfResponse = $this->renderInvoice($payment);
return $pdfResponse->toString();
return $this->renderInvoice($payment)->toString();
}
}

0 comments on commit e2ffb6f

Please sign in to comment.