<?php include('includes/Class_Branch.php');
	include('includes/AccountGroupMaster_Class.php');
 
	$modid=$_REQUEST['modid'];
	$frmid=$_REQUEST['frmid'];
	
	$action=$_REQUEST['action'];
	$daction=$_REQUEST['daction'];
 
	if($_REQUEST['Debit_note'] =='Edit' )
	$rurl="&modid=".$modid."&frmid=".$frmid."&action=".$action."&id=".$_REQUEST['id'];
	 else
	$rurl="&modid=".$modid."&frmid=".$frmid;
 	$msgErr=array();;
 if($_REQUEST['AddDRLedger'])
{
		$_SESSION['AcgBranch']=($_REQUEST['Branch']!=000? $_REQUEST['Branch']:'');
		$_SESSION['Acgvoucher_no']=($_REQUEST['voucher_no']!=''?$_REQUEST['voucher_no']:''); 
		$_SESSION['AcgDates']=($_REQUEST['Dates']!=''?$_REQUEST['Dates']:'') ;
		$_SESSION['AcgRef_no']=($_REQUEST['Ref_no']!=''?$_REQUEST['Ref_no']:''); 
		$_SESSION['Acgnarration']=($_REQUEST['narration']!=''?$_REQUEST['narration']:'');$_REQUEST['narration'];
		for($i=0;$i<count($_REQUEST['ledger_dr']);$i++)
		{
			if($_REQUEST['ledger_dr'][$i]!='' && $_REQUEST['debit_amount'][$i]!='' && $_REQUEST['dr_currency'][$i]!='')
			{
				$_SESSION['ACGLedgerId'][]=$_REQUEST['ledger_dr'][$i];
				$_SESSION['Amount'][]=$_REQUEST['debit_amount'][$i];
				$_SESSION['Currency'][]=$_REQUEST['dr_currency'][$i];
				$_SESSION['Amount_type'][]='DR';
			  }
		}
	 
 }
if($_REQUEST['AddCRLedger'])
{
 $j=0;
 
 		$_SESSION['AcgBranch']=($_REQUEST['Branch']!=000?$_REQUEST['Branch']:'');
		$_SESSION['Acgvoucher_no']=($_REQUEST['voucher_no']!=''?$_REQUEST['voucher_no']:''); 
		$_SESSION['AcgDates']=($_REQUEST['Dates']!=''?$_REQUEST['Dates']:'') ;
		$_SESSION['AcgRef_no']=($_REQUEST['Ref_no']!=''?$_REQUEST['Ref_no']:''); 
		$_SESSION['Acgnarration']=($_REQUEST['narration']!=''?$_REQUEST['narration']:'');$_REQUEST['narration'];

    if(count($_SESSION['ACGLedgerId'])>0)
   $j=count($_SESSION['ACGLedgerId']);
 		for($i=$j+1,$k=0;$k< count($_REQUEST['ledger_cr']);$i++,$k++)
		{
		  if($_REQUEST['ledger_cr'][$k]!=''){
 			  $_SESSION['ACGLedgerId'][]=$_REQUEST['ledger_cr'][$k];
			  $_SESSION['Amount'][]=$_REQUEST['credit_amount'][$k];
			  $_SESSION['Amount_type'][]='CR';
			 $_SESSION['Currency'][]=$_REQUEST['cr_currency'][$k];
			 }
		}	
		 
}

if($_REQUEST['daction']=='del')
{
 	$i=$_REQUEST['sid'];
	unset($_SESSION['ACGLedgerId'][$i]);
	unset($_SESSION['Amount'][$i]);
	unset($_SESSION['Currency'][$i]);
	unset($_SESSION['Amount_type'][$i]);
 	// $s->pageLocation('index.php?route=debit_note'.$rurl); 
}
if($_REQUEST['daction']=='datadel')
{
	$condition="id='".$_REQUEST['sid']."'";
	$s->DeleteRecord('account_book_details',  $condition);

}

