resources/commands/expenses/ (4 commands):
- expenses.log "потратил 500 на еду" → append entry
- expenses.today "сколько потратил сегодня" → today's total
- expenses.week "сколько на неделе" → 7-day rolling total
- expenses.breakdown "куда ушли деньги" → top-5 categories
Storage: `jarvis.state` (auto-namespaced per-pack JSON). Wire format is
compact "ts1,amount1,cat1|ts2,amount2,cat2" so we don't need a JSON parser
inside Lua.
Phrase parsing:
- Trigger stripped via jarvis.text.strip_trigger.
- First number captured; "12,50" comma decimals accepted.
- Category extracted from "на <слово>" (RU) or "for/on <word>" (EN);
defaults to "разное" / "misc".
Time math:
- Day boundary: local-calendar midnight via os.time({year,month,day,...}).
- Week boundary: rolling 7×86400 seconds.
Pack count: 98 → 99 functional.
37 lines
1.2 KiB
Lua
37 lines
1.2 KiB
Lua
-- Sum of the last 7 days' expenses (rolling window).
|
||
|
||
local lang = jarvis.context.language
|
||
local raw_state = jarvis.state.get("entries") or ""
|
||
if raw_state == "" then
|
||
return jarvis.cmd.not_found(lang == "ru"
|
||
and "На этой неделе ничего не тратили."
|
||
or "Nothing logged this week.")
|
||
end
|
||
|
||
local now = jarvis.context.time
|
||
local now_ts = os.time({
|
||
year = now.year, month = now.month, day = now.day,
|
||
hour = now.hour, min = now.minute, sec = now.second or 0,
|
||
})
|
||
local cutoff = now_ts - 7 * 86400
|
||
|
||
local total = 0
|
||
local count = 0
|
||
for chunk in raw_state:gmatch("([^|]+)") do
|
||
local ts, am = chunk:match("^(%d+),([%-%d%.]+),")
|
||
ts = tonumber(ts); am = tonumber(am)
|
||
if ts and am and ts >= cutoff then
|
||
total = total + am
|
||
count = count + 1
|
||
end
|
||
end
|
||
|
||
if total == 0 then
|
||
return jarvis.cmd.ok(lang == "ru"
|
||
and "На этой неделе тратами не похваляешься, сэр."
|
||
or "Nothing tracked this week, sir.")
|
||
end
|
||
|
||
return jarvis.cmd.ok(string.format(lang == "ru"
|
||
and "За неделю %d трат на сумму %.0f."
|
||
or "This week: %d expenses totalling %.0f.", count, total))
|