PhOrders.php 37 KB

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