Browse Source

Change customer_id to partner_id in payments table, pass all tests

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
dc24e0facb
  1. 4
      app/Entities/Payments/Payment.php
  2. 11
      app/Entities/Payments/PaymentsRepository.php
  3. 2
      app/Http/Controllers/PaymentsController.php
  4. 5
      app/Http/Requests/Payments/CreateRequest.php
  5. 5
      app/Http/Requests/Payments/UpdateRequest.php
  6. 30
      database/factories/PaymentFactory.php
  7. 9
      database/migrations/2016_11_15_151228_create_payments_table.php
  8. 2
      resources/views/payments/create.blade.php
  9. 2
      resources/views/payments/edit.blade.php
  10. 7
      resources/views/payments/index.blade.php
  11. 2
      resources/views/payments/partials/payment-show.blade.php
  12. 2
      resources/views/projects/payments.blade.php
  13. 20
      tests/Feature/Payments/ManagePaymentsTest.php
  14. 18
      tests/Feature/Payments/PaymentSearchTest.php
  15. 4
      tests/Unit/Models/PaymentTest.php

4
app/Entities/Payments/Payment.php

@ -31,10 +31,10 @@ class Payment extends Model
public function partner() public function partner()
{ {
if ($this->in_out == 1) { if ($this->in_out == 1) {
return $this->belongsTo(Customer::class, 'customer_id');
return $this->belongsTo(Customer::class, 'partner_id');
} }
return $this->belongsTo(Vendor::class, 'customer_id');
return $this->belongsTo(Vendor::class, 'partner_id');
} }
public function type() public function type()

11
app/Entities/Payments/PaymentsRepository.php

@ -25,11 +25,11 @@ class PaymentsRepository extends BaseRepository
} }
}) })
->where(function ($query) use ($queryStrings) { ->where(function ($query) use ($queryStrings) {
if (isset($queryStrings['customer_id'])) {
$query->where('customer_id', $queryStrings['customer_id']);
if (isset($queryStrings['partner_id'])) {
$query->where('partner_id', $queryStrings['partner_id']);
} }
}) })
->with('customer','project')
->with('partner', 'project')
->whereOwnerId(auth()->id()) ->whereOwnerId(auth()->id())
->paginate($this->_paginate); ->paginate($this->_paginate);
} }
@ -44,7 +44,10 @@ class PaymentsRepository extends BaseRepository
public function update($paymentData = [], $paymentId) public function update($paymentData = [], $paymentId)
{ {
foreach ($paymentData as $key => $value) { foreach ($paymentData as $key => $value) {
if (!$paymentData[$key]) $paymentData[$key] = null;
if ( ! $paymentData[$key]) {
$paymentData[$key] = null;
}
} }
$paymentData['amount'] = str_replace('.', '', $paymentData['amount']); $paymentData['amount'] = str_replace('.', '', $paymentData['amount']);

2
app/Http/Controllers/PaymentsController.php

@ -21,7 +21,7 @@ class PaymentsController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$payments = $this->repo->getPayments($request->only('q', 'customer_id'));
$payments = $this->repo->getPayments($request->only('q', 'partner_id'));
$usersList = User::pluck('name', 'id')->all(); $usersList = User::pluck('name', 'id')->all();
return view('payments.index', compact('payments', 'usersList')); return view('payments.index', compact('payments', 'usersList'));
} }

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

@ -4,7 +4,8 @@ namespace App\Http\Requests\Payments;
use App\Http\Requests\Request; use App\Http\Requests\Request;
class CreateRequest extends Request {
class CreateRequest extends Request
{
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
@ -29,7 +30,7 @@ class CreateRequest extends Request {
'amount' => 'required', 'amount' => 'required',
'project_id' => 'required|numeric', 'project_id' => 'required|numeric',
'type_id' => 'required|numeric', 'type_id' => 'required|numeric',
'customer_id' => 'required|numeric',
'partner_id' => 'required|numeric',
'description' => 'required|max:255', 'description' => 'required|max:255',
]; ];
} }

5
app/Http/Requests/Payments/UpdateRequest.php

