Templates + Statements

This commit is contained in:
David Bomba 2023-11-23 16:13:06 +11:00
parent 8ac5c7cf65
commit 4c60d47446
4 changed files with 69 additions and 7 deletions

View File

@ -158,7 +158,12 @@ class Statement
->where('company_id', $this->client->company_id)
->first();
$ts = $template->service()->build([
$ts = $template->service();
$ts->addGlobal(['show_credits' => $this->options['show_credits_table']]);
$ts->addGlobal(['show_aging' => $this->options['show_aging_table']]);
$ts->addGlobal(['show_payments' => $this->options['show_payments_table']]);
$ts->build([
'variables' => collect([$variables]),
'invoices' => $this->getInvoices()->get(),
'payments' => $this->options['show_payments_table'] ? $this->getPayments()->get() : collect([]),
@ -400,6 +405,7 @@ class Statement
protected function getAging(): array
{
return [
ctrans('texts.current') => $this->getAgingAmount('0'),
'0-30' => $this->getAgingAmount('30'),
'30-60' => $this->getAgingAmount('60'),
'60-90' => $this->getAgingAmount('90'),
@ -421,14 +427,23 @@ class Statement
$from = $ranges[0];
$to = $ranges[1];
$amount = Invoice::withTrashed()
$query = Invoice::withTrashed()
->where('client_id', $this->client->id)
->where('company_id', $this->client->company_id)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
->where('is_deleted', 0)
->whereBetween('due_date', [$to, $from])
->sum('balance');
->where('is_deleted', 0);
if($range == '0'){
$query->where(function ($q) use($to, $from){
$q->whereBetween('due_date', [$to, $from])->orWhereNull('due_date');
});
}
else {
$query->whereBetween('due_date', [$to, $from]);
}
$amount = $query->sum('balance');
return Number::formatMoney($amount, $this->client);
}
@ -444,6 +459,10 @@ class Statement
$ranges = [];
switch ($range) {
case '0':
$ranges[0] = now()->subYears(50);
$ranges[1] = now()->startOfDay()->subMinute();
return $ranges;
case '30':
$ranges[0] = now()->startOfDay();
$ranges[1] = now()->startOfDay()->subDays(30);

View File

@ -483,12 +483,15 @@ class PdfMock
'_rate3' => '',
'$taxes' => '$40.00',
'$total' => '$10.00',
'$refund' => '',
'$refunded' => '',
'$phone' => ' ',
'$terms' => 'Default company invoice terms',
'$from' => 'Bob Jones',
'$item' => '',
'$date' => '25/Feb/2023',
'$tax' => '',
'$net' => 'Net',
'$dir' => 'ltr',
'$to' => 'Jimmy Giggles',
'$show_paid_stamp' => $this->settings->show_paid_stamp ? 'flex' : 'none',
@ -803,6 +806,8 @@ class PdfMock
'$tax_rate1_label' => ctrans('texts.tax_rate1'),
'$tax_rate2_label' => ctrans('texts.tax_rate2'),
'$tax_rate3_label' => ctrans('texts.tax_rate3'),
'$refund_label' => ctrans('texts.refund'),
'$refunded_label' => ctrans('texts.refunded'),
'$phone_label' => ctrans('texts.phone'),
'$email_label' => ctrans('texts.email'),
'$taxes_label' => ctrans('texts.taxes'),
@ -811,6 +816,7 @@ class PdfMock
'$item_label' => ctrans('texts.item'),
'$date_label' => ctrans('texts.date'),
'$tax_label' => ctrans('texts.tax'),
'$net_label' => ctrans('texts.net'),
'$dir_label' => '',
'$to_label' => ctrans('texts.to'),
'$start_date_label' => ctrans('texts.start_date'),
@ -1115,6 +1121,8 @@ class PdfMock
'$tax_rate1' => '',
'$tax_rate2' => '',
'$tax_rate3' => '',
'$refund' => 'Refund',
'$refunded' => 'Refunded',
'$total' => '$10,256.40',
'$taxes' => '$488.40',
'$phone' => ' ',
@ -1123,6 +1131,7 @@ class PdfMock
'$date' => '14/Mar/2023',
'$tax' => '',
'$dir' => 'ltr',
'$net' => 'Net',
'$to' => '',
],
'labels' => $this->vendorLabels(),
@ -1334,6 +1343,8 @@ class PdfMock
'$tax_rate1_label' => ctrans('texts.tax_rate1'),
'$tax_rate2_label' => ctrans('texts.tax_rate2'),
'$tax_rate3_label' => ctrans('texts.tax_rate3'),
'$refund_label' => ctrans('texts.refund'),
'$refunded_label' => ctrans('texts.refunded'),
'$total_label' => ctrans('texts.total'),
'$taxes_label' => ctrans('texts.taxes'),
'$phone_label' => ctrans('texts.phone'),
@ -1341,6 +1352,7 @@ class PdfMock
'$item_label' => ctrans('texts.item'),
'$date_label' => ctrans('texts.date'),
'$tax_label' => ctrans('texts.tax'),
'$net_label' => ctrans('texts.net'),
'$dir_label' => '',
'$to_label' => ctrans('texts.to'),
];

View File

@ -53,6 +53,8 @@ class TemplateService
private array $variables = [];
private array $global_vars = [];
public ?Company $company;
private ?Client $client;
@ -124,6 +126,7 @@ class TemplateService
{
$this->compose()
->processData($data)
->setGlobals()
->parseNinjaBlocks()
->processVariables($data)
->parseGlobalStacks()
@ -145,6 +148,25 @@ class TemplateService
return $this;
}
private function setGlobals(): self
{
foreach($this->global_vars as $key => $value) {
$this->twig->addGlobal($key, $value);
}
$this->global_vars = [];
return $this;
}
public function addGlobal(array $var): self
{
$this->global_vars = array_merge($this->global_vars, $var);
return $this;
}
/**
* Returns a Mock Template
*
@ -159,6 +181,10 @@ class TemplateService
$this->data = $tm->engines;
$this->variables = $tm->variables[0];
$this->twig->addGlobal('currency_code', $this->company->currency()->code);
$this->twig->addGlobal('show_credits', true);
$this->twig->addGlobal('show_aging', true);
$this->twig->addGlobal('show_payments', true);
$this->parseNinjaBlocks()
->parseGlobalStacks()
@ -550,6 +576,8 @@ class TemplateService
$this->payment = $payment;
$this->addGlobal(['currency_code' => $payment->currency->code ?? $this->company->currency()->code]);
$credits = $payment->credits->map(function ($credit) use ($payment) {
return [
'credit' => $credit->number,
@ -827,11 +855,11 @@ class TemplateService
*/
public function processPayments($payments): array
{
nlog("processing payments");
$payments = collect($payments)->map(function ($payment) {
return $this->transformPayment($payment);
})->toArray();
nlog($payments);
return $payments;
}

View File

@ -166,6 +166,7 @@ class HtmlEngine
$data['$exchange_rate'] = ['value' => $this->entity->exchange_rate ?: ' ', 'label' => ctrans('texts.exchange_rate')];
$data['$triangular_tax'] = ['value' => ctrans('texts.triangular_tax'), 'label' => ''];
$data['$tax_info'] = ['value' => $this->taxLabel(), 'label' => ''];
$data['$net'] = ['value' => '', 'label' => ctrans('texts.net')];
if ($this->entity_string == 'invoice' || $this->entity_string == 'recurring_invoice') {
$data['$entity'] = ['value' => ctrans('texts.invoice'), 'label' => ctrans('texts.invoice')];
@ -664,6 +665,8 @@ class HtmlEngine
$data['$payment.custom2'] = ['value' => '', 'label' => ctrans('texts.payment')];
$data['$payment.custom3'] = ['value' => '', 'label' => ctrans('texts.payment')];
$data['$payment.custom4'] = ['value' => '', 'label' => ctrans('texts.payment')];
$data['$refund'] = ['value' => '', 'label' => ctrans('texts.refund')];
$data['$refunded'] = ['value' => '', 'label' => ctrans('texts.refunded')];
$data['$payment.amount'] = ['value' => '', 'label' => ctrans('texts.payment')];
$data['$payment.date'] = ['value' => '', 'label' => ctrans('texts.payment_date')];