Jump to content

[SOLVED] Help With Equation


dlebowski

Recommended Posts

Here is what I need.  I have a customer who gets charged an addition 1% for purchases they make with cash and 2% charge for using a credit card on top of their items purchased total.  So if they bought $100 worth of goods and used cash, they would get charged an additional $1 for using cash as a form of payment.  This would give them a grand total of $101.  There are some cases where a customer may use a credit card for some of the payment and cash for the rest.  I do not want them to have to pay the addition charges twice.  For example, if someone owes $75 for stuff they bought.  They want to put $38 in cash and the rest on their credit card.  I need to know what the total was for the $38 cash payment BEFORE the 1% was added on.  I need to know this so I can get an accurate amount for what the rest of the base amount owed is for the credit card and figure the 2% onto it.  How would I use .php to come up with the amounts?  I have spent days trying to figure this out and am at a loss.  This is probably something one of you could rattle off in your head fairly quickly.  Thanks a lot for your help.

 

Ryan

Link to comment
Share on other sites

This isn't a PHP problem, this is simple math. If you were able to accurately explain the problem I'm sure you could write the code for it. But, from the above description you didn't even follow through with the last example.

 

For example, the total is $75 and the customer is paying $38 in cash. It's not clear if the 1% applies to the total or the $38. Same goes for the 2% for the CC payment - does it apply to the remainder ($37) or to the total? If you were to simply show one solution, creating the code would be a snap.

 

And, while were on the subject of code, why didn't you supply the code you have created so far? This is obviously a homework problem (no one charges extra for a cash payment). It wouldn't be right for anyone to provide you the answer without showing that you have at least attempted it.

Link to comment
Share on other sites

I will give you hint though. Keep all the amounts separate. In other words have a value for the total, another value for the cash payment, another for the CC payment, another for the cash charge and a last one for the CC charge. When figuring complex order totals where the products are coming from different locations, different tax rates can apply to different portions for the total. This is done by keeping all the parts separate.

Link to comment
Share on other sites

Let me start off by apologizing.  I am fried right now from trying to work on this and wasn't thinking straight when I started this post.  I didn't mean to make it sound like I was just wanting you to come up with the solution.  I just was making sure what I was trying to do was even possible.  I actually posted the code in another post and didn't get any responses.  So either it was a totally stupid question or my request was hopless. 

 

Below are the two pieces of code.  Let me know if I can provide anything else.  I have been working on this for three days and just cannot get it. Thans for all your help.

 

