Coupon.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Coupon
  5. {
  6. # 获取分享活动的优惠券列表
  7. public function getData($shop_id)
  8. {
  9. $data = Dever::db('shop/coupon')->getData(array('shop_id' => $shop_id));
  10. if ($data) {
  11. foreach ($data as $k => $v) {
  12. $coupon = Dever::db('goods/coupon')->find($v['coupon_id']);
  13. $data[$k]['name'] = $coupon['name'];
  14. }
  15. }
  16. return $data;
  17. }
  18. # 获取分享活动的优惠券列表
  19. public function getAll($shop_id, $act_id)
  20. {
  21. $data = Dever::db('shop/coupon_act')->getData(array('shop_id' => $shop_id, 'act_id' => $act_id));
  22. if ($data) {
  23. foreach ($data as $k => $v) {
  24. $shop_coupon = Dever::db('shop/coupon')->getOne($v['shop_coupon_id']);
  25. if ($shop_coupon && $shop_coupon['total'] > 0) {
  26. $info = Dever::db('goods/coupon')->one($shop_coupon['coupon_id']);
  27. if (!$info) {
  28. continue;
  29. }
  30. $data[$k]['cash'] = $info['cash'];
  31. $data[$k]['total_cash'] = $info['total_cash'];
  32. $data[$k]['total'] = $shop_coupon['total'];
  33. $data[$k]['shop_coupon_id'] = $v['shop_coupon_id'];
  34. $data[$k]['name'] = $info['name'];
  35. } else {
  36. continue;
  37. }
  38. }
  39. }
  40. return $data;
  41. }
  42. # 验证是否可以领取
  43. public function getOne($uid, $shop, $shop_coupon_id, $act_id = 1, $num = 1, $act = true)
  44. {
  45. if (!$shop_coupon_id) {
  46. return false;
  47. }
  48. $data = Dever::db('shop/coupon')->getOne($shop_coupon_id);
  49. if (!$data) {
  50. Dever::alert('优惠券不存在');
  51. return false;
  52. }
  53. if ($data['total'] <= 0) {
  54. return false;
  55. }
  56. if ($num > $data['total']) {
  57. $num = $data['total'];
  58. }
  59. if ($act) {
  60. $data = Dever::db('shop/coupon_act')->getOne(array('shop_id' => $shop['id'], 'act_id' => $act_id, 'shop_coupon_id' => $shop_coupon_id));
  61. if (!$data) {
  62. Dever::alert('优惠券不存在');
  63. return false;
  64. }
  65. $state = Dever::load('shop/lib/share')->getOne($uid, $shop['id'], 1);
  66. if (!$state) {
  67. Dever::alert('您还完成分享');
  68. return false;
  69. }
  70. # 验证是否已经领取,一周内不能再次领取
  71. $user = Dever::db('shop/user_coupon')->getOne(array('uid' => $uid, 'coupon_id' => $data['id']));
  72. if ($user && time() - $user['cdate'] < 86400*7) {
  73. Dever::alert('您已领取过该优惠劵');
  74. return false;
  75. }
  76. }
  77. $state = Dever::load('shop/lib/coupon.take_commit', $uid, $shop, $data, $num);
  78. if ($state) {
  79. return true;
  80. }
  81. Dever::alert('领取失败');
  82. return false;
  83. }
  84. # 领取
  85. public function take_commit($uid, $shop, $shop_coupon, $num = 1)
  86. {
  87. $add_num = 0;
  88. for ($i = 0; $i < $num; $i++) {
  89. $data['uid'] = $uid;
  90. $data['shop_id'] = $shop['id'];
  91. $data['city'] = $shop['city'];
  92. $data['coupon_id'] = $shop_coupon['coupon_id'];
  93. $data['shop_coupon_id'] = $shop_coupon['id'];
  94. $data['edate'] = time() + ($shop_coupon['day'] * 86400);
  95. //$data['cash'] = $cash;
  96. $data['t'] = $i;
  97. $id = Dever::db('shop/user_coupon')->insert($data);
  98. if ($id) {
  99. $add_num += 1;
  100. }
  101. }
  102. if ($add_num > 0) {
  103. Dever::db('shop/coupon')->updateNum(array('where_id' => $shop_coupon['id'], 'act_num' => $add_num));
  104. }
  105. return $id;
  106. }
  107. # 获取能用的优惠券的店铺名称
  108. public function getShop($data, $shop = false, $shop_list = false)
  109. {
  110. $data['shop_name'] = '门店专属';
  111. if ($shop) {
  112. $data['shop_name'] = $shop['name'];
  113. } elseif (isset($data['shop_id'])) {
  114. $shop = Dever::db('shop/info')->getOne($data['shop_id']);
  115. $data['shop_name'] = $shop['name'];
  116. }
  117. if ($data['method'] == 1) {
  118. $data['shop_name'] = '全国门店通用';
  119. } elseif ($data['method'] == 2) {
  120. $where = array('coupon_id' => $data['coupon_id'], 'city' => $data['city']);
  121. if ($shop_list) {
  122. $shop_coupon = Dever::db('shop/coupon')->select($where);
  123. $data['shop'] = array();
  124. foreach ($shop_coupon as $k1 => $v1) {
  125. //$shop = Dever::db('shop/info')->getOne(array('id' => $v1['shop_id'], 'city' => $data['city']));
  126. $shop = Dever::load('shop/lib/info')->fetch($v1['shop_id'], $data['city'], Dever::input('lng'), Dever::input('lat'));
  127. if ($shop) {
  128. $data['shop'][] = $shop;
  129. }
  130. }
  131. $shop_coupon = count($data['shop']);
  132. } else {
  133. $shop_coupon = Dever::db('shop/coupon')->total($where);
  134. }
  135. if ($shop_coupon > 1) {
  136. $city = Dever::db('area/city')->find($data['city']);
  137. $data['shop_name'] = $city['name'] . '多店通用';
  138. }
  139. }
  140. return $data;
  141. }
  142. }