diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index 9e96587260f6..bb438a25ab04 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -31,7 +31,7 @@ trait ChartQueries return DB::select( DB::raw(" SELECT sum(invoices.paid_to_date) as paid_to_date, - JSON_EXTRACT( settings, '$.currency_id' ) AS currency_id + IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id FROM clients JOIN invoices on invoices.client_id = clients.id @@ -42,7 +42,7 @@ trait ChartQueries AND invoices.is_deleted = 0 AND (invoices.date BETWEEN :start_date AND :end_date) GROUP BY currency_id - "), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); + "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); } @@ -52,7 +52,7 @@ trait ChartQueries return DB::select( DB::raw(" SELECT sum(invoices.balance) as balance, - JSON_EXTRACT( settings, '$.currency_id' ) AS currency_id + IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id FROM clients JOIN invoices on invoices.client_id = clients.id @@ -63,7 +63,7 @@ trait ChartQueries AND invoices.is_deleted = 0 AND (invoices.due_date BETWEEN :start_date AND :end_date) GROUP BY currency_id - "), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); + "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); } @@ -72,13 +72,13 @@ trait ChartQueries return DB::select( DB::raw(" SELECT sum(expenses.amount) as amount, - expenses.currency_id as currency_id + IFNULL(expenses.currency_id, :company_currency) as currency_id FROM expenses WHERE expenses.is_deleted = 0 AND expenses.company_id = :company_id AND (expenses.date BETWEEN :start_date AND :end_date) GROUP BY currency_id - "), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); + "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); } diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index 320b766fca70..9a65f7ad17ba 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -70,8 +70,6 @@ class ChartService } - - public function totals($start_date, $end_date) :array { $data = []; @@ -114,7 +112,6 @@ class ChartService private function getRevenue($start_date, $end_date) :array { $revenue = $this->getRevenueQuery($start_date, $end_date); - $revenue = $this->parseTotals($revenue); $revenue = $this->addCountryCodes($revenue); return $revenue; @@ -123,7 +120,6 @@ class ChartService private function getOutstanding($start_date, $end_date) :array { $outstanding = $this->getOutstandingQuery($start_date, $end_date); - $outstanding = $this->parseTotals($outstanding); $outstanding = $this->addCountryCodes($outstanding); return $outstanding; @@ -132,34 +128,11 @@ class ChartService private function getExpenses($start_date, $end_date) :array { $expenses = $this->getExpenseQuery($start_date, $end_date); - $expenses = $this->parseTotals($expenses); $expenses = $this->addCountryCodes($expenses); + return $expenses; } - private function parseTotals($data_set) :array - { - /* Find the key where the company currency amount lives*/ - $c_key = array_search($this->company->id , array_column($data_set, 'currency_id')); - - if(!$c_key) - return $data_set; - - /* Find the key where null currency_id lives */ - $key = array_search(null , array_column($data_set, 'currency_id')); - - if(!$key) - return $data_set; - - $null_currency_amount = $data_set[$key]['amount']; - unset($data_set[$key]); - - $data_set[$c_key]['amount'] += $null_currency_amount; - - return $data_set; - - } - private function addCountryCodes($data_set) :array { @@ -177,7 +150,7 @@ class ChartService private function getCode($currencies, $currency_id) :string { - nlog($currency_id); + $currency_id = str_replace('"', '', $currency_id); $currency = $currencies->filter(function ($item) use($currency_id) { return $item->id == $currency_id; diff --git a/tests/Unit/Chart/ChartCurrencyTest.php b/tests/Unit/Chart/ChartCurrencyTest.php index a18b07d3d1a6..c664839cd67e 100644 --- a/tests/Unit/Chart/ChartCurrencyTest.php +++ b/tests/Unit/Chart/ChartCurrencyTest.php @@ -52,11 +52,11 @@ class ChartCurrencyTest extends TestCase $this->assertDatabaseHas('invoices', ['number' => 'db_record']); $cs = new ChartService($this->company); - nlog($cs->getRevenueQuery(now()->subDays(20)->format('Y-m-d'), now()->addDays(100)->format('Y-m-d'))); + // nlog($cs->getRevenueQuery(now()->subDays(20)->format('Y-m-d'), now()->addDays(100)->format('Y-m-d'))); $data = [ 'start_date' => now()->subDays(30)->format('Y-m-d'), - 'end_date' => now()->addDay()->format('Y-m-d') + 'end_date' => now()->addDays(100)->format('Y-m-d') ]; $response = $this->withHeaders([ @@ -66,8 +66,6 @@ class ChartCurrencyTest extends TestCase $response->assertStatus(200); - nlog($response->json()); - } @@ -141,7 +139,7 @@ class ChartCurrencyTest extends TestCase $cs = new ChartService($this->company); - nlog($cs->totals(now()->subYears(10), now())); + // nlog($cs->totals(now()->subYears(10), now())); $this->assertTrue(is_array($cs->totals(now()->subYears(10), now())));