<?
session_start();
//$brcode=$_SESSION['brcode'];

$user=$_SESSION['user'];
/*
if(strlen($brcode)<=0)
{
	print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
	return;
}*/
include("../opendb.php");
include('mysqli_table.php');
$custid=$_REQUEST['custid'];
$query1="select * from accledgersms where code='$custid'";
$res=mysqli_query($db,$query1);
$row=mysqli_fetch_array($res);
$cd=$row[acname];		
$id=$row[custid];
$brcode=$row[brcode];
$df='2021-04-01';
$dt=date('Y-m-d');
$dfrom = implode('-', array_reverse(explode('-', $df)));
$dto = implode('-', array_reverse(explode('-', $dt)));
$cd=str_replace('~','&',$cd);
if(strlen($cd)<=0)
{
    print "Invalid Link....!";
    exit;
}   
$query2="select corp,acname,id from customer where id='$id' and brcode='$brcode'";
//print $query2;
$res111=mysqli_query($db,$query2);
$row11=mysqli_fetch_array($res111);
$cd=$row11[acname];		
if(strlen($row11[2])<=0)
{
    print "Invalid ID ....!";
    exit;
}   	
$cusres=mysqli_query($db,"select * from customer where cname='$cd'");
$cusrow=mysqli_fetch_array($cusres);
$tr=0;$tp=0;
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$df' and (partic='$cd')";

$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error($db));
$r2=mysqli_fetch_row($res2);

$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brcode'");

$opb=mysqli_fetch_array($re);
$pdf = new FPDF('L','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetFont('','B');
$pdf->SetFont('','BU');
$pdf->Cell(250,5,'PSN AUTOMOBILES PRIVATE LTD',0,1,'C');
$pdf->Ln(4);
$pdf->SetFont('Arial','',10);
$pdf->Cell(250,5,'Transaction Details of '.$cd,0,1,'C');
$pdf->Ln(2);
$strv='Transactions from '.$dfrom.' to '.$dto;
$pdf->Cell(250,5,$strv,0,1,'C');


$pdf->Cell(20,10,'Date.','LTR');$pdf->Cell(191,10,'Vouch/Rec. No.','LTR');$pdf->Cell(22,10,'Debit','LTR');$pdf->Cell(22,10,'Credit','LTR');
                     $pdf->Cell(30,10,'Balance','LTR');
    $pdf->Ln(2);

$pdf->SetFont('Arial','',8);
if($opb[drcr]=='Cr' and $r2[0]<0)
{
	$openbal=$opb[op]+abs($r2[0]);
	//print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
	$pdf->Ln(4);
$pdf->Ln(4);
	$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
	
	
	$tp+=$openbal;
}
else if($opb[drcr]=='Cr' and $r2[0]>=0)
{
	$openbal=$opb[op]-$r2[0];
	if($openbal>0)
	{
		//print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
		$pdf->Ln(4);
$pdf->Ln(4);
		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');$pdf->Cell(22,6,'','LTRB',0, 'R');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
		
		$tp+=$openbal;
	}
	else
	{
		//print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
		$pdf->Ln(4);
$pdf->Ln(4);
		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
		
	$tr+=$openbal;
	}
}

$pdf->Ln(6);
//$pdf->Ln(4);
if($opb[drcr]=='Dr' and $r2[0]>=0)
{
	if($opb[op]>=0)
		$openbal= $opb[op] +abs($r2[0]);
	else
		$openbal= $opb[op]+$r2[0];
	if($openbal>0)
	{
		//print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
		$tr+=$openbal;
	}
	else
	{
		//print "<tr><td></td><td> Opening Balance</td><td>&nbsp;</td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
		
		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
		
		$tp+=abs($openbal);
	}
}

if($opb[drcr]=='Dr' and $r2[0]<0)
{
	if($opb[op]>=0)
		$openbal= $opb[op]+ $r2[0];
	else
		$openbal= $opb[op]+$r2[0];
	//print $opb[op].'-'.$r2[0];
	if($openbal>=0)
    	{
    		//print "<tr><td></td><td> Opening Balance</td><td align=right>".putComa($openbal)."</td><td colspan=2></td>";
    		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
     		$tr+=$openbal;
     	}
 	else
    	{
    		//print "<tr><td></td><td> Opening Balance</td><td></td><td align=right>".putComa(abs($openbal))."</td><td colspan=2></td>";
    		$pdf->Cell(20,6,'','LTRB',0, 'R'); $pdf->Cell(191,6,'Opening Balance','LTRB',0, 'L');
    $pdf->Cell(22,6,number_format($openbal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,'','LTRB',0, 'R');
     		$tp+=$openbal;
     	}

}

$mop_bal = abs($tr)- abs($tp);
$sql="select * from journal2 where brcode='$brcode' and partic='$cd' and date>='$df' and date<='$dt'order by date,jvno ";
$result=mysqli_query($db,$sql) or die (mysqli_error($db));
//$mop_bal = $openbal;
while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
	$mop_bal = $mop_bal -  $row[cramt];
	if($mop_bal<0)
		$mstr = "(".number_format(abs($mop_bal), 2, '.', ',').")";
	else
		$mstr = number_format($mop_bal, 2, '.', ',');
		$tostr='To '.$row[narr].'-('.$row[j_type];
		$date1=implode('-', array_reverse(explode('-', $row[date])));
     $pdf->Cell(20,6,$date1,'LTRB',0, 'R');$pdf->Cell(191,6,$tostr,'LTRB',0, 'L');
     $pdf->Cell(22,6,'','LTRB',0, 'R');
     $strvl=$row[against].':'.$row[no];
     $pdf->Cell(22,6,$row[cramt],'LTRB',0, 'R');$pdf->Cell(30,6,$mstr,'LTRB',0, 'R');
		
		$pdf->Ln(6);
 	//print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> &nbsp;&nbsp; <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[no]'> To $row[narr]-($row[j_type])</a></td><td align=right></td><td align=right>".putComa($row[cramt])."</td><td align=right>".$mstr."</td> <td>$row[against]:$row[no]</td><td>$row[jvno]</td>";
	//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
	//print "</tr>";
	$totcramt+=$row[cramt];
  }
  else //if($row[dr]==$cd)
  {

	$mop_bal = $mop_bal +  $row[dramt];
	if($mop_bal<0)
		$mstr = "(".number_format(abs($mop_bal), 2, '.', ',').")";
	else
		$mstr = number_format($mop_bal, 2, '.', ',');
		$tostr='To '.$row[narr].'-('.$row[j_type];
		$date1=implode('-', array_reverse(explode('-', $row[date])));
     $pdf->Cell(20,6,$date1,'LTRB',0, 'R');$pdf->Cell(191,6,$tostr,'LTRB',0, 'L');
     $pdf->Cell(22,6,$row[dramt],'LTRB',0, 'R');$pdf->Cell(22,6,'','LTRB',0, 'R');$pdf->Cell(30,6,$mstr,'LTRB',0, 'R');
		$pdf->Ln(6);
		
 	//print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>".datetohtml($row[date])."</td><td> <a href='multientry/viewtrans.php?code=$row[jvno]' title='Ref:$row[against] $row[no]'>By $row[narr]-($row[j_type])</a></td><td align=right>".putComa($row[dramt])."</td><td align=right></td><td align=right>".$mstr."</td> <td>$row[against]:$row[no]</td><td>$row[jvno]</td>";
	//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
	//print "</tr>";
	$totdramt+=$row[dramt];
  }
}

