From 5525479e275a7866b57502e9e17c939c3ad82ab9 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 12 Jul 2019 18:51:38 +0100 Subject: [PATCH] Added API for supplier table --- lib/Pear/LocalLoop/Controller/Api/External.pm | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm index 158d7d5..2c99645 100644 --- a/lib/Pear/LocalLoop/Controller/Api/External.pm +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -54,33 +54,41 @@ sub post_lcc_suppliers { # TODO give an error if user is not of Lancashire County Council - my $is_lcc = $user->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} ); $validation->optional('page')->number; + $validation->optional('filter'); return $c->api_validation_error if $validation->has_error; 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, - { - page => $validation->param('page') || 1, - rows => 10, - order_by => { -desc => 'organisation.name' }, - }, - ); + return 0 unless $lcc_import_ext_ref; + + my $lcc_suppliers = $user->entity->purchases->search_related('seller', + undef, + { + page => $validation->param('page') || 1, + rows => 10, + join => 'organisation', + order_by => [ + { -asc => 'organisation.name' }, + { -asc => 'seller.id' }, + ], + group_by => 'seller.id', + }); my @supplier_list = ( map {{ - supplier_external_id => $_->external_id, - name => $_->organisation->name, + entity_id => $_->id, + name => $_->name, street => $_->organisation->street_name, town => $_->organisation->town, - postcode => $_->organisation->post_code, + postcode => $_->organisation->postcode, country => $_->organisation->country, + spend => ($_->sales->get_column('value')->sum / 100000) // 0, }} $lcc_suppliers->all );