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

class Cronjob extends CI_Controller {

	// public function missbet()
	// {
		// $this->load->model('user_model');
		// $otherdb1 = $this->load->database('otherdb1', TRUE);
		// $otherdb2 = $this->load->database('otherdb2', TRUE);
		
		// //Syn missbet from asiagaming1688
		// $query_1 = $otherdb1->query("SELECT missbet_id, player_id, created_date FROM bctp_missbet WHERE status = 0");
		// if($query_1->num_rows() > 0) {
			// foreach($query_1->result() as $row_1) {
				// $query_2 = $this->db->query("SELECT member_id, username, insurance_date FROM sw_members WHERE casino_plyid_1 = ? LIMIT 1", array($row_1->player_id));
				// if($query_2->num_rows() > 0) {
					// $row_2 = $query_2->row();
					
					// $arr_1 = $this->user_model->update_wallet($row_2->member_id);
					
					// if(isset($arr_1['v_unit6'])) {
						// $DBdata_1 = array(
						   // 'transaction_type' => 1,
						   // 'from_member_id' => $row_2->member_id,
						   // 'from_member_username' => $row_2->username,
						   // 'v_unit6' => str_replace(',', '', $arr_1['v_unit6']),
						   // 'v_unit7' => str_replace(',', '', $arr_1['v_unit7']),
						   // 'v_unit8' => str_replace(',', '', $arr_1['v_unit8']),
						   // 'status' => 0,
						   // 'insurance_expiry_date' => $row_2->insurance_date,
						   // 'transaction_date' => $row_1->created_date,
						// );
						
						// $this->db->insert('sw_member_miss_bets', $DBdata_1);
					// }	
				// }	
				
				// $otherdb1->query("UPDATE bctp_missbet SET status = ? WHERE missbet_id = ? LIMIT 1", array(1, $row_1->missbet_id));
			// }
		// }
		
		// //Syn missbet from luckyb3t
		// $query_3 = $otherdb2->query("SELECT missbet_id, player_id, created_date FROM bctp_missbet WHERE status = 0");
		// if($query_3->num_rows() > 0) {
			// foreach($query_3->result() as $row_3) {
				// $query_4 = $this->db->query("SELECT member_id, username, insurance_date FROM sw_members WHERE casino_plyid_2 = ? LIMIT 1", array($row_3->player_id));
				// if($query_4->num_rows() > 0) {
					// $row_4 = $query_4->row();
					
					// $arr_2 = $this->user_model->update_wallet($row_4->member_id);
					
					// if(isset($arr_2['v_unit6'])) {
						// $DBdata_2 = array(
						   // 'transaction_type' => 2,
						   // 'from_member_id' => $row_4->member_id,
						   // 'from_member_username' => $row_4->username,
						   // 'v_unit6' => str_replace(',', '', $arr_2['v_unit6']),
						   // 'v_unit7' => str_replace(',', '', $arr_2['v_unit7']),
						   // 'v_unit8' => str_replace(',', '', $arr_2['v_unit8']),
						   // 'status' => 0,
						   // 'insurance_expiry_date' => $row_4->insurance_date,
						   // 'transaction_date' => $row_3->created_date,
						// );
						
						// $this->db->insert('sw_member_miss_bets', $DBdata_2);
					// }	
				// }

				// $otherdb2->query("UPDATE bctp_missbet SET status = ? WHERE missbet_id = ? LIMIT 1", array(1, $row_3->missbet_id));				
			// }
		// }
		
		// echo 'Done';
	// }
	
