ctms/pms-admin/controllers/ContractController.php
2025-04-10 23:19:13 +08:00

713 lines
25 KiB
PHP
Executable File

<?php
/**
* @Author: fm453
* @Date: 2018-11-22 03:40:46
# @Last modified by: fm453
# @Last modified time: 2021-09-15T15:22:35+08:00
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use common\models\Fans;
use backend\models\FmContract as Contract;
use backend\models\FmRoomOwner as RoomOver;
use backend\models\FmUserExt as UserExt;
class ContractController extends \yii\web\Controller
{
//替代常规的_construct 析构函数;其他方法调用前执行
public function init()
{
parent::init();
$session = Yii::$app->session;
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
$session->set('pid',(int)$_GET['pid']);
}
$pid = $session->get('pid');
if(!$pid){
$url = Url::toRoute('/index/index');
return $this->redirect($url);
}
}
//主界面
public function actionIndex()
{
global $_HI,$_FM;
return $this->render('../layouts/dev',[]);
}
//客户列表
public function actionList()
{
return $this->render('../layouts/dev',[]);
global $_HI,$_FM;
$pid = Yii::$app->session->get('pid');
$model = new AcCustomer();
$where = $where2 = [];
$where2[]='and';
$where['pid'] = $pid;
$post = Yii::$app->request->post();
$search = isset($post['search']) ? $post['search'] : [];
$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
$session = Yii::$app->session;
if($page==0){
$session->set('Guest:List:'.$pid,$search);
}else{
$search = $search ? $search : $session->get('Guest:List:'.$pid);
}
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
if(!empty($search['title'])){
$where2[] = ['LIKE','name',$search['title']];
}
$selectMid = false;
$mids = [];
$search['nickname'] = isset($search['nickname']) ? trim(htmlspecialchars_decode($search['nickname'])) : '';
if(!empty($search['nickname'])){
$_mids = [];
$_exts = LvUserExt::find()->where(['pid'=>$pid,'deleted'=>0,'key'=>'nickname'])->andwhere(['OR',['LIKE','value',$search['nickname']]])->indexBy('id')->groupBy('mid')->all();
foreach($_exts as $mid=>$ext){
$_mids[$ext['mid']] = $ext['mid'];
}
$selectMid = true;
$mids[] = $_mids;
}
$search['phone'] = isset($search['phone']) ? trim(htmlspecialchars_decode($search['phone'])) : '';
if(!empty($search['phone'])){
$where2[] = ['LIKE','mobile',$search['phone']];
}
$search['hotel'] = isset($search['hotel']) ? (int)$search['hotel'] : 0;
if(!empty($search['hotel'])){
$where2[] = ['=','hotel_id',$search['hotel']];
$hotel = AcHotel::find()->where(['id'=>$search['hotel']])->one();
$search['hotel_title'] = $hotel['title'];
}
$search['employee'] = isset($search['employee']) ? (int)$search['employee'] : 0;
if(!empty($search['employee'])){
$where2[] = ['=','employee_id',$search['employee']];
$employee = AcEmployee::find()->where(['id'=>$search['employee']])->one();
$search['employee_title'] = $employee['name'];
}
$search['channel'] = isset($search['channel']) ? (int)$search['channel'] : 0;
$channels= Yii::$app->params['saleChannels'];
if(!empty($search['channel'])){
$where2[] = ['=','channel',$search['channel']];
}
//入住时间筛选
$search['checkin_m'] = isset($search['checkin_m']) ? $search['checkin_m'] : '';
$search['checkin_after'] = isset($search['checkin_after']) ? $search['checkin_after'] : '';
$search['checkin_before'] = isset($search['checkin_before']) ? $search['checkin_before'] : '';
//按月份筛选
if(!empty($search['checkin_m'])){
$start = strtotime($search['checkin_m']);
$s = date("Y-m-d H:i:s", $start);
$ldm = strtotime("last day of ".$s);
$m = date("Y-m-d",$ldm)." 23:59:59";
$end = strtotime($m);
$where2[] = ['between','last_checkin',$start,$end];
}
//按指定时段筛选
elseif(!empty($search['checkin_after']) && !empty($search['checkin_before'])){
$start = strtotime($search['checkin_after']);
$end = strtotime($search['checkin_before']);
$where2[] = ['between','last_checkin',$start,$end];
}
//按指定时间临界点筛选(以不早于为优先筛选条件)
elseif(!empty($search['checkin_after'])){
$start = strtotime($search['checkin_after']);
$where2[] = ['>=','last_checkin',$start];
}elseif(!empty($search['checkin_before'])){
$end = strtotime($search['checkin_before']);
$where2[] = ['<=','last_checkin',$end];
}
//生日筛选
$mids2 = [];
$search['birthday_m'] = isset($search['birthday_m']) ? $search['birthday_m'] : '';
$search['birthday_after'] = isset($search['birthday_after']) ? $search['birthday_after'] : '';
$search['birthday_before'] = isset($search['birthday_before']) ? $search['birthday_before'] : '';
//按月份筛选
if(!empty($search['birthday_m'])){
$start = strtotime($search['birthday_m']);
$s = date("Y-m-d H:i:s", $start);
$ldm = strtotime("last day of ".$s);
$m = date("Y-m-d",$ldm)." 23:59:59";
$end = strtotime($m);
$_mids = [];
$_exts = LvUserExt::find()->where(['pid'=>$pid,'deleted'=>0,'key'=>'birthday'])->andwhere(['OR',['between','value',$start,$end]])->indexBy('id')->groupBy('mid')->all();
foreach($_exts as $mid=>$ext){
$_mids[$ext['mid']] = $ext['mid'];
}
$selectMid = true;
$mids[] = $_mids;
}
//按指定时段筛选
elseif(!empty($search['birthday_after']) && !empty($search['birthday_before'])){
$start = strtotime($search['birthday_after']);
$end = strtotime($search['birthday_before']);
$_mids = [];
$_exts = LvUserExt::find()->where(['pid'=>$pid,'deleted'=>0,'key'=>'birthday'])->andwhere(['OR',['between','value',$start,$end]])->indexBy('id')->groupBy('mid')->all();
foreach($_exts as $mid=>$ext){
$_mids[$ext['mid']] = $ext['mid'];
}
$selectMid = true;
$mids[] = $_mids;
}
//按指定时间临界点筛选(以不早于为优先筛选条件)
elseif(!empty($search['birthday_after'])){
$start = strtotime($search['birthday_after']);
$_mids = [];
$_exts = LvUserExt::find()->where(['pid'=>$pid,'deleted'=>0,'key'=>'birthday'])->andwhere(['OR',['>=','value',$start]])->indexBy('id')->groupBy('mid')->all();
foreach($_exts as $mid=>$ext){
$_mids[$ext['mid']] = $ext['mid'];
}
$selectMid = true;
$mids[] = $_mids;
}elseif(!empty($search['birthday_before'])){
$end = strtotime($search['birthday_before']);
$_mids = [];
$_exts = LvUserExt::find()->where(['pid'=>$pid,'deleted'=>0,'key'=>'birthday'])->andwhere(['OR',['<=','value',$end]])->indexBy('id')->groupBy('mid')->all();
foreach($_exts as $mid=>$ext){
$_mids[$ext['mid']] = $ext['mid'];
}
$selectMid = true;
$mids[] = $_mids;
}
if($selectMid){
$mids2 = $mids[0];
foreach($mids as $_mids){
if($_mids){
foreach($_mids as $mid){
if(!in_array($mid,$mids2)){
unset($_mids[$mid]);
};
$mids2 = $_mids;
}
}
}
$where2[] = ['IN','mid',$mids2];
}
$where['deleted'] = 0;
$data = $model->find()->where($where)->andwhere(['AND',$where2]);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$res = $data->orderBy('id DESC')->offset($pages->offset)->limit($pages->limit)->indexBy('mid')->all();
$status = ['0'=>'隐藏','1'=>'显示'];
$gender = ['0'=>'未设置','1'=>'男','2'=>'女'];
$model = new AcHotel();
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$_hotels = $model->find()->where($where)->all();
$hotels = [];
foreach($_hotels as $s){
$hotels[$s->id] = $s->toArray();
}
$eids = $mids = [];
foreach($res as $r){
$r = $r->toArray();
$eids[$r['employee_id']] = $r['employee_id'];
$mids[$r['mid']] = $r['mid'];
}
$model = new AcEmployee();
$employees = [];
$_employees = $model->find()->where(['in','id',$eids])->indexBy('id')->all();
foreach($_employees as $s){
$employees[$s->id] = $s->toArray();
}
$model = new LvUserExt();
$exts = [];
$_exts = $model->find()->where(['in','mid',$mids])->andwhere(['pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
foreach($_exts as $ext){
$ext = $ext->toArray();
$exts[$ext['mid']][$ext['key']] = $ext['value'];
}
$guests = [];
foreach($res as $s){
$s = $s->toArray();
foreach($exts[$s['mid']] as $col=>$val){
$s[$col] = $val;
}
$guests[] = $s;
}
$model = new User();
$users = [];
$_users = $model->find()->where(['IN','id',$mids])->all();
foreach($_users as $s){
$s = $s->toArray();
$users[$s['id']] = $s;
}
return $this->render('list',[
'guests' => $guests,
'pager' => $pages,
'employees'=>$employees,
'hotels'=>$hotels,
'exts'=>$exts,
'users'=>$users,
'status'=>$status,
'gender'=>$gender,
'search'=>$search,
'total'=>$data->count(),
]);
}
//客户列表选择
public function actionSelect()
{
return $this->render('../layouts/dev',[]);
global $_HI,$_FM;
$pid = Yii::$app->session->get('pid');
$model = new AcCustomer();
$where = $where2 = [];
$where2[]='and';
$where['pid'] = $pid;
$post = Yii::$app->request->post();
$search = [];
$search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : '';
if(!empty($search['title'])){
$where2[] = ['LIKE','name',$search['title']];
}
$where['deleted'] = 0;
$data = $model->find()->where($where);
if($search['title']){
$data = $data->andwhere($where2);
}
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$res = $data->offset($pages->offset)->limit($pages->limit)->indexBy('mid')->all();
$status = ['0'=>'隐藏','1'=>'显示'];
$gender = ['0'=>'未设置','1'=>'男','2'=>'女'];
$model = new AcHotel();
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$_hotels = $model->find()->where($where)->all();
$hotels = [];
foreach($_hotels as $s){
$hotels[$s->id] = $s->toArray();
}
$eids = $mids = [];
foreach($res as $r){
$eids[$r['employee_id']] = $r['employee_id'];
$mids[$r['mid']] = $r['mid'];
}
$model = new AcEmployee();
$employees = [];
$_employees = $model->find()->where(['in','id',$eids])->indexBy('id')->all();
foreach($_employees as $s){
$employees[$s->id] = $s->toArray();
}
$model = new LvUserExt();
$exts = [];
$_exts = $model->find()->where(['in','mid',$mids])->andwhere(['pid'=>$pid,'deleted'=>0])->indexBy('id')->all();
foreach($_exts as $ext){
$ext = $ext->toArray();
$exts[$ext['mid']][$ext['key']] = $ext['value'];
}
$guests = [];
foreach($res as $s){
$s = $s->toArray();
foreach($exts[$s['mid']] as $col=>$val){
$s[$col] = $val;
}
$guests[$s['id']] = $s;
}
$model = new User();
$users = [];
$_users = $model->find()->where(['in','id',$mids])->all();
foreach($_users as $s){
$s = $s->toArray();
$users[$s['id']] = $s;
}
$callback = !empty(Yii::$app->session->get('callback')) ? Yii::$app->session->get('callback') : 'carowner';
return $this->render('select',[
'guests' => $guests,
'pager' => $pages,
'employees'=>$employees,
'hotels'=>$hotels,
'exts'=>$exts,
'users'=>$users,
'status'=>$status,
'gender'=>$gender,
'search'=>$search,
'callback'=>$callback
]);
}
//新增车主
public function actionNew()
{
global $_HI,$_FM;
return $this->render('modify',[]);
}
//更新车主
public function actionEdit()
{
global $_HI,$_FM;
$model = new AcCustomer();
$id = Yii::$app->request->get('id');
$pid = Yii::$app->session->get('pid');
$res = $model->find()->where(['id' => $id])->one();
if($res){
$res = $res->toArray();
$res['last_checkin'] = $res['last_checkin'] ? date('Y-m-d',$res['last_checkin']) : '';
$res['last_contact'] = $res['last_contact'] ? date('Y-m-d',$res['last_contact']) : '';
$res['realname'] = $res['name'];
}else{
$res = [];
}
//查询对应的系统用户
$userModel = new User();
$user = User::find()->where(['id'=>$res['mid']])->one();
if($user){
$res['mobile'] = $user->mobile;
}
$hotel = AcHotel::find()->where(['id' => $res['hotel_id']])->one();
if($hotel){
$res['hotel_title'] = $hotel->title;
}
$employee = AcEmployee::find()->where(['id' => $res['employee_id']])->one();
if($employee){
$res['employee_title'] = $employee->name;
}
$exts = LvUserExt::find()->where(['mid' => $res['mid'],'pid'=>$pid,'deleted'=>0])->all();
if($exts){
foreach($exts as $ext){
$res[$ext['key']] = $ext['value'];
}
}
return $this->render('modify',[
'detail'=>$res,
'id'=>$id,
]);
}
//保存车主资料
public function actionSave()
{
$post = Yii::$app->request->post();
$id = (int)$post['id'];
$pid = Yii::$app->session->get('pid');
if(!$post['mobile']){
\Yii::$app->getSession()->setFlash('warning', '请输入主手机号码!');
$url = Url::toRoute(['guest/edit','id'=>$id]);
return $this->redirect(['new','id'=>$id]);
}
$mobile = $post['mobile'];
//查询对应的系统用户
$userModel = new User();
$user = User::find()->where(['mobile'=>$mobile])->one();
if(!$user){
$pwd = isset($post['password']) ? trim($post['password']) : Yii::$app->params['defaultUserPassword'];
$userModel->mobile = $mobile;
$userModel->username = $mobile;
$userModel->email = $mobile.'@hiluker.com';
$userModel->setPassword($pwd);
$userModel->generateAuthKey();
$userModel->avatar = Yii::$app->params['defaultUserAvatar'];
$userModel->created_at = time();
$userModel->status = 1;
$res = $userModel->save();
// $res = $userModel->getErrors();
$user_id = $userModel->attributes['id']; //获取插入后id
}else{
$user_id = $user->id;
$pwd = isset($post['password']) ? trim($post['password']) : '';
if($pwd){
$user->setPassword($pwd);
$user->generateAuthKey();
$user->updated_at = time();
$res = $user->save();
}
}
//格式化数据
$data = [];
//目标键=》POST键
$cols = ['name'=>'realname','mobile'=>'mobile','idcard'=>'idcard','passport'=>'passport','country'=>'country','province'=>'province','channel'=>'channel','thumb'=>'thumb'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['gender'=>'gender','total_fee'=>'total_fee','money'=>'money','jifen'=>'jifen','score'=>'score','hotel_id'=>'hotel','employee_id'=>'employee','status_code'=>'status_code','deleted'=>'deleted'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
$cols = ['last_checkin'=>'last_checkin','last_contact'=>'last_contact'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? strtotime($post[$key]) : 0;
}
$cols = ['remark'=>'editor'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? htmlspecialchars($post[$key]) : '';
}
$birthday = isset($post['ext']['birthday']) ? strtotime($post['ext']['birthday']) : 0;
if($birthday){
$data['birth_y'] = date('Y',$birthday);
$data['birth_m'] = date('m',$birthday);
$data['birth_d'] = date('d',$birthday);
}
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
$data['pid'] = $pid;
$data['mid'] = $user_id;
//格式化车主资料
$AcCustomer = new AcCustomer();
foreach($data as $key=>$val){
$AcCustomer->$key = $val;
}
//查询该客户是否在库
$customer = AcCustomer::find()->where(['pid'=>$data['pid'],'mid'=>$data['mid']])->one();
if($customer){
$id = $customer->id;
}
//客户资料入库
if($id){
$AcCustomer->id = $id;
unset($data['create_at']);
$res = $AcCustomer->updateAll($data,['id'=>$id]);
}else{
$res = $AcCustomer->save();
$id = $AcCustomer->attributes['id']; //获取插入后id
}
//保存扩展资料
$LvUserExt = new LvUserExt();
//批量软删
$LvUserExt->updateAll(['deleted'=>1],['deleted'=>0,'pid'=>$pid,'mid'=>$user_id]);
$datas = [];
$data = [];
$exts = $post['ext'];
foreach($exts as $col=>$val){
$data['pid'] = $pid;
$data['mid'] = $user_id;
$data['key'] = $col;
$data['mobile'] = $mobile;
$data['value'] = trim($val);
$datas[] = $data;
}
//批量插入
$model = clone $LvUserExt;
foreach($datas as $attributes)
{
$model->isNewRecord = true;
$model->setAttributes($attributes,false);
$model->save() && $model->id=0;
}
if($res){
\Yii::$app->getSession()->setFlash('success', '用户资料保存成功!');
return $this->redirect(['edit','id'=>$id]);
}else{
\Yii::$app->getSession()->setFlash('warning', '用户资料保存失败!');
return $this->goBack();
}
}
//删除用户
public function actionDelete($id)
{
global $_HI,$_FM;
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$data = $where = [];
$where['pid'] = Yii::$app->session->get('pid');
$where['id'] = $id;
$data['update_at'] = time();
$data['deleted'] = 1;
$model = new AcCustomer();
foreach($data as $key=>$val){
$model->$key = $val;
}
$model->id = $id;
$res = $model->updateAll($data,$where);
$return = [];
$return['url'] = Url::toRoute('guest/list');
// $return['url'] = Url::previous();
$return['timeout'] = 3; //3秒后自动跳转
$return['status'] = 0;
if($res){
$return['msg'] = '删除成功';
$return['errorcode'] = 200;
$return['data'] = $id;
}else{
$return['msg'] = '删除失败';
$return['errorcode'] = 0;
}
exit(json_encode($return));
}
//更新车主
public function actionMsg()
{
global $_HI,$_FM;
$model = new AcCustomer();
$id = Yii::$app->request->get('id');
$pid = Yii::$app->session->get('pid');
$res = $model->find()->where(['id' => $id])->one();
if($res){
$res = $res->toArray();
$res['last_checkin'] = $res['last_checkin'] ? date('Y-m-d',$res['last_checkin']) : '';
$res['last_contact'] = $res['last_contact'] ? date('Y-m-d',$res['last_contact']) : '';
$res['realname'] = $res['name'];
}else{
$res = [];
}
//查询对应的系统用户
$userModel = new User();
$user = User::find()->where(['id'=>$res['mid']])->one();
if($user){
$res['mobile'] = $user->mobile;
}
return $this->render('../layouts/dev',[]);
$hotel = AcHotel::find()->where(['id' => $res['hotel_id']])->one();
if($hotel){
$res['hotel_title'] = $hotel->title;
}
$employee = AcEmployee::find()->where(['id' => $res['employee_id']])->one();
if($employee){
$res['employee_title'] = $employee->name;
}
$exts = LvUserExt::find()->where(['mid' => $res['mid'],'pid'=>$pid,'deleted'=>0])->all();
if($exts){
foreach($exts as $ext){
$res[$ext['key']] = $ext['value'];
}
}
return $this->render('modify',[
'detail'=>$res,
'id'=>$id,
]);
}
public function actionAjax()
{
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$data = $where = [];
$where['pid'] = Yii::$app->session->get('pid');
$where['id'] = $id;
$data['update_at'] = time();
$model = new AcCustomer();
$res = $model->find()->where(['id' => $id])->one();
$return = [];
$return['timeout'] = 1; //3秒后自动跳转
$return['status_code'] = 0;
$return['ajax'] = 1;
if($res){
$res = $res->toArray();
}else{
$return['msg'] = '客户数据不存在';
$return['errorcode'] = 404;
exit(json_encode($return));
}
switch($get['do'])
{
case 'hide':
$data['status_code'] = 0;
$model->id = $id;
$res = $model->updateAll($data,$where);
if($res){
$return['msg'] = '客户隐藏标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css'=>'info'];
}else{
$return['msg'] = '客户隐藏标记失败';
$return['errorcode'] = 0;
}
break;
case 'show':
$data['status_code'] = 1;
$model->id = $id;
$res = $model->updateAll($data,$where);
if($res){
$return['msg'] = '客户显示标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css'=>'default'];
}else{
$return['msg'] = '客户显示标记失败';
$return['errorcode'] = 0;
}
break;
case 'contact':
$data['last_contact'] = time();
$model->id = $id;
$res = $model->updateAll($data,$where);
if($res){
$return['msg'] = '标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css'=>'default'];
}else{
$return['msg'] = '标记失败';
$return['errorcode'] = 0;
}
break;
default:
$return['msg'] = '无效的操作';
$return['errorcode'] = 200;
$return['data'] = ['css'=>'info'];
break;
}
exit(json_encode($return));
}
}