simple logic for calculating discount price depending on quantity [closed] - php

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);

Related

PHP calculate suggested price value

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.

Weighted Average Calculation Based on Different Criteria

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).

Change magento2 price priority (Original price , Special price , Tier Prices , Group prices)

We know that magento2 has multiple prices type , for example special price, tier prices, group prices , if one prodcut has multiple price , the magento2 will show the minimum price .
Scenario 1:
Regular Price = 250
Special Price = 200
Group Price (NOT LOGGED) = 180
*** Result in product page: 180
Scenario 2:
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180
*** Result in product page: 120
So my question is how to ignore the minimum price , i want to set the group price highest priority , like below sample , i want to show price is 180
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180

Calculation Of tax with different product taxes rates php sql

I have following table "Cart" & contains the following columns in mysql:
CartID, ProductID, ProductPrice, ProductTax (%), ProductQty
ProductTax is in Percentage but value stored as number/int.
How do i calculate total tax of all products in the cart.
So each product must use its own "ProductTax" Rate ie. in %tage .
(1) example contain 1 product in cart.
ProductPrice: 200 & ProductTax 18%. & ProductQTY:1
So "totaltax" here is: 36.
(2) example contains 2 products in cart
1- ProductPrice: 300 & ProductTax 18% & ProductQTY: 2
2- ProductPrice: 400 & ProductTax 20% & ProductQTY: 1
So for above example "totaltax" is : 108+80= 188
To calculate total tax of all products in the cart:
SELECT SUM((ProductPrice * ProductQty)* ProductTax/100) AS value_sum
FROM Cart
This solves it! – Webgen 10 secs ago edit

Calculating Discount Percentage of a given discounted price

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

Categories