From 6771a56197cf532e6356671d2d4b3f74c937d560 Mon Sep 17 00:00:00 2001 From: Jeremy Quinton Date: Mon, 9 Jul 2018 18:13:23 +0200 Subject: [PATCH] Added charging tax at the organiser level 1) Added new field to the organiser model called charge_tax. Added the migration for this. 2) Renamed tax fields columns in the database to be the same as the other organiser fields for consistency. 3) Added charge_tax option to the various organiser create and edit pages. 4) Have re-enabled some tests and used the @group passing label so we can start running tests for the various parts of the applicaiton. --- app/Http/Controllers/OrganiserController.php | 15 +++-- .../OrganiserCustomizeController.php | 9 ++- app/Models/Organiser.php | 27 +++++--- ...al_tax_field_rename_current_tax_fields.php | 37 +++++++++++ phpunit.xml | 4 +- .../ManageOrganiser/CreateOrganiser.blade.php | 25 ++++---- .../views/ManageOrganiser/Customize.blade.php | 56 +++++++++++++---- tests/OrganiserCustomizeTest.php | 8 ++- tests/OrganiserTest.php | 61 +++++++++++++++++-- 9 files changed, 192 insertions(+), 50 deletions(-) create mode 100644 database/migrations/2018_07_09_133243_additional_tax_field_rename_current_tax_fields.php diff --git a/app/Http/Controllers/OrganiserController.php b/app/Http/Controllers/OrganiserController.php index a9261025..fa64af95 100644 --- a/app/Http/Controllers/OrganiserController.php +++ b/app/Http/Controllers/OrganiserController.php @@ -40,6 +40,11 @@ class OrganiserController extends MyBaseController { $organiser = Organiser::createNew(false, false, true); + $chargeTax = $request->get('charge_tax'); + + if ($chargeTax == 'Yes') { + $organiser->addExtraValidationRules(); + } if (!$organiser->validate($request->all())) { return response()->json([ 'status' => 'error', @@ -53,13 +58,11 @@ class OrganiserController extends MyBaseController $organiser->facebook = $request->get('facebook'); $organiser->twitter = $request->get('twitter'); $organiser->confirmation_key = str_random(15); - $organiser->taxname = $request->get('taxname'); - $organiser->taxvalue = $request->get('taxvalue'); - $organiser->taxid = $request->get('taxid'); - $organiser->taxname = $request->get('taxname'); - $organiser->taxvalue = round($request->get('taxvalue'),2); - $organiser->taxid = $request->get('taxid'); + $organiser->tax_name = $request->get('tax_name'); + $organiser->tax_value = round($request->get('tax_value'),2); + $organiser->tax_id = $request->get('tax_id'); + $organiser->charge_tax = ($chargeTax == 'Yes') ? 1 : 0; if ($request->hasFile('organiser_logo')) { $organiser->setLogo($request->file('organiser_logo')); diff --git a/app/Http/Controllers/OrganiserCustomizeController.php b/app/Http/Controllers/OrganiserCustomizeController.php index a50b8c37..92ed3b4d 100644 --- a/app/Http/Controllers/OrganiserCustomizeController.php +++ b/app/Http/Controllers/OrganiserCustomizeController.php @@ -50,9 +50,12 @@ class OrganiserCustomizeController extends MyBaseController $organiser->enable_organiser_page = $request->get('enable_organiser_page'); $organiser->facebook = $request->get('facebook'); $organiser->twitter = $request->get('twitter'); - $organiser->taxname = $request->get('taxname'); - $organiser->taxvalue = $request->get('taxvalue'); - $organiser->taxid = $request->get('taxid'); + $organiser->tax_name = $request->get('tax_name'); + $organiser->tax_value = $request->get('tax_value'); + $organiser->tax_id = $request->get('tax_id'); + $organiser->charge_tax = ($request->get('charge_tax') == 1) ? 1 : 0; + + //var_dump($organiser->charge_tax); if ($request->get('remove_current_image') == '1') { $organiser->logo_path = ''; diff --git a/app/Models/Organiser.php b/app/Models/Organiser.php index 900d5ba1..89aa5cac 100644 --- a/app/Models/Organiser.php +++ b/app/Models/Organiser.php @@ -2,12 +2,15 @@ namespace App\Models; +use Illuminate\Auth\Authenticatable; use Illuminate\Http\UploadedFile; +use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Str; use Image; -class Organiser extends MyBaseModel +class Organiser extends MyBaseModel implements AuthenticatableContract { + use Authenticatable; /** * The validation rules for the model. * @@ -16,21 +19,24 @@ class Organiser extends MyBaseModel protected $rules = [ 'name' => ['required'], 'email' => ['required', 'email'], - 'taxname' => ['required','max:15'], - 'taxvalue' => ['required','numeric'], - 'taxid' => ['required','max:100'], 'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'], ]; + protected $extra_rules = [ + 'tax_name' => ['required','max:15'], + 'tax_value' => ['required','numeric'], + 'tax_id' => ['required','max:100'], + ]; + /** * The validation rules for the model. * * @var array $attributes */ protected $attributes = [ - 'taxname' => 'Tax Name', - 'taxvalue' => 'Tax Rate', - 'taxid' => 'Tax ID', + 'tax_name' => 'Tax Name', + 'tax_value' => 'Tax Rate', + 'tax_id' => 'Tax ID', ]; /** @@ -161,5 +167,12 @@ class Organiser extends MyBaseModel $this->logo_path = $relativePath; } } + + /** + * Adds extra validator rules to the organiser object depending on whether tax is required or not + */ + public function addExtraValidationRules() { + $this->rules = $this->rules + $this->extra_rules; + } } diff --git a/database/migrations/2018_07_09_133243_additional_tax_field_rename_current_tax_fields.php b/database/migrations/2018_07_09_133243_additional_tax_field_rename_current_tax_fields.php new file mode 100644 index 00000000..f4544bd0 --- /dev/null +++ b/database/migrations/2018_07_09_133243_additional_tax_field_rename_current_tax_fields.php @@ -0,0 +1,37 @@ +boolean('charge_tax')->default(0); + $table->renameColumn('taxname', 'tax_name'); + $table->renameColumn('taxvalue', 'tax_value'); + $table->renameColumn('taxid', 'tax_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('organisers', function (Blueprint $table) { + $table->dropColumn('charge_tax'); + $table->renameColumn('tax_name', 'taxname'); + $table->renameColumn('tax_value', 'taxvalue'); + $table->renameColumn('tax_id', 'taxid'); + }); + } +} diff --git a/phpunit.xml b/phpunit.xml index f1bd8182..5a2c22ea 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,7 +19,7 @@ - - + + diff --git a/resources/views/ManageOrganiser/CreateOrganiser.blade.php b/resources/views/ManageOrganiser/CreateOrganiser.blade.php index fe6d7f7c..a3d1e0c1 100644 --- a/resources/views/ManageOrganiser/CreateOrganiser.blade.php +++ b/resources/views/ManageOrganiser/CreateOrganiser.blade.php @@ -52,10 +52,6 @@ - - - -
{!! Form::label('about', trans("Organiser.organiser_description"), array('class'=>'control-label ')) !!} {!! Form::textarea('about', Input::old('about'), @@ -65,26 +61,33 @@ 'rows' => 4 )) !!}
+
+

Do you want to Charge Tax at your Events?

+ {!! Form::label('Yes', 'Yes', array('class'=>'control-label', 'id' => 'charge_yes')) !!} + {{ Form::radio('charge_tax', 'Yes' , false) }} + {!! Form::label('No', 'No', array('class'=>'control-label','id' => 'charge_no')) !!} + {{ Form::radio('charge_tax', 'No' , true) }} +
-
+
- {!! Form::label('taxid', 'Tax ID', array('class'=>'control-label required')) !!} - {!! Form::text('taxid', Input::old('taxid'), array('class'=>'form-control', 'placeholder'=>'Tax ID')) !!} + {!! Form::label('tax_id', 'Tax ID', array('class'=>'control-label required')) !!} + {!! Form::text('tax_id', Input::old('tax_id'), array('class'=>'form-control', 'placeholder'=>'Tax ID')) !!}
- {!! Form::label('taxname', 'Tax name', array('class'=>'control-label required')) !!} - {!! Form::text('taxname', Input::old('taxname'), array('class'=>'form-control', 'placeholder'=>'Tax name')) !!} + {!! Form::label('tax_name', 'Tax name', array('class'=>'control-label required')) !!} + {!! Form::text('tax_name', Input::old('tax_name'), array('class'=>'form-control', 'placeholder'=>'Tax name')) !!}
- {!! Form::label('taxvalue', 'Tax value', array('class'=>'control-label required')) !!} - {!! Form::text('taxvalue', Input::old('taxvalue'), array('class'=>'form-control', 'placeholder'=>'Tax Value')) !!} + {!! Form::label('tax_value', 'Tax value', array('class'=>'control-label required')) !!} + {!! Form::text('tax_value', Input::old('tax_value'), array('class'=>'form-control', 'placeholder'=>'Tax Value')) !!}
diff --git a/resources/views/ManageOrganiser/Customize.blade.php b/resources/views/ManageOrganiser/Customize.blade.php index 926e31e4..90c52f94 100644 --- a/resources/views/ManageOrganiser/Customize.blade.php +++ b/resources/views/ManageOrganiser/Customize.blade.php @@ -30,6 +30,25 @@ }); }); + + $(document).ready(function(){ + var charge_tax = $("input[type=radio][name='charge_tax']:checked").val(); + if (charge_tax == 1) { + $('#tax_fields').show(); + } else { + $('#tax_fields').hide(); + } + + $('input[type=radio][name=charge_tax]').change(function() { + if (this.value == 1) { + $('#tax_fields').show(); + } + else { + $('#tax_fields').hide(); + } + }); + }); + @stop @@ -94,24 +113,35 @@ )) !!}
-
+
- {!! Form::label('taxid', 'Tax ID', array('class'=>'control-label required')) !!} - {!! Form::text('taxid', Input::old('taxid'), array('class'=>'form-control', 'placeholder'=>'Tax ID')) !!} +

