Your IP : 216.73.216.26


Current Path : /home2/wtmwscom/public_html/admin/cronjob/
Upload File :
Current File : /home2/wtmwscom/public_html/admin/cronjob/cron_matching_income.php

<?php
session_start();
include '../../connection.php';
include '../../function_lib.php';
//error_reporting(E_ERROR | E_PARSE);

//$date =  "2018-06-18";
//$datetime = $date." 23:59:59";

$datetime = date('Y-m-d H:i:s');
$date = date('Y-m-d');

$today_day = date('l');

$capping_amount_array = array();
$package_result = mysqli_query($connection, "SELECT * FROM pin_package");
while ($package_row = mysqli_fetch_object($package_result)) {
    $capping_amount_array[$package_row->id] = $package_row->capping_amount;
}
$capping_amount_array[0] = $capping_amount_array[1];

// cron job command line paramter read
/*
parse_str($argv[1], $params);
$limit = $params['limit'];
switch ($limit) {
    case 1:
        $offset = 0;
        $rowCount = 1000;
        break;
    case 2:
        $offset = 1000;
        $rowCount = 1000;
        break;
    case 3:
        $offset = 2000;
        $rowCount = 1000;
        break;
    case 4:
        $offset = 3000;
        $rowCount = 100000;
        break;
    default:
        $offset = 0;
        $rowCount = 100000;
        $limit = 4;
        break;
}
*/
// do binary matching closing on saturday only
//if($today_day=="Saturday"){
//if(isset($_SESSION['adminid']) && !empty($_SESSION['adminid'])) {

