<?php include("../../include/dbconnect.php");

$filename = 'templedger';

// Get all _POST VALUES HERE..
if($status == 'NEW')
{
	foreach($_POST as $q=>$r)
	{
		$_POST[$q] = trim($r);
	}
	$myDate = $_POST['Date'];
}

if($filename == '')
{

	?>
    	<script language="javascript">
		alert("You have closed the Main Program Browser before saving the data, due to that Data won't be saved. Please login ");
        </script>
    <?
	exit;

}

?>

<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>
<SCRIPT LANGUAGE="JavaScript" src="../../include/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>
<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?>&status=<?=$status?>'>
  <table border=0 cellpadding=3 cellspacing=4 width="808" bgcolor="#E6E6FF">
    <tr>
      <td  colspan="100%"  bgcolor="#93BEE2"><font face="Verdana, Arial, Helvetica, sans-serif"><b>Edit Daybook 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 width="320"  ><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="244"  ><font  color="#FF0000"  face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>
        <?=$branchname?>
      </b></font></td>
    </tr>
    <tr>
      <td height="31" align="right"  colspan="2"><b><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif">Select Date</font></b></td>
    <td colspan="2"  ><?


$sBuyQuery = mysqli_query($conn,"SELECT distinct date from ledger where account_year = '$account_year' and compcode = '$compcode' and branch_code = '$branch' and amttype != 'J' order by date desc ") or die(mysqli_error());



?>
                  <select name='Date' onChange="javascript:submit();">
            <?
			if($status=='NEW')
			{
				echo "<option selected value=\0\">Date";
				while($row = mysqli_fetch_row($sBuyQuery))
				{
					$dt = british($row[0]);
					if(isset($myDate) && preg_match("/" . preg_quote($myDate, '/') . "/", $row[0]))
					{
						echo "<option selected value=\"$row[0]\">$dt";
					}
					else{
						echo "<option value=\"$row[0]\">$dt";
					}
				}
			}	
			if($mdt!='')
			{
				$dt = british($mdt);
				$myDate=$mdt;
				echo "<option selected value=\"$mdt\">$dt";
			}		
?>
        </select></td>
		
    </tr>
  </table>
  <table>
</table>
 
<?



	$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];

if($status=='NEW')
{
	$tresult = mysqli_query($conn,"truncate table $filename") or die(mysqli_error());

	$query= "insert into $filename(acctcode,date,narration,dbamt,cramt,type,invno,compcode,branch_code,trans_id,account_year,amttype,date_of_entry,cheqno,voucher_no,cheq_date) select acctcode,date,narration,dbamt,cramt,type,invno,compcode,branch_code,trans_id,account_year,amttype,date_of_entry,cheqno,voucher_no,taxtype from ledger where date = '$myDate' and amttype != 'J' and branch_code = '$branch' and compcode = '$compcode' ";

	mysqli_query($conn,$query) or die(mysqli_error());

}	


?>


<tr >
</form>  


<form name='myform2' method='POST' action='' onSubmit="return check()" >


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


<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>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,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]);
	$ttype = $row[9];
	$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><?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><?=$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>
	
	<?
}


$cresult = mysqli_query($conn,"select acctcode,date,narration,dbamt,cramt,type,trans_id,record,date from `$filename` ") or die(mysqli_error());
$crow = mysqli_fetch_row($cresult);
if(!is_array($crow))
{
	if($status !='NEW')
	{
	?>
    	<script language="javascript">
			alert("Please confirm and submit to incorprate the changes");
		
		</script>
    <?
	}
}




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="4" 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?>">

</table>
<table width="704">






 

</table>
</form>
<font size=1> 
<br><br><font size=1><center><a href= "javascript:window.close();">Close Window</a></center></font>
</font> 