	public function sync_withdrawal()
	{
		$this->load->model('user_model');
		$otherdb1 = $this->load->database('otherdb1', TRUE);
		$otherdb2 = $this->load->database('otherdb2', TRUE);
		
		$p1_w_sync = 0;
		$p2_w_sync = 0;
		$new_p1_w_sync = 0;
		$new_p2_w_sync = 0;
		$query = $this->db->query("SELECT p1_w_sync, p2_w_sync FROM sw_setting LIMIT 1");
		if($query->num_rows() > 0) {
			$row = $query->row();
			$p1_w_sync = $row->p1_w_sync;
			$p2_w_sync = $row->p2_w_sync;
		}
		
		//Sync withdrawal from asiagaming1688
		$query_1 = $otherdb1->query("SELECT withdrawal_id, amount, player_id, created_date FROM bctp_withdrawals WHERE withdrawal_id > ? ORDER BY withdrawal_id ASC", array($p1_w_sync));
		if($query_1->num_rows() > 0) {
			foreach($query_1->result() as $row_1) {
				$member_id = 0;
				$query_2 = $this->db->query("SELECT member_id FROM sw_members WHERE casino_plyid_1 = ? LIMIT 1", array($row_1->player_id));
				if($query_2->num_rows() > 0) {
					$row_2 = $query_2->row();
					$member_id = $row_2->member_id;
				}
				
				$mgw_id = 0;
				$deposit_date = 0;
				$query_3 = $this->db->query("SELECT member_transaction_id, deposit_date FROM sw_member_game_withdrawals WHERE from_member_id = ? ORDER BY member_transaction_id DESC LIMIT 1", array($member_id));
				if($query_3->num_rows() > 0) {
					$row_3 = $query_3->row();
					$mgw_id = $row_3->member_transaction_id;
					$deposit_date = $row_3->deposit_date;
				}
				
				if($mgw_id > 0) {
					$total_bet = 0;
					$total_turnover = 0;
					$total_winloss = 0;
					$query_4 = $otherdb1->query("SELECT COUNT(transaction_id) AS total_bet, SUM(bet_amount) AS total_turnover, SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row_1->player_id, $deposit_date, 1, 0, 'Baccarat'));
					if($query_4->num_rows() > 0) {
						$row_4 = $query_4->row();
						$total_bet = (isset($row_4->total_bet) ? $row_4->total_bet : 0);
						$total_turnover = (isset($row_4->total_turnover) ? $row_4->total_turnover : 0);
						$total_winloss = (isset($row_4->total_winloss) ? $row_4->total_winloss : 0);
					}
					
					$DBdata_1 = array(
					   'p1_w_id' => $row_1->withdrawal_id,
					   'p1_w_date' => $row_1->created_date,
					   'p1_w_amount' => $row_1->amount,
					   'p1_total_bet' => $total_bet,
					   'p1_total_turnover' => $total_turnover,
					   'p1_total_winloss' => $total_winloss,
					   'p1_sync_status' => 1
					);	
					
					$this->db->where('member_transaction_id', $mgw_id);
					$this->db->limit(1);
					$this->db->update('sw_member_game_withdrawals', $DBdata_1);
				}
				
				$new_p1_w_sync = $row_1->withdrawal_id;
			}
		}
		
		//Sync withdrawal from luckyb3t
		$query_5 = $otherdb2->query("SELECT withdrawal_id, amount, player_id, created_date FROM bctp_withdrawals WHERE withdrawal_id > ? ORDER BY withdrawal_id ASC", array($p2_w_sync));
		if($query_5->num_rows() > 0) {
			foreach($query_5->result() as $row_5) {
				$member_id = 0;
				$query_6 = $this->db->query("SELECT member_id FROM sw_members WHERE casino_plyid_2 = ? LIMIT 1", array($row_5->player_id));
				if($query_6->num_rows() > 0) {
					$row_6 = $query_6->row();
					$member_id = $row_6->member_id;
				}
				
				$mgw_id = 0;
				$deposit_date = 0;
				$query_7 = $this->db->query("SELECT member_transaction_id, deposit_date FROM sw_member_game_withdrawals WHERE from_member_id = ? ORDER BY member_transaction_id DESC LIMIT 1", array($member_id));
				if($query_7->num_rows() > 0) {
					$row_7 = $query_7->row();
					$mgw_id = $row_7->member_transaction_id;
					$deposit_date = $row_7->deposit_date;
				}
				
				if($mgw_id > 0) {
					$total_bet = 0;
					$total_turnover = 0;
					$total_winloss = 0;
					$query_8 = $otherdb2->query("SELECT COUNT(transaction_id) AS total_bet, SUM(bet_amount) AS total_turnover, SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row_5->player_id, $deposit_date, 1, 0, 'Baccarat'));
					if($query_8->num_rows() > 0) {
						$row_8 = $query_8->row();
						$total_bet = (isset($row_8->total_bet) ? $row_8->total_bet : 0);
						$total_turnover = (isset($row_8->total_turnover) ? $row_8->total_turnover : 0);
						$total_winloss = (isset($row_8->total_winloss) ? $row_8->total_winloss : 0);
					}
					
					$DBdata_2 = array(
					   'p2_w_id' => $row_5->withdrawal_id,
					   'p2_w_date' => $row_5->created_date,
					   'p2_w_amount' => $row_5->amount,
					   'p2_total_bet' => $total_bet,
					   'p2_total_turnover' => $total_turnover,
					   'p2_total_winloss' => $total_winloss,
					   'p2_sync_status' => 1
					);	
					
					$this->db->where('member_transaction_id', $mgw_id);
					$this->db->limit(1);
					$this->db->update('sw_member_game_withdrawals', $DBdata_2);
				}
				
				$new_p2_w_sync = $row_5->withdrawal_id;
			}
		}
		
		//Update sync
		if($new_p1_w_sync > 0 && $new_p2_w_sync > 0) {
			$this->db->query("UPDATE sw_setting SET p1_w_sync = ?, p2_w_sync = ? WHERE setting_id = ? LIMIT 1", array($new_p1_w_sync, $new_p2_w_sync, 1));
		}
		else if($new_p1_w_sync > 0 && $new_p2_w_sync == 0) {
			$this->db->query("UPDATE sw_setting SET p1_w_sync = ? WHERE setting_id = ? LIMIT 1", array($new_p1_w_sync, 1));
		}
		else if($new_p1_w_sync == 0 && $new_p2_w_sync > 0) {
			$this->db->query("UPDATE sw_setting SET p2_w_sync = ? WHERE setting_id = ? LIMIT 1", array($new_p2_w_sync, 1));
		}
		
		//Update game withdrawal
		$query_9 = $this->db->query("SELECT a.member_transaction_id, a.deposit_date, a.deposit_amount, a.deposit_bonus, a.p1_w_amount, a.p1_sync_status, a.p2_w_amount, a.p2_sync_status, b.member_id, b.casino_plyid_1, b.casino_plyid_2 FROM sw_member_game_withdrawals a, sw_members b WHERE (a.from_member_id = b.member_id) AND a.processing_status = 0");
		if($query_9->num_rows() > 0) {
			foreach($query_9->result() as $row_9) {
				$profit = (($row_9->p1_w_amount + $row_9->p2_w_amount + $row_9->deposit_bonus) - ($row_9->deposit_amount + ($row_9->deposit_bonus * 10)));
				
				if($row_9->p1_sync_status == 1 && $row_9->p2_sync_status == 1) {
					$this->db->query("UPDATE sw_member_game_withdrawals SET profit = ?, processing_status = ?, withdrawal_date = ? WHERE member_transaction_id = ? LIMIT 1", array($profit, 1, time(), $row_9->member_transaction_id));
				}
				else if($row_9->p1_sync_status == 0 && $row_9->p2_sync_status == 1) {
					$query_10 = $otherdb1->query("SELECT SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row_9->casino_plyid_1, $row_9->deposit_date, 1, 0, 'Baccarat'));
					if($query_10->num_rows() > 0) {
						$row_10 = $query_10->row();
						$total_winloss = (isset($row_10->total_winloss) ? $row_10->total_winloss : 0);
						
						if($total_winloss <= 0) {
							$this->db->query("UPDATE sw_member_game_withdrawals SET profit = ?, processing_status = ?, withdrawal_date = ?, p1_sync_status = ? WHERE member_transaction_id = ? LIMIT 1", array($profit, 1, time(), 1, $row_9->member_transaction_id));
						}
					}
				}
				else if($row_9->p1_sync_status == 1 && $row_9->p2_sync_status == 0) {
					$query_11 = $otherdb2->query("SELECT SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row_9->casino_plyid_2, $row_9->deposit_date, 1, 0, 'Baccarat'));
					if($query_11->num_rows() > 0) {
						$row_11 = $query_11->row();
						$total_winloss = (isset($row_11->total_winloss) ? $row_11->total_winloss : 0);
						
						if($total_winloss <= 0) {
							$this->db->query("UPDATE sw_member_game_withdrawals SET profit = ?, processing_status = ?, withdrawal_date = ?, p2_sync_status = ? WHERE member_transaction_id = ? LIMIT 1", array($profit, 1, time(), 1, $row_9->member_transaction_id));
						}
					}
				}
			}
		}	
		
		echo 'Done';
	}
}

/* End of file cronjob.php */
/* Location: ./application/controllers/console.php */