三級分銷執(zhí)行分銷規(guī)則往往都是用戶確認(rèn)收貨時候犬绒,所以分銷的方法最好也是在確認(rèn)收貨的方法里面刹勃。
代碼如下:
//這個方法需要傳遞一個變量(訂單號)柄错,作用在下面代碼體現(xiàn)
function fenxiao($orderid){
//因?yàn)槭侨壏咒N剩蟀,所以有三個等級比例催蝗,同理,如果兩級分銷就會有兩個育特。
//查詢數(shù)據(jù)庫設(shè)置的比例
$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'];、
//實(shí)例化會員的流水賬戶,因?yàn)橐獙憀og日志犬缨。
$member = new MemberAccount();
//這里是循環(huán)遍歷
foreach ($bili as $k=>$v){
//重點(diǎn)T骸!遍尺!
//數(shù)據(jù)庫里面有用戶ID(uid)和用戶的上級ID(pid)
//db('sys_user')->where('uid='.$pid)->value('pid'); 這句話就是查詢他的上級pid截酷,然后賦值為PID
//接著foreach循環(huán)查uid等于查出來的pid的值,以此類推乾戏。
$pid = db('sys_user')->where('uid='.$pid)->value('pid');
if($pid){
if($order['fenxiao_money']>0){
//計(jì)算分銷傭金
$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'];
//獲取數(shù)量
$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){
//數(shù)量
$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)
{
//數(shù)量
$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)
{
//數(shù)量
$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;
}
//執(zhí)行返金幣操作
//判斷用戶余額
$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', //在數(shù)據(jù)庫添加上
'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', //在數(shù)據(jù)庫添加上
'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;
}
}