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

582 lines
26 KiB
PHP

<?php
namespace app\controllers\channel;
use app\models\common\Printing;
use Illuminate\Support\Facades\DB;
use Yii;
use Yii\web\Session;
use app\models\common\Store;
use app\models\common\Config;
use app\models\common\Member;
class CashierCeshiController extends CommonController{
private $shop_id;
public function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
$request = Yii::$app->request;
if($request->isPost){
if(!$this->shop_id){
echo json_encode(['code'=>2,'msg'=>'无效的门店,请先添加商户']);die;
}
}
}
/* 1.初步选商品shop_car
2.落单order_goods addType 1.加菜2普通 addNum 加菜次数
* 3.收银台收银条件 员工ID桌号ID cashierType 1
*
* */
//商品加入购物车 shop_car
public function actionAddGoods(){
$request = Yii::$app->request;
if ($request->isPost) {
$result = axios_request();
$userId = $result['userId'];
$storeId = $result['storeId'] ?: $this->shop_id;
$uniacid = $this->wqData['uniacid'];
if (!$storeId) {
echo json_encode(['code' => 2, 'msg' => '无效的门店']);
die;
}
if (!$result['type']) {
echo json_encode(['code' => 2, 'msg' => '无效的请求']);
die;
}
$session = new Session;
$session->open();
$tableId = $result['tableId'] ?: 0;
$operatorId = Yii::$app->session->get('userInfo')['id'];
if (!$operatorId) {
return $this->result(2, '请用收银员账号登录');
}
$carData = (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId' => $tableId, 'state' => 1, 'storeId' => $storeId, 'uniacid' => $uniacid, 'operatorId' => $operatorId])
->all();
if (!$carData) {
echo json_encode(['code' => 2, 'msg' => '无效的请求']);
die;
}
$transaction = Yii::$app->db->beginTransaction();
try{
$outTradeNo = date("YmdHis") . randomAESKey(6);
$data = array(
'outTradeNo' => $outTradeNo,
'state' => 1,
'storeId' => $storeId,
'createdAt' => time(),
'uniacid' => $uniacid,
'userId' => $userId,
'orderMode' => 2,
'isOut' => 1,
'origin' => 5,
'operatorId' => $operatorId
);
Yii::$app->db->createCommand()->insert('{{%ybwm_instore_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
$money = 0;
$originMoney = 0;
$goodsNum = 0;
foreach ($carData as $v) {
$originMoney += $v['money'];
$money += $v['money'] + $v['materialMoney'];
$goodsNum += $v['num'];
$goodsData = array(
'orderId' => $orderId,
'goodsId' => $v['goodsId'],
'name' => $v['name'],
'money' => $v['money'],
'num' => $v['num'],
'data' => $v['groupName'],
'icon' => $v['icon'],
'storeId' => $v['storeId'],
'uniacid' => $v['uniacid'],
'createdAt' => time(),
'groupId' => $v['groupId'],
'material' => $v['material'],
'attribute' => $v['attribute'],
'item' => 4,
);
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['orderId'=>$orderId],['id'=>$v['id']])->execute();
YII::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $goodsData)->execute();
}
$orderData = array(
'money' => $money,
'originMoney' => $originMoney,
'goodsNum' => $goodsNum,
);
YII::$app->db->createCommand()->update('{{%ybwm_instore_order}}', $orderData, ['id' => $orderId])->execute();
$transaction->commit();//提交事务会真正的执行数据库操作
} catch (Exception $e) {
$transaction->rollback();
echo "insert data error:", $e->getMessage();die;
}
echo json_encode(['code' => 1, 'msg' => '成功', 'orderId' => $orderId]);die;
}
}
//获取购物车内商品列表
public function actionGetCarList(){
$request = Yii::$app->request;
$result=axios_request();
$operatorId= Yii::$app->session->get('userInfo')['id'];
$storeId=$result['storeId']?:$this->shop_id;
$tableId=$result['tableId']?:0;
if($request->isGet){
$uniacid=$this->wqData['uniacid'];
$uids=[2,1,3];
$tableInfo=(new \yii\db\Query())
->from('{{%ybwm_table}}')
->where(['id'=>$tableId])
->one();
if($tableId&&$tableInfo['state']==3){
$order =(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('tableId=:tableId AND state=1',[':tableId'=>$tableId])->one();
$data=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where(['orderId'=>$order['id']])->andWhere(['in','item',[2,5]])
->orderBy(["FIELD(addType, ".join(',',$uids).")" => true])
->all();
$money=number_format(array_sum(array_column($data,'sum')),2);
$money=bcsub($money,$order['discount'],2);
$discount=$order['discount'];
$outTradeNo=$order['outTradeNo'];
}else{
$data=(new \yii\db\Query())
->select('outTradeNo,id,name,unit,icon,createdAt,changeAt,isNew,money,(money*num)sum,attribute,goodsId,groupId,groupName,material,materialIds,materialMoney,materialName,num,materialMoney,(money*num)totalMoney,(originalMoney*num)originalMoney')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'operatorId'=>$operatorId,'uniacid'=>$uniacid,'storeId'=>$storeId,'state'=>1])
->all();
$discount=0;
$money=number_format(array_sum(array_column($data,'sum')),2);
$outTradeNo=array_column($data,'outTradeNo')[0];
$orderData=(new \yii\db\Query())
->select('outTradeNo')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'operatorId'=>$operatorId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>3])
->groupBy('outTradeNo')
->orderBy('id desc')
->all();
$orderCount=count($orderData);
}
$count=array_sum(array_column($data,'num'));
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$data,'money'=>$money,'count'=>$count,'discount'=>$discount,'outTradeNo'=>$outTradeNo,'orderCount'=>$orderCount]);die;
}
}
//$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}',['id'=>$row['id']])->execute();
//清空购物车
public function actionDelCar(){
$request = Yii::$app->request;
if($request->isGet){
return $this->result(2, '请求异常');
}
$result=axios_request();
$tableId=$result['tableId']?:0;
$operatorId= Yii::$app->session->get('userInfo')['id'];
$storeId=$result['storeId']?:$this->shop_id;
$res = Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['tableId'=>$tableId,'operatorId' => $operatorId, 'storeId' => $storeId,'status'=>1])->execute();
if($res){
return $this->result(1, '成功');
}else{
return $this->result(2, '失败');
}
}
//获取多规格商品详情
public function actionGetGoodOper(){
$request = Yii::$app->request;
if($request->isGet){
$result=axios_request();
$specsData=(new \yii\db\Query())
->select('id,specsName,fillType,SalesPrice,SalesStock,crossedPrice,goodCode,costPrice,boxMoney')
->from('{{%ybwm_good_specs}}')
->where(['goodId'=>$result['id']])
->where('goodId=:goodId AND SalesStock>0',[':goodId'=>$result['id']])
->all();
for($i=0;$i<count($specsData);$i++){
$specsData[$i]['vipPrice']=Member::getDiscountByGood($this->wqData['userId'],$result['id'],$specsData[$i]['SalesPrice'])['money'];
}
$attrData=(new \yii\db\Query())
->select('id,attrName,attrStr')
->from('{{%ybwm_good_attribute}}')
->where(['goodId'=>$result['id']])
->all();
$meterialData=(new \yii\db\Query())
->select('id,materialName,SalesPrice,SalesStock,fillType')
->from('{{%ybwm_good_materia}}')
->where('goodId=:goodId',[':goodId'=>$result['id']])
->all();
foreach ($attrData as $k=>$v){
$attrData[$k]['attrStr']=json_decode($v['attrStr']);
}
$data=array(
'specsData'=>$specsData,
'attrData'=>$attrData,
'meterialData'=>$meterialData,
);
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$data]);die;
}
}
//删除整行
public function actionDelLine(){
$result=axios_request();
$tableId=$result['tableId']?:0;
if($result['id']){
if($result['type']==1){
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['id'=>$result['id']])->execute();
}
if($result['type']==2){
$operatorId= Yii::$app->session->get('userInfo')['id'];
if($operatorId){
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['tableId'=>$tableId,'operatorId'=>$operatorId,'state'=>1])->execute();
}
}
if($result['type']==3){
$operatorId= Yii::$app->session->get('userInfo')['id'];
if($operatorId){
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}',['isNew'=>3,'changeAt'=>time()], ['tableId'=>$tableId,'operatorId'=>$operatorId,'state'=>1])->execute();
}
}
return $this->result(1, '成功');
}
}
//挂单列表
public function actionTakeOrderList(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$tableId=$result['tableId']?:0;
$operatorId= Yii::$app->session->get('userInfo')['id'];
$data=(new \yii\db\Query())
->select('outTradeNo')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'operatorId'=>$operatorId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>3])
->groupBy('outTradeNo')
->orderBy('id desc')
->all();
$newData=[];
foreach ($data as $k=>$v){
$newData[$k]['goodsData']= (new \yii\db\Query())
->select('FROM_UNIXTIME(createdAt)createdAt,name,icon,money,num,(money*num)sum')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'outTradeNo'=>$v['outTradeNo'],'storeId' => $storeId,'uniacid'=>$uniacid])
->orderBy('id desc')
->all();
$newData[$k]['createdAt']=array_column($newData[$k]['goodsData'],'createdAt')[0];
$newData[$k]['money']=number_format(array_sum(array_column($newData[$k]['goodsData'],'sum')),2);
$newData[$k]['outTradeNo']=$v['outTradeNo'];
}
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$newData]);die;
}
//挂单
public function actionPutOrder(){
$result=axios_request();
$tableId=$result['tableId']?:0;
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$operatorId= Yii::$app->session->get('userInfo')['id'];
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}',['state'=>3,'createdAt'=>time()], ['state'=>1,'tableId'=>$tableId,'operatorId'=>$operatorId,'storeId' => $storeId,'uniacid'=>$uniacid])->execute();
return $this->result(1, '成功');
}
//取单
public function actionTakeOrder(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$outTradeNo=$result['outTradeNo'];
$tableId=$result['tableId']?:0;
$operatorId= Yii::$app->session->get('userInfo')['id'];
if($result['type']==1){
$list=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->andWhere(['>','outTradeNo',0])
->all();
if($list){
$ids=array_column($list,'id');
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}',['state'=>3],['in','id',$ids])->execute();
}
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}',['state'=>1],['outTradeNo'=>$outTradeNo,'storeId' => $storeId,'uniacid'=>$uniacid])->execute();
return $this->result(1, '成功');
}else{
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}',['outTradeNo'=>$outTradeNo])->execute();
return $this->result(1, '成功');
}
}
//修改购物车数量
public function actionSetGoodNum(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$data=[];
if(trim($result['num'])){
$data['num']=trim($result['num']);
}
if(trim($result['price'])){
$data['money']=trim($result['price']);
}
try{
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', $data, ['id' =>$result['id']])->execute();
} catch(Exception $e) {
var_dump($e);die;
}
return $this->result(1, '成功');
}
//获取收银员订单列表
public function actionGetOrderList(){
$result=axios_request();
$uniacid = $this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$timeType=$result['timeType']?:1;
$keyword=$result['keyword'];
$startTime=$result['startTime'];
$endTime=$result['endTime'];
$state=$result['state'];
$page=$result['page']?:1;
$size=$result['size']?:10;
$orderBy='id desc';
$orderType=$result['orderType']?:1;
if($orderType==1){
$tableName='{{%ybwm_instore_order}}';
}else{
$tableName='{{%ybwm_cashier_order}}';
}
$operatorId= Yii::$app->session->get('userInfo')['id'];
$table=(new \yii\db\Query())
->select(['id','outTradeNo','money','callNo','origin','takeNo','isOut','from_unixtime(payAt)payAt','callNum'])
->from($tableName)
->where('origin=5 AND operatorId=:operatorId AND orderMode=2 AND uniacid=:uniacid AND deleteAt=0 AND storeId=:storeId AND state not in(1,5)',[':uniacid'=>$this->wqData['uniacid'],':storeId'=>$storeId,':operatorId'=>$operatorId]);
if($state){
$state=$state+1;
$table->andWhere("state=".$state);
}
if($startTime){
$startTime=strtotime($startTime." 00:00:00");
$endTime=strtotime($endTime." 23:59:59");
}else{
if($timeType==2){
$startTime=strtotime(date("Y-m-d")." 00:00:00 -1day");
$endTime=strtotime(date("Y-m-d")." 23:59:59 -1day");
}elseif($timeType==3){
$startTime=strtotime(date("Y-m-d")." 00:00:00 -6day");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
}else{
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
}
}
if($keyword){
$table->andWhere(['like','outTradeNo',$keyword]);
}
if($startTime){
$table->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
//var_dump(ddSql($table));die;
$count=$table->count();
$table->offset(($page - 1) * $size)->limit($size)->orderby($orderBy);
$res=$table->all();
for($i=0;$i<count($res);$i++){
$goods=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['outTradeNo'=>$res[$i]['outTradeNo'],'storeId' => $storeId,'uniacid'=>$uniacid])
->all();
$res[$i]['goodsArr']=$goods;
$info=(new \yii\db\Query())
->select(['a.id','a.origin','a.callNo','a.takeNo','a.outTradeNo','a.money','a.isOut','a.payMode','from_unixtime(a.payAt)payAt','a.state','a.userNote','b.storeActualMoney','c.userName','c.userTel','a.callNum','a.userId','(a.preferentialMoney+a.newMoney+a.platformCouponPreferential+a.couponPreferential) as allPreferential'])
->from($tableName.' as a')
->join('LEFT JOIN', '{{%ybwm_bill}} as b', 'b.outTradeNo = a.outTradeNo')
->join('LEFT JOIN', '{{%ybwm_member}} as c', 'c.id = a.userId')
->where('a.id=:id',[':id'=>$res[$i]['id']])->all();
$res[$i]['detail']=$info;
}
return $this->result(1,'成功',$res,$count);
}
//订单详情
public function actionGetOrderOper(){
$result=axios_request();
$uniacid = $this->wqData['uniacid'];
$storeId=$result['storeId']?:$this->shop_id;
$outTradeNo=$result['outTradeNo'];
$list= (new \yii\db\Query())
->select('name,userId,icon,money,num,outTradeNo,from_unixtime(createdAt)createdAt,count(*)count,(money*num)sum')
->from('{{%ybwm_cashier_goods}}')
->where(['outTradeNo'=>$outTradeNo,'storeId' => $storeId,'uniacid'=>$uniacid])
->orderBy('id desc')
->all();
$data= (new \yii\db\Query())
->select('money,from_unixtime(createdAt)createdAt')
->from('{{%ybwm_bill}}')
->where(['outTradeNo'=>$outTradeNo,'storeId' => $storeId,'uniacid'=>$uniacid])
->one();
echo json_encode(['cdde'=>1,'msg'=>'成功','data'=>$list,'createdAt'=>$data['createdAt'],'money'=>$data['money']]);die;
}
public function actionSaveGoods(){
$session = new Session;
$session->open();
$request = Yii::$app->request;
$result = axios_request();
$userId = $result['userId'];
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$tableId=$result['tableId'];
$operatorId= Yii::$app->session->get('userInfo')['id'];
if ($request->isPost) {
if (!$storeId) {
echo json_encode(['code' => 2, 'msg' => '无效的门店']);
die;
}
if (!$result['type']) {
echo json_encode(['code' => 2, 'msg' => '无效的请求']);
die;
}
if (!$result['goodsId']) {
echo json_encode(['code' => 2, 'msg' => '无效的请求']);
die;
}
$goodInfo = (new \yii\db\Query())
->from('{{%ybwm_core_goods}}')
->where(['id' => $result['goodsId'], 'uniacid' => $uniacid])
->one();
$maiId = Store::getMainStore($uniacid);
if ($storeId !== $maiId['id']) {
$storeGood = (new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['goodsId' => $result['goodsId'], 'storeId' => $storeId, 'uniacid' => $uniacid])
->one();
$goodInfo['price'] = $storeGood['price'];
$goodInfo['stock'] = $storeGood['stock'];
}
//判断库存
if ($result['groupId']) {
$count = (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId' => $tableId, 'operatorId' => $operatorId, 'goodsId' => $result['goodsId'], 'groupId' => $result['groupId'], 'storeId' => $storeId, 'uniacid' => $uniacid, 'state' => 1])
->count('num');
$specsInfo = (new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id' => $result['groupId'], 'goodId' => $result['goodsId']])
->one();
$stock = $specsInfo['SalesStock'];
} else {
$count = (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId' => $tableId, 'operatorId' => $operatorId, 'goodsId' => $result['goodsId'], 'storeId' => $storeId, 'uniacid' => $uniacid, 'state' => 1])
->one()['num'];
$stock = $goodInfo['stock'];
}
if ($result['type'] == 1) {
$maxCount = $count + $result['num'] ?: $count + 1;
if ($count + $result['num'] >= $stock) {
echo json_encode(['code' => 2, 'msg' => '库存不足']);
die;
}
}
}
$material = json_encode($result['materialId']);
$materialData=$result['materialId'];
if($result['attribute']){
$attribute=implode(',',$result['attribute']);
}else{
$attribute='';
}
if($result['materialId']){
$materData = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId' => $result['goodsId'], 'uniacid' => $uniacid])
->andWhere(['in', 'id', $materialData])
->all();
$materJson = json_encode($result['materialId']);
$ids=array_column($result['materialId'],'materialId');
$materialMoney = array_sum(array_column($materData, 'SalesPrice'));
$nameStr = implode(',', array_column($materData, 'materialName'));
}
$query=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'state'=>1,'storeId' => $storeId, 'uniacid' => $uniacid]);
if($result['groupId']){
$query->andWhere(['goodsId'=>$result['goodsId'],'material'=>$material,'attribute'=>$attribute]);
}else{
$query->andWhere(['goodsId'=>$result['goodsId']]);
}
$list=$query->one();
$id=$result['id']?:$list['id'];
if($result['id']){
$list=(new \yii\db\Query())->from('{{%ybwm_cashier_goods}}')->where(['id'=>$id])->one();
}
if($list){
if($result['type']==1){
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num'=>$list['num']+1],['id'=>$id])->execute();
}else{
if($list['num']==1){
Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}',['id'=>$id])->execute();
}else{
Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num'=>$list['num']-1],['id'=>$id])->execute();
}
}
}else{
$data = array(
'userId' => $userId,
'storeId' => $storeId,
'goodsId' => $result['goodsId'],
'name' => $goodInfo['name'],
'icon' => $goodInfo['icon'],
'num' => $goodInfo['minNum']?:1,
'money' =>$goodInfo['SalesPrice']?:$goodInfo['price'],
'createdAt' => time(),
'uniacid' => $uniacid,
'groupId' => $result['groupId'] ?: '',
'groupName' => $specsInfo['specsName'],
'attribute' =>$attribute,
'material' => $materJson ?: '',
'materialIds' => $ids ?: '',
'materialName' => $nameStr ?: '',
'materialMoney' => $materialMoney ?: 0.00,
'unit' => $goodInfo['unit'],
'tableId'=>$tableId,
'operatorId'=>$operatorId,
);
Yii::$app->db->createCommand()->insert('{{%ybwm_cashier_goods}}', $data)->execute();
}
echo json_encode(['code' => 1, 'msg' => '成功']);die;
}
public function saveMoney($orderId,$price,$materialMoney,$type){
//修改订单的金额
if($type==1){
$money=bcadd($price,$materialMoney,2);
}else{
$money=bcsub($price,$materialMoney,2);
}
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}',['money'=>$money],['id'=>$orderId])->execute();
}
}