2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2016-01-20 00:07:31 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreateBankAccountRequest;
|
2016-01-20 00:07:31 +01:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\BankAccount;
|
2016-01-26 21:22:33 +01:00
|
|
|
use App\Ninja\Repositories\BankAccountRepository;
|
2016-01-20 00:07:31 +01:00
|
|
|
use App\Services\BankAccountService;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use Crypt;
|
|
|
|
use File;
|
2016-05-15 12:58:11 +02:00
|
|
|
use Illuminate\Http\Request;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2016-01-20 00:07:31 +01:00
|
|
|
|
|
|
|
class BankAccountController extends BaseController
|
|
|
|
{
|
|
|
|
protected $bankAccountService;
|
2016-01-26 21:22:33 +01:00
|
|
|
protected $bankAccountRepo;
|
2016-01-20 00:07:31 +01:00
|
|
|
|
2016-01-26 21:22:33 +01:00
|
|
|
public function __construct(BankAccountService $bankAccountService, BankAccountRepository $bankAccountRepo)
|
2016-01-20 00:07:31 +01:00
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2016-01-20 00:07:31 +01:00
|
|
|
|
|
|
|
$this->bankAccountService = $bankAccountService;
|
2016-01-26 21:22:33 +01:00
|
|
|
$this->bankAccountRepo = $bankAccountRepo;
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Redirect::to('settings/' . ACCOUNT_BANKS);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable()
|
|
|
|
{
|
|
|
|
return $this->bankAccountService->getDatatable(Auth::user()->account_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$bankAccount = BankAccount::scope($publicId)->firstOrFail();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'title' => trans('texts.edit_bank_account'),
|
|
|
|
'banks' => Cache::get('banks'),
|
|
|
|
'bankAccount' => $bankAccount,
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('accounts.bank_account', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Displays the form for account creation.
|
2016-01-20 00:07:31 +01:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'banks' => Cache::get('banks'),
|
|
|
|
'bankAccount' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('accounts.bank_account', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = Input::get('bulk_action');
|
|
|
|
$ids = Input::get('bulk_public_id');
|
|
|
|
$count = $this->bankAccountService->bulk($ids, $action);
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.archived_bank_account'));
|
|
|
|
|
|
|
|
return Redirect::to('settings/' . ACCOUNT_BANKS);
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:22:33 +01:00
|
|
|
public function validateAccount()
|
2016-01-20 00:07:31 +01:00
|
|
|
{
|
2016-01-26 21:22:33 +01:00
|
|
|
$publicId = Input::get('public_id');
|
|
|
|
$username = trim(Input::get('bank_username'));
|
|
|
|
$password = trim(Input::get('bank_password'));
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-01-26 21:22:33 +01:00
|
|
|
if ($publicId) {
|
|
|
|
$bankAccount = BankAccount::scope($publicId)->firstOrFail();
|
|
|
|
if ($username != $bankAccount->username) {
|
|
|
|
// TODO update username
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
2016-01-26 21:22:33 +01:00
|
|
|
$username = Crypt::decrypt($username);
|
|
|
|
$bankId = $bankAccount->bank_id;
|
|
|
|
} else {
|
2017-07-11 12:22:11 +02:00
|
|
|
$bankAccount = new BankAccount;
|
|
|
|
$bankAccount->bank_id = Input::get('bank_id');
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
2016-01-26 21:22:33 +01:00
|
|
|
|
2017-07-11 12:22:11 +02:00
|
|
|
$bankAccount->app_version = Input::get('app_version');
|
|
|
|
$bankAccount->ofx_version = Input::get('ofx_version');
|
|
|
|
|
|
|
|
if ($publicId) {
|
|
|
|
$bankAccount->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($this->bankAccountService->loadBankAccounts($bankAccount, $username, $password, $publicId));
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 21:22:33 +01:00
|
|
|
public function store(CreateBankAccountRequest $request)
|
2016-01-20 00:07:31 +01:00
|
|
|
{
|
2016-01-26 21:22:33 +01:00
|
|
|
$bankAccount = $this->bankAccountRepo->save(Input::all());
|
|
|
|
|
2016-01-20 00:07:31 +01:00
|
|
|
$bankId = Input::get('bank_id');
|
2016-01-26 21:22:33 +01:00
|
|
|
$username = trim(Input::get('bank_username'));
|
|
|
|
$password = trim(Input::get('bank_password'));
|
2016-01-20 00:07:31 +01:00
|
|
|
|
2017-07-11 12:22:11 +02:00
|
|
|
return json_encode($this->bankAccountService->loadBankAccounts($bankAccount, $username, $password, true));
|
2016-01-26 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function importExpenses($bankId)
|
|
|
|
{
|
|
|
|
return $this->bankAccountService->importExpenses($bankId, Input::all());
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
|
|
|
|
2016-05-15 12:58:11 +02:00
|
|
|
public function showImportOFX()
|
|
|
|
{
|
|
|
|
return view('accounts.import_ofx');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doImportOFX(Request $request)
|
|
|
|
{
|
|
|
|
$file = File::get($request->file('ofx_file'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$data = $this->bankAccountService->parseOFX($file);
|
|
|
|
} catch (\Exception $e) {
|
2017-06-29 16:17:33 +02:00
|
|
|
Session::now('error', trans('texts.ofx_parse_failed'));
|
2016-08-22 12:32:10 +02:00
|
|
|
Utils::logError($e);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-05-15 12:58:11 +02:00
|
|
|
return view('accounts.import_ofx');
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'banks' => null,
|
|
|
|
'bankAccount' => null,
|
2017-01-30 20:40:43 +01:00
|
|
|
'transactions' => json_encode([$data]),
|
2016-05-15 12:58:11 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('accounts.bank_account', $data);
|
|
|
|
}
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|