Payroll sumission API improved and test added

This commit is contained in:
Finn 2017-09-18 17:13:18 +01:00
parent b224182e61
commit 616181def3
5 changed files with 342 additions and 16 deletions

View file

@ -17,7 +17,7 @@
* email: test4@example.com
* password: abc123
* Test Org
* email: test5@example.com
* email: org@example.com
* password: abc123
* Test Admin
* email: admin@example.com

View file

@ -17,47 +17,47 @@ has error_messages => sub {
required => { message => 'No gross payroll sent.', status => 400 },
},
payroll_income_tax => {
required => { message => 'no payroll income tax sent.', status => 400 },
required => { message => 'No total income tax sent.', status => 400 },
},
payroll_employee_ni => {
required => { message => 'no payroll employee ni sent.', status => 400 },
required => { message => 'No total employee NI sent.', status => 400 },
},
payroll_employer_ni => {
required => { message => 'no payroll employer ni sent.', status => 400 },
required => { message => 'No total employer NI sent.', status => 400 },
},
payroll_total_pension => {
required => { message => 'no payroll total pension sent.', status => 400 },
required => { message => 'No total total pension sent.', status => 400 },
},
payroll_other_benefit => {
required => { message => 'no payroll other benefit sent.', status => 400 },
required => { message => 'No total other benefits total sent.', status => 400 },
},
supplier_business_name => {
required => { message => 'no supplier business name sent.', status => 400 },
required => { message => 'No supplier business name sent.', status => 400 },
},
postcode => {
required => { message => 'no postcode sent.', status => 400 },
required => { message => 'No postcode sent.', status => 400 },
postcode => { message => 'postcode must be valid', status => 400 },
},
monthly_spend => {
required => { message => 'no monthly spend sent.', status => 400 },
required => { message => 'No monthly spend sent.', status => 400 },
},
employee_no => {
required => { message => 'no employee no sent.', status => 400 },
required => { message => 'No employee no sent.', status => 400 },
},
employee_income_tax => {
required => { message => 'no employee income tax sent.', status => 400 },
required => { message => 'No employee income tax sent.', status => 400 },
},
employee_gross_wage => {
required => { message => 'no employee gross wage sent.', status => 400 },
required => { message => 'No employee gross wage sent.', status => 400 },
},
employee_ni => {
required => { message => 'no employee ni sent.', status => 400 },
required => { message => 'No employee ni sent.', status => 400 },
},
employee_pension => {
required => { message => 'no employee pension sent.', status => 400 },
required => { message => 'No employee pension sent.', status => 400 },
},
employee_other_benefit => {
required => { message => 'no employee other benefits sent.', status => 400 },
required => { message => 'No employee other benefits sent.', status => 400 },
},
};
};
@ -86,11 +86,26 @@ sub post_payroll {
$validation->required('payroll_total_pension');
$validation->required('payroll_other_benefit');
my $gross_payroll = $validation->param('gross_payroll');
my $payroll_income_tax = $validation->param('payroll_income_tax');
my $payroll_employee_ni = $validation->param('payroll_employee_ni');
my $payroll_employer_ni = $validation->param('payroll_employer_ni');
my $payroll_total_pension = $validation->param('payroll_total_pension');
my $payroll_other_benefit = $validation->param('payroll_other_benefit');
return $c->api_validation_error if $validation->has_error;
$c->schema->txn_do( sub {
$user->entity->organisation->update({
$user->entity->organisation->payroll->create({
entry_period => $validation->param('entry_period'),
employee_amount => $validation->param('employee_amount'),
local_employee_amount => $validation->param('local_employee_amount'),
gross_payroll => $gross_payroll * 100000,
payroll_income_tax => $payroll_income_tax * 100000,
payroll_employee_ni => $payroll_employee_ni * 100000,
payroll_employer_ni => $payroll_employer_ni * 100000,
payroll_total_pension => $payroll_total_pension * 100000,
payroll_other_benefit => $payroll_other_benefit * 100000,
});
});

View file

@ -68,6 +68,13 @@ __PACKAGE__->belongs_to(
"entity_id",
);
__PACKAGE__->has_many(
"payroll",
"Pear::LocalLoop::Schema::Result::OrganisationPayroll",
{ "foreign.org_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->filter_column( pending => {
filter_to_storage => 'to_bool',
});

View file

@ -0,0 +1,83 @@
package Pear::LocalLoop::Schema::Result::OrganisationPayroll;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->load_components(qw/
InflateColumn::DateTime
TimeStamp
/);
__PACKAGE__->table("organisation_payroll");
__PACKAGE__->add_columns(
"id" => {
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
},
"org_id" => {
data_type => 'integer',
is_nullable => 0,
is_foreign_key => 1,
},
"submitted_at" => {
data_type => "datetime",
is_nullable => 0,
set_on_create => 1,
},
"entry_period" => {
data_type => "datetime",
is_nullable => 0,
},
"employee_amount" => {
data_type => "integer",
is_nullable => 0,
},
"local_employee_amount" => {
data_type => "integer",
is_nullable => 0,
},
"gross_payroll" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"payroll_income_tax" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"payroll_employee_ni" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"payroll_employer_ni" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"payroll_total_pension" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
"payroll_other_benefit" => {
data_type => "numeric",
size => [ 100, 0 ],
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to(
"organisation",
"Pear::LocalLoop::Schema::Result::Organisation",
"org_id",
);
1;

221
t/api/organisation.t Normal file
View file

@ -0,0 +1,221 @@
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('users');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $session_key = $framework->login({
email => 'org@example.com',
password => 'abc123',
});
## Payroll Data Submission
#No JSON sent
$t->post_ok('/api/org/payroll')
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/JSON is missing/i);
#Empty JSON
$t->post_ok('/api/org/payroll' => json => {})
->json_is('/success', Mojo::JSON->false);
# no session key
$t->post_ok('/api/org/payroll' => json => {
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(401)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/Invalid Session/);
# No entry_period
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No entry period/);
# No employee_amount
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No employee amount/);
# No local_employee_amount
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/local employee amount/);
# No gross_payroll
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No gross payroll/);
# No payroll_income_tax
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No total income tax/);
# No payroll_employee_ni
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No total employee NI/);
# No payroll_employer_ni
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/total employer NI/);
# No payroll_total_pension
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_other_benefit => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No total total pension/);
# No payroll_other_benefit
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
})
->status_is(400)
->json_is('/success', Mojo::JSON->false)
->json_like('/message', qr/No total other benefits total/);
# Valid payroll submission
$t->post_ok('/api/org/payroll' => json => {
session_key => $session_key,
entry_period => '2017-12',
employee_amount => '10',
local_employee_amount => '10',
gross_payroll => '10',
payroll_income_tax => '10',
payroll_employee_ni => '10',
payroll_employer_ni => '10',
payroll_total_pension => '10',
payroll_other_benefit => '10',
})
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true);
## Supplier Form submission
#TODO make the test!
## Employee Form submission
#TODO make the test!
$framework->logout( $session_key );
done_testing;