calc_helper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 $mUserId;
  19. private $mUserCards = null;
  20. private $mMemberInfo = null;
  21. private $mInvitees = 0;
  22. private $mOrderCount = 0;
  23. private $mCalcType = 0;
  24. private $mUsedInviteesNum = 0;
  25. public function __construct($userid)
  26. {
  27. $this->mUserId = intval($userid);
  28. if($userid > 0) {
  29. $this->mUserCards = new user_mcards($userid);
  30. $this->mMemberInfo = new member_info($userid);
  31. $this->mInvitees = $this->invitees();
  32. $this->mOrderCount = $this->order_num() + $this->vrorder_num();
  33. }
  34. }
  35. private function isVip()
  36. {
  37. if($this->mUserId > 0 && $this->mUserCards != null) {
  38. return $this->mUserCards->hasCards();
  39. }
  40. else {
  41. return false;
  42. }
  43. }
  44. private function order_num()
  45. {
  46. if($this->mUserId <= 0) return 0;
  47. $mod_member = Model('order');
  48. $items = $mod_member->field('COUNT(*) AS order_num')
  49. ->table('order')
  50. ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
  51. ->select();
  52. if(empty($items)) {
  53. return 0;
  54. } else {
  55. return intval($items[0]['order_num']);
  56. }
  57. }
  58. private function vrorder_num()
  59. {
  60. if($this->mUserId <= 0) return 0;
  61. $mod_member = Model('vr_order');
  62. $items = $mod_member->field('COUNT(*) AS order_num')
  63. ->table('vr_order')
  64. ->where(['buyer_id' => $this->mUserId,'order_state' => ['in',[ORDER_STATE_NEW,ORDER_STATE_PAY,ORDER_STATE_SEND,ORDER_STATE_SUCCESS]]])
  65. ->select();
  66. if(empty($items)) {
  67. return 0;
  68. } else {
  69. return intval($items[0]['order_num']);
  70. }
  71. }
  72. private function first_order($goods_id)
  73. {
  74. if($this->mOrderCount > 0) return false;
  75. $share_policy = $this->share_policy($goods_id);
  76. return empty($share_policy) ? false : $share_policy[0];
  77. }
  78. public function left_invitees()
  79. {
  80. if($this->mUserId <= 0 || $this->mMemberInfo == null) {
  81. $left_invitees = 0;
  82. } else {
  83. $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
  84. }
  85. return $left_invitees;
  86. }
  87. private function select_invitees($goods_id)
  88. {
  89. $left_invitees = $this->left_invitees();
  90. $share_policy = $this->share_policy($goods_id);
  91. if(empty($share_policy)) return false;
  92. foreach ($share_policy as $item)
  93. {
  94. $num = $item['num'];
  95. if($left_invitees >= $num) {
  96. $policy = $item;
  97. break;
  98. }
  99. }
  100. return empty($policy) ? false : $policy;
  101. }
  102. public function share_policy($goods_id)
  103. {
  104. global $config;
  105. $share_policy = $config['goods_share_policy'];
  106. if(array_key_exists($goods_id,$share_policy)) {
  107. return $share_policy[$goods_id];
  108. } else {
  109. return [];
  110. }
  111. }
  112. private function invitees()
  113. {
  114. $mod_member = Model('member');
  115. $ret = $mod_member->field('COUNT(*) AS inviter_count')
  116. ->where(['inviter_id' => $this->mUserId])
  117. ->select();
  118. if(empty($ret)) {
  119. return 0;
  120. }
  121. else {
  122. return intval($ret[0]['inviter_count']);
  123. }
  124. }
  125. private function isExcluded($goods_id)
  126. {
  127. global $config;
  128. $exclue_gids = $config['exclude_preferential_goods_ids'];
  129. if(empty($exclue_gids)) {
  130. return false;
  131. } else {
  132. return in_array(intval($goods_id),$exclue_gids);
  133. }
  134. }
  135. public function deduct_order($order_id)
  136. {
  137. if($this->mCalcType == self::CalcTypeVIP) {
  138. $this->detuct_mcard($order_id);
  139. }
  140. elseif($this->mCalcType == self::CalcTypeInvitees) {
  141. $this->deduct_invitees($order_id);
  142. }
  143. else {
  144. }
  145. }
  146. private function detuct_mcard($vorder_id)
  147. {
  148. $mod_vorder = Model('vr_order');
  149. $order = $mod_vorder->getOrderInfo(['order_id' => $vorder_id],'*',true);
  150. if(empty($order)) return true;
  151. global $config;
  152. $spec_card = $config['vgoods_spec_card'];
  153. $goods_id = intval($order['goods_id']);
  154. $num = intval($order['goods_num']);
  155. $goods_price = $order['goods_price'];
  156. if(array_key_exists($goods_id,$spec_card)) {
  157. $amount = $spec_card[$goods_id] * $num;
  158. } else {
  159. $amount = $goods_price * $num;
  160. }
  161. $this->mUserCards->deduct($amount);
  162. $mod_vorder->editOrder(['calctype' => self::CalcTypeVIP,'calcamount' => $amount],['order_id' => $vorder_id]);
  163. }
  164. private function deduct_invitees($vorder_id)
  165. {
  166. $model = Model('member');
  167. $num = $this->mUsedInviteesNum;
  168. $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]);
  169. $mod_vorder = Model('vr_order');
  170. $mod_vorder->editOrder(['calctype' => self::CalcTypeInvitees,'calcamount' => $num],['order_id' => $vorder_id]);
  171. }
  172. public function calc_tips()
  173. {
  174. // global $config;
  175. //
  176. // if($this->mUserId <= 0) {
  177. // return $config['tips']['first_order'];
  178. // }
  179. //
  180. // $fVip = $this->isVip();
  181. // if($fVip)
  182. // {
  183. // if($this->isFirstorOrder()) {
  184. // $tips = $config['tips']['vip_first_order'];
  185. // } else {
  186. // $tips = $config['tips']['vip_user'];
  187. // }
  188. // }
  189. // elseif($this->isFirstorOrder()) {
  190. // $tips = $config['tips']['first_order'];
  191. // } else {
  192. // $tips = $config['tips']['none_vip'];
  193. // }
  194. return "全国通用,即刻到账";
  195. }
  196. public function inviter_tips($goods_id)
  197. {
  198. if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType == self::CalcTypeInvitees)
  199. {
  200. $left_invitees = $this->left_invitees();
  201. $share_policy = $this->share_policy($goods_id);
  202. foreach ($share_policy as $item)
  203. {
  204. $num = $item['num'];
  205. if($left_invitees >= $num) {
  206. $cur = $item;
  207. break;
  208. } else {
  209. $next = $item;
  210. }
  211. }
  212. if(empty($next)) {
  213. $discount = $cur['discount'];
  214. $tip = "您已省{$discount}元,已经最优惠~";
  215. }
  216. else
  217. {
  218. $count = $next['num'] - $left_invitees;
  219. $discount = $next['discount'];
  220. if(empty($cur)) {
  221. $tip = "邀请{$count}人注册,可省{$discount}元";
  222. }
  223. else {
  224. $discount = $discount - $cur['discount'];
  225. $tip = "已省{$cur['discount']}元,再邀请{$count}人注册,还能省{$discount}元";
  226. }
  227. }
  228. return [$tip,true];
  229. }
  230. elseif($this->mCalcType == self::CalcTypeFirstOrder) {
  231. $share_policy = $this->first_order($goods_id);
  232. $tip = "已领新人优惠券,现在充值可省{$share_policy['discount']}元";
  233. return [$tip,false];
  234. }
  235. else {
  236. return ['',true];
  237. }
  238. }
  239. private function need_detuct() {
  240. return $this->mOrderCount < 10;
  241. }
  242. public function calc_vgoods_price($goods_info)
  243. {
  244. $goods_id = intval($goods_info['goods_id']);
  245. $goods_price = $goods_info['goods_price'];
  246. if($this->isExcluded($goods_id)) {
  247. $this->mCalcType = self::CalcTypeNone;
  248. return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
  249. }
  250. elseif($this->isVip()) {
  251. $this->mCalcType = self::CalcTypeVIP;
  252. $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
  253. $price = $this->mUserCards->calc_price($goods_id, $goods_price);
  254. return ['price_des' => '会员价', 'accu_price' => round($price,2)];
  255. }
  256. elseif(!empty($policy = $this->first_order($goods_id))) {
  257. $this->mCalcType = self::CalcTypeFirstOrder;
  258. $price = $policy['price'];
  259. return ['price_des' => '优惠价', 'accu_price' => round($price,2)];
  260. }
  261. elseif(!empty($policy = $this->select_invitees($goods_id))) {
  262. $this->mCalcType = self::CalcTypeInvitees;
  263. $price = $policy['price'];
  264. if($this->need_detuct()) {
  265. $this->mUsedInviteesNum = $policy['num'];
  266. } else {
  267. $this->mUsedInviteesNum = 0;
  268. }
  269. return ['price_des' => '优惠价', 'accu_price' => round($price,2)];
  270. }
  271. else {
  272. $this->mCalcType = self::CalcTypeNormal;
  273. return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
  274. }
  275. }
  276. //获取商品面额
  277. private function goods_spec_amount($goods_id,$goods_price)
  278. {
  279. global $config;
  280. $spec_card = $config['vgoods_spec_card'];
  281. if(array_key_exists($goods_id,$spec_card)) {
  282. return $spec_card[$goods_id];
  283. }
  284. else {
  285. Log::record("cannot find goods_id = {$goods_id} spec",Log::ERR);
  286. return $goods_price;
  287. }
  288. }
  289. public function calc_vorder_amount($vorder_info)
  290. {
  291. $goods_id = intval($vorder_info['goods_id']);
  292. $goods_price = $vorder_info['goods_price'];
  293. $num = $vorder_info['quantity'];
  294. if($this->isExcluded($goods_id)) {
  295. $this->mCalcType = self::CalcTypeNone;
  296. return round($goods_price * $num,2);
  297. }
  298. elseif($this->isVip()) {
  299. $this->mCalcType = self::CalcTypeVIP;
  300. $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
  301. return $this->mUserCards->calc_amount($goods_id, $goods_price * $num);
  302. }
  303. elseif(!empty($policy = $this->first_order($goods_id))) {
  304. $this->mCalcType = self::CalcTypeFirstOrder;
  305. $price = $policy['price'];
  306. return round($price * $num,2);
  307. }
  308. elseif(!empty($policy = $this->select_invitees($goods_id))) {
  309. $this->mCalcType = self::CalcTypeInvitees;
  310. $price = $policy['price'];
  311. if($this->need_detuct()) {
  312. $this->mUsedInviteesNum = $policy['num'];
  313. } else {
  314. $this->mUsedInviteesNum = 0;
  315. }
  316. return round($price * $num,2);
  317. }
  318. else {
  319. $this->mCalcType = self::CalcTypeNormal;
  320. return round($goods_price * $num,2);
  321. }
  322. }
  323. public static function onCancelVrOrder($order)
  324. {
  325. $calc_type = intval($order['calctype']);
  326. $calc_amount = $order['calcamount'];
  327. $userid = intval($order['buyer_id']);
  328. $order_id = $order['order_id'];
  329. if($calc_type === self::CalcTypeVIP)
  330. {
  331. $mod_cardkey = Model('card_key');
  332. $items = $mod_cardkey->field('*')->where(['order_id' => $order_id,'member_id' => $userid])->select();
  333. if(empty($items)) return false;
  334. $card = $items[0];
  335. $amount = $card['amount'];
  336. $cards = new user_mcards($userid);
  337. $cards->add_amount($amount);
  338. $mod_vorder = Model('vr_order');
  339. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  340. }
  341. elseif($calc_type === self::CalcTypeInvitees)
  342. {
  343. $num = intval($calc_amount);
  344. if($num > 0) {
  345. $mod_vorder = Model('vr_order');
  346. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  347. $model = Model('member');
  348. $ret = $model->editMember(['member_id' => $userid], ['used_invitees'=> ['exp', "used_invitees-{$num}"]]);
  349. return $ret;
  350. }
  351. }
  352. else {
  353. Log::record("onCancelOrder",Log::DEBUG);
  354. }
  355. return true;
  356. }
  357. }