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

90 lines
2.5 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Banking;
2019-11-16 10:21:14 +03:00
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;
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-10-31 00:50:41 +03:00
use App\Models\Setting\Category;
2017-09-14 22:21:00 +03:00
class Transactions extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2019-11-16 10:21:14 +03:00
$accounts = Account::enabled()->orderBy('name')->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)]), '');
2019-11-16 10:21:14 +03:00
$request_type = !request()->has('type') ? ['income', 'expense'] : request('type');
$categories = Category::enabled()->type($request_type)->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2020-06-07 12:11:37 +03:00
$transactions = Transaction::with('account', 'category', 'contact')->collect(['paid_at'=> 'desc']);
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
/**
2019-11-16 10:21:14 +03:00
* Import the specified resource.
*
* @param ImportRequest $request
2017-12-26 11:56:43 +03:00
*
2019-11-16 10:21:14 +03:00
* @return Response
2017-12-26 11:56:43 +03:00
*/
2019-11-16 10:21:14 +03:00
public function import(ImportRequest $request)
2017-12-26 11:56:43 +03:00
{
2019-11-16 10:21:14 +03:00
\Excel::import(new Import(), $request->file('import'));
$message = trans('messages.success.imported', ['type' => trans_choice('general.transactions', 2)]);
flash($message)->success();
return redirect()->route('transactions.index');
2017-12-26 11:56:43 +03:00
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
/**
* Remove the specified resource from storage.
*
* @param Transaction $transaction
*
* @return Response
*/
public function destroy(Transaction $transaction)
2017-12-26 11:56:43 +03:00
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new DeleteTransaction($transaction));
$response['redirect'] = url()->previous();
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => trans_choice('general.transactions', 1)]);
flash($message)->success();
2017-12-26 11:56:43 +03:00
} else {
2019-11-16 10:21:14 +03:00
$message = $response['message'];
flash($message)->error();
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
return response()->json($response);
}
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
2020-02-12 12:18:08 +03:00
return \Excel::download(new Export(), \Str::filename(trans_choice('general.transactions', 2)) . '.xlsx');
2017-09-14 22:21:00 +03:00
}
}