<?php include("../../include/dbconnect.php");
$filename='templedger';
?>
<script type="text/javascript" src="../../include/datetimepicker.js"></script>
<script language="javascript">
function check()
{


	if(myform2.dbs.value != myform2.crs.value)
	{
		alert("Closing Balance  is Not Tallying. Please correct");
		myform2.dbs.focus();
		return false;
	}	

	

}
</script>
<?
// Get all _POST VALUES HERE..

foreach($_POST as $q=>$r)
{
	$_POST[$q] = trim($r);
}
$myHead = $_POST['Head'];


if ($Clear == "clear")
{
	$res = mysqli_query($conn,"truncate $filename") or die(mysqli_error());
}
?>

<title>Receipt</title>
</head>
<body background="images/blater01.gif" topmargin=0>
<center> 
<form name='myform' method='POST' action='<?=$_SERVER['PHP_SELF']?>?compcode=<?=$compcode?>&branch=<?=$branch?>'>
  <table border=0 cellpadding=3 cellspacing=4 width="726" bgcolor="#E6E6FF">
    <tr>
      <td  colspan="100%"  bgcolor="#93BEE2"><font face="Verdana, Arial, Helvetica, sans-serif"><b>Journal Entries </b></font></td>
    </tr>
    <?
// Pull all the data from Cat master & populate it

$result= mysqli_query($conn," select name from company where compcode ='$compcode' order by name")or die( mysqli_error() );
$row = mysqli_fetch_row($result);
$compname = $row[0];


$result= mysqli_query($conn," select name,account_year from branches where code='$branch' order by name")or die( mysqli_error() );
$row = mysqli_fetch_row($result);
$branchname = $row[0];
$account_year = $row[1];


?>
    <tr >
      <td  width="104" height="31" align="left"  ><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>Company 
        &nbsp;</b></font></td>
      <td  ><font  color="#FF0000"  face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>
        <?=$compname?>
      </b></font></td>
      <td  width="96" height="31" align="left" ><font  color="#003366" size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b>Place&nbsp;</b></font></td>
      <td width="162"  ><font  color="#FF0000"  face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>
        <?=$branchname?>
      </b></font></td>
    </tr>
    <tr>
    <td  colspan="100%"><table width="707">
      <td width="63" height="31" align="left" ><b><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"> GL</font></b></td>
      <td width="316"  ><?


$res = mysqli_query($conn,"SELECT distinct acctcode,description from acctmast where active != 'No' and reserved_word = 'CASH'  order by acctmast.description ") or die(mysqli_error());
$arr = mysqli_fetch_row($res);

$sBuyQuery = mysqli_query($conn,"SELECT distinct acctcode,description from acctmast where active != 'No' and reserved_word not in('DEBITORS','CREDITORS','AADDEBITORS') order by acctmast.description ") or die(mysqli_error());
?>
          <select name='Head' onChange="javascript:submit();">
            <?
	//	echo "<option selected value=\"$arr[0]\">$arr[1]";
		while($row = mysqli_fetch_row($sBuyQuery))
		{
			
			if(isset($myHead) && preg_match("/" . preg_quote($myHead, '/') . "/", $row[0]))
			{
				echo "<option selected value=\"$row[0]\">$row[1]";
			}
			else{
				echo "<option value=\"$row[0]\">$row[1]";
			}
		}
?>
        </select></td>
        
        <?
		$clb = gl_balance($compcode,$branch,$myHead,$account_year);

		?>
        
		<td width="40" height="31" align="right" ><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#D6148D">CLB
		  &nbsp;</font></b></font></td>
		<td width="120" align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#D6148D"><b><?=$clb?></b></font></td>
        
		<td width="47" height="31" align="right" ><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Type
		  &nbsp;</font></b></font></td>
		<td width="93" > <select name="type">
        <option value="DR">Debit</option>
        <option value="CR">Credit</option>        
        
        </select>		</td>   </table></td>     
    </tr>
 
	
		<tr > 



		<td width="69"  align="left" ><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Voucher No
		  &nbsp;</font></b></font></td>
		<td width="300"  > <input type="text"  size="10" onKeyPress="return event.keyCode!=13" name="voucher_no" value= '<?=$voucher_no?>' >		</td>
		<td width="69"  align="left" ><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Bill No
		  &nbsp;</font></b></font></td>
		<td width="300"  ><input type="text"  size="10" onKeyPress="return event.keyCode!=13" name="bill_no" value= '<?=$bill_no?>' ></td>
	</tr>
        <tr><td width="71"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003366" ><b>Cheque/RTGS No</b></font></b></td>
		<td width="98"><input type="text"  size="15" onKeyPress="return event.keyCode!=13" name="cheqno" value= '<?=$cheqno?>' ></td>
		<td width="28"   align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Date</font></b></font> </td>
		<td width="113"  class="small_site_font_black" > 
		<?
		if($today== '')
		{
			//$today=strftime ("%d%m%Y", mktime(0,0,0,date("m"),date("d"),date("Y"))); 
		}	
		?>
		
		<? /*
		<input name="today" type="text" onKeyPress="return event.keyCode!=13" class="text_box_button" value="<?=$today?>" size="10"   > 
		<a href="#" onClick="cal.select(document.myform.today,'anchor1','dd/MM/yyyy'); return false;"name="anchor1" id="anchor1"><img  src="../../images/cal.gif" border="0"></a>		</td></table></td>
		*/ ?>

       <input name="today"  id="today" value="<?=$today?>" type="text" size="10">
