<script>
	function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

    // document.body.innerHTML = printContents.fontsize(12);
     window.print();

	 if( window.print()) {
         window.location.href = "../user/profitloss";
		alert(" printing...........");
    }else {

	
		      return      window.location.href = "../user/profitloss";

		    // document.body.innerHTML = originalContents;

	}
}

 
	</script>
 
 <div class="row">
  
   	

   	<div class="col-md-12">

  
		<div class="panel panel-default" data-collapsed="0">
        	<div class="panel-heading">
            	<div class="panel-title" id="non-printable" >
            		<i class="entypo-plus-circled"></i>
 <a href="<?php echo base_url();?>user/startsales" class="btn btn-info">  Go back</a>	
<a data-toggle='modal' data-target='#addreportModal' class="btn btn-info">View Customed Report  </a>


<div class="pull-right"  >

<form name="print" method="post" action="">
 <input type="submit" id="print"  accesskey="p" class="btn btn-primary"  name="print"onclick="printDiv('sortable_table')" value="Print P & L!" />
</form></div>
 

				
            	</div>
            </div>
			<div class="panel-body">

			
	<div class="widget-content nopadding table_holder table-responsive" ID="sortable_table">				
				<?PHP


								?>	
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	
									
  <table cellpadding="0" cellspacing="0" border="1" class="table  table-striped" id="example" style=" width:100%;"  >

             <thead>
			     <tr>
				     <th colspan="3"><CENTER> TRADING	PROFIT AND LOSS<br>
					 <?php echo "Starting ".$startdate. " To ".$enddate;
					 
					 
					 
					 ?>
					 
					 
					 </CENTER></th>
 </tr> <tr>
    <th colspan="3"><CENTER>	Income</CENTER></th>
  
                 </tr>
         <tr>
    <th>Code</th>
     <th>Account Name</th>
	 
<th>Total </th>	

      	

	   </tr>
             </thead><tbody>
<?php
   function formatMoney($number, $fractional=false) {
						if ($fractional) {
							$number = sprintf('%.2f', $number);
						}
						while (true) {
							$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
							if ($replaced != $number) {
								$number = $replaced;
							} else {
								break;
							}
						}
						return $number;
					}	
					$totalsubincome=0;
						 $MYPROFIT= 0;
						 $myincome=0;
						 $myexpense=0;
						 $ttcostofgoods=0;
						 $ttgeneralexpenses=0;
//$totalsubdirectexpense=0;
$totalsubdinirectexpense=0;
$totalsubdinirectincome=0;
$totalsubcostofgoods=0;

						 
	$ttotherexpense=0;

		$ttotherincome=0;
	
	$ttdirectincome=0;
$accounts_array = array();

	   			        		$mainaccounts= $this->usermodel->populateall('glaccounts','companyid', $companyid);
	 if(is_array($mainaccounts)) 

 {
	foreach ($mainaccounts as $rowgl){
		
					$accounts_array[]=array(	
			'accounttype'=>$rowgl->accounttype,
						'id'=>$rowgl->id,

				'accountname'=>$rowgl->accountname,
							'bankname'=>$rowgl->bankname,
							'number'=>$rowgl->number,

		  	'description'=>$rowgl->description,
				 'companyid'=>$rowgl->companyid,

				 
			
								);	
		
	}
  	
 }	
 
 
 //var_dump($accounts_array);