Do you want to Charge Tax at your Events?

+ + charge_tax == 1 ? 'checked' : '' }}> + + charge_tax == 0 ? 'checked' : '' }}>
- -
-
- {!! Form::label('taxname', 'Tax name', array('class'=>'control-label required')) !!} - {!! Form::text('taxname', Input::old('taxname'), array('class'=>'form-control', 'placeholder'=>'Tax name')) !!} +
+
+
+ {!! Form::label('tax_id', 'Tax ID', array('class'=>'control-label required')) !!} + {!! Form::text('tax_id', Input::old('tax_id'), array('class'=>'form-control', 'placeholder'=>'Tax ID')) !!} +
-
-
-
- {!! Form::label('taxvalue', 'Tax value', array('class'=>'control-label required')) !!} - {!! Form::text('taxvalue', Input::old('taxvalue'), array('class'=>'form-control', 'placeholder'=>'Tax Value')) !!} +
+
+ {!! Form::label('tax_name', 'Tax name', array('class'=>'control-label required')) !!} + {!! Form::text('tax_name', Input::old('tax_name'), array('class'=>'form-control', 'placeholder'=>'Tax name')) !!} +
+
+ +
+
+ {!! Form::label('tax_value', 'Tax value', array('class'=>'control-label required')) !!} + {!! Form::text('tax_value', Input::old('tax_value'), array('class'=>'form-control', 'placeholder'=>'Tax Value')) !!} +
diff --git a/tests/OrganiserCustomizeTest.php b/tests/OrganiserCustomizeTest.php index c21345c1..cfd1941f 100644 --- a/tests/OrganiserCustomizeTest.php +++ b/tests/OrganiserCustomizeTest.php @@ -7,18 +7,20 @@ use App\Models\Organiser; class OrganiserCustomizeTest extends TestCase { + /** + * @group passing + */ public function test_customize_organiser_is_successful() { $organiser = factory(App\Models\Organiser::class)->create(); - $this->actingAs($this->test_user) + $this->actingAs($organiser) ->visit(route('showOrganiserCustomize', ['organiser_id' => $organiser->id])) ->type($this->faker->name, 'name') ->type($this->faker->email, 'email') - ->type($this->faker->email, 'about') ->type($this->faker->word, 'facebook') ->type($this->faker->word, 'twitter') - ->press('Create Organiser') + ->press('Save Organiser') ->seeJson([ 'status' => 'success' ]); diff --git a/tests/OrganiserTest.php b/tests/OrganiserTest.php index c9be7f8e..390d59de 100644 --- a/tests/OrganiserTest.php +++ b/tests/OrganiserTest.php @@ -7,18 +7,69 @@ use App\Models\Organiser; class OrganiserTest extends TestCase { - public function test_create_organiser_is_successful() + /** + * @group passing + */ + public function test_create_organiser_is_successful_when_charge_tax_is_no() { + + $email = $this->faker->email; + $this->actingAs($this->test_user) ->visit(route('showCreateOrganiser')) ->type($this->faker->name, 'name') - ->type($this->faker->email, 'email') - ->type($this->faker->email, 'about') - ->type($this->faker->word, 'facebook') - ->type($this->faker->word, 'twitter') + ->type($email, 'email') + ->type('No', 'charge_tax') ->press('Create Organiser') ->seeJson([ 'status' => 'success' ]); + + //get the most recently created organiser from database + $this->organiser = Organiser::where('email','=', $email)->orderBy('created_at', 'desc')->first(); + //check the charge tax flag is 0 + $this->assertEquals($this->organiser->charge_tax, 0); + } + + /** + * @group passing + */ + public function test_create_organiser_is_successful_when_charge_tax_is_yes() + { + $email = $this->faker->email; + + $this->actingAs($this->test_user) + ->visit(route('showCreateOrganiser')) + ->type($this->faker->name, 'name') + ->type($email, 'email') + ->type('organisers', 'tax_name') + ->type(12323, 'tax_id') + ->type(15, 'tax_value') + ->type('Yes', 'charge_tax') + ->press('Create Organiser') + ->seeJson([ + 'status' => 'success' + ]); + + //get the most recently created organiser from database + $this->organiser = Organiser::where('email','=', $email)->orderBy('created_at', 'desc')->first(); + //check the charge tax flag is 1 + $this->assertEquals($this->organiser->charge_tax, 1); + } + + /** + * @group passing + */ + public function test_create_organiser_fails_when_organiser_details_missing() + { + $this->actingAs($this->test_user) + ->visit(route('showCreateOrganiser')) + ->type('', 'name') + ->type('', 'email') + ->type('No', 'charge_tax') + ->press('Create Organiser') + ->seeJson([ + 'status' => 'error' + ]); } }