|
@@ -12,6 +12,16 @@ require_once(BASE_ROOT_PATH . '/helper/bonus_helper.php');
|
|
|
require_once(BASE_ROOT_PATH . '/helper/account_helper.php');
|
|
|
|
|
|
|
|
|
+interface IMoneyCalc
|
|
|
+{
|
|
|
+ public function add_bonuses($items);
|
|
|
+ public function is_enough(&$rate,$amount);
|
|
|
+ public function with_hold($rate,$amount);
|
|
|
+ public function find_rate($amount);
|
|
|
+ public function format();
|
|
|
+ public function total();
|
|
|
+}
|
|
|
+
|
|
|
class RateMoney
|
|
|
{
|
|
|
const ASC = 1;
|
|
@@ -215,6 +225,144 @@ class RateMoney
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+class BonusAmount
|
|
|
+{
|
|
|
+ const ASC = 1;
|
|
|
+ const DESC = 2;
|
|
|
+ const PRED_RATE = 30;
|
|
|
+ private $mRates;
|
|
|
+ private $mDirty;
|
|
|
+ private $mAmount;
|
|
|
+
|
|
|
+ public function __construct($rates)
|
|
|
+ {
|
|
|
+ $this->mDirty = false;
|
|
|
+ $this->mAmount = 0.00;
|
|
|
+
|
|
|
+ $amount = 0.0;
|
|
|
+ foreach ($rates as $key => $val) {
|
|
|
+ $amount += $val;
|
|
|
+ }
|
|
|
+ $this->mAmount = intval(100 * $amount + 0.5) / 100;
|
|
|
+ }
|
|
|
+ public function add_bonuses($items)
|
|
|
+ {
|
|
|
+ foreach ($items as $item)
|
|
|
+ {
|
|
|
+ $bonus = \bonus\user_bonus::create_by_param($item);
|
|
|
+ $amount = intval($bonus->remain_amount() * 100 + 0.5) / 100;
|
|
|
+
|
|
|
+ if($amount <= 0) continue;
|
|
|
+ $this->mAmount += intval(100 * $amount + 0.5) / 100;
|
|
|
+ $this->mDirty = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public function is_enough(&$rate,$amount)
|
|
|
+ {
|
|
|
+ $rate = defaultBonusRate();
|
|
|
+ $amount = intval($amount * 100 + 0.5);
|
|
|
+ $tmp = intval($this->mAmount * 100 + 0.5);
|
|
|
+ return $tmp >= $amount;
|
|
|
+ }
|
|
|
+ public function with_hold($rate,$amount)
|
|
|
+ {
|
|
|
+ $this->mAmount -= ($amount * 100 + 0.5) / 100;
|
|
|
+ $this->mDirty = true;
|
|
|
+ }
|
|
|
+ public function find_rate($amount)
|
|
|
+ {
|
|
|
+ if($this->is_enough($rate,$amount)) {
|
|
|
+ return ['rate' => defaultBonusRate(),'amount' => intval($this->mAmount * 100 + 0.5) / 100];
|
|
|
+ } else {
|
|
|
+ $amount = intval($amount * 100 + 0.5);
|
|
|
+ return ['rate' => $rate,'amount' => $amount / 100];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public function format()
|
|
|
+ {
|
|
|
+ return [defaultBonusRate() => intval($this->mAmount * 100 + 0.5) / 100];
|
|
|
+ }
|
|
|
+ public function total()
|
|
|
+ {
|
|
|
+ return $this->mAmount;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function dirty() {
|
|
|
+ return $this->mDirty;
|
|
|
+ }
|
|
|
+ public function clean() {
|
|
|
+ $this->mDirty = false;
|
|
|
+ }
|
|
|
+ public function calc_rates($amount)
|
|
|
+ {
|
|
|
+ $rates = [];
|
|
|
+
|
|
|
+ $left = intval($amount * 100 + 0.5);
|
|
|
+ foreach ($this->mRates as $rate => $total)
|
|
|
+ {
|
|
|
+ if($left <= 0) break;
|
|
|
+ if($rate > 100) continue;
|
|
|
+
|
|
|
+ $total = intval($total * 100 + 0.5);
|
|
|
+ if($total <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if($total >= $left) {
|
|
|
+ $rates[$rate] = $left / 100;
|
|
|
+ $left = 0;
|
|
|
+ } else {
|
|
|
+ $rates[$rate] = $total / 100;
|
|
|
+ $left -= $total;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $rates;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function calc_money($price,&$rates)
|
|
|
+ {
|
|
|
+ $rates = [];
|
|
|
+ $disc = 0;
|
|
|
+ $left = intval($price * 100 + 0.5);
|
|
|
+ foreach ($this->mRates as $rate => $total)
|
|
|
+ {
|
|
|
+ if($left <= 0) break;
|
|
|
+ if($rate > 100) continue;
|
|
|
+
|
|
|
+ $total = intval($total * 100 + 0.5);
|
|
|
+ if($total <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $max_rate = intval($left * $rate / 100 + 0.5);
|
|
|
+ if($total >= $max_rate) {
|
|
|
+ $disc += $max_rate;
|
|
|
+ $left = 0;
|
|
|
+ $rates[$rate] = $max_rate / 100;
|
|
|
+ } else {
|
|
|
+ $disc += $total;
|
|
|
+ $left -= intval($total * 100 / $rate + 0.5);
|
|
|
+ $rates[$rate] = $total / 100;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $cur_price = intval($price * 100 + 0.5) - $disc;
|
|
|
+ return $cur_price / 100;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function calc_price($price,&$rates)
|
|
|
+ {
|
|
|
+ $cur_price = intval($this->calc_money($price,$rates) * 100 + 0.5);
|
|
|
+ return $cur_price / 100;
|
|
|
+ }
|
|
|
+ static function scale() {
|
|
|
+ return (100 - self::PRED_RATE) / 100;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
class predeposit_helper
|
|
|
{
|
|
|
private $model_pd;
|
|
@@ -235,10 +383,8 @@ class predeposit_helper
|
|
|
|
|
|
$pd_array = Model('member')->getMemberPdInfo($this->member_id);
|
|
|
$this->mRateVersion = intval($pd_array['rate_version']);
|
|
|
-
|
|
|
$this->init_rate();
|
|
|
}
|
|
|
-
|
|
|
public function __destruct()
|
|
|
{
|
|
|
if($this->mDirty)
|
|
@@ -249,17 +395,15 @@ class predeposit_helper
|
|
|
$this->inc_rate_version();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
public function member_id() {
|
|
|
return $this->member_id;
|
|
|
}
|
|
|
-
|
|
|
private function init_rate()
|
|
|
{
|
|
|
if($this->mFromSession)
|
|
|
{
|
|
|
$fUpdate = false;
|
|
|
- if(isset($_SESSION['bonus_rate_version']) == false) {
|
|
|
+ if(!isset($_SESSION['bonus_rate_version'])) {
|
|
|
$fUpdate = true;
|
|
|
}
|
|
|
else
|
|
@@ -271,8 +415,8 @@ class predeposit_helper
|
|
|
else
|
|
|
{
|
|
|
if(isset($_SESSION['bonus_rate']) && isset($_SESSION['bonus_state']) && isset($_SESSION['share_bonus_rate'])) {
|
|
|
- $this->mPayRates = new RateMoney($_SESSION['bonus_rate']);
|
|
|
- $this->mShareRates = new RateMoney($_SESSION['share_bonus_rate']);
|
|
|
+ $this->mPayRates = self::create_moneycalc($_SESSION['bonus_rate']);
|
|
|
+ $this->mShareRates = self::create_moneycalc($_SESSION['share_bonus_rate']);
|
|
|
$this->mBonusState = $_SESSION['bonus_state'];
|
|
|
} else {
|
|
|
$fUpdate = true;
|
|
@@ -288,12 +432,12 @@ class predeposit_helper
|
|
|
if($fUpdate || $this->need_update())
|
|
|
{
|
|
|
$mod_bonus = Model('user_bonus');
|
|
|
- $this->mPayRates = new RateMoney(array());
|
|
|
+ $this->mPayRates = self::create_moneycalc(array());
|
|
|
$pay_items = $mod_bonus->getUsableBonus($this->member_id);
|
|
|
$this->mPayRates->add_bonuses($pay_items);
|
|
|
$pay_bonus_rate = $this->mPayRates->format();
|
|
|
|
|
|
- $this->mShareRates = new RateMoney(array());
|
|
|
+ $this->mShareRates = self::create_moneycalc(array());
|
|
|
$share_items = $mod_bonus->getShareableBonus($this->member_id);
|
|
|
$this->mShareRates->add_bonuses($share_items);
|
|
|
$share_bonus_rate = $this->mShareRates->format();
|
|
@@ -312,7 +456,6 @@ class predeposit_helper
|
|
|
}
|
|
|
|
|
|
$this->write_rates($this->mBonusState,$pay_bonus_rate,$share_bonus_rate);
|
|
|
-
|
|
|
$this->mPayRates->clean();
|
|
|
$this->mShareRates->clean();
|
|
|
|
|
@@ -352,11 +495,9 @@ class predeposit_helper
|
|
|
public function share_total_bonus() {
|
|
|
return $this->mShareRates->total();
|
|
|
}
|
|
|
-
|
|
|
public function bonus_state() {
|
|
|
return $this->mBonusState;
|
|
|
}
|
|
|
-
|
|
|
private function query_cond($query_state)
|
|
|
{
|
|
|
static $stQuerys = array('usable','expiring','used','expired');
|
|
@@ -914,7 +1055,7 @@ class predeposit_helper
|
|
|
$pred = new predeposit_helper($_SESSION['member_id'],true);
|
|
|
$bonus_rate = $pred->pay_bonus_rates();
|
|
|
} else {
|
|
|
- $bonus_rate = new RateMoney($_SESSION['bonus_rate']);
|
|
|
+ $bonus_rate = self::create_moneycalc($_SESSION['bonus_rate']);
|
|
|
}
|
|
|
|
|
|
return $bonus_rate->calc_money($goods_amount,$rates);
|
|
@@ -928,7 +1069,7 @@ class predeposit_helper
|
|
|
$pred = new predeposit_helper($_SESSION['member_id'],true);
|
|
|
$bonus_rate = $pred->pay_bonus_rates();
|
|
|
} else {
|
|
|
- $bonus_rate = new RateMoney($_SESSION['bonus_rate']);
|
|
|
+ $bonus_rate = self::create_moneycalc($_SESSION['bonus_rate']);
|
|
|
}
|
|
|
|
|
|
return $bonus_rate->calc_price($goods_price,$rates);
|
|
@@ -942,7 +1083,7 @@ class predeposit_helper
|
|
|
static public function unlogin_bonus_price($goods_price,$rate_moneys,&$rates)
|
|
|
{
|
|
|
if(!empty($rate_moneys)) {
|
|
|
- $bonus_rate = new RateMoney($rate_moneys);
|
|
|
+ $bonus_rate = self::create_moneycalc($rate_moneys);
|
|
|
return $bonus_rate->calc_price($goods_price,$rates);
|
|
|
} else {
|
|
|
return $goods_price;
|
|
@@ -966,4 +1107,12 @@ class predeposit_helper
|
|
|
$types = bonus\type::create_by_paramer($bonus_type);
|
|
|
QueueClient::push('onPredeposit',array('change_type' => 'bonus_refund','buyer_id' => $types->sender_id(),'order_sn' => $types->getType_sn()));
|
|
|
}
|
|
|
+ static private function create_moneycalc($rate_moneys)
|
|
|
+ {
|
|
|
+ if(noBonusRate()) {
|
|
|
+ return new BonusAmount($rate_moneys);
|
|
|
+ } else {
|
|
|
+ return new RateMoney($rate_moneys);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|