I am working on a reverse auctioning system in PHP. My customers get bids from different vendors against their requested products/services. While displaying received bids to the logged in customer, I need to do a sorting based on weighted average against following criteria:
Price (40%) -> Lowest price is good.
Vendor Rating (20%) -> Rating = 1-5 (5 being best).
Delivery Options (10%) -> More delivery options offered the best.
Distance (10%) -> Lowest distance from customer location is the best.
Payment Methods (10%) -> More payment methods offered the best.
So far I have been able to create following formula:
$weightage = ($price*.40) + ($rating*.20) + ($delivery_options*.10) + ($distance*.10) + ($payment_methods*.10);
I need to show bids having highest weightage value on top. I am confused about the addition/subtraction of the weightage based on what is best for customer i.e if price is lower then this should be considered best for customer and should I add weightage or subtract weightage?
Any help would be appreciated. Thanks
I got it sorted out. Here's what I did:
Quote #1 (Best Quote)
Price: 120
Rating: 5
Delivery Options: 2
Distance: 2000 km
Payment Methods: 5
Availability: 1
Price Factor = 1 - ( (Price - Min Price) / (Max Price - Min Price) ) = 1
Rating Factor = ( (Rating - Min Rating) / (Max Rating - Min Rating) ) = 1
Delivery Options Factor = ( (Delivery Options - Min Delivery Options) / (Max Delivery Options - Min Delivery Options) ) = 1
Distance Factor = 1 - ( (Distance - Min Distance) / (Max Distance - Min Distance) ) = 1
Payment Methods Factor = ( (Payment Methods - Min Payment Methods) / (Max Payment Methods - Min Payment Methods) ) = 1
Availability Factor = ( (Availability - Min Availability) / (Max Availability - Min Availability) ) = 1
Quote #1 Weightage = (Price Factor * 40%) + (Rating Factor * 20%) + (Delivery Options Factor * 10%) + (Distance Factor * 10%) + (Payment Methods Factor * 10%) + (Availability Factor * 10%) = 1
Apply same formula to other quotes, you'll get weightage for each quote, and then you can sort quotes easily (Order by Quote Weightage Asc/Desc).
Related
I am badly stuck for couple of weeks for a solution.Suppose I have a total price of Rs. 1000, And the breakup of this amount is costprice+10% commission of the costprice+18% TAX of the costprice=1000.
So i am finding a solution to get costprice,commission,tax in PHP. Please help me out!
This is not a programming problem but a simple problem in basic arithmetic.
The total of cost price plus commission plus tax is the selling price.
With the numbers provided we get
cost price * 1 plus
commission: cost price * 0.1 plus
tax: cost price * 0.18
-----------------
selling price
From this we can see that the selling price is 1.28 * cost price.
Now we can work backwards:
cost price = selling price / 1.28
tax = cost price * 0.18 = (selling price/1.28) * 0.18
commission = cost price * 0.10 = (selling price/1.28) * 0.1
Converting that to PHP:
<?php
$sellingPrice = 1000;
$taxPercent = 0.18;
$commPercent = 0.10;
$costPrice = $sellingPrice/(1+$taxPercent +$commPercent);
$taxAmount = $costPrice * $taxPercent;
$commAmount = $costPrice * $commPercent;
I have below variable
quantity ($qty)
purchase price ($pp)
sale price ($sp)
commission 12% ($comm)
suggested sale price
the commission is calculated on the sale price for example if the sale price increase commission will increase too.
I want to calculate the Suggested sale price on a 10% profit ratio ($pr)
here is my code
$comm=12; //(commission 12%)
$pr=10; //(10% profit)
$percent = ((float)$sp/100)*(float)$comm;
$fees=round($percent,2);
$total=((float)$pp*(float)$qty)+(float)$fees;
$profit = ((float)$total/100)*(float)$pr;
$suggested = round(((float)$profit)+((float)$total),2);
I am calling this ajax function on button click, this will give few times different values on button click.
I think this is because when the sale price increase commission also increased based on that sale price.
there is no error in code only issue is it did't calculate correct value of sale price on first call, it need 2 or 3 calls to get correct value. how can I get correct value in first call.
First, a disclaimer: I’m a programmer, not an accountant.
So, thinking out loud for a few moments:
Define Terms
cogs: cost of goods sold (item cost)
sp: sales price
comm: commission
profit: profit
Formulas
profit = sales price - cogs - comm
profit = sales price * profitPercentage
comm = sales price * commPercentage
Substitute
sales price * profitPercentage = sales price - cogs - comm
For sake of example, let’s assume profit is 10% and commission is 15%
sales price * .10 = sales price - cogs - comm
we can then substitute commission
sales price * .10 = sales price - cogs - (sales price * .15)
Simplify
.1(sales price) = sales price - cogs - .15(sales price)
cogs = sales price - .1(sales price) - .15(sales price)
cogs = (1 - .1 - .15)(sales price)
cogs = (.75)(sales price)
sales price = cogs/.75
Test
cogs: $10
price: $13.33
comm: $2.00
profit: $1.33
Code
function calculateSalesPrice(float $cogs, float $commPercent, float $profitPercent) : float
{
$divisor = (1 - $commPercent - $profitPercent);
if($divisor <= 0) {
throw new Exception('Commission and Profit exceed 100%');
}
return round(($cogs/$divisor),2);
}
With this formula, both profit and commission use the sales price as their basis.
I am trying to find out the discount amount of a price.
The cost of item WAS £50.00
Sale Price £25.00
Discount = %50
However when use this formula below in PHP it doesnt give me the correct Discount percentage.
$percent = $rowx->Orgprice - $rowx->SalePrice / 100;
$percent = 50 - 25 / 100 = 49.75;
$percent = 50 - 20 / 100 = 49.8;
All above percentages are wrong.
Use this formula for calculating the discount percentage :
Discount%=(Original Price - Sale price)/Original price*100
Translating it into code, it should be something like :
$percent = (($rowx->Orgprice - $rowx->SalePrice)*100) /$rowx->Orgprice ;
The correct formula is 1 - (sale price / original) * 100, so:
$percent = 1 - ($rowx->SalePrice / $rowx->Orgprice) * 100;
$percent = 1 - (25 / 50) * 100 = 50
selling price = actual price - (actual price * (discount / 100))
So for example if (actual price) = $15, (discount) = 5%
selling price = 15 - (15 * (5 / 100)) = $14.25
I hope below code solved your problem :
$percent = 100 * $rowx->SalePrice / $rowx->Orgprice;
echo $percent;
Another option:
Original Selling Price = 15,
Discount = 5%
Original selling price *(1-discount) = sale price,
15*(1-.05)=14.25
If price values are known, but not percentage.
Sale price/Original selling price -1
14.25/15-1=-.05 so 5% discount
I have table Foo(id, name, rateAvg, rateNum). rateAvg is between 1 to 5 and rateNum is number of rates by users.
I query table with mysql and order them by most rated Foos like this:
SELECT * FROM Fooo ORDER BY rateAVG DESC, rateNum DESC
but that is not fair enough, for example one row has rateAvg of 4.8 with 1000 rates and the other with rateAvg of 5 and 10 rates and by my query item two come first.
Edit:
by comment of #kingkero I found out that each of rateAvg and rateNum should have some weight for ordering, how can apply that in my query
You can try to apply a bayesian average, but you should pre calculate this rating and store in one of your fields.
b(r) = [ W(a) * a + W(r) * r ] / (W(a) + W(r)]
r = average rating for an item
W(r) = weight of that rating, which is the number of ratings
a = average rating for your collection
W(a) = weight of that average, which is an arbitrary number,
but should be higher if you generally expect to have more ratings
for your items; 100 is used here, for a database which expects
many ratings per item
b(r) = new bayesian rating
For example in your case:
a = (4.8 * 1000 + 5 * 10) / 1010 = 4.8019
r1 = 4.8
r2 = 5
W(a) = 100 // arbitrary average weight
W(r1) = 1000 // weight of first raiting
W(r2) = 10 // weight of second rating
b(r1) = (100 * 4.8019 + 1000 * 4.8) / (100 + 1000) = 4.8001
b(r2) = (100 * 4.8019 + 10 * 5) / (100 + 10) = 4.8199
So you can see that these values are close to each other and you can try to change average weight to setup this bayesian average rating for your case.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working shopping cart with only one product, in which I have to offer 50% discount for every second item ("for every unit bought at full price, you can get another one at half price").
For example, let's say the product price is $10.
if 1 qty Total Price = $10 ($10).
if 2 qty Total Price = $15 ($10 + $5).
if 3 qty total price = $25 ($10 + $5 + $10).
if 4 qty total price = $30 ($10 + $5 + $10 + $5).
if 5 qty total price = $40 ($10 + $5 + $10 + $5 + $10).
and so on ...
So what would be the logic to find discount depending on quantity of items?
If the base price is unitcost, then the full price would be:
quantity * unitcost
A 50% discount for every second one can be calculated as (half the items, rounded down, multiplied by half the cost):
int (quantity / 2) * (unitcost / 2)
making the final price:
(quantity * unitcost) - (int (quantity / 2) * (unitcost / 2))
The following program (in Python, my language of choice for quick and dirty code samples) shows this in action for your test data:
unitcost = 10
for quant in range(1,10):
print "%2d -> $%d" % (quant, quant*unitcost-(int(quant/2)*unitcost/2))
Using unitcost as 10 as per your example, you get the following costs for each quantity (with my annotations on the right indicating how much the additional unit cost):
1 -> $10 + 10
2 -> $15 + 5
3 -> $25 + 10
4 -> $30 + 5
5 -> $40 + 10
6 -> $45 + 5
7 -> $55 + 10
8 -> $60 + 5
9 -> $70 + 10
Though it looks complicated,works perfectly..
echo $total = (floor($quantity/2)*($price/2)) + (($quantity - (floor($quantity/2)))*$price);