1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00

Working on statement templates

This commit is contained in:
David Bomba 2023-10-12 12:13:08 +11:00
parent 149637bb10
commit 1cf829b137
2 changed files with 95 additions and 56 deletions

View File

@ -30,12 +30,15 @@ use App\Services\PdfMaker\PdfMaker;
use App\Factory\InvoiceInvitationFactory; use App\Factory\InvoiceInvitationFactory;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait; use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
class Statement class Statement
{ {
use PdfMakerTrait; use PdfMakerTrait;
use MakesHash; use MakesHash;
use MakesDates;
/** /**
* @var Invoice|Payment|null * @var Invoice|Payment|null
*/ */
@ -55,12 +58,20 @@ class Statement
$html = new HtmlEngine($this->getInvitation()); $html = new HtmlEngine($this->getInvitation());
$variables = $html->generateLabelsAndValues(); $variables = [];
if($this->client->getSetting('statement_design_id') != '') { if($this->client->getSetting('statement_design_id') != '') {
$variables['values']['$start_date'] = $this->translateDate($this->options['start_date'], $this->client->date_format(), $this->client->locale());
$variables['values']['$end_date'] = $this->translateDate($this->options['end_date'], $this->client->date_format(), $this->client->locale());
$variables['labels']['$start_date_label'] = ctrans('texts.start_date');
$variables['labels']['$end_date_label'] = ctrans('texts.end_date');
return $this->templateStatement($variables); return $this->templateStatement($variables);
} }
$variables = $html->generateLabelsAndValues();
if ($this->getDesign()->is_custom) { if ($this->getDesign()->is_custom) {
$this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true); $this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true);
@ -119,7 +130,7 @@ class Statement
} }
private function templateStatement($variables) private function templateStatement($variables)
{nlog($this->options); {
if(isset($this->options['template'])) if(isset($this->options['template']))
$statement_design_id = $this->options['template']; $statement_design_id = $this->options['template'];
else else
@ -130,11 +141,11 @@ class Statement
->first(); ->first();
$ts = $template->service()->build([ $ts = $template->service()->build([
'variables' => $variables, 'variables' => collect([$variables]),
'invoices' => $this->getInvoices()->get(), 'invoices' => $this->getInvoices()->get(),
'payments' => $this->getPayments()->get(), 'payments' => $this->options['show_payments_table'] ? $this->getPayments()->get() : collect([]),
'credits' => $this->getCredits()->get(), 'credits' => $this->options['show_credits_table'] ? $this->getCredits()->get() : [],
'aging' => $this->getAging(), 'aging' => $this->options['show_aging_table'] ? $this->getAging() : [],
]); ]);
$html = $ts->getHtml(); $html = $ts->getHtml();
@ -305,7 +316,6 @@ class Statement
case 'unpaid': case 'unpaid':
return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]; return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL];
default: default:
return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]; return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID];
@ -337,7 +347,7 @@ class Statement
protected function getCredits(): Builder protected function getCredits(): Builder
{ {
return Credit::withTrashed() return Credit::withTrashed()
->with('client.country', 'invoices') ->with('client.country','invoice')
->where('is_deleted', false) ->where('is_deleted', false)
->where('company_id', $this->client->company_id) ->where('company_id', $this->client->company_id)
->where('client_id', $this->client->id) ->where('client_id', $this->client->id)
@ -386,7 +396,7 @@ class Statement
* @param mixed $range * @param mixed $range
* @return string * @return string
*/ */
private function getAgingAmount($range) private function getAgingAmount($range): string
{ {
$ranges = $this->calculateDateRanges($range); $ranges = $this->calculateDateRanges($range);

View File

@ -308,10 +308,11 @@ class TemplateService
$processed = []; $processed = [];
if(in_array($key, ['tasks','projects','aging']) || is_array($value) || !$value->first() ) if(in_array($key, ['tasks','projects','aging']) || !$value->first() )
return $processed; return $processed;
match ($key) { match ($key) {
'variables' => $processed = $value->first() ?? [],
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], 'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], 'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], 'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
@ -455,35 +456,6 @@ class TemplateService
})->toArray(); })->toArray();
} }
public function processInvoicesBak($invoices): array
{
$it = new InvoiceTransformer();
$it->setDefaultIncludes(['client','payments', 'credits']);
$manager = new Manager();
$manager->parseIncludes(['client','payments','payments.type','credits']);
$resource = new \League\Fractal\Resource\Collection($invoices, $it, null);
$invoices = $manager->createData($resource)->toArray();
foreach($invoices['data'] as $key => $invoice)
{
$invoices['data'][$key]['client'] = $invoice['client']['data'] ?? [];
$invoices['data'][$key]['client']['contacts'] = $invoice['client']['data']['contacts']['data'] ?? [];
$invoices['data'][$key]['payments'] = $invoice['payments']['data'] ?? [];
$invoices['data'][$key]['credits'] = $invoice['credits']['data'] ?? [];
if($invoice['payments']['data'] ?? false) {
foreach($invoice['payments']['data'] as $keyx => $payment) {
$invoices['data'][$key]['payments'][$keyx]['paymentables'] = $payment['paymentables']['data'] ?? [];
$invoices['data'][$key]['payments'][$keyx]['type'] = $payment['type']['data'] ?? [];
}
}
}
return $invoices['data'];
}
private function transformPayment(Payment $payment): array private function transformPayment(Payment $payment): array
{ {
@ -610,8 +582,6 @@ class TemplateService
} }
public function processQuotes($quotes): array public function processQuotes($quotes): array
{ {
$it = new QuoteTransformer(); $it = new QuoteTransformer();
@ -641,24 +611,75 @@ class TemplateService
*/ */
public function processCredits($credits): array public function processCredits($credits): array
{ {
$it = new CreditTransformer(); $credits = collect($credits)
$it->setDefaultIncludes(['client']); ->map(function ($credit){
$manager = new Manager();
$resource = new \League\Fractal\Resource\Collection($credits, $it, Credit::class);
$resources = $manager->createData($resource)->toArray();
foreach($resources['data'] as $key => $resource) { return [
'amount' => Number::formatMoney($credit->amount, $credit->client),
'balance' => Number::formatMoney($credit->balance, $credit->client),
'balance_raw' => $credit->balance,
'number' => $credit->number ?: '',
'discount' => $credit->discount,
'po_number' => $credit->po_number ?: '',
'date' => $this->translateDate($credit->date, $credit->client->date_format(), $credit->client->locale()),
'last_sent_date' => $this->translateDate($credit->last_sent_date, $credit->client->date_format(), $credit->client->locale()),
'next_send_date' => $this->translateDate($credit->next_send_date, $credit->client->date_format(), $credit->client->locale()),
'due_date' => $this->translateDate($credit->due_date, $credit->client->date_format(), $credit->client->locale()),
'terms' => $credit->terms ?: '',
'public_notes' => $credit->public_notes ?: '',
'private_notes' => $credit->private_notes ?: '',
'uses_inclusive_taxes' => (bool) $credit->uses_inclusive_taxes,
'tax_name1' => $credit->tax_name1 ?? '',
'tax_rate1' => (float) $credit->tax_rate1,
'tax_name2' => $credit->tax_name2 ?? '',
'tax_rate2' => (float) $credit->tax_rate2,
'tax_name3' => $credit->tax_name3 ?? '',
'tax_rate3' => (float) $credit->tax_rate3,
'total_taxes' => Number::formatMoney($credit->total_taxes, $credit->client),
'total_taxes_raw' => $credit->total_taxes,
'is_amount_discount' => (bool) $credit->is_amount_discount ?? false,
'footer' => $credit->footer ?? '',
'partial' => $credit->partial ?? 0,
'partial_due_date' => $this->translateDate($credit->partial_due_date, $credit->client->date_format(), $credit->client->locale()),
'custom_value1' => (string) $credit->custom_value1 ?: '',
'custom_value2' => (string) $credit->custom_value2 ?: '',
'custom_value3' => (string) $credit->custom_value3 ?: '',
'custom_value4' => (string) $credit->custom_value4 ?: '',
'custom_surcharge1' => (float) $credit->custom_surcharge1,
'custom_surcharge2' => (float) $credit->custom_surcharge2,
'custom_surcharge3' => (float) $credit->custom_surcharge3,
'custom_surcharge4' => (float) $credit->custom_surcharge4,
'exchange_rate' => (float) $credit->exchange_rate,
'custom_surcharge_tax1' => (bool) $credit->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool) $credit->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool) $credit->custom_surcharge_tax3,
'custom_surcharge_tax4' => (bool) $credit->custom_surcharge_tax4,
'line_items' => $credit->line_items ? $this->padLineItems($credit->line_items, $credit->client): (array) [],
'reminder1_sent' => $this->translateDate($credit->reminder1_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder2_sent' => $this->translateDate($credit->reminder2_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder3_sent' => $this->translateDate($credit->reminder3_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder_last_sent' => $this->translateDate($credit->reminder_last_sent, $credit->client->date_format(), $credit->client->locale()),
'paid_to_date' => Number::formatMoney($credit->paid_to_date, $credit->client),
'auto_bill_enabled' => (bool) $credit->auto_bill_enabled,
'client' => [
'name' => $credit->client->present()->name(),
'balance' => $credit->client->balance,
'payment_balance' => $credit->client->payment_balance,
'credit_balance' => $credit->client->credit_balance,
],
'payments' => [],
'total_tax_map' => $credit->calc()->getTotalTaxMap(),
'line_tax_map' => $credit->calc()->getTaxMap(),
];
$resources['data'][$key]['client'] = $resource['client']['data'] ?? []; });
$resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? [];
return $credits->toArray();
} }
return $resources['data'];
}
/** /**
* Pushes payments through the appropriate transformer * Pushes payments through the appropriate transformer
* *
@ -668,7 +689,7 @@ class TemplateService
public function processPayments($payments): array public function processPayments($payments): array
{ {
$payments = $payments->map(function ($payment) { $payments = collect($payments)->map(function ($payment) {
return $this->transformPayment($payment); return $this->transformPayment($payment);
})->toArray(); })->toArray();
@ -736,4 +757,12 @@ class TemplateService
{ {
return $this->company; return $this->company;
} }
public function overrideVariables($variables): self
{
$this->variables = $variables;
return $this;
}
} }