0
0

Settled.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class Settled extends Auth
  7. {
  8. protected $status = [
  9. 0=>'待分配',
  10. 1=>'进行中',
  11. 2=>'已完成',
  12. 3=>'已作废',
  13. 4=>'已结算'
  14. ];
  15. public function index(){
  16. if(request()->isAjax()){
  17. //分页参数
  18. $length = input('rows',10,'intval'); //每页条数
  19. $page = input('page',1,'intval'); //第几页
  20. $start = ($page - 1) * $length; //分页开始位置
  21. //排序
  22. $sortRow = input('sidx','u.id','trim'); //排序列
  23. $sort = input('sord','desc','trim'); //排序方式
  24. $order = 'a.id desc';
  25. $title = input('title','','trim');//用户
  26. if($title){
  27. $user = Db::name('user')
  28. ->alias('u')
  29. ->join('user_org uo','uo.user_id = u.id')
  30. ->where('u.real_name','like','%'.$title.'%')
  31. ->where('uo.org_id',$this->orgId)
  32. ->column('u.id');
  33. if(!empty($user)){
  34. $map[] = ['a.user_id','in',$user];
  35. }else{
  36. $map[] = ['a.user_id','=',0];
  37. }
  38. }
  39. $title1 = input('title1','','trim');//护工
  40. if($title1){
  41. $user = Db::name('worker')
  42. ->where('name','like','%'.$title1.'%')
  43. ->where('org_id',$this->orgId)
  44. ->column('id');
  45. if(!empty($user)){
  46. $ids = Db::name('todo')
  47. ->where('worker_id','in',$user)
  48. ->column('order_id');
  49. if(empty($ids)){
  50. $map[] = ['a.id','=',0];
  51. }else{
  52. $map[] = ['a.id','in',$ids];
  53. }
  54. }else{
  55. $map[] = ['a.id','=',0];
  56. }
  57. }
  58. $sn = input('sn','','trim');
  59. if($sn){
  60. $map[] = ['a.sn','=',$sn];
  61. }
  62. $settlement = input('settlement','','trim');
  63. if($settlement!=''){
  64. $map[] = ['a.settlement','=',$settlement];
  65. }
  66. $completion = input('completion','','trim');
  67. if($completion!=''){
  68. $map[] = ['a.completion','=',$completion];
  69. }
  70. $is_make_account = input('is_make_account','','trim');
  71. if($is_make_account!=''){
  72. $map[] = ['a.is_make_account','=',$is_make_account];
  73. }
  74. $addrId = input('addrId','','trim');
  75. if($addrId){
  76. $map[] = ['a.addr_id','=',$addrId];
  77. }
  78. $query = Db::name('ph_orders')->alias('a');
  79. $map[] = ['a.status','=',4];
  80. $map[] = ['a.org_id','=',$this->orgId];
  81. $b = input('begin','','trim');
  82. $e = input('end','','trim');
  83. if($b){
  84. $b = date('Y-m-d 00:00:00',strtotime($b));
  85. $map[] = ['a.create_time','>=',$b];
  86. }
  87. if($b){
  88. $e = date('Y-m-d 23:59:59',strtotime($e));
  89. $map[] = ['a.create_time','<=',$e];
  90. }
  91. $query1 = $query;
  92. $map= empty($map) ? true: $map;
  93. //数据查询
  94. $lists = $query->where($map)
  95. ->field('a.*')
  96. ->limit($start,$length)
  97. ->order($order)
  98. ->select();
  99. foreach ($lists as $k=>$v){
  100. $lists[$k]['userName'] = Db::name('user')
  101. ->where('id',$v['user_id'])
  102. ->value('real_name');
  103. $lists[$k]['depName'] = '';
  104. $lists[$k]['cateName'] = '';
  105. if($v['cate_id'] > 0){
  106. $lists[$k]['cateName'] = Db::name('cate')
  107. ->where('id',$v['cate_id'])
  108. ->value('title');
  109. }
  110. if($v['dep_id'] > 0){
  111. $lists[$k]['depName'] = Db::name('dep')
  112. ->where('id',$v['dep_id'])
  113. ->value('title');
  114. }
  115. }
  116. //数据返回
  117. $query2 = clone $query;
  118. $query3 = clone $query;
  119. $query4 = clone $query;
  120. $query5 = clone $query;
  121. $totalCount = $query1->alias('a')
  122. ->where($map)->count();
  123. $totalPage = ceil($totalCount/$length);
  124. $totalMoney = $query2->alias('a')
  125. ->where($map)->sum('a.settlement_money');
  126. $totalMoney1 = $query3->alias('a')
  127. ->where($map)->sum('a.amount');
  128. $result['totalMoney'] = $totalMoney;
  129. $result['zsr'] = $totalMoney1;
  130. $result['page'] = $page;
  131. $result['total'] = $totalPage;
  132. $result['records'] = $totalCount;
  133. $result['rows'] = $lists;
  134. return json($result);
  135. }else{
  136. $this->assign('status',$this->status);
  137. return $this->fetch();
  138. }
  139. }
  140. public function detail($id=0){
  141. $model = new \app\common\model\PhOrders();
  142. $info = $model->getInfo($id);
  143. if(!$info){
  144. $this->error('订单不存在');
  145. }
  146. $this->assign('info',$info);
  147. return $this->fetch();
  148. }
  149. //excel导出
  150. public function export(){
  151. if(request()->isGet()){
  152. $order = 'a.id desc';
  153. $title = input('title','','trim');//用户
  154. if($title){
  155. $user = Db::name('user')
  156. ->alias('u')
  157. ->join('user_org uo','uo.user_id = u.id')
  158. ->where('u.real_name','like','%'.$title.'%')
  159. ->where('uo.org_id',$this->orgId)
  160. ->column('u.id');
  161. if(!empty($user)){
  162. $map[] = ['a.user_id','in',$user];
  163. }else{
  164. $map[] = ['a.user_id','=',0];
  165. }
  166. }
  167. $title1 = input('title1','','trim');//护工
  168. if($title1){
  169. $user = Db::name('worker')
  170. ->where('name','like','%'.$title1.'%')
  171. ->where('org_id',$this->org_id)
  172. ->column('id');
  173. if(!empty($user)){
  174. $ids = Db::name('todo')
  175. ->where('worker_id','in',$user)
  176. ->column('order_id');
  177. if(empty($ids)){
  178. $map[] = ['a.id','=',0];
  179. }else{
  180. $map[] = ['a.id','in',$ids];
  181. }
  182. }else{
  183. $map[] = ['a.id','=',0];
  184. }
  185. }
  186. $sn = input('sn','','trim');
  187. if($sn){
  188. $map[] = ['a.sn','=',$sn];
  189. }
  190. $settlement = input('settlement','','trim');
  191. if($settlement!=''){
  192. $map[] = ['a.settlement','=',$settlement];
  193. }
  194. $completion = input('completion','','trim');
  195. if($completion!=''){
  196. $map[] = ['a.completion','=',$completion];
  197. }
  198. $is_make_account = input('is_make_account','','trim');
  199. if($is_make_account!=''){
  200. $map[] = ['a.is_make_account','=',$is_make_account];
  201. }
  202. $addrId = input('addrId','','trim');
  203. if($addrId){
  204. $map[] = ['a.addr_id','=',$addrId];
  205. }
  206. $query = Db::name('ph_orders')->alias('a');
  207. $map[] = ['a.status','=',4];
  208. $map[] = ['a.org_id','=',$this->orgId];
  209. $b = input('begin','','trim');
  210. $e = input('end','','trim');
  211. if($b){
  212. $b = date('Y-m-d 00:00:00',strtotime($b));
  213. $map[] = ['a.create_time','>=',$b];
  214. }
  215. if($b){
  216. $e = date('Y-m-d 23:59:59',strtotime($e));
  217. $map[] = ['a.create_time','<=',$e];
  218. }
  219. $query1 = $query;
  220. $map= empty($map) ? true: $map;
  221. //数据查询
  222. $lists = $query->where($map)
  223. ->field('a.*')
  224. ->order($order)
  225. ->select();
  226. foreach ($lists as $k=>$v){
  227. $lists[$k]['userName'] = Db::name('user')
  228. ->where('id',$v['user_id'])
  229. ->value('real_name');
  230. $lists[$k]['depName'] = '';
  231. $lists[$k]['cateName'] = '';
  232. if($v['cate_id'] > 0){
  233. $lists[$k]['cateName'] = Db::name('cate')->where('id',$v['cate_id'])->value('title');
  234. }
  235. if($v['dep_id'] > 0){
  236. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  237. }
  238. }
  239. //实例化PHPExcel类
  240. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  241. $objPHPExcel = new \PHPExcel();
  242. //激活当前的sheet表
  243. $objPHPExcel->setActiveSheetIndex(0);
  244. //设置表格头(即excel表格的第一行)
  245. $objPHPExcel->setActiveSheetIndex(0)
  246. ->setCellValue('A1', '订单编号')
  247. ->setCellValue('B1', '联系人')
  248. ->setCellValue('C1', '联系电话')
  249. ->setCellValue('D1', '科室')
  250. ->setCellValue('E1', '陪护服务')
  251. ->setCellValue('F1', '订单金额')
  252. ->setCellValue('G1', '结算金额')
  253. ->setCellValue('H1', '下单时间');
  254. // 设置表格头水平居中
  255. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  256. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  257. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  258. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  259. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  260. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  261. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  262. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  263. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  264. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  265. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  266. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  267. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  268. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  269. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  270. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  271. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  272. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  273. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  274. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  275. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
  276. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  277. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
  278. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  279. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M1')->getAlignment()
  280. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  281. //设置列水平居中
  282. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  283. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  284. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  285. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  286. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  287. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  288. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  289. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  290. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  291. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  292. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  293. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  294. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  295. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  296. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  297. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  298. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  299. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  300. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  301. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  302. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
  303. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  304. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
  305. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  306. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()
  307. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  308. //设置单元格宽度
  309. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  310. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  311. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  312. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  313. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  314. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  315. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  316. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  317. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  318. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  319. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(20);
  320. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(20);
  321. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(20);
  322. //循环刚取出来的数组,将数据逐一添加到excel表格。
  323. for ($i = 0; $i < count($lists); $i++) {
  324. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['sn']);
  325. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['contact']);
  326. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['phone']);
  327. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['depName']);
  328. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['cateName']);
  329. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['amount']);
  330. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['settlement_money']);
  331. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['create_time']);
  332. }
  333. //设置保存的Excel表格名称
  334. $filename = '已结算订单列表' . date('YmdHis', time()) . '.xls';
  335. //设置当前激活的sheet表格名称
  336. $objPHPExcel->getActiveSheet()->setTitle('已结算订单列表');
  337. //设置浏览器窗口下载表格
  338. ob_end_clean();
  339. header("Content-Type: application/force-download");
  340. header("Content-Type: application/octet-stream");
  341. header("Content-Type: application/download");
  342. header('Content-Disposition:inline;filename="' . $filename);
  343. //生成excel文件
  344. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  345. //下载文件在浏览器窗口
  346. return $objWriter->save('php://output');
  347. }
  348. }
  349. }