814 lines
37 KiB
PHP
814 lines
37 KiB
PHP
<?php
|
||
/**
|
||
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
||
* =========================================================
|
||
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
||
* ----------------------------------------------
|
||
* 官方网址: https://www.gobuysaas.com
|
||
* =========================================================
|
||
*/
|
||
|
||
namespace addon\commission\model;
|
||
|
||
use addon\aliapp\model\MinCode;
|
||
use addon\dividemoney\model\DividemoneyAccount;
|
||
use addon\team\model\Commission;
|
||
use app\model\BaseModel;
|
||
use app\model\order\OrderCommon;
|
||
use think\facade\Db;
|
||
|
||
class Account extends BaseModel
|
||
{
|
||
|
||
/**
|
||
* Common: 获取流水信息
|
||
* Author: wu-hui
|
||
* Time: 2024/05/13 17:20
|
||
* @param $page
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
public function accountList($page, $params)
|
||
{
|
||
$pageSize = $params['page_size'] ?? PAGE_LIST_ROWS;
|
||
// 生成查询条件
|
||
$where = [];
|
||
if (isset($params['member_id']) && $params['member_id'] !== '') $where[] = ['member_id', '=', $params['member_id']];
|
||
if (isset($params['account_type']) && $params['account_type'] !== '') {
|
||
if (is_array($params['account_type'])) $where[] = ['account_type', 'in', $params['account_type']];
|
||
else $where[] = ['account_type', '=', $params['account_type']];
|
||
}
|
||
// 关联查询
|
||
$result = model('commission_account')->pageList($where, true, 'id DESC', $page, $pageSize);
|
||
|
||
return $this->success($result);
|
||
}
|
||
|
||
/**
|
||
* Common: 获取流水信息
|
||
* Author: wu-hui
|
||
* Time: 2024/05/21 15:08
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
public function accountListV2($params)
|
||
{
|
||
$page = $params['page'] ?? 1;
|
||
$pageSize = $params['page_size'] ?? PAGE_LIST_ROWS;
|
||
// 生成查询条件
|
||
$where = [];
|
||
if (isset($params['member_id']) && $params['member_id'] !== '') $where[] = ['a.member_id', '=', $params['member_id']];
|
||
if (isset($params['account_type']) && $params['account_type'] !== '') {
|
||
// all=全部;promoter=推广员佣金;merchants=招商员佣金;partner=合伙人佣金;
|
||
switch ($params['account_type']) {
|
||
case 'promoter':
|
||
$where[] = ['a.account_type', 'in', ['promoter_refund', 'promoter']];
|
||
break;
|
||
case 'merchants':
|
||
$where[] = ['a.account_type', 'in', ['merchants_refund', 'merchants']];
|
||
break;
|
||
case 'partner':
|
||
$where[] = ['a.account_type', 'in', ['partner_refund', 'partner']];
|
||
break;
|
||
case 'legumes_integral':
|
||
$where[] = ['a.account_type', 'in', ['use_legumes_integral', 'close_legumes_integral']];
|
||
break;
|
||
default :
|
||
$where[] = ['a.account_type', 'in', ['sync', 'partner_refund', 'partner', 'promoter_refund', 'promoter', 'merchants_refund', 'merchants']];
|
||
}
|
||
}
|
||
if (isset($params['time_type']) && $params['time_type'] !== '' && $params['time_type'] !== 'all') {
|
||
// all=全部;day=当日;week=本周;month=本月;
|
||
$startTime = $endTime = 0;
|
||
switch ($params['time_type']) {
|
||
case 'day':
|
||
[$startTime, $endTime] = getTimeStamp('today');
|
||
|
||
$where[] = ['a.create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
case 'week':
|
||
[$startTime, $endTime] = getTimeStamp('week');
|
||
$where[] = ['a.create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
case 'month':
|
||
[$startTime, $endTime] = getTimeStamp('month');
|
||
$where[] = ['a.create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
}
|
||
}
|
||
if (isset($params['status']) && $params['status'] !== '') $where[] = ['a.status', '=', $params['status']];
|
||
// 流水列表
|
||
$join = [
|
||
['member m', 'm.member_id = a.member_id', 'left'],
|
||
['site s', 's.site_id = a.site_id', 'left'],
|
||
];
|
||
$field = 'a.*,m.username,m.nickname,m.headimg,s.site_name,s.contacts_name,s.contacts_mobile';
|
||
|
||
$result = model('commission_account')->pageList($where, $field, 'id DESC', $page, $pageSize, 'a', $join);
|
||
|
||
return $this->success($result);
|
||
}
|
||
|
||
/**
|
||
* Common: 获取流水信息(平台抽成流水和团队奖励流水合并)
|
||
* Author: wu-hui
|
||
* Time: 2024/08/13 11:51
|
||
* @param $params
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function accountListV3($params)
|
||
{
|
||
$page = $params['page'] ?? 1;
|
||
$pageSize = $params['page_size'] ?? PAGE_LIST_ROWS;
|
||
// 生成查询条件
|
||
$where = [];
|
||
$unionWhere = [];
|
||
if (isset($params['member_id']) && $params['member_id'] !== '') {
|
||
$where[] = ['member_id', '=', $params['member_id']];
|
||
$unionWhere[] = ['member_id', '=', $params['member_id']];
|
||
}
|
||
if (isset($params['account_type']) && $params['account_type'] !== '') {
|
||
// all=全部;promoter=推广员佣金;merchants=招商员佣金;partner=合伙人佣金;
|
||
switch ($params['account_type']) {
|
||
case 'promoter':
|
||
$where[] = ['account_type', 'in', ['promoter_refund', 'promoter']];
|
||
$unionWhere[] = ['id', '=', 0];
|
||
break;
|
||
case 'merchants':
|
||
$where[] = ['account_type', 'in', ['merchants_refund', 'merchants']];
|
||
$unionWhere[] = ['id', '=', 0];
|
||
break;
|
||
case 'partner':
|
||
$where[] = ['account_type', 'in', ['partner_refund', 'partner']];
|
||
$unionWhere[] = ['id', '=', 0];
|
||
break;
|
||
case 'legumes_integral':
|
||
$where[] = ['account_type', 'in', ['use_legumes_integral', 'close_legumes_integral']];
|
||
$unionWhere[] = ['id', '=', 0];
|
||
break;
|
||
case 'team':
|
||
$where[] = ['id', '=', 0];
|
||
break;
|
||
default :
|
||
$where[] = ['account_type', 'in', ['sync', 'partner_refund', 'partner', 'promoter_refund', 'promoter', 'merchants_refund', 'merchants']];
|
||
}
|
||
}
|
||
if (isset($params['time_type']) && $params['time_type'] !== '' && $params['time_type'] !== 'all') {
|
||
// all=全部;day=当日;week=本周;month=本月;
|
||
$startTime = $endTime = 0;
|
||
switch ($params['time_type']) {
|
||
case 'day':
|
||
[$startTime, $endTime] = getTimeStamp('today');
|
||
|
||
$where[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
$unionWhere[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
case 'week':
|
||
[$startTime, $endTime] = getTimeStamp('week');
|
||
$where[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
$unionWhere[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
case 'month':
|
||
[$startTime, $endTime] = getTimeStamp('month');
|
||
$where[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
$unionWhere[] = ['create_time', 'between', [date("Y-m-d H:i:s", $startTime), date("Y-m-d H:i:s", $endTime)]];
|
||
break;
|
||
}
|
||
}
|
||
if (isset($params['status']) && $params['status'] !== '') {
|
||
$where[] = ['status', '=', $params['status']];
|
||
$unionWhere[] = ['status', '=', $params['status']];
|
||
}
|
||
// 信息查询
|
||
$count = Db::name('commission_account')
|
||
->field(['id'])
|
||
->union(function ($query) use ($unionWhere) {
|
||
$query->name('team_account')->field(['id'])->where($unionWhere);
|
||
})->where($where)->count();
|
||
|
||
$list = Db::name('commission_account')
|
||
->field(['id', 'create_time', 'account_type', 'status', 'account_data', '"commission" as type'])
|
||
->union(function ($query) use ($unionWhere) {
|
||
$query->name('team_account')->field(['id', 'create_time', 'account_type', 'status', 'account_data', '"team" as type'])->where($unionWhere);
|
||
})
|
||
->where($where)
|
||
->order('create_time', 'DESC')
|
||
->page($page, $pageSize)
|
||
// ->fetchSql()
|
||
->select()
|
||
->toArray();
|
||
|
||
return $this->success([
|
||
'count' => $count,
|
||
'list' => $list,
|
||
'page_count' => ceil($count / $pageSize),
|
||
]);
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 统计信息
|
||
* Author: wu-hui
|
||
* Time: 2024/05/21 14:06
|
||
* @param $memberId
|
||
* @return array
|
||
*/
|
||
public function statistics($memberId)
|
||
{
|
||
// 总获得佣金
|
||
$all = model('commission_account')->getSum([
|
||
['account_data', '>', 0],
|
||
['member_id', '=', $memberId],
|
||
['account_type', 'in', ['sync', 'promoter', 'merchants', 'partner']],
|
||
], 'account_data');
|
||
// 可提现收益
|
||
$withdrawal = model('member')->getValue([
|
||
['member_id', '=', $memberId],
|
||
], 'commission_money');
|
||
// 总获得 推广收益
|
||
$totalPromoter = model('commission_account')->getSum([
|
||
['member_id', '=', $memberId],
|
||
['account_type', '=', 'promoter']
|
||
], 'account_data');
|
||
// 总获得 招商收益
|
||
$totalMerchants = model('commission_account')->getSum([
|
||
['member_id', '=', $memberId],
|
||
['account_type', '=', 'merchants']
|
||
], 'account_data');
|
||
// 总获得 合伙人分红
|
||
$totalPartner = model('commission_account')->getSum([
|
||
['member_id', '=', $memberId],
|
||
['account_type', '=', 'partner']
|
||
], 'account_data');
|
||
// 推广收益(推广订单数、推广订单总额、推广收益)
|
||
$promoter = Db::name('commission_account')
|
||
->field([
|
||
'COUNT(DISTINCT cr.order_id) AS promoter_order',
|
||
'sum(cr.real_goods_money) as promoter_order_money',
|
||
'sum(a.account_data) as promoter_account'
|
||
])
|
||
->alias('a')
|
||
->leftjoin("commission_record cr", "a.join_id = cr.id")
|
||
->where([
|
||
['a.member_id', '=', $memberId],
|
||
['a.account_type', '=', 'promoter'],
|
||
['cr.id', '>', 0]
|
||
])
|
||
->findOrEmpty();
|
||
// 招商收益(邀请门店、招商订单总额、招商收益)
|
||
$merchants = Db::name('commission_account')
|
||
->field([
|
||
'sum(cr.real_goods_money) as promoter_order_money',
|
||
'sum(a.account_data) as merchants_account'
|
||
])
|
||
->alias('a')
|
||
->leftjoin("commission_record cr", "a.join_id = cr.id")
|
||
->where([
|
||
['a.member_id', '=', $memberId],
|
||
['a.account_type', '=', 'merchants'],
|
||
['cr.id', '>', 0]
|
||
])
|
||
->findOrEmpty();
|
||
$merchants['total_store'] = model('store')->getCount([
|
||
['merchants_member_id', '=', $memberId]
|
||
]);
|
||
// 合伙人(持有权重值、分红收益)
|
||
$partner['hold_weight_value'] = model('commission_weight_value')->getSum([
|
||
['member_id', '=', $memberId]
|
||
], 'quantity');
|
||
$partner['partner_account'] = model('commission_account')->getSum([
|
||
['member_id', '=', $memberId],
|
||
['account_type', '=', 'partner']
|
||
], 'account_data');
|
||
// 团队奖励 - 相关统计
|
||
$teamStatistics = (new Commission())->statistics($memberId);
|
||
|
||
|
||
return $this->success([
|
||
'all' => (float)sprintf("%.2f", $all + $teamStatistics['team_total_award']),
|
||
'withdrawal' => $withdrawal,
|
||
'total_promoter' => $totalPromoter,
|
||
'total_merchants' => $totalMerchants,
|
||
'total_partner' => $totalPartner,
|
||
'promoter' => $promoter,
|
||
'merchants' => $merchants,
|
||
'partner' => $partner,
|
||
'team_statistics' => $teamStatistics
|
||
]);
|
||
}
|
||
|
||
|
||
/***
|
||
* 检查分账状态
|
||
* @param $params
|
||
* @return void
|
||
*/
|
||
public function checkAccountsAuth($params = [])
|
||
{
|
||
$accounts = [];
|
||
if (isset($params['out_trade_no'])) {
|
||
$is_divide = cache($params['out_trade_no']);
|
||
if ($is_divide == 1) {
|
||
$accounts = $this->checkAccountState($params);
|
||
} else if ($is_divide != 'no' || empty($is_divide)) { //预留位置多服务器查询
|
||
$order_model = new OrderCommon();
|
||
$order_info = $order_model->getOrderInfo([['site_id', '=', $params['site_id']], ['out_trade_no', '=', $params['out_trade_no']]])['data'];
|
||
$site_id = $params['site_id'];
|
||
$set = (new Setting())->getFinalSet((int)$site_id, 0);
|
||
if ($set['switch'] == 1) {
|
||
$total_rate = $set['integral_rate'] + $set['operations_rate']; //积分托管,营运抽佣总和
|
||
$goods_money = $order_info['goods_money'];
|
||
$partner_rate_proxy = $getFinalSet['partner_rate_proxy'] ?? 0;
|
||
$merchants_rate_proxy = $getFinalSet['merchants_rate_proxy'] ?? 0;
|
||
$promoter_rate_proxy = $getFinalSet['promoter_rate_proxy'] ?? 0;
|
||
if ($partner_rate_proxy) { //合伙人佣金发放
|
||
$total_rate += $getFinalSet['partner_rate'];
|
||
}
|
||
if ($merchants_rate_proxy) {//招商员分佣比例
|
||
$total_rate += $getFinalSet['merchants_rate'];
|
||
}
|
||
if ($promoter_rate_proxy) { //推广员分佣比例
|
||
$total_rate += $getFinalSet['promoter_rate'];
|
||
}
|
||
$commission = $goods_money * $total_rate / 100;
|
||
if ($commission > $order_info['legumes_integral_money']) {//如果拨出佣金大于抵扣直接使用现金分账
|
||
cache($params['out_trade_no'], 1, 7200); //设置分账状态
|
||
$accounts = $this->checkAccountState($params);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return $accounts;
|
||
}
|
||
|
||
|
||
/***
|
||
* 检查分账状态
|
||
* @param $data
|
||
* @return array|string
|
||
*/
|
||
public function checkAccountState($data = [])
|
||
{
|
||
$res = event('getDivideAccounts', $data);
|
||
$divideStatet = true;
|
||
$isDivide = false;
|
||
$is_pay_error = '';
|
||
$Accounts = [];
|
||
if ($res) {
|
||
foreach ($res as $v) {
|
||
foreach ($v as $item) {
|
||
$Accounts[] = $item;
|
||
}
|
||
}
|
||
$is_commission = array_sum(array_column($Accounts, 'is_commission'));
|
||
if ($is_commission <= 0) return '';//剔除免佣账号
|
||
$isDivide = true;
|
||
if ($data['pay_type'] == 'alipay') {//验证支付宝是否开启分账
|
||
$is_pay_error = cache('is_pay_error' . $data['site_id']);
|
||
if (!cache('alipayTradeBatchquery' . $data['site_id']) || $is_pay_error) {
|
||
$micode = new MinCode($data['site_id']);
|
||
$request = $micode->requestApi('alipay.trade.royalty.relation.batchquery', ['out_request_no' => date('YmdHisw')])['alipay_trade_royalty_relation_batchquery_response'];
|
||
if ($request['code'] == 10000) {
|
||
cache('alipayTradeBatchquery' . $data['site_id'], 1, 7200);
|
||
cache('is_pay_error' . $data['site_id'], null);
|
||
} else {
|
||
$divideStatet = false;
|
||
$is_pay_error = '未检开启分账权限';
|
||
}
|
||
}
|
||
}
|
||
$startTime = mktime(0, 0, 0, date('m'), date('d') - 30, date('Y'));
|
||
$where = [
|
||
['site_id', '=', $data['site_id']],
|
||
['states', 'in', [0, 2, 3]],
|
||
['is_order_account_locking', '=', 0],
|
||
['create_time', '<', $startTime],
|
||
];
|
||
$countMone = model('dividemoney_bill')->getSum($where, 'amount');
|
||
if ($countMone >= 50) {
|
||
$divideStatet = false;
|
||
$is_pay_error = '服务佣金未结清';
|
||
}
|
||
}
|
||
return [
|
||
'isDivide' => $isDivide,
|
||
'divideState' => $divideStatet,
|
||
'err_msg' => $is_pay_error,
|
||
];
|
||
}
|
||
|
||
/***
|
||
* 获取分账账号
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
public function getDivideAccounts($params = [], $proxyFee = 0, $reason = '')
|
||
{
|
||
$site_id = $params['site_id'];
|
||
$pay_type = $params['pay_type'];
|
||
$json = [
|
||
['website w', 's.agent_id=w.site_id', 'inner'],
|
||
];
|
||
$field = 's.is_ispfee,s.service_fee,w.site_id,w.web_contacts,w.web_phone,w.settlement_wecaht_mch_id,w.settlement_alipay_account,w.alipay_account_name,w.wecaht_account_name,w.is_divide,w.separate_accounts_value';
|
||
$siteInfo = model('site')->getInfo(['s.site_id' => $site_id], $field, 's', $json);
|
||
$accounts = [];
|
||
if ($siteInfo && $siteInfo['is_divide'] == 1) {
|
||
$getSet = (new Setting())->getFinalSet((int)$site_id, 0);
|
||
$platform = config('accounts');
|
||
$operations_rate = $getSet['operations_rate'];//运营抽佣
|
||
if ($siteInfo['is_ispfee'] == 1) {
|
||
$acc1 = [
|
||
'is_commission' => 1,
|
||
'is_platform' => 1,
|
||
'account_type' => 'technology',
|
||
'agent_id' => $siteInfo['site_id'],
|
||
'realname' => $platform['realname'],
|
||
'mobile' => $platform['mobile'],
|
||
'alipay_account_name' => $platform['alipay']['realname'],
|
||
'settlement_alipay_account' => $platform['alipay']['account'],
|
||
'wecaht_account_name' => $platform['weapp']['realname'],
|
||
'settlement_wecaht_mch_id' => $platform['weapp']['account'],
|
||
'fee_commission' => 0.6,
|
||
'commission_type' => 'sale',
|
||
'separate_accounts' => $platform,
|
||
'reason' => '平台技术服务费0.6%'
|
||
];
|
||
$cisp_fee = $siteInfo['service_fee'] ?? 0;
|
||
if ($cisp_fee > 0) {//如果技术费独立设置
|
||
$acc1['fee_commission'] = $cisp_fee;
|
||
$acc1['reason'] = "平台技术服务费{$cisp_fee}%";
|
||
} else {
|
||
$fee_commission = $platform['default'];
|
||
$service_fee = $operations_rate * $fee_commission / 100;
|
||
$acc1['fee_commission'] = $service_fee;
|
||
$acc1['reason'] = "平台技术服务费{$service_fee}%";
|
||
$operations_rate -= $service_fee;
|
||
}
|
||
$accounts[] = $acc1;
|
||
}
|
||
$integral_rate = $getSet['integral_rate'] ?? 0;
|
||
$acc2 = [
|
||
'is_commission' => 1,
|
||
'account_type' => 'integral',
|
||
'agent_id' => $siteInfo['site_id'],
|
||
'realname' => $siteInfo['wecaht_account_name'],
|
||
'mobile' => $siteInfo['web_phone'],
|
||
'alipay_account_name' => $siteInfo['alipay_account_name'],
|
||
'settlement_alipay_account' => $siteInfo['settlement_alipay_account'],
|
||
'wecaht_account_name' => $siteInfo['wecaht_account_name'],
|
||
'settlement_wecaht_mch_id' => $siteInfo['settlement_wecaht_mch_id'],
|
||
'fee_commission' => $integral_rate + $proxyFee,
|
||
'commission_type' => 'sale',
|
||
'is_platform' => 1,
|
||
'separate_accounts' => json_decode($siteInfo['separate_accounts_value'], true),
|
||
'reason' => "消费托管(含:积分托管{$integral_rate}%运营{$operations_rate}%{$reason})"
|
||
];
|
||
$acc2['fee_commission'] += $operations_rate;
|
||
$accounts[] = $acc2;
|
||
}
|
||
$where = [
|
||
['site_id', '=', $site_id],
|
||
];
|
||
switch ($pay_type) {
|
||
case 'wechatpay':
|
||
$account = array_column($accounts, 'settlement_wecaht_mch_id');
|
||
$where[] = ['channel_type', '=', 'wechatpay'];
|
||
$where[] = ['account', 'in', $account];
|
||
break;
|
||
case 'alipay':
|
||
$account = array_column($accounts, 'settlement_alipay_account');
|
||
$where[] = ['channel_type', '=', 'alipay'];
|
||
$where[] = ['account', 'in', $account];
|
||
break;
|
||
case 'cywechatpay':
|
||
case 'cyalipay':
|
||
$separate_accounts = array_column($accounts, 'separate_accounts');
|
||
$cypay = array_column($separate_accounts, 'cypay');
|
||
$account = array_column($cypay, 'account');
|
||
$where[] = ['channel_type', '=', 'cypay'];
|
||
$where[] = ['account', 'in', $account];
|
||
break;
|
||
default:
|
||
return $accounts;
|
||
}
|
||
$res = model('dividemoney_account')->getColumn($where, '*', 'account');
|
||
$model = new DividemoneyAccount();
|
||
foreach ($accounts as $k => &$v) {
|
||
$account = '';
|
||
$data = [
|
||
'is_system' => 1,
|
||
'is_platform' => 1,
|
||
];
|
||
if ($pay_type == 'wechatpay') {
|
||
$account = $res[$v['settlement_wecaht_mch_id']] ?? [];
|
||
$data['account'] = $v['settlement_wecaht_mch_id'];
|
||
$data['account_type'] = 'MERCHANT_ID';
|
||
$data['channel_type'] = 'wechatpay';
|
||
$data['realname'] = $v['wecaht_account_name'];
|
||
$class = 'BingWechatAccount';
|
||
} else if ($pay_type == 'alipay') {
|
||
$account = $res[$v['settlement_alipay_account']] ?? [];
|
||
$data['account'] = $v['settlement_alipay_account'];
|
||
$data['account_type'] = 'userId';
|
||
$data['channel_type'] = 'alipay';
|
||
$data['realname'] = $v['alipay_account_name'];
|
||
$class = 'BingAlipayAccount';
|
||
} else if ($pay_type == 'cywechatpay' || $pay_type == 'cyalipay') {
|
||
$cypay = $v['separate_accounts']['cypay'];
|
||
$account = $res[$cypay['account']] ?? [];
|
||
$data['account'] = $cypay['account'];
|
||
$data['account_type'] = 'PARTNER';
|
||
$data['channel_type'] = 'cypay';
|
||
$data['realname'] = $cypay['realname'];
|
||
$class = 'BingCyPayAccount';
|
||
}
|
||
if (empty($account) || ($account && $account['states'] != 1)) {
|
||
$bing = $model->$class($site_id, $data);
|
||
if ($bing['code'] >= 0) {
|
||
$v['is_auth'] = 1;
|
||
} else {
|
||
$v['is_auth'] = 0;
|
||
}
|
||
} else if ($account && $account['states'] == 1) {
|
||
$v['is_auth'] = 1;
|
||
}
|
||
}
|
||
return $accounts;
|
||
}
|
||
|
||
public $legumes_integral_community = 0;//积分佣金循环共存
|
||
|
||
/***
|
||
* 获取账单分佣
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
public function getDivideOrderCalculate($params = [])
|
||
{
|
||
$site_id = $params['site_id'];
|
||
$pay_type = $params['pay_type'];
|
||
$goods_id = $params['goods_id'] ?? 0;
|
||
$getFinalSet = (new Setting())->getFinalSet((int)$site_id, $goods_id);
|
||
if ($getFinalSet['switch'] == 1) {
|
||
$total_rate = $getFinalSet['integral_rate'] + $getFinalSet['operations_rate']; //消费积分托管+营运抽佣总和
|
||
$partner_rate_proxy = $getFinalSet['partner_rate_proxy'] ?? 0;
|
||
$merchants_rate_proxy = $getFinalSet['merchants_rate_proxy'] ?? 0;
|
||
$promoter_rate_proxy = $getFinalSet['promoter_rate_proxy'] ?? 0;
|
||
$reason = '';
|
||
$proxy = 0;
|
||
if ($partner_rate_proxy) { //合伙人佣金发放
|
||
$total_rate += $getFinalSet['partner_rate'];
|
||
$proxy += $getFinalSet['partner_rate'];
|
||
$reason = "合伙人佣金{$getFinalSet['partner_rate']}%";
|
||
}
|
||
if ($merchants_rate_proxy) {//招商员分佣比例
|
||
$total_rate += $getFinalSet['merchants_rate'];
|
||
$proxy += $getFinalSet['merchants_rate'];
|
||
$reason .= "招商佣金{$getFinalSet['merchants_rate']}%";
|
||
}
|
||
if ($promoter_rate_proxy) { //推广员分佣比例
|
||
$total_rate += $getFinalSet['promoter_rate'];
|
||
$proxy += $getFinalSet['promoter_rate'];
|
||
$reason .= "推广佣金{$getFinalSet['promoter_rate']}%";
|
||
}
|
||
$goods_money = $params['goods_money'];
|
||
$commission = $goods_money * $total_rate / 100; //佣金
|
||
$AccInfo = $this->getDivideAccounts($params, $proxy, $reason);
|
||
$accArr = [];
|
||
switch ($pay_type) {
|
||
case 'cyalipay':
|
||
case 'cywechatpay':
|
||
$account_type = 'PARTNER';
|
||
break;
|
||
case 'wechatpay':
|
||
$account_type = 'MERCHANT_ID';
|
||
break;
|
||
case 'alipay':
|
||
$account_type = 'userId';
|
||
break;
|
||
default:
|
||
$account_type = 'Integral';
|
||
break;
|
||
}
|
||
if ($pay_type != 'BALANCE' && $params['legumes_integral_money'] == 0) {
|
||
//如果未使用积分抵扣在线支付
|
||
foreach ($AccInfo as $k => $v) {
|
||
$v['account_type'] = $account_type;
|
||
$arr = $this->getMoneyArr($params, $v, $goods_money, $v['fee_commission'], 1, $v['reason']);
|
||
if ($arr) {
|
||
$accArr[] = $arr;
|
||
}
|
||
}
|
||
} else if ($pay_type == 'BALANCE' || ($params['legumes_integral_money'] && $params['legumes_integral_money'] >= $commission)) {
|
||
//如果抵扣积分大于佣金或使用余额直接使用积分抵扣
|
||
foreach ($AccInfo as $k => $v) {
|
||
$v['account_type'] = 'Integral';
|
||
$arr = $this->getMoneyArr($params, $v, $goods_money, $v['fee_commission'], 1, $v['reason']);
|
||
if ($arr) {
|
||
$accArr[] = $arr;
|
||
}
|
||
}
|
||
$amount = array_sum(array_column($accArr, 'amount'));
|
||
$this->legumes_integral_community += $amount;
|
||
// model('order')->update(['legumes_integral_community' => Db::raw('legumes_integral_community +' . $amount)], ['order_id' => $params['order_id']]);
|
||
} else {//使用积分+支付
|
||
$use_order_money = $params['order_money'] * 0.3;//最大分账率
|
||
$legumes_integral_money = $params['legumes_integral_money'];//使用积分抵扣金额
|
||
$legumes_integral_community = 0; //积分抽佣
|
||
//测试示例
|
||
// $commission = 5;
|
||
// $legumes_integral_money = 0.8;
|
||
// $use_order_money = 2;
|
||
foreach ($AccInfo as $k => $v) {
|
||
$getMoney = $this->getMoneyArr($params, $v, $goods_money, $v['fee_commission'], 1, $v['reason']);
|
||
if (empty($getMoney)) continue;
|
||
if ($pay_type != 'BALANCE' && $commission <= $use_order_money) { //如果佣金大于可抽佣金额直接抽现金
|
||
$getMoney['account_type'] = $account_type;
|
||
$accArr[] = $getMoney;
|
||
} else if ($pay_type == 'BALANCE') { //如果使用余额支付积分抵扣,可能积分是负数,在未结算总额扣除
|
||
$v['account_type'] = 'Integral';
|
||
$accArr[] = $this->getMoneyArr($params, $v, $goods_money, $v['fee_commission'], 1, $v['reason']);
|
||
} else if ($legumes_integral_money) { //如果使用积分抵扣大于佣金
|
||
if ($getMoney['amount'] <= $legumes_integral_money) { //如果积分抵扣大于佣金
|
||
$getMoney['account_type'] = 'Integral';
|
||
$legumes_integral_money -= $getMoney['amount'];
|
||
$legumes_integral_community += $getMoney['amount'];
|
||
$accArr[] = $getMoney;
|
||
} else {
|
||
$amount = $getMoney['amount'];
|
||
if ($amount > $legumes_integral_money) { //抵扣剩余积分
|
||
$amount -= $legumes_integral_money;
|
||
$getMoney['account_type'] = 'Integral';
|
||
$getMoney['amount'] = $legumes_integral_money;
|
||
$legumes_integral_community += $legumes_integral_money;
|
||
$legumes_integral_money = 0;
|
||
$accArr[] = $getMoney;
|
||
}
|
||
if ($amount > 0) { //扣除支付分账
|
||
$getMoney['account_type'] = $account_type;
|
||
$getMoney['amount'] = $amount;
|
||
$use_order_money -= $amount;
|
||
$accArr[] = $getMoney;
|
||
}
|
||
}
|
||
} else if ($pay_type != 'BALANCE' && $use_order_money) {
|
||
$amount = $getMoney['amount'];
|
||
if ($use_order_money >= $amount) {
|
||
$getMoney['account_type'] = $account_type;
|
||
$getMoney['amount'] = $amount;
|
||
$accArr[] = $getMoney;
|
||
} else if ($use_order_money < $amount) {
|
||
if ($use_order_money > 0) {
|
||
$getMoney['account_type'] = $account_type;
|
||
$getMoney['amount'] = $use_order_money;
|
||
$accArr[] = $getMoney;
|
||
$amount -= $use_order_money;
|
||
$use_order_money = 0;
|
||
}
|
||
if ($amount > 0) {
|
||
$getMoney['account_type'] = 'Integral';
|
||
$getMoney['amount'] = $amount;
|
||
$legumes_integral_community += $amount;
|
||
$accArr[] = $getMoney;
|
||
}
|
||
}
|
||
} else { //理论上不会出现
|
||
$getMoney['account_type'] = 'Integral';
|
||
$legumes_integral_community += $getMoney['amount'];
|
||
$accArr[] = $getMoney;
|
||
}
|
||
}
|
||
$this->legumes_integral_community += $legumes_integral_community;//记录循环值防止重复计算
|
||
}
|
||
if ($this->legumes_integral_community > 0) {//防止重复计算
|
||
model('order')->update(['legumes_integral_community' => $this->legumes_integral_community], ['order_id' => $params['order_id']]);
|
||
}
|
||
}
|
||
//如果拨出佣金大于抵扣直接使用现金分账
|
||
return $accArr;
|
||
}
|
||
|
||
|
||
/***
|
||
* 整理数据
|
||
* @param $data
|
||
* @param $accIfno
|
||
* @param $price
|
||
* @param $fee_commission
|
||
* @param $platform_type
|
||
* @param $reason
|
||
* @return array
|
||
*/
|
||
private function getMoneyArr($data, $accIfno, $price, $fee_commission, $platform_type = 2, $reason = '服务商服务费')
|
||
{
|
||
if (isset($accIfno['commission_type']) && $accIfno['commission_type'] == 'fixfee') {
|
||
$money = $fee_commission;
|
||
} else {
|
||
$money = $price * $fee_commission / 100;
|
||
if ($money < 0.01) {
|
||
$money = 0.01;
|
||
}
|
||
}
|
||
//判断是否按产品抽佣
|
||
$ag_site_id = $data['agent_id'] ?? 0;
|
||
$acc = [
|
||
'site_id' => $data['site_id'],
|
||
'ag_site_id' => $ag_site_id,
|
||
'is_platform' => $platform_type,
|
||
'order_id' => $data['order_id'],
|
||
'member_id' => $data['member_id'],
|
||
'out_trade_no' => $data['out_trade_no'],
|
||
'trade_no' => $data['trade_no'] ?? '', //订单号
|
||
'is_video_number' => $data['is_video_number'] ?: 0,
|
||
'fee_commission' => $fee_commission,
|
||
'pay_money' => $price,//支付金额,
|
||
'amount' => $money,
|
||
'account_type' => $accIfno['account_type'],
|
||
'channel_type' => $data['pay_type'],
|
||
'promotion_type' => $data['promotion_type'],
|
||
'reason' => $reason
|
||
];
|
||
switch ($data['pay_type']) {
|
||
case 'weapp':
|
||
case 'wechat':
|
||
case 'wechatpay':
|
||
$acc['channel_type'] = 'wechatpay';
|
||
$acc['realname'] = $accIfno['wecaht_account_name'];
|
||
$acc['account'] = $accIfno['settlement_wecaht_mch_id'];
|
||
break;
|
||
case 'alipay':
|
||
case 'aliapp':
|
||
case 'zmxxpay':
|
||
case 'huabie':
|
||
case 'zmgopay':
|
||
$acc['channel_type'] = 'alipay';
|
||
$acc['realname'] = $accIfno['alipay_account_name'];
|
||
$acc['account'] = $accIfno['settlement_alipay_account'];
|
||
break;
|
||
case 'cywechatpay':
|
||
case 'cyalipay':
|
||
$accounts = $accIfno['separate_accounts']['cypay'];
|
||
$acc['realname'] = $accounts['realname'];
|
||
$acc['account'] = $accounts['account'];
|
||
$acc['channel_type'] = 'cypay';
|
||
break;
|
||
case 'hfwechatpay':
|
||
case 'hfalipay':
|
||
$accounts = $accIfno['separate_accounts']['hfpay'];
|
||
$acc['realname'] = $accounts['realname'];
|
||
$acc['account'] = $accounts['account'];
|
||
$acc['channel_type'] = 'hfpay';
|
||
break;
|
||
case 'BALANCE':
|
||
$acc['channel_type'] = 'BALANCE';
|
||
$acc['realname'] = $accIfno['realname'];
|
||
$acc['account'] = $accIfno['settlement_wecaht_mch_id'];
|
||
break;
|
||
default:
|
||
$accounts = $accIfno['separate_accounts']['Integral'];
|
||
$acc['realname'] = $accounts['realname'];
|
||
$acc['account'] = $accounts['account'];
|
||
$acc['channel_type'] = 'Integral';
|
||
}
|
||
$account_type = $acc['account_type'];
|
||
if ($account_type == 'Integral') {
|
||
$accounts = $accIfno['separate_accounts']['Integral'];
|
||
$acc['realname'] = $accounts['realname'];
|
||
$acc['account'] = $accounts['account'];
|
||
$acc['channel_type'] = 'Integral';
|
||
}
|
||
if ($money <= 0 || !$acc['account'] || !$acc['realname']) { //未设置账号不分佣
|
||
$acc = [];
|
||
}
|
||
return $acc;
|
||
}
|
||
|
||
/***
|
||
* 设置标记是否该订单参与现金分账
|
||
* @param $params
|
||
* @return void
|
||
*/
|
||
public function setCheckAccountsDivide($params = [])
|
||
{
|
||
$order_id = $params['order_id'];
|
||
$order_model = new OrderCommon();
|
||
$order_info = $order_model->getOrderInfo([['order_id', '=', $order_id]])['data'];
|
||
$site_id = $params['site_id'];
|
||
$set = (new Setting())->getFinalSet((int)$site_id, 0);
|
||
if ($set['switch'] == 1) {
|
||
$total_rate = $set['integral_rate'] + $set['operations_rate']; //积分托管,营运抽佣总和
|
||
$goods_money = $order_info['goods_money'];
|
||
$commission = $goods_money * $total_rate / 100;
|
||
if ($commission > $order_info['legumes_integral_money']) {//如果拨出佣金大于抵扣直接使用现金分账
|
||
cache($order_info['out_trade_no'], 1, 7200); //设置分账状态
|
||
} else {
|
||
cache($order_info['out_trade_no'], 'no', 7200); //设置分账状态
|
||
}
|
||
}
|
||
return $this->success();
|
||
}
|
||
}
|