<script type="text/javascript">
$(document).ready(function() {
   $('select').select2();
}); 
 
function changeList(){
	var addyear = $('#add_year option:selected').val();
	var addper = $('#add_period option:selected').val();
	var fromperiod = $('#from_period option:selected').val();
	var acctNo = $('#acctNo option:selected').val();
	var urls = "<?php echo base_url();?>glreports/accountBalanceSummary/"+addyear+"/"+fromperiod+"/"+addper+"/"+acctNo;
		window.location = urls;
}

function do_print(){
	var addyear = $('#add_year option:selected').val();
	var addper = $('#add_period option:selected').val();
	var fromperiod = $('#from_period option:selected').val();
	var acctNo = $('#acctNo option:selected').val();
	window.open("<?php echo base_url() ?>glreports/accountBalanceSummary_print/"+addyear+"/"+fromperiod+"/"+addper+"/"+acctNo);
}

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].period+'">'
						+ content.message[i].financialPeriod + '</option>');
					}  //end for
					jQuery("#add_period").empty();
					jQuery("#from_period").empty();
					jQuery("#add_period").append(items.join('</br>'));
					jQuery("#from_period").append(items.join('</br>'));
					//$("#add_period").select2('val',null);
					$("#add_period").select2();
					//$("#from_period").select2('val',null);
					$("#from_period").select2();
				} else {
					jQuery("#add_period").empty();
					jQuery("#from_period").empty();
					$("#add_period").select2('val',null);
					$("#from_period").select2('val',null);
					$("#error").html('<p>'+content.message+'</p>');
				} //end if
			} //end success
				
			}); //end ajax
//	window.location.reload(true); 
}

