The dilemma:
I have options that need to store pricing data.
Each option can have a different formula to determine it's cost.
E.g., one option's total cost will be cost * sq feet, but another option will be cost * perimeter feet, while another option still will simply be a fixed cost.
These options are being pulled from the database by several sites that my company owns.
Each one of those sites needs to be able to calculate the options pricing in the exact same way.
We currently have PHP code on each site that pulls data from the database, and then determines how to calculate it.
The problem with this method is that we will sometimes change how a price is calculated, and when that happens, we have to edit the pricing module repository and then update it to each server that hosts one of these sites.
I'm looking for a better solution.
I'm toying with the idea of storing a formula in the option record in the database, then each site would simply need to call the option, apply data to the formula stored there, and then run the formula.
Now, this can be done with eval()
, but I don't really want to do it that way.
Eval example to give you a clearer idea of what I'm trying to do:
$product->width = 3;
$product->length = 4;
$formula = $option->cost_formula; // $product->width * $product->length * $option->cost
eval($formula);
Does anyone else have any other solution?
Use functions.
Pseudo-code:
CREATE FUNCTION calculation1 (@cost decimal(6,2), @feet int)
RETURNS decimal(7,2)
AS
BEGIN
--Other formula calculations
RETURN @cost * @feet
END
You would then call the function from your query: SELECT calculation1('1.50', 15)...
This way you can update any changes to your function and queries using the function will produce the same result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With