PhOrders.php 33 KB

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