added date range picker #141

This commit is contained in:
denisdulici 2017-12-12 19:09:16 +03:00
parent 3695dac363
commit 48eeec7fab
3 changed files with 298 additions and 215 deletions

View File

@ -21,6 +21,10 @@ class Dashboard extends Controller
public $today; public $today;
public $income_donut = ['colors', 'labels', 'values'];
public $expense_donut = ['colors', 'labels', 'values'];
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
@ -29,134 +33,46 @@ class Dashboard extends Controller
public function index() public function index()
{ {
$this->today = Date::today(); $this->today = Date::today();
$month_days = $this->today->daysInMonth;
/* list($total_incomes, $total_expenses, $total_profit) = $this->getTotals();
* Cash Flow
*/
// Daily $cashflow = $this->getCashFlow();
$day = array();
$month_ago = $this->today->subMonth();
for ($j = $month_days; $j > 0; $j--) {
$day[$month_days - $j] = $month_ago->addDay()->format('d M');
}
$daily_income = $this->getCashFlow('income', 'daily'); list($donut_incomes, $donut_expenses) = $this->getDonuts();
$daily_expense = $this->getCashFlow('expense', 'daily');
$daily_profit = $this->getProfit($daily_income, $daily_expense); $accounts = Account::enabled()->get();
// Monthly $latest_incomes = $this->getLatestIncomes();
$month = array();
$year_ago = $this->today->subYear();
for ($j = 12; $j >= 0; $j--) {
$month[12 - $j] = $year_ago->addMonth()->format('M Y');
}
$monthly_income = $this->getCashFlow('income', 'monthly'); $latest_expenses = $this->getLatestExpenses();
$monthly_expense = $this->getCashFlow('expense', 'monthly');
$monthly_profit = $this->getProfit($monthly_income, $monthly_expense); return view('dashboard.dashboard.index', compact(
'total_incomes',
'total_expenses',
'total_profit',
'cashflow',
'donut_incomes',
'donut_expenses',
'accounts',
'latest_incomes',
'latest_expenses'
));
}
$line_daily = Charts::multi('line', 'chartjs') public function cashFlow()
->dimensions(0, 300) {
->colors(['#6da252', '#00c0ef', '#F56954']) $this->today = Date::today();
->dataset(trans_choice('general.profits', 1), $daily_profit)
->dataset(trans_choice('general.incomes', 1), $daily_income)
->dataset(trans_choice('general.expenses', 1), $daily_expense)
->labels($day)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.multi.line');
$line_monthly = Charts::multi('bar', 'chartjs') $content = $this->getCashFlow()->render();
->dimensions(0, 300)
->colors(['#6da252', '#00c0ef', '#F56954'])
->dataset(trans_choice('general.profits', 1), $monthly_profit)
->dataset(trans_choice('general.incomes', 1), $monthly_income)
->dataset(trans_choice('general.expenses', 1), $monthly_expense)
->labels($month)
->credits(false);
/* //return response()->setContent($content)->send();
* Totals & Pie Charts
*/
$incomes = $expenses = array(); echo $content;
}
$incomes_amount = $expenses_amount = 0; private function getTotals()
{
// Invoices list($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill) = $this->calculateAmounts();
$invoices = Invoice::with('payments')->accrued()->get();
list($invoice_paid_amount, $open_invoice, $overdue_invoice) = $this->getTotals($invoices, 'invoice');
$incomes_amount += $invoice_paid_amount;
// Add to Incomes By Category
$donut_incomes_colors[] = '#00c0ef';
$donut_incomes_labels[] = money($invoice_paid_amount, setting('general.default_currency'), true)->format() . ' - ' . trans_choice('general.invoices', 2);
$donut_incomes_values[] = (int) $invoice_paid_amount;
// Bills
$bills = Bill::with('payments')->accrued()->get();
list($bill_paid_amount, $open_bill, $overdue_bill) = $this->getTotals($bills, 'bill');
$expenses_amount += $bill_paid_amount;
// Add to Expenses By Category
$donut_expenses_colors[] = '#dd4b39';
$donut_expenses_labels[] = money($bill_paid_amount, setting('general.default_currency'), true)->format() . ' - ' . trans_choice('general.bills', 2);
$donut_expenses_values[] = (int) $bill_paid_amount;
// Revenues & Payments
$categories = Category::orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
foreach ($category->revenues as $revenue) {
$amount += $revenue->getConvertedAmount();
}
$donut_incomes_colors[] = $category->color;
$donut_incomes_labels[] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $category->name;
$donut_incomes_values[] = (int) $amount;
$incomes_amount += $amount;
break;
case 'expense':
$amount = 0;
foreach ($category->payments as $payment) {
$amount += $payment->getConvertedAmount();
}
$donut_expenses_colors[] = $category->color;
$donut_expenses_labels[] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $category->name;
$donut_expenses_values[] = (int) $amount;
$expenses_amount += $amount;
break;
}
}
$donut_incomes = Charts::create('donut', 'chartjs')
->colors($donut_incomes_colors)
->labels($donut_incomes_labels)
->values($donut_incomes_values)
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
$donut_expenses = Charts::create('donut', 'chartjs')
->colors($donut_expenses_colors)
->labels($donut_expenses_labels)
->values($donut_expenses_values)
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
$incomes_progress = 100; $incomes_progress = 100;
@ -202,41 +118,145 @@ class Dashboard extends Controller
'progress' => $total_progress 'progress' => $total_progress
); );
/* return array($total_incomes, $total_expenses, $total_profit);
* Accounts
*/
$accounts = Account::enabled()->get();
/*
* Latest Incomes
*/
$latest_incomes = collect(Invoice::accrued()->latest()->take(10)->get());
$latest_incomes = $latest_incomes->merge(Revenue::latest()->take(10)->get())->take(5)->sortByDesc('invoiced_at');
/*
* Latest Expenses
*/
$latest_expenses = collect(Bill::accrued()->latest()->take(10)->get());
$latest_expenses = $latest_expenses->merge(Payment::latest()->take(10)->get())->take(5)->sortByDesc('billed_at');
return view('dashboard.dashboard.index', compact(
'total_incomes',
'total_expenses',
'total_profit',
'line_daily',
'line_monthly',
'donut_incomes',
'donut_expenses',
'accounts',
'latest_incomes',
'latest_expenses'
));
} }
private function getCashFlow($type, $period) private function getCashFlow()
{
$start = Date::parse(request('start', $this->today->startOfYear()->format('Y-m-d')));
$end = Date::parse(request('end', $this->today->endOfYear()->format('Y-m-d')));
$period = request('period', 'month');
$start_month = $start->month;
$end_month = $end->month;
// Monthly
$labels = array();
$s = clone $start;
for ($j = $end_month; $j >= $start_month; $j--) {
$labels[$end_month - $j] = $s->format('M Y');
if ($period == 'month') {
$s->addMonth();
} else {
$s->addMonths(3);
$j -= 2;
}
}
$income = $this->calculateCashFlowTotals('income', $start, $end, $period);
$expense = $this->calculateCashFlowTotals('expense', $start, $end, $period);
$profit = $this->calculateCashFlowProfit($income, $expense);
$chart = Charts::multi('bar', 'chartjs')
->dimensions(0, 300)
->colors(['#6da252', '#00c0ef', '#F56954'])
->dataset(trans_choice('general.profits', 1), $profit)
->dataset(trans_choice('general.incomes', 1), $income)
->dataset(trans_choice('general.expenses', 1), $expense)
->labels($labels)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.multi.line');
return $chart;
}
private function getDonuts()
{
$donut_incomes = Charts::create('donut', 'chartjs')
->colors($this->income_donut['colors'])
->labels($this->income_donut['labels'])
->values($this->income_donut['values'])
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
$donut_expenses = Charts::create('donut', 'chartjs')
->colors($this->expense_donut['colors'])
->labels($this->expense_donut['labels'])
->values($this->expense_donut['values'])
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
return array($donut_incomes, $donut_expenses);
}
private function getLatestIncomes()
{
$latest = collect(Invoice::accrued()->latest()->take(10)->get());
$latest = $latest->merge(Revenue::latest()->take(10)->get())->take(5)->sortByDesc('invoiced_at');
return $latest;
}
private function getLatestExpenses()
{
$latest = collect(Bill::accrued()->latest()->take(10)->get());
$latest = $latest->merge(Payment::latest()->take(10)->get())->take(5)->sortByDesc('billed_at');
return $latest;
}
private function calculateAmounts()
{
$incomes_amount = $expenses_amount = 0;
// Invoices
$invoices = Invoice::with('payments')->accrued()->get();
list($invoice_paid_amount, $open_invoice, $overdue_invoice) = $this->calculateTotals($invoices, 'invoice');
$incomes_amount += $invoice_paid_amount;
// Add to Incomes By Category
$this->addToIncomeDonut('#00c0ef', $invoice_paid_amount, trans_choice('general.invoices', 2));
// Bills
$bills = Bill::with('payments')->accrued()->get();
list($bill_paid_amount, $open_bill, $overdue_bill) = $this->calculateTotals($bills, 'bill');
$expenses_amount += $bill_paid_amount;
// Add to Expenses By Category
$this->addToExpenseDonut('#dd4b39', $bill_paid_amount, trans_choice('general.bills', 2));
// Revenues & Payments
$categories = Category::orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
foreach ($category->revenues as $revenue) {
$amount += $revenue->getConvertedAmount();
}
$this->addToIncomeDonut($category->color, $amount, $category->name);
$incomes_amount += $amount;
break;
case 'expense':
$amount = 0;
foreach ($category->payments as $payment) {
$amount += $payment->getConvertedAmount();
}
$this->addToExpenseDonut($category->color, $amount, $category->name);
$expenses_amount += $amount;
break;
}
}
return array($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill);
}
private function calculateCashFlowTotals($type, $start, $end, $period)
{ {
$totals = array(); $totals = array();
@ -248,48 +268,30 @@ class Dashboard extends Controller
$m2 = '\App\Models\Expense\BillPayment'; $m2 = '\App\Models\Expense\BillPayment';
} }
switch ($period) { $date_format = 'Y-m';
case 'yearly':
$f1 = 'subYear';
$f2 = 'addYear';
$date_format = 'Y'; $n = 1;
break; if ($period == 'quarter') {
case 'monthly': $n = 3;
$f1 = 'subYear';
$f2 = 'addMonth';
$date_format = 'Y-m';
break;
default:
case 'daily':
$f1 = 'subMonth';
$f2 = 'addDay';
$date_format = 'Y-m-d';
break;
} }
$now = Date::now(); $start_date = $start->format($date_format);
$sub = Date::now()->$f1(); $end_date = $end->format($date_format);
$start_date = $sub->format($date_format);
$end_date = $now->format($date_format);
$next_date = $start_date; $next_date = $start_date;
$totals[$start_date] = 0; $totals[$start_date] = 0;
do { do {
$next_date = Date::parse($next_date)->$f2()->format($date_format); $next_date = Date::parse($next_date)->addMonths($n)->format($date_format);
$totals[$next_date] = 0; $totals[$next_date] = 0;
} while ($next_date < $end_date); } while ($next_date < $end_date);
$items_1 = $m1::whereBetween('paid_at', [$sub, $now])->get(); $items_1 = $m1::whereBetween('paid_at', [$start, $end])->get();
$this->setCashFlowTotals($totals, $items_1, $date_format); $this->setCashFlowTotals($totals, $items_1, $date_format);
$items_2 = $m2::whereBetween('paid_at', [$sub, $now])->get(); $items_2 = $m2::whereBetween('paid_at', [$start, $end])->get();
$this->setCashFlowTotals($totals, $items_2, $date_format); $this->setCashFlowTotals($totals, $items_2, $date_format);
@ -305,7 +307,22 @@ class Dashboard extends Controller
} }
} }
private function getTotals($items, $type) private function calculateCashFlowProfit($incomes, $expenses)
{
$profit = [];
foreach ($incomes as $key => $income) {
if ($income > 0 && $income > $expenses[$key]) {
$profit[$key] = $income - $expenses[$key];
} else {
$profit[$key] = 0;
}
}
return $profit;
}
private function calculateTotals($items, $type)
{ {
$paid = $open = $overdue = 0; $paid = $open = $overdue = 0;
@ -338,18 +355,22 @@ class Dashboard extends Controller
return array($paid, $open, $overdue); return array($paid, $open, $overdue);
} }
private function getProfit($incomes, $expenses) private function addToIncomeDonut($color, $amount, $text)
{ {
$profit = []; $this->addToDonut('income', $color, $amount, $text);
}
foreach ($incomes as $key => $income) { private function addToExpenseDonut($color, $amount, $text)
if ($income > 0 && $income > $expenses[$key]) { {
$profit[$key] = $income - $expenses[$key]; $this->addToDonut('expense', $color, $amount, $text);
} else { }
$profit[$key] = 0;
}
}
return $profit; private function addToDonut($type, $color, $amount, $text)
{
$attribute = $type . '_donut';
$this->$attribute['colors'][] = $color;
$this->$attribute['labels'][] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $text;
$this->$attribute['values'][] = (int) $amount;
} }
} }

