Good Morning, to all.
Great, something proceeded, but we need to analyze, what happened and whats wrong or confusing.
Therefor we still drop journal_entry, we are not yet there.
And currency roundings are also ignored for now, we stay with Int, as we are dealing with more basic problems.
The current working code is:
spec = Dict("date" => date, credit_account => credit_amount, debit_account => debit_amount)
row = merge(default_row, spec)
ledgertransaction("Jan 1, 2000","expenses",100,"chequing",100)
It is working but there are some issues:
As the amount of the transactions must be equal, there is no need to pass the value 2 times. The function should therefor more look like:
As we change
ledger we should write it as:
We shouldn’t at this point return anything from ledgertransaction! (we should return nothing or true for no error and false for error)
If we change the ledger definition, e.g. adding more accounts as columns or adding the description column, we have to change our function, which is error prone. So, the function should be more generic, so that any ledger can be passed. (this is a bit advanced)
It is possible to pass accounts, which aren’t present in the ledger. There needs to be some checking for this.
debit_account are given equal, only a single amount is accounted, which is an error. A check that those accounts a different is needed.
default_row isn’t a default row, it’s only some default values for some columns. It should be named like that (see point 4.: create
date can be empty: check for not empty (at least)
As my aim is not to provide just something which works but isn’t understood, I would suggest, that at first, we work on these issues before we proceed. This will also avoid those circles of confusion.
If you agree, take the following complete code, where the new function ledgertransaction! has already been changed (issue 1., 2. and 3., check what I have done to adress these issues, please) and comments and suggestions are filled in, but it is up to you, to make it complete (put your code where you see the #…):
#check if date is not a empty string
if length(date) == 0
#check if credit_account is different from debit_account:
#check if credit_account exists:
if !( credit_account in names(ledger) )
#check if debit_account exists:
#create a default row more generically:
# this is advanced stuff, you may skip it for now
# skipped for now, stay with the original non-generic code:
spec = Dict("date" => date, credit_account => amount, debit_account => amount)
row = merge(default_values, spec)
The following calls should do fine and return true if an entry was added or false if nothing was added because of any error:
ledgertransaction!(ledger,"Jan 1, 2000",100,"expenses","chequing")
ledgertransaction!(ledger,"Jan 1, 2000",10,"expenses","expenses")
ledgertransaction!(ledger,"Jan 1, 2000",20,"expenses","cheqing")
If you didn't skip the part with the generic *default_values* (issue 4) the following should do also fine:
ledgertransaction!(another_ledger,"Jan 1, 2000",100,"expenses","sum")
In this example, the additional column sum can be accessed like an account. Of course this is not what a sum is. So this still needs special coding and can not be generic as the meaning of sum is something different.
Give me some feedback, if it’s clear what I did in the code. And of course, the issues may need some discussion.
Whatever questions you have, let’s get them out of the way before you proceed.