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

class Topgun extends CI_Controller {

	public function bonusbot($key = NULL)
	{
		set_time_limit(0);
		
		if($key == 'g2xnr6fsk1qp') {
			$current_time = time();
			$current_hm = date('Hi', $current_time);
			$is_allow = (($current_hm == '0000') ? 1 : 0);
			
			if($is_allow == 1) {
				$server_time = $current_time;
				$income_date = date('Y-m-d', $server_time);
				
				$this->benchmark->mark('code_start');
				
				//Get all package details
				$packages = array();
				$package_query = $this->db->query("SELECT * FROM sw_packages");
				if($package_query->num_rows() > 0) {
					foreach($package_query->result() as $package_row) {
						$packages[$package_row->package_id] = array(
																	'package_price' => $package_row->package_price,
																	'insurance' => $package_row->insurance
																);									
					}
				}
				
				$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
				$ins_lists = array();
				$member_lists = array();	
				$member_query = $this->db->query("SELECT a.member_id, a.member_type, a.username, a.package_type, a.special_level, a.active, b.v_unit2, b.v_unit3 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) {
						$member_lists[$member_row->member_id] = array(
																	'member_type' => $member_row->member_type,
																	'username' => $member_row->username,
																	'package_type' => $member_row->package_type,
																	'special_level' => $member_row->special_level,
																	'active' => $member_row->active,
																	'v_unit2' => $member_row->v_unit2,
																	'insurance_amount' => 0,
																	'total_intro' => 0,
																	'total_sales' => 0,
																	'algebra_earn' => 0,
																	'differential_earn' => 0,
																	'is_new_income_date' => 0,
																	'group_level' => 0,
																	'group_rate' => 0,
																	'same_level_bonus_group' => array()
																);	
						
						//Buy Insurance							
						if($member_row->member_type == 2 && $member_row->active == 1 && $member_row->package_type > 0) {
							if($member_row->v_unit3 >= $packages[$member_row->package_type]['insurance']) {
								$this->db->query("UPDATE sw_member_wallets SET v_unit3 = (v_unit3 - ?) WHERE member_id = ? LIMIT 1", array($packages[$member_row->package_type]['insurance'], $member_row->member_id));		

								$trans_data = array(
									'from_member_id' => $member_row->member_id,
									'from_member_username' => $member_row->username,
									'from_credit_balance' => $member_row->v_unit3,
									'credit_amount' => $packages[$member_row->package_type]['insurance'],
									'to_credit_balance' => ($member_row->v_unit3 - $packages[$member_row->package_type]['insurance']),
									'insurance_date' => $income_date,
									'status' => 1,
									'transaction_date' => $server_time
								);	
								
								array_push($ins_lists, $trans_data);
								
								$member_lists[$member_row->member_id]['insurance_amount'] = $packages[$member_row->package_type]['insurance'];
							}	
						}	
					}
				}
				
				if(sizeof($ins_lists) > 0) {
					$this->db->insert_batch('sw_member_buy_insurances', $ins_lists);
				}
				
				//Prepare direct sales
				$member_query = $this->db->query("SELECT a.package_type, b.member_id AS introducer_id FROM sw_members a, sw_members b WHERE (a.introducer = b.username) AND a.member_type = ? AND a.active = ? AND a.package_type > ? AND a.member_id <= ?", array(2, 1, 0, $last_member_id));
				if($member_query->num_rows() > 0) {
					foreach($member_query->result() as $member_row) {
						$member_lists[$member_row->introducer_id]['total_intro'] = ($member_lists[$member_row->introducer_id]['total_intro'] + 1);
						$member_lists[$member_row->introducer_id]['total_sales'] = ($member_lists[$member_row->introducer_id]['total_sales'] + $packages[$member_row->package_type]['package_price']);
					}
				}
				
				//Prepare and update all members level
				$member_query = $this->db->query("SELECT member_id, introducer_parent_ref FROM sw_members WHERE member_id <= ?", array($last_member_id));
				if($member_query->num_rows() > 0) {
					//Update level
					foreach($member_query->result() as $member_row) {
						$new_group_level = 0;
						
						if($member_lists[$member_row->member_id]['total_intro'] >= 20 && $member_lists[$member_row->member_id]['total_sales'] >= 200000) {
							$new_group_level = 4;
						}
						else if($member_lists[$member_row->member_id]['total_intro'] >= 15 && $member_lists[$member_row->member_id]['total_sales'] >= 150000) {
							$new_group_level = 3;
						}
						else if($member_lists[$member_row->member_id]['total_intro'] >= 10 && $member_lists[$member_row->member_id]['total_sales'] >= 100000) {
							$new_group_level = 2;
						}
						else if($member_lists[$member_row->member_id]['total_intro'] >= 5 && $member_lists[$member_row->member_id]['total_sales'] >= 50000) {
							$new_group_level = 1;
						}
						
						$this->db->query("UPDATE sw_members SET level = ? WHERE member_id = ? LIMIT 1", array($new_group_level, $member_row->member_id));
						$member_lists[$member_row->member_id]['group_level'] = $new_group_level;
						
						if($member_lists[$member_row->member_id]['special_level'] > $member_lists[$member_row->member_id]['group_level']) {
							$member_lists[$member_row->member_id]['group_level'] = $member_lists[$member_row->member_id]['special_level'];
						}
						
						switch($member_lists[$member_row->member_id]['group_level']) {
							case 1: $member_lists[$member_row->member_id]['group_rate'] = 5; break;
							case 2: $member_lists[$member_row->member_id]['group_rate'] = 10; break;
							case 3: $member_lists[$member_row->member_id]['group_rate'] = 20; break;
							case 4: $member_lists[$member_row->member_id]['group_rate'] = 30; break;
						}
					}
					
					//Prepare same level bonus
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$arr = explode(',', $member_row->introducer_parent_ref);
							
							$rate = 4;
							for($i=(sizeof($arr) - 1); $i>=0; $i--) {
								if($arr[$i] != '' AND $arr[$i] > 0) {
									if($rate > 0) {
										if($member_lists[$arr[$i]]['group_level'] == 4 && $member_lists[$member_row->member_id]['group_level'] == 4) {
											$num = 0;
											$arr2 = explode(',', $member_row->introducer_parent_ref);
											for($j=0; $j<sizeof($arr2); $j++) {
												for($k=0; $k<sizeof($member_lists[$arr[$i]]['same_level_bonus_group']); $k++) {
													if($arr2[$j] == $member_lists[$arr[$i]]['same_level_bonus_group'][$k]['member_id']) {
														$num++;
													}
												}
											}
											
											if($num < 4) {
												if($member_lists[$arr[$i]]['package_type'] >= 4) {
													$tmp_arr = array('member_id' => $member_row->member_id, 'rate' => $rate);
													array_push($member_lists[$arr[$i]]['same_level_bonus_group'], $tmp_arr);
												}
												
												$rate = ($rate - 1);
											}	
										}
									}
									else {
										break;
									}
								}
							}
						}
					}
				}
				
