2019-07-02 14:21:01 +00:00
|
|
|
package Pear::LocalLoop::Import::LCCCsv::Transactions;
|
|
|
|
use Moo;
|
2019-07-05 15:52:32 +00:00
|
|
|
use DateTime;
|
|
|
|
use DateTime::Format::Strptime;
|
2019-07-02 14:21:01 +00:00
|
|
|
|
|
|
|
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
|
|
|
|
2019-07-05 15:52:32 +00:00
|
|
|
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;
|
2019-07-05 16:56:21 +00:00
|
|
|
return 0 unless $rows;
|
|
|
|
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
|
2019-07-05 15:52:32 +00:00
|
|
|
foreach my $row ( @{$rows} ) {
|
|
|
|
$self->_row_to_result($row, $lcc_org);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _row_to_result {
|
|
|
|
my ( $self, $row, $lcc_org ) = @_;
|
|
|
|
|
2019-07-05 16:56:21 +00:00
|
|
|
use Devel::Dwarn;
|
|
|
|
|
2019-07-05 15:52:32 +00:00
|
|
|
Dwarn $row;
|
|
|
|
|
2019-07-05 16:56:21 +00:00
|
|
|
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");
|
|
|
|
}
|
2019-07-05 15:52:32 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2019-07-05 16:56:21 +00:00
|
|
|
# TODO negative values
|
2019-07-05 15:52:32 +00:00
|
|
|
$self->external_result->find_or_create_related('transactions', {
|
2019-07-05 16:56:21 +00:00
|
|
|
external_id => $row->{transaction_id},
|
2019-07-05 15:52:32 +00:00
|
|
|
transaction => {
|
2019-07-05 16:56:21 +00:00
|
|
|
seller => $organisation->entity,
|
2019-07-05 15:52:32 +00:00
|
|
|
buyer => $lcc_org,
|
|
|
|
purchase_time => $paid_date,
|
|
|
|
value => $row->{net_amount},
|
|
|
|
meta => {
|
|
|
|
gross_value => $row->{gross_amount},
|
|
|
|
sales_tax_value => $row->{"vat amount"},
|
|
|
|
net_value => $row->{net_amount},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
2019-07-02 14:21:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 15:52:32 +00:00
|
|
|
1;
|