// exit;
 	
				 function getAccountName($id, $accounts_array) {
    $key = array_search($id, array_column($accounts_array, 'id'));
    if ($key !== FALSE) {
        return $accounts_array[$key]['accountname'];
    }
}

 if(!is_array($income)) 

 {

   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='1'>";
    
    echo "</td>";
   echo "</tr>";
    }else {
?>
    <tr>
    <th colspan="3">
	    <b><center>Direct Income</center></b></th>
  
                 </tr>

             
             <?php 
			 


    $totalcredit=0;

	$i = 1;
$ttdincome=0;

	$watch = array();

  	foreach ($income as $c){
		
		
		$mainkey = array_search($c->mybankname, array_column($accounts_array, 'id'));
				$key2 = array_search($c->account, array_column($accounts_array, 'number'));
		  $directincomegroup=$c->mygroupcode;


		
		
		 		

?>
    <tr> 

<?php

 if($directincomegroup==27){

 
 
 
 $mainaccount="";
 
 
 
 
 
 
 
	  $accounttype=$c->accounttype;

				 
 // $totalcredit=$totalcredit+($c->credit-$c->debit);
  

			   if (!in_array($c->mybankname,$watch)) {
	//echo '<br /> Main=' . $accounts_array[$key2]['accountname'] ."--".$c->account. ' ' . getAccountName($c->mybankname, $accounts_array);

if( getAccountName($c->mybankname, $accounts_array)){
	$correctname=   getAccountName($c->mybankname, $accounts_array);
	$confirm=$c->mybankname;
	$mainaccount=$accounts_array[$mainkey]['number'];

			$subincome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
$ttdincome=0;

}else{
	$ttdincome=$c->credit-$c->debit;

	$confirm=$accounts_array[$key2]['id'];
		$subincome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
		$mainaccount=$c->account;

	$correctname=$accounts_array[$key2]['accountname'];
}
		   // $subincome = array_search($c->subaccountid, array_column($accounts_array, 'bankname'));
			
						
	
	 ?>


         
 <td>
 
 
  	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $mainaccount ; ?>  </a>

 </td>
 

  <td>
    	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php

			 echo $correctname;
		 
			 ?>  </a>
</td>
   
	 <td align="right">

	 <?php echo   formatMoney($ttdincome, true);

	     $ttdirectincome= $ttdirectincome+$ttdincome;

	 ?>
	 
	 	   <table  cellpadding="0" cellspacing="0" border="0" class="table" id="example" style="border-color:#CCCCCC; width:100%;"  >

	 
	 <?php
	 			
				//$subincome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$c->subaccountid,$startdate,$enddate);
 	 $totalsubincome=0;


 if(is_array($subincome)) 

 {

 foreach ($subincome as $subincome){
	?>

	<tr>
	<td><a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $subincome->account ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $subincome->myaccountname;  ; ?>  </a> </td>
	<td align="right"><?php echo formatMoney($subincome->credit-$subincome->debit, true);
    $totalsubincome= $totalsubincome+($subincome->credit-$subincome->debit);;
			array_push($watch, $subincome->mybankname);

	?> </td>
	</tr>
	
	 
	 
	<?php
}
   //$totalsubincome=$totalsubincome+ $ttdincome;


echo "<tr><td><b> Total ".$correctname."</b></td><td align='right'><b>".formatMoney($totalsubincome+$ttdincome, true)."</b></td></tr>"; 
 }	 

	 
	      $ttdirectincome= $ttdirectincome+$totalsubincome;

	 
	 
	 ?>
	 
	 </table></td>
	 
 
 
            

	<?php 
	}}
 }
 
	 //+$totalsubincome;

?>
 </tr>
<tr>
<td></td>
    <td><b>Total Direct Income</b></td>
	   <td align="right"><b> <?php if(isset($ttdirectincome)) {echo formatMoney($ttdirectincome, true); }?></b></td>
  
                 </tr>




<tr>
    <th colspan="3">
	    <b><center>Other Income</center></b></th>
  
                 </tr>

             <tr>


<?php

	 $watch = array();

  	foreach ($income as $c){ 
		  $othertincomegroup=$c->mygroupcode;

		  
 if($othertincomegroup==28){

		$mainkey = array_search($c->mybankname, array_column($accounts_array, 'id'));
				$key2 = array_search($c->account, array_column($accounts_array, 'number'));
	 
	// $ttotherincome=$ttotherincome+($c->credit-$c->debit);
	 

			   if (!in_array($c->mybankname,$watch)) {
	//echo '<br /> Main=' . $accounts_array[$key2]['accountname'] ."--".$c->account. ' ' . getAccountName($c->mybankname, $accounts_array);

if( getAccountName($c->mybankname, $accounts_array)){
	$correctname=   getAccountName($c->mybankname, $accounts_array);
	$confirm=$c->mybankname;
	$mainaccount=$accounts_array[$mainkey]['number'];

			$subindirectcome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
$ttincome=0;



}else{
	$ttincome=$c->credit-$c->debit;

	$confirm=$c->subaccountid;
		$subindirectcome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
		$mainaccount=$c->account;

	$correctname=$accounts_array[$key2]['accountname'];
}
		   // $subincome = array_search($c->subaccountid, array_column($accounts_array, 'bankname'));
			
	
	 

	 ?>


     <tr>  
	 
 <td>
  	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $mainaccount ; ?>  </a>
 
 </td>
  <td> <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php

			 echo $correctname;
		 
			 ?>  </a></td>
   
	 <td align="right"><?php echo   formatMoney($ttincome, true);
		 $ttotherincome= $ttotherincome+$ttincome;


	 ?>
	 
	 
	  <table  cellpadding="0" cellspacing="0" border="0" class="table" id="example" style="border-color:#CCCCCC; width:100%;"  >

	 
	 <?php
	 			
 	 $totalsubindirectcome=0;
						//$subindirectcome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$c->subaccountid,$startdate,$enddate);

 if(is_array($subindirectcome)) 

 {

 foreach ($subindirectcome as $subindirectcome){
	?>

	<tr>
	<td><a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $subindirectcome->account ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $subindirectcome->myaccountname;  ; ?>  </a> </td>
	<td align="right"><?php echo formatMoney($subindirectcome->credit-$subindirectcome->debit, true);
    $totalsubindirectcome= $totalsubindirectcome+($subindirectcome->credit-$subindirectcome->debit);;
			array_push($watch, $subindirectcome->mybankname);

	?> </td>
	</tr>
	
	 
	 
	<?php
}
    $totalsubindirectcome= $totalsubindirectcome+$ttincome;


echo "<tr><td><b> Total ".$correctname."</b></td><td align='right'><b>".formatMoney($totalsubindirectcome+$ttincome, true)."</b></td></tr>"; 
 }	 
	     $ttotherincome= $ttotherincome+$totalsubindirectcome;

	 
	 
	 
	 ?>
	 
	 </table>
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 </td>
	 
 
 
             

	<?php 
	
	}} }  } ?>
	</tr>
	<tr>
	<td></td>
    <td><b>Total Other Income</b></td>
	    <td align="right"><b> <?php if(isset($ttotherincome)) {echo formatMoney($ttotherincome, true); }?></b></td>
  
                 </tr>
	
		<?php 

 
	

