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/Suppliers.pm

67 lines
1.5 KiB
Perl
Raw Permalink Normal View History

2019-07-02 14:21:01 +00:00
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
use Moo;
extends qw/Pear::LocalLoop::Import::LCCCsv/;
2019-07-05 14:30:31 +00:00
has '+csv_required_columns' => (
2021-03-20 12:09:50 +00:00
builder => sub {
return [
qw/
supplier_id
name
/
];
},
2019-07-05 14:30:31 +00:00
);
2019-07-04 13:16:49 +00:00
2019-07-05 14:30:31 +00:00
sub import_csv {
2021-03-20 12:09:50 +00:00
my ($self) = @_;
2019-07-05 14:30:31 +00:00
2021-03-20 12:09:50 +00:00
$self->check_headers;
2019-07-08 15:41:59 +00:00
2021-03-20 12:09:50 +00:00
while ( my $row = $self->get_csv_line ) {
$self->_row_to_result($row);
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2019-07-02 14:21:01 +00:00
}
sub _row_to_result {
2021-03-20 12:09:50 +00:00
my ( $self, $row ) = @_;
my $addr2 = $row->{post_town};
my $address = (
defined $addr2
? ( $row->{"address line 2"} . ' ' . $addr2 )
2021-03-20 23:26:52 +00:00
: $row->{"address line 2"}
);
2021-03-20 12:09:50 +00:00
return
if $self->external_result->organisations->find(
{ external_id => $row->{supplier_id} } );
$self->schema->resultset('Entity')->create(
{
type => 'organisation',
organisation => {
name => $row->{name},
street_name => $row->{"address line 1"},
town => $address,
postcode => $row->{post_code},
country => $row->{country_code},
external_reference => [
{
external_reference => $self->external_result,
external_id => $row->{supplier_id},
}
],
}
}
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2019-07-02 14:21:01 +00:00
}
2019-07-04 13:16:49 +00:00
1;