from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND payMode in (1,2,5) AND type=2 AND entryAt<=:entryAt AND (payType=1 || payPort=2) ',['entryAt'=>time(),':uniacid'=>$uniacid,'storeId'=>$storeId]); if($startTime&&$endTime){ $orderQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); } $orderMoney=$orderQuery->sum('storeActualMoney');//计算已入账金额 $offlineOrderQuery=(new \yii\db\Query()) ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND payMode in (6,7,8,9,10) AND type=2 AND entryAt<=:entryAt AND (payType=1 || payPort=2) ',['entryAt'=>time(),':uniacid'=>$uniacid,'storeId'=>$storeId]); if($startTime&&$endTime){ $offlineOrderQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); } $offlineOrderMoney=$offlineOrderQuery->sum('money-storeActualMoney');//计算已入账金额线下扣佣金 $orderMoney=bcsub($orderMoney,$offlineOrderMoney,2); $financeQuery=(new \yii\db\Query()) ->from('{{%ybwm_store_finance}}') ->where('uniacid=:uniacid AND storeId=:storeId',[':uniacid'=>$uniacid,'storeId'=>$storeId]) ->andWhere(['in','state',[1,2]]); if($startTime&&$endTime){ $financeQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); } $financeMoney=$financeQuery->sum('money');//已提现金额 $waitQuery=(new \yii\db\Query()) ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND (type=1 || entryAt>=:entryAt) AND (payType=1 || payPort=2)',['entryAt'=>time(),':uniacid'=>$uniacid,'storeId'=>$storeId]); if($startTime&&$endTime){ $waitQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); } $waitQuery=$waitQuery->sum('storeActualMoney');//等待入账金额 // $frozenQuery=(new \yii\db\Query()) // ->from('{{%ybwm_bill}}') // ->where('uniacid=:uniacid AND storeId=:storeId AND type=1',[':uniacid'=>$uniacid,'storeId'=>$storeId]); // if($startTime&&$endTime){ // $frozenQuery->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); // } // $frozenQuery=$frozenQuery->sum('storeActualMoney');//冻结金额 $money=bcsub($orderMoney,$financeMoney,2)?:0.00; $allMoney=bcadd($money,$waitQuery,2)?:0.00; $data=array( 'orderMoney'=>$orderMoney?:0.00,//计算已入账金额 'financeMoney'=>$financeMoney?:0.00,//已提现金额 'money'=>$money,//可提现金额 'allMoney'=>$allMoney,//门店总金额 'waitMoney'=>$waitQuery?:0.00,//等待入账 'cumulative'=>bcadd($financeMoney,$allMoney,2)?:0.00,//累计 //'frozenMoney'=>$frozenQuery?:0.00//冻结金额 ); return $data?:[]; } //获取商家商品数量 public static function getGoodNum($uniacid,$storeId){ $storeInfo=(new \yii\db\Query()) ->from('{{%ybwm_store}}') ->where(['uniacid'=>$uniacid,'id'=>$storeId]) ->one(); if($storeInfo['isMain']==1||$storeInfo['goodsModel']==3){ $goodNum=(new \yii\db\Query()) ->from('{{%ybwm_core_goods}}') ->where('uniacid=:uniacid AND storeId=:storeId AND display=1 AND deleteAt=0',[':uniacid'=>$uniacid,'storeId'=>$storeId]) ->count(); }else{ $goodNum=(new \yii\db\Query()) ->from('{{%ybwm_store_goods}} a') ->leftJoin('{{%ybwm_core_goods}} b','a.goodsId=b.id') ->where('a.uniacid=:uniacid AND a.storeId=:storeId AND a.display=1 AND b.deleteAt=0',[':uniacid'=>$uniacid,'storeId'=>$storeId]) ->count(); } return $goodNum?:0; } //查看待结算账单 public static function waitMoney($uniacid,$storeId){ $bill=(new \yii\db\Query()) //->select(new Expression("from_unixtime(a.statisticsAt,'%Y-%m-%d')day,sum(a.storeActualMoney)money,from_unixtime(a.entryAt,'%Y-%m-%d')settlement")) // ->addSelect(new Expression("select sum(b.refundMoney) from {{%ybwm_bill}} b where b.uniacid=".$uniacid." and b.storeId=".$storeId." AND from_unixtime(b.refundAt,'%Y-%m-%d')=from_unixtime(a.statisticsAt,'%Y-%m-%d')) as refundMoney")) ->select(new Expression("from_unixtime(statisticsAt,'%Y-%m-%d')day,from_unixtime(entryAt,'%Y-%m-%d')settlement")) ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND (entryAt>:entryAt || type=1) AND (refundMoney=0 or refundMoney is null)',['entryAt'=>time(),':uniacid'=>$uniacid,'storeId'=>$storeId]) ->groupBy(new Expression("from_unixtime(statisticsAt,'%Y-%m-%d')")) ->orderBy('statisticsAt desc') ->all(); // print_R($bill);die; for($i=0;$ifrom('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND refundAt>=:startTime AND refundAt<=:endTime', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->sum('refundMoney')?:0; $money=(new \yii\db\Query()) ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND statisticsAt>=:startTime AND statisticsAt<=:endTime', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->sum('storeActualMoney')?:0; $bill[$i]['refundMoney']=$refund; $bill[$i]['money']=$money; $bill[$i]['state']=2; } return $bill; } //查看所有账单 public static function billList($uniacid,$storeId,$page,$size,$startTime,$endTime){ $page=$page?:1; $num=($page-1)*20; $table=(new \yii\db\Query()) ->select(['from_unixtime(statisticsAt,"%Y-%m-%d")day','count(id) as count','ifnull(sum(money),0) as money','ifnull(sum(storeActualMoney),0) as storeActualMoney', 'ifnull(sum(money-storeActualMoney),0) as serviceMoney']) ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND (refundAt=0 || refundAt is null)',[':uniacid'=>$uniacid]); if($storeId){ $table->andWhere('storeId=:storeId',[':storeId'=>$storeId]); } if($startTime){ $table->andWhere('createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]); } // var_dump(ddSql($table));die; $data['count']=(clone $table)->groupBy(new Expression("from_unixtime(statisticsAt,'%Y-%m-%d')"))->count(); $data['list']=(clone $table)->groupBy(new Expression("from_unixtime(statisticsAt,'%Y-%m-%d')"))->offset($num) ->limit($size)->orderBy('statisticsAt desc')->all(); return $data; } //账单详情 public static function billInfo($uniacid,$storeId,$startTime=null,$endTime=null){ $turnoverTable=(new \yii\db\Query()) ->select('sum(b.boxMoney+b.deliveryMoney+b.originMoney) allMoney') ->from('{{%ybwm_bill}} as a') ->join('LEFT JOIN', '{{%ybwm_takeout_order}} as b', 'b.outTradeNo = a.outTradeNo'); $turnover=$turnoverTable ->where('(a.refundMoney=0 or a.refundMoney is null) AND a.uniacid=:uniacid AND a.storeId=:storeId AND a.statisticsAt>=:startTime AND a.statisticsAt<=:endTime AND a.origin=1', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one();//总营业额 $data['turnover']=$turnover['allMoney']?:'0.00';//总营业额 $data['balanceTurnover']=$turnoverTable ->where('(a.refundMoney=0 or a.refundMoney is null) AND a.uniacid=:uniacid AND a.storeId=:storeId AND a.statisticsAt>=:startTime AND a.statisticsAt<=:endTime AND a.origin=1 AND a.payMode=5', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one()['allMoney']?:0;//余额支付营业额 $data['lineTurnover']=bcsub($data['turnover'],$data['balanceTurnover'],2);//线上营业额 $moneyData=(new \yii\db\Query()) ->select(['sum(storeActualMoney)storeActualMoney','sum(money)money','sum(money-storeActualMoney)serviceMoney','count(id)count', 'sum(storeBoxMoney)storeBoxMoney','sum(storeGoodsMoney)storeGoodsMoney','sum(storeDeliveryMoney)storeDeliveryMoney','sum(subsidy) subsidy']) ->from('{{%ybwm_bill}}') ->where('(payType=1 || payPort=2) AND (refundMoney=0 or refundMoney is null) AND uniacid=:uniacid AND storeId=:storeId AND statisticsAt>=:startTime AND statisticsAt<=:endTime AND origin=1', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one(); $data['balancePayment']=(new \yii\db\Query()) ->select(['sum(money) money']) ->from('{{%ybwm_bill}}') ->where('(refundMoney=0 or refundMoney is null) AND uniacid=:uniacid AND storeId=:storeId AND statisticsAt>=:startTime AND statisticsAt<=:endTime AND origin=1 AND payMode=5', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one()['money']?:0;//余额实付 $data['payment']=$moneyData['money']?:'0.00';//实付 $data['linePayment']=bcsub($data['payment'],$data['balancePayment'],2);//线上实付 $data['serviceMoney']=$moneyData['serviceMoney'];//服务费 $data['payNum']=$moneyData['count'];//有效订单数 //外卖订单 $data['outOrderNum']=(new \yii\db\Query()) ->from('{{%ybwm_takeout_order}}') ->where('uniacid=:uniacid',[':uniacid'=>$uniacid]) ->andWhere(['in','state',[2,3,4,5,6,9]]) ->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]) ->andWhere('storeId=:storeId',['storeId'=>$storeId]) ->andWhere(['<>','deliveryMode',10])->count(); //自提订单 $data['inOrderNum']=(new \yii\db\Query()) ->from('{{%ybwm_takeout_order}}') ->where('uniacid=:uniacid',[':uniacid'=>$uniacid]) ->andWhere(['in','state',[2,3,4,5,6,9]]) ->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]) ->andWhere('storeId=:storeId',['storeId'=>$storeId]) ->andWhere(['deliveryMode'=>10])->count(); $refund=(new \yii\db\Query()) ->select('sum(refundMoney) refundMoney,count(id) count') ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND refundAt>=:startTime AND refundAt<=:endTime', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one(); $userRefund=(new \yii\db\Query()) ->select('sum(refundMoney) refundMoney,count(id) count') ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND refundAt>=:startTime AND refundAt<=:endTime AND refundType=1', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one(); $storeRefund=(new \yii\db\Query()) ->select('sum(refundMoney) refundMoney,count(id) count') ->from('{{%ybwm_bill}}') ->where('uniacid=:uniacid AND storeId=:storeId AND refundAt>=:startTime AND refundAt<=:endTime AND refundType=2', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one(); $data['refundMoney']=$refund['refundMoney']?:'0.00';//退款金额 $data['refundNum']=$refund['count'];//退款数 $data['userRefundMoney']=$userRefund['refundMoney']?:'0.00';//用户退款金额 $data['userRefundNum']=$userRefund['count'];//用户退款数 $data['storeRefundMoney']=$storeRefund['refundMoney']?:'0.00';//商家退款金额 $data['storeRefundNum']=$storeRefund['count'];//商家退款数 $data['storeDeliveryMoney']=$moneyData['storeDeliveryMoney']?:'0.00';//配送费收益 $data['storeBoxMoney']=$moneyData['storeBoxMoney'];//餐盒费收益 $data['storeGoodsMoney']=$moneyData['storeGoodsMoney'];//商品收益 $other=(new \yii\db\Query()) ->select(['sum(storeActualMoney) storeActualMoney,count(id) count']) ->from('{{%ybwm_bill}}') ->where('(refundMoney=0 or refundMoney is null) AND uniacid=:uniacid AND storeId=:storeId AND statisticsAt>=:startTime AND statisticsAt<=:endTime AND origin not in (1,5,6,7)', ['startTime'=>$startTime,'endTime'=>$endTime,':uniacid'=>$uniacid,'storeId'=>$storeId]) ->one(); $data['otherMoney']=$other['storeActualMoney']?:'0.00';//其他费用 $data['estimate']=bcadd($moneyData['storeActualMoney'],$data['otherMoney'],2)?:'0.00';//预计结算 $data['subsidy']=$moneyData['subsidy']?:'0.00';//补贴 $data['otherNum']=$other['count'];//其他订单数 $data['views']=Statistics::getViews($uniacid,$storeId,$startTime,$endTime);//浏览量 $data['visitCount']=Statistics::getVisitCount($uniacid,$storeId,$startTime,$endTime);//访客数 $data['payPeople']=Statistics::getPayPeople($uniacid,$storeId,$startTime,$endTime);//付款人数 return $data; } //账单详情(新) public static function newBillInfo($uniacid,$storeId,$type,$startTime=null,$endTime=null){ $data['allMoney']=Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,[]);//总营业 $data['allEstimate']=Financial::getIncome($startTime,$endTime,$uniacid,$storeId,[]);//预计 $data['allEffective']=Financial::getEffective($startTime,$endTime,$uniacid,$storeId,[]);//有效 $data['allInvalidCount']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,[])['count']?:0;//无效订单 if($type==1){ $orderInfoMoney=Financial::getOrderInfoMoney($startTime,$endTime,$uniacid,$storeId);//订单详情金额 $data['deliveryMoney']=$orderInfoMoney['deliveryMoney']?:0; $data['boxMoney']=$orderInfoMoney['boxMoney']?:0; $data['goodsMoney']=$orderInfoMoney['goodsMoney']?:0; $origin=[1]; $data['otherMoney']=Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,[4]);//其他收入 $data['otherCount']=Financial::getEffective($startTime,$endTime,$uniacid,$storeId,[4]);//其他数量 }elseif($type==2){ $origin=[6,7]; $orderInfoMoney=Financial::getInStoreOrderInfoMoney($startTime,$endTime,$uniacid,$storeId);//订单详情金额 $data['boxMoney']=$orderInfoMoney['boxMoney']?:0; $data['goodsMoney']=$orderInfoMoney['goodsMoney']?:0; $data['inStoreMoney']=Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,[7]);//总营业 $data['inStoreSubsidy']=Financial::getSubsidy($startTime,$endTime,$uniacid,$storeId,[7]);//补贴 $data['inStoreService']=Financial::getService($startTime,$endTime,$uniacid,$storeId,[7]);//服务费 $data['inStoreIncome']=Financial::getIncome($startTime,$endTime,$uniacid,$storeId,[7]);//预计收入 $data['inStoreEffective']=Financial::getEffective($startTime,$endTime,$uniacid,$storeId,[7]);//有效订单数 $data['inStoreInvalidCount']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,[7])['count']?:0;//无效订单数 $data['inStoreInvalidMoney']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,[7])['money']?:0;//无效订单金额 $data['fastMoney']=Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,[6]);//总营业 $data['fastSubsidy']=Financial::getSubsidy($startTime,$endTime,$uniacid,$storeId,[6]);//补贴 $data['fastService']=Financial::getService($startTime,$endTime,$uniacid,$storeId,[6]);//服务费 $data['fastIncome']=Financial::getIncome($startTime,$endTime,$uniacid,$storeId,[6]);//预计收入 $data['fastEffective']=Financial::getEffective($startTime,$endTime,$uniacid,$storeId,[6]);//有效订单数 $data['fastInvalidCount']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,[6])['count']?:0;//无效订单数 $data['fastInvalidMoney']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,[6])['money']?:0;//无效订单金额 }else{ $origin=[5]; } $data['money']=Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,$origin);//总营业 $data['effective']=Financial::getEffective($startTime,$endTime,$uniacid,$storeId,$origin);//有效订单数 $data['service']=Financial::getService($startTime,$endTime,$uniacid,$storeId,$origin);//服务费 $data['income']=Financial::getIncome($startTime,$endTime,$uniacid,$storeId,$origin);//预计收入 if($type!=3){ $data['subsidy']=Financial::getSubsidy($startTime,$endTime,$uniacid,$storeId,$origin);//补贴 $data['unitPrice']=Financial::getUnitPrice($startTime,$endTime,$uniacid,$storeId,$origin);//客单价 $data['invalidCount']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,$origin)['count']?:0;//无效订单数 $data['invalidMoney']=Financial::getInvalidOrder($startTime,$endTime,$uniacid,$storeId,$origin)['money']?:0;//无效订单金额 } return $data; } //提现 public static function withdrawal($uniacid,$storeId){ $result=axios_request(); $money=$result['money'];//提现金额 $withdrawal=Config::getSystemSet('withdrawal',$uniacid);//获取提现设置 $downMoney=$withdrawal['downMoney']?:0; if($money<$downMoney){ return '最低提现'.$downMoney.'元'; } $record=(new \yii\db\Query()) ->from('{{%ybwm_store_finance}}') ->where('storeId=:storeId',[':storeId'=>$storeId]) ->orderBy('id desc') ->one(); if (time() < $record['createdAt'] + $withdrawal['day'] * 24 * 3600) { return '提现周期未到,不可提现'; } $storeWithdrawal=Config::getStoreSet('withdrawal',$storeId);//获取商家提现设置 if(!$storeWithdrawal){ return '商家提现方式未设置'; } $balance=StoreBill::getMerchantMoney($uniacid,$storeId);//获取商家余额 if($balance['money']<$money){ return '商家余额不足'; } if($storeWithdrawal['type']==1){ //微信 $account['userId']=$storeWithdrawal['userId']; $account['wxTel']=$storeWithdrawal['wxTel']; $account['wxImg']=$storeWithdrawal['wxImg']; } if($storeWithdrawal['type']==2){ //支付宝 $account['aliTel']=$storeWithdrawal['aliTel']; $account['aliAccount']=$storeWithdrawal['aliAccount']; $account['aliUserName']=$storeWithdrawal['aliUserName']; $account['aliImg']=$storeWithdrawal['aliImg']; } if($storeWithdrawal['type']==3){ //银行卡 $account['bankTel']=$storeWithdrawal['bankTel']; $account['bankName']=$storeWithdrawal['bankName']; $account['subBank']=$storeWithdrawal['subBank']; $account['bankUserName']=$storeWithdrawal['bankUserName']; $account['bankAccount']=$storeWithdrawal['bankAccount']; } $data['money']=$money; $charge=0; if($storeWithdrawal['rate']>0){ $charge = bcmul($money, $storeWithdrawal['rate'] / 100, 2); if ($charge < $storeWithdrawal['rateDownMoney']) { $charge = $storeWithdrawal['rateDownMoney']; } if ($charge > $storeWithdrawal['rateUpMoney'] AND $storeWithdrawal['rateUpMoney']>0) { $charge = $storeWithdrawal['rateUpMoney']; } } $data['account']=json_encode($account); $data['charge']=$charge;//手续费 $data['type']=$storeWithdrawal['type']; $data['actualMoney'] = bcsub($money, $charge, 2); $data['state'] = 1; $data['storeId'] = $storeId; $data['uniacid'] = $uniacid; $data['createdAt'] = time(); $data['outTradeNo'] = date("YmdHis") . rand(111111, 999999); $res=Yii::$app->db->createCommand()->insert('{{%ybwm_store_finance}}', $data)->execute(); if($res){ Bill::saveStoreBill($uniacid,$storeId,$money,2,6,'提现申请',time()); return '成功'; }else{ return '网络异常,请稍后再试'; } } //提现设置 public static function withdrawalSet($uniacid,$storeId){ $request = Yii::$app->request; $result=axios_request(); $res=Config::getStoreSet('withdrawal',$storeId); if ($request->isGet){ $withdrawal=Config::getSystemSet('withdrawal',$uniacid); $res['channel']=$withdrawal['channel']; $user=(new \yii\db\Query()) ->select('userName') ->from('{{%ybwm_member}}') ->where('id=:id AND uniacid=:uniacid',[':id'=>$res['userId'],':uniacid'=>$uniacid]) ->one(); $res['userName']=$user['userName']?:''; return '成功'; } if($res){ return '提现方式已设置,请联系管理员修改'; } $re=Config::saveStoreSet($result,'withdrawal',$storeId,'提现设置'); if($re){ return '成功'; }else{ return '网络异常,请稍后再试'; } } }