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

207 lines
10 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\Statistics;
use app\models\common\Store;
use app\models\common\StoreBill;
use app\models\common\Bill;
class FinanceController 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) {
return $this->result(3, '无效的门店,请先添加商店');
die;
}
}
}
public function actionGetFinanceData(){
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$storeId=$result['storeId']?$result['storeId']:$this->shop_id;
$startTime=strtotime(date('Y-m-d 00:00:00',strtotime($result['startTime'])));
$endTime=strtotime(date('Y-m-d 23:59:59',strtotime($result['endTime'])));
$payMoney=(new \yii\db\Query())
->select(['sum(money) as money'])
->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])->one()['money']?:0.00;
//微信支付
$wxPay=(new \yii\db\Query())
->select(['sum(money) as money'])
->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(['payMode'=>1])->one()['money']?:0.00;
//余额支付
$balance=(new \yii\db\Query())
->select(['sum(money) as money'])
->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(['payMode'=>5])->one()['money']?:0.00;
//支付订单数
$payNum=Statistics::getPayNum($uniacid,$storeId,$startTime,$endTime);
//外卖订单
$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();
//自提订单
$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();
//支付人数
$userCount=Statistics::getPayPeople($uniacid,$storeId,$startTime,$endTime);
//浏览量
$viewsCount=Statistics::getViews($uniacid,$storeId,$startTime,$endTime);
//访客量
$visitCount=Statistics::getVisitCount($uniacid,$storeId,$startTime,$endTime);
//总退款金额
$refundQuery=(new \yii\db\Query())
->select('sum(money)money')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['state'=>10])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId]);
$refundMoney=$refundQuery->one()['money']?:0.00;
//用户退款
$userRefundMoney=(new \yii\db\Query())
->select('sum(money)money')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['state'=>10])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId])
->andWhere(['refundType'=>3])->count()?:0;
//商家退款
$storeRefundMoney=(new \yii\db\Query())
->select('sum(money)money')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['state'=>10])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId])
->andWhere(['in','refundType',[1,4]])->count()?:0.00;
//客单价
$unitPrice=Statistics::getUnitPrice($uniacid,$storeId,$startTime,$endTime);
//下单转化率
$singleRate=bcmul(Statistics::getConversion($uniacid,$storeId,$startTime,$endTime),100,2).'%';
//下单跳失率
$jumpRate=bcmul(bcdiv(bcsub($visitCount,$userCount,2),$visitCount,2),100,2).'%';
$startTime=strtotime(date('Y-m-d 00:00:00',bcsub($endTime,14*24*60*60)));
$payMoneyArr=Bill::billDataByDay($uniacid,$startTime,$endTime,$storeId);
$countArr=Bill::billDataByDay($uniacid,$startTime,$endTime,$storeId,2);
$cashierTable=(new \yii\db\Query())
->from('{{%ybwm_cashier_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])
->andWhere(['in','state',[2]])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId]);
$cashierCount=(clone $cashierTable)->count()?:0;//当面付订单数
$cashierUser=(clone $cashierTable)->groupBy('userId')->count()?:0;//当面付支付人数
$cashierPay=(clone $cashierTable)->sum('money')?:0;//当面付金额
$cashierWxPay=(clone $cashierTable)->andWhere('payMode=1')->sum('money')?:0;//当面付微信支付
$cashierBalancePay=(clone $cashierTable)->andWhere('payMode=5')->sum('money')?:0;//当面付微信支付
$fastTable=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('uniacid=:uniacid AND orderMode=2',[':uniacid'=>$uniacid])
->andWhere(['in','state',[2,3,4,6]])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId]);
$fastPay=(clone $fastTable)->sum('money')?:0;//快餐金额
$fastBalancePay=(clone $fastTable)->andWhere('payMode=5')->sum('money')?:0;//快餐余额支付
$fastWxPay=(clone $fastTable)->andWhere('payMode=1')->sum('money')?:0;//快餐微信支付
$fastCount=(clone $fastTable)->count()?:0;//快餐订单数
$fastUser=(clone $fastTable)->groupBy('userId')->count()?:0;//快餐支付人数
$instoreTable=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('uniacid=:uniacid AND orderMode=1',[':uniacid'=>$uniacid])
->andWhere(['in','state',[3]])
->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])
->andWhere('storeId=:storeId',['storeId'=>$storeId]);
$instoreCount=(clone $instoreTable)->count()?:0;//堂食订单数
$instorePay=(clone $instoreTable)->sum('money')?:0;//堂食营业额
$instoreActualPay=(clone $instoreTable)->andWhere('payMode!=6')->sum('money')?:0;//堂食实际金额
$instoreWxPay=(clone $instoreTable)->andWhere('payMode=1')->sum('money')?:0;//堂食微信支付
$instoreBalancePay=(clone $instoreTable)->andWhere('payMode=5')->sum('money')?:0;//堂食余额支付
$instoreBalanceOfflinePay=(clone $instoreTable)->andWhere('payMode=6')->sum('money')?:0;//堂食线下订单金额
$instoreBalanceOfflineCount=(clone $instoreTable)->andWhere('payMode=6')->count()?:0;//堂食线下订单数
$data=array(
'payMoney'=>$payMoney,//有效支付金额
'wxPay'=>$wxPay,//微信支付
'balance'=>$balance,//余额支付
'payNum'=>$payNum,//支付订单数
'outOrderNum'=>$outOrderNum,//外卖订单
'inOrderNum'=>$inOrderNum,//自提订单
'userCount'=>$userCount,//支付人数
'viewsCount'=>$viewsCount,//浏览量
'visitCount'=>$visitCount,//访客量
'refundMoney'=>$refundMoney,//总退款金额
'userRefundMoney'=>$userRefundMoney,//用户取消订单
'storeRefundMoney'=>$storeRefundMoney,//商家拒单订单
'unitPrice'=>$unitPrice,//客单价
'singleRate'=>$singleRate,//下单转化率
'jumpRate'=>$jumpRate,//下单跳失率
'payMoneyArr'=>$payMoneyArr, //支付金额折线图
'countArr'=>$countArr, //支付订单数折线图
'cashierCount'=>$cashierCount,
'cashierPay'=>$cashierPay,
'cashierWxPay'=>$cashierWxPay,
'cashierBalancePay'=>$cashierBalancePay,
'fastCount'=>$fastCount,
'fastPay'=>$fastPay,
'fastWxPay'=>$fastWxPay,
'fastBalancePay'=>$fastBalancePay,
'instoreCount'=>$instoreCount,
'instorePay'=>$instorePay,
'instoreActualPay'=>$instoreActualPay,
'instoreWxPay'=>$instoreWxPay,
'instoreBalancePay'=>$instoreBalancePay,
'instoreBalanceOfflinePay'=>$instoreBalanceOfflinePay,
'instoreBalanceOfflineCount'=>$instoreBalanceOfflineCount,
'cashierUser'=>$cashierUser,
'fastUser'=>$fastUser,
);
return $this->result(1,'成功',$data);
}
}