Improvements for imports

This commit is contained in:
David Bomba 2024-10-05 15:53:15 +10:00
parent a49438945b
commit ffb44f111d
4 changed files with 30 additions and 34 deletions

View File

@ -1352,7 +1352,7 @@ class BaseExport
*/ */
public function mergeItemsKeys(string $entity_report_keys): array 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 public function buildHeader(): array

View File

@ -63,10 +63,13 @@ class InvoiceItemExport extends BaseExport
if (count($this->input['report_keys']) == 0) { if (count($this->input['report_keys']) == 0) {
$this->force_keys = true; $this->force_keys = true;
$this->input['report_keys'] = array_values($this->mergeItemsKeys('invoice_report_keys')); $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'])); $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() $query = Invoice::query()
->withTrashed() ->withTrashed()
->with('client') ->with('client')
@ -189,7 +192,7 @@ class InvoiceItemExport extends BaseExport
$entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity); $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 = []; $entity = [];
foreach (array_values($this->input['report_keys']) as $key) { foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key); $parts = explode('.', $key);
@ -218,34 +222,11 @@ class InvoiceItemExport extends BaseExport
} }
$entity = $this->decorateAdvancedFields($invoice, $entity); $entity = $this->decorateAdvancedFields($invoice, $entity);
return $this->convertFloats($entity); return $entity;
} }
private function decorateAdvancedFields(Invoice $invoice, array $entity): array 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'])) { if (in_array('invoice.recurring_id', $this->input['report_keys'])) {
$entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? ''; $entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? '';

View File

@ -359,15 +359,29 @@ class BaseTransformer
private function parseStringToFloat($data, $field): float private function parseStringToFloat($data, $field): float
{ {
$currency = $this->company->currency();
$amount = preg_replace('/[^\d,.-]/', '', $data[$field]); $currency = $this->company->currency();
$amount = $data[$field] ?? '';
$amount = str_replace($currency->thousand_separator, '', $amount); // 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);
$amount = $currency->decimal_separator !== '.' ? str_replace($currency->decimal_separator, '.', $amount) : $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;
return (float) $amount;
} }

View File

@ -33,6 +33,7 @@
<env name="DB_CONNECTION" value="sqlite"/> <env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/> <env name="DB_DATABASE" value=":memory:"/>
<env name="DB_STRICT" value="false"/> <env name="DB_STRICT" value="false"/>
<env name="SCOUT_DRIVER" value=null/>
</php> </php>
<logging/> <logging/>
</phpunit> </phpunit>