coupon.ctrl.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('tpl');
  8. load()->model('card');
  9. $dos = array('display', 'exchange', 'mine', 'use', 'detail', 'qrcode', 'opencard', 'addcard');
  10. $do = in_array($_GPC['do'], $dos) ? $_GPC['do'] : 'display';
  11. activity_coupon_type_init();
  12. $colors = activity_coupon_colors();
  13. if($do == 'display') {
  14. if ($unisettings['exchange_enable'] != '1') {
  15. message('未开启兑换功能');
  16. }
  17. $user = mc_fetch($_W['member']['uid'], array('groupid'));
  18. $fan = pdo_get('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));
  19. $groupid = $user['groupid'];
  20. $exchanges = pdo_fetchall("SELECT * FROM ". tablename('activity_exchange')." WHERE uniacid = :uniacid AND type = ".COUPON_TYPE." AND status = '1' AND starttime <= :time AND endtime >= :time", array(':uniacid' => $_W['uniacid'], ':time' => strtotime(date('Y-m-d'))), 'extra');
  21. foreach ($exchanges as $key => &$list) {
  22. $coupon_info = activity_coupon_info($list['extra']);
  23. $exchange_lists[$list['extra']] = $coupon_info;
  24. $person_total = pdo_fetchcolumn("SELECT COUNT(*) FROM ". tablename('coupon_record')." WHERE uid = :uid AND uniacid = :uniacid AND couponid = :couponid", array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid'], ':couponid' => $list['extra']));
  25. if ($person_total >= $list['pretotal'] || $person_total >= $coupon_info['get_limit']){
  26. unset($exchange_lists[$list['extra']]);
  27. continue;
  28. }
  29. $stock = pdo_fetchcolumn("SELECT COUNT(*) FROM ". tablename('coupon_record')." WHERE uniacid = :uniacid AND couponid = :couponid", array(':uniacid' => $_W['uniacid'], ':couponid' => $list['extra']));
  30. if ($stock > $coupon_info['quantity']) {
  31. unset($exchange_lists[$list['extra']]);
  32. continue;
  33. }
  34. $coupon_groups = pdo_getall('coupon_groups', array('uniacid' => $_W['uniacid'], 'couponid' => $list['extra']), array(), 'groupid');
  35. $coupon_groups = array_keys($coupon_groups);
  36. if (COUPON_TYPE == WECHAT_COUPON) {
  37. $fan_groups = explode(',', $fan['tag']);
  38. $group = array_intersect($coupon_groups, $fan_groups);
  39. } else {
  40. $group = pdo_get('coupon_groups', array('uniacid' => $_W['uniacid'], 'couponid' => $list['extra'], 'groupid' => $groupid));
  41. }
  42. if (empty($group) && !empty($coupon_groups)) {
  43. unset($exchange_lists[$list['extra']]);
  44. continue;
  45. }
  46. if (!empty($_W['current_module'])) {
  47. $coupon_modules = pdo_getall('coupon_modules', array('uniacid' => $_W['uniacid'], 'couponid' => $list['extra']), array(), 'module');
  48. if (!empty($coupon_modules) && !in_array($_W['current'], $coupon_modules)) {
  49. unset($exchange_lists[$list['extra']]);
  50. continue;
  51. }
  52. }
  53. $exchange_lists[$list['extra']]['extra_href'] = url('activity/coupon/exchange');
  54. if (!empty($exchange_lists[$list['extra']])) {
  55. $exchange_lists[$list['extra']]['extra_func'] = $list;
  56. $exchange_lists[$list['extra']]['extra_func']['pic'] = 'resource/images/icon-signed.png';
  57. }
  58. }
  59. }
  60. if($do == 'exchange') {
  61. if ($unisettings['exchange_enable'] != '1') {
  62. message(error(-1, '未开启兑换功能'), '', 'ajax');
  63. }
  64. $id = intval($_GPC['id']);
  65. $activity_exchange = pdo_get('activity_exchange', array('extra' => $id));
  66. $credit = mc_credit_fetch($_W['member']['uid'], array($activity_exchange['credittype']));
  67. if ($activity_exchange['credit'] < 0) {
  68. message(error(-1, '兑换' . $creditnames[$activity_exchange['credittype']] . '有误'), '', 'ajax');
  69. }
  70. if (intval($credit[$activity_exchange['credittype']]) < $activity_exchange['credit']) {
  71. message(error(-1, $creditnames[$activity_exchange['credittype']] . '不足'), '', 'ajax');
  72. }
  73. $pcount = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('coupon_record') . " WHERE `openid` = :openid AND `couponid` = :couponid", array(':couponid' => $coupon['id'], ':openid' => $_W['fans']['openid']));
  74. if ($pcount > $activity_exchange['pretotal']) {
  75. message(error(-1, '领取数量超限'), '', 'ajax');
  76. }
  77. if ($activity_exchange['starttime'] > strtotime(date('Y-m-d'))) {
  78. message(error(-1, '活动未开始'), '', 'ajax');
  79. }
  80. if ($activity_exchange['endtime'] < strtotime(date('Y-m-d'))) {
  81. message(error(-1, '活动已结束'), '', 'ajax');
  82. }
  83. $status = activity_coupon_grant($id, $_W['member']['uid']);
  84. if (is_error($status)) {
  85. message(error(-1, $status['message']), '', 'ajax');
  86. } else {
  87. mc_credit_update($_W['member']['uid'], $activity_exchange['credittype'], -1 * $activity_exchange['credit']);
  88. if ($activity_exchange['credittype'] == 'credit1') {
  89. mc_notice_credit1($_W['openid'], $_W['member']['uid'], -1 * $activity_exchange['credit'], '兑换卡券消耗积分');
  90. } else {
  91. $card_setting = pdo_get('mc_card', array('uniacid' => $_W['uniacid']));
  92. $recharges_set = card_params_setting('cardRecharge');
  93. if (empty($recharges_set['params']['recharge_type'])) {
  94. $grant_rate = $card_setting['grant_rate'];
  95. mc_credit_update($_W['member']['uid'], 'credit1', $grant_rate * $activity_exchange['credit']);
  96. }
  97. mc_notice_credit2($_W['openid'], $_W['member']['uid'], -1 * $activity_exchange['credit'], $grant_rate * $activity_exchange['credit'], '兑换卡券消耗余额');
  98. }
  99. message(error(0, '兑换成功'), url('activity/coupon/mine'), 'ajax');
  100. }
  101. }
  102. if($do == 'mine') {
  103. $title = '我的卡券';
  104. activity_coupon_give();
  105. $coupon_records = activity_coupon_owned();
  106. }
  107. if($do == 'use') {
  108. $recid = intval($_GPC['recid']);
  109. $coupon_record = pdo_get('coupon_record', array('id' => $recid));
  110. $coupon_info = activity_coupon_info(trim($coupon_record['couponid']));
  111. $coupon_info['color'] = $colors[$coupon_info['color']] ? $colors[$coupon_info['color']] : '#63b359';
  112. if ($coupon_info['date_info']['time_type'] == '2') {
  113. $starttime = strtotime(date('Y-m-d', $coupon_record['addtime'])) + $coupon_info['date_info']['deadline'] * 86400;
  114. $endtime = $starttime + ($coupon_info['date_info']['limit'] - 1) * 86400;
  115. $coupon_info['extra_date_info'] = '有效期:' . date('Y.m.d', $starttime) . '-' . date('Y.m.d', $endtime);
  116. }
  117. }
  118. if ($do == 'detail') {
  119. $couponid = intval($_GPC['couponid']);
  120. $coupon_record = pdo_get('coupon_record', array('id' => intval($_GPC['recid'])));
  121. $coupon_info = activity_coupon_info($couponid);
  122. $coupon_info['description'] = $coupon_info['description'] ? $coupon_info['description'] : '暂无说明';
  123. if ($coupon_info['type'] == '1') {
  124. $coupon_info['discount_info'] = '凭此券消费打' . $coupon_info['extra']['discount'] * 0.1 . '折';
  125. } else {
  126. $coupon_info['discount_info'] = '价值' . $coupon_info['extra']['reduce_cost'] * 0.01 . '元代金券一张,消费满' . $coupon_info['extra']['least_cost'] * 0.01 . '元可使用';
  127. }
  128. if ($coupon_info['date_info']['time_type'] == '1') {
  129. $coupon_info['detail_date_info'] = $coupon_info['date_info']['time_limit_start'] . '-' . $coupon_info['date_info']['time_limit_end'];
  130. } else {
  131. $starttime = $coupon_record['addtime'] + $coupon_info['date_info']['deadline'] * 86400;
  132. $endtime = $starttime + ($coupon_info['date_info']['limit'] - 1) * 86400;
  133. $coupon_info['detail_date_info'] = date('Y.m.d', $starttime) . '-' . date('Y.m.d', $endtime);
  134. }
  135. }
  136. if ($do == 'qrcode') {
  137. $couponid = intval($_GPC['couponid']);
  138. $recid = intval($_GPC['recid']);
  139. $coupon_info = activity_coupon_info($couponid);
  140. $coupon_info['color'] = $colors[$coupon_info['color']] ? $colors[$coupon_info['color']] : '#63b359';
  141. $code_info = pdo_get('coupon_record', array('id' => $recid), array('code'));
  142. $coupon_info['code'] = $code_info['code'];
  143. }
  144. if ($do == 'opencard') {
  145. $id = intval($_GPC['id']);
  146. $code = trim($_GPC['code']);
  147. if($_W['isajax'] && $_W['ispost']) {
  148. $card = $coupon_api->BuildCardExt($id);
  149. if (is_error($card)) {
  150. message(error(1, $card['message']), '', 'ajax');
  151. } else {
  152. $openCard['card_id'] = $card['card_id'];
  153. $openCard['code'] = $code;
  154. message(error(0, $openCard), '', 'ajax');
  155. }
  156. }
  157. }
  158. if ($do == 'addcard') {
  159. $id = intval($_GPC['id']);
  160. if($_W['isajax'] && $_W['ispost']) {
  161. $card = $coupon_api->BuildCardExt($id);
  162. if (is_error($card)) {
  163. message(error(1, $card['message']), '', 'ajax');
  164. } else {
  165. message(error(0, $card), url('activity/coupon/mine'), 'ajax');
  166. }
  167. }
  168. }
  169. template('activity/coupon');