Fully added Transaction importing
This commit is contained in:
parent
46b5496901
commit
d1cd30928e
4 changed files with 41 additions and 9 deletions
|
@ -52,8 +52,14 @@ sub post_transactions {
|
|||
schema => $c->app->schema
|
||||
)->import_csv;
|
||||
|
||||
if ($csv_import->csv_error) {
|
||||
$c->flash( error => $csv_import->csv_error );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
} else {
|
||||
$c->flash( success => "CSV imported" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -15,6 +15,7 @@ sub import_csv {
|
|||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
return $self unless $rows;
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,8 @@ sub import_csv {
|
|||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
my $lcc_org = $self->schema->resultset('Organisation')->find( name => "Lancashire County Council" );
|
||||
return 0 unless $rows;
|
||||
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row, $lcc_org);
|
||||
}
|
||||
|
@ -28,9 +29,19 @@ sub import_csv {
|
|||
sub _row_to_result {
|
||||
my ( $self, $row, $lcc_org ) = @_;
|
||||
|
||||
use Devel::Dwarn;
|
||||
|
||||
Dwarn $row;
|
||||
|
||||
my $organisation = $self->schema->resultset('Organisation')->find( external_id => $row->{supplier_id} );
|
||||
my $supplier_id = $row->{supplier_id};
|
||||
|
||||
my $organisation = $self->schema->resultset('Organisation')->find({
|
||||
'external_reference.external_id' => $supplier_id
|
||||
}, { join => 'external_reference' });
|
||||
|
||||
unless ($organisation) {
|
||||
Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id");
|
||||
}
|
||||
|
||||
my $date_formatter = DateTime::Format::Strptime->new(
|
||||
pattern => '%Y/%m/%d'
|
||||
|
@ -38,15 +49,15 @@ sub _row_to_result {
|
|||
|
||||
my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today );
|
||||
|
||||
# TODO negative values
|
||||
$self->external_result->find_or_create_related('transactions', {
|
||||
transaction_id => $row->{transaction_id},
|
||||
external_id => $row->{transaction_id},
|
||||
transaction => {
|
||||
seller => $organisation->entity->id,
|
||||
seller => $organisation->entity,
|
||||
buyer => $lcc_org,
|
||||
purchase_time => $paid_date,
|
||||
value => $row->{net_amount},
|
||||
meta => {
|
||||
transaction_id => $row->{transaction_id},
|
||||
gross_value => $row->{gross_amount},
|
||||
sales_tax_value => $row->{"vat amount"},
|
||||
net_value => $row->{net_amount},
|
||||
|
|
|
@ -3,6 +3,8 @@ use strict;
|
|||
use warnings;
|
||||
use Moo::Role;
|
||||
use Text::CSV;
|
||||
use Try::Tiny;
|
||||
use Pear::LocalLoop::Error;
|
||||
|
||||
requires 'csv_required_columns';
|
||||
|
||||
|
@ -16,6 +18,11 @@ has csv_string => (
|
|||
predicate => 1,
|
||||
);
|
||||
|
||||
has csv_error => (
|
||||
is => 'ro',
|
||||
predicate => 1,
|
||||
);
|
||||
|
||||
has _csv_filehandle => (
|
||||
is => 'lazy',
|
||||
builder => sub {
|
||||
|
@ -54,7 +61,8 @@ has csv_data => (
|
|||
is => 'lazy',
|
||||
builder => sub {
|
||||
my $self = shift;
|
||||
$self->check_headers;
|
||||
my $header_check = $self->check_headers;
|
||||
return 0 unless $header_check;
|
||||
return $self->_text_csv->getline_hr_all( $self->_csv_filehandle );
|
||||
}
|
||||
);
|
||||
|
@ -65,7 +73,13 @@ sub check_headers {
|
|||
use Devel::Dwarn;
|
||||
Dwarn $req_headers;
|
||||
# TODO catch the boom
|
||||
my @headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||
my @headers;
|
||||
try {
|
||||
@headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||
} catch {
|
||||
$self->csv_error = $_->[1];
|
||||
};
|
||||
return 0 unless @headers;
|
||||
Dwarn \@headers;
|
||||
my %header_map = ( map { $_ => 1 } @headers );
|
||||
for my $req_header ( @$req_headers ) {
|
||||
|
|
Reference in a new issue