VoucherModel.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 优惠券模块
  6. * @author fish
  7. *
  8. */
  9. class VoucherModel {
  10. public function checkUserCanGetOneVoucher( $voucher_id,$user_id,$check_count =false , $is_admin = false)
  11. {
  12. if($user_id <= 0)
  13. {
  14. return 4;
  15. }
  16. $voucher_info = M('lionfish_comshop_coupon')->where( array('id' => $voucher_id) )->find();
  17. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  18. return 1;//被抢光了
  19. }
  20. if(!$is_admin)
  21. {
  22. if($check_count) {
  23. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  24. return 1;//被抢光了
  25. }else {
  26. $get_count = M('lionfish_comshop_coupon_list')->where( array('voucher_id' => $voucher_id , 'user_id' => $user_id ) )->count();
  27. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  28. return 2;//已领过
  29. }
  30. }
  31. }
  32. //判断是否是新人专享的优惠券
  33. if( $voucher_info['is_new_man'] == 1 )
  34. {
  35. //检测是否购买过
  36. $od_status = "1,2,4,6,7,8,9,10,11,12,14";
  37. $buy_count = M('lionfish_comshop_order')->where(" order_status_id in ({$od_status}) and member_id=".$user_id )->count();
  38. if( !empty($buy_count) && $buy_count >0 )
  39. {
  40. return 4;
  41. }
  42. }
  43. }
  44. return 0;
  45. }
  46. public function send_user_voucher_byId($voucher_id,$user_id,$check_count =false, $is_admin = false)
  47. {
  48. if($user_id <= 0)
  49. {
  50. return 4;
  51. }
  52. $voucher_info = M('lionfish_comshop_coupon')->where( array('id' => $voucher_id) )->find();
  53. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  54. return 1;//被抢光了
  55. }
  56. if(!$is_admin)
  57. {
  58. if($check_count) {
  59. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  60. return 1;//被抢光了
  61. }else {
  62. $get_count = M('lionfish_comshop_coupon_list')->where( array('voucher_id' => $voucher_id , 'user_id' => $user_id ) )->count();
  63. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  64. return 2;//已领过
  65. }
  66. }
  67. }
  68. //判断是否是新人专享的优惠券
  69. if( $voucher_info['is_new_man'] == 1 )
  70. {
  71. //检测是否购买过
  72. $od_status = "1,2,4,6,7,8,9,10,11,12,14";
  73. $buy_count = M('lionfish_comshop_order')->where(" order_status_id in ({$od_status}) and member_id=".$user_id )->count();
  74. if( !empty($buy_count) && $buy_count >0 )
  75. {
  76. return 4;
  77. }
  78. }
  79. }
  80. //开始生产优惠券
  81. $begin_time = $voucher_info['begin_time'];
  82. $end_time = $voucher_info['end_time'];
  83. if( $voucher_info['timelimit'] == 0)
  84. {
  85. $begin_time = time();
  86. $end_time = time() + 3600 * $voucher_info['get_over_hour'];
  87. }
  88. $voucher = array(
  89. 'voucher_id' => $voucher_id,
  90. 'voucher_title' => $voucher_info['voucher_title'],
  91. 'user_id' => $user_id,
  92. 'store_id' => 0,
  93. 'type' => $voucher_info['type'],
  94. 'credit' => $voucher_info['credit'],
  95. 'limit_money' => $voucher_info['limit_money'],
  96. 'is_limit_goods_buy' => $voucher_info['is_limit_goods_buy'],
  97. 'limit_goods_list' => $voucher_info['limit_goods_list'],
  98. 'goodscates' => $voucher_info['goodscates'],
  99. 'consume' => 'N',
  100. 'begin_time' => $begin_time,
  101. 'end_time' => $end_time,
  102. 'add_time'=>time(),
  103. );
  104. //user_id
  105. $id = M('lionfish_comshop_coupon_list')->add( $voucher );
  106. if($id){
  107. M()->execute("update ".C('DB_PREFIX')."lionfish_comshop_coupon set send_count=send_count+1 where id =".$voucher_id );
  108. }
  109. return 3;//领取成功
  110. }
  111. /**
  112. 优惠券活动页面领券
  113. **/
  114. public function send_user_voucher_byId_frombonus($voucher_id,$user_id,$check_count =false,$is_double = false)
  115. {
  116. $voucher_info = M('voucher')->where( array('id' => $voucher_id) )->find();
  117. if($check_count) {
  118. if($voucher_info['total_count'] <= $voucher_info['send_count']){
  119. return -1;//被抢光了
  120. }else {
  121. $get_count = M('voucher_list')->where( "voucher_id={$voucher_id} and user_id={$user_id} " )->count();
  122. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  123. return -2;//已领过
  124. }
  125. }
  126. }
  127. $voucher_list_one = M('voucher_list')->where( array('voucher_id' =>$voucher_id,'user_id' =>0 ) )->order('id desc')->find();
  128. if($voucher_list_one){
  129. $credit = $voucher_list_one['credit'];
  130. //get_over_hour
  131. if($is_double)
  132. {
  133. $credit = 2 * $voucher_list_one['credit'];
  134. }
  135. $end_time = $voucher_list_one['end_time'];
  136. if( $voucher_info['get_over_hour'] > 0)
  137. {
  138. $end_time = time() + intval(3600 * $voucher_info['get_over_hour']);
  139. }
  140. M('voucher')->where( array('id' => $voucher_id) )->setInc('send_count');
  141. M('voucher_list')->where( array('id' => $voucher_list_one['id']) )->save( array('user_id' => $user_id,'end_time' => $end_time ,'credit' => $credit) );
  142. }
  143. return $voucher_list_one['id'];//领取成功
  144. }
  145. /**
  146. * 获取用户可用给当前店铺商品支付的优惠券
  147. * @param unknown $user_id
  148. * @param unknown $store_id
  149. * @param unknown $total_money
  150. */
  151. public function get_user_canpay_voucher($user_id,$store_id,$total_money, $uniacid = '',$goods_ids = array())
  152. {
  153. //ims_
  154. $voucher_list = M()->query("select * from ".C('DB_PREFIX')."lionfish_comshop_coupon_list
  155. where (is_limit_goods_buy = 0 and (limit_money = 0 or (limit_money<=".$total_money.") ) ) and (store_id=".$store_id." or store_id=0) and user_id=".$user_id." and consume='N' and begin_time<".time().' and end_time >'.time() );
  156. //-----------
  157. //var_dump( $voucher_list );
  158. //die();
  159. if( empty($voucher_list) )
  160. {
  161. $voucher_list = array();
  162. }
  163. //判断是否有限制商品的券
  164. $voucher_list_goods = M('lionfish_comshop_coupon_list')->where( "is_limit_goods_buy = 1 and (store_id={$store_id} or store_id=0) and user_id={$user_id} and consume='N' and begin_time<".time().' and end_time >'.time() )->select();
  165. if( !empty($voucher_list_goods) )
  166. {
  167. foreach($voucher_list_goods as $gd_quan)
  168. {
  169. if( empty($gd_quan['limit_goods_list']) )
  170. {
  171. //(limit_money = 0 or (limit_money<=:total_money) )
  172. if($gd_quan['limit_money'] ==0 || $gd_quan['limit_money'] <= $total_money)
  173. {
  174. $voucher_list[] = $gd_quan;
  175. }
  176. }else{
  177. $voucher_goods_ids = explode(',', $gd_quan['limit_goods_list']);
  178. $voucher_goods_ids_total_money = 0;
  179. $is_in = false;
  180. foreach($goods_ids as $key_goods_id => $money_goods_id)
  181. {
  182. if( in_array($key_goods_id, $voucher_goods_ids ) )
  183. {
  184. $voucher_goods_ids_total_money += $money_goods_id;
  185. $is_in = true;
  186. }
  187. }
  188. if( $is_in && $voucher_goods_ids_total_money >= $gd_quan['limit_money'] )
  189. {
  190. $voucher_list[] = $gd_quan;
  191. }
  192. }
  193. }
  194. }
  195. //判断是否有限制商品分类的券
  196. $voucher_list_cate = M('lionfish_comshop_coupon_list')->where( "is_limit_goods_buy = 2 and (store_id={$store_id} or store_id=0) and user_id={$user_id} and consume='N' and begin_time<".time().' and end_time >'.time() )->select();
  197. if( !empty($voucher_list_cate) )
  198. {
  199. foreach($voucher_list_cate as $gd_quan)
  200. {
  201. if( empty($gd_quan['goodscates']) )
  202. {
  203. if($gd_quan['limit_money'] ==0 || $gd_quan['limit_money'] <= $total_money)
  204. {
  205. $voucher_list[] = $gd_quan;
  206. }
  207. }else{
  208. $voucher_goods_cate = $gd_quan['goodscates'];
  209. $voucher_goods_ids_total_money = 0;
  210. $is_in = false ;
  211. foreach($goods_ids as $key_goods_id => $money_goods_id)
  212. {
  213. $cate_gd_arr = M('lionfish_comshop_goods_to_category')->field('cate_id')->where( array('goods_id' => $key_goods_id) )->select();
  214. if( !empty($cate_gd_arr) )
  215. {
  216. foreach($cate_gd_arr as $cate_val)
  217. {
  218. if( $cate_val['cate_id'] == $voucher_goods_cate )
  219. {
  220. $is_in = true;
  221. $voucher_goods_ids_total_money += $money_goods_id;
  222. }
  223. }
  224. }
  225. }
  226. if($is_in && $voucher_goods_ids_total_money >= $gd_quan['limit_money'] )
  227. {
  228. $voucher_list[] = $gd_quan;
  229. }
  230. }
  231. }
  232. }
  233. //---------------
  234. if( !empty($voucher_list) )
  235. {
  236. foreach($voucher_list as $key => $val)
  237. {
  238. $val['begin_time'] = date('Y-m-d H:i:s', $val['begin_time']);
  239. $val['end_time'] = date('Y-m-d H:i:s', $val['end_time']);
  240. //---begin
  241. $coupon_info = M('lionfish_comshop_coupon')->where( array('id' =>$val['voucher_id'] ) )->find();
  242. if( $coupon_info['catid'] > 0 )
  243. {
  244. $cate_info = M('lionfish_comshop_coupon_category')->where( array('id' => $coupon_info['catid']) )->find();
  245. $val['cate_name'] = $cate_info['name'];
  246. }else{
  247. $val['cate_name'] = '';
  248. }
  249. //--end
  250. $voucher_list[$key] = $val;
  251. }
  252. }
  253. //credit
  254. $last_index_sort = array_column($voucher_list, 'credit');
  255. array_multisort($last_index_sort, SORT_DESC, $voucher_list);
  256. return $voucher_list;
  257. }
  258. /**
  259. * @author cy 2021-01-03
  260. * @desc 获取优惠券最大可以优惠金额
  261. * @param unknown $vouche_list 优惠券列表
  262. * @param unknown $goods 购买商品列表
  263. * @param unknown $reduce_money 满级金额
  264. * @return number
  265. */
  266. public function get_voucher_amout_bygoods($vouche_list, $goods, $reduce_money){
  267. $order_total = 0;
  268. foreach($goods as $k=>$v){
  269. $order_total = $order_total + round($v['price']*$v['quantity'],2);
  270. }
  271. if(!empty($vouche_list)){
  272. foreach($vouche_list as $vk=>$vv){
  273. $vouche_amount = 0;//优惠券优惠总金额
  274. $limit_money = $vv['limit_money'];
  275. if(empty($limit_money)){
  276. $limit_money = 0;
  277. }
  278. $goods_amount = 0;
  279. if($vv['is_limit_goods_buy'] == 0){//所有商品
  280. foreach($goods as $k=>$v){
  281. if($v['level_total'] > 0){
  282. $goods_amount = $goods_amount + $v['level_total'];
  283. }else{
  284. $goods_amount = $goods_amount + $v['total'];
  285. }
  286. }
  287. }else if($vv['is_limit_goods_buy'] == 1){//指定商品
  288. $goods_array = explode(',', $vv['limit_goods_list']);
  289. foreach($goods as $k=>$v){
  290. if(in_array($v['goods_id'], $goods_array)){
  291. if($v['level_total'] > 0){
  292. $goods_amount = $goods_amount + $v['level_total'];
  293. }else{
  294. $goods_amount = $goods_amount + $v['total'];
  295. }
  296. }
  297. }
  298. }else if($vv['is_limit_goods_buy'] == 2){//指定商品分类
  299. $goodscates = $vv['goodscates'];
  300. foreach($goods as $k=>$v){
  301. $goods_id = $v['goods_id'];
  302. $cate_gd_arr = M('lionfish_comshop_goods_to_category')->field('cate_id')->where( array('goods_id' => $goods_id) )->select();
  303. if( !empty($cate_gd_arr) )
  304. {
  305. foreach($cate_gd_arr as $cate_val)
  306. {
  307. if( $cate_val['cate_id'] == $goodscates )
  308. {
  309. if($v['level_total'] > 0){
  310. $goods_amount = $goods_amount + $v['level_total'];
  311. }else{
  312. $goods_amount = $goods_amount + $v['total'];
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. if($goods_amount >= $limit_money){
  320. if($goods_amount >= $vv['credit']){
  321. $vouche_amount = $vv['credit'];
  322. }else{
  323. $vouche_amount = $goods_amount;
  324. }
  325. if($vouche_amount > $order_total - $reduce_money){
  326. $vouche_amount = $order_total - $reduce_money;
  327. }
  328. }
  329. $vouche_list[$vk]['can_vouche_amount'] = $vouche_amount;
  330. }
  331. }
  332. return $vouche_list;
  333. }
  334. /**
  335. * @author cy 2021-03-12
  336. * @desc 邀新有礼送优惠券
  337. * @param $voucher_id
  338. * @param $user_id
  339. * @param $invite_id
  340. * @param $receive_type
  341. * @return int
  342. */
  343. public function send_invitegift_coupon_byId($voucher_id ,$user_id, $invite_id ,$receive_type)
  344. {
  345. if($user_id <= 0)
  346. {
  347. return ['code'=>4];
  348. }
  349. $voucher_info = M('lionfish_comshop_coupon')->where( array('id' => $voucher_id) )->find();
  350. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  351. return ['code'=>1];//被抢光了
  352. }else {
  353. $get_count = M('lionfish_comshop_coupon_list')->where( array('voucher_id' => $voucher_id , 'user_id' => $user_id ) )->count();
  354. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  355. return ['code'=>2];//已领过
  356. }
  357. }
  358. //判断是否是新人专享的优惠券
  359. if( $voucher_info['is_new_man'] == 1 )
  360. {
  361. //检测是否购买过
  362. $od_status = "1,2,4,6,7,8,9,10,11,12,14";
  363. $buy_count = M('lionfish_comshop_order')->where(" order_status_id in ({$od_status}) and member_id=".$user_id )->count();
  364. if( !empty($buy_count) && $buy_count >0 )
  365. {
  366. return ['code'=>3];//不是新人
  367. }
  368. }
  369. //开始生产优惠券
  370. $begin_time = $voucher_info['begin_time'];
  371. $end_time = $voucher_info['end_time'];
  372. if( $voucher_info['timelimit'] == 0)
  373. {
  374. $begin_time = time();
  375. $end_time = time() + 3600 * $voucher_info['get_over_hour'];
  376. }
  377. $voucher = array(
  378. 'voucher_id' => $voucher_id,
  379. 'voucher_title' => $voucher_info['voucher_title'],
  380. 'user_id' => $user_id,
  381. 'store_id' => 0,
  382. 'type' => $voucher_info['type'],
  383. 'credit' => $voucher_info['credit'],
  384. 'limit_money' => $voucher_info['limit_money'],
  385. 'is_limit_goods_buy' => $voucher_info['is_limit_goods_buy'],
  386. 'limit_goods_list' => $voucher_info['limit_goods_list'],
  387. 'goodscates' => $voucher_info['goodscates'],
  388. 'consume' => 'N',
  389. 'begin_time' => $begin_time,
  390. 'end_time' => $end_time,
  391. 'receive_type' => $receive_type,
  392. 'receive_id' => $invite_id,
  393. 'add_time'=>time(),
  394. );
  395. //user_id
  396. $id = M('lionfish_comshop_coupon_list')->add( $voucher );
  397. if($id){
  398. M()->execute("update ".C('DB_PREFIX')."lionfish_comshop_coupon set send_count=send_count+1 where id =".$voucher_id );
  399. }
  400. return ['code' => 0, 'id'=> $id];//领取成功
  401. }
  402. }