<script type="text/javascript">
var oTable;
$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
   	$('select').select2();
   	$.each($(".select2-container"), function (i, n) {
        $(n).next().show().fadeTo(0,0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
        $(n).prepend($(n).next());
        $(n).delay(500).queue(function () {
            $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
            $(this).dequeue();
        });
    });	
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
    //Data table configurations
	
    $("table .checkall1").live("click", "checkall1",function(){           
		$(oTable.fnGetNodes()).find(':checkbox').prop('checked', this.checked);
	});
    
	
	$("table.editable").on("click",".remove",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_delete").dialog("open");
    });
	
	//Change types accourding to taxGroup
	$('#gstGroup').live('change',function(){
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>gstReport/getType/",
			data: 'taxGroup='+$('#gstGroup').select2('val')+"&taxType="+$('#gstType').select2('val'),
			async: false,
			success: function(data){
				$('#gstType').html(data.type);
				$('#gstCode').html(data.code);
			} //end success		
		}); //end ajax	
	});
	
	$('#gstType').live('change',function(){
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>gstReport/getType/",
			data: 'taxGroup='+$('#gstGroup').select2('val')+"&taxType="+$('#gstType').select2('val'),
			async: false,
			success: function(data){
				$('#gstCode').html(data.code);
			} //end success		
		}); //end ajax	
	});
});

function selectYearPeriod(obj){
	var year = obj.value;
	//		alert(year);
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "<?php echo base_url()?>glreports/get_TrialBalance_year/"+year,
		data: year,
		async: false,
		success: function(content){
			console.log(content.status);
			if(content.status == "success") {
				var items = [];
				//items.push('<option>-Please Select-</option>');
				//items.push('<option value="NULL" selected="selected">Period</option>');
				for ( var i = 0; i < content.message.length; i++) {
				items.push('<option value="'+content.message[i].period+'">'
					+ content.message[i].financialPeriod + '</option>');
				}  //end for
				jQuery("#periodFrom").empty();
				jQuery("#periodTo").empty();
				$("#periodFrom").html(items).select2('val','');
				$("#periodTo").html(items).select2('val','');
				//jQuery("#add_period").val(null);
				$("#add_period").select2();
			} else {
				jQuery("#periodFrom").empty();
				jQuery("#periodTo").empty();
				alert("Financial Year not found");
				$("#add_period").select2('val',null);
			} //end if
		} //end success		
	}); //end ajax
}

function changeList(obj){
	var df = $('#periodFrom').val();
	//var df = $("#periodFrom").select2('val');
	var dt = $('#periodTo').val();
	var urls = "<?php echo base_url();?>gst03/gst03Summary/"+df+"/"+dt;
		window.location = urls;
}

function getGST(){
	if(parseInt($('#periodFrom').select2('val')) > parseInt($('#periodTo').select2('val'))){
		var temp = $('#periodFrom').select2('val');
		$('#periodFrom').select2('val',$('#periodTo').select2('val'));
		$('#periodTo').select2('val',temp);
	}

	if($("#gstReport").validationEngine('validate')){
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>gstReport/getReport/",
			data: {
					 year 		: $("#add_year").select2('val'),
					 periodFrom : $("#periodFrom").select2('val'),
					 periodTo 	: $("#periodTo").select2('val'),
					 gstGroup 	: $("#gstGroup").select2('val'),
					 gstType 	: $("#gstType").select2('val'),
					 gstCode 	: $("#gstCode").select2('val')
					},
			async: false,
			success: function(data){
				if(data != false){	
					//Items listing
					var serial = 1;
					oTable.fnClearTable();
					jQuery.each(data.data,function(i,v){
												
						var rowIndex = oTable.fnAddData( [
												serial,
												v.transType,
												v.ref,
												v.effective_date,
												v.customerName,
												v.itemName,
												formatNumber(parseFloat(v.amountExcludedTax).toFixed(2)),
												formatNumber(parseFloat(v.taxRate).toFixed(2)),
												formatNumber(parseFloat(v.taxAmount).toFixed(2)),
												formatNumber(parseFloat(v.amountIncludedTax).toFixed(2)),
												v.gst,														
											] );
						var row = oTable.fnGetNodes(rowIndex);
						$(row).find('td:nth-child(7),td:nth-child(8),td:nth-child(9),td:nth-child(10)').addClass('tar');
						serial++;
					});
				}else{
					oTable.fnClearTable();
				}
				
			} //end success		
		}); //end ajax
	}
}


