<?
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("../opendb1.php");
include('mysqli_table.php');
include_once('testtot.php');
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];
$type=$_REQUEST['type'];
$dfrm=$fromdt;
$dto=$todt;
//print $dfrm;
$df=implode('-', array_reverse(explode('-', $fromdt)));
$dt = implode('-', array_reverse(explode('-', $todt)));;
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);

  $dfrm=implode('-', array_reverse(explode('-', $df)));
  $dto=implode('-', array_reverse(explode('-', $dt)));


 	$pdf = new FPDF('P','mm','A4');
	$pdf->AddPage();
	$pdf->SetFont('Arial','',15);
	//$pdf->SetFont('','B');
	$dedres1=mysqli_query($db,"select brname from branch where brcode='$brcode'") or die("er5:".mysqli_error($db));
	$br_1=mysqli_fetch_array($dedres1);
	$mon = date("M Y", strtotime("$dto"));
	//$pdf->SetFont('','BU');
	$pdf->Cell(200,8,$brrow[brname],0,1,'C');
	$pdf->SetFont('Arial','',11);
	$brnchnm=strtoupper($brnchnm);						
        $pdf->Cell(200,8,'RECEIPT AND DISBURSEMENT STATEMENT From '.$dfrm." To ".$dto,0,1,'C');
        $qr=mysqli_query($db,"select * from branch where brname='$rw[2]'");
	    $qw=mysqli_fetch_array($qr);                                                
        $pdf->SetFont('Arial','',12);
		$pdf->Cell(80,7,'Particulars','LTR');
		$pdf->Cell(50,7,'Reciept','LTR');
		$pdf->Cell(50,7,'Payment','LTR');
	    $pdf->Ln(7);

$bal=mysqli_query($db,"select op from ledger where led='Cash' and brcode='$brcode'");
$sql="select op from ledger where led='Cash' and brcode='$brcode'";
$opbalance=mysqli_fetch_row($bal);
$res=mysqli_query($db,"select acfrom from acperiod");
$r1=mysqli_fetch_row($res);

$quer1="select sum(dramt),sum(cramt) from journal2 where date < '$df' and brcode='$brcode' and left(j_type,5)!='Sales' and left(j_type,7)!='Counter' and partic='Cash'";
$res21=mysqli_query($db,$quer1)or die('jour err:'.mysqli_error($db));
$r21=mysqli_fetch_row($res21);

$quer="select sum(total),sum(redct),sum(mba),sum(strbtnml),sum(strbtspl),sum(taxamt),sum(staffdisc),sum(nettot) from prod_invoice where invdate<'$df' and brcode='$brcode' ";

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

$quer_2="select sum(nettot) from prod_invoice where invdate<'$df' and brcode='$brcode' and pmode = 'CREDIT'";
$res_2=mysqli_query($db,$quer_2)or die('jour err:'.mysqli_error($db));
$r_2=mysqli_fetch_row($res_2);

$opbal = $opbalance[0];
$opbal = $opbal + $r21[0]-$r21[1];

            $mstaffdisc_amt = 0;
            $mdummy=0;
            $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invdate < '$df' and staffdisc > 0");
            while($rrow=mysqli_fetch_array($rbt_tbl))
            {
                $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
                $row22=mysqli_fetch_row($rbt_val);
                if($row22[0]=='Yes')
                    $mstaffdisc_amt += ($row22[1]*$rrow[1])*$rrow[2]/100;
                else
                {
                    $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
                    $mdummy +=  $rrow[3]*$rrow[2]/100;
                }
            }
            $mval = $mstaffdisc_amt;