?>
 <tr>
    <td colspan="2"><b>Total Income</b></td>
	   <td align="right"><b> <?php 
	   	 $totalcredit=$ttdirectincome+$ttotherincome;

	   

	   
	   if(isset($totalcredit)) {echo formatMoney($totalcredit, true); }?></b></td>
  
                 </tr>
				
			
				 

				 <?php
		
          if(!is_array($expense)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='1'>";
    
    echo "</td>";
   echo "</tr>";
    }else {
		 $totaldebt=0;
?>
   
				 
				 
				 
				 
				 		<tr>
    <th colspan="3">
	    <b><center>Cost of Goods sold</center></b></th>
  
                 </tr>

           
	 
<?php


	  $watch = array();

  	foreach ($expense as $cgexpense): 
		 $costgroupcode=$cgexpense->mygroupcode;
		         

 if($costgroupcode==8){
	 

		$mainkey = array_search($cgexpense->mybankname, array_column($accounts_array, 'id'));
				$key2 = array_search($cgexpense->account, array_column($accounts_array, 'number'));
	 
	 
	 
	 
	 $id=$cgexpense->id;
    //$totaldebt= $totaldebt+$cgexpense->debit;
 
 //$ttcostofgoods= $ttcostofgoods+$cgexpense->debit;
	 //$totaldebt=$ttcostofgoods;
	 
	 
	 
	  if (!in_array($cgexpense->mybankname,$watch)) {
	//echo '<br /> Main=' . $accounts_array[$key2]['accountname'] ."--".$c->account. ' ' . getAccountName($c->mybankname, $accounts_array);

if( getAccountName($cgexpense->mybankname, $accounts_array)){
	$correctname=   getAccountName($cgexpense->mybankname, $accounts_array);
	$confirm=$cgexpense->mybankname;
	$mainaccount=$accounts_array[$mainkey]['number'];

			$subcog = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
$totalcostofgoods=0;



}else{
	$totalcostofgoods=$cgexpense->debit-$cgexpense->credit;

	$confirm=$cgexpense->subaccountid;
		$subcog = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
		$mainaccount=$cgexpense->account;

	$correctname=$accounts_array[$key2]['accountname'];
}
		   // $subincome = array_search($c->subaccountid, array_column($accounts_array, 'bankname'));
			
	
	 

	 ?>
	 
	 
	 

<tr>


        <td>
  	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $mainaccount ; ?>  </a>
 
 </td>       
 
 <td> <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php

			 echo $correctname;
		 
			 ?>  </a></td>
   
	 <td align="right"><?php echo   formatMoney($totalcostofgoods, true); 
	 $ttcostofgoods=$ttcostofgoods+$totalcostofgoods;


	 ?>
	 
	 
	 
	 
	 
	 
	 <table  cellpadding="0" cellspacing="0" border="0" class="table" id="example" style="border-color:#CCCCCC; width:100%;"  >

	 
	 <?php
				 
	 	 	 	 	 $totalsubcog=0;

	 					// $subcog = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$cgexpense->subaccountid,$startdate,$enddate);
 if(is_array($subcog)) 

 {


 foreach ($subcog as $subcog){
	?>

	<tr>
	<td><a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $subcog->account ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $subcog->myaccountname;  ; ?>  </a> </td>
	<td align="right"><?php echo formatMoney($subcog->debit, true);
    $totalsubcog= $totalsubcog + $subcog->debit;
			array_push($watch, $subcog->mybankname);

	?> </td>
	</tr>
	
	 
	 
	<?php
}
    //$totalsubcog= $totalsubcog+$totalcostofgoods;

	
echo "<tr><td><b> Total ".$correctname."</b></td><td align='right'><b>".formatMoney($totalsubcog+$totalcostofgoods, true)."</b></td></tr>"; 
 }	  	 $ttcostofgoods=$ttcostofgoods+$totalsubcog;

 

	
	 ?>
	 
	 </table>
	 
	 
	 
	 
	 
	 
	 
	 
	 </td>
	 
 
 
             </tr>
<?php 
	  }
 }
	 ?>
	 
	 
	 
	 
	<?php endforeach; ?>
			<tr>
<td></td>
    <th><b>Total Cost of Goods sold</b></th>
	   <td align="right"> <b><?php if(isset($ttcostofgoods)) {echo formatMoney($ttcostofgoods, true); }?></b></td>
  
                 </tr>
	   <tr>
	   <td>
	   <b>Gross profit</b>
	   </td>
    <td colspan="2" align="right">
<b>	
<?php 
	echo formatMoney($totalcredit-$ttcostofgoods, true); 



?>	
	
	</b>
	</td>
  
                 </tr>
				 
				  <tr>
    <th colspan="3"><CENTER>	EXPENSES</CENTER></th>
  
                 </tr>
				 <tr>
    <th colspan="3">
	    <b><center>General & Administrative expenses</center></b></th>
  
                 </tr> 
				 
				 

             <?php 
 
   //$totalsubdirectexpense=0;

	   $expwatch = array();
	   $mainexpensewatch = array();

  	foreach ($expense as $generalexpense): 

		 $generalexpenses=$generalexpense->mygroupcode;

	

	 
			$totalsubdirectexpense=0;
	 
		
		
 if($generalexpenses==29){
	 


		$mainkey = array_search($generalexpense->mybankname, array_column($accounts_array, 'id'));
				$key2 = array_search($generalexpense->account, array_column($accounts_array, 'number'));
	 
	 
	 
	 		 		 $accounttype=$generalexpense->accounttype;
$totaldirectexpense=0;
$subdirectincome=0;
	  if (!in_array($generalexpense->mybankname,$expwatch)) {
	//echo '<br /> Main=' . $accounts_array[$key2]['accountname'] ."--".$c->account. ' ' . getAccountName($c->mybankname, $accounts_array);

if( getAccountName($generalexpense->mybankname, $accounts_array)){
	$correctname=   getAccountName($generalexpense->mybankname, $accounts_array);
	$confirm=$generalexpense->mybankname;
	$mainaccount=$accounts_array[$mainkey]['number'];

			$subdirectincome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);




}



			array_push($mainexpensewatch, $mainaccount);







	// $subincome = array_search($c->subaccountid, array_column($accounts_array, 'bankname'));
			

	 

	 ?>
<tr>


         
        <td>
  	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $mainaccount ; ?>  </a>
 
 </td>        
 
 <td> <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php

			 echo $correctname;
		 
			 ?>  </a></td>
   
	 <td  align="right"><?php echo   formatMoney($totaldirectexpense, true);
	 		 $ttgeneralexpenses=$ttgeneralexpenses+$totaldirectexpense;

 ?>
	 
	 
	 <table  cellpadding="0" cellspacing="0" border="0" class="table" id="example" style="border-color:#CCCCCC; width:100%;"  >

	 
	 <?php
	 					// $subdirectincome = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$generalexpense->subaccountid,$startdate,$enddate);
 if(is_array($subdirectincome)) 

 {
 foreach ($subdirectincome as $subdirectincome){
	?>

	<tr>
	<td><a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $subdirectincome->account ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $subdirectincome->myaccountname;  ; ?>  </a> </td>
	<td align="right"><?php echo formatMoney($subdirectincome->debit, true);
    $totalsubdirectexpense= $totalsubdirectexpense+($subdirectincome->debit);;
			array_push($expwatch, $subdirectincome->mybankname);

	?> </td>
	</tr>
	
	 
	 
	<?php
}
    //$totalsubdirectexpense= $totalsubdirectexpense+$totaldirectexpense;


echo "<tr><td><b> Total ".$generalexpense->myaccountname."</b></td><td align='right'><b>".formatMoney($totalsubdirectexpense+$totaldirectexpense, true)."</b></td></tr>"; 
 }	 
	 
	 $ttgeneralexpenses=$ttgeneralexpenses+$totalsubdirectexpense;

	 
	 
	 //+$totalsubdirectexpense;
	 	// $ttgeneralexpenses=$ttgeneralexpenses+$generalexpense->debit;

	 ?>
	 
	 </table>
	 
	 
	 
	 
	 </td>
	 
 
 
             </tr>
<?php 
	  }
 }



	 ?>
	
		<?php endforeach; ?>  
	 
	 <tr>
