diff --git a/lib/Pear/LocalLoop/Controller/Admin/Users.pm b/lib/Pear/LocalLoop/Controller/Admin/Users.pm index e731d7b..a1d4aef 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Users.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Users.pm @@ -92,6 +92,11 @@ sub update { return $c->redirect_to( '/admin/users/' . $id ); } + my $location = $c->get_location_from_postcode( + $validation->param('postcode'), + $user->type, + ); + if ( $user->type eq 'customer' ){ try { @@ -100,6 +105,7 @@ sub update { full_name => $validation->param('full_name'), display_name => $validation->param('display_name'), postcode => $validation->param('postcode'), + ( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ), }); $user->update({ email => $validation->param('email'), @@ -125,6 +131,7 @@ sub update { town => $validation->param('town'), sector => $validation->param('sector'), postcode => $validation->param('postcode'), + ( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ), }); $user->update({ email => $validation->param('email'), diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index d626c46..af36a67 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -21,7 +21,8 @@ sub post_index { my $end = DateTime->today; my $start = $end->clone->subtract_duration( $duration ); - my $data = { purchases => [] }; + my $weeks = { purchases => [] }; + my $sectors = { sectors => [], purchases => [] }; my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; @@ -47,12 +48,13 @@ sub post_index { ); for ( $transaction_rs->all ) { - push @{ $data->{ purchases } }, ($_->get_column('count') || 0); + push @{ $weeks->{ purchases } }, ($_->get_column('count') || 0); } return $c->render( json => { success => Mojo::JSON->true, - data => $data, + weeks => $weeks, + sectors => $sectors, }); } diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 15fa500..3783d7f 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -146,12 +146,18 @@ sub post_upload { return $c->api_validation_error if $validation->has_error; + my $location = $c->get_location_from_postcode( + $validation->param('postcode'), + 'organisation', + ); + my $entity = $c->schema->resultset('Entity')->create_org({ submitted_by_id => $user->id, name => $validation->param('organisation_name'), street_name => $validation->param('street_name'), town => $validation->param('town'), postcode => $validation->param('postcode'), + ( defined $location ? ( %$location ) : ( latitude => undef, longitude => undef ) ), pending => 1, }); $organisation = $entity->organisation; diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index ae0340b..06c57b1 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -5,17 +5,51 @@ sub index { my $c = shift; my $entity = $c->stash->{api_user}->entity; - my $data = { data => [] }; + + my $purchase_rs = $entity->purchases; + my $local_org_local_purchase = $purchase_rs->search({ + "me.distance" => { '<', 20000 }, + 'organisation.is_local' => 1, + }, + { + join => { 'seller' => 'organisation' }, + } + ); + + my $local_org_non_local_purchase = $purchase_rs->search({ + "me.distance" => { '>=', 20000 }, + 'organisation.is_local' => 1, + }, + { + join => { 'seller' => 'organisation' }, + } + ); + + my $non_local_org_local_purchase = $purchase_rs->search({ + "me.distance" => { '<', 20000 }, + 'organisation.is_local' => 0, + }, + { + join => { 'seller' => 'organisation' }, + } + ); + + my $non_local_org_non_local_purchase = $purchase_rs->search({ + "me.distance" => { '>=', 20000 }, + 'organisation.is_local' => 0, + }, + { + join => { 'seller' => 'organisation' }, + } + ); my $data = { - 'Local shop local purchaser' => 20, - 'Local shop non-local purchaser' => 20, - 'Non-local shop local purchaser' => 20, - 'Non-local shop non-local purchaser' => 20, + 'Local shop local purchaser' => $local_org_local_purchase->count, + 'Local shop non-local purchaser' => $local_org_non_local_purchase->count, + 'Non-local shop local purchaser' => $non_local_org_local_purchase->count, + 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, }; - - #TODO insert code fetching numbers here - + return $c->render( json => { success => Mojo::JSON->true,