2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2016-08-15 15:43:26 +02:00
|
|
|
use stdClass;
|
2015-03-26 07:24:02 +01:00
|
|
|
use Auth;
|
2015-03-17 02:30:56 +01:00
|
|
|
use DB;
|
2015-03-26 07:24:02 +01:00
|
|
|
use View;
|
2016-09-11 16:30:23 +02:00
|
|
|
use Utils;
|
|
|
|
use App\Models\Client;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Models\Invoice;
|
2015-07-29 21:55:12 +02:00
|
|
|
use App\Models\Payment;
|
2016-08-15 05:46:47 +02:00
|
|
|
use App\Ninja\Repositories\DashboardRepository;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class DashboardController
|
|
|
|
*/
|
2015-03-26 07:24:02 +01:00
|
|
|
class DashboardController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-08-15 05:46:47 +02:00
|
|
|
public function __construct(DashboardRepository $dashboardRepo)
|
|
|
|
{
|
|
|
|
$this->dashboardRepo = $dashboardRepo;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2016-08-15 05:46:47 +02:00
|
|
|
$user = Auth::user();
|
|
|
|
$viewAll = $user->hasPermission('view_all');
|
|
|
|
$userId = $user->id;
|
2016-09-11 16:30:23 +02:00
|
|
|
$account = $user->account;
|
|
|
|
$accountId = $account->id;
|
2016-08-15 05:46:47 +02:00
|
|
|
|
|
|
|
$dashboardRepo = $this->dashboardRepo;
|
|
|
|
$metrics = $dashboardRepo->totals($accountId, $userId, $viewAll);
|
2016-10-31 10:11:30 +01:00
|
|
|
$paidToDate = $dashboardRepo->paidToDate($account, $userId, $viewAll);
|
|
|
|
$averageInvoice = $dashboardRepo->averages($account, $userId, $viewAll);
|
|
|
|
$balances = $dashboardRepo->balances($accountId, $userId, $viewAll);
|
2016-08-15 05:46:47 +02:00
|
|
|
$activities = $dashboardRepo->activities($accountId, $userId, $viewAll);
|
|
|
|
$pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll);
|
|
|
|
$upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll);
|
|
|
|
$payments = $dashboardRepo->payments($accountId, $userId, $viewAll);
|
2016-09-11 16:30:23 +02:00
|
|
|
$expenses = $dashboardRepo->expenses($accountId, $userId, $viewAll);
|
|
|
|
$tasks = $dashboardRepo->tasks($accountId, $userId, $viewAll);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-09-11 16:30:23 +02:00
|
|
|
// check if the account has quotes
|
2015-10-06 19:55:55 +02:00
|
|
|
$hasQuotes = false;
|
|
|
|
foreach ([$upcoming, $pastDue] as $data) {
|
|
|
|
foreach ($data as $invoice) {
|
2016-05-26 16:56:54 +02:00
|
|
|
if ($invoice->invoice_type_id == INVOICE_TYPE_QUOTE) {
|
2015-10-06 19:55:55 +02:00
|
|
|
$hasQuotes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-09-11 16:30:23 +02:00
|
|
|
// check if the account has multiple curencies
|
|
|
|
$currencyIds = $account->currency_id ? [$account->currency_id] : [DEFAULT_CURRENCY];
|
|
|
|
$data = Client::scope()
|
|
|
|
->withArchived()
|
|
|
|
->distinct()
|
|
|
|
->get(['currency_id'])
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
array_map(function ($item) use (&$currencyIds) {
|
|
|
|
$currencyId = intval($item['currency_id']);
|
|
|
|
if ($currencyId && ! in_array($currencyId, $currencyIds)) {
|
|
|
|
$currencyIds[] = $currencyId;
|
|
|
|
}
|
|
|
|
}, $data);
|
|
|
|
|
|
|
|
$currencies = [];
|
|
|
|
foreach ($currencyIds as $currencyId) {
|
|
|
|
$currencies[$currencyId] = Utils::getFromCache($currencyId, 'currencies')->code;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = [
|
2016-08-15 05:46:47 +02:00
|
|
|
'account' => $user->account,
|
2015-08-20 17:09:04 +02:00
|
|
|
'paidToDate' => $paidToDate,
|
|
|
|
'balances' => $balances,
|
|
|
|
'averageInvoice' => $averageInvoice,
|
|
|
|
'invoicesSent' => $metrics ? $metrics->invoices_sent : 0,
|
|
|
|
'activeClients' => $metrics ? $metrics->active_clients : 0,
|
|
|
|
'activities' => $activities,
|
|
|
|
'pastDue' => $pastDue,
|
|
|
|
'upcoming' => $upcoming,
|
|
|
|
'payments' => $payments,
|
|
|
|
'title' => trans('texts.dashboard'),
|
2015-10-06 19:55:55 +02:00
|
|
|
'hasQuotes' => $hasQuotes,
|
2016-09-11 16:30:23 +02:00
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'currencies' => $currencies,
|
|
|
|
'expenses' => $expenses,
|
|
|
|
'tasks' => $tasks,
|
2015-08-20 17:09:04 +02:00
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return View::make('dashboard', $data);
|
|
|
|
}
|
2016-09-11 16:30:23 +02:00
|
|
|
|
|
|
|
public function chartData($groupBy, $startDate, $endDate, $currencyCode, $includeExpenses)
|
|
|
|
{
|
|
|
|
$includeExpenses = filter_var($includeExpenses, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
$data = $this->dashboardRepo->chartData(Auth::user()->account, $groupBy, $startDate, $endDate, $currencyCode, $includeExpenses);
|
|
|
|
|
|
|
|
return json_encode($data);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|