diff --git a/app/Http/Controllers/ChartController.php b/app/Http/Controllers/ChartController.php index aedb958f18a7..2d3518ef484b 100644 --- a/app/Http/Controllers/ChartController.php +++ b/app/Http/Controllers/ChartController.php @@ -72,7 +72,14 @@ class ChartController extends BaseController { $cs = new ChartService(auth()->user()->company()); - return response()->json($cs->totals($request->input('start_date'), $request->input('end_date')),200); + return response()->json($cs->totals($request->input('start_date'), $request->input('end_date')), 200); + } + + public function chart_summary(ShowChartRequest $request) + { + $cs = new ChartService(auth()->user()->company()); + + return response()->json($cs->chart_summary($request->input('start_date'), $request->input('end_date')), 200); } } diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index b3d3f53de10b..6e874cd5c87e 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -98,37 +98,79 @@ trait ChartQueries "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); } - public function getChartData($start_date, $end_date, $group_by = 'DAYOFYEAR', $entity_type = 'invoice', $currency_id) + public function getInvoiceChartQuery($start_date, $end_date, $currency_id) { - if(!$currency_id) - $currency_id = $this->company->settings->currency_id; - - return DB::select( DB::raw(" + return DB::select( DB::raw(" SELECT - sum(invoices.balance) as balance, - sum(invoices.amount) as amount, - IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id + -- sum(invoices.balance) as balance, + sum(invoices.amount) as total, + invoices.date, + IFNULL(CAST(JSON_EXTRACT( 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 - GROUP BY :group_by "), [ + 'company_currency' => $this->company->settings->currency_id, 'currency_id' => $currency_id, - 'group_by' => $group_by, - 'company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date ]); + } - + public function getPaymentChartQuery($start_date, $end_date, $currency_id) + { + + return DB::select( DB::raw(" + SELECT + sum(payments.amount - payments.refunded) as total, + payments.date, + 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 + GROUP BY payments.date + HAVING currency_id = :currency_id + "), [ + 'company_currency' => $this->company->settings->currency_id, + 'currency_id' => $currency_id, + 'company_id' => $this->company->id, + 'start_date' => $start_date, + 'end_date' => $end_date + ]); + } + + public function getExpenseChartQuery($start_date, $end_date, $currency_id) + { + + return DB::select( DB::raw(" + SELECT + sum(expenses.amount) as total, + expenses.date, + IFNULL(expenses.currency_id, :company_currency) AS currency_id + FROM expenses + WHERE (expenses.date BETWEEN :start_date AND :end_date) + AND expenses.company_id = :company_id + AND expenses.is_deleted = 0 + GROUP BY expenses.date + HAVING currency_id = :currency_id + "), [ + 'company_currency' => $this->company->settings->currency_id, + 'currency_id' => $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 db932763986f..65907280be9f 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -70,13 +70,24 @@ class ChartService } -/* Payments */ - public function payments($start_date, $end_date) +/* Chart Data */ + public function chart_summary($start_date, $end_date) :array { - $payments = $this->getPaymentQuery(); + $currencies = $this->getCurrencyCodes(); + + $data = []; + + foreach($currencies as $key => $value) + { + $data[$key]['invoices'] = $this->getInvoiceChartQuery($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); + } + + return $data; } -/* Payments */ +/* Chart Data */ /* Totals */ @@ -85,33 +96,42 @@ class ChartService $data = []; $data['currencies'] = $this->getCurrencyCodes(); - $data['revenue'] = $this->getRevenue($start_date, $end_date); - $data['outstanding'] = $this->getOutstanding($start_date, $end_date); - $data['expenses'] = $this->getExpenses($start_date, $end_date); + + foreach($data['currencies'] as $key => $value) + { + $revenue = $this->getRevenue($start_date, $end_date); + $outstanding = $this->getOutstanding($start_date, $end_date); + $expenses = $this->getExpenses($start_date, $end_date); + + $data[$key]['revenue'] = count($revenue) > 0 ? $revenue[array_search($key,array_column($revenue,'currency_id'))] : new \stdClass; + $data[$key]['outstanding'] = count($outstanding) > 0 ? $outstanding[array_search($key,array_column($outstanding,'currency_id'))] : new \stdClass; + $data[$key]['expenses'] = count($expenses) > 0 ? $expenses[array_search($key,array_column($expenses,'currency_id'))] : new \stdClass; + + } return $data; } - private function getRevenue($start_date, $end_date) :array + public function getRevenue($start_date, $end_date) :array { $revenue = $this->getRevenueQuery($start_date, $end_date); - $revenue = $this->addCountryCodes($revenue); + $revenue = $this->addCurrencyCodes($revenue); return $revenue; } - private function getOutstanding($start_date, $end_date) :array + public function getOutstanding($start_date, $end_date) :array { $outstanding = $this->getOutstandingQuery($start_date, $end_date); - $outstanding = $this->addCountryCodes($outstanding); + $outstanding = $this->addCurrencyCodes($outstanding); return $outstanding; } - private function getExpenses($start_date, $end_date) :array + public function getExpenses($start_date, $end_date) :array { $expenses = $this->getExpenseQuery($start_date, $end_date); - $expenses = $this->addCountryCodes($expenses); + $expenses = $this->addCurrencyCodes($expenses); return $expenses; } @@ -120,7 +140,7 @@ class ChartService /* Helpers */ - private function addCountryCodes($data_set) :array + private function addCurrencyCodes($data_set) :array { $currencies = Cache::get('currencies'); diff --git a/routes/api.php b/routes/api.php index a3c7936229fc..e7f71619a675 100644 --- a/routes/api.php +++ b/routes/api.php @@ -33,6 +33,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale Route::post('charts/totals', 'ChartController@totals')->name('chart.totals'); + Route::post('charts/chart_summary', 'ChartController@chart_summary')->name('chart.chart_summary'); Route::post('claim_license', 'LicenseController@index')->name('license.index');