<?php include("../../include/dbconnect.php");
$filename = 'templedger';


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

	foreach($_POST as $k=>$l)
	{
		$_POST[$k] = trim($l);
	}
	$myGL = $_POST['GL'];
}

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?>&mdt=<?=$mdt?>&mGL=<?=$mGL?>&trans_id=<?=$trans_id?>&account_year=<?=$account_year?>'>
  <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?>&nbsp;&nbsp;&nbsp;(Financial Year :<?=$account_year?>)
      </b></font></td>
      <td  width="96" height="31" align="left" ><font  color="#003366" size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b>Branch 
        &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>
    
    <tr>
      <td height="31" align="right"  colspan="2"><b><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif">Select GL</font></b></td>
    <td colspan="2"  ><?


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



?>
          <select name='GL' onChange="javascript:submit();">
            <?
		if($status=='NEW')
		{
		echo "<option selected value=\0\">GL";
			while($row = mysqli_fetch_row($sBuyQuery))
			{
	
				if(isset($myGL) && preg_match("/" . preg_quote($myGL, '/') . "/", $row[0]))
				{
					echo "<option selected value=\"$row[0]\">$row[1]";
				}
				else{
					echo "<option value=\"$row[0]\">$row[1]";
				}
			}
		}
			if($mGL!='')
			{

				$glresult = mysqli_query($conn,"select description from acctmast where acctcode = '$mGL' ") or die(mysqli_error());
				$glrow = mysqli_fetch_row($glresult);
				$myGL= $mGL;
				echo "<option selected value=\"$mGL\">$glrow[0]";
			}		
?>
        </select></td>
		
    </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 trans_id,date,sum(dbamt),sum(cramt) from ledger where account_year = '$account_year' and compcode = '$compcode' and branch_code = '$branch' and amttype !='J' and acctcode = '$myGL' group by trans_id order by date desc ") or die(mysqli_error());



?>
          <select name='Trans' onChange="javascript:submit();">
            <?
			
			if($status=='NEW')
			{
				echo "<option selected value=\0\">Date";
				while($row = mysqli_fetch_row($sBuyQuery))
				{
					if($row[2]>0)
					{
						$amt = $row[2];
					}
					else
					{
						$amt = $row[3];
					}
					$dt = british($row[1]);
					if(isset($myTrans) && preg_match("/" . preg_quote($myTrans, '/') . "/", $row[0]))
					{
						echo "<option selected value=\"$row[0]\">$dt:&nbsp;Trans id:$row[0]&nbsp;&nbsp;Amount:$amt";
					}
					else{
						echo "<option value=\"$row[0]\">$dt:&nbsp;Trans id:$row[0]&nbsp;&nbsp;Amount:$amt";
					}
				}
			}	
			if($mdt!='')
			{
				$dt = british($mdt);
				$myDate=$mdt;
				echo "<option selected value=\"$mdt\">$dt:&nbsp;Trans id:$trans_id&nbsp;";
			}		
?>
        </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 `$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 trans_id = '$myTrans' and amttype !='J' and branch_code = '$branch' and compcode = '$compcode' and account_year = '$account_year' ";
	mysqli_query($conn,$query) or die(mysqli_error());

}	


?>


<tr >
</form>  


<form name='myform2' method='POST' action='update.php?compcode=<?=$compcode?>&branch=<?=$branch?>&status=<?=$status?>&trans_id=<?=$trans_id?>&account_year=<?=$account_year?>' 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="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;
// temperorily blocked due to 2010-11 data entry issue.
//twentycheck($filename);

$result = mysqli_query($conn,"select acctcode,date,narration,dbamt,cramt,type,trans_id,record,date from `$filename` order by trans_id,acctcode") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$acctcode = trim($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]);
	$ho_transfer = $row[9];
	$editable = $row[10];	
	$reconciled = $row[13];	
	$netdb = $netdb+$dbamt;
	$netcr = $netcr + $cramt;
	if($reconciled == 'Yes')
	{
		$editable = 'No';
	}

	$prodname = mysqli_fetch_array(mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode'")) or die(mysqli_error());
	if( $editable != 'No')
	{	
	
	?>
	<tr  bgcolor="#D7D7FF">
	<td width="49" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><a href = "peditview2.php?record=<?echo($record);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&mdt=<?=$row[1]?>&mGL=<?=$myGL?>&trans_id=<?=$trans_id?>"><?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="41" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$trans_id?></b></font></td>

	</tr>
	
	<?
	}
	else
	{
		?>
		<tr   bgcolor="#AA8F40">
		<td width="49" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?echo($record);?></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="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="2"></td>
</tr>

<input type="hidden" name="dbs" value="<?=$netdb?>">
<input type="hidden" name="crs" value="<?=$netcr?>">

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




<tr> 
    <td align="right" colspan="2" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Reason for Editing / Deletion</b></font></td>
	<td width="450"><input type="text" size="75" name="reason_text" value=""></td></tr>


  <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>
  
      <tr> 
    <td align="right" colspan="2" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Reconfirm if this day's all data are removed</b></font></td>
    <td> <input type="radio"    onKeyPress="return event.keyCode!=13"   name="reconfirm" 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="reconfirm" value = "Yes"> <font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Yes
      </b></font>&nbsp;</td>
  </tr> 
  
    
</table>
</form>
<font size=1> 
<br><br><font size=1><center><a href= "javascript:window.close();">Close Window</a></center></font>
</font> 
<?
function twentycheck($filename)
{

        global $conn;
		$cash_group = 111204;

		$myresult = mysqli_query($conn,"select distinct acctcode,cramt,trans_id from `$filename` where cramt >20000");
		while($myrow = mysqli_fetch_row($myresult))
		{		
			$amount = $myrow[1];
			$trans_id = $myrow[2];
			$groupresult = mysqli_query($conn,"select acctgroup from acctmast where acctcode = '$myrow[0]' ") or die(mysqli_error());
			$grouprow = mysqli_fetch_row($groupresult);
			if($grouprow[0] == $cash_group)
			{
				$exresult= mysqli_query($conn,"select distinct $filename.acctcode from `$filename`,acctmast_statutary where $filename.dbamt>0 and trans_id = '$trans_id' and $filename.acctcode= acctmast_statutary.acctcode ") or die(mysqli_error());
				$exrow = mysqli_fetch_row($exresult);
				if($exrow[0] >0)
				{
				}
				else
				{		
					?>
					<script language="javascript">
						alert("Cash  Transaction Credit amount exceeds Rs.20000/- which is not allowed. Please go back and correct the amount. If excemption required for any GL please add it in Statuatory GL. Data won't be saved");
					</script>

					<?
					

				}
			}
		}// end of while looop.	
			return;
}
?>