<td></td>
    <td><b>Total Direct expenses</b></td>
	   <td  align="right"><b> <?php if(isset($ttgeneralexpenses)) {echo formatMoney($ttgeneralexpenses, true); }?></b></td>
  
                 </tr>
	 
	 
	 
	 
	 
	 
		<tr>
    <th colspan="3">
	    <b><center>Other expenses</center></b></th>
  
                 </tr>

           
	 
<?php
  $otherexpwatch = array();


		
	 
  	foreach ($expense as $otherexpense): 
		 $ourgroupename=$otherexpense->mygroupcode;

 if($ourgroupename==30){

	// $ttotherexpense=$ttotherexpense+$otherexpense->debit;
	 //$totaldebt=$totaldebt+$ttotherexpense;
	$mainkey = array_search($otherexpense->mybankname, array_column($accounts_array, 'id'));
				$key2 = array_search($otherexpense->account, array_column($accounts_array, 'number'));
	 

	  if (!in_array($otherexpense->mybankname,$otherexpwatch)) {
	//echo '<br /> Main=' . $accounts_array[$key2]['accountname'] ."--".$c->account. ' ' . getAccountName($c->mybankname, $accounts_array);

if( getAccountName($otherexpense->mybankname, $accounts_array)){
	$correctname=   getAccountName($otherexpense->mybankname, $accounts_array);
	$confirm=$otherexpense->mybankname;
	$mainaccount=$accounts_array[$mainkey]['number'];

			$subdinrectexpense = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
$totalotherexpense=0;



}else{
	$totalotherexpense=$otherexpense->debit-$otherexpense->credit;

	$confirm=$otherexpense->subaccountid;
		$subdinrectexpense = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$confirm,$startdate,$enddate);
		$mainaccount=$otherexpense->account;

	$correctname=$accounts_array[$key2]['accountname'];
}

		   // $subincome = array_search($c->subaccountid, array_column($accounts_array, 'bankname'));
			
	
	 
	 
	 ?>
<tr>
     
        <td>
  	 	 <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $mainaccount ; ?>  </a>
 
 </td> 
              
 
  <td> <a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $mainaccount ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php

			 echo $correctname;
		 
			 ?>  </a></td>
			 
   
	 <td align="right"> <?php echo   formatMoney($totalotherexpense, true); 
	 
	 $ttotherexpense=$ttotherexpense+$totalotherexpense;

	 
	 
	 ?>
	 
	 
	 
	 
	 <table  cellpadding="0" cellspacing="0" border="0" class="table" id="example" style="border-color:#CCCCCC; width:100%;"  >

	 
	 <?php
				 
	 	 	 	 	 $totalsubdinrectexpense=0;
$mytotalsubdinrectexpense=0;
	 					// $subdinrectexpense = $this->usermodel->displaysuaccount('debitcredit','companyid',$companyid,'bankname',$otherexpense->subaccountid,$startdate,$enddate);
 if(is_array($subdinrectexpense)) 

 {


 foreach ($subdinrectexpense as $subdinrectexpense){
	?>

	<tr>
	<td><a  target="_blank" class="btn-default" href="<?php echo base_url();?>user/viewaccount?bankname=<?php   {echo  $subdinrectexpense->account ;}  ?>&startdate=<?php   {echo  $startdate ;}  ?>&enddate=<?php   {echo  $enddate ;}  ?>"><?php echo $subdinrectexpense->myaccountname;  ; ?>  </a> </td>
	<td align="right"><?php echo formatMoney($subdinrectexpense->debit, true);
    $totalsubdinrectexpense= $totalsubdinrectexpense + $subdinrectexpense->debit;
			array_push($otherexpwatch, $subdinrectexpense->mybankname);

	?> </td>
	</tr>
	
	 
	 
	<?php
}
    //$mytotalsubdinrectexpense= $totalsubdinrectexpense+$totalotherexpense;

	
echo "<tr><td><b> Total ".$otherexpense->myaccountname."</b></td><td align='right'><b>".formatMoney($totalsubdinrectexpense+$totalotherexpense, true)."</b></td></tr>"; 
 }	  	 $ttotherexpense=$ttotherexpense+$mytotalsubdinrectexpense;

 

	
	 ?>
	 
	 </table>
	 
	 
	 
	 
	 
	 
	 
	 </td>
	 
 
 
             </tr>
<?php 
	  }
 }
	 ?>
	 
	 
	 
	<?php endforeach; 
	}
	?>
	 <tr>
