master
wangxinglong 2022-03-15 18:51:28 +08:00
parent cb93ebc368
commit c107b72f43
4 changed files with 293 additions and 90 deletions

View File

@ -539,7 +539,11 @@ class Coupon extends Base
}
$business = BusinessRepository::getInstance()->getModel()->with(["agency"])->where(["code"=>$coupon->couponMain->business_code])->lock(true)->find();
$business = BusinessRepository::getInstance()->getModel()
->with(["agency"])
->where(["code"=>$coupon->couponMain->business_code])
->lock(true)
->find();
if(empty($business)){
return $this->json(4001, "商家不存在");
}
@ -552,6 +556,39 @@ class Coupon extends Base
return $this->json(4001, "商家余额不足");
}
$deductionMoney = $coupon->couponMain->deduction_money;
//可分配金额 如果是普通商家
if($business->model == BusinessModel::model_ordinary) {
$agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency);
$adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin);
$consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer);
$agencyMoney = round($agencyMoney,2); //四舍五入 精确到分
$adminMoney = round($adminMoney,2); //四舍五入 精确到分
$consumerMoney = round($consumerMoney,2); //四舍五入 精确到分
//开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款
if($coupon->couponMain->is_is_distribution == CouponMain::COMMON_ON){
$disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin);
//$disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution);
$disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed);
//渠道商就没得钱
$agencyMoney = 0 ;
$adminMoney = round($disAdminMoney,2); //四舍五入 精确到分
//$disDistributionMoney = round($disDistributionMoney,2); //四舍五入 精确到分
$consumerMoney = round($disDistributedMoney,2); //四舍五入 精确到分
}
}else{
$adminMoney = 0;
$consumerMoney = round($deductionMoney,2) ;
$agencyMoney = 0; //四舍五入 精确到分
}
//开始数据操作
Db::startTrans();
try {
@ -566,27 +603,7 @@ class Coupon extends Base
"using_count" => Db::raw("using_count - 1"),//进行中数量-1
]);
$deductionMoney = $coupon->couponMain->deduction_money;
//可分配金额 如果是普通商家
if($business->model == BusinessModel::model_ordinary) {
$agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency);
$adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin);
$consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer);
$disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin);
$disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution);
$disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed);
$agencyMoney = round($agencyMoney,2); //四舍五入 精确到分
$adminMoney = round($adminMoney,2); //四舍五入 精确到分
$consumerMoney = round($consumerMoney,2); //四舍五入 精确到分
}else{
$adminMoney = 0;
$consumerMoney = round($deductionMoney,2) ;
$agencyMoney = 0; //四舍五入 精确到分
}
// 2. 写入优惠券流水
$couponBillData = [
@ -626,15 +643,81 @@ class Coupon extends Base
//4. 商家扣钱
$business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]);
//5. 渠道商加钱
if(isset($business->agency) && $business->agency){
$business->agency->inc("balance",$agencyMoney)->update();
$payment = WechatPay::getInstance();
//5. 渠道商加钱 【关闭了分销 渠道商才能加钱 红包方式直接打款 成功失败不管】
if($coupon->couponMain->is_is_distribution == CouponMain::COMMON_OFF){
if(isset($business->agency) && $business->agency){
//$business->agency->inc("balance",$agencyMoney)->update();
//6. 用户提现到零钱 写入红包记录
if($agencyMoney > 0) {
//找到渠道商关联的用户
$agencyUser = Account::findOne([
["business_code","=",$business->agency->code],
["type","=",Account::type_business],
]);
if(empty($agencyUser)){
Db::rollback();
return $this->json(4001,"渠道商用户不存在");
}
$agencyRedpack = Redpack::getDistributionRedpack($agencyUser->user_code,$coupon->couponMain->id,$account->user_code);
//如果付款过一次了 就用同一个订单号发起
if(!empty($agencyRedpack)){
$mch_billno = $agencyRedpack->mch_billno;
}else{
$mch_billno = createUuid();
}
$agencyAmount = $agencyMoney * 100;
$agencyToBalanceData = [
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
'openid' => $agencyUser->open_id,
'check_name' => 'NO_CHECK',// NO_CHECK不校验真实姓名, FORCE_CHECK强校验真实姓名
'amount' => $agencyAmount * 100, //单位为分不小于100
'desc' => '用户核销优惠券,渠道商奖励',
];
// 写入红包
$agencyRedpackData = [
"coupon_id" => 0,
"mch_billno" => $mch_billno,
"openid" => $agencyUser->open_id,
"user_code" => $agencyUser->user_code,
"money" => $agencyAmount,
"create_time" => date("Y-m-d H:i:s",$time),
"pay_time" => '0000-00-00 00:00:00',
"user_type" => Redpack::userTypeAgency,
"distributed_user_code" => $account->user_code,
"distributed_coupon_main_id" => $coupon->couponMain->id,
];
//发起支付
$agencyResult = $payment->transfer->toBalance($agencyToBalanceData);
//var_dump($result);
//付款成功才提交!!!!!伪装成功
if( isset($agencyResult['payment_no']) ){
$agencyRedpackData['pay_time'] = date("Y-m-d H:i:s",$time);
}else{
//否则失败 记录失败原因
Log::info("企业发起付款【分销者红包】失败:" . json_encode($agencyResult,JSON_UNESCAPED_UNICODE));
}
// 不管成功还是失败 都要写入
Redpack::create($agencyRedpackData);
}
}
}
//6. 用户提现到零钱 写入红包记录
if($consumerMoney > 0){
$payment = WechatPay::getInstance();
//如果付款过一次了 就查询付款状态
if(isset($coupon->redpack) && $coupon->redpack){
$mch_billno = $coupon->redpack->mch_billno;
@ -662,7 +745,7 @@ class Coupon extends Base
'openid' => $account->open_id,
'check_name' => 'NO_CHECK',// NO_CHECK不校验真实姓名, FORCE_CHECK强校验真实姓名
'amount' => $amount, //单位为分不小于100
'desc' => '验证优惠券签到',
'desc' => '验证优惠券签到奖励',
];
// 写入红包
@ -783,7 +866,14 @@ class Coupon extends Base
"status" => CouponMain::status_on,
"on_shelf" => CouponMain::on_shelf_off,//默认下架 后台审核上架
"on_screen" => CouponMain::on_screen_yes,
"is_distribution" => CouponMain::COMMON_OFF,//默认关闭分销
"is_distribution" =>
//根据是否指派了渠道商 判断是否开启分销 指派了渠道商就不能开启分销
($account->business->is_assign == BusinessModel::COMMON_ON)
?
CouponMain::COMMON_OFF
:
CouponMain::COMMON_ON,
//下面是分配比例
"commission_agency" => $distributionProportion['agency'],
@ -829,12 +919,44 @@ class Coupon extends Base
$couponMain['deduction_money'] = floor($couponMain['deduction_money'] * 100) / 100;
$totalMoney = $couponMain['deduction_money'] * $couponMain['count'];
//检测消费者部分是否最少0.3元
$consumerMoney = ($couponMain['commission_consumer']/100) * $couponMain['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002, "扣除金额最低" . (CouponMain::min_redpack_money * (100 / ($data['commission_consumer']))) . "");
//如果开启了分销
if($couponMain["is_distribution"] == CouponMain::COMMON_ON){
//检测 【被分销者】部分是否最少0.3元
$distributedMoney = ($couponMain['commission_dis_distributed'] / 100) * $couponMain['deduction_money'];
if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"扣除金额最低"
. (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distributed'])))
. "");
}
//检测 【分销者】部分是否最少0.3元
$distributionMoney = ($couponMain['commission_dis_distribution'] / 100) * $couponMain['deduction_money'];
if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"扣除金额最低"
. (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution'])))
. "");
}
}else{
//检测 【消费者】部分是否最少0.3元
$consumerMoney = ($couponMain['commission_consumer']/100) * $couponMain['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"扣除金额最低"
. (CouponMain::min_redpack_money * (100 / ($data['commission_consumer'])))
. "");
}
//检测 【渠道商】部分是否最少0.3元
$agencyMoney = ($couponMain['commission_agency']/100) * $couponMain['deduction_money'];
if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"扣除金额最低"
. (CouponMain::min_redpack_money * (100 / ($data['commission_agency'])))
. "");
}
}
//未领取的优惠券
$NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($account->business["code"]);
if($account->business["model"] == BusinessModel::model_ordinary) {

View File

@ -243,41 +243,58 @@ class Coupon extends Base
//保留两位小数
$data['deduction_money'] = floor($data['deduction_money'] * 100) / 100;
//检测消费者部分是否最少0.3元
$consumerMoney = ($data['commission_consumer']/100) * $data['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"【分销者】红包不足"
.CouponMain::min_redpack_money
.";当前比例扣除金额最低"
. (CouponMain::min_redpack_money * (100 / ($data['commission_consumer'])))
. "");
}
//如果开启分销 添加操作 处理扣除金额
if($data["is_distribution"] == CouponMain::COMMON_ON ){
if($data['commission_dis_distribution']>$data['commission_dis_distributed']){
//检测【分销者】部分是否最少0.3元
$distributionMoney = ($data['commission_dis_distribution']/100) * $data['deduction_money'];
if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销后:【分销者】红包不足"
.CouponMain::min_redpack_money
.",扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution'])))
. "");
}
}else{
//检测【被分销者】部分是否最少0.3元
$distributedMoney = ($data['commission_dis_distributed']/100) * $data['deduction_money'];
if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销后:【被分销者】红包不足"
.CouponMain::min_redpack_money
.",扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution'])))
. "");
}
if($business->is_assign == BusinessModel::COMMON_ON){
return $this->json(5002, "商家已经指派了渠道商,不能开启分销");
}
//检测【分销者】部分是否最少0.3元
$distributionMoney = ($data['commission_dis_distribution'] / 100) * $data['deduction_money'];
if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销:【分销者】红包不足"
. CouponMain::min_redpack_money
. "元,扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution'])))
. "");
}
//检测【被分销者】部分是否最少0.3元
$distributedMoney = ($data['commission_dis_distributed'] / 100) * $data['deduction_money'];
if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销:【被分销者】红包不足"
. CouponMain::min_redpack_money
. "元,扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_dis_distributed'])))
. "");
}
}else{
if($business->is_assign == BusinessModel::COMMON_OFF){
return $this->json(5002, "商家没有指派渠道商,不能关闭分销");
}
//检测【渠道商】部分是否最少0.3元
$agencyMoney = ($data['commission_agency'] / 100) * $data['deduction_money'];
if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"关闭分销:【渠道商】红包不足"
. CouponMain::min_redpack_money
. "元,扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_agency'])))
. "");
}
//检测【消费者】部分是否最少0.3元
$consumerMoney = ($data['commission_consumer'] / 100) * $data['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"关闭分销:【消费者】红包不足"
. CouponMain::min_redpack_money
. "元,扣除金额最低" .
(CouponMain::min_redpack_money * (100 / ($data['commission_consumer'])))
. "");
}
}
@ -294,8 +311,6 @@ class Coupon extends Base
}
$date = date("Y-m-d H:i:s");
$data['create_time'] = $date;
$data['update_time'] = $date;
@ -340,6 +355,9 @@ class Coupon extends Base
if (empty($couponMain)) {
return $this->json(4001, "优惠券不存在");
}
if (!isset($couponMain->business) || empty($couponMain->business)) {
return $this->json(4001, "优惠券所属商家不存在");
}
$validate = new CouponRelease();
if (!$validate->scene("edit")->check($data)) {
return $this->json(4001, $validate->getError());
@ -382,22 +400,24 @@ class Coupon extends Base
if ($totalCommission != 100) {
return $this->json(5002, "分配比例总和不等于100");
}
$totalCommissionDis = $data['commission_dis_admin'] + $data['commission_dis_distribution'] + $data['commission_dis_distributed'];
if ($totalCommissionDis != 100) {
return $this->json(5002, "(开启分销)分配比例总和不等于100");
}
//如果开启分销 添加操作 处理扣除金额
//如果开启分销
if($data["is_distribution"] == CouponMain::COMMON_ON ){
if($business['is_assign'] == BusinessModel::COMMON_ON){
return $this->json(5002, "商家已经指派了渠道商,不能开启分销");
}
//检测【分销者】部分是否最少0.3元
$distributionMoney = ($data['commission_dis_distribution']/100) * $data['deduction_money'];
if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销:【分销者】红包不足"
"开启分销:【分销者】红包不足"
.CouponMain::min_redpack_money
.",(领券)分销者占比最低" .
( round((CouponMain::min_redpack_money / ($data['deduction_money']) )*100,2))
.",(领券)分销者占比最低" .
(round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2))
. "%"
);
}
@ -406,10 +426,35 @@ class Coupon extends Base
$distributedMoney = ($data['commission_dis_distributed']/100) * $data['deduction_money'];
if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"开启分销:【被分销者】红包不足"
"开启分销:【被分销者】红包不足"
.CouponMain::min_redpack_money
.",(核销)被分销者占比最低" .
( round((CouponMain::min_redpack_money / ($data['deduction_money']) )*100,2))
."元,(核销)被分销者占比最低" .
(round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2))
. "%");
}
}else{
if($business['is_assign'] == BusinessModel::COMMON_OFF){
return $this->json(5002, "商家没有指派渠道商,不能关闭分销");
}
//检测 【消费者】部分是否最少0.3元
$consumerMoney = ($couponMain['commission_consumer']/100) * $couponMain['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"关闭分销:【消费者】红包不足"
.CouponMain::min_redpack_money
."元,消费者占比最低" .
(round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2))
. "%");
}
//检测 【渠道商】部分是否最少0.3元
$agencyMoney = ($couponMain['commission_agency']/100) * $couponMain['deduction_money'];
if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"关闭分销:【渠道商】红包不足"
.CouponMain::min_redpack_money
."元,渠道商占比最低" .
(round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2))
. "%");
}
@ -494,21 +539,28 @@ class Coupon extends Base
{
$ids = input("ids/a", []);
$isDistribution = input("is_distribution/d");
//如果是开启分销操作
if ($isDistribution == CouponMain::COMMON_ON) {
$couponMains = CouponMain::findList([["id", "in", $ids]], [],1,0);
foreach ($couponMains["list"] as $key=> $item) {
//如果开启分销 添加操作 处理扣除金额
if ($item["is_distribution"] == CouponMain::COMMON_OFF) {
$couponMains = CouponMain::findList([["id", "in", $ids]], [],1,0,function ($q){
return $q->with("business");
});
foreach ($couponMains["list"] as $key=> $item) {
if (!isset($item->business) || empty($item->business)) {
return $this->json(4001,"{$item["name"]}】所属商家不存在");
}
//如果开启分销操作
if ($isDistribution == CouponMain::COMMON_ON) {
if($item->business->is_assign == BusinessModel::COMMON_ON){
return $this->json(4001,
"{$item["name"]}】所属商家【{$item->business->business_name}】已指派渠道商不能开启分销");
}
//检测【分销者】部分是否最少0.3元
$distributionMoney = ($item['commission_dis_distribution'] / 100) * $item['deduction_money'];
if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
$item['name']
."开启分销后:【分销者】红包不足"
"{$item['name']}开启分销后:【分销者】红包不足"
. CouponMain::min_redpack_money
. ",(领券)分销者占比最低" .
. ",(领券)分销者占比最低" .
(round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2))
. "%"
);
@ -518,17 +570,45 @@ class Coupon extends Base
$distributedMoney = ($item['commission_dis_distributed'] / 100) * $item['deduction_money'];
if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
$item['name']
. "开启分销后:【被分销者】红包不足"
"{$item['name']}】开启分销后:【被分销者】红包不足"
. CouponMain::min_redpack_money
. ",(核销)被分销者占比最低" .
(round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2))
. "%");
}
}else{
if($item->business->is_assign == BusinessModel::COMMON_OFF){
return $this->json(4001,
"{$item["name"]}】所属商家【{$item->business->business_name}】未指派渠道商不能关闭分销");
}
//检测【渠道商】部分是否最少0.3元
$agencyMoney = ($item['commission_agency'] / 100) * $item['deduction_money'];
if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"{$item['name']}】开启分销后:【渠道商】红包不足"
. CouponMain::min_redpack_money
. "元,渠道商占比最低" .
(round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2))
. "%"
);
}
//检测【消费者】部分是否最少0.3元
$consumerMoney = ($item['commission_consumer'] / 100) * $item['deduction_money'];
if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) {
return $this->json(4002,
"{$item['name']}】开启分销后:【消费者】红包不足"
. CouponMain::min_redpack_money
. ",消费者占比最低" .
(round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2))
. "%");
}
}
}
}
Db::startTrans();
try {
CouponMain::whereIn("id",$ids)->update(["is_distribution"=>$isDistribution]);

View File

@ -5,8 +5,9 @@ namespace app\model;
class Redpack extends Base
{
const userTypeDistribution = "distribution";//分销者
const userTypeDistribution = "distribution";//分销者
const userTypeDistributed = "distributed";//被分销者
const userTypeAgency = "agency";// 渠道商
/**
* 获取分销红包记录
* @param $distributionUserCode 分销人user_code

View File

@ -117,7 +117,7 @@
data-href="/manager/coupon/set-distribution?is_distribution=1"
lay-event="distribution" date-title="开启分销">开启分销</a>
<a class="layui-btn layui-btn-sm"
data-href="/manager/coupon/set-distribution?is_distribution=1"
data-href="/manager/coupon/set-distribution?is_distribution=0"
lay-event="distribution" date-title="开启分销">关闭分销</a>
</script>
<!-- 列 性别 -->