From 252d9a37455799f1b28e21a841ae3c844da8a981 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 May 2022 17:04:24 +1000 Subject: [PATCH] Recurring invoice export CSV --- app/Export/CSV/InvoiceExport.php | 2 + app/Export/CSV/RecurringInvoiceExport.php | 73 +++++++---------------- 2 files changed, 24 insertions(+), 51 deletions(-) diff --git a/app/Export/CSV/InvoiceExport.php b/app/Export/CSV/InvoiceExport.php index f6c4559b9d..76ecf16a18 100644 --- a/app/Export/CSV/InvoiceExport.php +++ b/app/Export/CSV/InvoiceExport.php @@ -71,6 +71,8 @@ class InvoiceExport extends BaseExport 'client', 'currency_id', 'status', + 'vendor', + 'project', ]; public function __construct(Company $company, array $input) diff --git a/app/Export/CSV/RecurringInvoiceExport.php b/app/Export/CSV/RecurringInvoiceExport.php index 81738e80d5..47232ebcfa 100644 --- a/app/Export/CSV/RecurringInvoiceExport.php +++ b/app/Export/CSV/RecurringInvoiceExport.php @@ -63,49 +63,11 @@ class RecurringInvoiceExport extends BaseExport 'tax_rate3' => 'tax_rate3', 'terms' => 'terms', 'total_taxes' => 'total_taxes', - 'currency' => 'client_id', + 'currency' => 'currency_id', 'vendor' => 'vendor_id', 'project' => 'project_id', ]; - protected array $all_keys = [ - 'amount', - 'balance', - 'client_id', - 'custom_surcharge1', - 'custom_surcharge2', - 'custom_surcharge3', - 'custom_surcharge4', - 'custom_value1', - 'custom_value2', - 'custom_value3', - 'custom_value4', - 'date', - 'discount', - 'due_date', - 'exchange_rate', - 'footer', - 'number', - 'paid_to_date', - 'partial', - 'partial_due_date', - 'po_number', - 'private_notes', - 'public_notes', - 'status_id', - 'tax_name1', - 'tax_name2', - 'tax_name3', - 'tax_rate1', - 'tax_rate2', - 'tax_rate3', - 'terms', - 'total_taxes', - 'client_id', - 'vendor_id', - 'project_id', - ]; - private array $decorate_keys = [ 'country', 'client', @@ -135,7 +97,7 @@ class RecurringInvoiceExport extends BaseExport $this->csv = Writer::createFromString(); if(count($this->input['report_keys']) == 0) - $this->input['report_keys'] = $this->all_keys; + $this->input['report_keys'] = array_values($this->entity_keys); //insert the header $this->csv->insertOne($this->buildHeader()); @@ -167,7 +129,13 @@ class RecurringInvoiceExport extends BaseExport foreach(array_values($this->input['report_keys']) as $key){ - $entity[$key] = $transformed_invoice[$key]; + $keyval = array_search($key, $this->entity_keys); + + if(array_key_exists($key, $transformed_invoice)) + $entity[$keyval] = $transformed_invoice[$key]; + else + $entity[$keyval] = ''; + } return $this->decorateAdvancedFields($invoice, $entity); @@ -176,20 +144,23 @@ class RecurringInvoiceExport extends BaseExport private function decorateAdvancedFields(RecurringInvoice $invoice, array $entity) :array { - if(array_key_exists('currency', $entity)) - $entity['currency'] = $invoice->client->currency()->code; + if(in_array('country_id', $this->input['report_keys'])) + $entity['country'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : ""; - if(array_key_exists('client_id', $entity)) - $entity['client_id'] = $invoice->client->present()->name(); + if(in_array('currency_id', $this->input['report_keys'])) + $entity['currency'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code; - if(array_key_exists('status_id', $entity)) - $entity['status_id'] = $invoice->stringStatus($invoice->status_id); + if(in_array('client_id', $this->input['report_keys'])) + $entity['client'] = $invoice->client->present()->name(); - if(array_key_exists('vendor_id', $entity)) - $entity['vendor_id'] = $invoice->vendor()->exists() ? $invoice->vendor->name : ''; + if(in_array('status_id',$this->input['report_keys'])) + $entity['status'] = $invoice->stringStatus($invoice->status_id); - if(array_key_exists('project_id', $entity)) - $entity['project'] = $invoice->project()->exists() ? $invoice->project->name : ''; + if(in_array('project_id',$this->input['report_keys'])) + $entity['project'] = $invoice->project ? $invoice->project->name : ""; + + if(in_array('vendor_id',$this->input['report_keys'])) + $entity['vendor'] = $invoice->vendor ? $invoice->vendor->name : ""; return $entity; }