model = new \app\admin\model\Lessonorder;
$this->view->assign("orderStatusList", $this->model->getOrderStatusList());
$this->view->assign("paytypeList", $this->model->getPaytypeList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//拿slot_id
$filter = $this->request->get('filter');
$filter = json_decode($filter,true);
$slot_id = isset($filter['slot_id']) ? $filter['slot_id'] : 0;
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['user','slot','lesson','coach','packageorder','lessonpackage','trylessonorder','trylesson'])
->where($where)
->where('lessonorder.order_status !=0 or lessonorder.jointype = 2') //已支付的 或 候补单
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('user')->visible(['firstname','lastname']);
$row->getRelation('slot')->visible(['starttime','endtime']);
$row->getRelation('lesson')->visible(['name','name_en','type']);
$row->getRelation('coach')->visible(['nickname']);
$row->getRelation('lessonpackage')->visible(['name','name_en']);
$row->getRelation('trylesson')->visible(['name','name_en']);
}
$result = array("total" => $list->total(), "rows" => $list->items(), "extend"=>[ 'slot_id' => $slot_id ]);
return json($result);
}
$is_back = input('is_back',0);
$this->assign('is_back',$is_back);
return $this->view->fetch();
}
/**
* 取消
*/
public function cancel(){
$id = input('id');
$info = Db::name('lesson_order')->where('id',$id)->find();
if(!$info){
$this->error('请刷新重试');
}
if($this->request->isPost()){
$cancel_reason = input('cancel_reason','');
$cancel_time = strtotime(input('cancel_time',''));
Db::startTrans();
//找到所有的已报名订单
$info = Db::name('lesson_order')->where('id',$id)->lock(true)->find();
if(empty($info)){
Db::rollback();
$this->error('请刷新重试');
}
//取消候补单
if($info['jointype'] == 2){
$update = [
'order_status' => 30,
'cancel_time' => $cancel_time,
'cancel_reason' => $cancel_reason,
];
$rs = Db::name('lesson_order')->where('id',$id)->update($update);
Db::commit();
$this->success('取消完成');
}
//剩下的都是预约单
if($info['order_status'] != 10){
Db::rollback();
$this->error('此订单已无法取消,请刷新重试');
}
//套餐给加回去
if($info['paytype'] == 1){
$package_order = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
$update = [
'remain' => bcadd($package_order['remain'],$info['usernumber_hours'],1),
'updatetime' => time(),
];
$rs_remain = Db::name('package_order')->where('id',$info['package_order_id'])->update($update);
if($rs_remain === false){
Db::rollback();
$this->error('取消失败');
}
}
//试课给改回去
if($info['paytype'] == 4){
$update = [
'order_status' => 10,
'updatetime' => time(),
'lesson_order_id' => 0,
];
$rs_remain = Db::name('trylesson_order')->where('id',$info['trylesson_order_id'])->update($update);
if($rs_remain === false){
Db::rollback();
$this->error('取消失败');
}
}
//现金支付不给退,线下处理
//取消预约单
$update2 = [
'order_status' => 30,
'cancel_time' => $cancel_time,
'cancel_reason' => $cancel_reason,
];
if($info['paytype'] == 1 || $info['paytype'] == 4){
$update2['order_status'] = 40;
}
$rs = Db::name('lesson_order')->where('id',$info['id'])->update($update2);
if($rs === false){
Db::rollback();
$this->error('取消失败');
}
//候补上位
$slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
$wait_rs = $this->lesson_order_wait($info,$slot);
//更新已预约人数
$pay_number = Db::name('lesson_order')->where('slot_id',$info['slot_id'])->where('order_status',10)->sum('usernumber');
$rs_slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->update(['bookednum' => $pay_number]);
if($rs_slot === false){
Db::rollback();
$this->error('取消失败');
}
Db::commit();
$slot_info = $slot;
$lesson_info = Db::name('lesson')->where('id',$slot_info['lesson_id'])->find();
//给用户发通知
$user_info = Db::name('user')->where('id',$info['user_id'])->find();
if(!empty($user_info['email'])){
try {
$message =
'Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].'!
We regret to inform you that the following class has been cancelled.
Class:'.$lesson_info['name_en'].'
Date: '.date('d F Y',$slot_info['starttime']).'
Time: '.date('H:i a',$slot_info['starttime']).'
Thank you for your kind understanding and look forward to seeing you in our studio soon!❤
Best Regards,
Elin Dance Studio
';
$obj = new Email();
$result = $obj
->to($user_info['email'])
->subject('Class is Cancelled!')
->message($message)
->send();
$obj = '';
$coach_name = Db::name('coach')->where('id',$slot_info['coach_ids'])->value('nickname');
//发whatsapp
$parameters = [
[
'type' => 'text',
'text' => $user_info['firstname'].' '.$user_info['lastname'],
],
[
'type' => 'text',
'text' => $lesson_info['name_en'],
],
/*[
'type' => 'text',
'text' => $coach_name,
],*/
[
'type' => 'text',
'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
],
];
$this->whatapp($user_info['whatsapp'],'class_cancelled','en_US',$parameters);
} catch (Exception $e) {
}
}
$this->success('取消完成');
}
$this->view->assign('row',$info);
return $this->view->fetch();
}
//候补上位
private function lesson_order_wait($lesson_order,$slot){
//找到所有的候补单
$houbu_list = Db::name('lesson_order')->alias('order')
->field('order.*,user.firstname,user.lastname,user.email,user.whatsapp,lesson.name_en')
->join('user','order.user_id = user.id','LEFT')
->join('lesson','order.lesson_id = lesson.id','LEFT')
->where('order.slot_id',$lesson_order['slot_id'])->where('order.jointype',2)->where('order.order_status',0)->order('order.id asc')->select();
//有多少个空位
$pay_number = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('jointype',1)->where('order_status',10)->sum('usernumber');
$num_remain = $slot['num_max'] - $pay_number;
$coach_name = Db::name('coach')->where('id',$slot['coach_ids'])->value('nickname');
if(!empty($houbu_list)){
$up_usernumber = 0; //此次循环转正的人数
foreach($houbu_list as $key => $order){
if($order['usernumber'] + $up_usernumber <= $num_remain){
//此订单可以转正
//检查可用的套餐,找一个就可以
$map = [
'user_id' => $order['user_id'],
'endtime' => ['gt',time()],
'remain' => ['gt',$order['usernumber_hours']],
'order_status' => 1,
'use_status' => 1, //已激活的
'id' => $order['package_order_id'],
];
$package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $slot['lesson_id']])->lock(true)->find();
if(!$package_order){
continue;
}
//扣除一节。
$update = [
'remain' => bcsub($package_order['remain'],$order['usernumber_hours'],1),
'updatetime' => time(),
];
$rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
if($rs1 === false){
Db::rollback();
// $this->error('扣除套餐余额失败');
$this->error('取消失败');
}
//修改预约单数据。延迟到转正的时候处理
$update_order['order_amount'] = 0;
$update_order['order_status'] = 10;
$update_order['paytime'] = time();
$update_order['package_order_id'] = $package_order['id'];
$update_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $order['usernumber_hours']) .'/'. $package_order['sessions'];
$update_order['paytype'] = 1; //修改支付方式为配套
$update_order['jointype'] = 1; //修改加入方式为预约
$rs2 = Db::name('lesson_order')->where('id',$order['id'])->update($update_order);
if($rs2 === false){
Db::rollback();
// $this->error('扣除套餐余额失败');
$this->error('取消失败');
}
//加上本次转正的人数
$up_usernumber += $order['usernumber'];
//额外的通知
try {
$message =
'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!
We are excited to inform you that a spot has opened up for the following class. You are now booked into the class!
Class:'.$order['name_en'].'
Date: '.date('d F Y',$slot['starttime']).'
Time: '.date('H:i a',$slot['starttime']).'
We look forward to seeing you in the studio!❤
Best Regards,
Elin Dance Studio
';
//给这些用户发邮件
$obj = new Email();
$result = $obj
->to($order['email'])
->subject('A spot for your waitlisted class is ready and you are now added in!')
->message($message)
->send();
$obj = '';
//发whatsapp
$parameters = [
[
'type' => 'text',
'text' => $order['firstname'].' '.$order['lastname'],
],
[
'type' => 'text',
'text' => $order['name_en'],
],
/*[
'type' => 'text',
'text' => $coach_name,
],*/
[
'type' => 'text',
'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
],
];
$this->whatapp($order['whatsapp'],'lessonorder_alternate_to_booking','en_US',$parameters);
} catch (Exception $e) {
}
//额外的通知
}else{
continue;//跳出
}
}
}
return true;
}
//发送whatapp消息的方法
private function whatapp($receive_mobile,$template,$code,$parameters){
if(empty($receive_mobile)){return true;}
$token = config('site.whatsapp_token');
//发送者
$mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
//发送
$url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
$header = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
];
$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $receive_mobile,
'type' => 'template',
'template' => [
'name' => $template,
'language' => [
'code' => $code
],
'components' => [
[
'type' => 'body',
'parameters' => $parameters
]
],
],
];
$body = json_encode($body);
$rs = curl_post($url,$body,$header);
return true;
}
}