208 行
10 KiB
PHP
可执行文件
208 行
10 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Distribution\Business;
|
|
|
|
use Distribution\Model\ConfigModel;
|
|
use Distribution\Model\OrderModel;
|
|
use Distribution\Model\DistributorModel;
|
|
use Distribution\Model\SellModel;
|
|
use Distribution\Model\StoreModel;
|
|
|
|
/**
|
|
* 佣金计算
|
|
* Class Commission
|
|
* @package Distribution\Business
|
|
*/
|
|
class Commission
|
|
{
|
|
/**
|
|
* 订单分佣计算
|
|
* @param $param
|
|
* merchant_id 商户ID
|
|
* bill_id 订单ID
|
|
* goods_id 商品ID
|
|
* goods_info 商品详细信息
|
|
* uid 购买商品的用户ID
|
|
* distributor_id 分享者的分销员ID 非必传
|
|
* @return mixed
|
|
*/
|
|
public function calculateCommission($param)
|
|
{
|
|
$SellModel = new SellModel();
|
|
$OrderModel = new OrderModel();
|
|
$StoreModel = new StoreModel();
|
|
$ConfigModel = new ConfigModel();
|
|
$DisLinkModel = new \Distribution\Model\LinkModel();
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
if (!$param['merchant_id'] || !$param['bill_id'] || empty($param['goods_id']) || empty($param['goods_info']) || !$param['uid']) {
|
|
return ['status' => 0, 'msg' => '必要参数缺失'];
|
|
}
|
|
// 验证bill是否已经记录
|
|
$verifyOrder = $OrderModel->getFind(['bill_id' => $param['bill_id']], 'id');
|
|
if ($verifyOrder) return ['status' => 0, 'msg' => '订单号已记录,请勿重复添加'];
|
|
|
|
// 获取分销配置
|
|
$configField = [
|
|
'is_open', // 是否开启分销 0关闭 1开启
|
|
'distribution_level', // 分销层级 1一级分销 2二级分销
|
|
'second_level_rate', // 二级佣金比
|
|
'is_self_buying', // 分销自购 0不可以获得佣金 1可以获得佣金
|
|
'is_customer_protect', //是否开启客户保护 1关闭 2开启
|
|
'customer_protect_type', //客户保护方式 1永久保护 2固定时间保护
|
|
'protect_date', //固定保护天数
|
|
];
|
|
$config = $ConfigModel->getFind(['merchant_id' => $param['merchant_id']], $configField);
|
|
if (empty($config) || $config['is_open'] == 0) return ['status' => 0, 'msg' => '商户未开启分销'];
|
|
|
|
// 获取商品信息--分销商品表ID
|
|
$gMap = [
|
|
'merchant_id' => $param['merchant_id'],
|
|
'goods_id' => ['in', $param['goods_id']],
|
|
'goods_status' => 1
|
|
];
|
|
$dis_goods = $StoreModel->getSelect($gMap, 'id,goods_id,first_proportion,second_proportion');
|
|
//判断购买的商品是否含有分销商品
|
|
if (empty($dis_goods)) return ['status' => 0, 'msg' => '不属于分销订单'];
|
|
|
|
//循环处理分销商品数据 便于插入分销商品出售明细表
|
|
$disGoods = []; //存储以商品ID为键名,商品分销设置为键值的分销商品数据
|
|
foreach ($dis_goods as $v) {
|
|
$disGoods[$v['goods_id']] = $v;
|
|
}
|
|
|
|
//查询购买者的分销员ID
|
|
$self_distributor_id = $DistributorModel->getFindField(['uid' => $param['uid'], 'status' => 0], 'id');
|
|
|
|
//请求参数无分销员ID或者用户自购
|
|
if (empty($param['distributor_id']) || $self_distributor_id || $self_distributor_id == $param['distributor_id']) {
|
|
//判断购买的用户是否是分销员 不存在分销员信息则为普通用户购买 获取是否属于某个分销员的客户
|
|
if (empty($self_distributor_id)) {
|
|
$linkData = $DisLinkModel->getOrderFind(['uid' => $param['uid'], 'status' => 1], '*', 'add_time desc');
|
|
|
|
//不存在绑定关系
|
|
if (empty($linkData)) return ['status' => 0, 'msg' => '不属于分销订单'];
|
|
|
|
//存在绑定关系 则获得一级佣金的分销员为绑定的分销员
|
|
$first_distributor_id = $DistributorModel->getFindField(['id' => $linkData['distributor_id'], 'status' => 0], 'id');
|
|
|
|
//绑定的分销员不正常 则return
|
|
if (empty($first_distributor_id)) return ['status' => 0, 'msg' => '不属于分销订单'];
|
|
} else if ($config['is_self_buying'] != 1) {
|
|
//自购不可以获得佣金 获取上级分销员的ID作为一级分销员
|
|
$first_distributor_id = $DistributorModel->getUserLeader($param['merchant_id'], $self_distributor_id);
|
|
|
|
//不存在上级
|
|
if (!$first_distributor_id) return ['status' => 0, 'msg' => '不属于分销订单'];
|
|
} else {
|
|
//自购能获得佣金
|
|
$first_distributor_id = $self_distributor_id;
|
|
}
|
|
} else {
|
|
//请求参数有分销员ID 并且购买的用户不是分销员
|
|
//获取是否存在绑定关系
|
|
$linkData = $DisLinkModel->getOrderFind(['uid' => $param['uid'], 'status' => 1], '*', 'add_time desc');
|
|
|
|
//不存在绑定关系或者绑定的分销员就是分享者 则分享者拿一级佣金
|
|
if (empty($linkData) || $linkData['distributor_id'] == $param['distributor_id']) {
|
|
$first_distributor_id = $param['distributor_id']; //获得一级佣金的分销员ID
|
|
} else {
|
|
//存在绑定关系并且绑定的分销员不是分享者
|
|
//开启客户保护 并且永久保护或固定时间段保护且在保护期内 则绑定的分销员拿佣金
|
|
if ($config['is_customer_protect'] == 2 && ($config['customer_protect_type'] == 1 || $linkData['protect_date'] * 86400 + $linkData['add_time'] > time())) {
|
|
|
|
//判断绑定的分销员是否正常
|
|
$first_distributor_id = $DistributorModel->getFindField(['id' => $linkData['distributor_id'], 'status' => 0], 'id');
|
|
if (empty($first_distributor_id)) {
|
|
//不正常则分享者拿佣金
|
|
$first_distributor_id = $param['distributor_id']; //获得一级佣金的分销员ID
|
|
}
|
|
} else {
|
|
//未开启客户保护或过了有效期 分享者拿佣金
|
|
$first_distributor_id = $param['distributor_id']; //获得一级佣金的分销员ID
|
|
}
|
|
}
|
|
//拿一级佣金的是分享者
|
|
if ($first_distributor_id == $param['distributor_id']) {
|
|
//判断分享者是否正常 不正常则return
|
|
$first_distributor_id = $DistributorModel->getFindField(['id' => $param['distributor_id'], 'status' => 0], 'id');
|
|
if (empty($first_distributor_id)) return ['status' => 0, 'msg' => '分享者状态异常,不能拿佣金'];
|
|
}
|
|
}
|
|
//获取一级佣金比
|
|
$first_rate = $DistributorModel->getUserPresent($first_distributor_id);
|
|
|
|
//判断是否应该含有上级
|
|
if ($config['distribution_level'] == 1) {
|
|
//一级分销 上级不能拿佣金
|
|
$lastData['pid'] = 0;
|
|
} else {
|
|
//二级分销 存在上级则上级拿佣金
|
|
$lastData['pid'] = $DistributorModel->getUserLeader($param['merchant_id'], $first_distributor_id);
|
|
}
|
|
|
|
//分销一级佣金
|
|
$totalFirMoney = 0;
|
|
//分销二级佣金
|
|
$totalSecMoney = 0;
|
|
//循环处理分销商品数据
|
|
foreach ($param['goods_info'] as $val) {
|
|
//当前商品属于分销商品
|
|
if (isset($disGoods[$val['goods_id']])) {
|
|
$firstProp = $disGoods[$val['goods_id']]['first_proportion'] ? $disGoods[$val['goods_id']]['first_proportion'] : $first_rate;
|
|
$secondProp = $disGoods[$val['goods_id']]['second_proportion'] ? $disGoods[$val['goods_id']]['second_proportion'] : $config['second_level_rate'];
|
|
$newSellAddData = [
|
|
'bill_id' => $param['bill_id'], //订单表ID
|
|
'sell_id' => $val['sell_id'], //retail_sell表ID
|
|
'total_amount' => $val['total_amount'], //总价
|
|
'dis_goods_id' => $disGoods[$val['goods_id']]['id'], //分销商品ID
|
|
'sell_first_money' => bcmul($val['total_amount'], $firstProp, 2), //订单一级佣金
|
|
'first_balance_rate' => $firstProp, //一级佣金比
|
|
'sell_second_money' => 0,
|
|
];
|
|
$totalFirMoney += $newSellAddData['sell_first_money'];
|
|
|
|
//开启二级分销且存在二级分销员
|
|
if ($config['distribution_level'] == 2 && $lastData['pid']) {
|
|
$newSellAddData['sell_second_money'] = bcmul($val['total_amount'], $secondProp, 2); //订单二级佣金
|
|
$newSellAddData['second_balance_rate'] = $secondProp; //二级佣金比
|
|
$totalSecMoney += $newSellAddData['sell_second_money'];
|
|
}
|
|
// 存在佣金才添加分销订单
|
|
if (bcadd($newSellAddData['sell_first_money'], $newSellAddData['sell_second_money'], 2) > 0) {
|
|
$SellModel->addOrder($newSellAddData);
|
|
}
|
|
}
|
|
}
|
|
if (bcadd($totalFirMoney, $totalSecMoney, 2) > 0) {
|
|
//如果分销商品自身存在一级佣金比
|
|
$lastData['did'] = $first_distributor_id;
|
|
$lastData['sell_first_money'] = $totalFirMoney;
|
|
|
|
//开启二级分销并且存在上级分销员
|
|
if ($config['distribution_level'] == 2 && $lastData['pid']) {
|
|
$lastData['sell_second_money'] = $totalSecMoney;
|
|
}
|
|
// 数据存储
|
|
$lastData['bill_id'] = $param['bill_id'];
|
|
$lastData['withdraw_state'] = 0;
|
|
$result = $OrderModel->addOrder($lastData);
|
|
|
|
if ($result) {
|
|
//更新已插入的商品列表记录
|
|
$SellModel->updateOrder(['bill_id' => $param['bill_id']], ['settled_id' => $result]);
|
|
$res['status'] = 1;
|
|
$res['msg'] = '佣金记录成功!';
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '佣金记录失败,请稍后重试!';
|
|
}
|
|
} else {
|
|
$res = [
|
|
'status' => 0,
|
|
'msg' => '不是分销订单',
|
|
];
|
|
}
|
|
return $res;
|
|
}
|
|
} |