if($_REQUEST['Debit_note'])
{
  $fstatus=true;
  if($_REQUEST['Branch']=='000')
  {
  	  $msgErr[]="Please Select Branch Name"	;
      $fstatus=false;
  }
  if($_REQUEST['voucher_no']=='')
  {
  	  $msgErr[]="Please Enter Voucher no"	;
      $fstatus=false;
  }
   if($_REQUEST['Dates']=='')
  {
  	  $msgErr[]="Please Enter Date"	;
      $fstatus=false;
  }
  
  if($_REQUEST['Ref_no']=='')
  {
  	  $msgErr[]="Please Enter Reference No"	;
      $fstatus=false;
  }
  if(count($_SESSION['ACGLedgerId'])==0)
  {
      	  $msgErr[]="Please Select DR/CR Amount "	;
     	 $fstatus=false;
   }

}
	 
  if($_REQUEST['Debit_note'] =='Save' && $fstatus )
  {
 
 	$dataArray['Branch']= $_REQUEST['Branch'] ;
	$dataArray['Voucher_NO']= $_REQUEST['voucher_no'] ;
	$dataArray['Dates']= FormatDateForSQL($_REQUEST['Dates']) ;	
	$dataArray['Ref_no']= FormatDateForSQL($_REQUEST['Ref_no']) ;	
	$dataArray['total_cramount']= $_REQUEST['total_cramount'] ;	
	$dataArray['total_dramount']= $_REQUEST['total_dramount'] ;
	$dataArray['narration']= $_REQUEST['narration'] ;
	$s->RecordInsert('account_book', $dataArray);
	  $getlastid=DB_Last_Insert_ID($db,'account_book', 'id');
	
	for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		{
			if($_SESSION['ACGLedgerId'][$i]!='' && $_SESSION['Amount'][$i]!='' && $_SESSION['Currency'][$i]!='')
			{
				$data['account_bookid']=$getlastid;
				$data['ledgerid']=$_SESSION['ACGLedgerId'][$i];
				$data['amount']=$_SESSION['Amount'][$i];
				$data['currency']=$_SESSION['Currency'][$i];
				$data['amount_type']=$_SESSION['Amount_type'][$i];
				$s->RecordInsert('account_book_details', $data);
			  }
		}
		unset($_SESSION['ACGLedgerId']);
	    unset($_SESSION['Amount']);
	    unset($_SESSION['Currency']);
	    unset($_SESSION['Amount_type']);
			
    $s->pageLocation('index.php?route=debit_note&daction=view'.$rurl); 
   }
  
  
  
//*****************************************************************************************

  if($_REQUEST['Debit_note'] =='Edit' && $fstatus)
  {
 
 	$dataArray['Branch']= $_REQUEST['Branch'] ;
	$dataArray['Voucher_NO']= $_REQUEST['voucher_no'] ;
	$dataArray['Dates']= ConvertSQLDate($_REQUEST['Dates']) ;	
	$dataArray['Ref_no']=$_REQUEST['Ref_no'];	
	$dataArray['total_cramount']= $_REQUEST['total_cramount'] ;	
	$dataArray['total_dramount']= $_REQUEST['total_dramount'] ;
	$dataArray['narration']= $_REQUEST['narration'] ;
	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('account_book', $dataArray,$cond);
	  $getlastid=DB_Last_Insert_ID($db,'account_book', 'id');
	
	for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		{
			if($_SESSION['ACGLedgerId'][$i]!='' && $_SESSION['Amount'][$i]!='' && $_SESSION['Currency'][$i]!='')
			{
				$data['account_bookid']=$_REQUEST['id'];
				$data['ledgerid']=$_SESSION['ACGLedgerId'][$i];
				$data['amount']=$_SESSION['Amount'][$i];
				$data['currency']=$_SESSION['Currency'][$i];
				$data['amount_type']=$_SESSION['Amount_type'][$i];				 
				$s->RecordInsert('account_book_details', $data);
			  }
		}
			 
		 

	for($i=0;$i<count($_REQUEST['address_book']);$i++)
		{
		 
			 if($_REQUEST['ledgerid'][$i]!='' && $_REQUEST['amount'][$i]!='' && $_REQUEST['currency'][$i]!='')
			{
			//	$data['account_bookid']=$_REQUEST['address_book'][$i];
				$data['ledgerid']=$_REQUEST['ledgerid'][$i];
				$data['amount']=$_REQUEST['amount'][$i];
				$data['currency']=$_REQUEST['currency'][$i];
				$data['amount_type']=$_REQUEST['amount_type'][$i];	
				$cond="id='".$_REQUEST['address_book'][$i]."'"; 
				$s->RecordEdit('account_book_details', $data, $cond);
				 
			  }
		}		
		
		
		unset($_SESSION['ACGLedgerId']);
	    unset($_SESSION['Amount']);
	    unset($_SESSION['Currency']);
	    unset($_SESSION['Amount_type']);
			
     $s->pageLocation('index.php?route=debit_note&daction=view'.$rurl); 
   }

