448 lines
14 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Http\Controllers\Common;
2017-09-14 22:21:00 +03:00
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-14 12:07:35 +03:00
public $income_donut = ['colors' => [], 'labels' => [], 'values' => []];
2017-12-12 19:09:16 +03:00
2017-12-14 12:07:35 +03:00
public $expense_donut = ['colors' => [], 'labels' => [], 'values' => []];
2017-12-12 19:09:16 +03:00
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-21 18:32:23 +03:00
$accounts = Account::enabled()->take(6)->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
2018-06-10 02:48:51 +03:00
return view('common.dashboard.index', compact(
2017-12-12 19:09:16 +03:00
'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) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
2017-09-14 22:21:00 +03:00
}
// 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) ($open_bill * 100) / ($open_bill + $overdue_bill);
2017-09-14 22:21:00 +03:00
}
$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) ($open_profit * 100) / ($open_profit + $overdue_profit);
2017-09-14 22:21:00 +03:00
}
$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
if ($range == 'last_12_months') {
2018-11-05 13:38:28 +03:00
$end_month = 12;
$start_month = 0;
} elseif ($range == 'custom') {
$end_month = $end->diffInMonths($start);
$start_month = 0;
}
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()
{
2017-12-13 14:43:47 +03:00
// Show donut prorated if there is no income
if (array_sum($this->income_donut['values']) == 0) {
foreach ($this->income_donut['values'] as $key => $value) {
$this->income_donut['values'][$key] = 1;
}
}
2017-12-26 00:33:26 +03:00
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->income_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->income_donut['colors'][$id];
$labels[$id] = $this->income_donut['labels'][$id];
}
2017-12-12 19:09:16 +03:00
$donut_incomes = Charts::create('donut', 'chartjs')
2017-12-26 00:33:26 +03:00
->colors($colors)
->labels($labels)
->values($values)
2017-12-12 19:09:16 +03:00
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
2017-12-13 14:43:47 +03:00
// Show donut prorated if there is no expense
if (array_sum($this->expense_donut['values']) == 0) {
foreach ($this->expense_donut['values'] as $key => $value) {
$this->expense_donut['values'][$key] = 1;
}
}
2017-12-26 00:33:26 +03:00
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->expense_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->expense_donut['colors'][$id];
$labels[$id] = $this->expense_donut['labels'][$id];
}
2017-12-12 19:09:16 +03:00
$donut_expenses = Charts::create('donut', 'chartjs')
2017-12-26 00:33:26 +03:00
->colors($colors)
->labels($labels)
->values($values)
2017-12-12 19:09:16 +03:00
->dimensions(0, 160)
->credits(false)
->view('vendor.consoletvs.charts.chartjs.donut');
return array($donut_incomes, $donut_expenses);
}
private function getLatestIncomes()
{
2018-01-17 13:23:03 +03:00
$invoices = collect(Invoice::orderBy('invoiced_at', 'desc')->accrued()->take(10)->get())->each(function ($item) {
$item->paid_at = $item->invoiced_at;
});
2018-03-08 11:48:34 +03:00
$revenues = collect(Revenue::orderBy('paid_at', 'desc')->isNotTransfer()->take(10)->get());
2018-01-17 13:23:03 +03:00
$latest = $revenues->merge($invoices)->take(5)->sortByDesc('paid_at');
2017-12-12 19:09:16 +03:00
return $latest;
2017-09-14 22:21:00 +03:00
}
2017-12-12 19:09:16 +03:00
private function getLatestExpenses()
{
2018-01-17 13:23:03 +03:00
$bills = collect(Bill::orderBy('billed_at', 'desc')->accrued()->take(10)->get())->each(function ($item) {
$item->paid_at = $item->billed_at;
});
2018-03-08 11:48:34 +03:00
$payments = collect(Payment::orderBy('paid_at', 'desc')->isNotTransfer()->take(10)->get());
2018-01-17 13:23:03 +03:00
$latest = $payments->merge($bills)->take(5)->sortByDesc('paid_at');
2017-12-12 19:09:16 +03:00
return $latest;
}
private function calculateAmounts()
{
2018-04-23 22:17:20 +03:00
$incomes_amount = $open_invoice = $overdue_invoice = 0;
$expenses_amount = $open_bill = $overdue_bill = 0;
2017-12-12 19:09:16 +03:00
2018-04-23 22:17:20 +03:00
// Get categories
$categories = Category::with(['bills', 'invoices', 'payments', 'revenues'])->orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
2017-12-12 19:09:16 +03:00
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
2018-04-23 22:17:20 +03:00
// Revenues
2017-12-12 19:09:16 +03:00
foreach ($category->revenues as $revenue) {
$amount += $revenue->getConvertedAmount();
}
2018-04-23 22:17:20 +03:00
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$incomes_amount += $paid;
$open_invoice += $open;
$overdue_invoice += $overdue;
$amount += $paid;
}
2017-12-12 19:09:16 +03:00
$this->addToIncomeDonut($category->color, $amount, $category->name);
break;
case 'expense':
$amount = 0;
2018-04-23 22:17:20 +03:00
// Payments
2017-12-12 19:09:16 +03:00
foreach ($category->payments as $payment) {
$amount += $payment->getConvertedAmount();
}
2018-04-23 22:17:20 +03:00
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$expenses_amount += $paid;
$open_bill += $open;
$overdue_bill += $overdue;
$amount += $paid;
}
2017-12-12 19:09:16 +03:00
$this->addToExpenseDonut($category->color, $amount, $category->name);
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
$items_1 = $m1::whereBetween('paid_at', [$start, $end])->isNotTransfer()->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
2018-04-18 16:12:46 +03:00
if (!isset($totals[$i])) {
continue;
}
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;
}
2018-04-23 22:17:20 +03:00
private function calculateInvoiceBillTotals($item, $type)
2017-11-07 05:11:03 +03:00
{
$paid = $open = $overdue = 0;
$today = $this->today->toDateString();
2018-04-23 22:17:20 +03:00
$paid += $item->getConvertedAmount();
2017-11-07 05:11:03 +03:00
2018-04-23 22:17:20 +03:00
$code_field = $type . '_status_code';
2017-11-07 05:11:03 +03:00
2018-04-23 22:17:20 +03:00
if ($item->$code_field != 'paid') {
2017-11-07 05:11:03 +03:00
$payments = 0;
2018-04-23 22:17:20 +03:00
2017-11-07 05:11:03 +03:00
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-14 12:07:35 +03:00
$this->income_donut['colors'][] = $color;
$this->income_donut['labels'][] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $text;
$this->income_donut['values'][] = (int) $amount;
2017-12-12 19:09:16 +03:00
}
2017-09-14 22:21:00 +03:00
2017-12-12 19:09:16 +03:00
private function addToExpenseDonut($color, $amount, $text)
{
2017-12-14 12:07:35 +03:00
$this->expense_donut['colors'][] = $color;
$this->expense_donut['labels'][] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $text;
$this->expense_donut['values'][] = (int) $amount;
2017-09-14 22:21:00 +03:00
}
}