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:
using DataFrames
ledger=DataFrame(
date=[""],
chequing=[0],
expenses=[0],
income=[0])
function ledgertransaction(
date,
credit_account,
credit_amount,
debit_account,
debit_amount
)
default_row=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
spec = Dict("date" => date, credit_account => credit_amount, debit_account => debit_amount)
row = merge(default_row, spec)
push!(ledger,row)
end
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:
ledgertransaction(date,amount,credit_account,debit_account)
-
As we change ledger
we should write it as:
ledgertransaction!(ledger,...)
-
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.
-
If credit_account
and 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 default_values
generically)
-
Parameter 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 #âŚ):
using DataFrames
ledger=DataFrame(
date=[""],
chequing=[0],
expenses=[0],
income=[0])
function ledgertransaction!(ledger,date,amount,credit_account,debit_account)
#check if date is not a empty string
if length(date) == 0
return false
end
#check if credit_account is different from debit_account:
#...
#check if credit_account exists:
if !( credit_account in names(ledger) )
return false
end
#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:
default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
spec = Dict("date" => date, credit_account => amount, debit_account => amount)
row = merge(default_values, spec)
push!(ledger,row)
return true
end
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")
ledgertransaction!(ledger,"",30,"expenses","chequing")
If you didn't skip the part with the generic *default_values* (issue 4) the following should do also fine:
another_ledger=DataFrame(
date=[""],
chequing=[0],
expenses=[0],
income=[0],
sum=[0]
)
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.