//*****************************************************************************************  


  
  
  
  
  
  
  		$_SESSION['AcgBranch']=($_REQUEST['Branch']!=000?$_REQUEST['Branch']:'');
		$_SESSION['Acgvoucher_no']=($_REQUEST['voucher_no']!=''?$_REQUEST['voucher_no']:''); 
		$_SESSION['AcgDates']=($_REQUEST['Dates']!=''?$_REQUEST['Dates']:'') ;
		$_SESSION['AcgRef_no']=($_REQUEST['Ref_no']!=''?$_REQUEST['Ref_no']:''); 
		$_SESSION['Acgnarration']=($_REQUEST['narration']!=''?$_REQUEST['narration']:'');$_REQUEST['narration'];



	$Branch= $_SESSION['AcgBranch'] ;
	$voucher_no= $_SESSION['Acgvoucher_no'] ;
	$Dates= ConvertSQLDate($_SESSION['AcgDates']) ;	
	$Ref_no= ($_SESSION['AcgRef_no']) ;	
 	$narration= $_SESSION['Acgnarration'] ;
	
	/*
  	$Branch= $_REQUEST['Branch'] ;
	$voucher_no= $_REQUEST['voucher_no'] ;
	$Dates= ConvertSQLDate($_REQUEST['Dates']) ;	
	$Ref_no= ConvertSQLDate($_REQUEST['Ref_no']) ;	
	$total_cramount= $_REQUEST['total_cramount'] ;	
	$total_dramount= $_REQUEST['total_dramount'] ;
	$narration= $_REQUEST['narration'] ;
	*/
  
  ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td ><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
  <tr>
  	<td style="text-align:center;"><h1>Debit Note </h1></td>
  </tr>
</table>
<? if($daction=='view')
{?>
<a href="index.php?route=debit_note&action=addnew<?php  echo $rurl;?>"><input name="Add New" type="button" value="Add New" /></a>
<table width="100%" border="1">
  <tr>
    <td><strong>VoucherId</strong></td>
    <td><strong>Branch</strong></td>
	 <td><strong>Date</strong></td>
    <td><strong>Narration</strong></td>
    <td>&nbsp; </td><td>&nbsp; </td>
  </tr><?php 
    $ResultData=DB_query("select *  from account_book  ",$db);
    while($RowData = DB_fetch_array($ResultData))
   {  if($j++%2==0)
     $bgcolor="#CCCCCC";
	 else{
	 $bgcolor="#FFFFFF";
	 }
	 ?>
   <tr bgcolor="<?php echo $bgcolor;?>">
	<td><?php echo $RowData['Voucher_NO'] ?></td>
	<td><?php echo $RowData['Branch'] ?></td>
	<td><?php echo ConvertSQLDate($RowData['Dates']); ?></td>
	<td><?php echo $RowData['narration'] ?></td>
    <td><a href="#" onclick='popup("address_book.php?id=<?php  echo $RowData['id'].$rurl?>",600,600)'>View</a></td>
	<td><a href="index.php?route=debit_note<?php  echo $rurl;?>&action=Edit&id=<?php  echo $RowData['id'];?>" >Edit</a></td>
  </tr>
  <?php } ?>
</table>

<?php }

