Fix various bits for import
This commit is contained in:
parent
a45c354834
commit
71189d18fc
8 changed files with 111 additions and 69 deletions
|
@ -5,76 +5,86 @@ use DateTime::Format::Strptime;
|
|||
|
||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
has target_entity_id => (
|
||||
is => 'ro',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has target_entity => (
|
||||
is => 'lazy',
|
||||
builder => sub {
|
||||
my $self = shift;
|
||||
my $entity = $self->schema->resultset('Entity')->find($self->target_entity_id);
|
||||
Pear::LocalLoop::Error->throw("Cannot find LCC Entity, did you pass the right id?") unless $entity;
|
||||
return $entity;
|
||||
},
|
||||
);
|
||||
|
||||
has '+csv_required_columns' => (
|
||||
builder => sub { return [ (
|
||||
'transaction_id',
|
||||
'supplier_id',
|
||||
'net_amount',
|
||||
'vat amount',
|
||||
'gross_amount',
|
||||
)]},
|
||||
builder => sub {return [ (
|
||||
'transaction_id',
|
||||
'supplier_id',
|
||||
'net_amount',
|
||||
'vat 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",
|
||||
street_name => "County Hall"
|
||||
});
|
||||
unless ($lcc_org) {
|
||||
Pear::LocalLoop::Error->throw("Cannot find LCC Organisation, please contact an admin");
|
||||
}
|
||||
foreach my $row ( @{$rows} ) {
|
||||
my $lcc_org = $self->target_entity;
|
||||
|
||||
foreach my $row (@{$rows}) {
|
||||
$self->_row_to_result($row, $lcc_org);
|
||||
}
|
||||
}
|
||||
|
||||
sub _row_to_result {
|
||||
my ( $self, $row, $lcc_org ) = @_;
|
||||
my ($self, $row, $lcc_org) = @_;
|
||||
|
||||
my $supplier_id = $row->{supplier_id};
|
||||
my $supplier_id = $row->{supplier_id};
|
||||
|
||||
my $organisation = $self->schema->resultset('Organisation')->find({
|
||||
'external_reference.external_id' => $supplier_id
|
||||
}, { join => 'external_reference' });
|
||||
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");
|
||||
}
|
||||
unless ($organisation) {
|
||||
Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id");
|
||||
}
|
||||
|
||||
my $date_formatter = DateTime::Format::Strptime->new(
|
||||
pattern => '%m/%d/%Y',
|
||||
time_zone => 'Europe/London'
|
||||
);
|
||||
my $date_formatter = DateTime::Format::Strptime->new(
|
||||
pattern => '%m/%d/%Y',
|
||||
time_zone => 'Europe/London'
|
||||
);
|
||||
|
||||
my $paid_date = ( $row->{paid_date} ?
|
||||
$date_formatter->parse_datetime($row->{paid_date}) :
|
||||
$date_formatter->parse_datetime($row->{invoice_date}) );
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
# TODO negative values are sometimes present
|
||||
$self->external_result->find_or_create_related('transactions', {
|
||||
external_id => $row->{transaction_id},
|
||||
transaction => {
|
||||
seller => $organisation->entity,
|
||||
buyer => $lcc_org->entity,
|
||||
purchase_time => $paid_date,
|
||||
value => $gross_value * 100000,
|
||||
meta => {
|
||||
gross_value => $gross_value * 100000,
|
||||
sales_tax_value => $sales_tax_value * 100000,
|
||||
net_value => $net_value * 100000,
|
||||
},
|
||||
}
|
||||
});
|
||||
# TODO negative values are sometimes present
|
||||
$self->external_result->find_or_create_related('transactions', {
|
||||
external_id => $row->{transaction_id},
|
||||
transaction => {
|
||||
seller => $organisation->entity,
|
||||
buyer => $lcc_org,
|
||||
purchase_time => $paid_date,
|
||||
value => $gross_value * 100000,
|
||||
meta => {
|
||||
gross_value => $gross_value * 100000,
|
||||
sales_tax_value => $sales_tax_value * 100000,
|
||||
net_value => $net_value * 100000,
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
Reference in a new issue