Your IP : 216.73.216.26


Current Path : /home2/wtmwscom/public_html/member/
Upload File :
Current File : /home2/wtmwscom/public_html/member/cron_binary_income_4-2-2016.php

<?php
include '../connection.php';
include '../function_lib.php';

$result_reg_date = mysqli_query($connection, "SELECT uid, date(reg_date) AS reg_date FROM user ORDER BY  recid ASC ");
$reg_date_array = array();
while ($reg_date_row = mysqli_fetch_object($result_reg_date)) {
    $reg_date_array[$reg_date_row->uid] = $reg_date_row->reg_date;
}

$closing_date = date("Y-m-d");
//$closing_date = "2016-01-13";

$result = mysqli_query($connection, "SELECT uid, type FROM user ORDER BY recid ASC");
while ($row = mysqli_fetch_object($result)) {
    $uid = $row->uid;
    $type = $row->type;
    calculation($uid, $type, $reg_date_array, $closing_date);
}

function calculation($uid, $type, $reg_date_array, $closing_date) {
    $childs_left = get_child_levels(get_child_ids_2($uid, 'L'));
    $childs_right = get_child_levels(get_child_ids_2($uid, 'R'));


    if ($type == 0) {
        //echo " if type = 0 <br/> ";
        $commission_ids_left = get_commission_ids($childs_left, $uid);
        $commission_ids_right = get_commission_ids($childs_right, $uid);

        $n_of_i_left = count($commission_ids_left);
        $n_of_i_right = count($commission_ids_right);

        if (($n_of_i_left > 1 || $n_of_i_right > 1) && $n_of_i_left != 0 && $n_of_i_right != 0) {
            //echo " n_of_i_left > 1, if type = 0 <br/> ";
            calculate_binary($commission_ids_left, $commission_ids_right, $uid, $closing_date);
            $type = 1;
            calculation($uid, $type, $reg_date_array, $closing_date);
        }
    } else {
        //echo " if type = 1 <br/>";
        $commission_ids_left = get_commission_ids_by_reg_date($childs_left, $uid, $reg_date_array, $closing_date);
        $commission_ids_right = get_commission_ids_by_reg_date($childs_right, $uid, $reg_date_array, $closing_date);

        $n_of_i_left = count($commission_ids_left);
        $n_of_i_right = count($commission_ids_right);

        if ($n_of_i_left != 0 && $n_of_i_right != 0) {
            //echo " n_of_i_left != 0 && n_of_i_right != 0 , if type =1 <br/>";
            calculate_binary_2($commission_ids_left, $commission_ids_right, $uid, $reg_date_array, $closing_date);
            //calculation($uid, $type);
        }
    }
}

/* For 1:1 pair this function is used */

