PhOrders.php 33 KB

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