Order.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\store\controller;
  15. use app\api\controller\Alipay;
  16. use EasyWeChat\Factory;
  17. use library\Controller;
  18. use think\Db;
  19. use app\api\controller\Area;
  20. use app\api\controller\Crontab;
  21. use function GuzzleHttp\Psr7\_caseless_remove;
  22. /**
  23. * 订单记录管理
  24. * Class Order
  25. * @package app\store\controller
  26. */
  27. class Order extends Controller
  28. {
  29. /**
  30. * 绑定数据表
  31. * @var string
  32. */
  33. protected $table = 'store_order';
  34. /**
  35. * 订单记录管理
  36. * @auth true
  37. * @menu true
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. * @throws \think\exception\PDOException
  43. */
  44. public function index()
  45. {
  46. $this->root = $this->request->root(true);
  47. $this->title = '订单记录管理';
  48. $array = $this->statistical_order_info();
  49. $this->assign('a', $array);
  50. $this->byWhere(1)->field('a.*,b.name,b.headimg')->order('a.id desc')->page();
  51. }
  52. protected function statistical_order_info()
  53. {
  54. $array = array();
  55. $array['all_order'] = $this->byWhere(2)->count();
  56. $array['all_price'] = $this->byWhere(2)->where('a.status', '>',0)->sum('a.price_total');
  57. $array['weixin_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '1')->sum('a.price_total');
  58. $array['zfb_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '2')->sum('a.price_total');
  59. return $array;
  60. }
  61. /**
  62. * 搜索条件
  63. * @return \library\helper\QueryHelper
  64. */
  65. protected function byWhere($type)
  66. {
  67. if ($type == 1) {
  68. $query = $this->_query($this->table);
  69. } elseif ($type == 2) {
  70. $query = Db::name($this->table);
  71. }
  72. $query = $query->alias('a')->join('store_member b', 'a.user_id=b.id');
  73. $query->where('a.status','>',0);
  74. if (isset($_GET['order_no']) && $_GET['order_no']) {
  75. $query->where('a.order_no', 'like','%'.$_GET['order_no'].'%');
  76. }
  77. if (isset($_GET['status']) && $_GET['status'] != '') {
  78. $query->where('a.status', $_GET['status']);
  79. }
  80. if (isset($_GET['pay_status']) && $_GET['pay_status'] != '') {
  81. $query->where('a.pay_status', $_GET['pay_status']);
  82. }
  83. if (isset($_GET['pay_type']) && $_GET['pay_type']) {
  84. $query->where('a.pay_type', $_GET['pay_type']);
  85. }
  86. if (isset($_GET['create_at']) && $_GET['create_at']) {
  87. $time = explode(' - ', $_GET['create_at']);
  88. $start_date_time = $time[0] . ' 00:00:00';
  89. $end_date_time = $time[1] . ' 23:59:59';
  90. $query->whereBetweenTime('a.create_at', $start_date_time, $end_date_time);
  91. }
  92. if (isset($_GET['pay_at']) && $_GET['pay_at']) {
  93. $time = explode(' - ', $_GET['pay_at']);
  94. $start_date_time = $time[0] . ' 00:00:00';
  95. $end_date_time = $time[1] . ' 23:59:59';
  96. $query->whereBetweenTime('a.pay_at', $start_date_time, $end_date_time);
  97. }
  98. if (isset($_GET['user_info']) && $_GET['user_info']) {
  99. $query->where('b.name|b.phone', '=', $_GET['user_info'] );
  100. }
  101. if (isset($_GET['worker_info']) && $_GET['worker_info']) {
  102. $worker_id = Db::name('store_engineer')->where('name|phone', '=', $_GET['worker_info'] )->value('id');
  103. $query->where('worker_id',$worker_id);
  104. }
  105. return $query;
  106. }
  107. /**
  108. * 订单列表处理
  109. * @param array $data
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. protected function _index_page_filter(array &$data)
  115. {
  116. $mids = array_unique(array_merge(array_column($data, 'user_id'), array_column($data, 'user_id')));
  117. $memberList = Db::name('StoreMember')->whereIn('id', $mids)->select();
  118. $wids = array_unique(array_merge(array_column($data, 'worker_id'), array_column($data, 'worker_id')));
  119. $workerList = Db::name('store_engineer')->whereIn('id', $wids)->select();
  120. foreach ($data as &$vo) {
  121. list($vo['member'], $vo['worker']) = [[],[]];
  122. foreach ($memberList as $member) if ($member['id'] === $vo['user_id']) {
  123. $vo['member'] = $member;
  124. }
  125. foreach ($workerList as $worker) if ($worker['id'] === $vo['worker_id']) {
  126. $vo['worker'] = $worker;
  127. }
  128. $vo['serve_title'] = Db::name('store_goods')->where('id',$vo['goods_id'])->value('title');
  129. }
  130. }
  131. /**
  132. * 表单数据处理
  133. * @param array $data
  134. * @throws \think\Exception
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. * @throws \think\exception\PDOException
  139. */
  140. protected function _form_filter(array &$data)
  141. {
  142. if ($this->request->isGet()) {
  143. //接单人员信息
  144. if($data['worker_id']){
  145. $data['worker_info'] = Db::name('store_engineer')->field('name,phone')->where('id',$data['worker_id'])->find();
  146. }
  147. $this->data = $data;
  148. }
  149. }
  150. /**
  151. * 订单备注
  152. * @auth true
  153. * @throws \think\Exception
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\ModelNotFoundException
  156. * @throws \think\exception\DbException
  157. * @throws \think\exception\PDOException
  158. */
  159. public function order_remark()
  160. {
  161. $id = $this->app->request->get('id');
  162. $this->assign('id', $id);
  163. $post = $this->app->request->post();
  164. if (isset($post['id']) && $post['id']) {
  165. Db::name($this->table)->where('id', $post['id'])->update(['mark' => $post['remark']]);
  166. $this->success('编辑成功!');
  167. } else {
  168. $this->_form($this->table, 'order_remark');
  169. }
  170. }
  171. /**
  172. * 订单详情页
  173. */
  174. public function order_detail()
  175. {
  176. $this->_form($this->table);
  177. }
  178. /**
  179. * 订单记录
  180. * @auth true
  181. * @throws \think\Exception
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. * @throws \think\exception\DbException
  185. * @throws \think\exception\PDOException
  186. */
  187. public function order_status()
  188. {
  189. $id = $this->app->request->get('id');
  190. $this->assign('id', $id);
  191. $post = $this->app->request->post();
  192. if (isset($post['id']) && $post['id']) {
  193. Db::name($this->table)->where('id', $post['id'])->update(['remark' => $post['remark']]);
  194. $this->success('编辑成功!');
  195. } else {
  196. $this->_form($this->table);
  197. }
  198. }
  199. /**
  200. * 立即退款
  201. * @auth true
  202. * @throws \think\Exception
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\ModelNotFoundException
  205. * @throws \think\exception\DbException
  206. * @throws \think\exception\PDOException
  207. */
  208. public function order_refund()
  209. {
  210. $id = $this->app->request->get('id');
  211. $this->assign('id', $id);
  212. $post = $this->app->request->post();
  213. if (isset($post['id']) && $post['id']) {
  214. $order_info = Db::name($this->table)->where('id', $post['id'])->find();
  215. if($post['amount'] < 0 || $post['amount'] > $order_info['price_total']){
  216. $this->error('退款金额错误');
  217. }
  218. $refund_success = 0;
  219. $refund_info = Db::name('store_order_refund')->where('order_no', $order_info['order_no'])->find();
  220. if($order_info['pay_type'] == 1){ //微信退款
  221. $app = Factory::payment(config('app.wx_pay'));
  222. $result = $app->refund->byOutTradeNumber($order_info['pay_no'], $refund_info['refund_no'],$order_info['price_total']*100, $post['amount']*100, [
  223. // 可在此处传入其他参数,详细参数见微信支付文档
  224. 'refund_desc' => $refund_info['reason'],
  225. ]);
  226. if($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS'){
  227. $refund_success = 1;
  228. }
  229. }elseif($order_info['pay_type'] == 2){
  230. $result = Alipay::ali_refund($order_info['pay_no'],$post['amount']);
  231. if($result){
  232. $refund_success = 1;
  233. }
  234. }else{ //余额退款
  235. $balance_data = array(
  236. 'amount' => $post['amount'],
  237. 'user_id' => $order_info['user_id'],
  238. 'user_type' => 1,
  239. 'get_type' => 4,
  240. 'order_id' => $order_info['id'],
  241. 'status' => 1
  242. );
  243. Db::name('store_balance_list')->insert($balance_data);
  244. Db::name('store_member')->where('id',$order_info['user_id'])->setInc('balance',$refund_info['amount']);
  245. $refund_success = 1;
  246. }
  247. if($refund_success == 1){
  248. Db::name('store_order_refund')->where('order_no',$order_info['order_no'])->update(array('status'=>1,'intervene_remark'=>$post['intervene_remark'],'dispose_at'=>date('Y-m-d H:i:s'),'amount'=>$post['amount']));
  249. //更改退款状态
  250. Db::name('store_order')->where('id',$id)->update(array('refund_state'=>2,'status'=>7,'refund_intervene'=>2,'refund_price'=>$post['amount']));
  251. //部分退款的剩下的给工人
  252. $residue_amount = $order_info['price_total'] - $post['amount'];
  253. if($residue_amount){
  254. //给接单端发送余额
  255. $balance_info = Db::name('store_balance_list')->where('user_id','>',0)->where('order_id',$id)->where('user_type',2)->where('status',0)->find();
  256. if($balance_info){
  257. //更改服务人员可拿的余额
  258. $balance_amount = sprintf("%.2f",$residue_amount * $order_info['worker_ratio'] / 100);
  259. Db::name('store_balance_list')->where('id',$balance_info['id'])->update(array('status'=>1,'amount'=>$balance_amount));
  260. Db::name('store_worker')->where('id',$balance_info['user_id'])->setInc('balance',$balance_amount);
  261. Db::name('store_worker')->where('id',$balance_info['user_id'])->setInc('balance_all',$balance_amount);
  262. }
  263. }
  264. $this->success('退款成功');
  265. }else{
  266. $this->error('退款失败');
  267. }
  268. } else {
  269. $this->_form($this->table, 'order_rufund');
  270. }
  271. }
  272. /**
  273. * 不退款
  274. * @auth true
  275. * @throws \think\Exception
  276. * @throws \think\db\exception\DataNotFoundException
  277. * @throws \think\db\exception\ModelNotFoundException
  278. * @throws \think\exception\DbException
  279. * @throws \think\exception\PDOException
  280. */
  281. public function order_no_refund()
  282. {
  283. $id = $this->app->request->get('id');
  284. $this->assign('id', $id);
  285. $post = $this->app->request->post();
  286. if (isset($post['id']) && $post['id']) {
  287. $order=Db::table('store_order')->where('id',$post['id'])->find();
  288. Db::name('store_order')->where('id', $post['id'])->update(['refund_state' => 3,'refund_intervene'=>2]);
  289. Db::name('store_order_refund')->where('order_no', $order['order_no'])
  290. ->update(['status' => 2,'intervene_remark'=>$post['intervene_remark'],'dispose_at'=>date('Y-m-d H:i:s')]);
  291. $this->success('拒绝成功!');
  292. } else {
  293. $this->_form($this->table, 'order_no_rufund');
  294. }
  295. }
  296. /**
  297. * 处理投诉
  298. * @auth true
  299. * @throws \think\Exception
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. * @throws \think\exception\DbException
  303. * @throws \think\exception\PDOException
  304. */
  305. public function dispose_complain()
  306. {
  307. $id = $this->app->request->get('id');
  308. $dispose_status = $this->app->request->get('dispose_status');
  309. $this->assign('id', $id);
  310. $this->assign('dispose_status', $dispose_status);
  311. $post = $this->app->request->post();
  312. if (isset($post['id']) && $post['id']) {
  313. $order_no = Db::name('store_order')->where('id',$id)->value('order_no');
  314. Db::name('store_complain')->where('order_no',$order_no)->update(array('result_info'=>$post['result_info'],'is_dispose'=>1,'dispose_at'=>date('Y-m-d H:i:s'),'dispose_status'=>$dispose_status));
  315. Db::name('store_order')->where('id',$id)->update(array('complain_status'=>2));
  316. $this->success('提交成功');
  317. }else{
  318. $this->_form($this->table, 'dispose_complain');
  319. }
  320. }
  321. /**
  322. * 修改时间
  323. * @auth true
  324. * @throws \think\Exception
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. * @throws \think\exception\DbException
  328. * @throws \think\exception\PDOException
  329. */
  330. public function update_time()
  331. {
  332. $id = $this->app->request->get('id');
  333. $this->assign('id', $id);
  334. $post = $this->app->request->post();
  335. if (isset($post['id']) && $post['id']) {
  336. $order_info = Db::name('store_order')->field('id,order_no,appoint_time')->where('id',$id)->where('status',1)->where('work_status','in',[0,1])->find();
  337. if(empty($order_info)){
  338. $this->error('订单信息有误');
  339. }
  340. $post['appoint_time'] = date('Y-m-d H:00:00',strtotime($post['appoint_time']));
  341. if($post['appoint_time'] == $order_info['appoint_time']){
  342. $this->error('修改后的时间与服务时间一致,无需修改');
  343. }
  344. $res = update_appoint_time($order_info['order_no'],1,$post['appoint_time']);
  345. if($res['code']){
  346. $this->success('修改成功');
  347. }else{
  348. $this->error($res['msg']);
  349. }
  350. } else {
  351. $this->_form($this->table, 'update_time');
  352. }
  353. }
  354. /**
  355. * 挂起订单
  356. * @auth true
  357. * @throws \think\Exception
  358. * @throws \think\exception\PDOException
  359. */
  360. public function hang_up()
  361. {
  362. //给用户发短信通知
  363. $id = $_POST['id'];
  364. $user_id = Db::name($this->table)->where('id',$id)->value('user_id');
  365. $phone = Db::name('store_member')->where('id',$user_id)->value('phone');
  366. short_note($phone);
  367. $this->_save($this->table, ['is_hang_up' => '1','apply_hang_up'=>0]);
  368. }
  369. public function export(){
  370. $data = $this->byWhere(2)->field('a.*,b.name,b.phone')->order('id desc')->select();
  371. foreach ($data as &$vo){
  372. //服务标题
  373. $vo['serve_title'] = Db::name('store_serve')->where('id',$vo['serve_id'])->value('title');
  374. //配件
  375. $vo['parts_title'] = '--';
  376. if($vo['parts_id']){
  377. $vo['parts_title'] = Db::name('store_serve_parts')->where('id',$vo['parts_id'])->value('title');
  378. }
  379. //分成信息
  380. $vo['worker_serve_amount'] = 0;
  381. $vo['worker_parts_amount'] = 0;
  382. $vo['agency_amount'] = 0;
  383. $vo['agency_parts_amount'] = 0;
  384. $vo['extend_amount'] = 0;
  385. if($vo['status'] > 0){
  386. $vo['worker_serve_amount'] = sprintf("%.2f",($vo['price_total'] - $vo['price_parts']) * $vo['worker_ratio']/100);
  387. $vo['worker_parts_amount'] = sprintf("%.2f",$vo['price_parts'] * $vo['worker_parts_ratio']/100);
  388. $vo['agency_amount'] = sprintf("%.2f",($vo['price_total'] - $vo['price_parts']) * $vo['agency_ratio']/100);
  389. $vo['agency_parts_amount'] = sprintf("%.2f",$vo['price_parts'] * $vo['agency_parts_ratio']/100);
  390. if($vo['puser_id']){
  391. $vo['extend_amount'] = sprintf("%.2f",($vo['price_total'] - $vo['price_parts']) * $vo['extend_ratio']/100);
  392. }
  393. }
  394. }
  395. $field=array(
  396. 'A' => array('name', '下单人姓名'),
  397. 'B' => array('phone', '下单人手机号'),
  398. 'C' => array('order_no', '订单编号'),
  399. 'D' => array('price_goods', '订单金额(元)'),
  400. 'E' => array('price_total', '支付金额(元)'),
  401. 'F' => array('serve_title', '服务名称'),
  402. 'G' => array('parts_title', '配件名称'),
  403. 'H' => array('worker_serve_amount', '工程师服务佣金(元)'),
  404. 'I' => array('worker_parts_amount', '工程师配件佣金(元)'),
  405. 'J' => array('agency_amount', '代理服务佣金(元)'),
  406. 'K' => array('agency_parts_amount', '代理配件佣金(元)'),
  407. 'L' => array('extend_amount', '推广员佣金(元)'),
  408. 'M' => array('create_at', '下单时间'),
  409. );
  410. $this->phpExcelList($field,$data,'订单列表');
  411. }
  412. public function phpExcelList($field=[],$list=[],$title='文件'){
  413. $PHPExcel=new \PHPExcel();
  414. $PHPSheet=$PHPExcel->getActiveSheet();
  415. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  416. foreach($list as $key=>$value)
  417. {
  418. foreach($field as $k=>$v){
  419. if($key == 0){
  420. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  421. }
  422. $i=$key+2;
  423. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  424. }
  425. }
  426. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  427. // Redirect output to a client’s web browser (Excel5)
  428. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  429. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  430. header('Cache-Control: max-age=0'); //禁止缓存
  431. $PHPWriter->save("php://output"); //输出到浏览器
  432. }
  433. }