Added major merge code for merging organisations

This commit is contained in:
Tom Bloor 2017-11-17 18:10:16 +00:00
parent a11727ba1c
commit baffe144f3
5 changed files with 219 additions and 0 deletions

View file

@ -190,6 +190,9 @@ sub startup {
$admin_routes->post('/organisations/add')->to('admin-organisations#add_org_submit');
$admin_routes->get('/organisations/:id')->to('admin-organisations#valid_read');
$admin_routes->post('/organisations/:id')->to('admin-organisations#valid_edit');
$admin_routes->get('/organisations/:id/merge')->to('admin-organisations#merge_list');
$admin_routes->get('/organisations/:id/merge/:target_id')->to('admin-organisations#merge_detail');
$admin_routes->post('/organisations/:id/merge/:target_id')->to('admin-organisations#merge_confirm');
$admin_routes->get('/feedback')->to('admin-feedback#index');
$admin_routes->get('/feedback/:id')->to('admin-feedback#read');

View file

@ -3,6 +3,11 @@ use Mojo::Base 'Mojolicious::Controller';
use Try::Tiny;
has result_set => sub {
my $c = shift;
return $c->schema->resultset('Organisation');
};
sub list {
my $c = shift;
@ -127,4 +132,104 @@ sub valid_edit {
$c->redirect_to( '/admin/organisations/');
}
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;
}
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 );
}
1;

View file

@ -12,6 +12,11 @@ sub register {
}
return $value;
});
$app->helper( format_currency_from_db => sub {
my ( $c, $value ) = @_;
return sprintf( '£%.2f', $value / 100000 );
});
}
1;

View file

@ -0,0 +1,81 @@
% layout 'admin_errors';
% title 'Organisations';
<div class="row">
<div class="col-12">
<h3 class="float-left">Merging <%= $org_result->name %> into <%= $target_result->name %></h3>
<a href="<%= url_for '/admin/organisations/' . $org_result->id . '/merge' %>" class="btn btn-success float-right">Back</a>
</div>
% for my $org ( $org_result, $target_result ) {
<div class="col-6">
<div class="card">
<h3 class="card-header">
<%= $org->name %>
</h3>
<div class="card-body">
<div class="row">
<div class="col-6">
Street Name
</div>
<div class="col-6">
%= $org->street_name
</div>
<div class="col-6">
Town/City
</div>
<div class="col-6">
%= $org->town
</div>
<div class="col-6">
Sector
</div>
<div class="col-6">
%= $org->sector
</div>
<div class="col-6">
Postcode
</div>
<div class="col-6">
%= $org->postcode
</div>
<div class="col-6">
Validated
</div>
<div class="col-6">
%= $org->pending ? 'no' : 'yes'
</div>
<div class="col-6">
Is Local
</div>
<div class="col-6">
%= $org->is_local ? 'yes' : 'no'
</div>
</div>
</div>
<div class="list-group list-group-flush">
<div class="list-group-item">
Transaction Count: <%= $org->entity->sales->count %>
</div>
</div>
</div>
</div>
% }
<div class="col-12">
<div class="card">
<div class="card-body">
<h1 class="card-title">
Warning: Cannot be undone!
</h1>
<p>
This will discard all basic information about this organisation, and
merge all transactions into the target organisation. This process has
no way of being undone.
</p>
<form action="<%= url_for %>" method="POST">
<input type="checkbox" name="confirm" value="checked">
<label>I confirm that I want this to happen</label>
<button type="submit" class="btn btn-danger">Confirm Merge</button>
</form>
</div>
</div>
</div>
</div>

View file

@ -0,0 +1,25 @@
% layout 'admin_errors';
% title 'Organisations';
<div class="row">
<div class="col-12">
<h3>Target to merge into for <%= $org_result->name %></h3>
</div>
<div class="col-12">
<div class="card">
<h3 class="card-header">
Organisations
<a href="<%= url_for '/admin/organisations/' . $org_result->id %>" class="btn btn-success float-right">Back</a>
</h3>
<div class="list-group list-group-flush">
% for my $org ( $org_rs->all ) {
<a href="<%= url_for . '/' . $org->id %>" class="list-group-item list-group-item-action">
%= $org->name
</a>
% }
</div>
</div>
</div>
<div class="col-12">
%= bootstrap_pagination( $c->param('page') || 1, $org_rs->pager->last_page, { class => 'justify-content-center' } );
</div>
</div>