Manage.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. namespace Active\Lib;
  3. use Dever;
  4. Class Manage
  5. {
  6. #全额退款
  7. public function refund_api($id)
  8. {
  9. $data = Dever::db('active/order')->find($id);
  10. // if ($data['mid'] && $data['mid'] > 0) {
  11. // $user = Dever::db('active/user')->find(array('mid'=>$data['mid']));
  12. // if ($user && $user['id']) {
  13. // $uid = $user['id'];
  14. // }
  15. // } else {
  16. // $uid = $data['uid'];
  17. // }
  18. $this->common($data,1);
  19. return 'reload';
  20. }
  21. public function insertInfoRefund ($id,$name,$data)
  22. {
  23. $num = Dever::param('num',$data);
  24. $order_id = Dever::param('order_id',$data);
  25. $data = Dever::db('active/order')->find($order_id);
  26. if ($num < 1) {
  27. Dever::alert('退款数量不正确');
  28. }
  29. if($num > $data['num']) {
  30. Dever::alert('退款数量大于购买数量');
  31. }
  32. $code = Dever::db('active/code')->getCode(array('order_id' => $order_id,'status'=>'1,2'));
  33. $use_code = count($code);#已领取的所有数量
  34. if (!$data['refund_num']) {
  35. $data['refund_num'] = 0;
  36. }
  37. $no_code = $data['num'] - $use_code - $data['refund_num'];#未使用的数量
  38. if ($num > $no_code){
  39. Dever::alert('退款数量不正确');
  40. }
  41. // if ($data['refund_num'] > 0){
  42. // $info = Dever::db('active/info_refund')->state(array('order_id'=>$order_id,'status'=>2));
  43. // $count = $data['num'] - count($info);
  44. // if ($num > $count) {
  45. // Dever::alert('退款数量大于购买数量');
  46. // }
  47. // }
  48. }
  49. public function updateInfoRefund ($id,$name,$data)
  50. {
  51. $num = Dever::param('num',$data);
  52. $order_id = Dever::param('order_id',$data);
  53. $code_id = Dever::param('code_id',$data);
  54. $data = Dever::db('active/order')->find($order_id);
  55. // $info = Dever::db('active/info_refund')->state(array('order_id'=>$order_id));
  56. // $active = Dever::db('active/info')->find($data['active_id']);
  57. $this->common($data,2,$num,$id);
  58. return 'reload';
  59. }
  60. public function show($id)
  61. {
  62. $data = Dever::db('active/order')->find($id);
  63. $refund_admin = '-';
  64. $rdate = '-';
  65. if ($data && isset($data['refund_admin']) && $data['refund_admin'] && $data['refund_admin']>0 && $data['rdate']) {
  66. $admin = Dever::load('manage/admin-find',$data['refund_admin']);
  67. $refund_admin = $admin['username'];
  68. $rdate = date('Y-m-d H:i',$data['rdate']);
  69. }
  70. $html = $refund_admin.'<br/>'.$rdate;
  71. return $html;
  72. }
  73. public function active($id)
  74. {
  75. $data = Dever::db('active/info')->find($id);
  76. $html = array();
  77. if (isset($data) && $data) {
  78. $html['active_name'] = $data['name'];
  79. }
  80. $info = Dever::db('active/code')->find($id);
  81. if ($info && isset($info['audit_admin']) && $info['audit_admin'] && $info['audit_admin'] > 0) {
  82. $aduit = Dever::load('manage/admin-find',$info['audit_admin']);
  83. $html['admin_name'] = $aduit['username'];
  84. } elseif ($info && $info['top_admin']) {
  85. $off = Dever::db('active/info_off')->find($info['top_admin']);
  86. $html['admin_name'] = $off['name'];
  87. }
  88. return $html;
  89. }
  90. public function user($id)
  91. {
  92. $html = '';
  93. $data = Dever::db('active/user')->find($id);
  94. if ($data) {
  95. $member = Dever::load('active/lib/user')->agent($data);
  96. if ($member) {
  97. $user = '<br/>(代理商)';
  98. } else {
  99. $user = '';
  100. }
  101. $html = $data['name'].'<br/>'.$data['mobile'].$user;
  102. }
  103. return $html;
  104. }
  105. public function dOff($id)
  106. {
  107. $data = Dever::db('active/code')->state(array('active_id' => $id,'status'=>1));
  108. $html = count($data);
  109. return $html;
  110. }
  111. public function insertInfoCode($id, $name, $data)
  112. {
  113. $code = Dever::param('code',$data);
  114. if ($code) {
  115. $info = Dever::db('active/info_code')->find(array('code'=>$code));
  116. if ($info['code']) {
  117. Dever::alert('核销码已核销');
  118. }
  119. }
  120. }
  121. public function updateInfoCode($id, $name, $data)
  122. {
  123. $code = Dever::param('code',$data);
  124. $data = Dever::db('active/code')->find(array('write_code'=>$code));
  125. if ($data) {
  126. $where['where_id'] = $data['id'];
  127. $where['status'] = 2;
  128. $where['offdate'] = time();
  129. $admin = Dever::load('manage/auth.info');
  130. $where['audit_admin'] = $admin['id'];
  131. Dever::db('active/code')->update($where);
  132. }
  133. }
  134. public function get()
  135. {
  136. $data[0]['name'] = '头衔';
  137. $data[0]['child'] = Dever::db('setting/title')->select();
  138. $data['state'] = 1;
  139. return $data;
  140. }
  141. public function search_api()
  142. {
  143. $value = Dever::input('value');
  144. if (!$value) {
  145. return '核销码或手机号不存在';
  146. }
  147. $active_id = Dever::input('search_option_active_id');
  148. if ($value) {
  149. $data = Dever::db('active/code')->one(array('write_code' => $value,'state' =>1));
  150. if (!$data) {
  151. if (strlen($value) != 11){
  152. return '核销码或手机号不正确';
  153. } else {
  154. $tell = preg_match("/^1[3456789]\d{9}$/",$value);
  155. if ($tell) {
  156. $user = Dever::db('active/user')->find(array('mobile'=>$value));
  157. if ($user) {
  158. $data = Dever::db('active/code')->find(array('join_uid'=>$user['id'],'active_id'=>1,'status' => 1));
  159. if (!$data) {
  160. return '核销码或手机号不正确或已核销';
  161. }
  162. } else {
  163. return '核销码或手机号不正确';
  164. }
  165. }
  166. }
  167. }
  168. $table = array();
  169. if ($data) {
  170. $user = Dever::db('active/user')->find(array('id'=>$data['join_uid']));
  171. $parent = Dever::db('active/user')->find(array('id'=>$data['buy_uid']));
  172. $table = array();
  173. $table['head'] = array('姓名', '手机号','邀请人', '邀请人手机号');
  174. $table['body'] = array();
  175. $table['body'] = array($user['name'], $user['mobile'],$parent['name'], $parent['mobile']);
  176. $result[] = array
  177. (
  178. 'type' => 'table',
  179. 'content' => $table,
  180. );
  181. $html = Dever::show('', $result, false, false);
  182. return $html;
  183. }else{
  184. return '核销码不存在';
  185. }
  186. } else {
  187. return '';
  188. }
  189. }
  190. public function checkMcode()
  191. {
  192. $mobile = Dever::input('mobile');
  193. $code = Dever::input('mcode');
  194. if (!$code) {
  195. Dever::alert('请输入验证码');
  196. }
  197. $code = Dever::load('passport/reg')->mcode($mobile, $code, 2);
  198. if (!$code) {
  199. Dever::alert('验证码输入错误');
  200. }
  201. return 'ok';
  202. }
  203. public function list()
  204. {
  205. $id = Dever::input('id');
  206. $info = Dever::db('active/order')->find($id);
  207. $active = Dever::db('active/info')->find($info['active_id']);
  208. $config = Dever::db('active/order')->config['config_status'];
  209. $status = Dever::status($config,$info['status']);
  210. if ( !$info['uid']) {
  211. $user = Dever::db('active/user')->find($info['mid']);
  212. $info['uid'] = $user['id'];
  213. }
  214. $code = Dever::db('active/code')->getCode(array('order_id' => $id,'status'=>'1,2'));
  215. $use_code = count($code);#已领取的所有数量
  216. // print_R($use_code);die;
  217. if (!$info['refund_num']) {
  218. $info['refund_num'] = 0;
  219. }
  220. $no_code = $info['num'] - $use_code - $info['refund_num'];#未使用的数量
  221. if ($info['uid'] && $info['uid'] > 0) {
  222. $user = Dever::db('active/user')->find($info['uid']);
  223. $user_name = $user['name'];
  224. $user_mobile = $user['mobile'];
  225. $user_idcard = $user['idcard'];
  226. $member = Dever::load('active/lib/user')->agent($user);
  227. if ($member) {
  228. $role = Dever::db('setting/role')->find($member['role']);
  229. if (isset($role) && $role && isset($role['name']) && $role['name']) {
  230. $role_name = '<br/>'.$role['name'];
  231. } else {
  232. $role_name = '';
  233. }
  234. $title = Dever::db('setting/title')->find($member['title_id']);
  235. if (isset($title) && $title && isset($title['name']) && $title['name']) {
  236. $title_name = '('.$title['name'].')';
  237. } else {
  238. $title_name = '';
  239. }
  240. $level = Dever::db('setting/level')->find($member['level_id']);
  241. if (isset($level) && $level && isset($level['name']) && $level['name']) {
  242. $level_name = '('.$level['name'].')';
  243. } else {
  244. $level_name = '';
  245. }
  246. $user_name = $member['name'].$role_name.$title_name.$level_name;
  247. $user_mobile = $member['mobile'];
  248. $user_idcard = $member['idcard'];
  249. }
  250. }
  251. // if ($active['col'] && $active['col'] == 'idcard') {
  252. // $user_idcard = $user['idcard'];
  253. // } else {
  254. // $user_idcard = '';
  255. // }
  256. $path = array();
  257. $path_data = array();
  258. if ($info['path']) {
  259. $path = Dever::json_decode($info['path']);
  260. foreach ($path as $k => $v) {
  261. $field = Dever::db('active/info_field')->find($v['id']);
  262. if ($field['type'] == 1) {
  263. $path_data[] = array($field['name'], $v['value']);
  264. } elseif ($field['type'] == 2) {
  265. $value = Dever::db('active/info_field_value')->find($v['value']);
  266. if ($value) {
  267. $path_data[] = array($field['name'], $value['name']);
  268. }
  269. }
  270. }
  271. }
  272. $result['订单信息'] = array
  273. (
  274. 'type' => 'info',
  275. 'content' => array
  276. (
  277. array
  278. (
  279. array('订单单号', $info['order_num']),
  280. array('活动名称', $active['name']),
  281. array('订单状态', $status),
  282. ),
  283. array
  284. (
  285. array('购买数量', $info['num']),
  286. array('支付金额', $info['price']),
  287. array('备注',$info['remark']),
  288. ),
  289. array
  290. (
  291. array('已使用', $use_code),
  292. array('未使用', $no_code),
  293. array('退款数量', $info['refund_num']),
  294. ),
  295. ),
  296. );
  297. $result['用户信息'] = array
  298. (
  299. 'type' => 'info',
  300. 'content' => array
  301. (
  302. array
  303. (
  304. array('用户姓名', $user_name),
  305. array('用户电话', $user_mobile),
  306. array('用户身份证号', $user_idcard),
  307. ),
  308. ),
  309. );
  310. if ($path) {
  311. $result['报名信息'] = array
  312. (
  313. 'type' => 'info',
  314. 'content' => array
  315. (
  316. $path_data,
  317. ),
  318. );
  319. }
  320. $button = array();
  321. if (Dever::load('manage/auth')->checkFunc('active.order', 'edit', '备注')) {
  322. $purl = Dever::url('project/database/update?project=active&table=order&where_id='.$info['id'].'&col=remark', 'manage');
  323. $button[] = array
  324. (
  325. 'type' => 'edit',
  326. 'link' => $purl,
  327. 'name' => '备注',
  328. );
  329. }
  330. if ($info['status'] == 2 && $info['code_num'] < 1 && time() < $active['act_start'] && $active['refund_type'] == 2 ) {
  331. if (Dever::load('manage/auth')->checkFunc('active.order', 'editrefund', '全额退款')) {
  332. $purl = Dever::url('active/lib/manage.refund&id='.$id);
  333. $button[] = array
  334. (
  335. 'type' => 'action',
  336. 'link' => $purl,
  337. 'name' => '全额退款',
  338. );
  339. }
  340. }
  341. if ($info['status'] > 1 && $no_code > 0 && $info['status'] != 3 && time()<$active['act_start'] && $active['refund_type'] == 2) {
  342. if (Dever::load('manage/auth')->checkFunc('active.order', 'editrefund1', '未使用退款')) {
  343. $purl = Dever::url('project/database/update?project=active&table=info_refund&search_option_order_id='.$info['id'].'&col=num,order_id', 'manage');
  344. $button[] = array
  345. (
  346. 'type' => 'edit',
  347. 'link' => $purl,
  348. 'name' => '未使用退款',
  349. );
  350. }
  351. }
  352. if ($use_code >= 0) {
  353. $result += $this->table($id);
  354. }
  355. $head = array
  356. (
  357. 'name' => '基本资料',
  358. 'btn' => $button,
  359. );
  360. $html = Dever::show($head, $result);
  361. return $html;
  362. }
  363. private function table ($id)
  364. {
  365. $data = Dever::db('active/code')->state(array('order_id' => $id));
  366. if ($data) {
  367. $head = array('参加人信息', '核销码', '核销状态', '领取时间', '核销时间', '核销人', '管理');
  368. $body = array();
  369. foreach ($data as $k => $v) {
  370. $user = Dever::db('active/user')->find($v['join_uid']);
  371. $join_name = '';
  372. if ($user) {
  373. $join_name = $user['name'].'<br/>'.$user['mobile'];
  374. }
  375. $admin_name = '';
  376. if ($v['status'] == 1) {
  377. $status = '待核销';
  378. } elseif($v['status'] == 2) {
  379. $status = '已核销';
  380. if ($v['audit_admin'] && $v['audit_admin'] >0) {
  381. $admin = Dever::load('manage/admin-find',$v['audit_admin']);
  382. if ($admin) {
  383. $admin_name = $admin['username'];
  384. }
  385. } else {
  386. if ($v['top_admin'] && $v['top_admin'] > 0) {
  387. $admin = Dever::db('active/info_off')->find($v['top_admin']);
  388. if ($admin) {
  389. $admin_name = $admin['name'];
  390. }
  391. }
  392. }
  393. } else {
  394. $status = '已作废';
  395. }
  396. $cdate = '';
  397. if ($v['cdate']) {
  398. $cdate = date('Y-m-d H:i',$v['cdate']);
  399. }
  400. $offdate = '';
  401. if ($v['offdate']) {
  402. $offdate = date('Y-m-d H:i',$v['offdate']);
  403. }
  404. $active = Dever::db('active/info')->find($v['active_id']);
  405. $m = '';
  406. if ($v['status'] == 1 && time()<$active['act_start'] && $active['refund_type'] == 2) {
  407. $url = Dever::url('lib/manage.setOrder&order_id=' . $v['order_id'].'&code_id='.$v['id'], 'active');
  408. $m = '<a href="javascript:;" onclick="load( \''.$url.'\')" class="layui-btn">退款</a>';
  409. }
  410. $d = array
  411. (
  412. $join_name,
  413. $v['write_code'],
  414. $status,
  415. $cdate,
  416. $offdate,
  417. $admin_name,
  418. $m,
  419. );
  420. $body[] = $d;
  421. }
  422. $result['核销码使用列表'] = array
  423. (
  424. 'type' => 'table',
  425. 'content' => array
  426. (
  427. 'head' => $head,
  428. 'body' => $body,
  429. )
  430. );
  431. return $result;
  432. } else {
  433. return array();
  434. }
  435. }
  436. public function setOrder_api($order_id,$code_id){
  437. $data = Dever::db('active/order')->find($order_id);
  438. $ids = $this->common($data,3);
  439. if ($ids) {
  440. $admin = Dever::load('manage/auth.info');
  441. $w['where_id'] = $code_id;
  442. $w['status'] = 3;
  443. $w['offdate'] = time();
  444. $w['audit_admin'] = $admin['id'];
  445. Dever::db('active/code')->update($w);
  446. }
  447. return 'reload';
  448. }
  449. public function common($data,$type,$num=false,$refund_id=false){
  450. Dever::config('base')->hook = true;
  451. $active = Dever::db('active/info')->find($data['active_id']);
  452. $info = Dever::db('active/info_refund')->state(array('order_id'=>$data['id']));
  453. if (time() > $active['act_start']) {
  454. Dever::alert('活动已开始,不能退款');
  455. }
  456. if ($type == 1) {
  457. $code = Dever::db('active/code')->find(array('order_id'=>$data['id']));
  458. if ($data['code_num'] > 0 || (isset($code) && $code)) {
  459. Dever::alert('此状态下不可退款');
  460. }
  461. if ($data['status'] != 2) {
  462. Dever::alert('此状态下不可退款');
  463. }
  464. $refund_num = $data['num'];
  465. $price = $data['price'];
  466. }
  467. if ($type == 2) {
  468. $price = $num * $active['price'];
  469. $refund_num = $num;
  470. }
  471. if ($type == 3) {
  472. $refund_num = 1;
  473. $price = $active['price'];
  474. }
  475. $param = array
  476. (
  477. 'project_id' => 6,
  478. 'channel_id' => 1,
  479. 'system_source' => 1,
  480. 'account_id' => 8,
  481. 'order_id' => $data['order_num'],
  482. 'refund_cash' => $price,
  483. //'refund_order_id' => '',
  484. );
  485. if ($type != 1) {
  486. $param['refund_order_id'] = count($info) + 1;
  487. }
  488. if ($data['price'] > 0) {
  489. $result = Dever::load('pay/api.refund', $param);
  490. if (!$result) {
  491. # 退款失败,抛出错误
  492. //throw new \Exception('退款失败');
  493. Dever::alert('退款失败,请联系管理员');
  494. } else {
  495. $admin = Dever::load('manage/auth.info');
  496. $time = time();
  497. $where['where_id'] = $data['id'];
  498. $where['status'] = 6;
  499. $where['refund_admin'] = $admin['id'];
  500. $where['rdate'] = $time;
  501. if (!$data['refund_num']) {
  502. $data['refund_num'] = 0;
  503. }
  504. $where['refund_num'] = $refund_num + $data['refund_num'];
  505. if ($type == 1 || ($price == $data['price']) || ($data['num'] == $where['refund_num'])) {
  506. $ids = Dever::db('active/order')->update($where);
  507. } else {
  508. $where['status'] = 2;
  509. $ids = Dever::db('active/order')->update($where);
  510. }
  511. if ($type == 2) {
  512. $insert['status'] = 2;
  513. $insert['where_id'] = $refund_id;
  514. $insert['audit_admin'] = $admin['id'];
  515. Dever::db('active/info_refund')->update($insert);
  516. } elseif ($type == 3) {
  517. $add['status'] = 2;
  518. $add['order_id'] = $data['id'];
  519. $add['num'] = 1;
  520. Dever::db('active/info_refund')->insert($add);
  521. }
  522. if (isset($ids) && $ids){
  523. return $ids;
  524. }
  525. }
  526. }
  527. }
  528. #扫码核销活动
  529. public function code_api(){
  530. $active_id = Dever::input('id');
  531. if (!$active_id) {
  532. return '活动id不存在';
  533. }
  534. $active = Dever::db('active/info')->find($active_id);
  535. $config = Dever::db('active/info')->config['config_act_status'];
  536. $code = Dever::input('code');
  537. if (!$code) {
  538. $data = array();
  539. $data['active_name'] = $active['name'];
  540. $data['status'] = Dever::status($config,$active['act_status']);
  541. $data['act_time'] = date('Y.m.d H:i:s',$active['act_start']).'~'.date('Y.m.d H:i:s',$active['act_end']);
  542. $data['active_id'] = $active_id;
  543. return Dever::render('off_code', $data);
  544. }
  545. // print_R($code);die;
  546. $data = Dever::db('active/code')->find(array('write_code'=>$code,'active_id'=>$active_id,'status'=>1));
  547. if ($data) {
  548. $data['active_name'] = $active['name'];
  549. $data['status'] = Dever::status($config,$active['act_status']);
  550. $data['act_time'] = date('Y.m.d H:i:s',$active['act_start']).'~'.date('Y.m.d H:i:s',$active['act_end']);
  551. $join_user = Dever::db('active/user')->find($data['join_uid']);
  552. $data['join_name'] = $join_user['name'];
  553. $data['join_mobile'] = $join_user['mobile'];
  554. $con = Dever::db('deploy/base-one');
  555. if ($join_user['avatar']) {
  556. $data['join_avatar'] = $join_user['avatar'];
  557. } else {
  558. $data['join_avatar'] = $con['avatar'];
  559. }
  560. $data['buy_name'] = '';
  561. $data['buy_mobile'] = '';
  562. $data['buy_avatar'] = '';
  563. $buy_user = Dever::db('active/user')->find($data['buy_uid']);
  564. if ($buy_user) {
  565. $data['buy_name'] = $buy_user['name'];
  566. $data['buy_mobile'] = $buy_user['mobile'];
  567. $data['buy_avatar'] = '';
  568. if ($buy_user['avatar']) {
  569. $data['buy_avatar'] = $buy_user['avatar'];
  570. }
  571. }
  572. $member = Dever::load('active/lib/user')->agent($buy_user);
  573. $data['role_name'] ='';
  574. $data['title_name'] = '';
  575. if ($member) {
  576. $role = Dever::db('setting/role')->find($member['role']);
  577. $title = Dever::db('setting/title')->find($member['title_id']);
  578. $level = Dever::db('setting/level')->find($member['level_id']);
  579. if ($role) {
  580. if (isset($level) && $level && isset($level['name']) && $level['name']) {
  581. $data['role_name'] = $role['name'] .'('.$level['name'].')';
  582. } else {
  583. $data['role_name'] = $role['name'];
  584. }
  585. }
  586. if ($title && $title['name']) {
  587. $data['title_name'] = $title['name'];
  588. }
  589. if (!$data['buy_avatar']) {
  590. $data['buy_avatar'] = $member['avatar'];
  591. } else {
  592. $data['buy_avatar'] = $con['avatar'];
  593. }
  594. }
  595. } else {
  596. return '核销码不存在或已核销';
  597. }
  598. $type = Dever::input('type');
  599. if ($type == 1) {
  600. return Dever::render('off_code', $data);
  601. }else {
  602. return $data;
  603. }
  604. }
  605. }