{
  "$schema": "./.schemas/page.schema.json",
  "title": "Budget",
  "icon": "chart.pie",
  "period": {
    "step": true,
    "choices": [
      {
        "id": "month",
        "label": "Month",
        "span": "month"
      }
    ]
  },
  "sources": {
    "month": {
      "url": "https://api.lunchmoney.dev",
      "secret": "lunch-money",
      "every": "1h",
      "requests": {
        "summary": "/v2/summary?start_date={period.start}&end_date={period.end}&include_totals=true&include_occurrences=true",
        "tx": "/v2/transactions?start_date={period.start}&end_date={period.end}&include_pending=true&limit=1000",
        "cats": "/v2/categories?format=flattened",
        "settings": "/v2/me/account/settings",
        "me": "/v2/me"
      },
      "derive": [
        {
          "name": "category_spend",
          "value": {
            "each": "summary.categories",
            "lookup": {
              "from": "cats.categories",
              "by": "id",
              "on": "category_id",
              "as": "category"
            },
            "fields": {
              "id": "category_id",
              "name": {
                "path": "category.name",
                "default": "Other"
              },
              "group_id": {
                "path": "category.group_id",
                "default": null
              },
              "is_group": {
                "path": "category.is_group",
                "default": false
              },
              "is_income": {
                "path": "category.is_income",
                "default": false
              },
              "exclude_budget": {
                "path": "category.exclude_from_budget",
                "default": false
              },
              "exclude_totals": {
                "path": "category.exclude_from_totals",
                "default": false
              },
              "budgeted": {
                "path": "totals.budgeted",
                "default": null
              },
              "spent": {
                "add": [
                  "totals.other_activity",
                  "totals.recurring_activity"
                ]
              }
            }
          }
        },
        {
          "name": "children",
          "value": {
            "each": "category_spend",
            "group": "group_id",
            "fields": {
              "id": "key",
              "budget_count": {
                "count": "rows",
                "where": {
                  "path": "budgeted",
                  "gte": 0
                }
              }
            }
          }
        },
        {
          "name": "parents",
          "value": {
            "each": "category_spend",
            "lookup": {
              "from": "children",
              "by": "id",
              "on": "id",
              "as": "children"
            },
            "fields": {
              "id": "id",
              "owns": {
                "if": {
                  "all": [
                    {
                      "path": "is_group",
                      "equals": true
                    },
                    {
                      "path": "budgeted",
                      "gte": 0
                    },
                    {
                      "value": {
                        "path": "children.budget_count",
                        "default": 0
                      },
                      "equals": 0
                    }
                  ]
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              }
            }
          }
        },
        {
          "name": "owned_categories",
          "value": {
            "each": "category_spend",
            "lookup": {
              "from": "parents",
              "by": "id",
              "on": "group_id",
              "as": "parent"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "group_id": "group_id",
              "is_group": "is_group",
              "is_income": "is_income",
              "exclude_budget": "exclude_budget",
              "exclude_totals": "exclude_totals",
              "budgeted": {
                "path": "budgeted",
                "default": null
              },
              "spent": "spent",
              "parent_owner": {
                "path": "parent.owns",
                "default": "no"
              }
            }
          }
        },
        {
          "name": "categories",
          "value": {
            "each": "owned_categories",
            "lookup": {
              "from": "parents",
              "by": "id",
              "on": "id",
              "as": "self"
            },
            "where": {
              "all": [
                {
                  "path": "is_income",
                  "not": true
                },
                {
                  "path": "exclude_budget",
                  "not": true
                },
                {
                  "any": [
                    {
                      "all": [
                        {
                          "path": "is_group",
                          "equals": true
                        },
                        {
                          "path": "self.owns",
                          "equals": "yes"
                        }
                      ]
                    },
                    {
                      "all": [
                        {
                          "path": "is_group",
                          "not": true
                        },
                        {
                          "path": "parent_owner",
                          "not": "yes"
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            "fields": {
              "id": "id",
              "name": "name",
              "group_id": "group_id",
              "is_group": "is_group",
              "is_income": "is_income",
              "exclude_budget": "exclude_budget",
              "exclude_totals": "exclude_totals",
              "budgeted": {
                "path": "budgeted",
                "default": null
              },
              "spent": "spent",
              "used": {
                "divide": [
                  "spent",
                  "budgeted"
                ],
                "default": 0
              }
            }
          }
        },
        {
          "name": "budget_spent",
          "value": {
            "sum": "spent",
            "of": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "gt": 0
                },
                {
                  "path": "exclude_totals",
                  "not": true
                }
              ]
            }
          }
        },
        {
          "name": "budget_total",
          "value": {
            "sum": "budgeted",
            "of": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "gt": 0
                },
                {
                  "path": "exclude_totals",
                  "not": true
                }
              ]
            }
          }
        },
        {
          "name": "category_count",
          "value": {
            "count": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "gt": 0
                },
                {
                  "path": "exclude_totals",
                  "not": true
                }
              ]
            }
          }
        },
        {
          "name": "period_start",
          "value": {
            "first": "start_date",
            "of": "summary.categories",
            "flatten": "occurrences",
            "sort": "start_date"
          }
        },
        {
          "name": "period_end",
          "value": {
            "first": "end_date",
            "of": "summary.categories",
            "flatten": "occurrences",
            "sort": {
              "by": "end_date",
              "order": "desc"
            }
          }
        },
        {
          "name": "transactions_url",
          "value": {
            "if": {
              "all": [
                {
                  "path": "period_start",
                  "exists": true
                },
                {
                  "path": "period_end",
                  "exists": true
                }
              ]
            },
            "then": {
              "template": "https://my.lunchmoney.app/transactions?time=custom&start_date={period_start}&end_date={period_end}&include_pending=true"
            },
            "else": {
              "template": "https://my.lunchmoney.app/transactions"
            }
          }
        },
        {
          "name": "transaction_rows",
          "value": {
            "each": "tx.transactions",
            "lookup": {
              "from": "cats.categories",
              "by": "id",
              "on": "category_id",
              "as": "category"
            },
            "where": {
              "all": [
                {
                  "path": "status",
                  "not": "delete_pending"
                },
                {
                  "path": "is_split_parent",
                  "not": true
                },
                {
                  "value": {
                    "path": "group_parent_id",
                    "default": 0
                  },
                  "in": [
                    0,
                    null
                  ]
                }
              ]
            },
            "fields": {
              "id": {
                "path": "id",
                "default": null
              },
              "category_id": {
                "path": "category_id",
                "default": null
              },
              "payee": {
                "path": "payee",
                "default": null
              },
              "original_name": {
                "path": "original_name",
                "default": null
              },
              "date": {
                "path": "date",
                "default": null
              },
              "to_base": {
                "path": "to_base",
                "default": null
              },
              "is_pending": {
                "path": "is_pending",
                "default": null
              },
              "status": {
                "path": "status",
                "default": null
              },
              "group_id": {
                "path": "category.group_id",
                "default": null
              }
            }
          }
        },
        {
          "name": "transactions",
          "value": {
            "if": {
              "path": "settings.include_pending_in_totals",
              "equals": true
            },
            "then": {
              "path": "transaction_rows"
            },
            "else": {
              "each": "transaction_rows",
              "where": {
                "path": "is_pending",
                "not": true
              },
              "fields": {
                "id": "id",
                "category_id": "category_id",
                "payee": "payee",
                "original_name": "original_name",
                "date": "date",
                "to_base": "to_base",
                "is_pending": "is_pending",
                "status": "status",
                "group_id": "group_id"
              }
            }
          }
        },
        {
          "name": "transaction_count",
          "value": {
            "count": "transactions"
          }
        },
        {
          "name": "transaction_details",
          "value": {
            "each": "transactions",
            "lookup": {
              "from": "parents",
              "by": "id",
              "on": "group_id",
              "as": "parent"
            },
            "sort": {
              "by": "date",
              "order": "desc"
            },
            "limit": 500,
            "fields": {
              "key": {
                "if": {
                  "path": "parent.owns",
                  "equals": "yes"
                },
                "then": {
                  "path": "group_id"
                },
                "else": {
                  "path": "category_id"
                }
              },
              "text": {
                "path": "payee",
                "default": {
                  "template": "{original_name}"
                }
              },
              "detail": {
                "if": {
                  "path": "is_pending",
                  "equals": true
                },
                "then": {
                  "template": "${to_base|round:2} · {date|date} · Pending"
                },
                "else": {
                  "template": "${to_base|round:2} · {date|date}"
                }
              },
              "url": {
                "template": "https://my.lunchmoney.app/transactions?transaction_id={id|raw}"
              }
            }
          }
        },
        {
          "name": "spend_rows",
          "value": {
            "each": "summary.categories",
            "lookup": {
              "from": "cats.categories",
              "by": "id",
              "on": "category_id",
              "as": "category"
            },
            "where": {
              "all": [
                {
                  "path": "category.is_income",
                  "not": true
                },
                {
                  "path": "category.exclude_from_totals",
                  "not": true
                },
                {
                  "path": "category.is_group",
                  "not": true
                }
              ]
            },
            "fields": {
              "name": {
                "path": "category.name",
                "default": "Uncategorized"
              },
              "recurring": {
                "path": "totals.recurring_activity",
                "default": 0
              },
              "everyday": {
                "path": "totals.other_activity",
                "default": 0
              },
              "total": {
                "add": [
                  {
                    "path": "totals.recurring_activity",
                    "default": 0
                  },
                  {
                    "path": "totals.other_activity",
                    "default": 0
                  }
                ]
              }
            }
          }
        },
        {
          "name": "spend_top",
          "value": {
            "each": "spend_rows",
            "where": {
              "path": "total",
              "gt": 0
            },
            "sort": {
              "by": "total",
              "order": "desc"
            },
            "limit": 7,
            "fields": {
              "name": "name"
            }
          }
        },
        {
          "name": "spend_dest",
          "value": {
            "each": "spend_rows",
            "where": {
              "path": "total",
              "gt": 0
            },
            "lookup": {
              "from": "spend_top",
              "by": "name",
              "on": "name",
              "as": "top"
            },
            "fields": {
              "dest": {
                "if": {
                  "value": {
                    "path": "top.name",
                    "default": ""
                  },
                  "equals": ""
                },
                "then": {
                  "template": "Other"
                },
                "else": {
                  "path": "name"
                }
              },
              "recurring": "recurring",
              "everyday": "everyday"
            }
          }
        },
        {
          "name": "spend_by",
          "value": {
            "each": "spend_dest",
            "group": "dest",
            "fields": {
              "label": "key",
              "recurring": {
                "sum": "recurring",
                "of": "rows"
              },
              "everyday": {
                "sum": "everyday",
                "of": "rows"
              },
              "total": {
                "add": [
                  {
                    "sum": "recurring",
                    "of": "rows"
                  },
                  {
                    "sum": "everyday",
                    "of": "rows"
                  }
                ]
              }
            }
          }
        },
        {
          "name": "links_recurring",
          "value": {
            "each": "spend_by",
            "where": {
              "value": {
                "path": "recurring",
                "round": 0
              },
              "gt": 0
            },
            "fields": {
              "from": {
                "template": "Recurring"
              },
              "to": "label",
              "value": "recurring",
              "rank": {
                "if": {
                  "path": "label",
                  "equals": "Other"
                },
                "then": {
                  "multiply": [
                    "total",
                    -1
                  ]
                },
                "else": {
                  "path": "total"
                }
              }
            }
          }
        },
        {
          "name": "links_everyday",
          "value": {
            "each": "spend_by",
            "where": {
              "value": {
                "path": "everyday",
                "round": 0
              },
              "gt": 0
            },
            "fields": {
              "from": {
                "template": "Everyday"
              },
              "to": "label",
              "value": "everyday",
              "rank": {
                "if": {
                  "path": "label",
                  "equals": "Other"
                },
                "then": {
                  "multiply": [
                    "total",
                    -1
                  ]
                },
                "else": {
                  "path": "total"
                }
              }
            }
          }
        },
        {
          "name": "spend_link_count",
          "value": {
            "add": [
              {
                "count": "links_recurring"
              },
              {
                "count": "links_everyday"
              }
            ]
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "views",
      "type": "tabs",
      "tabs": [
        {
          "label": "Budget",
          "cards": [
            {
              "id": "budgets",
              "type": "pacing",
              "label": "Budgeted spending",
              "unit": "USD",
              "items": [
                {
                  "label": "Category",
                  "value": 0,
                  "target": 1
                }
              ],
              "source": {
                "ref": "month",
                "map": {
                  "items": {
                    "each": "categories",
                    "where": {
                      "all": [
                        {
                          "path": "budgeted",
                          "gt": 0
                        },
                        {
                          "path": "exclude_totals",
                          "not": true
                        }
                      ]
                    },
                    "sort": {
                      "by": "used",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "name",
                        "default": "Other"
                      },
                      "value": "spent",
                      "target": "budgeted",
                      "key": "id",
                      "icon": {
                        "path": "name",
                        "values": {
                          "Groceries": "cart",
                          "Restaurants": "fork.knife",
                          "Coffee Shops": "cup.and.saucer",
                          "Utilities": "bolt",
                          "Transportation": "car",
                          "Gas": "fuelpump",
                          "Clothing": "tshirt",
                          "Shopping": "bag",
                          "Pets": "pawprint",
                          "Insurance": "shield",
                          "Entertainment": "tv",
                          "Subscriptions": "repeat",
                          "Education": "graduationcap",
                          "Gifts": "gift",
                          "Healthcare": "cross.case",
                          "Medical": "cross.case",
                          "Home Improvement": "hammer",
                          "Home & Living": "sofa",
                          "Rent": "house",
                          "Mortgage": "house",
                          "Personal Care": "comb",
                          "Shipping & Postage": "shippingbox",
                          "Tools & Workshop": "wrench.and.screwdriver",
                          "Software & Online Services": "cloud",
                          "Travel": "airplane",
                          "Fitness": "figure.run",
                          "Business": "briefcase",
                          "Electronics": "desktopcomputer",
                          "Taxes": "percent",
                          "Charity": "heart"
                        },
                        "default": "tag"
                      }
                    }
                  },
                  "value": "budget_spent",
                  "target": "budget_total",
                  "details": "transaction_details",
                  "unit": "me.primary_currency"
                },
                "when": {
                  "path": "summary.aligned",
                  "equals": true
                }
              },
              "hide_caption": true
            }
          ]
        },
        {
          "label": "Spending",
          "cards": [
            {
              "id": "spending",
              "type": "flow",
              "label": "By category",
              "unit": "USD",
              "from": [
                {
                  "label": "Recurring",
                  "color": "purple"
                },
                {
                  "label": "Everyday",
                  "color": "blue"
                }
              ],
              "links": [
                {
                  "from": "Everyday",
                  "to": "Groceries",
                  "value": 0
                }
              ],
              "source": {
                "ref": "month",
                "map": {
                  "links": {
                    "each": [
                      "links_recurring",
                      "links_everyday"
                    ],
                    "sort": {
                      "by": "rank",
                      "order": "desc"
                    },
                    "fields": {
                      "from": "from",
                      "to": "to",
                      "value": {
                        "path": "value",
                        "round": 0
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                },
                "empty": {
                  "when": {
                    "path": "spend_link_count",
                    "equals": 0
                  },
                  "text": "No spending this period."
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "zero-limits",
      "type": "list",
      "label": "Zero limits",
      "items": [],
      "source": {
        "ref": "month",
        "map": {
          "items": {
            "each": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "equals": 0
                },
                {
                  "path": "spent",
                  "not": 0
                }
              ]
            },
            "sort": {
              "by": "spent",
              "order": "desc"
            },
            "limit": 20,
            "fields": {
              "text": "name",
              "detail": {
                "template": "${spent|round:2}"
              },
              "icon": {
                "path": "name",
                "values": {
                  "Groceries": "cart",
                  "Restaurants": "fork.knife",
                  "Coffee Shops": "cup.and.saucer",
                  "Utilities": "bolt",
                  "Transportation": "car",
                  "Gas": "fuelpump",
                  "Clothing": "tshirt",
                  "Shopping": "bag",
                  "Pets": "pawprint",
                  "Insurance": "shield",
                  "Entertainment": "tv",
                  "Subscriptions": "repeat",
                  "Education": "graduationcap",
                  "Gifts": "gift",
                  "Healthcare": "cross.case",
                  "Medical": "cross.case",
                  "Home Improvement": "hammer",
                  "Home & Living": "sofa",
                  "Rent": "house",
                  "Mortgage": "house",
                  "Personal Care": "comb",
                  "Shipping & Postage": "shippingbox",
                  "Tools & Workshop": "wrench.and.screwdriver",
                  "Software & Online Services": "cloud",
                  "Travel": "airplane",
                  "Fitness": "figure.run",
                  "Business": "briefcase",
                  "Electronics": "desktopcomputer",
                  "Taxes": "percent",
                  "Charity": "heart"
                },
                "default": "tag"
              }
            }
          },
          "label": {
            "if": {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "equals": 0
                    },
                    {
                      "path": "spent",
                      "not": 0
                    }
                  ]
                }
              },
              "gt": 20
            },
            "then": {
              "template": "Zero limits · first 20"
            },
            "else": {
              "template": "Zero limits"
            }
          }
        },
        "when": {
          "all": [
            {
              "path": "summary.aligned",
              "equals": true
            },
            {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "equals": 0
                    },
                    {
                      "path": "spent",
                      "not": 0
                    }
                  ]
                }
              },
              "gt": 0
            }
          ]
        }
      }
    },
    {
      "id": "separate-limits",
      "type": "list",
      "label": "Separate limits",
      "items": [],
      "source": {
        "ref": "month",
        "map": {
          "items": {
            "each": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "gt": 0
                },
                {
                  "path": "exclude_totals",
                  "equals": true
                }
              ]
            },
            "sort": {
              "by": "spent",
              "order": "desc"
            },
            "limit": 20,
            "fields": {
              "text": "name",
              "detail": {
                "template": "${spent|round:2} of ${budgeted|round:2}"
              },
              "icon": {
                "path": "name",
                "values": {
                  "Groceries": "cart",
                  "Restaurants": "fork.knife",
                  "Coffee Shops": "cup.and.saucer",
                  "Utilities": "bolt",
                  "Transportation": "car",
                  "Gas": "fuelpump",
                  "Clothing": "tshirt",
                  "Shopping": "bag",
                  "Pets": "pawprint",
                  "Insurance": "shield",
                  "Entertainment": "tv",
                  "Subscriptions": "repeat",
                  "Education": "graduationcap",
                  "Gifts": "gift",
                  "Healthcare": "cross.case",
                  "Medical": "cross.case",
                  "Home Improvement": "hammer",
                  "Home & Living": "sofa",
                  "Rent": "house",
                  "Mortgage": "house",
                  "Personal Care": "comb",
                  "Shipping & Postage": "shippingbox",
                  "Tools & Workshop": "wrench.and.screwdriver",
                  "Software & Online Services": "cloud",
                  "Travel": "airplane",
                  "Fitness": "figure.run",
                  "Business": "briefcase",
                  "Electronics": "desktopcomputer",
                  "Taxes": "percent",
                  "Charity": "heart"
                },
                "default": "tag"
              }
            }
          },
          "label": {
            "if": {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "gt": 0
                    },
                    {
                      "path": "exclude_totals",
                      "equals": true
                    }
                  ]
                }
              },
              "gt": 20
            },
            "then": {
              "template": "Separate limits · first 20"
            },
            "else": {
              "template": "Separate limits"
            }
          }
        },
        "when": {
          "all": [
            {
              "path": "summary.aligned",
              "equals": true
            },
            {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "gt": 0
                    },
                    {
                      "path": "exclude_totals",
                      "equals": true
                    }
                  ]
                }
              },
              "gt": 0
            }
          ]
        }
      }
    },
    {
      "id": "unbudgeted",
      "type": "list",
      "label": "Without a budget",
      "items": [],
      "source": {
        "ref": "month",
        "map": {
          "items": {
            "each": "categories",
            "where": {
              "all": [
                {
                  "path": "budgeted",
                  "equals": null
                },
                {
                  "path": "spent",
                  "not": 0
                },
                {
                  "path": "exclude_totals",
                  "not": true
                }
              ]
            },
            "sort": {
              "by": "spent",
              "order": "desc"
            },
            "limit": 20,
            "fields": {
              "text": "name",
              "detail": {
                "template": "${spent|round:2}"
              },
              "icon": {
                "path": "name",
                "values": {
                  "Groceries": "cart",
                  "Restaurants": "fork.knife",
                  "Coffee Shops": "cup.and.saucer",
                  "Utilities": "bolt",
                  "Transportation": "car",
                  "Gas": "fuelpump",
                  "Clothing": "tshirt",
                  "Shopping": "bag",
                  "Pets": "pawprint",
                  "Insurance": "shield",
                  "Entertainment": "tv",
                  "Subscriptions": "repeat",
                  "Education": "graduationcap",
                  "Gifts": "gift",
                  "Healthcare": "cross.case",
                  "Medical": "cross.case",
                  "Home Improvement": "hammer",
                  "Home & Living": "sofa",
                  "Rent": "house",
                  "Mortgage": "house",
                  "Personal Care": "comb",
                  "Shipping & Postage": "shippingbox",
                  "Tools & Workshop": "wrench.and.screwdriver",
                  "Software & Online Services": "cloud",
                  "Travel": "airplane",
                  "Fitness": "figure.run",
                  "Business": "briefcase",
                  "Electronics": "desktopcomputer",
                  "Taxes": "percent",
                  "Charity": "heart"
                },
                "default": "tag"
              }
            }
          },
          "label": {
            "if": {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "equals": null
                    },
                    {
                      "path": "spent",
                      "not": 0
                    },
                    {
                      "path": "exclude_totals",
                      "not": true
                    }
                  ]
                }
              },
              "gt": 20
            },
            "then": {
              "template": "Without a budget · first 20"
            },
            "else": {
              "template": "Without a budget"
            }
          }
        },
        "when": {
          "all": [
            {
              "path": "summary.aligned",
              "equals": true
            },
            {
              "value": {
                "count": "categories",
                "where": {
                  "all": [
                    {
                      "path": "budgeted",
                      "equals": null
                    },
                    {
                      "path": "spent",
                      "not": 0
                    },
                    {
                      "path": "exclude_totals",
                      "not": true
                    }
                  ]
                }
              },
              "gt": 0
            }
          ]
        }
      }
    }
  ]
}
