Browse Source

Merge pull request #22 from nafiesl/bank-account-entries

Bank Account Entries from Their Own Table. Resolves #19.
pull/24/head
Nafies Luthfi 7 years ago
committed by GitHub
parent
commit
4a1ab11d9b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      app/Entities/Invoices/BankAccount.php
  2. 4
      app/Http/Controllers/Invoices/InvoicesController.php
  3. 112
      app/Http/Controllers/References/BankAccountsController.php
  4. 12
      composer.lock
  5. 12
      database/factories/BankAccountFactory.php
  6. 36
      database/migrations/2018_10_30_215937_create_bank_accounts_table.php
  7. 2
      resources/lang/de/bank_account.php
  8. 2
      resources/lang/en/bank_account.php
  9. 2
      resources/lang/id/bank_account.php
  10. 1
      resources/views/bank-accounts/forms.blade.php
  11. 22
      resources/views/bank-accounts/index.blade.php
  12. 1
      routes/web/references.php
  13. 91
      tests/Feature/References/ManageBankAccountsTest.php
  14. 22
      tests/Unit/Models/BankAccountTest.php

27
app/Entities/Invoices/BankAccount.php

@ -0,0 +1,27 @@
<?php
namespace App\Entities\Invoices;
use Illuminate\Database\Eloquent\Model;
class BankAccount extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'number', 'account_name', 'description', 'is_active',
];
/**
* Get status attribute.
*
* @return string
*/
public function getStatusAttribute()
{
return $this->is_active == 1 ? __('app.active') : __('app.in_active');
}
}

4
app/Http/Controllers/Invoices/InvoicesController.php

