1
0

ledger.lisp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 ()
  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 (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 "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>"))))
  118. (defun ledger/handle-set-info (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 (format nil "Добавил журнал с ~D записями. Веб-хук для обновления: ~A"
  126. (length journal)
  127. (ledger/get-hook-url *chat-id*))))
  128. (pta-ledger:journal-failed (e)
  129. (bot-send-message (format nil "Не смог спарсить: ~A" e)))
  130. (dex:http-request-failed (e)
  131. (bot-send-message (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 *args*))
  135. (:otherwise (ledger/handle-info))))
  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 "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>"
  144. :chat-id chat-id)))))
  145. (defun ledger/handle-balance (query)
  146. (with-chat-journal (*chat-id* journal updated)
  147. (bot-send-message (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 (if (eql *cmd* :budget) "budget" "assets")))
  157. (:otherwise (ledger/handle-balance (spaced *args*)))))
  158. (defun ledger/handle-journal (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 (if entries
  165. (text-chunks (subseq entries (- len count) len))
  166. "Не нашлось")
  167. :parse-mode "markdown"))))
  168. (def-message-cmd-handler handler-journal (:journal)
  169. (cond
  170. ((null *args*) (ledger/handle-journal "date:thisweek"))
  171. (:otherwise (ledger/handle-journal (spaced *args*)))))
  172. (def-message-cmd-handler hander-add-report (:add_report)
  173. (bot-send-message "Введи название"
  174. :reply-markup (telegram-force-reply))
  175. (on-next-message
  176. (let ((name *text*))
  177. (bot-send-message "Введи запрос"
  178. :reply-markup (telegram-force-reply))
  179. (on-next-message
  180. (let ((query *text*))
  181. (pta-ledger:parse-query query) ;; Validate query
  182. (bot-send-message "Введи расписание, ex: (:day-of-week 0 :hour 10), (:day * :hour 10)"
  183. :reply-markup (telegram-force-reply))
  184. (on-next-message
  185. (let* ((schedule *text*))
  186. (add-chat-cron :ledger-report *chat-id* schedule name query)
  187. (bot-send-message "Добавил"))))))))
  188. (defun run-report (chat-id name query)
  189. (with-chat-journal (chat-id journal updated)
  190. (bot-send-message (text-chunks (split-sequence:split-sequence
  191. #\Newline
  192. (format nil "*~A*~%~%~A" name
  193. (pta-ledger:journal-balance journal query)))
  194. :text-sep "
  195. ")
  196. :parse-mode "markdown"
  197. :chat-id chat-id)))
  198. (def-chat-cron-handler handler-chat-cron (:ledger-report chat-id schedule name query)
  199. (run-report chat-id name query))
  200. (def-message-cmd-handler handler-reports (:reports)
  201. (let ((crons (get-chat-crons :ledger-report *chat-id*)))
  202. (if crons
  203. (bot-send-message
  204. (format nil "Отчёты~%~%~{~A) ~A _~A_: ~A~^~%~}"
  205. (loop for (schedule name query) in crons
  206. for idx from 1
  207. append (list idx name schedule query)))
  208. :parse-mode "markdown"
  209. :reply-markup
  210. (get-inline-keyboard
  211. (append
  212. (loop for (schedule name query) in crons
  213. for idx from 0
  214. collect
  215. (let ((cron-index idx)
  216. (cron-name name))
  217. (list (inline-button ((format nil "Удалить '~A'" cron-name))
  218. (delete-chat-cron :ledger-report *source-chat-id* cron-index)
  219. (telegram-edit-message-reply-markup
  220. nil :chat-id *source-chat-id* :message-id *source-message-id*)
  221. (bot-send-message (format nil "Удалил *~A*" cron-name)
  222. :chat-id *source-chat-id*
  223. :parse-mode "markdown")))))
  224. (list (list (inline-button ("Отмена")
  225. (telegram-edit-message-reply-markup
  226. nil :chat-id *source-chat-id* :message-id source-message-id)))))))
  227. (bot-send-message
  228. "Отчётов пока нет"
  229. :reply-markup (telegram-reply-keyboard-markup (list (list (list :text "/add_report")))
  230. :one-time-keyboard t)))))
  231. (defun match-entry (text journal)
  232. (labels ((two-post (entries)
  233. (remove-if-not #'(lambda (e)
  234. (= 2 (length (pta-ledger:entry-postings e))))
  235. entries)))
  236. (let ((desc (two-post
  237. (pta-ledger:journal-entries journal (format nil "desc:'~A'" text)))))
  238. (if desc (car (last desc))
  239. (let ((comment (two-post
  240. (pta-ledger:journal-entries journal (format nil "comment:'~A'" text)))))
  241. (when comment (car (last comment))))))))
  242. (defvar *expenses-account-root* "expenses" "Expenses accounts root.")
  243. (defun create-entry (chat-id text amount currency)
  244. (with-chat-journal (chat-id journal updated)
  245. (let* ((old (match-entry text journal))
  246. (new (or (when old (pta-ledger:clone-entry old))
  247. (pta-ledger:make-entry
  248. :description text
  249. :postings (list
  250. (pta-ledger:make-posting :account *expenses-account-root*)
  251. (pta-ledger:make-posting)))))
  252. (postings (pta-ledger:entry-postings new)))
  253. (setf (pta-ledger:entry-date new) (get-universal-time)
  254. (pta-ledger:posting-amount (car postings)) (pta-ledger:make-amount
  255. :quantity amount
  256. :commodity currency)
  257. (pta-ledger:posting-amount (cadr postings)) (pta-ledger:make-amount
  258. :quantity (- amount)
  259. :commodity currency)
  260. (pta-ledger:posting-account (cadr postings)) (format nil "assets:Cash:~A" currency))
  261. new)))
  262. (defun find-posting (entry root)
  263. (when (and entry root)
  264. (find root (pta-ledger:entry-postings entry)
  265. :test #'equal
  266. :key #'(lambda (p)
  267. (car (pta-ledger:account-parents
  268. (pta-ledger:posting-account p) :tree :t))))))
  269. (defvar *budget-account-root* "budget" "Budget accounts root. Extend entry with matching budget if set")
  270. (defvar *budget-search-date-query* "date:last30" "Date part of matching budget search")
  271. (defun extend-entry! (chat-id entry)
  272. (let ((expense (find-posting entry *expenses-account-root*))
  273. (budget (find-posting entry *budget-account-root*)))
  274. (when (and *budget-account-root* expense (or (null budget)
  275. (equal #\! (pta-ledger:posting-status budget))))
  276. (with-chat-journal (chat-id journal updated)
  277. (let* ((last-entries (pta-ledger:journal-entries journal (format nil "~A acct:^~A"
  278. *budget-search-date-query*
  279. (pta-ledger:posting-account expense))))
  280. (last-budget (some #'(lambda (e) (find-posting e *budget-account-root*))
  281. (reverse last-entries)))
  282. (expense-amount (car (pta-ledger:get-amounts expense (pta-ledger:entry-postings entry) :t))))
  283. (when last-budget
  284. (unless budget
  285. (setf budget (pta-ledger:make-posting :status #\! :virtual #\())
  286. (setf (pta-ledger:entry-postings entry)
  287. (append (pta-ledger:entry-postings entry)
  288. (list budget))))
  289. (setf (pta-ledger:posting-amount budget) (pta-ledger:make-amount
  290. :quantity (- (pta-ledger:amount-quantity expense-amount))
  291. :commodity (pta-ledger:amount-commodity expense-amount))
  292. (pta-ledger:posting-account budget) (pta-ledger:posting-account last-budget))))))
  293. entry))
  294. (def-message-cmd-handler handler-create (:rub :usd :eur :thb :btc :czk)
  295. (cond
  296. ((>= (length *args*) 2)
  297. (ledger/new-chat-entry *chat-id* (create-entry *chat-id*
  298. (spaced (subseq *args* 1))
  299. (parse-float (car *args*))
  300. (symbol-name *cmd*))))
  301. (:otherwise (bot-send-message (format nil "/~A <amount> <description>" *cmd*)))))
  302. (def-webhook-handler ledger/handle-webhook ("ledger")
  303. (when (= 2 (length *paths*))
  304. (destructuring-bind (chat-id hmac) *paths*
  305. (let ((true-hmac (token-hmac chat-id)))
  306. (when (string= true-hmac hmac)
  307. (with-secret (info (list :ledger chat-id))
  308. (when info
  309. (let ((chat-id (parse-integer chat-id)))
  310. (log:info "Updating ledger for ~A" chat-id)
  311. (setf (gethash chat-id *ledger/chat-journals*) nil)
  312. (get-chat-journal-info chat-id info)
  313. "OK"))))))))
  314. ;; New entries
  315. (defun format-entry (entry)
  316. (let ((pta-ledger:*posting-length* 40))
  317. (format nil "```~%~A```" (pta-ledger:render entry))))
  318. (defun ledger/format-add-entry-message (from)
  319. (format nil "Ledger add from ~A ~A at ~A"
  320. (aget "first_name" from) (aget "last_name" from)
  321. (ledger/format-time (get-universal-time))))
  322. (defun ledger/process-add-entry (chat-id from entry)
  323. (with-secret (info (list :ledger chat-id))
  324. (if info
  325. (cond
  326. ((consp info)
  327. (handler-case (progn
  328. (ledger/add-git
  329. chat-id
  330. (car info) (cadr info)
  331. (pta-ledger:render entry)
  332. (ledger/format-add-entry-message from))
  333. "Добавил!")
  334. (error (e)
  335. (log:error "~A" e)
  336. "Не смог :(")))
  337. (:otherwise "Добавляю только в git журнал :("))
  338. "Добавь git журнал.")))
  339. (defun ledger/new-chat-entry (chat-id entry)
  340. (let ((entry (extend-entry! chat-id entry)))
  341. (bot-send-message
  342. (format-entry entry)
  343. :chat-id chat-id
  344. :parse-mode "markdown"
  345. :reply-markup (keyboard/entry 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 message-id entry))))
  353. (defun keyboard/entry (entry)
  354. (get-inline-keyboard
  355. (list
  356. (list (inline-button ("➕")
  357. (telegram-answer-callback-query *query-id* :text "Добавляю...")
  358. (telegram-send-message *source-chat-id* (ledger/process-add-entry
  359. *source-chat-id* *from* entry))
  360. (telegram-edit-message-reply-markup
  361. nil :chat-id *source-chat-id* :message-id *source-message-id*))
  362. (inline-button ("💲")
  363. (entry-edit *source-chat-id* *source-message-id* entry))
  364. (inline-button ("✖️")
  365. (telegram-delete-message *source-chat-id* *source-message-id*))))))
  366. (defun def (str default)
  367. (if (or (null str) (string= "" str)) default
  368. str))
  369. (defun keyboard/edit-entry (chat-id message-id entry)
  370. (get-inline-keyboard
  371. (append
  372. (list
  373. (list (inline-button ((format-date (pta-ledger:entry-date entry)))
  374. (bot-send-message "Введите дату" :chat-id chat-id
  375. :reply-markup (telegram-force-reply))
  376. (on-next-message
  377. (let ((date (pta-ledger:parse-date *text* (get-year
  378. (pta-ledger:entry-date entry)))))
  379. (if date
  380. (progn
  381. (setf (pta-ledger:entry-date entry) date)
  382. (entry-edit chat-id message-id entry))
  383. (bot-send-message "Не разобрал" :chat-id chat-id)))))
  384. (inline-button ((def (pta-ledger:entry-description entry) "Описание"))
  385. (bot-send-message "Введите описание" :chat-id chat-id
  386. :reply-markup (telegram-force-reply))
  387. (on-next-message
  388. (setf (pta-ledger:entry-description entry) *text*)
  389. (entry-edit chat-id message-id entry)))
  390. (inline-button ((def (pta-ledger:entry-comment entry) "Коммент"))
  391. (bot-send-message "Введите комментарий" :chat-id chat-id
  392. :reply-markup (telegram-force-reply))
  393. (on-next-message
  394. (setf (pta-ledger:entry-comment entry) *text*)
  395. (entry-edit chat-id message-id entry)))))
  396. (loop for posting in (pta-ledger:entry-postings entry)
  397. collect
  398. (let ((this-posting posting))
  399. (list (inline-button ((pta-ledger:posting-account posting))
  400. (account-edit chat-id *source-message-id* entry this-posting
  401. (pta-ledger:posting-account this-posting)))
  402. (inline-button ((def (pta-ledger:render
  403. (pta-ledger:posting-amount posting))
  404. "Сумма"))
  405. (bot-send-message "Введите сумму" :chat-id chat-id
  406. :reply-markup (telegram-force-reply))
  407. (on-next-message
  408. (let ((amount (pta-ledger:parse-amount *text*)))
  409. (setf (pta-ledger:posting-amount this-posting) amount
  410. (pta-ledger:posting-status this-posting) nil)
  411. (entry-edit chat-id message-id entry))))
  412. (inline-button ("❌")
  413. (setf (pta-ledger:entry-postings entry)
  414. (remove this-posting (pta-ledger:entry-postings entry)
  415. :test #'equalp))
  416. (entry-edit chat-id *source-message-id* entry)))))
  417. (list (list (inline-button ("Готово")
  418. (telegram-edit-message-reply-markup
  419. (keyboard/entry entry)
  420. :chat-id chat-id :message-id *source-message-id*)))))))
  421. (defun accounts/nav (account accounts &optional (offset 0) (count 5))
  422. (let* ((len (1+ (length account)))
  423. (sep-pos (position #\: account :from-end t))
  424. (parent (when sep-pos (subseq account 0 sep-pos)))
  425. (head (when account (concatenate 'string account ":")))
  426. (descendants (remove-if #'(lambda (a)
  427. (when head
  428. (not (equal head
  429. (subseq a 0 (min (length a) len))))))
  430. accounts)))
  431. (if descendants
  432. (let* ((children (sort (remove-duplicates
  433. (mapcar #'(lambda (d)
  434. (subseq d 0 (or (position #\: d :start len)
  435. (length d))))
  436. descendants)
  437. :test #'equal) #'string<))
  438. (total (length children)))
  439. (when (stringp offset)
  440. (let ((off (position offset children :test #'equal)))
  441. (setf offset (if off (max 0 (- off (round count 2))) 0))))
  442. (values account parent
  443. (mapcar
  444. #'(lambda (c)
  445. (let* ((needle (concatenate 'string c ":"))
  446. (n-l (length needle)))
  447. (cons c
  448. (not
  449. (find needle accounts :test #'equal
  450. :key #'(lambda (a)
  451. (subseq a 0 (min n-l (length a)))))))))
  452. (subseq children offset
  453. (min total (+ offset count))))
  454. (unless (zerop offset) (max 0 (- offset count)))
  455. (when (< (+ offset count) total) (+ offset count))))
  456. (when parent (accounts/nav parent accounts account)))))
  457. (defun account-edit (chat-id message-id entry posting account &optional (offset 0))
  458. (with-chat-journal (chat-id journal updated)
  459. (let* ((accounts (pta-ledger:journal-accounts journal))
  460. (nav-list (multiple-value-list
  461. (accounts/nav account accounts offset))))
  462. (telegram-edit-message-reply-markup
  463. (apply #'keyboard/account chat-id message-id entry posting nav-list)
  464. :chat-id chat-id :message-id message-id))))
  465. (defun keyboard/account (chat-id message-id entry posting account parent children prev next)
  466. (get-inline-keyboard
  467. (append
  468. (list (list (when account
  469. (inline-button (account)
  470. (setf (pta-ledger:posting-account posting) account
  471. (pta-ledger:posting-status posting) nil)
  472. (entry-edit chat-id *source-message-id* entry)))
  473. (inline-button ("Ввести")
  474. (bot-send-message "Введите счёт" :chat-id chat-id
  475. :reply-markup (telegram-force-reply))
  476. (on-next-message
  477. (let ((account (pta-ledger:parse-account *text*)))
  478. (if account
  479. (progn
  480. (setf (pta-ledger:posting-account posting) account
  481. (pta-ledger:posting-status posting) nil)
  482. (entry-edit chat-id message-id entry))
  483. (bot-send-message "Не разобрал" :chat-id chat-id)))))))
  484. (loop for (acc . leaf) in children
  485. collect (let ((this-account acc))
  486. (list (if leaf
  487. (inline-button (this-account)
  488. (setf (pta-ledger:posting-account posting) this-account
  489. (pta-ledger:posting-status posting) nil)
  490. (entry-edit chat-id *source-message-id* entry))
  491. (inline-button ((format nil "~A ..." this-account))
  492. (account-edit chat-id *source-message-id* entry posting
  493. this-account))))))
  494. (list (list (when prev
  495. (inline-button ("<<")
  496. (account-edit chat-id *source-message-id* entry posting
  497. account prev)))
  498. (when (or parent account)
  499. (inline-button ((or parent "ACC"))
  500. (account-edit chat-id *source-message-id* entry posting
  501. parent account)))
  502. (when next
  503. (inline-button (">>")
  504. (account-edit chat-id *source-message-id* entry posting
  505. account next))))))))