@ -4,7 +4,8 @@ namespace App\Http\Requests\Payments;
use App\Http\Requests\Request; use App\Http\Requests\Request;
class UpdateRequest extends Request {
class UpdateRequest extends Request
{
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
@ -29,7 +30,7 @@ class UpdateRequest extends Request {
'amount' => 'required', 'amount' => 'required',
'project_id' => 'required|numeric', 'project_id' => 'required|numeric',
'type_id' => 'required|numeric', 'type_id' => 'required|numeric',
'customer_id' => 'required|numeric',
'partner_id' => 'required|numeric',
'description' => 'required|max:255', 'description' => 'required|max:255',
]; ];
} }

30
database/factories/PaymentFactory.php

@ -21,25 +21,45 @@ $factory->define(Payment::class, function (Faker $faker) {
'owner_id' => function () { 'owner_id' => function () {
return factory(User::class)->create()->id; return factory(User::class)->create()->id;
}, },
'customer_id' => function () {
'partner_id' => function () {
return factory(Customer::class)->create()->id; return factory(Customer::class)->create()->id;
}, },
]; ];
}); });
$factory->state(Payment::class, 'income', function (Faker $faker) {
$factory->defineAs(Payment::class, 'income', function (Faker $faker) {
return [ return [
'project_id' => function () {
return factory(Project::class)->create()->id;
},
'amount' => 10000,
'in_out' => 1, 'in_out' => 1,
'customer_id' => function () {
'type_id' => rand(1, 3),
'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'),
'description' => $faker->paragraph,
'owner_id' => function () {
return factory(User::class)->create()->id;
},
'partner_id' => function () {
return factory(Customer::class)->create()->id; return factory(Customer::class)->create()->id;
}, },
]; ];
}); });
$factory->state(Payment::class, 'expanse', function (Faker $faker) {
$factory->defineAs(Payment::class, 'expanse', function (Faker $faker) {
return [ return [
'project_id' => function () {
return factory(Project::class)->create()->id;
},
'amount' => 10000,
'in_out' => 2, 'in_out' => 2,
'customer_id' => function () {
'type_id' => rand(1, 3),
'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'),
'description' => $faker->paragraph,
'owner_id' => function () {
return factory(User::class)->create()->id;
},
'partner_id' => function () {
return factory(Vendor::class)->create()->id; return factory(Vendor::class)->create()->id;
}, },
]; ];

9
database/migrations/2016_11_15_151228_create_payments_table.php

@ -3,7 +3,8 @@
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
class CreatePaymentsTable extends Migration {
class CreatePaymentsTable extends Migration
{
/** /**
* Run the migrations. * Run the migrations.
@ -12,8 +13,7 @@ class CreatePaymentsTable extends Migration {
*/ */
public function up() public function up()
{ {
Schema::create('payments', function(Blueprint $table)
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->integer('project_id')->unsigned(); $table->integer('project_id')->unsigned();
$table->integer('amount')->unsigned(); $table->integer('amount')->unsigned();
@ -21,13 +21,12 @@ class CreatePaymentsTable extends Migration {
$table->boolean('in_out')->default(1)->comment('0: out, 1: in'); $table->boolean('in_out')->default(1)->comment('0: out, 1: in');
$table->date('date'); $table->date('date');
$table->string('description'); $table->string('description');
$table->integer('customer_id')->unsigned();
$table->integer('partner_id')->unsigned();
$table->integer('owner_id')->unsigned(); $table->integer('owner_id')->unsigned();
$table->timestamps(); $table->timestamps();
}); });
} }
/** /**
* Reverse the migrations. * Reverse the migrations.
* *

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

@ -35,7 +35,7 @@
{!! FormField::select('project_id', $projects, ['label'=> trans('payment.project'),'value' => Request::get('project_id')]) !!} {!! FormField::select('project_id', $projects, ['label'=> trans('payment.project'),'value' => Request::get('project_id')]) !!}
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
{!! FormField::select('customer_id', $partners, ['label'=> trans('payment.customer'),'value' => Request::get('customer_id')]) !!}
{!! FormField::select('partner_id', $partners, ['label'=> trans('payment.customer'),'value' => Request::get('customer_id')]) !!}
</div> </div>
</div> </div>
{!! FormField::textarea('description',['label'=> trans('payment.description'),'rows' => 3]) !!} {!! FormField::textarea('description',['label'=> trans('payment.description'),'rows' => 3]) !!}

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

@ -32,7 +32,7 @@
{!! FormField::select('project_id', $projects, ['label'=> trans('payment.project')]) !!} {!! FormField::select('project_id', $projects, ['label'=> trans('payment.project')]) !!}
</div> </div>
<div class="col-sm-6"> <div class="col-sm-6">
{!! FormField::select('customer_id', $partners, ['label'=> trans('payment.customer')]) !!}
{!! FormField::select('partner_id', $partners, ['label'=> trans('payment.customer')]) !!}
</div> </div>
</div> </div>
{!! FormField::textarea('description',['label'=> trans('payment.description')]) !!} {!! FormField::textarea('description',['label'=> trans('payment.description')]) !!}

7
resources/views/payments/index.blade.php

@ -11,7 +11,7 @@
<div class="pull-left hidden-xs">{!! str_replace('/?', '?', $payments->appends(Request::except('page'))->render()) !!}</div> <div class="pull-left hidden-xs">{!! str_replace('/?', '?', $payments->appends(Request::except('page'))->render()) !!}</div>
{{ Form::open(['method'=>'get','class'=>'form-inline']) }} {{ Form::open(['method'=>'get','class'=>'form-inline']) }}
{{ Form::text('q', Request::get('q'), ['class'=>'form-control index-search-field','placeholder' => trans('payment.search')]) }} {{ Form::text('q', Request::get('q'), ['class'=>'form-control index-search-field','placeholder' => trans('payment.search')]) }}
{{ Form::select('customer_id', ['' => '-- '.trans('payment.customer').' --'] + $usersList, request('customer_id'), ['class' => 'form-control', 'id' => 'customer_id']) }}
{{ Form::select('partner_id', ['' => '-- '.trans('payment.customer').' --'] + $usersList, request('partner_id'), ['class' => 'form-control', 'id' => 'partner_id']) }}
{{ Form::submit(trans('app.search'), ['class' => 'btn btn-info btn-sm']) }} {{ Form::submit(trans('app.search'), ['class' => 'btn btn-info btn-sm']) }}
{{ link_to_route('payments.index','Reset',[],['class' => 'btn btn-default btn-sm']) }} {{ link_to_route('payments.index','Reset',[],['class' => 'btn btn-default btn-sm']) }}
{{ Form::close() }} {{ Form::close() }}
@ -34,7 +34,8 @@
<td class="text-center">{{ $payment->date }}</td> <td class="text-center">{{ $payment->date }}</td>
<td class="text-right">{{ $payment->present()->amount }}</td> <td class="text-right">{{ $payment->present()->amount }}</td>
<td>{{ $payment->description }}</td> <td>{{ $payment->description }}</td>
<td>{{ $payment->customer->name }}</td>
<td>{{ $payment->partner->name }}</td>
{{-- <td>{{ $payment->partner }}</td> --}}
<td> <td>
{!! link_to_route('payments.show', trans('app.show'), [$payment->id], ['class'=>'btn btn-info btn-xs']) !!} {!! link_to_route('payments.show', trans('app.show'), [$payment->id], ['class'=>'btn btn-info btn-xs']) !!}
{!! link_to_route('payments.edit', trans('app.edit'), [$payment->id], ['class'=>'btn btn-warning btn-xs']) !!} {!! link_to_route('payments.edit', trans('app.edit'), [$payment->id], ['class'=>'btn btn-warning btn-xs']) !!}
@ -61,7 +62,7 @@
@section('script') @section('script')
<script> <script>
(function () { (function () {
$('#customer_id').select2();
$('#partner_id').select2();
})(); })();
</script> </script>
@endsection @endsection

2
resources/views/payments/partials/payment-show.blade.php

@ -7,7 +7,7 @@
<tr><th>{{ trans('payment.type') }}</th><td>{{ $payment->present()->type_id }}</td></tr> <tr><th>{{ trans('payment.type') }}</th><td>{{ $payment->present()->type_id }}</td></tr>
<tr><th>{{ trans('payment.amount') }}</th><td class="text-right">{{ $payment->present()->amount }}</td></tr> <tr><th>{{ trans('payment.amount') }}</th><td class="text-right">{{ $payment->present()->amount }}</td></tr>
<tr><th>{{ trans('payment.description') }}</th><td>{{ $payment->description }}</td></tr> <tr><th>{{ trans('payment.description') }}</th><td>{{ $payment->description }}</td></tr>
<tr><th>{{ trans('payment.customer') }}</th><td>{{ $payment->customer->name }}</td></tr>
<tr><th>{{ trans('payment.customer') }}</th><td>{{ $payment->partner->name }}</td></tr>
</tbody> </tbody>
</table> </table>
<div class="panel-footer"> <div class="panel-footer">

2
resources/views/projects/payments.blade.php

@ -36,7 +36,7 @@
<td>{{ 1 + $key }}</td> <td>{{ 1 + $key }}</td>
<td class="text-center">{{ $payment->date }}</td> <td class="text-center">{{ $payment->date }}</td>
<td class="text-right">{{ formatRp($payment->amount) }}</td> <td class="text-right">{{ formatRp($payment->amount) }}</td>
<td>{{ $payment->customer->name }}</td>
<td>{{ $payment->partner->name }}</td>
<td>{{ $payment->description }} [{{ $payment->type() }}]</td> <td>{{ $payment->description }} [{{ $payment->type() }}]</td>
<td>{!! html_link_to_route('payments.show','',[$payment->id],['class' => 'btn btn-info btn-xs','icon' => 'search','title' => 'Lihat ' . trans('payment.show')]) !!}</td> <td>{!! html_link_to_route('payments.show','',[$payment->id],['class' => 'btn btn-info btn-xs','icon' => 'search','title' => 'Lihat ' . trans('payment.show')]) !!}</td>
</tr> </tr>

20
tests/Feature/Payments/ManagePaymentsTest.php

@ -27,7 +27,7 @@ class ManagePaymentsTest extends TestCase
$this->select(1, 'in_out'); $this->select(1, 'in_out');
$this->type(1000000, 'amount'); $this->type(1000000, 'amount');
$this->select($project->id, 'project_id'); $this->select($project->id, 'project_id');
$this->select($customer->id, 'customer_id');
$this->select($customer->id, 'partner_id');
$this->type('Pembayaran DP', 'description'); $this->type('Pembayaran DP', 'description');
$this->press(trans('payment.create')); $this->press(trans('payment.create'));
@ -37,7 +37,7 @@ class ManagePaymentsTest extends TestCase
'amount' => 1000000, 'amount' => 1000000,
'in_out' => 1, 'in_out' => 1,
'date' => '2015-05-01', 'date' => '2015-05-01',
'customer_id' => $customer->id,
'partner_id' => $customer->id,
]); ]);
} }
@ -59,7 +59,7 @@ class ManagePaymentsTest extends TestCase
$this->select(3, 'type_id'); $this->select(3, 'type_id');
$this->type(1000000, 'amount'); $this->type(1000000, 'amount');
$this->select($project->id, 'project_id'); $this->select($project->id, 'project_id');
$this->select($vendor->id, 'customer_id');
$this->select($vendor->id, 'partner_id');
$this->type('Pembayaran DP', 'description'); $this->type('Pembayaran DP', 'description');
$this->press(trans('payment.create')); $this->press(trans('payment.create'));
@ -69,7 +69,7 @@ class ManagePaymentsTest extends TestCase
'amount' => 1000000, 'amount' => 1000000,
'in_out' => 0, 'in_out' => 0,
'date' => '2015-05-01', 'date' => '2015-05-01',
'customer_id' => $vendor->id,
'partner_id' => $vendor->id,
]); ]);
} }
@ -80,8 +80,8 @@ class ManagePaymentsTest extends TestCase
$customer = factory(Customer::class)->create(); $customer = factory(Customer::class)->create();
$project = factory(Project::class)->create(); $project = factory(Project::class)->create();
$payment = factory(Payment::class)->create([
'customer_id' => $customer->id,
$payment = factory(Payment::class, 'expanse')->create([
'partner_id' => $customer->id,
'project_id' => $project->id, 'project_id' => $project->id,
'owner_id' => $user->id, 'owner_id' => $user->id,
]); ]);
@ -98,7 +98,7 @@ class ManagePaymentsTest extends TestCase
$this->see(trans('payment.updated')); $this->see(trans('payment.updated'));
$this->seeInDatabase('payments', [ $this->seeInDatabase('payments', [
'customer_id' => $customer->id,
'partner_id' => $customer->id,
'project_id' => $project->id, 'project_id' => $project->id,
'date' => '2016-05-20', 'date' => '2016-05-20',
'amount' => 1234567890, 'amount' => 1234567890,
@ -110,7 +110,7 @@ class ManagePaymentsTest extends TestCase
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$payment = factory(Payment::class)->create(['owner_id' => $user->id]);
$payment = factory(Payment::class, 'expanse')->create(['owner_id' => $user->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->click(trans('app.edit')); $this->click(trans('app.edit'));
$this->click(trans('payment.delete')); $this->click(trans('payment.delete'));
@ -125,7 +125,7 @@ class ManagePaymentsTest extends TestCase
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->id]); $project = factory(Project::class)->create(['owner_id' => $user->id]);
$payment = factory(Payment::class)->create(['project_id' => $project->id, 'owner_id' => $user->id]);
$payment = factory(Payment::class, 'expanse')->create(['project_id' => $project->id, 'owner_id' => $user->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->click(trans('app.show')); $this->click(trans('app.show'));
@ -134,6 +134,6 @@ class ManagePaymentsTest extends TestCase
$this->see($payment->date); $this->see($payment->date);
$this->see(formatRp($payment->amount)); $this->see(formatRp($payment->amount));
$this->see($payment->description); $this->see($payment->description);
$this->see($payment->customer->name);
$this->see($payment->partner->name);
} }
} }

18
tests/Feature/Payments/PaymentSearchTest.php

@ -13,33 +13,33 @@ class PaymentSearchTest extends TestCase
{ {
$admin = $this->adminUserSigningIn(); $admin = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['name' => 'Project']); $project = factory(Project::class)->create(['name' => 'Project']);
$payment = factory(Payment::class)->create(['owner_id' => $admin->id, 'project_id' => $project->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$payment = factory(Payment::class, 'expanse')->create(['owner_id' => $admin->id, 'project_id' => $project->id]);
$unShownPayment = factory(Payment::class, 'expanse')->create(['owner_id' => $admin->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->submitForm(trans('app.search'), [ $this->submitForm(trans('app.search'), [
'q' => 'Project', 'q' => 'Project',
'customer_id' => '',
'partner_id' => '',
]); ]);
$this->seePageIs(route('payments.index', ['customer_id' => '', 'q' => 'Project']));
$this->seePageIs(route('payments.index', ['partner_id' => '', 'q' => 'Project']));
$this->see($payment->project->name); $this->see($payment->project->name);
$this->dontSee($unShownPayment->project->name); $this->dontSee($unShownPayment->project->name);
} }
/** @test */ /** @test */
public function user_can_find_payment_by_customer_id()
public function partner_find_payment_by_customer_id()
{ {
$admin = $this->adminUserSigningIn(); $admin = $this->adminUserSigningIn();
$payment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$payment = factory(Payment::class, 'expanse')->create(['owner_id' => $admin->id]);
$unShownPayment = factory(Payment::class, 'expanse')->create(['owner_id' => $admin->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->submitForm(trans('app.search'), [ $this->submitForm(trans('app.search'), [
'q' => '', 'q' => '',
'customer_id' => $payment->customer_id,
'partner_id' => $payment->partner_id,
]); ]);
$this->seePageIs(route('payments.index', ['customer_id' => $payment->customer_id, 'q' => '']));
$this->seePageIs(route('payments.index', ['partner_id' => $payment->partner_id, 'q' => '']));
$this->see($payment->project->name); $this->see($payment->project->name);
$this->dontSee($unShownPayment->project->name); $this->dontSee($unShownPayment->project->name);

4
tests/Unit/Models/PaymentTest.php

@ -12,10 +12,10 @@ class PaymentTest extends TestCase
/** @test */ /** @test */
public function it_has_partner_relation() public function it_has_partner_relation()
{ {
$payment = factory(Payment::class)->states('income')->create(['in_out' => 1]);
$payment = factory(Payment::class, 'income')->create(['in_out' => 1]);
$this->assertTrue($payment->partner instanceof Customer); $this->assertTrue($payment->partner instanceof Customer);
$payment = factory(Payment::class)->states('expanse')->create(['in_out' => 0]);
$payment = factory(Payment::class, 'expanse')->create(['in_out' => 0]);
$this->assertTrue($payment->partner instanceof Vendor); $this->assertTrue($payment->partner instanceof Vendor);
} }
} }
Loading…
Cancel
Save