<td></td>
    <td><b>Total Other expenses</b></td>
	   <td align="right"><b> <?php if(isset($ttotherexpense)) {echo formatMoney($ttotherexpense, true); }?></b></td>
  
                 </tr>


 <tr>
    <td colspan="2"><b>Total Expenses</b></th>

	   <td align="right"> <b><?php
	    $myexpense= $ttcostofgoods+$ttotherexpense+$ttgeneralexpenses;
 

	   if(isset($myexpense)) {echo formatMoney($myexpense, true); }?>
	   
	   
	   </b></td>
  
                 </tr>
				 <tr>
    <td colspan="2"><b>Total Income</b></td>

	   <td align="right"> <b><?php

	 	$myincome=$myincome+$ttdirectincome+ $ttotherincome;


	   if(isset($myincome)) {echo formatMoney($myincome, true); }?>
	   
	   
	   </b></td>
  
                 </tr>
<tr>
    <td colspan="2"><b>NET INCOME</b></td>
	   <td align="right"> <b><?php 
	   
	   
	   
	   
	$MYPROFIT=  $myincome-$myexpense;
	   if(isset($MYPROFIT)) { echo formatMoney(  $MYPROFIT, true); }?></b></td>
  
                 </tr>
       <tbody> </table>
	   
   
			
	</div>
						
									
							</div>
						</div>
				

			
			</div>
			
			
			
			<div id="addreportModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">View customed Report</h4>
      </div>
      <div class="modal-body">
      
	  	

				<?PHP
