Amended category listing

This commit is contained in:
Finn 2018-03-26 14:42:18 +01:00
parent 4844174ead
commit 4b4d50de07
3 changed files with 36 additions and 22 deletions

View file

@ -33,30 +33,31 @@ sub post_category_list {
essential => 'essential',
},
],
group_by => [ qw/ category_id quantised_weeks essential value / ],
order_by => { '-desc' => 'value' },
group_by => [ qw/ category_id quantised_weeks essential / ],
}
);
my $data = { categories => {}, essentials => {} };
my $category_list = $c->schema->resultset('Category')->as_hash;
for my $cat_trans ( $month_transaction_category_rs->all ) {
my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised'));
my $days = $c->format_iso_date( $quantised ) || 0;
my $category = $cat_trans->get_column('category_id') || undef;
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} }, {
days => $days,
value => $value,
category => $category,
};
$data->{categories}->{$days}->{$category_list->{$category}} += $value;
next unless $cat_trans->get_column('essential');
$data->{essentials}->{$days}->{value} += $value;
}
for my $day ( keys %{ $data->{categories} } ) {
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @{ $data->{categories}->{$day} } ];
my @days = ( map{ {
days => $day,
value => $data->{categories}->{$day}->{$_},
category => $_,
} } keys %{ $data->{categories}->{$day} } );
$data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ];
}
return $c->render(

View file

@ -243,21 +243,12 @@ sub post_category {
my $c = shift;
my $self = $c;
my $category_rs = $c->schema->resultset('Category');
# for ( $category_rs->all ) {
# push @{ $categories->{ ids } }, $_->get_column('id');
# push @{ $categories->{ names } }, $_->get_column('name');
# }
my %category_list = (
map {
$_->id => $_->name,
} $category_rs->all
);
my $category_list = $c->schema->resultset('Category')->as_hash;
delete $category_list->{0};
return $self->render( json => {
success => Mojo::JSON->true,
categories => \%category_list,
categories => $category_list,
});
}

View file

@ -0,0 +1,22 @@
package Pear::LocalLoop::Schema::ResultSet::Category;
use strict;
use warnings;
use base 'DBIx::Class::ResultSet';
sub as_hash {
my ( $self ) = @_;
my %category_list = (
(
map {
$_->id => $_->name,
} $self->all
),
0 => 'Uncategorised',
);
return \%category_list;
}
1;