function calculate_binary_2($commission_ids_left, $commission_ids_right, $uid, $reg_date_array, $closing_date) {

    /* for applying capping at daily, weekly and monthly basis */
    //$date = date("Y-m-d", strtotime(date('Y-m-d H:i:s')));
    $daily_pair = mysqli_num_rows(mysqli_query($connection, "SELECT uid FROM binary_income WHERE uid = '$uid' AND datetime LIKE '%" . $closing_date . "%'")); // daily pair

    /*
      $week_date = date("Y-m-d", strtotime('-1 week', strtotime(date('Y-m-d H:i:s'))));
      $weekly_pair = mysqli_num_rows(mysqli_query($connection, "SELECT uid FROM binary_income WHERE uid = '$uid' AND datetime BETWEEN '$week_date' AND '$date'")); // weekly pair

      $month_date = date("Y-m-d", strtotime('-1 month', strtotime(date('Y-m-d H:i:s'))));
      $monthly_pair = mysqli_num_rows(mysqli_query($connection, "SELECT uid FROM binary_income WHERE uid = '$uid' AND datetime BETWEEN '$month_date' AND '$date'")); // monthly pair

      $n_of_i_left = count($commission_ids_left);
      $n_of_i_right = count($commission_ids_right);

      if ($n_of_i_left <= $n_of_i_right) {
      $j = $n_of_i_left;
      } elseif ($n_of_i_left > $n_of_i_right) {
      $j = $n_of_i_right;
      }

      $i = 0;
      while ($i <= $j) {

      if (isset($commission_ids_left[$i])) {
      $uid1 = $commission_ids_left[$i];

      if (isset($commission_ids_right[$i])) {
      $uid2 = $commission_ids_right[$i];
      $left_reg_date = mysqli_fetch_object(mysqli_query($connection, "SELECT date(reg_date) AS reg_date FROM user WHERE uid = '$uid1' "))->reg_date;
      $right_reg_date = mysqli_fetch_object(mysqli_query($connection, "SELECT date(reg_date) AS reg_date FROM user WHERE uid = '$uid2' "))->reg_date;

      if ($daily_pair < 2 && $left_reg_date == $right_reg_date) {

      mysqli_query($connection, "INSERT INTO binary_income (uid, amount, datetime) VALUES ('$uid', 500, '" . date('Y-m-d H:i:s') . "')");
      mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uid1 . "', '" . date('Y-m-d H:i:s') . "')");
      mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uid2 . "', '" . date('Y-m-d H:i:s') . "')");
      $daily_pair++;
      } else {
      break;
      }
      }
      }
      $i++;
      }
     */
//    $commission_id_array = array_merge($commission_ids_left, $commission_ids_right);
//    $result_reg_date = mysqli_query($connection, "SELECT uid, date(reg_date) AS reg_date FROM user WHERE uid IN  ( " . implode(" , ", $commission_id_array) . " ) ");
//    $reg_date_array = array();
//    while ($row = mysqli_fetch_object($result_reg_date)) {
//        $reg_date_array[$row->uid] = $row->reg_date;
//    }
    $datetime = $closing_date." ".date("H:i:s");
    
    foreach ($commission_ids_left as $keyL => $uidL) {
        foreach ($commission_ids_right as $keyR => $uidR) {
            //echo " $reg_date_array[$uidL] == $reg_date_array[$uidR] && $reg_date_array[$uidL] == $closing_date && $reg_date_array[$uidR] == $closing_date <br/>";
            if ($reg_date_array[$uidL] == $reg_date_array[$uidR] && $reg_date_array[$uidL] == $closing_date && $reg_date_array[$uidR] == $closing_date) {

                //echo " PairDone $keyL=>$uidL ($reg_date_array[$uidL]) == $keyR=>$uidR ($reg_date_array[$uidR]) <br/><br/> ";
                //$pairDoneCounter++;
                if ($daily_pair < 2) {
                    
                    //echo " daily pair = $daily_pair , 1 pair inseted for type 1 <br/> ";

                    mysqli_query($connection, "INSERT INTO binary_income (uid, amount, datetime) VALUES ('$uid', 500, '" . $datetime . "')");
                    mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uidL . "', '" . $datetime . "')");
                    mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uidR . "', '" . $datetime . "')");
                    $daily_pair++;
                }
//                else {
//                    break;
//                }
                unset($commission_ids_left[$keyL]);
                unset($commission_ids_right[$keyR]);
                break;
            }
//             else{
//                echo " Notmatching $keyL=>$uidL ($reg_date_array[$uidL]) == $keyR=>$uidR ($reg_date_array[$uidR]) <br/><br/> ";
//             }
        }
        if ($daily_pair == 2) {
            break;
        }
    }
    //echo " uid = $uid, daily pair = $daily_pair <br/>";
    $daily_pair = 0;
}

/* For 2:1 or 1:2 pair this function is used, means first time calculation */

function calculate_binary($commission_ids_left, $commission_ids_right, $uid, $closing_date) {
    $datetime = $closing_date." ".date("H:i:s");
    $n_of_i_left = count($commission_ids_left);
    $n_of_i_right = count($commission_ids_right);

    if ($n_of_i_left > 1 || $n_of_i_right > 1) {
        $uid1 = $commission_ids_left[0];
        $uid2 = $commission_ids_right[0];
        if ($n_of_i_left > 1) {
            $uid3 = $commission_ids_left[1];
        } else {
            $uid3 = $commission_ids_right[1];
        }

        mysqli_query($connection, "INSERT INTO binary_income (uid, amount, datetime) VALUES ('$uid', 500, '" . $datetime . "')");
        mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uid1 . "', '" . $datetime . "')");
        mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uid2 . "', '" . $datetime . "')");
        mysqli_query($connection, "INSERT INTO binary_done (uid, from_uid, datetime) VALUES ('$uid', '" . $uid3 . "', '" . $datetime . "')");
        mysqli_query($connection, "UPDATE user SET type = 1, type_update_datetime='" . $datetime . "', board_type = '1' WHERE uid = '$uid'");
        add_user_to_board($uid, 1, $datetime);
    }
}

echo "<br/> Closing complete. Please close this browser.";



?>