				$t_lists_1 = array();	
				$t_lists_2 = array();	
				$t_lists_3 = array();
				
				//Give all member algebra bonus, differential bonus & same level bonus
				$member_query = $this->db->query("SELECT member_id, introducer_parent_ref FROM sw_members WHERE member_id <= ?", array($last_member_id));
				if($member_query->num_rows() > 0) {
					//Give all member algebra bonus
					foreach($member_query->result() as $member_row) {
						if($member_row->introducer_parent_ref != '') {
							$arr = explode(',', $member_row->introducer_parent_ref);
							
							$j = 1;
							for($i=(sizeof($arr) - 1); $i>=0; $i--) {
								if($arr[$i] != '' AND $arr[$i] > 0) {
									if($j <= 10) {
										if($j == 2 || $j == 4 || $j == 6 || $j == 8 || $j == 10) {
											$bonus = (($member_lists[$member_row->member_id]['insurance_amount'] * 5) / 100);
											
											if($bonus > 0 && $member_lists[$arr[$i]]['active'] == 1) {
												$this->db->query("UPDATE sw_member_wallets SET v_unit2 = (v_unit2 + ?) WHERE member_id = ? LIMIT 1", array($bonus, $arr[$i]));
									
												//Transaction log
												//transaction_type 1 = Intro Bonus, 2 = Algebra Bonus, 3 = Differential Bonus, 4 = Same Bonus, 5 = Pool Bonus
												$trans_data = array(
													'transaction_type' => 2,
													'from_member_id' => $member_row->member_id,
													'from_member_username' => $member_lists[$member_row->member_id]['username'],
													'to_member_id' => $arr[$i],
													'to_member_username' => $member_lists[$arr[$i]]['username'],
													'from_v_unit2' => $member_lists[$arr[$i]]['v_unit2'],
													'v_unit2_amount' => $bonus,
													'to_v_unit2' => ($member_lists[$arr[$i]]['v_unit2'] + $bonus),
													'status' => 1,
													'transaction_date' => $server_time
												);
												
												array_push($t_lists_1, $trans_data);
												
												// //Income summary
												// if($member_lists[$arr[$i]]['is_new_income_date'] == 0) {
													// $income_data = array(
														// 'income_date' => $income_date,
														// 'algebra_bonus' => $bonus,
														// 'v_unit2' => $bonus,
														// 'member_id' => $arr[$i]
													// );	
													// $this->db->insert('sw_member_income_summary', $income_data);
													
													// $member_lists[$arr[$i]]['is_new_income_date'] = 1;
												// }
												// else {
													// $this->db->query("UPDATE sw_member_income_summary SET algebra_bonus = (algebra_bonus + ?), v_unit2 = (v_unit2 + ?) WHERE income_date = ? AND member_id = ? LIMIT 1", array($bonus, $bonus, $income_date, $arr[$i]));
												// }
												
												$member_lists[$arr[$i]]['v_unit2'] = ($member_lists[$arr[$i]]['v_unit2'] + $bonus);
												$member_lists[$arr[$i]]['algebra_earn'] = ($member_lists[$arr[$i]]['algebra_earn'] + $bonus);
											}
										}
									}
									else {
										break;
									}									
									
									$j++;
								}
							}
						}
					}
					
					if(sizeof($t_lists_1) > 0) {
						$this->db->insert_batch('sw_member_transactions', $t_lists_1);
					}
					
					//Give all member differential bonus
					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) {
									if($current_rate == 30) {
										break;
									}
									else {
										$rate = $member_lists[$arr[$i]]['group_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_lists[$member_row->member_id]['algebra_earn'] * $rate) / 100);
										
										if($bonus > 0 && $member_lists[$arr[$i]]['active'] == 1) {
											$this->db->query("UPDATE sw_member_wallets SET v_unit2 = (v_unit2 + ?) WHERE member_id = ? LIMIT 1", array($bonus, $arr[$i]));
								
											//Transaction log
											//transaction_type 1 = Intro Bonus, 2 = Algebra Bonus, 3 = Differential Bonus, 4 = Same Bonus, 5 = Pool Bonus
											$trans_data = array(
												'transaction_type' => 3,
												'from_member_id' => $member_row->member_id,
												'from_member_username' => $member_lists[$member_row->member_id]['username'],
												'to_member_id' => $arr[$i],
												'to_member_username' => $member_lists[$arr[$i]]['username'],
												'from_v_unit2' => $member_lists[$arr[$i]]['v_unit2'],
												'v_unit2_amount' => $bonus,
												'to_v_unit2' => ($member_lists[$arr[$i]]['v_unit2'] + $bonus),
												'status' => 1,
												'transaction_date' => $server_time
											);	
											
											array_push($t_lists_2, $trans_data);
											
											// //Income summary
											// if($member_lists[$arr[$i]]['is_new_income_date'] == 0) {
												// $income_data = array(
													// 'income_date' => $income_date,
													// 'differential_bonus' => $bonus,
													// 'v_unit2' => $bonus,
													// 'member_id' => $arr[$i]
												// );	
												// $this->db->insert('sw_member_income_summary', $income_data);
												
												// $member_lists[$arr[$i]]['is_new_income_date'] = 1;
											// }
											// else {
												// $this->db->query("UPDATE sw_member_income_summary SET differential_bonus = (differential_bonus + ?), v_unit2 = (v_unit2 + ?) WHERE income_date = ? AND member_id = ? LIMIT 1", array($bonus, $bonus, $income_date, $arr[$i]));
											// }
											
											$member_lists[$arr[$i]]['v_unit2'] = ($member_lists[$arr[$i]]['v_unit2'] + $bonus);
											$member_lists[$arr[$i]]['differential_earn'] = ($member_lists[$arr[$i]]['differential_earn'] + $bonus);
										}
									}
								}
							}
						}
					}
					
					if(sizeof($t_lists_2) > 0) {
						$this->db->insert_batch('sw_member_transactions', $t_lists_2);
					}
					
					//Give all member same level bonus
					foreach($member_query->result() as $member_row) {
						for($i=0; $i<sizeof($member_lists[$member_row->member_id]['same_level_bonus_group']); $i++) {
							$downline_id = $member_lists[$member_row->member_id]['same_level_bonus_group'][$i]['member_id'];
							$rate = $member_lists[$member_row->member_id]['same_level_bonus_group'][$i]['rate'];
							$total_earn = ($member_lists[$downline_id]['algebra_earn'] + $member_lists[$downline_id]['differential_earn']);
							$bonus = (($total_earn * $rate) / 100);
							
							if($bonus > 0 && $member_lists[$member_row->member_id]['active'] == 1) {
								$this->db->query("UPDATE sw_member_wallets SET v_unit2 = (v_unit2 + ?) WHERE member_id = ? LIMIT 1", array($bonus, $member_row->member_id));
					
								//Transaction log
								//transaction_type 1 = Intro Bonus, 2 = Algebra Bonus, 3 = Differential Bonus, 4 = Same Bonus, 5 = Pool Bonus
								$trans_data = array(
									'transaction_type' => 4,
									'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_unit2' => $member_lists[$member_row->member_id]['v_unit2'],
									'v_unit2_amount' => $bonus,
									'to_v_unit2' => ($member_lists[$member_row->member_id]['v_unit2'] + $bonus),
									'status' => 1,
									'transaction_date' => $server_time
								);	
								
								array_push($t_lists_3, $trans_data);
								
								// //Income summary
								// if($member_lists[$member_row->member_id]['is_new_income_date'] == 0) {
									// $income_data = array(
										// 'income_date' => $income_date,
										// 'same_bonus' => $bonus,
										// 'v_unit2' => $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 same_bonus = (same_bonus + ?), v_unit2 = (v_unit2 + ?) WHERE income_date = ? AND member_id = ? LIMIT 1", array($bonus, $bonus, $income_date, $member_row->member_id));
								// }
								
								$member_lists[$member_row->member_id]['v_unit2'] = ($member_lists[$member_row->member_id]['v_unit2'] + $bonus);
							}
						}
					}
					
					if(sizeof($t_lists_3) > 0) {
						$this->db->insert_batch('sw_member_transactions', $t_lists_3);
					}
					
					//Generate income summary
					$is_lists_1 = array();	
					$is_lists_2 = array();	
					$is_lists_3 = array();
					
					$query = $this->db->query("SELECT SUM(v_unit2_amount) AS total, to_member_id FROM sw_member_transactions WHERE transaction_date >= ? AND transaction_type = ? GROUP BY to_member_id", array($server_time, 2));
					if($query->num_rows() > 0) {
						foreach($query->result() as $row) {
							$income_data = array(
								'income_date' => $income_date,
								'algebra_bonus' => $row->total,
								'v_unit2' => $row->total,
								'member_id' => $row->to_member_id
							);	
							
							array_push($is_lists_1, $income_data);
							
							$member_lists[$row->to_member_id]['is_new_income_date'] = 1;
						}
						
						if(sizeof($is_lists_1) > 0) {
							$this->db->insert_batch('sw_member_income_summary', $is_lists_1);
						}
					}	
					
					$query = $this->db->query("SELECT SUM(v_unit2_amount) AS total, to_member_id FROM sw_member_transactions WHERE transaction_date >= ? AND transaction_type = ? GROUP BY to_member_id", array($server_time, 3));
					if($query->num_rows() > 0) {
						foreach($query->result() as $row) {
							if($member_lists[$row->to_member_id]['is_new_income_date'] == 1) {
								$this->db->query("UPDATE sw_member_income_summary SET differential_bonus = (differential_bonus + ?), v_unit2 = (v_unit2 + ?) WHERE member_id = ? AND income_date = ? LIMIT 1", array($row->total, $row->total, $row->to_member_id, $income_date));
							}
							else {
								$income_data = array(
									'income_date' => $income_date,
									'differential_bonus' => $row->total,
									'v_unit2' => $row->total,
									'member_id' => $row->to_member_id
								);	
								
								array_push($is_lists_2, $income_data);
								
								$member_lists[$row->to_member_id]['is_new_income_date'] = 1;
							}
						}
						
						if(sizeof($is_lists_2) > 0) {
							$this->db->insert_batch('sw_member_income_summary', $is_lists_2);
						}
					}	
					
					$query = $this->db->query("SELECT SUM(v_unit2_amount) AS total, to_member_id FROM sw_member_transactions WHERE transaction_date >= ? AND transaction_type = ? GROUP BY to_member_id", array($server_time, 4));
					if($query->num_rows() > 0) {
						foreach($query->result() as $row) {
							if($member_lists[$row->to_member_id]['is_new_income_date'] == 1) {
								$this->db->query("UPDATE sw_member_income_summary SET same_bonus = (same_bonus + ?), v_unit2 = (v_unit2 + ?) WHERE member_id = ? AND income_date = ? LIMIT 1", array($row->total, $row->total, $row->to_member_id, $income_date));
							}
							else {
								$income_data = array(
									'income_date' => $income_date,
									'same_bonus' => $row->total,
									'v_unit2' => $row->total,
									'member_id' => $row->to_member_id
								);	
								
								array_push($is_lists_3, $income_data);
								
								$member_lists[$row->to_member_id]['is_new_income_date'] = 1;
							}
						}
						
						if(sizeof($is_lists_3) > 0) {
							$this->db->insert_batch('sw_member_income_summary', $is_lists_3);
						}
					}	
				}

				$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 */