From 9ba2efbde665f02bd1a03e6bff710aeb5dc0acd9 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 7 Jul 2016 12:25:20 +0800 Subject: [PATCH] Update 2016-07-07.12.24 Add Payment with TDD Add Payment Model Factory Add default placeholder for FormField::select() --- app/Entities/BaseRepository.php | 6 ++ app/Http/Controllers/PaymentsController.php | 9 ++- app/Http/Requests/Payments/CreateRequest.php | 12 ++-- app/Http/Requests/Payments/DeleteRequest.php | 6 +- app/Http/Requests/Payments/UpdateRequest.php | 5 +- app/Services/FormField.php | 5 +- database/factories/ModelFactory.php | 18 ++++++ resources/views/payments/create.blade.php | 12 +++- resources/views/payments/delete.blade.php | 13 +++-- resources/views/payments/edit.blade.php | 5 +- tests/ManagePaymentsTest.php | 82 +++++++++++++++++++++++++++- 11 files changed, 147 insertions(+), 26 deletions(-) diff --git a/app/Entities/BaseRepository.php b/app/Entities/BaseRepository.php index 200fca9..9375b45 100755 --- a/app/Entities/BaseRepository.php +++ b/app/Entities/BaseRepository.php @@ -2,6 +2,7 @@ namespace App\Entities; +use App\Entities\Projects\Project; use App\Entities\Users\User; /** @@ -18,4 +19,9 @@ abstract class BaseRepository extends EloquentRepository { { return User::orderBy('name')->hasRoles(['worker'])->lists('name','id'); } + + public function getProjectsList() + { + return Project::orderBy('name')->lists('name','id'); + } } \ No newline at end of file diff --git a/app/Http/Controllers/PaymentsController.php b/app/Http/Controllers/PaymentsController.php index 32e2204..d811ece 100755 --- a/app/Http/Controllers/PaymentsController.php +++ b/app/Http/Controllers/PaymentsController.php @@ -27,14 +27,16 @@ class PaymentsController extends Controller { public function create() { - return view('payments.create'); + $projects = $this->repo->getProjectsList(); + $customers = $this->repo->getCustomersList(); + return view('payments.create',compact('projects','customers')); } public function store(CreateRequest $req) { $payment = $this->repo->create($req->except('_token')); flash()->success(trans('payment.created')); - return redirect()->route('payments.edit', $payment->id); + return redirect()->route('payments.show', $payment->id); } public function show($paymentId) @@ -46,8 +48,9 @@ class PaymentsController extends Controller { public function edit($paymentId) { $payment = $this->repo->requireById($paymentId); + $projects = $this->repo->getProjectsList(); $customers = $this->repo->getCustomersList(); - return view('payments.edit',compact('payment','customers')); + return view('payments.edit',compact('payment','projects','customers')); } public function update(UpdateRequest $req, $paymentId) diff --git a/app/Http/Requests/Payments/CreateRequest.php b/app/Http/Requests/Payments/CreateRequest.php index ca6e1bc..d4bec7a 100644 --- a/app/Http/Requests/Payments/CreateRequest.php +++ b/app/Http/Requests/Payments/CreateRequest.php @@ -1,6 +1,6 @@ user()->can('manage_masters'); + return auth()->user()->can('manage_payments'); } /** @@ -24,8 +24,12 @@ class CreateRequest extends Request { public function rules() { return [ - 'name' => 'required|max:60|unique:masters,name', - 'description' => 'max:255' + 'date' => 'required|date|date_format:Y-m-d', + 'type' => 'required|numeric', + 'amount' => 'required|numeric', + 'project_id' => 'required|numeric', + 'customer_id' => 'required|numeric', + 'description' => 'required|max:255', ]; } diff --git a/app/Http/Requests/Payments/DeleteRequest.php b/app/Http/Requests/Payments/DeleteRequest.php index b8d264c..1462f11 100644 --- a/app/Http/Requests/Payments/DeleteRequest.php +++ b/app/Http/Requests/Payments/DeleteRequest.php @@ -1,6 +1,6 @@ user()->can('manage_masters'); + return auth()->user()->can('manage_payments'); } /** @@ -24,7 +24,7 @@ class DeleteRequest extends Request { public function rules() { return [ - 'master_id' => 'required' + 'payment_id' => 'required' ]; } diff --git a/app/Http/Requests/Payments/UpdateRequest.php b/app/Http/Requests/Payments/UpdateRequest.php index 57274d8..67b99a2 100644 --- a/app/Http/Requests/Payments/UpdateRequest.php +++ b/app/Http/Requests/Payments/UpdateRequest.php @@ -24,11 +24,12 @@ class UpdateRequest extends Request { public function rules() { return [ + 'date' => 'required|date|date_format:Y-m-d', 'type' => 'required|numeric', 'amount' => 'required|numeric', - 'date' => 'required|date|date_format:Y-m-d', + 'project_id' => 'required|numeric', 'customer_id' => 'required|numeric', - 'description' => 'required|max:255' + 'description' => 'required|max:255', ]; } diff --git a/app/Services/FormField.php b/app/Services/FormField.php index ff4726e..65c27d9 100644 --- a/app/Services/FormField.php +++ b/app/Services/FormField.php @@ -101,7 +101,10 @@ class FormField if (isset($options['disabled']) && $options['disabled'] == true) { $fieldParams += ['disabled']; } if (isset($options['required']) && $options['required'] == true) { $fieldParams += ['required']; } if (isset($options['multiple']) && $options['multiple'] == true) { $fieldParams += ['multiple', 'name' => $name . '[]']; } - if (isset($options['placeholder'])) { $fieldParams += ['placeholder' => $options['placeholder']]; } + if (isset($options['placeholder'])) + $fieldParams += ['placeholder' => $options['placeholder']]; + else + $fieldParams += ['placeholder' => '-- Pilih ' . str_split_ucwords(str_replace('_id', '', $name)) . ' --']; $htmlForm .= $this->setFormFieldLabel($name, $options); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index b87ea0d..83bcdb6 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -1,5 +1,6 @@ define(Project::class, function (Faker\Generator $faker) { 'customer_id' => $customer->id ]; }); + +$factory->define(Payment::class, function (Faker\Generator $faker) { + $projectId = factory(Project::class)->create()->id; + + $customer = factory(User::class)->create(); + $customer->assignRole('customer'); + $customerId = $customer->id; + + return [ + 'project_id' => $projectId, + 'amount' => rand(1,5) * 500000, + 'type' => rand(0,1), + 'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'), + 'description' => $faker->paragraph, + 'customer_id' => $customerId, + ]; +}); \ No newline at end of file diff --git a/resources/views/payments/create.blade.php b/resources/views/payments/create.blade.php index 5f41e78..3f77680 100755 --- a/resources/views/payments/create.blade.php +++ b/resources/views/payments/create.blade.php @@ -9,9 +9,17 @@

{{ trans('payment.create') }}

- {!! FormField::text('date',['label'=> trans('payment.date')]) !!} {!! FormField::radios('type',['Pengeluaran','Pemasukan'],['label'=> trans('payment.type'),'value' => 1]) !!} - {!! FormField::price('amount', ['label'=> trans('payment.amount')]) !!} +
+
+ {!! FormField::text('date',['label'=> trans('payment.date')]) !!} +
+
+ {!! FormField::price('amount', ['label'=> trans('payment.amount')]) !!} +
+
+ {!! FormField::select('project_id', $projects, ['label'=> trans('payment.project')]) !!} + {!! FormField::select('customer_id', $customers, ['label'=> trans('payment.customer')]) !!} {!! FormField::textarea('description',['label'=> trans('payment.description')]) !!}
diff --git a/resources/views/payments/delete.blade.php b/resources/views/payments/delete.blade.php index 41491b0..676c3e0 100755 --- a/resources/views/payments/delete.blade.php +++ b/resources/views/payments/delete.blade.php @@ -1,24 +1,25 @@ @extends('layouts.app') -@section('title', trans('master.delete')) +@section('title', trans('payment.delete')) @section('content')

- {!! delete_button(['route'=>['masters.destroy',$master->id]], trans('app.delete_confirm_button'), ['class'=>'btn btn-danger'], ['master_id'=>$master->id]) !!} + {!! FormField::delete(['route'=>['payments.destroy',$payment->id]], trans('app.delete_confirm_button'), ['class'=>'btn btn-danger'], ['payment_id'=>$payment->id]) !!}
{{ trans('app.delete_confirm') }} - {!! link_to_route('masters.show', trans('app.cancel'), [$master->id], ['class' => 'btn btn-default']) !!} + {!! link_to_route('payments.show', trans('app.cancel'), [$payment->id], ['class' => 'btn btn-default']) !!}

-

{{ trans('master.masters') }} Detail

+

{{ trans('payment.payments') }} Detail

- - + + +
{{ trans('app.name') }}{{ $master->name }}
{{ trans('app.description') }}{{ $master->description }}
{{ trans('payment.date') }}{{ $payment->date }}
{{ trans('payment.amount') }}{{ formatRp($payment->amount) }}
{{ trans('payment.description') }}{{ $payment->description }}
diff --git a/resources/views/payments/edit.blade.php b/resources/views/payments/edit.blade.php index a6df842..1cbafb5 100755 --- a/resources/views/payments/edit.blade.php +++ b/resources/views/payments/edit.blade.php @@ -8,16 +8,17 @@ {!! Form::model($payment, ['route'=>['payments.update', $payment->id], 'method' => 'patch']) !!}

{{ trans('payment.edit') }}

-
+
{!! FormField::radios('type', ['Pengeluaran','Pemasukan'], ['label'=> trans('payment.type')]) !!}
- {!! FormField::text('date',['label'=> trans('payment.date')]) !!} + {!! FormField::text('date',['label'=> trans('app.date')]) !!}
{!! FormField::text('amount',['label'=> trans('payment.amount'),'addon' => ['before'=>'Rp'],'type' => 'number','class' => 'text-right']) !!}
+ {!! FormField::select('project_id', $projects, ['label'=> trans('payment.project')]) !!} {!! FormField::select('customer_id', $customers, ['label'=> trans('payment.customer')]) !!} {!! FormField::textarea('description',['label'=> trans('payment.description')]) !!}
diff --git a/tests/ManagePaymentsTest.php b/tests/ManagePaymentsTest.php index d4c7d22..1166b43 100644 --- a/tests/ManagePaymentsTest.php +++ b/tests/ManagePaymentsTest.php @@ -1,14 +1,90 @@ create(); + $user->assignRole('admin'); + $this->actingAs($user); + + $project = factory(Project::class)->create(); + + $customer = factory(User::class)->create(); + $customer->assignRole('customer'); + + $this->visit('payments'); + $this->seePageIs('payments'); + $this->click(trans('payment.create')); + + // Fill Form + $this->seePageIs('payments/create'); + $this->type('2015-05-01','date'); + $this->select(1,'type'); + $this->type(1000000,'amount'); + $this->select($project->id, 'project_id'); + $this->select($customer->id, 'customer_id'); + $this->type('Pembayaran DP','description'); + $this->press(trans('payment.create')); + + $this->see(trans('payment.created')); + $this->seeInDatabase('payments', ['project_id' => $project->id,'amount' => 1000000,'type' => 1,'date' => '2015-05-01']); + } + /** @test */ - public function () + public function admin_can_edit_payment_data() { - $this->visit('/'); + $user = factory(User::class)->create(); + $user->assignRole('admin'); + $this->actingAs($user); + + $project = factory(Project::class)->create(); + + $customer = factory(User::class)->create(); + $customer->assignRole('customer'); + + $payment = factory(Payment::class)->create(['customer_id' => $customer->id, 'project_id' => $project->id]); + + $this->visit('payments/' . $payment->id . '/edit'); + $this->seePageIs('payments/' . $payment->id . '/edit'); + $this->type('2016-05-20','date'); + $this->select(1, 'type'); + $this->type(1234567890,'amount'); + $this->press(trans('payment.update')); + + $this->seePageIs('payments/' . $payment->id . '/edit'); + $this->see(trans('payment.updated')); + $this->seeInDatabase('payments', [ + 'customer_id' => $customer->id, + 'project_id' => $project->id, + 'date' => '2016-05-20', + 'amount' => 1234567890, + ]); + } + + /** @test */ + public function admin_can_delete_a_payment() + { + $user = factory(User::class)->create(); + $user->assignRole('admin'); + $this->actingAs($user); + + $payment = factory(Payment::class)->create(); + $this->visit('/payments'); + $this->click(trans('app.edit')); + $this->click(trans('payment.delete')); + $this->press(trans('app.delete_confirm_button')); + $this->seePageIs('payments'); + $this->see(trans('payment.deleted')); } }