From be3c52d6498d73bf685d84a242a0820ca9364088 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 1 Nov 2017 21:45:55 +0800 Subject: [PATCH] Add morph relation of partner to Payment model Add partner_type on payments table Set grouped partner list on payment entry form Payment can have User model as partner Payment can have Vendor model as partner Payment can have Customer model as partner --- app/Entities/BaseRepository.php | 7 +++- app/Entities/Payments/Payment.php | 2 +- app/Entities/Payments/PaymentsRepository.php | 7 ++++ database/factories/PaymentFactory.php | 41 ++++++++++++++++++---- .../2016_11_15_151228_create_payments_table.php | 1 + tests/Feature/Payments/ManagePaymentsTest.php | 25 +++++++------ tests/Unit/Models/PaymentTest.php | 29 +++++++++++++-- 7 files changed, 90 insertions(+), 22 deletions(-) diff --git a/app/Entities/BaseRepository.php b/app/Entities/BaseRepository.php index c4cdaca..0b4e126 100755 --- a/app/Entities/BaseRepository.php +++ b/app/Entities/BaseRepository.php @@ -3,6 +3,7 @@ namespace App\Entities; use App\Entities\Partners\Customer; +use App\Entities\Partners\Vendor; use App\Entities\Projects\Feature; use App\Entities\Projects\Project; use App\Entities\Users\User; @@ -20,7 +21,11 @@ abstract class BaseRepository extends EloquentRepository public function getCustomersAndVendorsList() { - return Customer::orderBy('name')->pluck('name', 'id'); + $partners = [ + 'Customer' => Customer::orderBy('name')->pluck('name', 'id')->all(), + 'Vendor' => Vendor::orderBy('name')->pluck('name', 'id')->all(), + ]; + return $partners; } public function getWorkersList() diff --git a/app/Entities/Payments/Payment.php b/app/Entities/Payments/Payment.php index bcf44aa..831a29f 100755 --- a/app/Entities/Payments/Payment.php +++ b/app/Entities/Payments/Payment.php @@ -34,7 +34,7 @@ class Payment extends Model public function partner() { - return $this->belongsTo('App\Entities\Partners\Customer', 'partner_id'); + return $this->morphTo(); } public function type() diff --git a/app/Entities/Payments/PaymentsRepository.php b/app/Entities/Payments/PaymentsRepository.php index a5b9f13..8391379 100755 --- a/app/Entities/Payments/PaymentsRepository.php +++ b/app/Entities/Payments/PaymentsRepository.php @@ -36,6 +36,13 @@ class PaymentsRepository extends BaseRepository public function create($paymentData) { $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); + + if ($paymentData['in_out'] == 0) { + $paymentData['partner_type'] = 'App\Entities\Partners\Vendor'; + } else { + $paymentData['partner_type'] = 'App\Entities\Partners\Customer'; + } + return $this->storeArray($paymentData); } diff --git a/database/factories/PaymentFactory.php b/database/factories/PaymentFactory.php index fdf57a4..a1cb02d 100644 --- a/database/factories/PaymentFactory.php +++ b/database/factories/PaymentFactory.php @@ -1,23 +1,50 @@ define(Payment::class, function (Faker $faker) { return [ - 'project_id' => function () { + 'project_id' => function () { return factory(Project::class)->create()->id; }, - 'amount' => 10000, - 'in_out' => 1, - 'type_id' => rand(1, 3), - 'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'), - 'description' => $faker->paragraph, - 'partner_id' => function () { + 'amount' => 10000, + 'in_out' => 1, + 'type_id' => rand(1, 3), + 'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'), + 'description' => $faker->paragraph, + 'partner_type' => Customer::class, + 'partner_id' => function () { return factory(Customer::class)->create()->id; }, ]; }); + +$factory->state(Payment::class, 'vendor', function (Faker $faker) { + + return [ + 'in_out' => 1, + 'type_id' => 1, + 'partner_type' => Vendor::class, + 'partner_id' => function () { + return factory(Vendor::class)->create()->id; + }, + ]; +}); + +$factory->state(Payment::class, 'fee', function (Faker $faker) { + + return [ + 'in_out' => 1, + 'type_id' => 1, + 'partner_type' => User::class, + 'partner_id' => function () { + return factory(User::class)->create()->id; + }, + ]; +}); diff --git a/database/migrations/2016_11_15_151228_create_payments_table.php b/database/migrations/2016_11_15_151228_create_payments_table.php index 3c468b7..ecf698a 100644 --- a/database/migrations/2016_11_15_151228_create_payments_table.php +++ b/database/migrations/2016_11_15_151228_create_payments_table.php @@ -21,6 +21,7 @@ class CreatePaymentsTable extends Migration $table->boolean('in_out')->default(1)->comment('0: out, 1: in'); $table->date('date'); $table->string('description'); + $table->string('partner_type'); $table->integer('partner_id')->unsigned(); $table->timestamps(); }); diff --git a/tests/Feature/Payments/ManagePaymentsTest.php b/tests/Feature/Payments/ManagePaymentsTest.php index 6b81d24..72a19d7 100644 --- a/tests/Feature/Payments/ManagePaymentsTest.php +++ b/tests/Feature/Payments/ManagePaymentsTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Payments; use App\Entities\Partners\Customer; +use App\Entities\Partners\Vendor; use App\Entities\Payments\Payment; use App\Entities\Projects\Project; use Tests\TestCase; @@ -32,11 +33,12 @@ class ManagePaymentsTest extends TestCase $this->see(trans('payment.created')); $this->seeInDatabase('payments', [ - 'project_id' => $project->id, - 'amount' => 1000000, - 'in_out' => 1, - 'date' => '2015-05-01', - 'partner_id' => $customer->id, + 'project_id' => $project->id, + 'amount' => 1000000, + 'in_out' => 1, + 'date' => '2015-05-01', + 'partner_type' => Customer::class, + 'partner_id' => $customer->id, ]); } @@ -44,7 +46,7 @@ class ManagePaymentsTest extends TestCase public function admin_can_entry_project_an_expanse_payment() { $user = $this->adminUserSigningIn(); - $vendor = factory(Customer::class)->create(['owner_id' => $user->agency->id]); + $vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]); $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $this->visit(route('payments.index')); @@ -64,11 +66,12 @@ class ManagePaymentsTest extends TestCase $this->see(trans('payment.created')); $this->seeInDatabase('payments', [ - 'project_id' => $project->id, - 'amount' => 1000000, - 'in_out' => 0, - 'date' => '2015-05-01', - 'partner_id' => $vendor->id, + 'project_id' => $project->id, + 'amount' => 1000000, + 'in_out' => 0, + 'date' => '2015-05-01', + 'partner_type' => Vendor::class, + 'partner_id' => $vendor->id, ]); } diff --git a/tests/Unit/Models/PaymentTest.php b/tests/Unit/Models/PaymentTest.php index 64f4647..0f9be49 100644 --- a/tests/Unit/Models/PaymentTest.php +++ b/tests/Unit/Models/PaymentTest.php @@ -3,15 +3,40 @@ namespace Tests\Unit\Models; use App\Entities\Partners\Customer; +use App\Entities\Partners\Vendor; use App\Entities\Payments\Payment; +use App\Entities\Users\User; use Tests\TestCase; class PaymentTest extends TestCase { /** @test */ - public function it_has_partner_relation() + public function it_can_have_partner_relation_on_customer_model_for_income_payment() { $payment = factory(Payment::class)->create(); - $this->assertTrue($payment->partner instanceof Customer); + $this->assertTrue( + $payment->partner instanceof Customer, + 'An income payment should have a App\Entities\Partners\Customer model as partner relation' + ); + } + + /** @test */ + public function it_can_have_partner_relation_on_vendor_model_for_expanse_payment() + { + $payment = factory(Payment::class)->states('vendor')->create(); + $this->assertTrue( + $payment->partner instanceof Vendor, + 'An expanse payment can have a App\Entities\Partners\Vendor model as partner relation' + ); + } + + /** @test */ + public function it_can_have_partner_relation_on_user_model_for_fee_payment() + { + $payment = factory(Payment::class)->states('fee')->create(); + $this->assertTrue( + $payment->partner instanceof User, + 'An expanse payment can have a App\Entities\Users\User model as partner relation' + ); } }