Skip to content

Commit

Permalink
[simple-book-add-txn] keep trying until txn balanced
Browse files Browse the repository at this point in the history
because using weird fractions for amount eg amt=222/13 will lead to
imbalanced 0.01 transactions
  • Loading branch information
christopherlam committed Oct 23, 2023
1 parent a9eb053 commit e2009e7
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions doc/examples/simple-book-add-txn.scm
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,37 @@
(action-fn acc-EXP-LEAF)
(action-fn acc-EXP-TAX))

(define txn (xaccMallocTransaction book))

(define description (get-line "Description"))
(define net-amount (get-amount "Amount, without tax"))
(define tax-amount (* net-amount 1/10))
(define total-amount (+ tax-amount net-amount))

(accounts-action xaccAccountBeginEdit)
(xaccTransBeginEdit txn)
(xaccTransSetCurrency txn (xaccAccountGetCommodity acc-BANK))
(xaccTransSetDatePostedSecsNormalized txn (current-time))
(xaccTransSetDescription txn description)
(add-to-transaction book txn acc-BANK (- total-amount) "from bank")
(add-to-transaction book txn acc-EXP-LEAF net-amount "expense net")
(add-to-transaction book txn acc-EXP-TAX tax-amount "tax paid")
(newline)
(gnc:dump-transaction txn)

(cond
((get-binary-response "Please confirm transaction [YN]")
(xaccTransCommitEdit txn))
(else
(xaccTransRollbackEdit txn)
(xaccTransDestroy txn)))
(accounts-action xaccAccountCommitEdit)

(let lp ()
(define txn (xaccMallocTransaction book))
(define net-amount (get-amount "Amount, without tax"))
(define tax-amount (* net-amount 1/10))
(define total-amount (+ tax-amount net-amount))

(xaccTransBeginEdit txn)
(xaccTransSetCurrency txn (xaccAccountGetCommodity acc-BANK))
(xaccTransSetDatePostedSecsNormalized txn (current-time))
(xaccTransSetDescription txn description)
(add-to-transaction book txn acc-BANK (- total-amount) "from bank")
(add-to-transaction book txn acc-EXP-LEAF net-amount "expense net")
(add-to-transaction book txn acc-EXP-TAX tax-amount "tax paid")
(newline)
(gnc:dump-transaction txn)

(cond
((not (xaccTransIsBalanced txn))
(display "WARNING: transaction is not balanced. Try again.\n")
(xaccTransRollbackEdit txn)
(xaccTransDestroy txn)
(lp))
((get-binary-response "Please confirm transaction [YN]")
(accounts-action xaccAccountBeginEdit)
(xaccTransCommitEdit txn)
(accounts-action xaccAccountCommitEdit))
(else
(xaccTransRollbackEdit txn)
(xaccTransDestroy txn))))

;; (gnc:dump-book)
(when (qof-book-session-not-saved book)
Expand Down

0 comments on commit e2009e7

Please sign in to comment.