PhOrders.php 34 KB

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