PhOrders.php 38 KB

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