我有一个客户端,需要在现有解决方案中添加新功能。
他们使用以下列将财务数据存储在MySQL数据库中。这些都是 未付款,有些有罚款,有些没有。
ID | money_id | principal | interest | penalty | paid_pri | paid_int | paid_pen
1 1 50 60 5 0 0 0
2 1 55 55 5 0 0 0
3 1 60 50 0 0 0 0
在上述情况下,客户有两个罚款需要支付ID 1& 2,如果 客户支付200美元,然后需要先支付利息和罚金 支付本金。一旦所有出色的兴趣和罚款已经支付 本金可以还清。
支付200之后的新表应该是这样的。
ID | money_id | principal | interest | penalty | paid_pri | paid_int | paid_pen
1 1 50 60 5 25 60 5
2 1 55 55 5 0 25 5
3 1 60 50 0 0 50 0
这是因为分配200付,60 + 5 + 2 5 + 5 + 50 那么第一位委托人就是25岁。
编辑MARC B的观点:
我怎样才能使用php循环减少数据库的几个列和行中的值"
我必须使用从mysql返回的对象。
Array
(
[0] => Array
(
[id] => 1
[interest] => 60
[interestPaid] => 0
[penalty] => 5.00
[penaltyPaid] => 0
[principal] => 50
[principalPaid] => 0.00
)
[1] => Array
(
[id] => 2
[interest] => 55
[interestPaid] => 739.10
[penalty] => 5
[penaltyPaid] => 739.10
[principal] => 55
[principalPaid] => 0.00
)
)
我必须使用它来遍历数据库,它叫做$ payment_array。
我的代码到目前为止,请记住他们有一个扩展mysql($ this)的类
$amount_to_pay = 200;
foreach ($payment_array as $ordered_repayment) {
if ($amount_to_pay > 0) {
$interest_balance = $ordered_repayment['interest'] - $ordered_repayment['interestPaid'];
if ($interest_balance > 0) {
$sql = "update schedules set paid_int = paid_int + '" . $interest_balance . "' where id = '" . $ordered_repayment['id'] . "'";
$amount_to_pay -= $interest_balance;
$this->query($sql);
}
$penalty_balance = $ordered_repayment['penalty'] - $ordered_repayment['penaltyPaid'];
if ($penalty_balance > 0) {
$sql = "update schedules set paid_pen = paid_pen + '" . $penalty_balance . "' where id = '" . $ordered_repayment['id'] . "'";
$amount_to_pay -= $penalty_balance;
$this->query($sql);
}
}
}
foreach ($payment_array as $ordered_repayment) {
if ($amount_to_pay > 0) {
$capital_balance = $ordered_repayment['principal'] - $ordered_repayment['principalPaid'];
if ($capital_balance > 0) {
$sql = "update schedules set paid_pri = paid_pri + '" . $capital_balance . "' where id = '" . $ordered_repayment['id'] . "'";
$amount_to_pay -= $capital_balance;
$this->query($sql);
}
}
}
希望这有点冗长;)
答案 0 :(得分:0)
我会稍微修改一下以避免在某些行上进行2次更新: -
<?php
$amount_to_pay = 200;
foreach ($payment_array as $ordered_repayment)
{
switch (true)
{
case $amount_to_pay <= 0 :
break;
case $amount_to_pay > ($ordered_repayment['interest'] + $ordered_repayment['penalty']) :
$sql = "UPDATE schedules
SET paid_int = paid_int + " . $ordered_repayment['interest'] . ",
paid_pen = paid_pen + " . $ordered_repayment['penalty'] . ",
interest = 0,
penalty = 0
WHERE id = " . $ordered_repayment['id'] . "";
$amount_to_pay -= ($ordered_repayment['interest'] + $ordered_repayment['penalty']);
break
case $amount_to_pay > $ordered_repayment['penalty']) :
$sql = "UPDATE schedules
SET paid_int = paid_int + " . ($amount_to_pay - $ordered_repayment['penalty']) . ",
paid_pen = paid_pen + " . $ordered_repayment['penalty'] . ",
interest = " . ($ordered_repayment['interest'] - ($amount_to_pay - $ordered_repayment['penalty'])) . ",
penalty = 0
WHERE id = " . $ordered_repayment['id'] . "";
$amount_to_pay = 0;
break
default :
$sql = "UPDATE schedules
SET paid_pen = paid_pen + " . $amount_to_pay . ",
penalty = " . ($ordered_repayment['penalty'] - $amount_to_pay) . "
WHERE id = " . $ordered_repayment['id'] . "";
$amount_to_pay = 0;
break
}
}
foreach ($payment_array as $ordered_repayment)
{
if ($amount_to_pay > 0)
{
$capital_balance = $ordered_repayment['principal'] - $ordered_repayment['principalPaid'];
if ($capital_balance > 0)
{
$sql = "UPDATE schedules
SET paid_pri = paid_pri + " . $capital_balance . "
WHERE id ='" . $ordered_repayment['id'] . "";
$amount_to_pay -= $capital_balance;
$this->query($sql);
}
}
}