Math issue

This may be very simple to some but I’ve run across a situation where a client might have a negative balance in their account and then we are applying credit.
I’ve been saying Balance minus Payment equals New Balance but I’m going to need to check if balance is negative and if so use addition instead. I’ve set it up like below where $pbalance is the previous balance. Does this look right to you? Seems to work fine but was wondering if anyone has dealt with negative numbers like this and If my solution is sound.

$balance = ($pbalance < 0 ? $pbalance + $amount : $pbalance - $amount);

Why would you be applying credit? A debit is a debit, a credit is a credit. A debit is always NewBalance = Balance - Debit. A credit is always NewBalance = Balance + Credit

A debit doesnt magically become a credit once you pass 0. Your form processor needs to be able to distinguish between the two.

Do NOT use raw Floating point operations on money - you have been warned.

An in depth overview as to why.

Use the binary safe arithmetic library ([fphp]bcadd[/fphp], [fphp]bcmul[/fphp], [fphp]bcsub[/fphp], [fphp]bcdiv[/fphp]) to handle currency.

My point is the math issue regardless of terms used.
If Balance is 90 and payment is 70 then new balance is 20 using NewBalance = Balance - Debit. This is correct.

However if Balance is -90 and payment is 70 then new balance is -160 using NewBalance = Balance - Debit. This is not correct. It should be -20

Are you dealing with a T Ledger by any chance?

I think you are wrong… If my balance is -90 (meaning I have a credit of 90 dollars), and I still make a 70 dollar payment to you, my new balance is -160, I now have a 160 credit with you.

If my balance is -90 and you are charging my 70 for my monthly subscription, then you should be using -90 + 70 which gives you -20 for my balance, and that would be correct.

You need to get back to the basics on this one and make sure you know if a credit or a debit is being applied.

That is not correct - it is and always will be -160.

Ok, now even I’m getting confused. What needs to be sorted out is the meaning of credit and debit in an accounting sense. This is what T-Ledgers are for. Drummin hasn’t answered my question but I’m going to assume that’s what he’s writing code for, because if an accountant is using the software that’s likely how they’ll want the reports.

In a T-Ledger credit is an addition to the account and a debit is a reduction of the account but (this is very important) that isn’t a comment on whether the owner of the account is gaining or losing money. The direction of money flow is set by the disposition of the account as a whole. Broadly speaking accounts are either assets or expenses. If you credit an expense account you’re adding a bill to it, so you owe more money than you owed before the credit.

Confused yet everyone?

Ok, here’s the thing, and the saving grace of this madness. A negative number on a T-Ledger is NEVER EVER VALID. The whole point of a T-Ledger is to prevent accounting mistakes borne from confusing a credit for a debit, so the two amounts are placed into separate columns when they are written down and done by hand.

Now a computer doesn’t make mistakes like this, so you can store account debits as negative numbers. Then, at display time use the polarity of the number to choose the column to print it on and display the absolute value of the number.

Neither the computer nor the programmer really needs to worry about the disposition of the account and whether someone is losing or gaining money - that’s determined by the disposition of the account.

With all that in mind, let’s return to Drummin’s original post.

This may be very simple to some but I’ve run across a situation where a client might have a negative balance in their account and then we are applying credit.

I’ve been saying Balance minus Payment equals New Balance but I’m going to need to check if balance is negative and if so use addition instead. I’ve set it up like below where $pbalance is the previous balance. Does this look right to you? Seems to work fine but was wondering if anyone has dealt with negative numbers like this and If my solution is sound.

No, no, no!!!

From what you’ve described, you’re talking about an Accounts Receivable or Billing account - an asset account. Credits to that particular type of account are what the client owes, And debits from the account are what they are paying. The only way such an account goes negative is if the client overpays, in which case you’ll want to issue a refund check.

Do it right. The balance of any account are the credits of the account less the debits. So debits are stored as negative numbers for this reason.

You’re confusing yourself by losing sight of who is paying who. Payment is ambiguous here. A Payment by the client ALWAYS debits the account. It should be -160. Now If you are refunding money, you should call that something other than payment for clarity and sanity sake - might I suggest the word REFUND,. A refund of $70 to the client would reduce the balance to $20.

Do not use the same word (payment) to describe money transactions in two different directions. In fact, since it is naturally ambiguous, don’t use it at all. Use ‘credit’ or ‘debit’ to keep it clear what’s happening to the account.

That is not correct - it is and always will be -160.

I agree and THAT’S MY POINT hence my original post.

The balance on an account is a positive number in most case, e.g. client owes $100 then the balance stored in DB is 100. As they make a payment it is subtracted, simple. It’s not a bank but an account of what is owed.

So it’s an expense account. In such an account payments are debits and bills are credits. If the amount becomes negative the entity to whom the expense account is attached owes you money because you overpaid. Since this is possible the database field in question must allow negative numbers.

Computers use the sign on the number instead of separate columns. Debits are positive and credits are negative.

or Liabilities or Income or Equity. Expenses and Assets are debit accounts and Income, Liability and Equity accounts are credit accounts.

Credit accounts indicate where money came from - you earned it (income), borrowed it (liability) or contributed it as the owner to start things off (equity). Debit accounts indicate what you did with the money - you spent it (expenses) or invested it (assets).

