PhOrders.php 34 KB

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