PhOrders.php 33 KB

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