PhOrders.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 detail($id=0){
  203. $model = new \app\common\model\PhOrders();
  204. $info = $model->getInfo($id);
  205. if(!$info){
  206. $this->error('订单不存在');
  207. }
  208. $serviceMoney = model("Config")->getConfig("web_service_money",$this->orgId);
  209. $serviceMoney = floatval($serviceMoney) > 0? floatval($serviceMoney) : 0;
  210. $this->assign('service_money',$serviceMoney);
  211. $this->assign('info',$info);
  212. return $this->fetch();
  213. }
  214. /**
  215. * 派单
  216. */
  217. public function send($id = 0){
  218. $model = new \app\common\model\PhOrders();
  219. if(request()->isPost()){
  220. $res = $model->sendSave($this->userId,$this->orgId);
  221. if($res){
  222. $this->success('操作成功',url('index'));
  223. }else{
  224. $this->error($model->getError());
  225. }
  226. }else{
  227. $this->assign('id',$id);
  228. $workers =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  229. $this->assign('workers',$workers);
  230. return $this->fetch();
  231. }
  232. }
  233. /**
  234. * 预收金
  235. */
  236. public function payOrder($id = 0,$busType=0){
  237. if(request()->isPost()){
  238. $remark = input('remark','','trim');
  239. $money = input('money/f',0);
  240. $busType = input('busType/d',0);
  241. if($money <= 0){
  242. HelpHander::error('输入金额错误');
  243. }
  244. $res = model('PhOrderPay')->addSaveDispatch($this->orgId,$this->userId,$id,$money,$remark,$busType);
  245. if($res){
  246. $this->success('操作成功',url('index'));
  247. }else{
  248. $this->error(model('PhOrderPay')->getError());
  249. }
  250. }else{
  251. $serviceMoney = model("Config")->getConfig("web_service_money",$this->orgId);
  252. $serviceMoney = floatval($serviceMoney) > 0? floatval($serviceMoney) : 0;
  253. $this->assign('service_money',$serviceMoney);
  254. $this->assign('id',$id);
  255. $this->assign('busType',$busType);
  256. return $this->fetch();
  257. }
  258. }
  259. /**
  260. * 退款
  261. */
  262. public function refund($id = 0){
  263. if(request()->isPost()){
  264. $res = model('PhOrderPay')->refundOrder($this->userId,$this->orgId);
  265. if($res){
  266. $this->success('操作成功',url('index'));
  267. }else{
  268. $this->error(model('PhOrderPay')->getError());
  269. }
  270. }else{
  271. $pay = Db::name('ph_order_pay')->where('id',$id)->find();
  272. $money = round($pay['money'] - $pay['money2'],2);
  273. $this->assign('money',$money);
  274. $this->assign('pay',$pay);
  275. $this->assign('id',$id);
  276. return $this->fetch();
  277. }
  278. }
  279. /**
  280. * 完成订单
  281. */
  282. public function finish($id = 0){
  283. $model = new \app\common\model\PhOrders();
  284. if(request()->isPost()){
  285. $res = $model->finishSave($this->userId,$this->orgId);
  286. if($res){
  287. $this->success('操作成功',url('index'));
  288. }else{
  289. $this->error($model->getError());
  290. }
  291. }else{
  292. $info = Db::name('ph_orders')
  293. ->where('id',$id)->find();
  294. $info['end'] = $info['end']?$info['end']:date('Y-m-d H:i:s');
  295. $price = Db::name('cate')->where('id',$info['cate_id'])->value('price');
  296. $time = strtotime($info['end']) - strtotime($info['start']);
  297. $day = round($time/(60*60*24),1);
  298. $sfMoney = round($price * $day,2);
  299. $info['amount'] = $info['amount']>0?$info['amount']:$sfMoney;
  300. $bjMoney = $tkMoney = 0.00;
  301. if($sfMoney > $info['pre_money']){
  302. $bjMoney = $sfMoney - $info['pre_money'];
  303. }else{
  304. $tkMoney = $info['pre_money'] - $sfMoney;
  305. }
  306. $this->assign('bjMoney',$bjMoney);
  307. $this->assign('tkMoney',$tkMoney);
  308. $this->assign('info',$info);
  309. return $this->fetch();
  310. }
  311. }
  312. // 编辑
  313. public function editTodo($id = 0){
  314. $model = new \app\common\model\PhOrders();
  315. if(request()->isPost()){
  316. $res = $model->edit_todo($this->userId,$this->orgId);
  317. if($res){
  318. $this->success('操作成功',url('index'));
  319. }else{
  320. $this->error($model->getError());
  321. }
  322. }else{
  323. $info = Db::name('ph_todo')->where('id',$id)->find();
  324. $this->assign('info',$info);
  325. return $this->fetch();
  326. }
  327. }
  328. /**
  329. * 作废订单
  330. */
  331. public function cancel($id=0){
  332. if(request()->isPost()){
  333. $note = input('cancel_reason','','trim');
  334. $id = input('id/d',0);
  335. $model = new \app\common\model\PhOrders();
  336. $ret = $model->cancelOrder($id,$note,$this->userId);
  337. if(!$ret){
  338. $this->error($model->getError());
  339. }else{
  340. $this->success('操作成功');
  341. }
  342. }else{
  343. $this->assign('id',$id);
  344. return $this->fetch();
  345. }
  346. }
  347. //excel导出
  348. public function export(){
  349. if(request()->isGet()){
  350. $order = 'a.id desc';
  351. $title = input('title','','trim');//用户
  352. if($title){
  353. $user = Db::name('user')
  354. ->alias('u')
  355. ->join('user_org uo','uo.user_id = u.id')
  356. ->where('u.real_name','like','%'.$title.'%')
  357. ->where('uo.org_id',$this->orgId)
  358. ->column('u.id');
  359. $orderId = Db::name('ph_todo')
  360. ->alias('p')
  361. ->join('worker w','w.id=p.worker_id')
  362. ->where('w.user_id','in',$user)
  363. ->column('p.order_id');
  364. if(!empty($orderId)){
  365. $map[] = ['a.id','in',$orderId];
  366. }else{
  367. $map[] = ['a.id','=',0];
  368. }
  369. }
  370. $sn = input('sn','','trim');
  371. if($sn){
  372. $map[] = ['a.sn','=',$sn];
  373. }
  374. $cateId = input('cateId','','trim');
  375. if($cateId){
  376. $map[] = ['a.cate_id','=',$cateId];
  377. }
  378. $depId = input('depId','','trim');
  379. if($depId){
  380. $map[] = ['a.dep_id','=',$depId];
  381. }
  382. $status = input('status','','trim');
  383. if($status != ''){
  384. $map[] = ['a.status','=',$status];
  385. }
  386. $b = input('begin','','trim');
  387. $e = input('end','','trim');
  388. if($b){
  389. $map[] = ['a.create_time','>=',$b];
  390. }
  391. if($e){
  392. $map[] = ['a.create_time','<=',$e];
  393. }
  394. $map[] = ['a.org_id','=',$this->orgId];
  395. $map[] = ['a.is_service','=',1];
  396. $map= empty($map) ? true: $map;
  397. //数据查询
  398. $lists = Db::name('ph_orders')->alias('a')
  399. ->where($map)
  400. ->field('a.*')
  401. ->distinct(true)
  402. ->order($order)
  403. ->select();
  404. foreach ($lists as $k=>$v){
  405. $lists[$k]['name'] =preg_replace('/[^\x{4E00}-\x{9FFF}]/u', '', $v['name']);
  406. $lists[$k]['userName'] = Db::name('user')
  407. ->where('id',$v['user_id'])
  408. ->value('real_name');
  409. $lists[$k]['depName'] = '';
  410. $lists[$k]['cateName'] = '';
  411. $lists[$k]['price'] = '';
  412. $lists[$k]['workerName'] = '';
  413. $workerId = Db::name('ph_todo')
  414. ->where('order_id',$v['id'])
  415. ->whereIn('status',[1,2])
  416. ->column('worker_id');
  417. if (!empty($workerId)) {
  418. foreach ($workerId as $k2=>$v2){
  419. $userId = Db::name('worker')->where('id',$v2)->value('user_id');
  420. $workerId[$k2]= Db::name('user')->where('id',$userId)->value('real_name');
  421. }
  422. $lists[$k]['workerName'] = implode(',',$workerId);
  423. }
  424. if($v['cate_id'] > 0){
  425. $cate = Db::name('cate')
  426. ->where('id',$v['cate_id'])
  427. ->field('price,title')
  428. ->find();
  429. $lists[$k]['cateName'] =$cate['title'];
  430. $lists[$k]['price'] =$cate['price'];
  431. }
  432. if($v['dep_id'] > 0){
  433. $lists[$k]['depName'] = Db::name('dep')
  434. ->where('id',$v['dep_id'])
  435. ->value('title');
  436. }
  437. $lists[$k]['days'] = $v['status'] == 2 ? $this->getWorkerDay($v['start'],$v['end']) : '';
  438. }
  439. int_to_string($lists,['status' => $this->status]);
  440. //实例化PHPExcel类
  441. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  442. $objPHPExcel = new \PHPExcel();
  443. //激活当前的sheet表
  444. $objPHPExcel->setActiveSheetIndex(0);
  445. //设置表格头(即excel表格的第一行)
  446. $objPHPExcel->setActiveSheetIndex(0)
  447. ->setCellValue('A1', '订单编号')
  448. ->setCellValue('B1', '病人姓名')
  449. ->setCellValue('C1', '床号')
  450. ->setCellValue('D1', '联系人')
  451. ->setCellValue('E1', '联系电话')
  452. ->setCellValue('F1', '科室')
  453. ->setCellValue('G1', '陪护服务')
  454. ->setCellValue('H1', '服务单价')
  455. ->setCellValue('I1', '护工姓名')
  456. ->setCellValue('J1', '开始日期')
  457. ->setCellValue('K1', '结束日期')
  458. ->setCellValue('L1', '服务天数')
  459. ->setCellValue('M1', '订单金额')
  460. ->setCellValue('N1', '服务费')
  461. ->setCellValue('O1', '状态')
  462. ->setCellValue('P1', '下单日期')
  463. ->setCellValue('Q1', '完成日期');
  464. $arrs = ['A','B','C','D','E','F','G','H','I','J','H','I','J','K','L','M','N','O','P','Q'];
  465. foreach ($arrs as $arr){
  466. // 设置表格头水平居中
  467. $objPHPExcel->setActiveSheetIndex(0)->getStyle($arr.'1')->getAlignment()
  468. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  469. //设置列水平居中
  470. $objPHPExcel->setActiveSheetIndex(0)->getStyle($arr)->getAlignment()
  471. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  472. //设置单元格宽度
  473. if ($arr == 'E'){
  474. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($arr)->setWidth(50);
  475. }else{
  476. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($arr)->setWidth(20);
  477. }
  478. }
  479. //循环刚取出来的数组,将数据逐一添加到excel表格。
  480. for ($i = 0; $i < count($lists); $i++) {
  481. $objPHPExcel->getActiveSheet()->setCellValueExplicit('A' . ($i + 2), $lists[$i]['sn']);
  482. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['name']);
  483. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['bed']);
  484. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['contact']);
  485. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['phone']);
  486. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['depName']);
  487. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['cateName']);
  488. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['price']);
  489. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['workerName']);
  490. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['start']);
  491. $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $lists[$i]['end']);
  492. $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $lists[$i]['days']);
  493. $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $lists[$i]['amount']);
  494. $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $lists[$i]['service_money']);
  495. $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $lists[$i]['status_text']);
  496. $objPHPExcel->getActiveSheet()->setCellValue('P' . ($i + 2), $lists[$i]['create_time']);
  497. $objPHPExcel->getActiveSheet()->setCellValue('Q' . ($i + 2), $lists[$i]['update_time']);
  498. }
  499. //设置保存的Excel表格名称
  500. $filename = '订单列表' . date('YmdHis', time()) . '.xlsx';
  501. //设置当前激活的sheet表格名称
  502. $objPHPExcel->getActiveSheet()->setTitle('订单列表');
  503. //设置浏览器窗口下载表格
  504. ob_end_clean();
  505. header("Content-Type: application/force-download");
  506. header("Content-Type: application/octet-stream");
  507. header("Content-Type: application/download");
  508. header('Content-Disposition:inline;filename="' . $filename);
  509. //生成excel文件
  510. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  511. //下载文件在浏览器窗口
  512. return $objWriter->save('php://output');
  513. }
  514. }
  515. //陪护总览
  516. public function view(){
  517. $url = 'xxxxxxx';
  518. $downcode = think_encrypt($url);
  519. $url1= 'xxxxxxx';
  520. $this->assign('code',$downcode);
  521. $downcode1 = think_encrypt($url1);
  522. $this->assign('code1',$downcode1);
  523. $time=array();
  524. $currentTime = time();
  525. $cyear = floor(date("Y",$currentTime));
  526. $cMonth = floor(date("m",$currentTime));
  527. for($i=0;$i<12;$i++){
  528. $nMonth = $cMonth-$i;
  529. $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
  530. $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
  531. if(strlen($nMonth)==1){
  532. $nMonth = '0'.$nMonth;
  533. }
  534. $time[]=$cyear.'-'.$nMonth;
  535. }
  536. //订单金额
  537. $m = input('month','');
  538. $type = input('type','');
  539. if(request()->isPost()){
  540. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m',strtotime($m)),1,date('Y',strtotime($m))));
  541. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  542. }else{
  543. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m'),1,date('Y')));
  544. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  545. }
  546. $map[] = ['create_time','>=',$begin];
  547. $map[] = ['create_time','<=',$end];
  548. $map[] = ['org_id','=',$this->orgId];
  549. $total = Db::name('ph_orders')
  550. ->where($map)
  551. ->where('status','<>',3)
  552. ->sum('amount');
  553. $order['total'] =$total?'¥'.number_format($total,2):'¥0.00';
  554. $pre_money = Db::name('ph_orders')
  555. ->where($map)
  556. ->where('status','<>',3)
  557. ->sum('pre_money');
  558. $order['pre_money'] =$pre_money?'¥'.number_format($pre_money,2):'¥0.00';
  559. $pay = Db::name('ph_orders')
  560. ->where($map)
  561. ->where('status','in',[2,4])
  562. ->sum('amount');
  563. $order['pay'] =$pay?'¥'.number_format($pay,2):'¥0.00';
  564. $yjsTotal = Db::name('ph_orders')
  565. ->where($map)
  566. ->where('status','=',4)
  567. ->sum('amount');
  568. $order['yjs'] =$yjsTotal?'¥'.number_format($yjsTotal,2):'¥0.00';
  569. if($type==1){
  570. HelpHander::success($order);
  571. }
  572. //科室订单
  573. $typeList = Db::name('cate')
  574. ->where('enable',1)
  575. ->where('del',0)
  576. ->where('org_id',$this->orgId)
  577. ->select();
  578. $typeName = array_column($typeList,'title');
  579. $depIds = Db::name('ph_orders')
  580. ->where('org_id',$this->orgId)
  581. ->group('dep_id')
  582. ->column('dep_id');
  583. $viewData = [];
  584. if(empty($depIds)){
  585. $depName = [];
  586. }else{
  587. $depList = Db::name('dep')
  588. ->where('id','in',$depIds)
  589. ->select();
  590. $depName = array_column($depList,'title');
  591. foreach ($typeList as $k=>$v){
  592. $a = [
  593. 'name'=>$v['title'],
  594. 'type'=>'line',
  595. 'stack'=>'Total',
  596. ];
  597. $t = [];
  598. foreach ($depList as $k1=>$v1){
  599. $sum = Db::name('ph_orders')
  600. ->where($map)
  601. ->where('cate_id',$v['id'])
  602. ->where('dep_id',$v1['id'])
  603. ->count();
  604. $t[] = $sum?$sum:0;
  605. }
  606. $a['data'] = $t;
  607. $viewData[] = $a;
  608. }
  609. }
  610. $viewDataList = [
  611. 'data1'=>$typeName,
  612. 'data2'=>$depName,
  613. 'data3'=>$viewData,
  614. ];
  615. if($type==2){
  616. HelpHander::success($viewDataList);
  617. }
  618. $count = Db::name('ph_orders')
  619. ->where('org_id',$this->orgId)
  620. ->count();
  621. $dfp['count1'] = Db::name('ph_orders')
  622. ->where('org_id',$this->orgId)
  623. ->where('status','=',0)
  624. ->count();
  625. $dfp['count2'] = $count-$dfp['count1'];
  626. if($count >0 && $dfp['count1']>0){
  627. $dfp['bl'] =round($dfp['count1']/$count*100,2).'%';
  628. }else{
  629. $dfp['bl'] = '0%';
  630. }
  631. $jxz['count1'] = Db::name('ph_orders')
  632. ->where('org_id',$this->orgId)
  633. ->where('status','=',1)
  634. ->count();
  635. $jxz['count2'] = $count-$jxz['count1'];
  636. if($count >0 && $jxz['count1']>0){
  637. $jxz['bl'] =round($jxz['count1']/$count*100,2).'%';
  638. }else{
  639. $jxz['bl'] = '0%';
  640. }
  641. $ywc['count1'] = Db::name('ph_orders')
  642. ->where('org_id',$this->orgId)
  643. ->where('status','=',2)
  644. ->count();
  645. $ywc['count2'] = $count-$ywc['count1'];
  646. if($count >0 && $ywc['count1']>0){
  647. $ywc['bl'] =round($ywc['count1']/$count*100,2).'%';
  648. }else{
  649. $ywc['bl'] = '0%';
  650. }
  651. $yzf['count1'] = Db::name('ph_orders')
  652. ->where('org_id',$this->orgId)
  653. ->where('status','=',3)
  654. ->count();
  655. $yzf['count2'] = $count-$yzf['count1'];
  656. if($count >0 && $yzf['count1']>0){
  657. $yzf['bl'] =round($yzf['count1']/$count*100,2).'%';
  658. }else{
  659. $yzf['bl'] = '0%';
  660. }
  661. $yjs['count1'] = Db::name('ph_orders')
  662. ->where('org_id',$this->orgId)
  663. ->where('status',4)
  664. ->count();
  665. $yjs['count2'] = $count-$yjs['count1'];
  666. if($count >0 && $yjs['count1']>0){
  667. $yjs['bl'] =round($yjs['count1']/$count*100,2).'%';
  668. }else{
  669. $yjs['bl'] = '0%';
  670. }
  671. $standardTime = date('Y-m-1');
  672. $_lastMonthStart = date('Y-m-1 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  673. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  674. $lastOrder = Db::name('ph_orders')
  675. ->where('org_id',$this->orgId)
  676. ->where('create_time','>=',$_lastMonthStart)
  677. ->where('create_time','<=',$_lastMonthEnd)
  678. ->count();
  679. $curOrder = Db::name('ph_orders')
  680. ->where('org_id',$this->orgId)
  681. ->where('create_time','>=',$begin)
  682. ->where('create_time','<=',$end)
  683. ->count();
  684. if($count >0 && $lastOrder >0){
  685. $lastBl = round($lastOrder/$count*100,2).'%';
  686. }else{
  687. $lastBl = '0%';
  688. }
  689. if($count >0 && $curOrder >0){
  690. $curBl = round($curOrder/$count*100,2).'%';
  691. }else{
  692. $curBl = '0%';
  693. }
  694. $bl = 0;
  695. if($lastOrder==0 && $curOrder==0){
  696. $bl = 0;
  697. }
  698. if($lastOrder==0 && $curOrder>0){
  699. $bl = 100;
  700. }
  701. if($lastOrder>0){
  702. $bl = round(($curOrder-$lastOrder)/$lastOrder*100,2);
  703. }
  704. $phCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId);
  705. $zgIds = Db::name('ph_todo')
  706. ->where('status',1)
  707. ->group('worker_id')
  708. ->column('worker_id');
  709. if(empty($zgIds)){
  710. $zgCount = 0;
  711. }else{
  712. $p[] = ['a.id','in',$zgIds];
  713. $zgCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId,$p);
  714. }
  715. $kxCount = abs($phCount-$zgCount);
  716. $userCount = [
  717. 'count1'=>$phCount,
  718. 'count2'=>$zgCount,
  719. 'count3'=>$kxCount,
  720. ];
  721. $workerList =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  722. foreach ($workerList as $k=>$v){
  723. $ywcTotal = Db::name('ph_todo')
  724. ->where('worker_id',$v['id'])
  725. ->where('org_id',$this->orgId)
  726. ->where('status',2)
  727. ->count();
  728. $workerList[$k]['ywc'] = $ywcTotal?$ywcTotal:0;
  729. $workerList[$k]['day'] = Db::name('ph_todo')
  730. ->where('worker_id',$v['id'])
  731. ->where('org_id',$this->orgId)
  732. ->where('status',2)
  733. ->sum('day');
  734. }
  735. $this->assign('workerList',$workerList);
  736. $this->assign('userCount',$userCount);
  737. $this->assign('viewDataList',$viewDataList);
  738. $this->assign('lastOrder',$lastOrder);
  739. $this->assign('curOrder',$curOrder);
  740. $this->assign('bl',$bl);
  741. $this->assign('curBl',$curBl);
  742. $this->assign('lastBl',$lastBl);
  743. $this->assign('dfp',$dfp);
  744. $this->assign('jxz',$jxz);
  745. $this->assign('ywc',$ywc);
  746. $this->assign('yzf',$yzf);
  747. $this->assign('yjs',$yjs);
  748. $this->assign('orderData',$order);
  749. $this->assign('month',$time);
  750. $this->assign('curMonth',date('Y-m'));
  751. return $this->fetch();
  752. }
  753. public function calculateMoney(){
  754. $orderId= input('orderId');
  755. $end = input('end');
  756. if(empty($end)){
  757. $this->error('参数错误');
  758. }
  759. $info = Db::name('ph_orders')
  760. ->where('id',$orderId)->find();
  761. $price = Db::name('cate')->where('id',$info['cate_id'])->value('price');
  762. // $day = model('PhOrders')->getWorkerDay($info['start'],$end);
  763. $time = strtotime($end) - strtotime($info['start']);
  764. $day = round($time/(60*60*24),1);
  765. $sfMoney = round($price * $day,2);
  766. $bjMoney =$tkMoney = 0.00;
  767. if($sfMoney > $info['pre_money']){
  768. $bjMoney = $sfMoney - $info['pre_money'];
  769. }else{
  770. $tkMoney = $info['pre_money'] - $sfMoney;
  771. }
  772. $data = [
  773. 'sfMoney' =>$sfMoney,
  774. 'bjMoney' =>$bjMoney,
  775. 'tkMoney' =>$tkMoney,
  776. ];
  777. $this->success('操作成功','',$data);
  778. }
  779. /**
  780. * 计算工期(不满一天按0.5天计算)
  781. * @param string $startDate 开始时间(格式:Y-m-d H:i:s)
  782. * @param string $endDate 结束时间(格式:Y-m-d H:i:s)
  783. * @return float 计算后的天数
  784. */
  785. function getWorkerDay($startDate, $endDate)
  786. {
  787. // 将日期字符串转换为时间戳
  788. $startTimestamp = strtotime($startDate);
  789. $endTimestamp = strtotime($endDate);
  790. // 计算时间差(秒)
  791. $diffSeconds = $endTimestamp - $startTimestamp;
  792. // 计算完整的天数
  793. $fullDays = intval($diffSeconds / (24 * 3600)); // 完整的天数
  794. $remainingSeconds = $diffSeconds % (24 * 3600); // 剩余的秒数
  795. // 如果剩余时间大于0,则按0.5天计算
  796. $partialDay = ($remainingSeconds > 0) ? 0.5 : 0;
  797. $totalDays = $fullDays + $partialDay;
  798. return $totalDays;
  799. }
  800. public function exportWord()
  801. {
  802. $id = input('id',0);
  803. $orders = Db::name('ph_orders')
  804. ->where('id',$id)
  805. ->field('sign,sn')
  806. ->find();
  807. $todos = Db::name('ph_todo')
  808. ->where('order_id',$id)
  809. ->whereIn('status',[1,2])
  810. ->column('sign');
  811. $protocol = Db::name("ph_protocol")->where('org_id',$this->orgId)->value('content');
  812. $phpWord = new PhpWord();
  813. $section = $phpWord->addSection();
  814. Html::addHtml($section, $protocol);
  815. $section->addText('客户签名:', ['name' => 'Arial', 'size' => 26, 'bold' => true]);
  816. if ($orders['sign']){
  817. $section->addImage($orders['sign'], ['width' => 120, 'height' => 110]);
  818. }
  819. $section->addText('护工签名:', ['name' => 'Arial', 'size' => 26, 'bold' => true]);
  820. $table = $section->addTable();
  821. $length = count($todos) > 4 ? ceil(count($todos) / 4) : 1;
  822. for ($i = 0; $i < $length; $i++) {
  823. $table->addRow(1, 4); // 添加1行4列
  824. $imgs = array_slice($todos, $i * 4, 4);
  825. foreach ($imgs as $index => $v2) {
  826. if ($v2 != '') {
  827. $cell = $table->addCell(1000); // 单元格宽度,单位为EMU
  828. $cell->addImage($v2, [
  829. 'width' => 120,
  830. 'height' => 110
  831. ]);
  832. }
  833. }
  834. }
  835. $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
  836. $fileName = '陪护协议-'.$orders['sn'].'.docx';
  837. header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  838. header('Content-Disposition: attachment;filename="'.$fileName);
  839. header('Cache-Control: max-age=0');
  840. $objWriter->save('php://output');
  841. exit;
  842. }
  843. }