exchange.ctrl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. $type = $_GPC['type'];
  10. $type = in_array($type, array('goods', 'coupon', 'exchange_enable')) ? $type : 'coupon';
  11. $uni_setting = pdo_get('uni_settings', array('uniacid' => $_W['uniacid']), array('exchange_enable'));
  12. if ($do == 'coupon_info') {
  13. $coupon = activity_coupon_info(intval($_GPC['id']));
  14. message(error(0, $coupon), '', 'ajax');
  15. }
  16. if ($do == 'change_status') {
  17. $id = $_GPC['id'];
  18. $status = intval($_GPC['status']);
  19. pdo_update('activity_exchange', array('status' => $status),array('uniacid' => $_W['uniacid'], 'id' => $id));
  20. message(error(0), '', 'ajax');
  21. }
  22. if ($do == 'exchange_enable') {
  23. $status = pdo_update('uni_settings', array('exchange_enable' => intval($_GPC['status'])), array('uniacid' => $_W['uniacid']));
  24. if (!empty($status)) {
  25. cache_delete("unisetting:{$_W['uniacid']}");
  26. message(error(0, '修改成功'), referer(), 'ajax');
  27. } else {
  28. message(error(-1, '修改失败'), referer(), 'ajax');
  29. }
  30. }
  31. if ($type == 'coupon') {
  32. uni_user_permission_check('activity_coupon_display');
  33. $dos = array('display', 'post', 'delete');
  34. $do = $_GPC['do'];
  35. $do = in_array($do, $dos) ? $do : 'display';
  36. if ($do == 'post') {
  37. if (checksubmit('submit')) {
  38. $start = $_GPC['coupon_start'];
  39. $end = $_GPC['coupon_end'];
  40. $post = array(
  41. 'uniacid' => $_W['uniacid'],
  42. 'extra' => $_GPC['coupon'],
  43. 'status' => intval($_GPC['status']),
  44. 'credittype' => $_GPC['credittype'],
  45. 'credit' => abs(intval($_GPC['credit'])),
  46. 'pretotal' => empty($_GPC['pretotal']) ? 1 : intval($_GPC['pretotal']),
  47. 'status' => $_GPC['status'],
  48. 'starttime' => strtotime($_GPC['date']['start']),
  49. 'endtime' => strtotime($_GPC['date']['end']),
  50. );
  51. if ($start && $end) {
  52. $start = strtotime(str_replace('.', '-', $start));
  53. $end = strtotime(str_replace('.', '-', $end));
  54. if ($start > $post['starttime'] || $end < $post['starttime'] || $start > $post['endtime'] || $end < $post['endtime']) {
  55. message('日期范围超过卡券日期范围', '', 'info');
  56. }
  57. }
  58. $post['type'] = COUPON_TYPE;
  59. if (empty($id)) {
  60. pdo_insert('activity_exchange', $post);
  61. message('添加兑换卡券成功', url('activity/exchange/coupon'), 'success');
  62. }
  63. }
  64. $id = intval($_GPC['id']);
  65. if (!empty($id)) {
  66. $data = pdo_get('activity_exchange', array('id' => $id, 'uniacid' => $_W['uniacid']));
  67. $data['coupon'] = pdo_get('coupon', array('uniacid' => $_W['uniacid'], 'id' => $data['extra']));
  68. $data['coupon']['logo_url'] = tomedia($data['coupon']['logo_url']);
  69. } else {
  70. $data['starttime'] = time();
  71. $data['endtime'] = time();
  72. }
  73. $coupons = pdo_fetchall("SELECT * FROM ". tablename('coupon')." WHERE uniacid = :uniacid AND source = ".COUPON_TYPE, array(':uniacid' => $_W['uniacid']), 'id');
  74. $coupon_exists = pdo_getall('activity_exchange', array('type' => COUPON_TYPE, 'uniacid' => $_W['uniacid']), array(), 'extra');
  75. $coupon_exists = array_keys($coupon_exists);
  76. foreach ($coupons as $key => &$coupon) {
  77. $coupon = activity_coupon_info($coupon['id']);
  78. if (in_array($key, $coupon_exists)) {
  79. unset($coupons[$key]);
  80. }
  81. }
  82. }
  83. if ($do == 'display') {
  84. $title = trim($_GPC['title']);
  85. $condition = '';
  86. $pindex = max(1, intval($_GPC['page']));
  87. $psize = 20;
  88. if (!empty($title)) {
  89. $cids = pdo_fetchall("SELECT * FROM ". tablename('coupon')." WHERE uniacid = :uniacid AND title LIKE :title AND source = :source", array('uniacid' => $_W['uniacid'], ':title' => '%'.$title.'%', ':source' => COUPON_TYPE), 'id');
  90. $cids = implode('\',\'', array_keys($cids));
  91. $condition = ' AND extra IN(\''.$cids.'\')';
  92. }
  93. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('activity_exchange')." WHERE uniacid = :uniacid AND type = :type ".$condition, array(':uniacid' => $_W['uniacid'], ':type' => COUPON_TYPE));
  94. $list = pdo_fetchall("SELECT * FROM ".tablename('activity_exchange')." WHERE uniacid = :uniacid AND type = :type ".$condition." ORDER BY id desc LIMIT ".($pindex - 1)*$psize.','. $psize, array(':uniacid' => $_W['uniacid'], ':type' => COUPON_TYPE));
  95. if (!empty($list)) {
  96. foreach($list as &$ex) {
  97. $ex['coupon'] = activity_coupon_info($ex['extra']);
  98. $ex['starttime'] = date('Y-m-d', $ex['starttime']);
  99. $ex['endtime'] = date('Y-m-d', $ex['endtime']);
  100. }unset($ex);
  101. }
  102. $pager = pagination($total, $pindex, $psize);
  103. }
  104. if ($do == 'delete') {
  105. $id = intval($_GPC['id']);
  106. $exist = pdo_get('activity_exchange', array('id' => $id, 'uniacid' => $_W['uniacid']));
  107. if (empty($exist)) {
  108. message('兑换卡券不存在', url('activity/exchange/coupon'), 'info');
  109. }
  110. pdo_delete('activity_exchange', array('id' => $id, 'uniacid' => $_W['uniacid']));
  111. message('兑换卡券删除成功', url('activity/exchange/coupon'), 'success');
  112. }
  113. }
  114. if ($type == 'goods') {
  115. uni_user_permission_check('activity_goods_display');
  116. $dos = array('display', 'post', 'del', 'record', 'deliver', 'receiver', 'record-del');
  117. $do = in_array($do, $dos) ? $do : 'display';
  118. $creditnames = array();
  119. $unisettings = uni_setting($uniacid, array('creditnames'));
  120. foreach ($unisettings['creditnames'] as $key=>$credit) {
  121. if (!empty($credit['enabled'])) {
  122. $creditnames[$key] = $credit['title'];
  123. }
  124. }
  125. if($do == 'post') {
  126. $id = intval($_GPC['id']);
  127. if(!empty($id)){
  128. $item = pdo_fetch('SELECT * FROM '.tablename('activity_exchange').' WHERE id=:id AND uniacid=:uniacid',array(':id'=>$id, ':uniacid'=>$_W['uniacid']));
  129. if(empty($item)) {
  130. message('未找到指定兑换礼品或已删除.',url('activity/exchange', array('type' => 'goods')),'error');
  131. } else {
  132. $item['extra'] = iunserializer($item['extra']);
  133. }
  134. } else {
  135. $item['starttime'] = TIMESTAMP;
  136. $item['endtime'] = TIMESTAMP + 6 * 86400;
  137. }
  138. if(checksubmit('submit')) {
  139. $data['title'] = !empty($_GPC['title']) ? trim($_GPC['title']) : message('请输入兑换名称!');
  140. $data['credittype'] = !empty($_GPC['credittype']) ? trim($_GPC['credittype']) : message('请选择积分类型!');
  141. $data['credit'] = intval($_GPC['credit']);
  142. if(empty($_GPC['extra']['title'])) {
  143. message('请输入兑换礼品的名称');
  144. }
  145. $data['extra'] = iserializer($_GPC['extra']);
  146. $data['thumb'] = trim($_GPC['thumb']);
  147. $data['status'] = trim($_GPC['status']);
  148. $data['pretotal'] = intval($_GPC['pretotal']) ? intval($_GPC['pretotal']) : message('请输入每人最大兑换次数');
  149. $data['total'] = intval($_GPC['total']) ? intval($_GPC['total']) : message('请输入兑换总数');
  150. $data['type'] = 3;
  151. $data['description'] = !empty($_GPC['description']) ? trim($_GPC['description']) : message('请输入兑换说明!');
  152. $starttime = strtotime($_GPC['datelimit']['start']);
  153. $endtime = strtotime($_GPC['datelimit']['end']);
  154. if ($endtime == $starttime) {
  155. $endtime = $endtime + 86399;
  156. }
  157. $data['starttime'] = $starttime;
  158. $data['endtime'] = $endtime;
  159. if(empty($id)) {
  160. $data['uniacid'] = $_W['uniacid'];
  161. pdo_insert('activity_exchange', $data);
  162. message('添加真实物品兑换成功',url('activity/exchange', array('type' => 'goods')),'success');
  163. } else {
  164. pdo_update('activity_exchange', $data, array('id' => $id, 'uniacid'=>$_W['uniacid']));
  165. message('更新真实物品兑换成功',url('activity/exchange', array('type' => 'goods')),'success');
  166. }
  167. }
  168. }
  169. if($do == 'display') {
  170. $pindex = max(1, intval($_GPC['page']));
  171. $psize = 10;
  172. $where = ' WHERE type = 3 AND uniacid = :uniacid ';
  173. $params = array(':uniacid' => $_W['uniacid']);
  174. $title = trim($_GPC['keyword']);
  175. if (!empty($title)) {
  176. $where .= " AND title LIKE '%{$title}%'";
  177. }
  178. $list = pdo_fetchall('SELECT * FROM '.tablename('activity_exchange')." $where ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $params);
  179. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('activity_exchange'). $where , $params);
  180. $pager = pagination($total, $pindex, $psize);
  181. foreach ($list as &$row) {
  182. $extra = iunserializer($row['extra']);
  183. $row['extra'] = $extra;
  184. $row['thumb'] = tomedia($row['thumb']);
  185. }
  186. }
  187. if($do == 'del') {
  188. $id = intval($_GPC['id']);
  189. if(!empty($id)){
  190. $item = pdo_fetch('SELECT id FROM '.tablename('activity_exchange').' WHERE id=:id AND uniacid=:uniacid',array(':id'=>$id, ':uniacid'=>$_W['uniacid']));
  191. }
  192. if(empty($item)) {
  193. message('删除失败,指定兑换不存在或已删除.');
  194. }
  195. pdo_delete('activity_exchange', array('id'=>$id, 'uniacid'=>$_W['uniacid']));
  196. message('删除成功.', referer(),'success');
  197. }
  198. if($do == 'deliver') {
  199. $exchanges = pdo_fetchall('SELECT id, title FROM ' . tablename('activity_exchange') . ' WHERE uniacid = :uniacid ORDER BY id DESC', array(':uniacid' => $_W['uniacid']));
  200. $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
  201. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  202. $where = " WHERE a.uniacid=:uniacid AND a.createtime >= :starttime AND a.createtime <= :endtime";
  203. $params = array(
  204. ':uniacid' => $_W['uniacid'],
  205. ':starttime' => $starttime,
  206. ':endtime' => $endtime,
  207. );
  208. $uid = addslashes($_GPC['uid']);
  209. if (!empty($uid)) {
  210. $where .= ' AND ((a.name=:uid) or (a.mobile = :uid))';
  211. $params[':uid'] = $uid;
  212. }
  213. $exid = intval($_GPC['exid']);
  214. if (!empty($exid)) {
  215. $where .= " AND b.id = {$exid}";
  216. }
  217. $pindex = max(1, intval($_GPC['page']));
  218. $psize = 20;
  219. $list = pdo_fetchall("SELECT a.*, b.title,b.extra,b.thumb FROM ".tablename('activity_exchange_trades_shipping'). ' AS a LEFT JOIN ' . tablename('activity_exchange') . ' AS b ON a.exid = b.id ' . " $where ORDER BY tid DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $params);
  220. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('activity_exchange_trades_shipping') . ' AS a LEFT JOIN ' . tablename('activity_exchange') . ' AS b ON a.exid = b.id '. $where , $params);
  221. if (checksubmit('export')) {
  222. $header = array(
  223. 'title' => '标题', 'extra' => '兑换物品', 'name' => '收件人','createtime' => '兑换时间', 'mobile' => '收件人电话', 'zipcode' => '收件人邮编', 'address' => '收件地址', 'status' => '状态'
  224. );
  225. $html = "\xEF\xBB\xBF";
  226. foreach ($header as $li) {
  227. $html .= $li . "\t ,";
  228. }
  229. $html .= "\n";
  230. foreach ($list as $deliver) {
  231. foreach ($header as $key => $title) {
  232. if ($key == 'createtime') {
  233. $html .= date('Y-m-d', $deliver[$key]) . "\t ,";
  234. } elseif ($key == 'extra') {
  235. $extra = iunserializer($deliver[$key]);
  236. $html .= $extra['title']. "\t ,";
  237. } elseif ($key == 'status') {
  238. switch ($deliver['status']){
  239. case '0' :
  240. $status = '待发货';
  241. break;
  242. case '1' :
  243. $status = '待收货';
  244. break;
  245. case '2' :
  246. $status = '已收货';
  247. break;
  248. case '-1' :
  249. $status = '已关闭';
  250. break;
  251. }
  252. $html .= $status . "\t ,";
  253. } else {
  254. $html .= $deliver[$key] . "\t ,";
  255. }
  256. }
  257. $html .= "\n";
  258. }
  259. $html .= "\n";
  260. header("Content-type:text/csv");
  261. header("Content-Disposition:attachment; filename=会员数据.csv");
  262. echo $html;
  263. exit();
  264. }
  265. if(!empty($list)) {
  266. $uids = array();
  267. foreach ($list as $row) {
  268. $uids[] = $row['uid'];
  269. }
  270. $members = mc_fetch($uids, array('uid', 'nickname'));
  271. foreach ($list as &$row) {
  272. $row['extra'] = iunserializer($row['extra']);
  273. $row['nickname'] = $members[$row['uid']]['nickname'];
  274. $row['thumb'] = tomedia($row['thumb']);
  275. }
  276. }
  277. $pager = pagination($total, $pindex, $psize);
  278. }
  279. if($do == 'receiver') {
  280. $id = intval($_GPC['id']);
  281. $shipping = pdo_fetch('SELECT * FROM ' . tablename('activity_exchange_trades_shipping') . ' WHERE uniacid = :uniacid AND tid = :tid', array(':uniacid' => $_W['uniacid'], ':tid' => $id) );
  282. if(checksubmit('submit')) {
  283. $data = array(
  284. 'name'=>$_GPC['realname'],
  285. 'mobile'=>$_GPC['mobile'],
  286. 'province'=>$_GPC['reside']['province'],
  287. 'city'=>$_GPC['reside']['city'],
  288. 'district'=>$_GPC['reside']['district'],
  289. 'address'=>$_GPC['address'],
  290. 'zipcode'=>$_GPC['zipcode'],
  291. 'status'=>intval($_GPC['status'])
  292. );
  293. pdo_update('activity_exchange_trades_shipping', $data, array('tid' => $id));
  294. message('更新发货人信息成功', referer(), 'success');
  295. }
  296. }
  297. if($do == 'record-del') {
  298. $tid = intval($_GPC['id']);
  299. if(empty($tid)) {
  300. message('没有指定的兑换记录', url('activity/exchange/record'), 'error');
  301. }
  302. pdo_delete('activity_exchange_trades_shipping', array('uniacid' => $_W['uniacid'], 'tid' => $tid));
  303. pdo_delete('activity_exchange_trades', array('uniacid' => $_W['uniacid'], 'tid' => $tid));
  304. message('删除兑换记录成功', url('activity/exchange/record',array('type' => 'goods')), 'success');
  305. }
  306. }
  307. template('activity/exchange');