View File

@ -67,21 +67,23 @@
<div class="row"> <div class="row">
<!---Income, Expense and Profit Line Chart--> <!---Income, Expense and Profit Line Chart-->
<div class="col-md-12"> <div class="col-md-12">
<div class="nav-tabs-custom"> <div class="box box-success">
<ul class="nav nav-tabs pull-right ui-sortable-handle"> <div class="box-header with-border">
<li class=""><a href="#monthly-chart" data-toggle="tab" aria-expanded="false">{{ trans('general.monthly') }}</a></li> <h3 class="box-title">{{ trans('dashboard.cash_flow') }}</h3>
<li class="active"><a href="#daily-chart" data-toggle="tab" aria-expanded="true">{{ trans('general.daily') }}</a></li> <div class="box-tools pull-right">
<li class="pull-left header" style="font-size: 18px;">{{ trans('dashboard.cash_flow') }}</li> <button type="button" id="cashflow-monthly" class="btn btn-default btn-sm">Monthly</button>&nbsp;&nbsp;
</ul> <button type="button" id="cashflow-quarterly" class="btn btn-default btn-sm">Quarterly</button>&nbsp;&nbsp;
<div class="btn btn-default btn-sm">
<div class="tab-content no-padding"> <div id="cashflow-range" class="pull-right">
<div class="chart tab-pane active" id="daily-chart" style="position: relative; height: 310px;"> <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
{!! $line_daily->render() !!} <span></span> <b class="caret"></b>
</div> </div>
<div class="chart tab-pane" id="monthly-chart" style="position: relative; height: 310px;"> </div>
{!! $line_monthly->render() !!}
</div> </div>
</div> </div>
<div class="box-body" id="cashflow">
{!! $cashflow->render() !!}
</div>
</div> </div>
</div> </div>
</div> </div>
@ -92,9 +94,6 @@
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('dashboard.incomes_by_category') }}</h3> <h3 class="box-title">{{ trans('dashboard.incomes_by_category') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button> <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div> </div>
</div> </div>
@ -107,9 +106,6 @@
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('dashboard.expenses_by_category') }}</h3> <h3 class="box-title">{{ trans('dashboard.expenses_by_category') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button> <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div> </div>
</div> </div>
@ -125,9 +121,6 @@
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('dashboard.account_balance') }}</h3> <h3 class="box-title">{{ trans('dashboard.account_balance') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button> <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div> </div>
</div> </div>
@ -154,9 +147,6 @@
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('dashboard.latest_incomes') }}</h3> <h3 class="box-title">{{ trans('dashboard.latest_incomes') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button> <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div> </div>
</div> </div>
@ -191,9 +181,6 @@
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('dashboard.latest_expenses') }}</h3> <h3 class="box-title">{{ trans('dashboard.latest_expenses') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button> <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div> </div>
</div> </div>
@ -228,4 +215,78 @@
@push('js') @push('js')
{!! Charts::assets() !!} {!! Charts::assets() !!}
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
@endpush
@push('scripts')
<script type="text/javascript">
$(function() {
var start = moment().startOf('year');
var end = moment().endOf('year');
function cb(start, end) {
$('#cashflow-range span').html(start.format('D MMM YYYY') + ' - ' + end.format('D MMM YYYY'));
}
$('#cashflow-range').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'This Year': [moment().startOf('year'), moment().endOf('year')],
'Previous Year': [moment().subtract(1, 'year').startOf('year'), moment().subtract(1, 'year').endOf('year')],
'This Quarter': [moment().subtract(2, 'months').startOf('month'), moment().endOf('month')],
'Previous Quarter': [moment().subtract(5, 'months').startOf('month'), moment().subtract(3, 'months').endOf('month')],
'Last 12 Months': [moment().subtract(11, 'months').startOf('month'), moment().endOf('month')]
}
}, cb);
cb(start, end);
});
$(document).ready(function () {
$('#cashflow-range').on('apply.daterangepicker', function(ev, picker) {
$.ajax({
url: '{{ url("dashboard/dashboard/cashflow") }}',
type: 'GET',
dataType: 'HTML',
data: 'start=' + picker.startDate.format('YYYY-MM-DD') + '&end=' + picker.endDate.format('YYYY-MM-DD'),
success: function(data) {
$('#cashflow').html(data);
}
});
});
$('#cashflow-monthly').on('click', function() {
var picker = $('#cashflow-range').data('daterangepicker');
$.ajax({
url: '{{ url("dashboard/dashboard/cashflow") }}',
type: 'GET',
dataType: 'HTML',
data: 'period=month&start=' + picker.startDate.format('YYYY-MM-DD') + '&end=' + picker.endDate.format('YYYY-MM-DD'),
success: function(data) {
$('#cashflow').html(data);
}
});
});
$('#cashflow-quarterly').on('click', function() {
var picker = $('#cashflow-range').data('daterangepicker');
$.ajax({
url: '{{ url("dashboard/dashboard/cashflow") }}',
type: 'GET',
dataType: 'HTML',
data: 'period=quarter&start=' + picker.startDate.format('YYYY-MM-DD') + '&end=' + picker.endDate.format('YYYY-MM-DD'),
success: function(data) {
$('#cashflow').html(data);
}
});
});
});
</script>
@endpush @endpush

View File

@ -15,6 +15,7 @@ Route::group(['middleware' => 'language'], function () {
Route::group(['middleware' => ['adminmenu', 'permission:read-admin-panel']], function () { Route::group(['middleware' => ['adminmenu', 'permission:read-admin-panel']], function () {
Route::get('/', 'Dashboard\Dashboard@index'); Route::get('/', 'Dashboard\Dashboard@index');
Route::get('dashboard/dashboard/cashflow', 'Dashboard\Dashboard@cashFlow');
Route::group(['prefix' => 'search'], function () { Route::group(['prefix' => 'search'], function () {
Route::get('search/search', 'Search\Search@search'); Route::get('search/search', 'Search\Search@search');