akaunting/app/Http/Controllers/Reports/IncomeSummary.php

225 lines
8.0 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
2018-11-01 16:58:31 +03:00
use App\Models\Banking\Account;
use App\Models\Income\Customer;
2017-09-14 22:21:00 +03:00
use App\Models\Income\Invoice;
use App\Models\Income\InvoicePayment;
use App\Models\Income\Revenue;
use App\Models\Setting\Category;
use App\Utilities\Recurring;
2017-12-13 12:04:08 +03:00
use Charts;
2017-09-14 22:21:00 +03:00
use Date;
class IncomeSummary extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dates = $totals = $incomes = $incomes_graph = $categories = [];
$status = request('status');
2018-11-01 16:58:31 +03:00
$year = request('year', Date::now()->year);
2018-12-14 20:00:47 +05:30
// check and assign year start
if (($financial_start = Date::parse(setting('general.financial_start'))->month) != 1) {
2018-12-14 20:00:47 +05:30
// check if a specific year is requested
if (!is_null(request('year'))) {
$financial_start->year = $year;
}
$year = [$financial_start->format('Y'), $financial_start->addYear()->format('Y')];
$financial_start->subYear()->subMonth();
}
2017-09-14 22:21:00 +03:00
2018-11-12 15:09:46 +03:00
$categories = Category::enabled()->type('income')->orderBy('name')->pluck('name', 'id')->toArray();
2017-09-14 22:21:00 +03:00
2018-11-01 16:58:31 +03:00
if ($categories_filter = request('categories')) {
$cats = collect($categories)->filter(function ($value, $key) use ($categories_filter) {
return in_array($key, $categories_filter);
});
} else {
$cats = $categories;
2017-09-14 22:21:00 +03:00
}
// Dates
for ($j = 1; $j <= 12; $j++) {
2018-12-14 20:00:47 +05:30
$ym_string = is_array($year) ? $financial_start->addMonth()->format('Y-m') : $year . '-' . $j;
$dates[$j] = Date::parse($ym_string)->format('F');
2017-09-14 22:21:00 +03:00
2018-12-14 20:00:47 +05:30
$incomes_graph[Date::parse($ym_string)->format('F-Y')] = 0;
2017-09-14 22:21:00 +03:00
// Totals
$totals[$dates[$j]] = array(
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
2018-11-01 16:58:31 +03:00
foreach ($cats as $category_id => $category_name) {
$incomes[$category_id][$dates[$j]] = [
2017-09-14 22:21:00 +03:00
'category_id' => $category_id,
'name' => $category_name,
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
2018-11-01 16:58:31 +03:00
];
2017-09-14 22:21:00 +03:00
}
}
2018-11-01 16:58:31 +03:00
$revenues = Revenue::monthsOfYear('paid_at')->account(request('accounts'))->customer(request('customers'))->isNotTransfer()->get();
2017-09-14 22:21:00 +03:00
switch ($status) {
2017-11-07 05:11:03 +03:00
case 'paid':
// Invoices
2018-11-01 16:58:31 +03:00
$invoices = InvoicePayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
2017-11-07 05:11:03 +03:00
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'paid_at');
// Revenues
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
2017-09-14 22:21:00 +03:00
break;
case 'upcoming':
// Invoices
2018-11-01 16:58:31 +03:00
$invoices = Invoice::accrued()->monthsOfYear('due_at')->customer(request('customers'))->get();
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
2017-09-14 22:21:00 +03:00
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'due_at');
// Revenues
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
2017-09-14 22:21:00 +03:00
break;
default:
// Invoices
2018-11-01 16:58:31 +03:00
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->customer(request('customers'))->get();
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
2017-11-07 05:11:03 +03:00
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'invoiced_at');
2017-09-14 22:21:00 +03:00
// Revenues
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
break;
2017-09-14 22:21:00 +03:00
}
2018-11-01 16:58:31 +03:00
$statuses = collect([
'all' => trans('general.all'),
'paid' => trans('invoices.paid'),
'upcoming' => trans('dashboard.receivables'),
]);
$accounts = Account::enabled()->pluck('name', 'id')->toArray();
$customers = Customer::enabled()->pluck('name', 'id')->toArray();
2018-04-07 12:00:39 +03:00
// Check if it's a print or normal request
if (request('print')) {
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line_print';
$view_template = 'reports.income_summary.print';
} else {
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line';
$view_template = 'reports.income_summary.index';
}
2018-12-23 15:07:18 +05:30
$print_url = $this->getPrintUrl(is_array($year) ? $year[0] : $year);
2018-12-03 01:05:59 +03:00
2017-12-13 12:04:08 +03:00
// Incomes chart
$chart = Charts::multi('line', 'chartjs')
->dimensions(0, 300)
->colors(['#00c0ef'])
->dataset(trans_choice('general.incomes', 1), $incomes_graph)
->labels($dates)
->credits(false)
2018-04-07 12:00:39 +03:00
->view($chart_template);
2017-09-14 22:21:00 +03:00
2018-12-03 01:05:59 +03:00
return view($view_template, compact(
'chart',
'dates',
'categories',
'statuses',
'accounts',
'customers',
'incomes',
'totals',
'print_url'
));
2017-09-14 22:21:00 +03:00
}
private function setAmount(&$graph, &$totals, &$incomes, $items, $type, $date_field)
{
foreach ($items as $item) {
2018-11-01 16:58:31 +03:00
switch ($item->getTable()) {
case 'invoice_payments':
$invoice = $item->invoice;
if ($customers = request('customers')) {
if (!in_array($invoice->customer_id, $customers)) {
continue;
}
}
$item->category_id = $invoice->category_id;
break;
case 'invoices':
if ($accounts = request('accounts')) {
foreach ($item->payments as $payment) {
if (!in_array($payment->account_id, $accounts)) {
continue 2;
}
}
}
break;
}
2018-11-29 12:05:12 +03:00
$month = Date::parse($item->$date_field)->format('F');
$month_year = Date::parse($item->$date_field)->format('F-Y');
2017-09-14 22:21:00 +03:00
2018-11-29 12:05:12 +03:00
if (!isset($incomes[$item->category_id]) || !isset($incomes[$item->category_id][$month]) || !isset($graph[$month_year])) {
2017-09-14 22:21:00 +03:00
continue;
}
$amount = $item->getConvertedAmount();
// Forecasting
if (($type == 'invoice') && ($date_field == 'due_at')) {
foreach ($item->payments as $payment) {
$amount -= $payment->getConvertedAmount();
}
}
2018-11-29 12:05:12 +03:00
$incomes[$item->category_id][$month]['amount'] += $amount;
$incomes[$item->category_id][$month]['currency_code'] = $item->currency_code;
$incomes[$item->category_id][$month]['currency_rate'] = $item->currency_rate;
2017-09-14 22:21:00 +03:00
2018-11-29 12:05:12 +03:00
$graph[$month_year] += $amount;
2017-09-14 22:21:00 +03:00
2018-11-29 12:05:12 +03:00
$totals[$month]['amount'] += $amount;
2017-09-14 22:21:00 +03:00
}
}
2018-12-03 01:05:59 +03:00
private function getPrintUrl($year)
{
$print_url = 'reports/income-summary?print=1'
. '&status=' . request('status')
. '&year='. request('year', $year);
collect(request('accounts'))->each(function($item) use(&$print_url) {
$print_url .= '&accounts[]=' . $item;
});
collect(request('customers'))->each(function($item) use(&$print_url) {
$print_url .= '&customers[]=' . $item;
});
collect(request('categories'))->each(function($item) use(&$print_url) {
$print_url .= '&categories[]=' . $item;
});
return $print_url;
}
2017-09-14 22:21:00 +03:00
}