Lessonorder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\library\Email;
  6. /**
  7. * 售课预约订单
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Lessonorder extends Backend
  12. {
  13. /**
  14. * Lessonorder模型对象
  15. * @var \app\admin\model\Lessonorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Lessonorder;
  22. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  23. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //拿slot_id
  41. $filter = $this->request->get('filter');
  42. $filter = json_decode($filter,true);
  43. $slot_id = isset($filter['slot_id']) ? $filter['slot_id'] : 0;
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['user','slot','lesson','coach','packageorder','lessonpackage','trylessonorder','trylesson'])
  51. ->where($where)
  52. ->where('lessonorder.order_status !=0 or lessonorder.jointype = 2') //已支付的 或 候补单
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['firstname','lastname']);
  57. $row->getRelation('slot')->visible(['starttime','endtime']);
  58. $row->getRelation('lesson')->visible(['name','name_en','type']);
  59. $row->getRelation('coach')->visible(['nickname']);
  60. $row->getRelation('lessonpackage')->visible(['name','name_en']);
  61. $row->getRelation('trylesson')->visible(['name','name_en']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items(), "extend"=>[ 'slot_id' => $slot_id ]);
  64. return json($result);
  65. }
  66. $is_back = input('is_back',0);
  67. $this->assign('is_back',$is_back);
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 取消
  72. */
  73. public function cancel(){
  74. $id = input('id');
  75. $info = Db::name('lesson_order')->where('id',$id)->find();
  76. if(!$info){
  77. $this->error('请刷新重试');
  78. }
  79. if($this->request->isPost()){
  80. $cancel_reason = input('cancel_reason','');
  81. $cancel_time = strtotime(input('cancel_time',''));
  82. Db::startTrans();
  83. //找到所有的已报名订单
  84. $info = Db::name('lesson_order')->where('id',$id)->lock(true)->find();
  85. if(empty($info)){
  86. Db::rollback();
  87. $this->error('请刷新重试');
  88. }
  89. //取消候补单
  90. if($info['jointype'] == 2){
  91. $update = [
  92. 'order_status' => 30,
  93. 'cancel_time' => $cancel_time,
  94. 'cancel_reason' => $cancel_reason,
  95. ];
  96. $rs = Db::name('lesson_order')->where('id',$id)->update($update);
  97. Db::commit();
  98. $this->success('取消完成');
  99. }
  100. //剩下的都是预约单
  101. if($info['order_status'] != 10){
  102. Db::rollback();
  103. $this->error('此订单已无法取消,请刷新重试');
  104. }
  105. //套餐给加回去
  106. if($info['paytype'] == 1){
  107. $package_order = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
  108. $update = [
  109. 'remain' => bcadd($package_order['remain'],$info['usernumber_hours'],1),
  110. 'updatetime' => time(),
  111. ];
  112. $rs_remain = Db::name('package_order')->where('id',$info['package_order_id'])->update($update);
  113. if($rs_remain === false){
  114. Db::rollback();
  115. $this->error('取消失败');
  116. }
  117. }
  118. //试课给改回去
  119. if($info['paytype'] == 4){
  120. $update = [
  121. 'order_status' => 10,
  122. 'updatetime' => time(),
  123. 'lesson_order_id' => 0,
  124. ];
  125. $rs_remain = Db::name('trylesson_order')->where('id',$info['trylesson_order_id'])->update($update);
  126. if($rs_remain === false){
  127. Db::rollback();
  128. $this->error('取消失败');
  129. }
  130. }
  131. //现金支付不给退,线下处理
  132. //取消预约单
  133. $update2 = [
  134. 'order_status' => 30,
  135. 'cancel_time' => $cancel_time,
  136. 'cancel_reason' => $cancel_reason,
  137. ];
  138. if($info['paytype'] == 1 || $info['paytype'] == 4){
  139. $update2['order_status'] = 40;
  140. }
  141. $rs = Db::name('lesson_order')->where('id',$info['id'])->update($update2);
  142. if($rs === false){
  143. Db::rollback();
  144. $this->error('取消失败');
  145. }
  146. //候补上位
  147. $slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  148. $wait_rs = $this->lesson_order_wait($info,$slot);
  149. //更新已预约人数
  150. $pay_number = Db::name('lesson_order')->where('slot_id',$info['slot_id'])->where('order_status',10)->sum('usernumber');
  151. $rs_slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->update(['bookednum' => $pay_number]);
  152. if($rs_slot === false){
  153. Db::rollback();
  154. $this->error('取消失败');
  155. }
  156. Db::commit();
  157. $slot_info = $slot;
  158. $lesson_info = Db::name('lesson')->where('id',$slot_info['lesson_id'])->find();
  159. //给用户发通知
  160. $user_info = Db::name('user')->where('id',$info['user_id'])->find();
  161. if(!empty($user_info['email'])){
  162. try {
  163. $message =
  164. 'Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].'!<br/>
  165. We regret to inform you that the following class has been cancelled.<br/>
  166. Class:'.$lesson_info['name_en'].'<br/>
  167. Date: '.date('d F Y',$slot_info['starttime']).'<br/>
  168. Time: '.date('H:i a',$slot_info['starttime']).'<br/>
  169. Thank you for your kind understanding and look forward to seeing you in our studio soon!❤<br/>
  170. Best Regards,<br/>
  171. Elin Dance Studio<br/>
  172. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  173. ';
  174. $obj = new Email();
  175. $result = $obj
  176. ->to($user_info['email'])
  177. ->subject('Class is Cancelled!')
  178. ->message($message)
  179. ->send();
  180. $obj = '';
  181. $coach_name = Db::name('coach')->where('id',$slot_info['coach_ids'])->value('nickname');
  182. //发whatsapp
  183. $parameters = [
  184. [
  185. 'type' => 'text',
  186. 'text' => $user_info['firstname'].' '.$user_info['lastname'],
  187. ],
  188. [
  189. 'type' => 'text',
  190. 'text' => $lesson_info['name_en'],
  191. ],
  192. /*[
  193. 'type' => 'text',
  194. 'text' => $coach_name,
  195. ],*/
  196. [
  197. 'type' => 'text',
  198. 'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
  199. ],
  200. ];
  201. $this->whatapp($user_info['whatsapp'],'class_cancelled','en_US',$parameters);
  202. } catch (Exception $e) {
  203. }
  204. }
  205. $this->success('取消完成');
  206. }
  207. $this->view->assign('row',$info);
  208. return $this->view->fetch();
  209. }
  210. //候补上位
  211. private function lesson_order_wait($lesson_order,$slot){
  212. //找到所有的候补单
  213. $houbu_list = Db::name('lesson_order')->alias('order')
  214. ->field('order.*,user.firstname,user.lastname,user.email,user.whatsapp,lesson.name_en')
  215. ->join('user','order.user_id = user.id','LEFT')
  216. ->join('lesson','order.lesson_id = lesson.id','LEFT')
  217. ->where('order.slot_id',$lesson_order['slot_id'])->where('order.jointype',2)->where('order.order_status',0)->order('order.id asc')->select();
  218. //有多少个空位
  219. $pay_number = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('jointype',1)->where('order_status',10)->sum('usernumber');
  220. $num_remain = $slot['num_max'] - $pay_number;
  221. $coach_name = Db::name('coach')->where('id',$slot['coach_ids'])->value('nickname');
  222. if(!empty($houbu_list)){
  223. $up_usernumber = 0; //此次循环转正的人数
  224. foreach($houbu_list as $key => $order){
  225. if($order['usernumber'] + $up_usernumber <= $num_remain){
  226. //此订单可以转正
  227. //检查可用的套餐,找一个就可以
  228. $map = [
  229. 'user_id' => $order['user_id'],
  230. 'endtime' => ['gt',time()],
  231. 'remain' => ['gt',$order['usernumber_hours']],
  232. 'order_status' => 1,
  233. 'use_status' => 1, //已激活的
  234. 'id' => $order['package_order_id'],
  235. ];
  236. $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $slot['lesson_id']])->lock(true)->find();
  237. if(!$package_order){
  238. continue;
  239. }
  240. //扣除一节。
  241. $update = [
  242. 'remain' => bcsub($package_order['remain'],$order['usernumber_hours'],1),
  243. 'updatetime' => time(),
  244. ];
  245. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  246. if($rs1 === false){
  247. Db::rollback();
  248. // $this->error('扣除套餐余额失败');
  249. $this->error('取消失败');
  250. }
  251. //修改预约单数据。延迟到转正的时候处理
  252. $update_order['order_amount'] = 0;
  253. $update_order['order_status'] = 10;
  254. $update_order['paytime'] = time();
  255. $update_order['package_order_id'] = $package_order['id'];
  256. $update_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $order['usernumber_hours']) .'/'. $package_order['sessions'];
  257. $update_order['paytype'] = 1; //修改支付方式为配套
  258. $update_order['jointype'] = 1; //修改加入方式为预约
  259. $rs2 = Db::name('lesson_order')->where('id',$order['id'])->update($update_order);
  260. if($rs2 === false){
  261. Db::rollback();
  262. // $this->error('扣除套餐余额失败');
  263. $this->error('取消失败');
  264. }
  265. //加上本次转正的人数
  266. $up_usernumber += $order['usernumber'];
  267. //额外的通知
  268. try {
  269. $message =
  270. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  271. We are excited to inform you that a spot has opened up for the following class. You are now booked into the class!<br/>
  272. Class:'.$order['name_en'].'<br/>
  273. Date: '.date('d F Y',$slot['starttime']).'<br/>
  274. Time: '.date('H:i a',$slot['starttime']).'<br/>
  275. We look forward to seeing you in the studio!❤<br/>
  276. Best Regards,<br/>
  277. Elin Dance Studio<br/>
  278. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  279. ';
  280. //给这些用户发邮件
  281. $obj = new Email();
  282. $result = $obj
  283. ->to($order['email'])
  284. ->subject('A spot for your waitlisted class is ready and you are now added in!')
  285. ->message($message)
  286. ->send();
  287. $obj = '';
  288. //发whatsapp
  289. $parameters = [
  290. [
  291. 'type' => 'text',
  292. 'text' => $order['firstname'].' '.$order['lastname'],
  293. ],
  294. [
  295. 'type' => 'text',
  296. 'text' => $order['name_en'],
  297. ],
  298. /*[
  299. 'type' => 'text',
  300. 'text' => $coach_name,
  301. ],*/
  302. [
  303. 'type' => 'text',
  304. 'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
  305. ],
  306. ];
  307. $this->whatapp($order['whatsapp'],'lessonorder_alternate_to_booking','en_US',$parameters);
  308. } catch (Exception $e) {
  309. }
  310. //额外的通知
  311. }else{
  312. continue;//跳出
  313. }
  314. }
  315. }
  316. return true;
  317. }
  318. //发送whatapp消息的方法
  319. private function whatapp($receive_mobile,$template,$code,$parameters){
  320. if(empty($receive_mobile)){return true;}
  321. $token = config('site.whatsapp_token');
  322. //发送者
  323. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  324. //发送
  325. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  326. $header = [
  327. 'Authorization: Bearer ' . $token,
  328. 'Content-Type: application/json',
  329. ];
  330. $body = [
  331. 'messaging_product' => 'whatsapp',
  332. 'recipient_type' => 'individual',
  333. 'to' => $receive_mobile,
  334. 'type' => 'template',
  335. 'template' => [
  336. 'name' => $template,
  337. 'language' => [
  338. 'code' => $code
  339. ],
  340. 'components' => [
  341. [
  342. 'type' => 'body',
  343. 'parameters' => $parameters
  344. ]
  345. ],
  346. ],
  347. ];
  348. $body = json_encode($body);
  349. $rs = curl_post($url,$body,$header);
  350. return true;
  351. }
  352. }