0

My reputation is not enough to comment on this post: Relational Data Model for Double-Entry Accounting

For the data model from the linked question, can anyone explain the calculation for the closing balance on the AccountStatement, any sample SQL?

  • And a small clarification, correct me if I'm wrong. For the calculation of the closing balance for every first day of the month, I have a button that triggers on my module to calculate it?
PerformanceDBA
  • 32,198
  • 10
  • 64
  • 90
luke
  • 47
  • 5
  • Explain how a table works is a very open ended question. Please reword your question to be clear about what aspects of the table you do understand, and where you are confused. Also, Double entry accounting is a well understood problem area with books going back a century or more. IBM was building punched card accounting machines before they began building computers. There is much literature on database design for DEA. And there is the long answer to the question you referenced. – Walter Mitty Dec 29 '19 at 20:48
  • 1. The answer on that linked question is wrong, please ignore it. 2. Your question here shows that you don't understand accounting principles. Go and read up/ask questions about "how the general ledger ... works" before trying to jump into data models or processing. 3. It is very archaic design (dating back to pre-computer accounting systems) to have any month-end processing. Every transaction has a date; the 'closing balance' is merely the sum of transactions dated up to that month; there's no need for a 'button' to trigger anything. – AntC Dec 30 '19 at 04:22
  • 2
    @AntC. 1) Re "wrong". That contradicts reality on several points. (a) that seeker has already up-voted the Answer and accepted it (b) this seeker understood that Answer and asked for more info on a specific point. This Answer has already been accepted by this seeker. 2) Perhaps if you take one of the Basic Accounting Tutorials, you would be in a position to make comments re an Answer being right or wrong. – PerformanceDBA Dec 30 '19 at 06:33
  • @PerformanceDBA you said in your article and i saw your example that only one leg either debit/credit add on your transaction and ledger transaction table, if that so, what data should i add on my table, and if only sigle leg how can i get the other leg on my reports? – luke Dec 30 '19 at 12:00
  • 2
    @luke. That is not correct, I have not said that. You are not understanding what I have said. I have improved the wording in my Answer, please read again. The Credit/Debit pair is always one **business** transaction (as opposed to a database transaction), with two legs; two entries in a paper-based account book. That translates to one row in my database, with two side, one Credit side & one Debit side. – PerformanceDBA Dec 31 '19 at 03:13
  • 1
    @PerformanceDBA oh i see i just read it now again, now i understand i was confuse by the field name on the transaction table because of the one xactType_code. can you send me sql for ledgertransaction? – luke Dec 31 '19 at 04:00
  • also why on business transaction table dont have id for auto increment? – luke Dec 31 '19 at 04:11
  • 2
    1) If you do not read the **[IDEF1X Intro](https://www.softwaregems.com.au/Documents/Documentary%20Examples/IDEF1X%20Introduction.pdf)** you will not be able to understand the data model. 2) Yes, there was a tiny bit of confusion. I have upgraded all the graphics. Please check again, and ensure there is no confusion. 3) The SQL for Ledger Transaction is at [6.4] 4) What is an id field, and why do I need one ? – PerformanceDBA Dec 31 '19 at 14:21
  • 1
    @PerformanceDBA thank you so much sir for your help, one more question, when account transaction add the data it will add also on the ledger transaction right? – luke Dec 31 '19 at 17:18
  • 2
    1) In a simple sense, yes. But you still do not understand. You cannot write code that manipulates a thing X, without understanding precisely WHAT the thing X is. 2) You are asking questions, but you will not follow directions. I don't know if anyone can help you. – PerformanceDBA Dec 31 '19 at 23:24
  • 1
    @PerformanceDBA thank you so much.. i hope you will release a book.. – luke Jan 01 '20 at 01:31
  • @PerformanceDBA sir in your sql example to compute the closing balance why only AC and DP only computed, how about the Widraw? – luke Jan 04 '20 at 15:44
  • im reading your answer here and its different from your answer now sir, before double entry is 2 rows inserted on the transaction table, now single..https://stackoverflow.com/questions/4074737/accounting-database-storing-a-transaction – luke Jan 05 '20 at 12:02
  • 2
    1) You are welcome. 2) I can't find a *Compute Closing Balance*. If you mean [6.1], you have missed the keyword **`NOT`** `IN ( "AC", "DP" )`, which means `IN ( "AD", "FB", "Wd", "WA" )`. 3) As required in SO, the Answer matches the Question, and not other Questions. That second linked Question is not Double Entry, there is **nothing** in that Q & A that relates to this Q & A. If you read it, you will get even more confused, such as thinking that there is a contradiction. Stick to the first linked Q & A, and this, your Q & A. – PerformanceDBA Jan 06 '20 at 10:02
  • thank you sir, btw DP is a deposit sir right?and deposit is a debit.. in your computation, you query on totalCredit using IN ("AC","DP"), meaning the deposit will and ac will go to totalCredit? not in totaldebit? – luke Jan 06 '20 at 15:50
  • 2
    1) Refer to the `TransactionType_Ext` table in the linked **[data model](https://www.softwaregems.com.au/Documents/Student_Resolutions/Alex/Alex%20TA%20Data.png)**. The sample data lists the codes; meanings; & the Credit/Debit effect (+/-) on the `Account`. 2) All money is positive (there is no such thing as negative five dollars). So all the `AccountTransactions` that credit the `Account` for the month are `SUMmed` to produce its `TotalCredit` for the month, and all the `AccountTransactions` that debit the `Account` for the month are `SUMmed` to produce its `TotalDebit` for the month. – PerformanceDBA Jan 07 '20 at 03:10
  • 2
    3) Do not confuse those Credit/Debit **actions on the** `Account`, which at MonthEnd will be a single monthly row in `AccountStatement`, the **action** which is delineated by `TransactionType_Ext` ... with ... the Credit/Debit **legs of a Double-Entry Transaction**, which is a single row in `AccountTransaction`, the **leg** which is delineated by `TransactionType_DE`. DE is short for Double-Entry. – PerformanceDBA Jan 07 '20 at 03:25
  • i got confused because all i know is When you credit Cash, you subtract from it. Likewise, when you debit Cash, you add to it. so in double entry bookkeeping is different its opposite.. – luke Jan 07 '20 at 14:31
  • thank you sir, for example i want to have a report like ledger format, where i get that report on transaction ledger table sir? if yes why os your credit ia on the left and debit ia on the right?i read that double entry, the debit is on the right with amount and credit is on the left with amount also and group by accout number how can i produce that report? – luke Jan 07 '20 at 14:31
  • 2
    1) If that article is good, follow them, ask them. If my article is good, follow me, ask me. But do not ask me about their article, or ask them about my article. 2) My article is not paper-based, it has no "left" or "right", it may be too advanced to compare. There is nothing for me to answer. – PerformanceDBA Jan 07 '20 at 22:31
  • can you give my sql or idea how do.you output the monthly reports or weekly reports. – luke Jan 17 '20 at 15:47
  • 1
    You killed a question with "how to do X in php". @PerformanceDBA provides really good answers with excellent code samples you will never find in hundreds of forums and you can easily implement that in any lang and database system, but it's out of the scope. – Alex Jan 23 '20 at 11:35

