WashRecord.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\WashingRecord;
  4. use think\App;
  5. use think\Db;
  6. class WashRecord extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='washing_record';
  11. $this->model = new WashingRecord();
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $title = input('title','','trim');
  24. if($title!=''){
  25. $map[] = ['title','like','%'.$title.'%'];
  26. }
  27. $start1 = input('start','','trim');
  28. if($start1 != ''){
  29. $map[] = ['create_yyyymm','>=',date('Ymd',strtotime($start1))];
  30. }
  31. $end = input('end','','trim');
  32. if($end != ''){
  33. $map[] = ['create_yyyymm','<=',date('Ymd',strtotime($end))];
  34. }
  35. $mode = input('mode','','trim');
  36. if($mode!=''){
  37. $map[] = ['mode','=',$mode];
  38. }
  39. $dep_id = input('dep_id','','trim');
  40. if($dep_id!=''){
  41. $map[] = ['dep_id','=',$dep_id];
  42. }
  43. $map[] = ['del','=',0];
  44. $map[] = ['org_id','=',$this->orgId];
  45. $map= empty($map) ? true: $map;
  46. //数据查询
  47. $lists = db($this->table)->where($map)
  48. ->limit($start,$length)->order('id desc')->select();
  49. foreach ($lists as $k=>$v){
  50. $lists[$k]['real_name'] = Db::name('user')
  51. ->where('id',$v['user_id'])
  52. ->value('real_name');
  53. $lists[$k]['title'] = Db::name('address')
  54. ->where('id',$v['dep_id'])
  55. ->value('title');
  56. $items = Db::name('washing_record_item')
  57. ->where('washing_record_id',$v['id'])
  58. ->select();
  59. $s = '';
  60. if(!empty($items)){
  61. foreach ($items as $k1=>$v1){
  62. $it = Db::name('washing_items')
  63. ->where('id',$v1['items_id'])
  64. ->value('title');
  65. $s.=$it.':'.$v1['num'].' ';
  66. }
  67. }
  68. $lists[$k]['items'] = $s;
  69. }
  70. //数据返回
  71. $totalCount = db($this->table)->where($map)->count();
  72. $totalPage = ceil($totalCount/$length);
  73. $result['page'] = $page;
  74. $result['total'] = $totalPage;
  75. $result['records'] = $totalCount;
  76. $result['rows'] = $lists;
  77. return json($result);
  78. }else{
  79. $address = (new \app\common\model\Address())->getListByType(9,$this->orgId);
  80. $this->assign('address',$address);
  81. $this->assign('ids',is_login());
  82. return $this->fetch();
  83. }
  84. }
  85. public function returnWash($id) {
  86. if (!$id) {
  87. $this->error('参数错误');
  88. }
  89. $ret = $this->model->returnWash($id, $this->orgId);
  90. if (!$ret) {
  91. $this->error($this->model->getError());
  92. }
  93. // model('ActionLog')->addlog(is_login(),34,'洗涤记录:归还',['id' => $id],cur_org_id());
  94. $this->success('归还成功');
  95. }
  96. public function confirmWash($id) {
  97. if(!request()->isPost()){
  98. if (!$id) {
  99. $this->error('参数错误');
  100. }
  101. $this->assign('id',$id);
  102. return $this->fetch();
  103. }
  104. else {
  105. $sign = input('sign');
  106. $img = input('img');
  107. if (!$sign) {
  108. $this->error('请上传签名图');
  109. }
  110. $ret = $this->model->confirmWash($id, $this->orgId, $sign,$img);
  111. if (!$ret) {
  112. $this->error($this->model->getError());
  113. }
  114. // model('ActionLog')->addlog(is_login(),34,'洗涤记录:确认',['id' => $id],cur_org_id());
  115. $this->success('确认成功');
  116. }
  117. }
  118. /**
  119. * 新增/编辑
  120. */
  121. public function add($id=0){
  122. if(request()->isPost()){
  123. $data = request()->post();
  124. if (empty($data['dep_id'])) {
  125. $this->error('地点不能为空');
  126. }
  127. if (empty($data['items'])) {
  128. $this->error('物品不能为空');
  129. }
  130. $items = json_decode( $data['items'],true);
  131. if (empty($items)) {
  132. $this->error('物品不能为空');
  133. }
  134. foreach ($items as $k => $val) {
  135. if(empty($val['num'])){
  136. $this->error($val['title'] . '数量不能为空');
  137. }
  138. }
  139. foreach ($items as $k => $v) {
  140. $params['items'][$k] = [
  141. 'items_id' => $v['id'],
  142. 'num' => $v['num'],
  143. ];
  144. }
  145. $sqImg = input('sq_img','');
  146. $sqSign = input('sq_sign','');
  147. if(!$sqSign)$this->error('收取签名不能为空');
  148. $params['dep_id'] = $data['dep_id'];
  149. $params['org_id'] = $this->orgId;
  150. $params['user_id'] = $this->userId;
  151. $params['sq_img'] = $sqImg;
  152. $params['sq_sign'] = $sqSign;
  153. $res = $this->model->addSave($params);
  154. if($res){
  155. $this->success('操作成功',url('index'));
  156. }else{
  157. $this->error($this->model->getError());
  158. }
  159. }else{
  160. $address = (new \app\common\model\Address())->getListByType(9,$this->orgId);
  161. $this->assign('dep',$address);
  162. $items = (new \app\common\model\WashingItems())->getList($this->orgId);
  163. $this->assign('items',$items);
  164. return $this->fetch();
  165. }
  166. }
  167. /**
  168. * 删除记录
  169. * @param int $id
  170. */
  171. public function del($id=0){
  172. if(!$id){
  173. $this->error('参数错误');
  174. }
  175. $res = db($this->table)->where('id',$id)->update([
  176. 'del'=>1,
  177. 'del_time'=>getTime(),
  178. ]);
  179. if($res){
  180. // model('ActionLog')->addlog(is_login(),34,'洗涤记录:删除',['id' => $id],cur_org_id());
  181. $this->success('删除成功');
  182. }else{
  183. $this->error('删除失败');
  184. }
  185. }
  186. public function washRecordExport(){
  187. $start = input('start','','trim');
  188. if($start != ''){
  189. $map[] = ['create_yyyymm','>=',date('Ymd',strtotime($start))];
  190. }
  191. $end = input('end','','trim');
  192. if($end != ''){
  193. $map[] = ['create_yyyymm','<=',date('Ymd',strtotime($end))];
  194. }
  195. $mode = input('mode','','trim');
  196. if($mode!=''){
  197. $map[] = ['mode','=',$mode];
  198. }
  199. $dep_id = input('dep_id','','trim');
  200. if($dep_id!=''){
  201. $map[] = ['dep_id','=',$dep_id];
  202. }
  203. $map[] = ['del','=',0];
  204. $map[] = ['org_id','=',$this->orgId];
  205. $map= empty($map) ? true: $map;
  206. //数据查询
  207. $lists = Db::name('washing_record')->where($map)->order('id asc')->select();
  208. foreach ($lists as $k=>$v){
  209. $lists[$k]['real_name'] = Db::name('user')
  210. ->where('id',$v['user_id'])
  211. ->value('real_name');
  212. $lists[$k]['title'] = Db::name('address')
  213. ->where('id',$v['dep_id'])
  214. ->value('title');
  215. $items = Db::name('washing_record_item')
  216. ->where('washing_record_id',$v['id'])
  217. ->select();
  218. $s = '';
  219. if(!empty($items)){
  220. foreach ($items as $k1=>$v1){
  221. $it = Db::name('washing_items')
  222. ->where('id',$v1['items_id'])
  223. ->value('title');
  224. $s.=$it.':'.$v1['num'].' ';
  225. }
  226. }
  227. $lists[$k]['items'] = $s;
  228. }
  229. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  230. //实例化PHPExcel类
  231. $objPHPExcel = new \PHPExcel();
  232. //激活当前的sheet表
  233. $objPHPExcel->setActiveSheetIndex(0);
  234. //设置表格头(即excel表格的第一行)
  235. $objActSheet = $objPHPExcel->setActiveSheetIndex(0)
  236. ->setCellValue('A1', '编号')
  237. ->setCellValue('B1', '操作人')
  238. ->setCellValue('C1', '地点')
  239. ->setCellValue('D1', '物品')
  240. ->setCellValue('E1', '领取时间')
  241. ->setCellValue('F1', '归还时间')
  242. ->setCellValue('G1', '确定时间')
  243. ->setCellValue('H1', '状态')
  244. ->setCellValue('I1', '收取签名')
  245. ->setCellValue('J1', '送达签名');
  246. // 设置表格头水平居中
  247. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  248. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  249. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  250. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  251. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  252. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  253. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  254. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  255. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  256. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  257. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  258. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  259. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  260. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  261. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  262. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  263. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  264. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  265. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  266. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  267. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
  268. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  269. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
  270. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  271. //设置列水平居中
  272. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  273. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  274. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  275. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  276. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  277. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  278. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  279. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  280. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  281. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  282. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  283. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  284. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  285. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  286. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  287. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  288. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  289. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  290. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  291. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  292. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
  293. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  294. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
  295. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  296. //设置单元格宽度
  297. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  298. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  299. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  300. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  301. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  302. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  303. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  304. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  305. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  306. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  307. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(20);
  308. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(20);
  309. //循环刚取出来的数组,将数据逐一添加到excel表格。
  310. for ($i = 0; $i < count($lists); $i++) {
  311. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
  312. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['real_name']);
  313. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['title']);
  314. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['items']);
  315. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['create_time']);
  316. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['return_time']);
  317. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['confirm_time']);
  318. if($lists[$i]['mode'] == 1){
  319. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已收取');
  320. }elseif($lists[$i]['mode'] == 2){
  321. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已归还');
  322. }else{
  323. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已确认');
  324. }
  325. $sqsignList = explode(".",$lists[$i]['sq_sign']);
  326. if($sqsignList){
  327. $ext = end($sqsignList);
  328. $img ='';
  329. if ($ext == 'jpg' || $ext == 'jpeg'){
  330. $img = @imagecreatefromjpeg($lists[$i]['sq_sign']);
  331. }
  332. if ($ext == 'png'){
  333. $img = @imagecreatefrompng($lists[$i]['sq_sign']);
  334. }
  335. if($img){
  336. //实例化图片操作类
  337. $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing();
  338. $objDrawing->setImageResource($img);
  339. $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);//渲染方法
  340. $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
  341. $objDrawing->setHeight(40);//照片高度
  342. $objDrawing->setWidth(40); //照片宽度
  343. $objDrawing->setCoordinates('I'.($i+2));//图片要插入的单元格
  344. $objDrawing->setOffsetX(8);//图片X轴偏移
  345. $objDrawing->setOffsetY(8);//图片Y轴偏移
  346. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  347. //设置每一行高度
  348. }else{
  349. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), '');
  350. }
  351. }else{
  352. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), '');
  353. }
  354. $signList = explode(".",$lists[$i]['sign']);
  355. if($signList){
  356. $ext = end($signList);
  357. $img ='';
  358. if ($ext == 'jpg' || $ext == 'jpeg'){
  359. $img = @imagecreatefromjpeg($lists[$i]['sign']);
  360. }
  361. if ($ext == 'png'){
  362. $img = @imagecreatefrompng($lists[$i]['sign']);
  363. }
  364. if($img){
  365. //实例化图片操作类
  366. $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing();
  367. $objDrawing->setImageResource($img);
  368. $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);//渲染方法
  369. $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
  370. $objDrawing->setHeight(40);//照片高度
  371. $objDrawing->setWidth(40); //照片宽度
  372. $objDrawing->setCoordinates('J'.($i+2));//图片要插入的单元格
  373. $objDrawing->setOffsetX(8);//图片X轴偏移
  374. $objDrawing->setOffsetY(8);//图片Y轴偏移
  375. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  376. //设置每一行高度
  377. }else{
  378. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), '');
  379. }
  380. }else{
  381. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), '');
  382. }
  383. $objActSheet->getRowDimension($i + 2)->setRowHeight(40); // 行高
  384. }
  385. //设置保存的Excel表格名称
  386. $filename = '洗涤记录'.'_' . date('YmdHis', time()) . '.xls';
  387. //设置当前激活的sheet表格名称
  388. $objPHPExcel->getActiveSheet()->setTitle('洗涤记录');
  389. //设置浏览器窗口下载表格
  390. ob_end_clean();
  391. header("Content-Type: application/force-download");
  392. header("Content-Type: application/octet-stream");
  393. header("Content-Type: application/download");
  394. header('Content-Disposition:inline;filename="' . $filename);
  395. //生成excel文件
  396. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  397. //下载文件在浏览器窗口
  398. return $objWriter->save('php://output');
  399. }
  400. }