ActivityApply.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\ActivityApplyItem;
  4. use app\common\model\BillApply;
  5. use app\common\model\StoreOrderRefund;
  6. use app\common\model\UserMessage;
  7. use app\common\service\OrderCallback;
  8. use app\common\service\UserSynth;
  9. use library\Controller;
  10. use think\Db;
  11. /**
  12. * 活动
  13. * Class ActivityApply
  14. * @package app\operate\controller
  15. */
  16. class ActivityApply extends Controller
  17. {
  18. protected $table = 'ActivityApply';
  19. /**
  20. * 列表
  21. * @auth true
  22. * @menu true
  23. * @throws \think\Exception
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. * @throws \think\exception\PDOException
  28. */
  29. public function index()
  30. {
  31. $this->status_list = [0=>'待支付',1=>'已支付',2=>'已取消'];
  32. $this->order_status = $this->request->get('order_status',-1);
  33. $id = $this->request->get('act_id');
  34. $name = $this->request->get('name');
  35. $phone = $this->request->get('phone');
  36. $source_type = $this->request->get('source_type');
  37. $order_status = $this->request->get('order_status',-1);
  38. $tg_id = $this->request->get('tg_id',0);
  39. $this->title = '报名记录';
  40. $where = [];
  41. $where[]= ['a.act_id','=' ,$id];
  42. $where[]= ['a.is_deleted','=' ,0];
  43. if($name) $where[]= ['u.name','like' ,'%'.$name.'%'];
  44. if($phone) $where[]= ['u.phone','like' ,'%'.$phone.'%'];
  45. if($tg_id) $where[]= ['a.tg_id','=' ,$tg_id];
  46. if(in_array($order_status,[0,1,2])) $where[]= ['a.status','=' ,$order_status];
  47. if($source_type == 1) $where[]= ['a.pay_type','<>' ,9];
  48. if($source_type == 2) $where[]= ['a.pay_type','=' ,9];
  49. $this->qrarr= Db::name('activity_qrcode')->where('act_id',$id)->column('tg_param,id,phone','id');
  50. $query = $this->_query('activity_apply')
  51. ->alias('a')
  52. ->field('a.*,u.name user_name,u.headimg,u.phone')
  53. ->where($where)
  54. ->leftJoin('store_member u','u.id = a.user_id')
  55. ->order('a.id desc')->page();
  56. }
  57. /**
  58. * 编辑
  59. * @auth true
  60. * @menu true
  61. * @throws \think\Exception
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. * @throws \think\exception\PDOException
  66. */
  67. public function edit()
  68. {
  69. $this->title = '编辑';
  70. $this->_form($this->table, 'form');
  71. }
  72. /**
  73. * 删除
  74. * @auth true
  75. * @throws \think\Exception
  76. * @throws \think\exception\PDOException
  77. */
  78. public function del()
  79. {
  80. $this->_save($this->table, ['is_deleted' => '1']);
  81. }
  82. /**
  83. * 批量删除
  84. * @auth true
  85. * @throws \think\Exception
  86. * @throws \think\exception\PDOException
  87. */
  88. public function remove()
  89. {
  90. $this->_save($this->table, ['is_deleted' => '1']);
  91. }
  92. /**
  93. * 表单数据处理
  94. * @auth true
  95. * @menu true
  96. * @param array $data
  97. */
  98. protected function _form_filter(&$data)
  99. {
  100. if ($this->request->isGet() && $this->request->action() == 'edit'){
  101. $this->order_item = ActivityApplyItem::where(['o.apply_id'=>$data['id']])->alias('o')
  102. ->field('o.*,p.ladder_title,p.ladder_price')
  103. ->leftJoin('ActivityPrice p','p.id=o.price_id')
  104. ->select()->toArray();
  105. $this->data = $data;
  106. }
  107. if($this->request->get() && $this->request->action() == 'audit')
  108. {
  109. $data['refund_money'] = StoreOrderRefund::getToRefund($data['id']);
  110. }
  111. }
  112. /**
  113. * 确认收款
  114. * @auth true
  115. * @throws \think\Exception
  116. * @throws \think\exception\PDOException
  117. */
  118. public function confirm()
  119. {
  120. $order_info = \app\common\model\ActivityApply::where('id',input('id'))->find()->toArray();
  121. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  122. if(!$back_res['ret_val']) $this->error($back_res['msg']);
  123. $this->success('操作成功');
  124. }
  125. /**
  126. * 确认退款
  127. * @auth true
  128. * @throws \think\Exception
  129. * @throws \think\exception\PDOException
  130. */
  131. public function refund()
  132. {
  133. $id = input('post.id');
  134. \app\common\model\ActivityApply::where('id',$id)->update(['refund_state'=>2]);
  135. $order_info = \app\common\model\ActivityApply::where('id',$id)->find()->toArray();
  136. $act_title = \app\common\model\Activity::where('id',$order_info['act_id'])->value('title');
  137. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_order_info?id='.$id;
  138. $message = "您已取消《".$act_title."》活动订单,订单有效金额已原路返回,订单对应所有门票已失效";//."点击<a href=\"$info_url\">查看详情</a>";
  139. $res = \app\common\service\Activity::orderRefundMoney($id,$message);
  140. $res['code'] == 200 ? $this->success('操作成功') : $this->error($res['msg']);
  141. }
  142. /**
  143. * 退款审核
  144. * @auth true
  145. * @throws \think\Exception
  146. * @throws \think\exception\PDOException
  147. */
  148. public function audit()
  149. {
  150. $this->title = '退款审核';
  151. $this->state_arr = ['未申请','审核中','同意退款','审核拒绝','退款异常','退款成功','取消申请'];
  152. if($this->request->isPost())
  153. {
  154. list($post)= [$this->request->post()];
  155. if($post['before_refund_state'] == 0) $this->error('订单未申请退款');
  156. if($post['before_refund_state'] == 6) $this->error('订单退款申请已取消');
  157. if($post['before_refund_state'] == 5) $this->error('订单已退款');
  158. if(!in_array($post['before_refund_state'],[1,4])) $this->error('无需重复审核');
  159. $order_info = \app\common\model\ActivityApply::where('id',$post['id'])->find()->toArray();
  160. $act_title = \app\common\model\Activity::where('id',$order_info['act_id'])->value('title');
  161. // 线下订单
  162. if($order_info['pay_type'] == 9) {
  163. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>$post['refund_state']]);
  164. if($post['refund_state'] == 2) StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>3]);
  165. if($post['refund_state'] == 3) StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>2]);
  166. $this->success('操作成功');
  167. }
  168. switch ($post['refund_state'])
  169. {
  170. case 2:
  171. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_order_info?id='.$order_info['id'];
  172. $message = "您已取消《".$act_title."》活动订单,订单有效金额已原路返回,订单对应所有门票已失效";//."点击<a href=\"$info_url\">查看详情</a>";
  173. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>2]);
  174. $res = \app\common\service\Activity::orderRefundMoney($post['id'],$message);
  175. $res['code'] == 200 ? $this->success('操作成功') : $this->error($res['msg']);
  176. break;
  177. case 3:
  178. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>3]);
  179. StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>2]);
  180. break;
  181. }
  182. $this->success('操作成功');
  183. }
  184. $this->_form($this->table, 'audit');
  185. }
  186. /**
  187. * 重新提醒
  188. * @auth true
  189. * @throws \think\Exception
  190. * @throws \think\exception\PDOException
  191. */
  192. public function remind(){
  193. $order = \app\common\model\ActivityApply::where('id','in',input('id'))->select()->toArray();
  194. $ret_val = true;$msg='';
  195. Db::startTrans();
  196. try{
  197. foreach ($order as $k => $v){
  198. $order_info = $v;
  199. // 更改订单状态
  200. //\app\common\model\ActivityApply::where('id',$order_info['id'])->update(['status'=>1,'pay_state'=>1,'pay_at'=>date('Y-m-d H:i:s')]);
  201. $item_list = ActivityApplyItem::where('apply_id',$order_info['id'])->select()->toArray();
  202. $qrCode = new \Endroid\QrCode\QrCode();
  203. foreach ($item_list as $iv) {
  204. if($iv['sh_status'] == 1) {
  205. // 生成核销码
  206. $qr_content = $order_info['act_id'].'-'.$order_info['id'].'-'.$iv['id'];
  207. $qrCode->setText($qr_content);
  208. $qrCode->setSize(200);
  209. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/code';
  210. $filename = $dir.'/'.$iv['id'].'.png';
  211. $qrCode->writeFile($filename);
  212. // return json($order_info);
  213. $url='http://'.$_SERVER['SERVER_NAME']."/code/".$iv['id'].'.png';
  214. ActivityApplyItem::where('id',$iv['id'])->update(['status'=>1,'qr_code'=>$url,'qr_content'=>$qr_content]);
  215. $act_title = \app\common\model\Activity::where('id',$iv['act_id'])->value('title');
  216. $content = '您已成功报名"'.$act_title.'"';
  217. UserMessage::sendUserMessage($iv['user_id'],'apply',1,0,0,$iv['id'],$content,$iv['id']);
  218. UserSynth::ticketSend($iv['id']);
  219. }else{
  220. ActivityApplyItem::where('id',$iv['id'])->update(['status'=>1]);
  221. }
  222. }
  223. // 活动订单发票记录修改
  224. BillApply::where(['order_type'=>3,'order_id'=>$order_info['id']])->update(['order_pay'=>1]);
  225. }
  226. //UserMessage::sendUserMessage($order_info['user_id'],'activity',1,0,0,$order_info['id'],'',$order_info['id']);
  227. Db::commit();
  228. }catch (\Exception $e){
  229. $ret_val = false;
  230. $msg = $e->getMessage();
  231. Db::rollback();
  232. }
  233. $this->success('操作成功');
  234. }
  235. public function export(){
  236. $this->order_status = $this->request->get('order_status',-1);
  237. $id = $this->request->get('act_id');
  238. $name = $this->request->get('name');
  239. $phone = $this->request->get('phone');
  240. $source_type = $this->request->get('source_type');
  241. $order_status = $this->request->get('order_status',-1);
  242. $tg_id = $this->request->get('tg_id',0);
  243. $where[]= ['a.act_id','=' ,$id];
  244. $where[]= ['a.is_deleted','=' ,0];
  245. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  246. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  247. if($tg_id) $where[]= ['a.tg_id','=' ,$tg_id];
  248. if(in_array($order_status,[0,1,2])) $where[]= ['a.status','=' ,$order_status];
  249. if($source_type == 1) $where[]= ['a.pay_type','<>' ,9];
  250. if($source_type == 2) $where[]= ['a.pay_type','=' ,9];
  251. $data =Db::name('activity_apply')
  252. ->alias('a')
  253. ->field('a.*,u.name user_name,u.headimg,u.phone,t.phone tg_phone,t.qrcode,y.title act_title')
  254. ->where($where)
  255. ->leftJoin('store_member u','u.id = a.user_id')
  256. ->leftJoin('activity_qrcode t','t.id = a.tg_id')
  257. ->leftJoin('activity y','y.id = a.act_id')
  258. ->order('a.id desc')->select();
  259. if(empty($data)) $this->error('暂无可以导出的数据');
  260. foreach ($data as $k=>&$v)
  261. {
  262. $v['order_source'] = $v['pay_type'] == 9 ? '转账订单':'线上订单';
  263. $v['pay_desc'] = $v['pay_state'] == 1 ? '已支付':'未支付';
  264. $item = Db::name('activity_apply_item')->where('apply_id',$v['id'])->find();
  265. $v['i_name'] = $item['name'];
  266. $v['i_phone'] = $item['phone'];
  267. $v['i_email'] = $item['email'];
  268. $v['i_extend'] = '';
  269. $extend = json_decode($item['extend'],true);
  270. if($extend){
  271. foreach ($extend as $key => $val){
  272. $v['i_extend'] .= '问题:'.$val['title'].'
  273. ';
  274. if(isset($val['extend'])){
  275. $v['i_extend'] .= '选项:';
  276. foreach ($val['extend'] as $val1){
  277. $v['i_extend'] .= ''.$val1['item_title'].', ';
  278. }
  279. }else{
  280. $v['i_extend'] .= '回答:'.$val['value'].'
  281. ';
  282. }
  283. }
  284. }
  285. $v['i_extend'] = str_replace("\n", "\n", $v['i_extend']);
  286. // return json($v['i_extend']);
  287. $price = Db::name('activity_price')->where('id',$item['price_id'])->find();
  288. $v['ladder_title'] = $price['ladder_title'];
  289. if($v['pay_type'] == 1 || $v['pay_type'] == 2){
  290. $v['pay_type_text'] = 'H5';
  291. }else if($v['pay_type'] == 3 || $v['pay_type'] == 4){
  292. $v['pay_type_text'] = 'APP';
  293. }else if($v['pay_type'] == 5){
  294. $v['pay_type_text'] = '公众号';
  295. }else if($v['pay_type'] == 6 || $v['pay_type'] == 7){
  296. $v['pay_type_text'] = 'PC';
  297. }else{
  298. $v['pay_type_text'] = '未支付';
  299. }
  300. $v['is_sh'] = $price['is_sh'] == 1 ? '需要审核':'不需要审核';
  301. }
  302. $field=array(
  303. 'A' => array('order_no', '订单号'),
  304. 'B' => array('user_name', '联系人'),
  305. 'C' => array('phone', '电话'),
  306. 'D' => array('money','订单金额'),
  307. 'E' => array('num','人数'),
  308. 'F' => array('act_title','活动'),
  309. 'G' => array('order_source', '订单类型'),
  310. 'H' => array('pay_desc', '支付状态'),
  311. 'I' => array('tg_phone', '推广手机号'),
  312. 'J' => array('qrcode', '推广码链接'),
  313. 'K' => array('i_name', '姓名'),
  314. 'L' => array('i_phone', '手机号'),
  315. 'M' => array('i_email', '邮箱'),
  316. 'N' => array('ladder_title', '购票类型'),
  317. 'O' => array('is_sh', '是否审核'),
  318. 'P' => array('pay_type_text', '支付场景'),
  319. 'Q' => array('i_extend', '答题模板'),
  320. );
  321. // return json($data);
  322. $this->phpExcelList($field,$data,'报名订单_'.date('Y-m-d H:i:s'));
  323. }
  324. public function phpExcelList($field=[],$list=[],$title='文件'){
  325. $PHPExcel=new \PHPExcel();
  326. $PHPSheet=$PHPExcel->getActiveSheet();
  327. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  328. foreach($list as $key=>$value)
  329. {
  330. foreach($field as $k=>$v){
  331. if($key == 0){
  332. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  333. }
  334. $i=$key+2;
  335. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  336. }
  337. }
  338. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  339. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  340. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  341. header('Cache-Control: max-age=0'); //禁止缓存
  342. $PHPWriter->save("php://output"); //输出到浏览器
  343. }
  344. }