ActivityApplyItem.php 18 KB

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