ledger.lisp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. (in-package :cl-user)
  2. (defpackage chatikbot.plugins.ledger
  3. (:use :cl :chatikbot.common))
  4. (in-package :chatikbot.plugins.ledger)
  5. (eval-when (:compile-toplevel :load-toplevel :execute)
  6. (ql:quickload '(:pta-ledger :legit)))
  7. (defvar *ledger/chat-journals* (make-hash-table))
  8. (defvar *git-repo-locks* (make-hash-table :test #'equal))
  9. (defun git-get-repo-lock (repo)
  10. (let ((key (legit:location repo)))
  11. (setf key
  12. (etypecase key
  13. (string key)
  14. (pathname (namestring key))))
  15. (or (gethash key *git-repo-locks*)
  16. (setf (gethash key *git-repo-locks*)
  17. (bt:make-recursive-lock key)))))
  18. (defun git-get-repo (location remote)
  19. (let ((repo (make-instance 'legit:repository :location location)))
  20. (bt:with-recursive-lock-held ((git-get-repo-lock repo))
  21. (legit:init repo :remote remote :if-does-not-exist :clone))
  22. repo))
  23. (defsetting *git-repos-root* "/tmp/ledger-repos/")
  24. (defun git-get-chat-location (chat-id remote)
  25. (namestring (uiop:ensure-directory-pathname
  26. (merge-pathnames (format nil "~A-~A" chat-id (token-hmac remote))
  27. *git-repos-root*))))
  28. (defun git-read-latest-file (repo path)
  29. (bt:with-recursive-lock-held ((git-get-repo-lock repo))
  30. (legit:fetch repo :branch "master" :remote "origin")
  31. (legit:reset repo :hard t :to "origin/master")
  32. (uiop:read-file-string (merge-pathnames path (legit:location repo)))))
  33. (defun ledger/refresh-git (chat-id remote path)
  34. (let* ((location (git-get-chat-location chat-id remote))
  35. (repo (git-get-repo location remote))
  36. (content (git-read-latest-file repo path))
  37. (journal (pta-ledger:parse-journal content))
  38. (updated (legit:current-age repo)))
  39. (setf (gethash chat-id *ledger/chat-journals*)
  40. (cons journal updated))))
  41. (defun git-append-latest-file (repo path text message)
  42. (bt:with-recursive-lock-held ((git-get-repo-lock repo))
  43. (let ((repo-path (merge-pathnames path (legit:location repo))))
  44. (dotimes (tries 5)
  45. (let ((current (or (ignore-errors (git-read-latest-file repo path)) "")))
  46. (uiop/stream:with-output-file (s repo-path
  47. :if-exists :supersede
  48. :if-does-not-exist :create)
  49. (format s "~A~A" current text)))
  50. (legit:add repo (namestring repo-path))
  51. (legit:commit repo message)
  52. (handler-case
  53. (progn
  54. (legit:push repo)
  55. (return-from git-append-latest-file path))
  56. (legit:git-error ())))
  57. (error "Tried 5 times to push ~A to ~A" path (legit:remote-url repo)))))
  58. (defun ledger/add-git (chat-id remote path text message)
  59. (let* ((location (git-get-chat-location chat-id remote))
  60. (repo (git-get-repo location remote)))
  61. (git-append-latest-file repo path
  62. (format nil "~%~A~%" text)
  63. message)))
  64. (defun ledger/get-hook-url (chat-id)
  65. (get-webhook-url "ledger" chat-id (token-hmac (write-to-string chat-id))))
  66. (defun ledger/format-uri (url)
  67. (let ((uri (quri:uri url)))
  68. (quri:render-uri (quri:make-uri :userinfo (when (quri:uri-userinfo uri) "*:*")
  69. :defaults url))))
  70. (defun ledger/format-time (universal-time)
  71. (when universal-time
  72. (multiple-value-bind (sec min hour day month year dow dst-p tz)
  73. (decode-universal-time universal-time)
  74. (declare (ignore dow dst-p tz))
  75. (format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D"
  76. year month day hour min sec))))
  77. (defun format-date (ut)
  78. (multiple-value-bind (sec min hour day month year)
  79. (decode-universal-time ut)
  80. (declare (ignore sec min hour))
  81. (format nil "~4,'0D-~2,'0D-~2,'0D"
  82. year month day)))
  83. (defun get-year (ut)
  84. (multiple-value-bind (sec min hour day month year)
  85. (decode-universal-time ut)
  86. (declare (ignore sec min hour day month))
  87. year))
  88. (defun ledger/refresh-uri (chat-id uri)
  89. (setf (gethash chat-id *ledger/chat-journals*)
  90. (cons (pta-ledger:parse-journal (http-request uri))
  91. (get-universal-time))))
  92. (defun get-chat-journal-info (chat-id &optional info)
  93. (labels ((process-info (info)
  94. (cond
  95. ((stringp info) (ledger/refresh-uri chat-id info))
  96. ((consp info) (apply #'ledger/refresh-git chat-id info))
  97. (:otherwise nil))))
  98. (let ((journal-info (gethash chat-id *ledger/chat-journals*)))
  99. (if journal-info journal-info
  100. (if info (process-info info)
  101. (with-secret (info (list :ledger chat-id))
  102. (process-info info)))))))
  103. (Defun ledger/handle-info (chat-id)
  104. (with-secret (info (list :ledger chat-id))
  105. (if info
  106. (destructuring-bind (journal . ut)
  107. (get-chat-journal-info chat-id info)
  108. (let ((uri (cond
  109. ((consp info) (car info))
  110. ((stringp info) info))))
  111. (bot-send-message chat-id (format nil "Журнал с ~D записями, из ~A, обновлён ~A.~%Веб-хук: ~A"
  112. (length journal)
  113. (ledger/format-uri uri)
  114. (ledger/format-time ut)
  115. (ledger/get-hook-url chat-id))
  116. :disable-web-preview t)))
  117. (bot-send-message chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>"))))
  118. (defun ledger/handle-set-info (chat-id info)
  119. (setf (gethash chat-id *ledger/chat-journals*) nil)
  120. (handler-case
  121. (destructuring-bind (journal . ut)
  122. (get-chat-journal-info chat-id info)
  123. (declare (ignore ut))
  124. (secret-set (list :ledger chat-id) info)
  125. (bot-send-message chat-id (format nil "Добавил журнал с ~D записями. Веб-хук для обновления: ~A"
  126. (length journal)
  127. (ledger/get-hook-url chat-id))))
  128. (pta-ledger:journal-failed (e)
  129. (bot-send-message chat-id (format nil "Не смог спарсить: ~A" e)))
  130. (dex:http-request-failed (e)
  131. (bot-send-message chat-id (format nil "Не смог в урл: ~A" (dex:response-body e))))))
  132. (def-message-cmd-handler handler-ledger (:ledger)
  133. (cond
  134. ((<= 1 (length args) 2) (ledger/handle-set-info chat-id args))
  135. (:otherwise (ledger/handle-info chat-id))))
  136. (defmacro with-chat-journal ((chat-id journal updated) &body body)
  137. (let ((info (gensym "info")))
  138. `(let ((,info (get-chat-journal-info ,chat-id)))
  139. (if ,info
  140. (destructuring-bind (,journal . ,updated) ,info
  141. (declare (ignorable ,journal ,updated))
  142. ,@body)
  143. (bot-send-message ,chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>")))))
  144. (defun ledger/handle-balance (chat-id query)
  145. (with-chat-journal (chat-id journal updated)
  146. (bot-send-message chat-id
  147. (text-chunks (split-sequence:split-sequence
  148. #\Newline
  149. (or (pta-ledger:journal-balance journal query)
  150. "Не нашлось"))
  151. :text-sep "
  152. ")
  153. :parse-mode "markdown")))
  154. (def-message-cmd-handler handler-balance (:balance :bal :budget)
  155. (cond
  156. ((null args) (ledger/handle-balance chat-id (if (eql cmd :budget) "budget" "assets")))
  157. (:otherwise (ledger/handle-balance chat-id (spaced args)))))
  158. (defun ledger/handle-journal (chat-id query)
  159. (with-chat-journal (chat-id journal updated)
  160. (let* ((pta-ledger:*posting-length* 40)
  161. (entries (pta-ledger:journal-print journal query))
  162. (len (length entries))
  163. (count (min len 50)))
  164. (bot-send-message chat-id
  165. (if entries
  166. (text-chunks (subseq entries (- len count) len))
  167. "Не нашлось")
  168. :parse-mode "markdown"))))
  169. (def-message-cmd-handler handler-journal (:journal)
  170. (cond
  171. ((null args) (ledger/handle-journal chat-id "date:thisweek"))
  172. (:otherwise (ledger/handle-journal chat-id (spaced args)))))
  173. (def-message-cmd-handler hander-add-report (:add_report)
  174. (bot-send-message chat-id "Введи название"
  175. :reply-markup (telegram-force-reply))
  176. (on-next-message chat-id
  177. (let ((name text))
  178. (bot-send-message chat-id "Введи запрос"
  179. :reply-markup (telegram-force-reply))
  180. (on-next-message chat-id
  181. (let ((query text))
  182. (pta-ledger:parse-query query) ;; Validate query
  183. (bot-send-message chat-id "Введи расписание, ex: (:day-of-week 0 :hour 10), (:day * :hour 10)"
  184. :reply-markup (telegram-force-reply))
  185. (on-next-message chat-id
  186. (let* ((schedule text))
  187. (add-chat-cron :ledger-report chat-id schedule name query)
  188. (bot-send-message chat-id "Добавил"))))))))
  189. (defun run-report (chat-id name query)
  190. (with-chat-journal (chat-id journal updated)
  191. (bot-send-message chat-id
  192. (text-chunks (split-sequence:split-sequence
  193. #\Newline
  194. (format nil "*~A*~%~%~A" name
  195. (pta-ledger:journal-balance journal query)))
  196. :text-sep "
  197. ")
  198. :parse-mode "markdown")))
  199. (def-chat-cron-handler handler-chat-cron (:ledger-report chat-id schedule name query)
  200. (run-report chat-id name query))
  201. (def-message-cmd-handler handler-reports (:reports)
  202. (let ((crons (get-chat-crons :ledger-report chat-id)))
  203. (if crons
  204. (bot-send-message
  205. chat-id (format nil "Отчёты~%~%~{~A) ~A _~A_: ~A~^~%~}"
  206. (loop for (schedule name query) in crons
  207. for idx from 1
  208. append (list idx name schedule query)))
  209. :parse-mode "markdown"
  210. :reply-markup
  211. (get-inline-keyboard
  212. chat-id
  213. (append
  214. (loop for (schedule name query) in crons
  215. for idx from 0
  216. collect
  217. (let ((cron-index idx)
  218. (cron-name name))
  219. (list (inline-button ((format nil "Удалить '~A'" cron-name))
  220. (delete-chat-cron :ledger-report chat-id cron-index)
  221. (telegram-edit-message-reply-markup
  222. nil :chat-id source-chat-id :message-id source-message-id)
  223. (bot-send-message chat-id (format nil "Удалил *~A*" cron-name)
  224. :parse-mode "markdown")))))
  225. (list (list (inline-button ("Отмена")
  226. (telegram-edit-message-reply-markup
  227. nil :chat-id source-chat-id :message-id source-message-id)))))))
  228. (bot-send-message
  229. chat-id "Отчётов пока нет"
  230. :reply-markup (telegram-reply-keyboard-markup (list (list (list :text "/add_report")))
  231. :one-time-keyboard t)))))
  232. (defun match-entry (text journal)
  233. (labels ((two-post (entries)
  234. (remove-if-not #'(lambda (e)
  235. (= 2 (length (pta-ledger:entry-postings e))))
  236. entries)))
  237. (let ((desc (two-post
  238. (pta-ledger:journal-entries journal (format nil "desc:'~A'" text)))))
  239. (if desc (car (last desc))
  240. (let ((comment (two-post
  241. (pta-ledger:journal-entries journal (format nil "comment:'~A'" text)))))
  242. (when comment (car (last comment))))))))
  243. (defvar *expenses-account-root* "expenses" "Expenses accounts root.")
  244. (defun create-entry (chat-id text amount currency)
  245. (with-chat-journal (chat-id journal updated)
  246. (let* ((old (match-entry text journal))
  247. (new (or (when old (pta-ledger:clone-entry old))
  248. (pta-ledger:make-entry
  249. :description text
  250. :postings (list
  251. (pta-ledger:make-posting :account *expenses-account-root*)
  252. (pta-ledger:make-posting)))))
  253. (postings (pta-ledger:entry-postings new)))
  254. (setf (pta-ledger:entry-date new) (get-universal-time)
  255. (pta-ledger:posting-amount (car postings)) (pta-ledger:make-amount
  256. :quantity amount
  257. :commodity currency)
  258. (pta-ledger:posting-amount (cadr postings)) (pta-ledger:make-amount
  259. :quantity (- amount)
  260. :commodity currency)
  261. (pta-ledger:posting-account (cadr postings)) (format nil "assets:Cash:~A" currency))
  262. new)))
  263. (defun find-posting (entry root)
  264. (when (and entry root)
  265. (find root (pta-ledger:entry-postings entry)
  266. :test #'equal
  267. :key #'(lambda (p)
  268. (car (pta-ledger:account-parents
  269. (pta-ledger:posting-account p) :tree :t))))))
  270. (defvar *budget-account-root* "budget" "Budget accounts root. Extend entry with matching budget if set")
  271. (defvar *budget-search-date-query* "date:last30" "Date part of matching budget search")
  272. (defun extend-entry! (chat-id entry)
  273. (let ((expense (find-posting entry *expenses-account-root*))
  274. (budget (find-posting entry *budget-account-root*)))
  275. (when (and *budget-account-root* expense (or (null budget)
  276. (equal #\! (pta-ledger:posting-status budget))))
  277. (with-chat-journal (chat-id journal updated)
  278. (let* ((last-entries (pta-ledger:journal-entries journal (format nil "~A acct:^~A"
  279. *budget-search-date-query*
  280. (pta-ledger:posting-account expense))))
  281. (last-budget (some #'(lambda (e) (find-posting e *budget-account-root*))
  282. (reverse last-entries)))
  283. (expense-amount (car (pta-ledger:get-amounts expense (pta-ledger:entry-postings entry) :t))))
  284. (when last-budget
  285. (unless budget
  286. (setf budget (pta-ledger:make-posting :status #\! :virtual #\())
  287. (setf (pta-ledger:entry-postings entry)
  288. (append (pta-ledger:entry-postings entry)
  289. (list budget))))
  290. (setf (pta-ledger:posting-amount budget) (pta-ledger:make-amount
  291. :quantity (- (pta-ledger:amount-quantity expense-amount))
  292. :commodity (pta-ledger:amount-commodity expense-amount))
  293. (pta-ledger:posting-account budget) (pta-ledger:posting-account last-budget))))))
  294. entry))
  295. (def-message-cmd-handler handler-create (:rub :usd :eur :thb :btc)
  296. (cond
  297. ((>= (length args) 2)
  298. (ledger/new-chat-entry chat-id (create-entry chat-id
  299. (spaced (subseq args 1))
  300. (parse-float (car args))
  301. (symbol-name cmd))))
  302. (:otherwise (bot-send-message chat-id (format nil "/~A <amount> <description>" cmd)))))
  303. (def-webhook-handler ledger/handle-webhook ("ledger")
  304. (when (= 2 (length paths))
  305. (destructuring-bind (chat-id hmac) paths
  306. (let ((true-hmac (token-hmac chat-id)))
  307. (when (string= true-hmac hmac)
  308. (with-secret (info (list :ledger chat-id))
  309. (when info
  310. (let ((chat-id (parse-integer chat-id)))
  311. (log:info "Updating ledger for ~A" chat-id)
  312. (setf (gethash chat-id *ledger/chat-journals*) nil)
  313. (get-chat-journal-info chat-id info)
  314. "OK"))))))))
  315. ;; New entries
  316. (defun format-entry (entry)
  317. (let ((pta-ledger:*posting-length* 40))
  318. (format nil "```~%~A```" (pta-ledger:render entry))))
  319. (defun ledger/format-add-entry-message (from)
  320. (format nil "Ledger add from ~A ~A at ~A"
  321. (aget "first_name" from) (aget "last_name" from)
  322. (ledger/format-time (get-universal-time))))
  323. (defun ledger/process-add-entry (chat-id from entry)
  324. (with-secret (info (list :ledger chat-id))
  325. (if info
  326. (cond
  327. ((consp info)
  328. (handler-case (progn
  329. (ledger/add-git
  330. chat-id
  331. (car info) (cadr info)
  332. (pta-ledger:render entry)
  333. (ledger/format-add-entry-message from))
  334. "Добавил!")
  335. (error (e)
  336. (log:error "~A" e)
  337. "Не смог :(")))
  338. (:otherwise "Добавляю только в git журнал :("))
  339. "Добавь git журнал.")))
  340. (defun ledger/new-chat-entry (chat-id entry)
  341. (let ((entry (extend-entry! chat-id entry)))
  342. (bot-send-message
  343. chat-id (format-entry entry)
  344. :parse-mode "markdown"
  345. :reply-markup (keyboard/entry chat-id entry))))
  346. (defun entry-edit (chat-id message-id entry)
  347. (let ((entry (extend-entry! chat-id entry)))
  348. (telegram-edit-message-text
  349. (format-entry entry)
  350. :chat-id chat-id :message-id message-id
  351. :parse-mode "markdown"
  352. :reply-markup (keyboard/edit-entry chat-id entry))))
  353. (defun keyboard/entry (chat-id entry)
  354. (get-inline-keyboard
  355. chat-id
  356. (list
  357. (list (inline-button ("➕")
  358. (telegram-answer-callback-query query-id :text "Добавляю...")
  359. (telegram-send-message source-chat-id (ledger/process-add-entry
  360. source-chat-id from entry))
  361. (telegram-edit-message-reply-markup
  362. nil :chat-id source-chat-id :message-id source-message-id))
  363. (inline-button ("💲")
  364. (entry-edit source-chat-id source-message-id entry))
  365. (inline-button ("✖️")
  366. (telegram-delete-message source-chat-id source-message-id))))))
  367. (defun def (str default)
  368. (if (or (null str) (string= "" str)) default
  369. str))
  370. (defun keyboard/edit-entry (chat-id entry)
  371. (get-inline-keyboard
  372. chat-id
  373. (append
  374. (list
  375. (list (inline-button ((format-date (pta-ledger:entry-date entry)))
  376. (bot-send-message chat-id "Введите дату"
  377. :reply-markup (telegram-force-reply))
  378. (on-next-message chat-id
  379. (let ((date (pta-ledger:parse-date text (get-year
  380. (pta-ledger:entry-date entry)))))
  381. (if date
  382. (progn
  383. (setf (pta-ledger:entry-date entry) date)
  384. (entry-edit chat-id source-message-id entry))
  385. (bot-send-message chat-id "Не разобрал")))))
  386. (inline-button ((def (pta-ledger:entry-description entry) "Описание"))
  387. (bot-send-message chat-id "Введите описание"
  388. :reply-markup (telegram-force-reply))
  389. (on-next-message chat-id
  390. (setf (pta-ledger:entry-description entry) text)
  391. (entry-edit chat-id source-message-id entry)))
  392. (inline-button ((def (pta-ledger:entry-comment entry) "Коммент"))
  393. (bot-send-message chat-id "Введите комментарий"
  394. :reply-markup (telegram-force-reply))
  395. (on-next-message chat-id
  396. (setf (pta-ledger:entry-comment entry) text)
  397. (entry-edit chat-id source-message-id entry)))))
  398. (loop for posting in (pta-ledger:entry-postings entry)
  399. collect
  400. (let ((this-posting posting))
  401. (list (inline-button ((pta-ledger:posting-account posting))
  402. (account-edit chat-id source-message-id entry this-posting
  403. (pta-ledger:posting-account this-posting)))
  404. (inline-button ((def (pta-ledger:render
  405. (pta-ledger:posting-amount posting))
  406. "Сумма"))
  407. (bot-send-message chat-id "Введите сумму"
  408. :reply-markup (telegram-force-reply))
  409. (on-next-message chat-id
  410. (let ((amount (pta-ledger:parse-amount text)))
  411. (setf (pta-ledger:posting-amount this-posting) amount
  412. (pta-ledger:posting-status this-posting) nil)
  413. (entry-edit chat-id source-message-id entry))))
  414. (inline-button ("❌")
  415. (setf (pta-ledger:entry-postings entry)
  416. (remove this-posting (pta-ledger:entry-postings entry)
  417. :test #'equalp))
  418. (entry-edit chat-id source-message-id entry)))))
  419. (list (list (inline-button ("Готово")
  420. (telegram-edit-message-reply-markup
  421. (keyboard/entry chat-id entry)
  422. :chat-id chat-id :message-id source-message-id)))))))
  423. (defun accounts/nav (account accounts &optional (offset 0) (count 5))
  424. (let* ((len (1+ (length account)))
  425. (sep-pos (position #\: account :from-end t))
  426. (parent (when sep-pos (subseq account 0 sep-pos)))
  427. (head (when account (concatenate 'string account ":")))
  428. (descendants (remove-if #'(lambda (a)
  429. (when head
  430. (not (equal head
  431. (subseq a 0 (min (length a) len))))))
  432. accounts)))
  433. (if descendants
  434. (let* ((children (sort (remove-duplicates
  435. (mapcar #'(lambda (d)
  436. (subseq d 0 (or (position #\: d :start len)
  437. (length d))))
  438. descendants)
  439. :test #'equal) #'string<))
  440. (total (length children)))
  441. (when (stringp offset)
  442. (let ((off (position offset children :test #'equal)))
  443. (setf offset (if off (max 0 (- off (round count 2))) 0))))
  444. (values account parent
  445. (mapcar
  446. #'(lambda (c)
  447. (let* ((needle (concatenate 'string c ":"))
  448. (n-l (length needle)))
  449. (cons c
  450. (not
  451. (find needle accounts :test #'equal
  452. :key #'(lambda (a)
  453. (subseq a 0 (min n-l (length a)))))))))
  454. (subseq children offset
  455. (min total (+ offset count))))
  456. (unless (zerop offset) (max 0 (- offset count)))
  457. (when (< (+ offset count) total) (+ offset count))))
  458. (when parent (accounts/nav parent accounts account)))))
  459. (defun account-edit (chat-id message-id entry posting account &optional (offset 0))
  460. (with-chat-journal (chat-id journal updated)
  461. (let* ((accounts (pta-ledger:journal-accounts journal))
  462. (nav-list (multiple-value-list
  463. (accounts/nav account accounts offset))))
  464. (telegram-edit-message-reply-markup
  465. (apply #'keyboard/account chat-id entry posting nav-list)
  466. :chat-id chat-id :message-id message-id))))
  467. (defun keyboard/account (chat-id entry posting account parent children prev next)
  468. (get-inline-keyboard
  469. chat-id
  470. (append
  471. (list (list (when account
  472. (inline-button (account)
  473. (setf (pta-ledger:posting-account posting) account
  474. (pta-ledger:posting-status posting) nil)
  475. (entry-edit chat-id source-message-id entry)))
  476. (inline-button ("Ввести")
  477. (bot-send-message chat-id "Введите счёт"
  478. :reply-markup (telegram-force-reply))
  479. (on-next-message chat-id
  480. (let ((account (pta-ledger:parse-account text)))
  481. (if account
  482. (progn
  483. (setf (pta-ledger:posting-account posting) account
  484. (pta-ledger:posting-status posting) nil)
  485. (entry-edit chat-id source-message-id entry))
  486. (bot-send-message chat-id "Не разобрал")))))))
  487. (loop for (acc . leaf) in children
  488. collect (let ((this-account acc))
  489. (list (if leaf
  490. (inline-button (this-account)
  491. (setf (pta-ledger:posting-account posting) this-account
  492. (pta-ledger:posting-status posting) nil)
  493. (entry-edit chat-id source-message-id entry))
  494. (inline-button ((format nil "~A ..." this-account))
  495. (account-edit chat-id source-message-id entry posting
  496. this-account))))))
  497. (list (list (when prev
  498. (inline-button ("<<")
  499. (account-edit chat-id source-message-id entry posting
  500. account prev)))
  501. (when (or parent account)
  502. (inline-button ((or parent "ACC"))
  503. (account-edit chat-id source-message-id entry posting
  504. parent account)))
  505. (when next
  506. (inline-button (">>")
  507. (account-edit chat-id source-message-id entry posting
  508. account next))))))))