<a href="javascript:NewCal('today','DDMMYYYY')"><img
 src="../../images/cal.gif" alt="Pick a date" 
border="0"></a>    </td>             
    </tr>
    <tr>
      <td align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Description</b></font></td>
      <td  ><input type="text" size="50"   onKeyPress="return event.keyCode!=13" name="remarks" value="<?=$remarks?>"></td>
      <td width="71" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Amount</b></font></td>
      <td  align="left" ><input type="text" size="15"   onKeyPress="return event.keyCode!=13" name="amount" value=""></td>
    </tr>
   <tr>
    	      <td align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Type of Transaction</b></font></td>
      <td  >
      <select name="ttype">
      <option value="Payments">Payments
      <option value="Receipts">Receipts
      <option value="Debitnote">Debit Note
      <option value="Creditnote">Credit Note
      </select>
      </td></tr>    
  </table>
  <table>
</table>
 <table cellpading=0 cellspacing=10 width= 640>
<tr>
<td width="288" align="right">
 <blockquote>  <input type =submit name="Clear" value = "clear" ></blockquote></td>
<td width="288" align="right">
 <blockquote>  <input type =submit name="refresh" value = "Refresh" ></blockquote></td>
 <td width="307" align="left"><blockquote><input type =submit name="add" value = "Add" ></blockquote></td>

</table>

<?






