<?php
include 'get_user_info.php';
//  set default time format
date_default_timezone_set('Asia/Kolkata');      
$date=date("Y-m-d H:i:s");      //  for 24 hour

//  function for fetch downline user id
function get_downline_user_id($array_all_id){
    include 'include/db.php';
    //var_dump($array_all_id);
    for($i=0;$i<sizeof($array_all_id);$i++)
    {
        $qryy="select * from tree_data inner join registration on tree_data.position_id=registration.unique_user_id where tree_data.position_id='$array_all_id[$i]' ";
        //$qryy="select * from tree_data where position_id='B00002' ";
        $connection2=mysqli_query($conn,$qryy);
        if(mysqli_num_rows($connection2)>0)
        {
            while($rww=mysqli_fetch_array($connection2))
            {
                $left=$rww['position_left'];
                $right=$rww['position_right'];
                $main_id_rank=$rww['rank'];
            }
            upgrade_rank($left,$right,$main_id_rank,$array_all_id[$i]);
        }
    }
}
function upgrade_rank($left,$right,$main_id_rank,$rank_id){
    include 'include/db.php';
    //if(!EMPTY($left))
    {
        $slt_details="select * from wallet_balance inner join registration on wallet_balance.balance_user_id=registration.unique_user_id where unique_user_id='$left' ";
        $con2=mysqli_query($conn,$slt_details);
        if(mysqli_num_rows($con2))
        {
            while($rows=mysqli_fetch_array($con2))
            {
                $bv=$rows['bv_balance'];
                $rank=$rows['rank'];
            }
        }
        else
        {
            $bv='0';
            $rank='';
        }
    }
    //if(!EMPTY($right))
    {
        $slt_details1="select * from wallet_balance inner join registration on wallet_balance.balance_user_id=registration.unique_user_id where unique_user_id='$right' ";
        $con3=mysqli_query($conn,$slt_details1);
        if(mysqli_num_rows($con3))
        {
            while($rows2=mysqli_fetch_array($con3))
            {
                $bv2=$rows2['bv_balance'];
                $rank2=$rows2['rank'];
            }
        }
        else
        {
             $bv2='0';
             $rank2='';
        }
    }
    //  set default time format
        date_default_timezone_set('Asia/Kolkata');      
        $date=date("Y-m-d H:i:s");      //  for 24 hour
        $admin_id='B00001';
    /*  Declare all the money  */
            //  fetch all the user benefit amount for rank upgrade
            //  fetch supervisor amount
            $slt_benefit="select * from member_benefit where benefit_rank='supervisor' ";
            $con_benefit=mysqli_query($conn,$slt_benefit);
            if(mysqli_num_rows($con_benefit)>0)
            {
                while($fr=mysqli_fetch_array($con_benefit))
                {
                    $supervisor=$fr['performance_bonus'];
                }
            }
            //  fetch executive supervisor amount
            $slt_benefit2="select * from member_benefit where benefit_rank='executive supervisor' ";
            $con_benefit2=mysqli_query($conn,$slt_benefit2);
            if(mysqli_num_rows($con_benefit2)>0)
            {
                while($fr2=mysqli_fetch_array($con_benefit2))
                {
                    $executive_supervisor=$fr2['performance_bonus'];
                }
            }
            //  fetch marketing manager amount
            $slt_benefit3="select * from member_benefit where benefit_rank='marketing manager' ";
            $con_benefit3=mysqli_query($conn,$slt_benefit3);
            if(mysqli_num_rows($con_benefit3)>0)
            {
                while($fr3=mysqli_fetch_array($con_benefit3))
                {
                    $marketing_manager1= $fr3['performance_bonus'];
                    $marketing_manager2= $fr3['achivement_bonus'];
                    $marketing_manager=$marketing_manager1+$marketing_manager2;
                }
            }
            //  fetch senior marketing manager amount
            $slt_benefit4="select * from member_benefit where benefit_rank='senior manager' ";
            $con_benefit4=mysqli_query($conn,$slt_benefit4);
            if(mysqli_num_rows($con_benefit4)>0)
            {
                while($fr4=mysqli_fetch_array($con_benefit4))
                {
                    $senior_marketing_manager1= $fr4['performance_bonus'];
                    $senior_marketing_manager2= $fr4['achivement_bonus'];
                    $senior_marketing_manager=$senior_marketing_manager1+$senior_marketing_manager2;
                }
            }
            //  fetch advisor amount
            $slt_benefit5="select * from member_benefit where benefit_rank='advisor' ";
            $con_benefit5=mysqli_query($conn,$slt_benefit5);
            if(mysqli_num_rows($con_benefit5)>0)
            {
                while($fr5=mysqli_fetch_array($con_benefit5))
                {
                    $advisor1= $fr5['performance_bonus'];
                    $advisor2= $fr5['achivement_bonus'];
                    $advisor=$advisor1+$advisor2;
                }
            }
             //  fetch motivator amount
            $slt_benefit6="select * from member_benefit where benefit_rank='motivator' ";
            $con_benefit6=mysqli_query($conn,$slt_benefit6);
            if(mysqli_num_rows($con_benefit6)>0)
            {
                while($fr6=mysqli_fetch_array($con_benefit6))
                {
                    $motivator1= $fr6['performance_bonus'];
                    $motivator2= $fr6['achivement_bonus'];
                    $motivator=$motivator1+$motivator2;
                }
            }
             //  fetch salary holder amount
            $slt_benefit7="select * from member_benefit where benefit_rank='salary holder' ";
            $con_benefit7=mysqli_query($conn,$slt_benefit7);
            if(mysqli_num_rows($con_benefit7)>0)
            {
                while($fr7=mysqli_fetch_array($con_benefit7))
                {
                    $salary_holder=$fr7['salary'];
                }
            }
            //  fetch pension holder amount
            $slt_benefit8="select * from member_benefit where benefit_rank='pension holder' ";
            $con_benefit8=mysqli_query($conn,$slt_benefit8);
            if(mysqli_num_rows($con_benefit8)>0)
            {
                while($fr8=mysqli_fetch_array($con_benefit8))
                {
                    $pension_holder1=$fr8['salary'];
                    $pension_holder2=$fr8['pension_one_time'];
                    $pension_holder=$pension_holder1+$pension_holder2;
                }
            }
    //  check
    $qry_slt="update registration inner join wallet_balance on registration.unique_user_id=wallet_balance.balance_user_id ";  
    $money_deduct="update wallet_balance ";
    $wallet_history="insert into wallet(receiver_id,balance,sender_id,source,transaction_date,transaction_id,remarks,pending) ";
   if($rank_id!='B00001')
   {
        $admin_wallet ="insert into admin_wallet_details(wallet_user_id,receiver_amount,cr_dr,closer_amount,wallet_transaction_id,wallet_remarks,wallet_date)";
   }
    if($rank=='salary holder' && $rank2=='salary holder' && $main_id_rank!='pension holder')
    {
        $qry_slt.="set registration.rank='pension holder',wallet_balance.total_balance=wallet_balance.total_balance+'$pension_holder' ";
        $money_deduct.=" set total_balance=total_balance-'$pension_holder'";
        $trans_id=get_unique_transaction_id();
        $trans_id2=get_unique_transaction_id2($trans_id);
        $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$pension_holder1','$admin_id','commission','$date','$trans_id','Congratulations','1'),
                                  ('$rank_id','$pension_holder2','$admin_id','commission','$date','$trans_id2','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$pension_holder1','dr','$admin_total_balace'-'$pension_holder1','$trans_id','commission','$date')
                                ('$rank_id','$pension_holder2','dr','$admin_total_balace'-'$pension_holder2','$trans_id2','commission','$date')";
        }
    }
    else if($rank=='motivator' && $rank2=='motivator' && ($bv+$bv2)>=500000 && $main_id_rank!='salary holder')
    {
        $qry_slt.="set registration.rank='salary holder',wallet_balance.total_balance=wallet_balance.total_balance+'$salary_holder' ";
        $money_deduct.=" set total_balance=total_balance-'$salary_holder'";
        $trans_id=get_unique_transaction_id();
         $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$salary_holder','$admin_id','commission','$date','$trans_id','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$salary_holder','dr','$admin_total_balace'-'$salary_holder','$trans_id','commission','$date')";
        }
    }
    else if($rank=='advisor' && $rank2=='advisor' && ($bv+$bv2)>=4000 && $main_id_rank!='motivator')
    {
        $qry_slt.="set registration.rank='motivator',wallet_balance.total_balance=wallet_balance.total_balance+'$motivator' ";
        $money_deduct.=" set total_balance=total_balance-'$motivator'";
        $trans_id=get_unique_transaction_id();
        $trans_id2=get_unique_transaction_id2($trans_id);
        $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$motivator1','$admin_id','commission','$date','$trans_id','Congratulations','1'),
                                  ('$rank_id','$motivator2','$admin_id','commission','$date','$trans_id2','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$motivator1','dr','$admin_total_balace'-'$motivator1','$trans_id','commission','$date')
                                ('$rank_id','$motivator2','dr','$admin_total_balace'-'$motivator2','$trans_id2','commission','$date')";
        }
    }
    else if($rank=='senior manager' && $rank2=='senior manager' && $main_id_rank!='advisor')
    {
         $qry_slt.="set registration.rank='advisor',wallet_balance.total_balance=wallet_balance.total_balance+'$advisor' ";
         $money_deduct.=" set total_balance=total_balance-'$advisor'";
         $trans_id=get_unique_transaction_id();
         $trans_id2=get_unique_transaction_id2($trans_id);
         $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
         $wallet_history.=" values('$rank_id','$advisor1','$admin_id','commission','$date','$trans_id','Congratulations','1'),
                                  ('$rank_id','$advisor2','$admin_id','commission','$date','$trans_id2','Congratulations','1') ";
        if($rank_id!='B00001')
        {
         $admin_wallet .=" values('$rank_id','$advisor1','dr','$admin_total_balace'-'$advisor1','$trans_id','commission','$date')
                                ('$rank_id','$advisor2','dr','$admin_total_balace'-'$advisor2','$trans_id2','commission','$date')";
        }
    }
    else if($rank=='marketing manager' && $rank2=='marketing manager' && $main_id_rank!='senior marketing manager')
    {
        $qry_slt.="set registration.rank='senior marketing manager',wallet_balance.total_balance=wallet_balance.total_balance+'$senior_marketing_manager' ";
        $money_deduct.=" set total_balance=total_balance-'$senior_marketing_manager'";
        $trans_id=get_unique_transaction_id();
        $trans_id2=get_unique_transaction_id2($trans_id);
        $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$senior_marketing_manager1','$admin_id','commission','$date','$trans_id','Congratulations','1'),
                                  ('$rank_id','$senior_marketing_manager2','$admin_id','commission','$date','$trans_id2','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$senior_marketing_manager1','dr','$admin_total_balace'-'$senior_marketing_manager1','$trans_id','commission','$date')
                                ('$rank_id','$senior_marketing_manager2','dr','$admin_total_balace'-'$senior_marketing_manager2','$trans_id2','commission','$date')";
        }
    }
    else if($rank=='executive supervisor' && $rank2=='executive supervisor' && ($bv+$bv2)>=2000 && $main_id_rank!='marketing manager')
    {
        $trans_id=get_unique_transaction_id();
        $trans_id2=get_unique_transaction_id2($trans_id);
         $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $qry_slt.="set registration.rank='marketing manager',wallet_balance.total_balance=wallet_balance.total_balance+'$marketing_manager' ";
        $money_deduct.=" set total_balance=total_balance-'$marketing_manager'";
        $wallet_history.=" values('$rank_id','$marketing_manager1','$admin_id','commission','$date','$trans_id','Congratulations','1'),
                                  ('$rank_id','$marketing_manager2','$admin_id','commission','$date','$trans_id2','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$marketing_manager1','dr','$admin_total_balace'-'$marketing_manager1','$trans_id','commission','$date')
                                ('$rank_id','$marketing_manager2','dr','$admin_total_balace'-'$marketing_manager2','$trans_id2','commission','$date')";
        }
    }
    else if($rank=='supervisor' && $rank2=='supervisor' && $main_id_rank!='executive supervisor')
    {
        $qry_slt.="set registration.rank='executive supervisor',wallet_balance.total_balance=wallet_balance.total_balance+'$executive_supervisor' ";
        $money_deduct.=" set total_balance=total_balance-'$executive_supervisor'";
        $trans_id=get_unique_transaction_id();
          $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$executive_supervisor','$admin_id','commission','$date','$trans_id','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$executive_supervisor','dr','$admin_total_balace'-'$executive_supervisor','$trans_id','commission','$date')";
        }
    }
    else if(($bv+$bv2)>=6000 && $main_id_rank!='supervisor' && $main_id_rank!='executive supervisor' && $main_id_rank!='marketing manager' && $main_id_rank!='senior marketing manager' && $main_id_rank!='advisor' && $main_id_rank!='motivator' && $main_id_rank!='salary holder' && $main_id_rank!='pension holder')
    {
        $qry_slt.="set registration.rank='supervisor',wallet_balance.total_balance=wallet_balance.total_balance+'$supervisor' ";
        $money_deduct.=" set total_balance=total_balance-'$supervisor'";
        $trans_id=get_unique_transaction_id();
        $admin_total_balace=get_admin_total_balance();  //  to get admin total wallet balance
        $wallet_history.=" values('$rank_id','$supervisor','$admin_id','commission','$date','$trans_id','Congratulations','1') ";
        if($rank_id!='B00001')
        {
        $admin_wallet .=" values('$rank_id','$supervisor','dr','$admin_total_balace'-'$supervisor','$trans_id','commission','$date')";
        }
    }  
    else
    {
       // echo 'no of these';
    }
    $qry_slt.=" where registration.unique_user_id='$rank_id' ";
    $qry_connection=mysqli_query($conn,$qry_slt);
    $money_deduct.=" where balance_user_id='$admin_id'";
    $qry_connection2=mysqli_query($conn,$money_deduct);
    $qry_connection3=mysqli_query($conn,$wallet_history); 
    if($rank_id!='B00001')
   {
    $qry_connection4=mysqli_query($conn,$admin_wallet);
   }
        if($qry_connection3)
        {
            echo 'suucess';
        }
        else
        {
            echo 'not';
        }

}


   
 //  feth all user have into the database
 $slt_user="select * from registration order by unique_user_id ASC";
 $con_user=mysqli_query($conn,$slt_user);
 $all_user=array();  //  create array for stored all user id
 if(mysqli_num_rows($con_user)>0)
 {
     while($row=mysqli_fetch_array($con_user))
     {
         array_push($all_user,$row['unique_user_id']);
     }
 }

