From dc24e0facb619db199c57ab7d66b24c91cd82d05 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Fri, 27 Oct 2017 20:47:38 +0800 Subject: [PATCH] Change customer_id to partner_id in payments table, pass all tests --- app/Entities/Payments/Payment.php | 4 +- app/Entities/Payments/PaymentsRepository.php | 27 +++++----- app/Http/Controllers/PaymentsController.php | 2 +- app/Http/Requests/Payments/CreateRequest.php | 55 +++++++++---------- app/Http/Requests/Payments/UpdateRequest.php | 55 +++++++++---------- database/factories/PaymentFactory.php | 30 +++++++++-- .../2016_11_15_151228_create_payments_table.php | 63 +++++++++++----------- resources/views/payments/create.blade.php | 2 +- resources/views/payments/edit.blade.php | 2 +- resources/views/payments/index.blade.php | 9 ++-- .../views/payments/partials/payment-show.blade.php | 2 +- resources/views/projects/payments.blade.php | 4 +- tests/Feature/Payments/ManagePaymentsTest.php | 46 ++++++++-------- tests/Feature/Payments/PaymentSearchTest.php | 28 +++++----- tests/Unit/Models/PaymentTest.php | 4 +- 15 files changed, 179 insertions(+), 154 deletions(-) diff --git a/app/Entities/Payments/Payment.php b/app/Entities/Payments/Payment.php index 205e154..68443ea 100755 --- a/app/Entities/Payments/Payment.php +++ b/app/Entities/Payments/Payment.php @@ -31,10 +31,10 @@ class Payment extends Model public function partner() { 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() diff --git a/app/Entities/Payments/PaymentsRepository.php b/app/Entities/Payments/PaymentsRepository.php index 3ec3682..2057250 100755 --- a/app/Entities/Payments/PaymentsRepository.php +++ b/app/Entities/Payments/PaymentsRepository.php @@ -5,8 +5,8 @@ namespace App\Entities\Payments; use App\Entities\BaseRepository; /** -* Payments Repository Class -*/ + * Payments Repository Class + */ class PaymentsRepository extends BaseRepository { protected $model; @@ -18,18 +18,18 @@ class PaymentsRepository extends BaseRepository public function getPayments($queryStrings) { - return $this->model->orderBy('date','desc') - ->whereHas('project', function($query) use ($queryStrings) { + return $this->model->orderBy('date', 'desc') + ->whereHas('project', function ($query) use ($queryStrings) { if (isset($queryStrings['q'])) { - $query->where('name', 'like', '%' . $queryStrings['q'] . '%'); + $query->where('name', 'like', '%'.$queryStrings['q'].'%'); } }) ->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()) ->paginate($this->_paginate); } @@ -37,19 +37,22 @@ class PaymentsRepository extends BaseRepository public function create($paymentData) { $paymentData['owner_id'] = auth()->id(); - $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); + $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); return $this->storeArray($paymentData); } public function update($paymentData = [], $paymentId) { foreach ($paymentData as $key => $value) { - if (!$paymentData[$key]) $paymentData[$key] = null; + if ( ! $paymentData[$key]) { + $paymentData[$key] = null; + } + } $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); - $payment = $this->requireById($paymentId); + $payment = $this->requireById($paymentId); $payment->update($paymentData); return $payment; } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/PaymentsController.php b/app/Http/Controllers/PaymentsController.php index 97d9519..d2050f2 100755 --- a/app/Http/Controllers/PaymentsController.php +++ b/app/Http/Controllers/PaymentsController.php @@ -21,7 +21,7 @@ class PaymentsController extends Controller 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(); return view('payments.index', compact('payments', 'usersList')); } diff --git a/app/Http/Requests/Payments/CreateRequest.php b/app/Http/Requests/Payments/CreateRequest.php index e34a776..9f4f3df 100644 --- a/app/Http/Requests/Payments/CreateRequest.php +++ b/app/Http/Requests/Payments/CreateRequest.php @@ -4,34 +4,35 @@ namespace App\Http\Requests\Payments; use App\Http\Requests\Request; -class CreateRequest extends Request { +class CreateRequest extends Request +{ - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return auth()->user()->can('manage_payments'); - } + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return auth()->user()->can('manage_payments'); + } - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - 'date' => 'required|date|date_format:Y-m-d', - 'in_out' => 'required|numeric', - 'amount' => 'required', - 'project_id' => 'required|numeric', - 'type_id' => 'required|numeric', - 'customer_id' => 'required|numeric', - 'description' => 'required|max:255', - ]; - } + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'date' => 'required|date|date_format:Y-m-d', + 'in_out' => 'required|numeric', + 'amount' => 'required', + 'project_id' => 'required|numeric', + 'type_id' => 'required|numeric', + 'partner_id' => 'required|numeric', + 'description' => 'required|max:255', + ]; + } } diff --git a/app/Http/Requests/Payments/UpdateRequest.php b/app/Http/Requests/Payments/UpdateRequest.php index 106a2b2..12a2b11 100644 --- a/app/Http/Requests/Payments/UpdateRequest.php +++ b/app/Http/Requests/Payments/UpdateRequest.php @@ -4,34 +4,35 @@ namespace App\Http\Requests\Payments; use App\Http\Requests\Request; -class UpdateRequest extends Request { +class UpdateRequest extends Request +{ - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return auth()->user()->can('manage_payments'); - } + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return auth()->user()->can('manage_payments'); + } - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - 'date' => 'required|date|date_format:Y-m-d', - 'in_out' => 'required|numeric', - 'amount' => 'required', - 'project_id' => 'required|numeric', - 'type_id' => 'required|numeric', - 'customer_id' => 'required|numeric', - 'description' => 'required|max:255', - ]; - } + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'date' => 'required|date|date_format:Y-m-d', + 'in_out' => 'required|numeric', + 'amount' => 'required', + 'project_id' => 'required|numeric', + 'type_id' => 'required|numeric', + 'partner_id' => 'required|numeric', + 'description' => 'required|max:255', + ]; + } } diff --git a/database/factories/PaymentFactory.php b/database/factories/PaymentFactory.php index 420d545..4e9ff11 100644 --- a/database/factories/PaymentFactory.php +++ b/database/factories/PaymentFactory.php @@ -21,25 +21,45 @@ $factory->define(Payment::class, function (Faker $faker) { 'owner_id' => function () { return factory(User::class)->create()->id; }, - 'customer_id' => function () { + 'partner_id' => function () { return factory(Customer::class)->create()->id; }, ]; }); -$factory->state(Payment::class, 'income', function (Faker $faker) { +$factory->defineAs(Payment::class, 'income', function (Faker $faker) { return [ + 'project_id' => function () { + return factory(Project::class)->create()->id; + }, + 'amount' => 10000, '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; }, ]; }); -$factory->state(Payment::class, 'expanse', function (Faker $faker) { +$factory->defineAs(Payment::class, 'expanse', function (Faker $faker) { return [ + 'project_id' => function () { + return factory(Project::class)->create()->id; + }, + 'amount' => 10000, '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; }, ]; 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 7baf30f..7404f68 100644 --- a/database/migrations/2016_11_15_151228_create_payments_table.php +++ b/database/migrations/2016_11_15_151228_create_payments_table.php @@ -3,39 +3,38 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreatePaymentsTable extends Migration { +class CreatePaymentsTable extends Migration +{ - /** - * Run the migrations. - * - * @return void - */ - public function up() - { - Schema::create('payments', function(Blueprint $table) - { - $table->increments('id'); - $table->integer('project_id')->unsigned(); - $table->integer('amount')->unsigned(); - $table->boolean('type_id')->default(1)->comment('1:project, 2: add_feature, 3:maintenance'); - $table->boolean('in_out')->default(1)->comment('0: out, 1: in'); - $table->date('date'); - $table->string('description'); - $table->integer('customer_id')->unsigned(); - $table->integer('owner_id')->unsigned(); - $table->timestamps(); - }); - } + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('payments', function (Blueprint $table) { + $table->increments('id'); + $table->integer('project_id')->unsigned(); + $table->integer('amount')->unsigned(); + $table->boolean('type_id')->default(1)->comment('1:project, 2: add_feature, 3:maintenance'); + $table->boolean('in_out')->default(1)->comment('0: out, 1: in'); + $table->date('date'); + $table->string('description'); + $table->integer('partner_id')->unsigned(); + $table->integer('owner_id')->unsigned(); + $table->timestamps(); + }); + } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::drop('payments'); - } + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('payments'); + } } diff --git a/resources/views/payments/create.blade.php b/resources/views/payments/create.blade.php index 562ad30..305341a 100755 --- a/resources/views/payments/create.blade.php +++ b/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('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')]) !!}
{!! FormField::textarea('description',['label'=> trans('payment.description'),'rows' => 3]) !!} diff --git a/resources/views/payments/edit.blade.php b/resources/views/payments/edit.blade.php index 887ead9..ddb853b 100755 --- a/resources/views/payments/edit.blade.php +++ b/resources/views/payments/edit.blade.php @@ -32,7 +32,7 @@ {!! FormField::select('project_id', $projects, ['label'=> trans('payment.project')]) !!}
- {!! FormField::select('customer_id', $partners, ['label'=> trans('payment.customer')]) !!} + {!! FormField::select('partner_id', $partners, ['label'=> trans('payment.customer')]) !!}
{!! FormField::textarea('description',['label'=> trans('payment.description')]) !!} diff --git a/resources/views/payments/index.blade.php b/resources/views/payments/index.blade.php index 54c0db9..6e76b38 100755 --- a/resources/views/payments/index.blade.php +++ b/resources/views/payments/index.blade.php @@ -11,7 +11,7 @@ {{ Form::open(['method'=>'get','class'=>'form-inline']) }} {{ 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']) }} {{ link_to_route('payments.index','Reset',[],['class' => 'btn btn-default btn-sm']) }} {{ Form::close() }} @@ -34,7 +34,8 @@ {{ $payment->date }} {{ $payment->present()->amount }} {{ $payment->description }} - {{ $payment->customer->name }} + {{ $payment->partner->name }} + {{-- {{ $payment->partner }} --}} {!! 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']) !!} @@ -61,7 +62,7 @@ @section('script') -@endsection \ No newline at end of file +@endsection diff --git a/resources/views/payments/partials/payment-show.blade.php b/resources/views/payments/partials/payment-show.blade.php index bed4026..6342efb 100644 --- a/resources/views/payments/partials/payment-show.blade.php +++ b/resources/views/payments/partials/payment-show.blade.php @@ -7,7 +7,7 @@ {{ trans('payment.type') }}{{ $payment->present()->type_id }} {{ trans('payment.amount') }}{{ $payment->present()->amount }} {{ trans('payment.description') }}{{ $payment->description }} - {{ trans('payment.customer') }}{{ $payment->customer->name }} + {{ trans('payment.customer') }}{{ $payment->partner->name }}