v2 first commit
This commit is contained in:
@ -2,20 +2,17 @@
|
||||
|
||||
namespace App\Http\Controllers\Banking;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Exports\Banking\Transactions as Export;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Imports\Banking\Transactions as Import;
|
||||
use App\Jobs\Banking\DeleteTransaction;
|
||||
use App\Models\Banking\Account;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Expense\BillPayment;
|
||||
use App\Models\Expense\Payment;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\Revenue;
|
||||
use App\Models\Setting\Category;
|
||||
|
||||
class Transactions extends Controller
|
||||
{
|
||||
|
||||
public $transactions = [];
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@ -23,79 +20,70 @@ class Transactions extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$request = request();
|
||||
|
||||
$accounts = collect(Account::enabled()->pluck('name', 'id'));
|
||||
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$types = collect(['expense' => 'Expense', 'income' => 'Income'])
|
||||
->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), '');
|
||||
|
||||
$type = $request->get('type');
|
||||
$request_type = !request()->has('type') ? ['income', 'expense'] : request('type');
|
||||
$categories = Category::enabled()->type($request_type)->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$type_cats = empty($type) ? ['income', 'expense'] : $type;
|
||||
$categories = collect(Category::enabled()->type($type_cats)->pluck('name', 'id'));
|
||||
|
||||
if ($type != 'income') {
|
||||
$this->addTransactions(Payment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1));
|
||||
$this->addTransactions(BillPayment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1));
|
||||
}
|
||||
|
||||
if ($type != 'expense') {
|
||||
$this->addTransactions(Revenue::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1));
|
||||
$this->addTransactions(InvoicePayment::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1));
|
||||
}
|
||||
|
||||
$transactions = $this->getTransactions($request);
|
||||
$transactions = Transaction::with(['account', 'category', 'contact'])->collect(['paid_at'=> 'desc']);
|
||||
|
||||
return view('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items to transactions array.
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param $items
|
||||
* @param $type
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function addTransactions($items, $type)
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
if (!empty($item->category)) {
|
||||
$category_name = ($item->category) ? $item->category->name : trans('general.na');
|
||||
} else {
|
||||
if ($type == trans_choice('general.incomes', 1)) {
|
||||
$category_name = ($item->invoice->category) ? $item->invoice->category->name : trans('general.na');
|
||||
} else {
|
||||
$category_name = ($item->bill->category) ? $item->bill->category->name : trans('general.na');
|
||||
}
|
||||
}
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
$this->transactions[] = (object) [
|
||||
'paid_at' => $item->paid_at,
|
||||
'account_name' => $item->account->name,
|
||||
'type' => $type,
|
||||
'description' => $item->description,
|
||||
'amount' => $item->amount,
|
||||
'currency_code' => $item->currency_code,
|
||||
'category_name' => $category_name,
|
||||
];
|
||||
}
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.transactions', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('transactions.index');
|
||||
}
|
||||
|
||||
protected function getTransactions($request)
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Transaction $transaction)
|
||||
{
|
||||
// Sort items
|
||||
if (isset($request['sort'])) {
|
||||
if ($request['order'] == 'asc') {
|
||||
$f = 'sortBy';
|
||||
} else {
|
||||
$f = 'sortByDesc';
|
||||
}
|
||||
$response = $this->ajaxDispatch(new DeleteTransaction($transaction));
|
||||
|
||||
$transactions = collect($this->transactions)->$f($request['sort']);
|
||||
$response['redirect'] = url()->previous();
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.transactions', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$transactions = collect($this->transactions)->sortByDesc('paid_at');
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return $transactions;
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return \Excel::download(new Export(), trans_choice('general.transactions', 2) . '.xlsx');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user