Merge pull request #1396 from denisdulici/master
Refactored portal dashboard
This commit is contained in:
commit
922ea07cf4
@ -39,40 +39,20 @@ class Dashboard
|
|||||||
$s->addMonth();
|
$s->addMonth();
|
||||||
}
|
}
|
||||||
|
|
||||||
$unpaid = $paid = $overdue = $partial_paid = [];
|
$amounts = $this->calculateAmounts($invoices, $start, $end);
|
||||||
|
|
||||||
foreach ($invoices as $invoice) {
|
$grand = array_sum($amounts['unpaid']) + array_sum($amounts['paid']) + array_sum($amounts['overdue']);
|
||||||
switch ($invoice->status) {
|
|
||||||
case 'paid':
|
|
||||||
$paid[] = $invoice;
|
|
||||||
break;
|
|
||||||
case 'partial':
|
|
||||||
$partial_paid[] = $invoice;
|
|
||||||
break;
|
|
||||||
case 'sent':
|
|
||||||
default:
|
|
||||||
if (Date::today()->format('Y-m-d') > $invoice->due_at->format('Y-m-d')) {
|
|
||||||
$overdue[] = $invoice;
|
|
||||||
} else {
|
|
||||||
$unpaid[] = $invoice;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$total = count($unpaid) + count($paid) + count($partial_paid) + count($overdue);
|
$totals = [
|
||||||
|
'paid' => money(array_sum($amounts['paid']), setting('default.currency'), true),
|
||||||
|
'unpaid' => money(array_sum($amounts['unpaid']), setting('default.currency'), true),
|
||||||
$unpaid = $this->calculateTotals($unpaid, $start, $end, 'unpaid');
|
'overdue' => money(array_sum($amounts['overdue']), setting('default.currency'), true),
|
||||||
$paid = $this->calculateTotals($paid, $start, $end, 'paid');
|
];
|
||||||
$partial_paid = $this->calculateTotals($partial_paid, $start, $end, 'partial');
|
|
||||||
$overdue = $this->calculateTotals($overdue, $start, $end, 'overdue');
|
|
||||||
|
|
||||||
$progress = [
|
$progress = [
|
||||||
'unpaid' => array_sum($unpaid),
|
'paid' => !empty($grand) ? (100 / $grand) * array_sum($amounts['paid']) : '0',
|
||||||
'paid' => array_sum($paid),
|
'unpaid' => !empty($grand) ? (100 / $grand) * array_sum($amounts['unpaid']) : '0',
|
||||||
'overdue' => array_sum($overdue),
|
'overdue' => !empty($grand) ? (100 / $grand) * array_sum($amounts['overdue']) : '0',
|
||||||
'partially_paid' => array_sum($partial_paid),
|
|
||||||
'total' => $total,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$chart = new Chartjs();
|
$chart = new Chartjs();
|
||||||
@ -82,49 +62,39 @@ class Dashboard
|
|||||||
->options($this->getLineChartOptions())
|
->options($this->getLineChartOptions())
|
||||||
->labels(array_values($labels));
|
->labels(array_values($labels));
|
||||||
|
|
||||||
$chart->dataset(trans('general.unpaid'), 'line', array_values($unpaid))
|
$chart->dataset(trans('general.paid'), 'line', array_values($amounts['paid']))
|
||||||
->backgroundColor('#ef3232')
|
->backgroundColor('#6da252')
|
||||||
->color('#ef3232')
|
->color('#6da252')
|
||||||
->options([
|
->options([
|
||||||
'borderWidth' => 4,
|
'borderWidth' => 4,
|
||||||
'pointStyle' => 'line',
|
'pointStyle' => 'line',
|
||||||
])
|
])
|
||||||
->fill(false);
|
->fill(false);
|
||||||
|
|
||||||
$chart->dataset(trans('general.paid'), 'line', array_values($paid))
|
$chart->dataset(trans('general.unpaid'), 'line', array_values($amounts['unpaid']))
|
||||||
->backgroundColor('#6da252')
|
->backgroundColor('#efad32')
|
||||||
->color('#6da252')
|
->color('#efad32')
|
||||||
->options([
|
->options([
|
||||||
'borderWidth' => 4,
|
'borderWidth' => 4,
|
||||||
'pointStyle' => 'line',
|
'pointStyle' => 'line',
|
||||||
])
|
])
|
||||||
->fill(false);
|
->fill(false);
|
||||||
|
|
||||||
$chart->dataset(trans('general.overdue'), 'line', array_values($overdue))
|
$chart->dataset(trans('general.overdue'), 'line', array_values($amounts['overdue']))
|
||||||
->backgroundColor('#efad32')
|
->backgroundColor('#ef3232')
|
||||||
->color('#efad32')
|
->color('#ef3232')
|
||||||
->options([
|
->options([
|
||||||
'borderWidth' => 4,
|
'borderWidth' => 4,
|
||||||
'pointStyle' => 'line',
|
'pointStyle' => 'line',
|
||||||
])
|
])
|
||||||
->fill(false);
|
->fill(false);
|
||||||
|
|
||||||
$chart->dataset(trans('general.partially_paid'), 'line', array_values($partial_paid))
|
return view('portal.dashboard.index', compact('contact', 'invoices', 'totals', 'progress', 'chart'));
|
||||||
->backgroundColor('#328aef')
|
|
||||||
->color('#328aef')
|
|
||||||
->options([
|
|
||||||
'borderWidth' => 4,
|
|
||||||
'pointStyle' => 'line',
|
|
||||||
])
|
|
||||||
->fill(false);
|
|
||||||
|
|
||||||
|
|
||||||
return view('portal.dashboard.index', compact('contact', 'invoices', 'progress', 'chart'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateTotals($items, $start, $end, $type)
|
private function calculateAmounts($invoices, $start, $end)
|
||||||
{
|
{
|
||||||
$totals = [];
|
$amounts = ['paid', 'unpaid', 'overdue'];
|
||||||
|
|
||||||
$date_format = 'Y-m';
|
$date_format = 'Y-m';
|
||||||
|
|
||||||
@ -136,26 +106,50 @@ class Dashboard
|
|||||||
$s = clone $start;
|
$s = clone $start;
|
||||||
|
|
||||||
while ($next_date <= $end_date) {
|
while ($next_date <= $end_date) {
|
||||||
$totals[$next_date] = 0;
|
$amounts['paid'][$next_date] = $amounts['unpaid'][$next_date] = $amounts['overdue'][$next_date] = 0;
|
||||||
|
|
||||||
$next_date = $s->addMonths($n)->format($date_format);
|
$next_date = $s->addMonths($n)->format($date_format);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->setTotals($totals, $items, $date_format, $type);
|
$this->setAmounts($amounts, $invoices, $date_format);
|
||||||
|
|
||||||
return $totals;
|
return $amounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setTotals(&$totals, $items, $date_format, $type)
|
private function setAmounts(&$amounts, $invoices, $date_format)
|
||||||
{
|
{
|
||||||
foreach ($items as $item) {
|
$today = Date::today()->format('Y-m-d');
|
||||||
if ($type == 'partial') {
|
|
||||||
$item->amount = $item->transactions()->paid();
|
foreach ($invoices as $invoice) {
|
||||||
|
$date = Date::parse($invoice->due_at)->format($date_format);
|
||||||
|
|
||||||
|
$amount = $invoice->getAmountConvertedToDefault();
|
||||||
|
|
||||||
|
$is_overdue = $today > $invoice->due_at->format('Y-m-d');
|
||||||
|
|
||||||
|
switch ($invoice->status) {
|
||||||
|
case 'paid':
|
||||||
|
$amounts['paid'][$date] += $amount;
|
||||||
|
break;
|
||||||
|
case 'partial':
|
||||||
|
$paid = $invoice->paid;
|
||||||
|
$remainder = $amount - $paid;
|
||||||
|
|
||||||
|
$amounts['paid'][$date] += $paid;
|
||||||
|
|
||||||
|
if ($is_overdue) {
|
||||||
|
$amounts['overdue'][$date] += $remainder;
|
||||||
|
} else {
|
||||||
|
$amounts['unpaid'][$date] += $remainder;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if ($is_overdue) {
|
||||||
|
$amounts['overdue'][$date] += $amount;
|
||||||
|
} else {
|
||||||
|
$amounts['unpaid'][$date] += $amount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$i = Date::parse($item->paid_at)->format($date_format);
|
|
||||||
|
|
||||||
$totals[$i] += $item->getAmountConvertedToDefault();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,45 +4,14 @@
|
|||||||
|
|
||||||
@section('content')
|
@section('content')
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-3">
|
<div class="col-md-4">
|
||||||
<div class="card bg-gradient-danger card-stats">
|
|
||||||
<div class="card-body">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col">
|
|
||||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.unpaid') }}</h5>
|
|
||||||
<span class="font-weight-bold text-white mb-0">@money($progress['unpaid'], setting('default.currency'), true)</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-auto">
|
|
||||||
<div class="icon icon-shape bg-white text-danger rounded-circle shadow">
|
|
||||||
<i class="fa fa-money-bill"></i>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<el-tooltip
|
|
||||||
content="{{ $progress['total'] }} / {{ $progress['unpaid'] }}"
|
|
||||||
effect="dark"
|
|
||||||
:open-delay="100"
|
|
||||||
placement="top">
|
|
||||||
<div class="card-footer">
|
|
||||||
<div class="progress progress-xs mb-0">
|
|
||||||
<div class="progress-bar bg-danger" role="progressbar" aria-valuenow="{{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['unpaid'] : '0' }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['unpaid'] : '0' }}%"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</el-tooltip>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-md-3">
|
|
||||||
<div class="card bg-gradient-success card-stats">
|
<div class="card bg-gradient-success card-stats">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.paid') }}</h5>
|
<h5 class="text-uppercase text-white mb-0">{{ trans('general.paid') }}</h5>
|
||||||
<span class="font-weight-bold text-white mb-0">@money($progress['paid'], setting('default.currency'), true)</span>
|
<span class="font-weight-bold text-white mb-0">{{ $totals['paid'] }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-auto">
|
<div class="col-auto">
|
||||||
<div class="icon icon-shape bg-white text-success rounded-circle shadow">
|
<div class="icon icon-shape bg-white text-success rounded-circle shadow">
|
||||||
<i class="fa fa-money-bill"></i>
|
<i class="fa fa-money-bill"></i>
|
||||||
@ -50,29 +19,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<el-tooltip
|
<div class="card-footer">
|
||||||
content="{{ $progress['total'] }} / {{ $progress['paid'] }}"
|
<div class="progress progress-xs mb-0">
|
||||||
effect="dark"
|
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="{{ $progress['paid'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['paid'] }}%"></div>
|
||||||
:open-delay="100"
|
|
||||||
placement="top">
|
|
||||||
<div class="card-footer">
|
|
||||||
<div class="progress progress-xs mb-0">
|
|
||||||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="{{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['paid'] : '0' }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['paid'] : '0' }}%"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</el-tooltip>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-4">
|
||||||
<div class="col-md-3">
|
|
||||||
<div class="card bg-gradient-warning card-stats">
|
<div class="card bg-gradient-warning card-stats">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.overdue') }}</h5>
|
<h5 class="text-uppercase text-white mb-0">{{ trans('general.unpaid') }}</h5>
|
||||||
<span class="font-weight-bold text-white mb-0">@money($progress['overdue'], setting('default.currency'), true)</span>
|
<span class="font-weight-bold text-white mb-0">{{ $totals['unpaid'] }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-auto">
|
<div class="col-auto">
|
||||||
<div class="icon icon-shape bg-white text-warning rounded-circle shadow">
|
<div class="icon icon-shape bg-white text-warning rounded-circle shadow">
|
||||||
<i class="fa fa-money-bill"></i>
|
<i class="fa fa-money-bill"></i>
|
||||||
@ -80,50 +41,35 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<el-tooltip
|
<div class="card-footer">
|
||||||
content="{{ $progress['total'] }} / {{ $progress['overdue'] }}"
|
<div class="progress progress-xs mb-0">
|
||||||
effect="dark"
|
<div class="progress-bar bg-warning" role="progressbar" aria-valuenow="{{ $progress['unpaid'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['unpaid'] }}%"></div>
|
||||||
:open-delay="100"
|
|
||||||
placement="top">
|
|
||||||
<div class="card-footer">
|
|
||||||
<div class="progress progress-xs mb-0">
|
|
||||||
<div class="progress-bar bg-warning" role="progressbar" aria-valuenow="{{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['overdue'] : '0' }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['overdue'] : '0' }}%"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</el-tooltip>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-4">
|
||||||
<div class="col-md-3">
|
<div class="card bg-gradient-danger card-stats">
|
||||||
<div class="card bg-gradient-info card-stats">
|
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.partially_paid') }}</h5>
|
<h5 class="text-uppercase text-white mb-0">{{ trans('general.overdue') }}</h5>
|
||||||
<span class="font-weight-bold text-white mb-0">@money($progress['partially_paid'], setting('default.currency'), true)</span>
|
<span class="font-weight-bold text-white mb-0">{{ $totals['overdue'] }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-auto">
|
<div class="col-auto">
|
||||||
<div class="icon icon-shape bg-white text-info rounded-circle shadow">
|
<div class="icon icon-shape bg-white text-danger rounded-circle shadow">
|
||||||
<i class="fa fa-money-bill"></i>
|
<i class="fa fa-money-bill"></i>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<el-tooltip
|
<div class="card-footer">
|
||||||
content="{{ $progress['total'] }} / {{ $progress['partially_paid'] }}"
|
<div class="progress progress-xs mb-0">
|
||||||
effect="dark"
|
<div class="progress-bar bg-danger" role="progressbar" aria-valuenow="{{ $progress['overdue'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['overdue'] }}%"></div>
|
||||||
:open-delay="100"
|
|
||||||
placement="top">
|
|
||||||
<div class="card-footer">
|
|
||||||
<div class="progress progress-xs mb-0">
|
|
||||||
<div class="progress-bar bg-info" role="progressbar" aria-valuenow="{{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['partially_paid'] : '0' }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ !empty($progress['total']) ? (100 / $progress['total']) * $progress['partially_paid'] : '0' }}%"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</el-tooltip>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-12">
|
<div class="col-md-12">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user