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

954 lines
49 KiB
PHP

<?php
namespace app\controllers\channelApi;
use app\models\common\WeChatPay;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Config;
use app\models\common\Store;
use app\models\common\Order;
use app\models\common\CallbackOrder;
use app\models\common\Message;
use app\models\common\Power;
use app\models\common\Member;
use app\models\common\Printing;
use app\models\common\Good;
use app\models\common\WeChat;
use app\models\common\Voice;
class InStoreController extends CommonController{
//快餐下订单
public function actionSaveFastOrder() {
$request = Yii::$app->request;
$result=axios_request();
$item=3;
if($request->isGet){
return $this->result(2, '请求异常');
}
$now=time();
$userId=$this->wqData['userId'];
$uniacid=$this->wqData['uniacid'];
$appType = $this->wqData['appType'];
$user=(new \yii\db\Query())
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$userId])->one();
if ($user['isBlack'] == 1) {
return $this->result(2, '账号异常,请联系管理员!!!');
}
$moneyData = Order::getCarMoney($userId, $result['storeId'],3);
$originalMoney=$moneyData['originalMoney'];
$data['vipDiscount']=bcsub($originalMoney,$moneyData['goodsMoney'],2);//会员优惠金额
$data['originMoney'] = $moneyData['goodsMoney']; //商品金额
$data['people'] = $result['people']; //人数
$data['goodsNum'] = $moneyData['goodsNum']; //商品数量
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
$data['storeId'] = $result['storeId'];
$data['userId'] = $userId;
$data['state'] = 1;
$data['origin'] = $appType;
$data['isOut'] = $result['isOut']?:1;//1店内就餐2打包带走
if($data['isOut']==1){
$tablewareMoney=0;
}else{
$fastSet = Config::getStoreSet('fastSet', $result['storeId']);
$boxType=$fastSet['boxType']?:2;
if($boxType==1){
//按单
$tablewareMoney=$fastSet['boxMoney']?:0;
}else{
$tablewareMoney=$moneyData['boxMoney'];
}
}
$data['newMoney']=Order::newReduction($this->wqData['userId'],$result['storeId'])?:0;//新客
$data['tablewareMoney'] =$tablewareMoney; //餐具费
$data['money'] = bcadd($data['originMoney'], $data['tablewareMoney'], 2);
$reduce=Order::reduce($result['storeId'],$this->wqData['userId'],$data['originMoney'],3);//满减
$data['preferentialMoney']=$reduce['money']?:0;
$data['money']=bcsub($data['money'],$reduce['money'],2);//减去满减
if($data['newMoney']>0){
$data['money']=bcsub($data['money'],$data['newMoney'],2)?:0.01;
//echo "减去新客之后的钱".$moneyArr['goodsMoney'];
}
$data['platformCouponPreferential']=0;
$data['couponPreferential']=0;
if($result['couponId']){
$data['couponId']=json_encode($result['couponId']);
if($result['couponId']['store']){
$storeCoupon=(new \yii\db\Query())
->select('type,money,discount,goodsType,goodsArr')
->from('{{%ybwm_user_coupon}}')
->where('id=:id AND state=2',[':id'=>$result['couponId']['store']])->one();
if(!$storeCoupon){
return $this->result(2, '优惠券不存在或已使用');
}
if($storeCoupon['type']==1){
$data['couponPreferential']=$storeCoupon['money'];
$data['money']=bcsub($data['money'],$storeCoupon['money'],2)>0?bcsub($data['money'],$storeCoupon['money'],2):0.01;
}else{
$data['couponPreferential']=Member::couponMoney($userId,$result['storeId'],$result['couponId']['store'],3);
$data['money']=bcsub($data['money'],$data['couponPreferential'],2)>0?bcsub($data['money'],$data['couponPreferential'],2):0.01;
}
}
if($result['couponId']['platform']){
$platformCoupon=(new \yii\db\Query())
->select('type,money,discount,goodsType,goodsArr')
->from('{{%ybwm_user_coupon}}')
->where('id=:id AND state=2',[':id'=>$result['couponId']['platform']])->one();
if(!$platformCoupon){
return $this->result(2, '优惠券不存在或已使用');
}
if($platformCoupon['subsidy']){
$data['subsidy']=$platformCoupon['subsidy'];//补贴
}
if($platformCoupon['type']==1){
$data['platformCouponPreferential']=$platformCoupon['money'];
$data['money']=bcsub($data['money'],$platformCoupon['money'],2)>0?bcsub($data['money'],$platformCoupon['money'],2):0.01;
}else{
$data['platformCouponPreferential']=Member::couponMoney($userId,$result['storeId'],$result['couponId']['platform'],3);
$data['money']=bcsub($data['money'],$data['platformCouponPreferential'],2)>0?bcsub($data['money'],$data['platformCouponPreferential'],2):0.01;
}
}
}
$data['uniacid'] = $uniacid;
// $data['eatType'] = $result['eatType'] ?: 1;//1.餐前支付2餐后支付
$data['createdAt'] = $now;
$data['userNote'] = $result['userNote']; //备注
$data['orderMode'] = 2;
$transaction = Yii::$app->db->beginTransaction(); //开始事务
$res =Yii::$app->db->createCommand()->insert('{{%ybwm_instore_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if ($res) {
$arr = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where('userId=:userId AND storeId=:storeId AND item=:item', [':item'=>$item,':userId' => $userId, ':storeId' => $result['storeId']])->all();
for ($j = 0; $j < count($arr); $j++) {
$orderGood = [];
$discountOrderGood = [];
$discountNum=$arr[$j]['discountNum'];//活动商品
$originalNum=$arr[$j]['num']-$arr[$j]['discountNum'];//原价商品
if($discountNum){
$discountOrderGood['goodsId'] = $arr[$j]['goodsId'];
$discountOrderGood['attribute'] = $arr[$j]['attribute']?:'';
$discountOrderGood['data'] = $arr[$j]['groupName']?:'';
$discountOrderGood['material'] = $arr[$j]['materialName']?:'';
$discountOrderGood['orderId'] = $orderId;
$discountOrderGood['name'] = $arr[$j]['name'];
$discountOrderGood['num'] = $discountNum;
$discountOrderGood['icon'] = $arr[$j]['icon'];
$discountOrderGood['storeId'] = $result['storeId'];
$discountOrderGood['uniacid'] = $uniacid;
$discountOrderGood['createdAt'] = $now;
$discountOrderGood['item'] = $item;
$discountOrderGood['isActivity'] = $arr[$j]['discountType']?:0;
$discountOrderGood['money'] = $arr[$j]['discountPrice'];
$discountOrderGoodsRes=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $discountOrderGood)->execute();
if(!$discountOrderGoodsRes){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
}
if($originalNum){
$orderGood['goodsId'] = $arr[$j]['goodsId'];
$orderGood['attribute'] = $arr[$j]['attribute']?:'';
$orderGood['data'] = $arr[$j]['groupName']?:'';
$orderGood['material'] = $arr[$j]['materialName']?:'';
$orderGood['orderId'] = $orderId;
$orderGood['name'] = $arr[$j]['name'];
$orderGood['num'] = $originalNum;
$orderGood['icon'] = $arr[$j]['icon'];
$orderGood['storeId'] = $result['storeId'];
$orderGood['uniacid'] = $uniacid;
$orderGood['createdAt'] = $now;
$orderGood['item'] = $item;
$orderGood['money'] = $arr[$j]['money'];
$orderGoodsRes=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $orderGood)->execute();
if(!$orderGoodsRes){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
}
}
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',['item'=>$item,'userId'=>$this->wqData['userId'],'storeId'=>$result['storeId']])->execute();
if($result['couponId']){
if($result['couponId']['store']){
$useCoupon = Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['state'=>1,'useTime'=>$now], 'id=:id', ['id' =>$result['couponId']['store']])->execute();
if(!$useCoupon){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','优惠券修改失败');
}
}
if($result['couponId']['platform']){
$platformUseCoupon = Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['state'=>1,'useTime'=>$now], 'id=:id', ['id' =>$result['couponId']['platform']])->execute();
if(!$platformUseCoupon){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','优惠券修改失败');
}
}
}
WeChatPay::reporting($orderId,'CREATE_DEAL',3);
$transaction->commit();
return $this->result(1, '成功', $orderId);
} else {
return $this->result(2, '失败');
}
}
//我的快餐订单
public function actionMyFastOrder() {
$result=axios_request();
$page=$result['page']?:1;
$size=$result['size']?:10;
$state=$result['state'];
$table=(new \yii\db\Query())
->select(['a.id','a.money','a.callNo','a.goodsNum','a.isOut','a.payMode','a.createdAt','a.state','a.userNote','s.name as storeName','s.icon as storeIcon','s.storeTel','a.storeId'])
->from('{{%ybwm_instore_order}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.userDeleteAt=0 AND a.userId=:userId AND orderMode=2',[':uniacid'=>$this->wqData['uniacid'],':userId'=>$this->wqData['userId']]);
if($state==1){
$table->andWhere('a.state=3');
}
if($state==2){
$table->andWhere('a.state=4');
}
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->orderby('id desc')->all();
for($i=0;$i<count($res);$i++){
$goods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND item=3',[':orderId'=>$res[$i]['id']])->all();
$res[$i]['goodsArr']=$goods;
$res[$i]['isTk']=2;
if($res[$i]['state']==2){
$res[$i]['isTk']=1;
}
}
return $this->result(1,'成功',$res);
}
//快餐订单详情
public function actionFastOrderInfo() {
$result=axios_request();
$id=$result['id'];
$res=(new \yii\db\Query())
->select(['a.vipDiscount','a.id','a.money','a.preferentialMoney','a.platformCouponPreferential','a.couponPreferential','a.outTradeNo','a.payMode','a.tablewareMoney','a.callNo','a.goodsNum','a.isOut','a.payMode','a.createdAt','a.state','a.userNote','s.name as storeName','s.icon as storeIcon','s.storeTel','s.address as storeAddress','s.lat as storeLat','s.lng as storeLng','a.storeId'])
->from('{{%ybwm_instore_order}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.id=:id',[':id'=>$id])->one();
$goods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND item=3',[':orderId'=>$id])->all();
$res['goodsArr']=$goods;
return $this->result(1,'成功',$res);
}
//快餐取消订单//
public function actionRefundFast() {
$request = Yii::$app->request;
$result=axios_request();
if($request->isGet){
return $this->result(2, '请求异常');
}
$res = Order::fastCancel($result['orderId']);
if($res===true){
return $this->result(1,'成功');
}else{
return $this->result(2,'失败',$res);
}
}
//桌子详情
public function actionTableInfo() {
$result=axios_request();
$tableId=$result['tableId'];
$list=(new \yii\db\Query())
->select('a.id,a.manyType,a.userId,a.state,a.name,t.name as typeName,e.name as areaName,a.storeId')
->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();
$system = Config::getSystemSet('instoreSet', $this->wqData['uniacid']);
$storeSet = Config::getStoreSet('instoreSet', $list['storeId']);
$waitTime = $system['closeTime'] ?: 15;
$time = time() - 60 * $waitTime;
$clearTime = $storeSet['closeTime'] ?: 3;
$clearTime = time() - 60 * $clearTime;
$res =Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['state' =>4],'eatType=1 AND tableId=:tableId AND state=1 AND createdAt<:time AND orderMode=1', ['tableId'=>$tableId,'time'=>$time])->execute();
if ($res AND $system['autoClose']==1) {
Yii::$app->db->createCommand()->update('{{%ybwm_table}}', ['state' =>1,'userId'=>0],'id=:id', ['id'=>$tableId])->execute();
}
$clearOrder=(new \yii\db\Query())
->select('id,state,createdAt')
->from('{{%ybwm_instore_order}}')
->where('tableId=:tableId AND eatType=1',[':tableId'=>$tableId])->orderBy('id desc')->one();
if($clearOrder['state']==2 AND $clearOrder['createdAt']<$clearTime AND $storeSet['autoClose']==1){
Order::inStoreCheckOut($clearOrder['id']);
}
$systemDistance = $system['distance'] ?: 500;
// $sql = "SELECT a.manyType,a.userId,a.state,a.number,a.storeId,b.name as typeName FROM " . tablename('ybo2o_table') . " a" . " left join " . tablename("ybo2o_table_type") . " b on b.id=a.typeId where a.id=" . $_GPC['id'];
// $list = pdo_fetch($sql);
if (!$list) {
$this->result(1, '餐桌信息有误');
}
$store =(new \yii\db\Query())
->from('{{%ybwm_store}}')
->where('id=:id',[':id'=>$list['storeId']])->one();
if ($result['lat'] AND $system['distanceOpen'] == 1) {
$distance = getDistance($result['lat'], $result['lng'], $store['lat'], $store['lng']);
if ($distance * 100 > $systemDistance) {
return $this->result(2, '距离商家太远,无法下单');
}
}
$order =(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('tableId=:tableId AND state in (1,2)',[':tableId'=>$tableId])->orderBy('id desc')->one();
$instoreSet = Config::getStoreSet('instoreSet', $list['storeId']);
if($instoreSet['orderMode']==2){
$order=false;
}
if($order){
$goods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND item=2 AND addType=2',[':orderId'=>$order['id']])->all();
$order['goodsArr']=$goods;
$order['storeSet'] = Config::getStoreSet('instoreSet', $res['storeId']);
$addGoodsArr=[];
$addNum=(new \yii\db\Query())
->select('addNum')
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND addType=1 AND item=2',[':orderId'=>$order['id']])->orderBy('addNum desc')->one();
if($addNum){
for($i=1;$i<=$addNum['addNum'];$i++){
$addGoods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND addNum=:addNum AND addType=1 AND item=2',[':orderId'=>$order['id'],':addNum'=>$i])->orderBy('id desc')->all();
$addGoodsArr[] = $addGoods;
}
}
$order['addGoods'] = $addGoodsArr;
}
$list['orderInfo'] = $order;
if (!$list['userId']) {
Yii::$app->db->createCommand()->update('{{%ybwm_table}}', ['state' =>2,'userId'=>$this->wqData['userId']],'id=:id', ['id'=>$tableId])->execute();
}
return $this->result(1, '成功', $list);
}
//堂食下订单
public function actionSaveInStoreOrder() {
$request = Yii::$app->request;
$result=axios_request();
//test数据 start
$result['tableId'] = 21;
$result['storeId'] = 14;
$result['people'] = 1;
$result['userNote'] = '微辣!微辣!微辣!';
$result['people'] = 1;
$result['people'] = 1;
//test数据 end
$item=2;
// if($request->isGet){
// return $this->result(2, '请求异常');
// }
$tableTypeInfo=(new \yii\db\Query())
->from('{{%ybwm_table}} a')
->leftJoin('{{%ybwm_table_type}} b','a.typeId=b.id')
->where(['a.id'=>$result['tableId']])->one();
if($result['people']>$tableTypeInfo['maxNum']){
return $this->result(2, '超过桌台类型人数限制');
}
$tableTypeInfo=(new \yii\db\Query())
->from('{{%ybwm_table}} a')
->leftJoin('{{%ybwm_table_type}} b','a.typeId=b.id')
->where(['a.id'=>$result['tableId']])->one();
if($result['people']>$tableTypeInfo['maxNum']){
return $this->result(2, '超过桌台类型人数限制');
}
$now=time();
$userId=$this->wqData['userId'];
$uniacid=$this->wqData['uniacid'];
$appType=$this->wqData['appType'];
$user=(new \yii\db\Query())
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$userId])->one();
if ($user['isBlack'] == 1) {
return $this->result(2, '账号异常,请联系管理员!!!');
}
$storeSet = Config::getStoreSet('instoreSet', $result['storeId']);
$moneyData = Order::getCarMoney($userId, $result['storeId'],2,$result['tableId']);
$originalMoney=$moneyData['originalMoney'];
$data['vipDiscount']=bcsub($originalMoney,$moneyData['goodsMoney'],2);//会员优惠金额
$data['originMoney'] = $moneyData['goodsMoney']; //商品金额
$data['people'] = $result['people']; //人数
$data['goodsNum'] = $moneyData['goodsNum']; //商品数量
$data['outTradeNo'] = date("YmdHis") . rand(111111, 999999);
$data['storeId'] = $result['storeId'];
$data['userId'] = $userId;
$data['state'] = 1;
$data['origin'] = $appType;
if($storeSet['payMode']==2 AND $storeSet['receiving']==1){
$data['state'] = 2;
$data['receiptAt']=time();
}
if($storeSet['serviceType']==2){//按桌台收取
$data['tablewareMoney'] = $storeSet['serviceMoney']; //桌台费
$data['tablewareNum']=1;
}else{
//按人数收取
$data['tablewareNum']=$result['people'];
$data['tablewareMoney'] = bcmul($storeSet['serviceMoney'], $result['people'], 2); //桌台费
}
$data['money'] = bcadd($data['originMoney'], $data['tablewareMoney'], 2);
$reduce=Order::reduce($result['storeId'],$this->wqData['userId'],$data['originMoney'],2);//满减
$data['preferentialMoney']=$reduce['money']?:0;
$data['money']=0.01;//bcsub($data['money'],$reduce['money'],2);//减去满减
$data['platformCouponPreferential']=0;
$data['couponPreferential']=0;
if($result['couponId']){
$data['couponId']=json_encode($result['couponId']);
if($result['couponId']['store']){
$storeCoupon=(new \yii\db\Query())
->select('type,money,discount,goodsType,goodsArr')
->from('{{%ybwm_user_coupon}}')
->where('id=:id AND state=2',[':id'=>$result['couponId']['store']])->one();
if(!$storeCoupon){
return $this->result(2, '优惠券不存在或已使用');
}
if($storeCoupon['type']==1){
$data['couponPreferential']=$storeCoupon['money'];
$data['money']=bcsub($data['money'],$storeCoupon['money'],2)>0?bcsub($data['money'],$storeCoupon['money'],2):0.01;
}else{
$data['couponPreferential']=Member::couponMoney($userId,$result['storeId'],$result['couponId']['store'],2,$result['tableId']);
$data['money']=bcsub($data['money'],$data['couponPreferential'],2)>0?bcsub($data['money'],$data['couponPreferential'],2):0.01;
}
}
if($result['couponId']['platform']){
$platformCoupon=(new \yii\db\Query())
->select('type,money,discount,goodsType,goodsArr')
->from('{{%ybwm_user_coupon}}')
->where('id=:id AND state=2',[':id'=>$result['couponId']['platform']])->one();
if(!$platformCoupon){
return $this->result(2, '优惠券不存在或已使用');
}
if($platformCoupon['subsidy']){
$data['subsidy']=$platformCoupon['subsidy'];//补贴
}
if($platformCoupon['type']==1){
$data['platformCouponPreferential']=$platformCoupon['money'];
$data['money']=bcsub($data['money'],$platformCoupon['money'],2)>0?bcsub($data['money'],$platformCoupon['money'],2):0.01;
}else{
$data['platformCouponPreferential']=Member::couponMoney($userId,$result['storeId'],$result['couponId']['platform'],2,$result['tableId']);
$data['money']=bcsub($data['money'],$data['platformCouponPreferential'],2)>0?bcsub($data['money'],$data['platformCouponPreferential'],2):0.01;
}
}
}
$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'=>$result['tableId']])->one();
$data['uniacid'] = $uniacid;
$data['eatType'] = $result['eatType'] ?: 1;
$data['createdAt'] = time();
$data['regionName'] = $tableInfo['areaName']; //桌子区域类型
$data['typeName'] = $tableInfo['typeName'] . "(" . $tableInfo['minNum'] . "-" . $tableInfo['maxNum'] . ")"; //桌子分类名称
$data['number'] = $tableInfo['name']; //餐桌编号
$data['userNote'] = $result['userNote']; //备注
$data['tableId'] = $result['tableId']; //桌子id
$transaction = Yii::$app->db->beginTransaction(); //开始事务
$res =Yii::$app->db->createCommand()->insert('{{%ybwm_instore_order}}', $data)->execute();
$orderId = Yii::$app->db->getLastInsertID();
if ($res) {
if($storeSet['orderMode']==2){
$arr = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where('userId=:userId AND tableId=:tableId AND storeId=:storeId AND item=:item', [':userId'=>$userId,':item'=>$item,':tableId' => $result['tableId'], ':storeId' => $result['storeId']])->all();
}else{
$arr = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where('tableId=:tableId AND storeId=:storeId AND item=:item', [':item'=>$item,':tableId' => $result['tableId'], ':storeId' => $result['storeId']])->all();
}
// if(!$arr){
// $transaction->rollBack();//事务回滚
// return $this->result(2, '下单失败','购物车商品异常');
// }
$arr = [
[
'discountNum' => 1,
'num' => 20,
'goodsId' => 115,
'groupId' => 0,
'money' => 0.01,
'groupName' => '家常炒菜1',
'attribute' => '',
'materialName' => '',
'name' => '家常炒菜',
'icon' => 'http://s883i38834.vicp.fun/web/static/yb_wm/8/2024/03/15/202403151651341480.png',
'discountType' => 0.00,
'discountPrice' => 0.00,
],
];
for ($j = 0; $j < count($arr); $j++) {
$orderGood = [];
$discountOrderGood = [];
$discountNum=$arr[$j]['discountNum'];//活动商品数量
$originalNum=$arr[$j]['num']-$arr[$j]['discountNum'];//原价商品数量
if($discountNum){
$discountOrderGood['goodsId'] = $arr[$j]['goodsId'];
$discountOrderGood['attribute'] = $arr[$j]['attribute']?:'';
$discountOrderGood['data'] = $arr[$j]['groupName']?:'';
$discountOrderGood['material'] = $arr[$j]['materialName']?:'';
$discountOrderGood['orderId'] = $orderId;
$discountOrderGood['name'] = $arr[$j]['name'];
$discountOrderGood['num'] = $discountNum;
$discountOrderGood['icon'] = $arr[$j]['icon'];
$discountOrderGood['storeId'] = $result['storeId'];
$discountOrderGood['uniacid'] = $uniacid;
$discountOrderGood['createdAt'] = $now;
$discountOrderGood['item'] = $item;
$discountOrderGood['isActivity'] = $arr[$j]['discountType']?:0;
$discountOrderGood['money'] = $arr[$j]['discountPrice'];
$discountOrderGoodsRes=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $discountOrderGood)->execute();
if(!$discountOrderGoodsRes){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
$comboGoodsArr=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')->select('comboGoodsArr')->where(['id'=>$arr[$j]['goodsId']])->one();
if($comboGoodsArr = json_decode($comboGoodsArr['comboGoodsArr'],true)){
foreach ($comboGoodsArr as $v){
$discountOrderGoodOne['groupId'] = 0;
$discountOrderGoodOne['goodsId'] = $v['id'];
$discountOrderGoodOne['attribute'] = '';
$discountOrderGoodOne['data'] = '';
$discountOrderGoodOne['material'] = '';
$discountOrderGoodOne['orderId'] = $orderId;
$discountOrderGoodOne['name'] = $v['name'];
$discountOrderGoodOne['num'] = $v['num'];
$discountOrderGoodOne['icon'] = $v['icon'];
$discountOrderGoodOne['storeId'] = $v['storeId'];
$discountOrderGoodOne['uniacid'] = $uniacid;
$discountOrderGoodOne['createdAt'] = $now;
$discountOrderGoodOne['item'] = $item;
$discountOrderGoodOne['isActivity'] = 0;
$discountOrderGoodOne['money'] = 0;
$discountOrderGoodsResOne=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $discountOrderGoodOne)->execute();
if(!$discountOrderGoodsResOne){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
}
}
}
if($originalNum){
$orderGood['goodsId'] = $arr[$j]['goodsId'];
$orderGood['attribute'] = $arr[$j]['attribute']?:'';
$orderGood['data'] = $arr[$j]['groupName']?:'';
$orderGood['material'] = $arr[$j]['materialName']?:'';
$orderGood['orderId'] = $orderId;
$orderGood['name'] = $arr[$j]['name'];
$orderGood['num'] = $originalNum;
$orderGood['icon'] = $arr[$j]['icon'];
$orderGood['storeId'] = $result['storeId'];
$orderGood['uniacid'] = $uniacid;
$orderGood['createdAt'] = $now;
$orderGood['item'] = $item;
$orderGood['money'] = $arr[$j]['money'];
$orderGoodsRes=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $orderGood)->execute();
if(!$orderGoodsRes){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
$comboGoodsArr=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')->select('comboGoodsArr')->where(['id'=>$arr[$j]['goodsId']])->one();
if($comboGoodsArr = json_decode($comboGoodsArr['comboGoodsArr'],true)){
foreach ($comboGoodsArr as $v){
$discountOrderGoodOne['groupId'] = 0;
$discountOrderGoodOne['goodsId'] = $v['id'];
$discountOrderGoodOne['attribute'] = '';
$discountOrderGoodOne['data'] = '';
$discountOrderGoodOne['material'] = '';
$discountOrderGoodOne['orderId'] = $orderId;
$discountOrderGoodOne['name'] = $v['name'];
$discountOrderGoodOne['num'] = $v['num'];
$discountOrderGoodOne['icon'] = $v['icon'];
$discountOrderGoodOne['storeId'] = $v['storeId'];
$discountOrderGoodOne['uniacid'] = $uniacid;
$discountOrderGoodOne['createdAt'] = $now;
$discountOrderGoodOne['item'] = $item;
$discountOrderGoodOne['isActivity'] = 0;
$discountOrderGoodOne['money'] = 0;
$discountOrderGoodsResOne=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $discountOrderGoodOne)->execute();
if(!$discountOrderGoodsResOne){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','订单商品表插入失败');
}
}
}
}
}
if($storeSet['orderMode']==2){
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',['item'=>$item,'tableId'=>$result['tableId'],'userId'=>$this->wqData['userId'],'storeId'=>$result['storeId']])->execute();
}else{
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',['item'=>$item,'tableId'=>$result['tableId'],'storeId'=>$result['storeId']])->execute();
}
if($data['eatType'] == 2){
Order::reduceStock($orderId,1,2);
Order::instoreVoice($orderId, 'inNewOrder');
Message::newInStoreOrder($orderId);
}
if ($data['eatType'] == 2 AND $data['state']==2) {
Printing::orderPrint($orderId,2);
Message::inStoreOrder($orderId);
Yii::$app->db->createCommand()->update('{{%ybwm_table}}',['state'=>3],['id'=>$result['tableId']])->execute();
}
if($result['couponId']){
if($result['couponId']['store']){
$useCoupon = Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['state'=>1,'useTime'=>$now], 'id=:id', ['id' =>$result['couponId']['store']])->execute();
if(!$useCoupon){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','优惠券修改失败');
}
}
if($result['couponId']['platform']){
$platformUseCoupon = Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['state'=>1,'useTime'=>$now], 'id=:id', ['id' =>$result['couponId']['platform']])->execute();
if(!$platformUseCoupon){
$transaction->rollBack();//事务回滚
return $this->result(2, '下单失败','优惠券修改失败');
}
}
}
WeChatPay::reporting($orderId,'CREATE_DEAL',3);
$transaction->commit();
return $this->result(1, '成功', $orderId);
} else {
return $this->result(2, '失败');
}
}
//我的店内订单
public function actionMyInStoreOrder() {
$result=axios_request();
$page=$result['page']?:1;
$size=$result['size']?:10;
$state=$result['state'];
$table=(new \yii\db\Query())
->select(['a.id','a.eatType','a.money','a.people','a.typeName','a.number','a.callNo','a.goodsNum','a.isOut','a.payMode','a.createdAt','a.state','a.userNote','s.name as storeName','s.icon as storeIcon','s.storeTel','a.storeId'])
->from('{{%ybwm_instore_order}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.userDeleteAt=0 AND a.userId=:userId AND a.orderMode=1',[':uniacid'=>$this->wqData['uniacid'],':userId'=>$this->wqData['userId']]);
if($state==1){
$table->andWhere('a.state=1');
}
if($state==2){
$table->andWhere('a.state=3');
}
if($state==3){
$table->andWhere('a.state=4');
}
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->orderby('id desc')->all();
for($i=0;$i<count($res);$i++){
$goods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND item=2',[':orderId'=>$res[$i]['id']])->all();
$res[$i]['goodsArr']=$goods;
$res[$i]['isTk']=2;
if($res[$i]['state']==2){
$res[$i]['isTk']=1;
}
}
return $this->result(1,'成功',$res);
}
//订单详情
public function actionInStoreOrderInfo() {
$result=axios_request();
$id=$result['id'];
$res=(new \yii\db\Query())
->select(['a.vipDiscount','a.id','a.preferentialMoney','a.platformCouponPreferential','a.couponPreferential','a.money','a.eatType','a.tableId','a.typeName','a.number','a.people','a.outTradeNo','a.payMode','a.tablewareMoney','a.callNo','a.goodsNum','a.isOut','a.payMode','a.createdAt','a.state','a.userNote','s.name as storeName','s.icon as storeIcon','s.storeTel','a.storeId'])
->from('{{%ybwm_instore_order}} as a')
->join('LEFT JOIN', '{{%ybwm_store}} as s', 's.id = a.storeId')
->where('a.id=:id',[':id'=>$id])->one();
$goods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND item=2 AND addType=2',[':orderId'=>$id])->all();
$res['goodsArr']=$goods;
$res['storeSet'] = Config::getStoreSet('instoreSet', $res['storeId']);
$addGoodsArr=[];
$addNum=(new \yii\db\Query())
->select('addNum')
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND addType=1 AND item=2',[':orderId'=>$id])->orderBy('addNum desc')->one();
if($addNum){
for($i=1;$i<=$addNum['addNum'];$i++){
$addGoods=(new \yii\db\Query())
->from('{{%ybwm_order_goods}}')
->where('orderId=:orderId AND addNum=:addNum AND addType=1 AND item=2',[':orderId'=>$id,':addNum'=>$i])->orderBy('id desc')->all();
$addGoodsArr[] = $addGoods;
}
}
$res['addGoods'] = $addGoodsArr;
$system = Config::getStoreSet('instoreSet',$res['storeId']);
$res['onLine']=$system['onLine'];
return $this->result(1,'成功',$res);
}
//获取活码详细
public function actionGetCodeInfo() {
$result=axios_request();
$data=$result['data'];
if($data['type']=='fastOrder'){
//快餐
return $this->result(1,'成功',['type'=>$data['type'],'storeId'=>$data['id']]);
}
if($data['type']=='cashier'){
//收银
return $this->result(1,'成功',['type'=>$data['type'],'storeId'=>$data['id']]);
}
if($data['type']=='goods'){
//点餐页
return $this->result(1,'成功',['type'=>$data['type'],'storeId'=>$data['id']]);
}
//桌子
$code=(new \yii\db\Query())
->select('data')
->from('{{%ybwm_code}}')
->where('id=:id',[':id'=>$data['id']])->one();
if(!$code['data']){
return $this->result(2,'该码未绑定参数');
}
return $this->result(1,'成功',json_decode($code['data'],true));
}
//判断购物车必选品
public function actionShopCarRequire() {
$result=axios_request();
$item=$result['item'];
if($item==1){
$goodsType=1;
}else{
$goodsType=2;
}
$tableId=$result['tableId']?:0;
$userId=$this->wqData['userId'];
$typeId=Good::getGoodSData($this->wqData['uniacid'],$result['storeId'],$this->wqData['userId'],$goodsType,1);
$requireId=array_column($typeId,'id');
$storeModeSet = Config::getStoreSet($result['storeId'], $this->wqData['uniacid']);
$multiplayerMode = $storeModeSet['orderMode'];
for($i=0;$i<count($requireId);$i++){
if($multiplayerMode==2){
$res=(new \yii\db\Query())
->from('{{%ybwm_shop_car}} as a')
->join('LEFT JOIN', '{{%ybwm_core_goods}} as s', 's.id = a.goodsId')
->where(['a.tableId'=>$tableId,'a.item'=>$item,'s.typePid'=>$requireId[$i],'a.userId' => $userId,'a.storeId' => $result['storeId']])
->one();
}else{
$res=(new \yii\db\Query())
->from('{{%ybwm_shop_car}} as a')
->join('LEFT JOIN', '{{%ybwm_core_goods}} as s', 's.id = a.goodsId')
->where(['a.tableId'=>$tableId,'a.item'=>$item,'s.typePid'=>$requireId[$i],'a.storeId' => $result['storeId']])
->one();
}
if(!$res){
return $this->result(2,'请添加必选品');
}
}
return $this->result(1,'成功');
}
//我的购物车
public function actionMyInStoreCar() {
$result=axios_request();
$tableId=$result['tableId']?:0;
$uniacid=$this->wqData['uniacid'];
$userId=$this->wqData['userId'];
$time = time() - 60 * 60 * 2;
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}', 'tableId=:tableId AND storeId=:storeId AND item=2 AND createdAt<:createdAt',[':tableId' => $tableId, ':storeId' => $result['storeId'], ':createdAt' => $time])->execute();
$storeModeSet = Config::getStoreSet($result['storeId'], $uniacid);
$multiplayerMode = $storeModeSet['orderMode'];
if ($multiplayerMode == 2) {
//拼桌模式
$userList=(new \yii\db\Query())
->select('userId')
->from('{{%ybwm_shop_car}}')
->where(['tableId' => $tableId,'userId'=>$userId, 'storeId' => $result['storeId'], 'item' =>2])
->groupBy('userId')
->all();
} else {
$userList=(new \yii\db\Query())
->select('userId')
->from('{{%ybwm_shop_car}}')
->where(['tableId' => $tableId,'storeId' => $result['storeId'], 'item' =>2])
->groupBy('userId')
->all();
}
$table=(new \yii\db\Query())
->select('a.name,t.name as typeName,a.userId,a.state,a.storeId')
->from('{{%ybwm_table}} as a')
->join('LEFT JOIN', '{{%ybwm_table_type}} as t', 't.id = a.typeId')
->where(['a.id'=>$tableId])
->one();
if ($table['userId'] == 0) {
Yii::$app->db->createCommand()->delete('{{%ybwm_table}}', ['state' => 2, 'userId' => $userId],['id' => $tableId])->execute();
}
$data['table'] = $table;
$data['money'] = 0;
for ($u = 0; $u < count($userList); $u++) {
$data2 = [];
$user=(new \yii\db\Query())
->select('userName,portrait')
->from('{{%ybwm_member}}')
->where(['id'=>$userList[$u]['userId']])
->one();
$data2['userName'] = $user['userName'];
$data2['portrait'] = $user['portrait'];
$list =(new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where(['userId' => $userList[$u]['userId'], 'tableId' => $tableId, 'storeId' => $result['storeId'], 'item' => 2])
->all();
for ($i = 0; $i < count($list); $i++) {
$data['money'] = (float) bcadd(bcmul($list[$i]['money'], $list[$i]['num'], 2), $data['money'], 2);
// $goodsArr['name'] = $list[$i]['name'];
// $goodsArr['goodsId'] = $list[$i]['goodsId'];
// $goodsArr['groupId'] = $list[$i]['groupId'];
// $goodsArr['num'] = $list[$i]['num'];
// $goodsArr['price'] = $list[$i]['money'];
// $goodsArr['icon'] = $list[$i]['icon'];
// $goodsArr['groupName'] = $list[$i]['groupName'];
// $goodsArr['material'] = $list[$i]['material'];
// $goodsArr['attribute'] = $list[$i]['attribute'];
// $goodsArr['isSpec'] = $list[$i]['goodsId'] ? 1 : 2;
// $goodsArr['id'] = $list[$i]['id'];
$data2['goodsArr'][] = $list[$i];
}
$data['userInfo'][] = $data2;
}
return $this->result(1,'成功',$data);
}
//关闭堂食订单
public function actionCloseInOrder() {
$result=axios_request();
$order = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id' => $result['orderId']])
->one();
if ($order['state'] != 1) {
return $this->result(2, '订单状态异常');
}
$res =Order::inStoreClose($result['orderId']);
//$res =Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}',['state'=>4], ['id' => $result['orderId']])->execute();
if ($res) {
return $this->result(1, '成功');
} else {
return $this->result(2, '失败');
}
}
//加菜
public function actionAddFood(){
$request = Yii::$app->request;
$result=axios_request();
$userId=$this->wqData['userId'];
$uniacid=$this->wqData['uniacid'];
$now=time();
if($request->isGet){
return $this->result(2, '请求异常');
}
$order =(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where(['id' => $result['orderId']])
->one();
$orderGoods =(new \yii\db\Query())
->select('addNum')
->from('{{%ybwm_order_goods}}')
->where(['orderId' => $result['orderId'],'addType'=>1,'item'=>2])
->orderBy('addNum desc')
->one();
$addNum=$orderGoods['addNum']?:0;
if($order['state'] > 2) {
return $this->result(2, '订单状态异常');
}
if($order['eatType'] == 1) {
return $this->result(2, '餐前支付不允许加菜');
}
$moneyData = Order::getCarMoney($userId, $order['storeId'],2,$order['tableId']);
$arr = (new \yii\db\Query())
->from('{{%ybwm_shop_car}}')
->where('tableId=:tableId AND userId=:userId AND storeId=:storeId AND item=:item', [':tableId'=>$order['tableId'],':item'=>2,':userId' => $userId, ':storeId' => $order['storeId']])->all();
// var_dump($arr);die;
$goodsIdArr = [];
for ($j = 0; $j < count($arr); $j++) {
$orderGood = [];
$orderGood['goodsId'] = $arr[$j]['goodsId'];
$orderGood['attribute'] = $arr[$j]['attribute']?:'';
$orderGood['data'] = $arr[$j]['groupName']?:'';
$orderGood['material'] = $arr[$j]['materialName']?:'';
$orderGood['orderId'] = $result['orderId'];
$orderGood['name'] = $arr[$j]['name'];
$orderGood['num'] = $arr[$j]['num'];
$orderGood['icon'] = $arr[$j]['icon'];
$orderGood['storeId'] = $order['storeId'];
$orderGood['uniacid'] = $uniacid;
$orderGood['item'] = 2;
$orderGood['createdAt'] = $now;
$orderGood['money'] = $arr[$j]['money'];
$orderGood['addType'] = 1;
$orderGood['addNum'] = $addNum+1;
Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $orderGood)->execute();
$goodsIdArr[]= Yii::$app->db->getLastInsertID();
$comboGoodsArr=(new \yii\db\Query())->from('{{%ybwm_core_goods}}')->select('comboGoodsArr')->where(['id'=>$arr[$j]['goodsId']])->one();
if($comboGoodsArr = json_decode($comboGoodsArr['comboGoodsArr'],true)){
foreach ($comboGoodsArr as $v){
$discountOrderGoodOne['groupId'] = 0;
$discountOrderGoodOne['goodsId'] = $v['id'];
$discountOrderGoodOne['attribute'] = '';
$discountOrderGoodOne['data'] = '';
$discountOrderGoodOne['material'] = '';
$discountOrderGoodOne['orderId'] = $result['orderId'];
$discountOrderGoodOne['name'] = $v['name'];
$discountOrderGoodOne['num'] = $v['num'];
$discountOrderGoodOne['icon'] = $v['icon'];
$discountOrderGoodOne['storeId'] = $v['storeId'];
$discountOrderGoodOne['uniacid'] = $uniacid;
$discountOrderGoodOne['createdAt'] = $now;
$discountOrderGoodOne['item'] = 2;
$discountOrderGoodOne['isActivity'] = 0;
$discountOrderGoodOne['money'] = 0;
$discountOrderGoodsResOne=Yii::$app->db->createCommand()->insert('{{%ybwm_order_goods}}', $discountOrderGoodOne)->execute();
if(!$discountOrderGoodsResOne){
return $this->result(2, '下单失败','订单商品表插入失败');
}
$goodsIdArr[]= Yii::$app->db->getLastInsertID();
}
}
}
Printing::instoreOrderPrint($order['id'],$support=2,$goodsIdArr);
$storeSet = Config::getStoreSet('instoreSet', $order['storeId']);
if($storeSet['orderMode']==2){
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',['item'=>2,'tableId'=>$order['tableId'],'userId'=>$this->wqData['userId'],'storeId'=>$order['storeId']])->execute();
}else{
Yii::$app->db->createCommand()->delete('{{%ybwm_shop_car}}',['item'=>2,'tableId'=>$order['tableId'],'storeId'=>$order['storeId']])->execute();
}
Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}',['money'=>new \yii\db\Expression('money +'.$moneyData['goodsMoney']),'originMoney'=>new \yii\db\Expression('originMoney +'.$moneyData['goodsMoney']),'goodsNum'=>new \yii\db\Expression('goodsNum +'.$moneyData['goodsNum'])],['id'=>$result['orderId']])->execute();
// pdo_update('ybo2o_instore_order', array('money +=' => $moneyData['goodsMoney'], 'goodsNum +=' => $moneyData['goodsNum']), array('id' => $result['orderId']));
return $this->result(1, '成功', $result['orderId']);
}
}