PhOrders.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?php
  2. namespace app\admin\controller;
  3. use app\hander\HelpHander;
  4. use PhpOffice\PhpWord\Shared\Html;
  5. use think\Db;
  6. use PhpOffice\PhpWord\PhpWord;
  7. use PhpOffice\PhpWord\IOFactory;
  8. class PhOrders extends Auth
  9. {
  10. protected $status = [
  11. 0=>'待分配',
  12. 1=>'进行中',
  13. 2=>'已完成',
  14. 3=>'已作废',
  15. 4=>'已结算'
  16. ];
  17. public function index(){
  18. if(request()->isAjax()){
  19. //分页参数
  20. $length = input('rows',10,'intval'); //每页条数
  21. $page = input('page',1,'intval'); //第几页
  22. $start = ($page - 1) * $length; //分页开始位置
  23. //排序
  24. $sortRow = input('sidx','u.id','trim'); //排序列
  25. $sort = input('sord','desc','trim'); //排序方式
  26. $order = 'a.id desc';
  27. $title = input('title','','trim');//用户
  28. if($title){
  29. $user = Db::name('user')
  30. ->alias('u')
  31. ->join('user_org uo','uo.user_id = u.id')
  32. ->where('u.real_name','like','%'.$title.'%')
  33. ->where('uo.org_id',$this->orgId)
  34. ->column('u.id');
  35. $orderId = Db::name('ph_todo')
  36. ->alias('p')
  37. ->join('worker w','w.id=p.worker_id')
  38. ->where('w.user_id','in',$user)
  39. ->column('p.order_id');
  40. if(!empty($orderId)){
  41. $map[] = ['a.id','in',$orderId];
  42. }else{
  43. $map[] = ['a.id','=',0];
  44. }
  45. }
  46. $sn = input('sn','','trim');
  47. if($sn){
  48. $map[] = ['a.sn','=',$sn];
  49. }
  50. $name = input('name','','trim');
  51. if($name){
  52. $map[] = ['a.name','like','%'.$name.'%'];
  53. }
  54. $cateId = input('cateId','','trim');
  55. if($cateId){
  56. $map[] = ['a.cate_id','=',$cateId];
  57. }
  58. $depId = input('depId','','trim');
  59. if($depId){
  60. $map[] = ['a.dep_id','=',$depId];
  61. }
  62. $status = input('status','','trim');
  63. if($status != ''){
  64. $map[] = ['a.status','=',$status];
  65. }
  66. $b = input('begin','','trim');
  67. $e = input('end','','trim');
  68. if($b){
  69. $map[] = ['a.create_time','>=',$b];
  70. }
  71. if($e){
  72. $map[] = ['a.create_time','<=',$e];
  73. }
  74. $map[] = ['a.org_id','=',$this->orgId];
  75. $map[] = ['a.is_service','=',1];
  76. $map= empty($map) ? true: $map;
  77. //数据查询
  78. $lists = Db::name('ph_orders')
  79. ->alias('a')
  80. ->where($map)
  81. ->field('a.*')
  82. ->limit($start,$length)
  83. ->order($order)
  84. ->select();
  85. foreach ($lists as $k=>$v){
  86. $lists[$k]['userName'] = Db::name('user')
  87. ->where('id',$v['user_id'])
  88. ->value('real_name');
  89. $lists[$k]['depName'] = '';
  90. $lists[$k]['cateName'] = '';
  91. $lists[$k]['workerName'] = '';
  92. if($v['cate_id'] > 0){
  93. $lists[$k]['cateName'] = Db::name('cate')
  94. ->where('id',$v['cate_id'])
  95. ->value('title');
  96. }
  97. if($v['dep_id'] > 0){
  98. $lists[$k]['depName'] = Db::name('dep')
  99. ->where('id',$v['dep_id'])
  100. ->value('title');
  101. }
  102. $workerId =
  103. $v['status'] == 3 ?
  104. Db::name('ph_todo')->where('order_id',$v['id'])->column('worker_id')
  105. :
  106. Db::name('ph_todo')->where('order_id',$v['id'])->whereIn('status',[1,2])->column('worker_id');
  107. if (!empty($workerId)) {
  108. foreach ($workerId as $k2=>$v2){
  109. $userId = Db::name('worker')->where('id',$v2)->value('user_id');
  110. $workerId[$k2]= Db::name('user')->where('id',$userId)->value('real_name');
  111. }
  112. $lists[$k]['workerName'] = implode(',',$workerId);
  113. }
  114. $lists[$k]['is_zf'] = 0;
  115. $off = model('Config')->getConfig('ph_order_cancel_time',cur_org_id());
  116. if($off){
  117. $start = strtotime($v['create_time']) + $off*60*60;
  118. $end = strtotime(date('Y-m-d H:i:s'));
  119. if($start < $end){
  120. $lists[$k]['is_zf'] = 1;
  121. }
  122. }
  123. if ($v['status'] == 2){
  124. $lists[$k]['days'] = $this->getWorkerDay($v['start'],$v['end']);
  125. }
  126. }
  127. int_to_string($lists,['status' => $this->status]);
  128. //数据返回
  129. $totalCount = Db::name('ph_orders')->alias('a')
  130. ->where($map)->count();
  131. $totalPage = ceil($totalCount/$length);
  132. $totalMoney = Db::name('ph_orders')->alias('a')
  133. ->where($map)->sum('a.amount');
  134. $totalService = Db::name('ph_orders')->alias('a')
  135. ->where($map)->sum('a.service_money');
  136. $result['totalMoney'] = $totalMoney+$totalService;
  137. $result['page'] = $page;
  138. $result['total'] = $totalPage;
  139. $result['records'] = $totalCount;
  140. $result['rows'] = $lists;
  141. return json($result);
  142. }else{
  143. $cate =(new \app\common\model\Cate())->getAllByOrg($this->orgId);
  144. $this->assign('cate',$cate);
  145. $dep =(new \app\common\model\Dep())->getList($this->orgId);
  146. $this->assign('dep',$dep);
  147. $this->assign('status',$this->status);
  148. $this->assign('cur_status',input('status'));
  149. return $this->fetch();
  150. }
  151. }
  152. /**
  153. * 新增
  154. */
  155. public function add(){
  156. $model = new \app\common\model\PhOrders();
  157. if(request()->isPost()){
  158. $res = $model->addSave($this->userId,$this->orgId);
  159. if($res){
  160. $this->success('操作成功',url('index'));
  161. }else{
  162. $this->error($model->getError());
  163. }
  164. }else{
  165. $cate =(new \app\common\model\Cate())->getAllByOrg($this->orgId);
  166. foreach ($cate as $k => $v){
  167. $cate[$k]['deps'] = $v['deps'] ? explode(',',$v['deps']):[];
  168. }
  169. $this->assign('cate',$cate);
  170. $dep =(new \app\common\model\Dep())->getList($this->orgId);
  171. $this->assign('dep',$dep);
  172. $workers =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  173. $this->assign('workers',$workers);
  174. $this->assign('meta_title','创建订单');
  175. return $this->fetch();
  176. }
  177. }
  178. /**
  179. * 新增
  180. */
  181. public function edit($id = 0){
  182. $model = new \app\common\model\PhOrders();
  183. if(request()->isPost()){
  184. $res = $model->editSave($this->userId,$this->orgId);
  185. if($res){
  186. $this->success('操作成功',url('index'));
  187. }else{
  188. $this->error($model->getError());
  189. }
  190. }else{
  191. $info = Db::name('ph_orders')
  192. ->where('id',$id)->find();
  193. $this->assign('info',$info);
  194. $cate =(new \app\common\model\Cate())->getAllByOrg($this->orgId);
  195. $this->assign('cate',$cate);
  196. $dep =(new \app\common\model\Dep())->getList($this->orgId);
  197. $this->assign('dep',$dep);
  198. $this->assign('meta_title','编辑订单');
  199. return $this->fetch();
  200. }
  201. }
  202. public function editDept($id = 0){
  203. if(request()->isPost()){
  204. $res = Db::name('ph_orders')->where('id',$id)->update(['dep_id'=>input('dep_id',0)]);
  205. if($res){
  206. $this->success('操作成功',url('index'));
  207. }else{
  208. $this->error('操作失败');
  209. }
  210. }else{
  211. $depId= Db::name('ph_orders')
  212. ->where('id',$id)
  213. ->value('dep_id');
  214. $this->assign('depId',$depId);
  215. $dep =(new \app\common\model\Dep())->getList($this->orgId);
  216. $this->assign('deps',$dep);
  217. $this->assign('id',$id);
  218. return $this->fetch();
  219. }
  220. }
  221. public function detail($id=0){
  222. $model = new \app\common\model\PhOrders();
  223. $info = $model->getInfo($id);
  224. if(!$info){
  225. $this->error('订单不存在');
  226. }
  227. $serviceMoney = model("Config")->getConfig("web_service_money",$this->orgId);
  228. $serviceMoney = floatval($serviceMoney) > 0? floatval($serviceMoney) : 0;
  229. $this->assign('service_money',$serviceMoney);
  230. $this->assign('info',$info);
  231. return $this->fetch();
  232. }
  233. /**
  234. * 派单
  235. */
  236. public function send($id = 0){
  237. $model = new \app\common\model\PhOrders();
  238. if(request()->isPost()){
  239. $res = $model->sendSave($this->userId,$this->orgId);
  240. if($res){
  241. $this->success('操作成功',url('index'));
  242. }else{
  243. $this->error($model->getError());
  244. }
  245. }else{
  246. $this->assign('id',$id);
  247. $workers =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  248. $this->assign('workers',$workers);
  249. return $this->fetch();
  250. }
  251. }
  252. /**
  253. * 预收金
  254. */
  255. public function payOrder($id = 0,$busType=0){
  256. if(request()->isPost()){
  257. $remark = input('remark','','trim');
  258. $money = input('money/f',0);
  259. $busType = input('busType/d',0);
  260. if($money <= 0){
  261. HelpHander::error('输入金额错误');
  262. }
  263. $res = model('PhOrderPay')->addSaveDispatch($this->orgId,$this->userId,$id,$money,$remark,$busType);
  264. if($res){
  265. $this->success('操作成功',url('index'));
  266. }else{
  267. $this->error(model('PhOrderPay')->getError());
  268. }
  269. }else{
  270. $serviceMoney = model("Config")->getConfig("web_service_money",$this->orgId);
  271. $serviceMoney = floatval($serviceMoney) > 0? floatval($serviceMoney) : 0;
  272. $this->assign('service_money',$serviceMoney);
  273. $this->assign('id',$id);
  274. $this->assign('busType',$busType);
  275. return $this->fetch();
  276. }
  277. }
  278. /**
  279. * 退款
  280. */
  281. public function refund($id = 0){
  282. if(request()->isPost()){
  283. $res = model('PhOrderPay')->refundOrder($this->userId,$this->orgId);
  284. if($res){
  285. $this->success('操作成功',url('index'));
  286. }else{
  287. $this->error(model('PhOrderPay')->getError());
  288. }
  289. }else{
  290. $pay = Db::name('ph_order_pay')->where('id',$id)->find();
  291. $money = round($pay['money'] - $pay['money2'],2);
  292. $this->assign('money',$money);
  293. $this->assign('pay',$pay);
  294. $this->assign('id',$id);
  295. return $this->fetch();
  296. }
  297. }
  298. /**
  299. * 完成订单
  300. */
  301. public function finish($id = 0){
  302. $model = new \app\common\model\PhOrders();
  303. if(request()->isPost()){
  304. $res = $model->finishSave($this->userId,$this->orgId);
  305. if($res){
  306. $this->success('操作成功',url('index'));
  307. }else{
  308. $this->error($model->getError());
  309. }
  310. }else{
  311. $info = Db::name('ph_orders')
  312. ->where('id',$id)->find();
  313. $info['end'] = $info['end']?$info['end']:date('Y-m-d H:i:s');
  314. $price = Db::name('cate')->where('id',$info['cate_id'])->value('price');
  315. $time = strtotime($info['end']) - strtotime($info['start']);
  316. $day = round($time/(60*60*24),1);
  317. $sfMoney = round($price * $day,2);
  318. $info['amount'] = $info['amount']>0?$info['amount']:$sfMoney;
  319. $bjMoney = $tkMoney = 0.00;
  320. if($sfMoney > $info['pre_money']){
  321. $bjMoney = $sfMoney - $info['pre_money'];
  322. }else{
  323. $tkMoney = $info['pre_money'] - $sfMoney;
  324. }
  325. $this->assign('bjMoney',$bjMoney);
  326. $this->assign('tkMoney',$tkMoney);
  327. $this->assign('info',$info);
  328. return $this->fetch();
  329. }
  330. }
  331. // 编辑
  332. public function editTodo($id = 0){
  333. $model = new \app\common\model\PhOrders();
  334. if(request()->isPost()){
  335. $res = $model->edit_todo($this->userId,$this->orgId);
  336. if($res){
  337. $this->success('操作成功',url('index'));
  338. }else{
  339. $this->error($model->getError());
  340. }
  341. }else{
  342. $info = Db::name('ph_todo')->where('id',$id)->find();
  343. $this->assign('info',$info);
  344. return $this->fetch();
  345. }
  346. }
  347. /**
  348. * 作废订单
  349. */
  350. public function cancel($id=0){
  351. if(request()->isPost()){
  352. $note = input('cancel_reason','','trim');
  353. $id = input('id/d',0);
  354. $model = new \app\common\model\PhOrders();
  355. $ret = $model->cancelOrder($id,$note,$this->userId);
  356. if(!$ret){
  357. $this->error($model->getError());
  358. }else{
  359. $this->success('操作成功');
  360. }
  361. }else{
  362. $this->assign('id',$id);
  363. return $this->fetch();
  364. }
  365. }
  366. //excel导出
  367. public function export(){
  368. if(request()->isGet()){
  369. $order = 'a.id desc';
  370. $title = input('title','','trim');//用户
  371. if($title){
  372. $user = Db::name('user')
  373. ->alias('u')
  374. ->join('user_org uo','uo.user_id = u.id')
  375. ->where('u.real_name','like','%'.$title.'%')
  376. ->where('uo.org_id',$this->orgId)
  377. ->column('u.id');
  378. $orderId = Db::name('ph_todo')
  379. ->alias('p')
  380. ->join('worker w','w.id=p.worker_id')
  381. ->where('w.user_id','in',$user)
  382. ->column('p.order_id');
  383. if(!empty($orderId)){
  384. $map[] = ['a.id','in',$orderId];
  385. }else{
  386. $map[] = ['a.id','=',0];
  387. }
  388. }
  389. $sn = input('sn','','trim');
  390. if($sn){
  391. $map[] = ['a.sn','=',$sn];
  392. }
  393. $cateId = input('cateId','','trim');
  394. if($cateId){
  395. $map[] = ['a.cate_id','=',$cateId];
  396. }
  397. $depId = input('depId','','trim');
  398. if($depId){
  399. $map[] = ['a.dep_id','=',$depId];
  400. }
  401. $status = input('status','','trim');
  402. if($status != ''){
  403. $map[] = ['a.status','=',$status];
  404. }
  405. $b = input('begin','','trim');
  406. $e = input('end','','trim');
  407. if($b){
  408. $map[] = ['a.create_time','>=',$b];
  409. }
  410. if($e){
  411. $map[] = ['a.create_time','<=',$e];
  412. }
  413. $map[] = ['a.org_id','=',$this->orgId];
  414. $map[] = ['a.is_service','=',1];
  415. $map= empty($map) ? true: $map;
  416. //数据查询
  417. $lists = Db::name('ph_orders')->alias('a')
  418. ->where($map)
  419. ->field('a.*')
  420. ->distinct(true)
  421. ->order($order)
  422. ->select();
  423. foreach ($lists as $k=>$v){
  424. $lists[$k]['name'] =preg_replace('/[^\x{4E00}-\x{9FFF}]/u', '', $v['name']);
  425. $lists[$k]['userName'] = Db::name('user')
  426. ->where('id',$v['user_id'])
  427. ->value('real_name');
  428. $lists[$k]['depName'] = '';
  429. $lists[$k]['cateName'] = '';
  430. $lists[$k]['price'] = '';
  431. $lists[$k]['workerName'] = '';
  432. $workerId = Db::name('ph_todo')
  433. ->where('order_id',$v['id'])
  434. ->whereIn('status',[1,2])
  435. ->column('worker_id');
  436. if (!empty($workerId)) {
  437. foreach ($workerId as $k2=>$v2){
  438. $userId = Db::name('worker')->where('id',$v2)->value('user_id');
  439. $workerId[$k2]= Db::name('user')->where('id',$userId)->value('real_name');
  440. }
  441. $lists[$k]['workerName'] = implode(',',$workerId);
  442. }
  443. if($v['cate_id'] > 0){
  444. $cate = Db::name('cate')
  445. ->where('id',$v['cate_id'])
  446. ->field('price,title')
  447. ->find();
  448. $lists[$k]['cateName'] =$cate['title'];
  449. $lists[$k]['price'] =$cate['price'];
  450. }
  451. if($v['dep_id'] > 0){
  452. $lists[$k]['depName'] = Db::name('dep')
  453. ->where('id',$v['dep_id'])
  454. ->value('title');
  455. }
  456. $lists[$k]['days'] = $v['status'] == 2 ? $this->getWorkerDay($v['start'],$v['end']) : '';
  457. }
  458. int_to_string($lists,['status' => $this->status]);
  459. //实例化PHPExcel类
  460. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  461. $objPHPExcel = new \PHPExcel();
  462. //激活当前的sheet表
  463. $objPHPExcel->setActiveSheetIndex(0);
  464. //设置表格头(即excel表格的第一行)
  465. $objPHPExcel->setActiveSheetIndex(0)
  466. ->setCellValue('A1', '订单编号')
  467. ->setCellValue('B1', '病人姓名')
  468. ->setCellValue('C1', '床号')
  469. ->setCellValue('D1', '联系人')
  470. ->setCellValue('E1', '联系电话')
  471. ->setCellValue('F1', '科室')
  472. ->setCellValue('G1', '陪护服务')
  473. ->setCellValue('H1', '服务单价')
  474. ->setCellValue('I1', '护工姓名')
  475. ->setCellValue('J1', '开始日期')
  476. ->setCellValue('K1', '结束日期')
  477. ->setCellValue('L1', '服务天数')
  478. ->setCellValue('M1', '订单金额')
  479. ->setCellValue('N1', '服务费')
  480. ->setCellValue('O1', '状态')
  481. ->setCellValue('P1', '下单日期')
  482. ->setCellValue('Q1', '完成日期');
  483. $arrs = ['A','B','C','D','E','F','G','H','I','J','H','I','J','K','L','M','N','O','P','Q'];
  484. foreach ($arrs as $arr){
  485. // 设置表格头水平居中
  486. $objPHPExcel->setActiveSheetIndex(0)->getStyle($arr.'1')->getAlignment()
  487. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  488. //设置列水平居中
  489. $objPHPExcel->setActiveSheetIndex(0)->getStyle($arr)->getAlignment()
  490. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  491. //设置单元格宽度
  492. if ($arr == 'E'){
  493. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($arr)->setWidth(50);
  494. }else{
  495. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($arr)->setWidth(20);
  496. }
  497. }
  498. //循环刚取出来的数组,将数据逐一添加到excel表格。
  499. for ($i = 0; $i < count($lists); $i++) {
  500. $objPHPExcel->getActiveSheet()->setCellValueExplicit('A' . ($i + 2), $lists[$i]['sn']);
  501. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['name']);
  502. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['bed']);
  503. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['contact']);
  504. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['phone']);
  505. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['depName']);
  506. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['cateName']);
  507. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['price']);
  508. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['workerName']);
  509. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['start']);
  510. $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $lists[$i]['end']);
  511. $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $lists[$i]['days']);
  512. $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $lists[$i]['amount']);
  513. $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $lists[$i]['service_money']);
  514. $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $lists[$i]['status_text']);
  515. $objPHPExcel->getActiveSheet()->setCellValue('P' . ($i + 2), $lists[$i]['create_time']);
  516. $objPHPExcel->getActiveSheet()->setCellValue('Q' . ($i + 2), $lists[$i]['update_time']);
  517. }
  518. //设置保存的Excel表格名称
  519. $filename = '订单列表' . date('YmdHis', time()) . '.xlsx';
  520. //设置当前激活的sheet表格名称
  521. $objPHPExcel->getActiveSheet()->setTitle('订单列表');
  522. //设置浏览器窗口下载表格
  523. ob_end_clean();
  524. header("Content-Type: application/force-download");
  525. header("Content-Type: application/octet-stream");
  526. header("Content-Type: application/download");
  527. header('Content-Disposition:inline;filename="' . $filename);
  528. //生成excel文件
  529. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  530. //下载文件在浏览器窗口
  531. return $objWriter->save('php://output');
  532. }
  533. }
  534. //陪护总览
  535. public function view(){
  536. $url = 'xxxxxxx';
  537. $downcode = think_encrypt($url);
  538. $url1= 'xxxxxxx';
  539. $this->assign('code',$downcode);
  540. $downcode1 = think_encrypt($url1);
  541. $this->assign('code1',$downcode1);
  542. $time=array();
  543. $currentTime = time();
  544. $cyear = floor(date("Y",$currentTime));
  545. $cMonth = floor(date("m",$currentTime));
  546. for($i=0;$i<12;$i++){
  547. $nMonth = $cMonth-$i;
  548. $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
  549. $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
  550. if(strlen($nMonth)==1){
  551. $nMonth = '0'.$nMonth;
  552. }
  553. $time[]=$cyear.'-'.$nMonth;
  554. }
  555. //订单金额
  556. $m = input('month','');
  557. $type = input('type','');
  558. if(request()->isPost()){
  559. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m',strtotime($m)),1,date('Y',strtotime($m))));
  560. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  561. }else{
  562. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m'),1,date('Y')));
  563. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  564. }
  565. $map[] = ['create_time','>=',$begin];
  566. $map[] = ['create_time','<=',$end];
  567. $map[] = ['org_id','=',$this->orgId];
  568. $total = Db::name('ph_orders')
  569. ->where($map)
  570. ->where('status','<>',3)
  571. ->sum('amount');
  572. $order['total'] =$total?'¥'.number_format($total,2):'¥0.00';
  573. $pre_money = Db::name('ph_orders')
  574. ->where($map)
  575. ->where('status','<>',3)
  576. ->sum('pre_money');
  577. $order['pre_money'] =$pre_money?'¥'.number_format($pre_money,2):'¥0.00';
  578. $pay = Db::name('ph_orders')
  579. ->where($map)
  580. ->where('status','in',[2,4])
  581. ->sum('amount');
  582. $order['pay'] =$pay?'¥'.number_format($pay,2):'¥0.00';
  583. $yjsTotal = Db::name('ph_orders')
  584. ->where($map)
  585. ->where('status','=',4)
  586. ->sum('amount');
  587. $order['yjs'] =$yjsTotal?'¥'.number_format($yjsTotal,2):'¥0.00';
  588. if($type==1){
  589. HelpHander::success($order);
  590. }
  591. //科室订单
  592. $typeList = Db::name('cate')
  593. ->where('enable',1)
  594. ->where('del',0)
  595. ->where('org_id',$this->orgId)
  596. ->select();
  597. $typeName = array_column($typeList,'title');
  598. $depIds = Db::name('ph_orders')
  599. ->where('org_id',$this->orgId)
  600. ->group('dep_id')
  601. ->column('dep_id');
  602. $viewData = [];
  603. if(empty($depIds)){
  604. $depName = [];
  605. }else{
  606. $depList = Db::name('dep')
  607. ->where('id','in',$depIds)
  608. ->select();
  609. $depName = array_column($depList,'title');
  610. foreach ($typeList as $k=>$v){
  611. $a = [
  612. 'name'=>$v['title'],
  613. 'type'=>'line',
  614. 'stack'=>'Total',
  615. ];
  616. $t = [];
  617. foreach ($depList as $k1=>$v1){
  618. $sum = Db::name('ph_orders')
  619. ->where($map)
  620. ->where('cate_id',$v['id'])
  621. ->where('dep_id',$v1['id'])
  622. ->count();
  623. $t[] = $sum?$sum:0;
  624. }
  625. $a['data'] = $t;
  626. $viewData[] = $a;
  627. }
  628. }
  629. $viewDataList = [
  630. 'data1'=>$typeName,
  631. 'data2'=>$depName,
  632. 'data3'=>$viewData,
  633. ];
  634. if($type==2){
  635. HelpHander::success($viewDataList);
  636. }
  637. $count = Db::name('ph_orders')
  638. ->where('org_id',$this->orgId)
  639. ->count();
  640. $dfp['count1'] = Db::name('ph_orders')
  641. ->where('org_id',$this->orgId)
  642. ->where('status','=',0)
  643. ->count();
  644. $dfp['count2'] = $count-$dfp['count1'];
  645. if($count >0 && $dfp['count1']>0){
  646. $dfp['bl'] =round($dfp['count1']/$count*100,2).'%';
  647. }else{
  648. $dfp['bl'] = '0%';
  649. }
  650. $jxz['count1'] = Db::name('ph_orders')
  651. ->where('org_id',$this->orgId)
  652. ->where('status','=',1)
  653. ->count();
  654. $jxz['count2'] = $count-$jxz['count1'];
  655. if($count >0 && $jxz['count1']>0){
  656. $jxz['bl'] =round($jxz['count1']/$count*100,2).'%';
  657. }else{
  658. $jxz['bl'] = '0%';
  659. }
  660. $ywc['count1'] = Db::name('ph_orders')
  661. ->where('org_id',$this->orgId)
  662. ->where('status','=',2)
  663. ->count();
  664. $ywc['count2'] = $count-$ywc['count1'];
  665. if($count >0 && $ywc['count1']>0){
  666. $ywc['bl'] =round($ywc['count1']/$count*100,2).'%';
  667. }else{
  668. $ywc['bl'] = '0%';
  669. }
  670. $yzf['count1'] = Db::name('ph_orders')
  671. ->where('org_id',$this->orgId)
  672. ->where('status','=',3)
  673. ->count();
  674. $yzf['count2'] = $count-$yzf['count1'];
  675. if($count >0 && $yzf['count1']>0){
  676. $yzf['bl'] =round($yzf['count1']/$count*100,2).'%';
  677. }else{
  678. $yzf['bl'] = '0%';
  679. }
  680. $yjs['count1'] = Db::name('ph_orders')
  681. ->where('org_id',$this->orgId)
  682. ->where('status',4)
  683. ->count();
  684. $yjs['count2'] = $count-$yjs['count1'];
  685. if($count >0 && $yjs['count1']>0){
  686. $yjs['bl'] =round($yjs['count1']/$count*100,2).'%';
  687. }else{
  688. $yjs['bl'] = '0%';
  689. }
  690. $standardTime = date('Y-m-1');
  691. $_lastMonthStart = date('Y-m-1 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  692. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  693. $lastOrder = Db::name('ph_orders')
  694. ->where('org_id',$this->orgId)
  695. ->where('create_time','>=',$_lastMonthStart)
  696. ->where('create_time','<=',$_lastMonthEnd)
  697. ->count();
  698. $curOrder = Db::name('ph_orders')
  699. ->where('org_id',$this->orgId)
  700. ->where('create_time','>=',$begin)
  701. ->where('create_time','<=',$end)
  702. ->count();
  703. if($count >0 && $lastOrder >0){
  704. $lastBl = round($lastOrder/$count*100,2).'%';
  705. }else{
  706. $lastBl = '0%';
  707. }
  708. if($count >0 && $curOrder >0){
  709. $curBl = round($curOrder/$count*100,2).'%';
  710. }else{
  711. $curBl = '0%';
  712. }
  713. $bl = 0;
  714. if($lastOrder==0 && $curOrder==0){
  715. $bl = 0;
  716. }
  717. if($lastOrder==0 && $curOrder>0){
  718. $bl = 100;
  719. }
  720. if($lastOrder>0){
  721. $bl = round(($curOrder-$lastOrder)/$lastOrder*100,2);
  722. }
  723. $phCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId);
  724. $zgIds = Db::name('ph_todo')
  725. ->where('status',1)
  726. ->group('worker_id')
  727. ->column('worker_id');
  728. if(empty($zgIds)){
  729. $zgCount = 0;
  730. }else{
  731. $p[] = ['a.id','in',$zgIds];
  732. $zgCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId,$p);
  733. }
  734. $kxCount = abs($phCount-$zgCount);
  735. $userCount = [
  736. 'count1'=>$phCount,
  737. 'count2'=>$zgCount,
  738. 'count3'=>$kxCount,
  739. ];
  740. $workerList =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  741. foreach ($workerList as $k=>$v){
  742. $ywcTotal = Db::name('ph_todo')
  743. ->where('worker_id',$v['id'])
  744. ->where('org_id',$this->orgId)
  745. ->where('status',2)
  746. ->count();
  747. $workerList[$k]['ywc'] = $ywcTotal?$ywcTotal:0;
  748. $workerList[$k]['day'] = Db::name('ph_todo')
  749. ->where('worker_id',$v['id'])
  750. ->where('org_id',$this->orgId)
  751. ->where('status',2)
  752. ->sum('day');
  753. }
  754. $this->assign('workerList',$workerList);
  755. $this->assign('userCount',$userCount);
  756. $this->assign('viewDataList',$viewDataList);
  757. $this->assign('lastOrder',$lastOrder);
  758. $this->assign('curOrder',$curOrder);
  759. $this->assign('bl',$bl);
  760. $this->assign('curBl',$curBl);
  761. $this->assign('lastBl',$lastBl);
  762. $this->assign('dfp',$dfp);
  763. $this->assign('jxz',$jxz);
  764. $this->assign('ywc',$ywc);
  765. $this->assign('yzf',$yzf);
  766. $this->assign('yjs',$yjs);
  767. $this->assign('orderData',$order);
  768. $this->assign('month',$time);
  769. $this->assign('curMonth',date('Y-m'));
  770. return $this->fetch();
  771. }
  772. public function calculateMoney(){
  773. $orderId= input('orderId');
  774. $end = input('end');
  775. if(empty($end)){
  776. $this->error('参数错误');
  777. }
  778. $info = Db::name('ph_orders')
  779. ->where('id',$orderId)->find();
  780. $price = Db::name('cate')->where('id',$info['cate_id'])->value('price');
  781. // $day = model('PhOrders')->getWorkerDay($info['start'],$end);
  782. $time = strtotime($end) - strtotime($info['start']);
  783. $day = round($time/(60*60*24),1);
  784. $sfMoney = round($price * $day,2);
  785. $bjMoney =$tkMoney = 0.00;
  786. if($sfMoney > $info['pre_money']){
  787. $bjMoney = $sfMoney - $info['pre_money'];
  788. }else{
  789. $tkMoney = $info['pre_money'] - $sfMoney;
  790. }
  791. $data = [
  792. 'sfMoney' =>$sfMoney,
  793. 'bjMoney' =>$bjMoney,
  794. 'tkMoney' =>$tkMoney,
  795. ];
  796. $this->success('操作成功','',$data);
  797. }
  798. /**
  799. * 计算工期(不满一天按0.5天计算)
  800. * @param string $startDate 开始时间(格式:Y-m-d H:i:s)
  801. * @param string $endDate 结束时间(格式:Y-m-d H:i:s)
  802. * @return float 计算后的天数
  803. */
  804. function getWorkerDay($startDate, $endDate)
  805. {
  806. // 将日期字符串转换为时间戳
  807. $startTimestamp = strtotime($startDate);
  808. $endTimestamp = strtotime($endDate);
  809. // 计算时间差(秒)
  810. $diffSeconds = $endTimestamp - $startTimestamp;
  811. // 计算完整的天数
  812. $fullDays = intval($diffSeconds / (24 * 3600)); // 完整的天数
  813. $remainingSeconds = $diffSeconds % (24 * 3600); // 剩余的秒数
  814. // 如果剩余时间大于0,则按0.5天计算
  815. $partialDay = ($remainingSeconds > 0) ? 0.5 : 0;
  816. $totalDays = $fullDays + $partialDay;
  817. return $totalDays;
  818. }
  819. public function exportWord()
  820. {
  821. $id = input('id',0);
  822. $orders = Db::name('ph_orders')
  823. ->where('id',$id)
  824. ->field('sign,sn,name,gender,age,contact,phone')
  825. ->find();
  826. $todos = Db::name('ph_todo')
  827. ->where('order_id',$id)
  828. ->whereIn('status',[1,2])
  829. ->field('sign,worker_id')
  830. ->select();
  831. $protocol = Db::name("ph_protocol")->where('org_id',$this->orgId)->value('content');
  832. $phpWord = new PhpWord();
  833. $section = $phpWord->addSection();
  834. Html::addHtml($section, $protocol);
  835. $style = ['name' => '宋体', 'size' => 10.5];
  836. $gender = $orders['gender'] == 1 ? '男' : '女';
  837. $section->addText('甲方姓名:'.$orders['name'].' 性别:'.$gender.' 年龄:'.$orders['age'].' 联系人:'.$orders['contact'].' 电话:'.$orders['phone'],$style);
  838. foreach ($todos as $todo){
  839. $worker = Db::name('worker')
  840. ->alias('a')
  841. ->join('user b','a.user_id = b.id')
  842. ->where('a.id',$todo['worker_id'])
  843. ->field('a.gender,a.card,b.real_name,b.mobile')
  844. ->find();
  845. $gender = $worker['gender'] == 1 ? '男' : '女';
  846. $age = $this->getAgeByIdCard($worker['card']);
  847. $section->addText('乙方姓名:'.$worker['real_name'].' 性别:'.$gender.' 年龄:'.$age.' 电话:'.$worker['mobile'],$style);
  848. }
  849. $section->addText('丙方:'.getCompany($this->orgId),$style);
  850. $section->addTextBreak(1);
  851. $section->addText('客户签名:', ['name' => '宋体', 'size' => 26, 'bold' => true]);
  852. if ($orders['sign']){
  853. $section->addImage($orders['sign'], ['width' => 120, 'height' => 110]);
  854. }
  855. $section->addText('护工签名:', ['name' => '宋体', 'size' => 26, 'bold' => true]);
  856. $table = $section->addTable();
  857. $length = count($todos) > 4 ? ceil(count($todos) / 4) : 1;
  858. for ($i = 0; $i < $length; $i++) {
  859. $table->addRow(1, 4); // 添加1行4列
  860. $imgs = array_slice($todos, $i * 4, 4);
  861. foreach ($imgs as $v2) {
  862. if ($v2['sign'] != '') {
  863. $cell = $table->addCell(1000); // 单元格宽度,单位为EMU
  864. $cell->addImage($v2['sign'], [
  865. 'width' => 120,
  866. 'height' => 110
  867. ]);
  868. }
  869. }
  870. }
  871. $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
  872. $fileName = '陪护协议-'.$orders['sn'].'.docx';
  873. header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  874. header('Content-Disposition: attachment;filename="'.$fileName);
  875. header('Cache-Control: max-age=0');
  876. $objWriter->save('php://output');
  877. exit;
  878. }
  879. public function getAgeByIdCard($idCard) {
  880. if (strlen($idCard) != 18) {
  881. return '身份证号错误';
  882. }
  883. // 提取出生年月日
  884. $birthDate = substr($idCard, 6, 8);
  885. $birthYear = substr($birthDate, 0, 4);
  886. $birthMonth = substr($birthDate, 4, 2);
  887. $birthDay = substr($birthDate, 6, 2);
  888. // 创建日期对象
  889. $birthday = new \DateTime("$birthYear-$birthMonth-$birthDay");
  890. $today = new \DateTime();
  891. // 计算年龄
  892. $age = $today->diff($birthday)->y;
  893. return $age;
  894. }
  895. }