<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];

$zroot = $_SESSION['zdir'];

$trandb = "actran";
$myear = $_SESSION["tit_" . $dbname];
mysqli_select_db($conn, $zdb);
if (isset($_POST["view"])) {
    $acond = "";
    $scond = "";

    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);

        $pcond = "AND a.`party`='$accode'";
        $acond = "AND a.`accode`='$accode'";
    }
    $mtodate = $_POST["frmdt"];

    $mfile = "purmain";
    $mtype = "P";
    $mdate = 'vouchdt';
    $fintot = "billamt";
    $grid = "osbill.php?para=P";
    $formname1 = "Outstanding Payable";

    $formname = "$formname1";
    if ($_POST["dtype"] == 1) {
        $dtype = 1;
    } else {
        $dtype = 2;
    }
    include("includes/linkshead.php");



//    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <link href="MyStyle/rep_format.css" rel="stylesheet">

    <style>
        h5{
            font-size: 18px;
            margin-top: 7px;
            margin-left: 6px;
        }
        .report{
            margin-top: 0px;
        }
    </style>
    </head> 
    <body>
        <div class="row" style="margin-top: 15px;">

            <div class="col-lg-6" style="width:100%;">
                <?php
                mysqli_query($conn, "create temporary table cmain 
                        SELECT a.party, b.name  FROM `rawmain` a, accmast b WHERE a.party = b.accode and a.vouchdt <= '$mtodate' and a.vouchno != '' $pcond 
                        UNION all
                        SELECT a.party, b.name  FROM `puotmain` a, accmast b WHERE a.party = b.accode and a.vouchdt <= '$mtodate' and a.vouchno != '' $pcond
                        UNION all
                        SELECT a.party, b.name  FROM `purexpmain` a, accmast b WHERE a.party = b.accode and a.vouchdt <= '$mtodate' and a.vouchno != '' $pcond 
                        UNION all
                        SELECT a.accode, b.name FROM `prevbill` a, accmast b WHERE a.accode = b.accode  and a.billdate <= '$mtodate' and a.type = 'P' $acond ") or die(mysqli_error($conn));

                $acc = mysqli_query($conn, "SELECT party, name FROM cmain group by party order by name ") or die(mysqli_error($conn));
//                print table($acc);
//                $getno = mysqli_num_rows($acc);
                ?>

                <div class="box">
                    <?php include("includes/head_rep.php"); ?>
                    <div id="sortableTable" class="body collapse in">
                        <table class="table table-bordered sortableTable responsive-table">
                            <table style="width:100%;" id="" class="table-bordered sortableTable responsive-table report table table-bordered">
                                <thead>
                                    <tr class="mySearchTr">
                                        <th style="" colspan="5">Name</th>
                                        <th style="text-align: right;">Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    while ($getac = mysqli_fetch_array($acc)) {

                                        $maccode = $getac["party"];
//                                        $gparty = mysqli_query($conn, "SELECT name from accmast where accode = '$maccode' ")or die(mysqli_error($conn));
//                                        $fparty = mysqli_fetch_array($gparty);
                                        $macname = $getac["name"];

                                        $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM rawmain  WHERE `party` = '$maccode' AND `paidby` != 'C' ") or die(mysqli_error($conn));
                                        $sub2 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt2 FROM puotmain WHERE `party` = '$maccode' AND `paidby` != 'C' ") or die(mysqli_error($conn));

                                        $sub3 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt3 FROM `prevbill` WHERE `accode` = '$maccode'") or die(mysqli_error($conn));

                                        $msub1 = mysqli_fetch_array($sub1);
                                        $msub2 = mysqli_fetch_array($sub2);
                                        $msub3 = mysqli_fetch_array($sub3);

                                        $msamt1 = $msub1["amt1"];
                                        $msamt2 = $msub2["amt2"];
                                        $msamt3 = $msub3["amt3"];

                                        $qamt1 = $msamt1 + $msamt2 + $msamt3;


                                        $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
                                        $gtrn1 = mysqli_fetch_object($trn1);
                                        $msetoff1 = $gtrn1->setoff;

                                        $mbalamt1 = $qamt1 - $msetoff1;


                                        if (round($mbalamt1) > 0) {
                                            echo "<tr style='background-color:white'>";
                                            echo "<td colspan='5' style=\"text-align:left;\">" . strtoupper($macname) . "</td>";
                                            echo "<td style=\"text-align:right;\">";
                                            echo "$mbalamt1";
                                            echo "</td>";
                                            echo "</tr>";
                                            $grtotbalamt = $grtotbalamt + $mbalamt1;

                                            if ($dtype == 2) {
                                                ?>        

                                                <tr style="height:10px;" class="mySearchTr">
                                                    <th align="left" width="15%">Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="left" width="15%">Bill No.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Bill Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Paid Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="9%" style="text-align:right;">Days<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                </tr>
                                                <?php
                                                $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM rawmain WHERE `party` = '$maccode' AND `paidby` != 'C' 
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM puotmain WHERE `party` = '$maccode' AND `paidby` != 'C' 
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, 'E' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM purexpmain WHERE `party` = '$maccode' AND `paidby` != 'C' 
                                                                UNION                                                                
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                               `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' 
                                                                AND `type` = '$mtype'") or die(mysqli_error($conn));
                                                $totbillamt = 0;
                                                $totrecdamt = 0;
                                                $totbalamt = 0;


                                                while ($murl = mysqli_fetch_object($subtmp1)) {

                                                    $qyear = $murl->yearno;
                                                    $qbilldate = $murl->billdate;
                                                    $qbill = $murl->billno;
                                                    $qvouch = $murl->vouchno;
                                                    $qamt = $murl->billamt;
                                                    $qparty = $murl->accode;


                                                    $dt1 = date_create($mtodate);
                                                    $dt2 = date_create($qbilldate);

                                                    $subtdate = date_diff($dt1, $dt2);
                                                    $mldays = $subtdate->format('%a');

                                                    $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                       `vouchno`='$qvouch' AND `type`='$mtype'") or die(mysqli_error($conn));

                                                    $gtrn = mysqli_fetch_object($trn);

                                                    $msetoff = $gtrn->setoff;
                                                    $mbalamt = $qamt - $msetoff;

                                                    if ($mbalamt > 0) {
                                                        $totbillamt = $totbillamt + $qamt;
                                                        $totrecdamt = $totrecdamt + $msetoff;
                                                        $totbalamt = $totbalamt + $mbalamt;
                                                        echo "<tr style='height:10px;background-color:white'>";
                                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\">$qbilldate</td>";
                                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"100\">$qbill</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\">$qamt</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$msetoff</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">" . number_format($mbalamt, 2) . "</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$mldays</td>";
                                                        echo "</tr>";
                                                    }
                                                }
                                                echo "<tr style='height:10px;background-color:white'>";
                                                echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\"></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"><b>Total</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\"><b>$totbillamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totrecdamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totbalamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                                                echo "</tr>";
                                            }
                                        }
                                    }

                                    echo "<tr style='background-color:white'>";
                                    echo "<td colspan='5' style=\"text-align:right;font-size:15px\"><b>Total</b></td>";
                                    echo "<td style=\"text-align:right;font-size:15px\">";
                                    echo "<b>$grtotbalamt</b>";
                                    echo "</td>";
                                    echo "</tr>";
                                    ?>

                                </tbody>
                            </table>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

    if (isset($_POST["excel"])) {
        $acond = "";
        $scond = "";

        if (!empty($_POST["acname"])) {
            $accode = trim($_POST["acname"]);

            $pcond = "AND a.`party`='$accode'";
            $acond = "AND a.`accode`='$accode'";
        }
        $mtodate = $_POST["frmdt"];

        $mfile = "purmain";
        $mtype = "P";
        $mdate = 'vouchdt';
        $fintot = "billamt";
        $grid = "osbill.php?para=P";
        $formname1 = "Outstanding Payable";

        $formname = "$formname1";
        if ($_POST["dtype"] == 1) {
            $dtype = 1;
        } else {
            $dtype = 2;
        }

        mysqli_query($conn, "create temporary table cmain 
                        SELECT a.party, b.name  FROM `rawmain` a, accmast b WHERE a.party = b.accode and a.vouchno != '' $pcond 
                        UNION
                        SELECT a.party, b.name  FROM `puotmain` a, accmast b WHERE a.party = b.accode and a.vouchno != '' $pcond
                        UNION
                        SELECT a.accode, b.name FROM `prevbill` a, accmast b WHERE a.accode = b.accode  $acond ") or die(mysqli_error($conn));

        $acc = mysqli_query($conn, "SELECT party, name FROM cmain group by party order by name ") or die(mysqli_error($conn));
        $getno = mysqli_num_rows($acc);

        $contents .= "Name,Amount\n";

        while ($getac = mysqli_fetch_array($acc)) {

            $maccode = $getac["party"];
//                                        $gparty = mysqli_query($conn, "SELECT name from accmast where accode = '$maccode' ")or die(mysqli_error($conn));
//                                        $fparty = mysqli_fetch_array($gparty);
            $macname = $getac["name"];

            $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM rawmain  WHERE `party` = '$maccode' AND `paidby` != 'C' ") or die(mysqli_error($conn));
            $sub2 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt2 FROM puotmain WHERE `party` = '$maccode' AND `paidby` != 'C' ") or die(mysqli_error($conn));

            $sub3 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt3 FROM `prevbill` WHERE `accode` = '$maccode'") or die(mysqli_error($conn));

            $msub1 = mysqli_fetch_array($sub1);
            $msub2 = mysqli_fetch_array($sub2);
            $msub3 = mysqli_fetch_array($sub3);

            $msamt1 = $msub1["amt1"];
            $msamt2 = $msub2["amt2"];
            $msamt3 = $msub2["amt3"];

            $qamt1 = $msamt1 + $msamt2 + $msamt3;


            $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
            $gtrn1 = mysqli_fetch_object($trn1);
            $msetoff1 = $gtrn1->setoff;

            $mbalamt1 = $qamt1 - $msetoff1;


            if ($mbalamt1 > 0) {

                $contents .= strtoupper($macname) . ",";
                $contents .= "$mbalamt1" . "\n";
            }
//            $contents .= "$mbalamt1" . "\n";

            $grtotbalamt = $grtotbalamt + $mbalamt1;

            if ($dtype == 2) {

                $contents .= "Date,Bill No.,Bill Amount,Paid Amount,Balance,Days\n";


                $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM rawmain WHERE `party` = '$maccode' AND `paidby` != 'C' 
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM puotmain WHERE `party` = '$maccode' AND `paidby` != 'C' 
                                                                UNION                                                                
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                               `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' 
                                                                AND `type` = '$mtype'") or die(mysqli_error($conn));
                $totbillamt = 0;
                $totrecdamt = 0;
                $totbalamt = 0;


                while ($murl = mysqli_fetch_object($subtmp1)) {

                    $qyear = $murl->yearno;
                    $qbilldate = $murl->billdate;
                    $qbill = $murl->billno;
                    $qvouch = $murl->vouchno;
                    $qamt = $murl->billamt;
                    $qparty = $murl->accode;


                    $dt1 = date_create($mtodate);
                    $dt2 = date_create($qbilldate);

                    $subtdate = date_diff($dt1, $dt2);
                    $mldays = $subtdate->format('%a');

                    $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                       `vouchno`='$qvouch' AND `type`='$mtype'") or die(mysqli_error($conn));

                    $gtrn = mysqli_fetch_object($trn);

                    $msetoff = $gtrn->setoff;
                    $mbalamt = $qamt - $msetoff;

                    if ($mbalamt > 0) {
                        $totbillamt = $totbillamt + $qamt;
                        $totrecdamt = $totrecdamt + $msetoff;
                        $totbalamt = $totbalamt + $mbalamt;

                        $contents .= "$qbilldate" . ",";
                        $contents .= "$qbill" . ",";
                        $contents .= "$qamt" . ",";
                        $contents .= "$msetoff" . ",";
                        $contents .= "$mbalamt" . ",";
                        $contents .= "$mldays" . "\n";
                    }
                }

                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$totbillamt" . ",";
                $contents .= "$totrecdamt" . ",";
                $contents .= "$totbalamt" . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$grtotbalamt" . "\n";

        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1) {
            header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
            print $contents;
        } else {
            header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
            print $contents;
        }
    }

//    if (isset($_POST["excel"])) {
//        $acond = "";
//        $scond = "";
//
//        if (!empty($_POST["acname"])) {
//            $accode = trim($_POST["acname"]);
//            $acond = "AND a.`party`='$accode'";
//        }
//
//        $mtodate = $_POST["frmdt"];
//
//        if ($_POST["hiddenval"] == "R") {
//            $mfile = "salemain";
//            $mtype = "S";
//            $mdate = 'invdt';
//            $fintot = "netamt";
//            $grid = "osbill.php?para=R";
//            $formname1 = "Outstanding Receivable";
//        } else {
//            $mfile = "purmain";
//            $mtype = "P";
//            $mdate = 'vouchdt';
//            $fintot = "netamt";
//            $grid = "osbill.php?para=P";
//            $formname1 = "Outstanding Payable";
//        }
//        if ($_POST["dtype"] == 1) {
//            $formname = "$formname1 - Summary";
//            $dtype = 1;
//        } else {
//            $formname = "$formname1 - Detail";
//            $dtype = 2;
//        }
//
//
//
//        $mtdt = date("d-m-Y", strtotime($mtodate));
//
//        $contents .= "Name,Amount,\n";
//
//        $acc = mysqli_query($conn, "SELECT a.*, b.name FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode $acond GROUP BY a.party order by b.name") or die(mysqli_error($conn));
//        while ($getac = mysqli_fetch_array($acc)) {
//
//            $maccode = $getac["party"];
//            $macname = $getac["name"];
//
//            $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile WHERE `party` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));
//
//            $sub2 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt2 FROM `prevbill` WHERE `accode` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));
//            $msub1 = mysqli_fetch_array($sub1);
//            $msub2 = mysqli_fetch_array($sub2);
//            $msamt1 = $msub1["amt1"];
//
//            $msamt2 = $msub2["amt2"];
//            $qamt1 = $msamt1 + $msamt2;
//
//
//            $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
//            $gtrn1 = mysqli_fetch_object($trn1);
//            $msetoff1 = $gtrn1->setoff;
//
//            $mbalamt1 = $qamt1 - $msetoff1;
//
//
//            if ($mbalamt1 > 0) {
//
//                $contents .= strtoupper($macname) . ",";
//                $contents .= "$mbalamt1" . "\n";
//            }
//            $contents .= $mbalamt1 . "\n";
//
//            if ($dtype == 2) {
//
//                $contents .= "Date,Bill No.,Bill Amount,Recd. Amount,Balance,Days,\n";
//
//                $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
//                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
//                                                                FROM $mfile WHERE `party` = '$maccode' AND `cashcred` != 'C' 
//                                                                UNION
//                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
//                                                               `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' 
//                                                                AND `type` = '$mtype'") or die(mysqli_error($conn));
//                $totbillamt = 0;
//                $totrecdamt = 0;
//                $totbalamt = 0;
//
//                while ($murl = mysqli_fetch_object($subtmp1)) {
//
//                    $qyear = $murl->yearno;
//                    $qbilldate = $murl->billdate;
//                    $qbill = $murl->billno;
//                    $qvouch = $murl->vouchno;
//                    $qamt = $murl->billamt;
//                    $qparty = $murl->accode;
//
//
//                    $dt1 = date_create($mtodate);
//                    $dt2 = date_create($qbilldate);
//                    $subtdate = $dt1 - $dt2;
//                    $mldays = $subtdate;
//
//
//                    $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
//                                                                       `vouchno`='$qvouch' AND `type`='$mtype'") or die(mysqli_error($conn));
//
//                    $gtrn = mysqli_fetch_object($trn);
//
//                    $msetoff = $gtrn->setoff;
//                    $mbalamt = $qamt - $msetoff;
//
//                    if ($mbalamt > 0) {
//                        $totbillamt = $totbillamt + $qamt;
//                        $totrecdamt = $totrecdamt + $msetoff;
//                        $totbalamt = $totbalamt + $mbalamt;
//                        $contents .= $qbilldate . ",";
//                        $contents .= $qbill . ",";
//                        $contents .= $qamt . ",";
//                        $contents .= $msetoff . ",";
//                        $contents .= $mbalamt . ",";
//                        $contents .= $mldays . "\n";
//                    }
//                }
//
//                $contents .= " " . ",";
//                $contents .= "Total" . ",";
//                $contents .= $totbillamt . ",";
//                $contents .= $totrecdamt . ",";
//                $contents .= $totbalamt . ",";
//                $contents .= " " . "\n";
//            }
//        }
//        $contents = strip_tags($contents);
//
//        if ($_POST["dtype"] == 1) {
//            header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
//            print $contents;
//        } else {
//            header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
//            print $contents;
//        }
//    }



    if (isset($_POST["subpdf"])) {
        $acond = "";
        $scond = "";

        if (!empty($_POST["acname"])) {
            $accode = trim($_POST["acname"]);
            $acond = "AND a.`party`='$accode'";
        }

        $mtodate = $_POST["frmdt"];

        if ($_POST["hiddenval"] == "R") {
            $mfile = "salemain";
            $mtype = "S";
            $mdate = 'invdt';
            $fintot = "netamt";
            $grid = "osbill.php?para=R";
            $formname1 = "Outstanding Receivable";
        } else {
            $mfile = "purmain";
            $mtype = "P";
            $mdate = 'vouchdt';
            $fintot = "netamt";
            $grid = "osbill.php?para=P";
            $formname1 = "Outstanding Payable";
        }
        if ($_POST["dtype"] == 1) {
            $formname = "$formname1 - Summary";
            $dtype = 1;
        } else {
            $formname = "$formname1 - Detail";
            $dtype = 2;
        }
        $mtdt = date("d-m-Y", strtotime($mtodate));

        require("library/fpdf/fpdf.php");

        class pdf extends fpdf {

            function Footer() {



//Position at 1.5 cm from bottom

                $this->SetY(-15);

//Arial italic 8

                $this->SetFont('Arial', 'I', 8);

//Page number

                $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
            }

        }

        $pdf = new pdf('P', 'mm', 'A4');

        $pdf->AliasNbPages();

        $pdf->AddPage();
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(96, 130, 191);
        $pdf->Cell(120, 7, "Name", 1, 0, '', 1);
        $pdf->Cell(73, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $acc = mysqli_query($conn, "SELECT a.*, b.name FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode $acond GROUP BY a.party order by b.name") or die(mysqli_error($conn));
        while ($getac = mysqli_fetch_array($acc)) {

            $maccode = $getac["party"];
            $macname = $getac["name"];

            $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile WHERE `party` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));

            $sub2 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt2 FROM `prevbill` WHERE `accode` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));
            $msub1 = mysqli_fetch_array($sub1);
            $msub2 = mysqli_fetch_array($sub2);
            $msamt1 = $msub1["amt1"];

            $msamt2 = $msub2["amt2"];
            $qamt1 = $msamt1 + $msamt2;


            $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
            $gtrn1 = mysqli_fetch_object($trn1);
            $msetoff1 = $gtrn1->setoff;

            $mbalamt1 = $qamt1 - $msetoff1;


            if ($mbalamt1 > 0) {
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(120, 7, strtoupper($macname), 1);
                $pdf->Cell(73, 7, "$mbalamt1", 1);
                $pdf->Ln();

                if ($dtype == 2) {

                    $pdf->SetFont('Arial', 'I', 10);
                    $pdf->SetTextColor(255, 255, 255);
                    $pdf->SetFillColor(96, 130, 191);
                    $pdf->Cell(32, 7, "Date", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "Bill No.", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "Bill Amount", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "Recd. Amount", 1, 0, '', 1, 0, 'R');
                    $pdf->Cell(32, 7, "Balance", 1, 0, '', 1, 0, 'R');
                    $pdf->Cell(33, 7, "Days", 1, 0, '', 1, 0, 'R');
                    $pdf->SetFont('Arial', '', 1, 0);

                    $pdf->SetTextColor(0, 0, 0);
                    $pdf->Ln();

                    $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `party` as accode 
                                                                FROM $mfile WHERE `party` = '$maccode' AND `cashcred` != 'C' 
                                                                UNION
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                               `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' 
                                                                AND `type` = '$mtype'") or die(mysqli_error($conn));
                    $totbillamt = 0;
                    $totrecdamt = 0;
                    $totbalamt = 0;

                    while ($murl = mysqli_fetch_object($subtmp1)) {

                        $qyear = $murl->yearno;
                        $qbilldate = $murl->billdate;
                        $qbill = $murl->billno;
                        $qvouch = $murl->vouchno;
                        $qamt = $murl->billamt;
                        $qparty = $murl->accode;


                        $dt1 = date_create($mtodate);
                        $dt2 = date_create($qbilldate);
                        $subtdate = $dt1 - $dt2;
                        $mldays = $subtdate;


                        $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                       `vouchno`='$qvouch' AND `type`='$mtype'") or die(mysqli_error($conn));

                        $gtrn = mysqli_fetch_object($trn);

                        $msetoff = $gtrn->setoff;
                        $mbalamt = $qamt - $msetoff;

                        if ($mbalamt > 0) {
                            $totbillamt = $totbillamt + $qamt;
                            $totrecdamt = $totrecdamt + $msetoff;
                            $totbalamt = $totbalamt + $mbalamt;
                            $pdf->SetFont('Arial', '', 8);
                            $pdf->Cell(32, 7, "$qbilldate", 1);
                            $pdf->Cell(32, 7, "$qbill", 1);
                            $pdf->Cell(32, 7, "$qamt", 1);
                            $pdf->Cell(32, 7, "$msetoff", 1);
                            $pdf->Cell(32, 7, "$mbalamt", 1);
                            $pdf->Cell(33, 7, "$mldays", 1);
                            $pdf->Ln();
                        }
                    }
                    $pdf->SetFont('Arial', 'I', 10);
                    $pdf->SetTextColor(255, 255, 255);
                    $pdf->SetFillColor(255, 0, 0);

                    $pdf->Cell(32, 7, " ", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "Total", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "$totbillamt", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "$totrecdamt", 1, 0, '', 1);
                    $pdf->Cell(32, 7, "$totbalamt", 1, 0, '', 1);
                    $pdf->Cell(33, 7, "$totbalamt", 1, 0, '', 1);
                    $pdf->SetFont('Arial', '', 1, 0);

                    $pdf->SetTextColor(0, 0, 0);
                    $pdf->Ln();
                }
            }
        }
        $pdf->Output("Outstanding Bill.pdf", "D");
    }
    ?>