人人商城

trade.ctrl.php 13KB

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