Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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['all_refund_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->sum('a.refund_price');
  58. $array['all_price'] = $array['all_price'] - $array['all_refund_price'];
  59. $array['weixin_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '1')->sum('a.price_total');
  60. $array['weixin_refund_all_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->where('a.pay_type', '1')->sum('a.refund_price');
  61. $array['weixin_all_price'] = $array['weixin_all_price'] - $array['weixin_refund_all_price'];
  62. $array['zfb_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '2')->sum('a.price_total');
  63. $array['zfb_refund_all_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->where('a.pay_type', '2')->sum('a.refund_price');
  64. $array['zfb_all_price'] = $array['zfb_all_price'] - $array['zfb_refund_all_price'];
  65. $array['engineer_id'] = session('user.engineer_id');
  66. return $array;
  67. }
  68. /**
  69. * 搜索条件
  70. * @return \library\helper\QueryHelper
  71. */
  72. protected function byWhere($type)
  73. {
  74. if ($type == 1) {
  75. $query = $this->_query($this->table);
  76. } elseif ($type == 2) {
  77. $query = Db::name($this->table);
  78. }
  79. $query = $query->alias('a')->join('store_member b', 'a.user_id=b.id');
  80. /*$engineer_id = session('user.engineer_id');
  81. if($engineer_id > 0){
  82. $query->where('a.worker_id = '.$engineer_id.' or a.status = 1');
  83. }*/
  84. if (isset($_GET['order_no']) && $_GET['order_no']) {
  85. $query->where('a.order_no', 'like','%'.$_GET['order_no'].'%');
  86. }
  87. if (isset($_GET['status']) && $_GET['status'] != '') {
  88. $query->where('a.status', $_GET['status']);
  89. }
  90. if (isset($_GET['is_self_build']) && $_GET['is_self_build'] != '') {
  91. $query->where('a.is_self_build', $_GET['is_self_build']);
  92. }
  93. if (isset($_GET['pay_status']) && $_GET['pay_status'] != '') {
  94. $query->where('a.pay_status', $_GET['pay_status']);
  95. }
  96. if (isset($_GET['pay_type']) && $_GET['pay_type']) {
  97. $query->where('a.pay_type', $_GET['pay_type']);
  98. }
  99. if (isset($_GET['create_at']) && $_GET['create_at']) {
  100. $time = explode(' - ', $_GET['create_at']);
  101. $start_date_time = $time[0] . ' 00:00:00';
  102. $end_date_time = $time[1] . ' 23:59:59';
  103. $query->whereBetweenTime('a.create_at', $start_date_time, $end_date_time);
  104. }
  105. if (isset($_GET['pay_at']) && $_GET['pay_at']) {
  106. $time = explode(' - ', $_GET['pay_at']);
  107. $start_date_time = $time[0] . ' 00:00:00';
  108. $end_date_time = $time[1] . ' 23:59:59';
  109. $query->whereBetweenTime('a.pay_at', $start_date_time, $end_date_time);
  110. }
  111. if (isset($_GET['user_info']) && $_GET['user_info']) {
  112. $query->where('b.name|b.phone', '=', $_GET['user_info'] );
  113. }
  114. if (isset($_GET['worker_info']) && $_GET['worker_info']) {
  115. $worker_id = Db::name('store_engineer')->where('name|phone', '=', $_GET['worker_info'] )->value('id');
  116. $query->where('worker_id',$worker_id);
  117. }
  118. return $query;
  119. }
  120. /**
  121. * 订单列表处理
  122. * @param array $data
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. protected function _index_page_filter(array &$data)
  128. {
  129. $mids = array_unique(array_merge(array_column($data, 'user_id'), array_column($data, 'user_id')));
  130. $memberList = Db::name('StoreMember')->whereIn('id', $mids)->select();
  131. $wids = array_unique(array_merge(array_column($data, 'worker_id'), array_column($data, 'worker_id')));
  132. $workerList = Db::name('store_engineer')->whereIn('id', $wids)->select();
  133. foreach ($data as &$vo) {
  134. list($vo['member'], $vo['worker']) = [[],[]];
  135. foreach ($memberList as $member) if ($member['id'] === $vo['user_id']) {
  136. $vo['member'] = $member;
  137. }
  138. foreach ($workerList as $worker) if ($worker['id'] === $vo['worker_id']) {
  139. $vo['worker'] = $worker;
  140. }
  141. $vo['serve_title'] = Db::name('store_goods')->where('id',$vo['goods_id'])->value('title');
  142. }
  143. }
  144. /**
  145. * 添加订单
  146. * @auth true
  147. * @throws \think\Exception
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. * @throws \think\exception\DbException
  151. * @throws \think\exception\PDOException
  152. */
  153. public function add()
  154. {
  155. $this->title = '添加订单';
  156. //服务类目
  157. $this->cate_arr = Db::name('store_goods')->field('id,title')->where('status',1)->where('is_deleted',0)->order(['sort'=>'desc','id'=>'desc'])->select();
  158. //服务类型
  159. $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测');
  160. $this->serve_type_arr = $serve_type_arr;
  161. $this->member_list = Db::name('store_member')->field('id,name,phone')->where('status',1)->where('is_deleted',0)->select();
  162. $this->_form($this->table, 'form');
  163. }
  164. /**
  165. * 表单数据处理
  166. * @param array $data
  167. * @throws \think\Exception
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. * @throws \think\exception\DbException
  171. * @throws \think\exception\PDOException
  172. */
  173. protected function _form_filter(array &$data)
  174. {
  175. if ($this->request->isGet()) {
  176. //接单人员信息
  177. if(isset($data['worker_id']) && $data['worker_id']){
  178. $data['worker_info'] = Db::name('store_engineer')->field('name,phone')->where('id',$data['worker_id'])->find();
  179. }
  180. $this->data = $data;
  181. }else{
  182. $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测');
  183. $data['worker_id'] = 0;
  184. $data['price_amount'] = $data['price_total'];
  185. $data['is_self_build'] = 1;
  186. $data['serve_type'] = $serve_type_arr[$data['serve_type_id']];
  187. $data['order_no'] = get_order_sn();
  188. $data['pay_no'] = get_order_sn();
  189. }
  190. }
  191. /**
  192. * 表单结果处理
  193. * @param boolean $result
  194. */
  195. protected function _form_result($result)
  196. {
  197. if ($result && $this->request->isPost()) {
  198. $this->success('操作成功!', 'javascript:history.back()');
  199. }
  200. }
  201. /**
  202. * 订单详情
  203. * @auth true
  204. * @throws \think\Exception
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. * @throws \think\exception\DbException
  208. * @throws \think\exception\PDOException
  209. */
  210. public function order_detail()
  211. {
  212. $this->_form($this->table);
  213. }
  214. /**
  215. * 订单记录
  216. * @auth true
  217. * @throws \think\Exception
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. * @throws \think\exception\DbException
  221. * @throws \think\exception\PDOException
  222. */
  223. public function order_status()
  224. {
  225. $id = $this->app->request->get('id');
  226. $this->assign('id', $id);
  227. $post = $this->app->request->post();
  228. if (isset($post['id']) && $post['id']) {
  229. Db::name($this->table)->where('id', $post['id'])->update(['remark' => $post['remark']]);
  230. $this->success('编辑成功!');
  231. } else {
  232. $this->_form($this->table);
  233. }
  234. }
  235. /**
  236. * 接单
  237. * @auth true
  238. * @throws \think\Exception
  239. * @throws \think\exception\PDOException
  240. */
  241. public function receive()
  242. {
  243. //发送邮箱
  244. $engineer_mail = Db::name('store_engineer')->where('mail is not null')->where('id',session('user.engineer_id'))->where('is_deleted',0)->value('mail');
  245. if($engineer_mail){
  246. mail_push($engineer_mail,$this->request->post('id'));
  247. }
  248. $order_no = Db::name('store_order')->where('id',$this->request->post('id'))->value('order_no');
  249. $send_log_data = array(
  250. 'order_id' => $this->request->post('id'),
  251. 'order_no' => $order_no,
  252. 'worker_id' => session('user.engineer_id'),
  253. 'send_worker_id' => session('user.engineer_id')
  254. );
  255. Db::name('store_send_log')->insert($send_log_data);
  256. $this->_save($this->table, ['worker_id' => session('user.engineer_id'),'status'=>2,'start_at'=>date('Y-m-d H:i:s')]);
  257. }
  258. /**
  259. * 指派订单
  260. * @auth true
  261. * @throws \think\Exception
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. * @throws \think\exception\DbException
  265. * @throws \think\exception\PDOException
  266. */
  267. public function send_order()
  268. {
  269. $id = $this->app->request->get('id');
  270. $this->assign('id', $id);
  271. $post = $this->app->request->post();
  272. if (isset($post['id']) && $post['id']) {
  273. $worker_id = $post['worker_id'];
  274. $order_info = Db::name('store_order')->where('id',$id)->find();
  275. $order_data = array(
  276. 'status' => $post['status'],
  277. 'price_total' => $post['price_total'],
  278. 'price_amount' => $post['price_total'] + $order_info['coupon_amount'],
  279. 'remark' => $post['remark']
  280. );
  281. if($post['status'] == 1){ //未分配
  282. $order_data['worker_id'] = 0;
  283. $order_data['start_at'] = null;
  284. }else{
  285. $order_data['worker_id'] = $worker_id;
  286. }
  287. Db::name('store_order')->where('id',$id)->update($order_data);
  288. if($worker_id != $order_info['worker_id']){
  289. //添加指派记录
  290. $order_no = Db::name('store_order')->where('id',$id)->value('order_no');
  291. $send_log_data = array(
  292. 'order_id' => $id,
  293. 'order_no' => $order_no,
  294. 'worker_id' => session('user.engineer_id'),
  295. 'send_worker_id' => $worker_id
  296. );
  297. Db::name('store_send_log')->insert($send_log_data);
  298. //发送邮箱
  299. $engineer_mail = Db::name('store_engineer')->where('mail is not null')->where('id',$worker_id)->where('is_deleted',0)->value('mail');
  300. if($engineer_mail){
  301. mail_push($engineer_mail,$id);
  302. }
  303. }
  304. $this->success('操作成功!');
  305. } else {
  306. /*$engineer_id = session('user.engineer_id');*/
  307. $store_worker = Db::name('store_engineer')->field('id,name,phone')->where('is_deleted',0)->select();
  308. if($store_worker){
  309. $this->store_worker = $store_worker;
  310. $this->_form($this->table, 'send_order');
  311. }else{
  312. $this->error('暂没有合适的工程师');
  313. }
  314. }
  315. }
  316. /**
  317. * 修改金额
  318. * @auth true
  319. * @throws \think\Exception
  320. * @throws \think\db\exception\DataNotFoundException
  321. * @throws \think\db\exception\ModelNotFoundException
  322. * @throws \think\exception\DbException
  323. * @throws \think\exception\PDOException
  324. */
  325. public function edit_order()
  326. {
  327. $id = $this->app->request->get('id');
  328. $this->assign('id', $id);
  329. $post = $this->app->request->post();
  330. if (isset($post['id']) && $post['id']) {
  331. $order_data = array(
  332. 'price_total' => $post['price_total'],
  333. 'price_amount' => $post['price_total']
  334. );
  335. // 用户支付成功处理
  336. if($post['pay_status'] == 1){
  337. $order_data['pay_at'] = date('Y-m-d H:i:s');
  338. $order_data['pay_status'] = 1;
  339. $order_data['status'] = 1;
  340. }
  341. Db::name('store_order')->where('id',$id)->update($order_data);
  342. $this->success('操作成功!');
  343. } else {
  344. $this->_form($this->table, 'edit_order');
  345. }
  346. }
  347. /**
  348. * 立即退款
  349. * @auth true
  350. * @throws \think\Exception
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\ModelNotFoundException
  353. * @throws \think\exception\DbException
  354. * @throws \think\exception\PDOException
  355. */
  356. public function order_refund()
  357. {
  358. $id = $this->app->request->get('id');
  359. $this->assign('id', $id);
  360. $post = $this->app->request->post();
  361. if (isset($post['id']) && $post['id']) {
  362. $order_info = Db::name($this->table)->where('id', $post['id'])->find();
  363. if($post['amount'] < 0 || $post['amount'] > $order_info['price_total']){
  364. $this->error('退款金额错误');
  365. }
  366. $refund_success = 0;
  367. if($order_info['pay_type'] == 1){ //微信退款
  368. $app = Factory::payment(config('app.wx_pay'));
  369. $result = $app->refund->byOutTradeNumber($order_info['pay_no'], get_order_sn(),$order_info['price_total']*100, $post['amount']*100, [
  370. // 可在此处传入其他参数,详细参数见微信支付文档
  371. 'refund_desc' => '',
  372. ]);
  373. if($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS'){
  374. $refund_success = 1;
  375. }
  376. }elseif($order_info['pay_type'] == 2){
  377. $result = Alipay::ali_refund($order_info['pay_no'],$post['amount']);
  378. if($result){
  379. $refund_success = 1;
  380. }
  381. }
  382. if($refund_success == 1){
  383. //更改退款状态
  384. Db::name('store_order')->where('id',$post['id'])->update(array('refund_at'=>date('Y-m-d H:i:s'),'status'=>4,'refund_price'=>$post['amount']));
  385. $this->success('退款成功');
  386. }else{
  387. $this->error('退款失败');
  388. }
  389. } else {
  390. $this->_form($this->table, 'order_rufund');
  391. }
  392. }
  393. /**
  394. * 删除订单
  395. * @auth true
  396. * @throws \think\Exception
  397. * @throws \think\exception\PDOException
  398. */
  399. public function remove()
  400. {
  401. $this->_save($this->table, ['is_deleted' => '1']);
  402. }
  403. }