if($r2[0]>0)
{
  $opbal = $opbal + $r2[0]-$r2[1]-$r2[2]-$r2[3]-$r2[4]+$r2[5]-$mval;
}
if($r_2[0]>0)
{
  $opbal = $opbal - $r_2[0];
}
$quer="select sum(total),sum(redct),sum(mba),sum(strbtnml),sum(strbtspl),sum(taxamt),sum(staffdisc),sum(nettot) from salesretn where invdate<'$df' and brcode='$brcode' ";
$res22=mysqli_query($db,$quer)or die('jour err:'.mysqli_error($db));
$r22=mysqli_fetch_row($res22);
if($r22[0]>0)
{
  $opbal = $opbal - $r22[0]+$r22[1]+$r22[2]+$r22[3]+$r22[4]-$r22[5]+$r22[6];
}
$mtotdr = $opbal;
$pdf->SetFont('Arial','',9);
$pdf->Cell(80,10,'Opening Balance:','LTR');
$pdf->Cell(50,10,number_format($opbal,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,10,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$quer12="select led from ledger where brcode='$brcode' and grp = 'Bank Accounts'";
$res12=mysqli_query($db,$quer12)or die('jour err:'.mysqli_error($db));
$mremittance = 0;
$bnk_remittance = 0;
while($row1=mysqli_fetch_array($res12))
{
  $quer13="select sum(dramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and partic = '$row1[0]'";
  $res13=mysqli_query($db,$quer13)or die('jour err:'.mysqli_error($db));
  $r13=mysqli_fetch_row($res13);
  $mremittance = $mremittance + $r13[0];
  $quer13="select sum(dramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and partic = '$row1[0]' and j_type = 'BR'";
  $res13=mysqli_query($db,$quer13)or die('jour err:'.mysqli_error($db));
  $r13=mysqli_fetch_row($res13);
  $bnk_remittance += $r13[0];
}
$mtotcr = $mremittance;

$pdf->Cell(80,6,'Remittance','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,number_format($mremittance,2,'.',','),'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'Collection through bank','LTR');
$pdf->Cell(50,6,number_format($bnk_remittance,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$mtotdr += $bnk_remittance;
$quer14="select invdate,gname,sum(total),sum(redct),sum(mba),sum(strbtnml),sum(strbtspl),sum(taxamt),sum(staffdisc),sum(nettot),pmode from prod_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode'  group by gname";
$res14=mysqli_query($db,$quer14)or die('jour err:'.mysqli_error($db));
$taxamt = 0;
$mdebtors =0;
while($row14=mysqli_fetch_array($res14))
{
$mtotcr = $mtotcr + $row16[2];
$mtotdr = $mtotdr + $row16[4]+$row16[5]+$row16[6];
$pdf->Cell(80,6,$row14[gname].' Sales','LTR');
$pdf->Cell(50,6,$row14[2],'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'MDA','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row14[4],'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'State Rebate','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row14[5],'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'State Rebate Special','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row14[6],'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'Reduction','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row14[3],'LTR',0, 'R');
$pdf->Ln(7);
$mstaffdisc_amt = 0;
	$mdummy=0;
    $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invdate >= '$df' and invdate <= '$dt'  and gname = '$row14[gname]' and staffdisc > 0");
    while($rrow=mysqli_fetch_array($rbt_tbl))
    {
                $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
                $row22=mysqli_fetch_row($rbt_val);
                if($row22[0]=='Yes')
                    $mstaffdisc_amt += ($row22[1]*$rrow[1])*$rrow[2]/100;
                else
                {
                    $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
                    $mdummy +=  $rrow[3]*$rrow[2]/100;
                }
    }
    $mval = $mstaffdisc_amt;

$pdf->Cell(80,6,'Staff Discount','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,number_format($mval,2,'.',','),'LTR',0, 'R');
$pdf->Ln(7);
  $taxamt = $taxamt + $row14[7];
  $mtotdr = $mtotdr + $row14[2];
  $mtotcr = $mtotcr + $row14[4] + $row14[5] + $row14[6] + $row14[3]+ $mval;
}
$quer14="select sum(nettot),pmode from prod_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' and pmode = 'CREDIT'";
$res14=mysqli_query($db,$quer14)or die('jour err:'.mysqli_error($db));
$r13=mysqli_fetch_row($res14);
$mdebtors =$r13[0];
$mtotdr = $mtotdr + $taxamt;
$mtotcr +=$mdebtors;	
$pdf->Cell(80,6,'Debtors','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,number_format($mdebtors,2,'.',','),'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'VAT Collected','LTR');
$pdf->Cell(50,6,number_format($taxamt,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$quer15="select invdate,sum(nettot) from prod_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' and pmode = 'CREDIT' group by invdate";
$row15=mysqli_query($db,$quer15);
$mtotdr = $mtotdr + $row15[1];

$pdf->Cell(80,6,'Sundry Debtors','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,number_format($row15[1],2,'.',','),'LTR',0, 'R');
$pdf->Ln(7);
$quer13="select sum(dramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and j_type = 'CR'";
$res13=mysqli_query($db,$quer13)or die('jour err:'.mysqli_error($db));
$r13=mysqli_fetch_row($res13);
$mdr_collection = $r13[0];
$mtotdr+= $mdr_collection;

$pdf->Cell(80,6,'Debtors Collection','LTR');
$pdf->Cell(50,6,number_format($mdr_collection,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
//$quer13="select sum(cramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and partic = 'BANK CHARGES FOR CREDIT CARD'";
$quer13="select sum(cramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and (partic = 'BANK CHARGES FOR CREDIT CARD' or partic = 'BANK CHARGES')";
$res13=mysqli_query($db,$quer13)or die('jour err:'.mysqli_error($db));

$quer22="select sum(dramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and partic = 'Cash' and j_type='BP'";
$res22=mysqli_query($db,$quer22)or die('jour err:'.mysqli_error($db));
$r23=mysqli_fetch_row($res22);
$mtotdr+= $r23[0];
if($r23[0]>0)
{
  $pdf->Cell(80,6,'Cash Cheque Dishounored','LTR');
$pdf->Cell(50,6,number_format($r23[0],2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
}

$quer22="select sum(dramt) from journal2 where date >= '$df' and date <= '$dt' and brcode='$brcode' and partic = 'Cash' and j_type='JV' and narr='Cheque Dishonour Charge'";
$res22=mysqli_query($db,$quer22)or die('jour err:'.mysqli_error($db));
$r23=mysqli_fetch_row($res22);
$mtotdr+= $r23[0];
if($r23[0]>0)
{
	
	$pdf->Cell(80,6,'Cash Collected(Bank Charges)Cheque Dishounored','LTR');
$pdf->Cell(50,6,number_format($r23[0],2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
	
}

$r13=mysqli_fetch_row($res13);
$mdr_bnk_chrgs = $r13[0];
$mtotdr+= $mdr_bnk_chrgs;
if($mdr_bnk_chrgs>0)
{
$pdf->Cell(80,6,'Bank Charges for Credit Card','LTR');
$pdf->Cell(50,6,number_format($mdr_bnk_chrgs,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
}
$quer16="select invdate,gname,sum(total),sum(redct),sum(mba),sum(strbtnml),sum(strbtspl),sum(taxamt),sum(staffdisc),sum(nettot) from salesretn where invdate>='$df' and invdate<='$dt' and brcode='$brcode'  group by gname";
$res16=mysqli_query($db,$quer16);
$taxamt = 0;
while($row16=mysqli_fetch_array($res16))
{
  $mtotcr = $mtotcr + $row16[2];
  $mtotdr = $mtotdr + $row16[4]+$row16[5]+$row16[6];
  $pdf->Cell(80,6,'Sales Return'.$row16[gname],'LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row16[2],'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'Bank Charges for Credit Card','LTR');
$pdf->Cell(50,6,number_format($mdr_bnk_chrgs,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'MDA','LTR');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Cell(50,6,$row16[4],'LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'State Rebate','LTR');
$pdf->Cell(50,6,$row16[5],'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$pdf->Cell(80,6,'State Rebate Special','LTR');
$pdf->Cell(50,6,$row16[6],'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$taxamt = $taxamt + $row16[7];
}
$mtotcr = $mtotcr + $taxamt;

$pdf->Cell(80,6,'VAT Amount (Sales Return)','LTR');
$pdf->Cell(50,6,number_format($taxamt,2,'.',','),'LTR',0, 'R');
$pdf->Cell(50,6,'0.00','LTR',0, 'R');
$pdf->Ln(7);
$quer17="select invdate,gname,sum(total) from brtransferin where invdate>='$df' and invdate<='$dt' and brcode='$brcode'  group by gname";
//print $quer17;
$res17=mysqli_query($db,$quer17);
$taxamt = 0;
$mtot_purch = 0;
while($row17=mysqli_fetch_array($res17))
{
  $mtotcr = $mtotcr + $row17[2];
   $pdf->Cell(80,6,'Purchase of '.$row17[gname],'LTR');
  $pdf->Cell(50,6,'0.00','LTR',0, 'R');
  $pdf->Cell(50,6,$row17[2],'LTR',0, 'R');
  $pdf->Ln(7);
  $mtot_purch += $row17[2];
}
$mtotdr = $mtotdr + $mtot_purch;
  $pdf->Cell(80,6,'Sundry Creditors','LTR');
  $pdf->Cell(50,6,$mtot_purch,'LTR',0, 'R');
  $pdf->Cell(50,6,'0.00','LTR',0, 'R');
  $pdf->Ln(7);
  $quer18="select invdate,gname,sum(total) from brtransferout where invdate>='$df' and invdate<='$dt' and brcode='$brcode'  group by gname";
$res18=mysqli_query($db,$quer18);
$taxamt = 0;
$mitem_trans = 0;
while($row18=mysqli_fetch_array($res18))
{
  $mtotdr = $mtotdr + $row18[2];
  $mitem_trans += $row18[2];
  $pdf->Cell(80,6,'Transfer of '.$row18[gname],'LTR');
  $pdf->Cell(50,6,$row18[2],'LTR',0, 'R');
  $pdf->Cell(50,6,'0.00','LTR',0, 'R');
  $pdf->Ln(7);
 }
  $mtotcr = $mtotcr + $mitem_trans;
  $pdf->Cell(80,6,'Item Transfer','LTR');
  $pdf->Cell(50,6,'0.00','LTR',0, 'R');
  $pdf->Cell(50,6,$mitem_trans,'LTR',0, 'R');
  $pdf->Ln(7);
  $mclosing = $mtotdr-$mtotcr;
  $mtotcr+=$mclosing;
  $pdf->Cell(80,6,'Closing Balance','LTR');
  $pdf->Cell(50,6,'0.00','LTR',0, 'R');
  $pdf->Cell(50,6,number_format($mclosing,2,'.',','),'LTR',0, 'R');
  $pdf->Ln(7);
  $pdf->SetFont('Arial','',12);
  $pdf->Cell(80,6,'T O T A L','LTRB');
  $pdf->Cell(50,6,number_format($mtotdr,2,'.',','),'LTRB',0, 'R');
  $pdf->Cell(50,6,number_format($mtotcr,2,'.',','),'LTRB',0, 'R');
  $pdf->Ln(7);
		$pdf->Output();	
?>
