calc_helper.php 11 KB

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