2017-04-24 11:49:18 +00:00
|
|
|
package Pear::LocalLoop::Controller::Admin::Organisations;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2017-07-25 13:33:11 +00:00
|
|
|
use Try::Tiny;
|
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
sub list {
|
|
|
|
my $c = shift;
|
|
|
|
|
2017-09-04 13:03:58 +00:00
|
|
|
my $valid_orgs_rs = $c->schema->resultset('Organisation')->search({ pending => 0 });
|
|
|
|
my $pending_orgs_rs = $c->schema->resultset('Organisation')->search({ pending => 1 });
|
2017-04-24 11:49:18 +00:00
|
|
|
|
|
|
|
$c->stash(
|
|
|
|
valid_orgs_rs => $valid_orgs_rs,
|
|
|
|
pending_orgs_rs => $pending_orgs_rs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-21 14:10:33 +00:00
|
|
|
sub add_org {
|
|
|
|
my $c = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_org_submit {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
|
|
|
|
$validation->required('name');
|
|
|
|
$validation->optional('street_name');
|
|
|
|
$validation->required('town');
|
2017-08-25 14:25:52 +00:00
|
|
|
$validation->optional('sector');
|
2017-08-21 14:10:33 +00:00
|
|
|
$validation->optional('postcode')->postcode;
|
2017-09-04 13:09:36 +00:00
|
|
|
$validation->optional('pending');
|
2017-08-21 14:10:33 +00:00
|
|
|
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
$c->flash( error => 'The validation has failed' );
|
2017-08-31 13:52:45 +00:00
|
|
|
return $c->redirect_to( '/admin/organisations/add' );
|
2017-08-21 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $organisation;
|
|
|
|
|
|
|
|
try {
|
2017-08-31 13:52:45 +00:00
|
|
|
my $entity = $c->schema->resultset('Entity')->create({
|
|
|
|
organisation => {
|
|
|
|
name => $validation->param('name'),
|
|
|
|
street_name => $validation->param('street_name'),
|
|
|
|
town => $validation->param('town'),
|
|
|
|
sector => $validation->param('sector'),
|
|
|
|
postcode => $validation->param('postcode'),
|
2017-09-04 13:09:36 +00:00
|
|
|
submitted_by_id => $c->current_user->id,
|
|
|
|
pending => defined $validation->param('pending') ? 0 : 1,
|
2017-08-31 13:52:45 +00:00
|
|
|
},
|
|
|
|
type => 'organisation',
|
2017-08-21 14:10:33 +00:00
|
|
|
});
|
2017-08-31 13:52:45 +00:00
|
|
|
$organisation = $entity->organisation;
|
2017-08-21 14:10:33 +00:00
|
|
|
} finally {
|
|
|
|
if ( @_ ) {
|
|
|
|
$c->flash( error => 'Something went wrong Adding the Organisation' );
|
2017-08-31 13:52:45 +00:00
|
|
|
$c->redirect_to( '/admin/organisations/add' );
|
2017-08-21 14:10:33 +00:00
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Added Organisation' );
|
2017-08-31 13:52:45 +00:00
|
|
|
$c->redirect_to( '/admin/organisations/' . $organisation->id);
|
2017-08-21 14:10:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
sub valid_read {
|
|
|
|
my $c = shift;
|
|
|
|
my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') );
|
2017-08-31 13:52:45 +00:00
|
|
|
my $transactions = $valid_org->entity->sales->search(
|
2017-07-25 14:44:16 +00:00
|
|
|
undef, {
|
|
|
|
page => $c->param('page') || 1,
|
|
|
|
rows => 10,
|
2017-08-31 10:51:22 +00:00
|
|
|
order_by => { -desc => 'submitted_at' },
|
2017-07-25 14:44:16 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
$c->stash(
|
|
|
|
valid_org => $valid_org,
|
|
|
|
transactions => $transactions,
|
|
|
|
);
|
2017-04-24 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 11:56:14 +00:00
|
|
|
sub valid_edit {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
$validation->required('name');
|
|
|
|
$validation->required('street_name');
|
|
|
|
$validation->required('town');
|
2017-08-25 14:25:52 +00:00
|
|
|
$validation->optional('sector');
|
2017-08-18 11:56:14 +00:00
|
|
|
$validation->required('postcode')->postcode;
|
2017-09-04 13:03:58 +00:00
|
|
|
$validation->optional('pending');
|
2017-08-18 11:56:14 +00:00
|
|
|
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
$c->flash( error => 'The validation has failed' );
|
2017-08-31 13:52:45 +00:00
|
|
|
return $c->redirect_to( '/admin/organisations/' . $c->param('id') );
|
2017-08-18 11:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') );
|
|
|
|
|
|
|
|
try {
|
|
|
|
$c->schema->storage->txn_do( sub {
|
|
|
|
$valid_org->update({
|
|
|
|
name => $validation->param('name'),
|
|
|
|
street_name => $validation->param('street_name'),
|
|
|
|
town => $validation->param('town'),
|
2017-08-25 14:25:52 +00:00
|
|
|
sector => $validation->param('sector'),
|
2017-08-18 11:56:14 +00:00
|
|
|
postcode => $validation->param('postcode'),
|
2017-09-04 13:03:58 +00:00
|
|
|
pending => defined $validation->param('pending') ? 0 : 1,
|
2017-08-18 11:56:14 +00:00
|
|
|
});
|
|
|
|
} );
|
|
|
|
} finally {
|
|
|
|
if ( @_ ) {
|
|
|
|
$c->flash( error => 'Something went wrong Updating the Organisation' );
|
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Updated Organisation' );
|
|
|
|
}
|
|
|
|
};
|
2017-09-15 10:18:27 +00:00
|
|
|
$c->redirect_to( '/admin/organisations/');
|
2017-04-24 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|