From e1b5b8ef69f14a0ea0fc63eb72f470922ef1d9c8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 12 May 2023 10:52:06 +1000 Subject: [PATCH] Fixes for chart queries --- app/Services/Chart/ChartQueries.php | 38 ++++++++++++++++++++++++----- app/Services/Chart/ChartService.php | 1 + app/Services/Report/ProfitLoss.php | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index b41360ff6320..961a460a1001 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -86,9 +86,9 @@ trait ChartQueries IFNULL(payments.currency_id, :company_currency) AS currency_id FROM payments WHERE payments.status_id IN (4,5,6) - AND (payments.date BETWEEN :start_date AND :end_date) AND payments.company_id = :company_id AND payments.is_deleted = 0 + AND (payments.date BETWEEN :start_date AND :end_date) GROUP BY payments.date HAVING currency_id = :currency_id '), [ @@ -109,7 +109,7 @@ trait ChartQueries SELECT sum(invoices.balance) as amount, count(invoices.id) as outstanding_count, - IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id + IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id FROM clients JOIN invoices on invoices.client_id = clients.id @@ -128,8 +128,7 @@ trait ChartQueries return DB::select(DB::raw(" SELECT sum(invoices.paid_to_date) as paid_to_date, - sum(invoices.amount) as invoiced_amount, - IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id + IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id FROM clients JOIN invoices on invoices.client_id = clients.id @@ -143,21 +142,48 @@ trait ChartQueries "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]); } + public function getOutstandingChartQuery($start_date, $end_date, $currency_id) + { + return DB::select(DB::raw(" + SELECT + sum(invoices.balance) as total, + invoices.date, + IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id + FROM clients + JOIN invoices + on invoices.client_id = clients.id + WHERE invoices.status_id IN (2,3,4) + AND invoices.company_id = :company_id + AND clients.is_deleted = 0 + AND invoices.is_deleted = 0 + AND (invoices.date BETWEEN :start_date AND :end_date) + GROUP BY invoices.date + HAVING currency_id = :currency_id + "), [ + 'company_currency' => (int) $this->company->settings->currency_id, + 'currency_id' => $currency_id, + 'company_id' => $this->company->id, + 'start_date' => $start_date, + 'end_date' => $end_date, + ]); + } + + public function getInvoiceChartQuery($start_date, $end_date, $currency_id) { return DB::select(DB::raw(" SELECT sum(invoices.amount) as total, invoices.date, - IFNULL(CAST(JSON_EXTRACT( settings, '$.currency_id' ) AS SIGNED), :company_currency) AS currency_id + IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id FROM clients JOIN invoices on invoices.client_id = clients.id WHERE invoices.status_id IN (2,3,4) - AND (invoices.date BETWEEN :start_date AND :end_date) AND invoices.company_id = :company_id AND clients.is_deleted = 0 AND invoices.is_deleted = 0 + AND (invoices.date BETWEEN :start_date AND :end_date) GROUP BY invoices.date HAVING currency_id = :currency_id "), [ diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index 6c7c9914e82f..3b8262e369b6 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -75,6 +75,7 @@ class ChartService foreach ($currencies as $key => $value) { $data[$key]['invoices'] = $this->getInvoiceChartQuery($start_date, $end_date, $key); + // $data[$key]['outstanding'] = $this->getOutstandingChartQuery($start_date, $end_date, $key); $data[$key]['payments'] = $this->getPaymentChartQuery($start_date, $end_date, $key); $data[$key]['expenses'] = $this->getExpenseChartQuery($start_date, $end_date, $key); } diff --git a/app/Services/Report/ProfitLoss.php b/app/Services/Report/ProfitLoss.php index 2415a9b02444..12ecddacb2a5 100644 --- a/app/Services/Report/ProfitLoss.php +++ b/app/Services/Report/ProfitLoss.php @@ -237,7 +237,7 @@ class ProfitLoss sum(invoices.total_taxes) as total_taxes, (sum(invoices.total_taxes) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_taxes, sum(invoices.amount - invoices.total_taxes) as net_amount, - IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id, + IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id (sum(invoices.amount - invoices.total_taxes) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_amount FROM clients JOIN invoices