From ffb44f111df1dd129313d0e887800e17b2861720 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 5 Oct 2024 15:53:15 +1000 Subject: [PATCH] Improvements for imports --- app/Export/CSV/BaseExport.php | 2 +- app/Export/CSV/InvoiceItemExport.php | 31 +++++----------------- app/Import/Transformer/BaseTransformer.php | 30 +++++++++++++++------ phpunit.xml | 1 + 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index c7ff36cafe..1f953f8e07 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -1352,7 +1352,7 @@ class BaseExport */ public function mergeItemsKeys(string $entity_report_keys): array { - return array_merge($this->{$entity_report_keys}, $this->item_report_keys); + return array_merge(array_values($this->{$entity_report_keys}), array_values($this->item_report_keys)); } public function buildHeader(): array diff --git a/app/Export/CSV/InvoiceItemExport.php b/app/Export/CSV/InvoiceItemExport.php index 9b0ecb635f..02ab11b163 100644 --- a/app/Export/CSV/InvoiceItemExport.php +++ b/app/Export/CSV/InvoiceItemExport.php @@ -63,10 +63,13 @@ class InvoiceItemExport extends BaseExport if (count($this->input['report_keys']) == 0) { $this->force_keys = true; $this->input['report_keys'] = array_values($this->mergeItemsKeys('invoice_report_keys')); + nlog($this->input['report_keys']); } $this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys'])); + nlog("xx"); + nlog($this->input['report_keys']); $query = Invoice::query() ->withTrashed() ->with('client') @@ -189,7 +192,7 @@ class InvoiceItemExport extends BaseExport $entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity); - $this->storage_array[] = $entity; + $this->storage_array[] = $this->convertFloats($entity); } } @@ -200,6 +203,7 @@ class InvoiceItemExport extends BaseExport $entity = []; + foreach (array_values($this->input['report_keys']) as $key) { $parts = explode('.', $key); @@ -218,34 +222,11 @@ class InvoiceItemExport extends BaseExport } $entity = $this->decorateAdvancedFields($invoice, $entity); - return $this->convertFloats($entity); + return $entity; } private function decorateAdvancedFields(Invoice $invoice, array $entity): array { - // 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('tax_category', $entity)) { - // $entity['tax_category'] = $invoice->taxTypeString($entity['tax_category']); - // } - - // if (in_array('invoice.country_id', $this->input['report_keys'])) { - // $entity['invoice.country_id'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : ''; - // } - - // if (in_array('invoice.currency_id', $this->input['report_keys'])) { - // $entity['invoice.currency_id'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code; - // } - - // if (in_array('invoice.client_id', $this->input['report_keys'])) { - // $entity['invoice.client_id'] = $invoice->client->present()->name(); - // } - - // if (in_array('invoice.status', $this->input['report_keys'])) { - // $entity['invoice.status'] = $invoice->stringStatus($invoice->status_id); - // } if (in_array('invoice.recurring_id', $this->input['report_keys'])) { $entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? ''; diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index aef79cd25e..10683e30da 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -359,15 +359,29 @@ class BaseTransformer private function parseStringToFloat($data, $field): float { - $currency = $this->company->currency(); - - $amount = preg_replace('/[^\d,.-]/', '', $data[$field]); - $amount = str_replace($currency->thousand_separator, '', $amount); - - $amount = $currency->decimal_separator !== '.' ? str_replace($currency->decimal_separator, '.', $amount) : $amount; - - return (float) $amount; + $currency = $this->company->currency(); + $amount = $data[$field] ?? ''; + + // Remove any non-numeric characters except for the decimal and thousand separators + $amount = preg_replace('/[^\d' . preg_quote($currency->decimal_separator) . preg_quote($currency->thousand_separator) . '-]/', '', $amount); + + // Handle negative numbers + $isNegative = strpos($amount, '-') !== false; + $amount = str_replace('-', '', $amount); + + // Remove thousand separators + $amount = str_replace($currency->thousand_separator, '', $amount); + + // Replace decimal separator with a period if it's not already + if ($currency->decimal_separator !== '.') { + $amount = str_replace($currency->decimal_separator, '.', $amount); + } + + // Convert to float and apply negative sign if necessary + $result = (float) $amount; + return $isNegative ? -$result : $result; + } diff --git a/phpunit.xml b/phpunit.xml index 6347fa92a7..0304852db3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -33,6 +33,7 @@ +