<!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();
include "../../conf/conn.php";

if(isset($_POST['submit']))
{

	
	//$my_file = 'CW190001JK_269.V21'; //CW 2DigitTahun 4DigitNomorUrutPengiriman 2DigitInitialPerusahaan 3digitsociety 3DigitExtensionFile
	//cari id transaksi terakhir yang berawalan tanggal hari ini
	/*
	$query = "SELECT no_id, max(sequence) AS last FROM spy_file ORDER BY no_id DESC";
	$hasil = mysql_query($query);
	$data  = mysql_fetch_array($hasil);
	$lastNoInvoice = $data['last'];

	//baca nomor urut transaksi dari id transaksi terakhir
	$lastNoUrut = substr($lastNoInvoice, 4, 4);

	if($lastNoUrut != 0 or $lastNoUrut != '')
	{
		//nomor urut ditambah 1
		$nextNoUrut = $lastNoUrut + 1; //4digit
	}
	else
	{
		$nextNoUrut = '0001';
	}
	*/

	$nextNoUrut = $_POST['sequence'];


	$name_file = 'CW'.date('y').$nextNoUrut.'JK_'.'269'.'.'.'V21';
	$my_file = 'hasilGenerate'.'/'.'CW'.date('y').$nextNoUrut.'JK_'.'269'.'.'.'V21'; //269 menunjukan kode society WAMI
	$doc_file = 'docFile'.'/'.'CW'.date('y').$nextNoUrut.'JK_'.'269'.'.'.'V21'; //269 menunjukan kode society WAMI
	$handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);
	$dochandle = fopen($doc_file, 'a') or die('Cannot open file:  '.$doc_file);


	//Input nama identitas file CWR
	mysql_query("INSERT INTO ttk_file SET sequence = '$nextNoUrut', name_file = '$name_file', status_download = ''");
	mysql_query("INSERT INTO ttk_document SET sequence = '$nextNoUrut', name_file = '$name_file'");

	
	//TRANSMISSION HEADER
	//HDR
	//$query_hdr = mysql_query("SELECT * FROM ttk_hdr WHERE record_type = '$data_sequence_gi[record_type]'");
	$query_hdr = mysql_query("SELECT * FROM ttk_hdr");
	$data_hdr = mysql_fetch_array($query_hdr);
	//if($data_hdr['record_type'] == $data_sequence_gi['record_type'])
	//{
		$hdr = $data_hdr['record_type'].
				$data_hdr['sender_type'].
	   			str_pad($data_hdr['sender_id'], 9, " ").
				str_pad($data_hdr['sender_name'], 45, " ").
	   			str_pad($data_hdr['edi_standar_version_number'], 5, " ").
	   			str_pad($data_hdr['creation_date'], 8, " ").
	   			str_pad($data_hdr['creation_time'], 6, " ").
	   			str_pad($data_hdr['transmission_date'], 8, " ").
	   			str_pad($data_hdr['character_set'], 15, " ");
		fwrite($handle, $hdr);
		fwrite($dochandle, $hdr);
		fwrite($handle, "\r\n");
		fwrite($dochandle, "\r\n");
	//}	

	
	//GRH
		//$new_data = "\n".'New data line 2';
		/*
		$query_grh = mysql_query("SELECT * FROM ttk_grh");	
		$data_grh = mysql_fetch_array($query_grh);
			$grh = $data_grh['record_type'].
	   		$data_grh['transaction_type'].
	   			str_pad($data_grh['group_id'], 5, " ").
	   			str_pad($data_grh['version_number'], 5, " ").
	   			str_pad($data_grh['batch_request'], 10, " ").
	   			str_pad($data_grh['sub_distro_type'], 2, " ");
			fwrite($handle, $grh);
			fwrite($dochandle, $grh);
			fwrite($handle, "\r\n");
		*/

	//GROUP
	$no = 1;
	$query_sequence_gi = mysql_query("SELECT * FROM ttk_sequence GROUP BY group_id ORDER BY group_id");
	$jumlahbaris = mysql_num_rows($query_sequence_gi);
	while($data_sequence_gi = mysql_fetch_array($query_sequence_gi))
	{
		
		//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


		//GRH
		//$new_data = "\n".'New data line 2';
		$query_grh = mysql_query("SELECT * FROM ttk_grh WHERE group_id = '$data_sequence_gi[group_id]'");	
		$data_grh = mysql_fetch_array($query_grh);
		if($data_grh['group_id'] == $data_sequence_gi['group_id'])
		{
			$grh = $data_grh['record_type'].
	   		$data_grh['transaction_type'].
	   			str_pad($data_grh['group_id'], 5, " ").
	   			str_pad($data_grh['version_number'], 5, " ").
	   			str_pad($data_grh['batch_request'], 10, " ").
	   			str_pad($data_grh['sub_distro_type'], 2, " ");
			fwrite($handle, $grh);
			fwrite($dochandle, $grh);
			preg_replace('/\s+/','', fwrite($handle, "\r\n"));
			preg_replace('/\s+/','', fwrite($dochandle, "\r\n"));
		}
		

		//TRANSACTION HEADER
		$query_sequence_ts = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$data_sequence_gi[group_id]' GROUP BY transaction_sequence");
		while($data_sequence_ts = mysql_fetch_array($query_sequence_ts))
		{

			///NWR///
			$query_nwr = mysql_query("SELECT * FROM ttk_nwr WHERE group_id = '$data_sequence_ts[group_id]' 
									AND transaction_sequence = '$data_sequence_ts[transaction_sequence]'");
			$data_nwr = mysql_fetch_array($query_nwr);
			if($data_nwr['record_sequence'] == $data_sequence_ts['record_sequence'])
			{
  				$nwr = $data_nwr['record_type'].
					$data_nwr['transaction_sequence'].
					$data_nwr['record_sequence'].
						str_pad($data_nwr['work_title'], 60, " ").
						str_pad($data_nwr['language_code'], 2, " ").
						str_pad($data_nwr['submitter_work_number'], 14, " ").
						str_pad($data_nwr['iswc'], 11, " ").
						str_pad($data_nwr['copy_right_date'], 8, " ").
						str_pad($data_nwr['copyright_number'], 12, " ").
						str_pad($data_nwr['musical_work_distribution'], 3, " ").
						str_pad($data_nwr['duration'], 6, " ").
						str_pad($data_nwr['recorded_indicator'], 1, " ").
						str_pad($data_nwr['text_music_relationship'], 3, " ").
						str_pad($data_nwr['composite_type'], 3, " ").
						str_pad($data_nwr['version_type'], 3, " ").
						str_pad($data_nwr['excerpt_type'], 3, " ").
						str_pad($data_nwr['music_arrangement'], 3, " ").
						str_pad($data_nwr['lyric_adaptation'], 3, " ").
						str_pad($data_nwr['contact_name'], 30, " ").
						str_pad($data_nwr['contact_id'], 10, " ").
						str_pad($data_nwr['cwr_work_type'], 2, " ").
						str_pad($data_nwr['grand_right_indicator'], 1, " ").
						str_pad($data_nwr['composite_component_count'], 3, " ").
						str_pad($data_nwr['date_of_publication_of_printed_edition'], 8, " ").
						str_pad($data_nwr['exceptional_clause'], 1, " ").
						str_pad($data_nwr['opus_number'], 25, " ").
						str_pad($data_nwr['catalogue_number'], 25, " ").
						str_pad($data_nwr['priority_flag'], 1, " ");

  					fwrite($handle, $nwr);
  					fwrite($dochandle, $nwr);
			}
			
		
			//TRANSACTION DETAIL
			$query_sequence_rs = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$data_sequence_ts[group_id]' 
											AND transaction_sequence = '$data_sequence_ts[transaction_sequence]' 
								  	  		ORDER BY record_sequence ASC");
			$check = mysql_num_rows($query_sequence_rs);								
			while($data_sequence_rs = mysql_fetch_array($query_sequence_rs))
			{
				
				//SPU//
				$query_spu = mysql_query("SELECT * FROM ttk_spu WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
										AND record_type = '$data_sequence_rs[record_type]' 
								  	  	AND seq_spu = '$data_sequence_rs[seq_record_type]'");
				$data_spu = mysql_fetch_array($query_spu);
				
				if($data_spu['seq_spu'] == $data_sequence_rs['seq_record_type'])
				{
					$spu = $data_spu['record_type'].
					   $data_spu['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_spu['publisher_sequence_number'], 2, " ").
					   str_pad($data_spu['interested_party_number'], 9, " ").
					   str_pad($data_spu['publisher_name'], 45, " ").
					   str_pad($data_spu['publisher_unknown_indicator'], 1, " ").
					   str_pad($data_spu['publisher_type'], 2, " ").
					   str_pad($data_spu['tax_id_number'], 9, " ").
					   str_pad($data_spu['publisher_ipi_name_number'], 11, " ").
					   str_pad($data_spu['submitter_agreement_number'], 14, " ").
					   str_pad($data_spu['pr_affiliation_society'], 3, " ").
					   str_pad($data_spu['pr_ownership_share'], 5, " ").
					   str_pad($data_spu['mr_affiliation_society'], 3, " ").
					   str_pad($data_spu['mr_ownership_share'], 5, " ").
					   str_pad($data_spu['sr_affiliation_society'], 3, " ").
					   str_pad($data_spu['sr_ownership_share'], 5, " ").
					   str_pad($data_spu['special_agreements_indicator'], 1, " ").
					   str_pad($data_spu['first_recording_refusal_indicator'], 1, " ").
					   str_pad($data_spu['filler'], 1, " ").
					   str_pad($data_spu['publisher_ipi_base_number'], 13, " ").
					   str_pad($data_spu['isac'], 14, " ").
					   str_pad($data_spu['saan'], 14, " ").
					   str_pad($data_spu['agreement_type'], 2, " ").
					   str_pad($data_spu['usa_license_indicator'], 1, " ");
				
					fwrite($handle, $spu);
					fwrite($dochandle, $spu);
				}
			

				//SPT//
				$query_spt = mysql_query("SELECT * FROM ttk_spt WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
								  	  		AND record_type = '$data_sequence_rs[record_type]' 
											AND seq_spt = '$data_sequence_rs[seq_record_type]'");
				$data_spt = mysql_fetch_array($query_spt);
				
				if($data_spt['seq_spt'] == $data_sequence_rs['seq_record_type'])
				{
					$spt = $data_spt['record_type'].
					   $data_spt['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_spt['interested_party_number'], 9, " ").
					   str_pad($data_spt['constant'], 6, " ").
					   str_pad($data_spt['pr_collection_share'], 5, " ").
					   str_pad($data_spt['mr_collection_share'], 5, " ").
					   str_pad($data_spt['sr_collection_share'], 5, " ").
					   str_pad($data_spt['inclusion_exclusion_indicator'], 1, " ").
					   str_pad($data_spt['tis_numeric_code'], 4, " ").
					   str_pad($data_spt['shares_change'], 1, " ").
					   str_pad($data_spt['sequence_number'], 3, " ");
				
					fwrite($handle, $spt);
					fwrite($dochandle, $spt);

		  		}
				
				
				
				//SWR//
				$query_swr = mysql_query("SELECT * FROM ttk_swr WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
								  	  		AND record_type = '$data_sequence_rs[record_type]' 
											AND seq_swr = '$data_sequence_rs[seq_record_type]'");
				$data_swr = mysql_fetch_array($query_swr);

				if($data_swr['seq_swr'] == $data_sequence_rs['seq_record_type'])
				{
					$swr = $data_swr['record_type'].
					   $data_swr['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_swr['interested_party_number'], 9, " ").
					   str_pad($data_swr['writer_last_name'], 45, " ").
					   str_pad($data_swr['writer_first_name'], 30, " ").
					   str_pad($data_swr['writer_unknown_indicator'], 1, " ").
					   str_pad($data_swr['writer_designation_code'], 2, " ").
					   str_pad($data_swr['tax_id_number'], 9, " ").
					   str_pad($data_swr['writer_ipi_name_number'], 11, " ").
					   str_pad($data_swr['pr_affiliation_society'], 3, " ").
					   str_pad($data_swr['pr_ownership_share'], 5, " ").
					   str_pad($data_swr['mr_affiliation_society'], 3, " ").
					   str_pad($data_swr['mr_ownership_share'], 5, " ").
					   str_pad($data_swr['sr_affiliation_society'], 3, " ").
					   str_pad($data_swr['sr_ownership_share'], 5, " ").
					   str_pad($data_swr['reversionary_indicator'], 1, " ").
					   str_pad($data_swr['first_recording_refusal_indicator'], 1, " ").
					   str_pad($data_swr['work_for_hire_indicator'], 1, " ").
					   str_pad($data_swr['filler'], 1, " ").
					   str_pad($data_swr['writer_ipi_base_number'], 13, " ").
					   str_pad($data_swr['personal_number'], 12, " ").
					   str_pad($data_swr['usa_licence_indicator'], 1, " ");
				
					fwrite($handle, $swr);
					fwrite($dochandle, $swr);
		  		}
		
		
				//SWT//
				$query_swt = mysql_query("SELECT * FROM ttk_swt WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
								  	  		AND record_type = '$data_sequence_rs[record_type]' 
											AND seq_swt = '$data_sequence_rs[seq_record_type]'");
				$data_swt = mysql_fetch_array($query_swt);

				if ($data_swt['seq_swt'] == $data_sequence_rs['seq_record_type'])
				{
					$swt = $data_swt['record_type'].
					   $data_swt['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_swt['interested_party_number'], 9, " ").
					   str_pad($data_swt['pr_collection_share'], 5, " ").
					   str_pad($data_swt['mr_collection_share'], 5, " ").
					   str_pad($data_swt['sr_collection_share'], 5, " ").
					   str_pad($data_swt['inclusion_exclusion_indicator'], 1, " ").
					   str_pad($data_swt['tis_numeric_code'], 4, " ").
					   str_pad($data_swt['shares_change'], 1, " ").
					   str_pad($data_swt['sequence_number'], 3, " ");
				
					fwrite($handle, $swt);
					fwrite($dochandle, $swt);
		  		}
			
		
				//PWR//
				$query_pwr = mysql_query("SELECT * FROM ttk_pwr WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
								  	  		AND record_type = '$data_sequence_rs[record_type]' 
									  		AND seq_pwr = '$data_sequence_rs[seq_record_type]'");
				$data_pwr = mysql_fetch_array($query_pwr);
				
				if($data_pwr['seq_pwr'] == $data_sequence_rs['seq_record_type'])
				{
					$pwr = $data_pwr['record_type'].
					   $data_pwr['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_pwr['publisher_interested_party_number'], 9, " ").
					   str_pad($data_pwr['publisher_name'], 45, " ").
					   str_pad($data_pwr['submitter_agreement_number'], 14, " ").
					   str_pad($data_pwr['saan'], 14, " ").
					   str_pad($data_pwr['writer_interested_party_number'], 9, " ");
				
					fwrite($handle, $pwr);
					fwrite($dochandle, $pwr);
		  		}
		
		
				//OWR//
				$query_owr = mysql_query("SELECT * FROM ttk_owr WHERE transaction_sequence = '$data_sequence_rs[transaction_sequence]' 
								  	  	AND record_type = '$data_sequence_rs[record_type]' 
										AND seq_owr = '$data_sequence_rs[seq_record_type]'");
				$data_owr = mysql_fetch_array($query_owr);
				
				if($data_owr['seq_owr'] == $data_sequence_rs['seq_record_type'])
				{
					$owr = $data_owr['record_type'].
					   $data_owr['transaction_sequence'].
					   $data_sequence_rs['record_sequence'].
					   str_pad($data_owr['interested_party_number'], 9, " ").
					   str_pad($data_owr['writer_last_name'], 45, " ").
					   str_pad($data_owr['writer_first_name'], 30, " ").
					   str_pad($data_owr['writer_unknown_indicator'], 1, " ").
					   str_pad($data_owr['writer_designation_code'], 2, " ").
					   str_pad($data_owr['tax_id_number'], 9, " ").
					   str_pad($data_owr['writer_ipi_name_number'], 11, " ").
					   str_pad($data_owr['pr_affiliation_society'], 3, " ").
					   str_pad($data_owr['pr_ownership_share'], 5, " ").
					   str_pad($data_owr['mr_affiliation_society'], 3, " ").
					   str_pad($data_owr['mr_ownership_share'], 5, " ").
					   str_pad($data_owr['sr_affiliation_society'], 3, " ").
					   str_pad($data_owr['sr_ownership_share'], 5, " ").
					   str_pad($data_owr['reversionary_indicator'], 1, " ").
					   str_pad($data_owr['first_recording_refusal_indicator'], 1, " ").
					   str_pad($data_owr['work_for_hire_indicator'], 1, " ").
					   str_pad($data_owr['filler'], 1, " ").
					   str_pad($data_owr['writer_ipi_base_number'], 13, " ").
					   str_pad($data_owr['personal_number'], 12, " ").
					   str_pad($data_owr['usa_licence_indicator'], 1, " ");
				
					fwrite($handle, $owr);
					fwrite($dochandle, $owr);
		  		}
		
			fwrite($handle, "\r\n");
			fwrite($dochandle, "\r\n");

			}
	
			
		}


		//GRT
		$query_grt = mysql_query("SELECT * FROM ttk_grt WHERE group_id = '$data_sequence_gi[group_id]'");	
		$data_grt = mysql_fetch_array($query_grt);
		$grt = $data_grt['record_type'].
	   		str_pad($data_grt['group_id'], 5, " ").
	   		str_pad($data_grt['transaction_count'], 8, " ").
	   		str_pad($data_grt['record_count'], 8, " ").
	   		str_pad($data_grt['currency_indicator'], 3, " ").
	   		str_pad($data_grt['total_monetary_value'], 10, " ");
		fwrite($handle, $grt);
		fwrite($dochandle, $grt);

	$no++;	
	}


	fwrite($handle, "\r\n");
	fwrite($dochandle, "\r\n");
	//TRANSMISSION TRAILER
	//TRL
	$query_trl = mysql_query("SELECT * FROM ttk_trl");	
	$data_trl = mysql_fetch_array($query_trl);
	$trl = $data_trl['record_type'].
	   str_pad($data_trl['group_count'], 5, " ").
	   str_pad($data_trl['transaction_count'], 8, " ").
	   str_pad($data_trl['record_count'], 8, " ");
	fwrite($handle, $trl);
	fwrite($dochandle, $trl);


	if($_POST["status_cwr"]=='createCWR')
	{	
		mysql_query("UPDATE ttk_file SET status_download = '1' WHERE status_download = ''");
		mysql_query("UPDATE ttk_hdr SET status_download = '1' WHERE status_download = ''");
	}


	//echo '<script>alert("Create CWR Sukses !!!");
	//	window.location.href="../../index.php?page=tiktok_cwr"</script>';


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

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

	//sebelum redirect, daftarkan session pesan
	$_SESSION['gagal'] = 'Create CWR 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>