consume.ctrl.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $_W['page']['title'] = '卡券核销-积分兑换';
  8. load()->model('mc');
  9. uni_user_permission_check("activity_consume_coupon");
  10. $dos = array('consume', 'display', 'del');
  11. $do = in_array($do, $dos) ? $do : 'display';
  12. if (!empty($type)) {
  13. $type_title = activity_coupon_type_label($type);
  14. }
  15. if($do == 'consume') {
  16. $recid = intval($_GPC['id']);
  17. $record = pdo_get('coupon_record', array('uniacid' => $_W['uniacid'], 'id' => $recid));
  18. if(empty($record)) {
  19. message(error(-1, '兑换记录不存在'), referer(), 'ajax');
  20. }
  21. $source = intval($_GPC['source']);
  22. $clerk_name = trim($_W['user']['name']) ? trim($_W['user']['name']) : trim($_W['user']['username']);
  23. $update = array(
  24. 'status' => 3,
  25. 'usetime' => TIMESTAMP,
  26. 'clerk_id' => $_W['user']['clerk_id'],
  27. 'clerk_type' => $_W['user']['clerk_type'],
  28. 'store_id' => $_W['user']['store_id'],
  29. 'clerk_name' => $clerk_name,
  30. );
  31. if ($source == '2') {
  32. $status = $coupon_api->ConsumeCode(array('code' => $record['code']));
  33. if(is_error($status)) {
  34. if (strexists($status['message'], '40127')) {
  35. $status['message'] = '卡券已失效';
  36. pdo_update('coupon_record', array('status' => '2'), array('uniacid' => $_W['uniacid'], 'id' => $recid));
  37. }
  38. if (strexists($status['message'], '40099')) {
  39. $status['message'] = '卡券已被核销';
  40. pdo_update('coupon_record', array('status' => '3'), array('uniacid' => $_W['uniacid'], 'id' => $recid));
  41. }
  42. message(error(-1, $status['message']), '', 'ajax');
  43. }
  44. }
  45. $status = pdo_update('coupon_record', $update, array('uniacid' => $_W['uniacid'], 'id' => $recid));
  46. if (!empty($status)) {
  47. message(error(0, '核销成功'), referer(), 'ajax');
  48. }
  49. }
  50. if($do == 'display') {
  51. $source = (COUPON_TYPE == SYSTEM_COUPON) ? '1' : '2';
  52. $clerks = pdo_getall('activity_clerks', array('uniacid' => $_W['uniacid']), array('id', 'name'), 'id');
  53. $stores = pdo_getall('activity_stores', array('uniacid' => $_W['uniacid']), array('id', 'business_name', 'branch_name'), 'id');
  54. $store_ids = array_keys($stores);
  55. $nicknames_info = pdo_getall('mc_mapping_fans', array('uniacid' => $_W['uniacid']), array('nickname', 'openid'), 'openid');
  56. $condition = '';
  57. $coupons = pdo_fetchall('SELECT id, title FROM ' . tablename('coupon') . ' WHERE uniacid = :uniacid AND source = :source ORDER BY id DESC', array(':uniacid' => $_W['uniacid'], ':source' => $source), 'id');
  58. $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
  59. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  60. $type = intval($_GPC['type']);
  61. if (!empty($type)) {
  62. $condition = "AND b.type = $type";
  63. }
  64. $where = " WHERE a.uniacid = {$_W['uniacid']} ".$condition." AND b.source = {$source}";
  65. $params = array();
  66. $code = trim($_GPC['code']);
  67. if (!empty($code)) {
  68. $where .=' AND a.code=:code';
  69. $params[':code'] = $code;
  70. }
  71. $couponid = intval($_GPC['couponid']);
  72. if (!empty($couponid)) {
  73. $where .= " AND a.couponid = {$couponid}";
  74. }
  75. $clerk_id = intval($_GPC['clerk_id']);
  76. if (!empty($clerk_id)) {
  77. $where .= " AND a.clerk_id = :clerk_id";
  78. $params[':clerk_id'] = $clerk_id;
  79. }
  80. if (!empty($_GPC['nickname'])) {
  81. $nicknames = pdo_fetchall('SELECT * FROM '. tablename('mc_mapping_fans')." WHERE uniacid = :uniacid AND nickname LIKE :nickname", array(':uniacid' => $_W['uniacid'], ':nickname' => '%'.$_GPC['nickname'].'%'), 'openid');
  82. $nickname = array_keys($nicknames);
  83. $nickname = '\''.implode('\',\'', $nickname).'\'';
  84. $where .= " AND openid in ({$nickname}) ";
  85. }
  86. $status = intval($_GPC['status']);
  87. if (!empty($status)) {
  88. $where .= " AND a.status = :status";
  89. $params[':status'] = $status;
  90. } else {
  91. $where .= " AND a.status <> :status";
  92. $params[':status'] = 4;
  93. }
  94. $pindex = max(1, intval($_GPC['page']));
  95. $psize = 20;
  96. $list = pdo_fetchall("SELECT a.status AS rstatus,a.id AS recid, a.*, b.* FROM ".tablename('coupon_record'). ' AS a LEFT JOIN ' . tablename('coupon') . ' AS b ON a.couponid = b.id ' . " $where AND a.code <> '' ORDER BY a.status DESC, a.couponid DESC,a.id DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $params);
  97. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('coupon_record') . ' AS a LEFT JOIN ' . tablename('coupon') . ' AS b ON a.couponid = b.id '. $where ." AND a.code <> ''", $params);
  98. if(!empty($list)) {
  99. $uids = array();
  100. foreach ($list as &$row) {
  101. if (empty($row['store_id'])) {
  102. $row['store_name'] = '系统';
  103. } else {
  104. if (!in_array($row['store_id'], $store_ids)) {
  105. $row['store_name'] = '<span class="label label-danger">门店已删除</span>';
  106. } else {
  107. $row['store_name'] = $stores[$row['store_id']]['business_name'];
  108. }
  109. }
  110. $uids[] = $row['uid'];
  111. if($row['status'] == 2) {
  112. $operator = mc_account_change_operator($row['clerk_type'], $row['store_id'], $row['clerk_id']);
  113. $row['clerk_cn'] = $operator['clerk_cn'];
  114. $row['store_cn'] = $operator['store_cn'];
  115. }
  116. $row['extra'] = iunserializer($row['extra']);
  117. if ($row['type'] == COUPON_TYPE_DISCOUNT){
  118. $row['extra_notes'] = $row['extra']['discount'] * 0.1 . '折';
  119. } elseif ($row['type'] == COUPON_TYPE_CASH){
  120. $row['extra_notes'] = $row['extra']['reduce_cost'] * 0.01 . '元';
  121. }
  122. $date = iunserializer($row['date_info']);
  123. if ($date['time_type'] == 2) {
  124. $addtime = strtotime(date('Y-m-d', $row['addtime']));
  125. $row['starttime'] = $addtime + $date['deadline'] * 86400;
  126. $row['endtime'] = $starttime + ($date['limit'] - 1) * 86400;
  127. $row['time'] = strtotime(date('Y-m-d'));
  128. }
  129. }
  130. $members = mc_fetch($uids, array('uid', 'nickname'));
  131. foreach ($list as &$row) {
  132. $row['nickname'] = $members[$row['uid']]['nickname'];
  133. $row['logo_url'] = tomedia($row['logo_url']);
  134. }
  135. }
  136. $pager = pagination($total, $pindex, $psize);
  137. $status = array('1' => '未使用', '2' => '已使用');
  138. $clerks = pdo_getall('activity_clerks', array('uniacid' => $_W['uniacid']), array('id', 'name'), 'id');
  139. }
  140. if($do == 'del') {
  141. $recid = intval($_GPC['id']);
  142. $record = pdo_get('coupon_record', array('uniacid' => $_W['uniacid'], 'id' => $recid));
  143. if(empty($record)) {
  144. message(error(-1, '没有要删除的记录'), '', 'ajax');
  145. }
  146. $source = intval($_GPC['source']);
  147. if ($source == '2') {
  148. $status = $coupon_api->UnavailableCode(array('code' => $record['code']));
  149. if (is_error($status)) {
  150. message(error(-1, $status['message']), referer(), 'ajax');
  151. }
  152. }
  153. $status = pdo_delete('coupon_record', array('uniacid' => $_W['uniacid'], 'id' => $recid));
  154. if (!empty($status)) {
  155. message(error(0, '删除成功'), referer(), 'ajax');
  156. }
  157. }
  158. template('activity/consume');