PHP incremntal Price Calculator based upon quantity

Arjun Paudyal

I am trying to write a script that calculates a price based upon the quantities.

Database :
pid | prodict | unit | price
1 | coke   |100 | 2
2 | coke   |100 | 1.8
3 | coke   |300 | 1.5
4 | coke   |       | 1.1
1 | pepsi   |100 | 2.2
2 | pepsi   |50 | 2
3 | pepsi   |       | .8

Pid is product id, Product is Product name/Product code. Unit is Incremental unit, or in addition to the existing unit by the number, price is price per unit for incremental unit.

Real Problems:

  • if unit is '', or Null, that is that identifies the maximum cumulative unit and then the price in that null column applies to that.
  • Not all the products have same level of price. some might end up with 1 value (flat rate, in that case, unit would be null or ''. some might go as up as possible.
  • There is no cumulative column and i can not add that. It is not there, and is not possible to add now becuase products might be in millions (asssumption, to make the code flexible for unlimited products)
  • All you would get is a lumpsum of units and product code. eg. 100 units of coke, or 200 units of pepsi.
  • Number in unit column is Inclusive, that is Less than equal to.


I was a big fan of For loop and break(bad programming), but now i think i need either if condition or while loop, for both i do not have much confidence.

Thank you in advance

NoteIf you feel difficult to understand the problem, then just assume the income tax calculator, same or similar thing - upto x amount, base tax, then for NEXT y amount, y tax rate, for next z amount, z tax rate more than z, z+ tax

Scopey

Well, you certainly want to loop through the products first, and then work out a total based on the quantity passed in. Something like this?

// Using PostgreSQL as an example here
$entries = pg_fetch_all(pg_query('SELECT * FROM database ORDER BY prodict, pid ASC'));

// Have the quantities ready;
$quantities = array(
    'coke' => 1024,
    'pepsi' => 512,
);

// Prepare an array to hold the total values.
$totals = array();

// Loop through the entries in the array in a non-conventional way
while($entry = current($entries)) {
    // Get the name of the product from the array
    $product = $entry['prodict'];
    // And the quantity
    $quantity = $quantities[$product];
    // Prepare a price for this product starting at zero
    $price = 0;
    // Now use a do-while to figure out the price
    do {
        // At this point '$entry' contains information about the pricing for the first 'x' quanitity
        $quantityAtThisPrice = $entry['unit'];
        // Check the price
        $priceForThisUnit = $entry['price'];
        // Check we have any quantity remaining of this product
        if($quantity > 0) {
            // Check if the quantity at this price is null or if that quantity at this price is more than what we have left
            if($quantityAtThisPrice === null || $quantityAtThisPrice > $quantity) {
                // This means the rest of the quantity is at this price
                $price += ($quantity * $priceForThisUnit);
                // No more quantity left to price
                $quantity = 0;
            }
            // Otherwise we'll add for this unit, and move on to the next
            else {
                // Add to the price
                $price += ($quantityAtThisPrice * $priceForThisUnit);
                // Subtract the quantity we just priced
                $quantity -= $quantityAtThisPrice;
            }
        }
        // Now fetch the next entry
        $entry = next($entries);
    } while ($entry['prodict'] === $product);

    // Add the calculated price to the totals array
    $totals[$product] = $price;
}

var_dump($totals);

Got a bit carried away but I think that should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calculating quantity and price total in react

Applying sliding scale price in R based on quantity

Efficient database design for multiple price based on quantity and attribute

Total price calculation by quantity

Total price and Quantity in Javascripts

Change WooCommerce cart item price based on a custom field and quantity threshold

Extract Quantity and price from text

how to count the price of select option based on its quantity with javascript

Group by isnt calculating quantity and price?

PHP product price and quantity does not multiply

javascript to multiply product price and quantity

How to create checkbox-based price calculator?

Javascript Price Calculator Issues

HTML PHP Adding quantity and Price to my Menu items

Add a price with the chosen quantity - JavaScript

Sum Quantity and Price On Specific Condition

Dynamic textbox that calculates price based on change in quantity or item

Update the price based on quantity picked from select menu per form

Php calculator quantity multiplied to specified option

rails Calculation (price * quantity)

Update woocommerce price of a product based on php

jQuery recipe quantity calculator

How to update dynamic price based on quantity using vanilla JavaScript?

How to create a total quantity based upon multiple criteria from 2 arrays

Deserializing array of [price, quantity]

Calculate the price based upon inserted date in php

jQuery price calculator - change price based on value from slider

Calculate total price (items quantity * items price)

Condition for Quantity and Price for Woocommerce