文件
mashangyoujiu/Application/Common/Business/BranchProfit.class.php

264 行
10 KiB
PHP
可执行文件

<?php
namespace Common\Business;
use Common\Model\RetailAdminModel;
use Common\Model\RetailAgentModel;
use Common\Model\RetailContinentCashModel;
use Common\Model\RetailMerchantModel;
use Common\Model\RetailStoreModel;
//[1.7.90]
/**
* 分润类
* Class BranchProfit
* [
* 分润明细与统计 getList
* 分润明细与统计的查询条件 getListTerm
* 退款分润 refundBranchProfit
* ]
* @package Common\Business
*/
class BranchProfit
{
/**
* 分润明细与统计
* @param $param
* [
* operator_type 操作端 1后台
* user_type 1合伙人 2代理商 3商户 4门店
* partner_id
* agent_id
* merchant_id
* search_info=>查询条件json字符串
* [
* profit_type 1直连分润 2新大陆分润 3云闪付分润
* date_range 日期范围
* data_type 日期类型 1 今天 2昨天 3上个月 4前三个月
* agent_id 代理商id
* ]
* page
* limit
* ]
* @return mixed
*/
public function getList($param)
{
$where = $this->getListTerm($param); //查询条件
$ContinentCashModel = new RetailContinentCashModel();
$list = $ContinentCashModel->getSelect($where, '*', 'id desc', $param['page'] . ',' . $param['limit']);
if (!empty($list)) {
$count = $ContinentCashModel->getCount($where);
$StoreModel = new RetailStoreModel();
$MerchantModel = new RetailMerchantModel();
$AgentModel = new RetailAgentModel();
foreach ($list as $key => &$val) {
$val['index'] = $key + 1;
$val['add_time'] = date('Y-m-d H:i:s', $val['cdate']);
$val['store_name'] = $StoreModel->getFindField([
'id' => $val['storeid']
], 'storetag');
$val['merchant_name'] = $MerchantModel->getFindField([
'id' => $val['merchantid']
], 'name');
$val['agent_rate'] = floatval($val['agent_rate']);
$val['agent_profit'] = floatval($val['agent_profit']);
$val['partner_rate'] = floatval($val['partner_rate']);
$val['partner_profit'] = floatval($val['partner_profit']);
$val['agent_refund_profit'] = floatval($val['agent_refund_profit']);
$val['partner_refund_profit'] = floatval($val['partner_refund_profit']);
$val['trade_amount'] = floatval($val['trade_amount']);
$val['refund_amount'] = floatval($val['refund_amount']);
$val['merchant_rate'] = floatval($val['merchant_rate']);
$val['agent_name'] = $AgentModel->getFindField([
'id' => $val['agent_id']
], 'name');
$val['pay_type'] = $val['pay_type'] == 2 ? '支付宝' : '微信';
}
}
return callBack(statusCode()['success'], '查询成功', [
'list' => $list,
'count' => $count ? $count : 0
]);
}
/**
* 分润统计
* @param $param
* [
* operator_type 操作端 1后台
* user_type 1合伙人 2代理商 3商户 4门店
* partner_id
* agent_id
* merchant_id
* search_info=>查询条件json字符串
* [
* profit_type 1直连分润 2新大陆分润 3云闪付分润
* date_range 日期范围
* data_type 日期类型 1 今天 2昨天 3上个月 4前三个月
* agent_id 代理商id
* ]
* ]
* @return mixed
*/
public function profitTotal($param)
{
$where = $this->getListTerm($param); //查询条件
$ContinentCashModel = new RetailContinentCashModel();
$list = $ContinentCashModel->getSelect($where, 'trade_amount,refund_amount,partner_profit,partner_refund_profit,agent_profit,agent_refund_profit');
$all_money = 0; //订单总金额
$all_refund_money = 0; //订单总退款金额
$partner_net_profit = 0; //合伙人的净分润
$agent_net_profit = 0; //代理商净分润
if (!empty($list)) {
foreach ($list as $val) {
$all_money += $val['trade_amount'];
$all_refund_money += $val['refund_amount'];
$partner_net_profit += ($val['partner_profit'] - $val['partner_refund_profit']);
$agent_net_profit += ($val['agent_profit'] - $val['agent_refund_profit']);
}
}
return callBack(statusCode()['success'], '查询成功', [
'all_money' => round($all_money, 2),
'all_refund_money' => round($all_refund_money, 2),
'partner_net_profit' => round($partner_net_profit, 2),
'agent_net_profit' => round($agent_net_profit, 2),
'transactions_count' => $list ? count($list) : 0,
]);
}
/**
* 分润明细与统计的查询条件
* @param $param
* [
* operator_type 操作端 1后台
* user_type 1合伙人 2代理商 3商户 4门店
* partner_id
* agent_id
* merchant_id
* search_info=>查询条件json字符串
* [
* profit_type 1直连分润 2新大陆分润 3云闪付分润
* date_range 日期范围
* data_type 日期类型 1 今天 2昨天 3上个月 4前三个月
* agent_id 代理商id
* merchant_id 商户id
* ]
* ]
* @return mixed
*/
public function getListTerm($param)
{
$search_info = json_decode($param['search_info'], true); //条件
//支付渠道
if (!empty($search_info['profit_type'])) {
$where['type'] = $search_info['profit_type'];
}
//某代理商
if (!empty($search_info['agent_id'])) {
$where['agent_id'] = $search_info['agent_id'];
}
//某商户
if (!empty($search_info['merchant_id'])) {
$where['merchantid'] = $search_info['merchant_id'];
}
if (!empty($search_info['data_type'])) {
switch ($search_info['data_type']) {
case 1:
//今日
$start_time = strtotime(date('Y-m-d 00:00:00'));
$end_time = strtotime(date('Y-m-d 23:59:59'));
break;
case 2:
//昨天
$start_time = strtotime(date('Y-m-d', time() - 86400));
$end_time = strtotime(date('Y-m-d 23:59:59', time() - 86400));
break;
case 3:
//上个月
$start_time = mktime(0, 0, 0, date("m") - 1, 1, date("Y"));
$end_time = mktime(23, 59, 59, date("m"), 0, date("Y"));
break;
case 4:
//三个月
$start_time = strtotime(date('Y-m-01 00:00:00', strtotime('-3 month')));
$end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d') . 'day')));
break;
}
} else if (!empty($search_info['date_range'])) {
$start_time = strtotime($search_info['date_range'][0] . '00:00:00');
$end_time = strtotime($search_info['date_range'][1] . '23:59:59');
}
if (!empty($start_time) && !empty($end_time)) {
$where['cdate'] = ['between', [$start_time, $end_time]];
}
switch ($param['user_type']) {
case 1:
$where['partner_id'] = $param['partner_id'];
break;
case 2:
$where['agent_id'] = $param['agent_id'];
break;
}
$where['state'] = 0;
return $where;
}
/**
* 退款分润
* @param $param
* [
* pay_order 支付流水信息
* ]
* @return mixed
*/
public function refundBranchProfit($param)
{
try {
$payOrder = $param['pay_order'];
$ContinentCashModel = new RetailContinentCashModel();
$info = $ContinentCashModel->getFind([
'out_trade_no' => $payOrder['before_trade_no'],
'state' => 0
]);
if (!empty($info)) {
$refundMoney = $payOrder['trade_amount'];
if ((floatval($refundMoney) < round($info['trade_amount'] + $info['refund_amount'], 2))) {
$partner_refund_profit = round(($info['agent_rate'] - $info['partner_rate']) / 100 * $refundMoney, 2);
$agent_refund_profit = round(($info['merchant_rate'] - $info['agent_rate']) / 100 * $refundMoney, 2);
} else {
$partner_refund_profit = $info['partner_profit'];
$agent_refund_profit = $info['agent_profit'];
}
$save['partner_refund_profit'] = round($info['partner_refund_profit'] + $partner_refund_profit, 2);
$save['agent_refund_profit'] = round($info['agent_refund_profit'] + $agent_refund_profit, 2);
if ($save['partner_refund_profit'] > $info['partner_profit']) {
$partner_refund_profit = round($info['partner_profit'] - $info['partner_refund_profit'], 2);
$save['partner_refund_profit'] = $info['partner_profit'];
}
if ($agent_refund_profit > $info['agent_profit']) {
$agent_refund_profit = round($info['agent_profit'] - $info['agent_refund_profit'], 2);
$save['agent_refund_profit'] = $info['agent_profit'];
}
$save['refund_amount'] = round($refundMoney + $info['refund_amount'], 2);
//合伙人余额
$AdminModel = new RetailAdminModel();
$AdminModel->toSetDec(['id' => $info['partner_id']], 'balance', $agent_refund_profit);
//代理商余额
$AgentModel = new RetailAgentModel();
$AgentModel->toSetDec([
'id' => $info['agent_id']
], 'balance', $partner_refund_profit);
//分润记录修改
$ContinentCashModel->updateOrder([
'id' => $info['id']
], $save);
}
return callBack(statusCode()['success'], '分润修改成功');
} catch (\Exception $e) {
return callBack(statusCode()['error'], '分润修改失败');
}
}
}