<?php
//Start session
	session_start();
include '../db_connect.php';
include '../db_table_select.php';

function date_formatc($temp_date)
{
list($year, $month, $day) = explode('-', $temp_date);
return "$day/$month/$year";
}
$vch_type=$_POST['vouchertype'];
$sum_amount=0;
$today_date=date("d/m/Y");
$from_date = $today_date;
$to_date = $today_date;

$cmp_key=$_POST['cmp_key'];

if(!empty($_POST['filltertext'])){

if($_POST['filltertype']=='COMPANY'){
	
	if($vch_type=='SALES'){
		$fillpart="AND billedto like '". $_POST['filltertext']."%'";}
        else{
    	$fillpart="AND comcode like '". $_POST['filltertext']."%'";}
	}
	else if($_POST['filltertype']=='BILLNO'){
	
		$fillpart="AND bill_no like '". $_POST['filltertext']."%'";
	}
	else if($_POST['filltertype']=='CITY'&&$vch_type=='SALES'){
		$fillpart="AND usedcity like '". $_POST['filltertext']."%'";
	}
	else if($_POST['filltertype']=='ACCOUNT'){
		$fillpart="AND account like '". $_POST['filltertext']."%'";
	}
}
	else{
		$fillpart="";
		
		}

if($_POST['from_date']!='')
$from_date =$_POST['from_date'];

if($_POST['to_date']!='')
$to_date =$_POST['to_date'];



if($vch_type=='all')
{
	if($cmp_key=="ALL")
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst,tdsamt,sortpayment from vchreg INNER JOIN commst
	ON vchreg.comcode=commst.compid
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst,tdsamt,sortpayment from vchreg INNER JOIN commst
	ON vchreg.comcode=commst.compid
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and cmp='$cmp_key' and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc"; 
}
else if($vch_type=='RECEIPT')
{
		

	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

    elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		

	
	/*query changed to view SUNDRY DEBTORS and SUNDRY CREDITORS */
	if($cmp_key=="ALL")
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,vchreg.expst as expst,account,tdsamt,bill_no,agnstref,sortpayment,dramount-tdsamt as netamount from vchreg INNER JOIN ledgerreg
	ON vchreg.comcode=ledgerreg.alias
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,vchreg.expst as expst,account,tdsamt,bill_no,sortpayment,agnstref,dramount-tdsamt as netamount from vchreg INNER JOIN ledgerreg
	ON vchreg.comcode=ledgerreg.alias
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and  STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type'  and vchreg.cmp='$cmp_key' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";


	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead> <tr><th width='8%'><textarea>VCH NO</textarea></th> <th><textarea>VCH DATE</textarea></th><th><textarea>COMPANY</textarea></th> <th><textarea>REF NO</textarea></th><th><textarea>INST/INVNO</textarea></th><th><textarea>AGNST REF</textarea></th><th><textarea>BANK</textarea></th> <th><textarea>AMOUNT</textarea></th><th><textarea name='b_package[]'>TDS</textarea></th><th><textarea name='b_sortpayment[]'>SHORT PAYMENT</textarea></th><th><textarea name='b_netamount[]'>NET AMOUNT</textarea></th><th align='center' filter-type='ddl' ><textarea>STATUS</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result))
	{
	
	echo "<tr><td width='8%'><a href='vch_entry.php?mode=$row[vchtype]&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name=' v_srno'>$row[vchno]</textarea></a></td><td><textarea name='v_vchdate'>".date_formatc($row[vchdate])."</textarea></td><td><textarea name='v_billedto'>$row[comcode]</textarea></td><td><textarea name='v_refno'>$row[refno]</textarea></td><td><textarea name='v_bill_no'>$row[bill_no]</textarea></td><td><textarea name='v_bill_no'>$row[agnstref]</textarea></td><td><textarea name='v_account'>$row[account]</textarea></td><td><textarea name='v_debit'>$row[dramount]</textarea></td><td><textarea name='v_vchtds'>$row[tdsamt]</textarea></td><td><textarea name='v_vchsortpayment'>$row[sortpayment]</textarea></td><td><textarea name='v_netamount'>$row[netamount]</textarea></td><td><textarea name='v_status' >$row[expst]</textarea></td></tr>";
	
	$sum_amount = $sum_amount + $row['netamount'];
	$i++;
	}

echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);
}



else if($vch_type=='SALES')
{		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%' OR expst like '0 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		

	
	if($cmp_key=="ALL")
	$sql = "select orderid as srno, orderdate as vchdate, DATE_FORMAT(orderdate,'%d/%m/%Y') as billdate, buyercode as billto, orderid as billno,  ptotal as total, pnamount as basic, $billreg.vat  as sertax, expst, paid from $billreg  INNER JOIN commst
	ON $billreg.buyercode=commst.compid
	where orderdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')  $qrypart $fillpart  order by orderdate, CONVERT(orderid, SIGNED INTEGER)";
	 else
	$sql = "select orderid as srno, orderdate as vchdate, DATE_FORMAT(orderdate,'%d/%m/%Y') as billdate, buyercode as billto, orderid as billno,  ptotal as total, pnamount as basic, $billreg.vat  as sertax, expst, paid from $billreg  INNER JOIN commst
	ON $billreg.buyercode=commst.compid
	where orderdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')   $qrypart $fillpart  order by CONVERT(orderid, SIGNED INTEGER)"; 

	$result = mysqli_query($con,$sql);
	
	if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead> <tr><th><textarea name='v_srno'>VCH NO</textarea></th> <th><textarea name='v_billdate'>BILL DATE</textarea></th><th><textarea name='v_billedto'>BILLED TO</textarea></th> <th><textarea name='v_billno'>BILL NO</textarea></th><th><textarea name='v_vchtype'>VCHTYPE</textarea></th> <th width='7%'><textarea name='v_basic'>Basic Amt.</textarea></th><th width='7%'><textarea name='v_ser'>Ser. Tax</textarea></th><th><textarea name='v_debit' class='calc_sum'>DEBIT</textarea></th> <th><textarea name='v_credit'>CREDIT</textarea></th><th align='center' filter-type='ddl'><textarea name='v_status' width='8%'>STATUS</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result))
	{
	echo "<tr><td><textarea name='v_srno' class = 'click_bill_pdf link'>$row[srno]</textarea></td><td><textarea name='v_billdate'>$row[billdate]</textarea></td><td><textarea name='v_billedto'>$row[billto]</textarea></td><td><textarea name='v_billno'>$row[billno]</textarea></td><td><textarea name='v_vchtype'>SALES</textarea></td><td width='7%'><textarea name='v_basic'>$row[basic]</textarea></td><td width='7%'><textarea name='v_ser'>$row[sertax]</textarea></td><td><textarea name='v_debit' class='calc_sum'>$row[total]</textarea></td><td><textarea name='v_credit'>$row[paid]</textarea></td><td><textarea name='v_status'>$row[expst]</textarea></td></tr>";
	$i++;
	$sum_amount = $sum_amount + $row[total];
	}

echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}



