Added submitting Transactions (currently breaking on headers)
This commit is contained in:
parent
bf4b092a12
commit
46b5496901
3 changed files with 69 additions and 18 deletions
|
@ -4,6 +4,7 @@ use Moo;
|
|||
use Devel::Dwarn;
|
||||
|
||||
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||
use Pear::LocalLoop::Import::LCCCsv::Transactions;
|
||||
|
||||
sub index {
|
||||
my $c = shift;
|
||||
|
@ -46,8 +47,10 @@ sub post_transactions {
|
|||
$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') );
|
||||
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
|
||||
csv_string => $c->param('transactions_csv')->slurp,
|
||||
schema => $c->app->schema
|
||||
)->import_csv;
|
||||
|
||||
$c->flash( success => "CSV imported" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
|
|
|
@ -24,21 +24,21 @@ sub import_csv {
|
|||
sub _row_to_result {
|
||||
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', {
|
||||
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' },
|
||||
}
|
||||
});
|
||||
$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;
|
||||
|
|
|
@ -1,10 +1,58 @@
|
|||
package Pear::LocalLoop::Import::LCCCsv::Transactions;
|
||||
use Moo;
|
||||
use DateTime;
|
||||
use DateTime::Format::Strptime;
|
||||
|
||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
sub import {
|
||||
my $self = shift;
|
||||
has '+csv_required_columns' => (
|
||||
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;
|
Reference in a new issue