$pdf->Cell(20,6,'','LTRB',0, 'R');$pdf->Cell(191,6,'Total','LTRB',0, 'L');


//print "<tr style={background-color:silver}><td colspan=2 align=center><font color=black><B>Total</td>"; 

 $totdramt+=abs($tr);

 $totcramt+=abs($tp);

//print "<td align=right><font color=black>".putComa($totdramt)."</td>";
//print "<td align=right><font color=black>".putComa(abs($totcramt))."</td><td colspan=2></td></tr>";
$pdf->Cell(22,6,number_format($totdramt,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,number_format($totcramt,2,'.',','),'LTRB',0, 'R');
$pdf->Cell(30,6,'','LTRB',0, 'R');

/*if($totdramt>=0)
$bal=($totdramt-$totcramt);
else
{$bal=($totdramt+$totcramt); $bal*=-1;}
*/
$pdf->Ln(6);
if(abs($totdramt)>abs($totcramt))
{
$bal=(abs($totdramt)-abs($totcramt));
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".round(abs($bal),2)."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
$pdf->Cell(20,6,'','LTRB',0, 'R');$pdf->Cell(191,6,'Balance','LTRB',0, 'L');
$pdf->Cell(22,6,number_format($bal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(22,6,'Debit','LTRB',0, 'R');
$pdf->Cell(30,6,'','LTRB',0, 'R');


}
else if(abs($totdramt)<abs($totcramt))
{$bal=(abs($totcramt)-abs($totdramt)); 
$pdf->Cell(20,6,'','LTRB',0, 'R');$pdf->Cell(191,6,'Balance','LTRB',0, 'L');$pdf->Cell(22,6,'','LTRB',0, 'R');
$pdf->Cell(22,6,number_format($bal,2,'.',','),'LTRB',0, 'R');$pdf->Cell(30,6,'Credit','LTRB',0, 'R');
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";
}


//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  $bal=number_format($bal,2,'','');
//********************************************************
if($bal>0)
{
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black><b> Debit </td><td colspan=2></td></tr>";
}
else
//print "\n<tr><td colspan=2 align=center><b><font color=black>Balance</td><td></td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td><font color=black> <b>Credit </td> </tr>";


//**************************************************************8888888888888888888888888






//echo "test";

$filename=$cusrow[id].".pdf";

//$path = $_SERVER["DOCUMENT_ROOT"].'/psneicher/paypdf/custfolder/'.$filename;
 
		//$pdf->Output('I');	
		$pdf->Output($filename,'I');
?>
