Manage.php 24 KB

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