<?php
session_start();
include '../db_connect.php';
include '../db_table_select.php';
$vchtype=$_GET['mvch'];
if($_GET[comcode]!=''){
if ($vchtype=='RECEIPT'){
//Getting Pending Bills
//and cmp = '$_SESSION[SESS_CMPC]' and fny = '$prefny'
$mquery="Select  'SALES' as vchtype,orderid as billno,orderid as srno,(grtotal-paid) as pedtotal, grtotal as totamount 
from  $billreg 
where buyercode = '$_GET[comcode]'  and (grtotal-paid)<>0 
UNION ALL 
Select 'SALESCF' as vchtype,orderid as billno, orderid as  srno,(grtotal-paid) as pedtotal, grtotal as totamount 
from  $prebillreg 
where buyercode = '$_GET[comcode]'  and (grtotal-paid)<>0 
UNION ALL 
select vchtype,Bill_no as billno, refno as srno,(totamount-paid) as pedtotal, totamount as totamount 
from vchreg 
where account Like '$_GET[comcode]' and vchtype like 'JOURNAL' and (totamount-paid)<>0 
UNION ALL 
select 'ADVRECPT' as vchtype,  billno, entryid as srno,  0-(amount-cramount)  as pedtotal, '0' as totamount  from vchdetails where vchno in (SELECT vchno FROM `vchreg` where comcode like '$_GET[comcode]' and vchtype='RECEIPT' ) and paytype in ('advance', 'On account') and amount-cramount > 0 ";
}
elseif ($vchtype=='JOURNAL'){
$mquery="Select 'DR' AS drcr, 'SALES' as vchtype, billto,  billno,srno,(total-paid) as pedtotal, total as totamount from  $billreg where billto = '$_GET[comcode]' and cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]' and (total-paid)<>0 UNION ALL select 'DR' AS drcr, 'ADVRECPT' as vchtype, '' as billto, billno, entryid as srno,  0-(amount-cramount)  as pedtotal, '0' as totamount from vchdetails where vchno in (SELECT vchno FROM `vchreg` where comcode like '$_GET[comcode]' and vchreg.cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]'  and vchtype='RECEIPT' ) and paytype in ('advance', 'On account') and amount-cramount > 0  UNION ALL select 'CR' AS drcr, vchtype, comcode as billto, Bill_no as billno, refno as srno,(totamount-paid) as pedtotal, totamount as totamount from purcreg where comcode = '$_GET[account]' and vchtype Like 'PURCHASE' and cmp = '$_SESSION[SESS_CMPC]'  and fny = '$_SESSION[SESS_FNY]' and (totamount-paid)<>0 UNION ALL select 'CR' AS drcr, vchtype,account as billto, Bill_no as billno, refno as srno,(totamount-paid) as pedtotal, totamount as totamount from vchreg where account Like '$_GET[comcode]' and vchtype in ('JOURNAL') and cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]' and (totamount-paid)<>0";
}
elseif ($vchtype=='PURCHASE'){

$tempArray = array('FUEL EXPENSES','MAINTENANCE EXPENSES');

if(isset($_GET['account'])&&!empty($_GET['account'])&&in_array($_GET['account'],$tempArray))
{
	if($_GET['account']=='FUEL EXPENSES')
	$mquery="Select  'PURCHASE' as vchtype,DATE_FORMAT(FDATE,'%d/%m/%Y') as billno, entryid as brn, vehno, (IF(AMOUNT = '', '0.0', AMOUNT)) as pedtotal,'0' as tamount from  fuelreg where ppname like '$_GET[comcode]' and cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]'";
	else if($_GET['account']=='MAINTENANCE EXPENSES')
	$mquery="Select  'PURCHASE' as vchtype,DATE_FORMAT(serdate,'%d/%m/%Y') as billno, entryid as brn, vehno, (IF(balance = '', '0.0', balance)) as pedtotal,amount as tamount from  vehdetails where agency like '$_GET[comcode]' and cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]';";

}	
else
{	
//cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]' and status!='VOID' and status!='CANCELLED'
$mquery="Select buyercode as billto, 'PURCHASE' as vchtype,orderid as billno,orderid as brn, '' as vehno, '' as pedtotal,grtotal from  $pinvoicereg where buyercode like '$_GET[comcode]' and orderid not in (select orderid from $pinvoicedetails)";
}
}
elseif ($vchtype=='PAYMENT'){
$mquery="Select 'PURCHASE' as vchtype, vchno,bill_no as billno,(totamount-paid) as pedtotal, totamount from  purcreg where comcode = '$_GET[comcode]' and cmp = '$_SESSION[SESS_CMPC]'  and vchtype in('PURCHASE','JOURNAL') and (totamount-paid)<>0 UNION ALL Select 'JOURNAL' as vchtype, vchno, bill_no as billno,(totamount-paid) as pedtotal, totamount from  vchreg where comcode = '$_GET[comcode]' and cmp = '$_SESSION[SESS_CMPC]' and vchtype like 'JOURNAL' and (totamount-paid)<>0  UNION ALL select 'ADVRECPT' as vchtype,  billno, entryid as srno,  0-(amount-cramount)  as pedtotal, '0' as totamount  from vchdetails where vchno in (SELECT vchno FROM `vchreg` where comcode like '$_GET[comcode]' and  vchreg.cmp = '$_SESSION[SESS_CMPC]' and fny = '$_SESSION[SESS_FNY]'  and vchtype='PAYMENT' ) and paytype in ('advance', 'On account') and amount-cramount > 0";

}

if($mquery){
if($sql=mysqli_query($con,$mquery))
{
	if ($vchtype=='PURCHASE'){
		echo "<table cellspacing='0' cellpadding='5' border='1' width='100%'><thead><tr><th align='center'>SELECT</th><th align='center'>BILL NO.</th><th align='center'>Srno</th><th align='center'></th><th align='center'>PEN. AMT.</th><th align='center'>BILL AMT.</th></tr></thead><tbody>";
		$penamt=0;
	$totamt=0;
	while($sql_row=mysqli_fetch_array($sql))
	{
		echo "<tr><td align='center'><input type='checkbox' class='chk_indv' /></td><td align='center'>$sql_row[billno]</td><td align='center' class='pen_srno pen_billno' >$sql_row[brn]</td><td align='center'>$sql_row[vehno]</td><td align='right' class='pen_total'>".number_format($sql_row[pedtotal],2, '.', ',')."</td><td align='right' >".number_format($sql_row[tamount],2, '.', '')."</td></tr>\n";
		$penamt+=$sql_row[pedtotal];
		$totamt+=$sql_row[tamount];
		}
		echo "<tr><td align='center'><input type='checkbox' class='chk_indv' id='checkall' /></td><td align='center'>$sql_row[billno]</td><td align='center' class='pen_srno pen_billno' >$sql_row[brn]</td><td align='center'>$sql_row[vehno]</td><td align='right' class='pen_total'>".number_format($penamt,2, '.', ',')."</td><td align='right' >".number_format($totamt,2, '.', '')."</td></tr>\n";
	echo "</tbody></table>";
	}
	
	
	else{
	echo "<table cellspacing='0' cellpadding='5' border='1' width='100%'><thead><tr>
	<th align='center' filter='false'>SELECT</th>";
    if($vchtype=='JOURNAL')
	echo "<th align='center'>COMPANY</th>";
	
	echo "<th align='center'>BILL NO.</th>
	<th align='center'>PEN. AMT.</th>
	<th align='center'>BILL AMT.</th>
	<th style='display:none' filter='false'></th>
	<th style='display:none' filter='false'></th>
	<th style='display:none' filter='false'></th>
	</tr></thead><tbody>";
	$penamt=0;
	$totamt=0;
	while($sql_row=mysqli_fetch_array($sql))
	{
		echo "<tr>
		<td align='center'><input type='checkbox' class='chk_indv' /></td>";
		if($vchtype=='JOURNAL')
		echo "<td align='center' class='pen_billto'>$sql_row[billto]</td>";
		echo "<td align='center' class='pen_billno'>$sql_row[billno]</td>
		<td align='center' class='pen_total'>".number_format($sql_row[pedtotal],2, '.', '')."</td>
		<td align='right' >".number_format($sql_row[totamount],2, '.', '')."</td>
		<td class='pen_srno' style='display:none'>$sql_row[srno]</td>
		<td align='center' class='vtype' style='display:none' >$sql_row[vchtype]</td>
		<td align='center' class='pen_drcr' style='display:none' >$sql_row[drcr]</td>
		</tr>";
		$penamt+=$sql_row[pedtotal];
		$totamt+=$sql_row[totamount];
		}
	
		echo "<tr>
		<td align='center'><input type='checkbox' class='chk_indv' id='checkall' /></td>";
		
		if($vchtype=='JOURNAL')
		echo "<td class='pen_vtyp' ></td>";
		echo "<td align='center' class='pen_billno'>TOTAL</td>
		<td align='center' class='pen_total'>".number_format($penamt,2, '.', '')."</td>
		<td align='right' >".number_format($totamt,2, '.', '')."</td>
		<td class='pen_srno' style='display:none'></td>
		<td  style='display:none'></td>
		<td align='center' class='pen_drcr'  ></td>
		</tr>";
	echo "</tbody></table>";
	}
}
else
echo mysqli_error($con);
}
}
?>