2018-01-17 16:47:05 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Categories;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2018-01-24 13:19:35 +00:00
|
|
|
use List::Util qw/ max /;
|
2018-01-17 16:47:05 +00:00
|
|
|
|
|
|
|
sub post_category_list {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $entity = $c->stash->{api_user}->entity;
|
|
|
|
|
2018-01-22 16:26:45 +00:00
|
|
|
my $duration = DateTime::Duration->new( days => 28 );
|
|
|
|
my $end = DateTime->today;
|
|
|
|
my $start = $end->clone->subtract_duration( $duration );
|
2018-01-17 16:47:05 +00:00
|
|
|
|
|
|
|
my $dtf = $c->schema->storage->datetime_parser;
|
|
|
|
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
2018-03-02 16:32:28 +00:00
|
|
|
my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search(
|
2018-01-17 16:47:05 +00:00
|
|
|
{
|
|
|
|
purchase_time => {
|
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($start),
|
|
|
|
$dtf->format_datetime($end),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
buyer_id => $entity->id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
columns => [
|
|
|
|
{
|
2018-01-22 16:26:45 +00:00
|
|
|
quantised => 'quantised_weeks',
|
2018-03-02 16:32:28 +00:00
|
|
|
value => { sum => 'value' },
|
2018-01-22 16:26:45 +00:00
|
|
|
category_id => 'category_id',
|
2018-03-02 16:32:28 +00:00
|
|
|
essential => 'essential',
|
2018-01-17 16:47:05 +00:00
|
|
|
}
|
|
|
|
],
|
2018-01-24 13:19:35 +00:00
|
|
|
group_by => [ qw/ category_id quantised_weeks / ],
|
2018-01-26 09:51:28 +00:00
|
|
|
order_by => { '-desc' => 'value' },
|
2018-01-17 16:47:05 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-02 16:32:28 +00:00
|
|
|
my $data = { categories => {}, essentials => {} };
|
2018-01-22 16:26:45 +00:00
|
|
|
|
2018-03-02 16:32:28 +00:00
|
|
|
for my $cat_trans ( $month_transaction_category_rs->all ) {
|
|
|
|
my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised'));
|
2018-01-22 16:26:45 +00:00
|
|
|
my $days = $c->format_iso_date( $quantised ) || 0;
|
2018-03-02 16:32:28 +00:00
|
|
|
my $category = $cat_trans->get_column('category_id') || 0;
|
|
|
|
my $value = ($cat_trans->get_column('value') || 0) / 100000;
|
|
|
|
$data->{categories}->{$days} = [] unless exists $data->{categories}->{$days};
|
|
|
|
push @{ $data->{categories}->{$days} }, {
|
2018-01-22 16:26:45 +00:00
|
|
|
days => $days,
|
|
|
|
value => $value,
|
|
|
|
category => $category,
|
|
|
|
};
|
2018-03-02 16:32:28 +00:00
|
|
|
next unless $cat_trans->get_column('essential');
|
|
|
|
$data->{essentials}->{$days}->{value} += $value;
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:33:57 +00:00
|
|
|
for my $day ( keys %{ $data->{categories} } ) {
|
2018-03-02 16:32:28 +00:00
|
|
|
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @{ $data->{categories}->{$day} } ];
|
2018-01-17 16:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->true,
|
2018-01-22 16:26:45 +00:00
|
|
|
data => $data,
|
2018-01-17 16:47:05 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pg_or_sqlite {
|
|
|
|
my ( $c, $pg_sql, $sqlite_sql ) = @_;
|
|
|
|
|
|
|
|
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
|
|
|
|
|
|
|
if ( $driver eq 'Pg' ) {
|
|
|
|
return \$pg_sql;
|
|
|
|
} elsif ( $driver eq 'SQLite' ) {
|
|
|
|
return \$sqlite_sql;
|
|
|
|
} else {
|
|
|
|
$c->app->log->warn('Unknown Driver Used');
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|