<script type="text/javascript">
	$(function(){
		
	});
	function fnHideZeroField(me, option){
		var tbody;
		if (option == 'balance') {
			tbody = $('#balance > tbody > tr');
		} else {
			tbody = $('#profit > tbody > tr');
		}
		var style = $(me).is(':checked') ? 'display:none' : '';
		$(tbody).each(function(){
			if (accounting.unformat($(this).find('td:eq(3) input').val()) == 0 && accounting.unformat($(this).find('td:eq(2) input').val()) == 0) {
				$(this).attr('style',style);
			}
		});
	}
	function fnGetTotal(me, option){
		var v = $(me).val();
		$(me).val(accounting.formatNumber(v,2));
		var tbody, totalDebit = 0, totalCredit = 0;
		if (option == 'balance') {
			tbody = $('#balance > tbody > tr');
		}
		else {
			tbody = $('#profit > tbody > tr');
		}
		$(tbody).each(function(){
		    totalDebit += accounting.unformat($(this).find('td:eq(2) input').val());
		    totalCredit += accounting.unformat($(this).find('td:eq(3) input').val());
		});
		$('#' + option + '_totalDebit').val(accounting.formatNumber(totalDebit, 2));
		$('#' + option + '_totalCredit').val(accounting.formatNumber(totalCredit, 2));
		$('#totalDebit').val(accounting.formatNumber(accounting.unformat($('#balance_totalDebit').val()) + accounting.unformat($('#profit_totalDebit').val()), 2));
		$('#totalCredit').val(accounting.formatNumber(accounting.unformat($('#balance_totalCredit').val()) + accounting.unformat($('#profit_totalCredit').val()), 2));
		fnPostRecord(me);
	}
	function fnPostRecord(me) {
		var tr = $(me).parent().parent();
		var debit = accounting.unformat(tr.find('.data-debit').val());
		var credit = accounting.unformat(tr.find('.data-credit').val());

		if ((debit == 0 && credit >= 0) ||
			 (credit == 0 && debit >= 0)) {
			tr.find('.status').html('');
			$.ajax({
				type: 'POST',
				dataType: 'json',
				cache: false,
				data: {
					debit : debit,
					credit : credit,
					year : '<?php echo $year ?>',
					balance: tr.find('.data-balance').val()
				},
				url: '<?php echo base_url() ?>glsetting/updateOpeningBalance/' + tr.find('.data-account').val(),
				success: function(data) {
					if (data.openingBalanceId != '' && data.hasOwnProperty('openingBalanceId')) {
						console.log(' have openingBalanceId ');
						tr.find('.data-balance').val(data.openingBalanceId);

						tr.find('.status').animate({}, 'slow', function() {
							$(this).html('<span class="i-checkmark-3 alert-success"><?php echo $this->lang->line('saved') ?></span>');
						});
						
						tr.find('.status').animate('', 'slow');
					}

					if (accounting.unformat($('#totalDebit').val()) == accounting.unformat($('#totalCredit').val())) {
						$('#openingBalanceWarning').hide();
					}
					else {
						$('#openingBalanceWarning').show();
						//$('html, body').animate({scrollTop: $('#openingBalanceWarning').offset().top}, 'slow');
					}
				}
			});
		}
		else {
			alert('<?php echo $this->lang->line('OnlyDebitOrCredit') ?>');
		}
	}


//-----------------------------------------------------farid start
	$(document).ready(function(){
		//Balanace Sheet datatable configuration
	
   
});	    
	function printbalancesheetlist(){
		//alert("dkaksfk");
		//var df = $('#selDateFrom').val();
		//var dt = $('#selDateTo').val();
		window.open("<?php echo base_url();?>glsetting/balancesheetList_print");
	}



    function printopeningbalancelist(){
        //alert("dkaksfk");
        //var df = $('#selDateFrom').val();
        //var dt = $('#selDateTo').val();
        window.open("<?php echo base_url();?>glsetting/openingbalanceList_print");
    }
  
	
	function printprofitnlostlist(){
		//var df = $('#selDateFrom').val();
		//var dt = $('#selDateTo').val();
		window.open("<?php echo base_url();?>glsetting/profitnlossList_print");
	}
        
        function setZero(me, option){
            var tr = $(me).parents("tr");
            
            
            if(option == "cr") {
              $(tr).find("td:eq(3) input").val("0.00");
                //$(me).val("");
            }else {
              $(tr).find("td:eq(2) input").val("0.00");
               //  $(me).val("");
            }
        }
        
        function getTotal(me,opt){
            
            var v=$(me).val();
            
            $(me).val(accounting.formatNumber(v,2));
            var tbody = $("#balanceSheet > tbody > tr");
            var totalDebit = 0;
            var totalCredit = 0;
            if (opt == "pl") {
                tbody = $("#profitNloss > tbody > tr");
            }
            $(tbody).each(function(){
                totalDebit += accounting.unformat($(this).find("td:eq(2) input").val());
                totalCredit += accounting.unformat($(this).find("td:eq(3) input").val());
            });
           $("#"+ opt +"_totaldb").val(accounting.formatNumber(totalDebit,2));
           $("#"+ opt +"_totalcr").val(accounting.formatNumber(totalCredit,2));
           
           $("#totalDebit").val(accounting.formatNumber(accounting.unformat($("#pl_totaldb").val()) + accounting.unformat($("#bs_totaldb").val()),2));
           $("#totalCredit").val(accounting.formatNumber(accounting.unformat($("#pl_totalcr").val()) + accounting.unformat($("#bs_totalcr").val()),2));
        }
        
        function checkzeroInclude(me, opt){
             var tbody = $("#balanceSheet > tbody > tr");
            if (opt == "pl") {
                tbody = $("#profitNloss > tbody > tr");
            }
            var style = ($(me).is(':checked')) ? "display:none" :"";
            $(tbody).each(function(){
               
                if (accounting.unformat($(this).find("td:eq(3) input").val())==0 && accounting.unformat($(this).find("td:eq(2) input").val())==0){
               $(this).attr("style",style);
           }
            });
        }
