Coupon.php 5.6 KB

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