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();
|
||||
}
|
||||
|
||||
$unpaid = $paid = $overdue = $partial_paid = [];
|
||||
$amounts = $this->calculateAmounts($invoices, $start, $end);
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
$grand = array_sum($amounts['unpaid']) + array_sum($amounts['paid']) + array_sum($amounts['overdue']);
|
||||
|
||||
$total = count($unpaid) + count($paid) + count($partial_paid) + count($overdue);
|
||||
|
||||
|
||||
$unpaid = $this->calculateTotals($unpaid, $start, $end, 'unpaid');
|
||||
$paid = $this->calculateTotals($paid, $start, $end, 'paid');
|
||||
$partial_paid = $this->calculateTotals($partial_paid, $start, $end, 'partial');
|
||||
$overdue = $this->calculateTotals($overdue, $start, $end, 'overdue');
|
||||
$totals = [
|
||||
'paid' => money(array_sum($amounts['paid']), setting('default.currency'), true),
|
||||
'unpaid' => money(array_sum($amounts['unpaid']), setting('default.currency'), true),
|
||||
'overdue' => money(array_sum($amounts['overdue']), setting('default.currency'), true),
|
||||
];
|
||||
|
||||
$progress = [
|
||||
'unpaid' => array_sum($unpaid),
|
||||
'paid' => array_sum($paid),
|
||||
'overdue' => array_sum($overdue),
|
||||
'partially_paid' => array_sum($partial_paid),
|
||||
'total' => $total,
|
||||
'paid' => !empty($grand) ? (100 / $grand) * array_sum($amounts['paid']) : '0',
|
||||
'unpaid' => !empty($grand) ? (100 / $grand) * array_sum($amounts['unpaid']) : '0',
|
||||
'overdue' => !empty($grand) ? (100 / $grand) * array_sum($amounts['overdue']) : '0',
|
||||
];
|
||||
|
||||
$chart = new Chartjs();
|
||||
@ -82,49 +62,39 @@ class Dashboard
|
||||
->options($this->getLineChartOptions())
|
||||
->labels(array_values($labels));
|
||||
|
||||
$chart->dataset(trans('general.unpaid'), 'line', array_values($unpaid))
|
||||
->backgroundColor('#ef3232')
|
||||
->color('#ef3232')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
$chart->dataset(trans('general.paid'), 'line', array_values($amounts['paid']))
|
||||
->backgroundColor('#6da252')
|
||||
->color('#6da252')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
|
||||
$chart->dataset(trans('general.paid'), 'line', array_values($paid))
|
||||
->backgroundColor('#6da252')
|
||||
->color('#6da252')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
$chart->dataset(trans('general.unpaid'), 'line', array_values($amounts['unpaid']))
|
||||
->backgroundColor('#efad32')
|
||||
->color('#efad32')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
|
||||
$chart->dataset(trans('general.overdue'), 'line', array_values($overdue))
|
||||
->backgroundColor('#efad32')
|
||||
->color('#efad32')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
$chart->dataset(trans('general.overdue'), 'line', array_values($amounts['overdue']))
|
||||
->backgroundColor('#ef3232')
|
||||
->color('#ef3232')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
|
||||
$chart->dataset(trans('general.partially_paid'), 'line', array_values($partial_paid))
|
||||
->backgroundColor('#328aef')
|
||||
->color('#328aef')
|
||||
->options([
|
||||
'borderWidth' => 4,
|
||||
'pointStyle' => 'line',
|
||||
])
|
||||
->fill(false);
|
||||
|
||||
|
||||
return view('portal.dashboard.index', compact('contact', 'invoices', 'progress', 'chart'));
|
||||
return view('portal.dashboard.index', compact('contact', 'invoices', 'totals', 'progress', 'chart'));
|
||||
}
|
||||
|
||||
private function calculateTotals($items, $start, $end, $type)
|
||||
private function calculateAmounts($invoices, $start, $end)
|
||||
{
|
||||
$totals = [];
|
||||
$amounts = ['paid', 'unpaid', 'overdue'];
|
||||
|
||||
$date_format = 'Y-m';
|
||||
|
||||
@ -136,26 +106,50 @@ class Dashboard
|
||||
$s = clone $start;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$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) {
|
||||
if ($type == 'partial') {
|
||||
$item->amount = $item->transactions()->paid();
|
||||
$today = Date::today()->format('Y-m-d');
|
||||
|
||||
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')
|
||||
<div class="row">
|
||||
<div class="col-md-3">
|
||||
<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="col-md-4">
|
||||
<div class="card bg-gradient-success card-stats">
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<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 class="col-auto">
|
||||
<div class="icon icon-shape bg-white text-success rounded-circle shadow">
|
||||
<i class="fa fa-money-bill"></i>
|
||||
@ -50,29 +19,21 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<el-tooltip
|
||||
content="{{ $progress['total'] }} / {{ $progress['paid'] }}"
|
||||
effect="dark"
|
||||
: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 class="card-footer">
|
||||
<div class="progress progress-xs mb-0">
|
||||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="{{ $progress['paid'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['paid'] }}%"></div>
|
||||
</div>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-3">
|
||||
<div class="col-md-4">
|
||||
<div class="card bg-gradient-warning card-stats">
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.overdue') }}</h5>
|
||||
<span class="font-weight-bold text-white mb-0">@money($progress['overdue'], setting('default.currency'), true)</span>
|
||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.unpaid') }}</h5>
|
||||
<span class="font-weight-bold text-white mb-0">{{ $totals['unpaid'] }}</span>
|
||||
</div>
|
||||
|
||||
<div class="col-auto">
|
||||
<div class="icon icon-shape bg-white text-warning rounded-circle shadow">
|
||||
<i class="fa fa-money-bill"></i>
|
||||
@ -80,50 +41,35 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<el-tooltip
|
||||
content="{{ $progress['total'] }} / {{ $progress['overdue'] }}"
|
||||
effect="dark"
|
||||
: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 class="card-footer">
|
||||
<div class="progress progress-xs mb-0">
|
||||
<div class="progress-bar bg-warning" role="progressbar" aria-valuenow="{{ $progress['unpaid'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['unpaid'] }}%"></div>
|
||||
</div>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-3">
|
||||
<div class="card bg-gradient-info card-stats">
|
||||
<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.partially_paid') }}</h5>
|
||||
<span class="font-weight-bold text-white mb-0">@money($progress['partially_paid'], setting('default.currency'), true)</span>
|
||||
<h5 class="text-uppercase text-white mb-0">{{ trans('general.overdue') }}</h5>
|
||||
<span class="font-weight-bold text-white mb-0">{{ $totals['overdue'] }}</span>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<el-tooltip
|
||||
content="{{ $progress['total'] }} / {{ $progress['partially_paid'] }}"
|
||||
effect="dark"
|
||||
: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 class="card-footer">
|
||||
<div class="progress progress-xs mb-0">
|
||||
<div class="progress-bar bg-danger" role="progressbar" aria-valuenow="{{ $progress['overdue'] }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $progress['overdue'] }}%"></div>
|
||||
</div>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
|
Loading…
x
Reference in New Issue
Block a user