<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 printGSTreportSummary(obj){
	var df = $('#selDateFrom').val();
	var dt = $('#selDateTo').val();
	var urls = "<?php echo base_url();?>gstReport/gstReportSummaryPrint/"+df+"/"+dt;
	window.open(urls,"_blank");
		window.location = urls;
}

function changeList(obj){
	var df = $('#selDateFrom').val();
	var dt = $('#selDateTo').val();
	var urls = "<?php echo base_url();?>gstReport/gstReportSummary/"+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 $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>
                                <div class="input-prepend">
                                <span class="add-on"><i class="i-calendar"></i></span>
                                <?php
                                                $data = array(
                                                'name' => 'selDateFrom',
                                                'id' => 'selDateFrom',
                                                'type' => 'text',
                                                'class' => 'input-small datepicker2',);
                                                $js='';
                                                echo form_input($data, date("d-m-Y", strtotime($dateFrom)), $js);
                                                
                                            ?>       
                                </div>
                                </div>
                                <div class="span5 tac">
                                <div class="span2 tar"><?php echo form_label($this->lang->line('dateTo'));?></div>
                                <div class="input-prepend">
                                <span class="add-on"><i class="i-calendar"></i></span>
                                <?php
                                                $data = array(
                                                'name' => 'selDateTo',
                                                'id' => 'selDateTo',
                                                'type' => 'text',
                                                'class' => 'input-small datepicker2',);
                                                $js='';
                                                echo form_input($data, date("d-m-Y", strtotime($dateTo)), $js);
                                            ?>       
                                </div>
                                </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(); ?>gstReport/gstReportSummaryPrint/<?php echo date("d-m-Y", strtotime($dateFrom))."/".date("d-m-Y", strtotime($dateTo));?>" class="btn-form btn-success-form" title="GST 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 -->
                        <div>
                            <b><?php echo $this->lang->line('supply') ?></b>
                        </div>
                        <?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'>#</th>
                                    <th class='tac'>".$this->lang->line('taxCode')."</th>";
									for($i=0; $i<$monthDiff; $i++){
										if($monthDiff > 1){
												if($i == 0){	
													if(date("M", strtotime($i." months", strtotime($dateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
													}
												}
												else{
													if(date("M", strtotime($i." months", strtotime($dateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
													}
												}
										}
										else{
											echo "<th class='tac'>".date("M", strtotime($dateFrom))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
										}
									}
									echo "<th class='tac'>".$this->lang->line('total')."</th>";
                                "</tr>
                                </thead>
                                <tbody>";
                                    	$bil = 0;
										//$bil2 = 0;
										$totCol = array();
										if($datatbls){
										foreach($datatbls as $datatbl):
										$bil++;
										echo"<tr class='itemList'>
                                            <td class='tac'>".$bil."</td>
                                        	<td class='tac'>".$datatbl->code."</td>";
											//for($i=0; $i<$monthDiff; $i++){
											$total = 0;
											$i = 0;
											
										
											foreach ($datatbl as $col => $val) {
												if (strtoupper(substr($col,0,5))=='MONTH'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
													//echo "<td class='tac'>".$datatbl->month3."</td>";
											}
										
											//foreach ($totSales as $totSale) {
												echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>';
												//echo '<td align="right">'.number_format($totSales[$bil]->totGstSales, 2, '.', ',').'</td>';
												//$bil2++;
											//}
                                        "</tr>";
										
										
									endforeach;
											}
									$bil = $bil + 1;
									echo"<tr>
											<td class='tac'></td>
											<td class='tar'><b>".$this->lang->line('total')."</b></td>";
											//var_dump($totCol);
											$totAmount = 0;
											if($datatbls){
												for($j=0; $j<$i; $j++){
													$totAmount += $totCol[$j];
													echo '<td align="right"><b>'.number_format($totCol[$j], 2, '.', ',').'</b></td>';
												}
											}
											else{
												for($j=0; $j<$i; $j++){
													echo '<td align="right"><b>'.number_format(0, 2, '.', ',').'</b></td>';
												}
											}
											echo"<td align='right'><b>".number_format($totAmount, 2, '.', ',')."</b></td>";
										"</tr>";
                                echo"</tbody>         
                            </table>
                        </div><!-- content -->";  ?>                            
                    </div><!-- block -->
                    
                    <div>
                    	<b><?php echo $this->lang->line('purchase') ?></b>
                    </div>
                    <div class="block">
                        <?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'>#</th>
                                    <th class='tac'>".$this->lang->line('taxCode')."</th>";
									for($i=0; $i<$monthDiff; $i++){
										if($monthDiff > 1){
												if($i == 0){	
													if(date("M", strtotime($i." months", strtotime($dateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
														//$count = 0;
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
													}
												}
												else{
													if(date("M", strtotime($i." months", strtotime($dateFrom))) == "Dec"){
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
														$count ++;
													}
													else{
														echo "<th class='tac'>".date("M", strtotime($i." months", strtotime($dateFrom)))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
													}
												}
										}
										else{
											echo "<th class='tac'>".date("M", strtotime($dateFrom))."'".date("y", strtotime($count." years", strtotime($dateFrom)))."</th>";
										}
									}
									echo "<th class='tac'>".$this->lang->line('total')."</th>";
                                "</tr>
                                </thead>
                                <tbody>";
                                    	$bil = 0;
										//$bil2 = 0;
										$totCol = array();
										if($datatbls2){
										foreach($datatbls2 as $datatbl2):
										$bil++;
										echo"<tr class='itemList'>
                                            <td class='tac'>".$bil."</td>
                                        	<td class='tac'>".$datatbl2->code."</td>";
											//for($i=0; $i<$monthDiff; $i++){
											$total = 0;
											$i = 0;
											
											foreach ($datatbl2 as $col => $val) {
												if (strtoupper(substr($col,0,5))=='MONTH'){
													echo '<td align="right">'.number_format($val, 2, '.', ',').'</td>';
													$total += $val;
													if($bil == 1)
														$totCol[] += $val;
													else
														$totCol[$i] += $val;	
													$i++;
												}
													//echo "<td class='tac'>".$datatbl->month3."</td>";
											}
											//foreach ($totSales as $totSale) {
												echo '<td align="right"><b>'.number_format($total, 2, '.', ',').'</b></td>';
												//echo '<td align="right">'.number_format($totSales[$bil]->totGstSales, 2, '.', ',').'</td>';
												//$bil2++;
											//}
                                        "</tr>";
										
										
									endforeach;
											}
									$bil = $bil + 1;
									echo"<tr>
											<td class='tac'></td>
											<td class='tar'><b>".$this->lang->line('total')."</b></td>";
											//var_dump($totCol);
											$totAmount = 0;
											if($datatbls2){
												for($j=0; $j<$i; $j++){
													$totAmount += $totCol[$j];
													echo '<td align="right"><b>'.number_format($totCol[$j], 2, '.', ',').'</b></td>';
												}
											}
											else{
												for($j=0; $j<$i; $j++){
													echo '<td align="right"><b>'.number_format(0, 2, '.', ',').'</b></td>';
												}
											}
											echo"<td align='right'><b>".number_format($totAmount, 2, '.', ',')."</b></td>";
										"</tr>";
                                echo"</tbody>         
                            </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>  

