WxBookDinner.php 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  1. <?php
  2. namespace app\h5\controller;
  3. use app\hander\HelpHander;
  4. use EasyWeChat\Factory;
  5. use think\App;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Exception;
  9. class WxBookDinner extends Controller
  10. {
  11. protected $user = [];
  12. protected $orgId = 0;
  13. protected $addressId = 0;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $userId = dinner_is_login();
  18. // $userId = 205;
  19. if(empty($userId)){
  20. $this->redirect(url('WxHome/login'));
  21. }
  22. $sn = decodecookie('sn');
  23. $orgId = decodecookie('HospitalId');
  24. if(!$sn||!$orgId){
  25. $this->redirect(url('WxHome/login'));
  26. }
  27. $data = get_qrcode_arr($sn);
  28. if(!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address'){
  29. $this->redirect(url('WxHome/index'));
  30. }
  31. $this->addressId = $data['id'];
  32. $orgName = Db::name('org')->where('id',$orgId)->cache(3600)->value('name');
  33. $this->assign('orgName',$orgName);
  34. $user = Db::name('wxuser')->where('id',$userId)->where('del',0)->find();
  35. if(!$user){
  36. $this->redirect(url('WxHome/login'));
  37. }
  38. if($user['curr_org_id'] != $orgId){
  39. Db::name('wxuser')->where('id',$user['id'])->update(['curr_org_id'=>$orgId]);
  40. }
  41. $user['isdinder'] = decodecookie('isdinder') == 1?1:0;
  42. $user['HospitalName'] = $orgName;
  43. $this->orgId = $orgId;
  44. $this->user = $user;
  45. }
  46. public function index(){
  47. $is_spot = decodecookie('is_spot');
  48. $is_spot = $is_spot==2?2:1; // 默认1=当日餐 2=预定餐
  49. encodecookie('is_spot',$is_spot);
  50. $dinner_type_id = input('dinner_type_id/d',0);
  51. // 判断是否是点餐员
  52. $bDinder = isset($this->user['isdinder'])?$this->user['isdinder']:false;
  53. if ($bDinder||$is_spot==1) {
  54. if($bDinder){
  55. encodecookie('is_spot',1);
  56. }
  57. $is_spot = 1;
  58. // 显示当天的
  59. $is_today = 1;
  60. } else {
  61. $is_today = 0;
  62. }
  63. $cartCount = $this->cartCount();
  64. $this->assign('cartCount',$cartCount);
  65. $this->assign('user',$this->user);
  66. $this->assign('is_spot',$is_spot);
  67. $this->assign('is_today',$is_today);
  68. $this->assign('bDinder',$bDinder);
  69. $typeList = model('DinnerType')->getList($this->orgId);
  70. if(!$dinner_type_id){
  71. if(isset($typeList[0])){
  72. $dinner_type_id=$typeList[0]['id'];
  73. }
  74. }
  75. $this->assign('dinner_type_id',$dinner_type_id);
  76. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  77. $this->assign('content1',$config?$config['content1']:'');
  78. $this->assign('content2',$config?$config['content2']:'');
  79. $this->assign('typeList',$typeList);
  80. $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special');
  81. $special = $special==1?$special:0;
  82. if($special){
  83. $isSpecial = $this->checkSpecial();
  84. $this->assign('isSpecial',$isSpecial);
  85. }
  86. $this->assign('special',$special);
  87. return $this->fetch();
  88. }
  89. // 点餐页
  90. public function tc(){
  91. if(!$this->checkSpecial()){
  92. $this->error("只有特殊人群才可进入");
  93. }
  94. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  95. $this->assign('week',$week);
  96. $curday = date('Y-m-d');
  97. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  98. // 当天12点之前可以定明天的餐,12点之后可以定后台的餐
  99. $hour = intval(date('H'));
  100. if($hour < 12){
  101. $curday = date('Y-m-d');
  102. }else{
  103. $curday = date('Y-m-d');
  104. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  105. }
  106. $cur = get_week_days($week);
  107. // 获取当前周已经定过餐的日期
  108. $days1 = Db::name('dinner_order_refuse_item')
  109. ->alias('a')
  110. ->join('dinner_order_refuse c','c.id = a.order_refuse_id')
  111. ->join('dinner_order b','c.dinner_order_id = b.id')
  112. ->where('b.cate',1)
  113. ->where('c.status',4)
  114. ->where('a.day','>=',$cur[0])
  115. ->where('a.day','<=',$cur[1])
  116. ->where('b.user_id',$this->user['id'])
  117. ->where('b.org_id',$this->orgId)
  118. ->group('a.day')
  119. ->distinct(true)
  120. ->column('a.day');
  121. $days1 = $days1?$days1:[];
  122. $seldays = Db::name('dinner_order_item')
  123. ->alias('a')
  124. ->join('dinner_order b','a.order_id = b.id')
  125. ->where('b.cate',1)
  126. ->where('a.is_refuse',0)
  127. ->where('a.day','>=',$cur[0])
  128. ->where('a.day','<=',$cur[1])
  129. ->where('b.user_id',$this->user['id'])
  130. ->where('b.org_id',$this->orgId)
  131. ->group('a.day')
  132. ->distinct(true)
  133. ->column('a.day');
  134. $seldays = $seldays?$seldays:[];
  135. $seldays = array_unique(array_merge($seldays,$days1));
  136. // 获取当前周已选择的套餐
  137. $groupId = Db::name('dinner_order_item')
  138. ->alias('a')
  139. ->join('dinner_order b','a.order_id = b.id')
  140. ->where('b.cate',1)
  141. ->where('a.day','>=',$cur[0])
  142. ->where('a.day','<=',$cur[1])
  143. ->where('b.user_id',$this->user['id'])
  144. ->where('b.org_id',$this->orgId)
  145. ->value('a.group_id');
  146. if($groupId > 0){
  147. $map[] = ['id','=',$groupId];
  148. }
  149. $map[] = ['start','=',$cur[0]];
  150. $map[] = ['del','=',0];
  151. $map[] = ['enable','=',1];
  152. $map[] = ['org_id','=',$this->orgId];
  153. $groups = Db::name('dinner_group')
  154. ->where($map)
  155. ->field('id,title,start,end')
  156. ->select();
  157. $groups = $groups?$groups:[];
  158. $days = [];
  159. $sday = $cur[0];
  160. while (true){
  161. if($sday > $cur[1]){
  162. break;
  163. }
  164. if($curday < $sday){
  165. if(!$seldays || ($seldays && !in_array($sday,$seldays))){
  166. $days[] = [
  167. 'count' => 0,
  168. 'money' => 0,
  169. 'day' => $sday,
  170. 'text' => get_week_txt($sday),
  171. ];
  172. }
  173. }
  174. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  175. }
  176. $typeList = model('DinnerType')->getList($this->orgId);
  177. $typeList = $typeList?$typeList:[];
  178. foreach ($groups as $gk=>$gv){
  179. $combination = Db::name('dinner_group_combination')
  180. ->where('group_id',$gv['id'])
  181. ->select();
  182. $combination = $combination?$combination:[];
  183. foreach ($combination as $ck=>$cv){
  184. $combination[$ck]['items'] = explode(',',$cv['items']);
  185. }
  186. $groups[$gk]['combination'] = $combination;
  187. $dinners = Db::name('dinner_group_item')
  188. ->alias('a')
  189. ->join('dinner b','b.id = a.dinner_id')
  190. ->where('b.enable',1)
  191. ->where('b.cate',1)
  192. ->where('a.group_id',$gv['id'])
  193. ->field('a.id,a.day,a.group_id,a.max,a.required,a.money,b.name,b.content,b.money as oldmoney,b.imgs,b.dinner_type_id,b.type')
  194. ->order('b.sort asc,a.id desc')
  195. ->select();
  196. $dinners = $dinners?$dinners:[];
  197. foreach ($dinners as $dkk=>$dvv){
  198. if($dvv['money'] == 0 && $dvv['oldmoney'] > 0){
  199. $dinners[$dkk]['money'] = $dvv['oldmoney'];
  200. }
  201. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  202. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  203. $dinners[$dkk]['nums'] = 0;
  204. $dinners[$dkk]['cid'] = 0;
  205. // 组合id
  206. $items = [];
  207. foreach ($combination as $ck=>$cv){
  208. if(in_array($dvv['id'],$cv['items'])){
  209. $items = array_merge($items,$cv['items']);
  210. $dinners[$dkk]['cid'] = $cv['id'];
  211. }
  212. }
  213. if($items){
  214. $items = array_diff(array_unique($items),[$dvv['id']]);
  215. sort($items);
  216. }
  217. $dinners[$dkk]['combination'] = $items;
  218. }
  219. $groups[$gk]['dinners'] = $dinners?$dinners:[];
  220. foreach ($days as $dk=>$dv){
  221. $days[$dk]['group'] = $gv['title'];
  222. $types = [];
  223. foreach ($typeList as $tk=>$tv){
  224. $limit = Db::name('dinner_group_type')
  225. ->where('group_id',$gv['id'])
  226. ->where('dinner_type_id',$tv['id'])
  227. ->where('day',$dv['day'])
  228. ->value('limit');
  229. $coms = [];
  230. foreach ($combination as $ck=>$cv){
  231. if($cv['dinner_type_id'] == $tv['id'] && $cv['day'] == $dv['day']){
  232. $coms[] = $cv;
  233. }
  234. }
  235. $combination2 = Db::name('dinner_group_combination2')
  236. ->where('group_id',$gv['id'])
  237. ->where('dinner_type_id',$tv['id'])
  238. ->where('day',$dv['day'])
  239. ->select();
  240. $combination2 = $combination2?$combination2:[];
  241. $ndinners = [];
  242. foreach ($dinners as $dkk=>$dvv){
  243. if($dvv['day'] == $dv['day'] && $dvv['group_id'] == $gv['id'] && $dvv['dinner_type_id'] == $tv['id']){
  244. $dvv['cid'] = 0;
  245. $dvv['ctitle'] = '';
  246. foreach ($coms as $cck=>$ccv){
  247. if(in_array($dvv['id'],$ccv['items'])){
  248. $dvv['cid'] = $ccv['id'];
  249. $dvv['ctitle'] = $ccv['title'];
  250. break;
  251. }
  252. }
  253. $coms2 = [];
  254. foreach ($combination2 as $ck=>$cv){
  255. $items = explode(',',$cv['items']);
  256. if(in_array($dvv['id'],$items)){
  257. foreach ($items as $ik=>$iv){
  258. if($iv != $dvv['id'] && !in_array($iv,$coms2)){
  259. $coms2[] = $iv;
  260. }
  261. }
  262. }
  263. }
  264. $dvv['combination2'] = $coms2;
  265. $ndinners[] = $dvv;
  266. }
  267. }
  268. $types[] = [
  269. 'id' => $tv['id'],
  270. 'name' => $tv['name'],
  271. 'group_id' => $gv['id'],
  272. 'group_title' => $gv['title'],
  273. 'limit' => $limit > 0?$limit:0,
  274. 'dinners' => $ndinners,
  275. 'combination' => $coms,
  276. ];
  277. }
  278. $days[$dk]['types'] = $types;
  279. }
  280. $groups[$gk]['days'] = $days;
  281. }
  282. // halt($groups);
  283. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  284. $this->assign('standard',$config['standard']?$config['standard']:0);
  285. $this->assign('standardmin',$config['min']?$config['min']:0);
  286. $this->assign('groups',$groups);
  287. $this->assign('days',$days);
  288. $this->assign('user',$this->user);
  289. return $this->fetch();
  290. }
  291. public function tc备份2(){
  292. if(!$this->checkSpecial()){
  293. $this->error("只有特殊人群才可进入");
  294. }
  295. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  296. $this->assign('week',$week);
  297. $curday = date('Y-m-d');
  298. $cur = get_week_days($week);
  299. // 获取当前周已经定过餐的日期
  300. $seldays = Db::name('dinner_order_item')
  301. ->alias('a')
  302. ->join('dinner_order b','a.order_id = b.id')
  303. ->where('b.cate',1)
  304. ->where('a.day','>=',$cur[0])
  305. ->where('a.day','<=',$cur[1])
  306. ->where('b.user_id',$this->user['id'])
  307. ->where('b.org_id',$this->orgId)
  308. ->group('a.day')
  309. ->distinct(true)
  310. ->column('a.day');
  311. // 获取当前周已选择的套餐
  312. $groupId = Db::name('dinner_order_item')
  313. ->alias('a')
  314. ->join('dinner_order b','a.order_id = b.id')
  315. ->where('b.cate',1)
  316. ->where('a.day','>=',$cur[0])
  317. ->where('a.day','<=',$cur[1])
  318. ->where('b.user_id',$this->user['id'])
  319. ->where('b.org_id',$this->orgId)
  320. ->value('a.group_id');
  321. if($groupId > 0){
  322. $map[] = ['id','=',$groupId];
  323. }
  324. $map[] = ['start','=',$cur[0]];
  325. $map[] = ['del','=',0];
  326. $map[] = ['enable','=',1];
  327. $map[] = ['org_id','=',$this->orgId];
  328. $groups = Db::name('dinner_group')
  329. ->where($map)
  330. ->field('id,title,start,end')
  331. ->select();
  332. $groups = $groups?$groups:[];
  333. $days = [];
  334. $sday = $cur[0];
  335. while (true){
  336. if($sday > $cur[1]){
  337. break;
  338. }
  339. if($curday < $sday){
  340. if(!$seldays || ($seldays && !in_array($sday,$seldays))){
  341. $days[] = [
  342. 'count' => 0,
  343. 'money' => 0,
  344. 'day' => $sday,
  345. 'text' => get_week_txt($sday),
  346. ];
  347. }
  348. }
  349. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  350. }
  351. $typeList = model('DinnerType')->getList($this->orgId);
  352. $typeList = $typeList?$typeList:[];
  353. foreach ($groups as $gk=>$gv){
  354. foreach ($days as $dk=>$dv){
  355. $days[$dk]['group'] = $gv['title'];
  356. $types = [];
  357. foreach ($typeList as $tk=>$tv){
  358. $dinners = Db::name('dinner_group_item')
  359. ->alias('a')
  360. ->join('dinner b','b.id = a.dinner_id')
  361. ->where('b.enable',1)
  362. ->where('b.cate',1)
  363. ->where('b.dinner_type_id',$tv['id'])
  364. ->where('a.group_id',$gv['id'])
  365. ->where('a.day',$dv['day'])
  366. ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id,b.type')
  367. ->order('b.sort asc,a.id desc')
  368. ->select();
  369. $dinners = $dinners?$dinners:[];
  370. foreach ($dinners as $dkk=>$dvv){
  371. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  372. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  373. $dinners[$dkk]['nums'] = 0;
  374. }
  375. $limit = Db::name('dinner_group_type')
  376. ->where('group_id',$gv['id'])
  377. ->where('dinner_type_id',$tv['id'])
  378. ->where('day',$dv['day'])
  379. ->value('limit');
  380. $types[] = [
  381. 'id' => $tv['id'],
  382. 'name' => $tv['name'],
  383. 'group_id' => $gv['id'],
  384. 'group_title' => $gv['title'],
  385. 'limit' => $limit > 0?$limit:0,
  386. 'dinners' => $dinners,
  387. ];
  388. }
  389. $days[$dk]['types'] = $types;
  390. }
  391. $groups[$gk]['days'] = $days;
  392. }
  393. $this->assign('groups',$groups);
  394. $this->assign('days',$days);
  395. $this->assign('user',$this->user);
  396. return $this->fetch();
  397. }
  398. public function tc备份(){
  399. if(!$this->checkSpecial()){
  400. $this->error("只有特殊人群才可进入");
  401. }
  402. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  403. $this->assign('week',$week);
  404. $curday = date('Y-m-d');
  405. $cur = get_week_days($week);
  406. $weeks = [
  407. ['week'=>$week,'start'=>$cur[0],'end'=>$cur[1],'selected'=>1],
  408. ];
  409. foreach ($weeks as $k=>$v){
  410. $days = [];
  411. $sday = $v['start'];
  412. while (true){
  413. if($sday > $v['end']){
  414. break;
  415. }
  416. if($curday < $sday){
  417. $days[] = [
  418. 'count' => 0,
  419. 'money' => 0,
  420. 'day' => $sday,
  421. 'text' => get_week_txt($sday),
  422. ];
  423. }
  424. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  425. }
  426. foreach ($days as $dk=>$dv){
  427. $groups = Db::name('dinner_group')
  428. ->where('org_id',$this->orgId)
  429. ->where('del',0)
  430. ->where('enable',1)
  431. ->where('start',$v['start'])
  432. ->select();
  433. $groups = $groups?$groups:[];
  434. $types = [];
  435. foreach ($groups as $kk=>$vv){
  436. $typeList = model('DinnerType')->getList($this->orgId);
  437. $typeList = $typeList?$typeList:[];
  438. foreach ($typeList as $tk=>$tv){
  439. $dinners = Db::name('dinner_group_item')
  440. ->alias('a')
  441. ->join('dinner b','b.id = a.dinner_id')
  442. ->where('b.enable',1)
  443. ->where('b.cate',1)
  444. ->where('b.dinner_type_id',$tv['id'])
  445. ->where('a.group_id',$vv['id'])
  446. ->where('a.day',$dv['day'])
  447. ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id')
  448. ->order('b.sort asc,a.id desc')
  449. ->select();
  450. $dinners = $dinners?$dinners:[];
  451. foreach ($dinners as $dkk=>$dvv){
  452. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  453. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  454. // $dinners[$dkk]['selected'] = $dvv['required']?1:0;
  455. }
  456. $limit = Db::name('dinner_group_type')
  457. ->where('group_id',$vv['id'])
  458. ->where('dinner_type_id',$tv['id'])
  459. ->where('day',$dv['day'])
  460. ->value('limit');
  461. $types[] = [
  462. 'id' => $tv['id'],
  463. 'name' => $tv['name'],
  464. 'group_id' => $vv['id'],
  465. 'group_title' => $vv['title'],
  466. 'group_type' => $vv['type'],
  467. 'limit' => $limit > 0?$limit:0,
  468. 'dinners' => $dinners,
  469. ];
  470. }
  471. }
  472. $days[$dk]['types'] = $types;
  473. }
  474. $weeks[$k]['days'] = $days;
  475. }
  476. $this->assign('weeks',$weeks);
  477. $groups = Db::name('dinner_group')
  478. ->where('org_id',$this->orgId)
  479. ->where('del',0)
  480. ->where('enable',1)
  481. ->where('start',$cur[0])
  482. ->select();
  483. $groups = $groups?$groups:[];
  484. $this->assign('groups',$groups);
  485. $this->assign('user',$this->user);
  486. return $this->fetch();
  487. }
  488. // 点套餐商品页
  489. public function group(){
  490. }
  491. public function cartCount(){
  492. $cartCount = Db::name('dinner_cart')
  493. ->where('user_id',$this->user['id'])
  494. ->where('org_id',$this->orgId)
  495. ->sum('count');
  496. return $cartCount;
  497. }
  498. public function indexchange(){
  499. $is_spot = input('spot/d',1);
  500. $is_spot = $is_spot==1?1:2; // 默认1当日餐 2=预定餐
  501. // 清空购物车
  502. Db::name('dinner_cart')->where('user_id',$this->user['id'])->delete();
  503. encodecookie('is_spot',$is_spot);
  504. $this->redirect(url('WxBookDinner/index'));
  505. }
  506. public function get_type_json(){
  507. $size = input('size');
  508. $page = input('page');
  509. $today = input('today');
  510. $btoday = false;
  511. if (!empty($today)&&($today==1)) {
  512. $btoday = true;
  513. }
  514. $DinnerTypeId = input('type');
  515. $ret = $this->show_all_page_by_type($btoday, $DinnerTypeId,$page, $size);
  516. if (empty($ret)) {
  517. header('Content-Type:application/json; charset=utf-8');
  518. exit(json_encode([]));
  519. } else {
  520. header('Content-Type:application/json; charset=utf-8');
  521. exit(json_encode($ret));
  522. }
  523. }
  524. public function show_all_page_by_type($today, $DinnerTypeId, $page, $size){
  525. $query = Db::name('dinner');
  526. $query->where('cate', 0);
  527. if ($DinnerTypeId>0) {
  528. $query->where('dinner_type_id', $DinnerTypeId);
  529. }
  530. if($today){
  531. $query->where('today_enable', 1);
  532. // 限定时间
  533. $currtime = date('H:i');
  534. $query->whereRaw(Db::raw('(today_start_time<="'.$currtime.'" and today_end_time>="'.$currtime.'") or today_start_time= null or today_end_time=null'));
  535. }else{
  536. $query->where('enable', 1);
  537. // 限定时间
  538. $currtime = date('H:i');
  539. $query->whereRaw(Db::raw('(start_time<="'.$currtime.'" and end_time>="'.$currtime.'") or start_time= null or end_time=null'));
  540. }
  541. $query->where('org_id', $this->orgId);
  542. $query->order('sort ASC,id desc');
  543. $query->page($page,$size);
  544. $ret = $query->select();
  545. if (empty($ret)) {
  546. return array();
  547. }
  548. $isSpecial = $this->checkSpecial();
  549. foreach ($ret as $key => $value) {
  550. $img = $value['imgs']?explode(',',$value['imgs']):[];
  551. $ret[$key]['img'] = $img?$img[0]:'';
  552. if($isSpecial){
  553. $money = $this->getDiscountMoney($value);
  554. }else{
  555. $money = $value['money'];
  556. }
  557. $ret[$key]['money'] = sprintf("%.2f", $money);
  558. }
  559. return $ret;
  560. }
  561. public function getDiscountMoney($value){
  562. $money = $value['money'];
  563. if($value['free'] == 1){
  564. $money = 0;
  565. }else{
  566. $money = $money*$value['discount'];
  567. }
  568. return round($money,2);
  569. }
  570. public function addCart(){
  571. $id = input('id/d',0);
  572. if($id <= 0){
  573. HelpHander::error('参数错误');
  574. }
  575. $map[] = ['dinner_id','=',$id];
  576. $map[] = ['org_id','=',$this->orgId];
  577. $map[] = ['user_id','=',$this->user['id']];
  578. $num = input('num/d',1);
  579. $info = Db::name('dinner_cart')->where($map)->find();
  580. if(!empty($info)){
  581. Db::name('dinner_cart')->where($map)->setInc('count',$num);
  582. $count = $num+$info['count'];
  583. }else{
  584. $a = [
  585. 'dinner_id'=>$id,
  586. 'org_id'=>$this->orgId,
  587. 'user_id'=>$this->user['id'],
  588. 'count'=>1,
  589. 'create_time'=>getTime(),
  590. ];
  591. Db::name('dinner_cart')->insertGetId($a);
  592. $count = 1;
  593. }
  594. HelpHander::success(['count'=>$count],'操作成功');
  595. }
  596. public function updateCart(){
  597. $id = input('id/d',0);
  598. if($id <= 0){
  599. HelpHander::error('参数错误');
  600. }
  601. $map[] = ['id','=',$id];
  602. $map[] = ['org_id','=',$this->orgId];
  603. $map[] = ['user_id','=',$this->user['id']];
  604. $num = input('num/d',1);
  605. Db::name('dinner_cart')->where($map)->update([
  606. 'count'=>$num
  607. ]);
  608. Db::name('dinner_cart')->where($map)->where('count',0)->delete();
  609. HelpHander::success(['count'=>$this->cartCount()],'操作成功');
  610. }
  611. public function detail(){
  612. $id = input('id/d',0);
  613. $res = Db::name('dinner')->where('id',$id)->find();
  614. if(!$res){
  615. return $this->fetch('h5/msg_error',['msg'=>'记录不存在']);
  616. }
  617. $res['imgs'] = $res['imgs']?explode(',',$res['imgs']):[];
  618. $isSpecial = $this->checkSpecial();
  619. if($isSpecial){
  620. $res['money'] = $this->getDiscountMoney($res);
  621. }
  622. $this->assign('info',$res);
  623. $this->assign('t',time());
  624. return $this->fetch();
  625. }
  626. public function cart(){
  627. // 需要检查是否还在售卖时间范围内,只取范围内的数据
  628. $cartCount = $this->cartCount();
  629. $map[] = ['org_id','=',$this->orgId];
  630. $map[] = ['user_id','=',$this->user['id']];
  631. $info = Db::name('dinner_cart')->where($map)->select();
  632. $a = [];
  633. foreach ($info as $k=>$v){
  634. $data = Db::name('dinner')->where('id',$v['dinner_id'])->find();
  635. $imgs = $data['imgs']?explode(',',$data['imgs']):[];
  636. $data['img'] = $imgs?$imgs[0]:'';
  637. $data['count'] = $v['count'];
  638. $data['cart_id'] = $v['id'];
  639. $a[] = $data;
  640. }
  641. $this->assign('cartCount',$cartCount);
  642. $this->assign('data',$a);
  643. return $this->fetch();
  644. }
  645. public function submitorder(){
  646. $bDinder = $this->user['isdinder'];
  647. if (request()->isGet()) {
  648. if(!decodecookie('is_spot')){
  649. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  650. }
  651. $orderids = input('orders');
  652. if (empty($orderids)) {
  653. return $this->fetch('h5/msg_error',['msg'=>'ids不能为空']);
  654. }
  655. $ids = explode(',', $orderids);
  656. //创建订单
  657. $cart =Db::name('dinner_cart')
  658. ->where('id','in',$ids)
  659. ->where('org_id',$this->orgId)
  660. ->where('user_id',$this->user['id'])
  661. ->column('dinner_id');
  662. if(empty($cart)){
  663. return $this->fetch('h5/msg_error',['msg'=>'购物车信息不存在']);
  664. }
  665. $all_data = Db::name('dinner')
  666. ->where('id','in',$cart)
  667. ->select();
  668. if(count($all_data) != count($ids)){
  669. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  670. }
  671. $is_spot = decodecookie('is_spot');
  672. $is_spot = $is_spot==1?1:0;
  673. $price_count =0;
  674. $curTime = date('Y-m-d H:i:s');
  675. $isSpecial = $this->checkSpecial();
  676. foreach ($all_data as $key => $value) {
  677. $catNum = Db::name('dinner_cart')
  678. ->where('dinner_id',$value['id'])
  679. ->where('org_id',$this->orgId)
  680. ->where('user_id',$this->user['id'])
  681. ->value('count');
  682. if($is_spot == 1){
  683. if($value['today_enable'] != 1){
  684. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']);
  685. }
  686. if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){
  687. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']);
  688. }
  689. }
  690. if($is_spot == 0){
  691. if($value['enable'] != 1){
  692. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']);
  693. }
  694. if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){
  695. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']);
  696. }
  697. }
  698. $money = $value['money'];
  699. if($isSpecial == 1){
  700. $money = $this->getDiscountMoney($value);
  701. }
  702. $all_data[$key]['money'] = $money;
  703. $all_data[$key]['count'] = $catNum;
  704. $price_count += $money*$catNum;
  705. }
  706. $addressInfo = Db::name('dinner_address')
  707. ->where('id',$this->addressId)
  708. ->find();
  709. if(!$addressInfo){
  710. return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]);
  711. }
  712. $address = $addressInfo?$addressInfo['title']:'';
  713. $is_pay = true;
  714. $ret = Db::name('dinner_order')
  715. ->where('org_id',$this->orgId)
  716. ->where('user_id',$this->user['id'])
  717. ->order('id desc')
  718. ->find();
  719. if($ret){
  720. $this->assign('name',$ret['name']);
  721. $this->assign('phone',$ret['mobile']);
  722. }else{
  723. $this->assign('name','');
  724. $this->assign('phone','');
  725. }
  726. $this->assign('list',$all_data);
  727. $this->assign('count',$price_count);
  728. $this->assign('address',$address);
  729. $this->assign('bDinder',$bDinder);
  730. $this->assign('is_pay',$is_pay);
  731. $this->assign('HospitalName',$this->user['HospitalName']);
  732. $this->assign('ids',$orderids);
  733. //显示当前医院
  734. return $this->fetch();
  735. } else {
  736. if(!decodecookie('is_spot')){
  737. HelpHander::error('参数错误');
  738. }
  739. $orderids = input('orders');
  740. if (empty($orderids)) {
  741. HelpHander::error('ids不能为空');
  742. }
  743. if (empty($this->user)) {
  744. HelpHander::error('未登录请先登录');
  745. }
  746. $ids = explode(',', $orderids);
  747. //创建订单
  748. $cart =Db::name('dinner_cart')
  749. ->where('id','in',$ids)
  750. ->where('org_id',$this->orgId)
  751. ->where('user_id',$this->user['id'])
  752. ->column('dinner_id');
  753. if(empty($cart)){
  754. HelpHander::error('购物车信息不存在');
  755. }
  756. $all_data = Db::name('dinner')
  757. ->where('id','in',$cart)
  758. ->select();
  759. if(count($all_data) != count($ids)){
  760. HelpHander::error('参数错误');
  761. }
  762. $is_spot = decodecookie('is_spot');
  763. $is_spot = $is_spot==1?1:0;
  764. $is_take = input('is_take');
  765. if($is_take==1){
  766. $typeId = array_unique(array_column($all_data,'dinner_type_id'));
  767. if($typeId >1){
  768. HelpHander::error('自取订单只能下单同一分类菜品');
  769. }
  770. }
  771. $price_count = 0;
  772. $money_count = 0;
  773. $curTime = date('Y-m-d H:i:s');
  774. $isSpecial = $this->checkSpecial();
  775. foreach ($all_data as $key => $value) {
  776. $catNum = Db::name('dinner_cart')
  777. ->where('dinner_id',$value['id'])
  778. ->where('org_id',$this->orgId)
  779. ->where('user_id',$this->user['id'])
  780. ->value('count');
  781. if($is_spot == 1){
  782. if($value['today_enable'] != 1){
  783. HelpHander::error($value['name'].'已下架');
  784. }
  785. if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){
  786. HelpHander::error($value['name'].'不再售卖时间内');
  787. }
  788. }
  789. if($is_spot == 0){
  790. if($value['enable'] != 1){
  791. HelpHander::error($value['name'].'已下架');
  792. }
  793. if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){
  794. HelpHander::error($value['name'].'不再售卖时间内');
  795. }
  796. }
  797. $money1 = $value['money'];
  798. if($isSpecial == 1){
  799. $money1 = $this->getDiscountMoney($value);
  800. }
  801. $all_data[$key]['money'] = $money1;
  802. $all_data[$key]['original_price'] = $value['money'];
  803. $all_data[$key]['count'] = $catNum;
  804. $price_count += $money1*$catNum;
  805. $money_count += $value['money']*$catNum;
  806. }
  807. $money = $money_count - $price_count;
  808. $name =input('name');
  809. $mobile = input('mobile');
  810. $address = input('address');
  811. $order_type = input('order_type');
  812. $remark = trim(input('remark'));
  813. if($remark && mb_strlen($remark) > 15){
  814. HelpHander::error('床号及备注最多15字');
  815. }
  816. // log_message('error','值是:'.$ORDER_TYPE);
  817. // 如果没有的话,表示的就是自己订餐
  818. // 1-微信;2-食堂卡;3-现金;4-未付款(不需要付款)
  819. if (empty($order_type)) {
  820. $order_type = 1;
  821. $is_replace = 0;
  822. }
  823. // 4-未付款
  824. elseif ($order_type==4) {
  825. $is_replace = 0;
  826. }
  827. //
  828. else {
  829. $is_replace = 1;
  830. }
  831. $bDinder = $this->user['isdinder'];
  832. if($bDinder){
  833. $is_spot = 1; // 点餐员只能点当日餐
  834. }
  835. if (empty($name)) {
  836. HelpHander::error('姓名不能为空');
  837. }
  838. if (empty($mobile)) {
  839. HelpHander::error('手机号不能为空');
  840. }
  841. if(!check_mobile($mobile)){
  842. HelpHander::error('手机号格式错误');
  843. }
  844. if (empty($address)) {
  845. HelpHander::error('地址不能为空');
  846. }
  847. if (empty($remark)) {
  848. HelpHander::error('床号及备注不能为空');
  849. }
  850. Db::startTrans();
  851. if ($order_type==1) {
  852. $state = 0;
  853. } else {
  854. $state = 1;
  855. }
  856. if($price_count == 0){
  857. $state = 1;
  858. }
  859. try{
  860. $pp = array(
  861. 'price' => $price_count,
  862. 'state' => $state,
  863. 'org_id'=>$this->orgId,
  864. 'sn' => get_unique_id(),
  865. 'create_time' => date('Y-m-d H:i:s'),
  866. 'ymd'=>date('Ymd'),
  867. 'y'=>date('Y'),
  868. 'ym'=>date('Ym'),
  869. 'w'=>date('Y').date('W'),
  870. 'pay_time' => date('Y-m-d H:i:s'),
  871. 'user_id' => $this->user['id'],
  872. 'order_type'=>$order_type,
  873. 'name'=>$name,
  874. 'mobile'=>$mobile,
  875. 'address'=>$address,
  876. 'is_replace'=>$is_replace,
  877. 'is_spot'=>$is_spot,
  878. 'remark'=>$remark,
  879. 'is_take'=>$is_take,
  880. 'cate' => 0,
  881. 'money' => $money,
  882. );
  883. $orderId = Db::name('dinner_order')
  884. ->insertGetId($pp);
  885. if(!$orderId){
  886. \exception('订单添加失败');
  887. }
  888. $item = [];
  889. foreach ($all_data as $kk=>$vv){
  890. $item[] = [
  891. 'dinner_id'=>$vv['id'],
  892. 'order_id'=>$orderId,
  893. 'price'=>$vv['money']*$vv['count'],
  894. 'num'=>$vv['count'],
  895. 'dinner_type_id'=>$vv['dinner_type_id'],
  896. 'original_price'=>$vv['original_price'],
  897. 'money'=>$vv['money'],
  898. ];
  899. }
  900. $r = Db::name('dinner_order_item')->insertAll($item);
  901. if(!$r){
  902. \exception('操作失败');
  903. }
  904. $rr = Db::name('dinner_cart')
  905. ->where('id','in',$ids)
  906. ->delete();
  907. if(!$rr){
  908. \exception('操作失败');
  909. }
  910. Db::commit();
  911. if ($state==0) {
  912. HelpHander::success(['url'=>url('pay').'?id='.$orderId],'操作成功' );
  913. } else {
  914. HelpHander::success(['url'=>url('payresult').'?id='.$orderId], '操作成功');
  915. }
  916. }catch (Exception $e){
  917. Db::rollback();
  918. HelpHander::error($e->getMessage());
  919. }
  920. }
  921. }
  922. public function submitorder2(){
  923. $user = $this->user;
  924. if (request()->isGet()) {
  925. $ret = Db::name('dinner_apply')
  926. ->where('org_id',$this->orgId)
  927. ->where('user_id',$user['id'])
  928. ->where('del',0)
  929. ->where('status',1)
  930. ->find();
  931. if(!$ret){
  932. return $this->fetch('h5/msg_error',['msg'=>'只有特殊人群才可进入']);
  933. }
  934. $this->assign('name',$ret['name']);
  935. $this->assign('phone',$ret['phone']);
  936. $addressInfo = Db::name('dinner_address')
  937. ->where('id',$this->addressId)
  938. ->find();
  939. if(!$addressInfo){
  940. return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]);
  941. }
  942. $address = $addressInfo?$addressInfo['title']:'';
  943. $this->assign('HospitalName',$this->user['HospitalName']);
  944. $this->assign('address',$address);
  945. return $this->fetch();
  946. } else {
  947. if (empty($this->user)) {
  948. $this->error('未登录请先登录');
  949. }
  950. $is_take = 0;
  951. $name =input('name','','trim');
  952. $mobile = input('mobile','','trim');
  953. $address = input('address','','trim');
  954. $order_type = 4;
  955. $remark = trim(input('remark'));
  956. if($remark && mb_strlen($remark) > 15){
  957. $this->error('床号及备注最多15字');
  958. }
  959. if (empty($name)) {
  960. $this->error('姓名不能为空');
  961. }
  962. if (empty($mobile)) {
  963. $this->error('手机号不能为空');
  964. }
  965. if(!check_mobile($mobile)){
  966. $this->error('手机号格式错误');
  967. }
  968. if (empty($address)) {
  969. $this->error('地址不能为空');
  970. }
  971. if (empty($remark)) {
  972. $this->error('床号及备注不能为空');
  973. }
  974. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  975. $special = $config&&$config['special']==1?1:0;
  976. if(!$special){
  977. $this->error('未开启套餐预定模式');
  978. }
  979. $money = 0;
  980. $img = '';
  981. $goods = input('goods');
  982. if(!$goods){
  983. $this->error('参数错误');
  984. }
  985. $goods = json_decode($goods,true);
  986. if(!$goods){
  987. $this->error('参数错误');
  988. }
  989. $days = [];
  990. $type = 0;
  991. $all_data = [];
  992. $groups = [];
  993. foreach ($goods as $k=>$v){
  994. if($v['nums'] <= 0){
  995. $this->error('参数错误');
  996. }
  997. $dinner = Db::name('dinner_group_item')
  998. ->alias('a')
  999. ->join('dinner_group b','a.group_id = b.id')
  1000. ->join('dinner c','a.dinner_id = c.id')
  1001. ->where('a.id',$v['id'])
  1002. ->where('b.enable',1)
  1003. ->where('b.del',0)
  1004. ->where('c.enable',1)
  1005. ->where('c.cate',1)
  1006. ->field('a.group_id,a.day,a.money,c.id,c.money as oldmoney,c.dinner_type_id,c.type,a.max,c.name')
  1007. ->find();
  1008. if(!$dinner){
  1009. $this->error('选择的部分商品不存在或已下架');
  1010. }
  1011. if($dinner['max'] > 0 && $v['nums'] > $dinner['max']){
  1012. $this->error($dinner['name'].'最多选择'.$dinner['max'].'个');
  1013. }
  1014. if($dinner['money'] == 0 && $dinner['oldmoney'] > 0){
  1015. $dinner['money'] == $dinner['oldmoney'];
  1016. }
  1017. unset($dinner['oldmoney']);
  1018. // 当天12点之前可以定明天的餐,12点之后可以定后台的餐
  1019. $hour = intval(date('H'));
  1020. if($hour < 12){
  1021. $curday = date('Y-m-d');
  1022. if($dinner['day'] <= $curday){
  1023. $this->error('12点之前只能提前一天预订');
  1024. }
  1025. }else{
  1026. $curday = date('Y-m-d');
  1027. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  1028. if($dinner['day'] <= $curday){
  1029. $this->error('12点之后只能后天的餐');
  1030. }
  1031. }
  1032. $groups[] = $dinner['group_id'];
  1033. $dinner['nums'] = $v['nums'];
  1034. $all_data[] = $dinner;
  1035. $days[] = $dinner['day'];
  1036. if($dinner['type'] == 1){
  1037. $type = 1;
  1038. }
  1039. $money = $money + $dinner['money']*$v['nums'];
  1040. }
  1041. if($config['standard'] > 0){
  1042. foreach ($days as $day){
  1043. $dmoney = 0;
  1044. foreach ($all_data as $k=>$v){
  1045. if($v['day'] == $day){
  1046. $dmoney += $v['money']*$v['nums'];
  1047. }
  1048. }
  1049. if($dmoney > $config['standard']){
  1050. $this->error($day.'超过餐标标准');
  1051. }
  1052. }
  1053. }
  1054. $groups = array_unique($groups);
  1055. if(count($groups) > 1){
  1056. $this->error('一周只能点一种套餐');
  1057. }
  1058. // 检查本周是否有其他订单
  1059. $week = get_week_days(0); // 本周
  1060. $week1 = get_week_days(1); // 下周
  1061. $day = $days[0];
  1062. $start = "";
  1063. $end = "";
  1064. if($day >= $week[0] && $day <= $week[1]){
  1065. $start = $week[0];
  1066. $end = $week[1];
  1067. }
  1068. if($day >= $week1[0] && $day <= $week1[1]){
  1069. $start = $week1[0];
  1070. $end = $week1[1];
  1071. }
  1072. if(!$start){
  1073. $this->error('只能预订本周或下周的餐');
  1074. }
  1075. $ret = Db::name('dinner_order_item')
  1076. ->alias('a')
  1077. ->join('dinner_order b','a.order_id = b.id')
  1078. ->where('b.cate',1)
  1079. ->where('b.user_id',$this->user['id'])
  1080. ->where('a.group_id','<>',$groups[0])
  1081. ->where('a.day','>=',$start)
  1082. ->where('a.day','<=',$end)
  1083. ->find();
  1084. if($ret){
  1085. $this->error('一周只能点一种套餐');
  1086. }
  1087. // 验证这些天是否已有订单
  1088. foreach ($days as $k=>$v){
  1089. $days1 = Db::name('dinner_order_refuse_item')
  1090. ->alias('a')
  1091. ->join('dinner_order_refuse c','c.id = a.order_refuse_id')
  1092. ->join('dinner_order b','c.dinner_order_id = b.id')
  1093. ->where('b.cate',1)
  1094. ->where('c.status',4)
  1095. ->where('a.day',$v)
  1096. ->where('b.user_id',$this->user['id'])
  1097. ->field('a.*')
  1098. ->find();
  1099. if($days1){
  1100. $this->error($v.'已下过订单,不能下单');
  1101. }
  1102. $ret = Db::name('dinner_order_item')
  1103. ->alias('a')
  1104. ->join('dinner_order b','a.order_id = b.id')
  1105. ->where('b.user_id',$this->user['id'])
  1106. ->where('b.cate',1)
  1107. ->where('a.is_refuse',0)
  1108. ->where('a.day',$v)
  1109. ->find();
  1110. if($ret){
  1111. $this->error($v.'已下过订单,不能下单');
  1112. }
  1113. }
  1114. if($type == 1){ // 必须上传医嘱证明
  1115. if(!$_FILES['files']['tmp_name']){
  1116. $this->error('未上传医嘱证明');
  1117. }
  1118. try{
  1119. $updir = config('upload_dir');
  1120. $file = request()->file('files');
  1121. $fconfig = config('admin_upload_img');
  1122. $finfo = $file->validate($fconfig)
  1123. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1124. if($finfo){
  1125. $img = '/'.$updir.'/files/' . $finfo->getSaveName();
  1126. $img = request()->domain(true).str_replace('\\', '/', $img);
  1127. }else{
  1128. // 上传失败获取错误信息
  1129. \exception($file->getError());
  1130. }
  1131. }catch (\Exception $e){
  1132. $this->error($e->getMessage());
  1133. }
  1134. }
  1135. Db::startTrans();
  1136. try{
  1137. $pp = array(
  1138. 'price' => 0,
  1139. 'state' => 1,
  1140. 'org_id' => $this->orgId,
  1141. 'sn' => get_unique_id(),
  1142. 'create_time' => date('Y-m-d H:i:s'),
  1143. 'ymd'=>date('Ymd'),
  1144. 'y'=>date('Y'),
  1145. 'ym'=>date('Ym'),
  1146. 'w'=>date('Y').date('W'),
  1147. 'pay_time' => date('Y-m-d H:i:s'),
  1148. 'user_id' => $this->user['id'],
  1149. 'order_type'=>$order_type,
  1150. 'name'=>$name,
  1151. 'mobile'=>$mobile,
  1152. 'address'=>$address,
  1153. 'remark'=>$remark,
  1154. 'is_take'=>$is_take,
  1155. 'cate' => 1,
  1156. 'money' => $money,
  1157. 'img' => $img
  1158. );
  1159. $orderId = Db::name('dinner_order')->insertGetId($pp);
  1160. if(!$orderId){
  1161. \exception('订单添加失败');
  1162. }
  1163. $item = [];
  1164. foreach ($all_data as $kk=>$vv){
  1165. $item[] = [
  1166. 'dinner_id'=>$vv['id'],
  1167. 'order_id'=>$orderId,
  1168. 'price'=>$vv['money']*$vv['nums'],
  1169. 'original_price' => $vv['money'],
  1170. 'num'=>$vv['nums'],
  1171. 'dinner_type_id'=>$vv['dinner_type_id'],
  1172. 'group_id'=>$vv['group_id'],
  1173. 'day' => $vv['day'],
  1174. 'money'=>$vv['money'],
  1175. ];
  1176. }
  1177. $r = Db::name('dinner_order_item')->insertAll($item);
  1178. if(!$r){
  1179. \exception('操作失败');
  1180. }
  1181. Db::commit();
  1182. }catch (Exception $e){
  1183. Db::rollback();
  1184. $this->error($e->getMessage());
  1185. }
  1186. $this->redirect(url('payresult').'?id='.$orderId);
  1187. }
  1188. }
  1189. public function pay()
  1190. {
  1191. $id = input('id');
  1192. if (empty($id)) {
  1193. return $this->fetch('h5/msg_error',['msg'=>'ID不能为空']);
  1194. }
  1195. $_order = Db::name('dinner_order')
  1196. ->where('id',$id)
  1197. ->find();
  1198. if (empty($_order)) {
  1199. return $this->fetch('h5/msg_error',['msg'=>'不存此订单']);
  1200. }
  1201. if($_order['state'] != 0){
  1202. return $this->fetch('h5/msg_error',['msg'=>'此订单不能支付']);
  1203. }
  1204. if($_order['state'] == 0){
  1205. $tt = time() - strtotime($_order['create_time']);
  1206. if($tt >= 30*60){ // 半小时内可以支付
  1207. return $this->fetch('h5/msg_error',['msg'=>'此订单已过期不能支付']);
  1208. }
  1209. }
  1210. $notify_url = getSite().url('Notify/payDinner',['org_id'=>$_order['org_id']]);
  1211. $org = Db::name('org')->where('id',$_order['org_id'])->find();
  1212. $body = $org['name'].'_'.$_order['org_id'];
  1213. $config =get_pay_wechat($_order['org_id']);
  1214. $app = \EasyWeChat\Factory::payment($config);
  1215. $result = $app->order->unify([
  1216. 'body' => $body,
  1217. 'out_trade_no' => $_order['sn'],
  1218. 'total_fee' => $_order['price']*100,
  1219. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  1220. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  1221. 'openid' => $this->user['openid'],
  1222. ]);
  1223. trace($notify_url);
  1224. if($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS'){
  1225. $msg = isset($result['return_msg'])?$result['return_msg']:'调起失败支付';
  1226. return $this->fetch('h5/msg_error',['msg'=>$msg]);
  1227. }
  1228. $jssdk = $app->jssdk;
  1229. $jsApiParameters = $jssdk->bridgeConfig($result['prepay_id']);
  1230. $params = array(
  1231. 'orderid'=>$id,
  1232. 'localorder' => $_order,
  1233. 'openid' => $this->user['openid'],
  1234. // 'input' => $input,
  1235. 'config' => $config,
  1236. // 'order' => $order,
  1237. 'jsApiParameters' => $jsApiParameters,
  1238. // 'editAddress' => $editAddress,
  1239. );
  1240. $params['jsApiParameters'] = json_encode($params['jsApiParameters']);
  1241. // var_dump($params);exit();
  1242. $this->assign('params',$params);
  1243. return $this->fetch();
  1244. }
  1245. public function payresult()
  1246. {
  1247. $msg = '';
  1248. $detail= '';
  1249. $type=0;
  1250. $id = input('id');
  1251. if (empty($id)) {
  1252. $msg='ID不能为空';
  1253. } else {
  1254. $_order = Db::name('dinner_order')
  1255. ->where('id',$id)
  1256. ->find();
  1257. if (empty($_order)) {
  1258. $msg='不存在此订单';
  1259. } else {
  1260. if ($_order['state']==1) {
  1261. $msg='订购成功!';
  1262. $type=1;
  1263. // 减库存
  1264. // $this->load->model('DinnercartModel');
  1265. // $this->DinnercartModel->dec_diner($_order);
  1266. } else {
  1267. $msg='支付可能失败';
  1268. $detail= '没有查询到订单成功的状态,可能系统缓慢原因导致,稍后在我的订单里查看';
  1269. }
  1270. }
  1271. }
  1272. $this->assign('msg',$msg);
  1273. $this->assign('type',$type);
  1274. $this->assign('detail',$detail);
  1275. return $this->fetch();
  1276. }
  1277. public function checkSpecial(){
  1278. $isSpecial = false;
  1279. $user = $this->user;
  1280. $ret = Db::name('dinner_apply')
  1281. ->where('org_id',$this->orgId)
  1282. ->where('user_id',$user['id'])
  1283. ->where('del',0)
  1284. ->where('status',1)
  1285. ->find();
  1286. if($ret){
  1287. $isSpecial = true;
  1288. }
  1289. return $isSpecial;
  1290. }
  1291. public function order()
  1292. {
  1293. $DinnerCount = $this->cartCount();
  1294. //用户个人信息
  1295. $user = $this->user;
  1296. $org = Db::name('org')->where('id',$this->orgId)->find();
  1297. $user['HospitalName'] = $org['name'];
  1298. $this->assign('orgName',$org['name']);
  1299. $DinnerCart = [];
  1300. $this->assign('cart',$DinnerCart);
  1301. $this->assign('cartCount',$DinnerCount);
  1302. $this->assign('user',$user);
  1303. $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special');
  1304. $special = $special==1?$special:0;
  1305. if($special){
  1306. $isSpecial = 0; //未申请
  1307. $apply = Db::name('dinner_apply')
  1308. ->where('org_id',$this->orgId)
  1309. ->where('user_id',$user['id'])
  1310. ->where('del',0)
  1311. ->where('status','in',[0,1])
  1312. ->find();
  1313. if($apply && $apply['status'] == 1){
  1314. $isSpecial = 2; // 通过审核
  1315. }
  1316. if($apply && $apply['status'] == 0){
  1317. $isSpecial = 1; // 审核中
  1318. }
  1319. $this->assign('isSpecial',$isSpecial);
  1320. }
  1321. $this->assign('special',$special);
  1322. return $this->fetch();
  1323. }
  1324. public function userapply(){
  1325. if(request()->isPost()){
  1326. $user = $this->user;
  1327. $ret = Db::name('dinner_apply')
  1328. ->where('org_id',$this->orgId)
  1329. ->where('user_id',$user['id'])
  1330. ->where('del',0)
  1331. ->find();
  1332. if($ret && $ret['status'] == 0){
  1333. $this->error('信息审核中,勿重复提交');
  1334. }
  1335. if($ret && $ret['status'] == 1){
  1336. $this->error('你已是特殊人群,勿重复提交');
  1337. }
  1338. $updir = config('upload_dir');
  1339. $imgs = [];
  1340. if(!$_FILES['files']['tmp_name'][0]){
  1341. $this->error('未上传证明图片');
  1342. }
  1343. try{
  1344. $files = request()->file('files');
  1345. foreach($files as $file){
  1346. // 移动到框架应用根目录/uploads/ 目录下
  1347. $info = $file->validate(config('app.admin_upload_img'))->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1348. if($info){
  1349. $img = '/'.$updir.'/files/' . $info->getSaveName();
  1350. $img = str_replace('\\', '/', $img);
  1351. $path = request()->domain(true).$img;
  1352. $imgs[] = $path;
  1353. }else{
  1354. \exception($file->getError());
  1355. }
  1356. }
  1357. }catch (\Exception $e){
  1358. $this->error($e->getMessage());
  1359. }
  1360. $data = [
  1361. 'name' => input('name','','trim'),
  1362. 'phone' => input('TEL','','trim'),
  1363. 'imgs' => $imgs?implode(',',$imgs):'',
  1364. 'org_id' => $this->orgId,
  1365. 'user_id' => $this->user['id'],
  1366. 'status' => 0,
  1367. 'create_time' => date('Y-m-d H:i:s'),
  1368. 'remark' => input('remark','','trim')
  1369. ];
  1370. if(!$data['name']){
  1371. $this->error('请输入姓名');
  1372. }
  1373. if(!$data['phone']){
  1374. $this->error('请输入手机号');
  1375. }
  1376. if(!check_mobile($data['phone'])){
  1377. $this->error('手机号格式错误');
  1378. }
  1379. $ret = Db::name('dinner_apply')->insert($data);
  1380. if(!$ret){
  1381. $this->error('操作失败');
  1382. }else {
  1383. $this->redirect(url('order'));
  1384. }
  1385. }else{
  1386. return $this->fetch();
  1387. }
  1388. }
  1389. public function get_orders_json()
  1390. {
  1391. $user = $this->user;
  1392. $size = input('size');
  1393. $page = input('page');
  1394. $ret=$this->show_all_orders($user['id'], $page, $size);
  1395. if (empty($ret)) {
  1396. header('Content-Type:application/json; charset=utf-8');
  1397. exit(json_encode([]));
  1398. } else {
  1399. header('Content-Type:application/json; charset=utf-8');
  1400. exit(json_encode($ret));
  1401. }
  1402. }
  1403. public function show_all_orders($wxuserid, $page, $size)
  1404. {
  1405. $map[] = ['user_id','=',$wxuserid];
  1406. $ret = Db::name('dinner_order')
  1407. ->where($map)
  1408. ->page($page,$size)
  1409. ->order('id','desc')->select();
  1410. foreach ($ret as $key => $value) {
  1411. $ret[$key]['Todo'] = null;
  1412. $ret[$key]['pay'] = 0;
  1413. $ret[$key]['refund'] = 0;
  1414. if($value['state'] == 0){
  1415. $tt = time() - strtotime($value['create_time']);
  1416. if($tt < 30*60){ // 半小时内可以支付
  1417. $ret[$key]['pay'] = 1;
  1418. }
  1419. }
  1420. if($value['state']== 1||$value['state'] == 3){
  1421. if(in_array($value['is_ok'],[0,2])){
  1422. $day = date('Y-m-d');
  1423. $cday = date('Y-m-d',strtotime($value['create_time']));
  1424. if($day == $cday){ // 仅当天可退款
  1425. $ret[$key]['refund'] = 1;
  1426. }
  1427. }
  1428. }
  1429. }
  1430. return $ret;
  1431. }
  1432. public function order_detail(){
  1433. $id = input('id');
  1434. $info = Db::name('dinner_order')
  1435. ->where('id',$id)
  1436. ->find();
  1437. if(!$info){
  1438. $this->error('订单不存在');
  1439. }
  1440. if($info['cate'] == 0){
  1441. $all = Db::name('dinner_order_item')
  1442. ->alias('a')
  1443. ->join('dinner b','a.dinner_id=b.id')
  1444. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.content,b.imgs')
  1445. ->where('a.order_id',$info['id'])
  1446. ->where('a.is_refuse',0)
  1447. ->select();
  1448. foreach ($all as $k=>$v){
  1449. $im = $v['imgs']?explode(',',$v['imgs']):[];
  1450. $all[$k]['img'] = $im?$im[0]:'';
  1451. }
  1452. $refuse = Db::name('dinner_order_refuse_item')
  1453. ->alias('a')
  1454. ->join('dinner_order_refuse c','a.order_refuse_id=c.id')
  1455. ->join('dinner b','a.dinner_id=b.id')
  1456. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs')
  1457. ->where('c.dinner_order_id',$info['id'])
  1458. ->select();
  1459. foreach ($refuse as $k=>$v){
  1460. $im = $v['imgs']?explode(',',$v['imgs']):[];
  1461. $refuse[$k]['img'] = $im?$im[0]:'';
  1462. }
  1463. $this->assign('item',$all);
  1464. $this->assign('refuse_item',$refuse);
  1465. }else{ // 套餐
  1466. $all = Db::name('dinner_order_item')
  1467. ->alias('a')
  1468. ->join('dinner b','a.dinner_id=b.id')
  1469. ->join('dinner_group c','a.group_id=c.id')
  1470. ->join('dinner_type d','a.dinner_type_id=d.id')
  1471. ->field('a.*,b.name as dinner_name,c.title as group_title,d.name as type_name,b.type')
  1472. ->where('a.order_id',$info['id'])
  1473. ->where('a.is_refuse',0)
  1474. ->order('a.id asc')
  1475. ->select();
  1476. $all = $all?$all:[];
  1477. $types = [];
  1478. $tids = [];
  1479. foreach ($all as $k=>$v){
  1480. $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day'];
  1481. if(!in_array($tid,$tids)){
  1482. $tids[] = $tid;
  1483. }
  1484. }
  1485. $curday = date('Y-m-d');
  1486. // $curday = date('Y-m-d',strtotime($curday) + 24*60*60);
  1487. foreach ($tids as $tk=>$tv){
  1488. $dinners = [];
  1489. $type = 0;
  1490. $dinner_type_id = 0;
  1491. $dinner_type = '';
  1492. $group = '';
  1493. $group_id = 0;
  1494. $day = '';
  1495. foreach ($all as $k=>$v){
  1496. $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day'];
  1497. if($tid == $tv){
  1498. $dinner_type = $v['type_name'];
  1499. $dinner_type_id = $v['dinner_type_id'];
  1500. $group = $v['group_title'];
  1501. $group_id = $v['group_id'];
  1502. $day = $v['day'];
  1503. $dinners[] = $v['dinner_name'].'*'.$v['num'];
  1504. if($v['type'] == 1){
  1505. $type = 1;
  1506. }
  1507. }
  1508. }
  1509. $options = 1;
  1510. if($info['state'] != 1){
  1511. $options = 0;
  1512. }
  1513. if($day <= $curday){
  1514. $options = 0;
  1515. }
  1516. $types[] = [
  1517. 'dinner_type_id' => $dinner_type_id,
  1518. 'dinner_type' => $dinner_type,
  1519. 'day' => $day,
  1520. 'options' => $options,
  1521. 'group' => $group,
  1522. 'group_id' => $group_id,
  1523. 'type' => $type,
  1524. 'dinners' => implode(',',$dinners),
  1525. 'sign' => think_encrypt($info['id'].'|'.$dinner_type_id.'|'.$day.'|'.$group_id)
  1526. ];
  1527. }
  1528. $info['types'] = $types;
  1529. }
  1530. $info['orgName'] = Db::name('org')
  1531. ->where('id',$info['org_id'])
  1532. ->value('name');
  1533. $this->assign('info',$info);
  1534. return $this->fetch();
  1535. }
  1536. public function canceldinner(){
  1537. $sign = think_decrypt(input('sign','','trim'));
  1538. if(!$sign){
  1539. $this->error('参数错误');
  1540. }
  1541. $signs = explode('|',$sign);
  1542. if(count($signs) != 4){
  1543. $this->error('参数错误');
  1544. }
  1545. $orderId = $signs[0];
  1546. $dinnerTypeId = $signs[1];
  1547. $day = $signs[2];
  1548. $groupId = $signs[3];
  1549. $cur = date('Y-m-d');
  1550. if($day <= $cur){
  1551. $this->error('只能取消明天或之后的餐');
  1552. }
  1553. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1554. if(!$order){
  1555. $this->error("订单不存在");
  1556. }
  1557. if($order['state'] != 1){
  1558. $this->error('订单该状态无法操作');
  1559. }
  1560. if($order['user_id'] != $this->user['id']){
  1561. $this->error('无权限操作');
  1562. }
  1563. // 取出
  1564. $ids = Db::name('dinner_order_item')
  1565. ->where('is_refuse',0)
  1566. ->where('dinner_type_id',$dinnerTypeId)
  1567. ->where('group_id',$groupId)
  1568. ->where('day',$day)
  1569. ->where('order_id',$orderId)
  1570. ->column('id');
  1571. if(!$ids){
  1572. $this->error("参数错误或已取消");
  1573. }
  1574. Db::startTrans();
  1575. try{
  1576. // 查询订单下是否还有有效餐品
  1577. $ids2 = Db::name('dinner_order_item')
  1578. ->where('is_refuse',0)
  1579. ->where('id','not in',$ids)
  1580. ->where('order_id',$orderId)
  1581. ->column('id');
  1582. if(!$ids2){ // 没有有效的餐品,订单改完已退款状态
  1583. $ret = Db::name('dinner_order')->where('id',$orderId)->update(['state'=>5,'update_time'=>date('Y-m-d H:i;s')]);
  1584. if(!$ret){
  1585. \exception('操作失败');
  1586. }
  1587. }
  1588. $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]);
  1589. if(!$ret){
  1590. \exception('操作失败');
  1591. }
  1592. Db::commit();
  1593. }catch (\Exception $e){
  1594. Db::rollback();
  1595. $this->error('操作失败');
  1596. }
  1597. $this->success('操作成功');
  1598. }
  1599. public function changedinner(){
  1600. $initsign = input('sign','','trim');
  1601. $sign = think_decrypt($initsign);
  1602. if(!$sign){
  1603. if(request()->isPost()){
  1604. $this->error('参数错误');
  1605. }else{
  1606. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1607. }
  1608. }
  1609. $signs = explode('|',$sign);
  1610. if(count($signs) != 4){
  1611. if(request()->isPost()){
  1612. $this->error('参数错误');
  1613. }else{
  1614. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1615. }
  1616. }
  1617. $orderId = $signs[0];
  1618. $dinnerTypeId = $signs[1];
  1619. $day = $signs[2];
  1620. $groupId = $signs[3];
  1621. if(request()->isPost()){
  1622. $dinnerId = input('dinner_id/d',0);
  1623. if($dinnerId <= 0){
  1624. $this->error('请选择要替换的流食或半流食餐品');
  1625. }
  1626. $info = Db::name('dinner_group_item')
  1627. ->alias('a')
  1628. ->join('dinner b','a.dinner_id = b.id')
  1629. ->where('a.id',$dinnerId)
  1630. ->where('b.dinner_type_id',$dinnerTypeId)
  1631. ->where('b.type',1)
  1632. ->where('b.enable',1)
  1633. ->field('a.*,b.money as oldmoney')
  1634. ->find();
  1635. if(!$info){
  1636. $this->error('选择的流食或半流食餐品不存在或已下架');
  1637. }
  1638. if($info['money'] == 9 && $info['old_money'] > 0){
  1639. $info['money'] = $info['old_money'];
  1640. }
  1641. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1642. if(!$order){
  1643. $this->error('订单不存在');
  1644. }
  1645. if($order['state'] != 1){
  1646. $this->error('订单该状态无法操作');
  1647. }
  1648. if($order['user_id'] != $this->user['id']){
  1649. $this->error('无权限操作');
  1650. }
  1651. // 取出要退款的餐品
  1652. $ids = Db::name('dinner_order_item')
  1653. ->where('is_refuse',0)
  1654. ->where('dinner_type_id',$dinnerTypeId)
  1655. ->where('group_id',$groupId)
  1656. ->where('day',$day)
  1657. ->where('order_id',$orderId)
  1658. ->column('id');
  1659. if(!$ids){
  1660. $this->error("参数错误或已取消");
  1661. }
  1662. $tmoney = Db::name('dinner_order_item')->where('id','in',$ids)->sum('price');
  1663. $money = $order['money'] - $tmoney + $info['money'];
  1664. $img = $order['img'];
  1665. if(!$img){
  1666. if(!$_FILES['files']['tmp_name']){
  1667. $this->error('未上传医嘱证明');
  1668. }
  1669. try{
  1670. $updir = config('upload_dir');
  1671. $file = request()->file('files');
  1672. $config = config('admin_upload_img');
  1673. $finfo = $file->validate($config)
  1674. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1675. if($finfo){
  1676. $img = '/'.$updir.'/files/' . $finfo->getSaveName();
  1677. $img = request()->domain(true).str_replace('\\', '/', $img);
  1678. }else{
  1679. // 上传失败获取错误信息
  1680. \exception($file->getError());
  1681. }
  1682. }catch (\Exception $e){
  1683. $this->error($e->getMessage());
  1684. }
  1685. }
  1686. Db::startTrans();
  1687. try{
  1688. $ret = Db::name('dinner_order')->where('id',$orderId)->update(['money'=>$money,'img'=>$img,'type'=>1,'update_time'=>date('Y-m-d H:i;s')]);
  1689. if(!$ret){
  1690. \exception('操作失败');
  1691. }
  1692. $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]);
  1693. if(!$ret){
  1694. \exception('操作失败');
  1695. }
  1696. // 添加流食商品
  1697. $ret = Db::name('dinner_order_item')->insert([
  1698. 'dinner_id' => $info['dinner_id'],
  1699. 'order_id' => $orderId,
  1700. 'price' => $info['money'],
  1701. 'original_price' => $info['money'],
  1702. 'money' => $info['money'],
  1703. 'num' => 1,
  1704. 'dinner_type_id' => $dinnerTypeId,
  1705. 'group_id' => $groupId,
  1706. 'day' => $day
  1707. ]);
  1708. if(!$ret){
  1709. \exception('操作失败');
  1710. }
  1711. Db::commit();
  1712. }catch (\Exception $e){
  1713. Db::rollback();
  1714. $this->error('操作失败');
  1715. }
  1716. $this->redirect(url('order_detail',['id'=>$orderId]));
  1717. }else{
  1718. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1719. if(!$order){
  1720. return $this->fetch('h5/msg_error',['msg'=>'订单不存在']);
  1721. }
  1722. $this->assign('order',$order);
  1723. // 获取流食商品
  1724. $dinners = Db::name('dinner_group_item')
  1725. ->alias('a')
  1726. ->join('dinner b','a.dinner_id = b.id')
  1727. ->where('a.day',$day)
  1728. ->where('a.group_id',$groupId)
  1729. ->where('b.dinner_type_id',$dinnerTypeId)
  1730. ->where('b.type',1)
  1731. ->field('a.id,b.name,b.money as oldmoney,a.money')
  1732. ->select();
  1733. $dinners = $dinners?$dinners:[];
  1734. foreach ($dinners as $k=>$v){
  1735. if($v['money'] == 0 && $v['oldmoney'] > 0){
  1736. $dinners[$k]['money'] = $v['oldmoney'];
  1737. }
  1738. }
  1739. $this->assign('dinners',$dinners);
  1740. $group = Db::name('dinner_group')->where('id',$groupId)->value('title');
  1741. $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name');
  1742. $ids = Db::name('dinner_order_item')
  1743. ->where('is_refuse',0)
  1744. ->where('dinner_type_id',$dinnerTypeId)
  1745. ->where('group_id',$groupId)
  1746. ->where('day',$day)
  1747. ->where('order_id',$orderId)
  1748. ->column('dinner_id');
  1749. $names = [];
  1750. if($ids){
  1751. $names = Db::name('dinner')->where('id','in',$ids)->column('name');
  1752. }
  1753. $this->assign('group',$group?$group:'');
  1754. $this->assign('dinnertype',$dinnertype?$dinnertype:'');
  1755. $this->assign('day',$day);
  1756. $this->assign('dinnernames',$names?implode(',',$names):'');
  1757. $this->assign('orderId',$orderId);
  1758. $this->assign('sign',$initsign);
  1759. return $this->fetch();
  1760. }
  1761. }
  1762. public function refunddinner(){
  1763. $initsign = input('sign','','trim');
  1764. $sign = think_decrypt($initsign);
  1765. if(!$sign){
  1766. if(request()->isPost()){
  1767. $this->error('参数错误');
  1768. }else{
  1769. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1770. }
  1771. }
  1772. $signs = explode('|',$sign);
  1773. if(count($signs) != 4){
  1774. if(request()->isPost()){
  1775. $this->error('参数错误');
  1776. }else{
  1777. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1778. }
  1779. }
  1780. $curday = date('Y-m-d');
  1781. // $curday = date('Y-m-d',strtotime($curday) + 24*60*60);
  1782. $orderId = $signs[0];
  1783. $dinnerTypeId = $signs[1];
  1784. $day = $signs[2];
  1785. $groupId = $signs[3];
  1786. if(request()->isPost()){
  1787. $day = input('day',0);
  1788. if(!$day){
  1789. $this->error('请选择要退餐的日期');
  1790. }
  1791. if($day <= $curday){
  1792. $this->error('已超过退餐时间,不能退餐');
  1793. }
  1794. $data = Db::name('dinner_order')
  1795. ->where('id',$orderId)
  1796. ->find();
  1797. if (!in_array($data['state'],[1])) {
  1798. $this->error('订单号不是已支付状态,不能执行退款');
  1799. }
  1800. if ($data['is_replace'] !=0) {
  1801. $this->error('不是您自己订的不能退款,退款请联系工作人员');
  1802. }
  1803. if (!in_array($data['is_ok'],[0,2])) {
  1804. $this->error('当前订单已标记完成');
  1805. }
  1806. $items = Db::name('dinner_order_item')->where('order_id',$orderId)->where('day',$day)->select();
  1807. $ids = [];
  1808. foreach ($items as $k=>$v){
  1809. if($v['is_refuse'] == 1){
  1810. $this->error('已有商品退餐,勿重复退餐');
  1811. }
  1812. $ids[] = $v['id'];
  1813. }
  1814. Db::startTrans();
  1815. try{
  1816. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  1817. 'dinner_order_id'=>$orderId
  1818. ,'refuse_price'=>0
  1819. ,'refuse_y'=>date('Y')
  1820. ,'refuse_ymd'=>date('Ymd')
  1821. ,'refuse_ym'=>date('Ym')
  1822. ,'created_user_id'=>$this->user['id']
  1823. ,'create_time'=>date('Y-m-d H:i:s')
  1824. ,'refuse_w'=>date('Y').date('W')
  1825. ,'order_type'=>$data['order_type']
  1826. ,'is_replace'=>$data['is_replace']
  1827. ,'org_id'=>$this->orgId
  1828. ,'status'=>4
  1829. ,'refuse_remark'=>''
  1830. ));
  1831. if(!$refuse_id){
  1832. \exception('退餐单添加失败');
  1833. }
  1834. $refuse_item = [];
  1835. foreach ($items as $k=>$v){
  1836. $refuse_item[] = [
  1837. 'dinner_id'=>$v['dinner_id'],
  1838. 'order_refuse_id'=>$refuse_id,
  1839. 'price'=>$v['price'],
  1840. 'num'=>$v['num'],
  1841. 'dinner_type_id'=>$v['dinner_type_id'],
  1842. 'day' => $v['day'],
  1843. 'group_id' => $v['group_id']
  1844. ];
  1845. }
  1846. $rr = Db::name('dinner_order_refuse_item')
  1847. ->insertAll($refuse_item);
  1848. if(!$rr){
  1849. \exception('操作失败2');
  1850. }
  1851. $rr = Db::name('dinner_order_item')
  1852. ->where('id','in',$ids)
  1853. ->update([
  1854. 'is_refuse'=>1
  1855. ]);
  1856. if(!$rr){
  1857. \exception('操作失败1');
  1858. }
  1859. // $res = Db::name('dinner_order')
  1860. // ->where('id',$orderId)
  1861. // ->update([
  1862. // 'state'=>4,
  1863. // 'update_time'=>getTime()
  1864. // ]);
  1865. //
  1866. // if(!$res){
  1867. // \exception('订单更新失败');
  1868. // }
  1869. Db::commit();
  1870. }catch (\Exception $e){
  1871. Db::rollback();
  1872. $this->error('操作失败'.$e->getMessage());
  1873. }
  1874. $this->redirect(url('order_detail',['id'=>$orderId]));
  1875. }else{
  1876. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1877. if(!$order){
  1878. return $this->fetch('h5/msg_error',['msg'=>'订单不存在']);
  1879. }
  1880. $this->assign('order',$order);
  1881. $days = Db::name('dinner_order_item')
  1882. ->where('is_refuse',0)
  1883. ->where('dinner_type_id',$dinnerTypeId)
  1884. ->where('group_id',$groupId)
  1885. ->where('order_id',$orderId)
  1886. ->where('day','>',$curday)
  1887. ->group('day')
  1888. ->column('day');
  1889. $this->assign('day',$day);
  1890. $this->assign('days',$days);
  1891. // $group = Db::name('dinner_group')->where('id',$groupId)->value('title');
  1892. // $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name');
  1893. //
  1894. // $ids = Db::name('dinner_order_item')
  1895. // ->where('is_refuse',0)
  1896. // ->where('dinner_type_id',$dinnerTypeId)
  1897. // ->where('group_id',$groupId)
  1898. // ->where('day',$day)
  1899. // ->where('order_id',$orderId)
  1900. // ->column('dinner_id');
  1901. // $names = [];
  1902. // if($ids){
  1903. // $names = Db::name('dinner')->where('id','in',$ids)->column('name');
  1904. // }
  1905. // $this->assign('group',$group?$group:'');
  1906. // $this->assign('dinnertype',$dinnertype?$dinnertype:'');
  1907. // $this->assign('day',$day);
  1908. // $this->assign('dinnernames',$names?implode(',',$names):'');
  1909. $this->assign('orderId',$orderId);
  1910. $this->assign('sign',$initsign);
  1911. return $this->fetch();
  1912. }
  1913. }
  1914. public function refuse(){
  1915. $orderid = input('id');
  1916. if (empty($orderid)) {
  1917. return $this->fetch('h5/msg_error',['msg'=>'订单号不能为空']);
  1918. }
  1919. $data = Db::name('dinner_order')
  1920. ->where('id',$orderid)
  1921. ->find();
  1922. if (!in_array($data['state'],[1])) {
  1923. return $this->fetch('h5/msg_error',['msg'=>'订单号不是已支付状态,不能执行退款']);
  1924. }
  1925. if ($data['is_replace'] !=0) {
  1926. return $this->fetch('h5/msg_error',['msg'=>'不是您自己订的不能退款,退款请联系工作人员']);
  1927. }
  1928. if (!in_array($data['is_ok'],[0,2])) {
  1929. return $this->fetch('h5/msg_error',['msg'=>'当前订单已标记完成']);
  1930. }
  1931. if($data['state'] == 1 ||$data['state'] == 3){
  1932. $day = date('Y-m-d');
  1933. $cday = date('Y-m-d',strtotime($data['create_time']));
  1934. if($day != $cday){ // 仅当天可退款
  1935. return $this->fetch('h5/msg_error',['msg'=>'订单仅当天能退款']);
  1936. }
  1937. }
  1938. $all = Db::name('dinner_order_item')
  1939. ->alias('a')
  1940. ->join('dinner b','a.dinner_id=b.id')
  1941. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs')
  1942. ->where('a.order_id',$orderid)
  1943. ->where('a.is_refuse',0)
  1944. ->select();
  1945. foreach ($all as $k=>$v){
  1946. $im = explode(',',$v['imgs']);
  1947. $all[$k]['img'] = $im[0];
  1948. }
  1949. $data['cartInfo'] = $all;
  1950. if (request()->isGet()) {
  1951. $this->assign('data',$data);
  1952. $this->assign('id',$orderid);
  1953. return $this->fetch();
  1954. } else {
  1955. $d = request()->post();
  1956. if (empty($d)) {
  1957. HelpHander::error('没有选择退单的商品');
  1958. }
  1959. $post_d = array();
  1960. foreach ($d as $key => $value) {
  1961. $arr = explode("_", $key);
  1962. $post_d[] = $arr[1];
  1963. }
  1964. $refuse_price = 0;
  1965. $items = Db::name('dinner_order_item')
  1966. ->where('id','in',$post_d)
  1967. ->select();
  1968. foreach ($items as $k=>$v){
  1969. if($v['is_refuse']!=0){
  1970. HelpHander::error('当前选择物品有已退单物品,请刷新,重新选择');
  1971. }
  1972. $refuse_price+=$v['price'];
  1973. }
  1974. try{
  1975. Db::startTrans();
  1976. if($data['is_spot']==0){//预定餐
  1977. $config3 = Db::name('dinner_config')
  1978. ->where('org_id',$data['org_id'])
  1979. ->find();
  1980. $zd = 0;//0自动退款1审核退款
  1981. if(empty($config3) || empty($config3['content3'])){
  1982. $zd =1;
  1983. }else{
  1984. $cur = date('Y-m-d H:i');
  1985. $cc = date('Y-m-d H:i',strtotime(date('Y-m-d').' '.$config3['content3']));
  1986. if($cur >$cc){
  1987. $zd = 1;
  1988. }else{
  1989. $zd = 0;
  1990. }
  1991. }
  1992. if($zd == 0){
  1993. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  1994. 'dinner_order_id'=>$orderid
  1995. ,'refuse_price'=>$refuse_price
  1996. ,'refuse_y'=>date('Y')
  1997. ,'refuse_ymd'=>date('Ymd')
  1998. ,'refuse_ym'=>date('Ym')
  1999. ,'created_user_id'=>$this->user['id']
  2000. ,'create_time'=>date('Y-m-d H:i:s')
  2001. ,'refuse_w'=>date('Y').date('W')
  2002. ,'order_type'=>$data['order_type']
  2003. ,'is_replace'=>$data['is_replace']
  2004. ,'org_id'=>$this->orgId
  2005. ,'status'=>5
  2006. ,'refuse_remark'=>'自动退款'
  2007. ));
  2008. if(!$refuse_id){
  2009. \exception('退款单添加失败');
  2010. }
  2011. $refuse_item = [];
  2012. foreach ($items as $k=>$v){
  2013. $refuse_item[] = [
  2014. 'dinner_id'=>$v['dinner_id'],
  2015. 'order_refuse_id'=>$refuse_id,
  2016. 'price'=>$v['price'],
  2017. 'num'=>$v['num'],
  2018. 'dinner_type_id'=>$v['dinner_type_id'],
  2019. ];
  2020. }
  2021. $rr = Db::name('dinner_order_refuse_item')
  2022. ->insertAll($refuse_item);
  2023. if(!$rr){
  2024. \exception('操作失败');
  2025. }
  2026. $rr = Db::name('dinner_order_item')
  2027. ->where('id','in',$post_d)
  2028. ->update([
  2029. 'is_refuse'=>1
  2030. ]);
  2031. if(!$rr){
  2032. \exception('操作失败');
  2033. }
  2034. $checkAll = Db::name('dinner_order_item')
  2035. ->where('order_id',$orderid)
  2036. ->where('is_refuse',0)
  2037. ->find();
  2038. if($checkAll){//部分退款
  2039. $res = Db::name('dinner_order')
  2040. ->where('id',$orderid)
  2041. ->update([
  2042. 'state'=>7,
  2043. 'update_time'=>getTime()
  2044. ]);
  2045. }else{//全部退款
  2046. $res = Db::name('dinner_order')
  2047. ->where('id',$orderid)
  2048. ->update([
  2049. 'state'=>5,
  2050. 'update_time'=>getTime()
  2051. ]);
  2052. }
  2053. if(!$res){
  2054. \exception('订单更新失败');
  2055. }
  2056. $config = get_pay_wechat($data['org_id']);
  2057. $app = \EasyWeChat\Factory::payment($config);
  2058. $ret = $app->refund->byOutTradeNumber($data['sn'],
  2059. 'T' . $data['sn'],
  2060. $data['price'] * 100, $refuse_price * 100);
  2061. if ($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS') {
  2062. trace(json_encode($ret));
  2063. \exception('微信退订失败:'.$ret['return_msg']);
  2064. }
  2065. }else{
  2066. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  2067. 'dinner_order_id'=>$orderid
  2068. ,'refuse_price'=>$refuse_price
  2069. ,'refuse_y'=>date('Y')
  2070. ,'refuse_ymd'=>date('Ymd')
  2071. ,'refuse_ym'=>date('Ym')
  2072. ,'created_user_id'=>$this->user['id']
  2073. ,'create_time'=>date('Y-m-d H:i:s')
  2074. ,'refuse_w'=>date('Y').date('W')
  2075. ,'order_type'=>$data['order_type']
  2076. ,'is_replace'=>$data['is_replace']
  2077. ,'org_id'=>$this->orgId
  2078. ,'status'=>4
  2079. ));
  2080. if(!$refuse_id){
  2081. \exception('退款单添加失败');
  2082. }
  2083. $refuse_item = [];
  2084. foreach ($items as $k=>$v){
  2085. $refuse_item[] = [
  2086. 'dinner_id'=>$v['dinner_id'],
  2087. 'order_refuse_id'=>$refuse_id,
  2088. 'price'=>$v['price'],
  2089. 'num'=>$v['num'],
  2090. 'dinner_type_id'=>$v['dinner_type_id'],
  2091. ];
  2092. }
  2093. $rr = Db::name('dinner_order_refuse_item')
  2094. ->insertAll($refuse_item);
  2095. if(!$rr){
  2096. \exception('操作失败');
  2097. }
  2098. $rr = Db::name('dinner_order_item')
  2099. ->where('id','in',$post_d)
  2100. ->update([
  2101. 'is_refuse'=>1
  2102. ]);
  2103. if(!$rr){
  2104. \exception('操作失败');
  2105. }
  2106. $res = Db::name('dinner_order')
  2107. ->where('id',$orderid)
  2108. ->update([
  2109. 'state'=>4,
  2110. 'update_time'=>getTime()
  2111. ]);
  2112. if(!$res){
  2113. \exception('订单更新失败');
  2114. }
  2115. }
  2116. }else{
  2117. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  2118. 'dinner_order_id'=>$orderid
  2119. ,'refuse_price'=>$refuse_price
  2120. ,'refuse_y'=>date('Y')
  2121. ,'refuse_ymd'=>date('Ymd')
  2122. ,'refuse_ym'=>date('Ym')
  2123. ,'created_user_id'=>$this->user['id']
  2124. ,'create_time'=>date('Y-m-d H:i:s')
  2125. ,'refuse_w'=>date('Y').date('W')
  2126. ,'order_type'=>$data['order_type']
  2127. ,'is_replace'=>$data['is_replace']
  2128. ,'org_id'=>$this->orgId
  2129. ,'status'=>4
  2130. ));
  2131. if(!$refuse_id){
  2132. \exception('退款单添加失败');
  2133. }
  2134. $refuse_item = [];
  2135. foreach ($items as $k=>$v){
  2136. $refuse_item[] = [
  2137. 'dinner_id'=>$v['dinner_id'],
  2138. 'order_refuse_id'=>$refuse_id,
  2139. 'price'=>$v['price'],
  2140. 'num'=>$v['num'],
  2141. 'dinner_type_id'=>$v['dinner_type_id'],
  2142. ];
  2143. }
  2144. $rr = Db::name('dinner_order_refuse_item')
  2145. ->insertAll($refuse_item);
  2146. if(!$rr){
  2147. \exception('操作失败');
  2148. }
  2149. $rr = Db::name('dinner_order_item')
  2150. ->where('id','in',$post_d)
  2151. ->update([
  2152. 'is_refuse'=>1
  2153. ]);
  2154. if(!$rr){
  2155. \exception('操作失败');
  2156. }
  2157. $res = Db::name('dinner_order')
  2158. ->where('id',$orderid)
  2159. ->update([
  2160. 'state'=>4,
  2161. 'update_time'=>getTime()
  2162. ]);
  2163. if(!$res){
  2164. \exception('订单更新失败');
  2165. }
  2166. }
  2167. Db::commit();
  2168. HelpHander::success([],'操作成功');
  2169. }catch (Exception $e){
  2170. Db::rollback();
  2171. trace($e->getMessage());
  2172. HelpHander::error($e->getMessage());
  2173. }
  2174. }
  2175. }
  2176. public function loginuser(){
  2177. if (request()->isGet()) {
  2178. return $this->fetch();
  2179. } else {
  2180. $tel = input('TEL');
  2181. $code = input('VCODE');
  2182. if(!$tel){
  2183. HelpHander::error('手机不能为空');
  2184. }
  2185. $info = Db::name('dinner_user')
  2186. ->where('org_id',$this->orgId)
  2187. ->where('mobile',$tel)
  2188. ->where('del',0)
  2189. ->where('enable',1)
  2190. ->find();
  2191. if(!$info){
  2192. HelpHander::error('点餐员不存在');
  2193. }
  2194. if(!$code){
  2195. HelpHander::error('验证码不能为空');
  2196. }
  2197. if(!verify_sms($tel,$code)){
  2198. HelpHander::error('验证码信息错误');
  2199. }
  2200. $user = $this->user;
  2201. $user['isdinder'] = 1;
  2202. encodecookie('isdinder',1);
  2203. // 清空购物车
  2204. Db::name('dinner_cart')->where('user_id')->delete();
  2205. HelpHander::success([],'操作成功');
  2206. }
  2207. }
  2208. public function sms(){
  2209. $phone = input('tel');
  2210. if(empty($phone)){
  2211. HelpHander::error('手机号不能为空');
  2212. }
  2213. $res = send_verify_sms($phone);
  2214. if(!$res){
  2215. HelpHander::error('发送失败');
  2216. }
  2217. HelpHander::success([],'发送成功');
  2218. }
  2219. }