@ -2,10 +2,10 @@
namespace App\Http\Controllers\Invoices;
use Option;
use App\Entities\Invoices\Invoice;
use App\Entities\Projects\Project;
use App\Http\Controllers\Controller;
use App\Entities\Invoices\BankAccount;
/**
* Invoices Controller.
@ -72,7 +72,7 @@ class InvoicesController extends Controller
public function pdf(Invoice $invoice)
{
$bankAccounts = json_decode(Option::get('bank_accounts'), true) ?: [];
$bankAccounts = BankAccount::where('is_active', 1)->get();
return view('invoices.pdf', compact('invoice', 'bankAccounts'));
}

112
app/Http/Controllers/References/BankAccountsController.php

@ -5,6 +5,7 @@ namespace App\Http\Controllers\References;
use Illuminate\Http\Request;
use App\Entities\Options\Option;
use App\Http\Controllers\Controller;
use App\Entities\Invoices\BankAccount;
/**
* Bank Account Controller.
@ -14,28 +15,17 @@ use App\Http\Controllers\Controller;
class BankAccountsController extends Controller
{
/**
* Display a listing of the bankAccount.
* Display a listing of the bank account.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\View\View
*/
public function index()
{
$editableBankAccount = null;
$bankAccounts = Option::where('key', 'bank_accounts')->first();
if (!is_null($bankAccounts)) {
$bankAccounts = $bankAccounts->value;
$bankAccounts = json_decode($bankAccounts, true);
$bankAccounts = collect($bankAccounts)
->map(function ($bankAccount) {
return (object) $bankAccount;
});
$bankAccounts = BankAccount::all();
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
$editableBankAccount = $bankAccounts[request('id')];
}
} else {
$bankAccounts = collect([]);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
$editableBankAccount = BankAccount::find(request('id'));
}
return view('bank-accounts.index', compact('bankAccounts', 'editableBankAccount'));
@ -44,9 +34,8 @@ class BankAccountsController extends Controller
/**
* Store a newly created bank account in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
@ -57,24 +46,7 @@ class BankAccountsController extends Controller
'description' => 'nullable|max:255',
]);
$option = Option::firstOrNew(['key' => 'bank_accounts']);
if ($option->exists) {
$bankAccounts = $option->value;
$bankAccounts = json_decode($bankAccounts, true);
if ($bankAccounts == []) {
$bankAccounts[1] = $newBankAccount;
} else {
$bankAccounts[] = $newBankAccount;
}
} else {
$bankAccounts = [];
$bankAccounts[1] = $newBankAccount;
}
$bankAccounts = json_encode($bankAccounts);
$option->value = $bankAccounts;
$option->save();
BankAccount::create($newBankAccount);
flash(trans('bank_account.created'), 'success');
@ -84,32 +56,21 @@ class BankAccountsController extends Controller
/**
* Update the specified bank account in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Entities\Invoices\BankAccount $bankAccount
*
* @return \Illuminate\Http\Response
* @param \Illuminate\Http\Request $request
* @param \App\Entities\Invoices\BankAccount $bankAccount
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $bankAccountId)
public function update(Request $request, BankAccount $bankAccount)
{
$bankAccountData = $request->validate([
'name' => 'required|max:60',
'number' => 'required|max:60',
'account_name' => 'required|max:60',
'description' => 'nullable|max:255',
'is_active' => 'required|in:0,1',
]);
$bankAccounts = Option::where('key', 'bank_accounts')->first();
$bankAccounts = $bankAccounts->value;
$bankAccounts = json_decode($bankAccounts, true);
$bankAccounts[$bankAccountId] = $bankAccountData;
$bankAccounts = json_encode($bankAccounts);
$option = Option::where('key', 'bank_accounts')->first();
$option->value = $bankAccounts;
$option->save();
$bankAccount->update($bankAccountData);
flash(trans('bank_account.updated'), 'success');
@ -119,30 +80,16 @@ class BankAccountsController extends Controller
/**
* Remove the specified bank account from storage.
*
* @param \App\Entities\Invoices\BankAccount $bankAccount
*
* @return \Illuminate\Http\Response
* @param \App\Entities\Invoices\BankAccount $bankAccount
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($bankAccountId)
public function destroy(BankAccount $bankAccount)
{
request()->validate([
'bank_account_id' => 'required',
]);
if (request('bank_account_id') == $bankAccountId) {
$bankAccounts = Option::where('key', 'bank_accounts')->first();
$bankAccounts = $bankAccounts->value;
$bankAccounts = json_decode($bankAccounts, true);
unset($bankAccounts[$bankAccountId]);
$bankAccounts = json_encode($bankAccounts);
$option = Option::where('key', 'bank_accounts')->first();
$option->value = $bankAccounts;
$option->save();
if (request('bank_account_id') == $bankAccount->id && $bankAccount->delete()) {
flash(trans('bank_account.deleted'), 'success');
return redirect()->route('bank-accounts.index');
@ -150,4 +97,25 @@ class BankAccountsController extends Controller
return back();
}
/**
* Import bank account from site_options table.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function import()
{
$bankAccounts = Option::where('key', 'bank_accounts')->first();
if ($bankAccounts && $bankAccounts->value) {
$bankAccountList = json_decode($bankAccounts->value, true);
foreach ($bankAccountList as $bankAccountData) {
$bankAccount = new BankAccount($bankAccountData);
$bankAccount->save();
}
$bankAccounts->delete();
flash(__('bank_account.imported', ['count' => count($bankAccountList)]), 'success');
}
return back();
}
}

12
composer.lock

@ -1,7 +1,7 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "fd28a38cbcaf3bccc1d6fb34444372ee",
@ -3008,16 +3008,16 @@
},
{
"name": "luthfi/simple-crud-generator",
"version": "1.2.3",
"version": "1.2.7",
"source": {
"type": "git",
"url": "https://github.com/nafiesl/SimpleCrudGenerator.git",
"reference": "6a64e62c3b91eb92739b7c7b89d2e3439d180c94"
"reference": "0dc629fab1c1708eedf545aa81c428905e7e4864"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nafiesl/SimpleCrudGenerator/zipball/6a64e62c3b91eb92739b7c7b89d2e3439d180c94",
"reference": "6a64e62c3b91eb92739b7c7b89d2e3439d180c94",
"url": "https://api.github.com/repos/nafiesl/SimpleCrudGenerator/zipball/0dc629fab1c1708eedf545aa81c428905e7e4864",
"reference": "0dc629fab1c1708eedf545aa81c428905e7e4864",
"shasum": ""
},
"require": {
@ -3060,7 +3060,7 @@
"tdd-workflow",
"testing"
],
"time": "2018-09-10T09:02:29+00:00"
"time": "2018-10-23T09:49:27+00:00"
},
{
"name": "maximebf/debugbar",

12
database/factories/BankAccountFactory.php

@ -0,0 +1,12 @@
<?php
use Faker\Generator as Faker;
use App\Entities\Invoices\BankAccount;
$factory->define(BankAccount::class, function (Faker $faker) {
return [
'name' => 'Bank '.strtoupper(str_random(4)),
'number' => str_random(10),
'account_name' => $faker->name,
];
});

36
database/migrations/2018_10_30_215937_create_bank_accounts_table.php

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBankAccountsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bank_accounts', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 60);
$table->string('number', 30);
$table->string('account_name', 60);
$table->string('description')->nullable();
$table->boolean('is_active')->default(1); // 1:active, 0:in_active
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('bank_accounts');
}
}

2
resources/lang/de/bank_account.php

@ -8,6 +8,8 @@ return [
'back_to_index' => 'Zurück zur Bankverbindunsliste',
// Actions
'import' => 'Import Bank Account',
'imported' => ':count bank account(s) has imported.',
'create' => 'Neue Bankverbindung erstellen',
'created' => 'Neue Bankverbindung wurde erstellt.',
'show' => 'Zeige Bankverbindung Details',

2
resources/lang/en/bank_account.php

@ -8,6 +8,8 @@ return [
'back_to_index' => 'Back to Bank Account list',
// Actions
'import' => 'Import Bank Account',
'imported' => ':count bank account(s) has imported.',
'create' => 'Create new Bank Account',
'created' => 'New Bank Account has been created.',
'show' => 'Show Bank Account detail',

2
resources/lang/id/bank_account.php

@ -8,6 +8,8 @@ return [
'back_to_index' => 'Kembali ke daftar Rekening Bank',
// Actions
'import' => 'Import Rekening Bank',
'imported' => ':count rekening bank telah diimport.',
'create' => 'Input Rekening Bank Baru',
'created' => 'Input Rekening Bank baru telah berhasil.',
'show' => 'Lihat Detail Rekening Bank',

1
resources/views/bank-accounts/forms.blade.php

@ -24,6 +24,7 @@
{!! FormField::text('number', ['required' => true, 'label' => trans('bank_account.number')]) !!}
{!! FormField::text('account_name', ['required' => true, 'label' => trans('bank_account.account_name')]) !!}
{!! FormField::textarea('description', ['label' => trans('bank_account.description')]) !!}
{!! FormField::radios('is_active', [__('app.in_active'), __('app.active')], ['label' => __('app.status')]) !!}
</div>
<div class="panel-footer">
{!! Form::submit(trans('bank_account.update'), ['class' => 'btn btn-success']) !!}

22
resources/views/bank-accounts/index.blade.php

@ -3,14 +3,16 @@
@section('title', trans('bank_account.list'))
@section('content-dashboard')
<div class="row">
<div class="col-md-8">
@foreach ($bankAccounts as $key => $bankAccount)
@foreach ($bankAccounts as $bankAccount)
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading text-center"><h3 class="panel-title">{{ $bankAccount->name }}</h3></div>
<div class="panel-heading text-center">
<h3 class="panel-title">{{ $bankAccount->name }}</h3>
</div>
<div class="panel-body">
<span class="pull-right">{{ $bankAccount->status }}</span>
<p>{{ trans('bank_account.number') }}:<br><strong class="lead">{{ $bankAccount->number }}</strong></p>
<p>{{ trans('bank_account.account_name') }}:<br><strong class="lead">{{ $bankAccount->account_name }}</strong></p>
@if ($bankAccount->description)
@ -21,14 +23,14 @@
{!! link_to_route(
'bank-accounts.index',
trans('app.edit'),
['action' => 'edit', 'id' => $key],
['id' => 'edit-bank_account-' . $key]
['action' => 'edit', 'id' => $bankAccount->id],
['id' => 'edit-bank_account-' . $bankAccount->id]
) !!}
{!! link_to_route(
'bank-accounts.index',
trans('app.delete'),
['action' => 'delete', 'id' => $key],
['id' => 'del-bank_account-' . $key, 'class' => 'pull-right']
['action' => 'delete', 'id' => $bankAccount->id],
['id' => 'del-bank_account-' . $bankAccount->id, 'class' => 'pull-right']
) !!}
</div>
</div>
@ -43,6 +45,12 @@
]) !!}
@endif
@includeWhen(Request::has('action'), 'bank-accounts.forms')
@if (Option::get('bank_accounts'))
{!! FormField::formButton(['route' => 'bank-accounts.import'], __('bank_account.import'), [
'id' => 'import-bank-accounts',
'class' => 'btn btn-primary',
]) !!}
@endif
</div>
</div>
@endsection

1
routes/web/references.php

@ -19,4 +19,5 @@ Route::group(['namespace' => 'References', 'middleware' => ['web', 'role:admin']
* Bank Accounts Routes
*/
Route::apiResource('bank-accounts', 'BankAccountsController');
Route::post('bank-accounts/import', 'BankAccountsController@import')->name('bank-accounts.import');
});

