Fully added working import and API

This commit is contained in:
Finn 2019-07-10 17:23:27 +01:00
parent 975c348299
commit 95042d7359
3 changed files with 34 additions and 22 deletions

View file

@ -18,27 +18,29 @@ sub post_lcc_transactions {
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;
my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); my $lcc_import_ext_ref = $c->schema->resultset('ExternalReference')->find({ name => 'LCC CSV' });
my $lcc_transactions = $lcc_import_ext_ref->search_related('transactions', return 0 unless $lcc_import_ext_ref;
undef,
{ my $lcc_transactions = $lcc_import_ext_ref->transactions->search(
page => $validation->param('page') || 1, undef,
rows => 10, {
order_by => { -desc => 'purchase_time' }, page => $validation->param('page') || 1,
}, rows => 10,
); join => 'transaction',
order_by => { -desc => 'transaction.purchase_time' },
});
# purchase_time needs timezone attached to it # purchase_time needs timezone attached to it
my @transaction_list = ( my @transaction_list = (
map {{ map {{
transaction_external_id => $_->external_id, transaction_external_id => $_->external_id,
seller => $_->transaction->seller->name, seller => $_->transaction->seller->name,
net_value => $_->transaction->value, net_value => $_->transaction->meta->net_value,
gross_value => $_->transaction->meta->gross_value, gross_value => $_->transaction->meta->gross_value,
sales_tax_value => $_->transaction->meta->sales_tax_value, sales_tax_value => $_->transaction->meta->sales_tax_value,
purchase_time => $c->transaction->format_iso_datetime($_->purchase_time), purchase_time => $c->format_iso_datetime($_->transaction->purchase_time),
}} $transactions->all }} $lcc_transactions->all
); );
return $c->render( json => { return $c->render( json => {
@ -55,7 +57,7 @@ sub post_lcc_suppliers {
# TODO give an error if user is not of Lancashire County Council # TODO give an error if user is not of Lancashire County Council
my $is_lcc = $self->entity->organisation->count({ name => "Lancashire County Council" }); my $is_lcc = $user->entity->organisation->count({ name => "Lancashire County Council" });
my $validation = $c->validation; my $validation = $c->validation;
$validation->input( $c->stash->{api_json} ); $validation->input( $c->stash->{api_json} );
@ -63,7 +65,7 @@ sub post_lcc_suppliers {
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;
my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); my $lcc_import_ext_ref = $c->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' });
my $lcc_suppliers = $lcc_import_ext_ref->search_related('organisations', my $lcc_suppliers = $lcc_import_ext_ref->search_related('organisations',
undef, undef,
@ -74,7 +76,6 @@ sub post_lcc_suppliers {
}, },
); );
# purchase_time needs timezone attached to it
my @supplier_list = ( my @supplier_list = (
map {{ map {{
supplier_external_id => $_->external_id, supplier_external_id => $_->external_id,

View file

@ -27,15 +27,17 @@ sub _row_to_result {
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', { return if $self->external_result->organisations->find({external_id => $row->{supplier_id}});
external_id => $row->{supplier_id},
$self->schema->resultset('Entity')->create({
type => 'organisation',
organisation => { organisation => {
name => $row->{name}, name => $row->{name},
street_name => $row->{"address line 1"}, street_name => $row->{"address line 1"},
town => $address, town => $address,
postcode => $row->{post_code}, postcode => $row->{post_code},
country => $row->{country_code}, country => $row->{country_code},
entity => { type => 'organisation' }, external_reference => [ { external_reference => $self->external_result, external_id => $row->{supplier_id} } ],
} }
}); });
} }

View file

@ -19,9 +19,15 @@ sub import_csv {
my ($self) = @_; my ($self) = @_;
my $rows = $self->csv_data; my $rows = $self->csv_data;
# my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" }); 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} ) { foreach my $row ( @{$rows} ) {
$self->_row_to_result($row); $self->_row_to_result($row, $lcc_org);
} }
} }
@ -38,6 +44,9 @@ sub _row_to_result {
Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id"); Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id");
} }
use Devel::Dwarn;
Dwarn $organisation->entity->id;
my $date_formatter = DateTime::Format::Strptime->new( my $date_formatter = DateTime::Format::Strptime->new(
pattern => '%Y/%m/%d' pattern => '%Y/%m/%d'
); );
@ -56,9 +65,9 @@ sub _row_to_result {
external_id => $row->{transaction_id}, external_id => $row->{transaction_id},
transaction => { transaction => {
seller => $organisation->entity, seller => $organisation->entity,
buyer => $lcc_org, buyer => $lcc_org->entity,
purchase_time => $paid_date, purchase_time => $paid_date,
value => $row->{net_amount}, value => $gross_value * 100000,
meta => { meta => {
gross_value => $gross_value * 100000, gross_value => $gross_value * 100000,
sales_tax_value => $sales_tax_value * 100000, sales_tax_value => $sales_tax_value * 100000,