Coupon.php 5.8 KB

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