Skip to content

Commit

Permalink
added filters to reports
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Nov 1, 2018
1 parent de7dd93 commit 6c95593
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 114 deletions.
59 changes: 45 additions & 14 deletions app/Http/Controllers/Reports/ExpenseSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Http\Controllers\Reports;

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\Expense\Vendor;
use App\Models\Setting\Category;
use App\Utilities\Recurring;
use Charts;
Expand All @@ -23,13 +25,16 @@ public function index()
$dates = $totals = $expenses = $expenses_graph = $categories = [];

$status = request('status');
$year = request('year', Date::now()->year);

$categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();

// Get year
$year = request('year');
if (empty($year)) {
$year = Date::now()->year;
if ($categories_filter = request('categories')) {
$cats = collect($categories)->filter(function ($value, $key) use ($categories_filter) {
return in_array($key, $categories_filter);
});
} else {
$cats = $categories;
}

// Dates
Expand All @@ -45,7 +50,7 @@ public function index()
'currency_rate' => 1
);

foreach ($categories as $category_id => $category_name) {
foreach ($cats as $category_id => $category_name) {
$expenses[$category_id][$dates[$j]] = array(
'category_id' => $category_id,
'name' => $category_name,
Expand All @@ -56,20 +61,20 @@ public function index()
}
}

$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
$payments = Payment::monthsOfYear('paid_at')->account(request('accounts'))->vendor(request('vendors'))->isNotTransfer()->get();

switch ($status) {
case 'paid':
// Bills
$bills = BillPayment::monthsOfYear('paid_at')->get();
$bills = BillPayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'paid_at');

// Payments
$this->setAmount($expenses_graph, $totals, $expenses, $payments, 'payment', 'paid_at');
break;
case 'upcoming':
// Bills
$bills = Bill::accrued()->monthsOfYear('due_at')->get();
$bills = Bill::accrued()->monthsOfYear('due_at')->vendor(request('vendors'))->get();
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'due_at');

Expand All @@ -79,7 +84,7 @@ public function index()
break;
default:
// Bills
$bills = Bill::accrued()->monthsOfYear('billed_at')->get();
$bills = Bill::accrued()->monthsOfYear('billed_at')->vendor(request('vendors'))->get();
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'billed_at');

Expand All @@ -89,6 +94,15 @@ public function index()
break;
}

$statuses = collect([
'all' => trans('general.all'),
'paid' => trans('invoices.paid'),
'upcoming' => trans('dashboard.payables'),
]);

$accounts = Account::enabled()->pluck('name', 'id')->toArray();
$vendors = Vendor::enabled()->pluck('name', 'id')->toArray();

// Check if it's a print or normal request
if (request('print')) {
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line_print';
Expand All @@ -107,16 +121,33 @@ public function index()
->credits(false)
->view($chart_template);

return view($view_template, compact('chart', 'dates', 'categories', 'expenses', 'totals'));
return view($view_template, compact('chart', 'dates', 'categories', 'statuses', 'vendors', 'expenses', 'totals'));
}

private function setAmount(&$graph, &$totals, &$expenses, $items, $type, $date_field)
{
foreach ($items as $item) {
if ($item->getTable() == 'bill_payments') {
$bill = $item->bill;

$item->category_id = $bill->category_id;
switch ($item->getTable()) {
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 'bills':
if ($accounts = request('accounts')) {
foreach ($item->payments as $payment) {
if (!in_array($payment->account_id, $accounts)) {
continue 2;
}
}
}
break;
}

$date = Date::parse($item->$date_field)->format('F');
Expand Down
83 changes: 66 additions & 17 deletions app/Http/Controllers/Reports/IncomeExpenseSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace App\Http\Controllers\Reports;

use App\Http\Controllers\Controller;
use App\Models\Banking\Account;
use App\Models\Income\Customer;
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;
use App\Models\Expense\Vendor;
use App\Models\Setting\Category;
use App\Utilities\Recurring;
use Charts;
Expand All @@ -26,16 +29,16 @@ public function index()
$dates = $totals = $compares = $profit_graph = $categories = [];

$status = request('status');
$year = request('year', Date::now()->year);
$categories_filter = request('categories');

$income_categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
$income_categories = Category::enabled()->type('income')->when($categories_filter, function ($query) use ($categories_filter) {
return $query->whereIn('id', $categories_filter);
})->pluck('name', 'id')->toArray();

$expense_categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();

// Get year
$year = request('year');
if (empty($year)) {
$year = Date::now()->year;
}
$expense_categories = Category::enabled()->type('expense')->when($categories_filter, function ($query) use ($categories_filter) {
return $query->whereIn('id', $categories_filter);
})->pluck('name', 'id')->toArray();

// Dates
for ($j = 1; $j <= 12; $j++) {
Expand Down Expand Up @@ -71,28 +74,28 @@ public function index()
}
}

$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
$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();

switch ($status) {
case 'paid':
// Invoices
$invoices = InvoicePayment::monthsOfYear('paid_at')->get();
$invoices = InvoicePayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'paid_at');

// Revenues
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');

// Bills
$bills = BillPayment::monthsOfYear('paid_at')->get();
$bills = BillPayment::monthsOfYear('paid_at')->account(request('accounts'))->get();
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'paid_at');

// Payments
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
break;
case 'upcoming':
// Invoices
$invoices = Invoice::accrued()->monthsOfYear('due_at')->get();
$invoices = Invoice::accrued()->monthsOfYear('due_at')->customer(request('customers'))->get();
Recurring::reflect($invoices, 'invoice', 'due_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'due_at');

Expand All @@ -101,7 +104,7 @@ public function index()
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');

// Bills
$bills = Bill::accrued()->monthsOfYear('due_at')->get();
$bills = Bill::accrued()->monthsOfYear('due_at')->vendor(request('vendors'))->get();
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'due_at');

Expand All @@ -111,7 +114,7 @@ public function index()
break;
default:
// Invoices
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get();
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->customer(request('customers'))->get();
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'invoiced_at');

Expand All @@ -120,7 +123,7 @@ public function index()
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');

// Bills
$bills = Bill::accrued()->monthsOfYear('billed_at')->get();
$bills = Bill::accrued()->monthsOfYear('billed_at')->vendor(request('vendors'))->get();
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'billed_at');

Expand All @@ -130,6 +133,17 @@ public function index()
break;
}

$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();

// Check if it's a print or normal request
if (request('print')) {
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line_print';
Expand All @@ -148,7 +162,7 @@ public function index()
->credits(false)
->view($chart_template);

return view($view_template, compact('chart', 'dates', 'income_categories', 'expense_categories', 'compares', 'totals'));
return view($view_template, compact('chart', 'dates', 'income_categories', 'expense_categories', 'categories', 'statuses', 'accounts', 'customers', 'vendors', 'compares', 'totals'));
}

private function setAmount(&$graph, &$totals, &$compares, $items, $type, $date_field)
Expand All @@ -160,6 +174,41 @@ private function setAmount(&$graph, &$totals, &$compares, $items, $type, $date_f
$item->category_id = $type_item->category_id;
}

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;
}

$date = Date::parse($item->$date_field)->format('F');

$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
Expand Down

0 comments on commit 6c95593

Please sign in to comment.