91
tests/Feature/References/ManageBankAccountsTest.php

@ -2,8 +2,9 @@
namespace Tests\Feature\References;
use Option;
use Tests\TestCase as TestCase;
use Tests\TestCase;
use App\Entities\Options\Option;
use App\Entities\Invoices\BankAccount;
use Illuminate\Foundation\Testing\DatabaseMigrations;
/**
@ -19,7 +20,10 @@ class ManageBankAccountsTest extends TestCase
public function user_can_see_bank_account_list_in_bank_account_index_page()
{
$this->adminUserSigningIn();
$bankAccount = factory(BankAccount::class)->create();
$this->visit(route('bank-accounts.index'));
$this->seeText($bankAccount->name);
}
/** @test */
@ -28,10 +32,10 @@ class ManageBankAccountsTest extends TestCase
$this->adminUserSigningIn();
$this->visit(route('bank-accounts.index'));
$this->click(trans('bank_account.create'));
$this->click(__('bank_account.create'));
$this->seePageIs(route('bank-accounts.index', ['action' => 'create']));
$this->submitForm(trans('bank_account.create'), [
$this->submitForm(__('bank_account.create'), [
'name' => 'BankAccount 1 name',
'number' => '1234567890',
'account_name' => 'John Doe',
@ -40,57 +44,44 @@ class ManageBankAccountsTest extends TestCase
$this->seePageIs(route('bank-accounts.index'));
$bankAccounts = [];
$bankAccounts[1] = [
$this->seeInDatabase('bank_accounts', [
'name' => 'BankAccount 1 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 1 description',
];
$this->seeInDatabase('site_options', [
'value' => json_encode($bankAccounts),
]);
}
/** @test */
public function user_can_edit_a_bank_account_within_search_query()
public function user_can_edit_a_bank_account()
{
$this->adminUserSigningIn();
$bankAccounts = [];
$bankAccounts[1] = [
'name' => 'BankAccount 1 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 1 description',
];
Option::set('bank_accounts', json_encode($bankAccounts));
$bankAccount = factory(BankAccount::class)->create();
$this->visit(route('bank-accounts.index'));
$this->click('edit-bank_account-1');
$this->seePageIs(route('bank-accounts.index', ['action' => 'edit', 'id' => '1']));
$this->submitForm(trans('bank_account.update'), [
$this->seePageIs(route('bank-accounts.index', [
'action' => 'edit', 'id' => $bankAccount->id,
]));
$this->submitForm(__('bank_account.update'), [
'name' => 'BankAccount 2 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 2 description',
'is_active' => 0,
]);
$this->seePageIs(route('bank-accounts.index'));
$bankAccounts[1] = [
$this->seeInDatabase('bank_accounts', [
'name' => 'BankAccount 2 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 2 description',
];
$this->seeInDatabase('site_options', [
'value' => json_encode($bankAccounts),
'is_active' => 0,
]);
}
@ -99,28 +90,56 @@ class ManageBankAccountsTest extends TestCase
{
$this->adminUserSigningIn();
$bankAccount = factory(BankAccount::class)->create();
$this->visit(route('bank-accounts.index'));
$this->click('del-bank_account-'.$bankAccount->id);
$this->seePageIs(route('bank-accounts.index', [
'action' => 'delete', 'id' => $bankAccount->id,
]));
$this->press(__('app.delete_confirm_button'));
$this->dontSeeInDatabase('bank_accounts', [
'id' => $bankAccount->id,
]);
}
/** @test */
public function user_can_import_existing_bank_account_list()
{
$this->adminUserSigningIn();
$bankAccounts = [];
$bankAccounts[2] = [
$bankAccounts[1] = [
'name' => 'BankAccount 1 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 1 description',
];
Option::set('bank_accounts', json_encode($bankAccounts));
$this->seeInDatabase('site_options', [
Option::create([
'key' => 'bank_accounts',
'value' => json_encode($bankAccounts),
]);
$this->visit(route('bank-accounts.index'));
$this->click('del-bank_account-2');
$this->seePageIs(route('bank-accounts.index', ['action' => 'delete', 'id' => '2']));
$this->press(trans('app.delete_confirm_button'));
$this->seeElement('button', ['id' => 'import-bank-accounts']);
$this->press('import-bank-accounts');
$this->seePageIs(route('bank-accounts.index'));
$this->seeText(__('bank_account.imported', ['count' => 1]));
$this->dontSeeInDatabase('site_options', [
'value' => json_encode($bankAccounts),
]);
$this->seeInDatabase('bank_accounts', [
'name' => 'BankAccount 1 name',
'number' => '1234567890',
'account_name' => 'John Doe',
'description' => 'BankAccount 1 description',
'is_active' => 1,
]);
}
}

22
tests/Unit/Models/BankAccountTest.php

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Entities\Invoices\BankAccount;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class BankAccountTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function a_bank_account_has_status_attribute()
{
$bankAccount = factory(BankAccount::class)->make(['is_active' => 1]);
$this->assertEquals(__('app.active'), $bankAccount->status);
$bankAccount->is_active = 0;
$this->assertEquals(__('app.in_active'), $bankAccount->status);
}
}
Loading…
Cancel
Save