This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm

81 lines
2.1 KiB
Perl
Raw Normal View History

2019-07-02 14:21:01 +00:00
package Pear::LocalLoop::Import::LCCCsv::Transactions;
use Moo;
use DateTime;
use DateTime::Format::Strptime;
2019-07-02 14:21:01 +00:00
extends qw/Pear::LocalLoop::Import::LCCCsv/;
has '+csv_required_columns' => (
2019-07-05 17:44:46 +00:00
builder => sub { return [ (
'transaction_id',
'supplier_id',
'net_amount',
'vat amount',
'gross_amount',
)]},
);
sub import_csv {
my ($self) = @_;
my $rows = $self->csv_data;
2019-07-10 16:23:27 +00:00
my $lcc_org = $self->schema->resultset('Organisation')->find({
name => "Lancashire County Council",
street_name => "County Hall"
});
unless ($lcc_org) {
Pear::LocalLoop::Error->throw("Cannot find LCC Organisation, please contact an admin");
}
foreach my $row ( @{$rows} ) {
2019-07-10 16:23:27 +00:00
$self->_row_to_result($row, $lcc_org);
}
}
sub _row_to_result {
my ( $self, $row, $lcc_org ) = @_;
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-10 16:23:27 +00:00
use Devel::Dwarn;
Dwarn $organisation->entity->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 );
2019-07-09 17:06:31 +00:00
my $gross_value = $row->{gross_amount};
$gross_value =~ s/,//g;
my $sales_tax_value = $row->{"vat amount"};
$sales_tax_value =~ s/,//g;
my $net_value = $row->{net_amount};
$net_value =~ s/,//g;
2019-07-09 16:55:50 +00:00
2019-07-05 17:44:46 +00:00
# TODO negative values are sometimes present
$self->external_result->find_or_create_related('transactions', {
2019-07-05 16:56:21 +00:00
external_id => $row->{transaction_id},
transaction => {
2019-07-05 16:56:21 +00:00
seller => $organisation->entity,
2019-07-10 16:23:27 +00:00
buyer => $lcc_org->entity,
purchase_time => $paid_date,
2019-07-10 16:23:27 +00:00
value => $gross_value * 100000,
meta => {
2019-07-09 17:06:31 +00:00
gross_value => $gross_value * 100000,
sales_tax_value => $sales_tax_value * 100000,
net_value => $net_value * 100000,
},
}
});
2019-07-02 14:21:01 +00:00
}
1;