PhOrders.php 31 KB

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