akaunting/app/Http/Controllers/Banking/Transactions.php

102 lines
3.2 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Banking;
use App\Http\Controllers\Controller;
2017-10-31 00:50:41 +03:00
use App\Models\Banking\Account;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Transaction;
2017-12-26 11:56:43 +03:00
use App\Models\Expense\BillPayment;
2017-09-14 22:21:00 +03:00
use App\Models\Expense\Payment;
2017-12-26 11:56:43 +03:00
use App\Models\Income\InvoicePayment;
2017-09-14 22:21:00 +03:00
use App\Models\Income\Revenue;
2017-10-31 00:50:41 +03:00
use App\Models\Setting\Category;
2017-09-14 22:21:00 +03:00
class Transactions extends Controller
{
2017-12-26 11:56:43 +03:00
public $transactions = [];
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$request = request();
2017-10-31 00:50:41 +03:00
2018-11-02 11:32:42 +03:00
$accounts = collect(Account::enabled()->pluck('name', 'id'));
2017-10-31 00:50:41 +03:00
$types = collect(['expense' => 'Expense', 'income' => 'Income'])
->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), '');
$type = $request->get('type');
2018-10-20 16:29:48 +03:00
$type_cats = empty($type) ? ['income', 'expense'] : $type;
2018-11-02 11:32:42 +03:00
$categories = collect(Category::enabled()->type($type_cats)->pluck('name', 'id'));
2018-10-20 16:29:48 +03:00
2017-10-31 00:50:41 +03:00
if ($type != 'income') {
$this->addTransactions(Payment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1));
2018-10-24 17:32:00 +03:00
$this->addTransactions(BillPayment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1));
2017-09-14 22:21:00 +03:00
}
2017-10-31 00:50:41 +03:00
if ($type != 'expense') {
$this->addTransactions(Revenue::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1));
2018-10-24 17:32:00 +03:00
$this->addTransactions(InvoicePayment::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1));
2017-09-14 22:21:00 +03:00
}
2017-12-26 11:56:43 +03:00
$transactions = $this->getTransactions($request);
2017-09-14 22:21:00 +03:00
2017-12-26 11:56:43 +03:00
return view('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
}
2017-09-14 22:21:00 +03:00
2017-12-26 11:56:43 +03:00
/**
* Add items to transactions array.
*
* @param $items
* @param $type
*/
2018-10-24 17:32:00 +03:00
protected function addTransactions($items, $type)
2017-12-26 11:56:43 +03:00
{
foreach ($items as $item) {
2018-10-24 17:32:00 +03:00
if (!empty($item->category)) {
$category_name = $item->category->name;
} else {
if ($type == trans_choice('general.incomes', 1)) {
$category_name = $item->invoice->category->name;
} else {
$category_name = $item->bill->category->name;
}
}
$this->transactions[] = (object) [
2017-12-26 11:56:43 +03:00
'paid_at' => $item->paid_at,
'account_name' => $item->account->name,
'type' => $type,
'description' => $item->description,
'amount' => $item->amount,
'currency_code' => $item->currency_code,
2018-10-24 17:32:00 +03:00
'category_name' => $category_name,
2017-12-26 11:56:43 +03:00
];
}
}
2017-09-14 22:21:00 +03:00
2017-12-26 11:56:43 +03:00
protected function getTransactions($request)
{
// Sort items
if (isset($request['sort'])) {
if ($request['order'] == 'asc') {
$f = 'sortBy';
} else {
$f = 'sortByDesc';
2017-09-14 22:21:00 +03:00
}
2017-12-26 11:56:43 +03:00
$transactions = collect($this->transactions)->$f($request['sort']);
} else {
$transactions = collect($this->transactions)->sortByDesc('paid_at');
2017-09-14 22:21:00 +03:00
}
2017-12-26 11:56:43 +03:00
return $transactions;
2017-09-14 22:21:00 +03:00
}
}