calc_helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 isFirstorOrder()
  73. {
  74. return ($this->mOrderCount == 0);
  75. }
  76. private function left_invitees()
  77. {
  78. if($this->mUserId <= 0 || $this->mMemberInfo == null) {
  79. $left_invitees = 0;
  80. } else {
  81. $left_invitees = $this->mInvitees - $this->mMemberInfo->used_invitees();
  82. }
  83. return $left_invitees;
  84. }
  85. private function select_invitees($goods_id)
  86. {
  87. $left_invitees = $this->left_invitees();
  88. $share_policy = $this->share_policy($goods_id);
  89. if(empty($share_policy)) return false;
  90. foreach ($share_policy as $item)
  91. {
  92. $num = $item['num'];
  93. if($left_invitees >= $num) {
  94. $policy = $item;
  95. }
  96. }
  97. return empty($policy) ? false : $policy;
  98. }
  99. private function share_policy($goods_id)
  100. {
  101. global $config;
  102. $share_policy = $config['goods_share_policy'];
  103. if(array_key_exists($goods_id,$share_policy)) {
  104. return $share_policy[$goods_id];
  105. } else {
  106. return [];
  107. }
  108. }
  109. private function invitees()
  110. {
  111. $mod_member = Model('member');
  112. $ret = $mod_member->field('COUNT(*) AS inviter_count')
  113. ->where(['inviter_id' => $this->mUserId])
  114. ->select();
  115. if(empty($ret)) {
  116. return 0;
  117. }
  118. else {
  119. return intval($ret[0]['inviter_count']);
  120. }
  121. }
  122. private function isExcluded($goods_id)
  123. {
  124. global $config;
  125. $exclue_gids = $config['exclude_preferential_goods_ids'];
  126. if(empty($exclue_gids)) {
  127. return false;
  128. } else {
  129. return in_array(intval($goods_id),$exclue_gids);
  130. }
  131. }
  132. public function deduct_order($order_id)
  133. {
  134. if($this->mCalcType == self::CalcTypeVIP) {
  135. $this->detuct_mcard($order_id);
  136. }
  137. elseif($this->mCalcType == self::CalcTypeInvitees) {
  138. $this->deduct_invitees($order_id);
  139. }
  140. else {
  141. }
  142. }
  143. private function detuct_mcard($vorder_id)
  144. {
  145. $mod_vorder = Model('vr_order');
  146. $order = $mod_vorder->getOrderInfo(['order_id' => $vorder_id],'*',true);
  147. if(empty($order)) return true;
  148. global $config;
  149. $spec_card = $config['vgoods_spec_card'];
  150. $goods_id = intval($order['goods_id']);
  151. $num = intval($order['goods_num']);
  152. $goods_price = $order['goods_price'];
  153. if(array_key_exists($goods_id,$spec_card)) {
  154. $amount = $spec_card[$goods_id] * $num;
  155. } else {
  156. $amount = $goods_price * $num;
  157. }
  158. $this->mUserCards->deduct($amount);
  159. $mod_vorder->editOrder(['calctype' => self::CalcTypeVIP,'calcamount' => $amount],['order_id' => $vorder_id]);
  160. }
  161. private function deduct_invitees($vorder_id)
  162. {
  163. $model = Model('member');
  164. $num = $this->mUsedInviteesNum;
  165. $model->editMember(['member_id' => $this->mUserId], ['used_invitees'=> ['exp', "used_invitees+{$num}"]]);
  166. $mod_vorder = Model('vr_order');
  167. $mod_vorder->editOrder(['calctype' => self::CalcTypeInvitees,'calcamount' => $num],['order_id' => $vorder_id]);
  168. }
  169. public function calc_tips()
  170. {
  171. // global $config;
  172. //
  173. // if($this->mUserId <= 0) {
  174. // return $config['tips']['first_order'];
  175. // }
  176. //
  177. // $fVip = $this->isVip();
  178. // if($fVip)
  179. // {
  180. // if($this->isFirstorOrder()) {
  181. // $tips = $config['tips']['vip_first_order'];
  182. // } else {
  183. // $tips = $config['tips']['vip_user'];
  184. // }
  185. // }
  186. // elseif($this->isFirstorOrder()) {
  187. // $tips = $config['tips']['first_order'];
  188. // } else {
  189. // $tips = $config['tips']['none_vip'];
  190. // }
  191. return "全国通用,即刻到账";
  192. }
  193. public function inviter_tips($goods_id)
  194. {
  195. // $now = time();
  196. // $nine = strtotime(date('Y-m-d',$now)) + 9*3600;
  197. // $twenty_first = strtotime(date('Y-m-d',$now)) + 21*3600;
  198. //
  199. // if($now < $nine || $now > $twenty_first) {
  200. // return '试营业时间为9:00--21:00,其它时间会慢';
  201. // }
  202. if($this->mCalcType == self::CalcTypeNormal || $this->mCalcType == self::CalcTypeInvitees)
  203. {
  204. $left_invitees = $this->left_invitees();
  205. $share_policy = $this->share_policy($goods_id);
  206. foreach ($share_policy as $item)
  207. {
  208. $num = $item['num'];
  209. if($left_invitees >= $num) {
  210. $cur = $item;
  211. break;
  212. } else {
  213. $next = $item;
  214. }
  215. }
  216. if(empty($next)) {
  217. $discount = $cur['discount'];
  218. $tip = "您的分享人数,已获最高补贴{$discount}元";
  219. } else {
  220. $count = $next['num'] - $left_invitees;
  221. $discount = $next['discount'];
  222. $tip = "再分享{$count}人,可领补贴{$discount}元";
  223. }
  224. return $tip;
  225. } else {
  226. return "";
  227. }
  228. }
  229. public function calc_vgoods_price($goods_info)
  230. {
  231. $goods_id = intval($goods_info['goods_id']);
  232. $goods_price = $goods_info['goods_price'];
  233. if($this->isExcluded($goods_id)) {
  234. $this->mCalcType = self::CalcTypeNone;
  235. return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
  236. }
  237. elseif($this->isVip()) {
  238. $this->mCalcType = self::CalcTypeVIP;
  239. $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
  240. $price = $this->mUserCards->calc_price($goods_id, $goods_price);
  241. return ['price_des' => '会员价', 'accu_price' => round($price,2)];
  242. }
  243. elseif(!empty($policy = $this->select_invitees($goods_id))) {
  244. $this->mCalcType = self::CalcTypeInvitees;
  245. $price = $policy['price'];
  246. return ['price_des' => '补贴价', 'accu_price' => round($price,2)];
  247. }
  248. else {
  249. $this->mCalcType = self::CalcTypeNormal;
  250. return ['price_des' => '售价', 'accu_price' => round($goods_price,2)];
  251. }
  252. }
  253. //获取商品面额
  254. private function goods_spec_amount($goods_id,$goods_price)
  255. {
  256. global $config;
  257. $spec_card = $config['vgoods_spec_card'];
  258. if(array_key_exists($goods_id,$spec_card)) {
  259. return $spec_card[$goods_id];
  260. }
  261. else {
  262. Log::record("cannot find goods_id = {$goods_id} spec",Log::ERR);
  263. return $goods_price;
  264. }
  265. }
  266. public function calc_vorder_amount($vorder_info)
  267. {
  268. $goods_id = intval($vorder_info['goods_id']);
  269. $goods_price = $vorder_info['goods_price'];
  270. $num = $vorder_info['quantity'];
  271. if($this->isExcluded($goods_id)) {
  272. $this->mCalcType = self::CalcTypeNone;
  273. return round($goods_price * $num,2);
  274. }
  275. elseif($this->isVip()) {
  276. $this->mCalcType = self::CalcTypeVIP;
  277. $goods_price = $this->goods_spec_amount($goods_id,$goods_price);
  278. return $this->mUserCards->calc_amount($goods_id, $goods_price * $num);
  279. }
  280. elseif(!empty($policy = $this->select_invitees($goods_id))) {
  281. $this->mCalcType = self::CalcTypeInvitees;
  282. $price = $policy['price'];
  283. return round($price * $num,2);
  284. }
  285. else {
  286. $this->mCalcType = self::CalcTypeNormal;
  287. return round($goods_price * $num,2);
  288. }
  289. }
  290. public static function onCancelVrOrder($order)
  291. {
  292. $calc_type = intval($order['calctype']);
  293. $calc_amount = $order['calcamount'];
  294. $userid = intval($order['buyer_id']);
  295. $order_id = $order['order_id'];
  296. if($calc_type === self::CalcTypeVIP)
  297. {
  298. $mod_cardkey = Model('card_key');
  299. $items = $mod_cardkey->field('*')->where(['order_id' => $order_id,'member_id' => $userid])->select();
  300. if(empty($items)) return false;
  301. $card = $items[0];
  302. $amount = $card['amount'];
  303. $cards = new user_mcards($userid);
  304. $cards->add_amount($amount);
  305. $mod_vorder = Model('vr_order');
  306. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  307. }
  308. elseif($calc_type === self::CalcTypeInvitees)
  309. {
  310. $num = intval($calc_amount);
  311. if($num > 0) {
  312. $mod_vorder = Model('vr_order');
  313. $mod_vorder->editOrder(['calctype' => self::CalcTypeNormal,'calcamount' => 0],['order_id' => $order_id]);
  314. $model = Model('member');
  315. $ret = $model->editMember(['member_id' => $userid], ['used_invitees'=> ['exp', "used_invitees-{$num}"]]);
  316. return $ret;
  317. }
  318. }
  319. else {
  320. Log::record("onCancelOrder",Log::DEBUG);
  321. }
  322. return true;
  323. }
  324. }