activity.mod.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. function activity_coupon_type_init() {
  8. global $_W;
  9. static $coupon_api;
  10. if (!defined('COUPON_TYPE')) {
  11. load()->classs('coupon');
  12. if(empty($coupon_api)) {
  13. $coupon_api = new coupon();
  14. }
  15. $is_coupon_supported = $coupon_api->isCouponSupported();
  16. if (!empty($is_coupon_supported)) {
  17. define('COUPON_TYPE', WECHAT_COUPON);
  18. } else {
  19. define('COUPON_TYPE', SYSTEM_COUPON);
  20. }
  21. }
  22. }
  23. function activity_coupon_available(){
  24. activity_coupon_type_init();
  25. global $_W;
  26. $condition = array('uniacid' => $_W['uniacid'], 'is_display' => 1, 'source' => COUPON_TYPE);
  27. if (!empty($_W['current_module']['name'])) {
  28. $coupon_modules = pdo_fetchall("SELECT a.id FROM " . tablename('coupon') . " as a LEFT JOIN " . tablename('coupon_modules') . " as b ON a.id = b.couponid WHERE b.couponid is null or b.module = :module AND a.uniacid = :uniacid", array(':uniacid' => $_W['uniacid'], ':module' => 'shopping'), 'id');
  29. if (!empty($coupon_modules)) {
  30. $condition['id'] = array_keys($coupon_modules);
  31. }
  32. }
  33. $coupons = pdo_getall('coupon', $condition);
  34. if (!empty($coupons)) {
  35. foreach ($coupons as $key => &$coupon) {
  36. $coupon = activity_coupon_info($coupon['id']);
  37. $endtime = strtotime(str_replace('.', '-', $coupon['time_limit_end']));
  38. $time = strtotime(date('Y-m-d'));
  39. if ( ($coupon['time_type']['time_type'] == 1 && $endtime > $time) || $coupon['quantity'] <= 0) {
  40. unset($coupons[$key]);
  41. }
  42. }
  43. }
  44. return $coupons;
  45. }
  46. function activity_coupon_user_available() {
  47. global $_W;
  48. load()->model('mc');
  49. $uid = $_W['member']['uid'];
  50. $user = mc_fetch($uid, array('groupid'));
  51. $fan = mc_fansinfo($uid, '', $_W['uniacid']);
  52. $openid = $_W['openid'];
  53. $groupid = $user['groupid'];
  54. $coupons = activity_coupon_available();
  55. foreach ($coupons as $key => &$coupon) {
  56. $coupon = activity_coupon_info($coupon['id']);
  57. $person_total = pdo_getcolumn('coupon_record', array('uniacid' => $_W['uniacid'], 'openid' => $_W['openid'], 'couponid' => $coupon['id']), 'COUNT(*)');
  58. if ($person_total > $coupon['get_limit']) {
  59. unset($coupons[$key]);
  60. continue;
  61. }
  62. $coupon_groups = pdo_getall('coupon_groups', array('uniacid' => $_W['uniacid'], 'couponid' => $coupon['id']), array(), 'groupid');
  63. $coupon_groups = array_keys($coupon_groups);
  64. if (COUPON_TYPE == WECHAT_COUPON) {
  65. $fan_groups = explode(',', $fan['tag']);
  66. $group = array_intersect($coupon_groups, $fan_groups);
  67. } else {
  68. $group = pdo_get('coupon_groups', array('uniacid' => $_W['uniacid'], 'couponid' => $coupon['id'], 'groupid' => $groupid));
  69. }
  70. if (empty($group) && !empty($coupon_groups)) {
  71. unset($coupons[$key]);
  72. continue;
  73. }
  74. }
  75. unset($coupon);
  76. return $coupons;
  77. }
  78. function activity_coupon_owned() {
  79. global $_W, $_GPC;
  80. $uid = $_W['member']['uid'];
  81. $param = array('uniacid' => $_W['uniacid'], 'openid' => $_W['openid'], 'status' => 1);
  82. $data = pdo_getall('coupon_record', $param);
  83. foreach ($data as $key => $record) {
  84. $coupon = activity_coupon_info($record['couponid']);
  85. if ($coupon['source'] != COUPON_TYPE) {
  86. unset($data[$key]);
  87. continue;
  88. }
  89. if ($coupon['status'] != '3') {
  90. pdo_delete('coupon_record', array('id' => $record['id']));
  91. unset($data[$key]);
  92. continue;
  93. }
  94. if (is_error($coupon)) {
  95. unset($data[$key]);
  96. continue;
  97. }
  98. $modules = array();
  99. if (!empty($coupon['modules'])) {
  100. foreach ($coupon['modules'] as $module) {
  101. $modules[] = $module['name'];
  102. }
  103. }
  104. if (!empty($modules) && !in_array($_W['current_module']['name'], $modules) && !empty($_W['current_module']['name'])) {
  105. unset($data[$key]);
  106. continue;
  107. }
  108. if (is_array($coupon['date_info']) && $coupon['date_info']['time_type'] == '2') {
  109. $starttime = $record['addtime'] + $coupon['date_info']['deadline'] * 86400;
  110. $endtime = $starttime + ($coupon['date_info']['limit'] - 1) * 86400;
  111. if ($endtime < time()) {
  112. unset($data[$key]);
  113. pdo_delete('coupon_record', array('id' => $record['id']));
  114. continue;
  115. } else {
  116. $coupon['extra_date_info'] = '有效期:' . date('Y.m.d', $starttime) . '-' . date('Y.m.d', $endtime);
  117. }
  118. }
  119. if (is_array($coupon['date_info']) && $coupon['date_info']['time_type'] == '1') {
  120. $endtime = str_replace('.', '-', $coupon['date_info']['time_limit_end']);
  121. $endtime = strtotime($endtime);
  122. if ($endtime < time()) {
  123. pdo_delete('coupon_record', array('id' => $record['id']));
  124. unset($data[$key]);
  125. continue;
  126. }
  127. }
  128. if ($coupon['type'] == COUPON_TYPE_DISCOUNT) {
  129. $coupon['icon'] = '<div class="price">' . $coupon['extra']['discount'] * 0.1 . '<span>折</span></div>';
  130. }
  131. elseif($coupon['type'] == COUPON_TYPE_CASH) {
  132. $coupon['icon'] = '<div class="price">' . $coupon['extra']['reduce_cost'] * 0.01 . '<span>元</span></div><div class="condition">满' . $coupon['extra']['least_cost'] * 0.01 . '元可用</div>';
  133. }
  134. elseif($coupon['type'] == COUPON_TYPE_GIFT) {
  135. $coupon['icon'] = '<img src="resource/images/wx_gift.png" alt="" />';
  136. }
  137. elseif($coupon['type'] == COUPON_TYPE_GROUPON) {
  138. $coupon['icon'] = '<img src="resource/images/groupon.png" alt="" />';
  139. }
  140. elseif($coupon['type'] == COUPON_TYPE_GENERAL) {
  141. $coupon['icon'] = '<img src="resource/images/general_coupon.png" alt="" />';
  142. }
  143. $data[$key] = $coupon;
  144. $data[$key]['recid'] = $record['id'];
  145. $data[$key]['code'] = $record['code'];
  146. if ($coupon['source'] == '2') {
  147. if (empty($data[$key]['code'])) {
  148. $data[$key]['extra_ajax'] = url('entry', array('m' => 'we7_coupon', 'do' => 'activity', 'type' => 'coupon', 'op' => 'addcard'));
  149. } else {
  150. $data[$key]['extra_ajax'] = url('entry', array('m' => 'we7_coupon', 'do' => 'activity', 'type' => 'coupon', 'op' => 'opencard'));
  151. }
  152. }
  153. }
  154. return $data;
  155. }
  156. function activity_coupon_info($id) {
  157. global $_W;
  158. activity_coupon_type_init();
  159. $id = intval($id);
  160. if (empty($_W['current_module']) && !empty($GLOBALS['site']) && is_object($GLOBALS['site']) && $GLOBALS['site'] instanceof WeModuleSite && !empty($GLOBALS['site']->module)) {
  161. $_W['current_module'] = $GLOBALS['site']->module;
  162. }
  163. $coupon = pdo_get('coupon', array('uniacid' => $_W['uniacid'], 'id' => $id));
  164. if (empty($coupon)) {
  165. return error(1, '卡券不存在或是已经被删除');
  166. }
  167. $coupon['date_info'] = iunserializer($coupon['date_info']);
  168. if ($coupon['date_info']['time_type'] == '1'){
  169. $coupon['extra_date_info'] = '有效期:' . $coupon['date_info']['time_limit_start'] . '-' . $coupon['date_info']['time_limit_end'];
  170. } else {
  171. $coupon['extra_date_info'] = '有效期:领取后' . $coupon['date_info']['deadline'] . '天可用,有效期' . $coupon['date_info']['limit'] . '天';
  172. }
  173. if ($coupon['type'] == COUPON_TYPE_DISCOUNT) {
  174. $coupon['extra'] = iunserializer($coupon['extra']);
  175. $coupon['extra_instruction'] = '凭此券消费打' . $coupon['extra']['discount'] * 0.1 . '折';
  176. } elseif ($coupon['type'] == COUPON_TYPE_CASH) {
  177. $coupon['extra'] = iunserializer($coupon['extra']);
  178. $coupon['extra_instruction'] = '消费满' . $coupon['extra']['least_cost'] * 0.01 . '元,减' . $coupon['extra']['reduce_cost'] * 0.01 . '元';
  179. } else {
  180. }
  181. $coupon['logo_url'] = tomedia($coupon['logo_url']);
  182. $coupon['description'] = htmlspecialchars_decode($coupon['description']);
  183. $modules = pdo_getall('coupon_modules', array('uniacid' => $_W['uniacid'], 'couponid' => $coupon['id']), array(), 'module');
  184. if (!empty($modules)) {
  185. $unimodules = uni_modules();
  186. foreach ($modules as $modulename => $row) {
  187. $coupon['modules'][$modulename] = array('title' => $unimodules[$modulename]['title'], 'name' => $modulename);
  188. }
  189. }
  190. $stores = pdo_getall('coupon_store', array('uniacid' => $_W['uniacid'], 'couponid' => $coupon['id']), array(), 'storeid');
  191. if (!empty($stores)) {
  192. $stores = pdo_getall('activity_stores', array('id' => array_keys($stores)), array('business_name', 'location_id', 'id'), 'id');
  193. $coupon['location_id_list'] = $stores;
  194. }
  195. return $coupon;
  196. }
  197. function activity_coupon_grant($id,$openid) {
  198. activity_coupon_type_init();
  199. global $_W, $_GPC;
  200. if (empty($openid)) {
  201. $openid = $_W['openid'];
  202. if(empty($openid)) {
  203. $openid = $_W['member']['uid'];
  204. }
  205. if (empty($openid)) {
  206. return error(-1, '没有找到指定会员');
  207. }
  208. }
  209. $fan = mc_fansinfo($openid, '', $_W['uniacid']);
  210. $openid = $fan['openid'];
  211. if (empty($openid)) {
  212. return error(-1, '兑换失败');
  213. }
  214. $code = base_convert(md5(uniqid() . random(4)), 16, 10);
  215. $code = substr($code, 1, 16);
  216. $user = mc_fetch($fan['uid'], array('groupid'));
  217. $credit_names = array('credit1' => '积分', 'credit2' => '余额');
  218. $coupon = activity_coupon_info($id);
  219. $pcount = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('coupon_record') . " WHERE `openid` = :openid AND `couponid` = :couponid", array(':couponid' => $id, ':openid' => $openid));
  220. $coupongroup = pdo_fetchall("SELECT * FROM " . tablename('coupon_groups') . " WHERE `couponid` = :couponid", array(':couponid' => $id), 'groupid');
  221. $coupon_group = array_keys($coupongroup);
  222. $member = pdo_get('mc_members', array('uniacid' => $_W['uniacid'], 'uid' => $fan['uid']));
  223. if (COUPON_TYPE == WECHAT_COUPON) {
  224. $fan_groups = $fan['tag']['tagid_list'];
  225. } else {
  226. $fan_groups[] = $user['groupid'];
  227. }
  228. $group = @array_intersect($coupon_group, $fan_groups);
  229. if (empty($coupon)) {
  230. return error(-1, '未找到指定卡券');
  231. }
  232. elseif (empty($group) && !empty($coupon_group)) {
  233. if (!empty($fan_groups)) {
  234. return error(-1, '无权限兑换');
  235. } else {
  236. if (is_array($coupon_group) && !in_array('0', $coupon_group)) {
  237. return error(-1, '无权限兑换');
  238. }
  239. }
  240. }
  241. elseif (strtotime(str_replace('.', '-', $coupon['date_info']['time_limit_end'])) < strtotime(date('Y-m-d')) && $coupon['date_info']['time_type'] != 2) {
  242. return error(-1, '活动已结束');
  243. }
  244. elseif ($coupon['quantity'] <= 0) {
  245. return error(-1, '卡券发放完毕');
  246. }
  247. elseif ($pcount >= $coupon['get_limit'] && !empty($coupon['get_limit'])) {
  248. return error(-1, '数量超限');
  249. }
  250. elseif (!empty($coupon['modules']) && !in_array($_W['current_module']['name'], array_keys($coupon['modules'])) && ($_GPC['c'] != 'activity' && $_GPC['c'] != 'mc' )) {
  251. return error(-1, '该模块没有此卡券发放权限');
  252. }
  253. $give = $_W['activity_coupon_id'] ? true :false;
  254. $uid = !empty($_W['member']['uid']) ? $_W['member']['uid'] : $fan['uid'];
  255. $insert = array(
  256. 'couponid' => $id,
  257. 'uid' => $uid,
  258. 'uniacid' => $_W['uniacid'],
  259. 'openid' => $fan['openid'],
  260. 'code' => $code,
  261. 'grantmodule' => $give ? $_W['activity_coupon_id'] : $_W['current_module']['name'],
  262. 'addtime' => TIMESTAMP,
  263. 'status' => 1,
  264. 'remark' => $give ? '系统赠送' : '用户使用' . $coupon['exchange']['credit'] . $credit_names[$coupon['exchange']['credittype']] . '兑换'
  265. );
  266. if ($coupon['source'] == 2) {
  267. $insert['card_id'] = $coupon['card_id'];
  268. $insert['code'] = '';
  269. }
  270. pdo_insert('coupon_record', $insert);
  271. pdo_update('coupon', array('quantity' => $coupon['quantity'] - 1, 'dosage' => $coupon['dosage'] +1), array('uniacid' => $_W['uniacid'],'id' => $coupon['id']));
  272. return true;
  273. }
  274. function activity_coupon_use($couponid, $recid, $module = 'system') {
  275. global $_W, $_GPC;
  276. $clerk_name = $_W['user']['name'];
  277. $clerk_id = $_W['user']['clerk_id'];
  278. $clerk_type = $_W['user']['type'];
  279. $store_id = $_W['user']['store_id'];
  280. $coupon_record = pdo_get('coupon_record', array('id' => $recid, 'status' => '1'));
  281. $coupon_info = activity_coupon_info(trim($couponid));
  282. $uid = $coupon_record['uid'];
  283. load()->model('mc');
  284. $user = mc_fetch($uid, array('groupid'));
  285. $fan = mc_fansinfo($uid, '', $_W['uniacid']);
  286. $coupongroup = pdo_fetchall("SELECT * FROM " . tablename('coupon_groups') . " WHERE `couponid` = :couponid", array(':couponid' => $couponid), 'groupid');
  287. $coupon_group = array_keys($coupongroup);
  288. if (COUPON_TYPE == WECHAT_COUPON) {
  289. $fan_groups = $fan['tag']['tagid_list'];
  290. } else {
  291. $fan_groups[] = $user['groupid'];
  292. }
  293. $group = @array_intersect($coupon_group, $fan_groups);
  294. if (empty($coupon_info)) {
  295. return error(-1, '没有指定的卡券信息');
  296. }
  297. if (empty($group) && !empty($coupon_group)) {
  298. return error(-1, '无法使用该卡券');
  299. }
  300. if ($module == 'paycenter') {
  301. if (!empty($coupon_info['location_id_list'])) {
  302. $store_ids = array_keys($coupon_info['location_id_list']);
  303. }
  304. if (!empty($store_ids) && !in_array($store_id, $store_ids)) {
  305. return error(-1, '门店信息错误');
  306. }
  307. } else {
  308. $coupon_modules = array_keys($coupon_info['modules']);
  309. if (!empty($coupon_info['modules']) && !in_array($module, $coupon_modules) && $module != 'system') {
  310. return error(-1, '非法模块');
  311. }
  312. }
  313. $date_info = iunserializer($coupon_info['date_info']);
  314. if ($date_info['time_type'] == '1') {
  315. if (strtotime(str_replace('.', '-', $date_info['time_limit_start'])) > strtotime(date('Y-m-d'))) {
  316. return error(-1, '卡券活动尚未开始');
  317. } elseif (strtotime(str_replace('.', '-', $date_info['time_limit_end'])) < strtotime(date('Y-m-d'))) {
  318. return error(-1, '卡券活动已经结束');
  319. }
  320. } else {
  321. $starttime = strtotime(date('Y-m-d', $coupon_record['addtime'])) + $date_info['deadline'] * 86400;
  322. $endtime = $starttime + $date_info['limit'] * 86400;
  323. if ($starttime > strtotime(date('Y-m-d'))) {
  324. return error(-1, '卡券活动尚未开始');
  325. } elseif ($endtime < strtotime(date('Y-m-d'))) {
  326. return error(-1, '卡券活动已经结束');
  327. }
  328. }
  329. if (empty($coupon_record)) {
  330. return error(-1, '没有可使用的卡券');
  331. }
  332. if ($coupon_info['source'] == '2') {
  333. load()->classs('coupon');
  334. $coupon_api = new coupon($_W['acid']);
  335. $status = $coupon_api->ConsumeCode(array('code' => $coupon_record['code']));
  336. if(is_error($status)) {
  337. return error('-1', $status['message']);
  338. }
  339. }
  340. $update = array(
  341. 'status' => 3,
  342. 'usetime' => TIMESTAMP,
  343. 'clerk_name' => $clerk_name,
  344. 'clerk_id' => intval($clerk_id),
  345. 'clerk_type' => $clerk_type,
  346. 'store_id' => $store_id
  347. );
  348. pdo_update('coupon_record', $update, array('id' => $coupon_record['id']));
  349. return true;
  350. }
  351. function activity_paycenter_coupon_available() {
  352. $coupon_owned = activity_coupon_owned();
  353. foreach ($coupon_owned as $key => &$val) {
  354. if (empty($val['code'])) {
  355. unset($val);
  356. }
  357. if ($val['type'] == '1' || $val['type'] == '2') {
  358. $coupon_available[$val['id']] = $val;
  359. }
  360. }
  361. return $coupon_available;
  362. }
  363. function activity_goods_grant($uid, $exid){
  364. global $_W;
  365. $exid = intval($exid);
  366. $uid = intval($uid);
  367. $exchange = activity_exchange_info($exid, $_W['uniacid']);
  368. if (empty($exchange)) {
  369. return error(-1, '没有指定的实物兑换');
  370. }
  371. if ($exchange['starttime'] > TIMESTAMP) {
  372. return error(-1, '该实物兑换尚未开始');
  373. }
  374. if ($exchange['endtime'] < TIMESTAMP) {
  375. return error(-1, '该实物兑换已经结束');
  376. }
  377. $pnum = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('activity_exchange_trades') . ' WHERE uniacid = :uniacid AND uid = :uid AND exid = :exid', array(':uniacid' => $_W['uniacid'], ':uid' => $uid, ':exid' => $exid));
  378. if ($pnum >= $exchange['pretotal']) {
  379. return error(-1, '该实物兑换每人只能使用' . $exchange['pretotal'] . '次');
  380. }
  381. if ($exchange['num'] >= $exchange['total']) {
  382. return error(-1, '该实物兑换已兑换完');
  383. }
  384. $data = array(
  385. 'uniacid' => $_W['uniacid'],
  386. 'uid' => $uid,
  387. 'type' => 3,
  388. 'exid' => $exid,
  389. 'createtime' => TIMESTAMP,
  390. );
  391. pdo_insert('activity_exchange_trades', $data);
  392. $insert_id = pdo_insertid();
  393. if (empty($insert_id)) {
  394. return error(-1, '实物兑换失败');
  395. }
  396. $insert = array(
  397. 'tid' => $insert_id,
  398. 'uniacid' => $_W['uniacid'],
  399. 'uid' => $uid,
  400. 'status' => 0,
  401. 'exid' => $exid,
  402. 'createtime' => TIMESTAMP
  403. );
  404. pdo_insert('activity_exchange_trades_shipping', $insert);
  405. pdo_update('activity_exchange', array('num' => $exchange['num'] + 1), array('id' => $exid, 'uniacid' => $_W['uniacid']));
  406. return $insert_id;
  407. }
  408. function activity_exchange_info($exchangeid, $uniacid = 0){
  409. global $_W;
  410. $uniacid = intval($uniacid) ? intval($uniacid) : $_W['uniacid'];
  411. $exchange = pdo_fetch('SELECT * FROM '.tablename('activity_exchange').' WHERE id=:id AND uniacid = :uniacid', array(':id'=>$exchangeid, ':uniacid' => $uniacid));
  412. if (!empty($exchange) && !empty($exchange['extra'])) {
  413. $exchange['extra'] = iunserializer($exchange['extra']);
  414. }
  415. return $exchange;
  416. }
  417. function activity_exchange_shipping($id){
  418. global $_W;
  419. return pdo_fetch('SELECT * FROM ' . tablename('activity_exchange_trades_shipping') . ' WHERE tid=:id AND uniacid=:uniacid', array(':id' => $id, ':uniacid' => $_W['uniacid']));
  420. }
  421. function activity_type_title($type){
  422. switch (intval($type)) {
  423. case 1: return '卡券';
  424. case 2: return '代金券';
  425. case 3: return '实体物品';
  426. case 4: return '虚拟物品';
  427. case 5:
  428. default: return '活动参与次数';
  429. }
  430. }
  431. function activity_coupon_give() {
  432. global $_W;
  433. $openid = $_W['openid'];
  434. if (!empty($openid)) {
  435. $member = array();
  436. $new_members = activity_get_member('new_member');
  437. if (!empty($new_members['members'])) {
  438. $member['is_newmember'] = in_array($openid, $new_members['members'])? 'new_member' : '';
  439. } else {
  440. $member['is_newmember'] = '';
  441. }
  442. $old_members = activity_get_member('old_member');
  443. if (!empty($old_members['members'])) {
  444. $member['is_oldmember'] = in_array($openid, $old_members['members'])? 'old_member' : '';
  445. } else {
  446. $member['is_oldmember'] = '';
  447. }
  448. $quiet_members = activity_get_member('quiet_member');
  449. if (!empty($quiet_members['members'])) {
  450. $member['is_quietmember'] = in_array($openid, $quiet_members['members'])? 'quiet_member' : '';
  451. } else {
  452. $member['is_quietmember'] = '';
  453. }
  454. $activity_members = activity_get_member('activity_member');
  455. if (!empty($activity_members['members'])) {
  456. $member['is_activitymember'] = in_array($openid, $activity_members['members'])? 'activity_member' : '';
  457. } else {
  458. $member['is_activitymember'] = '';
  459. }
  460. } else {
  461. $member = array();
  462. }
  463. $coupon_activitys = pdo_getall('coupon_activity', array('uniacid' => $_W['uniacid'], 'type' => 1, 'status' => 1));
  464. foreach ($coupon_activitys as $activity) {
  465. $is_give = pdo_get('coupon_record', array('grantmodule' => $activity['id'], 'remark' => '系统赠送'));
  466. if (!empty($is_give)) {
  467. continue;
  468. }
  469. $activity['members'] = empty($activity['members']) ? array() : iunserializer($activity['members']);
  470. if (in_array('group_member', $activity['members'])) {
  471. $groupid = pdo_fetchcolumn("SELECT groupid FROM ". tablename('mc_members')." WHERE uniacid = :uniacid AND uid = :uid", array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid']));
  472. if ($groupid == $activity['members']['groupid']) {
  473. $member['is_groupmember'] = 'group_member';
  474. }
  475. }
  476. if (in_array('cash_time', $activity['members'])) {
  477. $cash_member = pdo_fetch("SELECT * FROM " . tablename('mc_cash_record') . " WHERE uniacid = :uniacid AND uid = :uid AND createtime > :start AND createtime < :end", array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid'], ':start' => strtotime($activity['members']['start']), ':end' => strtotime($activity['members']['start'])));
  478. if (!empty($cash_member)) {
  479. $member['is_cashtime'] = 'cash_time';
  480. }
  481. }
  482. if (in_array('openids', $activity['members'])) {
  483. $fan = pdo_get('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));
  484. $openid = $_W['openid'];
  485. if (in_array($openid, $activity['members']['openids'])) {
  486. $member['is_openids'] = 'openids';
  487. }
  488. }
  489. if (array_intersect($activity['members'], $member)) {
  490. $activity['coupons'] = empty($activity['coupons']) ? array() : iunserializer($activity['coupons']);
  491. foreach ($activity['coupons'] as $id){
  492. $coupon = activity_coupon_info($id);
  493. if(is_error($coupon)){
  494. continue;
  495. }
  496. $_W['activity_coupon_id'] = $activity['id'];
  497. $ret = activity_coupon_grant($id, $_W['member']['uid']);
  498. unset($_W['activity_coupon_id']);
  499. if(is_error($ret)) {
  500. continue;
  501. }
  502. }
  503. unset($id);
  504. }
  505. }
  506. unset($activity);
  507. }
  508. function activity_get_member($type, $param = array()) {
  509. activity_coupon_type_init();
  510. global $_W;
  511. $types = array('new_member', 'old_member', 'quiet_member', 'activity_member', 'group_member', 'cash_time', 'openids');
  512. if (!in_array($type, $types)) {
  513. return error('1', '没有匹配的用户类型');
  514. }
  515. $propertys = activity_member_propertys();
  516. if ($type == 'new_member') {
  517. $property_time = strtotime('-' . $propertys['newmember'] . ' month', time());
  518. $members_sql = "SELECT c.openid FROM ( SELECT a.uid FROM ". tablename('mc_members')." as a LEFT JOIN ".tablename('mc_cash_record')." as b ON a.uid = b.uid WHERE a.uniacid = :uniacid AND a.createtime > :time AND (b.createtime > :time or b.id is null) GROUP BY a.uid HAVING COUNT(*) < 2) as d LEFT JOIN ". tablename('mc_mapping_fans')." as c ON d.uid = c.uid WHERE c.openid <> ''";
  519. $members = pdo_fetchall($members_sql, array(':uniacid' => $_W['uniacid'], ':time' => $property_time), 'openid');
  520. }
  521. if ($type == 'old_member') {
  522. $property_time = strtotime('-' . $propertys['oldmember'] . ' month', time());
  523. $members = pdo_fetchall("SELECT b.openid FROM ".tablename('mc_members')." as a LEFT JOIN ". tablename('mc_mapping_fans')." as b ON a.uid = b.uid WHERE a.createtime < :time AND a.uniacid = :uniacid AND b.openid <> ''", array(':time' => $property_time, ':uniacid' => $_W['uniacid']), 'openid');
  524. }
  525. if ($type == 'activity_member') {
  526. $property_time = strtotime('-' . $propertys['activitymember'] . ' month', time());
  527. $members = pdo_fetchall("SELECT * FROM " . tablename('mc_cash_record') . " as a LEFT JOIN ". tablename('mc_mapping_fans')." as b ON a.uid = b.uid WHERE a.uniacid = :uniacid AND a.createtime > :time AND b.openid <> '' GROUP BY a.uid HAVING COUNT(*) > 2", array(':uniacid' => $_W['uniacid'], ':time' => $property_time), 'openid');
  528. }
  529. if ($type == 'quiet_member') {
  530. $property_time = strtotime('-' . $propertys['quietmember'] . ' month', time());
  531. $members = pdo_fetchall("SELECT a.openid FROM " . tablename('mc_mapping_fans') . " as a LEFT JOIN ".tablename('mc_cash_record')." as b ON a.uid = b.uid WHERE a.uniacid = :uniacid AND b.id is null GROUP BY a.uid ", array(':uniacid' => $_W['uniacid']), 'openid');
  532. $member = pdo_fetchall("SELECT a.openid FROM " . tablename('mc_mapping_fans') . " as a LEFT JOIN ".tablename('mc_cash_record')." as b ON a.uid = b.uid WHERE a.uniacid = :uniacid AND b.createtime > :time GROUP BY a.uid ", array(':uniacid' => $_W['uniacid'], ':time' => $property_time), 'openid');
  533. if (!empty($member)) {
  534. foreach ($member as $key => $mem) {
  535. unset($members[$key]);
  536. }
  537. }
  538. }
  539. if ($type == 'group_member') {
  540. if (empty($param)) {
  541. return error(1, '请选择会员组');
  542. }
  543. if (COUPON_TYPE == WECHAT_COUPON) {
  544. $members = pdo_getall('mc_mapping_fans', array('uniacid' => $_W['uniacid']), array(), 'openid');
  545. foreach ($members as $key => &$fan) {
  546. $fan['groupid'] = explode(',', $fan['groupid']);
  547. if (!is_array($fan['groupid']) || !in_array($param['groupid'], $fan['groupid'])) {
  548. unset($members[$key]);
  549. }
  550. }
  551. } else {
  552. $members = pdo_fetchall('SELECT b.openid FROM '.tablename('mc_members')." as a LEFT JOIN ". tablename('mc_mapping_fans')." as b ON a.uid = b.uid WHERE a.groupid = :groupid AND a.uniacid = :uniacid AND b.openid <> ''", array(':groupid' => $param['groupid'], ':uniacid' => $_W['uniacid']), 'openid');
  553. }
  554. }
  555. if ($type == 'cash_time') {
  556. $members = pdo_fetchall("SELECT a.openid FROM ". tablename('mc_mapping_fans')." as a LEFT JOIN ".tablename('mc_cash_record')." as b ON a.uid = b.uid WHERE a.uniacid = :uniacid AND b.createtime >= :start AND b.createtime <= :end GROUP BY a.openid", array(':uniacid' => $_W['uniacid'], ':start' => $param['start'], ':end' => $param['end']), 'openid');
  557. }
  558. if ($type == 'openids') {
  559. $members = json_decode($_COOKIE['fans_openids'.$_W['uniacid']]);
  560. }
  561. if (is_array($members)) {
  562. $member = $type == 'openids' ? $members : array_keys($members);
  563. $members = array();
  564. $members['members'] = $member;
  565. $members['total'] = count($members['members']);
  566. } else {
  567. $members = array();
  568. }
  569. return $members;
  570. }
  571. function activity_coupon_sync() {
  572. global $_W;
  573. $cachekey = cache_system_key('couponsync', array('uniacid' => $_W['uniacid']));
  574. $cache = cache_load($cachekey);
  575. if (!empty($cache) && $cache['expire'] > time()) {
  576. return false;
  577. }
  578. $coupon_api = new coupon($_W['acid']);
  579. $cards = pdo_getall('coupon', array('acid' => $_W['acid']), array('id', 'status', 'card_id', 'acid'));
  580. foreach ($cards as $val) {
  581. $card = $coupon_api -> fetchCard($val['card_id']);
  582. if (is_error($card)) {
  583. return(error(-1, $card['message']));
  584. }
  585. $type = strtolower($card['card_type']);
  586. $coupon_status = activity_coupon_status();
  587. $status = $coupon_status[$card[$type]['base_info']['status']];
  588. pdo_update('coupon', array('status' => $status), array('id' => $val['id']));
  589. }
  590. cache_write($cachekey, array('expire' => time() + 1800));
  591. return true;
  592. }
  593. function activity_coupon_download($card_list) {
  594. global $_W;
  595. $coupon_api = new coupon($_W['acid']);
  596. $local_coupon_list = pdo_getall('coupon', array('acid' => $_W['acid'], 'source' => 2), array('id', 'status', 'card_id', 'type'), 'card_id');
  597. if (!empty($card_list['card_id_list'])) {
  598. foreach ($card_list['card_id_list'] as $card_id) {
  599. $coupon_info = $coupon_api->fetchCard($card_id);
  600. if (is_error($coupon_info)) {
  601. return(error(-1, $coupon_info['message']));
  602. }
  603. if ($coupon_info['card_type'] == 'DISCOUNT') {
  604. $type = 1;
  605. } elseif ($coupon_info['card_type'] == 'CASH') {
  606. $type = 2;
  607. } elseif ($coupon_info['card_type'] == 'GENERAL_COUPON') {
  608. $type = 3;
  609. } elseif ($coupon_info['card_type'] == 'GIFT') {
  610. $type = 4;
  611. } elseif ($coupon_info['card_type'] == 'GROUPON') {
  612. $type = 5;
  613. }
  614. $coupon_type_label = activity_coupon_type_label($type);
  615. $coupon = Card::create($type);
  616. $coupon->logo_url = $coupon_info[$coupon_type_label[1]]['base_info']['logo_url'];
  617. $coupon->brand_name = $coupon_info[$coupon_type_label[1]]['base_info']['brand_name'];
  618. $coupon->title = $coupon_info[$coupon_type_label[1]]['base_info']['title'];
  619. $coupon->sub_title = $coupon_info[$coupon_type_label[1]]['base_info']['sub_title'];
  620. $coupon_colors = array_flip(activity_coupon_colors());
  621. $coupon->color = $coupon_colors[$coupon_info[$coupon_type_label[1]]['base_info']['color']];
  622. $coupon->notice = $coupon_info[$coupon_type_label[1]]['base_info']['notice'];
  623. $coupon->service_phone = $coupon_info[$coupon_type_label[1]]['base_info']['service_phone'];
  624. $coupon->description = $coupon_info[$coupon_type_label[1]]['base_info']['description'];
  625. $coupon->get_limit = $coupon_info[$coupon_type_label[1]]['base_info']['get_limit'];
  626. $coupon->can_share = $coupon_info[$coupon_type_label[1]]['base_info']['can_share'];
  627. $coupon->can_give_friend = $coupon_info[$coupon_type_label[1]]['base_info']['can_give_friend'];
  628. if ($coupon_info[$coupon_type_label[1]]['base_info']['date_info']['type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
  629. $coupon->date_info = array(
  630. 'type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => $coupon_info[$coupon_type_label[1]]['base_info']['date_info']['begin_timestamp'],
  631. 'end_timestamp' => $coupon_info[$coupon_type_label[1]]['base_info']['date_info']['end_timestamp'],
  632. );
  633. } elseif ($coupon_info[$coupon_type_label[1]]['base_info']['date_info']['type'] == 'DATE_TYPE_FIX_TERM') {
  634. $coupon->setDateinfoFix($coupon_info[$coupon_type_label[1]]['base_info']['date_info']['fixed_begin_term'], $coupon_info[$coupon_type_label[1]]['base_info']['date_info']['fixed_term']);
  635. }
  636. if (!empty($coupon_info[$coupon_type_label[1]]['base_info']['promotion_url_name']) && !empty($coupon_info[$coupon_type_label[1]]['base_info']['promotion_url'])) {
  637. $coupon->setPromotionMenu($coupon_info[$coupon_type_label[1]]['base_info']['promotion_url_name'], $coupon_info[$coupon_type_label[1]]['base_info']['promotion_url_sub_title'], $coupon_info[$coupon_type_label[1]]['base_info']['promotion_url']);
  638. }
  639. if (!empty($coupon_info[$coupon_type_label[1]]['base_info']['location_id_list'])) {
  640. $coupon->location_id_list = $coupon_info[$coupon_type_label[1]]['base_info']['location_id_list'];
  641. }
  642. $coupon->setCustomMenu('立即使用', '', murl('entry', array('m' => 'paycenter', 'do' => 'consume'), true, true));
  643. $coupon->setQuantity($coupon_info[$coupon_type_label[1]]['base_info']['sku']['quantity']);
  644. $coupon->code_type = $coupon_info[$coupon_type_label[1]]['base_info']['code_type'];
  645. $coupon->discount = $coupon_info[$coupon_type_label[1]]['discount'];
  646. $coupon->least_cost = $coupon_info[$coupon_type_label[1]]['least_cost'];
  647. $coupon->reduce_cost = $coupon_info[$coupon_type_label[1]]['reduce_cost'];
  648. $coupon->gift = $coupon_info[$coupon_type_label[1]]['gift'];
  649. $coupon->deal_detail = $coupon_info[$coupon_type_label[1]]['deal_detail'];
  650. $coupon->default_detail = $coupon_info[$coupon_type_label[1]]['default_detail'];
  651. $coupon->card_id = $coupon_info[$coupon_type_label[1]]['base_info']['id'];
  652. $coupon->source = 2;
  653. $coupon_status = activity_coupon_status();
  654. $coupon->status = $coupon_status[$coupon_info[$coupon_type_label[1]]['base_info']['status']];
  655. $coupon_insert_data = $coupon->getCardArray();
  656. $coupon_insert_data['uniacid'] = $_W['uniacid'];
  657. $coupon_insert_data['acid'] = $_W['acid'];
  658. if (empty($local_coupon_list[$coupon_insert_data['card_id']])) {
  659. pdo_insert('coupon', $coupon_insert_data);
  660. }
  661. }
  662. }
  663. return true;
  664. }
  665. function activity_store_sync() {
  666. global $_W;
  667. load()->classs('coupon');
  668. $cachekey = cache_system_key('storesync', array('uniacid' => $_W['uniacid']));
  669. $cache = cache_load($cachekey);
  670. if (!empty($cache) && $cache['expire'] > time()) {
  671. return false;
  672. }
  673. $stores = pdo_getall('activity_stores', array('uniacid' => $_W['uniacid'], 'source' => 2));
  674. foreach ($stores as $val) {
  675. if ($val['status'] == 3) {
  676. continue;
  677. }
  678. $acc = new coupon($_W['acid']);
  679. $location = $acc->LocationGet($val['location_id']);
  680. if(is_error($location)) {
  681. return error(-1, $location['message']);
  682. }
  683. $location = $location['business']['base_info'];
  684. $status2local = array('', 3, 2, 1, 3);
  685. $location['status'] = $status2local[$location['available_state']];
  686. $location['location_id'] = $location['poi_id'];
  687. $category_temp = explode(',', $location['categories'][0]);
  688. $location['category'] = iserializer(array('cate' => $category_temp[0], 'sub' => $category_temp[1], 'clas' => $category_temp[2]));
  689. $location['photo_list'] = iserializer($location['photo_list']);
  690. unset($location['categories'], $location['poi_id'], $location['update_status'], $location['available_state'],$location['offset_type'], $location['sid'], $location['type']);
  691. pdo_update('activity_stores', $location, array('uniacid' => $_W['uniacid'], 'id' => $val['id']));
  692. }
  693. cache_write($cachekey, array('expire' => time() + 1800));
  694. return true;
  695. }
  696. function activity_coupon_colors() {
  697. $colors = array(
  698. 'Color010' => '#55bd47',
  699. 'Color020' => '#10ad61',
  700. 'Color030' => '#35a4de',
  701. 'Color040' => '#3d78da',
  702. 'Color050' => '#9058cb',
  703. 'Color060' => '#de9c33',
  704. 'Color070' => '#ebac16',
  705. 'Color080' => '#f9861f',
  706. 'Color081' => '#f08500',
  707. 'Color082' => '#a9d92d',
  708. 'Color090' => '#e75735',
  709. 'Color100' => '#d54036',
  710. 'Color101' => '#cf3e36',
  711. 'Color102' => '#5e6671',
  712. );
  713. return $colors;
  714. }
  715. function activity_coupon_type_label($type = '') {
  716. $types = array(
  717. COUPON_TYPE_DISCOUNT => array('折扣券', 'discount'),
  718. COUPON_TYPE_CASH => array('代金券', 'cash'),
  719. COUPON_TYPE_GIFT => array('礼品券', 'gift'),
  720. COUPON_TYPE_GROUPON => array('团购券', 'groupon'),
  721. COUPON_TYPE_GENERAL => array('优惠券', 'general_coupon'),
  722. );
  723. return $types[$type] ? $types[$type] : $types;
  724. }
  725. function activity_shipping_status_title($status){
  726. if ($status == 0) {
  727. return '正常';
  728. } elseif ($status == 1) {
  729. return '已发货';
  730. } elseif ($status == 2) {
  731. return '已完成';
  732. } elseif ($status == -1) {
  733. return '关闭';
  734. }
  735. }
  736. function activity_coupon_status() {
  737. return array(
  738. 'CARD_STATUS_NOT_VERIFY' => 1, 'CARD_STATUS_VERIFY_FAIL' => 2, 'CARD_STATUS_VERIFY_OK' => 3, 'CARD_STATUS_USER_DELETE' => 4,
  739. 'CARD_STATUS_DELETE' => 4, 'CARD_STATUS_USER_DISPATCH' => 5, 'CARD_STATUS_DISPATCH' => 5, );
  740. }
  741. function activity_member_propertys() {
  742. global $_W;
  743. $current_property_info = pdo_get('mc_member_property', array('uniacid' => $_W['uniacid']));
  744. if (!empty($current_property_info)) {
  745. $propertys = json_decode($current_property_info['property'], true);
  746. } else {
  747. $propertys = array(
  748. 'newmember' => '1',
  749. 'oldmember' => '2',
  750. 'activitymember' => '1',
  751. 'quietmember' => '1'
  752. );
  753. }
  754. return $propertys;
  755. }