Add code formatter, format all code
This commit is contained in:
parent
602a59f1c3
commit
47a55f6322
120 changed files with 8061 additions and 6967 deletions
|
@ -2,99 +2,116 @@ package Pear::LocalLoop::Controller::Admin::Categories;
|
|||
use Mojo::Base 'Mojolicious::Controller';
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Category');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Category');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $category_rs = $c->result_set;
|
||||
$category_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||
$c->stash( categories => [ $category_rs->all ] );
|
||||
my $category_rs = $c->result_set;
|
||||
$category_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||
$c->stash( categories => [ $category_rs->all ] );
|
||||
}
|
||||
|
||||
# POST
|
||||
sub create {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
$validation->required('category', 'trim')->not_in_resultset('name', $c->result_set);
|
||||
my $validation = $c->validation;
|
||||
$validation->required( 'category', 'trim' )
|
||||
->not_in_resultset( 'name', $c->result_set );
|
||||
|
||||
my $category_name = $validation->param('category');
|
||||
my $category_name = $validation->param('category');
|
||||
|
||||
if ( $validation->has_error ) {
|
||||
my $check = shift @{ $c->validation->error('category') };
|
||||
if ( $check eq 'required' ) {
|
||||
$c->flash( error => 'Category name is required' );
|
||||
} elsif ( $check eq 'like' ) {
|
||||
$c->flash( error => 'Category name not valid - Alphanumeric characters and Underscore only' );
|
||||
} elsif ( $check eq 'not_in_resultset' ) {
|
||||
$c->flash( error => 'Category Already Exists' );
|
||||
if ( $validation->has_error ) {
|
||||
my $check = shift @{ $c->validation->error('category') };
|
||||
if ( $check eq 'required' ) {
|
||||
$c->flash( error => 'Category name is required' );
|
||||
}
|
||||
elsif ( $check eq 'like' ) {
|
||||
$c->flash( error =>
|
||||
'Category name not valid - Alphanumeric characters and Underscore only'
|
||||
);
|
||||
}
|
||||
elsif ( $check eq 'not_in_resultset' ) {
|
||||
$c->flash( error => 'Category Already Exists' );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$c->flash( success => 'Category Created' );
|
||||
$c->result_set->create({ name => $category_name });
|
||||
}
|
||||
$c->redirect_to( '/admin/categories' );
|
||||
else {
|
||||
$c->flash( success => 'Category Created' );
|
||||
$c->result_set->create( { name => $category_name } );
|
||||
}
|
||||
$c->redirect_to('/admin/categories');
|
||||
}
|
||||
|
||||
# GET
|
||||
sub read {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $category = $c->result_set->find($id) ) {
|
||||
$c->stash( category => $category );
|
||||
} else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
$c->redirect_to( '/admin/categories' );
|
||||
}
|
||||
if ( my $category = $c->result_set->find($id) ) {
|
||||
$c->stash( category => $category );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
$c->redirect_to('/admin/categories');
|
||||
}
|
||||
}
|
||||
|
||||
# POST
|
||||
sub update {
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required('id');
|
||||
$validation->required('category', 'trim')->like(qr/^[\w]*$/);
|
||||
$validation->optional('line_icon');
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required('id');
|
||||
$validation->required( 'category', 'trim' )->like(qr/^[\w]*$/);
|
||||
$validation->optional('line_icon');
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( $validation->has_error ) {
|
||||
my $names = $validation->failed;
|
||||
$c->flash( error => 'Error in submitted data: ' . join(', ', @$names) );
|
||||
$c->redirect_to( '/admin/categories/' . $id );
|
||||
} elsif ( my $category = $c->result_set->find($id) ) {
|
||||
$category->update({
|
||||
id => $validation->param('id'),
|
||||
name => $validation->param('category'),
|
||||
line_icon => (defined $validation->param('line_icon') ? $validation->param('line_icon') : undef ),
|
||||
});
|
||||
$c->flash( success => 'Category Updated' );
|
||||
$c->redirect_to( '/admin/categories/' . $validation->param('id') );
|
||||
} else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
$c->redirect_to( '/admin/categories' );
|
||||
}
|
||||
if ( $validation->has_error ) {
|
||||
my $names = $validation->failed;
|
||||
$c->flash(
|
||||
error => 'Error in submitted data: ' . join( ', ', @$names ) );
|
||||
$c->redirect_to( '/admin/categories/' . $id );
|
||||
}
|
||||
elsif ( my $category = $c->result_set->find($id) ) {
|
||||
$category->update(
|
||||
{
|
||||
id => $validation->param('id'),
|
||||
name => $validation->param('category'),
|
||||
line_icon => (
|
||||
defined $validation->param('line_icon')
|
||||
? $validation->param('line_icon')
|
||||
: undef
|
||||
),
|
||||
}
|
||||
);
|
||||
$c->flash( success => 'Category Updated' );
|
||||
$c->redirect_to( '/admin/categories/' . $validation->param('id') );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
$c->redirect_to('/admin/categories');
|
||||
}
|
||||
}
|
||||
|
||||
# DELETE
|
||||
sub delete {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $category = $c->result_set->find($id) ) {
|
||||
$category->transaction_category->delete;
|
||||
$category->delete;
|
||||
$c->flash( success => 'Category Deleted' );
|
||||
} else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
}
|
||||
$c->redirect_to( '/admin/categories' );
|
||||
if ( my $category = $c->result_set->find($id) ) {
|
||||
$category->transaction_category->delete;
|
||||
$category->delete;
|
||||
$c->flash( success => 'Category Deleted' );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Category found' );
|
||||
}
|
||||
$c->redirect_to('/admin/categories');
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -2,50 +2,52 @@ package Pear::LocalLoop::Controller::Admin::Feedback;
|
|||
use Mojo::Base 'Mojolicious::Controller';
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Feedback');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Feedback');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $feedback_rs = $c->result_set->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 12,
|
||||
order_by => { -desc => 'submitted_at' },
|
||||
},
|
||||
);
|
||||
$c->stash( feedback_rs => $feedback_rs );
|
||||
my $feedback_rs = $c->result_set->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 12,
|
||||
order_by => { -desc => 'submitted_at' },
|
||||
},
|
||||
);
|
||||
$c->stash( feedback_rs => $feedback_rs );
|
||||
}
|
||||
|
||||
sub read {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $feedback = $c->result_set->find($id) ) {
|
||||
$c->stash( feedback => $feedback );
|
||||
} else {
|
||||
$c->flash( error => 'No Feedback found' );
|
||||
$c->redirect_to( '/admin/feedback' );
|
||||
}
|
||||
if ( my $feedback = $c->result_set->find($id) ) {
|
||||
$c->stash( feedback => $feedback );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Feedback found' );
|
||||
$c->redirect_to('/admin/feedback');
|
||||
}
|
||||
}
|
||||
|
||||
sub actioned {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $feedback = $c->result_set->find($id) ) {
|
||||
$feedback->actioned( ! $feedback->actioned );
|
||||
$feedback->update;
|
||||
$c->redirect_to( '/admin/feedback/' . $id );
|
||||
} else {
|
||||
$c->flash( error => 'No Feedback found' );
|
||||
$c->redirect_to( '/admin/feedback' );
|
||||
}
|
||||
if ( my $feedback = $c->result_set->find($id) ) {
|
||||
$feedback->actioned( !$feedback->actioned );
|
||||
$feedback->update;
|
||||
$c->redirect_to( '/admin/feedback/' . $id );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Feedback found' );
|
||||
$c->redirect_to('/admin/feedback');
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -5,322 +5,352 @@ use Text::CSV;
|
|||
use Try::Tiny;
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('ImportSet');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('ImportSet');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $import_rs = $c->result_set->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -desc => 'date' },
|
||||
},
|
||||
);
|
||||
$c->stash( import_rs => $import_rs );
|
||||
my $import_rs = $c->result_set->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -desc => 'date' },
|
||||
},
|
||||
);
|
||||
$c->stash( import_rs => $import_rs );
|
||||
}
|
||||
|
||||
sub list {
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
|
||||
my $include_ignored = $c->param('ignored');
|
||||
my $include_imported = $c->param('imported');
|
||||
my $include_ignored = $c->param('ignored');
|
||||
my $include_imported = $c->param('imported');
|
||||
|
||||
my $import_set = $c->result_set->find($set_id);
|
||||
my $import_value_rs = $c->result_set->get_values($set_id, $include_ignored, $include_imported);
|
||||
my $import_users_rs = $c->result_set->get_users($set_id, $include_ignored, $include_imported);
|
||||
my $import_org_rs = $c->result_set->get_orgs($set_id, $include_ignored, $include_imported);
|
||||
my $import_lookup_rs = $c->result_set->get_lookups($set_id);
|
||||
my $import_set = $c->result_set->find($set_id);
|
||||
my $import_value_rs = $c->result_set->get_values( $set_id, $include_ignored,
|
||||
$include_imported );
|
||||
my $import_users_rs =
|
||||
$c->result_set->get_users( $set_id, $include_ignored, $include_imported );
|
||||
my $import_org_rs =
|
||||
$c->result_set->get_orgs( $set_id, $include_ignored, $include_imported );
|
||||
my $import_lookup_rs = $c->result_set->get_lookups($set_id);
|
||||
|
||||
$c->stash(
|
||||
import_set => $import_set,
|
||||
import_value_rs => $import_value_rs,
|
||||
import_users_rs => $import_users_rs,
|
||||
import_org_rs => $import_org_rs,
|
||||
import_lookup_rs => $import_lookup_rs,
|
||||
);
|
||||
$c->stash(
|
||||
import_set => $import_set,
|
||||
import_value_rs => $import_value_rs,
|
||||
import_users_rs => $import_users_rs,
|
||||
import_org_rs => $import_org_rs,
|
||||
import_lookup_rs => $import_lookup_rs,
|
||||
);
|
||||
}
|
||||
|
||||
sub get_add {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
}
|
||||
|
||||
sub post_add {
|
||||
my $c = shift;
|
||||
|
||||
my $csv_data = $c->param('csv');
|
||||
my $date_format = $c->param('date_format');
|
||||
my $c = shift;
|
||||
|
||||
my $csv = Text::CSV->new({
|
||||
binary => 1,
|
||||
allow_whitespace => 1,
|
||||
});
|
||||
my $csv_data = $c->param('csv');
|
||||
my $date_format = $c->param('date_format');
|
||||
|
||||
open my $fh, '<', \$csv_data;
|
||||
my $csv = Text::CSV->new(
|
||||
{
|
||||
binary => 1,
|
||||
allow_whitespace => 1,
|
||||
}
|
||||
);
|
||||
|
||||
# List context returns the actual headers
|
||||
my @csv_headers;
|
||||
my $error;
|
||||
try {
|
||||
@csv_headers = $csv->header( $fh );
|
||||
} catch {
|
||||
$error = $_;
|
||||
};
|
||||
open my $fh, '<', \$csv_data;
|
||||
|
||||
if ( defined $error ) {
|
||||
$c->_csv_flash_error( $error );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
return;
|
||||
}
|
||||
# List context returns the actual headers
|
||||
my @csv_headers;
|
||||
my $error;
|
||||
try {
|
||||
@csv_headers = $csv->header($fh);
|
||||
}
|
||||
catch {
|
||||
$error = $_;
|
||||
};
|
||||
|
||||
# Text::CSV Already errors on duplicate columns, so this is fine
|
||||
my @required = grep {/^user$|^value$|^date$|^organisation$/} @csv_headers;
|
||||
|
||||
unless ( scalar( @required ) == 4 ) {
|
||||
$c->_csv_flash_error( 'Required columns not available' );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
return;
|
||||
}
|
||||
|
||||
my $csv_output = $csv->getline_hr_all( $fh );
|
||||
|
||||
unless ( scalar( @$csv_output ) ) {
|
||||
$c->_csv_flash_error( "No data found" );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
return;
|
||||
}
|
||||
|
||||
for my $data ( @$csv_output ) {
|
||||
for my $key ( qw/ user value organisation / ) {
|
||||
unless ( defined $data->{$key} ) {
|
||||
$c->_csv_flash_error( "Undefined [$key] data found" );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
if ( defined $error ) {
|
||||
$c->_csv_flash_error($error);
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ( defined $data->{date} ) {
|
||||
my $dtp = DateTime::Format::Strptime->new( pattern => $date_format );
|
||||
my $dt_obj = $dtp->parse_datetime($data->{date});
|
||||
unless ( defined $dt_obj ) {
|
||||
$c->_csv_flash_error( "Undefined or incorrect format for [date] data found" );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
|
||||
# Text::CSV Already errors on duplicate columns, so this is fine
|
||||
my @required = grep { /^user$|^value$|^date$|^organisation$/ } @csv_headers;
|
||||
|
||||
unless ( scalar(@required) == 4 ) {
|
||||
$c->_csv_flash_error('Required columns not available');
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
$data->{date} = $dt_obj;
|
||||
}
|
||||
}
|
||||
|
||||
my $value_set;
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
$value_set = $c->result_set->create({});
|
||||
my $csv_output = $csv->getline_hr_all($fh);
|
||||
|
||||
$value_set->values->populate(
|
||||
[
|
||||
[ qw/ user_name purchase_value purchase_date org_name / ],
|
||||
( map { [ @{$_}{qw/ user value date organisation /} ] } @$csv_output ),
|
||||
]
|
||||
);
|
||||
unless ( scalar(@$csv_output) ) {
|
||||
$c->_csv_flash_error("No data found");
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
unless ( defined $value_set ) {
|
||||
$c->_csv_flash_error( 'Error creating new Value Set' );
|
||||
$c->redirect_to( '/admin/import/add' );
|
||||
return;
|
||||
}
|
||||
for my $data (@$csv_output) {
|
||||
for my $key (qw/ user value organisation /) {
|
||||
unless ( defined $data->{$key} ) {
|
||||
$c->_csv_flash_error("Undefined [$key] data found");
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ( defined $data->{date} ) {
|
||||
my $dtp =
|
||||
DateTime::Format::Strptime->new( pattern => $date_format );
|
||||
my $dt_obj = $dtp->parse_datetime( $data->{date} );
|
||||
unless ( defined $dt_obj ) {
|
||||
$c->_csv_flash_error(
|
||||
"Undefined or incorrect format for [date] data found");
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
$data->{date} = $dt_obj;
|
||||
}
|
||||
}
|
||||
|
||||
$c->flash( success => 'Created Value Set' );
|
||||
$c->redirect_to( '/admin/import/' . $value_set->id );
|
||||
my $value_set;
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
$value_set = $c->result_set->create( {} );
|
||||
|
||||
$value_set->values->populate(
|
||||
[
|
||||
[qw/ user_name purchase_value purchase_date org_name /],
|
||||
(
|
||||
map { [ @{$_}{qw/ user value date organisation /} ] }
|
||||
@$csv_output
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
unless ( defined $value_set ) {
|
||||
$c->_csv_flash_error('Error creating new Value Set');
|
||||
$c->redirect_to('/admin/import/add');
|
||||
return;
|
||||
}
|
||||
|
||||
$c->flash( success => 'Created Value Set' );
|
||||
$c->redirect_to( '/admin/import/' . $value_set->id );
|
||||
}
|
||||
|
||||
sub _csv_flash_error {
|
||||
my ( $c, $error ) = @_;
|
||||
$error //= "An error occurred";
|
||||
my ( $c, $error ) = @_;
|
||||
$error //= "An error occurred";
|
||||
|
||||
$c->flash(
|
||||
error => $error,
|
||||
# If csv info is huge, this fails epically
|
||||
#csv_data => $c->param('csv'),
|
||||
date_format => $c->param('date_format'),
|
||||
);
|
||||
$c->flash(
|
||||
error => $error,
|
||||
|
||||
# If csv info is huge, this fails epically
|
||||
#csv_data => $c->param('csv'),
|
||||
date_format => $c->param('date_format'),
|
||||
);
|
||||
}
|
||||
|
||||
sub get_user {
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $user_name = $c->param('user');
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $user_name = $c->param('user');
|
||||
|
||||
my $values_rs = $c->result_set->find($set_id)->values->search(
|
||||
{
|
||||
user_name => $user_name,
|
||||
ignore_value => 0,
|
||||
}
|
||||
);
|
||||
|
||||
unless ( $values_rs->count > 0 ) {
|
||||
$c->flash( error => 'User not found or all values are ignored' );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
|
||||
my $lookup_result = $c->result_set->find($set_id)->lookups->find(
|
||||
{ name => $user_name },
|
||||
);
|
||||
|
||||
my $entity_id = $c->param('entity');
|
||||
|
||||
my $users_rs = $c->schema->resultset('User');
|
||||
|
||||
if ( defined $entity_id && $users_rs->find({ entity_id => $entity_id }) ) {
|
||||
if ( defined $lookup_result ) {
|
||||
$lookup_result->update({ entity_id => $entity_id });
|
||||
} else {
|
||||
$lookup_result = $c->result_set->find($set_id)->lookups->create(
|
||||
my $values_rs = $c->result_set->find($set_id)->values->search(
|
||||
{
|
||||
name => $user_name,
|
||||
entity_id => $entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
} elsif ( defined $entity_id ) {
|
||||
$c->stash( error => "User does not exist" );
|
||||
}
|
||||
user_name => $user_name,
|
||||
ignore_value => 0,
|
||||
}
|
||||
);
|
||||
|
||||
$c->stash(
|
||||
users_rs => $users_rs,
|
||||
lookup => $lookup_result,
|
||||
user_name => $user_name,
|
||||
);
|
||||
unless ( $values_rs->count > 0 ) {
|
||||
$c->flash( error => 'User not found or all values are ignored' );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
|
||||
my $lookup_result =
|
||||
$c->result_set->find($set_id)->lookups->find( { name => $user_name }, );
|
||||
|
||||
my $entity_id = $c->param('entity');
|
||||
|
||||
my $users_rs = $c->schema->resultset('User');
|
||||
|
||||
if ( defined $entity_id && $users_rs->find( { entity_id => $entity_id } ) )
|
||||
{
|
||||
if ( defined $lookup_result ) {
|
||||
$lookup_result->update( { entity_id => $entity_id } );
|
||||
}
|
||||
else {
|
||||
$lookup_result = $c->result_set->find($set_id)->lookups->create(
|
||||
{
|
||||
name => $user_name,
|
||||
entity_id => $entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
elsif ( defined $entity_id ) {
|
||||
$c->stash( error => "User does not exist" );
|
||||
}
|
||||
|
||||
$c->stash(
|
||||
users_rs => $users_rs,
|
||||
lookup => $lookup_result,
|
||||
user_name => $user_name,
|
||||
);
|
||||
}
|
||||
|
||||
sub get_org {
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $org_name = $c->param('org');
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $org_name = $c->param('org');
|
||||
|
||||
my $values_rs = $c->result_set->find($set_id)->values->search(
|
||||
{
|
||||
org_name => $org_name,
|
||||
ignore_value => 0,
|
||||
}
|
||||
);
|
||||
|
||||
unless ( $values_rs->count > 0 ) {
|
||||
$c->flash( error => 'Organisation not found or all values are ignored' );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
|
||||
my $lookup_result = $c->result_set->find($set_id)->lookups->find(
|
||||
{ name => $org_name },
|
||||
);
|
||||
|
||||
my $entity_id = $c->param('entity');
|
||||
|
||||
my $orgs_rs = $c->schema->resultset('Organisation');
|
||||
|
||||
if ( defined $entity_id && $orgs_rs->find({ entity_id => $entity_id }) ) {
|
||||
if ( defined $lookup_result ) {
|
||||
$lookup_result->update({ entity_id => $entity_id });
|
||||
} else {
|
||||
$lookup_result = $c->result_set->find($set_id)->lookups->create(
|
||||
my $values_rs = $c->result_set->find($set_id)->values->search(
|
||||
{
|
||||
name => $org_name,
|
||||
entity_id => $entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
} elsif ( defined $entity_id ) {
|
||||
$c->stash( error => "Organisation does not exist" );
|
||||
}
|
||||
org_name => $org_name,
|
||||
ignore_value => 0,
|
||||
}
|
||||
);
|
||||
|
||||
$c->stash(
|
||||
orgs_rs => $orgs_rs,
|
||||
lookup => $lookup_result,
|
||||
org_name => $org_name,
|
||||
);
|
||||
unless ( $values_rs->count > 0 ) {
|
||||
$c->flash(
|
||||
error => 'Organisation not found or all values are ignored' );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
|
||||
my $lookup_result =
|
||||
$c->result_set->find($set_id)->lookups->find( { name => $org_name }, );
|
||||
|
||||
my $entity_id = $c->param('entity');
|
||||
|
||||
my $orgs_rs = $c->schema->resultset('Organisation');
|
||||
|
||||
if ( defined $entity_id && $orgs_rs->find( { entity_id => $entity_id } ) ) {
|
||||
if ( defined $lookup_result ) {
|
||||
$lookup_result->update( { entity_id => $entity_id } );
|
||||
}
|
||||
else {
|
||||
$lookup_result = $c->result_set->find($set_id)->lookups->create(
|
||||
{
|
||||
name => $org_name,
|
||||
entity_id => $entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
elsif ( defined $entity_id ) {
|
||||
$c->stash( error => "Organisation does not exist" );
|
||||
}
|
||||
|
||||
$c->stash(
|
||||
orgs_rs => $orgs_rs,
|
||||
lookup => $lookup_result,
|
||||
org_name => $org_name,
|
||||
);
|
||||
}
|
||||
|
||||
sub ignore_value {
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $value_id = $c->param('value_id');
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $value_id = $c->param('value_id');
|
||||
|
||||
my $set_result = $c->result_set->find($set_id);
|
||||
unless ( defined $set_result ) {
|
||||
$c->flash( error => "Set does not exist" );
|
||||
return $c->redirect_to( '/admin/import' );
|
||||
}
|
||||
my $set_result = $c->result_set->find($set_id);
|
||||
unless ( defined $set_result ) {
|
||||
$c->flash( error => "Set does not exist" );
|
||||
return $c->redirect_to('/admin/import');
|
||||
}
|
||||
|
||||
my $value_result = $set_result->values->find($value_id);
|
||||
unless ( defined $value_result ) {
|
||||
$c->flash( error => "Value does not exist" );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
my $value_result = $set_result->values->find($value_id);
|
||||
unless ( defined $value_result ) {
|
||||
$c->flash( error => "Value does not exist" );
|
||||
return $c->redirect_to( '/admin/import/' . $set_id );
|
||||
}
|
||||
|
||||
$value_result->update({ ignore_value => $value_result->ignore_value ? 0 : 1 });
|
||||
$value_result->update(
|
||||
{ ignore_value => $value_result->ignore_value ? 0 : 1 } );
|
||||
|
||||
$c->flash( success => "Updated value" );
|
||||
my $referer = $c->req->headers->header('Referer');
|
||||
return $c->redirect_to(
|
||||
defined $referer
|
||||
? $c->url_for($referer)->path_query
|
||||
: '/admin/import/' . $set_id
|
||||
);
|
||||
$c->flash( success => "Updated value" );
|
||||
my $referer = $c->req->headers->header('Referer');
|
||||
return $c->redirect_to(
|
||||
defined $referer
|
||||
? $c->url_for($referer)->path_query
|
||||
: '/admin/import/' . $set_id
|
||||
);
|
||||
}
|
||||
|
||||
sub run_import {
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
my $c = shift;
|
||||
my $set_id = $c->param('set_id');
|
||||
|
||||
my $set_result = $c->result_set->find($set_id);
|
||||
unless ( defined $set_result ) {
|
||||
$c->flash( error => "Set does not exist" );
|
||||
return $c->redirect_to( '/admin/import' );
|
||||
}
|
||||
|
||||
my $import_value_rs = $c->result_set->get_values($set_id, undef, undef);
|
||||
my $import_lookup = $c->result_set->get_lookups($set_id);
|
||||
my $entity_rs = $c->schema->resultset('Entity');
|
||||
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
for my $value_result ( $import_value_rs->all ) {
|
||||
my $user_lookup = $import_lookup->{ $value_result->user_name };
|
||||
my $org_lookup = $import_lookup->{ $value_result->org_name };
|
||||
my $value_lookup = $c->parse_currency( $value_result->purchase_value );
|
||||
|
||||
if ( defined $user_lookup && defined $org_lookup && $value_lookup ) {
|
||||
my $user_entity = $entity_rs->find($user_lookup->{entity_id});
|
||||
my $org_entity = $entity_rs->find($org_lookup->{entity_id});
|
||||
my $distance = $c->get_distance_from_coords( $user_entity->type_object, $org_entity->type_object );
|
||||
my $transaction = $c->schema->resultset('Transaction')->create(
|
||||
{
|
||||
buyer => $user_entity,
|
||||
seller => $org_entity,
|
||||
value => $value_lookup * 100000,
|
||||
purchase_time => $value_result->purchase_date,
|
||||
distance => $distance,
|
||||
}
|
||||
);
|
||||
$value_result->update({transaction_id => $transaction->id });
|
||||
} else {
|
||||
$c->app->log->warn("Failed value import for value id [" . $value_result->id . "], ignoring");
|
||||
}
|
||||
}
|
||||
my $set_result = $c->result_set->find($set_id);
|
||||
unless ( defined $set_result ) {
|
||||
$c->flash( error => "Set does not exist" );
|
||||
return $c->redirect_to('/admin/import');
|
||||
}
|
||||
);
|
||||
|
||||
$c->flash( success => "Import completed for ready values" );
|
||||
my $referer = $c->req->headers->header('Referer');
|
||||
return $c->redirect_to(
|
||||
defined $referer
|
||||
? $c->url_for($referer)->path_query
|
||||
: '/admin/import/' . $set_id
|
||||
);
|
||||
my $import_value_rs = $c->result_set->get_values( $set_id, undef, undef );
|
||||
my $import_lookup = $c->result_set->get_lookups($set_id);
|
||||
my $entity_rs = $c->schema->resultset('Entity');
|
||||
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
for my $value_result ( $import_value_rs->all ) {
|
||||
my $user_lookup = $import_lookup->{ $value_result->user_name };
|
||||
my $org_lookup = $import_lookup->{ $value_result->org_name };
|
||||
my $value_lookup =
|
||||
$c->parse_currency( $value_result->purchase_value );
|
||||
|
||||
if ( defined $user_lookup
|
||||
&& defined $org_lookup
|
||||
&& $value_lookup )
|
||||
{
|
||||
my $user_entity =
|
||||
$entity_rs->find( $user_lookup->{entity_id} );
|
||||
my $org_entity =
|
||||
$entity_rs->find( $org_lookup->{entity_id} );
|
||||
my $distance =
|
||||
$c->get_distance_from_coords( $user_entity->type_object,
|
||||
$org_entity->type_object );
|
||||
my $transaction =
|
||||
$c->schema->resultset('Transaction')->create(
|
||||
{
|
||||
buyer => $user_entity,
|
||||
seller => $org_entity,
|
||||
value => $value_lookup * 100000,
|
||||
purchase_time => $value_result->purchase_date,
|
||||
distance => $distance,
|
||||
}
|
||||
);
|
||||
$value_result->update(
|
||||
{ transaction_id => $transaction->id } );
|
||||
}
|
||||
else {
|
||||
$c->app->log->warn( "Failed value import for value id ["
|
||||
. $value_result->id
|
||||
. "], ignoring" );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
$c->flash( success => "Import completed for ready values" );
|
||||
my $referer = $c->req->headers->header('Referer');
|
||||
return $c->redirect_to(
|
||||
defined $referer
|
||||
? $c->url_for($referer)->path_query
|
||||
: '/admin/import/' . $set_id
|
||||
);
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -5,123 +5,131 @@ use Try::Tiny;
|
|||
use Mojo::File qw/path/;
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
$c->stash->{org_entities} = [
|
||||
map {
|
||||
{ id => $_->entity_id, name => $_->name }
|
||||
} $c->schema->resultset('Organisation')->search({ name => { like => '%lancashire%' }}, { columns => [qw/ entity_id name / ]})
|
||||
];
|
||||
my $c = shift;
|
||||
$c->stash->{org_entities} = [
|
||||
map { { id => $_->entity_id, name => $_->name } }
|
||||
$c->schema->resultset('Organisation')->search(
|
||||
{ name => { like => '%lancashire%' } },
|
||||
{ columns => [qw/ entity_id name /] }
|
||||
)
|
||||
];
|
||||
|
||||
$c->app->max_request_size(104857600);
|
||||
$c->app->max_request_size(104857600);
|
||||
}
|
||||
|
||||
sub post_suppliers {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
unless ($c->param('suppliers_csv')) {
|
||||
$c->flash(error => "No CSV file given");
|
||||
unless ( $c->param('suppliers_csv') ) {
|
||||
$c->flash( error => "No CSV file given" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ( $c->req->is_limit_exceeded ) {
|
||||
$c->flash( error => "CSV file size is too large" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('suppliers_csv');
|
||||
|
||||
my $filename =
|
||||
path( $c->app->config->{upload_path}, time . 'suppliers.csv' );
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue( 'csv_supplier_import' => [$filename] );
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash( success =>
|
||||
"CSV import started, see status of minion job at: $job_url" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ($c->req->is_limit_exceeded) {
|
||||
$c->flash(error => "CSV file size is too large");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('suppliers_csv');
|
||||
|
||||
my $filename = path($c->app->config->{upload_path}, time . 'suppliers.csv');
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue('csv_supplier_import' => [ $filename ]);
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
sub post_postcodes {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
unless ($c->param('postcodes_csv')) {
|
||||
$c->flash(error => "No CSV file given");
|
||||
unless ( $c->param('postcodes_csv') ) {
|
||||
$c->flash( error => "No CSV file given" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ( $c->req->is_limit_exceeded ) {
|
||||
$c->flash( error => "CSV file size is too large" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('postcodes_csv');
|
||||
|
||||
my $filename =
|
||||
path( $c->app->config->{upload_path}, time . 'postcodes.csv' );
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue( 'csv_postcode_import' => [$filename] );
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash( success =>
|
||||
"CSV import started, see status of minion job at: $job_url" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ($c->req->is_limit_exceeded) {
|
||||
$c->flash(error => "CSV file size is too large");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('postcodes_csv');
|
||||
|
||||
my $filename = path($c->app->config->{upload_path}, time . 'postcodes.csv');
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue('csv_postcode_import' => [ $filename ]);
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
sub post_transactions {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
unless ($c->param('entity_id') ne '') {
|
||||
$c->flash(error => "Please Choose an organisation");
|
||||
unless ( $c->param('entity_id') ne '' ) {
|
||||
$c->flash( error => "Please Choose an organisation" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
unless ( $c->param('transactions_csv') ) {
|
||||
$c->flash( error => "No CSV file given" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ( $c->req->is_limit_exceeded ) {
|
||||
$c->flash( error => "CSV file size is too large" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('transactions_csv');
|
||||
|
||||
my $filename =
|
||||
path( $c->app->config->{upload_path}, time . 'transactions.csv' );
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue(
|
||||
'csv_transaction_import' => [ $filename, $c->param('entity_id') ] );
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash( success =>
|
||||
"CSV import started, see status of minion job at: $job_url" );
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
unless ($c->param('transactions_csv')) {
|
||||
$c->flash(error => "No CSV file given");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
# Check file size
|
||||
if ($c->req->is_limit_exceeded) {
|
||||
$c->flash(error => "CSV file size is too large");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
my $file = $c->param('transactions_csv');
|
||||
|
||||
my $filename = path($c->app->config->{upload_path}, time . 'transactions.csv');
|
||||
|
||||
$file->move_to($filename);
|
||||
|
||||
my $job_id = $c->minion->enqueue('csv_transaction_import' => [ $filename, $c->param('entity_id') ]);
|
||||
|
||||
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
||||
|
||||
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
|
||||
return $c->redirect_to('/admin/import_from');
|
||||
}
|
||||
|
||||
sub org_search {
|
||||
my $c = shift;
|
||||
my $term = $c->param('term');
|
||||
my $c = shift;
|
||||
my $term = $c->param('term');
|
||||
|
||||
my $rs = $c->schema->resultset('Organisation')->search(
|
||||
{ name => { like => $term . '%' } },
|
||||
{
|
||||
join => 'entity',
|
||||
columns => [ qw/ me.name entity.id / ]
|
||||
},
|
||||
);
|
||||
my $rs = $c->schema->resultset('Organisation')->search(
|
||||
{ name => { like => $term . '%' } },
|
||||
{
|
||||
join => 'entity',
|
||||
columns => [qw/ me.name entity.id /]
|
||||
},
|
||||
);
|
||||
|
||||
my @results = ( map { {
|
||||
label => $_->name,
|
||||
value => $_->entity->id,
|
||||
} } $rs->all);
|
||||
my @results = (
|
||||
map { { label => $_->name, value => $_->entity->id, } } $rs->all
|
||||
);
|
||||
|
||||
$c->render( json => \@results );
|
||||
$c->render( json => \@results );
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -4,261 +4,297 @@ use Mojo::Base 'Mojolicious::Controller';
|
|||
use Try::Tiny;
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Organisation');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Organisation');
|
||||
};
|
||||
|
||||
sub list {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $orgs_rs = $c->schema->resultset('Organisation')->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -asc => 'name' },
|
||||
},
|
||||
);
|
||||
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,
|
||||
);
|
||||
$c->stash( orgs_rs => $orgs_rs, );
|
||||
}
|
||||
|
||||
sub add_org {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
}
|
||||
|
||||
sub add_org_submit {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
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');
|
||||
$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' );
|
||||
}
|
||||
|
||||
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);
|
||||
if ( $validation->has_error ) {
|
||||
$c->flash( error => 'The validation has failed' );
|
||||
return $c->redirect_to('/admin/organisations/add');
|
||||
}
|
||||
};
|
||||
|
||||
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 );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
sub valid_read {
|
||||
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,
|
||||
};
|
||||
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,
|
||||
);
|
||||
$c->stash(
|
||||
valid_org => $valid_org,
|
||||
transactions => $transactions,
|
||||
associations => $assoc,
|
||||
);
|
||||
}
|
||||
|
||||
sub valid_edit {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
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');
|
||||
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') );
|
||||
}
|
||||
|
||||
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 {
|
||||
if ( @_ ) {use Devel::Dwarn; Dwarn \@_;
|
||||
$c->flash( error => 'Something went wrong Updating the Organisation' );
|
||||
} else {
|
||||
$c->flash( success => 'Updated Organisation' );
|
||||
if ( $validation->has_error ) {
|
||||
$c->flash( error => 'The validation has failed' );
|
||||
return $c->redirect_to( '/admin/organisations/' . $c->param('id') );
|
||||
}
|
||||
};
|
||||
$c->redirect_to( '/admin/organisations/' . $c->param('id') );
|
||||
|
||||
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 {
|
||||
if (@_) {
|
||||
use Devel::Dwarn;
|
||||
Dwarn \@_;
|
||||
$c->flash(
|
||||
error => 'Something went wrong Updating the Organisation' );
|
||||
}
|
||||
else {
|
||||
$c->flash( success => 'Updated Organisation' );
|
||||
}
|
||||
};
|
||||
$c->redirect_to( '/admin/organisations/' . $c->param('id') );
|
||||
}
|
||||
|
||||
sub merge_list {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $org_id = $c->param('id');
|
||||
my $org_result = $c->result_set->find($org_id);
|
||||
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' },
|
||||
if ( defined $org_result->entity->user ) {
|
||||
$c->flash( error => 'Cannot merge from user-owned organisation!' );
|
||||
$c->redirect_to( '/admin/organisations/' . $org_id );
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
$c->stash(
|
||||
org_result => $org_result,
|
||||
org_rs => $org_rs,
|
||||
);
|
||||
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 $c = shift;
|
||||
|
||||
my $org_id = $c->param('id');
|
||||
my $org_result = $c->result_set->find($org_id);
|
||||
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;
|
||||
}
|
||||
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 $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;
|
||||
}
|
||||
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,
|
||||
);
|
||||
$c->stash(
|
||||
org_result => $org_result,
|
||||
target_result => $target_result,
|
||||
);
|
||||
}
|
||||
|
||||
sub merge_confirm {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $org_id = $c->param('id');
|
||||
my $org_result = $c->result_set->find($org_id);
|
||||
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;
|
||||
}
|
||||
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');
|
||||
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 );
|
||||
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;
|
||||
|
|
|
@ -4,77 +4,78 @@ use Mojo::Base 'Mojolicious::Controller';
|
|||
use Mojo::JSON qw/ encode_json /;
|
||||
|
||||
sub transaction_data {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $quantised_column = 'quantised_hours';
|
||||
if ( defined $c->param('scale') && $c->param('scale') eq 'days' ) {
|
||||
$quantised_column = 'quantised_days';
|
||||
}
|
||||
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
|
||||
{},
|
||||
{
|
||||
columns => [
|
||||
{
|
||||
quantised => $quantised_column,
|
||||
count => \"COUNT(*)",
|
||||
sum_distance => $c->pg_or_sqlite(
|
||||
'SUM("me"."distance")',
|
||||
'SUM("me"."distance")',
|
||||
),
|
||||
average_distance => $c->pg_or_sqlite(
|
||||
'AVG("me"."distance")',
|
||||
'AVG("me"."distance")',
|
||||
),
|
||||
sum_value => $c->pg_or_sqlite(
|
||||
'SUM("me"."value")',
|
||||
'SUM("me"."value")',
|
||||
),
|
||||
average_value => $c->pg_or_sqlite(
|
||||
'AVG("me"."value")',
|
||||
'AVG("me"."value")',
|
||||
),
|
||||
}
|
||||
],
|
||||
group_by => $quantised_column,
|
||||
order_by => { '-asc' => $quantised_column },
|
||||
my $quantised_column = 'quantised_hours';
|
||||
if ( defined $c->param('scale') && $c->param('scale') eq 'days' ) {
|
||||
$quantised_column = 'quantised_days';
|
||||
}
|
||||
);
|
||||
|
||||
my $transaction_data = [
|
||||
map{
|
||||
my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised'));
|
||||
{
|
||||
sum_value => ($_->get_column('sum_value') || 0) * 1,
|
||||
sum_distance => ($_->get_column('sum_distance') || 0) * 1,
|
||||
average_value => ($_->get_column('average_value') || 0) * 1,
|
||||
average_distance => ($_->get_column('average_distance') || 0) * 1,
|
||||
count => $_->get_column('count'),
|
||||
quantised => $c->format_iso_datetime($quantised),
|
||||
}
|
||||
} $transaction_rs->all
|
||||
];
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $transaction_rs =
|
||||
$c->schema->resultset( 'ViewQuantisedTransaction' . $driver )->search(
|
||||
{},
|
||||
{
|
||||
columns => [
|
||||
{
|
||||
quantised => $quantised_column,
|
||||
count => \"COUNT(*)",
|
||||
sum_distance => $c->pg_or_sqlite(
|
||||
'SUM("me"."distance")', 'SUM("me"."distance")',
|
||||
),
|
||||
average_distance => $c->pg_or_sqlite(
|
||||
'AVG("me"."distance")', 'AVG("me"."distance")',
|
||||
),
|
||||
sum_value => $c->pg_or_sqlite(
|
||||
'SUM("me"."value")', 'SUM("me"."value")',
|
||||
),
|
||||
average_value => $c->pg_or_sqlite(
|
||||
'AVG("me"."value")', 'AVG("me"."value")',
|
||||
),
|
||||
}
|
||||
],
|
||||
group_by => $quantised_column,
|
||||
order_by => { '-asc' => $quantised_column },
|
||||
}
|
||||
);
|
||||
|
||||
$c->respond_to(
|
||||
json => { json => { data => $transaction_data } },
|
||||
html => { transaction_rs => encode_json( $transaction_data ) },
|
||||
);
|
||||
my $transaction_data = [
|
||||
map {
|
||||
my $quantised = $c->db_datetime_parser->parse_datetime(
|
||||
$_->get_column('quantised') );
|
||||
{
|
||||
sum_value => ( $_->get_column('sum_value') || 0 ) * 1,
|
||||
sum_distance => ( $_->get_column('sum_distance') || 0 ) * 1,
|
||||
average_value => ( $_->get_column('average_value') || 0 ) * 1,
|
||||
average_distance => ( $_->get_column('average_distance') || 0 )
|
||||
* 1,
|
||||
count => $_->get_column('count'),
|
||||
quantised => $c->format_iso_datetime($quantised),
|
||||
}
|
||||
} $transaction_rs->all
|
||||
];
|
||||
|
||||
$c->respond_to(
|
||||
json => { json => { data => $transaction_data } },
|
||||
html => { transaction_rs => encode_json($transaction_data) },
|
||||
);
|
||||
}
|
||||
|
||||
sub pg_or_sqlite {
|
||||
my ( $c, $pg_sql, $sqlite_sql ) = @_;
|
||||
my ( $c, $pg_sql, $sqlite_sql ) = @_;
|
||||
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
|
||||
if ( $driver eq 'Pg' ) {
|
||||
return \$pg_sql;
|
||||
} elsif ( $driver eq 'SQLite' ) {
|
||||
return \$sqlite_sql;
|
||||
} else {
|
||||
$c->app->log->warn('Unknown Driver Used');
|
||||
return;
|
||||
}
|
||||
if ( $driver eq 'Pg' ) {
|
||||
return \$pg_sql;
|
||||
}
|
||||
elsif ( $driver eq 'SQLite' ) {
|
||||
return \$sqlite_sql;
|
||||
}
|
||||
else {
|
||||
$c->app->log->warn('Unknown Driver Used');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -2,96 +2,109 @@ package Pear::LocalLoop::Controller::Admin::Tokens;
|
|||
use Mojo::Base 'Mojolicious::Controller';
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('AccountToken');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('AccountToken');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $token_rs = $c->result_set;
|
||||
$token_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||
$c->stash( tokens => [ $token_rs->all ] );
|
||||
my $token_rs = $c->result_set;
|
||||
$token_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||
$c->stash( tokens => [ $token_rs->all ] );
|
||||
}
|
||||
|
||||
# POST
|
||||
sub create {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
$validation->required('token', 'trim')->like(qr/^[\w]*$/)->not_in_resultset('name', $c->result_set);
|
||||
my $validation = $c->validation;
|
||||
$validation->required( 'token', 'trim' )->like(qr/^[\w]*$/)
|
||||
->not_in_resultset( 'name', $c->result_set );
|
||||
|
||||
my $token_name = $validation->param('token');
|
||||
my $token_name = $validation->param('token');
|
||||
|
||||
if ( $validation->has_error ) {
|
||||
my $check = shift @{ $c->validation->error('token') };
|
||||
if ( $check eq 'required' ) {
|
||||
$c->flash( error => 'Token name is required' );
|
||||
} elsif ( $check eq 'like' ) {
|
||||
$c->flash( error => 'Token name not valid - Alphanumeric characters and Underscore only' );
|
||||
} elsif ( $check eq 'not_in_resultset' ) {
|
||||
$c->flash( error => 'Token Already Exists' );
|
||||
}
|
||||
} else {
|
||||
$c->flash( success => 'Token Created' );
|
||||
$c->result_set->create({ name => $token_name });
|
||||
}
|
||||
$c->redirect_to( '/admin/tokens' );
|
||||
if ( $validation->has_error ) {
|
||||
my $check = shift @{ $c->validation->error('token') };
|
||||
if ( $check eq 'required' ) {
|
||||
$c->flash( error => 'Token name is required' );
|
||||
}
|
||||
elsif ( $check eq 'like' ) {
|
||||
$c->flash( error =>
|
||||
'Token name not valid - Alphanumeric characters and Underscore only'
|
||||
);
|
||||
}
|
||||
elsif ( $check eq 'not_in_resultset' ) {
|
||||
$c->flash( error => 'Token Already Exists' );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$c->flash( success => 'Token Created' );
|
||||
$c->result_set->create( { name => $token_name } );
|
||||
}
|
||||
$c->redirect_to('/admin/tokens');
|
||||
}
|
||||
|
||||
# GET
|
||||
sub read {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $token = $c->result_set->find($id) ) {
|
||||
$c->stash( token => $token );
|
||||
} else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
$c->redirect_to( '/admin/tokens' );
|
||||
}
|
||||
if ( my $token = $c->result_set->find($id) ) {
|
||||
$c->stash( token => $token );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
$c->redirect_to('/admin/tokens');
|
||||
}
|
||||
}
|
||||
|
||||
# POST
|
||||
sub update {
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required('token', 'trim')->like(qr/^[\w]*$/);
|
||||
$validation->required('used')->in( qw/ 0 1 / );
|
||||
my $c = shift;
|
||||
my $validation = $c->validation;
|
||||
$validation->required( 'token', 'trim' )->like(qr/^[\w]*$/);
|
||||
$validation->required('used')->in(qw/ 0 1 /);
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( $validation->has_error ) {
|
||||
my $names = $validation->failed;
|
||||
$c->flash( error => 'Error in submitted data: ' . join(', ', @$names) );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
} elsif ( my $token = $c->result_set->find($id) ) {
|
||||
$token->update({
|
||||
name => $validation->param('token'),
|
||||
used => $validation->param('used'),
|
||||
});
|
||||
$c->flash( success => 'Token Updated' );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
} else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
$c->redirect_to( '/admin/tokens' );
|
||||
}
|
||||
if ( $validation->has_error ) {
|
||||
my $names = $validation->failed;
|
||||
$c->flash(
|
||||
error => 'Error in submitted data: ' . join( ', ', @$names ) );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
}
|
||||
elsif ( my $token = $c->result_set->find($id) ) {
|
||||
$token->update(
|
||||
{
|
||||
name => $validation->param('token'),
|
||||
used => $validation->param('used'),
|
||||
}
|
||||
);
|
||||
$c->flash( success => 'Token Updated' );
|
||||
$c->redirect_to( '/admin/tokens/' . $id );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
$c->redirect_to('/admin/tokens');
|
||||
}
|
||||
}
|
||||
|
||||
# DELETE
|
||||
sub delete {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $token = $c->result_set->find($id) ) {
|
||||
$token->delete;
|
||||
$c->flash( success => 'Token Deleted' );
|
||||
} else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
}
|
||||
$c->redirect_to( '/admin/tokens' );
|
||||
if ( my $token = $c->result_set->find($id) ) {
|
||||
$token->delete;
|
||||
$c->flash( success => 'Token Deleted' );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No Token found' );
|
||||
}
|
||||
$c->redirect_to('/admin/tokens');
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -4,117 +4,133 @@ use Mojo::Base 'Mojolicious::Controller';
|
|||
use List::Util qw/ max sum /;
|
||||
|
||||
has result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Transaction');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Transaction');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $pending_transaction_rs = $c->schema->resultset('Organisation')->search({ pending => 1 })->entity->sales;
|
||||
my $pending_transaction_rs =
|
||||
$c->schema->resultset('Organisation')->search( { pending => 1 } )
|
||||
->entity->sales;
|
||||
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
|
||||
{},
|
||||
{
|
||||
select => [
|
||||
{ count => 'me.value', '-as' => 'count' },
|
||||
{ sum => 'me.value', '-as' => 'sum_value' },
|
||||
'quantised_weeks',
|
||||
],
|
||||
group_by => 'quantised_weeks',
|
||||
order_by => { '-asc' => 'quantised_weeks' },
|
||||
}
|
||||
);
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $week_transaction_rs =
|
||||
$c->schema->resultset( 'ViewQuantisedTransaction' . $driver )->search(
|
||||
{},
|
||||
{
|
||||
select => [
|
||||
{ count => 'me.value', '-as' => 'count' },
|
||||
{ sum => 'me.value', '-as' => 'sum_value' },
|
||||
'quantised_weeks',
|
||||
],
|
||||
group_by => 'quantised_weeks',
|
||||
order_by => { '-asc' => 'quantised_weeks' },
|
||||
}
|
||||
);
|
||||
|
||||
my @all_weeks = $week_transaction_rs->all;
|
||||
my $first_week_count = defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0;
|
||||
my $first_week_value = defined $all_weeks[0] ? $all_weeks[0]->get_column('sum_value') / 100000 || 0 : 0;
|
||||
my $second_week_count = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0;
|
||||
my $second_week_value = defined $all_weeks[1] ? $all_weeks[1]->get_column('sum_value') / 100000 || 0 : 0;
|
||||
my @all_weeks = $week_transaction_rs->all;
|
||||
my $first_week_count =
|
||||
defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0;
|
||||
my $first_week_value =
|
||||
defined $all_weeks[0]
|
||||
? $all_weeks[0]->get_column('sum_value') / 100000 || 0
|
||||
: 0;
|
||||
my $second_week_count =
|
||||
defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0;
|
||||
my $second_week_value =
|
||||
defined $all_weeks[1]
|
||||
? $all_weeks[1]->get_column('sum_value') / 100000 || 0
|
||||
: 0;
|
||||
|
||||
my $transaction_rs = $c->schema->resultset('Transaction');
|
||||
my $value_rs_col = $transaction_rs->get_column('value');
|
||||
my $max_value = $value_rs_col->max / 100000 || 0;
|
||||
my $avg_value = sprintf( '%.2f', $value_rs_col->func('AVG') / 100000) || 0;
|
||||
my $sum_value = $value_rs_col->sum / 100000 || 0;
|
||||
my $count = $transaction_rs->count || 0;
|
||||
my $transaction_rs = $c->schema->resultset('Transaction');
|
||||
my $value_rs_col = $transaction_rs->get_column('value');
|
||||
my $max_value = $value_rs_col->max / 100000 || 0;
|
||||
my $avg_value = sprintf( '%.2f', $value_rs_col->func('AVG') / 100000 ) || 0;
|
||||
my $sum_value = $value_rs_col->sum / 100000 || 0;
|
||||
my $count = $transaction_rs->count || 0;
|
||||
|
||||
my $placeholder = 'Placeholder';
|
||||
$c->stash(
|
||||
placeholder => $placeholder,
|
||||
pending_trans => $pending_transaction_rs->count,
|
||||
weeks => {
|
||||
first_count => $first_week_count,
|
||||
second_count => $second_week_count,
|
||||
first_value => $first_week_value,
|
||||
second_value => $second_week_value,
|
||||
max => $max_value,
|
||||
avg => $avg_value,
|
||||
sum => $sum_value,
|
||||
count => $count,
|
||||
},
|
||||
);
|
||||
my $placeholder = 'Placeholder';
|
||||
$c->stash(
|
||||
placeholder => $placeholder,
|
||||
pending_trans => $pending_transaction_rs->count,
|
||||
weeks => {
|
||||
first_count => $first_week_count,
|
||||
second_count => $second_week_count,
|
||||
first_value => $first_week_value,
|
||||
second_value => $second_week_value,
|
||||
max => $max_value,
|
||||
avg => $avg_value,
|
||||
sum => $sum_value,
|
||||
count => $count,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
sub read {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $transaction = $c->result_set->find($id) ) {
|
||||
$c->stash( transaction => $transaction );
|
||||
} else {
|
||||
$c->flash( error => 'No transaction found' );
|
||||
$c->redirect_to( '/admin/transactions' );
|
||||
}
|
||||
if ( my $transaction = $c->result_set->find($id) ) {
|
||||
$c->stash( transaction => $transaction );
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No transaction found' );
|
||||
$c->redirect_to('/admin/transactions');
|
||||
}
|
||||
}
|
||||
|
||||
sub image {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
my $transaction = $c->result_set->find($id);
|
||||
my $transaction = $c->result_set->find($id);
|
||||
|
||||
if ( $transaction->proof_image ) {
|
||||
$c->reply->asset($c->get_file_from_uuid($transaction->proof_image));
|
||||
} else {
|
||||
$c->reply->static('image/no_transaction.jpg');
|
||||
}
|
||||
if ( $transaction->proof_image ) {
|
||||
$c->reply->asset( $c->get_file_from_uuid( $transaction->proof_image ) );
|
||||
}
|
||||
else {
|
||||
$c->reply->static('image/no_transaction.jpg');
|
||||
}
|
||||
}
|
||||
|
||||
sub delete {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $transaction = $c->result_set->find($id) ) {
|
||||
if (defined $transaction->category) {
|
||||
$transaction->category->delete;
|
||||
if ( my $transaction = $c->result_set->find($id) ) {
|
||||
if ( defined $transaction->category ) {
|
||||
$transaction->category->delete;
|
||||
}
|
||||
$transaction->delete;
|
||||
$c->flash( success => 'Successfully deleted transaction' );
|
||||
$c->redirect_to('/admin/transactions');
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No transaction found' );
|
||||
$c->redirect_to('/admin/transactions');
|
||||
}
|
||||
$transaction->delete;
|
||||
$c->flash( success => 'Successfully deleted transaction' );
|
||||
$c->redirect_to( '/admin/transactions' );
|
||||
} else {
|
||||
$c->flash( error => 'No transaction found' );
|
||||
$c->redirect_to( '/admin/transactions' );
|
||||
}
|
||||
}
|
||||
|
||||
sub pg_or_sqlite {
|
||||
my ( $c, $pg_sql, $sqlite_sql ) = @_;
|
||||
my ( $c, $pg_sql, $sqlite_sql ) = @_;
|
||||
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||
|
||||
if ( $driver eq 'Pg' ) {
|
||||
return \$pg_sql;
|
||||
} elsif ( $driver eq 'SQLite' ) {
|
||||
return \$sqlite_sql;
|
||||
} else {
|
||||
$c->app->log->warn('Unknown Driver Used');
|
||||
return;
|
||||
}
|
||||
if ( $driver eq 'Pg' ) {
|
||||
return \$pg_sql;
|
||||
}
|
||||
elsif ( $driver eq 'SQLite' ) {
|
||||
return \$sqlite_sql;
|
||||
}
|
||||
else {
|
||||
$c->app->log->warn('Unknown Driver Used');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -5,150 +5,190 @@ use Try::Tiny;
|
|||
use Data::Dumper;
|
||||
|
||||
has user_result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('User');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('User');
|
||||
};
|
||||
|
||||
has customer_result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Customer');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Customer');
|
||||
};
|
||||
|
||||
has organisation_result_set => sub {
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Organisation');
|
||||
my $c = shift;
|
||||
return $c->schema->resultset('Organisation');
|
||||
};
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $user_rs = $c->user_result_set->search(
|
||||
undef, {
|
||||
prefech => { entity => [ qw/ customer organisation / ] },
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -asc => 'email' },
|
||||
}
|
||||
);
|
||||
$c->stash( user_rs => $user_rs );
|
||||
my $user_rs = $c->user_result_set->search(
|
||||
undef,
|
||||
{
|
||||
prefech => { entity => [qw/ customer organisation /] },
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -asc => 'email' },
|
||||
}
|
||||
);
|
||||
$c->stash( user_rs => $user_rs );
|
||||
}
|
||||
|
||||
sub read {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
if ( my $user = $c->user_result_set->find($id) ) {
|
||||
my $transactions = $user->entity->purchases->search(
|
||||
undef, {
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -desc => 'submitted_at' },
|
||||
},
|
||||
);
|
||||
$c->stash(
|
||||
user => $user,
|
||||
transactions => $transactions,
|
||||
);
|
||||
} else {
|
||||
$c->flash( error => 'No User found' );
|
||||
$c->redirect_to( '/admin/users' );
|
||||
}
|
||||
if ( my $user = $c->user_result_set->find($id) ) {
|
||||
my $transactions = $user->entity->purchases->search(
|
||||
undef,
|
||||
{
|
||||
page => $c->param('page') || 1,
|
||||
rows => 10,
|
||||
order_by => { -desc => 'submitted_at' },
|
||||
},
|
||||
);
|
||||
$c->stash(
|
||||
user => $user,
|
||||
transactions => $transactions,
|
||||
);
|
||||
}
|
||||
else {
|
||||
$c->flash( error => 'No User found' );
|
||||
$c->redirect_to('/admin/users');
|
||||
}
|
||||
}
|
||||
|
||||
sub update {
|
||||
my $c = shift;
|
||||
my $c = shift;
|
||||
|
||||
my $id = $c->param('id');
|
||||
my $id = $c->param('id');
|
||||
|
||||
my $user;
|
||||
my $user;
|
||||
|
||||
unless ( $user = $c->user_result_set->find($id) ) {
|
||||
$c->flash( error => 'No User found' );
|
||||
return $c->redirect_to( '/admin/users/' . $id );
|
||||
}
|
||||
|
||||
my $validation = $c->validation;
|
||||
|
||||
my $not_myself_user_rs = $c->user_result_set->search({
|
||||
id => { "!=" => $user->id },
|
||||
});
|
||||
$validation->required('email')->email->not_in_resultset( 'email', $not_myself_user_rs );
|
||||
$validation->required('postcode')->postcode;
|
||||
$validation->optional('new_password');
|
||||
|
||||
if ( $user->type eq 'customer' ) {
|
||||
$validation->required('display_name');
|
||||
$validation->required('full_name');
|
||||
} elsif ( $user->type eq 'organisation' ) {
|
||||
$validation->required('name');
|
||||
$validation->required('street_name');
|
||||
$validation->required('town');
|
||||
$validation->optional('sector');
|
||||
}
|
||||
|
||||
if ( $validation->has_error ) {
|
||||
$c->flash( error => 'The validation has failed' );
|
||||
return $c->redirect_to( '/admin/users/' . $id );
|
||||
}
|
||||
|
||||
my $location = $c->get_location_from_postcode(
|
||||
$validation->param('postcode'),
|
||||
$user->type,
|
||||
);
|
||||
|
||||
if ( $user->type eq 'customer' ){
|
||||
|
||||
try {
|
||||
$c->schema->txn_do( sub {
|
||||
$user->entity->customer->update({
|
||||
full_name => $validation->param('full_name'),
|
||||
display_name => $validation->param('display_name'),
|
||||
postcode => $validation->param('postcode'),
|
||||
( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ),
|
||||
});
|
||||
$user->update({
|
||||
email => $validation->param('email'),
|
||||
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
if ( @_ ) {
|
||||
$c->flash( error => 'Something went wrong Updating the User' );
|
||||
$c->app->log->warn(Dumper @_);
|
||||
} else {
|
||||
$c->flash( success => 'Updated User' );
|
||||
};
|
||||
unless ( $user = $c->user_result_set->find($id) ) {
|
||||
$c->flash( error => 'No User found' );
|
||||
return $c->redirect_to( '/admin/users/' . $id );
|
||||
}
|
||||
}
|
||||
elsif ( $user->type eq 'organisation' ) {
|
||||
|
||||
try {
|
||||
$c->schema->txn_do( sub {
|
||||
$user->entity->organisation->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 ) ),
|
||||
});
|
||||
$user->update({
|
||||
email => $validation->param('email'),
|
||||
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
if ( @_ ) {
|
||||
$c->flash( error => 'Something went wrong Updating the User' );
|
||||
$c->app->log->warn(Dumper @_);
|
||||
} else {
|
||||
$c->flash( success => 'Updated User' );
|
||||
}
|
||||
my $validation = $c->validation;
|
||||
|
||||
my $not_myself_user_rs = $c->user_result_set->search(
|
||||
{
|
||||
id => { "!=" => $user->id },
|
||||
}
|
||||
);
|
||||
$validation->required('email')
|
||||
->email->not_in_resultset( 'email', $not_myself_user_rs );
|
||||
$validation->required('postcode')->postcode;
|
||||
$validation->optional('new_password');
|
||||
|
||||
if ( $user->type eq 'customer' ) {
|
||||
$validation->required('display_name');
|
||||
$validation->required('full_name');
|
||||
}
|
||||
elsif ( $user->type eq 'organisation' ) {
|
||||
$validation->required('name');
|
||||
$validation->required('street_name');
|
||||
$validation->required('town');
|
||||
$validation->optional('sector');
|
||||
}
|
||||
};
|
||||
|
||||
$c->redirect_to( '/admin/users/' . $id );
|
||||
if ( $validation->has_error ) {
|
||||
$c->flash( error => 'The validation has failed' );
|
||||
return $c->redirect_to( '/admin/users/' . $id );
|
||||
}
|
||||
|
||||
my $location =
|
||||
$c->get_location_from_postcode( $validation->param('postcode'),
|
||||
$user->type, );
|
||||
|
||||
if ( $user->type eq 'customer' ) {
|
||||
|
||||
try {
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
$user->entity->customer->update(
|
||||
{
|
||||
full_name => $validation->param('full_name'),
|
||||
display_name => $validation->param('display_name'),
|
||||
postcode => $validation->param('postcode'),
|
||||
(
|
||||
defined $location
|
||||
? (%$location)
|
||||
: ( latitude => undef, longitude => undef )
|
||||
),
|
||||
}
|
||||
);
|
||||
$user->update(
|
||||
{
|
||||
email => $validation->param('email'),
|
||||
(
|
||||
defined $validation->param('new_password')
|
||||
? ( password =>
|
||||
$validation->param('new_password') )
|
||||
: ()
|
||||
),
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
finally {
|
||||
if (@_) {
|
||||
$c->flash( error => 'Something went wrong Updating the User' );
|
||||
$c->app->log->warn( Dumper @_ );
|
||||
}
|
||||
else {
|
||||
$c->flash( success => 'Updated User' );
|
||||
}
|
||||
}
|
||||
}
|
||||
elsif ( $user->type eq 'organisation' ) {
|
||||
|
||||
try {
|
||||
$c->schema->txn_do(
|
||||
sub {
|
||||
$user->entity->organisation->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 )
|
||||
),
|
||||
}
|
||||
);
|
||||
$user->update(
|
||||
{
|
||||
email => $validation->param('email'),
|
||||
(
|
||||
defined $validation->param('new_password')
|
||||
? ( password =>
|
||||
$validation->param('new_password') )
|
||||
: ()
|
||||
),
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
finally {
|
||||
if (@_) {
|
||||
$c->flash( error => 'Something went wrong Updating the User' );
|
||||
$c->app->log->warn( Dumper @_ );
|
||||
}
|
||||
else {
|
||||
$c->flash( success => 'Updated User' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$c->redirect_to( '/admin/users/' . $id );
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
Reference in a new issue