calc_helper.php 13 KB

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