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

1115 lines
54 KiB
PHP

<?php
namespace app\controllers\channel;
use app\models\common\Printing;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\captcha\CaptchaValidator;
use Yii\web\Session;
use app\models\common\Store;
use app\models\common\Config;
use app\models\common\Member;
use app\models\common\Order;
use app\models\common\Good;
class CashierGoodsController extends CommonController{
private $shop_id;
public function init(){
parent::init();
$this->shop_id=Store::getMainStore($this->wqData['uniacid'])['id'];
$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(){
$session = new Session;
$session->open();
$request = Yii::$app->request;
$result = axios_request();
$userId = $result['userId']?:0;
$storeId=$result['storeId']?:$this->shop_id;
$uniacid = $this->wqData['uniacid'];
$tableId=$result['tableId']?:0;
$operatorId= Yii::$app->session->get('userInfo')['id'];
if(!$operatorId){
return $this->result(2, '请用收银员账号登录');
}
$list= (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'state'=>1,'storeId' => $storeId, 'uniacid' => $uniacid,'operatorId'=>$operatorId])
->one();
$orderId=$list['orderId'];
$goodInfo=Good::getGoodInfo($uniacid,$storeId,$result['goodsId'],$result['groupId']);
$payModeData=Config::getStoreSet('instoreSet',$storeId);
$eatType=$payModeData['payMode']?:2;
if($tableId){
if($eatType==1){
$state=1;
}else{
$state=2;
}
}
if($tableId){
$orderMode=1;
$orderInfo=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['tableId'=>$tableId])
->andWhere(['<=','state',$state])
->orderBy('id desc')
->one();
$orderId=$orderInfo['id'];
$tableInfo=(new \yii\db\Query())
->select('a.name,t.name as typeName,e.name as areaName,t.minNum,t.maxNum')
->from('{{%ybwm_table}} as a')
->join('LEFT JOIN', '{{%ybwm_table_type}} as t', 't.id = a.typeId')
->join('LEFT JOIN', '{{%ybwm_table_area}} as e', 'e.id = a.areaId')
->where('a.id=:id',[':id'=>$tableId])->one();
}else{
$orderMode=2;
}
if(!$orderId){
$outTradeNo = '3409' . date("YmdHis") . randomAESKey(6);
$data = array(
'money'=>0,
'originMoney'=>0,
'outTradeNo' => $outTradeNo,
'state' =>1,
'storeId' => $storeId,
'createdAt' => time(),
'uniacid' => $uniacid,
'userId' => $userId,
'orderMode' =>$orderMode,
'isOut' => 1,
'origin' => 5,
'operatorId' => $operatorId,
'tableId'=>$tableId,
'eatType'=>$eatType,
'typeName'=>$tableInfo['typeName'] . "(" . $tableInfo['minNum'] . "-" . $tableInfo['maxNum'] . ")",
'regionName'=>$tableInfo['areaName'],
'number'=>$tableInfo['name']
);
Yii::$app->db->createCommand()->insert('{{%ybwm_instore_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if($tableId){
Yii::$app->db->createCommand()->update('{{%ybwm_table}}', ['state' =>2], ['id' =>$tableId])->execute();
}
}
$orderInfo=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id'=>$orderId])
->one();
if ($request->isPost) {
if(!$storeId){
echo json_encode(['code' => 2, 'msg' => '无效的门店']);die;
}
if(!$result['type']){
echo json_encode(['code' => 2, 'msg' => '无效的请求']);die;
}
//判断库存
if($result['groupId']){
$count= (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['goodsId' => $result['goodsId'], 'groupId' => $result['groupId'],'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->one()['num'];
}else{
$count= (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['goodsId' => $result['goodsId'], 'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->one()['num'];
}
$stock=$goodInfo['stock'];
$result['num']=$result['num']?$result['num']:1;
if($result['type']==1){
$result['num']=$result['num']?$result['num']:1;
$maxCount=$count+$result['num'];
if($maxCount>$stock){
echo json_encode(['code' => 2, 'msg' => '库存不足']);die;
}
}
if($result['attribute']){
$attribute=implode(',',$result['attribute']);
}else{
$attribute='';
}
if($result['id']){
$row=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['id' => $result['id']])
->one();
$changeMoney=$row['money'];
if($result['type']==1){
if($row['num']+1>$goodInfo['maxNum']&&$goodInfo['maxNum']>0){
echo json_encode(['code' => 2, 'msg' => '超出限购数量']);die;
}
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']+1], ['id' =>$result['id']])->execute();
$this->changePrice($orderId,$changeMoney,1);
}else{
if($goodInfo['minNum']){
if($row['num']>$goodInfo['minNum']){
$this->changePrice($orderId,$changeMoney,2,1);
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], ['id' =>$result['id']])->execute();
}else{
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['id' =>$result['id']])->execute();
$this->changePrice($orderId,$changeMoney,2,$goodInfo['minNum']);
}
}else{
if($row['num']>1){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], ['id' =>$result['id']])->execute();
$this->changePrice($orderId,$changeMoney,2,1);
}else{
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['id' =>$result['id']])->execute();
$this->changePrice($orderId,$changeMoney,2,1);
}
}
}
}else{
//如果是有规格商品
if($result['groupId']){
//$ids=array_column($result['material'],'materialId');
$ids=$result['materialId'];
if(count($ids)>1){
$ids=implode(',',$ids);
}else{
$ids=$ids[0]?:'';
}
//dd($ids);die;
//判断购物车是否有商品
$row = (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'goodsId' => $result['goodsId'], 'groupId' => $result['groupId'],'attribute' =>$attribute,'materialIds' =>$ids, 'storeId' => $storeId, 'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->one();
$changeMoney=$row['money'];
//print_R(['goodsId' => $result['goodsId'], 'groupId' => $result['groupId'],'attribute' => json_encode($result['attribute']),'materialIds' =>$ids, 'storeId' => $storeId, 'userId' => $userId,'uniacid'=>$uniacid]);die;
if($result['type']==1){
if($row){
$addNum=1;
}else{
$addNum=$goodInfo['minNum'];
}
$number=$result['num']?:$addNum;
$count=$row['num']+$number;
if($count>$goodInfo['maxNum']&&$goodInfo['maxNum']>0){
echo json_encode(['code' => 2, 'msg' => '超出限购数量']);die;
}
if($row){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' => $number+$row['num']], ['id' => $row['id']])->execute();
$changeMoney=$row['money'];
$this->changePrice($orderId,$changeMoney,1);
}else{
$money=$goodInfo['price'];
//写入部分
if ($result['groupId']) {
$specsInfo = (new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id' => $result['groupId'],'goodId' => $result['goodsId']])
->one();
$groupName = $specsInfo['specsName'];
}
if ($result['materialId']) {
//$materialData = array_column($result['material'], 'materialId');
$materialData=$result['materialId'];
$materData = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId' => $result['goodsId'], 'uniacid' => $uniacid])
->andWhere(['in', 'id', $materialData])
->all();
$materialMoney = array_sum(array_column($materData, 'SalesPrice'));
$nameStr = implode(',', array_column($materData, 'materialName'));
$materJson = json_encode($result['materialId']);
}
$money = $specsInfo['SalesPrice'];
//$newMoney=Member::getDiscountByGood($userId,$result['goodsId'],$money);
$data = array(
'userId' => $userId,
'storeId' => $storeId,
'goodsId' => $result['goodsId'],
'name' => $goodInfo['name'],
'icon' => $goodInfo['icon'],
'num' => $number,
'money' => bcadd($money,$materialMoney,2),
'operatorId' => $operatorId,
'createdAt' => time(),
'uniacid' => $uniacid,
'groupId' => $result['groupId']?:0,
'groupName' => $groupName,
'attribute' =>$attribute,
'material' => $materJson ?: '',
'materialIds' => $ids ?: '',
'materialName' => $nameStr ?: '',
'materialMoney' => $materialMoney ?: 0.00,
'originalMoney'=>$money,
'unit' => $goodInfo['unit'],
'tableId' => $tableId,
'orderId'=>$orderId,
'boxMoney'=>$goodInfo['boxMoney'],
);
$re = Yii::$app->db->createCommand()->insert('{{%ybwm_cashier_goods}}', $data)->execute();
$changeMoney=bcadd($money,$materialMoney,2);
$this->changePrice($orderId,$changeMoney,1,$number);
}
}else{
if($goodInfo['minNum']){
if($row['num']>$goodInfo['minNum']){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], ['goodsId' =>$result['goodsId'],'operatorId'=>$operatorId])->execute();
$this->changePrice($orderId,$changeMoney,2);
}else{
$this->changePrice($orderId,$changeMoney,2,$goodInfo['minNum']);
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['goodsId' =>$result['goodsId'],'operatorId'=>$operatorId])->execute();
}
}else{
if($row['num']>1){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], ['goodsId' =>$result['goodsId']])->execute();
}else{
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['goodsId' =>$result['goodsId'],'operatorId'=>$operatorId])->execute();
}
$this->changePrice($orderId,$changeMoney,2);
}
}
}else{
$material=$result['material']?json_encode($result['material']):'';
$row=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'goodsId' => $result['goodsId'],'storeId' => $storeId, 'uniacid'=>$uniacid,'material'=>$material,'attribute'=>$attribute,'state'=>1,'operatorId'=>$operatorId])
->one();
//print_R(['goodsId' => $result['goodsId'],'storeId' => $storeId, 'userId' => $userId,'uniacid'=>$uniacid,'material'=>$material,'attribute'=>json_encode($result['attribute'])]);die;
if($result['type']==1){
if(!$row){
$number=$goodInfo['minNum']?:1;
if($result['num']){
if($result['num']<$goodInfo['minNum']){
$result['num']=$goodInfo['minNum'];
//echo json_encode(['code' => 2, 'msg' => '起购数量不足'.$goodInfo['minNum']]);die;
}
}
}
if($result['num']){
$number=$result['num'];
}
if($row['num']+$number>$goodInfo['maxNum']&&$goodInfo['maxNum']>0){
echo json_encode(['code' => 2, 'msg' => '超出限购数量']);die;
}else{
if($row){
$changeMoney=$row['money'];
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' => $number+$row['num']], ['id' => $row['id']])->execute();
$this->changePrice($orderId,$changeMoney,1);
}else{
if ($result['materialId']) {
$materialData = $result['materialId'];
$materData = (new \yii\db\Query())
->from('{{%ybwm_good_materia}}')
->where(['goodId' => $result['goodsId'], 'uniacid' => $uniacid])
->andWhere(['in', 'id', $materialData])
->all();
$materialMoney = array_sum(array_column($materData, 'SalesPrice'));
$nameStr = implode(',', array_column($materData, 'materialName'));
$materJson = json_encode($result['material']);
}
$ids=$result['materialId'];
if(count($ids)>1){
$ids=implode(',',$ids);
}else{
$ids=$ids[0];
}
$newMoney=Member::getDiscountByGood($userId,$result['goodsId'],$goodInfo['price']);
$data = array(
'userId' => $userId,
'storeId' => $storeId,
'goodsId' => $result['goodsId'],
'name' => $goodInfo['name'],
'icon' => $goodInfo['icon'],
'num' => $number,
'money' => bcadd($goodInfo['price'],$materialMoney,2),
'operatorId'=>$operatorId,
'createdAt' => time(),
'uniacid' => $uniacid,
'originalMoney'=>$goodInfo['price'],
'attribute' =>$attribute,
'material' => $materJson ?: '',
'materialIds' => $ids ?: '',
'materialName' => $nameStr ?: '',
'materialMoney' => $materialMoney ?: 0.00,
'tableId' => $tableId,
'orderId'=>$orderId,
'boxMoney'=>$goodInfo['boxMoney']
);
$re = Yii::$app->db->createCommand()->insert('{{%ybwm_cashier_goods}}', $data)->execute();
$changeMoney=bcadd($goodInfo['price'],$materialMoney,2);
$this->changePrice($orderId,$changeMoney,1,$number);
}
}
}else{
//扣除购物车商品的Moeny
$updateData = ['tableId'=>$tableId, 'userId' => $userId, 'storeId' => $storeId, 'goodsId' => $result['goodsId'],'operatorId'=>$operatorId];
$cashInfo=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where($updateData)->one();
$changeMoney=$cashInfo['money'];
if($goodInfo['minNum']){
if($row['num']>$goodInfo['minNum']){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], $updateData)->execute();
$this->changePrice($orderId,$changeMoney,2);
}else{
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', $updateData)->execute();
$this->changePrice($orderId,$changeMoney,2,$goodInfo['minNum']);
}
}else{
if($row['num']>1){
$re = Yii::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['num' =>$row['num']-1], $updateData)->execute();
}else{
$re=Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', $updateData)->execute();
}
$this->changePrice($orderId,$changeMoney,2);
}
}
}
}
echo json_encode(['code' => 1, 'msg' => '成功']);die;
$data = (new \yii\db\Query())
->select('id,(originalMoney*num)originalMoney,name,icon,money,attribute,goodsId,groupId,groupName,material,materialIds,materialMoney,materialName,num,materialMoney,(money*num)totalMoney')
->from('{{%ybwm_cashier_goods}}')
->where(['uniacid' => $uniacid, 'storeId' => $storeId,'state'=>1,'operatorId'=>$operatorId])
->all();
$materTotal = 0;
$goodTotal = 0;
$originalMoney=0;
foreach ($data as $v) {
$goodTotal += $v['totalMoney'];
$originalMoney+=$v['originalMoney'];
$materTotal +=bcmul($v['materialMoney'],$v['num'],2);
}
$newData = array(
'vipDiscount'=>bcsub($originalMoney,$goodTotal,2)?:0,
'data' => $data,
'price' => $goodTotal,
'oldPrice' => bcadd($goodTotal,2),
'reduce'=>0.00
);
}
$count=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['uniacid' => $uniacid, 'goodsId'=>$result['goodsId'],'storeId' => $storeId,'state'=>1,'operatorId'=>$operatorId])
->sum('num')?:0;
echo json_encode(['code' => 1, 'msg' => '成功', 'data' => $newData, 'count' =>$count]);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;
$uniacid=$this->wqData['uniacid'];
$orderCount=0;$boxMoney=0.00;
if($request->isGet){
$tableInfo=(new \yii\db\Query())
->from('{{%ybwm_table}}')
->where('id=:id',[':id'=>$tableId])->one();
$res=Config::getStoreSet('instoreSet',$result['storeId']);
if($tableId){
$item=2;
$uids=[2,1,3];
$payModeData=Config::getStoreSet('instoreSet',$storeId);
$eatType=$payModeData['payMode']?:2;;
if($tableId){
if($eatType==1){
$state=1;
}else{
$state=2;
}
}
$order =(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('tableId=:tableId',[':tableId'=>$tableId])
->andWhere(['eatType'=>$eatType,'storeId' => $storeId])
->andWhere(['<','state',3])
// ->andWhere(['<=','state',$state])
->orderBy('id desc')->one();
if($order){
$data=(new \yii\db\Query())
->select('*,material materialName,(money*num)sum')
->from('{{%ybwm_order_goods}}')
->where(['orderId'=>$order['id'],'item'=>$item])
->orderBy(["FIELD(addType, ".join(',',$uids).")" => true])
->all();
$tablewareSum=$order['tablewareMoney'];
$count=0;$foodMoney=0;
foreach ($data as $k=>$v){
if($v['addType']<3){
$count+=$v['num'];
$foodMoney+=bcmul($v['money'],$v['num'],2);
}
$data[$k]['groupName']='';
if($v['groupId']){
$data[$k]['groupName']=(new \yii\db\Query())
->from('{{%ybwm_good_specs}}')
->where(['id'=>$v['groupId']])
->one()['specsName'];
}
}
$money=bcadd($foodMoney,$tablewareSum,2);
$money=bcsub($money,$order['discount'],2);
//商品列表其他
$tableData=array(
'tablewareMoney'=>$order['tablewareMoney'],
'tablewareNum'=>$order['tablewareNum'],
'tablewareSum'=>$tablewareSum,
'people'=>$tableInfo['people']?:$order['people'],
'serviceMoney'=>number_format($res['serviceMoney'],2)
);
$detail=$order;
}else{
$data=(new \yii\db\Query())
->select('*,(money*num)sum')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->all();
//var_dump(ddSql($data));die;
$count=array_sum(array_column($data,'num'));
$money=array_sum(array_column($data,'sum'));
$money = sprintf("%.2f", $money);
}
$cashierData=(new \yii\db\Query())
->select('*,(money*num)sum')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->all();
$carCount=array_sum(array_column($cashierData,'num'));
$carMoney=array_sum(array_column($cashierData,'sum'));
$carMoney = sprintf("%.2f", $carMoney);
}else{
$data=(new \yii\db\Query())
->select('*,(money*num)sum,(money*num)totalMoney,(boxMoney*num)boxMoney')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>1,'operatorId'=>$operatorId])
->all();
$orderId=array_column($data,'orderId')[0];
$orderCount=(new \yii\db\Query())
// ->select('id,orderId')
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'state'=>2,'operatorId'=>$operatorId])
->andWhere(['>','orderId',''])
->groupBy('orderId')
->count();
$orderInfo=[];$count=0;$money=0.00;
if($data){
$detail=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('id=:id',[':id'=>$data[0]['orderId']])->one();
$count=$detail['goodsNum'];
$money=$detail['money'];
}
if($result['isOut']==2){
$fastSet= Config::getStoreSet('fastSet',$storeId);
if($fastSet['boxType']==2){
$boxMoney=number_format(array_sum(array_column($data,'boxMoney')),2);
}else{
$boxMoney=$fastSet['boxMoney'];
}
}
//var_dump($data);die;
$carMoney=number_format(array_sum(array_column($data,'sum')),2);
}
$money=bcadd($money,$boxMoney,2);
$instoreSet=array(
'serviceType'=>$res['serviceType'],
'serviceMoney'=>$res['serviceMoney'],
'people'=>$order['people'],
'boxMoney'=>$boxMoney
);
if($detail){
$detail['costPrice']=bcadd($detail['originMoney'],$detail['tablewareMoney'],2);
}
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$data,'cashierData'=>$cashierData,'money'=>$money,'count'=>$count,'orderCount'=>$orderCount,'instoreSet'=>$instoreSet,'tableData'=>$tableData,'carCount'=>$carCount,'carMoney'=>$carMoney,'detail'=>$detail]);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, 'storeId' => $storeId,'status'=>1,'operatorId'=>$operatorId])->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($tableId){
$item=2;
}else{
$item=3;
}
if($result['id']){
if($result['type']==1){
$res=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where('id=:id',[':id'=>$result['id']])
->one();
$orderInfo=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('id=:id',[':id'=>$res['orderId']])
->one();
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['id'=>$result['id']])->execute();
$orderId=$orderInfo['id'];
$num=bcsub($orderInfo['goodsNum'],$res['num'],2);
$money=bcsub($orderInfo['money'],$res['money'],2);
$originMoney=bcsub($orderInfo['originMoney'],$res['money'],2);
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['goodsNum'=>$num,'money'=>$money,'originMoney'=>$originMoney], ['id' =>$orderId])->execute();
}
//整单取消
if($result['type']==2){
$storeId=$result['storeId']?:$this->shop_id;
$tableId=$result['tableId']?:0;
$uniacid=$this->wqData['uniacid'];
$operatorId= Yii::$app->session->get('userInfo')['id'];
$list= (new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['tableId'=>$tableId,'state'=>1,'storeId' => $storeId, 'uniacid' => $uniacid,'operatorId'=>$operatorId])
->one();
$orderId=$list['orderId'];
YII::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}', ['tableId'=>$tableId,'state'=>1,'operatorId'=>$operatorId])->execute();
YII::$app->db->createCommand()->delete('{{%ybwm_order_goods}}', ['id'=>$orderId['id'],'item'=>$item])->execute();
YII::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['state'=>5],['id' =>$orderId['id']])->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('orderId')
->from('{{%ybwm_cashier_goods}}')
->where(['state'=>2,'tableId'=>$tableId,'storeId' => $storeId,'uniacid'=>$uniacid,'operatorId'=>$operatorId])
->groupBy('orderId')
->all();
$newData=[];
foreach ($data as $k=>$v){
$newData[$k]['orderId']=$v['orderId'];
$newData[$k]['goodsData']= (new \yii\db\Query())
->select('FROM_UNIXTIME(createdAt)createdAt,name,icon,money,num,(money*num)sum')
->from('{{%ybwm_order_goods}}')
->where(['orderId'=>$v['orderId'],'item'=>3])
->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);
}
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$newData]);die;
}
//挂单
public function actionPutOrder(){
$request = Yii::$app->request;
if ($request->isPost) {
$result = axios_request();
$userId = $result['userId']?:0;
$storeId = $result['storeId'] ?: $this->shop_id;
$uniacid = $this->wqData['uniacid'];
if (!$storeId) {
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;
}
$orderId=array_column($carData,'orderId')[0];
YII::$app->db->createCommand()->delete('{{%ybwm_order_goods}}', ['item'=>3,'orderId'=>$orderId])->execute();
foreach ($carData as $v) {
$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']?:0,
'material' => $v['material'],
'attribute' => $v['attribute'],
'item' =>3,
);
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}', ['state'=>2,'orderId'=>$orderId],['id'=>$v['id']])->execute();
YII::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $goodsData)->execute();
}
echo json_encode(['code' => 1, 'msg' => '成功', 'orderId' => $orderId]);die;
}
}
//取单
public function actionTakeOrder(){
$result=axios_request();
$orderId=$result['orderId'];
$storeId = $result['storeId'] ?: $this->shop_id;
$uniacid = $this->wqData['uniacid'];
$operatorId = Yii::$app->session->get('userInfo')['id'];
if($result['type']==1){
$list=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_cashier_goods}}')
->where(['state'=>1,'tableId'=>0,'storeId' => $storeId, 'uniacid' => $uniacid])
->all();
if($list){
return $this->result(2, '请先将购物车内商品挂单或者结账后再取单');
}
YII::$app->db->createCommand()->update('{{%ybwm_cashier_goods}}',['state'=>1],['orderId'=>$orderId])->execute();
return $this->result(1, '成功');
}else{
Yii::$app->db->createCommand()->delete('{{%ybwm_instore_order}}',['id'=>$orderId])->execute();
Yii::$app->db->createCommand()->delete('{{%ybwm_order_goods}}',['orderId'=>$orderId])->execute();
Yii::$app->db->createCommand()->delete('{{%ybwm_cashier_goods}}',['orderId'=>$orderId])->execute();
return $this->result(1, '成功');
}
}
//修改购物车数量
public function actionSetGoodNum(){
$result=axios_request();
$operatorId = Yii::$app->session->get('userInfo')['id'];
$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']);
}
$goodList=(new \yii\db\Query())->from('{{%ybwm_cashier_goods}}')
->where(['id' =>$result['id']])->one();
$goodInfo=Good::getGoodInfo($uniacid,$storeId,$goodList['goodsId'],$goodList['groupId']);
//判断库存
$count=$goodInfo['num'];
$stock=$goodInfo['stock'];
if($result['num']>($stock-$count)){
echo json_encode(['code' => 2, 'msg' => '库存不足']);die;
}
$orderInfo=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id'=>$goodList['orderId']])
->one();
$orderId=$goodList['orderId'];
try{
if($result['num']){
if($result['num']<$goodList['num']){
$num=bcsub($goodList['num'],$result['num']);//修改的数量
$money=bcsub($orderInfo['money'],bcmul($goodList['money'],$num,2),2);
$originMoney=bcsub($orderInfo['originMoney'],bcmul($goodList['money'],$num,2),2);
$number=bcsub($orderInfo['goodsNum'],$num);
}else{
$num=bcsub($result['num'],$goodList['num']);
$money=bcadd($orderInfo['money'],bcmul($goodList['money'],$num,2),2);
$originMoney=bcadd($orderInfo['originMoney'],bcmul($goodList['money'],$num,2),2);
$number=bcadd($orderInfo['goodsNum'],$num);
}
if($money<0||$originMoney<0){
return $this->result(2, '计算金额有误');
}
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['goodsNum'=>$number,'money'=>$money,'originMoney'=>$originMoney], ['id' =>$orderId])->execute();
}
if($result['price']){
$list=(new \yii\db\Query())
->from('{{%ybwm_cashier_goods}}')
->where(['state'=>1,'tableId'=>0,'storeId' => $storeId, 'uniacid' => $uniacid,'operatorId'=>$operatorId])
->all();
$money=0;$number=0;
foreach ($list as $v){
$money+=bcmul($v['money'],$v['num'],2);
$number+=$v['num'];
}
$money=number_format($money,2);
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['goodsNum'=>$number,'money'=>$money], ['id' =>$goodList['orderId']])->execute();
}
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'];
$keyword=$result['keyword'];
$startTime=$result['startTime'];
$endTime=$result['endTime'];
$state=$result['state'];
$page=$result['page']?:1;
$size=$result['size']?:10;
$orderType=$result['orderType']?:1;
$orderBy='id desc';
$table=(new \yii\db\Query())
->select(['id','eatType','number','receiptAt','createdAt','completeAt','people','payMode','state','discount','outTradeNo','tablewareMoney','money','callNo','origin','takeNo','isOut','payAt','callNum','tableId'])
->from('{{%ybwm_instore_order}}')
->where('uniacid=:uniacid AND deleteAt=0 AND storeId=:storeId ',[':uniacid'=>$this->wqData['uniacid'],':storeId'=>$storeId]);
switch ($result['state']){
case 0;
if($orderType==2){
$table->andWhere(['in','state',[2,3,4,6,7,8,9]]);
}
break;
case 1;
if($orderType==1){
$table->andWhere(['state'=>2]);
}
if($orderType==2){
$table->andWhere(['in','state',[2,3,6,8]]);
}
break;
case 2;
if($orderType==1){
$table->andWhere(['in','state',[3,5]]);
}
if($orderType==2){
$table->andWhere(['in','state',[4,7,9]]);
}
break;
case 3;
$table->andWhere(['in','state',[5,6,7,8,9]]);
break;
}
if($orderType==1){
$table->andWhere(['orderMode'=>1]);
}
if($orderType==2){
$table->andWhere(['orderMode'=>2]);
}
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");
}elseif($timeType==1){
$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++){
$uids=[2,1,3];
if($res[$i]['tableId']){
$item=2;
}else{
$item=3;
}
$goods=(new \yii\db\Query())
->select('name,money,num,(num*money)sumMoney,material,attribute')
->from('{{%ybwm_order_goods}}')
->where(['orderId'=>$res[$i]['id'],'item'=>$item])
->orderBy(["FIELD(addType, ".join(',',$uids).")" => true])
->all();
$res[$i]['goodsArr']=$goods;
$info=(new \yii\db\Query())
->select(['a.id','a.refundMoney','a.state','a.eatType','d.name as number','a.tablewareMoney','a.originMoney','a.origin','a.discount','a.callNo','a.takeNo','a.outTradeNo','a.money','a.isOut','a.payMode','a.payAt','a.state','a.userNote','b.storeActualMoney','c.userName','c.userTel','a.callNum','a.userId','(a.discount+a.preferentialMoney+a.newMoney+a.platformCouponPreferential+a.couponPreferential) as allPreferential'])
->from('{{%ybwm_instore_order}} as a')
->join('LEFT JOIN', '{{%ybwm_bill}} as b', 'b.outTradeNo = a.outTradeNo')
->join('LEFT JOIN', '{{%ybwm_member}} as c', 'c.id = a.userId')
->join('LEFT JOIN', '{{%ybwm_table}} as d', 'd.id = a.tableId')
->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 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();
}
public function actionDelGoods(){
$result=axios_request();
$id=$result['id'];
$tableId=$result['tableId'];
if(!$id){
return $this->result(2,'无效的请求');
}
$item=2;
$reason=$result['reason'];
$num=intval($result['num']);
$goodsInfo=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where(['id'=>$id])
->one();
if($goodsInfo['num']<$num){
return $this->result(2,'退菜数量填写有误');
}
$goodsData = array(
'orderId' => $goodsInfo['orderId'],
'goodsId' => $goodsInfo['goodsId'],
'name' => $goodsInfo['name'],
'money' => $goodsInfo['money'],
'num' =>$num,
'data' => $goodsInfo['groupName'],
'icon' => $goodsInfo['icon'],
'storeId' => $goodsInfo['storeId'],
'uniacid' => $goodsInfo['uniacid'],
'createdAt' => time(),
'groupId' => $goodsInfo['groupId']?:0,
'material' => $goodsInfo['material'],
'attribute' => $goodsInfo['attribute'],
'item' =>$item,
'addType'=>3,
'reason'=>$reason
);
$transaction = Yii::$app->db->beginTransaction();
try{
$instoreData=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id'=>$goodsInfo['orderId']])
->one();
$delMoney=Order::refundGoodsMoney($id);
if(!$delMoney){
return false;
}
$delMoney=bcmul($delMoney,$num,2);
$money=0;$originMoney=0;
$money=bcsub($instoreData['money'],$delMoney,2);
$number=bcsub($instoreData['goodsNum'],$num);
$originMoney=bcsub($instoreData['originMoney'],$delMoney,2);
// if(intval($number)==intval($goodsInfo['num'])){
// echo json_encode(['code' => 2, 'msg' =>'退菜异常']);die;
// }
$bool=Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}',['goodsNum'=>$number,'money'=>$money,'originMoney'=>$originMoney],['id'=>$goodsInfo['orderId']])->execute();
if(!$bool){
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>'订单异常']);die;
}
if($goodsInfo['num']==$num){
$update=Yii::$app->db->createCommand()->delete('{{%ybwm_order_goods}}',['id'=>$id])->execute();
}else{
$update=Yii::$app->db->createCommand()->update('{{%ybwm_order_goods}}',['num'=>$goodsInfo['num']-$num],['id'=>$id])->execute();
}
if(!$update){
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>'退菜数量异常']);die;
}
$insert=YII::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $goodsData)->execute();
if(!$insert){
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>'退菜数量记录失败']);die;
}
$comboGoodsArr=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')->select('comboGoodsArr')->where(['id'=>$goodsInfo['goodsId']])->one();
if($comboGoodsArr = json_decode($comboGoodsArr['comboGoodsArr'],true)){
foreach ($comboGoodsArr as $j){
$orderGoods = (new \yii\db\Query())->from('{{%ybwm_order_goods}}')->where(['orderId' => $goodsInfo['orderId'],'goodsId' => $j['id'],'addType'=>2])->one();
if(!$orderGoods){
continue;
}
if($orderGoods['num']==($num*$j['num'])){
$update=Yii::$app->db->createCommand()->delete('{{%ybwm_order_goods}}',['id'=>$orderGoods['id']])->execute();
}else{
$update=Yii::$app->db->createCommand()->update('{{%ybwm_order_goods}}',['num'=>$orderGoods['num']-$j['num']],['id'=>$orderGoods['id']])->execute();
}
if(!$update){
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>'退菜数量异常']);die;
}
$goodsData = array(
'orderId' => $goodsInfo['orderId'],
'goodsId' => $j['id'],
'name' => $j['name'],
'money' => 0,
'num' =>$j['num'],
'data' => '',
'icon' => $j['icon'],
'storeId' => $j['storeId'],
'uniacid' => $goodsInfo['uniacid'],
'createdAt' => time(),
'groupId' => 0,
'material' => '',
'attribute' => '',
'item' =>$item,
'addType'=>3,
'reason'=>$reason
);
$insert=YII::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $goodsData)->execute();
if(!$insert){
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>'退菜数量记录失败']);die;
}
}
}
$transaction->commit();//提交事务会真正的执行数据库操作
}catch (Exception $e) {
$transaction->rollback();
echo json_encode(['code' => 2, 'msg' =>$e->getMessage()]);die;
}
Printing::returnFoodPrint($goodsInfo['orderId']);
return $this->result(1,'成功');
}
public function changePrice($orderId,$changeMoney,$type,$number=1){
$orderInfo=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id'=>$orderId])
->one();
if($type==1){
$num=bcadd($orderInfo['goodsNum'],$number);
$money=bcadd($orderInfo['money'],bcmul($changeMoney,$number,2),2);
$originMoney=bcadd($orderInfo['originMoney'],bcmul($changeMoney,$number,2),2);
}else{
$num=bcsub($orderInfo['goodsNum'],$number);
$money=bcsub($orderInfo['money'],bcmul($changeMoney,$number,2),2);
$originMoney=bcsub($orderInfo['originMoney'],bcmul($changeMoney,$number,2),2);
}
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['goodsNum'=>$num,'money'=>$money,'originMoney'=>$originMoney], ['id' =>$orderId])->execute();
}
}