<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Topgun extends CI_Controller {

	public function cronjob($key = NULL)
	{
		set_time_limit(0);
		
		if($key == 'w8fkx3aby9fe') {
			$current_hour = date('Hi', time());
			$is_allow = (($current_hour == '0000') ? 1 : 0);
			
			if($is_allow == 1) {
				$this->ranking($key);
				$this->bonusbot($key);
			}
		}
	}	
	
	public function ranking($key = NULL)
	{
		set_time_limit(0);
		
		if($key == 'w8fkx3aby9fe') {
			$current_hour = date('H', time());
			$is_allow = (($current_hour == '00') ? 1 : 0);
			
			if($is_allow == 1) {
				//Prepare member list
				$member_lists = array();	
				$member_query = $this->db->query("SELECT member_id, username FROM sw_members");
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						$member_lists[$member_row->member_id] = array(
																	'username' => $member_row->username,
																	'ranking' => 0,
																	'total_ct' => 0,
																	'total_cp' => 0,
																	'total_direct_intro' => 0,
																	'total_direct_agents' => 0,
																	'total_direct_managers' => 0,
																	'total_direct_senior_masters' => 0,
																	'total_agents' => 0,
																	'total_managers' => 0,
																);
					}
				}
				
				//Prepare group sales
				$member_query = $this->db->query("SELECT from_member_id, SUM(credit_amount) AS sales FROM sw_member_placement WHERE transaction_type = 1 AND status = 1 GROUP BY from_member_id");
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						$member_lists[$member_row->from_member_id]['total_ct'] = ($member_lists[$member_row->from_member_id]['total_ct'] + $member_row->sales);
					}
				}
				
				//Prepare direct intro
				$member_query = $this->db->query("SELECT a.member_id, b.member_id AS introducer_id FROM sw_members a, sw_members b WHERE (a.introducer = b.username) AND a.member_type = 2 AND a.active = 1");
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						$member_lists[$member_row->introducer_id]['total_direct_intro'] = ($member_lists[$member_row->introducer_id]['total_direct_intro'] + 1);
					}
				}
				
				//Prepare ranking
				$member_query = $this->db->query("SELECT member_id, introducer, introducer_parent_ref FROM sw_members WHERE member_type = 2 AND active = 1");
				if($member_query->num_rows() > 0) {
					//Prepare Agent
					foreach($member_query->result() as $member_row) {
						if($member_lists[$member_row->member_id]['total_ct'] >= 80000 && $member_lists[$member_row->member_id]['total_direct_intro'] >= 40) {
							$member_lists[$member_row->member_id]['ranking'] = 3;
						}
						else if($member_lists[$member_row->member_id]['total_ct'] >= 26000 && $member_lists[$member_row->member_id]['total_direct_intro'] >= 13) {
							$member_lists[$member_row->member_id]['ranking'] = 2;
						}
						else if($member_lists[$member_row->member_id]['total_ct'] >= 8000 && $member_lists[$member_row->member_id]['total_direct_intro'] >= 4) {
							$member_lists[$member_row->member_id]['ranking'] = 1;
						}
					}
					
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$arr = explode(',', $member_row->introducer_parent_ref);
							$i = (sizeof($arr) - 2);
							
							if(isset($member_lists[$arr[$i]]['total_direct_agents'])) {
								if($member_lists[$arr[$i]]['username'] == $member_row->introducer && ($member_lists[$member_row->member_id]['ranking'] == 1 || $member_lists[$member_row->member_id]['ranking'] == 2 || $member_lists[$member_row->member_id]['ranking'] == 3)) {
									$member_lists[$arr[$i]]['total_direct_agents'] = ($member_lists[$arr[$i]]['total_direct_agents'] + 1);
								}
								
								if($member_lists[$member_row->member_id]['ranking'] == 1 || $member_lists[$member_row->member_id]['ranking'] == 2 || $member_lists[$member_row->member_id]['ranking'] == 3) {
									$member_lists[$arr[$i]]['total_agents'] = ($member_lists[$arr[$i]]['total_agents'] + 1);
								}
							}
						}
					}
					
					//Prepare Manager
					foreach($member_query->result() as $member_row) {
						if($member_lists[$member_row->member_id]['total_ct'] >= 750000 && $member_lists[$member_row->member_id]['total_agents'] >= 15 && $member_lists[$member_row->member_id]['total_direct_agents'] >= 5) {
							$member_lists[$member_row->member_id]['ranking'] = 5;
						}
						else if($member_lists[$member_row->member_id]['total_ct'] >= 250000 && $member_lists[$member_row->member_id]['total_agents'] >= 5 && $member_lists[$member_row->member_id]['total_direct_agents'] >= 3) {
							$member_lists[$member_row->member_id]['ranking'] = 4;
						}
					}
					
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$arr = explode(',', $member_row->introducer_parent_ref);
							$i = (sizeof($arr) - 2);
							
							if(isset($member_lists[$arr[$i]]['total_direct_managers'])) {
								if($member_lists[$arr[$i]]['username'] == $member_row->introducer && ($member_lists[$member_row->member_id]['ranking'] == 4 || $member_lists[$member_row->member_id]['ranking'] == 5)) {
									$member_lists[$arr[$i]]['total_direct_managers'] = ($member_lists[$arr[$i]]['total_direct_managers'] + 1);
								}
								
								if($member_lists[$member_row->member_id]['ranking'] == 4 || $member_lists[$member_row->member_id]['ranking'] == 5) {
									$member_lists[$arr[$i]]['total_managers'] = ($member_lists[$arr[$i]]['total_managers'] + 1);
								}
							}
						}
					}
					
					//Prepare Master
					foreach($member_query->result() as $member_row) {
						if($member_lists[$member_row->member_id]['total_ct'] >= 5000000 && $member_lists[$member_row->member_id]['total_managers'] >= 10 && $member_lists[$member_row->member_id]['total_direct_managers'] >= 5) {
							$member_lists[$member_row->member_id]['ranking'] = 7;
						}
						else if($member_lists[$member_row->member_id]['total_ct'] >= 2000000 && $member_lists[$member_row->member_id]['total_managers'] >= 5 && $member_lists[$member_row->member_id]['total_direct_managers'] >= 3) {
							$member_lists[$member_row->member_id]['ranking'] = 6;
						}
					}
					
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$arr = explode(',', $member_row->introducer_parent_ref);
							$i = (sizeof($arr) - 2);
							
							if(isset($member_lists[$arr[$i]]['total_direct_senior_masters'])) {
								if($member_lists[$arr[$i]]['username'] == $member_row->introducer && $member_lists[$member_row->member_id]['ranking'] == 7) {
									$member_lists[$arr[$i]]['total_direct_senior_masters'] = ($member_lists[$arr[$i]]['total_direct_senior_masters'] + 1);
								}
							}
						}
					}
					
					//Prepare Senior Master
					foreach($member_query->result() as $member_row) {
						if($member_lists[$member_row->member_id]['total_ct'] >= 15000000 && $member_lists[$member_row->member_id]['total_direct_senior_masters'] >= 3) {
							$member_lists[$member_row->member_id]['ranking'] = 8;
						}
					}
					
					//Update ranking
					foreach($member_query->result() as $member_row) {
						$this->db->query("UPDATE sw_members SET level = ? WHERE member_id = ? LIMIT 1", array($member_lists[$member_row->member_id]['ranking'], $member_row->member_id));
					}
				}
			}	
		}
		
		echo 'Done';
	}
	
	public function bonusbot($key = NULL)
	{
		set_time_limit(0);
		
		if($key == 'w8fkx3aby9fe') {
			$current_hour = date('H', time());
			$is_allow = (($current_hour == '00') ? 1 : 0);
			
			if($is_allow == 1) {
				$server_time = time();
				$income_date = date('Y-m-d', $server_time);
				
				$this->benchmark->mark('code_start');
			
				$last_member_id = 10000000;
				$member_query = $this->db->query("SELECT member_id FROM sw_members ORDER BY member_id DESC LIMIT 1");
				if($member_query->num_rows() > 0) {
					$member_row = $member_query->row();
					$last_member_id = $member_row->member_id;
				}
				
				//Prepare member list
				$member_lists = array();	
				$member_query = $this->db->query("SELECT a.member_id, a.username, a.level, a.special_level, a.member_type, b.v_unit FROM sw_members a, sw_member_wallets b WHERE (a.member_id = b.member_id) AND a.member_id <= ?", array($last_member_id));
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						$highest_level = (($member_row->special_level > $member_row->level) ? $member_row->special_level : $member_row->level);
						
						$member_lists[$member_row->member_id] = array(
																	'username' => $member_row->username,
																	'level' => $member_row->level,
																	'member_type' => $member_row->member_type,
																	'v_unit' => $member_row->v_unit,
																	'rate' => (($highest_level > 0) ? ($highest_level + 2) : 0),
																	'is_new_income_date' => 0,
																	'group_bonus' => array()
																);	
					}
				}
				
				//Prepare profit sharing
				$member_query = $this->db->query("SELECT a.member_transaction_id, a.amount, b.member_id, b.introducer_parent_ref FROM sw_member_daily_bonus a, sw_members b WHERE (a.from_member_id = b.member_id) AND a.status = 0");
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$current_rate = 0;
							$arr = explode(',', $member_row->introducer_parent_ref);
							for($i=(sizeof($arr) - 1);$i>0;$i--) {
								if($arr[$i] != '' AND $arr[$i] > 0) {
									$rate = $member_lists[$arr[$i]]['rate'];
									
									if($rate > 0) {
										if($current_rate < $rate) {
											$upgrade_rate = $rate;
											if($current_rate > 0) {
												$rate = ($rate - $current_rate);
											}
											
											$current_rate = $upgrade_rate;
										}
										else if($current_rate >= $rate) {
											$rate = 0;
										}
									}
									
									$bonus = (($member_row->amount * $rate) / 100);
									
									if($bonus > 0 && $member_lists[$arr[$i]]['member_type'] == 2) {	
										$tmp_arr = array('member_id' => $member_row->member_id, 'bonus' => $bonus);
										array_push($member_lists[$arr[$i]]['group_bonus'], $tmp_arr);
									}
								}
							}
						}
						
						$this->db->query("UPDATE sw_member_daily_bonus SET status = ? WHERE member_transaction_id = ? LIMIT 1", array(1, $member_row->member_transaction_id));
					}
				}
				
				//Give all member profit sharing
				$member_query = $this->db->query("SELECT member_id FROM sw_members WHERE active = ? AND member_id <= ?", array(1, $last_member_id));
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						for($i=0; $i<sizeof($member_lists[$member_row->member_id]['group_bonus']); $i++) {
							$downline_id = $member_lists[$member_row->member_id]['group_bonus'][$i]['member_id'];
							$bonus = $member_lists[$member_row->member_id]['group_bonus'][$i]['bonus'];
							
							if($bonus > 0) {
								$this->db->query("UPDATE sw_member_wallets SET v_unit = (v_unit + ?) WHERE member_id = ? LIMIT 1", array($bonus, $member_row->member_id));
								
								//Transaction log
								//transaction_type 1 = Profit Sharing
								$trans_data = array(
									'transaction_type' => 1,
									'from_member_id' => $downline_id,
									'from_member_username' => $member_lists[$downline_id]['username'],
									'to_member_id' => $member_row->member_id,
									'to_member_username' => $member_lists[$member_row->member_id]['username'],
									'from_v_unit' => $member_lists[$member_row->member_id]['v_unit'],
									'v_unit_amount' => $bonus,
									'to_v_unit' => ($member_lists[$member_row->member_id]['v_unit'] + $bonus),
									'wallet_type' => 1,
									'wallet_id' => $member_row->member_id,
									'status' => 1,
									'transaction_date' => $server_time
								);	
								$this->db->insert('sw_member_transactions', $trans_data);
								
								//Income summary
								if($member_lists[$member_row->member_id]['is_new_income_date'] == 0) {
									$income_data = array(
										'income_date' => $income_date,
										'leader_bonus' => $bonus,
										'v_unit' => $bonus,
										'member_id' => $member_row->member_id
									);	
									$this->db->insert('sw_member_income_summary', $income_data);
									
									$member_lists[$member_row->member_id]['is_new_income_date'] = 1;
								}
								else {
									$this->db->query("UPDATE sw_member_income_summary SET leader_bonus = (leader_bonus + ?), v_unit = (v_unit + ?) WHERE income_date = ? AND member_id = ? LIMIT 1", array($bonus, $bonus, $income_date, $member_row->member_id));
								}
								
								$member_lists[$member_row->member_id]['v_unit'] = ($member_lists[$member_row->member_id]['v_unit'] + $bonus);
							}
						}	
					}
				}
				
				$this->benchmark->mark('code_end');

				$bonus_elapsed_time = $this->benchmark->elapsed_time('code_start', 'code_end');
				$this->db->query("INSERT INTO sw_bonus_hours (elapsed_time, server_time) VALUES ('" . $bonus_elapsed_time . "', '" . time() . "')");
			}	
		}
		
		echo 'Done';
	}
}

/* End of file topgun.php */
/* Location: ./application/controllers/topgun.php */