DinnerOrder.php 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class DinnerOrder extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='dinner_order';
  11. $this->model= new \app\common\model\DinnerOrder();
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $offset = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $title = input('title','','trim');
  24. if($title){
  25. $map[] = ['a.name|a.sn','like','%'.$title.'%'];
  26. }
  27. $is_ok = input('is_ok',0,'trim');
  28. if($is_ok>=0){
  29. $map[] = ['a.is_ok','=',$is_ok];
  30. }
  31. $is_spot = input('is_spot/d',0);
  32. if($is_spot == 2){
  33. $map[] = ['a.cate','=',1];
  34. $start = input('start',date('Y-m-d'),'trim');
  35. $oids = Db::name('dinner_order_item')
  36. ->where('day',$start)
  37. ->group('order_id')
  38. ->distinct(true)
  39. ->column('order_id');
  40. if($oids){
  41. $map[] = ['a.id','in',$oids];
  42. }else{
  43. $map[] = ['a.id','=',-1];
  44. }
  45. }else{
  46. $map[] = ['a.is_spot','=',$is_spot];
  47. $map[] = ['a.cate','=',0];
  48. $start = input('start',date('Y-m-d'),'trim');
  49. $map[] = ['a.ymd','=',date('Ymd',strtotime($start))];
  50. }
  51. $dinner_type_id = input('type','','trim');
  52. if($dinner_type_id != ''){
  53. $dinner_order_item = Db::name('dinner_order_item')
  54. ->where('dinner_type_id',$dinner_type_id)
  55. ->where('is_refuse',0)
  56. ->column('order_id');
  57. if(empty($dinner_order_item)){
  58. $map[] = ['a.id','=',-1];
  59. }else{
  60. $map[] = ['a.id','in',$dinner_order_item];
  61. }
  62. }
  63. $map[] = ['a.org_id','=',$this->orgId];
  64. $map[] = ['a.state','<>',0];
  65. $map= empty($map) ? true: $map;
  66. //数据查询
  67. $lists = Db::name($this->table)
  68. ->alias('a')
  69. ->field('a.*')
  70. ->where($map)->limit($offset,$length)->order(
  71. ['a.id'=>'desc']
  72. )->select();
  73. foreach ($lists as $k=>$v){
  74. $dmap = [];
  75. $dmap[] = ['a.order_id','=',$v['id']];
  76. $dmap[] = ['a.is_refuse','=',0];
  77. if($dinner_type_id > 0){
  78. $dmap[] = ['a.dinner_type_id','=',$dinner_type_id];
  79. }
  80. if($is_spot == 2){
  81. $dmap[] = ['a.day','=',$start];
  82. }
  83. $all = Db::name('dinner_order_item')
  84. ->alias('a')
  85. ->join('dinner b','a.dinner_id=b.id')
  86. ->join('dinner_type c','a.dinner_type_id=c.id')
  87. ->field('a.*,b.money as oldmoney,b.name as dinner_name,c.color,c.name as dinner_type_name')
  88. ->where($dmap)
  89. ->select();
  90. $typeIdS = $all?array_unique(array_column($all,'dinner_type_id')):[];
  91. $typeName = '';
  92. foreach ($typeIdS as $k1=>$v1){
  93. $tInfo = Db::name('dinner_type')
  94. ->where('id',$v1)
  95. ->find();
  96. $typeName.='<span class="label" style="border-radius:0rem !important;background-color: '.$tInfo['color'].';color: white">'.$tInfo['name'].'</span>';
  97. if(count($typeIdS)>1 && $k1<count($typeIdS)-1){
  98. $typeName.='<br/>';
  99. }
  100. }
  101. $lists[$k]['tName'] = $typeName;
  102. $item = '';
  103. foreach ($all as $k2=>$v2){
  104. if($v2['money'] == 0 && $v2['oldmoney'] > 0){
  105. $v2['money'] = $v2['oldmoney'];
  106. }
  107. $ss = $k2+1;
  108. if($ss<10){
  109. $ss= '0'.$ss;
  110. }
  111. $ur = url('dinner/detail').'?id='.$v2['dinner_id'];
  112. $item.=''.$ss.',<span class="label" style="border-radius:0rem !important;background-color: '.$v2['color'].';color: white">'.$v2['dinner_type_name'].'</span><a data-title="详情" href="javascript:;" url="'.$ur.'">'.$v2['dinner_name'].'*'.$v2['num'];
  113. if(count($all)>1 && $k2<count($all)){
  114. $item.='<br/>';
  115. }
  116. }
  117. $lists[$k]['item'] = $item;
  118. }
  119. //数据返回
  120. $totalCount = db($this->table)
  121. ->alias('a')
  122. ->where($map)->count();
  123. $totalPage = ceil($totalCount/$length);
  124. $result['page'] = $page;
  125. $result['total'] = $totalPage;
  126. $result['records'] = $totalCount;
  127. $result['rows'] = $lists;
  128. return json($result);
  129. }else{
  130. $this->assign('start',date('Y-m-d'));
  131. $typeList = model('DinnerType')->getList($this->orgId);
  132. $this->assign('typeList',$typeList);
  133. return $this->fetch();
  134. }
  135. }
  136. public function index1(){
  137. $is_spot = input('is_spot/d',0);
  138. if($is_spot == 2){
  139. $map[] = ['cate','=',1];
  140. $start = input('start',date('Y-m-d'),'trim');
  141. $oids = Db::name('dinner_order_item')
  142. ->where('day',$start)
  143. ->group('order_id')
  144. ->distinct(true)
  145. ->column('order_id');
  146. if($oids){
  147. $map[] = ['id','in',$oids];
  148. }else{
  149. $map[] = ['id','=',-1];
  150. }
  151. }else{
  152. $map[] = ['is_spot','=',$is_spot];
  153. $map[] = ['cate','=',0];
  154. $start = input('start',date('Y-m-d'),'trim');
  155. $map[] = ['ymd','=',date('Ymd',strtotime($start))];
  156. }
  157. $map[] = ['org_id','=',$this->orgId];
  158. $map[] = ['state','<>',0];
  159. $map= empty($map) ? true: $map;
  160. //数据查询
  161. $lists = Db::name($this->table)->where($map)->column('id');
  162. $newArr = [];
  163. $type = [];
  164. $rows = [];
  165. if(!empty($lists)){
  166. $dmap[] = ['a.order_id','in',$lists];
  167. $dmap[] = ['a.is_refuse','=',0];
  168. if($is_spot == 2){
  169. $dmap[] = ['a.day','=',$start];
  170. }
  171. $all = Db::name('dinner_order_item')
  172. ->alias('a')
  173. ->join('dinner_type c','a.dinner_type_id=c.id')
  174. ->field('c.id,a.order_id,c.color,c.name as dinner_type_name')
  175. ->where($dmap)
  176. ->group('a.dinner_type_id')
  177. ->select();
  178. $type = $all;
  179. $maxrow = 0;
  180. foreach ($type as $k=>$v){
  181. $total = Db::name('dinner_order_item')
  182. ->alias('a')
  183. ->join('dinner b','a.dinner_id=b.id')
  184. ->join('dinner_type c','a.dinner_type_id=c.id')
  185. ->field('sum(a.num) as nums,b.name as dinner_name,c.color,c.name as dinner_type_name')
  186. ->where('a.dinner_type_id',$v['id'])
  187. ->where($dmap)
  188. ->group('a.dinner_id')
  189. ->select();
  190. $total = $total?$total:[];
  191. $count = count($total);
  192. if($count > $maxrow){
  193. $maxrow = $count;
  194. }
  195. $newArr[] = $total;
  196. }
  197. $i = 0;
  198. while (true){
  199. if($maxrow <= 0){
  200. break;
  201. }
  202. $d = [];
  203. foreach ($type as $k=>$v){
  204. $dd = $newArr[$k];
  205. $d['name'.$v['id']] = isset($dd[$i])?$dd[$i]['dinner_name']:'';
  206. $d['nums'.$v['id']] = isset($dd[$i])?$dd[$i]['nums']:'';
  207. }
  208. $rows[] = $d;
  209. $maxrow--;
  210. $i++;
  211. }
  212. }
  213. $this->assign('type',$type);
  214. $this->assign('data',$rows);
  215. $this->assign('is_spot',$is_spot);
  216. $this->assign('start',$start);
  217. return $this->fetch();
  218. }
  219. public function day(){
  220. $cate = input('cate/d',0);
  221. $this->assign('cate',$cate);
  222. $map[] = ['cate','=',$cate];
  223. if($cate == 1){
  224. $start = input('start',date('Y-m-d'),'trim');
  225. $oids = Db::name('dinner_order_item')
  226. ->where('day',$start)
  227. ->group('order_id')
  228. ->distinct(true)
  229. ->column('order_id');
  230. if($oids){
  231. $map[] = ['id','in',$oids];
  232. }else{
  233. $map[] = ['id','=',-1];
  234. }
  235. }else{
  236. $start = input('start',date('Y-m-d'),'trim');
  237. $map[] = ['ymd','=',date('Ymd',strtotime($start))];
  238. }
  239. $map[] = ['org_id','=',$this->orgId];
  240. $map[] = ['state','<>',0];
  241. $map= empty($map) ? true: $map;
  242. //数据查询
  243. $lists = Db::name($this->table)
  244. ->where($map)->column('id');
  245. $newArr = [];
  246. $type = [];
  247. $rows = [];
  248. if(!empty($lists)){
  249. $dmap = [];
  250. $dmap[] = ['a.order_id','in',$lists];
  251. $dmap[] = ['a.is_refuse','=',0];
  252. if($cate == 1){
  253. $dmap[] = ['a.day','=',$start];
  254. }
  255. $all = Db::name('dinner_order_item')
  256. ->alias('a')
  257. ->join('dinner_type c','a.dinner_type_id=c.id')
  258. ->field('c.id,c.color,c.name as dinner_type_name')
  259. ->where($dmap)
  260. ->group('a.dinner_type_id')
  261. ->select();
  262. $type = $all;
  263. $maxrow = 0;
  264. foreach ($type as $k=>$v){
  265. $total = Db::name('dinner_order_item')
  266. ->alias('a')
  267. ->join('dinner b','a.dinner_id=b.id')
  268. ->join('dinner_type c','a.dinner_type_id=c.id')
  269. ->field('sum(a.price) as nums,b.name as dinner_name,c.color,c.name as dinner_type_name')
  270. ->where('a.dinner_type_id',$v['id'])
  271. ->where($dmap)
  272. ->group('a.dinner_id')
  273. ->select();
  274. $count = Db::name('dinner_order_item')
  275. ->alias('a')
  276. ->join('dinner b','a.dinner_id=b.id')
  277. ->join('dinner_type c','a.dinner_type_id=c.id')
  278. ->where('a.dinner_type_id',$v['id'])
  279. ->where($dmap)
  280. ->sum('a.price');
  281. $type[$k]['total'] = sprintf("%.2f",$count);
  282. $newArr[] = $total;
  283. $count = count($total);
  284. if($count > $maxrow){
  285. $maxrow = $count;
  286. }
  287. }
  288. $i = 0;
  289. while (true){
  290. if($maxrow <= 0){
  291. break;
  292. }
  293. $d = [];
  294. foreach ($type as $k=>$v){
  295. $dd = $newArr[$k];
  296. $d['name'.$v['id']] = isset($dd[$i])?$dd[$i]['dinner_name']:'';
  297. $d['price'.$v['id']] = isset($dd[$i])?$dd[$i]['nums']:'';
  298. }
  299. $rows[] = $d;
  300. $maxrow--;
  301. $i++;
  302. }
  303. }
  304. $this->assign('type',$type);
  305. $this->assign('data',$rows);
  306. $this->assign('start',$start);
  307. return $this->fetch();
  308. }
  309. public function typeTj(){
  310. $start = input('start',date('Y-m-d'),'trim');
  311. $end = input('end',date('Y-m-d'),'trim');
  312. $data = $this->typeTjData($start,$end);
  313. $this->assign('total',$data['b']);
  314. $this->assign('data',$data['a']);
  315. $this->assign('start',$start);
  316. $this->assign('end',$end);
  317. return $this->fetch();
  318. }
  319. public function typeTjData($start,$end){
  320. $cate = input('cate/d',0);
  321. $this->assign('cate',$cate);
  322. $map[] = ['org_id','=',$this->orgId];
  323. $map[] = ['state','<>',0];
  324. $map[] = ['cate','=',$cate];
  325. $map= empty($map) ? true: $map;
  326. //数据查询
  327. $day = getDateFromRange($start,$end);
  328. $a = [];
  329. $b0 = $b1 = $b2=$b3=$b4=0;
  330. foreach ($day as $k=>$v){
  331. $map1 = $map;
  332. $day = date('Ymd',strtotime($v));
  333. if($cate == 1){
  334. $oids = Db::name('dinner_order_item')
  335. ->where('day',$day)
  336. ->group('order_id')
  337. ->distinct(true)
  338. ->column('order_id');
  339. if($oids){
  340. $map1[] = ['id','in',$oids];
  341. }else{
  342. $map1[] = ['id','=',-1];
  343. }
  344. }else{
  345. $map1[] = ['ymd','=',$day];
  346. }
  347. $a0 = Db::name($this->table)
  348. ->where($map1)
  349. ->column('id');
  350. if(empty($a0)){
  351. $k0 = 0;
  352. }else{
  353. $map2 = [];
  354. $map2[] = ['a.is_refuse','=',0];
  355. $map2[] = ['a.order_id','in',$a0];
  356. if($cate == 1){
  357. $map2[] = ['a.day','=',$day];
  358. }
  359. $k0= Db::name('dinner_order_item')
  360. ->alias('a')
  361. ->where($map2)
  362. ->sum('a.price');
  363. }
  364. $b0+=$k0;
  365. $a1 = Db::name($this->table)
  366. ->where($map)
  367. ->where('ymd',date('Ymd',strtotime($v)))
  368. ->where('order_type',1)
  369. ->column('id');
  370. if(empty($a1)){
  371. $k1 = 0;
  372. }else{
  373. $k1 = Db::name('dinner_order_item')
  374. ->alias('a')
  375. ->where('a.is_refuse',0)
  376. ->where('a.order_id','in',$a1)
  377. ->sum('a.price');
  378. }
  379. $b1+=$k1;
  380. $a2 = Db::name($this->table)
  381. ->where($map)
  382. ->where('ymd',date('Ymd',strtotime($v)))
  383. ->where('order_type',2)
  384. ->column('id');
  385. if(empty($a1)){
  386. $k2 = 0;
  387. }else{
  388. $k2 = Db::name('dinner_order_item')
  389. ->alias('a')
  390. ->where('a.is_refuse',0)
  391. ->where('a.order_id','in',$a2)
  392. ->sum('a.price');
  393. }
  394. $b2+=$k2;
  395. $a3 = Db::name($this->table)
  396. ->where($map)
  397. ->where('ymd',date('Ymd',strtotime($v)))
  398. ->where('order_type',3)
  399. ->column('id');
  400. if(empty($a1)){
  401. $k3 = 0;
  402. }else{
  403. $k3 = Db::name('dinner_order_item')
  404. ->alias('a')
  405. ->where('a.is_refuse',0)
  406. ->where('a.order_id','in',$a3)
  407. ->sum('a.price');
  408. }
  409. $b3+=$k3;
  410. $a4 = Db::name($this->table)
  411. ->where($map)
  412. ->where('ymd',date('Ymd',strtotime($v)))
  413. ->count();
  414. $b4+=$a4;
  415. $a[] = [
  416. 'title'=>$v,
  417. 'a0'=>$k0,
  418. 'a1'=>$k1,
  419. 'a2'=>$k2,
  420. 'a3'=>$k3,
  421. 'a4'=>$a4,
  422. ];
  423. }
  424. $b = [
  425. 'b0'=>$b0,
  426. 'b1'=>$b1,
  427. 'b2'=>$b2,
  428. 'b3'=>$b3,
  429. 'b4'=>$b4,
  430. ];
  431. return ['a'=>$a,'b'=>$b];
  432. }
  433. /**
  434. * 删除记录
  435. * @param int $id
  436. */
  437. public function del($id=0){
  438. if(!$id){
  439. $this->error('参数错误');
  440. }
  441. $res = db($this->table)->where('id',$id)->setField('del',1);
  442. if($res){
  443. $this->success('删除成功');
  444. }else{
  445. $this->error('删除失败');
  446. }
  447. }
  448. /**
  449. * 改变字段值
  450. * @param int $fv
  451. * @param string $fn
  452. * @param int $fv
  453. */
  454. public function changeField($id=0,$fn='',$fv=0){
  455. if(!$fn||!$id){
  456. $this->error('参数错误');
  457. }
  458. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  459. if($res){
  460. $this->success('操作成功');
  461. }else{
  462. $this->error('操作失败');
  463. }
  464. }
  465. public function print($id=0){
  466. $info = db($this->table)->where('id',$id)->find();
  467. $all = Db::name('dinner_order_item')
  468. ->alias('a')
  469. ->join('dinner b','a.dinner_id=b.id')
  470. ->join('dinner_type c','a.dinner_type_id=c.id')
  471. ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
  472. ->where('a.order_id',$id)
  473. ->where('a.is_refuse',0)
  474. ->select();
  475. $info['item'] = $all;
  476. $this->assign('info',$info);
  477. return $this->fetch();
  478. }
  479. public function option($fv=0,$id=0){
  480. $res = db($this->table)
  481. ->where('org_id',$this->orgId)
  482. ->where('id',$id)
  483. ->update([
  484. 'is_ok'=>$fv,
  485. 'update_time'=>getTime()
  486. ]);
  487. if($res){
  488. $this->success('操作成功');
  489. }else{
  490. $this->error('操作失败');
  491. }
  492. }
  493. public function export(){
  494. $ex_type = input('ex_type',1);//1导出2发饭表导出
  495. if($ex_type==1){
  496. $data = $this->ex_type_data1();
  497. $this->ex_type_export1($data);
  498. }elseif ($ex_type==2){
  499. $data = $this->ex_type_data2();
  500. $this->ex_type_export2($data);
  501. }
  502. }
  503. public function ex_type_export1($data){
  504. $lists = $data['list'];
  505. $count = $data['total'];
  506. //实例化PHPExcel类
  507. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  508. $objPHPExcel = new \PHPExcel();
  509. //激活当前的sheet表
  510. $sheet = $objPHPExcel->setActiveSheetIndex(0);
  511. //设置表格头(即excel表格的第一行)
  512. $objPHPExcel->setActiveSheetIndex(0)
  513. ->setCellValue('A1', '序号')
  514. ->setCellValue('B1', '单号')
  515. ->setCellValue('C1', '总价')
  516. ->setCellValue('D1', '下单人')
  517. ->setCellValue('E1', '下单人电话')
  518. ->setCellValue('F1', '送货地址')
  519. ->setCellValue('G1', '床号及备注')
  520. ->setCellValue('H1', '下单时间')
  521. ->setCellValue('I1', '类别')
  522. ->setCellValue('J1', '购买物品');
  523. // 设置表格头水平居中
  524. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  525. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  526. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  527. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  528. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  529. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  530. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  531. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  532. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  533. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  534. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  535. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  536. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  537. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  538. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  539. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  540. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  541. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  542. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  543. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  544. //设置列水平居中
  545. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  546. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  547. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  548. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  549. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  550. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  551. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  552. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  553. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  554. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  555. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  556. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  557. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  558. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  559. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  560. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  561. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  562. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  563. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  564. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  565. //设置单元格宽度
  566. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  567. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  568. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  569. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  570. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  571. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  572. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  573. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  574. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  575. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  576. //循环刚取出来的数组,将数据逐一添加到excel表格。
  577. for ($i = 0; $i < count($lists); $i++) {
  578. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
  579. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['sn']);
  580. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['price']);
  581. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['name']);
  582. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['mobile']);
  583. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['address']);
  584. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['remark']);
  585. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['pay_time']);
  586. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['tName']);
  587. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['item']);
  588. }
  589. //$objPHPExcel->getActiveSheet()->setCellValue('J' .(count($lists)+3), '总价:'.$count);
  590. $titles = array('序号','单号','总价','下单人','下单人电话','送货地址','床号及备注','下单时间','类别','购买物品');
  591. $count_length = count($titles)-1;
  592. $num =count($lists)+2;
  593. $sheet->setCellValue(\PHPExcel_Cell::stringFromColumnIndex(0).$num, '总价');
  594. $sheets = sprintf('%s:%s', chr(65).$num, \PHPExcel_Cell::stringFromColumnIndex($count_length-1).$num);
  595. $objPHPExcel->setActiveSheetIndex(0)->mergeCells($sheets);
  596. $objPHPExcel->getActiveSheet()->getStyle($sheets)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  597. //
  598. $sheet->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($count_length).$num,$count.'元');
  599. //设置保存的Excel表格名称
  600. $filename = '订单列表' . date('YmdHis', time()) . '.xls';
  601. //设置当前激活的sheet表格名称
  602. $objPHPExcel->getActiveSheet()->setTitle('订单列表');
  603. //设置浏览器窗口下载表格
  604. ob_end_clean();
  605. header("Content-Type: application/force-download");
  606. header("Content-Type: application/octet-stream");
  607. header("Content-Type: application/download");
  608. header('Content-Disposition:inline;filename="' . $filename);
  609. //生成excel文件
  610. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  611. //下载文件在浏览器窗口
  612. return $objWriter->save('php://output');
  613. }
  614. public function ex_type_data1(){
  615. $title = input('title','','trim');
  616. if($title){
  617. $map[] = ['a.name|a.sn','like','%'.$title.'%'];
  618. }
  619. $is_ok = input('is_ok',0,'trim');
  620. if($is_ok>=0){
  621. $map[] = ['a.is_ok','=',$is_ok];
  622. }
  623. $is_spot = input('is_spot',0,'trim');
  624. if($is_spot>=0){
  625. $map[] = ['a.is_spot','=',$is_spot];
  626. }
  627. $start = input('start','','trim');
  628. if($start){
  629. $map[] = ['a.ymd','=',date('Ymd',strtotime($start))];
  630. }
  631. $dinner_type_id = input('type','','trim');
  632. if($dinner_type_id != ''){
  633. $dinner_order_item = Db::name('dinner_order_item')
  634. ->where('dinner_type_id',$dinner_type_id)
  635. ->where('is_refuse',0)
  636. ->column('order_id');
  637. if(empty($dinner_order_item)){
  638. $map[] = ['a.id','=',-1];
  639. }else{
  640. $map[] = ['a.id','in',$dinner_order_item];
  641. }
  642. }
  643. $map[] = ['a.org_id','=',$this->orgId];
  644. $map[] = ['a.state','<>',0];
  645. $map= empty($map) ? true: $map;
  646. //数据查询
  647. $lists = db($this->table)
  648. ->alias('a')
  649. ->field('a.*')
  650. ->where($map)->order(
  651. ['a.id'=>'desc']
  652. )->select();
  653. foreach ($lists as $k=>$v){
  654. $all = Db::name('dinner_order_item')
  655. ->alias('a')
  656. ->join('dinner b','a.dinner_id=b.id')
  657. ->join('dinner_type c','a.dinner_type_id=c.id')
  658. ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
  659. ->where('a.order_id',$v['id'])
  660. ->where('a.is_refuse',0)
  661. ->select();
  662. $typeIdS = $all?array_unique(array_column($all,'dinner_type_id')):[];
  663. $typeName = '';
  664. foreach ($typeIdS as $k1=>$v1){
  665. $tInfo = Db::name('dinner_type')
  666. ->where('id',$v1)
  667. ->find();
  668. $typeName.=$tInfo['name'];
  669. if(count($typeIdS)>1 && $k1<count($typeIdS)-1){
  670. $typeName.=',';
  671. }
  672. }
  673. $lists[$k]['tName'] = $typeName;
  674. $item = '';
  675. foreach ($all as $k2=>$v2){
  676. $item.=$v2['dinner_name'].'('.$v2['dinner_type_name'].')'.'*'.$v2['num'];
  677. if(count($all)>1 && $k2<count($all)-1){
  678. $item.=',';
  679. }
  680. }
  681. if(!$item){
  682. unset($lists[$k]);
  683. continue;
  684. }else{
  685. $lists[$k]['item'] = $item;
  686. }
  687. }
  688. sort($lists);
  689. $num = db($this->table)
  690. ->alias('a')
  691. ->where($map)->sum('price');
  692. $data = [
  693. 'list'=>$lists,
  694. 'total'=>$num
  695. ];
  696. return $data;
  697. }
  698. public function ex_type_data2(){
  699. $title = input('title','','trim');
  700. if($title){
  701. $map[] = ['a.name|a.sn','like','%'.$title.'%'];
  702. }
  703. $is_ok = input('is_ok',0,'trim');
  704. if($is_ok>=0){
  705. $map[] = ['a.is_ok','=',$is_ok];
  706. }
  707. // $is_spot = input('is_spot',0,'trim');
  708. // if($is_spot>=0){
  709. // $map[] = ['a.is_spot','=',$is_spot];
  710. // }
  711. // $start = input('start','','trim');
  712. // if($start){
  713. // $map[] = ['a.ymd','=',date('Ymd',strtotime($start))];
  714. // }
  715. $is_spot = input('is_spot/d',0);
  716. if($is_spot == 2){
  717. $map[] = ['a.cate','=',1];
  718. $start = input('start',date('Y-m-d'),'trim');
  719. $oids = Db::name('dinner_order_item')
  720. ->where('day',$start)
  721. ->group('order_id')
  722. ->distinct(true)
  723. ->column('order_id');
  724. if($oids){
  725. $map[] = ['a.id','in',$oids];
  726. }else{
  727. $map[] = ['a.id','=',-1];
  728. }
  729. }else{
  730. $map[] = ['a.is_spot','=',$is_spot];
  731. $map[] = ['a.cate','=',0];
  732. $start = input('start',date('Y-m-d'),'trim');
  733. $map[] = ['a.ymd','=',date('Ymd',strtotime($start))];
  734. }
  735. $dinner_type_id = input('type','','trim');
  736. if($dinner_type_id != ''){
  737. $dinner_order_item = Db::name('dinner_order_item')
  738. ->where('dinner_type_id',$dinner_type_id)
  739. ->where('is_refuse',0)
  740. ->column('order_id');
  741. if(empty($dinner_order_item)){
  742. $map[] = ['a.id','=',-1];
  743. }else{
  744. $map[] = ['a.id','in',$dinner_order_item];
  745. }
  746. }
  747. $map[] = ['a.org_id','=',$this->orgId];
  748. $map[] = ['a.state','<>',0];
  749. $map= empty($map) ? true: $map;
  750. //数据查询
  751. $lists = Db::name($this->table)
  752. ->alias('a')
  753. ->field('a.*')
  754. ->where($map)->order(
  755. ['a.address'=>'asc','a.id'=>'desc']
  756. )->select();
  757. foreach ($lists as $k=>$v){
  758. $dmap = [];
  759. $dmap[] = ['a.order_id','=',$v['id']];
  760. $dmap[] = ['a.is_refuse','=',0];
  761. if($dinner_type_id > 0){
  762. $dmap[] = ['a.dinner_type_id','=',$dinner_type_id];
  763. }
  764. if($is_spot == 2){
  765. $dmap[] = ['a.day','=',$start];
  766. }
  767. $all = Db::name('dinner_order_item')
  768. ->alias('a')
  769. ->join('dinner b','a.dinner_id=b.id')
  770. ->join('dinner_type c','a.dinner_type_id=c.id')
  771. ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
  772. ->where($dmap)
  773. ->select();
  774. $typeIdS = $all?array_unique(array_column($all,'dinner_type_id')):[];
  775. $typeName = [];
  776. foreach ($typeIdS as $k1=>$v1){
  777. $tInfo = Db::name('dinner_type')
  778. ->where('id',$v1)
  779. ->find();
  780. $typeName[] = $tInfo['name'];
  781. }
  782. $lists[$k]['tName'] = implode(',',$typeName);
  783. $item = '';
  784. foreach ($all as $k2=>$v2){
  785. $item.=$v2['dinner_name'].'('.$v2['dinner_type_name'].')'.'*'.$v2['num'];
  786. if(count($all)>1 && $k2<count($all)-1){
  787. $item.=',';
  788. }
  789. }
  790. if(!$item){
  791. unset($lists[$k]);
  792. continue;
  793. }else{
  794. $lists[$k]['item'] = $item;
  795. }
  796. }
  797. sort($lists);
  798. $orderId = array_column($lists,'id');
  799. $dmap2 = [];
  800. $dmap2[] = ['a.order_id','in',$orderId];
  801. $dmap2[] = ['a.is_refuse','=',0];
  802. if($dinner_type_id > 0){
  803. $dmap2[] = ['a.dinner_type_id','=',$dinner_type_id];
  804. }
  805. if($is_spot == 2){
  806. $dmap2[] = ['a.day','=',$start];
  807. }
  808. $total = Db::name('dinner_order_item')
  809. ->alias('a')
  810. ->join('dinner b','a.dinner_id=b.id')
  811. ->join('dinner_type c','a.dinner_type_id=c.id')
  812. ->field('sum(a.num) as nums,b.name as dinner_name,c.color,c.name as dinner_type_name')
  813. ->where($dmap2)
  814. ->group('a.dinner_id')
  815. ->select();
  816. $items = '';
  817. foreach ($total as $k3=>$v3){
  818. $items.=$v3['dinner_name'].'('.$v3['dinner_type_name'].')'.'*'.$v3['nums'];
  819. if(count($total)>1 && $k3<count($total)-1){
  820. $items.=',';
  821. }
  822. }
  823. $data = [
  824. 'list'=>$lists,
  825. 'total'=>$items
  826. ];
  827. return $data;
  828. }
  829. public function print_all(){
  830. $data = $this->ex_type_data2();
  831. $this->assign('day',date('Y年m月d日'));
  832. $this->assign('data',$data);
  833. return $this->fetch();
  834. }
  835. public function ex_type_export2($data){
  836. $lists = $data['list'];
  837. $count = $data['total'];
  838. //实例化PHPExcel类
  839. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  840. $objPHPExcel = new \PHPExcel();
  841. //激活当前的sheet表
  842. $sheet = $objPHPExcel->setActiveSheetIndex(0);
  843. $titles = array('地址'=>30,'床号及备注'=>14,'姓名'=>10,'手机号'=>14,'餐别'=>8,'总价'=>8,'内容'=>60);
  844. $count_length = count($titles)-1;
  845. $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit('A1', '分餐列表', \PHPExcel_Cell_DataType::TYPE_STRING);
  846. $sheet = sprintf('%s:%s', chr(65).'1', \PHPExcel_Cell::stringFromColumnIndex($count_length).'1');
  847. $objPHPExcel->setActiveSheetIndex(0)->mergeCells($sheet);
  848. $objPHPExcel->setActiveSheetIndex(0)->getStyle($sheet)->getFont()->getColor($sheet)->setARGB(\PHPExcel_Style_Color::COLOR_BLACK);
  849. $as = $objPHPExcel->getActiveSheet();
  850. $as->getStyle($sheet)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  851. $as->getStyle($sheet)->getFont()->setBold(true);
  852. $as->getStyle($sheet)->getFont()->setSize(18);
  853. $column=0;
  854. foreach ($titles as $key => $value) {
  855. $k = \PHPExcel_Cell::stringFromColumnIndex($column);
  856. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.'2', $key);
  857. $objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth($value);
  858. // $objPHPExcel->getActiveSheet()->getColumnDimension($k)->setAutoSize(true);
  859. $objPHPExcel->getActiveSheet(0)->getStyle($k.'2')->getFont()->setBold(true);
  860. $column++;
  861. }
  862. $num=3;
  863. $cell = $objPHPExcel->setActiveSheetIndex(0);
  864. // 订餐
  865. $dinner_order = array();
  866. foreach ($lists as $key => $value) {
  867. if(empty($value['item'])){
  868. continue;
  869. }
  870. $column = 0;
  871. // 床号
  872. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  873. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['address'].' ');
  874. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  875. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['remark'].' ');
  876. // 姓名
  877. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  878. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['name']);
  879. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  880. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['mobile'].' ');
  881. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  882. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['tName']);
  883. // 总价
  884. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  885. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['price'].' ');
  886. // 内容
  887. $objPHPExcel->getActiveSheet()->getStyle(\PHPExcel_Cell::stringFromColumnIndex($column).$num)->getAlignment()->setWrapText(true)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  888. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex($column++).$num, $value['item']);
  889. $num++;
  890. }
  891. $sheet = sprintf('%s:%s', chr(65).$num, \PHPExcel_Cell::stringFromColumnIndex($count_length).$num);
  892. $objPHPExcel->setActiveSheetIndex(0)->mergeCells($sheet);
  893. $objPHPExcel->getActiveSheet()->getStyle($sheet)->getAlignment()->setWrapText(true)->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  894. $objPHPExcel->getActiveSheet()->getStyle($sheet)->getFont()->setBold(20);
  895. $cell->setCellValue(\PHPExcel_Cell::stringFromColumnIndex(0).$num, '合计:'.$count);
  896. $styleThinBlackBorderOutline = array(
  897. 'borders' => array(
  898. 'allborders' => array( //设置全部边框
  899. 'style' => \PHPExcel_Style_Border::BORDER_THIN, //粗的是thick
  900. 'color' => array('argb' => 'FF000000'),
  901. ),
  902. ),
  903. );
  904. $count = \PHPExcel_Cell::stringFromColumnIndex($count_length).$num;
  905. // var_dump(sprintf('A1:%s',$count));exit;
  906. $objPHPExcel->getActiveSheet()->getStyle(sprintf('A1:%s', $count))->applyFromArray($styleThinBlackBorderOutline);
  907. //设置保存的Excel表格名称
  908. $filename = '分餐列表' . date('YmdHis', time()) . '.xls';
  909. //设置当前激活的sheet表格名称
  910. $objPHPExcel->getActiveSheet()->setTitle('分餐列表');
  911. //设置浏览器窗口下载表格
  912. ob_end_clean();
  913. header("Content-Type: application/force-download");
  914. header("Content-Type: application/octet-stream");
  915. header("Content-Type: application/download");
  916. header('Content-Disposition:inline;filename="' . $filename);
  917. //生成excel文件
  918. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  919. //下载文件在浏览器窗口
  920. return $objWriter->save('php://output');
  921. }
  922. public function exportTypeTj(){
  923. $start = input('start',date('Y-m-d'),'trim');
  924. $end = input('end',date('Y-m-d'),'trim');
  925. $data = $this->typeTjData($start,$end);
  926. //实例化PHPExcel类
  927. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  928. $objPHPExcel = new \PHPExcel();
  929. //激活当前的sheet表
  930. $sheet = $objPHPExcel->setActiveSheetIndex(0);
  931. //设置表格头(即excel表格的第一行)
  932. $objPHPExcel->setActiveSheetIndex(0)
  933. ->setCellValue('A1', '订餐日期')
  934. ->setCellValue('B1', '订单金额(元)')
  935. ->setCellValue('C1', '微信支付(元)')
  936. ->setCellValue('D1', '食堂卡(元)')
  937. ->setCellValue('E1', '现金(元)')
  938. ->setCellValue('F1', '订单数');
  939. // 设置表格头水平居中
  940. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  941. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  942. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  943. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  944. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  945. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  946. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  947. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  948. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  949. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  950. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  951. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  952. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  953. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  954. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  955. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  956. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  957. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  958. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  959. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  960. //设置列水平居中
  961. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  962. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  963. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  964. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  965. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  966. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  967. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  968. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  969. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  970. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  971. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  972. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  973. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  974. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  975. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  976. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  977. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  978. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  979. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  980. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  981. //设置单元格宽度
  982. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  983. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  984. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  985. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  986. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  987. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  988. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  989. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  990. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  991. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  992. $lists = $data['a'];
  993. //循环刚取出来的数组,将数据逐一添加到excel表格。
  994. for ($i = 0; $i < count($lists); $i++) {
  995. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['title']);
  996. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['a0']);
  997. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['a1']);
  998. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['a2']);
  999. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['a3']);
  1000. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['a4']);
  1001. }
  1002. $objPHPExcel->getActiveSheet()->setCellValue('A' . (count($lists) + 2), '合计:');
  1003. $objPHPExcel->getActiveSheet()->setCellValue('B' . (count($lists) + 2), $data['b']['b0']);
  1004. $objPHPExcel->getActiveSheet()->setCellValue('C' . (count($lists) + 2), $data['b']['b1']);
  1005. $objPHPExcel->getActiveSheet()->setCellValue('D' . (count($lists) + 2), $data['b']['b2']);
  1006. $objPHPExcel->getActiveSheet()->setCellValue('E' . (count($lists) + 2), $data['b']['b3']);
  1007. $objPHPExcel->getActiveSheet()->setCellValue('F' . (count($lists) + 2), $data['b']['b4']);
  1008. //设置保存的Excel表格名称
  1009. $filename = '订餐统计表' . date('YmdHis', time()) . '.xls';
  1010. //设置浏览器窗口下载表格
  1011. ob_end_clean();
  1012. header("Content-Type: application/force-download");
  1013. header("Content-Type: application/octet-stream");
  1014. header("Content-Type: application/download");
  1015. header('Content-Disposition:inline;filename="' . $filename);
  1016. //生成excel文件
  1017. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1018. //下载文件在浏览器窗口
  1019. return $objWriter->save('php://output');
  1020. }
  1021. public function orgasdinner(){
  1022. $start = input('start',date('Y-m-d'),'trim');
  1023. $end = input('end',date('Y-m-d'),'trim');
  1024. $type = input('type',0);
  1025. $orgId = input('org_id',0);
  1026. $data = $this->orgasdinnerData($start,$end,$type,$orgId);
  1027. $this->assign('total',$data['b']);
  1028. $this->assign('data',$data['a']);
  1029. $this->assign('start',$start);
  1030. $this->assign('end',$end);
  1031. $this->assign('type',$type);
  1032. $this->assign('orgId',$orgId);
  1033. $orgList = Db::name('org')
  1034. ->where('del',0)
  1035. ->where('enable',1)
  1036. ->where('type',2)
  1037. ->where('parent_id','>',0)
  1038. ->select();
  1039. $this->assign('orgList',$orgList);
  1040. return $this->fetch();
  1041. }
  1042. public function orgasdinnerData($start,$end,$type,$orgId){
  1043. $cate = input('cate/d',0);
  1044. $this->assign('cate',$cate);
  1045. if($orgId >0){
  1046. $map[] = ['org_id','=',$orgId];
  1047. }
  1048. $map[] = ['cate','=',0];
  1049. $map[] = ['state','<>',0];
  1050. $map= empty($map) ? true: $map;
  1051. //数据查询
  1052. if($type==0){//按天
  1053. $day = getDateFromRange($start,$end);
  1054. $a = [];
  1055. $b0 = $b1 = $b2=$b3=$b4=0;
  1056. foreach ($day as $k=>$v){
  1057. $a0 = Db::name($this->table)
  1058. ->where($map)
  1059. ->where('ymd',date('Ymd',strtotime($v)))
  1060. ->column('id');
  1061. if(empty($a0)){
  1062. $k0 = 0;
  1063. }else{
  1064. $k0= Db::name('dinner_order_item')
  1065. ->alias('a')
  1066. ->where('a.is_refuse',0)
  1067. ->where('a.order_id','in',$a0)
  1068. ->sum('a.price');
  1069. }
  1070. $b0+=$k0;
  1071. $a1 = Db::name($this->table)
  1072. ->where($map)
  1073. ->where('ymd',date('Ymd',strtotime($v)))
  1074. ->where('order_type',1)
  1075. ->column('id');
  1076. if(empty($a1)){
  1077. $k1 = 0;
  1078. }else{
  1079. $k1 = Db::name('dinner_order_item')
  1080. ->alias('a')
  1081. ->where('a.is_refuse',0)
  1082. ->where('a.order_id','in',$a1)
  1083. ->sum('a.price');
  1084. }
  1085. $b1+=$k1;
  1086. $a2 = Db::name($this->table)
  1087. ->where($map)
  1088. ->where('ymd',date('Ymd',strtotime($v)))
  1089. ->where('order_type',2)
  1090. ->column('id');
  1091. if(empty($a1)){
  1092. $k2 = 0;
  1093. }else{
  1094. $k2 = Db::name('dinner_order_item')
  1095. ->alias('a')
  1096. ->where('a.is_refuse',0)
  1097. ->where('a.order_id','in',$a2)
  1098. ->sum('a.price');
  1099. }
  1100. $b2+=$k2;
  1101. $a3 = Db::name($this->table)
  1102. ->where($map)
  1103. ->where('ymd',date('Ymd',strtotime($v)))
  1104. ->where('order_type',3)
  1105. ->column('id');
  1106. if(empty($a1)){
  1107. $k3 = 0;
  1108. }else{
  1109. $k3 = Db::name('dinner_order_item')
  1110. ->alias('a')
  1111. ->where('a.is_refuse',0)
  1112. ->where('a.order_id','in',$a3)
  1113. ->sum('a.price');
  1114. }
  1115. $b3+=$k3;
  1116. $a4 = Db::name($this->table)
  1117. ->where($map)
  1118. ->where('ymd',date('Ymd',strtotime($v)))
  1119. ->count();
  1120. $b4+=$a4;
  1121. $a[] = [
  1122. 'title'=>$v,
  1123. 'a0'=>$k0,
  1124. 'a1'=>$k1,
  1125. 'a2'=>$k2,
  1126. 'a3'=>$k3,
  1127. 'a4'=>$a4,
  1128. ];
  1129. }
  1130. $b = [
  1131. 'b0'=>$b0,
  1132. 'b1'=>$b1,
  1133. 'b2'=>$b2,
  1134. 'b3'=>$b3,
  1135. 'b4'=>$b4,
  1136. ];
  1137. }else{//按项目
  1138. $map[] = ['ymd','>=',date('Ymd',strtotime($start))];
  1139. $map[] = ['ymd','<=',date('Ymd',strtotime($end))];
  1140. $ow = [];
  1141. if($orgId >0){
  1142. $ow[] = ['id','=',$orgId];
  1143. }
  1144. $day = Db::name('org')
  1145. ->where('del',0)
  1146. ->where('enable',1)
  1147. ->where('type',2)
  1148. ->where('parent_id','>',0)
  1149. ->where($ow)
  1150. ->select();
  1151. $a = [];
  1152. $b0 = $b1 = $b2=$b3=$b4=0;
  1153. foreach ($day as $k=>$v){
  1154. $a0 = Db::name($this->table)
  1155. ->where($map)
  1156. ->where('org_id',$v['id'])
  1157. ->column('id');
  1158. if(empty($a0)){
  1159. $k0 = 0;
  1160. }else{
  1161. $k0= Db::name('dinner_order_item')
  1162. ->alias('a')
  1163. ->where('a.is_refuse',0)
  1164. ->where('a.order_id','in',$a0)
  1165. ->sum('a.price');
  1166. }
  1167. $b0+=$k0;
  1168. $a1 = Db::name($this->table)
  1169. ->where($map)
  1170. ->where('org_id',$v['id'])
  1171. ->where('order_type',1)
  1172. ->column('id');
  1173. if(empty($a1)){
  1174. $k1 = 0;
  1175. }else{
  1176. $k1 = Db::name('dinner_order_item')
  1177. ->alias('a')
  1178. ->where('a.is_refuse',0)
  1179. ->where('a.order_id','in',$a1)
  1180. ->sum('a.price');
  1181. }
  1182. $b1+=$k1;
  1183. $a2 = Db::name($this->table)
  1184. ->where($map)
  1185. ->where('org_id',$v['id'])
  1186. ->where('order_type',2)
  1187. ->column('id');
  1188. if(empty($a1)){
  1189. $k2 = 0;
  1190. }else{
  1191. $k2 = Db::name('dinner_order_item')
  1192. ->alias('a')
  1193. ->where('a.is_refuse',0)
  1194. ->where('a.order_id','in',$a2)
  1195. ->sum('a.price');
  1196. }
  1197. $b2+=$k2;
  1198. $a3 = Db::name($this->table)
  1199. ->where($map)
  1200. ->where('org_id',$v['id'])
  1201. ->where('order_type',3)
  1202. ->column('id');
  1203. if(empty($a1)){
  1204. $k3 = 0;
  1205. }else{
  1206. $k3 = Db::name('dinner_order_item')
  1207. ->alias('a')
  1208. ->where('a.is_refuse',0)
  1209. ->where('a.order_id','in',$a3)
  1210. ->sum('a.price');
  1211. }
  1212. $b3+=$k3;
  1213. $a4 = Db::name($this->table)
  1214. ->where($map)
  1215. ->where('org_id',$v['id'])
  1216. ->count();
  1217. $b4+=$a4;
  1218. $a[] = [
  1219. 'title'=>$v['name'],
  1220. 'a0'=>$k0,
  1221. 'a1'=>$k1,
  1222. 'a2'=>$k2,
  1223. 'a3'=>$k3,
  1224. 'a4'=>$a4,
  1225. ];
  1226. }
  1227. $b = [
  1228. 'b0'=>$b0,
  1229. 'b1'=>$b1,
  1230. 'b2'=>$b2,
  1231. 'b3'=>$b3,
  1232. 'b4'=>$b4,
  1233. ];
  1234. }
  1235. return ['a'=>$a,'b'=>$b];
  1236. }
  1237. public function exportorgasdinner(){
  1238. $start = input('start',date('Y-m-d'),'trim');
  1239. $end = input('end',date('Y-m-d'),'trim');
  1240. $type = input('type',0);
  1241. $orgId = input('org_id',0);
  1242. $data = $this->orgasdinnerData($start,$end,$type,$orgId);
  1243. //实例化PHPExcel类
  1244. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  1245. $objPHPExcel = new \PHPExcel();
  1246. //激活当前的sheet表
  1247. $sheet = $objPHPExcel->setActiveSheetIndex(0);
  1248. //设置表格头(即excel表格的第一行)
  1249. $objPHPExcel->setActiveSheetIndex(0)
  1250. ->setCellValue('A1', $type==0?'订餐日期':'项目')
  1251. ->setCellValue('B1', '订单金额(元)')
  1252. ->setCellValue('C1', '微信支付(元)')
  1253. ->setCellValue('D1', '食堂卡(元)')
  1254. ->setCellValue('E1', '现金(元)')
  1255. ->setCellValue('F1', '订单数');
  1256. // 设置表格头水平居中
  1257. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  1258. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1259. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  1260. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1261. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  1262. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1263. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  1264. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1265. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  1266. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1267. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  1268. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1269. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  1270. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1271. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  1272. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1273. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  1274. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1275. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  1276. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1277. //设置列水平居中
  1278. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  1279. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1280. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  1281. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1282. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  1283. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1284. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  1285. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1286. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  1287. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1288. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  1289. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1290. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  1291. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1292. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  1293. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1294. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  1295. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1296. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  1297. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1298. //设置单元格宽度
  1299. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  1300. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  1301. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  1302. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  1303. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  1304. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  1305. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  1306. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  1307. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  1308. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  1309. $lists = $data['a'];
  1310. //循环刚取出来的数组,将数据逐一添加到excel表格。
  1311. for ($i = 0; $i < count($lists); $i++) {
  1312. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['title']);
  1313. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['a0']);
  1314. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['a1']);
  1315. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['a2']);
  1316. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['a3']);
  1317. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['a4']);
  1318. }
  1319. $objPHPExcel->getActiveSheet()->setCellValue('A' . (count($lists) + 2), '合计:');
  1320. $objPHPExcel->getActiveSheet()->setCellValue('B' . (count($lists) + 2), $data['b']['b0']);
  1321. $objPHPExcel->getActiveSheet()->setCellValue('C' . (count($lists) + 2), $data['b']['b1']);
  1322. $objPHPExcel->getActiveSheet()->setCellValue('D' . (count($lists) + 2), $data['b']['b2']);
  1323. $objPHPExcel->getActiveSheet()->setCellValue('E' . (count($lists) + 2), $data['b']['b3']);
  1324. $objPHPExcel->getActiveSheet()->setCellValue('F' . (count($lists) + 2), $data['b']['b4']);
  1325. //设置保存的Excel表格名称
  1326. $filename = '各项目营业报表' . date('YmdHis', time()) . '.xls';
  1327. //设置浏览器窗口下载表格
  1328. ob_end_clean();
  1329. header("Content-Type: application/force-download");
  1330. header("Content-Type: application/octet-stream");
  1331. header("Content-Type: application/download");
  1332. header('Content-Disposition:inline;filename="' . $filename);
  1333. //生成excel文件
  1334. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1335. //下载文件在浏览器窗口
  1336. return $objWriter->save('php://output');
  1337. }
  1338. public function orgcasdinner(){
  1339. $start = input('start',date('Y-m-d'),'trim');
  1340. $end = input('end',date('Y-m-d'),'trim');
  1341. $type = input('type',0);
  1342. $orgId = input('org_id',0);
  1343. $data = $this->orgcasdinnerData($start,$end,$type,$orgId);
  1344. $this->assign('total',$data['b']);
  1345. $this->assign('data',$data['a']);
  1346. $this->assign('start',$start);
  1347. $this->assign('end',$end);
  1348. $this->assign('type',$type);
  1349. $this->assign('orgId',$orgId);
  1350. $orgList = Db::name('org')
  1351. ->where('del',0)
  1352. ->where('enable',1)
  1353. ->where('type',2)
  1354. ->where('parent_id','>',0)
  1355. ->select();
  1356. $this->assign('orgList',$orgList);
  1357. return $this->fetch();
  1358. }
  1359. public function orgcasdinnerData($start,$end,$type,$orgId){
  1360. $cate = input('cate/d',0);
  1361. $this->assign('cate',$cate);
  1362. if($orgId >0){
  1363. $map[] = ['org_id','=',$orgId];
  1364. }
  1365. $map[] = ['state','<>',0];
  1366. $map[] = ['cate','=',0];
  1367. $map= empty($map) ? true: $map;
  1368. //数据查询
  1369. if($type==0){//按天
  1370. $day = getDateFromRange($start,$end);
  1371. $a = [];
  1372. $b0 = $b1 = $b2=$b3=$b4=0;
  1373. foreach ($day as $k=>$v){
  1374. $a0 = Db::name($this->table)
  1375. ->where($map)
  1376. ->where('ymd',date('Ymd',strtotime($v)))
  1377. ->column('id');
  1378. if(empty($a0)){
  1379. $k0 = 0;
  1380. }else{
  1381. $k0= Db::name('dinner_order_item')
  1382. ->alias('a')
  1383. ->where('a.is_refuse',0)
  1384. ->where('a.order_id','in',$a0)
  1385. ->sum('a.price');
  1386. }
  1387. $b0+=$k0;
  1388. $a1 = Db::name($this->table)
  1389. ->where($map)
  1390. ->where('ymd',date('Ymd',strtotime($v)))
  1391. ->where('order_type',1)
  1392. ->column('id');
  1393. if(empty($a1)){
  1394. $k1 = 0;
  1395. }else{
  1396. $k1 = Db::name('dinner_order_item')
  1397. ->alias('a')
  1398. ->where('a.is_refuse',0)
  1399. ->where('a.order_id','in',$a1)
  1400. ->sum('a.price');
  1401. }
  1402. $b1+=$k1;
  1403. $k2 = round($k1*0.006,2);
  1404. $b2+=$k2;
  1405. $a3 = Db::name($this->table)
  1406. ->where($map)
  1407. ->where('ymd',date('Ymd',strtotime($v)))
  1408. ->where('order_type',2)
  1409. ->column('id');
  1410. if(empty($a3)){
  1411. $k3 = 0;
  1412. }else{
  1413. $k3 = Db::name('dinner_order_item')
  1414. ->alias('a')
  1415. ->where('a.is_refuse',0)
  1416. ->where('a.order_id','in',$a3)
  1417. ->sum('a.price');
  1418. }
  1419. $b3+=$k3;
  1420. $a4 = Db::name($this->table)
  1421. ->where($map)
  1422. ->where('ymd',date('Ymd',strtotime($v)))
  1423. ->where('order_type',3)
  1424. ->column('id');
  1425. if(empty($a1)){
  1426. $k4 = 0;
  1427. }else{
  1428. $k4 = Db::name('dinner_order_item')
  1429. ->alias('a')
  1430. ->where('a.is_refuse',0)
  1431. ->where('a.order_id','in',$a4)
  1432. ->sum('a.price');
  1433. }
  1434. $b4+=$k4;
  1435. $a[] = [
  1436. 'title'=>$v,
  1437. 'a0'=>$k0,
  1438. 'a1'=>$k1,
  1439. 'a2'=>$k2,
  1440. 'a3'=>$k3,
  1441. 'a4'=>$k4,
  1442. ];
  1443. }
  1444. $b = [
  1445. 'b0'=>$b0,
  1446. 'b1'=>$b1,
  1447. 'b2'=>$b2,
  1448. 'b3'=>$b3,
  1449. 'b4'=>$b4,
  1450. ];
  1451. }else{//按项目
  1452. $map[] = ['ymd','>=',date('Ymd',strtotime($start))];
  1453. $map[] = ['ymd','<=',date('Ymd',strtotime($end))];
  1454. $ow = [];
  1455. if($orgId >0){
  1456. $ow[] = ['id','=',$orgId];
  1457. }
  1458. $day = Db::name('org')
  1459. ->where('del',0)
  1460. ->where('enable',1)
  1461. ->where('type',2)
  1462. ->where('parent_id','>',0)
  1463. ->where($ow)
  1464. ->select();
  1465. $a = [];
  1466. $b0 = $b1 = $b2=$b3=$b4=0;
  1467. foreach ($day as $k=>$v){
  1468. $a0 = Db::name($this->table)
  1469. ->where($map)
  1470. ->where('org_id',$v['id'])
  1471. ->column('id');
  1472. if(empty($a0)){
  1473. $k0 = 0;
  1474. }else{
  1475. $k0= Db::name('dinner_order_item')
  1476. ->alias('a')
  1477. ->where('a.is_refuse',0)
  1478. ->where('a.order_id','in',$a0)
  1479. ->sum('a.price');
  1480. }
  1481. $b0+=$k0;
  1482. $a1 = Db::name($this->table)
  1483. ->where($map)
  1484. ->where('org_id',$v['id'])
  1485. ->where('order_type',1)
  1486. ->column('id');
  1487. if(empty($a1)){
  1488. $k1 = 0;
  1489. }else{
  1490. $k1 = Db::name('dinner_order_item')
  1491. ->alias('a')
  1492. ->where('a.is_refuse',0)
  1493. ->where('a.order_id','in',$a1)
  1494. ->sum('a.price');
  1495. }
  1496. $b1+=$k1;
  1497. $k2 = round($k1*0.006,2);
  1498. $b2+=$k2;
  1499. $a3 = Db::name($this->table)
  1500. ->where($map)
  1501. ->where('org_id',$v['id'])
  1502. ->where('order_type',2)
  1503. ->column('id');
  1504. if(empty($a3)){
  1505. $k3 = 0;
  1506. }else{
  1507. $k3 = Db::name('dinner_order_item')
  1508. ->alias('a')
  1509. ->where('a.is_refuse',0)
  1510. ->where('a.order_id','in',$a3)
  1511. ->sum('a.price');
  1512. }
  1513. $b3+=$k3;
  1514. $a4 = Db::name($this->table)
  1515. ->where($map)
  1516. ->where('org_id',$v['id'])
  1517. ->where('order_type',3)
  1518. ->column('id');
  1519. if(empty($a1)){
  1520. $k4 = 0;
  1521. }else{
  1522. $k4 = Db::name('dinner_order_item')
  1523. ->alias('a')
  1524. ->where('a.is_refuse',0)
  1525. ->where('a.order_id','in',$a4)
  1526. ->sum('a.price');
  1527. }
  1528. $b4+=$k4;
  1529. $a[] = [
  1530. 'title'=>$v['name'],
  1531. 'a0'=>$k0,
  1532. 'a1'=>$k1,
  1533. 'a2'=>$k2,
  1534. 'a3'=>$k3,
  1535. 'a4'=>$k4,
  1536. ];
  1537. }
  1538. $b = [
  1539. 'b0'=>$b0,
  1540. 'b1'=>$b1,
  1541. 'b2'=>$b2,
  1542. 'b3'=>$b3,
  1543. 'b4'=>$b4,
  1544. ];
  1545. }
  1546. return ['a'=>$a,'b'=>$b];
  1547. }
  1548. public function exportorgcasdinner(){
  1549. $start = input('start',date('Y-m-d'),'trim');
  1550. $end = input('end',date('Y-m-d'),'trim');
  1551. $type = input('type',0);
  1552. $orgId = input('org_id',0);
  1553. $data = $this->orgcasdinnerData($start,$end,$type,$orgId);
  1554. //实例化PHPExcel类
  1555. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  1556. $objPHPExcel = new \PHPExcel();
  1557. //激活当前的sheet表
  1558. $sheet = $objPHPExcel->setActiveSheetIndex(0);
  1559. //设置表格头(即excel表格的第一行)
  1560. $objPHPExcel->setActiveSheetIndex(0)
  1561. ->setCellValue('A1', $type==0?'订餐日期':'项目')
  1562. ->setCellValue('B1', '订单金额(元)')
  1563. ->setCellValue('C1', '微信支付(元)')
  1564. ->setCellValue('D1', '微信手续费0.6%(元)')
  1565. ->setCellValue('E1', '食堂卡(元)')
  1566. ->setCellValue('F1', '现金(元)');
  1567. // 设置表格头水平居中
  1568. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  1569. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1570. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  1571. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1572. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  1573. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1574. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  1575. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1576. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  1577. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1578. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  1579. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1580. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  1581. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1582. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  1583. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1584. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  1585. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1586. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  1587. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1588. //设置列水平居中
  1589. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  1590. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1591. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  1592. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1593. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  1594. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1595. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  1596. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1597. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  1598. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1599. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  1600. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1601. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  1602. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1603. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  1604. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1605. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  1606. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1607. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  1608. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1609. //设置单元格宽度
  1610. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  1611. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  1612. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  1613. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  1614. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  1615. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  1616. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  1617. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  1618. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
  1619. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
  1620. $lists = $data['a'];
  1621. //循环刚取出来的数组,将数据逐一添加到excel表格。
  1622. for ($i = 0; $i < count($lists); $i++) {
  1623. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['title']);
  1624. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['a0']);
  1625. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['a1']);
  1626. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['a2']);
  1627. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['a3']);
  1628. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['a4']);
  1629. }
  1630. $objPHPExcel->getActiveSheet()->setCellValue('A' . (count($lists) + 2), '合计:');
  1631. $objPHPExcel->getActiveSheet()->setCellValue('B' . (count($lists) + 2), $data['b']['b0']);
  1632. $objPHPExcel->getActiveSheet()->setCellValue('C' . (count($lists) + 2), $data['b']['b1']);
  1633. $objPHPExcel->getActiveSheet()->setCellValue('D' . (count($lists) + 2), $data['b']['b2']);
  1634. $objPHPExcel->getActiveSheet()->setCellValue('E' . (count($lists) + 2), $data['b']['b3']);
  1635. $objPHPExcel->getActiveSheet()->setCellValue('F' . (count($lists) + 2), $data['b']['b4']);
  1636. //设置保存的Excel表格名称
  1637. $filename = '各项目财务报表' . date('YmdHis', time()) . '.xls';
  1638. //设置浏览器窗口下载表格
  1639. ob_end_clean();
  1640. header("Content-Type: application/force-download");
  1641. header("Content-Type: application/octet-stream");
  1642. header("Content-Type: application/download");
  1643. header('Content-Disposition:inline;filename="' . $filename);
  1644. //生成excel文件
  1645. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1646. //下载文件在浏览器窗口
  1647. return $objWriter->save('php://output');
  1648. }
  1649. public function refund($id=0){
  1650. $info = Db::name('dinner_order')->where('org_id',$this->orgId)->where('id',$id)->find();
  1651. if(request()->isPost()){
  1652. if(!$info){
  1653. $this->error('订单不存在');
  1654. }
  1655. if($info['cate'] != 1){
  1656. $this->error('特殊套餐才能退餐');
  1657. }
  1658. $days = input('days','','trim');
  1659. $remark = input('remark','','trim');
  1660. if(!$days){
  1661. $this->error('请选择退餐日期');
  1662. }
  1663. $days = explode(',',$days);
  1664. $olddays = Db::name('dinner_order_item')->where('order_id',$id)->where('is_refuse',0)->group('day')->distinct(true)->column('day');
  1665. $olddays = $olddays?$olddays:[];
  1666. foreach ($days as $v){
  1667. if(!in_array($v,$olddays)){
  1668. $this->error($v.'已退餐,勿重复操作');
  1669. }
  1670. }
  1671. $dinners = Db::name('dinner_order_item')
  1672. ->where('order_id',$id)
  1673. ->where('day','in',$days)
  1674. ->where('is_refuse',0)
  1675. ->select();
  1676. Db::startTrans();
  1677. try{
  1678. Db::name('dinner_order_item')
  1679. ->where('order_id',$id)
  1680. ->where('day','in',$days)
  1681. ->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i:s')]);
  1682. $rid = Db::name('dinner_order_refuse')->insertGetId([
  1683. 'dinner_order_id'=>$id
  1684. ,'refuse_price'=>0
  1685. ,'refuse_y'=>date('Y')
  1686. ,'refuse_ymd'=>date('Ymd')
  1687. ,'refuse_ym'=>date('Ym')
  1688. ,'created_user_id'=>$this->userId
  1689. ,'create_time'=>date('Y-m-d H:i:s')
  1690. ,'refuse_w'=>date('Y').date('W')
  1691. ,'order_type'=>$info['order_type']
  1692. ,'is_replace'=>$info['is_replace']
  1693. ,'org_id'=>$this->orgId
  1694. ,'status'=>5
  1695. ,'op_user_id'=>$this->userId
  1696. ,'op_time'=>date('Y-m-d H:i:s')
  1697. ,'refuse_remark'=>$remark
  1698. ]);
  1699. if(!$rid){
  1700. \exception('操作失败');
  1701. }
  1702. $items = [];
  1703. foreach ($dinners as $k=>$v){
  1704. $items[] = [
  1705. 'dinner_id' => $v['dinner_id'],
  1706. 'order_refuse_id' => $rid,
  1707. 'price'=>$v['price'],
  1708. 'num'=>$v['num'],
  1709. 'dinner_type_id'=>$v['dinner_type_id'],
  1710. 'day' => $v['day'],
  1711. 'group_id' => $v['group_id']
  1712. ];
  1713. }
  1714. $rr = Db::name('dinner_order_refuse_item')->insertAll($items);
  1715. if(!$rr){
  1716. \exception('操作失败');
  1717. }
  1718. Db::commit();
  1719. }catch (Exception $e){
  1720. Db::rollback();
  1721. $this->error($e->getMessage());
  1722. }
  1723. $this->success('操作成功');
  1724. }else{
  1725. if(!$info){
  1726. exit('订单不存在');
  1727. }
  1728. if($info['cate'] != 1){
  1729. exit('特殊套餐才能退餐');
  1730. }
  1731. $days = Db::name('dinner_order_item')->where('order_id',$id)->where('is_refuse',0)->group('day')->distinct(true)->column('day');
  1732. $news = [];
  1733. foreach ($days as $v){
  1734. $news[] = ['id'=>$v,'title'=>$v];
  1735. }
  1736. $this->assign('days',$news);
  1737. $this->assign('id',$id);
  1738. return $this->fetch();
  1739. }
  1740. }
  1741. }