1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/DashboardController.php

165 lines
5.9 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2015-03-17 02:30:56 +01:00
2016-09-11 16:30:23 +02:00
use App\Models\Client;
use App\Models\Expense;
2016-08-15 05:46:47 +02:00
use App\Ninja\Repositories\DashboardRepository;
2017-01-30 20:40:43 +01:00
use Auth;
use Utils;
use View;
2015-03-16 22:45:25 +01:00
/**
2017-01-30 20:40:43 +01: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;
}
/**
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function index()
{
2018-01-14 20:40:53 +01:00
//dd(dispatch(new \App\Jobs\ConvertInvoiceToUbl(\App\Models\Invoice::find(51))));
2018-01-11 08:58:43 +01:00
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);
$paidToDate = $dashboardRepo->paidToDate($account, $userId, $viewAll);
$averageInvoice = $dashboardRepo->averages($account, $userId, $viewAll);
2016-11-16 11:41:32 +01:00
$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);
$expenses = $dashboardRepo->expenses($account, $userId, $viewAll);
2016-09-11 16:30:23 +02:00
$tasks = $dashboardRepo->tasks($accountId, $userId, $viewAll);
2016-11-16 11:41:32 +01:00
2017-10-15 11:54:43 +02:00
$showBlueVinePromo = false;
if ($user->is_admin && env('BLUEVINE_PARTNER_UNIQUE_ID')) {
$showBlueVinePromo = ! $account->company->bluevine_status
&& $account->created_at <= date('Y-m-d', strtotime('-1 month'));
if (request()->bluevine) {
$showBlueVinePromo = true;
}
}
2015-03-16 22:45:25 +01:00
$showWhiteLabelExpired = Utils::isSelfHost() && $account->company->hasExpiredPlan(PLAN_WHITE_LABEL);
2016-09-11 16:30:23 +02:00
// check if the account has quotes
$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) {
$hasQuotes = true;
}
}
}
2015-03-16 22:45:25 +01:00
$data = [
2016-08-15 05:46:47 +02:00
'account' => $user->account,
2017-01-30 17:05:31 +01:00
'user' => $user,
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'),
'hasQuotes' => $hasQuotes,
2016-09-11 16:30:23 +02:00
'showBreadcrumbs' => false,
'currencies' => $this->getCurrencyCodes(),
2016-09-11 16:30:23 +02:00
'expenses' => $expenses,
'tasks' => $tasks,
2017-01-30 17:05:31 +01:00
'showBlueVinePromo' => $showBlueVinePromo,
'showWhiteLabelExpired' => $showWhiteLabelExpired,
'showExpenses' => count($expenses) && $account->isModuleEnabled(ENTITY_EXPENSE),
2017-06-15 09:53:18 +02:00
'headerClass' => in_array(\App::getLocale(), ['lt', 'pl', 'cs', 'sl', 'tr_TR']) ? 'in-large' : 'in-thin',
'footerClass' => in_array(\App::getLocale(), ['lt', 'pl', 'cs', 'sl', 'tr_TR']) ? '' : 'in-thin',
2015-08-20 17:09:04 +02:00
];
2015-03-16 22:45:25 +01:00
2017-01-30 17:05:31 +01:00
if ($showBlueVinePromo) {
$usdLast12Months = 0;
$pastYear = date('Y-m-d', strtotime('-1 year'));
$paidLast12Months = $dashboardRepo->paidToDate($account, $userId, $viewAll, $pastYear);
foreach ($paidLast12Months as $item) {
if ($item->currency_id == null) {
$currency = $user->account->currency_id ?: DEFAULT_CURRENCY;
} else {
$currency = $item->currency_id;
}
if ($currency == CURRENCY_DOLLAR) {
$usdLast12Months += $item->value;
}
}
$data['usdLast12Months'] = $usdLast12Months;
}
2016-11-03 21:54:33 +01:00
2015-03-16 22:45:25 +01:00
return View::make('dashboard', $data);
}
2016-09-11 16:30:23 +02:00
private function getCurrencyCodes()
{
$account = Auth::user()->account;
$currencyIds = $account->currency_id ? [$account->currency_id] : [DEFAULT_CURRENCY];
// get client/invoice currencies
$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);
// get expense currencies
$data = Expense::scope()
->withArchived()
->distinct()
->get(['expense_currency_id'])
->toArray();
array_map(function ($item) use (&$currencyIds) {
$currencyId = intval($item['expense_currency_id']);
if ($currencyId && ! in_array($currencyId, $currencyIds)) {
$currencyIds[] = $currencyId;
}
}, $data);
$currencies = [];
foreach ($currencyIds as $currencyId) {
$currencies[$currencyId] = Utils::getFromCache($currencyId, 'currencies')->code;
}
return $currencies;
}
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
}