Jump to content

Calculate result based on separate total costs displayed


imkesaurus
Go to solution Solved by Jacques1,

Recommended Posts

Hi

 

The user has 3 choices of either calculating A - transfer costs, B - bond costs or C - Ancillary costs which is A + B + C (C is just a fixed value added to the final costs of A + B)

The user enters their purchase price which then shows all the costs for that option. I would like to know what would be the best way to calculate C?

Here is my code so far (please ignore the fact that I don't have prepared statements yet, still trying to figure those out :) ):

 

HTML:

<h1>Transfer Cost Calculator</h1>
<form action="calculate_cost.php" method="post">
Property Purchase Price: <input type="text" name="ppprice">
<input type="submit" value="Calculate">
</form>

<h1>Bond Cost Calculator</h1>
<form action="calculate_cost.php" method="post">
Property Purchase Price: <input type="text" name="pppriceb">
<input type="submit" value="Calculate">
</form>

<h1>Ancillary Cost Calculator</h1>
<form action="calculate_cost.php" method="post">
Property Purchase Price: <input type="text" name="pppricec">
<input type="submit" value="Calculate">
</form>

PHP

if(isset($_POST['ppprice'])){ 

$ppp = $_POST['ppprice'];
echo $ppp . "<br>";

$sql = "SELECT *
FROM
  transfer_cost
ORDER BY
  ABS(purchase_price - $ppp) ASC,
  purchase_price ASC 
LIMIT 1
;";
$result = $con->query($sql);


while($row = $result->fetch_assoc()) {
   echo "Price: " . $row["purchase_price"]. " Duties: " . $row["transfer_duties"]. " Transfer: " . $row["transfer_fee"]. " Deeds: " . $row["deeds_office_fee"]. " VAT:" . $row["vat"]. " Total: " . $row["total_transfer_costs"]. "<br>";
	
	$VAT = $row["vat"] + 238.6;
	$ttotal = $row["total_transfer_costs"] + 1697.4;
	$totalcost = $VAT + $ttotal;
	
	echo $VAT . "<br>" . $ttotal . "<br>" . $totalcost;    
   }
}

elseif(isset($_POST['pppriceb'])){ 

$pppb = $_POST['pppriceb']; 
    echo $pppb . "<br>";
	$sql = "SELECT *
FROM
  bond_cost
ORDER BY
  ABS(purchase_price_b - $pppb) ASC,
  purchase_price_b ASC 
LIMIT 1
;";
$result = $con->query($sql);


while($row = $result->fetch_assoc()) {
   echo "Price: " . $row["purchase_price_b"]. " Reg Fee: " . $row["reg_fee"]. " Deeds: " . $row["deeds_office_fee_b"]. " VAT:" . $row["vat_b"]. " Total: " . $row["total_bond_cost"]. "<br>";
	
	$VAT = $row["vat_b"] + 238.6;
	$ttotal = $row["total_bond_cost"] + 1697.4;
	$totalcost = $VAT + $ttotal;
	
	echo $VAT . "<br>" . $ttotal . "<br>" . $totalcost;
	
    
   }

}

I'm not sure if I should just run both statements again and calculate from there, but that seems like a lot of unnecessary code. If there is an easier way of doing it that would be wonderful.

 

Thank you!

Link to comment
Share on other sites

I don't quite understand how the functions will work though, I only get a total transfer or bond cost if the user enters it in one of the first two boxes, so if they input in the 3rd box then the first two statements won't be executed right? 

Or do I work the functions in differently?

Link to comment
Share on other sites

 

 

(C is just a fixed value added to the final costs of A + B)

 

If that is the case, then it would be impossible to determine C without the known costs of both A and B.

If you know the costs of A or B when the user selects either one of those values then you, as Jacques1 stated, call one function which calculates C based on the return values of calculate_transfer_costs() and calculate_bond_costs()

Link to comment
Share on other sites

  • Solution

imkesaurus, it might make sense to forget about the user interface for now and just look at the logic:

 

You want to be able to obtain one of three values: the transfer costs, the bond costs or the ancillary costs. All of those depend only on the purchase price, and the last one is derived from the other two. So why not create one function for each value? The function takes a purchase price (and a PDO object), selects the required data from the database, calculates the cost and then returns the result.

 

For example:

function calculate_transfer_costs($purchase_price, PDO $database_connection)
{
    $transfer_costs_stmt = $database_connection->prepare('
        SELECT
            transfer_duties,
            transfer_fee,
            deeds_office_fee,
            vat,
            total_transfer_costs
        FROM
          transfer_cost
        ORDER BY
          ABS(purchase_price - :purchase_price) ASC,
          purchase_price ASC
        LIMIT
          1
    ');
    $transfer_costs_stmt->execute(['purchase_price' => $purchase_price]);
    $transfer_costs = $transfer_costs_stmt->fetch();

    // TODO: use constants instead of hard-coded numbers
    $vat = $transfer_costs['vat'] + 238.6;
    $total = $transfer_costs['total_transfer_costs'] + 1697.4;

    return [
        'transfer_duties' => $transfer_costs['transfer_duties'],
        'transfer_fee' => $transfer_costs['transfer_fee'],
        'deeds' => $transfer_costs['deeds_office_fee'],
        'vat' => $transfer_costs['vat'],
        'total_transfer_costs' => $transfer_costs['total_transfer_costs'],
        'total' => $total,
        'total_costs' => $vat + $total,
    ];
}

Now you can call those functions and calculate the costs at any time in any order, no matter which button your user clicked.

  • Like 2
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.