Problems calculating balances...


#1

The Problem…

If a user signs up for a free account, they are charged $2 plus $1 per pic they add. Or they can pay $5.95 for a full account. And so far everything works just fine. However… if a user say pays for a free account, uploads one pic… pays the $3… then later decides to upgrade… It tells them they only have to pay $2.95 because its taking the $3 they already paid into account. (its not supposed to) - also this is resulting in some cases where the account balance is reading negative, which then causes half the profile buttoms to vanish. (not real sure why) Anyway, I’ve looked at it until I am blue in the face, and I am pretty sure that if I can get it to stop deducting what they already previously paid when they first got the account when they upgrade, that the button problem will more or less fix itself.

This is the code used to calculate the balance… any ideas?

function calcBalance($id){
	$accountType = mysql_result(mysql_query("Select custAccountType from customers where custID=$id"),0);
	if ($accountType == 1){
		$balance = "5.95";
	}
	else{
		$sqlst = "Select * from pics inner join pets on petID=picPetID where petCustID='$id' and picDeleted=0";
		//echo "<BR>" . $sqlst . "<BR>";
		$pics = mysql_query($sqlst) or myerror("Getting Pic Count");
		$picCount = mysql_num_rows($pics);
		//echo $picCount  . "***<BR>";
		$balance = $picCount;
	}
	$paid = round(mysql_result(mysql_query("Select sum(payAmount) as paid from payments where payCustID='$id'"),0),2);
	//echo "Paid: $paid<BR>";
	$balance = $balance - $paid;
	return $balance;

#2

It looks to me that your next to the last line should be gotten rid of.

[php]
$balance = $balance - $paid;
[/php]

This is where you are subtracting what was already paid with what is apparently owed.

If they are “Upgrading” and you don’t wish to allow previous payments applied, then you should just set the balance owed to 5.95.

If you end up with “Negative Numbers” and you don’t want them you can either make use of the abs function http://php.net/abs or you can just put an if statement it.

[php] if ($balance < 0) $balance = 0;[/php]


#3

That fixed it! Thanks!

one down, one more problem to go… but thats a post for another thread.