Coupon.php 5.2 KB

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