calc_helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. declare(strict_types=0);
  3. require_once (BASE_HELPER_PATH . '/model/member_info.php');
  4. use mcard\user_mcards;
  5. interface ICalc
  6. {
  7. public function calc_vgoods_price($goods_info);
  8. public function calc_vorder_amount($order_info);
  9. public function calc_tips();
  10. }
  11. class CalcPrice implements ICalc
  12. {
  13. public const CalcTypeNormal = 1;
  14. public const CalcTypeVIP = 2;
  15. public const CalcTypeFirstOrder = 3;
  16. public const CalcTypeInvitees = 4;
  17. public const CalcTypeNone = 5;
  18. private const FIRST_ORDER_PRICE = 0.95;
  19. private const DEFAULT_ORDER_PRICE = 0.99;
  20. private const STEP_PRICE_ITEMS = [
  21. ['num' => 10, 'discount' => 0.95,'tip' => '邀请10人可享95折扣'],
  22. ['num' => 5, 'discount' => 0.96, 'tip' => '邀请5人可享96折扣'],
  23. ['num' => 3, 'discount' => 0.97, 'tip' => '邀请3人可享97折扣']
  24. ];
  25. private $mUserId;
  26. private $mUserCards = null;
  27. private $mMemberInfo = null;
  28. private $mInvitees = 0;
  29. private $mOrderCount = 0;
  30. private $mCalcType = 0;
  31. private $mUsedInviteesNum = 0;
  32. private $mOrderId = 0;
  33. public function __construct($userid)
  34. {
  35. $this->mUserId = intval($userid);
  36. if($userid > 0) {
  37. $this->mUserCards = new user_mcards($userid);
  38. $this->mMemberInfo = new member_info($userid);
  39. $this->mInvitees = $this->invitees();
  40. $this->mOrderCount = $this->order_num() + $this->vrorder_num();
  41. }
  42. }
  43. private function isVip()
  44. {
  45. if($this->mUserId > 0 && $this->mUserCards != null) {
  46. return $this->mUserCards->hasCards();
  47. }
  48. else {
  49. return false;
  50. }
  51. }
  52. private function order_num()
  53. {
  54. if($this->mUserId <= 0) return 0;
  55. $mod_member = Model('order');
  56. $items = $mod_member->field('COUNT(*) AS order_num')
  57. ->table('order')
  58. ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
  59. ->select();
  60. if(empty($items)) {
  61. return 0;
  62. } else {
  63. return intval($items[0]['order_num']);
  64. }
  65. }
  66. private function vrorder_num()
  67. {
  68. if($this->mUserId <= 0) return 0;
  69. $mod_member = Model('vr_order');
  70. $items = $mod_member->field('COUNT(*) AS order_num')
  71. ->table('vr_order')
  72. ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
  73. ->select();
  74. if(empty($items)) {
  75. return 0;
  76. } else {
  77. return intval($items[0]['order_num']);
  78. }
  79. }
  80. private function isFirstorOrder()
  81. {
  82. return ($this->mOrderCount == 0);
  83. }
  84. private function select_invitees()
  85. {
  86. if($this->mUserId <= 0 || $this->mMemberInfo == null) {
  87. return false;
  88. }
  89. $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
  90. foreach (self::STEP_PRICE_ITEMS as $item) {
  91. $num = $item['num'];
  92. if($left_invitees >= $num) {
  93. $this->mUsedInviteesNum = $num;
  94. return $item;
  95. }
  96. }
  97. return false;
  98. }
  99. private function invitees()
  100. {
  101. $mod_member = Model('member');
  102. $ret = $mod_member->field('COUNT(*) AS inviter_count')
  103. ->where(['inviter_id' => $this->mUserId])
  104. ->select();
  105. if(empty($ret)) {
  106. return 0;
  107. }
  108. else {
  109. return intval($ret[0]['inviter_count']);
  110. }
  111. }
  112. private function isExcluded($goods_id)
  113. {
  114. global $config;
  115. $exclue_gids = $config['exclude_preferential_goods_ids'];
  116. if(empty($exclue_gids)) {
  117. return false;
  118. } else {
  119. return in_array(intval($goods_id),$exclue_gids);
  120. }
  121. }
  122. public function deduct_order($order_id)
  123. {
  124. if($this->mCalcType == self::CalcTypeVIP) {
  125. $this->detuct_mcard($order_id);
  126. }
  127. elseif($this->mCalcType == self::CalcTypeFirstOrder) {
  128. }
  129. elseif($this->mCalcType == self::CalcTypeInvitees) {
  130. $this->deduct_invitees($order_id);
  131. }
  132. else {
  133. }
  134. }
  135. private function detuct_mcard($vorder_id)
  136. {
  137. $mod_vorder = Model('vr_order');
  138. $order = $mod_vorder->getOrderInfo(['order_id' => $vorder_id],'*',true);
  139. if(empty($order)) return true;
  140. global $config;
  141. $spec_card = $config['vgoods_spec_card'];
  142. $goods_id = intval($order['goods_id']);
  143. $num = intval($order['goods_num']);
  144. $goods_price = $order['goods_price'];
  145. if(array_key_exists($goods_id,$spec_card)) {
  146. $amount = $spec_card[$goods_id] * $num;
  147. } else {
  148. $amount = $goods_price * $num;
  149. }
  150. $this->mUserCards->deduct($amount);
  151. $mod_vorder->editOrder(['calctype' => self::CalcTypeVIP,'calcamount' => $amount],['order_id' => $vorder_id]);
  152. }
  153. private function deduct_invitees($vorder_id)
  154. {
  155. $model = Model('member');
  156. $num = $this->mUsedInviteesNum;
  157. $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]);
  158. $mod_vorder = Model('vr_order');
  159. $mod_vorder->editOrder(['calctype' => self::CalcTypeInvitees,'calcamount' => $num],['order_id' => $vorder_id]);
  160. }
  161. public function calc_tips()
  162. {
  163. global $config;
  164. if($this->mUserId <= 0) {
  165. return $config['tips']['first_order'];
  166. }
  167. $fVip = $this->isVip();
  168. if($fVip)
  169. {
  170. if($this->isFirstorOrder()) {
  171. $tips = $config['tips']['vip_first_order'];
  172. } else {
  173. $tips = $config['tips']['vip_user'];
  174. }
  175. }
  176. elseif($this->isFirstorOrder()) {
  177. $tips = $config['tips']['first_order'];
  178. } else {
  179. $tips = $config['tips']['none_vip'];
  180. }
  181. return $tips;
  182. }
  183. public function inviter_tips()
  184. {
  185. if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType != self::CalcTypeInvitees)
  186. {
  187. $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
  188. $cur = [];
  189. $next = [];
  190. foreach (self::STEP_PRICE_ITEMS as $item)
  191. {
  192. $num = $item['num'];
  193. if($left_invitees >= $num) {
  194. $cur = $item;
  195. } else {
  196. $next = $item;
  197. }
  198. }
  199. $count = $next['num'] - $left_invitees;
  200. $discount = $next['discount'] * 100;
  201. $tip = "再分享给{$count}人,即可享受{$discount}折";
  202. return $tip;
  203. } else {
  204. return '';
  205. }
  206. }
  207. public function calc_vgoods_price($goods_info)
  208. {
  209. $goods_id = intval($goods_info['goods_id']);
  210. $goods_price = $goods_info['goods_price'];
  211. if($this->isExcluded($goods_id)) {
  212. $this->mCalcType = self::CalcTypeNone;
  213. return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
  214. }
  215. elseif($this->isVip()) {
  216. $this->mCalcType = self::CalcTypeVIP;
  217. $price = $this->mUserCards->calc_price($goods_id, $goods_price);
  218. return ['price_des' => '会员价', 'accu_price' => round($price,2)];
  219. }
  220. elseif($this->isFirstorOrder()) {
  221. $this->mCalcType = self::CalcTypeFirstOrder;
  222. return ['price_des' => '首单价', 'accu_price' => round($goods_price * self::FIRST_ORDER_PRICE,2)];
  223. }
  224. elseif(!empty($num_dis = $this->select_invitees())) {
  225. $this->mCalcType = self::CalcTypeInvitees;
  226. $discount = $num_dis['discount'];
  227. return ['price_des' => '邀请价', 'accu_price' => round($goods_price * $discount,2)];
  228. }
  229. else {
  230. $this->mCalcType = self::CalcTypeNormal;
  231. return ['price_des' => '售价', 'accu_price' => round($goods_price * self::DEFAULT_ORDER_PRICE,2)];
  232. }
  233. }
  234. public function calc_vorder_amount($vorder_info)
  235. {
  236. $goods_id = intval($vorder_info['goods_id']);
  237. $goods_price = $vorder_info['goods_price'];
  238. $num = $vorder_info['quantity'];
  239. if($this->isExcluded($goods_id)) {
  240. $this->mCalcType = self::CalcTypeNone;
  241. return round($goods_price * $num,2);
  242. }
  243. elseif($this->isVip()) {
  244. $this->mCalcType = self::CalcTypeVIP;
  245. return $this->mUserCards->calc_amount($goods_id, $goods_price);
  246. }
  247. elseif($this->isFirstorOrder()) {
  248. $this->mCalcType = self::CalcTypeFirstOrder;
  249. return round($goods_price * self::FIRST_ORDER_PRICE * $num,2);
  250. }
  251. elseif(!empty($num_dis = $this->select_invitees())) {
  252. $this->mCalcType = self::CalcTypeInvitees;
  253. $discount = $num_dis['discount'];
  254. return round($goods_price * $discount * $num,2);
  255. }
  256. else {
  257. $this->mCalcType = self::CalcTypeNormal;
  258. return round($goods_price * $num * self::DEFAULT_ORDER_PRICE,2);
  259. }
  260. }
  261. public static function onCancelVrOrder($order)
  262. {
  263. $calc_type = intval($order['calctype']);
  264. $calc_amount = $order['calcamount'];
  265. $userid = intval($order['buyer_id']);
  266. $order_id = $order['order_id'];
  267. if($calc_type === self::CalcTypeVIP)
  268. {
  269. $mod_cardkey = Model('card_key');
  270. $items = $mod_cardkey->field('*')->where(['order_id' => $order_id,'member_id' => $userid])->select();
  271. if(empty($items)) return false;
  272. $card = $items[0];
  273. $amount = $card['amount'];
  274. $cards = new user_mcards($userid);
  275. $cards->add_amount($amount);
  276. $mod_vorder = Model('vr_order');
  277. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  278. }
  279. elseif($calc_type === self::CalcTypeInvitees)
  280. {
  281. $num = intval($calc_amount);
  282. if($num > 0) {
  283. $mod_vorder = Model('vr_order');
  284. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  285. $model = Model('member');
  286. $ret = $model->editMember(['member_id' => $userid], ['used_invitees'=> ['exp', "used_invitees-{$num}"]]);
  287. return $ret;
  288. }
  289. }
  290. else {
  291. Log::record("onCancelOrder",Log::DEBUG);
  292. }
  293. return true;
  294. }
  295. }