我必须根据这些方面的费率结构来计算价格:
$303.00 fixed price up to 500 units
$0.023 additional per unit from 501-10,000 units
$0.022 additional per unit from 10,001-25,000 units
$0.021 additional per unit from 25,001-50,000 units
我在设置数据库结构和算法(更大的粘滞点)时有点迷失了。有没有人这样做过?有没有一种漂亮,优雅的方式来计算这种东西?
编辑作为一个例子,前500个单位的25,100单位运行费用为303.00美元,接下来的9,500单位为218.50美元,接下来的15,000单位为330.00美元,接下来的100个单位为2.10美元,总计$ 853.60。
它不会是一个简单的25,100 * $ 0.021计算 - 我很清楚如何选择和计算它。
与所得税的评估方式类似 - 在边际基础上。
答案 0 :(得分:3)
我认为你想要一些灵活的东西,否则硬编码会很简单。
您可以使用定价表:
ID MAX FIX UNIT
1 500 303 0
2 9500 0 .23
3 15000 0 .22
4 25000 0 .21
然后你可以按如下方式计算:
$items = ?;
$cost = 0;
$rows = get_rows("select max, fix, unit from pricing order by id asc");
foreach ($rows as $r)
{
if ($items <= 0)
break;
$cost += $r['fix'] + min($r['max'], $items) * $r['unit'];
$items -= $r['max'];
}
我假设您想要PHP中的算法。
答案 1 :(得分:2)
的Python
from collections import namedtuple
RateRule= namedtuple( 'RateRule', ['qty_band','fixed','per_unit'] )
rate_table = [
RateRule(500, 303, None),
RateRule(9500, None, 0.023),
RateRule(15000, None, 0.022),
RateRule(25000, None, 0.021)
]
def total_price( units, rate_table ):
# Base
total = rate_table[0].fixed
units_purchased_so_far = rate_table[0].qty_band
# Whole Price Bands
rule = 1
while units > units_purchased_so_far + rate_table[rule].qty_band:
total += rate_table[rule].qty_band * rate_table[rule].per_unit
units_purchased_so_far += rate_table[rule].qty_band
rule += 1
# Units within the top Price Band
if units > units_purchased_so_far:
total += (units - units_purchased_so_far) * rate_table[rule].per_unit
return total
答案 2 :(得分:0)
这样的事情:
Product
-------
[PK] ProductID
Price
-----
[PK] PriceID
[FK] ProductID
Price
QtyMin
QtyMax
如此有效地产品与价格之间的一对多关系。如果您需要统一费率,无论数量多少,都可以使用最大值的哨兵值。
答案 3 :(得分:0)
SELECT
CASE is_fixed_price
WHEN 1
THEN unit_price / ?
ELSE
unit_price
END
FROM rate_structure
WHERE ? BETWEEN min_qty AND max_qty
在哪里?是您的客户想要订购的数量。对于mysql 5.x,我的头顶语法。这样做的副作用是潜在的舍入错误累积。
答案 4 :(得分:-1)
我做了什么:
size units fixed per
1 500 303.000 0.000
1 10000 0.000 0.023
1 25000 0.000 0.022
1 50000 0.000 0.021
function calculate_price($size, $quantity) {
global $db;
$price = 0;
$count = 0;
// fetch rates from the database
// note: $size is already sanitised by the calling function
$query = "SELECT units, flat, per FROM rates WHERE size={$size} ORDER BY units ASC";
$result = $db->query($query);
// step through the rates
while($rate = $result->fetch_object()) {
// figure out how many of our units fall within this tier
$tier_count = max(0, min($quantity - $count, $rate->units - $count));
// calculate the price for this tier, including any flat rate
$tier_price = $rate->flat + ($rate->per * $tier_count);
// add tier price and count to the totals
$price += $tier_price;
$count += $tier_count;
// store the last, largest number of units rate for any leftovers outside our tiers
$last_rate = $rate;
}
// if some of our units fall outside our defined tiers, use the last tier's values for them
if($count < $quantity) {
$tier_count = $quantity - $count;
$tier_price = $last_rate->flat + ($last_rate->per * $tier_count);
$price += $tier_price;
$count += $tier_count;
}
return $price;
}