diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index eda22f9..0be74ca 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -315,10 +315,7 @@ class Coupon extends Base return $this->json(4001, "商家已禁用"); } - //如果商家指派了渠道商 分销人重新定义为空 - if ($couponMain->business->is_agency == BusinessModel::COMMON_ON){ - $distributionUserCode = ""; - } + try { //检查优惠券状态 @@ -335,12 +332,11 @@ class Coupon extends Base return $this->json(4001,$e->getMessage()); } - //检查通过 执行领取 $time = time(); Db::startTrans(); try { - //写入领取记录 + //1写入领取记录 $data = [ "coupon_id" =>$couponMain->id, "name" =>$couponMain["name"], @@ -358,16 +354,52 @@ class Coupon extends Base "end_time" => date($couponMain->end_time . " 00:00:00"), "edition" => couponMain::COMMON_ON,//版本 未知作用 "is_verificated" => couponMain::COMMON_OFF,//是否验证 - "distribution_user_code" => $distributionUserCode,//分销人user_code ]; + //如果商家指派了渠道商 分销人重新定义为空 + if ($couponMain->business->is_agency == BusinessModel::COMMON_ON){ + $data["is_distribution_coupon"] = couponMain::COMMON_OFF;//是否是分销优惠券 + $data["distribution_user_code"] = '';//分销人user_code + }else{ + $data["is_distribution_coupon"] = couponMain::COMMON_ON;//是否是分销优惠券 + $data["distribution_user_code"] = $distributionUserCode; + } + + $receiveCoupon = CouponRepository::getInstance()->receiveCoupon($data); - CouponRepository::getInstance()->receiveCoupon($data); $couponMain->save(["received_count"=>Db::raw("received_count + 1")]); + $deductionAmount = (($couponMain->deduction_money / 100) * $couponMain->commission_dis_distribution); //是否开启分销 并且有分销人 给分销人发红包 if($couponMain->is_distribution == CouponMain::COMMON_ON && !empty($distributionUserCode)) { + + // 2. 写入优惠券流水 + $couponBillData = [ + "coupon_main_id" => $couponMain->id, + "coupon_id" => $receiveCoupon->id, + "user_code" => $account->user_code, + "business_code" => $couponMain->business->code, + "agency_code" => $couponMain->business->agency_code, + "commission_agency" => $couponMain->commission_agency, + "commission_admin" => $couponMain->commission_admin, + "commission_consumer" => $couponMain->commission_consumer, + "commission_dis_admin" => $couponMain->commission_dis_admin, + "commission_dis_distribution" => $couponMain->commission_dis_distribution, + "commission_dis_distributed" => $couponMain->commission_dis_distributed, + "money" => $couponMain->money, + "agency_money" => 0, + "admin_money" => 0, + "consumer_money" => 0, + "distribution_money" => $deductionAmount, + "lat" => $lat, + "lng" => $lng, + "create_time" => date("Y-m-d H:i:s" ,$time), + ]; + $amount = round($deductionAmount,2); //四舍五入 精确到分 + + CouponBill::create($couponBillData); + $distributionUser = Account::findOne([["user_code","=",$distributionUserCode]]); if(empty($distributionUser)){ Db::rollback(); @@ -382,8 +414,7 @@ class Coupon extends Base $mch_billno = createUuid(); } - $amount = (($couponMain->deduction_money / 100) * $couponMain->commission_dis_distribution); - $amount = round($amount,2); //四舍五入 精确到分 + $toBalanceData = [ 'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) @@ -567,13 +598,14 @@ class Coupon extends Base //可分配金额 如果是普通商家 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 = (($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); //四舍五入 精确到分 - $agencyMoney = round($agencyMoney,2); //四舍五入 精确到分 - $adminMoney = round($adminMoney,2); //四舍五入 精确到分 - $consumerMoney = round($consumerMoney,2); //四舍五入 精确到分 //开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款 if( @@ -592,9 +624,9 @@ class Coupon extends Base } }else{ - $adminMoney = 0; - $consumerMoney = round($deductionMoney,2) ; - $agencyMoney = 0; //四舍五入 精确到分 + $adminMoney = 0; + $consumerMoney = round($deductionMoney,2) ; + $agencyMoney = 0; //四舍五入 精确到分 } //开始数据操作 @@ -612,26 +644,41 @@ class Coupon extends Base ]); - // 2. 写入优惠券流水 - $couponBillData = [ - "coupon_main_id" => $coupon->couponMain->id, - "coupon_id" => $coupon->id, - "user_code" => $account->user_code, - "business_code" => $business->code, - "agency_code" => $business->agency_code, - "commission_agency" => $coupon->couponMain->commission_agency, - "commission_admin" => $coupon->couponMain->commission_admin, - "commission_consumer" => $coupon->couponMain->commission_consumer, - "money" => $coupon->couponMain->money, - "agency_money" => $agencyMoney, - "admin_money" => $adminMoney, - "consumer_money" => $consumerMoney, - "lat" => $lat, - "lng" => $lng, - "create_time" => date("Y-m-d H:i:s" ,$time), - ]; - $couponBill = CouponBill::create($couponBillData); + $couponBill = CouponBill::getUserOne( $account->user_code,$coupon->couponMain->id,$coupon->id); + if(empty($couponBill)){ + $couponBillData = [ + "coupon_main_id" => $coupon->couponMain->id, + "coupon_id" => $coupon->id, + "user_code" => $account->user_code, + "business_code" => $business->code, + "agency_code" => $business->agency_code, + "commission_agency" => $coupon->couponMain->commission_agency, + "commission_admin" => $coupon->couponMain->commission_admin, + "commission_consumer" => $coupon->couponMain->commission_consumer, + "commission_dis_admin" => $coupon->couponMain->commission_dis_admin, + "commission_dis_distribution" => $coupon->couponMain->commission_dis_distribution, + "commission_dis_distributed" => $coupon->couponMain->commission_dis_distributed, + "money" => $coupon->couponMain->money, + "agency_money" => $agencyMoney, + "admin_money" => $adminMoney, + "consumer_money" => $consumerMoney, + "lat" => $lat, + "lng" => $lng, + "create_time" => date("Y-m-d H:i:s" ,$time), + ]; + $couponBill = CouponBill::create($couponBillData); + }else{ + $couponBillData = [ + "agency_money" => $agencyMoney, + "admin_money" => $adminMoney, + "consumer_money" => $consumerMoney, + "lat" => $lat, + "lng" => $lng, + ]; + $couponBill->save($couponBillData); + } + // 3. 写入商家扣费记录 $deductionData = [ diff --git a/app/controller/manager/Bill.php b/app/controller/manager/Bill.php index 481ec84..ce58ac9 100644 --- a/app/controller/manager/Bill.php +++ b/app/controller/manager/Bill.php @@ -29,12 +29,14 @@ class Bill extends Base $list['agency_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::agency_money, $keyword, $startTime, $endTime); $list['admin_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::admin_money, $keyword, $startTime, $endTime); $list['consumer_money_sum'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::consumer_money, $keyword, $startTime, $endTime); + $list['distribution_money_money'] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::distribution_money_money, $keyword, $startTime, $endTime); return $this->json(0, 'success', $list); } $this->data["agency_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::agency_money); $this->data["admin_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::admin_money); $this->data["consumer_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::consumer_money); + $this->data["distribution_money_sum"] = BillRepository::getInstance()->getAgencyMoneySum(CouponBill::distribution_money_money); $this->data["startTime"] = $startTime; $this->data["endTime"] = $endTime; diff --git a/app/model/CouponBill.php b/app/model/CouponBill.php index 660514b..bfea99c 100644 --- a/app/model/CouponBill.php +++ b/app/model/CouponBill.php @@ -14,9 +14,10 @@ use think\db\exception\ModelNotFoundException; */ class CouponBill extends Base { - public const agency_money = "agency_money"; - public const admin_money = "admin_money"; - public const consumer_money = "consumer_money"; + public const agency_money = "agency_money";//渠道商的流水字段 + public const admin_money = "admin_money";//平台的流水字段 + public const consumer_money = "consumer_money";//消费者的流水字段 + public const distribution_money_money = "distribution_money";//分销者的流水字段 public function couponMain() { return $this->hasOne(CouponMain::class, 'id',"coupon_main_id"); @@ -30,4 +31,16 @@ class CouponBill extends Base { return $this->hasOne(Account::class, 'user_code',"user_code"); } + + public static function getUserOne($userCode,$couponMainId,$couponId) + { + return self::where( + [ + ["user_code","=",$userCode], + ["coupon_main_id","=",$couponMainId], + ["coupon_id","=",$couponId], + ] + )->find(); + + } } \ No newline at end of file diff --git a/app/repository/BillRepository.php b/app/repository/BillRepository.php index 3a0e0e2..055eda5 100644 --- a/app/repository/BillRepository.php +++ b/app/repository/BillRepository.php @@ -43,6 +43,7 @@ class BillRepository extends Repository ->join("business b", "a.business_code = b.code") ->join("account c", "a.user_code = c.user_code") ->join("coupon_main d", "a.coupon_main_id = d.id") + ->join("coupon e", "a.coupon_id = e.id") ->when(!empty($keyword), function ($q) use ($keyword) { $q->where("b.business_name|c.nick_name|d.name", "like", "%{$keyword}%"); }) @@ -58,7 +59,9 @@ class BillRepository extends Repository ->when(!empty($endTime), function ($q) use ($endTime) { $q->whereTime("a.create_time", "<=", $endTime); }) - ->field("a.*,a.coupon_id as user_coupon_id,b.business_name,c.nick_name,c.avatar_url,d.name as coupon_main_name,d.deduction_money"); + ->field("a.*,a.coupon_id as user_coupon_id,b.business_name, + c.nick_name,c.avatar_url,d.name as coupon_main_name,d.deduction_money + ,e.is_distribution_coupon"); $failData ['total'] = $rep->count(); $failData ['list'] = $rep diff --git a/app/repository/CouponRepository.php b/app/repository/CouponRepository.php index 489f163..1c05fda 100644 --- a/app/repository/CouponRepository.php +++ b/app/repository/CouponRepository.php @@ -110,9 +110,4 @@ class CouponRepository extends Repository { return Coupon::create($data); } - - - - - } \ No newline at end of file diff --git a/public/static/manager/js/bill.js b/public/static/manager/js/bill.js index 977a62c..942deb1 100644 --- a/public/static/manager/js/bill.js +++ b/public/static/manager/js/bill.js @@ -38,6 +38,7 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect',"laydate"] $("#new_agency_money_sum").text(res.data.agency_money_sum?res.data.agency_money_sum:0) $("#new_admin_money_sum").text(res.data.admin_money_sum?res.data.admin_money_sum:0) $("#new_consumer_money_sum").text(res.data.consumer_money_sum?res.data.consumer_money_sum:0) + $("#new_distribution_money_sum").text(res.data.new_distribution_money_sum?res.data.new_distribution_money_sum:0) return { "code": res.code, //解析接口状态 "msg": res.msg, //解析提示文本 @@ -49,25 +50,39 @@ layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect',"laydate"] cols: [[ {type: 'checkbox'}, {field: "nick_name",title: '用户昵称'}, - {field: "user_coupon_id",title: '核销编码'}, + {field: "user_coupon_id",width:120,title: '核销编码'}, {field: "business_name", title: '商家名称'}, {field: "coupon_main_name", title: '优惠券名称'}, - {templet:"#row-commission", title: '持有比例(渠道商:平台:消费者)'}, - {field: 'deduction_money', minWidth: 200, title: '扣除金额'}, - {field: 'create_time', minWidth: 200, title: '时间'}, + {templet:"#row-commission", title: '持有比例'}, + {field: 'deduction_money', width: 200, title: '扣除金额'}, + {field: 'create_time', width: 180, title: '时间'}, ]], done: function (res, curr, count) { Tools.setInsTb(insTb); var rdata=[]; res.data.forEach(function (d,index) { + let disstr =''; + if (d.is_distribution_coupon == 0) { + disstr = " 渠道商:平台:消费者" + + " 比例: " + d.commission_agency + ":" + d.commission_admin + ":" + d.commission_consumer + + "金额: " + d.agency_money + ":" + d.admin_money + " :" + d.consumer_money; + } else { + disstr = "分销者:平台:被分销者" + + " 比例: " + d.commission_dis_distribution + ":" + d.commission_dis_admin + ":" + d.commission_dis_distributed + + "金额: " + d.distribution_money + ":" + d.admin_money + " :" + d.consumer_money; + } + rdata[index] = [ d.nick_name , d.user_coupon_id , d.business_name , d.coupon_main_name , - ("比例:"+d.commission_agency +":"+ d.commission_admin+ ":"+ d.commission_consumer + - "金额:" + d.agency_money+":" + d.admin_money + ":" + d.consumer_money) , + disstr, + // ("比例:"+d.commission_agency +":"+ d.commission_admin+ ":"+ d.commission_consumer + + // "金额:" + d.agency_money+":" + d.admin_money + ":" + d.consumer_money) , d.money, d.create_time, ] diff --git a/view/manager/bill/index.html b/view/manager/bill/index.html index e2bb028..fd81044 100644 --- a/view/manager/bill/index.html +++ b/view/manager/bill/index.html @@ -26,7 +26,7 @@
-
+

总流水:

@@ -34,7 +34,7 @@
-
+

渠道商提成总额

@@ -42,7 +42,7 @@
-
+

太古平台提成总额

@@ -50,7 +50,7 @@
-
+

消费者提成总额

@@ -58,10 +58,17 @@
- +
+
+
+

分销者提成总额

+ {$distribution_money_sum?? 0} +
+
+
-
+

当前流水:

@@ -69,7 +76,7 @@
-
+

渠道商提成总额

@@ -77,7 +84,7 @@
-
+

太古平台提成总额

@@ -85,7 +92,7 @@
-
+

消费者提成总额

@@ -93,7 +100,14 @@
- +
+
+
+

分销者提成总额

+   +
+
+
@@ -158,8 +172,15 @@ \ No newline at end of file