<?
<?
                                                include("dbinfo.inc.php");
                                                mysql_connect("localhost",$username,$password);
                                                @mysql_select_db($database) or die( "Unable to select database");
                                            
                                               // POST value of id that sent from form submission
                                               $PaymentAuctionDate=$_POST['pay_LotAuctionDate'];
                                                                                           
                                               $query="SELECT * FROM auctions WHERE AuctionDate='$PaymentAuctionDate'";
                                               $result=mysql_query($query);
                                               $percent= "%";
                                                
                                                while($row = mysql_fetch_array( $result )) {
                                                    
                                                    
                                                    
                                               $BuyerCashPrem = ($row['BuyerCashPrem']/100);
                                               $BuyerCheckPrem = ($row['BuyerCheckPrem']/100);
                                               $BuyerCreditPrem = ($row['BuyerCreditPrem']/100);
                                               
                                               $CheckPaid = ($checking * $BuyerCheckPrem);
                                               $CreditPaid = ($charge * $BuyerCreditPrem);
                                               $CashPaid = ($cash * $BuyerCashPrem);
                                               
                                               $CashPaid = substr($CashPaid, 0, ((strpos($CashPaid, '.')+1)+2)); 

                                               
                                               ?>
                                                <td style='color: black; text-align: left; width: 100px; font-size: 12px; border-collapse: collapse; border-spacing: 0px; order-spacing: 0px; padding: 0pt;'>PAYMENT PREM:</td>
                                                 <TD width="1"></td>
                                                 <?  
                                                  $grandtotal = (($sum + $absenteebid + $taxtotal+ $BuyerCashPrem)-(($cash - $CashPaid)+($check - $CheckPaid)+($credit - $CreditPaid)));
                                         $cashprem=($grandtotal * $BuyerCashPrem);
                                         $cashprem = substr($cashprem, 0, ((strpos($cashprem, '.')+1)+2)); 

                                         ?>
                                         
                                                 <TD class=grid><? echo $row['BuyerCashPrem'];echo $percent;?>  =  <? echo "$cym$cashprem";?></td>
                                                 <TD width="1"></td>
                                                <? 
                                                  $grandtotal = number_format((($sum + $absenteebid + $taxtotal)-(($cash - $CashPaid)+($checking - $CheckPaid)+($charge - $CreditPaid))),2,".",",");
                                         $checkprem=number_format(($grandtotal * $BuyerCheckPrem),2,".",",");
                                         ?>
                                                 <TD class=grid><? echo $row['BuyerCheckPrem'];echo $percent;?>  =  <? echo "$cym$checkprem";?></td>
                                                 <TD width="1"></td>
                                                <? 
                                                  $grandtotal = number_format((($sum + $absenteebid + $taxtotal)-(($cash - $CashPaid)+($checking - $CheckPaid)+($charge - $CreditPaid))),2,".",",");
                                         $creditprem=number_format(($grandtotal * $BuyerCreditPrem),2,".",",");
                                         ?>
                                                 <TD class=grid><? echo $row['BuyerCreditPrem'];echo $percent;?>  =  <? echo "$cym$creditprem";?></td>

 

<? if ($Exempt=="YES")
                                        {
                                         $grandtotal = (($sum + $absenteebid));
                                         $AllPaymentTotal = (($cash + $checking + $charge));
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         print "$cym";
                                         print $amountdue;
                                        }
                                        else
                                        {
                                      $grandtotal = ($sum + $absenteebid + $taxtotal + $cashprem);
                                        $AllPaymentTotal = (($cash + $checking + $charge)-($CashPaid+$CheckPaid+$CreditPaid));
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         $amountdue = $amountdue;
                                         print "$cym";
                                         print $amountdue;
                                        }
                                        
                                    ?>
                                </td>
                              
                                <TD width="1"></td>
                                <td class=grid>
                                <? if ($Exempt=="YES")
                                        {
                                         $grandtotal = (($sum + $absenteebid + $checkprem));
                                         $AllPaymentTotal = (($cash + $checking + $charge));
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         print "$cym";
                                         print $amountdue;
                                        }
                                        else
                                        {
                                          $grandtotal = ($sum + $absenteebid + $taxtotal + $checkprem);
                                        $AllPaymentTotal = (($cash + $checking + $charge)-($CashPaid+$CheckPaid+$CreditPaid));
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         $amountdue = number_format($amountdue,2,".",",");
                                         print "$cym";
                                         print $amountdue;
                                        }
                                    ?>
                                </td>
                                                 <TD width="1"></td>
                                               <td class=grid>
                                <? if ($Exempt=="YES")
                                        {
                                         $grandtotal = ($sum + $absenteebid + $creditprem);
                                         $AllPaymentTotal = ($cash + $checking + $charge);
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         print "$cym";
                                         print $amountdue;
                                        }
                                        else
                                        {
                                          $grandtotal = ($sum + $absenteebid + $taxtotal + $creditprem);
                                        $AllPaymentTotal = (($cash + $checking + $charge)-($CashPaid+$CheckPaid+$CreditPaid));
                                         $amountdue = ($grandtotal - $AllPaymentTotal);
                                         $amountdue = number_format($amountdue,2,".",",");
                                         print "$cym";
                                         print $amountdue;
                                        }
                                    ?>

Link to comment
Share on other sites

I have isolated the problem to this.  When someone makes a payment via cash, credit card, or check the values are stored in the database individually and then retried in the values $cash, $charge, and $checking.  The problem is that I need to figure out how much of the payment was the "premium" and how much came off of the base total.  So, $100 at 1% would be an additional $1.  So the table would have $101 entry in it.  Well, when I retrieve that payment in the queries below, I need to know how much of that is the premium and how much is from the base total. This will allow me to figure out how much the credit card premium or the check premium will be with the remaining base total.  I think the equation to find that would be Premium = PaymentTotal - (PremiumPercentage x PaymentTotal).  I'm just not sure how to implement that.  Thanks for any help you can provide.

Link to comment
Share on other sites

Again, give me a complete example using your 2nd scenario above with a $75 total and the user paying $38 in cash and $37 using a CC. I don't understand if the premiums apply only to the partial payments or to the total. Show me what all the values are supposed to be.

Link to comment
Share on other sites

Again, give me a complete example using your 2nd scenario above with a $75 total and the user paying $38 in cash and $37 using a CC. I don't understand if the premiums apply only to the partial payments or to the total. Show me what all the values are supposed to be.

 

$75 Total Items Purchased

1% Buyers Premium for Cash

2% Buyers Premium for CC

 

$38 for Cash is actually = $37.63 + .37 (1% Buyers Cash Premium)

 

$75 Total Items Purchased

-$37.63 Cash

------------

$37.37 Amount Left To Pay on CC Before 3% CC Premium

 

$37.37 + $1.12 (3% Buyers Credit Card Prem) = $38.49

 

When all is said and done, he paid $38.49 on his CC and $38 in cash.

 

Thanks for having a look again.

Link to comment
Share on other sites

this is, for the most part, a simple math problem:

 

totalCash = 1.01*actualPayment

 

rearranging gives:

 

actualPayment = totalCash/1.01

 

therefore to figure out the portion of cash actually applied against the purchase price, divide the total cash payment by 1.01.  the premium paid on that cash payment is simply the totalCash - actualPayment, as you might expect.  be sure to stay consistent with your rounding here.

 

the rest is merely a matter of changing the premium fraction.  as mjdamato said, it's pretty difficult to offer an exact solution because i have no idea what a typical set of data would look like.

Link to comment
Share on other sites

Honestly.  I think you just figured it out for me.  I never have been outstanding at math and I kept wanting to use .01 instead of 1.01.  I think that may have just taken care of my entire problem.  So far I have used that and it appears to work on every number I have used it on.

Link to comment
Share on other sites

OK, with your last example fully explained this is very easy. As I said, just keep all the values separate and just walk through the example posted. Assuming you know the order total (before payments and premiums) and the total cash payment (with premium) the following code will calculate all the other values

 

<?php

//defined vars
$premium_cash = .01;
$premium_cc = .03;

//Input (known) values
$purchase_total = 75;
$cash_total = 38;

//Calculate remaining values
$cash_subtotal = round(($cash_total / (1+$premium_cash)), 2);
$cash_premium = ($cash_total - $cash_subtotal);

$cc_subtotal = ($purchase_total - $cash_subtotal);
$cc_premium = round(($premium_cc * $cc_subtotal), 2);
$cc_total = ($cc_subtotal + $cc_premium);


//output the results
echo "Order Total Purchase: $" . number_format($purchase_total, 2, '.', ',');
echo "<br><br>";
echo "Customer Paid in cash: $" . number_format($cash_total, 2, '.', ',');
echo "<br>";
echo "  Cash Payment towards total: $" . number_format($cash_subtotal, 2, '.', ',');
echo "<br>";
echo "  Cash Premium (".($premium_cash*100)."%): $" . number_format($cash_premium, 2, '.', ',');
echo "<br><br>";
echo "Customer Paid with CC: $" . number_format($cc_total, 2, '.', ',');
echo "<br>";
echo "  CC Payment towards total: $" . number_format($cc_subtotal, 2, '.', ',');
echo "<br>";
echo "  CC Premium (".($premium_cc*100)."%): $" . number_format($cc_premium, 2, '.', ',');

?>

 

Output

Order Total Purchase: $75.00

Customer Paid in cash: $38.00
  Cash Payment towards total: $37.62
  Cash Premium (1%): $0.38

Customer Paid with CC: $38.50
  CC Payment towards total: $37.38
  CC Premium (3%): $1.12

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.