diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 26a6c4b2ae..c7ff36cafe 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -1632,4 +1632,18 @@ class BaseExport { return \Illuminate\Support\Facades\Schema::hasColumn($table, $column); } + + public function convertFloats(iterable $entity): iterable + { + $currency = $this->company->currency(); + + foreach ($entity as $key => $value) { + if (is_float($value)) { + $entity[$key] = \App\Utils\Number::formatValue($value, $currency); + } + } + + return $entity; + + } } diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index a3b77eed9f..941f0987eb 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -190,7 +190,8 @@ class ClientExport extends BaseExport } } - return $this->decorateAdvancedFields($client, $entity); + $entity = $this->decorateAdvancedFields($client, $entity); + $this->convertFloats($entity); } public function processMetaData(array $row, $resource): array diff --git a/app/Export/CSV/CreditExport.php b/app/Export/CSV/CreditExport.php index 4c82e62d6f..364d431b5f 100644 --- a/app/Export/CSV/CreditExport.php +++ b/app/Export/CSV/CreditExport.php @@ -167,16 +167,13 @@ class CreditExport extends BaseExport } elseif(isset($transformed_credit[$searched_credit_key])) { $entity[$keyval] = $transformed_credit[$searched_credit_key]; } else { - - // nlog($key); $entity[$key] = $this->decorator->transform($key, $credit); - // $entity[$key] = ''; - // $entity[$keyval] = $this->resolveKey($keyval, $credit, $this->credit_transformer); } } - return $this->decorateAdvancedFields($credit, $entity); + $entity = $this->decorateAdvancedFields($credit, $entity); + return $this->convertFloats($entity); } public function addCreditStatusFilter($query, $status): Builder diff --git a/app/Export/CSV/ExpenseExport.php b/app/Export/CSV/ExpenseExport.php index e18c598d22..a0f2189da2 100644 --- a/app/Export/CSV/ExpenseExport.php +++ b/app/Export/CSV/ExpenseExport.php @@ -162,7 +162,8 @@ class ExpenseExport extends BaseExport } - return $this->decorateAdvancedFields($expense, $entity); + $entity = $this->decorateAdvancedFields($expense, $entity); + return $this->convertFloats($entity); } protected function addExpenseStatusFilter($query, $status): Builder diff --git a/app/Export/CSV/InvoiceExport.php b/app/Export/CSV/InvoiceExport.php index 73a0d47a44..c17a42fd16 100644 --- a/app/Export/CSV/InvoiceExport.php +++ b/app/Export/CSV/InvoiceExport.php @@ -147,7 +147,8 @@ class InvoiceExport extends BaseExport } - return $this->decorateAdvancedFields($invoice, $entity); + $entity = $this->decorateAdvancedFields($invoice, $entity); + return $this->convertFloats($entity); } private function decorateAdvancedFields(Invoice $invoice, array $entity): array diff --git a/app/Export/CSV/InvoiceItemExport.php b/app/Export/CSV/InvoiceItemExport.php index 8657baed6c..9b0ecb635f 100644 --- a/app/Export/CSV/InvoiceItemExport.php +++ b/app/Export/CSV/InvoiceItemExport.php @@ -213,14 +213,12 @@ class InvoiceItemExport extends BaseExport } elseif (array_key_exists($key, $transformed_invoice)) { $entity[$key] = $transformed_invoice[$key]; } else { - // nlog($key); $entity[$key] = $this->decorator->transform($key, $invoice); - // $entity[$key] = ''; - // $entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer); } } - // return $entity; - return $this->decorateAdvancedFields($invoice, $entity); + + $entity = $this->decorateAdvancedFields($invoice, $entity); + return $this->convertFloats($entity); } private function decorateAdvancedFields(Invoice $invoice, array $entity): array diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index a646bfc31a..8f3bd06cd2 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -137,63 +137,17 @@ class PaymentExport extends BaseExport } elseif (array_key_exists($key, $transformed_entity)) { $entity[$key] = $transformed_entity[$key]; } else { - - // nlog($key); $entity[$key] = $this->decorator->transform($key, $payment); - // $entity[$key] = $this->resolveKey($key, $payment, $this->entity_transformer); } } - // return $entity; - return $this->decorateAdvancedFields($payment, $entity); + $entity = $this->decorateAdvancedFields($payment, $entity); + return $this->convertFloats($entity); } private function decorateAdvancedFields(Payment $payment, array $entity): array { - // if (in_array('status_id', $this->input['report_keys'])) { - // $entity['status'] = $payment->stringStatus($payment->status_id); - // } - - // if (in_array('vendor_id', $this->input['report_keys'])) { - // $entity['vendor'] = $payment->vendor()->exists() ? $payment->vendor->name : ''; - // } - - // if (in_array('project_id', $this->input['report_keys'])) { - // $entity['project'] = $payment->project()->exists() ? $payment->project->name : ''; - // } - - // if (in_array('currency_id', $this->input['report_keys'])) { - // $entity['currency'] = $payment->currency()->exists() ? $payment->currency->code : ''; - // } - - // if (in_array('payment.currency', $this->input['report_keys'])) { - // $entity['payment.currency'] = $payment->currency()->exists() ? $payment->currency->code : ''; - // } - - // if (in_array('exchange_currency_id', $this->input['report_keys'])) { - // $entity['exchange_currency'] = $payment->exchange_currency()->exists() ? $payment->exchange_currency->code : ''; - // } - - // if (in_array('client_id', $this->input['report_keys'])) { - // $entity['client'] = $payment->client->present()->name(); - // } - - // if (in_array('type_id', $this->input['report_keys'])) { - // $entity['type'] = $payment->translatedType(); - // } - - // if (in_array('payment.method', $this->input['report_keys'])) { - // $entity['payment.method'] = $payment->translatedType(); - // } - - // if (in_array('payment.status', $this->input['report_keys'])) { - // $entity['payment.status'] = $payment->stringStatus($payment->status_id); - // } - - // if (in_array('gateway_type_id', $this->input['report_keys'])) { - // $entity['gateway'] = $payment->gateway_type ? $payment->gateway_type->name : 'Unknown Type'; - // } if (in_array('payment.assigned_user_id', $this->input['report_keys'])) { $entity['payment.assigned_user_id'] = $payment->assigned_user ? $payment->assigned_user->present()->name() : ''; @@ -203,8 +157,6 @@ class PaymentExport extends BaseExport $entity['payment.user_id'] = $payment->user ? $payment->user->present()->name() : ''; } - // $entity['invoices'] = $payment->invoices()->exists() ? $payment->invoices->pluck('number')->implode(',') : ''; - return $entity; } } diff --git a/app/Export/CSV/ProductExport.php b/app/Export/CSV/ProductExport.php index e377a30233..90f0b2999b 100644 --- a/app/Export/CSV/ProductExport.php +++ b/app/Export/CSV/ProductExport.php @@ -125,27 +125,11 @@ class ProductExport extends BaseExport if (array_key_exists($key, $transformed_entity)) { $entity[$keyval] = $transformed_entity[$key]; } else { - // nlog($key); $entity[$key] = $this->decorator->transform($key, $product); - // $entity[$key] = ''; - } } - return $entity; - // return $this->decorateAdvancedFields($product, $entity); + return $this->convertFloats($entity); } - // private function decorateAdvancedFields(Product $product, array $entity): array - // { - // if (in_array('vendor_id', $this->input['report_keys'])) { - // $entity['vendor'] = $product->vendor()->exists() ? $product->vendor->name : ''; - // } - - // // if (array_key_exists('project_id', $this->input['report_keys'])) { - // // $entity['project'] = $product->project()->exists() ? $product->project->name : ''; - // // } - - // return $entity; - // } } diff --git a/app/Export/CSV/ProductSalesExport.php b/app/Export/CSV/ProductSalesExport.php index 77f33108e2..46f980ee2a 100644 --- a/app/Export/CSV/ProductSalesExport.php +++ b/app/Export/CSV/ProductSalesExport.php @@ -66,12 +66,6 @@ class ProductSalesExport extends BaseExport 'custom_value4' => 'custom_value4', ]; - // private array $decorate_keys = [ - // 'client', - // 'currency', - // 'date', - // ]; - public function __construct(Company $company, array $input) { $this->company = $company; @@ -233,6 +227,8 @@ class ProductSalesExport extends BaseExport } $entity = $this->decorateAdvancedFields($invoice, $entity); + $entity = $this->convertFloats($entity); + $this->sales->push($entity); return $entity; diff --git a/app/Export/CSV/PurchaseOrderExport.php b/app/Export/CSV/PurchaseOrderExport.php index a80f05c0e1..5b1d8023bd 100644 --- a/app/Export/CSV/PurchaseOrderExport.php +++ b/app/Export/CSV/PurchaseOrderExport.php @@ -142,17 +142,16 @@ class PurchaseOrderExport extends BaseExport if (is_array($parts) && $parts[0] == 'purchase_order' && array_key_exists($parts[1], $transformed_purchase_order)) { $entity[$key] = $transformed_purchase_order[$parts[1]]; } else { - nlog($key); $entity[$key] = $this->decorator->transform($key, $purchase_order); - // $entity[$key] = ''; - - // $entity[$key] = $this->resolveKey($key, $purchase_order, $this->purchase_order_transformer); } } - // return $entity; - return $this->decorateAdvancedFields($purchase_order, $entity); + + $entity = $this->decorateAdvancedFields($purchase_order, $entity); + + return $this->convertFloats($entity); + } private function decorateAdvancedFields(PurchaseOrder $purchase_order, array $entity): array diff --git a/app/Export/CSV/PurchaseOrderItemExport.php b/app/Export/CSV/PurchaseOrderItemExport.php index ea7e82d0bb..876e5674f1 100644 --- a/app/Export/CSV/PurchaseOrderItemExport.php +++ b/app/Export/CSV/PurchaseOrderItemExport.php @@ -197,33 +197,19 @@ class PurchaseOrderItemExport extends BaseExport } elseif (array_key_exists($key, $transformed_purchase_order)) { $entity[$key] = $transformed_purchase_order[$key]; } else { - // nlog($key); $entity[$key] = $this->decorator->transform($key, $purchase_order); - // $entity[$key] = ''; - // $entity[$key] = $this->resolveKey($key, $purchase_order, $this->purchase_order_transformer); } } - return $this->decorateAdvancedFields($purchase_order, $entity); + $entity = $this->decorateAdvancedFields($purchase_order, $entity); + $entity = $this->convertFloats($entity); + return $entity; + } private function decorateAdvancedFields(PurchaseOrder $purchase_order, array $entity): array { - // if (in_array('currency_id', $this->input['report_keys'])) { - // $entity['currency'] = $purchase_order->vendor->currency() ? $purchase_order->vendor->currency()->code : $purchase_order->company->currency()->code; - // } - - // if(array_key_exists('tax_category', $entity)) { - // $entity['tax_category'] = $purchase_order->taxTypeString($entity['tax_category']); - // } - - // if($this->force_keys) { - // $entity['vendor'] = $purchase_order->vendor->present()->name(); - // $entity['vendor_id_number'] = $purchase_order->vendor->id_number; - // $entity['vendor_number'] = $purchase_order->vendor->number; - // $entity['status'] = $purchase_order->stringStatus($purchase_order->status_id); - // } - + if (in_array('purchase_order.currency_id', $this->input['report_keys'])) { $entity['purchase_order.currency_id'] = $purchase_order->vendor->currency() ? $purchase_order->vendor->currency()->code : $purchase_order->company->currency()->code; } diff --git a/app/Export/CSV/QuoteExport.php b/app/Export/CSV/QuoteExport.php index 4b2881fe7b..fbdcb11d4e 100644 --- a/app/Export/CSV/QuoteExport.php +++ b/app/Export/CSV/QuoteExport.php @@ -31,12 +31,6 @@ class QuoteExport extends BaseExport private Decorator $decorator; - // private array $decorate_keys = [ - // 'client', - // 'currency', - // 'invoice', - // ]; - public function __construct(Company $company, array $input) { $this->company = $company; @@ -148,15 +142,15 @@ class QuoteExport extends BaseExport if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_invoice)) { $entity[$key] = $transformed_invoice[$parts[1]]; } else { - // nlog($key); $entity[$key] = $this->decorator->transform($key, $quote); - // $entity[$key] = ''; - // $entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer); } } - // return $entity; - return $this->decorateAdvancedFields($quote, $entity); + + $entity = $this->decorateAdvancedFields($quote, $entity); + return $this->convertFloats($entity); + + } private function decorateAdvancedFields(Quote $quote, array $entity): array diff --git a/app/Export/CSV/QuoteItemExport.php b/app/Export/CSV/QuoteItemExport.php index 62e4931338..c11135b306 100644 --- a/app/Export/CSV/QuoteItemExport.php +++ b/app/Export/CSV/QuoteItemExport.php @@ -152,6 +152,7 @@ class QuoteItemExport extends BaseExport $transformed_quote = $this->buildRow($quote); $transformed_items = []; + $currency = $this->company->currency(); foreach ($quote->line_items as $item) { $item_array = []; @@ -180,7 +181,8 @@ class QuoteItemExport extends BaseExport $transformed_items = array_merge($transformed_quote, $item_array); $entity = $this->decorateAdvancedFields($quote, $transformed_items); - $entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity); + $entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity); + $entity = $this->convertFloats($entity, $currency); $this->storage_array[] = $entity; } @@ -211,22 +213,12 @@ class QuoteItemExport extends BaseExport } } - // return $entity; - return $this->decorateAdvancedFields($quote, $entity); + $entity = $this->decorateAdvancedFields($quote, $entity); + return $this->convertFloats($entity); + } private function decorateAdvancedFields(Quote $quote, array $entity): array { - // if (in_array('currency_id', $this->input['report_keys'])) { - // $entity['currency'] = $quote->client->currency() ? $quote->client->currency()->code : $quote->company->currency()->code; - // } - - // if (in_array('client_id', $this->input['report_keys'])) { - // $entity['client'] = $quote->client->present()->name(); - // } - - // if (in_array('status_id', $this->input['report_keys'])) { - // $entity['status'] = $quote->stringStatus($quote->status_id); - // } if (in_array('quote.assigned_user_id', $this->input['report_keys'])) { $entity['quote.assigned_user_id'] = $quote->assigned_user ? $quote->assigned_user->present()->name() : ''; diff --git a/app/Export/CSV/RecurringInvoiceExport.php b/app/Export/CSV/RecurringInvoiceExport.php index 01ba57eb5a..7d228dcdc3 100644 --- a/app/Export/CSV/RecurringInvoiceExport.php +++ b/app/Export/CSV/RecurringInvoiceExport.php @@ -129,6 +129,7 @@ class RecurringInvoiceExport extends BaseExport $transformed_invoice = $this->invoice_transformer->transform($invoice); $entity = []; + $currency = $this->company->currency(); foreach (array_values($this->input['report_keys']) as $key) { @@ -139,12 +140,12 @@ class RecurringInvoiceExport extends BaseExport } elseif($parts[0] == 'item') { $entity[$key] = ''; } else { - // nlog($key); $entity[$key] = $this->decorator->transform($key, $invoice); - // $entity[$key] = ''; - // $entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer); } + if (is_float($entity[$key])) + $entity[$key] = \App\Utils\Number::formatValue($entity[$key], $currency); + } // return $entity; @@ -153,29 +154,6 @@ class RecurringInvoiceExport extends BaseExport private function decorateAdvancedFields(RecurringInvoice $invoice, array $entity): array { - // if (in_array('country_id', $this->input['report_keys'])) { - // $entity['country'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : ''; - // } - - // if (in_array('currency_id', $this->input['report_keys'])) { - // $entity['currency'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code; - // } - - // if (in_array('client_id', $this->input['report_keys'])) { - // $entity['client'] = $invoice->client->present()->name(); - // } - - // if (in_array('recurring_invoice.status', $this->input['report_keys'])) { - // $entity['recurring_invoice.status'] = $invoice->stringStatus($invoice->status_id); - // } - - // 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 : ''; - // } if (in_array('recurring_invoice.frequency_id', $this->input['report_keys']) || in_array('frequency_id', $this->input['report_keys'])) { $entity['recurring_invoice.frequency_id'] = $invoice->frequencyForKey($invoice->frequency_id); diff --git a/app/Export/CSV/TaskExport.php b/app/Export/CSV/TaskExport.php index 3a7d6f99cd..2df0cea676 100644 --- a/app/Export/CSV/TaskExport.php +++ b/app/Export/CSV/TaskExport.php @@ -164,6 +164,8 @@ class TaskExport extends BaseExport } + $entity = $this->convertFloats($entity); + if (is_null($task->time_log) || (is_array(json_decode($task->time_log, true)) && count(json_decode($task->time_log, true)) == 0)) { $this->storage_array[] = $entity; } else { @@ -221,7 +223,7 @@ class TaskExport extends BaseExport } $entity = $this->decorateAdvancedFields($task, $entity); - + $this->storage_array[] = $entity; $entity['task.start_date'] = ''; diff --git a/app/Services/Report/ARDetailReport.php b/app/Services/Report/ARDetailReport.php index 68d2dd11be..ab69edc258 100644 --- a/app/Services/Report/ARDetailReport.php +++ b/app/Services/Report/ARDetailReport.php @@ -125,8 +125,8 @@ class ARDetailReport extends BaseExport $client->number, $client->id_number, intval(abs(Carbon::parse($invoice->due_date)->diffInDays(now()))), - Number::formatMoney($invoice->amount, $client), - Number::formatMoney($invoice->balance, $client), + Number::formatMoney($invoice->amount, $this->company), + Number::formatMoney($invoice->balance, $this->company), ]; } diff --git a/app/Services/Report/ARSummaryReport.php b/app/Services/Report/ARSummaryReport.php index 3ba32b3bea..9105556de3 100644 --- a/app/Services/Report/ARSummaryReport.php +++ b/app/Services/Report/ARSummaryReport.php @@ -113,7 +113,7 @@ class ARSummaryReport extends BaseExport $this->getAgingAmount('90'), $this->getAgingAmount('120'), $this->getAgingAmount('120+'), - Number::formatMoney($this->total, $this->client), + Number::formatMoney($this->total, $this->company), ]; $this->total = 0; diff --git a/app/Services/Report/ClientBalanceReport.php b/app/Services/Report/ClientBalanceReport.php index bf9cbf5c9d..82b161b3f3 100644 --- a/app/Services/Report/ClientBalanceReport.php +++ b/app/Services/Report/ClientBalanceReport.php @@ -11,15 +11,16 @@ namespace App\Services\Report; -use App\Export\CSV\BaseExport; -use App\Libraries\MultiDB; +use App\Utils\Ninja; +use App\Utils\Number; use App\Models\Client; +use League\Csv\Writer; use App\Models\Company; use App\Models\Invoice; -use App\Utils\Ninja; +use App\Libraries\MultiDB; +use App\Export\CSV\BaseExport; use App\Utils\Traits\MakesDates; use Illuminate\Support\Facades\App; -use League\Csv\Writer; class ClientBalanceReport extends BaseExport { @@ -119,8 +120,8 @@ class ClientBalanceReport extends BaseExport $client->id_number, $query->count(), $query->sum('balance'), - $client->credit_balance, - $client->payment_balance, + Number::formatMoney($client->credit_balance, $this->company), + Number::formatMoney($client->payment_balance, $this->company), ]; } } diff --git a/app/Services/Report/ClientSalesReport.php b/app/Services/Report/ClientSalesReport.php index 2e88dcccc1..2a31f52b7b 100644 --- a/app/Services/Report/ClientSalesReport.php +++ b/app/Services/Report/ClientSalesReport.php @@ -114,10 +114,10 @@ class ClientSalesReport extends BaseExport $client->number, $client->id_number, $query->count(), - Number::formatMoney($amount, $client), - Number::formatMoney($balance, $client), - Number::formatMoney($query->sum('total_taxes'), $client), - Number::formatMoney($amount - $balance, $client), + Number::formatMoney($amount, $this->company), + Number::formatMoney($balance, $this->company), + Number::formatMoney($query->sum('total_taxes'), $this->company), + Number::formatMoney($amount - $balance, $this->company), ]; }