//mysqli_query($connection, "INSERT INTO `test` (`id`, `to`, `subject`, `message`, `header`, `datetime`) VALUES (NULL, 'limit=>$limit', 'offset=>$offset', 'rowCount=>$rowCount', 'SQL=>$sql', '".date('Y-m-d H:i:s')."');");
//$sql = "select uid, refer_id, is_paid, plan_type, type from user ORDER BY recid ASC LIMIT $offset , $rowCount ";
$sql = "select uid, refer_id, placement_id, is_paid, plan_type, type, binary_type from user WHERE DATE(reg_date) <= '$date' ORDER BY recid ASC  ";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_object($result)){
    $uid = $row->uid;
    $refer_id = $row->refer_id;
    $is_paid = $row->is_paid;
    $plan_type = $row->plan_type;
    $type = $row->binary_type;
    
//    echo $placement_id;
    //$row_uid = mysqli_fetch_object(mysqli_query($connection, "select `left`, `rigth` from user where uid = '".$uid."'"));
//    $uid_left = $row_uid->left;
//    $uid_right = $row_uid->rigth;
    //echo $uid."<br/>";
    $amount_left  = 0;
    $amount_right = 0;
     $childs_left = get_child_levels(get_child_ids_by_reg_date($uid, 'L', $datetime));
    $childs_right = get_child_levels(get_child_ids_by_reg_date($uid, 'R', $datetime));
    $amount_left = get_pv_amount($childs_left, $date);
    $amount_right = get_pv_amount($childs_right, $date);
//    if($uid_left != 0){        
//        $uid_array_left = array();
//        $tree = new Tree();
//        $tree->root  = $uid_left;
//        $uid_array_left = $tree->get_tree_node($uid_left, 0); 
//        $amount_left = get_pv_amount($uid_array_left); 
//        
//    }
//    if($uid_right != 0){ 
//        $uid_array_right = array();
//        $tree = new Tree();
//        $tree->root  = $uid_right;
//        $uid_array_right = $tree->get_tree_node($uid_right, 0);
//        $amount_right = get_pv_amount($uid_array_right);
//    }
    
    if($amount_left !=0 || $amount_right != 0) {
        $amount_left = $amount_left + get_carry_amount($uid, 'left_carry_amount');
        $amount_right = $amount_right + get_carry_amount($uid, 'right_carry_amount');
    }
    
    //echo $uid.' '.$amount_left.' - '.$amount_right."<br/>"; 
    
    $count = 0;
    if($amount_left != 0 && $amount_right == 0){  
       $is_insert = mysqli_query($connection, "insert into my_earning(uid, date, amount_left, amount_right, matching_amount, left_carry_amount, right_carry_amount) values(
            $uid, '".$date."', $amount_left, $amount_right, 0, $amount_left, $amount_right)");
       if($is_insert) $count++;
        
    }
    elseif($amount_left == 0 && $amount_right != 0){       
        $is_insert = mysqli_query($connection, "insert into my_earning(uid, date, amount_left, amount_right, matching_amount, left_carry_amount, right_carry_amount) values(
            $uid, '".$date."', $amount_left, $amount_right, 0 , $amount_left, $amount_right)");
        if($is_insert) $count++;
        
    }
    elseif($amount_left != 0 && $amount_right != 0){
        $capping_matching_amount = $capping_amount_array[$plan_type];
        if($amount_left > $amount_right){ 
            //$matching_amount = $amount_right >= $capping_matching_amount ? $capping_matching_amount : $amount_right;
            $matching_amount = $amount_right;
            $left_carry = $amount_left - $amount_right;
            $right_carry = 0;
            }
        elseif($amount_right > $amount_left){ 
            //$matching_amount = $amount_left >= $capping_matching_amount ? $capping_matching_amount : $amount_left;
            $matching_amount = $amount_left;
            $right_carry = $amount_right - $amount_left; 
            $left_carry = 0;
            }
        elseif($amount_left == $amount_right){ 
            //$matching_amount = $amount_left >= $capping_matching_amount ? $capping_matching_amount : $amount_left;
            $matching_amount = $amount_left;
            $right_carry = 0; 
            $left_carry = 0;
            }
        else{ 
            $matching_amount = 0;
            $left_carry = 0;
            $right_carry = 0;
            }
        $is_insert = mysqli_query($connection, "insert into my_earning(uid, date, amount_left, amount_right, matching_amount, left_carry_amount, right_carry_amount) values(
            $uid, '".$date."', $amount_left, $amount_right, $matching_amount, $left_carry, $right_carry)");
        
        $balance_temp = $matching_amount * 0.1;
        $balance = $balance_temp > $capping_matching_amount ? $capping_matching_amount : $balance_temp;
//        mysqli_query($connection, "update user set wallet = wallet + $balance where uid = '$uid'");

        // matching income
//        echo $type;
        if($is_paid == 1 && $type == 1) {
//            echo '.....';
//        if($is_paid == 1) {
            $sql_income = "INSERT INTO income (uid, amount, income_type, datetime, status) VALUES ('$uid', $balance, '3', '".$datetime."', '0' )";
            echo $sql_income."<br/>";
             mysqli_query($connection, $sql_income);
            
            $income_on_inome_amount = $balance * 0.1;
            $refer_id_paid_check = mysqli_fetch_object(mysqli_query($connection,"SELECT is_paid, binary_type from user where uid = '$refer_id'"))->is_paid;
            if($refer_id_paid_check == 1 && $refer_id_row->binary_type==1){
            mysqli_query($connection, "INSERT INTO income (uid, amount, income_type, datetime, status) VALUES ('$refer_id', $income_on_inome_amount, '5', '".$datetime."', '0' )");
}
            mysqli_query($connection, "INSERT INTO binary_income (uid, amount, datetime, closing_type, matching_amount) VALUES "
               . "('$uid', $balance, '".$datetime."', '0', $matching_amount )");
            $left_placement_uid = 0;
    $right_placement_uid = 0;
    echo "SELECT uid, position from user where placement_id = $uid AND is_paid = 1";
    $result_placement = mysqli_query($connection, "SELECT uid, position from user where placement_id = $uid AND is_paid = 1"); 
    while ($row_placement = mysqli_fetch_object($result_placement)) {
        if($row_placement->position == 'L'){
            $left_placement_uid = $row_placement->uid;
        } else if($row_placement->position == 'R'){
            $right_placement_uid = $row_placement->uid;
        }
    }
    $downline_matching_amount = $balance * 0.05;
    if($left_placement_uid != 0){
        mysqli_query($connection,"INSERT into income(uid, amount, income_type, datetime, status) VALUES('$left_placement_uid', '$downline_matching_amount', '7', '".$datetime."', '0')");
      
    }
    if($right_placement_uid != 0){
        mysqli_query($connection,"INSERT into income(uid, amount, income_type, datetime, status) VALUES('$right_placement_uid', '$downline_matching_amount', '7', '".$datetime."', '0')");
      
    }
        }
        
           
        
           
        // income on income
//        $income_on_inome_amount = $balance * 0.1;
//        mysqli_query($connection, "INSERT INTO income (uid, amount, income_type, datetime) VALUES ('$refer_id', $income_on_inome_amount, '5', '".$datetime."' )");
//            $refer_uid_array = get_top_referral_id_n($uid, 2);
//   foreach ($refer_uid_array as $level_index => $level_uid) {
//        $income_on_income_amount = $balance * 0.1;
//           $is_paid_check = mysqli_fetch_object(mysqli_query($connection,"select is_paid from user where uid = $level_uid "))->is_paid;
////       echo $is_paid_check;
//       if($is_paid_check == 1){
//            $sql_level_income = "INSERT INTO `income` (uid, amount, income_type, datetime, status) VALUES ('$level_uid', '$income_on_income_amount', '5', '" . $datetime . "', '0')";
//       mysqli_query($connection,$sql_level_income);
////          update_direct_referal_count($refer_id, $datetime);
//       }
//      
//   }
        if($is_insert) $count++;
        //if($uid == 100){ echo $amount_left.' - '.$amount_right; die;}
    }
    //$matching_amount_t = empty($matching_amount) ? 0 : $matching_amount;
   // echo "date: $date | uid: $uid | is_paid: $is_paid | amount_left: $amount_left | amount_right: $amount_right | Matching Amount: $matching_amount | balance: $balance <br/>";
    
    
}
mysqli_query($connection, "update product_sale set status = 1 where status = 0 AND date <= '$date' ");
//if($limit == 4){
//    mysqli_query($connection, "update product_sale set status = 1 where status = 0");
//}
//echo "Record generated = $count";
echo "<br/> Closing complete. Please close this browser.";
//}else{
  //  redirect('./index.php');
