Akaunting/app/Http/Controllers/Portal/Dashboard.php

102 lines
3.0 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Http\Controllers\Portal;
2020-12-23 22:28:38 +00:00
use App\Models\Document\Document;
2020-01-24 09:50:06 +00:00
use App\Traits\Charts;
2021-06-04 13:21:05 +00:00
use App\Traits\DateTime;
2023-10-03 08:06:08 +00:00
use App\Utilities\Date;
2019-11-16 07:21:14 +00:00
2020-01-24 09:50:06 +00:00
class Dashboard
2019-11-16 07:21:14 +00:00
{
2021-06-04 13:21:05 +00:00
use Charts, DateTime;
2020-01-24 09:50:06 +00:00
2019-11-16 07:21:14 +00:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$contact = user()->contact;
// Redirect user redirect landing page..
if (!$contact) {
return redirect(user()->getLandingPageOfUser());
}
2023-10-03 08:06:08 +00:00
$financial_year = $this->getFinancialYear();
2019-11-16 07:21:14 +00:00
2023-10-03 08:06:08 +00:00
$start = Date::parse(request('start_date', $financial_year->copy()->getStartDate()->toDateString()));
$end = Date::parse(request('end_date', $financial_year->copy()->getEndDate()->toDateString()));
2021-06-04 13:21:05 +00:00
//$invoices = Document::invoice()->accrued()->where('contact_id', $contact->id)->get();
$invoices = Document::invoice()->accrued()->whereBetween('due_at', [$start, $end])->where('contact_id', $contact->id)->get();
2023-10-03 08:06:08 +00:00
2020-04-05 16:50:51 +00:00
$amounts = $this->calculateAmounts($invoices, $start, $end);
2019-11-16 07:21:14 +00:00
2022-06-01 07:15:55 +00:00
return view('portal.dashboard.index', compact('contact', 'invoices'));
2019-11-16 07:21:14 +00:00
}
2020-04-05 16:50:51 +00:00
private function calculateAmounts($invoices, $start, $end)
2019-11-16 07:21:14 +00:00
{
2020-04-05 16:50:51 +00:00
$amounts = ['paid', 'unpaid', 'overdue'];
2019-11-16 07:21:14 +00: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 16:50:51 +00:00
$amounts['paid'][$next_date] = $amounts['unpaid'][$next_date] = $amounts['overdue'][$next_date] = 0;
2019-11-16 07:21:14 +00:00
$next_date = $s->addMonths($n)->format($date_format);
}
2020-04-05 16:50:51 +00:00
$this->setAmounts($amounts, $invoices, $date_format);
2019-11-16 07:21:14 +00:00
2020-04-05 16:50:51 +00:00
return $amounts;
2019-11-16 07:21:14 +00:00
}
2020-04-05 16:50:51 +00:00
private function setAmounts(&$amounts, $invoices, $date_format)
2019-11-16 07:21:14 +00:00
{
2020-04-05 16:50:51 +00:00
$today = Date::today()->format('Y-m-d');
2019-11-16 07:21:14 +00:00
2020-04-05 16:50:51 +00:00
foreach ($invoices as $invoice) {
$date = Date::parse($invoice->due_at)->format($date_format);
2019-11-16 07:21:14 +00:00
2020-04-05 16:50:51 +00: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 07:21:14 +00:00
}
}
}