Amended category listing

This commit is contained in:
Finn 2018-03-26 14:42:18 +01:00
parent 8a12442eac
commit 3a738a1000
3 changed files with 36 additions and 22 deletions

View file

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

View file

@ -243,21 +243,12 @@ sub post_category {
my $c = shift; my $c = shift;
my $self = $c; my $self = $c;
my $category_rs = $c->schema->resultset('Category'); my $category_list = $c->schema->resultset('Category')->as_hash;
delete $category_list->{0};
# 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
);
return $self->render( json => { return $self->render( json => {
success => Mojo::JSON->true, 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;