From cf9c463426703ebc751c0e4e8e3eeb014793c55a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 19 Feb 2025 11:47:18 +1100 Subject: [PATCH] templates --- app/Services/Template/TemplateService.php | 65 ++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index fbd974fd9b..4c0d68bd82 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -983,6 +983,27 @@ class TemplateService ] : []; } + private function getVendor($entity): array + { + + return $entity->vendor ? [ + 'name' => $entity->vendor->present()->name(), + 'phone' => $entity->vendor->present()->phone(), + 'website' => $entity->vendor->website ?? '', + 'number' => $entity->vendor->number ?? '', + 'id_number' => $entity->vendor->id_number ?? '', + 'vat_number' => $entity->vendor->vat_number ?? '', + 'currency' => $entity->vendor->currency()->code ?? 'USD', + 'custom_value1' => $entity->vendor->custom_value1 ?? '', + 'custom_value2' => $entity->vendor->custom_value2 ?? '', + 'custom_value3' => $entity->vendor->custom_value3 ?? '', + 'custom_value4' => $entity->vendor->custom_value4 ?? '', + 'address' => $entity->vendor->present()->address(), + 'shipping_address' => $entity->vendor->present()->shipping_address(), + 'locale' => substr($entity->vendor->locale(), 0, 2), + ] : []; + } + private function processInvoiceTask(string $task_id): array { $task = Task::where('company_id', $this->company->id) @@ -1011,6 +1032,47 @@ class TemplateService ] : []; } + /** + * + * @param \App\Models\Expense[] $expenses + * @return array + */ + public function processExpenses($expenses, bool $nested = false): array + { + + return collect($expenses)->map(function ($expense) use ($nested) { + /** @var \App\Models\Expense $expense */ + return [ + 'category' => $expense->category ? $expense->category->name : '', + 'amount' => Number::formatMoney($expense->amount, $expense->client ?? $expense->company), + 'amount_raw' => $expense->amount, + 'date' => $expense->date ? $this->translateDate($expense->date, $expense->client->date_format(), $expense->client->locale()) : '', + 'private_notes' => (string) $expense->private_notes ?: '', + 'public_notes' => (string) $expense->public_notes ?: '', + 'exchange_rate' => (float) $expense->exchange_rate, + 'tax_name1' => $expense->tax_name1 ?: '', + 'tax_rate1' => (float) $expense->tax_rate1, + 'tax_name2' => $expense->tax_name2 ?: '', + 'tax_rate2' => (float) $expense->tax_rate2, + 'tax_name3' => $expense->tax_name3 ?: '', + 'tax_rate3' => (float) $expense->tax_rate3, + 'tax_amount1' => (float) $expense->tax_amount1, + 'tax_amount2' => (float) $expense->tax_amount2, + 'tax_amount3' => (float) $expense->tax_amount3, + 'payment_date' => $expense->payment_date ? $this->translateDate($expense->payment_date, $expense->client->date_format(), $expense->client->locale()) : '', + 'transaction_reference' => $expense->transaction_reference ?: '', + 'custom_value1' => $expense->custom_value1 ?: '', + 'custom_value2' => $expense->custom_value2 ?: '', + 'custom_value3' => $expense->custom_value3 ?: '', + 'custom_value4' => $expense->custom_value4 ?: '', + 'calculate_tax_by_amount' => (bool) $expense->calculate_tax_by_amount, + 'uses_inclusive_taxes' => (bool) $expense->uses_inclusive_taxes, + 'client' => $this->getClient($expense), + 'vendor' => $this->getVendor($expense), + 'project' => ($expense->project && !$nested) ? $this->transformProject($expense->project, true) : [], + ]; + }); + } /** * @todo refactor @@ -1102,7 +1164,8 @@ class TemplateService 'client' => $this->getClient($project), 'user' => $this->userInfo($project->user), 'assigned_user' => $project->assigned_user ? $this->userInfo($project->assigned_user) : [], - 'invoices' => $this->processInvoices($project->invoices) + 'invoices' => $this->processInvoices($project->invoices), + 'expenses' => ($project->expenses && !$nested) ? $this->processExpenses($project->expenses, true) : [], ]; }