Also load available last month

This commit is contained in:
Jan Bader 2021-12-08 15:15:47 +00:00
parent 7cb7527704
commit 11179a1593
2 changed files with 34 additions and 37 deletions

View File

@ -165,12 +165,13 @@ type GetCategoriesWithBalanceParams struct {
} }
type GetCategoriesWithBalanceRow struct { type GetCategoriesWithBalanceRow struct {
ID uuid.UUID ID uuid.UUID
Name string Name string
Group string Group string
Available Numeric Available Numeric
Activity Numeric AvailableLastMonth Numeric
Assigned Numeric Activity Numeric
Assigned Numeric
} }
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) { func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
@ -187,6 +188,7 @@ func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategorie
&i.Name, &i.Name,
&i.Group, &i.Group,
&i.Available, &i.Available,
&i.AvailableLastMonth,
&i.Activity, &i.Activity,
&i.Assigned, &i.Assigned,
); err != nil { ); err != nil {

View File

@ -21,37 +21,32 @@ WHERE category_groups.budget_id = $1;
-- name: GetCategoriesWithBalance :many -- name: GetCategoriesWithBalance :many
SELECT categories.id, categories.name, category_groups.name as group, SELECT categories.id, categories.name, category_groups.name as group,
(COALESCE( (
( COALESCE((
SELECT SUM(a_hist.amount) SELECT SUM(a_hist.amount) FROM assignments a_hist
FROM assignments a_hist WHERE categories.id = a_hist.category_id AND a_hist.date < @to_date
WHERE categories.id = a_hist.category_id ), 0)+COALESCE((
AND a_hist.date < @to_date SELECT SUM(t_hist.amount) FROM transactions t_hist
) WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date
, 0)+COALESCE( ), 0)
( )::decimal(12,2) as available,
SELECT SUM(t_hist.amount) (
FROM transactions t_hist COALESCE((
WHERE categories.id = t_hist.category_id SELECT SUM(a_hist.amount) FROM assignments a_hist
AND t_hist.date < @to_date WHERE categories.id = a_hist.category_id AND a_hist.date < @from_date
) ), 0)+COALESCE((
, 0))::decimal(12,2) as available, SELECT SUM(t_hist.amount) FROM transactions t_hist
COALESCE( WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date
( ), 0)
SELECT SUM(t_this.amount) )::decimal(12,2) as available_last_month,
FROM transactions t_this COALESCE((
WHERE categories.id = t_this.category_id SELECT SUM(t_this.amount) FROM transactions t_this
AND t_this.date >= @from_date AND t_this.date < @to_date WHERE categories.id = t_this.category_id AND t_this.date >= @from_date AND t_this.date < @to_date
) ), 0)::decimal(12,2) as activity,
, 0)::decimal(12,2) as activity, COALESCE((
COALESCE( SELECT SUM(a_hist.amount) FROM assignments a_hist
( WHERE categories.id = a_hist.category_id AND a_hist.date >= @from_date AND a_hist.date < @to_date
SELECT SUM(a_hist.amount) ), 0)::decimal(12,2) as assigned
FROM assignments a_hist
WHERE categories.id = a_hist.category_id
AND a_hist.date >= @from_date AND a_hist.date < @to_date
)
,0)::decimal(12,2) as assigned
FROM categories FROM categories
INNER JOIN category_groups ON categories.category_group_id = category_groups.id INNER JOIN category_groups ON categories.category_group_id = category_groups.id