Added submitting Transactions (currently breaking on headers)

This commit is contained in:
Finn 2019-07-05 16:52:32 +01:00
parent bf4b092a12
commit 46b5496901
No known key found for this signature in database
GPG key ID: 7455B4B17685B598
3 changed files with 69 additions and 18 deletions

View file

@ -4,6 +4,7 @@ use Moo;
use Devel::Dwarn; use Devel::Dwarn;
use Pear::LocalLoop::Import::LCCCsv::Suppliers; use Pear::LocalLoop::Import::LCCCsv::Suppliers;
use Pear::LocalLoop::Import::LCCCsv::Transactions;
sub index { sub index {
my $c = shift; my $c = shift;
@ -46,8 +47,10 @@ sub post_transactions {
$c->flash( error => "CSV file size is too large" ); $c->flash( error => "CSV file size is too large" );
return $c->redirect_to( '/admin/import_from' ); return $c->redirect_to( '/admin/import_from' );
} }
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->import( $c->param('transactions_csv') ); csv_string => $c->param('transactions_csv')->slurp,
schema => $c->app->schema
)->import_csv;
$c->flash( success => "CSV imported" ); $c->flash( success => "CSV imported" );
return $c->redirect_to( '/admin/import_from' ); return $c->redirect_to( '/admin/import_from' );

View file

@ -24,21 +24,21 @@ sub import_csv {
sub _row_to_result { sub _row_to_result {
my ( $self, $row ) = @_; my ( $self, $row ) = @_;
my $addr2 = $row->{post_town}; my $addr2 = $row->{post_town};
my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} ); my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} );
$self->external_result->find_or_create_related('organisations', { $self->external_result->find_or_create_related('organisations', {
external_id => $row->{supplier_id}, external_id => $row->{supplier_id},
organisation => { organisation => {
name => $row->{name}, name => $row->{name},
street_name => $row->{"address line 1"}, street_name => $row->{"address line 1"},
town => $address, town => $address,
postcode => $row->{post_code}, postcode => $row->{post_code},
country => $row->{country_code}, country => $row->{country_code},
entity => { type => 'organisation' }, entity => { type => 'organisation' },
} }
}); });
} }
1; 1;

View file

@ -1,10 +1,58 @@
package Pear::LocalLoop::Import::LCCCsv::Transactions; package Pear::LocalLoop::Import::LCCCsv::Transactions;
use Moo; use Moo;
use DateTime;
use DateTime::Format::Strptime;
extends qw/Pear::LocalLoop::Import::LCCCsv/; extends qw/Pear::LocalLoop::Import::LCCCsv/;
sub import { has '+csv_required_columns' => (
my $self = shift; builder => sub { return [ qw/
transaction_id
supplier_id
net_amount
gross_amount
/ ]},
);
sub import_csv {
my ($self) = @_;
my $rows = $self->csv_data;
my $lcc_org = $self->schema->resultset('Organisation')->find( name => "Lancashire County Council" );
foreach my $row ( @{$rows} ) {
$self->_row_to_result($row, $lcc_org);
}
return 1;
}
sub _row_to_result {
my ( $self, $row, $lcc_org ) = @_;
Dwarn $row;
my $organisation = $self->schema->resultset('Organisation')->find( external_id => $row->{supplier_id} );
my $date_formatter = DateTime::Format::Strptime->new(
pattern => '%Y/%m/%d'
);
my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today );
$self->external_result->find_or_create_related('transactions', {
transaction_id => $row->{transaction_id},
transaction => {
seller => $organisation->entity->id,
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},
},
}
});
} }
1; 1;