123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- declare(strict_types=0);
- require_once (BASE_HELPER_PATH . '/model/member_info.php');
- require_once (BASE_HELPER_PATH . '/icalc.php');
- use mcard\user_mcards;
- class CalcPrice implements ICalc
- {
- public const CalcTypeNormal = 1;
- public const CalcTypeVIP = 2;
- public const CalcTypeFirstOrder = 3; //已经删除...
- public const CalcTypeInvitees = 4;
- public const CalcTypeNone = 5;
- private $mUserId;
- private $mUserCards = null;
- private $mMemberInfo = null;
- private $mInvitees = 0;
- private $mOrderCount = 0;
- private $mCalcType = 0;
- private $mUsedInviteesNum = 0;
- public function __construct($userid)
- {
- $this->mUserId = intval($userid);
- if($userid > 0) {
- $this->mUserCards = new user_mcards($userid);
- $this->mMemberInfo = new member_info($userid);
- $this->mInvitees = $this->invitees();
- $this->mOrderCount = $this->order_num() + $this->vrorder_num();
- }
- }
- private function isVip()
- {
- if($this->mUserId > 0 && $this->mUserCards != null) {
- return $this->mUserCards->hasCards();
- }
- else {
- return false;
- }
- }
- private function order_num()
- {
- if($this->mUserId <= 0) return 0;
- $mod_member = Model('order');
- $items = $mod_member->field('COUNT(*) AS order_num')
- ->table('order')
- ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
- ->select();
- if(empty($items)) {
- return 0;
- } else {
- return intval($items[0]['order_num']);
- }
- }
- private function vrorder_num()
- {
- if($this->mUserId <= 0) return 0;
- $mod_member = Model('vr_order');
- $items = $mod_member->field('COUNT(*) AS order_num')
- ->table('vr_order')
- ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
- ->select();
- if(empty($items)) {
- return 0;
- } else {
- return intval($items[0]['order_num']);
- }
- }
- private function first_order($goods_id)
- {
- if($this->mOrderCount > 0) return false;
- $share_policy = $this->share_policy($goods_id);
- return empty($share_policy) ? false : $share_policy[0];
- }
- public function left_invitees()
- {
- if($this->mUserId <= 0 || $this->mMemberInfo == null) {
- $left_invitees = 0;
- } else {
- $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
- }
- return $left_invitees;
- }
- private function select_invitees($goods_id)
- {
- $left_invitees = $this->left_invitees();
- $share_policy = $this->share_policy($goods_id);
- if(empty($share_policy)) return false;
- foreach ($share_policy as $item)
- {
- $num = $item['num'];
- if($left_invitees >= $num) {
- $policy = $item;
- break;
- }
- }
- return empty($policy) ? false : $policy;
- }
- public function share_policy($goods_id)
- {
- global $config;
- $share_policy = $config['goods_share_policy'];
- if(array_key_exists($goods_id,$share_policy)) {
- return $share_policy[$goods_id];
- } else {
- return [];
- }
- }
- private function invitees()
- {
- $mod_member = Model('member');
- $ret = $mod_member->field('COUNT(*) AS inviter_count')
- ->where(['inviter_id' => $this->mUserId])
- ->select();
- if(empty($ret)) {
- return 0;
- }
- else {
- return intval($ret[0]['inviter_count']);
- }
- }
- private function isExcluded($goods_id)
- {
- global $config;
- $exclue_gids = $config['exclude_preferential_goods_ids'];
- if(empty($exclue_gids)) {
- return false;
- } else {
- return in_array(intval($goods_id),$exclue_gids);
- }
- }
- public function deduct_order($order_id)
- {
- if($this->mCalcType == self::CalcTypeVIP) {
- $this->detuct_mcard($order_id);
- }
- elseif($this->mCalcType == self::CalcTypeInvitees) {
- $this->deduct_invitees($order_id);
- }
- else {
- }
- }
- private function detuct_mcard($vorder_id)
- {
- $mod_vorder = Model('vr_order');
- $order = $mod_vorder->getOrderInfo(['order_id' => $vorder_id],'*',true);
- if(empty($order)) return true;
- global $config;
- $spec_card = $config['vgoods_spec_card'];
- $goods_id = intval($order['goods_id']);
- $num = intval($order['goods_num']);
- $goods_price = $order['goods_price'];
- if(array_key_exists($goods_id,$spec_card)) {
- $amount = $spec_card[$goods_id] * $num;
- } else {
- $amount = $goods_price * $num;
- }
- $this->mUserCards->deduct($amount);
- $mod_vorder->editOrder(['calctype' => self::CalcTypeVIP,'calcamount' => $amount],['order_id' => $vorder_id]);
- }
- private function deduct_invitees($vorder_id)
- {
- $model = Model('member');
- $num = $this->mUsedInviteesNum;
- $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]);
- $mod_vorder = Model('vr_order');
- $mod_vorder->editOrder(['calctype' => self::CalcTypeInvitees,'calcamount' => $num],['order_id' => $vorder_id]);
- }
- public function calc_tips()
- {
- // global $config;
- //
- // if($this->mUserId <= 0) {
- // return $config['tips']['first_order'];
- // }
- //
- // $fVip = $this->isVip();
- // if($fVip)
- // {
- // if($this->isFirstorOrder()) {
- // $tips = $config['tips']['vip_first_order'];
- // } else {
- // $tips = $config['tips']['vip_user'];
- // }
- // }
- // elseif($this->isFirstorOrder()) {
- // $tips = $config['tips']['first_order'];
- // } else {
- // $tips = $config['tips']['none_vip'];
- // }
- return "全国通用,即刻到账";
- }
- public function inviter_tips($goods_id)
- {
- if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType == self::CalcTypeInvitees)
- {
- $left_invitees = $this->left_invitees();
- $share_policy = $this->share_policy($goods_id);
- foreach ($share_policy as $item)
- {
- $num = $item['num'];
- if($left_invitees >= $num) {
- $cur = $item;
- break;
- } else {
- $next = $item;
- }
- }
- if(empty($next)) {
- $discount = $cur['discount'];
- $tip = "您已省{$discount}元,已经最优惠~";
- }
- else
- {
- $count = $next['num'] - $left_invitees;
- $discount = $next['discount'];
- if(empty($cur)) {
- $tip = "邀请{$count}人注册,可省{$discount}元";
- }
- else {
- $discount = $discount - $cur['discount'];
- $tip = "已省{$cur['discount']}元,再邀请{$count}人注册,还能省{$discount}元";
- }
- }
- return [$tip,true];
- }
- elseif($this->mCalcType == self::CalcTypeFirstOrder) {
- $share_policy = $this->first_order($goods_id);
- $tip = "已领新人优惠券,现在充值可省{$share_policy['discount']}元";
- return [$tip,false];
- }
- else {
- return ['',true];
- }
- }
- private function need_detuct() {
- return true;//$this->mOrderCount < 10;
- }
- public function calc_vgoods_price($goods_info)
- {
- $goods_id = intval($goods_info['goods_id']);
- $goods_price = $goods_info['goods_price'];
- if($this->isExcluded($goods_id)) {
- $this->mCalcType = self::CalcTypeNone;
- return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
- }
- elseif($this->isVip()) {
- $this->mCalcType = self::CalcTypeVIP;
- $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
- $price = $this->mUserCards->calc_price($goods_id, $goods_price);
- return ['price_des' => '会员价', 'accu_price' => round($price,2)];
- }
- elseif(!empty($policy = $this->first_order($goods_id))) {
- $this->mCalcType = self::CalcTypeFirstOrder;
- $price = $policy['price'];
- return ['price_des' => '优惠价', 'accu_price' => round($price,2)];
- }
- elseif(!empty($policy = $this->select_invitees($goods_id))) {
- $this->mCalcType = self::CalcTypeInvitees;
- $price = $policy['price'];
- if($this->need_detuct()) {
- $this->mUsedInviteesNum = $policy['num'];
- } else {
- $this->mUsedInviteesNum = 0;
- }
- return ['price_des' => '优惠价', 'accu_price' => round($price,2)];
- }
- else {
- $this->mCalcType = self::CalcTypeNormal;
- return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
- }
- }
- //获取商品面额
- private function goods_spec_amount($goods_id,$goods_price)
- {
- global $config;
- $spec_card = $config['vgoods_spec_card'];
- if(array_key_exists($goods_id,$spec_card)) {
- return $spec_card[$goods_id];
- }
- else {
- Log::record("cannot find goods_id = {$goods_id} spec",Log::ERR);
- return $goods_price;
- }
- }
- public function calc_vorder_amount($vorder_info)
- {
- $goods_id = intval($vorder_info['goods_id']);
- $goods_price = $vorder_info['goods_price'];
- $num = $vorder_info['quantity'];
- if($this->isExcluded($goods_id)) {
- $this->mCalcType = self::CalcTypeNone;
- return round($goods_price * $num,2);
- }
- elseif($this->isVip()) {
- $this->mCalcType = self::CalcTypeVIP;
- $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
- return $this->mUserCards->calc_amount($goods_id, $goods_price * $num);
- }
- elseif(!empty($policy = $this->first_order($goods_id))) {
- $this->mCalcType = self::CalcTypeFirstOrder;
- $price = $policy['price'];
- return round($price * $num,2);
- }
- elseif(!empty($policy = $this->select_invitees($goods_id))) {
- $this->mCalcType = self::CalcTypeInvitees;
- $price = $policy['price'];
- if($this->need_detuct()) {
- $this->mUsedInviteesNum = $policy['num'];
- } else {
- $this->mUsedInviteesNum = 0;
- }
- return round($price * $num,2);
- }
- else {
- $this->mCalcType = self::CalcTypeNormal;
- return round($goods_price * $num,2);
- }
- }
- public static function onCancelVrOrder($order)
- {
- $calc_type = intval($order['calctype']);
- $calc_amount = $order['calcamount'];
- $userid = intval($order['buyer_id']);
- $order_id = $order['order_id'];
- if($calc_type === self::CalcTypeVIP)
- {
- $mod_cardkey = Model('card_key');
- $items = $mod_cardkey->getCards(['order_id' => $order_id,'member_id' => $userid]);
- if(empty($items)) return false;
- $card = $items[0];
- $amount = $card['amount'];
- $cards = new user_mcards($userid);
- $cards->add_amount($amount);
- $mod_vorder = Model('vr_order');
- $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
- }
- elseif($calc_type === self::CalcTypeInvitees)
- {
- $num = intval($calc_amount);
- if($num > 0) {
- $mod_vorder = Model('vr_order');
- $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
- $model = Model('member');
- $ret = $model->editMember(['member_id' => $userid], ['used_invitees'=> ['exp', "used_invitees-{$num}"]]);
- return $ret;
- }
- }
- else {
- Log::record("onCancelOrder",Log::DEBUG);
- }
- return true;
- }
- }
|