</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 base_url()."home" ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
					<li><a href='<?php echo base_url()."GeneralLedger" ?>'><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
					<li class="active"><?php echo $this->lang->line('accBalSum') ?></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" id="grid_content_1">
			<div class="wrap">     
				<div class="row-fluid scRow">
					<div class="span12 scCol">
						<div class="block">
							<div class="head">
								<h2><?php echo $this->lang->line('accBalSum') ?></h2>	
								<ul class="buttons">
									<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
								</ul>
							</div><!-- head -->
							<div class="content np">    
								<div class="controls-row"><!-- Row 1-->
									<div class="span2"><?php echo form_label($this->lang->line('year'),'add_year');?></div>
									<div class="span4">
									<?php
										$preselyear = $selyear;
										$options = '';
										$options[] = $this->lang->line('selectyear');
                                                                             if($yrs)
										foreach($yrs as $row){
											$options[$row] = $row;
										}
										$js='onchange="selectYearPeriod(this)"class="input-medium" id="add_year" style="width:100%"';

										echo form_dropdown('add_year', $options, $preselyear, $js);
									?>       
									</div>
									
								</div><!-- End Row 1-->
								<div class="controls-row"><!-- Row 2-->
									<div class="span2"><?php echo form_label($this->lang->line('from_period'),'from_period');?></div>
									<div class="span4">
										<?php
										$preselperiodfrom = $selperfrom;
										$options = '';
										$options[] = $this->lang->line('period');
										if($period){
											foreach($period as $row){
												$options[$row->period] = $row->financialPeriod;
											}
										}
										$js='class="span4 input-medium" id="from_period" style="width:100%"';

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

										echo form_dropdown('add_period', $options, $preselperiod, $js);?>       
									</div>
								</div><!-- End Row 2-->
								<div class="controls-row"><!-- Row 3-->
									<div class="span2"><?php echo $this->lang->line('acctNo') ?></div>
									<div class="span4">
									<?php
										$preselacct = $acctNo;
										$options = '';
										$options[NULL] = $this->lang->line('pleaseselect');
                                                                              if($chartAccounts)
										foreach($chartAccounts as $row){
											$options[$row->ID] = substr($row->acctCode,0,4).'-'.substr($row->acctCode,4,3).'-'.substr($row->acctCode,7) . " [" . $row->acctName . "]";
											}
											$js='id="acctNo" style="width: 100%"';
										echo form_dropdown('acctNo', $options, $preselacct , $js);
									?>       
									</div> 
								</div><!-- End Row 3-->
							</div><!-- content -->
							<div class="footer">
								<div class="side fr">
								<?php 
									$data = array(
										'name'=>'generatereport', 
										'class'=>'btn-form btn-warning-form');
									$js="onClick='changeList()'";
										echo form_button($data,$this->lang->line('generatereport'),$js);
								?>
								</div>										
							</div>  
						</div>  <!-- Block grid-->
					</div>  <!-- span12 -->
				</div> <!-- row-fluid scRow-->
				<div class="row-fluid">
					<div class="span12">
						<div class="block">
							<div class="head">
								<h2><?php echo $this->lang->line('accBalSum') ?></h2>
								<ul class="buttons">
									<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
								</ul>
								<div class="side fr">
									<button class="btn-form btn-success-form" onClick="do_print()"><?php echo $this->lang->line('print') ?></button>                        
								</div>
							</div><!-- head -->
							<div class="content accordion">
							<?php
								$bil = 0; 
								if($datatbls){
									foreach($datatbls as $datatbl):
								$bil++?>
								<h3>
									<?php echo substr($datatbl->acctCode,0, 4).'-'.substr($datatbl->acctCode,4, 3).'-'.substr($datatbl->acctCode,7, 2) ?>
									<?php echo $datatbl->acctName ?>
									<!--div style="float:right"><?php echo number_format(($datatbl->yeardebit - $datatbl->yearcredit),2);?>
									</div-->
								</h3>
								<div>
									<?php 
										if($datatbl->acctTypeID != '3' && $datatbl->acctTypeID != '4'){
											if($datatbl->acctCode == '120000000'){
												$openbal = ($currRetain)*-1;
						   					}else{
						   						$openbal = ($datatbl->lastyeardebit+$datatbl->oDebit) - ($datatbl->lastyearcredit+$datatbl->oCredit);
						   					}
											}else{																							 
												$openbal = ($datatbl->lastyeardebit+$datatbl->oDebitPNL) - ($datatbl->lastyearcredit+$datatbl->oCreditPNL);
											}

											$monthdebit = 0;
											$monthcredit = 0;
										if($datatbls1){ ?>
											<?php
												$controlHead = 0; 
												$monthdebit = 0;
												$monthcredit = 0;
												foreach($datatbls1 as $row):
													if($row->acctCode == $datatbl->ID){
														if($controlHead == 0){ ?>
										<div class="row">             
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('date') ?></u></strong></div>
											<div class="span2 tac"><strong><u><?php echo $this->lang->line('journalno') ?></u></strong></div>
											<div class="span1"><strong><u><?php echo $this->lang->line('refNo') ?></u></strong></div>
											<div class="span1"><strong><u><?php echo $this->lang->line('description') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthdebit') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthcredit') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('balance') ?></u></strong></div>
										</div>
										<div class="row">    
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span1 tar">&nbsp;</div>
											<div class="span1"><?php echo $this->lang->line('openbalance') ?></div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar"><?php echo number_format($openbal,2) ?></div>
										</div>
											<?php 
												$controlHead = 1;
												} 
												$monthcredit = $monthcredit - $row->amount_cr;
												$monthdebit = $monthdebit + $row->amount_dr;
												$openbal = $openbal + $row->amount_dr;
												$openbal = $openbal - $row->amount_cr;
												?>
										<div class="row">
											<div class="span2 tar"><?php echo date("d-m-Y", strtotime($row->effective_date)) ?></div>
											<div class="span2">
											<a href="<?php echo base_url()?>glreports/journalDetails1/<?php echo $row->journalID ?>" title="<?php echo $this->lang->line('details');?>" class="btn btn-mini btn-link edit"><?php echo $row->journalID ?></a>
											</div>
											<div class="span1"><?php echo $row->refNo; ?></div>
											<div class="span1"><?php echo $row->description ?></div>
											<div class="span2 tar"><?php echo number_format($row->amount_dr,2) ?></div>
											<div class="span2 tar"><?php echo number_format($row->amount_cr,2) ?></div>
											<div class="span2 tar"><?php echo number_format($openbal,2) ?></div>
										</div>
										<?php		}
											endforeach; 
											if($controlHead == 0){ ?>
												<div class="row"> 
													<div class="span2 tar"><strong><u><?php echo $this->lang->line('date') ?></u></strong></div>
													<div class="span2 tac"><strong><u><?php echo $this->lang->line('journalno') ?></u></strong></div>
													<div class="span1"><strong><u><?php echo $this->lang->line('refNo') ?></u></strong></div>
													<div class="span1"><strong><u><?php echo $this->lang->line('description') ?></u></strong></div>
													<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthdebit') ?></u></strong></div>
													<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthcredit') ?></u></strong></div>
													<div class="span2 tar"><strong><u><?php echo $this->lang->line('balance') ?></u></strong></div>
												</div>
												<div class="row"> 
													<div class="span2 tar">&nbsp;</div>
													<div class="span2 tac">&nbsp;</div>
													<div class="span1">&nbsp;</div>
													<div class="span1"><?php echo $this->lang->line('openbalance') ?></div>
													<div class="span2 tar"></div>
													<div class="span2 tar"></div>
													<div class="span2 tar"><?php echo number_format($openbal,2) ?></div>
												</div>
												<div class="row"> 
													<div class="span2 tar">&nbsp;</div>
													<div class="span2 tac">&nbsp;</div>
													<div class="span1 tar">&nbsp;</div>
													<div class="span1"><?php echo $this->lang->line('total') ?></div>
													<div class="span2 tar"><?php echo number_format($monthdebit,2) ?></div>
													<div class="span2 tar"><?php echo number_format($monthcredit,2) ?></div>
													<div class="span2 tar">&nbsp;</div>
												</div>
								</div>
									<?php }else{?>
										<div class="row">
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span1 tar">&nbsp;</div>
											<div class="span1"><b><?php echo $this->lang->line('total') ?></b></div>
											<div class="span2 tar"><b><?php echo number_format($monthdebit,2) ?></b></div>
											<div class="span2 tar"><b><?php echo number_format(abs($monthcredit),2) ?></b></div>
											<div class="span2 tar"></div>
										</div>
							</div>
									<?php } 
										}else{ ?> 
										<div class="row"> 
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('date') ?></u></strong></div>
											<div class="span2 tac"><strong><u><?php echo $this->lang->line('journalno') ?></u></strong></div>
											<div class="span1"><strong><u><?php echo $this->lang->line('refNo') ?></u></strong></div>
											<div class="span1"><strong><u><?php echo $this->lang->line('description') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthdebit') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('monthcredit') ?></u></strong></div>
											<div class="span2 tar"><strong><u><?php echo $this->lang->line('balance') ?></u></strong></div>
										</div>
										<div class="row"> 
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span1">&nbsp;</div>
											<div class="span1"><?php echo $this->lang->line('openbalance') ?></div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar"><?php echo number_format($openbal,2) ?></div>
										</div>
										<div class="row"> 
											<div class="span2 tar">&nbsp;</div>
											<div class="span2 tar">&nbsp;</div>
											<div class="span1">&nbsp;</div>
											<div class="span1"><?php echo $this->lang->line('total') ?></div>
											<div class="span2 tar"><?php echo number_format($monthdebit,2) ?></div>
											<div class="span2 tar"><?php echo number_format($monthcredit,2) ?></div>
											<div class="span2 tar"></div>
										</div>
						</div>
                            <?php }
								endforeach; }?>
					</div><!-- content -->
				</div><!-- block -->
			</div><!-- span12 -->
		</div><!-- row-fluid -->	
	</div><!-- wrap -->
</div><!-- content -->

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