canyin-project/ybcy/controllers/channel/CashierController.php
2024-11-01 16:07:54 +08:00

530 lines
25 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Store;
use app\models\common\Config;
use app\models\common\Member;
use app\models\common\Good;
class CashierController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
public function init(){
parent::init();
$operatorId= Yii::$app->session->get('userInfo')['id'];
$userInfo=(new \yii\db\Query())
->select('userName,trueName,loginTime,storeId')
->from('{{%ybwm_users}}')
->where(['id'=>$operatorId])
->one();
$storeId=$userInfo['storeId'];
$this->shop_id=$storeId;
$request = Yii::$app->request;
if($request->isPost){
if(!$this->shop_id){
echo json_encode(['code'=>2,'msg'=>'无效的门店,请先添加商户']);die;
}
}
}
//获取收银台商品分类列表
public function actionGetGoodsCategory(){
file_put_contents();
$request = Yii::$app->request;
$result = axios_request();
$item = 9;
if ($request->isGet) {
$storeId=$result['storeId']?:$this->shop_id;
$re=Good::getGoodSData($this->wqData['uniacid'],$storeId,'',2);
//判断平台设置店内是否开启
$isInStore = 1;
$instore = Config::getSystemSet('sysTopImage', $this->wqData['uniacid'])['instore'];
if ($instore !== '1') {
//判断总设置店内是否开启
$instore = Config::getSystemSet('superPower', 0)['instore'];
if ($instore !== '1') {
$isInStore = 2;
}
}
//
// $re=array_values($re);
$count=count($re);
echo json_encode(['code' => 1, 'msg' => '成功', 'data' => $re, 'count' => intval($count), 'isInStore' => $isInStore]);
die;
}
}
//根据商品分类获取商品列表
public function actionGetGoodList(){
$result = axios_request();
$uniacid=$this->wqData['uniacid'];
$userId= $result['userId']?:0;
$storeId=$result['storeId']?:$this->shop_id;
$keyword=$result['keyword'];
$goodsType=2;
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
//获取门店商品设置
$storeConfig=Config::getSystemSet('currency', $uniacid);
if($result['id']){
$catecoryRes=(new \yii\db\Query()) ->from('{{%ybwm_core_category}}')
->where(['id'=>$result['id']])->one();
$page=Yii::$app->request->get('page')?Yii::$app->request->get('page'):1;
$num=($page-1)*10;
$limit=10;
//saleShow 销量显示 1开启2关闭 stockShow 库存显示 1开启2关闭 sellOutShow 售罄显示 1开启2关闭
if($storeRe['isMain']==1||$storeRe['goodsModel']==3) {
$goodQuery= (new \yii\db\Query())
->select('a.id,a.unit,a.name,icon,0 + CAST(price AS CHAR)price,salesNum,stock,minNum,maxNum,num,a.body,b.name labelName,b.color as labelColor,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where(['a.goodsType'=>$goodsType, 'a.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if($storeConfig['sellOutShow']==2){
$goodQuery->andWhere(['>','a.stock',0]);
}
if($keyword){
$goodQuery->andWhere(['or',['like','a.name',$keyword],['like','a.pinYin',$keyword]]);
}
if($catecoryRes['item']==12 || $catecoryRes['item']==13){
if($catecoryRes['customName']=='新品'){
$goodQuery->andWhere(['a.specialType' => 1]);
}
if($catecoryRes['customName']=='热卖'){
$goodQuery->andWhere(['a.specialType' => 2]);
}
if($catecoryRes['customName']=='推荐'){
$goodQuery->andWhere(['a.specialType' => 3]);
}
}else{
$goodQuery->andWhere(['a.typePid' => $result['id']]);
}
}else{
$goodQuery=(new \yii\db\Query())
->select('a.goodsId id,b.unit,b.name,icon,0 + CAST(a.price AS CHAR)price,b.salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,c.name labelName,c.color as labelColor,a.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->leftJoin('{{%ybwm_core_label}} c', 'b.goodLabel=c.id')
->where(['b.typePid' =>$result['id'],'b.goodsType'=>$goodsType, 'b.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if($storeConfig['sellOutShow']==2){
$goodQuery->andWhere(['>','a.stock',0]);
}
if($keyword){
$goodQuery->andWhere(['or',['like','b.name',$keyword],['like','b.pinYin',$keyword]]);
}
if($catecoryRes['item']==12 || $catecoryRes['item']==13){
if($catecoryRes['customName']=='新品'){
$goodQuery->andWhere(['a.specialType' => 1]);
}
if($catecoryRes['customName']=='热卖'){
$goodQuery->andWhere(['a.specialType' => 2]);
}
if($catecoryRes['customName']=='推荐'){
$goodQuery->andWhere(['a.specialType' => 3]);
}
}else{
$goodQuery->andWhere(['b.typePid' => $result['id']]);
}
}
// if($catecoryRes['item']==12 || $catecoryRes['item']==13){
// if($catecoryRes['customName']=='新品'){
// $goodQuery->andWhere(['a.specialType' => 1]);
// }
// if($catecoryRes['customName']=='热卖'){
// $goodQuery->andWhere(['a.specialType' => 2]);
// }
// if($catecoryRes['customName']=='推荐'){
// $goodQuery->andWhere(['a.specialType' => 3]);
// }
// }else{
// $goodQuery->andWhere(['b.typePid' => $result['id']]);
// }
$data=$goodQuery->offset($num)
//->limit($limit)->orderBy('a.sort asc,a.id desc')
->all();
}else{
if($storeRe['isMain']==1||$storeRe['goodsModel']==3) {
$goodQuery = (new \yii\db\Query())
->select('a.id,a.unit,a.name,icon,0 + CAST(price AS CHAR)price,salesNum,stock,minNum,maxNum,num,a.body,b.name labelName,b.color as labelColor,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where(['a.goodsType' => $goodsType, 'a.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if ($storeConfig['sellOutShow'] == 2) {
$goodQuery->andWhere(['>', 'a.stock', 0]);
}
if($keyword){
$goodQuery->andWhere(['or',['like','a.name',$keyword],['like','a.pinYin',$keyword]]);
}
$data = $goodQuery->all();
}else{
$goodQuery=(new \yii\db\Query())
->select('a.goodsId id,b.unit,b.name,icon,0 + CAST(a.price AS CHAR)price,b.salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,c.name labelName,c.color as labelColor,a.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->leftJoin('{{%ybwm_core_label}} c', 'b.goodLabel=c.id')
->where(['b.goodsType'=>$goodsType, 'b.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1]);
if($storeConfig['sellOutShow']==2){
$goodQuery->andWhere(['>','a.stock',0]);
}
if($keyword){
$goodQuery->andWhere(['or',['like','b.name',$keyword],['like','b.pinYin',$keyword]]);
}
$data=$goodQuery->all();
}
}
foreach ($data as $ko=>$vo){
//待处理 商品分类的时效判断
$data[$ko]['vipPrice']=Member::getDiscountByGood($userId,$vo['id'],$vo['price'])['money'];
if($vo['salesType']==2){
if($vo['startTime']>time()||$vo['endTime']<time()) {
unset($data[$ko]);
}
if(in_array(getWeek(),json_decode($vo['weekDay'],true))==false){
unset($data[$ko]);
}
if($vo['weekSalesType']==2){
$goodTimeQuery=(new \yii\db\Query())
->select('startTime,endTime')
->from('{{%ybwm_good_hours}}')
->where(['uniacid' => $uniacid, 'goodId' =>$vo['id']]);
if($storeRe['isMain']==1||$storeRe['goodsModel']==3){
$goodTimeQuery=$goodTimeQuery->andWhere(['storeId' => $storeId]);
}
$goodTimeArr= $goodTimeQuery->all();
$bool=0;
foreach ($goodTimeArr as $vs){
$strBool=getCurrentTimeSection($vs['startTime'],$vs['endTime']);
if($strBool){
$bool++;
}
}
if(!$bool){
unset($data[$ko]);
}
}
}
if($data[$ko]['id']){
//判断规格
$specsData=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')->where(['goodId'=>$vo['id']])->all();
if(!$specsData){
$data[$ko]['isSpecs']=2;
}
//判断属性
$attrData=(new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')->where(['goodId'=>$vo['id']])->all();
if(!$attrData){
$data[$ko]['isAttr']=2;
}
//判断加料
$materialData=(new \yii\db\Query())
->from('{{%ybwm_good_materia}}')->where(['goodId'=>$vo['id']])->all();
if(!$materialData){
$data[$ko]['isMaterial']=2;
}
}
}
$newData=array_values($data);
//$count=count($newData);
//return $this->result(1,'成功',$newData,$count);
return $this->result(1,'成功',$newData);
}
//获取会员信息
public function actionGetMember(){
$result=axios_request();
$now=time();
$phone=trim($result['phone']);
$id=$result['id'];
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
if($id){
$user = (new \yii\db\Query())
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$id])->one();
$userBindData=(new \yii\db\Query())
->from('{{%ybwm_member_bind}}')
->where('userId=:userId and storeId=:storeId', [':userId' =>$id,':storeId' => $storeId])->one();
$user['balance'] = isset($userBindData['balance']) ? $userBindData['balance'] : 0;
$user['integral'] = isset($userBindData['integral']) ? $userBindData['integral'] : 0;
$user['createdAt']=date('Y-m-d H:i:s',$user['createdAt']);
}else{
if($phone){
if(preg_match('/[a-zA-Z]/',$phone)){
$id=substr($phone,0,-6);
$phone='';
if(!$id){
return $this->result(1, '成功');
}
$user = (new \yii\db\Query())
->select('id,realName,userName,userTel,sex')
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$id])
->all();
return $this->result(1, '成功', $user);
}
if(strlen($phone)<4){
return $this->result(1, '成功');
}
if(strlen($phone)>4){
$user = (new \yii\db\Query())
->select('id,realName,userName,userTel,sex')
->from('{{%ybwm_member}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['like','userTel',$phone])
->all();
}else{
$subTel=substr($phone,-4);
$user = (new \yii\db\Query())
->select('id,realName,userName,userTel,sex')
->from('{{%ybwm_member}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['substring(userTel,-4)'=>$subTel])
->all();
}
return $this->result(1, '成功', $user);
}else{
return $this->result(1, '成功');
}
}
//var_dump($user);die;
// if(!$user){
// return $this->result(2, '该会员不存在');
// }
$coupon=(new \yii\db\Query())
->from('{{%ybwm_user_coupon}}')
->where('userId=:userId',[':userId'=>$id])
->andWhere('state=2 AND storeId=:storeId',[':storeId'=>$storeId])->count();
$user['couponNum']=$coupon;
$card=(new \yii\db\Query())
->select('name')
->from('{{%ybwm_user_card}}')
->where('level=:level AND uniacid=:uniacid',[':level'=>$user['level'],':uniacid'=>$user['uniacid']])->one();
$user['cardName']=$card['name'];
$money=(new \yii\db\Query())
->select('sum(money) as money,count(id) as count')
->from('{{%ybwm_bill}}')
->where(['uniacid'=>$uniacid,'storeId'=>$storeId,'userId'=>$result['id']])
->andWhere('refundMoney=0 or refundMoney is null')
->one();
$user['payMoney']=$money['money'];
$user['payCount']=$money['count'];
$reund=(new \yii\db\Query())
->select('sum(refundMoney) as money,count(id) as count')
->from('{{%ybwm_bill}}')
->where(['uniacid'=>$uniacid,'storeId'=>$storeId,'userId'=>$result['id']])
->andWhere('refundMoney>0')
->one();
$user['refundCount']=$reund['count'];
$user['refundMoney']=$reund['money'];
$user['birthday']=date('Y-m-d',$user['birthday']);
return $this->result(1, '成功', $user);
}
//添加商品到临时表
public function actionAddGoods(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$storeRe=(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
if($storeRe['isMain']==1){
$res= (new \yii\db\Query())
->select('a.id,a.name,icon,0 + CAST(price AS CHAR)price,salesNum,stock,minNum,maxNum,num,a.body,a.isSpecs,a.isAttr,a.isMaterial,a.salesType,a.startTime,a.endTime,a.weekDay,a.weekSalesType')
->leftJoin('{{%ybwm_core_label}} b', 'a.goodLabel=b.id')
->from('{{%ybwm_core_goods}} a')
->where(['a.id'=>$result['id'],'a.deleteAt'=>0, 'a.uniacid'=>$uniacid, 'a.storeId' => $storeId, 'a.display' => 1])
->one();
}else{
$res=(new \yii\db\Query())
->select('a.goodsId id,b.name,icon,0 + CAST(a.price AS CHAR)price,b.salesNum,a.stock,b.minNum,b.maxNum,b.num,b.body,b.isSpecs,b.isAttr,b.isMaterial,b.salesType,b.startTime,b.endTime,b.weekDay,b.weekSalesType')
->from('{{%ybwm_store_goods}} a')
->leftJoin('{{%ybwm_core_goods}} b', 'a.goodsId=b.id')
->where(['a.goodsId' =>$result['id'], 'b.deleteAt' => 0, 'a.uniacid' => $uniacid, 'a.storeId' => $storeId, 'a.display' => 1])
->one();
}
if($res['isSpecs']==1){
$specsRes=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where('uniacid=:uniacid AND storeId=:storeId and goodId=:goodId',[':uniacid'=>$uniacid,':storeId'=>$storeId,':goodId'=>$result['id']])
->one();
$stock=$specsRes['SalesStock'];
}else{
$stock=$res['stock'];
}
$row=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where('uniacid=:uniacid AND id=:id and goodsId=:goodsId and state=1',[':uniacid'=>$uniacid,':id'=>$storeId,':goodsId'=>$result['id']])
->one();
if($row){
if($row['num']+1>$stock){
return $this->result(2,'超过库存限制');
}
if($row['num']+1>$res['maxNum']){
return $this->result(2,'超过购买数量限制');
}
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']+1], ['id'=>$row['id']])->execute();
}else{
$data=array(
'goodsId'=>$result['id'],
'name'=>$res['name'],
'money'=>$res['name'],
'num'=>$res['name'],
'icon'=>$res['icon'],
'storeId'=>$storeId,
'createdAt'=>time(),
'uniacid'=>$uniacid,
);
if($res['isSpecs']==1){
$specsData=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where('uniacid=:uniacid AND storeId=:storeId and goodId=:goodId',[':uniacid'=>$uniacid,':storeId'=>$storeId,':goodId'=>$result['id']])
->all();
$data['specs']=json_encode($specsData);
}
if($res['isMaterial']==1){
$materialData=(new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where('uniacid=:uniacid AND storeId=:storeId and goodId=:goodId',[':uniacid'=>$uniacid,':storeId'=>$storeId,':goodId'=>$result['id']])
->all();
$data['material']=json_encode($materialData);
}
if($res['isAttr']==1){
$attrData=(new \yii\db\Query())
->from('{{%ybwm_good_attribute}}')
->where('uniacid=:uniacid AND storeId=:storeId and goodId=:goodId',[':uniacid'=>$uniacid,':storeId'=>$storeId,':goodId'=>$result['id']])
->all();
$data['attr']=json_encode($attrData);
}
$data['num']=$res['minNum']?:1;
Yii::$app->db->createCommand()->insert('{{%ybwm_cashier_goods}}', $data)->execute();
}
return $this->result(1, '成功');
}
//收银员交班
public function actionGiveOut(){
$operatorId= Yii::$app->session->get('cashierInfo')['id']?:67;
YII::$app->db->createCommand()->update('{{%ybwm_users}}', ['giveTime'=>time()], ['id'=>$operatorId])->execute();
return $this->result(1, '成功');
}
//获取收银员交班信息
public function actionGetCashierInfo(){
$operatorId= Yii::$app->session->get('userInfo')['id'];
$userInfo=(new \yii\db\Query())
->select('userName,trueName,loginTime,storeId')
->from('{{%ybwm_users}}')
->where(['id'=>$operatorId])
->one();
$storeId=$userInfo['storeId'];
$start_time=strtotime(date("Y-m-d",time()));
$end_time=$start_time+60*60*24;
//收款金额(元)
$query=(new \yii\db\Query())->from('{{%ybwm_instore_order}}')
->where(['>=','payAt',$start_time])->andWhere(['storeId'=>$storeId])
->andWhere(['<=','payAt',$end_time])
->andWhere(['>=','state',2]);
$instoreMoney=(clone $query)->sum('money');
$instoreMoney= sprintf("%.2f",$instoreMoney);
$instoreCount=(clone $query)->count();
//微信收款金额(元)
$wxMoney=(clone $query)->andWhere(['payMode'=>1])->sum('money');
$wxMoney= sprintf("%.2f",$wxMoney);
$wxCount=(clone $query)->andWhere(['payMode'=>1])->count();
//支付宝收款金额(元)
$alipayMoney=(clone $query)->andWhere(['payMode'=>2])->sum('money');
$alipayMoney= sprintf("%.2f",$alipayMoney);
$alipayCount=(clone $query)->andWhere(['payMode'=>2])->count();
//标记支付收款金额(元)
$otherMoney=(clone $query)->andWhere(['in','payMode',[7,8,9]])->sum('money');
$otherMoney= sprintf("%.2f",$otherMoney);
$otherCount=(clone $query)->andWhere(['in','payMode',[7,8,9]])->count();
//现金总金额(元)
$discountMoney=(clone $query)->andWhere(['payMode'=>6])->sum('money+discount');
$discountMoney= sprintf("%.2f",$discountMoney);
$discountCount=(clone $query)->andWhere(['payMode'=>6])->count();
//现金实收金额(元)
$money=(clone $query)->andWhere(['payMode'=>6])->sum('money');
$money= sprintf("%.2f",$money);
$count=(clone $query)->andWhere(['payMode'=>6])->count();
$newData=array(
'userName'=>$userInfo['userName'],
'trueName'=>$userInfo['trueName'],
'loginTime'=>date('Y-m:d H:i:s',$userInfo['loginTime']),
'instoreMoney'=>$instoreMoney,
'instoreCount'=>$instoreCount,
'wxMoney'=>$wxMoney,
'wxCount'=>$wxCount,
'alipayMoney'=>$alipayMoney,
'alipayCount'=>$alipayCount,
'otherMoney'=>$otherMoney,
'otherCount'=>$otherCount,
'discountMoney'=>$discountMoney,
'discountCount'=>$discountCount,
'money'=>$money,
'count'=>$count,
);
return $this->result(1, '成功',$newData);
}
public function actionCashierInfo(){
$result=axios_request();
$uniacid=$this->wqData['uniacid']?:$result['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$storeRe=(new \yii\db\Query())
->select('name')
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,':id'=>$storeId])
->one();
$sysTopImage=Config::getSystemSet('sysTopImage',$this->wqData['uniacid']);
$superPower=Config::getSystemSet('superPower',0);
$cashierName=$sysTopImage['cashierName']?:$superPower['cashierName'];
$cashierIcon=$sysTopImage['cashierIcon']?:$superPower['cashierIcon'];
$cashierBg=$sysTopImage['cashierBg']?:$superPower['cashierBg'];
$cashierSet['bgIcon']=$cashierBg?:null;
$cashierSet['icon']=$cashierIcon?:null;
$cashierSet['cashName']=$cashierName?:'收银台';
$cashierSet['name']=$storeRe['name'];
$cashierSetConfig=Config::getSystemSet('cashierSet',$this->wqData['uniacid']);
$cashierSet['pay']= $cashierSetConfig['pay']?:1;
$cashierSet['cash']=$cashierSetConfig['cash']?:1;
$cashierSet['balance']=$cashierSetConfig['balance']?:1;
$cashierSet['otherSet']=$cashierSetConfig['otherSet']?:1;
$cashierSet['cashier']=$cashierSetConfig['cashier']?:1;
$cashierSet['fastFood']=$cashierSetConfig['fastFood']?:1;
$cashierSet['vipTips']=$cashierSetConfig['vipTips']?:1;
$cashierSet['discountStatus']=$cashierSetConfig['discountStatus']?:1;
if($cashierSetConfig['discountStatus']==1&&$cashierSetConfig['discountAuto']==1){
$cashierSet['discountStatus']=2;
}
return $this->result(1, '成功',$cashierSet);
}
//效验付费插件开通
public function actionCheckPlug(){
Config::checkPlug($this->wqData['uniacid'],'cashier');
}
}