if($amount >0 and $add == 'Add')
{


	
	$today = actdate($today);


	$result = mysqli_query($conn,"select account_year,start_date,end_date from branches where code = '$branch' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$account_year = $row[0];
	$startdt = $row[1];
	$enddt = $row[2];
	$date = us($today);
	

	if($date < $startdt or $date > $enddt)
	{
	?>
		<script language="JavaScript">
		alert("Selected date does not belong to current accounting year. Please recheck the date");
		</script>	
	<?
	exit;
	}

	if($remarks=='')
	{
	?>
		<script language="JavaScript">
		alert("Please enter Description");
		</script>	
	<?
	exit;
	}	
	
	$sresult = mysqli_query($conn,"select sum(dbamt),sum(cramt) from `$filename`") or die(mysqli_error());
	$srow = mysqli_fetch_row($sresult);
	$cdbamt = $srow[0];
	$ccramt = $srow[1];

	if($cdbamt>0 or $cdramt>0)
	{
		if($cdbamt == $ccramt )
		{
			?>
			<script language="JavaScript">
			alert("Please Submit the Talied Entries and add again. PRESS REFRESH BUTTON TO VIEW ENTRIES");
			</script>	
		  
			<?
			exit;
		}
	}
	/*
	$result = mysqli_query($conn,"select acctcode from `$filename` where acctcode = '$myHead' and branch_code = '$branch' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$mhd = $row[0];
	if($myHead == $mhd)
	{
		?>
		<script language="JavaScript">
		alert("This GL is already Selected Please recheck the GL");
		</script>	
      
        <?
		exit;
	}	
	*/
	
	

	$result = mysqli_query($conn,"select distinct date from `$filename` where date and branch_code = '$branch' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	if(is_array($row))
	{
	$tmpdate = $row[0];
	if($date != $tmpdate)
	{
		?>
		<script language="JavaScript">
		alert("You have opted a different date, which is not allowed. Please correct the date");
		</script>	
   
        <?
		exit;
	}	
	}

	
	
	if($type== 'CR')
	{
		$cramt= $amount;
		$dbamt=0;
	}
	elseif($type=='DR')
	{
		$dbamt= $amount;
		$cramt=0;		
	}


	$result = mysqli_query($conn,"insert into $filename (acctcode,date,narration,cheqno,dbamt,cramt,type,amttype,invno,compcode,branch_code,trans_id,account_year,entered_by,voucher_no,cheq_date) values ( '$myHead','$date','$remarks','$cheqno','$dbamt','$cramt','$type','J','$bill_no','$compcode','$branch','$trans_id','$account_year','$filename','$voucher_no','$ttype')") or die(mysqli_error());

	
}

?>


<tr >
</form>  


<form name='myform2' method='POST' action='entry.php?compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>' >


<input type="hidden" name="compcode" value="<?=$compcode?>">
<input type="hidden" name="branch" value="<?=$branch?>">
<input type="hidden" name="account_year" value="<?=$account_year?>">



<table width="809">
  <tr  bgcolor="#A4A4FF">

<td width="49" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Modify&nbsp;</b></font></td>
<td width="64" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Date&nbsp;</b></font></td>
<td width="216" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>GL Head&nbsp;</b></font></td>
<td width="238" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Description&nbsp;</b></font></td>
<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Bill No</b></font></td>
<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>V. No</b></font></td>

<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Cheqno</b></font></td>

<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Debit</b></font></td>
<td width="55" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Credit</b></font></td>
<td width="46" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Type</b></font></td>
<td width="46" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Transaction</b></font></td>
<td width="41" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Trans.Id</b></font></td>

<?

$netdb = 0;
$netcr=0;

$result = mysqli_query($conn,"select acctcode,date,narration,dbamt,cramt,type,trans_id,record,date,cheqno,invno,voucher_no,cheq_date from `$filename` ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$acctcode = $row[0];
	$date = british($row[1]);
	$narration = $row[2];
	$dbamt = $row[3];
	$cramt = $row[4];
	$type = $row[5];
	$trans_id = $row[6];
	$record = $row[7];
	$mdt = british($row[8]);
	$cheqno = $row[9];
	$bill_no = $row[10];
	$voucher_no = $row[11];
	$ttype = $row[12];
	$netdb = $netdb+$dbamt;
	$netcr = $netcr + $cramt;

	$prodname = mysqli_fetch_array(mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode'")) or die(mysqli_error());
	?>
	<tr  bgcolor="#D7D7FF">
	<td width="49" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><a href = "peditview.php?record=<?echo($record);?>&compcode=<?=$compcode?>&branch=<?=$branch?>"><?echo($record);?></a></b></font></td>
	<td width="64" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$mdt?></b></font></td>
	<td width="216" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$prodname['description']?></b></font></td>
	<td width="238" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$narration?></b></font></td>
	<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$bill_no?></b></font></td>
    <td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$voucher_no?></b></font></td>    
	<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$cheqno?></b></font></td>
	<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$dbamt?></b></font></td>
	<td width="55" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$cramt?></b></font></td>  
	<td width="46" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$type?></b></font></td>
	<td width="46" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$ttype?></b></font></td>    
	<td width="41" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$trans_id?></b></font></td>

	</tr>
	
	<?
}
if($ledgclosing < 0)
{
	$gtotal = $op +$ledgclosing+$nettot;
}
elseif($ledgclosing >= 0)
{
	$gtotal = $op -$ledgclosing+$nettot;
}
if($gtotal < 0)
{
	$gtotal= $gtotal*-1;
	$amt = $gtotal.'&nbsp;&nbsp;CR';
}
elseif($gtotal >0)
{
	$amt = $gtotal.'&nbsp;&nbsp;DR';
}
$aresult = mysqli_query($conn,"select sum(dbamt) from `$filename`   ") or die(mysqli_error());
$arow = mysqli_fetch_row($aresult);
$tpayments = $arow[0];	

$aresult = mysqli_query($conn,"select sum(cramt) from `$filename`   ") or die(mysqli_error());
$arow = mysqli_fetch_row($aresult);
$treceipts = $arow[0];	



?>

<tr  bgcolor="#D7D7FF">
<td  colspan="7" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Total</b></font></td>
<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($netdb,2)?></b></font></td>
<td width="64" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($netcr,2)?></b></font></td>
<td colspan="3"></td>
</tr>
<input type="hidden" name="dbs" value="<?=$netdb?>">
<input type="hidden" name="crs" value="<?=$netcr?>">

<?
if($netdb == $netcr)
{
	if($netdb >0 and $netcr>0)
	{

		?>
        <script>
			alert("Balance Talyed. Submit if this transaction is over");
		</script>
        <?
		
	}
}
?>
</table>
<table width="704">





  <tr> 
    <td align="right" colspan="2" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Confirm</b></font></td>
    <td> <input type="radio"    onKeyPress="return event.keyCode!=13"   name="cnf" value = "No" checked> <font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>No 
    </b></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio"    onKeyPress="return event.keyCode!=13"   name="cnf" value = "Yes"> <font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Yes
      </b></font>&nbsp;</td>
  </tr> 
  <table cellpading=0 cellspacing=10 width= 50>
    <tr> 
      <td> <blockquote> <input type =submit name="submit" value = "Submit" onClick="return check()"></blockquote></td>
  </table>
</table>
</form>
<font size=1> 
<br><br><font size=1><center><a href= "javascript:window.close();">Close Window</a></center></font>
</font> 