1 Answers1

4

can any body explain to me how the general ledger transaction table works

In the hope that this is what you are seeking, that this will suffice:

  • merely INSERT LedgerTransaction ... for each real world Ledger Transaction

If you are seeking more than that, it means you need accounting basics, which cannot be answered here. Check for the free Tutorials available on the web.

also for the calculation of the closing balance on the AccountStatement any sample sql?

SQL • View

I have upgraded the View from the linked question, to obtain TotalCredit & TotalDebit columns, for all Transactions since the 1st day of the previous month.

CREATE VIEW Account_Current_V 
AS
    SELECT 
        AccountNo,
        Date = DATEADD( DD, -1, GETDATE() ), -- show previous day
        ClosingBalance,
        TotalCredit = (
            -- TotalCredit_Subquery
            SELECT SUM( Amount )
                FROM AccountTransaction
                WHERE AccountNo = @AccountNo
                    AND XactTypeCode_Ext IN ( "AC", "Dp" )
                        -- this month
                    AND DateTime >= CONVERT( CHAR(6), GETDATE(), 2 ) + "01"
                ),
        TotalDebit = (
            -- TotalDebit_Subquery
            SELECT SUM( Amount )
                FROM AccountTransaction
                WHERE AccountNo = @AccountNo
                    AND XactTypeCode_Ext NOT IN ( "AC", "Dp" )
                    AND DateTime >= CONVERT( CHAR(6), GETDATE(), 2 ) + "01"
                ),
        CurrentBalance = ClosingBalance + 
            <TotalCredit_Subquery> - 
            <TotalDebit_Subquery>
        FROM AccountStatement                -- 1st day of this month
        WHERE Date = CONVERT( CHAR(6), GETDATE(), 2 ) + "01"

SQL • MonthEnd • Insert AccountStatement

On the 1st day of the new month, the idea is to close the previous month, dated the 1st of the new month. We use the above View which to obtains the TotalCredit & TotalDebit columns for all Transactions since the 1st day of the previous month.

This is just one Task in the Month End job, on the 1st day of the month. It would normally be run on the batch queue, for all Accounts,with proper transaction control limits (eg. SET ROWCOUNT 500), etc.

INSERT AccountStatement 
    SELECT  AccountNo,
            -- Date: 1st day of this month
            CONVERT( CHAR(6), GETDATE(), 2 ) + "01",
            ACV.CurrentBalance,
            ACV.TotalCredit,
            ACV.TotalDebit
        FROM Account_Current_V    ACV
            JOIN AccountStatement ASS
                ON ACV.AccountNo = ASS.AccountNo
        -- AccountStatements that are not yet MonthEnd-ed
        -- get single row that is newest
        WHERE ASS.Date = (
            SELECT MAX( Date )
                FROM AccountStatement
                WHERE AccountNo = ASS.AccountNo
                )
                -- that is not 1st day of this month
            AND ASS.Date != CONVERT( CHAR(6), GETDATE(), 2 ) + "01"

It is the same for updating the LedgerStatement.

correct me if im wrong, for the calculation of the closing balance for every first day of the next month, i have a button that trigger on my module to. calculate it?

No. Whereas the GUI interface is online, an app of any reasonable complexity needs to run jobs on the back end server. Eg. Month End; Daily backups; Transaction Log roll-ups; etc. Generally there is a set up on the server to do that, else you have to write one.

There will be many Tasks that comprise Month End. This is just one of those Tasks. There is a limit to what you can do in PHP, and I wouldn't dream of doing this in PHP. For technical and modularity reasons, I would place the code for that Task, and all the other Month End Tasks, in a stored proc Account_MonthEnd_btr.

You can't do it from a button because:

  • it will hang up the GUI until the Month End Task is finished, which may be more than a few minutes (depends on the number of Accounts, LedgerAccounts, etc).

  • it will blow the Transaction Log (if the number of Ledgers or Accounts is at all large). That control as well, has to be in the back end.

halfer
  • 19,824
  • 17
  • 99
  • 186
PerformanceDBA
  • 32,198
  • 10
  • 64
  • 90
  • noticed Account_Current_V is calculated out from the AccountStatement table which is supposed to be generated at the end/beginning of every month. How then do you get correct the daily account balance for a user before a withdrawal or transfers. – philip_nunoo Jul 06 '20 at 06:46