SettlementLog.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class SettlementLog extends Auth
  7. {
  8. public function index(){
  9. if(request()->isAjax()){
  10. //分页参数
  11. $length = input('rows',10,'intval'); //每页条数
  12. $page = input('page',1,'intval'); //第几页
  13. $start = ($page - 1) * $length; //分页开始位置
  14. //排序
  15. $sortRow = input('sidx','u.id','trim'); //排序列
  16. $sort = input('sord','desc','trim'); //排序方式
  17. $order = 'id desc';
  18. $title = input('title','','trim');
  19. if($title){
  20. $map[] = ['name','like','%'.$title.'%'];
  21. }
  22. $phone = input('phone','','trim');
  23. if($phone){
  24. $map[] = ['phone','like','%'.$phone.'%'];
  25. }
  26. $recommend = input('recommend','','trim');
  27. if($recommend != ''){
  28. $map[] = ['recommend','=',$recommend];
  29. }
  30. $enable = input('enable','','trim');
  31. if($enable != ''){
  32. $map[] = ['enable','=',$enable];
  33. }
  34. $gender = input('gender','','trim');
  35. if($gender != ''){
  36. $map[] = ['gender','=',$gender];
  37. }
  38. $map[] = ['org_id','=',$this->orgId];
  39. $b = input('begin','','trim');
  40. $e = input('end','','trim');
  41. if($b!=''){
  42. $b = date('Y-m-d 00:00:00',strtotime($b));
  43. $map[] = ['create_time','>=',$b];
  44. }
  45. if($e!=''){
  46. $e = date('Y-m-d 23:59:59',strtotime($e));
  47. $map[] = ['create_time','<=',$e];
  48. }
  49. $map= empty($map) ? true: $map;
  50. //数据查询
  51. $lists = Db::name('settlement_log')
  52. ->where($map)
  53. ->limit($start,$length)
  54. ->order($order)
  55. ->select();
  56. foreach ($lists as $k=>$v){
  57. $lists[$k]['uName'] = Db::name('user')
  58. ->where('id',$v['user_id'])
  59. ->value('real_name');
  60. }
  61. //数据返回
  62. $totalCount = Db::name('settlement_log')
  63. ->where($map)->count();
  64. $totalPage = ceil($totalCount/$length);
  65. $result['page'] = $page;
  66. $result['total'] = $totalPage;
  67. $result['records'] = $totalCount;
  68. $result['rows'] = $lists;
  69. return json($result);
  70. }else{
  71. return $this->fetch();
  72. }
  73. }
  74. public function detail($id){
  75. $list = Db::name('worker_balance')
  76. ->where('org_id',$this->orgId)
  77. ->where('settlement_log_id',$id)
  78. ->order('id','desc')
  79. ->paginate(10,false,[
  80. 'query'=>input()
  81. ]);
  82. $render = $list->render();
  83. $data = $list->toArray();
  84. foreach ($data['data'] as $k=>$v){
  85. $data['data'][$k]['uName'] = Db::name('user')
  86. ->where('id',$v['user_id'])
  87. ->value('real_name');
  88. $work_user_id = Db::name('worker')
  89. ->where('id',$v['worker_id'])
  90. ->value('user_id');
  91. $data['data'][$k]['workName'] = Db::name('user')
  92. ->where('id',$work_user_id)
  93. ->value('real_name');
  94. }
  95. $this->assign('list',$data['data']);
  96. $this->assign('page',$render);
  97. $this->assign('id',$id);
  98. $this->assign('meta_title','结算记录详情');
  99. return $this->fetch();
  100. }
  101. //导出
  102. public function export(){
  103. $id = input('id','');
  104. //数据查询
  105. $lists = Db::name('settlement_log')
  106. ->where('id',$id)
  107. ->find();
  108. $ret = [];
  109. $log = Db::name('worker_balance')
  110. ->where('settlement_log_id',$id)
  111. ->select();
  112. foreach ($log as $k1=>&$v1){
  113. $v1['uName'] = Db::name('user')
  114. ->where('id',$v1['user_id'])
  115. ->value('real_name');
  116. $work_user_id = Db::name('worker')
  117. ->where('id',$v1['worker_id'])
  118. ->value('user_id');
  119. $v1['workName'] = Db::name('user')
  120. ->where('id',$work_user_id)
  121. ->value('real_name');
  122. $ret[] = $v1;
  123. }
  124. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  125. //实例化PHPExcel类
  126. $objPHPExcel = new \PHPExcel();
  127. //激活当前的sheet表
  128. $objPHPExcel->setActiveSheetIndex(0);
  129. //设置表格头(即excel表格的第一行)
  130. $objPHPExcel->setActiveSheetIndex(0)
  131. ->setCellValue('A1', '护工')
  132. ->setCellValue('B1', '金额')
  133. ->setCellValue('C1', '结算时间');
  134. // 设置表格头水平居中
  135. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  136. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  137. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  138. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  139. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  140. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  141. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  142. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  143. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  144. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  145. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  146. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  147. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  148. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  149. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  150. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  151. //设置列水平居中
  152. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  153. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  154. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  155. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  156. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  157. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  158. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  159. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  160. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  161. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  162. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  163. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  164. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  165. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  166. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  167. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  168. //设置单元格宽度
  169. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  170. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  171. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  172. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  173. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  174. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  175. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  176. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  177. //循环刚取出来的数组,将数据逐一添加到excel表格。
  178. for ($i = 0; $i < count($ret); $i++) {
  179. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['workName']);
  180. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['money']);
  181. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['create_time']);
  182. }
  183. //设置保存的Excel表格名称
  184. $filename = '结算记录_' . date('YmdHis', time()) . '.xls';
  185. //设置当前激活的sheet表格名称
  186. $objPHPExcel->getActiveSheet()->setTitle('结算记录');
  187. //设置浏览器窗口下载表格
  188. ob_end_clean();
  189. header("Content-Type: application/force-download");
  190. header("Content-Type: application/octet-stream");
  191. header("Content-Type: application/download");
  192. header('Content-Disposition:inline;filename="' . $filename);
  193. //生成excel文件
  194. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  195. //下载文件在浏览器窗口
  196. return $objWriter->save('php://output');
  197. }
  198. }