三级分销执行分销规则往往都是用户确认收货时候,所以分销的方法最好也是在确认收货的方法里面。
代码如下:
//这个方法需要传递一个变量(订单号),作用在下面代码体现
function fenxiao($orderid){
//因为是三级分销,所以有三个等级比例,同理,如果两级分销就会有两个。
//查询数据库设置的比例
$fenxiao = db('sys_fenxiao_bili')->where('id=1')->find();
$bili[1] = $fenxiao['level_1'];
$bili[2] = $fenxiao['level_2'];
$bili[3] = $fenxiao['level_3'];
//通过订单号,获得购买者的ID(buyer_id)
$order = $this->getDetail($orderid);
//$pid 就是购买者ID
$pid = $order['buyer_id'];、
//实例化会员的流水账户,因为要写log日志。
$member = new MemberAccount();
//这里是循环遍历
foreach ($bili as $k=>$v){
//重点!!!
//数据库里面有用户ID(uid)和用户的上级ID(pid)
//db('sys_user')->where('uid='.$pid)->value('pid'); 这句话就是查询他的上级pid,然后赋值为PID
//接着foreach循环查uid等于查出来的pid的值,以此类推。
$pid = db('sys_user')->where('uid='.$pid)->value('pid');
if($pid){
if($order['fenxiao_money']>0){
//计算分销佣金
$money = $order['fenxiao_money']*$v/100;
//写入日志
$member->addMemberAccountData(0, 2, $pid, '1', $money, 8, $orderid, $k.'代返利');
}
}
}
}
上面的代码没有考虑返利时候会员级别有高有低的情况,并且不能一直追查上线,下面是修改的
/*
* 分销方法
* zheng
* */
public function fenxiao($orderid)
{
//订单号查order_id
// $orderid = db('ns_order')->where('order_no',$order_no)->value('order_id');
//每个商品的级别价格
$gid = db('ns_order_goods')->where('order_id',$orderid)->select();
/**************************************************员工价格*/
$staff = 0;
foreach($gid as $k => $gg){
//获得订单里面商品的ID $gg['goods_id'];
//获取数量
$num = $gg['num'];
//1,查员工价格
$jgs = db('ns_goods_sku')->where('goods_id',json_decode($gg['goods_id']))->value('price_staff');
$jg = $jgs * $num;
$cc = $jg + $staff;
$staff = $cc;
}
// echo $staff;
/**************************************************代理价格*/
$agent = 0;
foreach($gid as $ag){
//数量
$num = $ag['num'];
//查代理的价格
$dl = db('ns_goods_sku')->where('goods_id',json_decode($ag['goods_id']))->value('price_agent');
$dls = $dl * $num;
$cc = $dls + $agent;
$agent = $cc;
// $agent += db('ns_goods_sku')->where('goods_id',json_decode($ag['goods_id']))->value('price_agent');
}
// dump($agent);
// echo $agent;
/*********************************************会员价格*/
$vp = 0;
foreach($gid as $vip)
{
//数量
$num = $vip['num'];
//查会员的价格
$hy = db('ns_goods_sku')->where('goods_id',json_decode($vip['goods_id']))->value('price_vip');
$hys = $hy * $num;
$cc = $hys + $vp;
$vp = $cc;
// $vp += db('ns_goods_sku')->where('goods_id',json_decode($vip['goods_id']))->value('price_vip');
}
/*************************************************游客价格*/
$p = 0;
foreach($gid as $pr)
{
//数量
$num = $pr['num'];
//查游客的价格
$yk = db('ns_goods_sku')->where('goods_id',json_decode($pr['goods_id']))->value('price');
$yks = $yk * $num;
$cc = $yks + $p;
$p = $cc;
// $p += db('ns_goods_sku')->where('goods_id',json_decode($pr['goods_id']))->value('price');
}
// echo $p;
//通过orderid查用户ID
$uid = db('ns_order')->where('order_id',$orderid)->value('buyer_id');
//获取购买者level
$u_level = db('ns_member')->where('uid',$uid)->value('member_level'); //
$maxlevel = 0; //初始最大级别
for($i = 0; $i<100; $i++)
{
file_put_contents('./logs/'.$maxlevel,'1.txt'.PHP_EOL,FILE_APPEND);
//查看等级 47游客 50会员 51代理 52员工
$level = db('ns_member')->where('uid',$uid)->value('member_level'); //购买者等级 47 51
if($level > $maxlevel){
$maxlevel = $level; //maxlevel
}
//查购买用户的上级
$pid = db('sys_user')->where('uid',$uid)->value('top'); //购买者上级ID
//上级的级别
$le = db('ns_member')->where('uid',$pid)->value('member_level'); //购买者上级等级 51 50
if ($pid==0 || $pid == ''){
break;
}else{
$uid = $pid;
}
//记录上级的最大值
// $maxlevel = max($level,$le);
if($le > $maxlevel)
{
$maxlevel = $le; //maxlevel
$money = $this->suan($level,$le,$staff,$agent,$vp,$p);
if($money == 0)
{
continue;
}
if($money == 100)
{
break;
}
//执行返金币操作
//判断用户余额
$balance = db('ns_member_account')->where('uid',$pid)->value('balance');
if(empty($balance)){
// $money=-$money;
$datas = [
'uid' => $pid,
'shop_id' => '0',
'point' => '0',
'balance' => $money,
'coin' => 0,
'member_cunsum' => '0',
'member_sum_point' => '0',
'dx_point' => '0'
];
db('ns_member_account')->insert($datas);
//写入ns_member_account_records(会员流水账)记录表
$data = [
'uid' => $pid,
'shop_id' => '0',
'account_type' => '2',
'sign' => '1',
'number' => $money,
'from_type' => '11', //在数据库添加上
'data_id' => $orderid,
'text' => '下级购买商品获得奖励',
'create_time' => date("Y-m-d H:i:s", time())
];
db('ns_member_account_records')->insert($data);
}else{
//更改用户余额
db('ns_member_account')->where('uid',$pid)->setInc('balance', $money);
//写入ns_member_account_records(会员流水账)记录表
$data = [
'uid' => $pid,
'shop_id' => '0',
'account_type' => '2',
'sign' => '1',
'number' => $money,
'from_type' => '11', //在数据库添加上
'data_id' => $orderid,
'text' => '下级购买商品获得奖励',
'create_time' => date("Y-m-d H:i:s", time())
];
db('ns_member_account_records')->insert($data);
}
}
}
}
public function suan($a,$b,$staff,$agent,$vp,$p)
{
//$staff;//员工 $agent;代理 $vp;会员 $p; youke
if($a == 47 ){
if($b == 47){
return 0;
}
if($b == 50){
return $p - $vp;
}
if($b == 51){
return $p - $agent ;
}
if($b == 52){
return $p - $staff;
}
}
if($a == 50){
if($b == 47){
return 0;
}
if($b == 50){
return 0;
}
if($b == 51){
return $vp - $agent;
}
if($b == 52){
return $vp - $staff;
}
}
if($a == 51){
if($b == 47){
return 0;
}
if($b == 50){
return 0;
}
if($b == 51){
return 0;
}
if($b == 52){
return $agent - $staff;
}
}
if($a == 52){
return 100;
}
}