This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm

317 lines
9.3 KiB
Perl
Raw Permalink Normal View History

package Pear::LocalLoop::Controller::Admin::Organisations;
use Mojo::Base 'Mojolicious::Controller';
use Try::Tiny;
has result_set => sub {
2021-03-20 12:09:50 +00:00
my $c = shift;
return $c->schema->resultset('Organisation');
};
sub list {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $orgs_rs = $c->schema->resultset('Organisation')->search(
undef,
{
page => $c->param('page') || 1,
rows => 10,
order_by => { -asc => 'name' },
},
);
$c->stash( orgs_rs => $orgs_rs, );
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
2017-08-21 14:10:33 +00:00
sub add_org {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-08-21 14:10:33 +00:00
}
sub add_org_submit {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $validation = $c->validation;
$validation->required('name');
$validation->optional('street_name');
$validation->required('town');
$validation->optional('sector');
$validation->optional('postcode')->postcode;
$validation->optional('pending');
$validation->optional('is_local');
$validation->optional('is_fair');
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
return $c->redirect_to('/admin/organisations/add');
2017-08-21 14:10:33 +00:00
}
2021-03-20 12:09:50 +00:00
my $organisation;
my $location =
$c->get_location_from_postcode( $validation->param('postcode'),
'organisation', );
try {
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'),
(
defined $location
? (%$location)
: ( latitude => undef, longitude => undef )
),
submitted_by_id => $c->current_user->id,
pending => defined $validation->param('pending') ? 0 : 1,
is_local => $validation->param('is_local'),
is_fair => $validation->param('is_fair'),
},
type => 'organisation',
}
);
$organisation = $entity->organisation;
}
finally {
if (@_) {
$c->flash(
error => 'Something went wrong Adding the Organisation' );
$c->redirect_to('/admin/organisations/add');
}
else {
$c->flash( success => 'Added Organisation' );
$c->redirect_to( '/admin/organisations/' . $organisation->id );
}
};
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-08-21 14:10:33 +00:00
}
sub valid_read {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $valid_org =
$c->schema->resultset('Organisation')->find( $c->param('id') );
my $transactions = $valid_org->entity->purchases->search(
undef,
{
page => $c->param('page') || 1,
rows => 10,
order_by => { -desc => 'submitted_at' },
},
);
my $associations = $valid_org->entity->associations;
my $assoc = {
lis => defined $associations ? $associations->lis : 0,
esta => defined $associations ? $associations->esta : 0,
};
$c->stash(
valid_org => $valid_org,
transactions => $transactions,
associations => $assoc,
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub valid_edit {
2021-03-20 23:26:52 +00:00
my ( $c, $warning ) = @_;
2021-03-20 12:09:50 +00:00
my $validation = $c->validation;
$validation->required('name');
$validation->optional('street_name');
$validation->required('town');
$validation->optional('sector');
$validation->required('postcode')->postcode;
$validation->optional('pending');
$validation->optional('is_local');
$validation->optional('is_fair');
$validation->optional('is_lis');
$validation->optional('is_esta');
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
return $c->redirect_to( '/admin/organisations/' . $c->param('id') );
}
2021-03-20 12:09:50 +00:00
my $valid_org =
$c->schema->resultset('Organisation')->find( $c->param('id') );
my $location =
$c->get_location_from_postcode( $validation->param('postcode'),
'organisation', );
try {
$c->schema->storage->txn_do(
sub {
$valid_org->update(
{
name => $validation->param('name'),
street_name => $validation->param('street_name'),
town => $validation->param('town'),
sector => $validation->param('sector'),
postcode => $validation->param('postcode'),
(
defined $location ? (%$location)
: ( latitude => undef, longitude => undef )
),
pending => defined $validation->param('pending') ? 0
: 1,
is_local => $validation->param('is_local'),
is_fair => $validation->param('is_fair'),
}
);
$valid_org->entity->update_or_create_related(
'associations',
{
lis => $validation->param('is_lis'),
esta => $validation->param('is_esta')
}
);
}
);
}
finally {
2021-03-20 19:53:38 +00:00
if ($warning) {
2021-03-20 12:09:50 +00:00
use Devel::Dwarn;
2021-03-20 19:53:38 +00:00
Dwarn \$warning;
2021-03-20 12:09:50 +00:00
$c->flash(
error => 'Something went wrong Updating the Organisation' );
}
else {
$c->flash( success => 'Updated Organisation' );
}
};
$c->redirect_to( '/admin/organisations/' . $c->param('id') );
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub merge_list {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
my $org_id = $c->param('id');
my $org_result = $c->result_set->find($org_id);
2021-03-20 12:09:50 +00:00
if ( defined $org_result->entity->user ) {
$c->flash( error => 'Cannot merge from user-owned organisation!' );
$c->redirect_to( '/admin/organisations/' . $org_id );
return;
}
2021-03-20 12:09:50 +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,
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub merge_detail {
2021-03-20 12:09:50 +00:00
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,
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub merge_confirm {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
my $org_id = $c->param('id');
my $org_result = $c->result_set->find($org_id);
2021-03-20 12:09:50 +00:00
if ( defined $org_result->entity->user ) {
$c->flash( error => 'Cannot merge from user-owned organisation!' );
$c->redirect_to( '/admin/organisations/' . $org_id );
return;
}
2021-03-20 12:09:50 +00:00
my $target_id = $c->param('target_id');
my $target_result = $c->result_set->find($target_id);
my $confirm = $c->param('confirm');
2021-03-20 12:09:50 +00:00
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 );
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
1;