else if($vch_type=='PURCHASE')
{
	//	
//		if(empty($_POST[billedto]))
//	$qrypart.='';
//	else
//		$qrypart.="and comcode='$_POST[billedto]'";
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		

		if($cmp_key=="ALL")
	$sql = "select vchdate, comcode, vchtype, vchno, refno, dramount, tdsamt, toll_park, basic_amt,(ser_tax+educess+seducess) as sertax, misamt, totamount, bill_no, expst from purcreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate, comcode, vchtype, vchno, refno, dramount, tdsamt, toll_park, basic_amt,(ser_tax+educess+seducess) as sertax, misamt, totamount, bill_no, expst from purcreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')  and vchtype ='$vch_type' and cmp='$cmp_key' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc"; 
	//echo $sql;
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead>
<tr><th width='7%'><textarea name='v_srno'>VCH NO</textarea></th>
<th width='7%'><textarea name='v_vchdate'>VCH DATE</textarea></th>
<th width='9%'><textarea name='v_billedto'>COMPANY</textarea></th>
<th width='7%'><textarea name='v_refno'>REF NO</textarea></th>
<th width='7%'><textarea name='v_billno'>INST/INVNO</textarea></th>
<th width='7%'><textarea name='v_repadd'>VCHTYPE</textarea></th>
<th width='7%'><textarea name='v_basic'>Basic Amt.</textarea></th>
<th width='7%'><textarea name='v_ser'>Ser. Tax</textarea></th>
<th width='7%'><textarea name='v_misc'>Misc. Amt.</textarea></th>
<th width='7%'><textarea name='v_debit'>DEBIT</textarea></th>
<th width='7%'><textarea name='v_tdsamt'>TDS</textarea></th>
<th width='7%'><textarea name='v_totalamt' class='calc_sum'>Total Amt.</textarea></th>
<th align='center' filter-type='ddl' width='7%'><textarea name='v_status' >STATUS</textarea></th> 
</tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result))
	{
	echo "<tr><td width='7%'><a href='vch_entry.php?mode=$row[vchtype]&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name='v_srno'>$row[vchno]</textarea></a></td>
<td width='7%'><textarea name='v_vchdate'>".date_formatc($row[vchdate])."</textarea></td>
<td width='9%'><textarea name='v_billedto'>$row[comcode]</textarea></td>
<td width='7%'><textarea name='v_refno'>$row[refno]</textarea></td>
<td width='7%'><textarea name='v_billno'>$row[bill_no]</textarea></td>
<td width='7%'><textarea name='v_repadd'>$row[vchtype]</textarea></td>
<td width='7%'><textarea name='v_basic'>$row[basic_amt]</textarea></td>
<td width='7%'><textarea name='v_ser'>".round($row[sertax])."</textarea></td>

<td width='7%'><textarea name='v_misc'>$row[misamt]</textarea></td>
<td width='7%'><textarea name='v_debit'>$row[dramount]</textarea></td>
<td width='7%'><textarea name='v_tdsamt'>$row[tdsamt]</textarea></td>
<td width='7%'><textarea name='v_totalamt' class='calc_sum'>$row[totamount]</textarea></td>
<td width='7%'><textarea name='v_status'>$row[expst]</textarea></td></tr>";
	$sum_amount = $sum_amount + $row[totamount];
	$i++;
	}


echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}
else if($vch_type=='CREDITNOTE')
{
	
		//if(empty($_POST[billedto]))
//	$qrypart.='';
//	else
//		$qrypart.="and comcode='$_POST[billedto]'";
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		
		if($cmp_key=="ALL")
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,bill_no,expst from vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,bill_no,expst from vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')  and vchtype ='$vch_type' and cmp='$cmp_key' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc"; 
	//echo $sql;
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead><tr><th width='8%'><textarea name=' b_id[]'>VCH NO</textarea></th><th width='12%'><textarea name='b_city[]'>VCH DATE</textarea></th><th width='12%'><textarea name='b_billedto[]'>COMPANY</textarea></th><th width='12%'><textarea name='b_usedby[]'>REF NO</textarea></th><th><textarea>INST/INVNO</textarea></th><th width='12%'><textarea name='b_repadd[]'>VCHTYPE</textarea></th> <th width='12%'><textarea name='b_dutydate[]'>DEBIT</textarea></th><th width='12%'><textarea name='b_package[]'>TDS</textarea></th><th align='center' filter-type='ddl' width='8%'><textarea name='b_status[]' >STATUS</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result)){
	echo "<tr><td width='8%'><a href='vch_entry.php?mode=$row[vchtype]&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name='v_srno'>$row[vchno]</textarea></a></td><td width='12%'><textarea name='v_vchdate'>".date_formatc($row[vchdate])."</textarea></td><td width='12%'><textarea name='v_billedto'>$row[comcode]</textarea></td><td width='12%'><textarea name='v_refno'>$row[refno]</textarea></td><td><textarea name='v_bill_no'>$row[bill_no]</textarea></td><td width='12%'><textarea name='v_vchtype'>$row[vchtype]</textarea></td><td width='12%'><textarea name='v_debit'>$row[dramount]</textarea></td><td width='12%'><textarea name='v_credit'>$row[cramount]</textarea></td><td width='8%'><textarea name='v_status'>$row[expst]</textarea></td></tr>";
	$sum_amount = $sum_amount + $row['dramount'];
	$i++;
	}
echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}
else if($vch_type=='JOURNAL')
{
	//if(empty($_POST[billedto]))
//	$qrypart.='';
//	else
//		$qrypart.="and comcode='$_POST[billedto]'";
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		
		if($cmp_key=="ALL")
	$sql = "select vchdate, comcode, vchtype, vchno, refno, dramount, tdsamt, toll_park, basic_amt,(ser_tax+educess+seducess) as sertax, misamt, totamount, bill_no, expst vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type' and fny = '$_SESSION[SESS_FNY]' $fillpart $qrypart order by vchdate desc";
	 else
	$sql = "select vchdate, comcode, vchtype, vchno, refno, dramount, tdsamt, toll_park, basic_amt,(ser_tax+educess+seducess) as sertax, misamt, totamount, bill_no, expst from vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')  and vchtype ='$vch_type' and cmp='$cmp_key' and fny = '$_SESSION[SESS_FNY]' $fillpart $qrypart order by vchdate desc"; 
	//echo $sql;
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead>
<tr><th width='7%'><textarea name='v_srno'>VCH NO</textarea></th>
<th width='7%'><textarea name='v_vchdate'>VCH DATE</textarea></th>
<th width='9%'><textarea name='v_billedto'>COMPANY</textarea></th>
<th width='7%'><textarea name='v_refno'>REF NO</textarea></th>
<th width='7%'><textarea name='v_billno'>INST/INVNO</textarea></th>
<th width='7%'><textarea name='v_repadd'>VCHTYPE</textarea></th>
<th width='7%'><textarea name='v_basic'>Basic Amt.</textarea></th>
<th width='7%'><textarea name='v_ser'>Ser. Tax</textarea></th>
<th width='7%'><textarea name='v_toll'>Misc. Type</textarea></th>
<th width='7%'><textarea name='v_misc'>Misc. Amt.</textarea></th>
<th width='7%'><textarea name='v_debit'>DEBIT</textarea></th>
<th width='7%'><textarea name='v_tdsamt'>TDS</textarea></th>
<th width='7%'><textarea name='v_totalamt' class='calc_sum'>Total Amt.</textarea></th>
<th align='center' filter-type='ddl' width='7%'><textarea name='v_status' >STATUS</textarea></th> 
</tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result)){
	echo "<tr><td width='7%'><a href='vch_entry.php?mode=$row[vchtype]&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name='v_srno'>$row[vchno]</textarea></a></td>
<td width='7%'><textarea name='v_vchdate'>".date_formatc($row[vchdate])."</textarea></td>
<td width='9%'><textarea name='v_billedto'>$row[comcode]</textarea></td>
<td width='7%'><textarea name='v_refno'>$row[refno]</textarea></td>
<td width='7%'><textarea name='v_billno'>$row[bill_no]</textarea></td>
<td width='7%'><textarea name='v_repadd'>$row[vchtype]</textarea></td>
<td width='7%'><textarea name='v_basic'>$row[basic_amt]</textarea></td>
<td width='7%'><textarea name='v_ser'>".round($row[sertax])."</textarea></td>
<td width='7%'><textarea name='v_toll'>$row[toll_park]</textarea></td>
<td width='7%'><textarea name='v_misc'>$row[misamt]</textarea></td>
<td width='7%'><textarea name='v_debit'>$row[dramount]</textarea></td>
<td width='7%'><textarea name='v_tdsamt'>$row[tdsamt]</textarea></td>
<td width='7%'><textarea name='v_totalamt' class='calc_sum'>$row[totamount]</textarea></td>
<td width='7%'><textarea name='v_status'>$row[expst]</textarea></td></tr>";
	$sum_amount = $sum_amount + $row['dramount'];
	$i++;
	}
echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}

if($vch_type=='CONTRA')
{
	//if(empty($_POST[billedto]))
//	$qrypart.='';
//	else
//		$qrypart.="and comcode='$_POST[billedto]'";
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		
		if($cmp_key=="ALL")
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst from vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='$vch_type' and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst from vchreg 
	where vchdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')  and vchtype ='$vch_type' and cmp='$cmp_key' and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc"; 
	//echo $sql;
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead><tr><th width='8%'><textarea name=' b_id[]'>VCH NO</textarea></th><th width='12%'><textarea name='b_city[]'>VCH DATE</textarea></th><th width='12%'><textarea name='b_billedto[]'>COMPANY</textarea></th><th width='12%'><textarea name='b_usedby[]'>REF NO</textarea></th><th width='12%'><textarea name='b_repadd[]'>VCHTYPE</textarea></th> <th width='12%'><textarea name='b_dutydate[]'>DEBIT</textarea></th><th width='12%'><textarea name='b_package[]'>TDS</textarea></th><th align='center' filter-type='ddl' width='8%'><textarea name='b_status[]' >STATUS</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result)){
	echo "<tr><td width='8%'><a href='vch_entry.php?mode=$row[vchtype]&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name=' b_id[]'>$row[vchno]</textarea></a></td><td width='12%'><textarea name='b_city[]'>".date_formatc($row[vchdate])."</textarea></td><td width='12%'><textarea name='b_billedto[]'>$row[comcode]</textarea></td><td width='12%'><textarea name='b_usedby[]'>$row[refno]</textarea></td><td width='12%'><textarea name='b_repadd[]'>$row[vchtype]</textarea></td><td width='12%'><textarea name='b_dutydate[]'>$row[dramount]</textarea></td><td width='12%'><textarea name='b_package[]'>$row[cramount]</textarea></td><td width='8%'><textarea name='b_status[]'>$row[expst]</textarea></td></tr>";
	$sum_amount = $sum_amount + $row['dramount'];
	$i++;
	}
echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}
else if($vch_type=='PAYMENT')
{
	
	
			//if(empty($_POST[billedto]))
//	$qrypart.='';
//	else
//		$qrypart.="and comcode='$_POST[billedto]'";
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="and (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="and expst like '%$_POST[expst]%'";
		
		
		if($cmp_key=="ALL")
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst,narration,tdsamt,sortpayment,bill_no,account from vchreg 
	where vchdate>=STR_TO_DATE('$from_date','%d/%m/%Y') and vchdate<=STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='PAYMENT' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc";
	 else
	$sql = "select vchdate,comcode,vchtype,vchno,refno,dramount,cramount,expst,narration,tdsamt,sortpayment,bill_no,account from vchreg 
	where vchdate>=STR_TO_DATE('$from_date','%d/%m/%Y') and vchdate<=STR_TO_DATE('$to_date','%d/%m/%Y') and vchtype ='PAYMENT' and cmp='$cmp_key' $qrypart and fny = '$_SESSION[SESS_FNY]' $fillpart order by vchdate desc"; 
	//echo $sql;
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead><tr><th width='8%'><textarea name=' b_id[]'>VCH NO</textarea></th><th><textarea name='b_city[]'>VCH DATE</textarea></th><th><textarea name='b_billedto[]'>COMPANY</textarea></th><th><textarea name='b_narration[]'>NARRATION</textarea></th><th><textarea name='b_usedby[]'>REF NO</textarea></th><th><textarea name='b_inst[]'>INST/INVNO</textarea></th><th><textarea name='b_repadd[]'>VCHTYPE</textarea></th><th><textarea name='b_bank[]'>BANK</textarea></th> <th><textarea name='b_dutydate[]'>DEBIT</textarea></th><th><textarea name='b_package[]'>TDS</textarea></th><th><textarea name='b_sortpayment[]'>SORT PAYMENT</textarea></th><th align='center' filter-type='ddl' ><textarea name='b_status[]' width='8%'>STATUS</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result)){
	echo "<tr><td width='8%'><a href='vch_entry.php?mode=PAYMENT&vchno=$row[vchno]' onClick='positionedPopup(this.href,620);return false' ><textarea class='link' name='v_srno'>$row[vchno]</textarea></a></td><td><textarea name='v_vchdate'>".date_formatc($row[vchdate])."</textarea></td><td><textarea name='v_billedto'>$row[comcode]</textarea></td><td><textarea name='v_narration'>$row[narration]</textarea></td><td><textarea name='v_refno'>$row[refno]</textarea></td><td><textarea name='v_inst'>$row[bill_no]</textarea></td><td><textarea name='v_vchtype'>$row[vchtype]</textarea></td><td><textarea name='v_account'>$row[account]</textarea></td><td><textarea name='v_debit'>$row[dramount]</textarea></td><td><textarea name='v_tds'>$row[tdsamt]</textarea></td><td><textarea name='v_credit'>$row[sortpayment]</textarea></td><td><textarea name='v_status' width='8%'>$row[expst]</textarea></td></tr>";
	$sum_amount = $sum_amount + $row['dramount'];
	$i++;
	}
echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}


else if($vch_type=='LEDGER')
{
		
	if(empty($_POST['expst']) || $_POST['expst'] == 'ALL')
	        $qrypart.='';
	
	elseif ($_POST['expst'] == 'PENDING')
		$qrypart.="where (expst like '%$_POST[expst]%' OR expst like '')";

        elseif ($_POST['expst'] == 'ERROR')
		$qrypart.="where (expst like '%$_POST[expst]%' OR expst like '1 IN%')";
	else
		$qrypart.="where expst like '%$_POST[expst]%'";
		
		
		if($cmp_key=="ALL")
	$sql = "select * from ledgerreg $qrypart";
	 else
	$sql = "select * from ledgerreg $qrypart"; 
	
	$result = mysqli_query($con,$sql);
	
		if($result)
	{
	echo "<table  id='demotable1' width='100%' cellspacing='0' ><thead><tr><th ><textarea name='v_id[]'>SR. NO</textarea></th><th ><textarea name='v_ledger[]'>LEDGER</textarea></th><th><textarea name='v_alias[]'>ALIAS</textarea></th><th><textarea name='b_ledtype[]'>LEDTYPE</textarea></th><th><textarea name='v_openbal[]'>Opening Bal</textarea></th><th><textarea name='v_status[]'>Status</textarea></th> </tr></thead> <tbody>";
	$i=0;
	while($row = mysqli_fetch_array($result)){
	echo "<tr><td><textarea  name='v_entryid'>$row[id]</textarea></td><td ><textarea  name='v_ledger'>$row[ledger]</textarea></td><td><textarea name='v_alias'>$row[alias]</textarea></td><td><textarea name='v_ledtype'>$row[ledtype]</textarea></td><td><textarea name='v_openbal'>$row[openingbal]</textarea></td><td><textarea name='v_status'>$row[expst]</textarea></td></tr>";

	}
echo "</tbody></table>###$sum_amount###$i";
	}
	else
	echo mysqli_error($con);

}


mysqli_close($con);
?>

