trade.ctrl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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()->model('mc');
  8. load()->model('card');
  9. load()->model('module');
  10. permission_check_account_user('mc_member');
  11. $_W['page']['title'] = '会员交易-会员管理';
  12. $dos = array('consume', 'user', 'modal', 'credit', 'card', 'cardsn', 'tpl', 'cardconsume');
  13. $do = in_array($do, $dos) ? $do : 'tpl';
  14. if($do == 'user') {
  15. $type = trim($_GPC['type']);
  16. if(!in_array($type, array('uid', 'mobile'))) {
  17. $type = 'mobile';
  18. }
  19. $username = trim($_GPC['username']);
  20. $data = pdo_getall('mc_members', array('uniacid' => $_W['uniacid'], $type => $username));
  21. if(empty($data)) {
  22. exit(json_encode(array('error' => 'empty', 'message' => '没有找到对应用户')));
  23. } elseif(count($data) > 1) {
  24. exit(json_encode(array('error' => 'not-unique', 'message' => '用户不唯一,请重新输入用户信息')));
  25. } else {
  26. $card = array();
  27. $user = $data[0];
  28. $user['groupname'] = $_W['account']['groups'][$user['groupid']]['title'];
  29. $we7_coupon_info = module_fetch('we7_coupon');
  30. if (!empty($we7_coupon_info)) {
  31. $card = card_setting();
  32. $member = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $user['uid']));
  33. if(!empty($card) && $card['status'] == 1) {
  34. if(!empty($member)) {
  35. $str = "会员卡号:{$member['cardsn']}.";
  36. $user['discount'] = $card['discount'][$user['groupid']];
  37. $user['cardsn'] = $member['cardsn'];
  38. if(!empty($user['discount']) && !empty($user['discount']['discount'])) {
  39. $str .= "折扣:满{$user['discount']['condition']}元";
  40. if($card['discount_type'] == 1) {
  41. $str .= "减{$user['discount']['discount']}元";
  42. } else {
  43. $discount = $user['discount']['discount'] * 10;
  44. $str .= "打{$discount}折";
  45. }
  46. $user['discount_cn'] = $str;
  47. }
  48. } else {
  49. $user['discount_cn'] = '会员未领取会员卡,不能享受优惠';
  50. }
  51. } else {
  52. $user['discount_cn'] = '商家未开启会员卡功能';
  53. }
  54. }
  55. $html = "姓名:{$user['realname']},会员组:{$user['groupname']}<br>";
  56. if(!empty($we7_coupon_info)) {
  57. $html .= "{$user['discount_cn']}<br>";
  58. }
  59. $html .= "余额:{$user['credit2']}元,积分:{$user['credit1']}<br>";
  60. if(!empty($we7_coupon_info) && !empty($card) && $card['offset_rate'] > 0 && $card['offset_max'] > 0) {
  61. $html .= "{$card['offset_rate']}积分可抵消1元。最多可抵消{$card['offset_max']}元";
  62. }
  63. exit(json_encode(array('error' => 'none', 'user' => $user, 'html' => $html, 'card' => $card, 'group' => $_W['account']['groups'], 'grouplevel' => $_W['account']['grouplevel'])));
  64. }
  65. }
  66. if($do == 'cardsn') {
  67. $uid = intval($_GPC['uid']);
  68. $cardsn = trim($_GPC['cardsn']);
  69. $type = trim($_GPC['type']);
  70. if($_W['isajax'] && $type == 'check') {
  71. $data = pdo_get('mc_card_members', array('cardsn' => $cardsn, 'uniacid' => $_W['uniacid']));
  72. if(!empty($data) ) {
  73. exit(json_encode(array('valid' => false)));
  74. } else {
  75. exit(json_encode(array('valid' => true)));
  76. }
  77. } else {
  78. pdo_update('mc_card_members', array('cardsn' => $cardsn), array('uid' => $uid, 'uniacid' => $_W['uniacid']));
  79. exit('success');
  80. }
  81. }
  82. if($_W['isajax'] && !in_array($do, array('user', 'clerk', 'cardsn', 'cardconsume'))) {
  83. $uid = intval($_GPC['uid']);
  84. $user = pdo_get('mc_members', array('uniacid' => $_W['uniacid'], 'uid' => $uid));
  85. if(empty($user)) {
  86. exit('会员不存在');
  87. }
  88. }
  89. if($do == 'consume') {
  90. $total = $money = floatval($_GPC['total']);
  91. if(!$total) {
  92. exit('消费金额不能为空');
  93. }
  94. $log = "系统日志:会员消费【{$total}】元";
  95. load()->model('card');
  96. $user['groupname'] = $_W['account']['groups'][$user['groupid']]['title'];
  97. $card = array();
  98. $card = card_setting();
  99. $member = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $user['uid']));
  100. if(!empty($card) && $card['status'] == 1 && !empty($member)) {
  101. $user['discount'] = $card['discount'][$user['groupid']];
  102. if(!empty($user['discount']) && !empty($user['discount']['discount'])) {
  103. if($total >= $user['discount']['condition']) {
  104. $log .= ",所在会员组【{$user['groupname']}】,可享受满【{$user['discount']['condition']}】元";
  105. if($card['discount_type'] == 1) {
  106. $log .= "减【{$user['discount']['discount']}】元";
  107. $money = $total - $user['discount']['discount'];
  108. } else {
  109. $discount = $user['discount']['discount'] * 10;
  110. $log .= "打【{$discount}】折";
  111. $money = $total * $user['discount']['discount'];
  112. $money = sprintf("%.1f", $money);
  113. }
  114. if($money < 0) {
  115. $money = 0;
  116. }
  117. $log .= ",实收金额【{$money}】元";
  118. }
  119. }
  120. }
  121. $post_money = floatval($_GPC['money']);
  122. if($post_money != $money) {
  123. exit('实收金额错误');
  124. }
  125. $post_credit1 = intval($_GPC['credit1']);
  126. if($post_credit1 > 0) {
  127. if($post_credit1 > $user['credit1']) {
  128. exit('超过会员账户可用积分');
  129. }
  130. }
  131. $post_offset_money = intval($_GPC['offset_money']);
  132. $offset_money = 0;
  133. if($post_credit1 && $card['offset_rate'] > 0 && $card['offset_max'] > 0) {
  134. $offset_money = min($card['offset_max'], $post_credit1/$card['offset_rate']);
  135. if($offset_money != $post_offset_money) {
  136. exit('积分抵消金额错误');
  137. }
  138. $credit1 = $post_credit1;
  139. $log .= ",使用【{$post_credit1}】积分抵消【{$offset_money}】元";
  140. }
  141. $credit2 = floatval($_GPC['credit2']);
  142. if($credit2 > 0) {
  143. if($credit2 > $user['credit2']) {
  144. exit('超过会员账户可用余额');
  145. }
  146. $log .= ",使用余额支付【{$credit2}】元";
  147. }
  148. $cash = floatval($_GPC['cash']);
  149. $sum = $credit2 + $cash + $offset_money;
  150. $final_cash = $money - $credit2 - $offset_money;
  151. $return_cash = $sum - $money;
  152. if($sum < $money) {
  153. exit('支付金额小于实收金额');
  154. }
  155. if($cash > 0) {
  156. $log .= ",使用现金支付【{$cash}】元";
  157. }
  158. if($return_cash > 0) {
  159. $log .= ",找零【{$return_cash}】元";
  160. }
  161. if(!empty($_GPC['remark'])) {
  162. $note = "店员备注:{$_GPC['remark']}";
  163. }
  164. $log = $note.$log;
  165. if($credit2 > 0) {
  166. $status = mc_credit_update($uid, 'credit2', -$credit2, array(0, $log, 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  167. if(is_error($status)) {
  168. exit($status['message']);
  169. }
  170. }
  171. if($credit1 > 0) {
  172. $status = mc_credit_update($uid, 'credit1', -$credit1, array(0, $log, 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  173. if(is_error($status)) {
  174. exit($status['message']);
  175. }
  176. }
  177. $data = array(
  178. 'uniacid' => $_W['uniacid'],
  179. 'uid' => $uid,
  180. 'fee' => $total,
  181. 'final_fee' => $money,
  182. 'credit1' => $post_credit1,
  183. 'credit1_fee' => $offset_money,
  184. 'credit2' => $credit2,
  185. 'cash' => $cash,
  186. 'final_cash' => $final_cash,
  187. 'return_cash' => $return_cash,
  188. 'remark' => $log,
  189. 'clerk_id' => $_W['user']['clerk_id'],
  190. 'store_id' => $_W['user']['store_id'],
  191. 'clerk_type' => $_W['user']['clerk_type'],
  192. 'createtime' => TIMESTAMP,
  193. );
  194. pdo_insert('mc_cash_record', $data);
  195. $tips = "用户消费{$money}元,使用{$data['credit1']}积分,抵现{$data['credit1_fee']}元,使用余额支付{$data['credit2']}元,现金支付{$data['final_cash']}元";
  196. $recharges_set = card_params_setting('cardRecharge');
  197. $grant_rate_switch = intval($recharges_set['params']['grant_rate_switch']);
  198. $grant_credit1_enable = false;
  199. $grant_money = $money;
  200. if (!empty($card) && $card['grant_rate'] > 0 && !empty($member)) {
  201. if (empty($recharges_set['params']['recharge_type'])) {
  202. $grant_credit1_enable = true;
  203. } else {
  204. if ($grant_rate_switch == '1') {
  205. $grant_money = $data['cash'] + $data['credit2'];
  206. $grant_credit1_enable = true;
  207. } else {
  208. if (!empty($data['cash'])) {
  209. $grant_money = $data['cash'];
  210. $grant_credit1_enable = true;
  211. }
  212. }
  213. }
  214. }
  215. if(!empty($grant_credit1_enable)) {
  216. $num = floor($grant_money * $card['grant_rate']);
  217. $tips .= ",积分赠送比率为:【1:{$card['grant_rate']}】,共赠送【{$num}】积分";
  218. mc_credit_update($uid, 'credit1', $num, array(0, $tips, 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  219. }
  220. $openid = pdo_fetchcolumn('SELECT openid FROM ' . tablename('mc_mapping_fans') . ' WHERE acid = :acid AND uid = :uid', array(':acid' => $_W['acid'], ':uid' => $uid));
  221. $consume_tips = array(
  222. 'uid' => $uid,
  223. 'credit2_num' => $money,
  224. 'credit1_num' => $num,
  225. 'store' => '系统后台',
  226. 'remark' => $tips,
  227. );
  228. if(!empty($openid)) {
  229. mc_notice_consume($openid, '会员消费通知', $consume_tips);
  230. }
  231. exit('success');
  232. }
  233. if($do == 'credit') {
  234. $type = trim($_GPC['type']);
  235. $num = floatval($_GPC['num']);
  236. $names = array('credit1' => '积分', 'credit2' => '余额');
  237. $credits = mc_credit_fetch($uid);
  238. if($num < 0 && abs($num) > $credits[$type]) {
  239. exit("会员账户{$names[$type]}不够");
  240. }
  241. $status = mc_credit_update($uid, $type, $num, array($_W['user']['uid'], trim($_GPC['remark']), 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  242. if(is_error($status)) {
  243. exit($status['message']);
  244. }
  245. if($type == 'credit1') {
  246. mc_group_update($uid);
  247. }
  248. $openid = pdo_fetchcolumn('SELECT openid FROM ' . tablename('mc_mapping_fans') . ' WHERE acid = :acid AND uid = :uid', array(':acid' => $_W['acid'], ':uid' => $uid));
  249. if(!empty($openid)) {
  250. if($type == 'credit1') {
  251. mc_notice_credit1($openid, $uid, $num, '管理员后台操作积分');
  252. }
  253. if($type == 'credit2') {
  254. if($num > 0) {
  255. mc_notice_recharge($openid, $uid, $num, '', "管理员后台操作余额,增加{$value}余额");
  256. } else {
  257. mc_notice_credit2($openid, $uid, $num, 0, '', '', "管理员后台操作余额,减少{$value}余额");
  258. }
  259. }
  260. }
  261. exit('success');
  262. }
  263. if($do == 'card') {
  264. load()->model('card');
  265. $card = card_setting();
  266. if(empty($card)) {
  267. exit('公众号未设置会员卡');
  268. }
  269. $member = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $user['uid']));
  270. if(!empty($member)) {
  271. exit('该会员已领取会员卡');
  272. }
  273. $cardsn = $card['format'];
  274. preg_match_all('/(\*+)/', $card['format'], $matchs);
  275. if (!empty($matchs)) {
  276. foreach ($matchs[1] as $row) {
  277. $cardsn = str_replace($row, random(strlen($row), 1), $cardsn);
  278. }
  279. }
  280. preg_match('/(\#+)/', $card['format'], $matchs);
  281. $length = strlen($matchs[1]);
  282. $pos = strpos($card['format'], '#');
  283. $cardsn = str_replace($matchs[1], str_pad($card['snpos']++, $length - strlen($number), '0', STR_PAD_LEFT), $cardsn);
  284. $record = array(
  285. 'uniacid' => $_W['uniacid'],
  286. 'openid' => '',
  287. 'uid' => $uid,
  288. 'cid' => $card['id'],
  289. 'cardsn' => $_GPC['username'],
  290. 'status' => '1',
  291. 'createtime' => TIMESTAMP,
  292. 'endtime' => TIMESTAMP
  293. );
  294. if(pdo_insert('mc_card_members', $record)) {
  295. pdo_update('mc_card', array('snpos' => $card['snpos']), array('uniacid' => $_W['uniacid'], 'id' => $card['id']));
  296. $notice = '';
  297. if($card['grant']['credit1'] > 0) {
  298. $log = array(
  299. $uid,
  300. "领取会员卡,赠送{$card['grant']['credit1']}积分",
  301. 'system',
  302. $_W['user']['clerk_id'],
  303. $_W['user']['store_id'],
  304. $_W['user']['clerk_type']
  305. );
  306. mc_credit_update($uid, 'credit1', $card['grant']['credit1'], $log);
  307. }
  308. if($card['grant']['credit2'] > 0) {
  309. $log = array(
  310. $uid,
  311. "领取会员卡,赠送{$card['credit2']['credit1']}余额",
  312. 'system',
  313. $_W['user']['clerk_id'],
  314. $_W['user']['store_id'],
  315. $_W['user']['clerk_type']
  316. );
  317. mc_credit_update($uid, 'credit2', $card['grant']['credit2'], $log);
  318. }
  319. if (!empty($card['grant']['coupon']) && is_array($card['grant']['coupon'])) {
  320. foreach ($card['grant']['coupon'] as $grant_coupon) {
  321. load()->model('activity');
  322. activity_coupon_grant($grant_coupon['coupon'], $uid);
  323. }
  324. }
  325. exit('success');
  326. }
  327. }
  328. if ($do == 'cardconsume') {
  329. load() -> model('activity');
  330. $code = trim($_GPC['code']);
  331. $coupon_record = pdo_get('coupon_record', array('code' => $code, 'status' => '1'));
  332. if (!empty($coupon_record)) {
  333. $status = activity_coupon_use($coupon_record['couponid'], $coupon_record['id'], 'paycenter');
  334. if (is_error($status)) {
  335. exit($status['message']);
  336. }else {
  337. exit('success');
  338. }
  339. } else {
  340. exit('卡券已核销或失效');
  341. }
  342. }
  343. if($do == 'group') {
  344. $credit6 = floatval($_GPC['credit6']);
  345. $credit = $credit1 + $credit6;
  346. if($credit < 0) {
  347. exit('积分和贡献相加不能小于0');
  348. }
  349. if($credit6 != $user['credit6']) {
  350. mc_credit_update($uid, 'credit6', (-$user['credit6'] + $credit6), array(0, "通过修改贡献值,来变更会员用户组", 'group', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  351. }
  352. $groupid = $user['groupid'];
  353. $_W['member'] = $user;
  354. $_W['openid'] = pdo_fetchcolumn('SELECT openid FROM ' . tablename('mc_mapping_fans') . ' WHERE acid = :acid AND uid = :uid', array(':acid' => $_W['acid'], ':uid' => $user['uid']));
  355. mc_group_update();
  356. exit('success');
  357. }
  358. template('mc/trade');