Fixes for chart queries

This commit is contained in:
David Bomba 2023-05-12 10:52:06 +10:00
parent 52cac9ddea
commit e1b5b8ef69
3 changed files with 34 additions and 7 deletions

View File

@ -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
"), [

View File

@ -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);
}

View File

@ -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