Browse Source

Update 2016-07-07.12.24

Add Payment with TDD
Add Payment Model Factory
Add default placeholder for FormField::select()
pull/1/head
Nafies Luthfi 10 years ago
parent
commit
9ba2efbde6
  1. 6
      app/Entities/BaseRepository.php
  2. 9
      app/Http/Controllers/PaymentsController.php
  3. 12
      app/Http/Requests/Payments/CreateRequest.php
  4. 6
      app/Http/Requests/Payments/DeleteRequest.php
  5. 5
      app/Http/Requests/Payments/UpdateRequest.php
  6. 5
      app/Services/FormField.php
  7. 18
      database/factories/ModelFactory.php
  8. 12
      resources/views/payments/create.blade.php
  9. 13
      resources/views/payments/delete.blade.php
  10. 5
      resources/views/payments/edit.blade.php
  11. 82
      tests/ManagePaymentsTest.php

6
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');
}
}

9
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)

12
app/Http/Requests/Payments/CreateRequest.php

@ -1,6 +1,6 @@
<?php
namespace App\Http\Requests\Masters;
namespace App\Http\Requests\Payments;
use App\Http\Requests\Request;
@ -13,7 +13,7 @@ class CreateRequest extends Request {
*/
public function authorize()
{
return auth()->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',
];
}

6
app/Http/Requests/Payments/DeleteRequest.php

@ -1,6 +1,6 @@
<?php
namespace App\Http\Requests\Masters;
namespace App\Http\Requests\Payments;
use App\Http\Requests\Request;
@ -13,7 +13,7 @@ class DeleteRequest extends Request {
*/
public function authorize()
{
return auth()->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'
];
}

5
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',
];
}

5
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);

18
database/factories/ModelFactory.php

@ -1,5 +1,6 @@
<?php
use App\Entities\Payments\Payment;
use App\Entities\Projects\Project;
use App\Entities\Users\User;
@ -43,3 +44,20 @@ $factory->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,
];
});

12
resources/views/payments/create.blade.php

@ -9,9 +9,17 @@
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ trans('payment.create') }}</h3></div>
<div class="panel-body">
{!! 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')]) !!}
<div class="row">
<div class="col-md-6">
{!! FormField::text('date',['label'=> trans('payment.date')]) !!}
</div>
<div class="col-md-6">
{!! FormField::price('amount', ['label'=> trans('payment.amount')]) !!}
</div>
</div>
{!! 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')]) !!}
</div>

13
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')
<h1 class="page-header">
<div class="pull-right">
{!! 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]) !!}
</div>
{{ 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']) !!}
</h1>
<div class="row">
<div class="col-md-4">
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title">{{ trans('master.masters') }} Detail</h3></div>
<div class="panel-heading"><h3 class="panel-title">{{ trans('payment.payments') }} Detail</h3></div>
<div class="panel-body">
<table class="table table-condensed">
<tbody>
<tr><th>{{ trans('app.name') }}</th><td>{{ $master->name }}</td></tr>
<tr><th>{{ trans('app.description') }}</th><td>{{ $master->description }}</td></tr>
<tr><th>{{ trans('payment.date') }}</th><td>{{ $payment->date }}</td></tr>
<tr><th>{{ trans('payment.amount') }}</th><td class="text-right">{{ formatRp($payment->amount) }}</td></tr>
<tr><th>{{ trans('payment.description') }}</th><td>{{ $payment->description }}</td></tr>
</tbody>
</table>
</div>

5
resources/views/payments/edit.blade.php

@ -8,16 +8,17 @@
{!! Form::model($payment, ['route'=>['payments.update', $payment->id], 'method' => 'patch']) !!}
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ trans('payment.edit') }}</h3></div>
<div class="panel-body">
<div class="panel-body">
{!! FormField::radios('type', ['Pengeluaran','Pemasukan'], ['label'=> trans('payment.type')]) !!}
<div class="row">
<div class="col-md-6">
{!! FormField::text('date',['label'=> trans('payment.date')]) !!}
{!! FormField::text('date',['label'=> trans('app.date')]) !!}
</div>
<div class="col-md-6">
{!! FormField::text('amount',['label'=> trans('payment.amount'),'addon' => ['before'=>'Rp'],'type' => 'number','class' => 'text-right']) !!}
</div>
</div>
{!! 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')]) !!}
</div>

82
tests/ManagePaymentsTest.php

@ -1,14 +1,90 @@
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Entities\Payments\Payment;
use App\Entities\Projects\Project;
use App\Entities\Users\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
class ManagePaymentsTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function admin_can_entry_project_a_payment()
{
$user = factory(User::class)->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'));
}
}
Loading…
Cancel
Save