PhOrders.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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]['userName'] = Db::name('user')
  397. ->where('id',$v['user_id'])
  398. ->value('real_name');
  399. $lists[$k]['depName'] = '';
  400. $lists[$k]['cateName'] = '';
  401. $lists[$k]['price'] = '';
  402. if($v['cate_id'] > 0){
  403. $cate = Db::name('cate')
  404. ->where('id',$v['cate_id'])
  405. ->field('price,title')
  406. ->find();
  407. $lists[$k]['cateName'] =$cate['title'];
  408. $lists[$k]['price'] =$cate['price'];
  409. }
  410. if($v['dep_id'] > 0){
  411. $lists[$k]['depName'] = Db::name('dep')
  412. ->where('id',$v['dep_id'])
  413. ->value('title');
  414. }
  415. $worker = explode(',',$v['worker_id']);
  416. foreach ($worker as $kk=>$vv){
  417. $worker[$kk] = Db::name('worker')
  418. ->alias('a')
  419. ->join('user b','a.user_id = b.id')
  420. ->where('a.id',$vv)
  421. ->value('b.real_name');
  422. }
  423. $lists[$k]['workerName'] = implode(',',$worker);
  424. $lists[$k]['days'] = $v['status'] == 2 ? $this->getWorkerDay($v['start'],$v['end']) : '';
  425. }
  426. int_to_string($lists,['status' => $this->status]);
  427. //实例化PHPExcel类
  428. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  429. $objPHPExcel = new \PHPExcel();
  430. //激活当前的sheet表
  431. $objPHPExcel->setActiveSheetIndex(0);
  432. //设置表格头(即excel表格的第一行)
  433. $objPHPExcel->setActiveSheetIndex(0)
  434. ->setCellValue('A1', '订单编号')
  435. ->setCellValue('B1', '病人姓名')
  436. ->setCellValue('C1', '床号')
  437. ->setCellValue('D1', '联系人')
  438. ->setCellValue('E1', '联系电话')
  439. ->setCellValue('F1', '科室')
  440. ->setCellValue('G1', '陪护服务')
  441. ->setCellValue('H1', '服务单价')
  442. ->setCellValue('I1', '护工姓名')
  443. ->setCellValue('J1', '开始日期')
  444. ->setCellValue('K1', '结束日期')
  445. ->setCellValue('L1', '服务天数')
  446. ->setCellValue('M1', '订单金额')
  447. ->setCellValue('N1', '服务费')
  448. ->setCellValue('O1', '状态')
  449. ->setCellValue('P1', '下单日期')
  450. ->setCellValue('Q1', '完成日期');
  451. // 设置表格头水平居中
  452. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  453. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  454. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  455. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  456. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  457. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  458. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  459. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  460. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  461. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  462. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  463. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  464. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  465. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  466. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  467. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  468. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  469. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  470. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  471. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  472. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
  473. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  474. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
  475. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  476. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M1')->getAlignment()
  477. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  478. $objPHPExcel->setActiveSheetIndex(0)->getStyle('N1')->getAlignment()
  479. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  480. $objPHPExcel->setActiveSheetIndex(0)->getStyle('O1')->getAlignment()
  481. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  482. $objPHPExcel->setActiveSheetIndex(0)->getStyle('P1')->getAlignment()
  483. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  484. $objPHPExcel->setActiveSheetIndex(0)->getStyle('Q1')->getAlignment()
  485. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  486. //设置列水平居中
  487. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  488. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  489. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  490. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  491. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  492. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  493. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  494. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  495. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  496. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  497. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  498. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  499. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  500. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  501. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  502. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  503. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  504. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  505. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  506. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  507. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
  508. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  509. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
  510. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  511. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()
  512. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  513. $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()
  514. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  515. $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()
  516. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  517. $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()
  518. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  519. $objPHPExcel->setActiveSheetIndex(0)->getStyle('Q')->getAlignment()
  520. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  521. //设置单元格宽度
  522. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  523. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  524. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  525. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  526. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(50);
  527. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
  528. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  529. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  530. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  531. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  532. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(20);
  533. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(20);
  534. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(20);
  535. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('N')->setWidth(20);
  536. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(20);
  537. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('P')->setWidth(20);
  538. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('Q')->setWidth(20);
  539. //循环刚取出来的数组,将数据逐一添加到excel表格。
  540. for ($i = 0; $i < count($lists); $i++) {
  541. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['sn']);
  542. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['name']);
  543. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['bed']);
  544. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['contact']);
  545. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['phone']);
  546. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['depName']);
  547. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['cateName']);
  548. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['price']);
  549. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['workerName']);
  550. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['start']);
  551. $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $lists[$i]['end']);
  552. $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $lists[$i]['days']);
  553. $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $lists[$i]['amount']);
  554. $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $lists[$i]['service_money']);
  555. $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $lists[$i]['status_text']);
  556. $objPHPExcel->getActiveSheet()->setCellValue('P' . ($i + 2), $lists[$i]['create_time']);
  557. $objPHPExcel->getActiveSheet()->setCellValue('Q' . ($i + 2), $lists[$i]['update_time']);
  558. }
  559. //设置保存的Excel表格名称
  560. $filename = '订单列表' . date('YmdHis', time()) . '.xls';
  561. //设置当前激活的sheet表格名称
  562. $objPHPExcel->getActiveSheet()->setTitle('订单列表');
  563. //设置浏览器窗口下载表格
  564. ob_end_clean();
  565. header("Content-Type: application/force-download");
  566. header("Content-Type: application/octet-stream");
  567. header("Content-Type: application/download");
  568. header('Content-Disposition:inline;filename="' . $filename);
  569. //生成excel文件
  570. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  571. //下载文件在浏览器窗口
  572. return $objWriter->save('php://output');
  573. }
  574. }
  575. //陪护总览
  576. public function view(){
  577. $url = 'xxxxxxx';
  578. $downcode = think_encrypt($url);
  579. $url1= 'xxxxxxx';
  580. $this->assign('code',$downcode);
  581. $downcode1 = think_encrypt($url1);
  582. $this->assign('code1',$downcode1);
  583. $time=array();
  584. $currentTime = time();
  585. $cyear = floor(date("Y",$currentTime));
  586. $cMonth = floor(date("m",$currentTime));
  587. for($i=0;$i<12;$i++){
  588. $nMonth = $cMonth-$i;
  589. $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
  590. $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
  591. if(strlen($nMonth)==1){
  592. $nMonth = '0'.$nMonth;
  593. }
  594. $time[]=$cyear.'-'.$nMonth;
  595. }
  596. //订单金额
  597. $m = input('month','');
  598. $type = input('type','');
  599. if(request()->isPost()){
  600. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m',strtotime($m)),1,date('Y',strtotime($m))));
  601. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  602. }else{
  603. $begin=date('Y-m-d H:i:s',mktime(0,0,0,date('m'),1,date('Y')));
  604. $end=date('Y-m-d H:i:s',mktime(23,59,59,date('m',strtotime($begin)),date('t',strtotime($begin)),date('Y',strtotime($begin))));
  605. }
  606. $map[] = ['create_time','>=',$begin];
  607. $map[] = ['create_time','<=',$end];
  608. $map[] = ['org_id','=',$this->orgId];
  609. $total = Db::name('ph_orders')
  610. ->where($map)
  611. ->where('status','<>',3)
  612. ->sum('amount');
  613. $order['total'] =$total?'¥'.number_format($total,2):'¥0.00';
  614. $pre_money = Db::name('ph_orders')
  615. ->where($map)
  616. ->where('status','<>',3)
  617. ->sum('pre_money');
  618. $order['pre_money'] =$pre_money?'¥'.number_format($pre_money,2):'¥0.00';
  619. $pay = Db::name('ph_orders')
  620. ->where($map)
  621. ->where('status','in',[2,4])
  622. ->sum('amount');
  623. $order['pay'] =$pay?'¥'.number_format($pay,2):'¥0.00';
  624. $yjsTotal = Db::name('ph_orders')
  625. ->where($map)
  626. ->where('status','=',4)
  627. ->sum('amount');
  628. $order['yjs'] =$yjsTotal?'¥'.number_format($yjsTotal,2):'¥0.00';
  629. if($type==1){
  630. HelpHander::success($order);
  631. }
  632. //科室订单
  633. $typeList = Db::name('cate')
  634. ->where('enable',1)
  635. ->where('del',0)
  636. ->where('org_id',$this->orgId)
  637. ->select();
  638. $typeName = array_column($typeList,'title');
  639. $depIds = Db::name('ph_orders')
  640. ->where('org_id',$this->orgId)
  641. ->group('dep_id')
  642. ->column('dep_id');
  643. $viewData = [];
  644. if(empty($depIds)){
  645. $depName = [];
  646. }else{
  647. $depList = Db::name('dep')
  648. ->where('id','in',$depIds)
  649. ->select();
  650. $depName = array_column($depList,'title');
  651. foreach ($typeList as $k=>$v){
  652. $a = [
  653. 'name'=>$v['title'],
  654. 'type'=>'line',
  655. 'stack'=>'Total',
  656. ];
  657. $t = [];
  658. foreach ($depList as $k1=>$v1){
  659. $sum = Db::name('ph_orders')
  660. ->where($map)
  661. ->where('cate_id',$v['id'])
  662. ->where('dep_id',$v1['id'])
  663. ->count();
  664. $t[] = $sum?$sum:0;
  665. }
  666. $a['data'] = $t;
  667. $viewData[] = $a;
  668. }
  669. }
  670. $viewDataList = [
  671. 'data1'=>$typeName,
  672. 'data2'=>$depName,
  673. 'data3'=>$viewData,
  674. ];
  675. if($type==2){
  676. HelpHander::success($viewDataList);
  677. }
  678. $count = Db::name('ph_orders')
  679. ->where('org_id',$this->orgId)
  680. ->count();
  681. $dfp['count1'] = Db::name('ph_orders')
  682. ->where('org_id',$this->orgId)
  683. ->where('status','=',0)
  684. ->count();
  685. $dfp['count2'] = $count-$dfp['count1'];
  686. if($count >0 && $dfp['count1']>0){
  687. $dfp['bl'] =round($dfp['count1']/$count*100,2).'%';
  688. }else{
  689. $dfp['bl'] = '0%';
  690. }
  691. $jxz['count1'] = Db::name('ph_orders')
  692. ->where('org_id',$this->orgId)
  693. ->where('status','=',1)
  694. ->count();
  695. $jxz['count2'] = $count-$jxz['count1'];
  696. if($count >0 && $jxz['count1']>0){
  697. $jxz['bl'] =round($jxz['count1']/$count*100,2).'%';
  698. }else{
  699. $jxz['bl'] = '0%';
  700. }
  701. $ywc['count1'] = Db::name('ph_orders')
  702. ->where('org_id',$this->orgId)
  703. ->where('status','=',2)
  704. ->count();
  705. $ywc['count2'] = $count-$ywc['count1'];
  706. if($count >0 && $ywc['count1']>0){
  707. $ywc['bl'] =round($ywc['count1']/$count*100,2).'%';
  708. }else{
  709. $ywc['bl'] = '0%';
  710. }
  711. $yzf['count1'] = Db::name('ph_orders')
  712. ->where('org_id',$this->orgId)
  713. ->where('status','=',3)
  714. ->count();
  715. $yzf['count2'] = $count-$yzf['count1'];
  716. if($count >0 && $yzf['count1']>0){
  717. $yzf['bl'] =round($yzf['count1']/$count*100,2).'%';
  718. }else{
  719. $yzf['bl'] = '0%';
  720. }
  721. $yjs['count1'] = Db::name('ph_orders')
  722. ->where('org_id',$this->orgId)
  723. ->where('status',4)
  724. ->count();
  725. $yjs['count2'] = $count-$yjs['count1'];
  726. if($count >0 && $yjs['count1']>0){
  727. $yjs['bl'] =round($yjs['count1']/$count*100,2).'%';
  728. }else{
  729. $yjs['bl'] = '0%';
  730. }
  731. $standardTime = date('Y-m-1');
  732. $_lastMonthStart = date('Y-m-1 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  733. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  734. $lastOrder = Db::name('ph_orders')
  735. ->where('org_id',$this->orgId)
  736. ->where('create_time','>=',$_lastMonthStart)
  737. ->where('create_time','<=',$_lastMonthEnd)
  738. ->count();
  739. $curOrder = Db::name('ph_orders')
  740. ->where('org_id',$this->orgId)
  741. ->where('create_time','>=',$begin)
  742. ->where('create_time','<=',$end)
  743. ->count();
  744. if($count >0 && $lastOrder >0){
  745. $lastBl = round($lastOrder/$count*100,2).'%';
  746. }else{
  747. $lastBl = '0%';
  748. }
  749. if($count >0 && $curOrder >0){
  750. $curBl = round($curOrder/$count*100,2).'%';
  751. }else{
  752. $curBl = '0%';
  753. }
  754. $bl = 0;
  755. if($lastOrder==0 && $curOrder==0){
  756. $bl = 0;
  757. }
  758. if($lastOrder==0 && $curOrder>0){
  759. $bl = 100;
  760. }
  761. if($lastOrder>0){
  762. $bl = round(($curOrder-$lastOrder)/$lastOrder*100,2);
  763. }
  764. $phCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId);
  765. $zgIds = Db::name('ph_todo')
  766. ->where('status',1)
  767. ->group('worker_id')
  768. ->column('worker_id');
  769. if(empty($zgIds)){
  770. $zgCount = 0;
  771. }else{
  772. $p[] = ['a.id','in',$zgIds];
  773. $zgCount = (new \app\common\model\Worker())->getAllByOrgCount($this->orgId,$p);
  774. }
  775. $kxCount = abs($phCount-$zgCount);
  776. $userCount = [
  777. 'count1'=>$phCount,
  778. 'count2'=>$zgCount,
  779. 'count3'=>$kxCount,
  780. ];
  781. $workerList =(new \app\common\model\Worker())->getAllByOrg($this->orgId);
  782. foreach ($workerList as $k=>$v){
  783. $ywcTotal = Db::name('ph_todo')
  784. ->where('worker_id',$v['id'])
  785. ->where('org_id',$this->orgId)
  786. ->where('status',2)
  787. ->count();
  788. $workerList[$k]['ywc'] = $ywcTotal?$ywcTotal:0;
  789. $workerList[$k]['day'] = Db::name('ph_todo')
  790. ->where('worker_id',$v['id'])
  791. ->where('org_id',$this->orgId)
  792. ->where('status',2)
  793. ->sum('day');
  794. }
  795. $this->assign('workerList',$workerList);
  796. $this->assign('userCount',$userCount);
  797. $this->assign('viewDataList',$viewDataList);
  798. $this->assign('lastOrder',$lastOrder);
  799. $this->assign('curOrder',$curOrder);
  800. $this->assign('bl',$bl);
  801. $this->assign('curBl',$curBl);
  802. $this->assign('lastBl',$lastBl);
  803. $this->assign('dfp',$dfp);
  804. $this->assign('jxz',$jxz);
  805. $this->assign('ywc',$ywc);
  806. $this->assign('yzf',$yzf);
  807. $this->assign('yjs',$yjs);
  808. $this->assign('orderData',$order);
  809. $this->assign('month',$time);
  810. $this->assign('curMonth',date('Y-m'));
  811. return $this->fetch();
  812. }
  813. public function calculateMoney(){
  814. $orderId= input('orderId');
  815. $end = input('end');
  816. if(empty($end)){
  817. $this->error('参数错误');
  818. }
  819. $info = Db::name('ph_orders')
  820. ->where('id',$orderId)->find();
  821. $price = Db::name('cate')->where('id',$info['cate_id'])->value('price');
  822. // $day = model('PhOrders')->getWorkerDay($info['start'],$end);
  823. $time = strtotime($end) - strtotime($info['start']);
  824. $day = round($time/(60*60*24),1);
  825. $sfMoney = round($price * $day,2);
  826. $bjMoney =$tkMoney = 0.00;
  827. if($sfMoney > $info['pre_money']){
  828. $bjMoney = $sfMoney - $info['pre_money'];
  829. }else{
  830. $tkMoney = $info['pre_money'] - $sfMoney;
  831. }
  832. $data = [
  833. 'sfMoney' =>$sfMoney,
  834. 'bjMoney' =>$bjMoney,
  835. 'tkMoney' =>$tkMoney,
  836. ];
  837. $this->success('操作成功','',$data);
  838. }
  839. /**
  840. * 计算工期(不满一天按0.5天计算)
  841. * @param string $startDate 开始时间(格式:Y-m-d H:i:s)
  842. * @param string $endDate 结束时间(格式:Y-m-d H:i:s)
  843. * @return float 计算后的天数
  844. */
  845. function getWorkerDay($startDate, $endDate)
  846. {
  847. // 将日期字符串转换为时间戳
  848. $startTimestamp = strtotime($startDate);
  849. $endTimestamp = strtotime($endDate);
  850. // 计算时间差(秒)
  851. $diffSeconds = $endTimestamp - $startTimestamp;
  852. // 计算完整的天数
  853. $fullDays = intval($diffSeconds / (24 * 3600)); // 完整的天数
  854. $remainingSeconds = $diffSeconds % (24 * 3600); // 剩余的秒数
  855. // 如果剩余时间大于0,则按0.5天计算
  856. $partialDay = ($remainingSeconds > 0) ? 0.5 : 0;
  857. $totalDays = $fullDays + $partialDay;
  858. return $totalDays;
  859. }
  860. }