Fully added working import and API

This commit is contained in:
Finn 2019-07-10 17:23:27 +01:00
parent aa2d429403
commit efbf8cbad7
No known key found for this signature in database
GPG Key ID: 7455B4B17685B598
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;
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',
undef,
{
page => $validation->param('page') || 1,
rows => 10,
order_by => { -desc => 'purchase_time' },
},
);
return 0 unless $lcc_import_ext_ref;
my $lcc_transactions = $lcc_import_ext_ref->transactions->search(
undef,
{
page => $validation->param('page') || 1,
rows => 10,
join => 'transaction',
order_by => { -desc => 'transaction.purchase_time' },
});
# purchase_time needs timezone attached to it
my @transaction_list = (
map {{
transaction_external_id => $_->external_id,
seller => $_->transaction->seller->name,
net_value => $_->transaction->value,
net_value => $_->transaction->meta->net_value,
gross_value => $_->transaction->meta->gross_value,
sales_tax_value => $_->transaction->meta->sales_tax_value,
purchase_time => $c->transaction->format_iso_datetime($_->purchase_time),
}} $transactions->all
purchase_time => $c->format_iso_datetime($_->transaction->purchase_time),
}} $lcc_transactions->all
);
return $c->render( json => {
@ -55,7 +57,7 @@ sub post_lcc_suppliers {
# 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;
$validation->input( $c->stash->{api_json} );
@ -63,7 +65,7 @@ sub post_lcc_suppliers {
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',
undef,
@ -74,7 +76,6 @@ sub post_lcc_suppliers {
},
);
# purchase_time needs timezone attached to it
my @supplier_list = (
map {{
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"} );
$self->external_result->find_or_create_related('organisations', {
external_id => $row->{supplier_id},
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},
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 $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} ) {
$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");
}
use Devel::Dwarn;
Dwarn $organisation->entity->id;
my $date_formatter = DateTime::Format::Strptime->new(
pattern => '%Y/%m/%d'
);
@ -56,9 +65,9 @@ sub _row_to_result {
external_id => $row->{transaction_id},
transaction => {
seller => $organisation->entity,
buyer => $lcc_org,
buyer => $lcc_org->entity,
purchase_time => $paid_date,
value => $row->{net_amount},
value => $gross_value * 100000,
meta => {
gross_value => $gross_value * 100000,
sales_tax_value => $sales_tax_value * 100000,