//  EDIT SECTION
else if($_REQUEST['action']=='Edit')
{

$query="select *  from account_book  where id='".$_REQUEST['id']."' ";
   	$result=DB_query($query,$db);
			$myrow=DB_fetch_array($result);
 			$voucher_no=$myrow['Voucher_NO'];
			$Dates=ConvertSQLDate($myrow['Dates']);
			$Ref_no= $myrow['Ref_no'] ;
			$voucher_no=$myrow['Voucher_NO'];
			$narration=trim($myrow['narration']);
			$Branch=$myrow['Branch'];

?>
<form action="" method="post">
 <table width="99%" border="0" cellspacing="1" cellpadding="3" bgcolor="#ccc" style="border:1px sol #CCC;">
    <?php if(count($msgErr)>0){?>
   <tr>
   <td colspan="4"><?php
   for($i=0;$i<count($msgErr);$i++)
   {
     echo "<div class='error'>".$msgErr[$i].'</div>';
   }
    ?></td>
   </tr>
   <?php } ?>
  <tr bgcolor="#FFFFFF">
    <th width="23%">Branch</th>
    <td width="29%"><?php echo $ClassBranch->Branch($Branch);?> </td>
    <th width="21%">Voucher No. </th>
    <td width="27%"><input name="voucher_no" type="text" size="27"  value="<?php echo $voucher_no;?>" style="width:98%;" /></td>
    </tr>
  <tr  bgcolor="#FFFFFF">
    <th>Date</th>
    <td><input name="Dates" type="text" size="27" class="datepicker"  value="<?php echo $Dates;?>"   style="width:98%;" /></td>
    <th>Debit Ref. No.</th>
    <td><input name="Ref_no" type="text" size="27"  value="<?php echo $Ref_no;?>"   style="width:98%"/></td>
	</tr>
  
   <tr  bgcolor="#FFFFFF">
    <td colspan="4"><table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
  <tr>
    <th nowrap="nowrap">Dr. Ledger Name</th>
    
    <th>Debit(INR)</th>
     
    <th>Curr.</th>
  </tr>
  <tr  bgcolor="#FFFFFF">
    <td><?php $AccountMaster-> AccountLeger($ledger_dr,'ledger_dr[]');?>
	
			 
	 	    </td>
    <td> <input name="debit_amount[]" type="text" value="<?php $debit_amount;?>" /></td>
    <td>
	   <select name="dr_currency[]" style="width:100%" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
       </select>
	  </td>
    <td style="text-align:right; width:10%;" nowrap="nowrap"> <input type="submit" name="AddDRLedger" value="Add" /> 
	
	<input type="button" name="input" value="Bill wise Adj." /></td>
  </tr>
  <tr>
    <th nowrap="nowrap">Cr. Ledger Name</th>
    <th>Credit(INR)</th>
    <th>Curr.</th>
    </tr>
  <tr  bgcolor="#FFFFFF">
    <td>
	 <?php $AccountMaster-> AccountLeger($ledger_cr,'ledger_cr[]');?>
			
	 	    </td>
    <td> <input name="credit_amount[]" type="text" value="<?php $credit_amount;?>" /></td>
	
    <td><select name="cr_currency[]" style="width:100%" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
      </select></td>
    <td style="text-align:right;"> <input type="submit" name="AddCRLedger" value="Add" /> </td>
  </tr>
  <tr>
  	<td colspan="8"><table width="100%" border="0" cellspacing="1" cellpadding="2">
  	  <tr>
  	    <th scope="col"><strong> Ledger Name</strong></th>
  	    <th scope="col"><strong>Amount</strong></th>
  	    <th scope="col"><strong>Credit</strong></th>
  	    <th scope="col"><strong>Debit</strong></th>
  	  
  	    <th scope="col"><strong>Currency</strong></th>
		 <th scope="col"><strong>&nbsp;</strong></th>
  	    </tr>
		<?php 
 		$total_cr=array();$total_dr=array();
	
 
 
          $query1="select *  from account_book_details where account_bookid ='".$myrow['id']."'  ";
   		$result1=DB_query($query1,$db);
		while($myrow1=DB_fetch_array($result1)){
 		?>
  	  <tr bgcolor="#ffffff" >
  	    <td><?php  //echo $i;?> 
		<input name="address_book[]" type="hidden" value="<?php  echo $myrow1['id'];?>" />
		 
	 	
		<?php $AccountMaster-> AccountLeger($myrow1['ledgerid'],'ledgerid[]');?>
		 
		 </td>
  	    <td> 
		<input name="amount[]" type="text"  value="<?php  echo  $myrow1['amount'];?>"/>
		</td>
  	    <td> <?php
		echo ($myrow1['amount_type']=='CR'?  $myrow1['amount']:"0");
		$total_cr[]=($myrow1['amount_type']=='CR'?  $myrow1['amount']:"0");
		
		?>
		<input name="amount_type[]" type="hidden"  value="<?php  echo  $myrow1['amount_type'];?>"/>
		</td>
  	     <td> <?php  echo  ($myrow1['amount_type']=='DR'?  $myrow1['amount']:"0");
		 $total_dr[]=($myrow1['amount_type']=='DR'?  $myrow1['amount']:"0");
		 ?></td>
  	   <td> 
	   <input name="currency[]" type="text"  value="<?php  echo $myrow1['currency'];?>"/>
	   </td>
	   <td> <a href="index.php?route=debit_note<?php echo $rurl;?>&daction=datadel&sid=<?php  echo $myrow1['id'];?>&id=<?php echo $_REQUEST['id'];?>">DEL</a></td>
  	    </tr>
		 
<?php     }
		
if(count($_SESSION['ACGLedgerId'])>0)		 
{
 
foreach ($_SESSION['ACGLedgerId'] as $i => $LED)
{ 
 		?>
  	  <tr bgcolor="#ffffff" >
  	    <td><?php  //echo $i;?> <?php  echo $s->getField('accountledger','AccountsID',$LED,'AccountName');;?></td>
  	    <td> <?php  echo $_SESSION['Amount'][$i];?></td>
  	    <td> <?php
		echo ($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		$total_cr[]=($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		
		?></td>
  	     <td> <?php  echo  ( $_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 $total_dr[]=($_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 ?></td>
  	   <td> <?php  echo $_SESSION['Currency'][$i];?></td>
	   <td> <a href="index.php?route=debit_note<?php echo $rurl;?>&daction=del&sid=<?php echo $i?>">DEL</a></td>
  	    </tr>
		<?php } 
} 		
		?>
  	   
	  </table></td>
  </tr>
  <tr>
  	<td colspan="8">
    	<table width="100%" cellpadding="0" cellspacing="0" border="0">

        	<tr bgcolor="#fff">
    <th nowrap="nowrap">Total Credit Amt.</th>
    <td><input type="text" name="total_cramount"  id='total_cramount' value="<?php echo array_sum($total_cr); ?>"   /></td>
    <th align="right" nowrap="nowrap">Total Debit Amt.</th>
    <td><input type="text" name="total_dramount" id="total_dramount" value="<?php echo array_sum($total_dr); ?>" /></td>
    <th align="right" nowrap="nowrap">Difference Amt.</th>
    <td><?php 
$diff=	array_sum($total_cr)-array_sum($total_dr);
	?>
	<input type="text" name="diff_amount" id="credit2" value="<?php echo array_sum($total_cr)-array_sum($total_dr); ?>" /></td>
  </tr>
  <tr bgcolor="#fff">
    <th> Narration</th>
    <td colspan="5"><textarea name="narration" id="narration" cols="45" rows="5" style="width:100%; height:40px;">
	<?php echo $narration;?></textarea></td>
    </tr>
        </table>    </td>
  </tr>
    </table></td>
    </tr>
</table>
<?php if($diff==0) ?>
 <input type="submit" name="Debit_note" value="Edit" /> 
</form>



<?php 
}
else{?>
 
<form action="" method="post">
 <table width="99%" border="0" cellspacing="1" cellpadding="3" bgcolor="#ccc" style="border:1px sol #CCC;">
<?php if(isset($daction)){?>
 <tr bgcolor="#FFFFFF">
 <td colspan="4" ><div class="success"><?php echo "Record ".$daction." Successfully" ?></div></td>
  </tr><?php } ?>
   
   
     <?php if(count($msgErr)>0){?>
   <tr>
   <td colspan="4"><div class='error'><?php
   for($i=0;$i<count($msgErr);$i++)
   {
     echo "".$msgErr[$i].'<br>';
   }
    ?></div></td>
   </tr>
   <?php } ?>
  <tr bgcolor="#FFFFFF">
    <th width="23%">Branch</th>
    <td width="29%"><?php echo $ClassBranch->Branch($_REQUEST['Branch']);?> </td>
    <th width="21%">Voucher No. </th>
    <td width="27%"><input name="voucher_no" type="text" size="27"  value="<?php echo $voucher_no;?>" style="width:98%;" /></td>
    </tr>
  <tr  bgcolor="#FFFFFF">
    <th>Date</th>
    <td><input name="Dates" type="text" size="27" class="datepicker"  value="<?php echo $Dates;?>"   style="width:98%;" /></td>
    <th>Debit Ref. No.</th>
    <td><input name="Ref_no" type="text" size="27" class="datepicker"  value="<?php echo $Ref_no;?>"   style="width:98%"/></td>
	</tr>
  
   <tr  bgcolor="#FFFFFF">
    <td colspan="4"><table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
  <tr>
    <th nowrap="nowrap">Dr. Ledger Name</th>
    
    <th>Debit(INR)</th>
     
    <th>Curr.</th>
  </tr>
  <tr  bgcolor="#FFFFFF">
    <td><?php $AccountMaster-> AccountLeger($ledger_dr,'ledger_dr[]');?>
	
			 
	 	    </td>
    <td> <input name="debit_amount[]" type="text" value="<?php $debit_amount;?>" /></td>
    <td>
	   <select name="dr_currency[]" style="width:100%" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
       </select>
	  </td>
    <td style="text-align:right; width:10%;" nowrap="nowrap"> <input type="submit" name="AddDRLedger" value="Add" /> 
	
	<input type="button" name="input" value="Bill wise Adj." /></td>
  </tr>
  <tr>
    <th nowrap="nowrap">Cr. Ledger Name</th>
    <th>Credit(INR)</th>
    <th>Curr.</th>
    </tr>
  <tr  bgcolor="#FFFFFF">
    <td>
	 <?php $AccountMaster-> AccountLeger($ledger_cr,'ledger_cr[]');?>
			
	 	    </td>
    <td> <input name="credit_amount[]" type="text" value="<?php $credit_amount;?>" /></td>
	
    <td><select name="cr_currency[]" style="width:100%" >
       <option value="INR" <?php echo $currency_type=='INR'?"selected":"";?>>INR</option>
      </select></td>
    <td style="text-align:right;"> <input type="submit" name="AddCRLedger" value="Add" /> </td>
  </tr>
  <tr>
  	<td colspan="8"><table width="100%" border="0" cellspacing="1" cellpadding="2">
  	  <tr>
  	    <th scope="col"><strong> Ledger Name</strong></th>
  	    <th scope="col"><strong>Amount</strong></th>
  	    <th scope="col"><strong>Credit</strong></th>
  	    <th scope="col"><strong>Debit</strong></th>
  	  
  	    <th scope="col"><strong>Currency</strong></th>
		 <th scope="col"><strong>&nbsp;</strong></th>
  	    </tr>
		<?php 
		 
		$total_cr=array();$total_dr=array();
		
if(count($_SESSION['ACGLedgerId'])>0)		 
{
 
foreach ($_SESSION['ACGLedgerId'] as $i => $LED)
{ 
		//  for($i=0;$i<count($_SESSION['ACGLedgerId']);$i++)
		 
		?>
  	  <tr bgcolor="#ffffff" >
  	    <td><?php  //echo $i;?> <?php  echo $s->getField('accountledger','AccountsID',$LED,'AccountName');;?></td>
  	    <td> <?php  echo $_SESSION['Amount'][$i];?></td>
  	    <td> <?php
		echo ($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		$total_cr[]=($_SESSION['Amount_type'][$i]=='CR'? $_SESSION['Amount'][$i]:"0");
		
		?></td>
  	     <td> <?php  echo  ( $_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 $total_dr[]=($_SESSION['Amount_type'][$i]=='DR'? $_SESSION['Amount'][$i]:"0");
		 ?></td>
  	   <td> <?php  echo $_SESSION['Currency'][$i];?></td>
	   <td> <a href="index.php?route=debit_note<?php echo $rurl;?>&daction=del&sid=<?php echo $i?>">DEL</a></td>
  	    </tr>
		<?php } 
}else{

echo "<tr><td colspan=5>No record</td></tr>";
}		
		?>
  	   
	  </table></td>
  </tr>
  <tr>
  	<td colspan="8">
    	<table width="100%" cellpadding="0" cellspacing="0" border="0">
        	<tr bgcolor="#fff">
    <th nowrap="nowrap">Total Credit Amt.</th>
    <td><input type="text" name="total_cramount"  id='total_cramount' value="<?php echo array_sum($total_cr); ?>"   /></td>
    <th align="right" nowrap="nowrap">Total Debit Amt.</th>
    <td><input type="text" name="total_dramount" id="total_dramount" value="<?php echo array_sum($total_dr); ?>" /></td>
    <th align="right" nowrap="nowrap">Difference Amt.</th>
    <td><?php 
$diff=	array_sum($total_cr)-array_sum($total_dr);
	?>
	<input type="text" name="diff_amount" id="credit2" value="<?php echo array_sum($total_cr)-array_sum($total_dr); ?>" /></td>
  </tr>
  <tr bgcolor="#fff">
    <th> Narration</th>
    <td colspan="5"><textarea name="narration" id="narration" cols="45" rows="5" style="width:100%; height:40px;">
	<?php echo $narration;?></textarea></td>
    </tr>
        </table>    </td>
  </tr>
    </table></td>
    </tr>
</table>
<?php if($diff==0) ?>
 <input type="submit" name="Debit_note" value="Save" /> 
</form>
<?php } ?>

 