Added submitting Transactions (currently breaking on headers)

This commit is contained in:
Finn 2019-07-05 16:52:32 +01:00
parent c814085630
commit 28c9ee8f65
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

@ -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;