Added in importing Supplier CSV
This commit is contained in:
parent
c9fce1c887
commit
c814085630
17 changed files with 4696 additions and 23 deletions
|
@ -1,25 +1,55 @@
|
|||
package Pear::LocalLoop::Controller::Admin::ImportFrom;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
use Moo;
|
||||
use Devel::Dwarn;
|
||||
|
||||
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
|
||||
$c->app->max_request_size(1048576);
|
||||
$c->app->max_request_size(104857600);
|
||||
}
|
||||
|
||||
sub post_suppliers {
|
||||
my $c = shift;
|
||||
|
||||
Dwarn "yahoo!";
|
||||
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 $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new(
|
||||
csv_string => $c->param('suppliers_csv')->slurp,
|
||||
schema => $c->app->schema
|
||||
)->import_csv;
|
||||
|
||||
$c->flash( success => "CSV imported" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
}
|
||||
|
||||
sub post_transactions {
|
||||
my $c = shift;
|
||||
|
||||
Dwarn "yahoo!";
|
||||
$c->flash( success => "CSV imported!" );
|
||||
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 $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->import( $c->param('transactions_csv') );
|
||||
|
||||
$c->flash( success => "CSV imported" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,6 @@ extends 'Throwable::Error';
|
|||
package Pear::LocalLoop::ImplementationError;
|
||||
use Moo;
|
||||
use namespace::clean;
|
||||
extends Pear::LocalLoop::Error;
|
||||
extends 'Pear::LocalLoop::Error';
|
||||
|
||||
1;
|
||||
1;
|
||||
|
|
|
@ -2,12 +2,6 @@ package Pear::LocalLoop::Import::LCCCsv;
|
|||
use Moo;
|
||||
use Pear::LocalLoop::Error;
|
||||
|
||||
with qw/
|
||||
Pear::LocalLoop::Import::Role::ExternalName
|
||||
Pear::LocalLoop::Import::Role::Schema
|
||||
Pear::LocalLoop::Import::Role::CSV
|
||||
/;
|
||||
|
||||
has external_name => (
|
||||
is => 'ro',
|
||||
default => 'LCC CSV',
|
||||
|
@ -20,4 +14,10 @@ has csv_required_columns => (
|
|||
},
|
||||
);
|
||||
|
||||
1;
|
||||
with qw/
|
||||
Pear::LocalLoop::Import::Role::ExternalName
|
||||
Pear::LocalLoop::Import::Role::Schema
|
||||
Pear::LocalLoop::Import::Role::CSV
|
||||
/;
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,18 +1,44 @@
|
|||
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||
use Moo;
|
||||
use Devel::Dwarn;
|
||||
|
||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
sub import {
|
||||
my $self = shift;
|
||||
has '+csv_required_columns' => (
|
||||
builder => sub { return [ qw/
|
||||
supplier_id
|
||||
name
|
||||
/ ]},
|
||||
);
|
||||
|
||||
$import = Pear::LocalLoop::Import::LCCCsv->new;
|
||||
sub import_csv {
|
||||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub _row_to_result {
|
||||
my ( $self, $row ) = @_;
|
||||
|
||||
my $addr2 = $row->{post_town};
|
||||
|
||||
my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} );
|
||||
|
||||
$self->external_result->find_or_create_related('organisations', {
|
||||
external_id => $row->{supplier_id},
|
||||
organisation => {
|
||||
name => $row->{name},
|
||||
street_name => $row->{"address line 1"},
|
||||
town => $address,
|
||||
postcode => $row->{post_code},
|
||||
country => $row->{country_code},
|
||||
entity => { type => 'organisation' },
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -8,13 +8,27 @@ requires 'csv_required_columns';
|
|||
|
||||
has csv_file => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
predicate => 1,
|
||||
);
|
||||
|
||||
has csv_string => (
|
||||
is => 'ro',
|
||||
predicate => 1,
|
||||
);
|
||||
|
||||
has _csv_filehandle => (
|
||||
is => 'lazy',
|
||||
builder => sub {
|
||||
open my $fh, '<', $self->csv_file;
|
||||
my $self = shift;
|
||||
my $fh;
|
||||
if ( $self->has_csv_file ) {
|
||||
open $fh, '<', \${$self->csv_file};
|
||||
} elsif ( $self->has_csv_string ) {
|
||||
my $string = $self->csv_string;
|
||||
open $fh, '<', \$string;
|
||||
} else {
|
||||
die "Must provide csv_file or csv_string"
|
||||
}
|
||||
return $fh;
|
||||
}
|
||||
);
|
||||
|
@ -40,7 +54,25 @@ has csv_data => (
|
|||
is => 'lazy',
|
||||
builder => sub {
|
||||
my $self = shift;
|
||||
$self->check_headers;
|
||||
return $self->_text_csv->getline_hr_all( $self->_csv_filehandle );
|
||||
}
|
||||
);
|
||||
|
||||
1;
|
||||
sub check_headers {
|
||||
my $self = shift;
|
||||
my $req_headers = $self->csv_required_columns;
|
||||
use Devel::Dwarn;
|
||||
Dwarn $req_headers;
|
||||
# TODO catch the boom
|
||||
my @headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||
Dwarn \@headers;
|
||||
my %header_map = ( map { $_ => 1 } @headers );
|
||||
for my $req_header ( @$req_headers ) {
|
||||
next if $header_map{$req_header};
|
||||
die "Require header [" . $req_header . "]";
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -12,8 +12,8 @@ has external_result => (
|
|||
is => 'lazy',
|
||||
builder => sub {
|
||||
my $self = shift;
|
||||
return $self->resultset('ExternalReference')->find_or_create({ name => $self->external_name });
|
||||
return $self->schema->resultset('ExternalReference')->find_or_create({ name => $self->external_name });
|
||||
}
|
||||
);
|
||||
|
||||
1;
|
||||
1;
|
||||
|
|
|
@ -6,7 +6,7 @@ use warnings;
|
|||
|
||||
use base 'DBIx::Class::Schema';
|
||||
|
||||
our $VERSION = 26;
|
||||
our $VERSION = 27;
|
||||
|
||||
__PACKAGE__->load_namespaces;
|
||||
|
||||
|
|
|
@ -113,10 +113,16 @@ __PACKAGE__->belongs_to(
|
|||
|
||||
__PACKAGE__->belongs_to(
|
||||
"social_type",
|
||||
"Pear::LocalLoop::Schema::Result::OrganisationType",
|
||||
"Pear::LocalLoop::Schema::Result::OrganisationSocialType",
|
||||
"social_type_id",
|
||||
);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"external_reference",
|
||||
"Pear::LocalLoop::Schema::Result::OrganisationExternal",
|
||||
{ 'foreign.org_id' => 'self.id' },
|
||||
);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"payroll",
|
||||
"Pear::LocalLoop::Schema::Result::OrganisationPayroll",
|
||||
|
|
Reference in a new issue