PHP calculate suggested price value - php

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.

Related

I need a solution to find cost price ,tax,commission from a total amount in php

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;

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