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

100 lines
2.9 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' => trans_choice('general.expenses', 1), 'income' => trans_choice('general.incomes', 1)])
2017-10-31 00:50:41 +03:00
->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
2020-11-06 00:43:46 +03:00
return $this->response('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
2017-12-26 11:56:43 +03:00
}
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
{
2021-02-11 18:08:06 +03:00
$response = $this->importExcel(new Import, $request);
2019-11-16 10:21:14 +03:00
2021-02-11 18:08:06 +03:00
if ($response['success']) {
$response['redirect'] = route('transactions.index');
2019-11-16 10:21:14 +03:00
2021-02-11 18:08:06 +03:00
$message = trans('messages.success.imported', ['type' => trans_choice('general.transactions', 1)]);
2019-11-16 10:21:14 +03:00
2021-02-11 18:08:06 +03:00
flash($message)->success();
} else {
$response['redirect'] = route('import.create', ['banking', 'transactions']);
$message = $response['message'];
flash($message)->error()->important();
}
return response()->json($response);
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()->important();
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()
{
return $this->exportExcel(new Export, trans_choice('general.transactions', 2));
2017-09-14 22:21:00 +03:00
}
}