Coupon.php 5.8 KB

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