credit2.ctrl.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. uni_user_permission_check('stat_credit2');
  8. $dos = array('index', 'chart');
  9. $do = in_array($do, $dos) ? $do : 'index';
  10. load()->model('mc');
  11. $_W['page']['title'] = "积分统计-会员中心";
  12. $starttime = empty($_GPC['time']['start']) ? mktime(0, 0, 0, date('m') , 1, date('Y')) : strtotime($_GPC['time']['start']);
  13. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  14. $num = ($endtime + 1 - $starttime) / 86400;
  15. if($do == 'index') {
  16. $clerks = pdo_getall('activity_clerks', array('uniacid' => $_W['uniacid']), array('id', 'name'), 'id');
  17. $stores = pdo_getall('activity_stores', array('uniacid' => $_W['uniacid']), array('id', 'business_name', 'branch_name'), 'id');
  18. $condition = ' WHERE uniacid = :uniacid AND credittype = :credittype AND createtime >= :starttime AND createtime <= :endtime';
  19. $params = array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => $starttime, ':endtime' => $endtime);
  20. if(intval($_W['user']['clerk_type']) == ACCOUNT_OPERATE_CLERK) {
  21. $condition .= ' AND clerk_id = :clerk_id';
  22. $params[':clerk_id'] = $_W['user']['clerk_id'];
  23. }
  24. $num = intval($_GPC['num']);
  25. if($num > 0) {
  26. if($num == 1) {
  27. $condition .= ' AND num >= 0';
  28. } else {
  29. $condition .= ' AND num <= 0';
  30. }
  31. }
  32. $min = intval($_GPC['min']);
  33. if($min > 0 ) {
  34. $condition .= ' AND abs(num) >= :minnum';
  35. $params[':minnum'] = $min;
  36. }
  37. $max = intval($_GPC['max']);
  38. if($max > 0 ) {
  39. $condition .= ' AND abs(num) <= :maxnum';
  40. $params[':maxnum'] = $max;
  41. }
  42. $clerk_id = intval($_GPC['clerk_id']);
  43. if (!empty($clerk_id)) {
  44. $condition .= ' AND clerk_id = :clerk_id';
  45. $params[':clerk_id'] = $clerk_id;
  46. }
  47. $store_id = trim($_GPC['store_id']);
  48. if (!empty($store_id)) {
  49. $condition .= " AND store_id = :store_id";
  50. $params[':store_id'] = $store_id;
  51. }
  52. $user = trim($_GPC['user']);
  53. if(!empty($user)) {
  54. $condition .= ' AND (uid IN (SELECT uid FROM '.tablename('mc_members').' WHERE uniacid = :uniacid AND (realname LIKE :username OR uid = :uid OR mobile LIKE :mobile)))';
  55. $params[':username'] = "%{$user}%";
  56. $params[':uid'] = intval($user);
  57. $params[':mobile'] = "%{$user}%";
  58. }
  59. $psize = 30;
  60. $pindex = max(1, intval($_GPC['page']));
  61. $limit = " ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ", {$psize}";
  62. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_credits_record') . $condition, $params);
  63. $data = pdo_fetchall('SELECT * FROM ' . tablename('mc_credits_record') . $condition . $limit, $params);
  64. if(!empty($data)) {
  65. load()->model('clerk');
  66. $uids = array();
  67. foreach($data as &$da) {
  68. if(!in_array($da['uid'], $uids)) {
  69. $uids[] = $da['uid'];
  70. }
  71. $operator = mc_account_change_operator($da['clerk_type'], $da['store_id'], $da['clerk_id']);
  72. $da['clerk_cn'] = $operator['clerk_cn'];
  73. $da['store_cn'] = $operator['store_cn'];
  74. }
  75. $uids = implode(',', $uids);
  76. $users = pdo_fetchall('SELECT mobile,uid,realname FROM ' . tablename('mc_members') . " WHERE uniacid = :uniacid AND uid IN ($uids)", array(':uniacid' => $_W['uniacid']), 'uid');
  77. }
  78. $pager = pagination($total, $pindex, $psize);
  79. if ($_GPC['export'] != '') {
  80. $exports = pdo_fetchall('SELECT * FROM '.tablename('mc_credits_record'). $condition . ' ORDER BY id DESC', $params);
  81. if(!empty($exports)) {
  82. load()->model('clerk');
  83. $uids = array();
  84. foreach($exports as &$da) {
  85. if(!in_array($da['uid'], $uids)) {
  86. $uids[] = $da['uid'];
  87. }
  88. $operator = mc_account_change_operator($da['clerk_type'], $da['store_id'], $da['clerk_id']);
  89. $da['clerk_cn'] = $operator['clerk_cn'];
  90. $da['store_cn'] = $operator['store_cn'];
  91. }
  92. $uids = implode(',', $uids);
  93. $user = pdo_fetchall('SELECT mobile,uid,realname FROM ' . tablename('mc_members') . " WHERE uniacid = :uniacid AND uid IN ($uids)", array(':uniacid' => $_W['uniacid']), 'uid');
  94. }
  95. $html = "\xEF\xBB\xBF";
  96. $filter = array(
  97. 'uid' => '会员编号',
  98. 'name' => '姓名',
  99. 'phone' => '手机',
  100. 'type' => '类型',
  101. 'num' => '数量',
  102. 'store' => '消费门店 ',
  103. 'operator' => '操作人 ',
  104. 'createtime' => '操作时间 ',
  105. 'remark' => '备注'
  106. );
  107. foreach ($filter as $title) {
  108. $html .= $title . "\t,";
  109. }
  110. $html .= "\n";
  111. foreach ($exports as $k => $v) {
  112. foreach ($filter as $key => $title) {
  113. if ($key == 'name') {
  114. $html .= $user[$v['uid']]['realname']. "\t, ";
  115. } elseif ($key == 'phone') {
  116. $html .= $user[$v['uid']]['mobile']. "\t, ";
  117. }elseif ($key == 'type') {
  118. if ($v['num'] > 0) {
  119. $html .= "充值\t, ";
  120. } else {
  121. $html .= "消费\t, ";
  122. }
  123. }
  124. elseif ($key == 'num') {
  125. $html .= abs($v[$key]). "\t, ";
  126. } elseif ($key == 'store') {
  127. if ($v['store_id'] > 0) {
  128. $html .= $stores[$v['store_id']]['business_name']. '-'. $stores[$v['store_id']]['branch_name']. "\t, ";
  129. } else {
  130. $html .= "未知\t, ";
  131. }
  132. }elseif ($key == 'operator') {
  133. if ($v['clerk_id'] > 0) {
  134. $html .= $v['clerk_cn']. "\t, ";
  135. } elseif ($v['clerk_type'] == 1) {
  136. $html .= "系统\t, ";
  137. }else {
  138. $html .= "未知\t, ";
  139. }
  140. }elseif ($key == 'createtime') {
  141. $html .= date('Y-m-d H:i', $v['createtime']). "\t, ";
  142. }elseif ($key == 'remark') {
  143. $html .= cutstr($v['remark'], '30', '...'). "\t, ";
  144. }else {
  145. $html .= $v[$key]. "\t, ";
  146. }
  147. }
  148. $html .= "\n";
  149. }
  150. header("Content-type:text/csv");
  151. header("Content-Disposition:attachment; filename=全部数据.csv");
  152. echo $html;
  153. exit();
  154. }
  155. }
  156. if($do == 'chart') {
  157. $today_recharge = floatval(pdo_fetchcolumn('SELECT SUM(num) FROM ' . tablename('mc_credits_record') . ' WHERE uniacid = :uniacid AND credittype = :credittype AND num > 0 AND createtime >= :starttime AND createtime <= :endtime', array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => strtotime(date('Y-m-d')), ':endtime' => TIMESTAMP)));
  158. $today_consume = floatval(pdo_fetchcolumn('SELECT SUM(num) FROM ' . tablename('mc_credits_record') . ' WHERE uniacid = :uniacid AND credittype = :credittype AND num < 0 AND createtime >= :starttime AND createtime <= :endtime', array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => strtotime(date('Y-m-d')), ':endtime' => TIMESTAMP)));
  159. $total_recharge = floatval(pdo_fetchcolumn('SELECT SUM(num) FROM ' . tablename('mc_credits_record') . ' WHERE uniacid = :uniacid AND credittype = :credittype AND num > 0 AND createtime >= :starttime AND createtime <= :endtime', array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => $starttime, ':endtime' => $endtime)));
  160. $total_consume = floatval(pdo_fetchcolumn('SELECT SUM(num) FROM ' . tablename('mc_credits_record') . ' WHERE uniacid = :uniacid AND credittype = :credittype AND num < 0 AND createtime >= :starttime AND createtime <= :endtime', array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => $starttime, ':endtime' => $endtime)));
  161. if($_W['isajax']) {
  162. $stat = array();
  163. for($i = 0; $i < $num; $i++) {
  164. $time = $i * 86400 + $starttime;
  165. $key = date('m-d', $time);
  166. $stat['consume'][$key] = 0;
  167. $stat['recharge'][$key] = 0;
  168. }
  169. $data = pdo_fetchall('SELECT id,num,credittype,createtime,uniacid FROM ' . tablename('mc_credits_record') . ' WHERE uniacid = :uniacid AND credittype = :credittype AND createtime >= :starttime AND createtime <= :endtime', array(':uniacid' => $_W['uniacid'], ':credittype' => 'credit2', ':starttime' => $starttime, ':endtime' => $endtime));
  170. if(!empty($data)) {
  171. foreach($data as $da) {
  172. $key = date('m-d', $da['createtime']);
  173. if($da['num'] > 0) {
  174. $stat['recharge'][$key] += $da['num'];
  175. } else {
  176. $stat['consume'][$key] += abs($da['num']);
  177. }
  178. }
  179. }
  180. $out['label'] = array_keys($stat['consume']);
  181. $out['datasets'] = array('recharge' => array_values($stat['recharge']), 'consume' => array_values($stat['consume']));
  182. exit(json_encode($out));
  183. }
  184. }
  185. template('stat/credit2');