393 lines
12 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Models\Banking\Account;
use App\Models\Expense\Bill;
use App\Models\Expense\BillPayment;
use App\Models\Expense\Payment;
use App\Models\Income\Invoice;
use App\Models\Income\InvoicePayment;
use App\Models\Income\Revenue;
use App\Models\Setting\Category;
use App\Traits\Currencies;
2017-12-11 19:07:09 +03:00
use Charts;
2017-09-14 22:21:00 +03:00
use Date;
class Dashboard extends Controller
{
use Currencies;
2017-11-07 05:11:03 +03:00
public $today;
2017-12-12 19:09:16 +03:00
public $income_donut = ['colors', 'labels', 'values'];
public $expense_donut = ['colors', 'labels', 'values'];
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2017-11-07 05:11:03 +03:00
$this->today = Date::today();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
list($total_incomes, $total_expenses, $total_profit) = $this->getTotals();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$cashflow = $this->getCashFlow();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
list($donut_incomes, $donut_expenses) = $this->getDonuts();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$accounts = Account::enabled()->get();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$latest_incomes = $this->getLatestIncomes();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$latest_expenses = $this->getLatestExpenses();
2017-12-11 19:07:09 +03:00
2017-12-12 19:09:16 +03:00
return view('dashboard.dashboard.index', compact(
'total_incomes',
'total_expenses',
'total_profit',
'cashflow',
'donut_incomes',
'donut_expenses',
'accounts',
'latest_incomes',
'latest_expenses'
));
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
public function cashFlow()
{
$this->today = Date::today();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$content = $this->getCashFlow()->render();
2017-12-11 19:07:09 +03:00
2017-12-12 19:09:16 +03:00
//return response()->setContent($content)->send();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
echo $content;
}
2017-12-11 19:07:09 +03:00
2017-12-12 19:09:16 +03:00
private function getTotals()
{
list($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill) = $this->calculateAmounts();
2017-09-14 22:21:00 +03:00
$incomes_progress = 100;
if (!empty($open_invoice) && !empty($overdue_invoice)) {
$incomes_progress = (int) 100 - (100 * ($open_invoice / $overdue_invoice));
}
// Totals
$total_incomes = array(
'total' => $incomes_amount,
'open_invoice' => money($open_invoice, setting('general.default_currency'), true),
'overdue_invoice' => money($overdue_invoice, setting('general.default_currency'), true),
'progress' => $incomes_progress
);
$expenses_progress = 100;
if (!empty($open_bill) && !empty($overdue_bill)) {
$expenses_progress = (int) 100 - (100 * ($open_bill / $overdue_bill));
}
$total_expenses = array(
'total' => $expenses_amount,
'open_bill' => money($open_bill, setting('general.default_currency'), true),
'overdue_bill' => money($overdue_bill, setting('general.default_currency'), true),
'progress' => $expenses_progress
);
$amount_profit = $incomes_amount - $expenses_amount;
$open_profit = $open_invoice - $open_bill;
$overdue_profit = $overdue_invoice - $overdue_bill;
$total_progress = 100;
if (!empty($open_profit) && !empty($overdue_profit)) {
$total_progress = (int) 100 - (100 * ($open_profit / $overdue_profit));
}
$total_profit = array(
'total' => $amount_profit,
'open' => money($open_profit, setting('general.default_currency'), true),
'overdue' => money($overdue_profit, setting('general.default_currency'), true),
'progress' => $total_progress
);
2017-12-12 19:09:16 +03:00
return array($total_incomes, $total_expenses, $total_profit);
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
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');
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$start_month = $start->month;
$end_month = $end->month;
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
// Monthly
$labels = array();
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$s = clone $start;
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
for ($j = $end_month; $j >= $start_month; $j--) {
$labels[$end_month - $j] = $s->format('M Y');
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
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);
2017-12-13 12:04:08 +03:00
$chart = Charts::multi('line', 'chartjs')
2017-12-12 19:09:16 +03:00
->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;
2017-09-14 22:21:00 +03:00
}
2017-12-12 19:09:16 +03:00
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)
2017-09-14 22:21:00 +03:00
{
$totals = array();
if ($type == 'income') {
$m1 = '\App\Models\Income\Revenue';
$m2 = '\App\Models\Income\InvoicePayment';
} else {
$m1 = '\App\Models\Expense\Payment';
$m2 = '\App\Models\Expense\BillPayment';
}
2017-12-12 19:09:16 +03:00
$date_format = 'Y-m';
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
if ($period == 'month') {
$n = 1;
$start_date = $start->format($date_format);
$end_date = $end->format($date_format);
$next_date = $start_date;
} else {
2017-12-12 19:09:16 +03:00
$n = 3;
2017-12-12 19:30:36 +03:00
$start_date = $start->quarter;
$end_date = $end->quarter;
$next_date = $start_date;
2017-12-12 19:09:16 +03:00
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
$s = clone $start;
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
//$totals[$start_date] = 0;
while ($next_date <= $end_date) {
$totals[$next_date] = 0;
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
if ($period == 'month') {
$next_date = $s->addMonths($n)->format($date_format);
} else {
if (isset($totals[4])) {
break;
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
$next_date = $s->addMonths($n)->quarter;
}
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$items_1 = $m1::whereBetween('paid_at', [$start, $end])->get();
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
$this->setCashFlowTotals($totals, $items_1, $date_format, $period);
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
$items_2 = $m2::whereBetween('paid_at', [$start, $end])->get();
2017-09-14 22:21:00 +03:00
2017-12-12 19:30:36 +03:00
$this->setCashFlowTotals($totals, $items_2, $date_format, $period);
2017-09-14 22:21:00 +03:00
return $totals;
}
2017-12-12 19:30:36 +03:00
private function setCashFlowTotals(&$totals, $items, $date_format, $period)
2017-09-14 22:21:00 +03:00
{
foreach ($items as $item) {
2017-12-12 19:30:36 +03:00
if ($period == 'month') {
$i = Date::parse($item->paid_at)->format($date_format);
} else {
$i = Date::parse($item->paid_at)->quarter;
}
2017-09-14 22:21:00 +03:00
$totals[$i] += $item->getConvertedAmount();
}
}
2017-12-12 19:09:16 +03:00
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)
2017-11-07 05:11:03 +03:00
{
$paid = $open = $overdue = 0;
$today = $this->today->toDateString();
foreach ($items as $item) {
$paid += $item->getConvertedAmount();
$code_field = $type . '_status_code';
if ($item->$code_field == 'paid') {
continue;
}
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->payments as $payment) {
$payments += $payment->getConvertedAmount();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getConvertedAmount() - $payments;
} else {
$overdue += $item->getConvertedAmount() - $payments;
}
}
return array($paid, $open, $overdue);
}
2017-12-12 19:09:16 +03:00
private function addToIncomeDonut($color, $amount, $text)
2017-09-14 22:21:00 +03:00
{
2017-12-12 19:09:16 +03:00
$this->addToDonut('income', $color, $amount, $text);
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
private function addToExpenseDonut($color, $amount, $text)
{
$this->addToDonut('expense', $color, $amount, $text);
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
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;
2017-09-14 22:21:00 +03:00
}
}