2019-07-02 14:21:01 +00:00
|
|
|
package Pear::LocalLoop::Controller::Admin::ImportFrom;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
2019-07-05 14:30:31 +00:00
|
|
|
use Moo;
|
2019-07-05 17:44:46 +00:00
|
|
|
use Try::Tiny;
|
2019-07-14 14:15:14 +00:00
|
|
|
use Mojo::File qw/path/;
|
2019-07-05 14:30:31 +00:00
|
|
|
|
2021-03-20 19:03:59 +00:00
|
|
|
sub idx {
|
2021-03-20 12:09:50 +00:00
|
|
|
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);
|
2021-03-20 15:02:00 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-07-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub post_suppliers {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
2019-07-04 13:16:49 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
unless ( $c->param('suppliers_csv') ) {
|
|
|
|
$c->flash( error => "No CSV file given" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
|
|
|
}
|
2019-07-05 14:30:31 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
# 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');
|
|
|
|
}
|
2019-07-08 15:41:59 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $file = $c->param('suppliers_csv');
|
2019-07-08 15:41:59 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $filename =
|
|
|
|
path( $c->app->config->{upload_path}, time . 'suppliers.csv' );
|
2019-07-08 15:41:59 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$file->move_to($filename);
|
2019-07-08 15:41:59 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_id = $c->minion->enqueue( 'csv_supplier_import' => [$filename] );
|
2019-07-08 15:41:59 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
2019-09-11 14:02:08 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->flash( success =>
|
|
|
|
"CSV import started, see status of minion job at: $job_url" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
2019-08-29 15:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub post_postcodes {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
unless ( $c->param('postcodes_csv') ) {
|
|
|
|
$c->flash( error => "No CSV file given" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
|
|
|
}
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
# 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');
|
|
|
|
}
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $file = $c->param('postcodes_csv');
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $filename =
|
|
|
|
path( $c->app->config->{upload_path}, time . 'postcodes.csv' );
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$file->move_to($filename);
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_id = $c->minion->enqueue( 'csv_postcode_import' => [$filename] );
|
2019-08-29 15:37:55 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
2019-09-11 14:02:08 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->flash( success =>
|
|
|
|
"CSV import started, see status of minion job at: $job_url" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
2019-07-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub post_transactions {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $c = shift;
|
2019-07-04 13:16:49 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
unless ( $c->param('entity_id') ne '' ) {
|
|
|
|
$c->flash( error => "Please Choose an organisation" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
|
|
|
}
|
2019-07-14 14:15:14 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
unless ( $c->param('transactions_csv') ) {
|
|
|
|
$c->flash( error => "No CSV file given" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
|
|
|
}
|
2019-07-05 14:30:31 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
# 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');
|
|
|
|
}
|
2019-07-08 17:12:35 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $file = $c->param('transactions_csv');
|
2019-07-08 17:12:35 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $filename =
|
|
|
|
path( $c->app->config->{upload_path}, time . 'transactions.csv' );
|
2019-07-08 17:12:35 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$file->move_to($filename);
|
2019-07-08 17:12:35 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_id = $c->minion->enqueue(
|
|
|
|
'csv_transaction_import' => [ $filename, $c->param('entity_id') ] );
|
2019-07-08 17:12:35 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
|
2019-09-11 14:02:08 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
$c->flash( success =>
|
|
|
|
"CSV import started, see status of minion job at: $job_url" );
|
|
|
|
return $c->redirect_to('/admin/import_from');
|
2019-07-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 14:37:26 +00:00
|
|
|
sub org_search {
|
2021-03-20 12:09:50 +00:00
|
|
|
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 @results = (
|
|
|
|
map { { label => $_->name, value => $_->entity->id, } } $rs->all
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->render( json => \@results );
|
2021-03-20 15:02:00 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-09-09 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 13:16:49 +00:00
|
|
|
1;
|