credit1.ctrl.php 8.0 KB

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