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-11-17 18:10:16 +00:00
|
|
|
has result_set => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Organisation');
|
|
|
|
};
|
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
sub list {
|
|
|
|
my $c = shift;
|
|
|
|
|
2017-10-24 12:31:04 +00:00
|
|
|
my $orgs_rs = $c->schema->resultset('Organisation')->search(
|
|
|
|
undef,
|
|
|
|
{
|
|
|
|
page => $c->param('page') || 1,
|
|
|
|
rows => 10,
|
|
|
|
order_by => { -asc => 'name' },
|
|
|
|
},
|
|
|
|
);
|
2017-04-24 11:49:18 +00:00
|
|
|
|
|
|
|
$c->stash(
|
2017-10-23 16:44:38 +00:00
|
|
|
orgs_rs => $orgs_rs,
|
2017-04-24 11:49:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-10-24 14:23:09 +00:00
|
|
|
$validation->optional('is_local');
|
2018-01-02 19:57:12 +00:00
|
|
|
$validation->optional('is_fair');
|
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;
|
|
|
|
|
2018-01-02 22:12:38 +00:00
|
|
|
my $location = $c->get_location_from_postcode(
|
|
|
|
$validation->param('postcode'),
|
|
|
|
'organisation',
|
|
|
|
);
|
|
|
|
|
2017-08-21 14:10:33 +00:00
|
|
|
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'),
|
2018-01-02 22:12:38 +00:00
|
|
|
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
|
2017-09-04 13:09:36 +00:00
|
|
|
submitted_by_id => $c->current_user->id,
|
|
|
|
pending => defined $validation->param('pending') ? 0 : 1,
|
2017-10-24 14:23:09 +00:00
|
|
|
is_local => $validation->param('is_local'),
|
2018-01-02 19:57:12 +00:00
|
|
|
is_fair => $validation->param('is_fair'),
|
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') );
|
2018-01-02 19:57:12 +00:00
|
|
|
my $transactions = $valid_org->entity->purchases->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
|
|
|
},
|
|
|
|
);
|
2017-11-23 14:54:59 +00:00
|
|
|
my $associations = $valid_org->entity->associations;
|
2017-11-23 16:25:14 +00:00
|
|
|
my $assoc = {
|
|
|
|
lis => defined $associations ? $associations->lis : 0,
|
2017-12-08 12:30:49 +00:00
|
|
|
esta => defined $associations ? $associations->esta : 0,
|
2017-11-23 16:25:14 +00:00
|
|
|
};
|
|
|
|
|
2017-07-25 14:44:16 +00:00
|
|
|
$c->stash(
|
|
|
|
valid_org => $valid_org,
|
|
|
|
transactions => $transactions,
|
2017-11-23 16:25:14 +00:00
|
|
|
associations => $assoc,
|
2017-07-25 14:44:16 +00:00
|
|
|
);
|
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');
|
2017-11-29 16:24:03 +00:00
|
|
|
$validation->optional('street_name');
|
2017-08-18 11:56:14 +00:00
|
|
|
$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-10-24 14:23:09 +00:00
|
|
|
$validation->optional('is_local');
|
2018-01-02 19:57:12 +00:00
|
|
|
$validation->optional('is_fair');
|
2017-11-23 16:25:14 +00:00
|
|
|
$validation->optional('is_lis');
|
2017-12-08 12:30:49 +00:00
|
|
|
$validation->optional('is_esta');
|
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') );
|
|
|
|
|
2018-01-02 22:12:38 +00:00
|
|
|
my $location = $c->get_location_from_postcode(
|
|
|
|
$validation->param('postcode'),
|
|
|
|
'organisation',
|
|
|
|
);
|
|
|
|
|
2017-08-18 11:56:14 +00:00
|
|
|
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'),
|
2018-01-02 22:12:38 +00:00
|
|
|
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
|
2017-09-04 13:03:58 +00:00
|
|
|
pending => defined $validation->param('pending') ? 0 : 1,
|
2017-10-24 14:23:09 +00:00
|
|
|
is_local => $validation->param('is_local'),
|
2018-01-02 19:57:12 +00:00
|
|
|
is_fair => $validation->param('is_fair'),
|
2017-08-18 11:56:14 +00:00
|
|
|
});
|
2017-11-23 16:25:14 +00:00
|
|
|
$valid_org->entity->update_or_create_related( 'associations', {
|
|
|
|
lis => $validation->param('is_lis'),
|
2017-12-08 12:30:49 +00:00
|
|
|
esta => $validation->param('is_esta')
|
2017-11-23 16:25:14 +00:00
|
|
|
});
|
2017-08-18 11:56:14 +00:00
|
|
|
} );
|
|
|
|
} finally {
|
2017-11-23 16:25:14 +00:00
|
|
|
if ( @_ ) {use Devel::Dwarn; Dwarn \@_;
|
2017-08-18 11:56:14 +00:00
|
|
|
$c->flash( error => 'Something went wrong Updating the Organisation' );
|
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Updated Organisation' );
|
|
|
|
}
|
|
|
|
};
|
2017-12-04 12:27:53 +00:00
|
|
|
$c->redirect_to( '/admin/organisations/' . $c->param('id') );
|
2017-04-24 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 18:10:16 +00:00
|
|
|
sub merge_list {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $org_id = $c->param('id');
|
|
|
|
my $org_result = $c->result_set->find($org_id);
|
|
|
|
|
|
|
|
if ( defined $org_result->entity->user ) {
|
|
|
|
$c->flash( error => 'Cannot merge from user-owned organisation!' );
|
|
|
|
$c->redirect_to( '/admin/organisations/' . $org_id );
|
|
|
|
return;
|
|
|
|
}
|
2017-11-23 14:54:59 +00:00
|
|
|
|
2017-11-17 18:10:16 +00:00
|
|
|
my $org_rs = $c->result_set->search(
|
|
|
|
{
|
|
|
|
id => { '!=' => $org_id },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
page => $c->param('page') || 1,
|
|
|
|
rows => 10,
|
|
|
|
order_by => { '-asc' => 'name' },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->stash(
|
|
|
|
org_result => $org_result,
|
|
|
|
org_rs => $org_rs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub merge_detail {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $org_id = $c->param('id');
|
|
|
|
my $org_result = $c->result_set->find($org_id);
|
|
|
|
|
|
|
|
if ( defined $org_result->entity->user ) {
|
|
|
|
$c->flash( error => 'Cannot merge from user-owned organisation!' );
|
|
|
|
$c->redirect_to( '/admin/organisations/' . $org_id );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $target_id = $c->param('target_id');
|
|
|
|
my $target_result = $c->result_set->find($target_id);
|
|
|
|
|
|
|
|
unless ( defined $target_result ) {
|
|
|
|
$c->flash( error => 'Unknown target organisation' );
|
|
|
|
$c->redirect_to( '/admin/organisations/' . $org_id . '/merge' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c->stash(
|
|
|
|
org_result => $org_result,
|
|
|
|
target_result => $target_result,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub merge_confirm {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $org_id = $c->param('id');
|
|
|
|
my $org_result = $c->result_set->find($org_id);
|
|
|
|
|
|
|
|
if ( defined $org_result->entity->user ) {
|
|
|
|
$c->flash( error => 'Cannot merge from user-owned organisation!' );
|
|
|
|
$c->redirect_to( '/admin/organisations/' . $org_id );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $target_id = $c->param('target_id');
|
|
|
|
my $target_result = $c->result_set->find($target_id);
|
|
|
|
my $confirm = $c->param('confirm');
|
|
|
|
|
|
|
|
if ( $confirm eq 'checked' && defined $org_result && defined $target_result ) {
|
|
|
|
try {
|
|
|
|
$c->schema->txn_do( sub {
|
|
|
|
# Done as an update, not update_all, so its damn fast - we're only
|
|
|
|
# editing an id which is guaranteed to be an integer here, and this
|
|
|
|
# makes it only one update statement.
|
|
|
|
$org_result->entity->sales->update(
|
|
|
|
{ seller_id => $target_result->entity->id }
|
|
|
|
);
|
|
|
|
my $count = $org_result->entity->sales->count;
|
|
|
|
die "Failed to migrate all sales" if $count;
|
|
|
|
$org_result->entity->delete;
|
|
|
|
$c->schema->resultset('ImportLookup')->search({ entity_id => $org_result->entity->id })->delete;
|
|
|
|
my $org_count = $c->result_set->search({id => $org_result->id })->count;
|
|
|
|
my $entity_count = $c->schema->resultset('Entity')->search({id => $org_result->entity->id })->count;
|
|
|
|
die "Failed to remove org" if $org_count;
|
|
|
|
die "Failed to remove entity" if $entity_count;
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
$c->app->log->warn($_);
|
|
|
|
};
|
|
|
|
$c->flash( error => 'Engage' );
|
|
|
|
} else {
|
|
|
|
$c->flash( error => 'You must tick the confirmation box to proceed' );
|
|
|
|
}
|
|
|
|
$c->redirect_to( '/admin/organisations/' . $org_id . '/merge/' . $target_id );
|
|
|
|
}
|
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
1;
|