DinnerOrders.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class DinnerOrders extends Model
  8. {
  9. public function lists(){
  10. $page = input('page/d',1);
  11. $size = input('size/d',10);
  12. $map[] = ['b.del','=',0];
  13. $title = input('title','','trim');
  14. if($title != ''){
  15. $map[] = ['b.name','like','%'.$title.'%'];
  16. }
  17. $status = input('status/d',-1);
  18. if($status >= 0){
  19. $map[] = ['b.status','=',$status];
  20. }
  21. $cateId = input('cateId/d',0);
  22. if($cateId > 0){
  23. $map[] = ['a.cate_id','=',$cateId];
  24. }
  25. $companyId = input('companyId/d',0);
  26. if($companyId > 0){
  27. $map[] = ['b.company_id','=',$companyId];
  28. }
  29. $day = input('day','','trim');
  30. if($day){
  31. $map[] = ['b.day','=',$day];
  32. }
  33. $lists = Db::name('dinner_order_goods')
  34. ->alias('a')
  35. ->join('dinner_orders b','a.order_id = b.id')
  36. ->join('dinner_company d','d.id = b.company_id')
  37. ->join('dinner_user u','u.id = b.user_id')
  38. ->where($map)
  39. ->page($page,$size)
  40. ->order('b.id desc')
  41. ->group('a.order_id')
  42. ->distinct(true)
  43. ->field('b.*,d.title as company,u.phone,sum(a.price*a.nums) as money')
  44. ->select();
  45. $lists = $lists?$lists:[];
  46. foreach ($lists as $k=>$v){
  47. $m = [];
  48. if($cateId > 0){
  49. $m[] = ['a.cate_id','=',$cateId];
  50. }
  51. $m[] = ['a.order_id','=',$v['id']];
  52. $goods = Db::name('dinner_order_goods')
  53. ->alias('a')
  54. ->join('dinner_cate c','c.id = a.cate_id')
  55. ->join('dinner e','e.id = a.dinner_id')
  56. ->where($m)
  57. ->order('a.cate_id asc')
  58. ->field('a.*,c.title as cate_title,e.title as dinner_title,c.color')
  59. ->select();
  60. $lists[$k]['goods'] = $goods?$goods:[];
  61. }
  62. $total = Db::name('dinner_order_goods')
  63. ->alias('a')
  64. ->join('dinner_orders b','a.order_id = b.id')
  65. ->join('dinner_company d','d.id = b.company_id')
  66. ->join('dinner_user u','u.id = b.user_id')
  67. ->group('a.order_id')
  68. // ->distinct(true)
  69. // ->fetchSql(true)
  70. ->where($map)->count();
  71. // dump($lists);
  72. // halt($total);
  73. $totalMoney = Db::name('dinner_order_goods')
  74. ->alias('a')
  75. ->join('dinner_orders b','a.order_id = b.id')
  76. ->join('dinner_company d','d.id = b.company_id')
  77. ->join('dinner_user u','u.id = b.user_id')
  78. ->where($map)
  79. ->sum('a.price*a.nums');
  80. $goods = Db::name('dinner_order_goods')
  81. ->alias('a')
  82. ->join('dinner_orders b','a.order_id = b.id')
  83. ->join('dinner_company d','d.id = b.company_id')
  84. ->join('dinner_user u','u.id = b.user_id')
  85. ->join('dinner_cate c','c.id = a.cate_id')
  86. ->join('dinner e','e.id = a.dinner_id')
  87. ->where($map)
  88. ->group('a.dinner_id')
  89. ->distinct(true)
  90. ->field('a.dinner_id,sum(a.nums) as tnums,e.title,c.title as cate_title')
  91. ->select();
  92. $goods = $goods?$goods:[];
  93. $garr = [];
  94. foreach ($goods as $k=>$v){
  95. $garr[] = '['.$v['cate_title'].']'.$v['title'].'*'.$v['tnums'];
  96. }
  97. $data = [
  98. 'total_money' => $totalMoney,
  99. 'total' => $total,
  100. 'list' => $lists,
  101. 'goods' => implode(';',$garr)
  102. ];
  103. return $data;
  104. }
  105. public function applys($userId){
  106. $page = input('page/d',1);
  107. $size = input('size/d',10);
  108. $map[] = ['b.del','=',0];
  109. $title = input('title','','trim');
  110. if($title != ''){
  111. $map[] = ['b.name','like','%'.$title.'%'];
  112. }
  113. $status = input('status/d',-1);
  114. if($status >= 0){
  115. $map[] = ['b.status','=',$status];
  116. }
  117. $cateId = input('cateId/d',0);
  118. if($cateId > 0){
  119. $map[] = ['a.cate_id','=',$cateId];
  120. }
  121. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  122. if($companyIds){
  123. $map[] = ['b.company_id','in',$companyIds];
  124. }else{
  125. $map[] = ['b.company_id','=',-1];
  126. }
  127. $day = input('day','','trim');
  128. if($day){
  129. $map[] = ['b.day','=',$day];
  130. }
  131. $lists = Db::name('dinner_order_goods')
  132. ->alias('a')
  133. ->join('dinner_orders b','a.order_id = b.id')
  134. ->join('dinner_company d','d.id = b.company_id')
  135. ->join('dinner_user u','u.id = b.user_id')
  136. ->where($map)
  137. ->page($page,$size)
  138. ->order('b.id desc')
  139. ->group('a.order_id')
  140. ->distinct(true)
  141. ->field('b.*,d.title as company,u.phone,sum(a.price*a.nums) as money')
  142. ->select();
  143. $lists = $lists?$lists:[];
  144. foreach ($lists as $k=>$v){
  145. $m = [];
  146. if($cateId > 0){
  147. $m[] = ['a.cate_id','=',$cateId];
  148. }
  149. $m[] = ['a.order_id','=',$v['id']];
  150. $goods = Db::name('dinner_order_goods')
  151. ->alias('a')
  152. ->join('dinner_cate c','c.id = a.cate_id')
  153. ->join('dinner e','e.id = a.dinner_id')
  154. ->where($m)
  155. ->order('a.cate_id asc')
  156. ->field('a.*,c.title as cate_title,e.title as dinner_title,c.color')
  157. ->select();
  158. $lists[$k]['goods'] = $goods?$goods:[];
  159. }
  160. $total = Db::name('dinner_order_goods')
  161. ->alias('a')
  162. ->join('dinner_orders b','a.order_id = b.id')
  163. ->join('dinner_company d','d.id = b.company_id')
  164. ->join('dinner_user u','u.id = b.user_id')
  165. ->group('a.order_id')
  166. // ->distinct(true)
  167. ->where($map)->count();
  168. $totalMoney = Db::name('dinner_order_goods')
  169. ->alias('a')
  170. ->join('dinner_orders b','a.order_id = b.id')
  171. ->join('dinner_company d','d.id = b.company_id')
  172. ->join('dinner_user u','u.id = b.user_id')
  173. ->where($map)
  174. ->sum('a.price*a.nums');
  175. $data = [
  176. 'total_money' => $totalMoney,
  177. 'total' => $total,
  178. 'list' => $lists
  179. ];
  180. return $data;
  181. }
  182. public function applyexport($userId){
  183. $map[] = ['b.del','=',0];
  184. $title = input('title','','trim');
  185. if($title != ''){
  186. $map[] = ['b.name','like','%'.$title.'%'];
  187. }
  188. $status = input('status/d',-1);
  189. if($status >= 0){
  190. $map[] = ['b.status','=',$status];
  191. }
  192. $cateId = input('cateId/d',0);
  193. if($cateId > 0){
  194. $map[] = ['a.cate_id','=',$cateId];
  195. }
  196. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  197. if($companyIds){
  198. $map[] = ['b.company_id','in',$companyIds];
  199. }else{
  200. $map[] = ['b.company_id','=',-1];
  201. }
  202. $day = input('day','','trim');
  203. if($day){
  204. $map[] = ['b.day','=',$day];
  205. }
  206. $lists = Db::name('dinner_order_goods')
  207. ->alias('a')
  208. ->join('dinner_orders b','a.order_id = b.id')
  209. ->join('dinner_company d','d.id = b.company_id')
  210. ->join('dinner_user u','u.id = b.user_id')
  211. ->where($map)
  212. ->order('b.id desc')
  213. ->group('a.order_id')
  214. ->distinct(true)
  215. ->field('b.*,d.title as company,u.phone,sum(a.price*a.nums) as money')
  216. ->select();
  217. $lists = $lists?$lists:[];
  218. foreach ($lists as $k=>$v){
  219. $m = [];
  220. if($cateId > 0){
  221. $m[] = ['a.cate_id','=',$cateId];
  222. }
  223. $m[] = ['a.order_id','=',$v['id']];
  224. $goods = Db::name('dinner_order_goods')
  225. ->alias('a')
  226. ->join('dinner_cate c','c.id = a.cate_id')
  227. ->join('dinner e','e.id = a.dinner_id')
  228. ->where($m)
  229. ->order('a.cate_id asc')
  230. ->field('a.*,c.title as cate_title,e.title as dinner_title,c.color')
  231. ->select();
  232. $goods = $goods?$goods:[];
  233. if($v['status'] == 0){
  234. $lists[$k]['status_text'] = '已取消';
  235. }else if($v['status'] == 1){
  236. $lists[$k]['status_text'] = '待确认';
  237. }else{
  238. $lists[$k]['status_text'] = '已确认';
  239. }
  240. $strs = [];
  241. foreach ($goods as $kk=>$vv){
  242. $strs[] = '['.$vv['cate_title'].']'.$vv['dinner_title'] .' * '.$vv['nums'];
  243. }
  244. $lists[$k]['goods'] = $strs?implode(';',$strs):'';
  245. }
  246. $columns[] = ["title" => "单位","key" => "company"];
  247. $columns[] = ["title" => "姓名","key" => "name"];
  248. $columns[] = ["title" => "手机号","key" => "phone"];
  249. $columns[] = ["title" => "配送日期","key" => "day"];
  250. $columns[] = ["title" => "金额","key" => "money"];
  251. $columns[] = ["title" => "状态","key" => "statusText"];
  252. $columns[] = ["title" => "购买商品","key" => "goods"];
  253. $columns[] = ["title" => "备注","key" => "remark"];
  254. $data = [
  255. 'columns' => $columns,
  256. 'list' => $lists
  257. ];
  258. return $data;
  259. }
  260. public function agree($id,$userId){
  261. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  262. if(!$companyIds){
  263. HelpHander::error('无权限操作');
  264. }
  265. $info = Db::name('dinner_orders')->where('del',0)->where('id',$id)->find();
  266. if(!$info){
  267. HelpHander::error('订单不存在');
  268. }
  269. if($info['status'] != 1){
  270. HelpHander::error('订单已处理');
  271. }
  272. if(!in_array($info['company_id'],$companyIds)){
  273. HelpHander::error('无权限操作');
  274. }
  275. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  276. if($vals){
  277. $limitTime = date('Y-m-d').' '.$vals;
  278. $curTime = date('Y-m-d H:i');
  279. if($curTime > $limitTime){
  280. HelpHander::error('审核时间已过');
  281. }
  282. }
  283. $ret = Db::name('dinner_orders')->where('id',$id)->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
  284. if(!$ret){
  285. HelpHander::error('操作失败');
  286. }
  287. return true;
  288. }
  289. public function agreeall($userId){
  290. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  291. if(!$companyIds){
  292. HelpHander::error('无权限操作');
  293. }
  294. $ids = Db::name('dinner_orders')
  295. ->where('del',0)
  296. ->where('company_id','in',$companyIds)
  297. ->where('status',1)
  298. ->column('id');
  299. if(!$ids){
  300. HelpHander::error('无可处理的订单');
  301. }
  302. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  303. if($vals){
  304. $limitTime = date('Y-m-d').' '.$vals;
  305. $curTime = date('Y-m-d H:i');
  306. if($curTime > $limitTime){
  307. HelpHander::error('审核时间已过');
  308. }
  309. }
  310. $ret = Db::name('dinner_orders')->where('id','in',$ids)->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
  311. if(!$ret){
  312. HelpHander::error('操作失败');
  313. }
  314. return true;
  315. }
  316. public function del($id,$userId){
  317. $ret = $this->where('id',$id)->update(['del'=>1,'del_user_id'=>$userId,'del_time'=>date('Y-m-d H:i:s')]);
  318. if(!$ret){
  319. HelpHander::error('删除失败');
  320. }
  321. return true;
  322. }
  323. public function export(){
  324. $map[] = ['b.del','=',0];
  325. $title = input('title','','trim');
  326. if($title != ''){
  327. $map[] = ['b.name','like','%'.$title.'%'];
  328. }
  329. $status = input('status/d',-1);
  330. if($status >= 0){
  331. $map[] = ['b.status','=',$status];
  332. }
  333. $cateId = input('cateId/d',0);
  334. if($cateId > 0){
  335. $map[] = ['a.cate_id','=',$cateId];
  336. }
  337. $companyId = input('companyId/d',0);
  338. if($companyId > 0){
  339. $map[] = ['b.company_id','=',$companyId];
  340. }
  341. $day = input('day','','trim');
  342. if($day){
  343. $map[] = ['b.day','=',$day];
  344. }
  345. $lists = Db::name('dinner_order_goods')
  346. ->alias('a')
  347. ->join('dinner_orders b','a.order_id = b.id')
  348. ->join('dinner_company d','d.id = b.company_id')
  349. ->join('dinner_user u','u.id = b.user_id')
  350. ->where($map)
  351. ->order('b.id desc')
  352. ->group('a.order_id')
  353. ->distinct(true)
  354. ->field('b.*,d.title as company,u.phone,sum(a.price*a.nums) as money')
  355. ->select();
  356. $lists = $lists?$lists:[];
  357. foreach ($lists as $k=>$v){
  358. $m = [];
  359. if($cateId > 0){
  360. $m[] = ['a.cate_id','=',$cateId];
  361. }
  362. $m[] = ['a.order_id','=',$v['id']];
  363. $goods = Db::name('dinner_order_goods')
  364. ->alias('a')
  365. ->join('dinner_cate c','c.id = a.cate_id')
  366. ->join('dinner e','e.id = a.dinner_id')
  367. ->where($m)
  368. ->order('a.cate_id asc')
  369. ->field('a.*,c.title as cate_title,e.title as dinner_title,e.remark as dinner_remark')
  370. ->select();
  371. $goods = $goods?$goods:[];
  372. if($v['status'] == 0){
  373. $lists[$k]['status_text'] = '已取消';
  374. }else if($v['status'] == 1){
  375. $lists[$k]['status_text'] = '待确认';
  376. }else{
  377. $lists[$k]['status_text'] = '已确认';
  378. }
  379. $strs = [];
  380. foreach ($goods as $kk=>$vv){
  381. if($vv['dinner_remark']){
  382. $strs[] = '['.$vv['cate_title'].']'.$vv['dinner_title'] .'('.$vv['dinner_remark'].')'.' * '.$vv['nums'];
  383. }else{
  384. $strs[] = '['.$vv['cate_title'].']'.$vv['dinner_title'] .' * '.$vv['nums'];
  385. }
  386. }
  387. $lists[$k]['goods'] = $strs?implode(';',$strs):'';
  388. }
  389. $columns[] = ["title" => "单位","key" => "company"];
  390. $columns[] = ["title" => "姓名","key" => "name"];
  391. $columns[] = ["title" => "手机号","key" => "phone"];
  392. $columns[] = ["title" => "配送日期","key" => "day"];
  393. $columns[] = ["title" => "金额","key" => "money"];
  394. $columns[] = ["title" => "状态","key" => "statusText"];
  395. $columns[] = ["title" => "购买商品","key" => "goods"];
  396. $columns[] = ["title" => "备注","key" => "remark"];
  397. $data = [
  398. 'columns' => $columns,
  399. 'list' => $lists
  400. ];
  401. return $data;
  402. }
  403. public function book($data){
  404. $result = validate('DinnerOrders')->check($data,[],'');
  405. if(true !== $result){
  406. HelpHander::error(validate('DinnerOrders')->getError());
  407. }
  408. $data['order_sn'] = get_unique_id('D');
  409. $data['status'] = 1;
  410. $data['create_time'] = date('Y-m-d H:i:s');
  411. $user = Db::name('dinner_user')->where('id',$data['user_id'])->find();
  412. $data['name'] = $user['name'];
  413. $data['company_id'] = $user['company_id'];
  414. $amount = 0;
  415. $goods = json_decode($data['goods'],true);
  416. unset($data['goods']);
  417. $day = '';
  418. foreach ($goods as $k=>$v){
  419. $info = Db::name('dinner')->where('id',$v['dinner_id'])->find();
  420. $goods[$k]['price'] = $info['price'];
  421. $goods[$k]['cate_id'] = $info['cate_id'];
  422. $amount = $amount + $v['nums']*$info['price'];
  423. $day = $info['day'];
  424. }
  425. $data['amount'] = round($amount,2);
  426. $data['day'] = $day;
  427. Db::startTrans();
  428. try {
  429. $orderId = Db::name('dinner_orders')->insertGetId($data);
  430. if(!$orderId){
  431. \exception('操作失败');
  432. }
  433. foreach ($goods as $k=>$v){
  434. $goods[$k]['order_id'] = $orderId;
  435. }
  436. $res = Db::name('dinner_order_goods')->insertAll($goods);
  437. if($res != count($goods)){
  438. \exception('操作失败');
  439. }
  440. Db::commit();
  441. }catch (Exception $e){
  442. Db::rollback();
  443. HelpHander::error('操作失败');
  444. }
  445. return true;
  446. }
  447. public function delgoods($id,$userId){
  448. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  449. if(!$companyIds){
  450. HelpHander::error('无权限操作');
  451. }
  452. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  453. if($vals){
  454. $limitTime = date('Y-m-d').' '.$vals;
  455. $curTime = date('Y-m-d H:i');
  456. if($curTime > $limitTime){
  457. HelpHander::error('审核时间已过');
  458. }
  459. }
  460. $info = Db::name('dinner_order_goods')
  461. ->alias('a')
  462. ->join('dinner_orders b','b.id = a.order_id')
  463. ->where('a.id',$id)
  464. ->where('b.del',0)
  465. ->field('b.*,a.nums,a.price')
  466. ->find();
  467. if(!$info){
  468. HelpHander::error('订单不存在');
  469. }
  470. if($info['status'] != 1){
  471. HelpHander::error('订单已处理');
  472. }
  473. if(!in_array($info['company_id'],$companyIds)){
  474. HelpHander::error('无权限操作');
  475. }
  476. Db::startTrans();
  477. try {
  478. $ret = Db::name('dinner_order_goods')->where('id',$id)->delete();
  479. if(!$ret){
  480. \exception('操作失败');
  481. }
  482. $amount = round($info['amount'] - $info['price']*$info['nums'],2);
  483. $res = Db::name('dinner_orders')->where('id',$info['id'])->update([
  484. 'amount'=>$amount,
  485. 'update_time'=>date('Y-m-d H:i:s')
  486. ]);
  487. if(!$res){
  488. \exception('操作失败');
  489. }
  490. Db::commit();
  491. }catch (Exception $e){
  492. Db::rollback();
  493. HelpHander::error('操作失败');
  494. }
  495. return true;
  496. }
  497. public function addgoods($orderId,$dinnerId,$nums,$userId){
  498. if($dinnerId <= 0){
  499. HelpHander::error('参数错误');
  500. }
  501. if($nums <= 0){
  502. HelpHander::error('商品数量错误');
  503. }
  504. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  505. if(!$companyIds){
  506. HelpHander::error('无权限操作');
  507. }
  508. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  509. if($vals){
  510. $limitTime = date('Y-m-d').' '.$vals;
  511. $curTime = date('Y-m-d H:i');
  512. if($curTime > $limitTime){
  513. HelpHander::error('审核时间已过');
  514. }
  515. }
  516. $info = Db::name('dinner_orders')
  517. ->where('id',$orderId)
  518. ->where('del',0)
  519. ->find();
  520. if(!$info){
  521. HelpHander::error('订单不存在');
  522. }
  523. if($info['status'] != 1){
  524. HelpHander::error('订单已处理');
  525. }
  526. if(!in_array($info['company_id'],$companyIds)){
  527. HelpHander::error('无权限操作');
  528. }
  529. $dinner = Db::name('dinner')->where('id',$dinnerId)->where('del',0)->where('enable',1)->find();
  530. if(!$dinner){
  531. HelpHander::error('商品已下架');
  532. }
  533. $og = Db::name('dinner_order_goods')->where('order_id',$orderId)->where('dinner_id',$dinnerId)->find();
  534. if($og){
  535. HelpHander::error('商品已存在');
  536. }
  537. Db::startTrans();
  538. try {
  539. $ret = Db::name('dinner_order_goods')->insert([
  540. 'order_id' => $orderId,
  541. 'dinner_id' => $dinnerId,
  542. 'nums' => $nums,
  543. 'price' => $dinner['price'],
  544. 'cate_id' => $dinner['cate_id']
  545. ]);
  546. if(!$ret){
  547. \exception('操作失败');
  548. }
  549. $amount = round($info['amount'] + $dinner['price']*$nums,2);
  550. $res = Db::name('dinner_orders')->where('id',$info['id'])->update([
  551. 'amount'=>$amount,
  552. 'update_time'=>date('Y-m-d H:i:s')
  553. ]);
  554. if(!$res){
  555. \exception('操作失败');
  556. }
  557. Db::commit();
  558. }catch (Exception $e){
  559. Db::rollback();
  560. HelpHander::error('操作失败');
  561. }
  562. return true;
  563. }
  564. public function editgoods($id,$nums,$userId){
  565. if($nums <= 0){
  566. HelpHander::error('商品数量必须大于0');
  567. }
  568. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  569. if(!$companyIds){
  570. HelpHander::error('无权限操作');
  571. }
  572. $vals = Db::name('config')->where('name','dinner_apply_limit')->value('value');
  573. if($vals){
  574. $limitTime = date('Y-m-d').' '.$vals;
  575. $curTime = date('Y-m-d H:i');
  576. if($curTime > $limitTime){
  577. HelpHander::error('审核时间已过');
  578. }
  579. }
  580. $info = Db::name('dinner_order_goods')
  581. ->alias('a')
  582. ->join('dinner_orders b','b.id = a.order_id')
  583. ->where('a.id',$id)
  584. ->where('b.del',0)
  585. ->field('b.*,a.nums,a.price')
  586. ->find();
  587. if(!$info){
  588. HelpHander::error('订单不存在');
  589. }
  590. if($info['status'] != 1){
  591. HelpHander::error('订单已处理');
  592. }
  593. if(!in_array($info['company_id'],$companyIds)){
  594. HelpHander::error('无权限操作');
  595. }
  596. if($info['nums'] == $nums){
  597. HelpHander::error('未做任何修改');
  598. }
  599. Db::startTrans();
  600. try {
  601. $ret = Db::name('dinner_order_goods')->where('id',$id)->setField('nums',$nums);
  602. if(!$ret){
  603. \exception('操作失败');
  604. }
  605. $lnums = $nums - $info['nums'];
  606. $amount = round($info['amount'] + $info['price']*$lnums,2);
  607. $res = Db::name('dinner_orders')->where('id',$info['id'])->update([
  608. 'amount'=>$amount,
  609. 'update_time'=>date('Y-m-d H:i:s')
  610. ]);
  611. if(!$res){
  612. \exception('操作失败');
  613. }
  614. Db::commit();
  615. }catch (Exception $e){
  616. Db::rollback();
  617. HelpHander::error('操作失败');
  618. }
  619. return true;
  620. }
  621. public function orders($page,$size,$userId){
  622. $lists = Db::name('dinner_orders')
  623. ->where('user_id',$userId)
  624. ->where('del',0)
  625. ->order('id desc')
  626. ->page($page,$size)
  627. ->select();
  628. $lists = $lists?$lists:[];
  629. foreach ($lists as $k=>$v){
  630. $goods = Db::name('dinner_order_goods')
  631. ->alias('a')
  632. ->join('dinner_cate c','c.id = a.cate_id')
  633. ->join('dinner e','e.id = a.dinner_id')
  634. ->where('a.order_id',$v['id'])
  635. ->order('a.cate_id asc')
  636. ->field('a.*,c.title as cate_title,e.title as dinner_title,c.color,e.img,e.remark')
  637. ->select();
  638. $goods = $goods?$goods:[];
  639. foreach ($goods as $kk=>$vv){
  640. $goods[$kk]['img'] = $vv['img']?$vv['img']:request()->domain().'/static/dinner.png';
  641. }
  642. $lists[$k]['goods'] = $goods;
  643. $lists[$k]['cancel'] = 0; // 可取消
  644. $curTime = time();
  645. $limitTime = strtotime($v['day'].' 08:30:00');
  646. if($curTime > $limitTime){
  647. $lists[$k]['cancel'] = 1; //不可取消
  648. }
  649. }
  650. return $lists;
  651. }
  652. public function pclists(){
  653. $cateId = input('cateId/d',0);
  654. if($cateId > 0){
  655. $map[] = ['a.cate_id','=',$cateId];
  656. }
  657. $companyId = input('companyId/d',0);
  658. if($companyId > 0){
  659. $map[] = ['b.company_id','=',$companyId];
  660. }
  661. $day = input('day','','trim');
  662. if($day){
  663. $map[] = ['b.day','=',$day];
  664. }else{
  665. $map[] = ['b.day','=',0];
  666. }
  667. $map[] = ['b.status','=',2];
  668. $map[] = ['b.del','=',0];
  669. $lists = Db::name('dinner_order_goods')
  670. ->alias('a')
  671. ->join('dinner_orders b','a.order_id = b.id')
  672. ->join('dinner e','e.id = a.dinner_id')
  673. ->join('dinner_cate c','c.id = a.cate_id')
  674. ->where($map)
  675. ->order('a.cate_id asc,a.id asc')
  676. ->group('a.dinner_id')
  677. ->distinct(true)
  678. ->field('sum(a.nums) as tnums,a.dinner_id,e.title,c.title as cate_title')
  679. ->select();
  680. $lists = $lists?$lists:[];
  681. return $lists;
  682. }
  683. public function pcexport(){
  684. $cateId = input('cateId/d',0);
  685. if($cateId > 0){
  686. $map[] = ['a.cate_id','=',$cateId];
  687. }
  688. $companyId = input('companyId/d',0);
  689. if($companyId > 0){
  690. $map[] = ['b.company_id','=',$companyId];
  691. }
  692. $day = input('day','','trim');
  693. if($day){
  694. $map[] = ['b.day','=',$day];
  695. }else{
  696. $map[] = ['b.day','=',0];
  697. }
  698. $map[] = ['b.status','=',2];
  699. $map[] = ['b.del','=',0];
  700. $lists = Db::name('dinner_order_goods')
  701. ->alias('a')
  702. ->join('dinner_orders b','a.order_id = b.id')
  703. ->join('dinner e','e.id = a.dinner_id')
  704. ->join('dinner_cate c','c.id = a.cate_id')
  705. ->where($map)
  706. ->order('a.cate_id asc,a.id asc')
  707. ->group('a.dinner_id')
  708. ->distinct(true)
  709. ->field('sum(a.nums) as tnums,a.dinner_id,e.title,c.title as cate_title')
  710. ->select();
  711. $lists = $lists?$lists:[];
  712. $columns[] = ["title" => "类型","key" => "cateTitle"];
  713. $columns[] = ["title" => "菜品","key" => "title"];
  714. $columns[] = ["title" => "数量","key" => "tnums"];
  715. $data = [
  716. 'columns' => $columns,
  717. 'list' => $lists
  718. ];
  719. return $data;
  720. }
  721. //
  722. public function finance(){
  723. $page = input('page/d',1);
  724. $size = input('size/d',10);
  725. // $companyId = input('companyId/d',0);
  726. // if($companyId > 0){
  727. // $map[] = ['company_id','=',$companyId];
  728. // $map2[] = ['b.company_id','=',$companyId];
  729. // }
  730. $start = input('start','','trim');
  731. $end = input('end','','trim');
  732. if($start){
  733. $map[] = ['day','>=',$start];
  734. $map2[] = ['b.day','=',$start];
  735. }
  736. if($end){
  737. $map[] = ['day','<=',$end];
  738. $map2[] = ['b.day','=',$end];
  739. }
  740. $map[] = ['del','=',0];
  741. $map[] = ['status','=',2];
  742. $map2[] = ['b.del','=',0];
  743. $map2[] = ['b.status','=',2];
  744. $cates = Db::name('dinner_order_goods')
  745. ->alias('a')
  746. ->join('dinner_orders b','b.id = a.order_id')
  747. ->join('dinner_cate c','c.id = a.cate_id')
  748. ->where($map2)
  749. ->group('a.cate_id')
  750. ->distinct(true)
  751. ->field('c.id,c.title')
  752. ->order('id asc')
  753. ->select();
  754. $cates = $cates?$cates:[];
  755. $companys = Db::name('dinner_company')
  756. ->where('del',0)
  757. ->where('enable',1)
  758. ->order('sorts asc,id asc')
  759. ->select();
  760. $companys = $companys?$companys:[];
  761. $lists = Db::name('dinner_orders')
  762. ->where($map)
  763. ->group('day')
  764. ->distinct(true)
  765. ->field('day,sum(amount) as money')
  766. ->order('day desc')
  767. ->page($page,$size)
  768. ->select();
  769. $lists = $lists?$lists:[];
  770. foreach ($lists as $k=>$v){
  771. // 各分类
  772. foreach ($cates as $kk=>$vv){
  773. $m = $map2;
  774. $m[] = ['b.day','=',$v['day']];
  775. $m[] = ['a.cate_id','=',$vv['id']];
  776. $money = Db::name('dinner_order_goods')
  777. ->alias('a')
  778. ->join('dinner_orders b','b.id = a.order_id')
  779. ->where($m)
  780. ->sum('a.nums*a.price');
  781. $lists[$k]['money'.$vv['id']] = $money;
  782. }
  783. $lists[$k]['company_id'] = 0;
  784. $lists[$k]['day2'] = $v['day'];
  785. $lists[$k]['id'] = md5($v['day'].'|0');
  786. // 各单位
  787. $children = [];
  788. foreach ($companys as $ck=>$cv){
  789. $a = [];
  790. $a['day'] = $cv['title'];
  791. $a['company_id'] = $cv['id'];
  792. $a['day2'] = $v['day'];
  793. $lists[$k]['id'] = md5($v['day'].'|'.$cv['title']);
  794. $money = Db::name('dinner_orders')
  795. ->where('day',$v['day'])
  796. ->where('company_id',$cv['id'])
  797. ->where('status',2)
  798. ->where('del',0)
  799. ->sum('amount');
  800. $a['money'] = $money;
  801. // 各分类
  802. foreach ($cates as $kk=>$vv){
  803. $m = $map2;
  804. $m[] = ['b.company_id','=',$cv['id']];
  805. $m[] = ['b.day','=',$v['day']];
  806. $m[] = ['a.cate_id','=',$vv['id']];
  807. $money = Db::name('dinner_order_goods')
  808. ->alias('a')
  809. ->join('dinner_orders b','b.id = a.order_id')
  810. ->where($m)
  811. ->sum('a.nums*a.price');
  812. $a['money'.$vv['id']] = $money;
  813. }
  814. $children[] = $a;
  815. }
  816. if($children){
  817. $lists[$k]['children'] = $children;
  818. }
  819. }
  820. $total = Db::name('dinner_orders')
  821. ->where($map)
  822. ->group('day')
  823. // ->distinct(true)
  824. ->count();
  825. $totalMoney = Db::name('dinner_orders')
  826. ->where($map)
  827. ->sum('amount');
  828. $data = [
  829. 'total_money' => $totalMoney,
  830. 'cates' => $cates,
  831. 'total' => $total,
  832. 'list' => $lists
  833. ];
  834. return $data;
  835. }
  836. public function finexport(){
  837. $companyId = input('companyId/d',0);
  838. if($companyId > 0){
  839. $map[] = ['a.company_id','=',$companyId];
  840. }
  841. $day = input('day','','trim');
  842. if($day){
  843. $map[] = ['a.day','=',$day];
  844. }else{
  845. $map[] = ['a.day','=',0];
  846. }
  847. $map[] = ['a.status','=',2];
  848. $map[] = ['a.del','=',0];
  849. $lists = Db::name('dinner_orders')
  850. ->alias('a')
  851. ->join('dinner_company b','b.id = a.company_id')
  852. ->join('dinner_user c','c.id = a.user_id')
  853. ->where($map)
  854. ->field('a.*,b.title as company,c.phone')
  855. ->order('a.id asc')
  856. ->select();
  857. foreach ($lists as $k=>$v){
  858. $goods = Db::name('dinner_order_goods')
  859. ->alias('a')
  860. ->join('dinner_cate c','c.id = a.cate_id')
  861. ->join('dinner e','e.id = a.dinner_id')
  862. ->where('a.order_id',$v['id'])
  863. ->order('a.cate_id asc')
  864. ->field('a.*,c.title as cate_title,e.title as dinner_title')
  865. ->select();
  866. $goods = $goods?$goods:[];
  867. $strs = [];
  868. foreach ($goods as $kk=>$vv){
  869. $strs[] = '['.$vv['cate_title'].']'.$vv['dinner_title'] .' * '.$vv['nums'];
  870. }
  871. $lists[$k]['goods'] = $strs?implode(';',$strs):'';
  872. }
  873. $columns[] = ["title" => "单位","key" => "company"];
  874. $columns[] = ["title" => "姓名","key" => "name"];
  875. $columns[] = ["title" => "手机号","key" => "phone"];
  876. $columns[] = ["title" => "配送日期","key" => "day"];
  877. $columns[] = ["title" => "金额","key" => "amount"];
  878. $columns[] = ["title" => "购买商品","key" => "goods"];
  879. $columns[] = ["title" => "备注","key" => "remark"];
  880. $data = [
  881. 'columns' => $columns,
  882. 'list' => $lists
  883. ];
  884. return $data;
  885. }
  886. public function cfinance($userId){
  887. $page = input('page/d',1);
  888. $size = input('size/d',10);
  889. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  890. if($companyIds){
  891. $map[] = ['company_id','in',$companyIds];
  892. $map2[] = ['b.company_id','in',$companyIds];
  893. }else{
  894. $map[] = ['company_id','=',-1];
  895. $map2[] = ['b.company_id','=',-1];
  896. }
  897. $start = input('start','','trim');
  898. $end = input('end','','trim');
  899. if($start){
  900. $map[] = ['day','>=',$start];
  901. $map2[] = ['b.day','=',$start];
  902. }
  903. if($end){
  904. $map[] = ['day','<=',$end];
  905. $map2[] = ['b.day','=',$end];
  906. }
  907. $map[] = ['del','=',0];
  908. $map[] = ['status','=',2];
  909. $map2[] = ['b.del','=',0];
  910. $map2[] = ['b.status','=',2];
  911. $cates = Db::name('dinner_order_goods')
  912. ->alias('a')
  913. ->join('dinner_orders b','b.id = a.order_id')
  914. ->join('dinner_cate c','c.id = a.cate_id')
  915. ->where($map2)
  916. ->group('a.cate_id')
  917. ->distinct(true)
  918. ->field('c.id,c.title')
  919. ->order('id asc')
  920. ->select();
  921. $cates = $cates?$cates:[];
  922. $lists = Db::name('dinner_orders')
  923. ->where($map)
  924. ->group('day')
  925. ->distinct(true)
  926. ->field('day,sum(amount) as money')
  927. ->order('day desc')
  928. ->page($page,$size)
  929. ->select();
  930. $lists = $lists?$lists:[];
  931. foreach ($lists as $k=>$v){
  932. // 各分类
  933. foreach ($cates as $kk=>$vv){
  934. $m = $map2;
  935. $m[] = ['a.cate_id','=',$vv['id']];
  936. $money = Db::name('dinner_order_goods')
  937. ->alias('a')
  938. ->join('dinner_orders b','b.id = a.order_id')
  939. ->where($m)
  940. ->sum('a.nums*a.price');
  941. $lists[$k]['money'.$vv['id']] = $money;
  942. }
  943. }
  944. $total = Db::name('dinner_orders')
  945. ->where($map)
  946. ->group('day')
  947. // ->distinct(true)
  948. ->count();
  949. $totalMoney = Db::name('dinner_orders')
  950. ->where($map)
  951. ->sum('amount');
  952. $data = [
  953. 'total_money' => $totalMoney,
  954. 'cates' => $cates,
  955. 'total' => $total,
  956. 'list' => $lists
  957. ];
  958. return $data;
  959. }
  960. public function cfinexport($userId){
  961. $companyIds = Db::name('dinner_company')->where('user_id',$userId)->where('del',0)->where('enable',1)->column('id');
  962. if($companyIds){
  963. $map[] = ['a.company_id','in',$companyIds];
  964. }else{
  965. $map[] = ['a.company_id','=',-1];
  966. }
  967. $day = input('day','','trim');
  968. if($day){
  969. $map[] = ['a.day','=',$day];
  970. }else{
  971. $map[] = ['a.day','=',0];
  972. }
  973. $map[] = ['a.status','=',2];
  974. $map[] = ['a.del','=',0];
  975. $lists = Db::name('dinner_orders')
  976. ->alias('a')
  977. ->join('dinner_company b','b.id = a.company_id')
  978. ->join('dinner_user c','c.id = a.user_id')
  979. ->where($map)
  980. ->field('a.*,b.title as company,c.phone')
  981. ->order('a.id asc')
  982. ->select();
  983. foreach ($lists as $k=>$v){
  984. $goods = Db::name('dinner_order_goods')
  985. ->alias('a')
  986. ->join('dinner_cate c','c.id = a.cate_id')
  987. ->join('dinner e','e.id = a.dinner_id')
  988. ->where('a.order_id',$v['id'])
  989. ->order('a.cate_id asc')
  990. ->field('a.*,c.title as cate_title,e.title as dinner_title')
  991. ->select();
  992. $goods = $goods?$goods:[];
  993. $strs = [];
  994. foreach ($goods as $kk=>$vv){
  995. $strs[] = '['.$vv['cate_title'].']'.$vv['dinner_title'] .' * '.$vv['nums'];
  996. }
  997. $lists[$k]['goods'] = $strs?implode(';',$strs):'';
  998. }
  999. $columns[] = ["title" => "单位","key" => "company"];
  1000. $columns[] = ["title" => "姓名","key" => "name"];
  1001. $columns[] = ["title" => "手机号","key" => "phone"];
  1002. $columns[] = ["title" => "配送日期","key" => "day"];
  1003. $columns[] = ["title" => "金额","key" => "amount"];
  1004. $columns[] = ["title" => "购买商品","key" => "goods"];
  1005. $columns[] = ["title" => "备注","key" => "remark"];
  1006. $data = [
  1007. 'columns' => $columns,
  1008. 'list' => $lists
  1009. ];
  1010. return $data;
  1011. }
  1012. }