ActivityApply.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\ActivityApplyItem;
  4. use app\common\model\StoreOrderRefund;
  5. use app\common\service\OrderCallback;
  6. use library\Controller;
  7. use think\Db;
  8. /**
  9. * 活动
  10. * Class ActivityApply
  11. * @package app\operate\controller
  12. */
  13. class ActivityApply extends Controller
  14. {
  15. protected $table = 'ActivityApply';
  16. /**
  17. * 列表
  18. * @auth true
  19. * @menu true
  20. * @throws \think\Exception
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. * @throws \think\exception\PDOException
  25. */
  26. public function index()
  27. {
  28. $this->status_list = [0=>'待支付',1=>'已支付',2=>'已取消'];
  29. $this->order_status = $this->request->get('order_status',-1);
  30. $id = $this->request->get('act_id');
  31. $name = $this->request->get('name');
  32. $phone = $this->request->get('phone');
  33. $source_type = $this->request->get('source_type');
  34. $order_status = $this->request->get('order_status',-1);
  35. $tg_id = $this->request->get('tg_id',0);
  36. $this->title = '报名记录';
  37. $where = [];
  38. $where[]= ['a.act_id','=' ,$id];
  39. $where[]= ['a.is_deleted','=' ,0];
  40. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  41. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  42. if($tg_id) $where[]= ['a.tg_id','=' ,$tg_id];
  43. if(in_array($order_status,[0,1,2])) $where[]= ['a.status','=' ,$order_status];
  44. if($source_type == 1) $where[]= ['a.pay_type','<>' ,9];
  45. if($source_type == 2) $where[]= ['a.pay_type','=' ,9];
  46. $this->qrarr= Db::name('activity_qrcode')->where('act_id',$id)->column('tg_param,id,phone','id');
  47. $query = $this->_query('activity_apply')
  48. ->alias('a')
  49. ->field('a.*,u.name user_name,u.headimg,u.phone')
  50. ->where($where)
  51. ->leftJoin('store_member u','u.id = a.user_id')
  52. ->order('a.id desc')->page();
  53. }
  54. /**
  55. * 编辑
  56. * @auth true
  57. * @menu true
  58. * @throws \think\Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @throws \think\exception\DbException
  62. * @throws \think\exception\PDOException
  63. */
  64. public function edit()
  65. {
  66. $this->title = '编辑';
  67. $this->_form($this->table, 'form');
  68. }
  69. /**
  70. * 删除
  71. * @auth true
  72. * @throws \think\Exception
  73. * @throws \think\exception\PDOException
  74. */
  75. public function del()
  76. {
  77. $this->_save($this->table, ['is_deleted' => '1']);
  78. }
  79. /**
  80. * 批量删除
  81. * @auth true
  82. * @throws \think\Exception
  83. * @throws \think\exception\PDOException
  84. */
  85. public function remove()
  86. {
  87. $this->_save($this->table, ['is_deleted' => '1']);
  88. }
  89. /**
  90. * 表单数据处理
  91. * @auth true
  92. * @menu true
  93. * @param array $data
  94. */
  95. protected function _form_filter(&$data)
  96. {
  97. if ($this->request->isGet() && $this->request->action() == 'edit'){
  98. $this->order_item = ActivityApplyItem::where(['o.apply_id'=>$data['id']])->alias('o')
  99. ->field('o.*,p.ladder_title,p.ladder_price')
  100. ->leftJoin('ActivityPrice p','p.id=o.price_id')
  101. ->select()->toArray();
  102. $this->data = $data;
  103. }
  104. if($this->request->get() && $this->request->action() == 'audit')
  105. {
  106. $data['refund_money'] = StoreOrderRefund::getToRefund($data['id']);
  107. }
  108. }
  109. /**
  110. * 确认收款
  111. * @auth true
  112. * @throws \think\Exception
  113. * @throws \think\exception\PDOException
  114. */
  115. public function confirm()
  116. {
  117. $order_info = \app\common\model\ActivityApply::where('id',input('id'))->find()->toArray();
  118. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  119. if(!$back_res['ret_val']) $this->error($back_res['msg']);
  120. $this->success('操作成功');
  121. }
  122. /**
  123. * 确认退款
  124. * @auth true
  125. * @throws \think\Exception
  126. * @throws \think\exception\PDOException
  127. */
  128. public function refund()
  129. {
  130. $id = input('post.id');
  131. \app\common\model\ActivityApply::where('id',$id)->update(['refund_state'=>2]);
  132. $order_info = \app\common\model\ActivityApply::where('id',$id)->find()->toArray();
  133. $act_title = \app\common\model\Activity::where('id',$order_info['act_id'])->value('title');
  134. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_order_info?id='.$id;
  135. $message = "您已取消《".$act_title."》活动订单,订单有效金额已原路返回,订单对应所有门票已失效";//."点击<a href=\"$info_url\">查看详情</a>";
  136. $res = \app\common\service\Activity::orderRefundMoney($id,$message);
  137. $res['code'] == 200 ? $this->success('操作成功') : $this->error($res['msg']);
  138. }
  139. /**
  140. * 退款审核
  141. * @auth true
  142. * @throws \think\Exception
  143. * @throws \think\exception\PDOException
  144. */
  145. public function audit()
  146. {
  147. $this->title = '退款审核';
  148. $this->state_arr = ['未申请','审核中','同意退款','审核拒绝','退款异常','退款成功','取消申请'];
  149. if($this->request->isPost())
  150. {
  151. list($post)= [$this->request->post()];
  152. if($post['before_refund_state'] == 0) $this->error('订单未申请退款');
  153. if($post['before_refund_state'] == 6) $this->error('订单退款申请已取消');
  154. if($post['before_refund_state'] == 5) $this->error('订单已退款');
  155. if(!in_array($post['before_refund_state'],[1,4])) $this->error('无需重复审核');
  156. $order_info = \app\common\model\ActivityApply::where('id',$post['id'])->find()->toArray();
  157. $act_title = \app\common\model\Activity::where('id',$order_info['act_id'])->value('title');
  158. // 线下订单
  159. if($order_info['pay_type'] == 9) {
  160. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>$post['refund_state']]);
  161. if($post['refund_state'] == 2) StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>3]);
  162. if($post['refund_state'] == 3) StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>2]);
  163. $this->success('操作成功');
  164. }
  165. switch ($post['refund_state'])
  166. {
  167. case 2:
  168. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_order_info?id='.$order_info['id'];
  169. $message = "您已取消《".$act_title."》活动订单,订单有效金额已原路返回,订单对应所有门票已失效";//."点击<a href=\"$info_url\">查看详情</a>";
  170. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>2]);
  171. $res = \app\common\service\Activity::orderRefundMoney($post['id'],$message);
  172. $res['code'] == 200 ? $this->success('操作成功') : $this->error($res['msg']);
  173. break;
  174. case 3:
  175. \app\common\model\ActivityApply::where('id',$post['id'])->update(['refund_state'=>3]);
  176. StoreOrderRefund::where(['order_id'=>$post['id']])->where('status','in','0,1,4')->update(['status'=>2]);
  177. break;
  178. }
  179. $this->success('操作成功');
  180. }
  181. $this->_form($this->table, 'audit');
  182. }
  183. public function export(){
  184. $this->order_status = $this->request->get('order_status',-1);
  185. $id = $this->request->get('act_id');
  186. $name = $this->request->get('name');
  187. $phone = $this->request->get('phone');
  188. $source_type = $this->request->get('source_type');
  189. $order_status = $this->request->get('order_status',-1);
  190. $tg_id = $this->request->get('tg_id',0);
  191. $where[]= ['a.act_id','=' ,$id];
  192. $where[]= ['a.is_deleted','=' ,0];
  193. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  194. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  195. if($tg_id) $where[]= ['a.tg_id','=' ,$tg_id];
  196. if(in_array($order_status,[0,1,2])) $where[]= ['a.status','=' ,$order_status];
  197. if($source_type == 1) $where[]= ['a.pay_type','<>' ,9];
  198. if($source_type == 2) $where[]= ['a.pay_type','=' ,9];
  199. $data =Db::name('activity_apply')
  200. ->alias('a')
  201. ->field('a.*,u.name user_name,u.headimg,u.phone,t.phone tg_phone,t.qrcode,y.title act_title')
  202. ->where($where)
  203. ->leftJoin('store_member u','u.id = a.user_id')
  204. ->leftJoin('activity_qrcode t','t.id = a.tg_id')
  205. ->leftJoin('activity y','y.id = a.act_id')
  206. ->order('a.id desc')->select();
  207. if(empty($data)) $this->error('暂无可以导出的数据');
  208. foreach ($data as $k=>&$v)
  209. {
  210. $v['order_source'] = $v['pay_type'] == 9 ? '转账订单':'线上订单';
  211. $v['pay_desc'] = $v['pay_state'] == 1 ? '已支付':'未支付';
  212. }
  213. $field=array(
  214. 'A' => array('order_no', '订单号'),
  215. 'B' => array('user_name', '联系人'),
  216. 'C' => array('phone', '电话'),
  217. 'D' => array('money','订单金额'),
  218. 'E' => array('num','人数'),
  219. 'F' => array('act_title','活动'),
  220. 'G' => array('order_source', '订单类型'),
  221. 'H' => array('pay_desc', '支付状态'),
  222. 'I' => array('tg_phone', '推广手机号'),
  223. 'J' => array('qrcode', '推广码链接'),
  224. );
  225. $this->phpExcelList($field,$data,'报名订单_'.date('Y-m-d H:i:s'));
  226. }
  227. public function phpExcelList($field=[],$list=[],$title='文件'){
  228. $PHPExcel=new \PHPExcel();
  229. $PHPSheet=$PHPExcel->getActiveSheet();
  230. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  231. foreach($list as $key=>$value)
  232. {
  233. foreach($field as $k=>$v){
  234. if($key == 0){
  235. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  236. }
  237. $i=$key+2;
  238. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  239. }
  240. }
  241. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  242. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  243. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  244. header('Cache-Control: max-age=0'); //禁止缓存
  245. $PHPWriter->save("php://output"); //输出到浏览器
  246. }
  247. }