Noy you have made a mistake and got the sign reversed. Debits are positive and credits are negative. Non-accountants often get the two backwards because the place they most often see the terms is on their band statement where the terms are used from the banks viewpoint. When you lend money to a bank it is an asset (debit) in your accounts and a credcit (liability) in the bank’s account - therefore the statement lists it as a credit because that’s what it is FOR THE BANK.

Yes, and expense or asset account with a credit balance will have a MINUS sign added to the value to indicate that the account is in credit and any income, liability or equity account that somehow goes int debit will have the minus sign automatically removed. Usually though all the expense and asset accounts will have positive balances and the income, liability and equity accounts will have negative balances - regardless of what all the balances are though, adding them all together will equal ZERO - since the amount of money in the “where it came from” accounts (credits) must equal the amount of money in the “what you did with it” accounts (debits).

exactly - now you have it the right way around.

and now you have it backwards again - credits are stored as negatives.

Payment is NOT ambiguous. In your accounts you always refer to the transactions from your viewpoint. A payment is where YOU pay someone else - either an expense or a liability (loan or accounts payable). When a client pays you that is a receipt of money not a payment of money. That’s why the account is called Accounts Receivable.

Same difference. Okay, so balance is 100 to indicate the client owes 100, client sends a check for 160. So now the balance is -60. If the client sends another check (out of error) for 70, the new balance is -130, not 10.

Another example: client owes 100, client sends check for 160, new balance is -60. Company renews monthly subscription (which is 80 dollars a month), so now the balance is 20. Client sends a check for 20, balance is 0.

You simply need to look at it as credits/debits. Anything the client sends is considered a payment (debit -) to the balance. Anything the company charges for a given service is a credit (+).

Everyone seems to be fixed on the terms used and that’s my fault for not explaining better. Where this error is happening is behind the scene when product is changed from one to another, and maybe a simple example might help.
100 original order
80 payment
20 balance due

Normal apply CREDIT to account would use NEWBALNCE = OLDBALNCE-CREDIT; or 15 = 20-5;
So if I were to apply this $5 credit to their account their balance is $15
So in this case the “term CREDIT” means we are giving the client money or credit towards their balance due. For example $10 OFF or something.

The situation that has come up is that because of product change to one of lesser value where a their balance might be a negative value. Again we owe the client a credit for this difference and using the same interface, because of the negative value, the new balance is shifted in the wrong direction. Using the same 20 - 5 only the 20 is now negative.
NEWBALNCE = OLDBALNCE-CREDIT; or -25 = -20-5;
So if you are following the logic flow here the credit applied to the account should reduce the balance the client owes in both cases.
15 = 20-5
-15 = -20+5 instead of -25 = -20-5

Note transactions are all stored in the separate columns charges, payments, credits and balance but it’s the calculation of the balance that is getting thrown off.

I think you missed our point. Our point wasn’t necessarily the terminology, rather it was you can’t base your equation on whether the balance is currently positive or negative. You have to base it on whether or not you are processing a payment from the client or a charge from the company.

It has nothing to do with processing a payment from a client, it’s about getting the correct balance on the account when doing order change transactions and applying credit to their account.

The issue is one of applying the proper sign to each item. It it’s an “asset” or a “debit” it is in the debit column: use plus sign. If its a “liability” or a “credit” it is in the credit column: use minus sign. Then everything falls into place automatically.

[table=“width: 300, align: left”]
[tr]
[td]new A/R balance[/td]
[td]=[/td]
[td]balance[/td]
[td]-[/td]
[td]payment[/td]
[/tr]
[tr]
[td]100[/td]
[td]=[/td]
[td]1000[/td]
[td]-[/td]
[td]900[/td]
[/tr]
[tr]
[td]-1900[/td]
[td]=[/td]
[td]-1000[/td]
[td]-[/td]
[td]900[/td]
[/tr]
[/table]
[table=“width: 300, align: left”]
[tr]
[td]new A/P balance[/td]
[td]=[/td]
[td]balance[/td]
[td]-[/td]
[td]payment[/td]
[/tr]
[tr]
[td]-100[/td]
[td]=[/td]
[td]-1000[/td]
[td]-[/td]
[td]-900[/td]
[/tr]
[tr]
[td]1900[/td]
[td]=[/td]
[td]1000[/td]
[td]-[/td]
[td]-900[/td]
[/tr]
[/table]

Regardless of whether a balance is positive or negative you would still treat it the same when you issue a credit to their account.

So if you are storing what they owe you: 100, and you want to credit their account 20 dollars, you will subtract 20, thus the new balance is 80 (think 100 + -20).
If their balance is -20 (because they overpaid, thus you owe them) and you want to update their account because you sent them a check for 20, you would add 20, thus the new balance is 0 (think -20 + 20).

However, read those statements closely, they don’t deal with checking whether the balance is positive or negative, they are denoting whether you are putting money in or taking money out. It purely is based on knowing whether you are applying a credit (deposit) or a debit (withdrawal) to the balance. Both of the above operations were additive, one was taking 20 dollars off the balance (negative 20), the other was putting 20 in the balance (positive 20).

I thank you all for your patience with me. Seems the real flaw in my thinking was handling all credits with subtraction from the balance, where some cases addition is called for and really all should be handled with addition with positive and negative amounts as needed. Thank you again.

Geeks arguing about how $$$ and Accounting work.

This thread is hilarious… :lol:

Debbie

Hi Debbie,
Glad to have brought you a laugh at my expense. Have a good one.