<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>JKRoyalty | Upload Report Youtube</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="../../bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../cloudflare/ajax/libs/font-awesome-4.7.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../../cloudflare/ajax/libs/ionicons-2.0.1/css/ionicons.min.css">
  <!-- DataTables -->
  <link rel="stylesheet" href="../../plugins/datatables/dataTables.bootstrap.css">
  <!-- Select2 -->
  <link rel="stylesheet" href="../../plugins/select2/select2.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="../../dist/css/skins/_all-skins.min.css">
  
  <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>-->
  <script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
</head>
<body>
<div class="form-group">
	<div align="center" style=" margin-top:300px; "><b>Proses Input Data ....</b></div>
	<div align="center" class="progress" style=" margin-left:300px; height:25px; width:50%">
    	<div class="progress-bar progress-bar-striped progress-bar-animated" id="progress-bar" style="height:25px; width:100%; padding:0px">
        	<span id="progess-info"></span>
        </div>
    </div>
</div>

<?php
session_start();
ini_set('max_execution_time', 0);
include "../../conf/conn.php";
error_reporting(E_ALL ^ E_NOTICE); 
require_once '../../conf/excel_reader2.php';

//jika tombol import ditekan 
if($_GET['page']=='three')
{
	
	//---------------------------------------------------------- GRT --------------------------------------------------------------------
	$no = 1;
	$querygrt = mysql_query("SELECT * FROM ttk_tmp GROUP BY group_id ORDER BY group_id");
	$jumlahbaris = mysql_num_rows($querygrt);
	while($data_grt = mysql_fetch_array($querygrt))
	{
		
		//PROGRESS BAR
		$barisXls = $jumlahbaris-1;
		$k = $no-1;
		$percent = @(intval($k/$barisXls * 100))."%";
		// mengupdate progress bar

		echo '<script language="javascript">
		document.getElementById("progress-bar").style.width="'.$percent.'";
		document.getElementById("progess-info").innerHTML="'.($no-1).' record ('.$percent.')";
		</script>';
		//AKHIR PROGRESS BAR

		
		//GRT transaction_count
		$q_nwr = mysql_query("SELECT * FROM ttk_nwr WHERE group_id = '$data_grt[group_id]' AND record_type = 'NWR'");
		$jum_nwr = mysql_num_rows($q_nwr);


		//GRT record_count
		$q_grh = mysql_query("SELECT * FROM ttk_grh WHERE group_id = '$data_grt[group_id]' AND transaction_type = 'NWR'");
		$jum_grh = mysql_num_rows($q_grh);

		$q_seq = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$data_grt[group_id]'");
		$jum_seq = mysql_num_rows($q_seq);

		$jum_grt = '1';

		$jum_record_count = $jum_grh + $jum_seq + $jum_grt;


		//sequence
		$grt_transaction_count = sprintf("%08s", $jum_nwr);
		$grt_record_count = sprintf("%08s", $jum_record_count);




		//setelah data dibaca, masukkan ke tabel grt sql
		$query_grt = "INSERT INTO ttk_grt (record_type,
		group_id,
		transaction_count,
		record_count,
		currency_indicator,
		total_monetary_value)values('GRT',
		'$data_grt[group_id]',
		'$grt_transaction_count',
		'$grt_record_count',
		'',
		'0000000000')";
		mysql_query($query_grt);

	$no++;	
	}
	//-------------------------------------------------------- GRT ---------------------------------------------------------------------


	//-------------------------------------------------------- TRL ---------------------------------------------------------------------
	$no = 1;
	$querytrl = mysql_query("SELECT * FROM ttk_tmp GROUP BY group_id ORDER BY group_id");
	$jumlahbaris = mysql_num_rows($querytrl);
	while($data_trl = mysql_fetch_array($querytrl))
	{
		
		//PROGRESS BAR
		$barisXls = $jumlahbaris-1;
		$k = $no-1;
		$percent = @(intval($k/$barisXls * 100))."%";
		// mengupdate progress bar

		echo '<script language="javascript">
		document.getElementById("progress-bar").style.width="'.$percent.'";
		document.getElementById("progess-info").innerHTML="'.($no-1).' record ('.$percent.')";
		</script>';
		//AKHIR PROGRESS BAR
		
		
		//TRL transaction_count
		$q_nwr = mysql_query("SELECT * FROM ttk_nwr WHERE group_id = '$data_trl[group_id]' AND record_type = 'NWR'");
		$jumtrl_nwr = mysql_num_rows($q_nwr);


		//TRL record_count
		$q_hdr = mysql_query("SELECT * FROM ttk_hdr WHERE record_type = 'HDR'");
		$jum_hdr = mysql_num_rows($q_hdr);

		$q_grh = mysql_query("SELECT * FROM ttk_grh WHERE group_id = '$data_trl[group_id]' AND transaction_type = 'NWR'");
		$jum_grh = mysql_num_rows($q_grh);

		$q_seq = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$data_trl[group_id]'");
		$jum_seq = mysql_num_rows($q_seq);

		$q_grt = mysql_query("SELECT * FROM ttk_grt WHERE group_id = '$data_trl[group_id]' AND record_type = 'GRT'");
		$jum_grt = mysql_num_rows($q_grt);

		$jum_trl = '1';

		$jumtrl_record_count = $jum_hdr + $jum_grh + $jum_seq + $jum_grt + $jum_trl;


		//sequence
		$trl_transaction_count = sprintf("%08s", $jumtrl_nwr);
		$trl_record_count = sprintf("%08s", $jumtrl_record_count);


		//setelah data dibaca, masukkan ke tabel trl sql
		$query_trl = "INSERT INTO ttk_trl (record_type,
		group_count,
		transaction_count,
		record_count)values('TRL',
		'$data_trl[group_id]',
		'$trl_transaction_count',
		'$trl_record_count')";
		mysql_query($query_trl);

	$no++;	
	}
	//-------------------------------------------------------- TRL ---------------------------------------------------------------------

	

	
	/*
	echo '<script>alert("Proses import selesai !!!");
	window.location.href="../../index.php?page=tiktok_cwr"</script>';
	*/

	//sebelum redirect, daftarkan session pesan
    $_SESSION['sukses'] = 'Upload Sukses';
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../../index.php?page=tiktok_cwr">';
	exit;

}
else
{
	/*
	echo '<script>alert("Proses import gagal !!!");
	window.location.href="../../index.php?page=tiktok_cwr"</script>';
	*/

	//sebelum redirect, daftarkan session pesan
    $_SESSION['gagal'] = 'Proses import gagal !!!';
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../../index.php?page=tiktok_cwr">';
	exit;

}
 
?>

<!-- jQuery 2.2.3 -->
<script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../../bootstrap/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="../../plugins/select2/select2.full.min.js"></script>
<!-- DataTables -->
<script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="../../plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="../../dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../../dist/js/demo.js"></script>

</body>
</html>