|
@@ -0,0 +1,216 @@
|
|
|
+<?php
|
|
|
+declare(strict_types=0);
|
|
|
+
|
|
|
+require_once (BASE_HELPER_PATH . '/model/member_info.php');
|
|
|
+
|
|
|
+use mcard\user_mcards;
|
|
|
+
|
|
|
+interface ICalc
|
|
|
+{
|
|
|
+ public function calc_vgoods_price($goods_info);
|
|
|
+ public function calc_vorder_amount($order_info);
|
|
|
+ public function calc_tips();
|
|
|
+}
|
|
|
+
|
|
|
+class CalcPrice implements ICalc
|
|
|
+{
|
|
|
+ private const FIRST_ORDER_PRICE = 0.95;
|
|
|
+ private const DEFAULT_ORDER_PRICE = 0.99;
|
|
|
+ private const STEP_PRICE_ITEMS = [
|
|
|
+ ['num' => 10, 'discount' => 0.95,'tip' => '邀请10人可享95折扣'],
|
|
|
+ ['num' => 5, 'discount' => 0.96,'tip' => '邀请5人可享96折扣'],
|
|
|
+ ['num' => 3, 'discount' => 0.97,'tip' => '邀请3人可享97折扣']
|
|
|
+ ];
|
|
|
+
|
|
|
+ private $mUserId;
|
|
|
+ private $mUserCards = null;
|
|
|
+ private $mMemberInfo = null;
|
|
|
+ private $mInvitees = 0;
|
|
|
+
|
|
|
+ private $mUsedInvitees = false;
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function isVip()
|
|
|
+ {
|
|
|
+ if($this->mUserId > 0 && $this->mUserCards != null) {
|
|
|
+ return $this->mUserCards->hasCards();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function isFirstorOrder()
|
|
|
+ {
|
|
|
+ if($this->mUserId > 0 && $this->mMemberInfo != null) {
|
|
|
+ return $this->mMemberInfo->order_num() == 0;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function select_invitees()
|
|
|
+ {
|
|
|
+ if($this->mUserId <= 0 || $this->mMemberInfo == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
|
|
|
+
|
|
|
+ foreach (self::STEP_PRICE_ITEMS as $item) {
|
|
|
+ $num = $item['num'];
|
|
|
+ if($left_invitees >= $num) {
|
|
|
+ return $item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function invitees()
|
|
|
+ {
|
|
|
+ $model = model('member');
|
|
|
+ $ret = $model->field('count(*) 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_invitees()
|
|
|
+ {
|
|
|
+ if($this->mUsedInvitees) {
|
|
|
+ $model = model('member');
|
|
|
+ $num = $this->mUsedInviteesNum;
|
|
|
+ $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function calc_tips()
|
|
|
+ {
|
|
|
+ global $config;
|
|
|
+
|
|
|
+ if($this->mUserId <= 0) {
|
|
|
+ return $config['tips']['first_order'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $fVip = $this->isVip();
|
|
|
+ if($fVip)
|
|
|
+ {
|
|
|
+ if(session_helper::first_order()) {
|
|
|
+ $tips = $config['tips']['vip_first_order'];
|
|
|
+ } else {
|
|
|
+ $tips = $config['tips']['vip_user'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ elseif(session_helper::first_order()) {
|
|
|
+ $tips = $config['tips']['first_order'];
|
|
|
+ } else {
|
|
|
+ $tips = $config['tips']['none_vip'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $tips;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function inviter_tips()
|
|
|
+ {
|
|
|
+ if(!$this->mUsedInvitees) return '';
|
|
|
+
|
|
|
+ $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
|
|
|
+
|
|
|
+ $cur = [];
|
|
|
+ $next = [];
|
|
|
+ foreach (self::STEP_PRICE_ITEMS as $item)
|
|
|
+ {
|
|
|
+ $num = $item['num'];
|
|
|
+ if($left_invitees >= $num) {
|
|
|
+ $cur = $item;
|
|
|
+ } else {
|
|
|
+ $next = $item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $count = $next['num'] - $left_invitees;
|
|
|
+ $discount = $next['discount'] * 100;
|
|
|
+
|
|
|
+ $tip = "再邀请{$count}人,可享受{$discount}折扣";
|
|
|
+
|
|
|
+ return $tip;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)) {
|
|
|
+ return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
|
|
|
+ }
|
|
|
+ elseif($this->isVip()) {
|
|
|
+ $price = $this->mUserCards->calc_price($goods_id, $goods_price);
|
|
|
+ return ['price_des' => '会员价', 'accu_price' => round($price,2)];
|
|
|
+ }
|
|
|
+ elseif($this->isFirstorOrder()) {
|
|
|
+ return ['price_des' => '首单价', 'accu_price' => round($goods_price * self::FIRST_ORDER_PRICE,2)];
|
|
|
+ }
|
|
|
+ elseif(!empty($num_dis = $this->select_invitees())) {
|
|
|
+ $num = $num_dis['num'];
|
|
|
+ $this->mUsedInvitees = true;
|
|
|
+ $discount = $num_dis['discount'];
|
|
|
+ return ['price_des' => '邀请价', 'accu_price' => round($goods_price * $discount,2)];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return ['price_des' => '售价', 'accu_price' => round($goods_price * self::DEFAULT_ORDER_PRICE,2)];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function calc_vorder_amount($goods_info)
|
|
|
+ {
|
|
|
+ $goods_id = intval($goods_info['goods_id']);
|
|
|
+ $goods_price = $goods_info['goods_price'];
|
|
|
+ $num = $goods_info['quantity'];
|
|
|
+
|
|
|
+ if($this->isExcluded($goods_id)) {
|
|
|
+ return round($goods_price * $num,2);
|
|
|
+ }
|
|
|
+ elseif($this->isVip()) {
|
|
|
+ return $this->mUserCards->calc_amount($goods_id, $goods_price);
|
|
|
+ }
|
|
|
+ elseif($this->isFirstorOrder()) {
|
|
|
+ return round($goods_price * self::FIRST_ORDER_PRICE * $num,2);
|
|
|
+ }
|
|
|
+ elseif(!empty($num_dis = $this->select_invitees())) {
|
|
|
+ $num = $num_dis['num'];
|
|
|
+ $this->mUsedInvitees = true;
|
|
|
+ $discount = $num_dis['discount'];
|
|
|
+ return round($goods_price * $discount * $num);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return round($goods_price * $num * self::DEFAULT_ORDER_PRICE,2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|