fixed #169
This commit is contained in:
parent
8ff9cdf983
commit
04ec2af3c8
@ -5,13 +5,17 @@ namespace App\Http\Controllers\Banking;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
use App\Models\Banking\Transaction;
|
use App\Models\Banking\Transaction;
|
||||||
|
use App\Models\Expense\BillPayment;
|
||||||
use App\Models\Expense\Payment;
|
use App\Models\Expense\Payment;
|
||||||
|
use App\Models\Income\InvoicePayment;
|
||||||
use App\Models\Income\Revenue;
|
use App\Models\Income\Revenue;
|
||||||
use App\Models\Setting\Category;
|
use App\Models\Setting\Category;
|
||||||
|
|
||||||
class Transactions extends Controller
|
class Transactions extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public $transactions = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
@ -21,8 +25,6 @@ class Transactions extends Controller
|
|||||||
{
|
{
|
||||||
$request = request();
|
$request = request();
|
||||||
|
|
||||||
$transactions = array();
|
|
||||||
|
|
||||||
$accounts = collect(Account::enabled()->pluck('name', 'id'))
|
$accounts = collect(Account::enabled()->pluck('name', 'id'))
|
||||||
->prepend(trans('general.all_type', ['type' => trans_choice('general.accounts', 2)]), '');
|
->prepend(trans('general.all_type', ['type' => trans_choice('general.accounts', 2)]), '');
|
||||||
|
|
||||||
@ -35,62 +37,64 @@ class Transactions extends Controller
|
|||||||
$type = $request->get('type');
|
$type = $request->get('type');
|
||||||
|
|
||||||
if ($type != 'income') {
|
if ($type != 'income') {
|
||||||
$payments = Payment::collect('paid_at');
|
$this->addTransactions(Payment::collect('paid_at'), trans_choice('general.expenses', 1));
|
||||||
|
$this->addTransactions(BillPayment::collect('paid_at'), trans_choice('general.expenses', 1), trans_choice('general.bills', 1));
|
||||||
foreach ($payments as $payment) {
|
|
||||||
$transactions[] = (object)[
|
|
||||||
'paid_at' => $payment->paid_at,
|
|
||||||
'account_name' => $payment->account->name,
|
|
||||||
'type' => trans_choice('general.expenses', 1),
|
|
||||||
'category_name' => $payment->category->name,
|
|
||||||
'description' => $payment->description,
|
|
||||||
'amount' => $payment->amount,
|
|
||||||
'currency_code' => $payment->currency_code,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type != 'expense') {
|
if ($type != 'expense') {
|
||||||
$revenues = Revenue::collect('paid_at');
|
$this->addTransactions(Revenue::collect('paid_at'), trans_choice('general.incomes', 1));
|
||||||
|
$this->addTransactions(InvoicePayment::collect('paid_at'), trans_choice('general.incomes', 1), trans_choice('general.invoices', 1));
|
||||||
foreach ($revenues as $revenue) {
|
|
||||||
$transactions[] = (object)[
|
|
||||||
'paid_at' => $revenue->paid_at,
|
|
||||||
'account_name' => $revenue->account->name,
|
|
||||||
'type' => trans_choice('general.incomes', 1),
|
|
||||||
'category_name' => $revenue->category->name,
|
|
||||||
'description' => $revenue->description,
|
|
||||||
'amount' => $revenue->amount,
|
|
||||||
'currency_code' => $revenue->currency_code,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$special_key = array(
|
$transactions = $this->getTransactions($request);
|
||||||
'account.name' => 'account_name',
|
|
||||||
'category.name' => 'category_name',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isset($request['sort']) && array_key_exists($request['sort'], $special_key)) {
|
|
||||||
$sort_order = array();
|
|
||||||
|
|
||||||
foreach ($transactions as $key => $value) {
|
|
||||||
$sort = $request['sort'];
|
|
||||||
|
|
||||||
if (array_key_exists($request['sort'], $special_key)) {
|
|
||||||
$sort = $special_key[$request['sort']];
|
|
||||||
}
|
|
||||||
|
|
||||||
$sort_order[$key] = $value->{$sort};
|
|
||||||
}
|
|
||||||
|
|
||||||
$sort_type = (isset($request['order']) && $request['order'] == 'asc') ? SORT_ASC : SORT_DESC;
|
|
||||||
|
|
||||||
array_multisort($sort_order, $sort_type, $transactions);
|
|
||||||
}
|
|
||||||
|
|
||||||
$transactions = (object) $transactions;
|
|
||||||
|
|
||||||
return view('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
|
return view('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add items to transactions array.
|
||||||
|
*
|
||||||
|
* @param $items
|
||||||
|
* @param $type
|
||||||
|
* @param $category
|
||||||
|
*/
|
||||||
|
protected function addTransactions($items, $type, $category = null)
|
||||||
|
{
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$data = [
|
||||||
|
'paid_at' => $item->paid_at,
|
||||||
|
'account_name' => $item->account->name,
|
||||||
|
'type' => $type,
|
||||||
|
'description' => $item->description,
|
||||||
|
'amount' => $item->amount,
|
||||||
|
'currency_code' => $item->currency_code,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!is_null($category)) {
|
||||||
|
$data['category_name'] = $category;
|
||||||
|
} else {
|
||||||
|
$data['category_name'] = $item->category->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->transactions[] = (object) $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getTransactions($request)
|
||||||
|
{
|
||||||
|
// Sort items
|
||||||
|
if (isset($request['sort'])) {
|
||||||
|
if ($request['order'] == 'asc') {
|
||||||
|
$f = 'sortBy';
|
||||||
|
} else {
|
||||||
|
$f = 'sortByDesc';
|
||||||
|
}
|
||||||
|
|
||||||
|
$transactions = collect($this->transactions)->$f($request['sort']);
|
||||||
|
} else {
|
||||||
|
$transactions = collect($this->transactions)->sortByDesc('paid_at');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $transactions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,9 +28,9 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="col-md-2">@sortablelink('paid_at', trans('general.date'))</th>
|
<th class="col-md-2">@sortablelink('paid_at', trans('general.date'))</th>
|
||||||
<th class="col-md-2">@sortablelink('account.name', trans('accounts.account_name'))</th>
|
<th class="col-md-2">@sortablelink('account_name', trans('accounts.account_name'))</th>
|
||||||
<th class="col-md-2">@sortablelink('type', trans_choice('general.types', 1))</th>
|
<th class="col-md-2">@sortablelink('type', trans_choice('general.types', 1))</th>
|
||||||
<th class="col-md-2">@sortablelink('category.name', trans_choice('general.categories', 1))</th>
|
<th class="col-md-2">@sortablelink('category_name', trans_choice('general.categories', 1))</th>
|
||||||
<th class="col-md-2">@sortablelink('description', trans('general.description'))</th>
|
<th class="col-md-2">@sortablelink('description', trans('general.description'))</th>
|
||||||
<th class="col-md-2">@sortablelink('amount', trans('general.amount'))</th>
|
<th class="col-md-2">@sortablelink('amount', trans('general.amount'))</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user