//$session_data = $this->session->userdata('logged_in');

  $finalstartdate=date("Y-m-01 00:00:00");
  $finalendate=date("Y-m-30 23:59:59");


				echo $this->session->flashdata('msg'); 
		
                 echo form_open('user/profitloss' , array('class' => 'form-horizontal form-groups-bordered validate', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>

  <div class="form-group">
    <label for="inputbranchname" class="col-sm-2 control-label">Counter</label>
    <div class="col-sm-10">
	<select class="form-control" name="branchname"  id="branchname" required>
	
	
	<option value=""  >---Select Branch/Counter Name--</option>

					<option value="all"  selected="selected">All branches/Counters</option>
			
	<?php 
					 
					 		$maincategory = $this->usermodel->populateall('branch','companyid',$companyid);


							


if(is_array($maincategory)) {

   foreach($maincategory as $maincategoryrow){

   //echo $courserow->coursename;
    ?>
					<option  value="<?=  $maincategoryrow->id;   ?>"><?=  $maincategoryrow->name; ?></option>



	<?php

}} else {
    echo 'No main counter available.';
}
	
?>
</select>
		

		
	</div>
  </div>
	
	
	
	
	
	
	
	
	
  <div class="form-group">
    <label for="inputstartdate" class="col-sm-2 control-label">From Date </label>
    <div class="col-sm-10">
	
		  		<input type="date" name="startdate" value="<?php echo set_value('fromdate',$finalstartdate); ?>" class="form-control" required placeholder="Enter start date">
		 
				
		<span class="text-danger"> <?php echo form_error('startdate'); ?>
		
		</span>
	</div>
  </div>
  
  	<div class="form-group">
    <label for="inputenddate" class="col-sm-2 control-label">	To Date </label>
    <div class="col-sm-10">
	
		  		<input type="date" name="enddate" value="<?php echo set_value('enddate',$finalendate); ?>" class="form-control" required placeholder="Enter  close date">
		 
				
		<span class="text-danger"> <?php echo form_error('todate'); ?>
		
		</span>
	</div>
  </div>
  
    


	

	
	 <div class="form-group">

    <div class="col-sm-offset-2 col-sm-10">
     
	         <button type="submit"  value="report" name="btn_report" class="btn btn-primary"> View Report  </button>

    </div>
  </div>
  					<?php echo form_close();?>
			</div>							   
				
									
							
								
			<div class="modal-footer">

    

				</div>

		</div>
	    </div>
    </div>
	
	
			
			
			
			
			
			
			
			
			
			
		</div>
  
  </div>
			
			
			
		
		
  

 
</div>


















