<script type="text/javascript">
$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
   $('select').select2();
}); 
 
function changeList(){
	var addyear = $('#add_year option:selected').val();
	var addper = $('#add_period option:selected').val();
	var urls = "<?php echo base_url();?>taxTransaction/processGAF/"+addyear+"/"+addper;
		window.location = urls;
}


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){
					if(content.status == "success") {
					var items = [];
					//items.push('<option>-Please Select-</option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
						+ content.message[i].financialPeriod + '</option>');
					}  //end for
					jQuery("#add_period").empty();
					jQuery("#add_period").append(items.join('</br>'));
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				} //end if
			} //end success
				
			}); //end ajax
//	window.location.reload(true); 
}
function createGAF(){
	var addyear = $('#add_year option:selected').val();
	var addper = $('#add_period option:selected').val();
	var url = "<?php echo base_url();?>taxTransaction/processGAF_excell/"+addyear+"/"+addper;
	window.open(url);
}
</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 $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('title') ?></li>
            </ul>
		</div><!-- info -->
	<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('title') ?></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('year'),'add_year');?></div>
						<?php
							$preselyear = $selyear;
							$options = '';
							foreach($yrs as $row){
								$options[$row] = $row;
							}
							$js='onchange="selectYearPeriod(this)" class="input-medium" id="add_year"';

							echo form_dropdown('add_year', $options, $preselyear, $js);
						?>       
                        </div>
						<div class="span5 tac">
                    	<div class="span2 tar"><?php echo form_label($this->lang->line('period'),'add_period');?></div>
						<?php
							$preselperiod = $selper;
							$options = '';
							$options[] = $this->lang->line('period');
							if($period){
								foreach($period as $row){
									$options[$row->period] = $row->financialPeriod;
								}
							}
							$js='class="input-medium" id="add_period" style="width:75%"';

							echo form_dropdown('add_period', $options, $preselperiod, $js);
						?>       
                        </div>
						<div class="span2 tac">
                        <?php 
							$data = array(
									'name'=>'mysubmit', 
									'class'=>'btn btn-small btn-primary');
									$js="onClick='changeList()'";
							echo form_button($data,$this->lang->line('submit'),$js);
							?>
                        </div>
                 	</div><!-- End Row 4-->
				</div><!-- content -->
            	<div class="content np table-sorting">
            	<table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable">
                	<thead>
                    <tr>
            			<th class="tac">col 1</th>
            			<th class="tac">col 2</th>
            			<th class="tac">col 3</th>
            			<th class="tac">col 4</th>
            			<th class="tac">col 5</th>
            			<th class="tac">col 6</th>
            			<th class="tac">col 7</th>
            			<th class="tac">col 8</th>
            			<th class="tac">col 9</th>
            			<th class="tac">col 10</th>
            			<th class="tac">col 11</th>
            			<th class="tac">col 12</th>
            			<th class="tac">col 13</th>
            			<th class="tac">col 14</th>
            		</tr>
        			</thead>
			<?php $bil = 0; 
				if($companyTables){
				foreach($companyTables as $datatbl):
				$bil++?>
            	<tr>
                	<td class="tac">C</td>
                    <td><?php echo $datatbl->companyName ?></td>
                	<td><?php echo $datatbl->companyNo?></td>
                	<td class="tac"><?php echo $datatbl->gstRegisteredNo; ?></td>
                	<td><?php echo date("d/m/Y", strtotime($startDate)); ?></td>
                	<td><?php echo date("d/m/Y", strtotime($endDate)); ?></td>
                	<td><?php echo date("d/m/Y"); ?></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                </tr>
            <?php endforeach; }?>
            <?php
					$purchaseCount = 0;
					$purchaseAmount = 0;
					$purchaseGST = 0;
					$salesCount = 0;
					$salesAmount = 0;
					$salesGST = 0;
					$glCount = 0;
					$glDebitAmount = 0;
					$glCreditAmount = 0;
					$glTotalBalance = 0;
				if($datatbls){
				foreach($datatbls as $datatbl):
				$bil++;
				switch ($datatbl->col1) {
					case 'P':
						$purchaseAmount = $purchaseAmount + $datatbl->col9;
						$purchaseCount = $purchaseCount + 1;
					    $purchaseGST = $purchaseGST + $datatbl->col10;
						break;
					case 'S':
						$salesAmount = $salesAmount + $datatbl->col9;
						$salesCount = $salesCount + 1;
					    $salesGST = $salesGST + $datatbl->col10;
						break;
				}
				?>
            	<tr>
                    <td><?php echo $datatbl->col1 ?></td>
                    <td><?php echo $datatbl->col2 ?></td>
                	<td><?php echo $datatbl->col3?></td>
                	<td><?php echo date("d/m/Y", strtotime($datatbl->col4))?></td>
                	<td><?php echo $datatbl->col5?></td>
                	<td><?php echo $datatbl->col6?></td>
                	<td><?php echo $datatbl->col7?></td>
                	<td><?php echo $datatbl->col8?></td>
                	<td class="tar"><?php echo number_format($datatbl->col9,2)?></td>
                    <?php if($datatbl->col1 == 'P'){?>
                	<td class="tar"><?php echo number_format($datatbl->col10,2)?></td>
                    <?php }else{ ?>
                	<td><?php echo $datatbl->col10 ?></td>
                    <?php } ?>
                	<td><?php echo $datatbl->col11?></td>
                	<td><?php echo $datatbl->col12?></td>
                	<td class="tar"><?php echo number_format($datatbl->col13,2)?></td>
                	<td><?php echo number_format($datatbl->col14)?></td>
                </tr>
            <?php endforeach; }?>
			<?php if($glDetails){ ?>
			<?php foreach($glDetails as $datatbl): ?>
            <?php $balance = $datatbl->yeardebit - $datatbl->yearcredit;?>
            <?php $glTotalBalance = $glTotalBalance + $balance;?>
            <?php $tempFlag = '';?>
            <?php if($journalDetails){?>
				<?php foreach($journalDetails as $row): ?>
					<?php if($row->acctCode == $datatbl->ID){ ?>
                    <?php if($tempFlag == ''){?>
                    <?php $glCount++;?>
            	<tr>
                    <td>L</td>
                    <td><?php echo date("d/m/Y", strtotime($startDate)) ?></td>
                	<td><?php echo $datatbl->acctCode?></td>
                	<td><?php echo $datatbl->acctName?></td>
                	<td>OPENING BALANCE</td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td></td>
                	<td class="tar"><?php echo number_format($datatbl->yeardebit - $datatbl->yearcredit,2) ?></td>
                	<td></td>
                	<td></td>
                    </tr>
					<?php 	$tempFlag = '1';?>
					<?php }?>
                    <?php $totalAmount = $row->amount_dr - $row->amount_cr;?>
                    <?php $balance = $balance + $totalAmount;?>
                    <?php $glTotalBalance = $glTotalBalance + $totalAmount;?>
                    <?php $glDebitAmount = $glDebitAmount + $row->amount_dr;?>
                    <?php $glCreditAmount = $glCreditAmount + $row->amount_cr;?>
                    <?php $glCount++;?>
            			<tr>
                    		<td>L</td>
                    		<td><?php echo date("d/m/Y", strtotime($row->effective_date)) ?></td>
                			<td><?php echo $datatbl->acctCode?></td>
                			<td><?php echo $datatbl->acctName?></td>
                			<td><?php echo $row->description?></td>
                			<td><?php echo $row->supplier?></td>
                			<td><?php echo $row->journalID?></td>
                			<td><?php echo $row->formNo?></td>
                			<td><?php echo $row->moduleName?></td>
                			<td class="tar"><?php echo number_format($row->amount_dr,2)?></td>
                			<td class="tar"><?php echo number_format($row->amount_cr,2)?></td>
                			<td class="tar"><?php echo number_format($balance,2)?></td>
                			<td></td>
                			<td></td>
                        </tr>
                    <?php } ?>
            	<?php endforeach;?>
                    <?php } ?>
            <?php endforeach;?>
			<?php } ?>
            	<tr>
                    <td>F</td>
                    <td><?php echo number_format($purchaseCount) ?></td>
                	<td><?php echo number_format($purchaseAmount,2)?></td>
                	<td><?php echo number_format($purchaseGST,2)?></td>
                	<td><?php echo number_format($salesCount)?></td>
                	<td><?php echo number_format($salesAmount,2)?></td>
                	<td><?php echo number_format($salesGST,2)?></td>
                	<td><?php echo number_format($glCount)?></td>
                	<td><?php echo number_format($glDebitAmount,2)?></td>
                	<td><?php echo number_format($glCreditAmount,2)?></td>
                	<td><?php echo number_format($glTotalBalance,2)?></td>
                	<td></td>
                	<td></td>
                	<td></td>
                </tr>
       				 <tbody>
                     </tbody>
                     </table>
				</div><!-- content -->
            <div class="footer">
            	<div class="side fr">
                	<div class="btn-group">
                        <?php 
							$data = array(
									'name'=>'mysubmit', 
									'class'=>'btn btn-primary');
							$js='onclick="createGAF()"';
							echo form_button($data,$this->lang->line('export'),$js);
							?>
                    </div>
                </div>
            </div>                                    
				</div><!-- block -->
			</div><!-- span12 -->
		</div><!-- row-fluid -->	
	</div><!-- wrap -->
</div><!-- content -->
