Yii2中的平板电费计算 [英] Slab wise electricity bill calculation in Yii2
问题描述
我正在开发 yii2
.我想明智地计算电费单板.为此,我已经在表格中插入了平板及其各自的费率.现在我想计算一下.
I am working on yii2
. I want to calculate electricity bill slab wise. For this, I have already inserted slabs and their respective rates in a table. Now I want to calculate it.
我的单位是 128.82
,下面是我的平板和它们的价格.
My units are 128.82
and below is my slab and their rates.
我可以使用以下查询来执行平板
I am able to carry out the slabs by using the following query
$mData = \Yii::$app->db->createCommand("SELECT s.`slab_start`, s.`slab_end`, s.`rate`, r.`cust_id`
FROM `mdc_tariff_slabs` s
INNER JOIN `mdc_meter_cust_rel` r ON s.`t_id` = r.`tariff_id`
WHERE r.`cust_id` = $consumer_no")->queryAll();
array(3) { [0]=> array(3) { ["slab_start"]=> string(1) "1" ["slab_end"]=> string(3) "100" ["rate"]=> string(2) "10" } [1]=> array(3) { ["slab_start"]=> string(3) "101" ["slab_end"]=> string(3) "150" ["rate"]=> string(2) "12" } [2]=> array(3) { ["slab_start"]=> string(3) "151" ["slab_end"]=> NULL ["rate"]=> string(2) "14" } }
注意:板块未来可能会有所不同
Note: The slabs may differ in future
我已经尝试了以下代码,根据我的要求设置了slab并试图获得账单
I have tried the following code, set the slabs according to my requirements and tried to get the bill
public static function calcBill($units)
{
if($units < 100){
$bill = $units*10;
}elseif($units > 100 && $units <=150){
$temp = 100*10;
$remaining_units = $units - 100;
$bill = $temp + ($remaining_units *12);
}else{
$temp = (100*10)+ (100*12);
$remaining_units = $units - 150;
$bill = $temp + ($remaining_units *14);
}
echo $bill;
die();
}
账单是1345.84
.
现在我要做的是传递slabs,而不是硬编码它们.我觉得有点难.
Now what I am trying to do is to pass the slabs as well and not to hard code them. I am finding it a bit difficult.
更新 1
我有 3 个平板/单位的限制.每块板都有自己的费率.费率用于计算账单.下面是场景.
I have 3 slabs/units limit. Each slab has its own rate. Rate is used to calculate the bill. Below is the scenario.
- 如果单位 <= 100,则收取 10 卢比/单位
- 如果单位是 >100 & &<= 150 然后收取 12 卢比/单位
- 如果单位是 >150 然后收取 14 卢比/单位
在我的数组中,除了最后一个只有 slab_start
和 rate
之外,我拥有所有三个值.
In my array, I have all the three values except for the last one which has only slab_start
and rate
.
通过对平板和费率进行硬编码,我可以计算账单,但我想在使用数组时进行计算.
By hard coding the slabs and rate I can calculate the bill but I want to calculate while using the array.
所以代码可能看起来像
if($units < slab_end){
$bill = $units*rate;
}elseif($units > slab_start && $units <=slab_end){<=slab_end
$temp = prev_slab_start *rate;
$remaining_units = $units - prev_slab;
$bill = $temp + ($remaining_units *rate);
}else{
$temp = (prev_slab*10)+ (prev_slab*12);
$remaining_units = $units - slab_start;
$bill = $temp + ($remaining_units *14);
}
不知道上面的逻辑对不对.
I don't know the above logic is correct or not.
注意: 大板范围和数量可能会发生变化.所以我想要一个通用的函数来做需要的
Note: Slab range and quantity may be changed. So I want to have a generic function that will do the needful
更新 2
遵循提到的解决方案.
$array = ArrayHelper::map($mData, 'slab_end', 'rate');
var_dump($array);
die();
以上给了我
array(3) { [100]=> string(2) "10" [150]=> string(2) "12" [""]=> string(2) "14" }
然后我添加了一个函数
public function billCalc($input, $slabs)
{
$result = [];
$bill = 0;
$previous_slab = 0;
foreach($slabs as $slab => $rate)
{
// calculate distance between current and previous slab
$slab_distance = $slab - $previous_slab;
// if current remainder of input value is >= distance, add distance to result,
// and subtract distance from remainder of input
if( $input >= $slab_distance )
{
$result[] = $slab_distance;
$bill += $slab_distance * $rate;
$input -= $slab_distance;
}// otherwise, add remainder as last item of result, and break out of the loop here
else
{
$result[] = $input;
$bill += $input * $rate;
break;
}
$previous_slab = $slab;
}
var_dump($result, $bill);
exit();
}
现在我面临一个问题.当输入小于 150
时,结果完全没问题,但当它大于或等于 150
时,我得到以下错误
Now I am facing an issue. When the input is less than 150
the result is perfectly ok, but when it's greater or equal to 150
I am getting below error
遇到非数字值
它给了我这个错误
$slab_distance = $slab - $previous_slab;
任何帮助将不胜感激.
推荐答案
所以你只想将每个平板覆盖的数量(单位")乘以当时的比率 - 所以我将创建一个具有平板末端的数组作为 key,并将速率作为值 - 然后您可以使用扩展的 foreach 语法简单地循环它,并且始终可以访问与平板相对应的速率.
So you simply want to multiply the amount covered by each slab ("units") with the rate then - so I would create an array that has the slab end as key, and the rate as value - then you can simply loop over that with the extended foreach syntax, and have access to the rate corresponding to the slab at all times.
扩展我对上一个问题的回答,然后看起来像这样:
Expanding on my answer to the previous question, this would look like this then:
$input = '128.82';
$slabs = [100 => 10, 150 => 12, PHP_INT_MAX => 14];
$result = [];
$bill = 0;
$previous_slab = 0;
foreach($slabs as $slab => $rate) {
// calculate distance between current and previous slab
$slab_distance = $slab - $previous_slab;
// if current remainder of input value is >= distance, add distance to result,
// and subtract distance from remainder of input
if( $input >= $slab_distance ) {
$result[] = $slab_distance;
$bill += $slab_distance * $rate;
$input -= $slab_distance;
}
// otherwise, add remainder as last item of result, and break out of the loop here
else {
$result[] = $input;
$bill += $input * $rate;
break;
}
$previous_slab = $slab;
}
var_dump($result, $bill);
结果:
array (size=2)
0 => int 100
1 => float 28.82
float 1345.84
我的方法在这里再次仅使用上层板边界 - 您在数据库中的最后一条记录中没有,所以我在这里用 PHP_INT_MAX 替换该 NULL 值.考虑到这一点,我希望当您从数据库内容创建该数组时,应该不会有挑战.
My approach again uses only the upper slab boundaries here - you don’t have one in your last record from the database, so I am substituting PHP_INT_MAX here for that NULL value. Taking that into account, when you create that array from your database content, should be not challenge, I hope.
(如果您在这里只需要 1345.84
作为结果,而不需要显示跨单个平板的分布的数组,那么您只需删除包含 $result的所有行代码> 任何地方.)
(If you only need the 1345.84
as result here, and not the array that shows the distribution accross the indivudual slabs, then you can just remove all lines that contain $result
anywhere.)
这篇关于Yii2中的平板电费计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!