diff --git a/lib/Pear/LocalLoop/Controller/Api/Categories.pm b/lib/Pear/LocalLoop/Controller/Api/Categories.pm index 10b4380..8113053 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Categories.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Categories.pm @@ -14,7 +14,31 @@ sub post_category_list { my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; - my $month_transaction_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( + my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($start), + $dtf->format_datetime($end), + ], + }, + buyer_id => $entity->id, + }, + { + columns => [ + { + quantised => 'quantised_weeks', + value => { sum => 'value' }, + category_id => 'category_id', + essential => 'essential', + } + ], + group_by => [ qw/ category_id quantised_weeks / ], + order_by => { '-desc' => 'value' }, + } + ); + + my $month_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( { purchase_time => { -between => [ @@ -29,27 +53,34 @@ sub post_category_list { { quantised => 'quantised_weeks', value => 'value', - category_id => 'category_id', + essential => 'essential', } ], - group_by => [ qw/ category_id quantised_weeks / ], + group_by => [ qw/ essential quantised_weeks / ], order_by => { '-desc' => 'value' }, } ); - my $data = {}; + my $data = { categories => {}, essentials => {} }; - for ( $month_transaction_rs->all ) { - my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised')); + for my $cat_trans ( $month_transaction_category_rs->all ) { + use Devel::Dwarn; Dwarn {$cat_trans->get_columns}; + my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); my $days = $c->format_iso_date( $quantised ) || 0; - my $category = $_->get_column('category_id') || 0; - my $value = ($_->get_column('value') || 0) / 100000; - $data->{$days} = [] unless exists $data->{$days}; - push @{ $data->{$days} }, { + 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, }; + 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} } ]; } return $c->render( diff --git a/lib/Pear/LocalLoop/Schema/Result/Transaction.pm b/lib/Pear/LocalLoop/Schema/Result/Transaction.pm index 538b03a..14405e6 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Transaction.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Transaction.pm @@ -81,4 +81,14 @@ __PACKAGE__->might_have( "Pear::LocalLoop::Schema::Result::TransactionCategory" => "transaction_id", ); +sub sqlt_deploy_hook { + my ( $source_instance, $sqlt_table ) = @_; + my $pending_field = $sqlt_table->get_field('essential'); + if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $pending_field->{default_value} = 0; + } else { + $pending_field->{default_value} = \"false"; + } +} + 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategoryPg.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategoryPg.pm index 2ae0445..b288c65 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategoryPg.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategoryPg.pm @@ -15,6 +15,7 @@ SELECT "transactions.value", "transactions.purchase_time", "transactions.buyer_id", "transactions.seller_id", + "transactions.essential", "transaction_category.category_id", DATE_TRUNC('hour', "transactions.purchase_time") AS "quantised_hours", DATE_TRUNC('day', "transactions.purchase_time") AS "quantised_days", diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm index fcef5a2..7992458 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionCategorySQLite.pm @@ -15,6 +15,7 @@ SELECT "transactions"."value", "transactions"."purchase_time", "transactions"."buyer_id", "transactions"."seller_id", + "transactions"."essential", "transaction_category"."category_id", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions"."purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time")) AS "quantised_days", diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm index 91ceede..d9b0f05 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm @@ -15,6 +15,7 @@ SELECT "value", "purchase_time", "buyer_id", "seller_id", + "essential", DATE_TRUNC('hour', "purchase_time") AS "quantised_hours", DATE_TRUNC('day', "purchase_time") AS "quantised_days", DATE_TRUNC('week', "purchase_time") AS "quantised_weeks" diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm index abf95dd..a84c461 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm @@ -15,6 +15,7 @@ SELECT "value", "purchase_time", "buyer_id", "seller_id", + "essential", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time", 'weekday 1')) AS "quantised_weeks"