//------------------------------------------------------------------------------------------farid end  		
	
</script>
<div id="content">
	<div class="wrap">
		<div class="head">
			<div class="info">
				<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('title_ob') ?></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>
		<div class="content">
         <?php 
         	if (count($balance) > 0 && count($profit) > 0) {
			?>
			<!--Table for balance Sheet start-->
			<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('accntMstBalanceSheet') . " ($year)" ?></h2>
									<ul class="buttons">
										<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
									</ul>
									<div class="side fr">
									    <a class="btn-form btn-success-form" href="" name="" onclick="printopeningbalancelist()"><?php echo $this->lang->line('print') ?></a>                                 

										<a class="btn-form btn-warning-form" href="<?php echo base_url(); ?>glsetting/chartOfAcct" title="<?php echo $this->lang->line('addnew') ?>"> <span class="i-plus-2"></span></a>
									</div>
								</div>
								<div class="content np table-sorting">
									<table id="balance" class="editable">
										<thead>
											<tr>
												<th><?php echo $this->lang->line('accountno') ?></th>
												<th><?php echo $this->lang->line('accountName') ?></th>
												<th><?php echo $this->lang->line('debit') ?></th>
												<th><?php echo $this->lang->line('credit') ?></th>
												<?php if ($disabledBalance == '') { ?>
												<th><?php echo $this->lang->line('action') ?></th>
												<?php } ?>
											</tr>
										</thead>
										<tbody>
										<?php
											$debit = 0;
											$credit = 0;
											foreach($balance as $row) {
												$debit += $row->openingBalanceDr;
												$credit += $row->openingBalanceCr;
										?>
											<tr>
												<td class="span2">
													<a href="<?php echo base_url().'glsetting/chartOfAcct'.'/'.$row->ID; ?>"><?php echo $row->acctCode; ?></a>
													<input class="data-account" type="hidden" value="<?php echo $row->ID ?>" />
													<input class="data-balance" type="hidden" value="<?php echo $row->openingBalanceID ?>" />
												</td>
												<td class="span5">
													<span class="acctName"><?php echo $row->acctName; ?></span>
												</td>
												<td class="span2">
													<input type="text" value="<?php echo number_format($row->openingBalanceDr,2) ?>" class="span12 data-debit" <?php echo $disabledBalance ?> onChange="fnGetTotal(this, 'balance')" />
												</td>
												<td class="span2">
													<input type="text" value="<?php echo number_format($row->openingBalanceCr,2) ?>" class="span12 data-credit" <?php echo $disabledBalance ?> onChange="fnGetTotal(this, 'balance')" />
												</td>
												<?php if ($disabledBalance == '') { ?>
												<td class="span1 tac status">
													
												</td>
												<?php } ?>
											</tr>
										<?php
											}
										?>
										</tbody>
									</table>
								</div>
								<div class="footer">
									<div class="side fr">
										<div class="bs_total">
											<div class="controls-row"></div>
											<!-- row control -->
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('HideZeroField') ?></div>
												<div class="span7">
													<input type="checkbox" onchange="fnHideZeroField(this, 'balance')" <?php echo $disabledProfit ?> />
												</div>
											</div>
											<!-- row control -->
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('SubTotalDebit') ?></div>
												<div class="span7">
													<input type="text" id="balance_totalDebit" name="balance_totalDebit" value="<?php echo number_format($debit, 2) ?>" class="span12 amount sub-total-debit" disabled />
												</div>
											</div>
											<!-- row control -->
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('SubTotalCredit') ?></div>
												<div class="span7">
													<input type="text" id="balance_totalCredit" name="balance_totalCredit" value="<?php echo number_format($credit, 2) ?>" class="span12 amount sub-total-credit" disabled />
												</div>
											</div>
											<!-- row control -->
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!--Table for balance Sheet end-->
			<!--Table for balance Sheet start-->
			<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('accntMstProfitNLossAccounts') . " ($year)" ?></h2>
									<ul class="buttons">
										<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
									</ul>
									<div class="side fr">
										<a class="btn-form btn-success-form" href="" name="" onclick="printprofitnlostlist()"><?php echo $this->lang->line('print') ?></a>                                 

										<a class="btn-form btn-warning-form" href="<?php echo base_url(); ?>glsetting/chartOfAcct" title="<?php echo $this->lang->line('addnew') ?>"> <span class="i-plus-2"></span></a>
									</div>
								</div>
								<div class="content np table-sorting">
									<table id="profit" class="editable">
										<thead>
											<tr>
												<th><?php echo $this->lang->line('accountno') ?></th>
												<th><?php echo $this->lang->line('accountName') ?></th>
												<th><?php echo $this->lang->line('debit') ?></th>
												<th><?php echo $this->lang->line('credit') ?></th>
												<?php if ($disabledProfit == '') { ?>
												<th><?php echo $this->lang->line('action') ?></th>
												<?php } ?>
											</tr>
										</thead>
										<tbody>
											<?php
												$debit2 = 0;
												$credit2 = 0;
												foreach($profit as $row) {
													$debit2 += $row->openingBalanceDr;
													$credit2 += $row->openingBalanceCr;
											?>
											<tr>
												<td class="span2">
													<a href="<?php echo base_url().'glsetting/chartOfAcct'.'/'.$row->ID; ?>"><?php echo $row->acctCode; ?></a>
													<input class="data-account" type="hidden" value="<?php echo $row->ID ?>" />
													<input class="data-balance" type="hidden" value="<?php echo $row->openingBalanceID ?>" />
												</td>
												<td class="span5">
													<span class="acctName"><?php echo $row->acctName; ?></span>
												</td>
												<td class="span2">
													<input type="text" value="<?php echo number_format($row->openingBalanceDr,2) ?>" class="span12 data-debit" <?php echo $disabledProfit ?> onChange="fnGetTotal(this, 'profit')" />
												</td>
												<td class="span2">
													<input type="text" value="<?php echo number_format($row->openingBalanceCr,2) ?>" class="span12 data-credit" <?php echo $disabledProfit ?> onChange="fnGetTotal(this, 'profit')" />
												</td>
												<?php if ($disabledProfit == '') { ?>
												<td class="span1 tac status">
												
												</td>
												<?php } ?>
											</tr>
											<?php
												}
											?>
										</tbody>
									</table>
								</div>
								<div class="footer">
									<div class="side fr">
										<div class="pl_total">
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('HideZeroField') ?></div>
												<div class="span7">
													<input type="checkbox" onChange="fnHideZeroField(this, 'profit')" <?php echo $disabledProfit ?> />
												</div>
											</div>
											<!-- row control -->
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('SubTotalDebit') ?></div>
												<div class="span7">
													<input type="text" id="profit_totalDebit" name="profit_totalDebit" value="<?php echo number_format($debit2, 2) ?>" class="span12 amount sub-total-debit" disabled />
												</div>
											</div>
											<!-- row control -->
											<div class="controls-row">
												<div class="span5"><?php echo $this->lang->line('SubTotalCredit') ?></div>
												<div class="span7">
													<input type="text" id="profit_totalCredit" name="profit_totalCredit" value="<?php echo number_format($credit2, 2) ?>" class="span12 amount sub-total-credit" disabled />
												</div>
											</div>
											<!-- row control -->
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!--Table for balance Sheet end-->
			<?php } ?>
			<!--Main content end here-->
			<div class="content">
				<div class="wrap">
					<div class="row-fluid scRow">
						<div class="span12 scCol">
							<div class="block">
								<div class="content np">
									<div class="span5 fr">
										<div class="controls-row">
											<div class="span5"><?php echo $this->lang->line('TotalDebit') ?></div>
											<div class="span7">
												<input type="text" id="totalDebit" name="totalDebit" value="<?php echo number_format($debit + $debit2, 2) ; ?>" class="span12 amount" disabled />
											</div>
										</div>
										<!-- row control -->
										<div class="controls-row">
											<div class="span5"><?php echo $this->lang->line('TotalCredit') ?></div>
											<div class="span7">
												<input type="text" id="totalCredit" name="totalCredit" value="<?php echo number_format($credit + $credit2, 2) ; ?>" class="span12 amount" disabled />
											</div>
										</div>
										<!-- row control -->
									</div>
									<div class="footer">
										<div class="side fr">
											<div class="btn-group">
												
											</div>
										</div>
									</div>
								</div>
								<!-- grid block 1-->
							</div>
						</div>
						<!-- row-fluid scRow-->
					</div>
					<!-- grid 1 -->
				</div>
			</div>
		</div>
	</div>
</div>