156 lines
4.7 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Portal;
2019-12-31 15:49:09 +03:00
use App\Models\Sale\Invoice;
2020-01-24 12:50:06 +03:00
use App\Traits\Charts;
2019-11-16 10:21:14 +03:00
use App\Utilities\Chartjs;
use Date;
2020-01-24 12:50:06 +03:00
class Dashboard
2019-11-16 10:21:14 +03:00
{
2020-01-24 12:50:06 +03:00
use Charts;
2019-11-16 10:21:14 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$contact = user()->contact;
2020-01-21 11:11:23 +03:00
$invoices = Invoice::accrued()->where('contact_id', $contact->id)->get();
2019-11-16 10:21:14 +03:00
$start = Date::parse(request('start', Date::today()->startOfYear()->format('Y-m-d')));
$end = Date::parse(request('end', Date::today()->endOfYear()->format('Y-m-d')));
$start_month = $start->month;
$end_month = $end->month;
// Monthly
$labels = [];
$s = clone $start;
for ($j = $end_month; $j >= $start_month; $j--) {
$labels[$end_month - $j] = $s->format('M Y');
$s->addMonth();
}
2020-04-05 19:50:51 +03:00
$amounts = $this->calculateAmounts($invoices, $start, $end);
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
$grand = array_sum($amounts['unpaid']) + array_sum($amounts['paid']) + array_sum($amounts['overdue']);
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
$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),
];
2019-11-16 10:21:14 +03:00
$progress = [
2020-04-05 19:50:51 +03:00
'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',
];
2019-11-16 10:21:14 +03:00
$chart = new Chartjs();
$chart->type('line')
->width(0)
->height(300)
2020-01-24 12:50:06 +03:00
->options($this->getLineChartOptions())
->labels(array_values($labels));
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
$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.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($amounts['overdue']))
->backgroundColor('#ef3232')
->color('#ef3232')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
return view('portal.dashboard.index', compact('contact', 'invoices', 'totals', 'progress', 'chart'));
2019-11-16 10:21:14 +03:00
}
2020-04-05 19:50:51 +03:00
private function calculateAmounts($invoices, $start, $end)
2019-11-16 10:21:14 +03:00
{
2020-04-05 19:50:51 +03:00
$amounts = ['paid', 'unpaid', 'overdue'];
2019-11-16 10:21:14 +03:00
$date_format = 'Y-m';
$n = 1;
$start_date = $start->format($date_format);
$end_date = $end->format($date_format);
$next_date = $start_date;
$s = clone $start;
while ($next_date <= $end_date) {
2020-04-05 19:50:51 +03:00
$amounts['paid'][$next_date] = $amounts['unpaid'][$next_date] = $amounts['overdue'][$next_date] = 0;
2019-11-16 10:21:14 +03:00
$next_date = $s->addMonths($n)->format($date_format);
}
2020-04-05 19:50:51 +03:00
$this->setAmounts($amounts, $invoices, $date_format);
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
return $amounts;
2019-11-16 10:21:14 +03:00
}
2020-04-05 19:50:51 +03:00
private function setAmounts(&$amounts, $invoices, $date_format)
2019-11-16 10:21:14 +03:00
{
2020-04-05 19:50:51 +03:00
$today = Date::today()->format('Y-m-d');
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
foreach ($invoices as $invoice) {
$date = Date::parse($invoice->due_at)->format($date_format);
2019-11-16 10:21:14 +03:00
2020-04-05 19:50:51 +03:00
$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;
}
}
2019-11-16 10:21:14 +03:00
}
}
}