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\Expense\Bill;
|
|
|
|
use App\Models\Expense\BillPayment;
|
|
|
|
use App\Models\Expense\Payment;
|
2018-11-01 16:58:31 +03:00
|
|
|
use App\Models\Expense\Vendor;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Models\Setting\Category;
|
2018-10-30 13:01:33 +03:00
|
|
|
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 IncomeExpenseSummary extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2017-12-13 12:04:08 +03:00
|
|
|
$dates = $totals = $compares = $profit_graph = $categories = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$status = request('status');
|
2018-11-01 16:58:31 +03:00
|
|
|
$year = request('year', Date::now()->year);
|
|
|
|
$categories_filter = request('categories');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-01 16:58:31 +03:00
|
|
|
$income_categories = Category::enabled()->type('income')->when($categories_filter, function ($query) use ($categories_filter) {
|
|
|
|
return $query->whereIn('id', $categories_filter);
|
2018-11-12 15:09:46 +03:00
|
|
|
})->orderBy('name')->pluck('name', 'id')->toArray();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-01 16:58:31 +03:00
|
|
|
$expense_categories = Category::enabled()->type('expense')->when($categories_filter, function ($query) use ($categories_filter) {
|
|
|
|
return $query->whereIn('id', $categories_filter);
|
2018-11-12 15:09:46 +03:00
|
|
|
})->orderBy('name')->pluck('name', 'id')->toArray();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
// Dates
|
|
|
|
for ($j = 1; $j <= 12; $j++) {
|
|
|
|
$dates[$j] = Date::parse($year . '-' . $j)->format('F');
|
|
|
|
|
2017-12-13 12:04:08 +03:00
|
|
|
$profit_graph[Date::parse($year . '-' . $j)->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
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($income_categories as $category_id => $category_name) {
|
|
|
|
$compares['income'][$category_id][$dates[$j]] = array(
|
|
|
|
'category_id' => $category_id,
|
|
|
|
'name' => $category_name,
|
|
|
|
'amount' => 0,
|
|
|
|
'currency_code' => setting('general.default_currency'),
|
|
|
|
'currency_rate' => 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($expense_categories as $category_id => $category_name) {
|
|
|
|
$compares['expense'][$category_id][$dates[$j]] = array(
|
|
|
|
'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
|
|
|
$revenues = Revenue::monthsOfYear('paid_at')->account(request('accounts'))->customer(request('customers'))->isNotTransfer()->get();
|
|
|
|
$payments = Payment::monthsOfYear('paid_at')->account(request('accounts'))->vendor(request('vendors'))->isNotTransfer()->get();
|
2018-10-30 13:01:33 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
switch ($status) {
|
2017-11-07 05:11:03 +03:00
|
|
|
case 'paid':
|
2018-10-30 13:01:33 +03:00
|
|
|
// Invoices
|
2018-11-01 16:58:31 +03:00
|
|
|
$invoices = InvoicePayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
|
2017-12-13 12:04:08 +03:00
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-10-30 13:01:33 +03:00
|
|
|
// Revenues
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-10-30 13:01:33 +03:00
|
|
|
// Bills
|
2018-11-01 16:58:31 +03:00
|
|
|
$bills = BillPayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
|
2017-12-13 12:04:08 +03:00
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'paid_at');
|
2018-10-30 13:01:33 +03:00
|
|
|
|
|
|
|
// Payments
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
case 'upcoming':
|
2018-10-30 13:01:33 +03:00
|
|
|
// Invoices
|
2018-11-01 16:58:31 +03:00
|
|
|
$invoices = Invoice::accrued()->monthsOfYear('due_at')->customer(request('customers'))->get();
|
2018-10-30 13:01:33 +03:00
|
|
|
Recurring::reflect($invoices, 'invoice', 'due_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'due_at');
|
|
|
|
|
|
|
|
// Revenues
|
|
|
|
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
|
|
|
|
|
|
|
|
// Bills
|
2018-11-01 16:58:31 +03:00
|
|
|
$bills = Bill::accrued()->monthsOfYear('due_at')->vendor(request('vendors'))->get();
|
2018-10-30 13:01:33 +03:00
|
|
|
Recurring::reflect($bills, 'bill', 'billed_at', $status);
|
2017-12-13 12:04:08 +03:00
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'due_at');
|
2018-10-30 13:01:33 +03:00
|
|
|
|
|
|
|
// Payments
|
|
|
|
Recurring::reflect($payments, 'payment', 'paid_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-30 13:01:33 +03:00
|
|
|
// Invoices
|
2018-11-01 16:58:31 +03:00
|
|
|
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->customer(request('customers'))->get();
|
2018-10-30 13:01:33 +03:00
|
|
|
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'invoiced_at');
|
|
|
|
|
|
|
|
// Revenues
|
|
|
|
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
|
|
|
|
|
|
|
|
// Bills
|
2018-11-01 16:58:31 +03:00
|
|
|
$bills = Bill::accrued()->monthsOfYear('billed_at')->vendor(request('vendors'))->get();
|
2018-10-30 13:01:33 +03:00
|
|
|
Recurring::reflect($bills, 'bill', 'billed_at', $status);
|
2017-12-13 12:04:08 +03:00
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'billed_at');
|
2018-10-30 13:01:33 +03:00
|
|
|
|
|
|
|
// Payments
|
|
|
|
Recurring::reflect($payments, 'payment', 'paid_at', $status);
|
|
|
|
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:58:31 +03:00
|
|
|
$statuses = collect([
|
|
|
|
'all' => trans('general.all'),
|
|
|
|
'paid' => trans('invoices.paid'),
|
|
|
|
'upcoming' => trans('general.upcoming'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$accounts = Account::enabled()->pluck('name', 'id')->toArray();
|
|
|
|
$customers = Customer::enabled()->pluck('name', 'id')->toArray();
|
|
|
|
$vendors = Vendor::enabled()->pluck('name', 'id')->toArray();
|
|
|
|
$categories = Category::enabled()->type(['income', 'expense'])->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_expense_summary.print';
|
|
|
|
} else {
|
|
|
|
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line';
|
|
|
|
$view_template = 'reports.income_expense_summary.index';
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:04:08 +03:00
|
|
|
// Profit chart
|
|
|
|
$chart = Charts::multi('line', 'chartjs')
|
|
|
|
->dimensions(0, 300)
|
|
|
|
->colors(['#6da252'])
|
|
|
|
->dataset(trans_choice('general.profits', 1), $profit_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-11-01 16:58:31 +03:00
|
|
|
return view($view_template, compact('chart', 'dates', 'income_categories', 'expense_categories', 'categories', 'statuses', 'accounts', 'customers', 'vendors', 'compares', 'totals'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function setAmount(&$graph, &$totals, &$compares, $items, $type, $date_field)
|
|
|
|
{
|
|
|
|
foreach ($items as $item) {
|
2018-10-30 13:01:33 +03:00
|
|
|
if ($item->getTable() == 'bill_payments' || $item->getTable() == 'invoice_payments') {
|
2018-07-19 14:54:23 +03:00
|
|
|
$type_item = $item->$type;
|
|
|
|
|
|
|
|
$item->category_id = $type_item->category_id;
|
|
|
|
}
|
|
|
|
|
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 'bill_payments':
|
|
|
|
$bill = $item->bill;
|
|
|
|
|
|
|
|
if ($vendors = request('vendors')) {
|
|
|
|
if (!in_array($bill->vendor_id, $vendors)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->category_id = $bill->category_id;
|
|
|
|
break;
|
|
|
|
case 'invoices':
|
|
|
|
case 'bills':
|
|
|
|
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-04-23 22:17:20 +03:00
|
|
|
$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-29 12:05:12 +03:00
|
|
|
if (!isset($compares[$group][$item->category_id]) || !isset($compares[$group][$item->category_id][$month]) || !isset($graph[$month_year])) {
|
2017-09-14 22:21:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$amount = $item->getConvertedAmount();
|
|
|
|
|
|
|
|
// Forecasting
|
|
|
|
if ((($type == 'invoice') || ($type == 'bill')) && ($date_field == 'due_at')) {
|
|
|
|
foreach ($item->payments as $payment) {
|
|
|
|
$amount -= $payment->getConvertedAmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:05:12 +03:00
|
|
|
$compares[$group][$item->category_id][$month]['amount'] += $amount;
|
|
|
|
$compares[$group][$item->category_id][$month]['currency_code'] = $item->currency_code;
|
|
|
|
$compares[$group][$item->category_id][$month]['currency_rate'] = $item->currency_rate;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($group == 'income') {
|
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
|
|
|
} else {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|