//}
//}

function get_pv_amount($id_array, $date){
    //foreach($id_array as $value){
       $amount = 0;
       if(!empty ($id_array)) {
           $uid_in = implode(" , ", $id_array);
        $result = mysqli_query($GLOBALS['connection'], "select SUM(pv_amount) AS pv_amount_sum from product_sale where uid in ( $uid_in ) and status = 0 AND date <= '$date'  ");
       
        $amount = mysqli_fetch_object($result)->pv_amount_sum;
        
        /*$rec_id_array = array();
        while($row = mysqli_fetch_object($result)){
            $amount = $amount + $row->pv_amount;
           // $rec_id_array[] = $row->rec_id;
        } */
   // }
   /* if(!empty($rec_id_array)){
        $rec_id = implode(" , ", $rec_id_array); 
        mysqli_query($connection, "update product_sale set status = 1 where rec_id in ( $rec_id) ");
    }*/
    
       }
    return $amount;
}
function get_carry_amount($uid, $position){
    $amount = 0;
    //die("select max(rec_id), $position as amount from my_earning where uid = $uid and $position != 0");
    $rs = mysqli_query($GLOBALS['connection'], "select $position as amount from my_earning where rec_id = ( select max(rec_id) from my_earning where uid = $uid )");
    if(mysqli_num_rows($rs)){ $amount = $amount + mysqli_fetch_object($rs)->amount; }
    return $amount;
}



?>