//  Rank calculation function will call at a specific time
$send_all_id_with_reverse=array_reverse($all_user);
$return_value=get_downline_user_id($send_all_id_with_reverse);
//var_dump($return_value);


//   create serial wise transaction id
function get_unique_transaction_id(){
       include 'include/db.php';
       $qr2="select MAX(transaction_id) from wallet LIMIT 1";
        $con2=mysqli_query($conn,$qr2);
        if(mysqli_num_rows($con2)>0)
        {
        	while($rw=mysqli_fetch_array($con2))
        	{
        		 $xyz=$rw['MAX(transaction_id)'];
        		 $ak=substr($xyz,2);
        		 $old_unique_id=intval($ak);  //  convert string to int
        	}
        	for ($num = $old_unique_id; $num <= $old_unique_id+1; $num++)
            $net_val=sprintf("%'.06d", $num);
            return 'WT'.$net_val;
        }
   }
function get_unique_transaction_id2($pre_val){
       
    		 $xyz=$pre_val;
    		 $aa=substr($xyz,2);
    		 $old_unique_id=intval($aa);  //  convert string to int
               	for ($num = $old_unique_id; $num <= $old_unique_id+1; $num++)
                $net_val=sprintf("%'.06d", $num);
                return 'WT'.$net_val;
    }

?>