</script>

<div id="content">                        
<div class="wrap">
	<div class="head">
		<div class="info">
			<!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
				<?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
			<ul class="breadcrumb">
            	<li><a href="#"><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href="<?php echo base_url() ?>ap/home"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('title') ?></li>
            </ul>
		</div>
		<div class="search">
			<form action="<?php echo base_url() ?>admin/search" method="post">
				<input name="search_text" type="text" placeholder="search..."/>                                
            	<button type="submit"><span class="i-magnifier"></span></button>
			</form>
		</div>                        
	</div><!-- head --> 
    <div class="content">
        <div class="wrap">                    
            <div class="row-fluid">
                <div class="span12">
                    <div class="block">
                    	
                    	<div class="head">
                    		<h2><?php echo "Period"//$this->lang->line('date') ?></h2>
                    	</div><!-- head -->
                        <div class="content np">    
                            <div class="controls-row"><!-- Row 4-->
                                <div class="span5 tac">
                                <div class="span2 tar"><?php echo form_label($this->lang->line('dateFrom'));?></div>
                                <?php
									//$selperiodFrom = $periodFrom;
									$selperiodFrom = $periodFromID;
									$options = '';
									$options[] = $this->lang->line('dateFrom');
									//var_dump($period);
									if($period){
										foreach($period as $row){
											$options[$row->ID] = $row->financialPeriod;
										}
									}
									$js='class="input-medium" id="periodFrom" style="width:75%"';
		
									echo form_dropdown('periodFrom', $options, $selperiodFrom, $js);
								?>
                                </div>
                                <div class="span5 tac">
                                <div class="span2 tar"><?php echo form_label($this->lang->line('dateTo'));?></div>
                                <?php
									//$selperiodTo = $periodTo;
									$selperiodTo = $periodToID;
									$options = '';
									$options[] = $this->lang->line('dateTo');
									if($period){
										foreach($period as $row){
											$options[$row->ID]= $row->financialPeriod;
										}
									}
									$js='class="input-medium" id="periodTo" style="width:75%"';
		
									echo form_dropdown('periodTo', $options, $selperiodTo, $js);
								?>
                                </div>
                                     <div class="span2 tac">
                                	<?php 
                                    $data = array(
                                            'name'=>'mysubmit', 
                                            'class'=>'btn-form btn-success-form');
                                            $js="onClick='changeList()'";
                                    echo form_button($data,$this->lang->line('submit'),$js);
                                    ?>
                                </div>
                            </div><!-- End Row 4-->
                        </div><!-- content -->
                                                
                        <div class="head">
                            <h2><?php echo $this->lang->line('titlelist') ?></h2>
                            <div class="side fr">
                                 <a href="<?php echo base_url(); ?>gst03/gst03SummaryPrint/<?php echo $periodFromID;?>/<?php echo $periodToID;?>" class="btn-form btn-success-form" title="GST 03 Report print" target="_blank"><?php echo $this->lang->line('print') ?></a>
                                 <!--
                                 <a href="" class="btn-form btn-success-form" title="GST Report print" onclick="printGSTreportSummary()"><?php echo $this->lang->line('print') ?></a>
                                 
                                 <li><a href="<?php echo base_url(); ?>locationtransfer/add" title="Add New"><span class="i-plus-2"></span></a></li>  -->
                            </div>
                        </div><!-- head -->
                        <?php
						$count = 0;
                        echo "<div class='content np table-sorting'>
                            <table cellpadding='0' cellspacing='0' width='100%' id='test' class='editable'>
                                <thead>
                                <tr>
                                    <th class='tac'>No.</th>
                                    <th class='tac'>".$this->lang->line('itemDesc')."</th>";
									for($i=0; $i<$periodDiff; $i++){
										if($periodDiff > 1){
												if($i == 0){	
													if(date("M", strtotime($i." months", strtotime($startDateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($startDateFrom)))."'".date("y", strtotime($count." years", strtotime($startDateFrom)))."</th>";
														//$count = 0;
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($startDateFrom)))."'".date("y", strtotime($count." years", strtotime($startDateFrom)))."</th>";
													}
												}
												else{
													if(date("M", strtotime($i." months", strtotime($startDateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($startDateFrom)))."'".date("y", strtotime($count." years", strtotime($startDateFrom)))."</th>";
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($startDateFrom)))."'".date("y", strtotime($count." years", strtotime($startDateFrom)))."</th>";
													}
												}
										}
										else{
											echo "<th class='tac'>".date("M", strtotime($startDateFrom))."'".date("y", strtotime($count." years", strtotime($startDateFrom)))."</th>";
										}
									}
									echo "<th class='tac'>".$this->lang->line('total')."</th>";
                                "</tr>
                                </thead>
								<tbody>";
								foreach($datatbls as $datatbl):
                                echo"<tr>
									<td class='tac'>5</td>
									<td>".$this->lang->line('outputTax')."</td>";
									foreach ($datatbl as $col => $val) {
										if (strtoupper(substr($col,0,8))=='MONTHB5A'){
											echo '<td></td>';
										}
									}
									echo"<td></td>
								</tr>";
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'></td>
									<td>".$this->lang->line('5a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHB5A'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'></td>
									<td>".$this->lang->line('5b')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHB5B'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								foreach($datatbls as $datatbl):
								echo"<tr>
									<td class='tac' height='10'></td>
									<td></td>";
									foreach ($datatbl as $col => $val) {
										if (strtoupper(substr($col,0,8))=='MONTHB6A'){
											echo '<td></td>';
										}
									}
									echo"<td></td>
								</tr>";
								endforeach;
								
								foreach($datatbls as $datatbl):
								echo"<tr>
									<td class='tac'>6</td>
									<td>".$this->lang->line('inputTax')."</td>";
									foreach ($datatbl as $col => $val) {
										if (strtoupper(substr($col,0,8))=='MONTHB6A'){
											echo '<td></td>';
										}
									}
									echo"<td></td>
								</tr>";
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'></td>
									<td>".$this->lang->line('6a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHB6A'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'></td>
									<td>".$this->lang->line('6b')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHB6B'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								foreach($datatbls as $datatbl):
								echo"<tr>
									<td class='tac' height='10'></td>
									<td></td>";
									foreach ($datatbl as $col => $val) {
										if (strtoupper(substr($col,0,7))=='MONTHB7'){
											echo '<td></td>';
										}
									}
									echo"<td></td>
								</tr>";
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>7</td>
									<td>".$this->lang->line('7a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,7))=='MONTHB7'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>8</td>
									<td>".$this->lang->line('8a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,7))=='MONTHB8'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								foreach($datatbls as $datatbl):
								echo"<tr>
									<td class='tac' height='10'></td>
									<td></td>";
									foreach ($datatbl as $col => $val) {
										if (strtoupper(substr($col,0,8))=='MONTHC10'){
											echo '<td></td>';
										}
									}
									echo"<td></td>
								</tr>";
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>10</td>
									<td>".$this->lang->line('10a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC10'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>11</td>
									<td>".$this->lang->line('11a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC11'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>12</td>
									<td>".$this->lang->line('12a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC12'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>13</td>
									<td>".$this->lang->line('13a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC13'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>14</td>
									<td>".$this->lang->line('14a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC14'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>15</td>
									<td>".$this->lang->line('15a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC15'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>16</td>
									<td>".$this->lang->line('16a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC16'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>17</td>
									<td>".$this->lang->line('17a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC17'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								$bil = 0;
								$totCol = array();
								foreach($datatbls as $datatbl):
								$bil++;
								echo"<tr>
									<td class='tac'>18</td>
									<td>".$this->lang->line('18a')."</td>";
									$total = 0;
									$i = 0;
									foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,8))=='MONTHC18'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
											}
									echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>
								</tr>';
								endforeach;
								
								echo"</tbody>";        
                            echo"</table>
                        </div><!-- content -->";  ?>                            
                    </div><!-- block -->
                  
                </div><!-- span12 -->
            </div><!-- row-fluid -->	
        </div><!-- wrap -->
    </div><!-- content -->
	</div><!-- wrap -->
</div><!-- content -->

<div class="dialog" id="row_edit" style="display: none;" title=". <?php echo $this->lang->line("edit") ?> . ?">
    <p><?php echo $this->lang->line('message1');?></p>
</div>  

