0
0

PhOrders.php 30 KB

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