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

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zcompany = $_SESSION["zcompany"];
$formname = "Cash Book";

$grid = "cashbook_in_out.php";
$cust = "accmast";
$ztit = $_SESSION["tit_" . $dbname];
$zroot = $_SESSION["zdir"];
$maindb = $cust;
$accdb = "actran";

$fseries = $_SESSION["series"];
$keycond = "";
$abal = 'accmtn';
$accbal = $abal . $ztit;
$compcond = '';

//if (isset($_POST["view"])) {
$a = 1;

$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);
$mname1 = trim($_REQUEST["accode"]);

$mfdt = $mfrmdate;
$mtdt = $mtodate;

if ($mfrmdate > $mtodate) {
    browse("To Date Cannot Be Less Than From Date");
    header("refresh: 1; ledger_new.php");
    exit();
    $a = 0;
}

$maccode = '';
if ($mname1 == '') {
    browse("A/c. Name Cannot Be Empty");
    header("refresh: 1; ledger_new.php");
    exit();
    $a = 0;
} else {
    $maccode = $mname1;
}
$paytypecond = '';
if (!empty($_POST["paytype"])) {
    $paytype = trim($_POST["paytype"]);
    $paytypecond = "AND `paytype`='$partytype'";
}


$zyear = right($zdb, 4);
//$mfrmdate = getzdates($zyear, 'S', $conn);
//$mtodate = getzdates($zyear, 'E', $conn);
mysqli_select_db($conn, $zdb);


//if ($a == 1) {
$recqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount < 0 and accode = '$maccode' and chqret != 'Y'
                                and date >= '$mfrmdate' and date <= '$mtodate' 
                                order by date, source, vouchno ") or die(mysqli_error($conn));
$rcount = mysqli_num_rows($recqry);

$payqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount > 0 and accode = '$maccode' and chqret != 'Y'
                                and date >= '$mfrmdate' and date <= '$mtodate' 
                                order by date, source, vouchno ") or die(mysqli_error($conn));
$lcount = mysqli_num_rows($payqry);

$getopen = mysqli_query($conn, "SELECT grpcode, accode, name, openbal, dc  FROM `$maindb` WHERE `accode` = '$maccode'  ") or die(mysqli_error($conn));

$ctr = 0;
$totrecp = 0;
$fetopen = mysqli_fetch_array($getopen);
$mname = $fetopen['name'];

$opdc = $fetopen["dc"];

if ($opdc == 'D') {
    $opb = $fetopen["openbal"] * -1;
} else {
    $opb = $fetopen["openbal"];
}
$opbt = $opb;

$mopen = $opbt;

$getacc = mysqli_query($conn, "SELECT SUM(amount) AS `openamt` FROM `$accdb` WHERE `accode` = '$maccode' and `date` < '$mfrmdate' and chqret != 'Y' ") or die(mysqli_error($conn));
$fetacc = mysqli_fetch_array($getacc);
$openacc = $fetacc['openamt'];

$finopen = $mopen + $openacc;
$finop = abs($finopen);

//        $getacc2 = mysqli_query($conn, "SELECT SUM(amount) AS `openamt` FROM `$accdb` WHERE `accode` = '$maccode' and `date` <= '$mtodate' and chqret != 'Y' $compcond $keycond")or die(mysqli_error($conn));
//        $fetacc2 = mysqli_fetch_array($getacc2);
//        $openacc2 = $fetacc2['openamt'];
//
//        $finclos = $mopen + $openacc2;
//        $fincl = abs($finclos);

include("includes/linkshead.php");
?>
<style>
    @media print {
        a {
            text-decoration: none;
            /* Remove the underline */
            color: black;
            /* Change the color to black */
        }

        a[href]:after {
            content: none;
            /* Hide the href */
        }

        table td {
            font-size: 10px;
        }
    }

    .noprint {
        margin-top: 5px;
        border: 2px solid;
        border-radius: 10px;
    }

    .noprint1 {
        margin-top: 5px;
        border: 2px solid;
        border-radius: 10px;
    }
</style>

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>

<script>
    function saveAsPDF() {
        const element = document.getElementById('content-to-save');
        // Get the invoice number from an element
        const vouchno = document.getElementById('vouchno')?.textContent || 'NoINV';

        html2pdf()
            .set({
                margin: 0,
                filename: `CASHBOOKPDF_${vouchno}.pdf`, // Dynamically set filename
                image: {
                    type: 'jpeg',
                    quality: 0.98
                },
                html2canvas: {
                    scale: 3
                }, // Increase scale for better quality and fit
                jsPDF: {
                    unit: 'in',
                    format: 'a4',
                    orientation: 'portrait'
                }
            })
            .from(element)
            .save();
    }
</script>
</head>

<body>

    <div class="container">
        <button class="btn btn-primary goBack noprint" onclick="window.close()" style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Go Back</button>
        <button class="btn btn-primary noprint1" onclick="saveAsPDF()"
            style="float: right;width: 150px;height:35px;background-color: #40916c;color:#ffffff;">PDF</button>
        <div id="content-to-save">
            <div class="row">
                <div class="col-lg-6" style="width:100%;">
                    <div class="box">
                        <?php
                        //                    include("includes/head_rep.php");
                        ?>

                        <table width="100%">
                            <tr style="border:2px">
                                <th style="font-size: 18px;color:black" colspan="2"> <?= $mname ?></th>
                            </tr>
                            <tr>
                                <td width="50%" style="float:left">
                                    <table style="background: white;float:right" class="table  table-bordered" style="width:100%">
                                        <thead class="mySearchTr" style="background-color: #8d99ae;">
                                            <tr style="border:2px">
                                                <th colspan="5" style="font-size: 16px">Received</th>
                                            </tr>

                                            <tr style="border:2px;background:#40916c">
                                                <th width="28%">Date</th>
                                                <th width="4%">Source</th>
                                                <th width="8%">Voucher</th>
                                                <th width="40%">Description</th>
                                                <th width="18%" class="text-right">Amount</th>
                                            </tr>
                                        </thead>

                                        <tbody>
                                            <?php
                                            $openflag = 0;
                                            if ($finopen <= 0) {
                                                $ctr++;
                                                echo "<tr>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td>Opening</td>";
                                                echo "<td></td>";
                                                echo "<td class='text-right'>$finop</td>";
                                                echo "</tr>";

                                                $totrecp = $totrecp + $finop;
                                                $openflag = 1;
                                            } else {
                                                if ($lop == 1) {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td>&nbsp;</td>";
                                                    echo "<td></td>";
                                                    echo "</tr>";
                                                }
                                            }

                                            $ctr = 0;
                                            while ($receipt = mysqli_fetch_array($recqry)) {
                                                $source = $receipt['source'];
                                                $vouchno = $receipt['vouchno'];
                                                $vouchdt = $receipt['vouchdt'];
                                                $grpcode = $receipt['grpcode'];
                                                $recamt = abs($receipt['amount']);
                                                $amt = $receipt['amount'];

                                                $colr = "";
                                                $acname = '';

                                                if ($source == 'PYIN') {
                                                    $url = "payin.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "payin_fr.php?vouchno=$vouchno";
                                                    $desc = "Payment In";

                                                    if ($grpcode == '002') {
                                                        $getcash = mysqli_query($conn, "Select party as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    } else {
                                                        $getcash = mysqli_query($conn, "Select bankname as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    }
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $accode = $fetcash['accode'];

                                                    $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
                                                    $fetacc = mysqli_fetch_array($getacc);
                                                    $desc = $fetacc['name'];
                                                }

                                                if ($source == 'CR') {
                                                    $url = "cashrecp.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "cashrecp_fr.php?vouchno=$vouchno";
                                                    $desc = "Receipt";

                                                    if ($grpcode == '002') {
                                                        $getcash = mysqli_query($conn, "Select party as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    } else {
                                                        $getcash = mysqli_query($conn, "Select bankname as accode from `cashrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    }
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $accode = $fetcash['accode'];

                                                    $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
                                                    $fetacc = mysqli_fetch_array($getacc);
                                                    $desc = $fetacc['name'];
                                                }

                                                if ($source == 'TR') {
                                                    $url = "contra.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "contrec.php?vouchno=$vouchno";
                                                    $desc = "Contra Entry";

                                                    $getcash = mysqli_query($conn, "Select b.name from `contra` a, accmast b 
                                                                                       where a.`vouchno`='$vouchno' and a.frombc = b.accode ") or die(mysqli_error($conn));
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $desc = $fetcash['name'];
                                                }

                                                if ($source == 'JV') {
                                                    $url = "jventry.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "jventry.php?vouchno=$vouchno";
                                                    $desc = "JV Entry";

                                                    if ($amt > 0) {
                                                        $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.party = b.accode ") or die(mysqli_error($conn));
                                                    } else {
                                                        $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.bankname = b.accode ") or die(mysqli_error($conn));
                                                    }
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $desc = $fetcash['name'];
                                                }


                                                $ctr++;
                                                echo "<tr style='$colr' class='clickable-row2' data-href='$url' data-target='_blank'>";
                                                echo "<td>$vouchdt</td>";
                                                echo "<td>$source</td>";
                                                echo "<td><a href='$url2' target='_blank'>$vouchno</a></td>";
                                                echo "<td>$desc</td>";
                                                echo "<td class='text-right'>$recamt</td>";
                                                echo "</tr>";

                                                $totrecp = $totrecp + $recamt;
                                            }

                                            if ($rcount < $lcount) {
                                                $ext = $lcount - $rcount;
                                                for ($i = 0; $i < $ext; $i++) {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td>&nbsp;</td>";
                                                    echo "<td></td>";
                                                    echo "</tr>";
                                                }
                                            }

                                            echo "<tr style='background:#95d5b2'>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td class='text-right' style'font-size:12px !important;'>Total</td>";
                                            echo "<td class='text-right' style'font-size:12px !important;'><b>$totrecp</b></td>";
                                            echo "</tr>";
                                            ?>
                                        </tbody>
                                    </table>
                                </td>

                                <td width="50%" style="float:right">
                                    <table style="background:white; width: 100%;" class="table table-bordered">
                                        <thead class="mySearchTr" style="background-color: #8d99ae;">

                                            <tr style="border:2px">
                                                <th colspan="6" style="font-size: 16px">Paid</th>
                                            </tr>
                                            <tr style="border:2px;background:#40916c">
                                                <th width="28%">Date</th>
                                                <th width="4%">Source</th>
                                                <th width="8%">Voucher</th>
                                                <th width="40%">Description</th>
                                                <th width="18%" class="text-right">Amount</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $lop = 0;
                                            if ($finopen > 0) {
                                                $ctr++;
                                                echo "<tr>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td>Opening</td>";
                                                echo "<td></td>";
                                                echo "<td class='text-right'>$finop</td>";
                                                echo "</tr>";
                                                $lop = 1;
                                                $totpay = $totpay + $finop;
                                            }

                                            if ($openflag > 0) {
                                                echo "<tr>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td>&nbsp;</td>";
                                                echo "<td></td>";
                                                echo "</tr>";
                                            }
                                            ?>
                                            <?php
                                            $ctr2 = 0;
                                            $totpay = 0;

                                            while ($payment = mysqli_fetch_array($payqry)) {
                                                $source = $payment['source'];
                                                $vouchno = $payment['vouchno'];
                                                $vouchdt = $payment['vouchdt'];
                                                $payamt = abs($payment['amount']);
                                                $amt = $payment['amount'];
                                                $trcolr = "";

                                                if ($source == 'PYOU') {
                                                    $url = "payout.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "payout_fr.php?vouchno=$vouchno";
                                                    $desc = "Payment Out";

                                                    $mtransacdb = 'cashpay';

                                                    $getcash = mysqli_query($conn, "Select party as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $accode = $fetcash['accode'];

                                                    $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
                                                    $fetacc = mysqli_fetch_array($getacc);
                                                    $desc = $fetacc['name'];

                                                    //                                                        $trn2 = mysqli_query($conn, "SELECT SUM(`setoff`) AS `totadj`, nodays FROM `billsett` WHERE 
                                                    //                                                                                    `billno` = '$gdtl[vouchno]' AND 
                                                    //                                                                                    `party` = '$gdtl[party]' LIMIT 0,1 ") or die(mysqli_error($conn));
                                                    //
                                                    //                                                        $tranqry2 = mysqli_fetch_assoc($trn2);
                                                    //                                                        $totadj = $tranqry2["totadj"];
                                                    //
                                                    //                                                        if ($totadj != $gdtl['netamt']) {
                                                    //                                                            $trcolr = "color:red";
                                                    //                                                        }
                                                }


                                                if ($source == 'CP') {
                                                    $url = "cashpay.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "cashpay_fr.php?vouchno=$vouchno";
                                                    $desc = "Payment";

                                                    if ($grpcode == '002') {
                                                        $getcash = mysqli_query($conn, "Select party as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    } else {
                                                        $getcash = mysqli_query($conn, "Select bankname as accode from `cashpay` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                    }
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $accode = $fetcash['accode'];

                                                    $getacc = mysqli_query($conn, "Select name from `accmast` where `accode`='$accode' ") or die(mysqli_error($conn));
                                                    $fetacc = mysqli_fetch_array($getacc);
                                                    $desc = $fetacc['name'];
                                                }

                                                if ($source == 'TR') {
                                                    $url = "contra.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "contrec.php?vouchno=$vouchno";
                                                    $desc = "Contra Entry";

                                                    $getcash = mysqli_query($conn, "Select b.name from `contra` a, accmast b 
                                                                                       where a.`vouchno`='$vouchno' and a.tobc = b.accode ") or die(mysqli_error($conn));
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $desc = $fetcash['name'];
                                                }

                                                if ($source == 'JV') {
                                                    $url = "jventry.php?vouchno=$vouchno&mode=edit";
                                                    $url2 = "jventry.php?vouchno=$vouchno";
                                                    $desc = "JV Entry";

                                                    if ($amt > 0) {
                                                        $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.party = b.accode ") or die(mysqli_error($conn));
                                                    } else {
                                                        $getcash = mysqli_query($conn, "Select b.name from `jventry` a, accmast b where 
                                                                                           a.`vouchno`='$vouchno' and a.bankname = b.accode ") or die(mysqli_error($conn));
                                                    }
                                                    $fetcash = mysqli_fetch_array($getcash);
                                                    $desc = $fetcash['name'];
                                                }

                                                $ctr2++;
                                                echo "<tr style='$trcolr' class='clickable-row2' data-href='$url' data-target='_blank'>";
                                                echo "<td>$vouchdt</td>";
                                                echo "<td>$source</td>";
                                                echo "<td><a href='$url2' target='_blank'>$vouchno</a></td>";
                                                echo "<td>$desc</td>";
                                                echo "<td class='text-right'>$payamt</td>";
                                                echo "</tr>";

                                                $totpay = $totpay + $payamt;
                                            }

                                            if ($lcount < $rcount) {
                                                $ext = $rcount - $lcount;
                                                for ($i = 0; $i < $ext; $i++) {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td>&nbsp;</td>";
                                                    echo "<td></td>";
                                                    echo "</tr>";
                                                }
                                            }

                                            echo "<tr style='background:#95d5b2'>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td></td>";
                                            echo "<td class='text-right' style'font-size:12px !important;'>Total</td>";
                                            echo "<td class='text-right' style'font-size:12px !important;'><b>$totpay</b></td>";
                                            echo "</tr>";
                                            ?>
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                            <tr>


                                <table style="background: white;float:right" class="table  table-bordered" style="width:100%">

                                    <?php
                                    $diff = $totrecp - $totpay;

                                    if ($diff > 0) {
                                        $ctr2++;
                                    ?>

                                        <thead class="mySearchTr">
                                            <tr style="border:2px;background:#40916c">
                                                <th width="11%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="24%" class="text-right">&nbsp;</th>
                                                <th width="9%" class="text-right">&nbsp;</th>
                                                <th width="11%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="24%" class="text-right">Closing</th>
                                                <th width="9%" class="text-right"><b><?= $diff ?></b></th>
                                            </tr>
                                        </thead>

                                    <?php } else { ?>

                                        <thead class="mySearchTr">
                                            <tr style="border:2px;background:#40916c">
                                                <th width="11%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="24%" class="text-right">Closing</th>
                                                <th width="9%" class="text-right"><b><?= abs($diff) ?></b></th>
                                                <th width="11%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="3%">&nbsp;</th>
                                                <th width="24%">&nbsp;</th>
                                                <th width="9%" class="text-right">&nbsp;</th>
                                            </tr>
                                        </thead>
                                    <?php } ?>
                                </table>
                            </tr>

                        </table>
                    </div>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php") ?>

</body>

</html>
<?php
//}
//}



if (isset($_POST["excel"])) {
    $a = 1;

    $compcond = '';


    if (!empty($_POST['company'])) {
        foreach ($_POST['company'] as $check) {
            $mycheck[$count++] .= " OR `company` = '" . $check . "' ";
        }

        foreach ($mycheck as $g => $values) {
            $mcheck .= $values;
        }

        $compcond = " AND (`company` = '' " . $mcheck . " )";
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);
    $mname = trim($_POST["acname"]);

    $mfdt = $mfrmdate;
    $mtdt = $mtodate;

    if ($mfrmdate > $mtodate) {
        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger_new.php");
        exit();
        $a = 0;
    }

    $maccode = '';
    if ($mname == '') {
        browse("A/c. Name Cannot Be Empty");
        header("refresh: 1; ledger_new.php");
        exit();
        $a = 0;
    } else {
        $maccode = $mname;
    }

    if ($a == 1) {
        $recqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount > 0 and accode = '$maccode' and chqret != 'Y'
                                and date >= '$mfrmdate' and date <= '$mtodate' $compcond     
                                order by date, source, vouchno ") or die(mysqli_error($conn));
        $rcount = mysqli_num_rows($recqry);

        $payqry = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                where amount < 0 and accode = '$maccode' and chqret != 'Y'
                                and date >= '$mfrmdate' and date <= '$mtodate' $compcond     
                                order by date, source, vouchno ") or die(mysqli_error($conn));
        $lcount = mysqli_num_rows($payqry);

        $getopen = mysqli_query($conn, "SELECT grpcode, accode, name, openbal, dc  FROM `$maindb` WHERE `accode` = '$maccode' ") or die(mysqli_error($conn));

        $baltmp = mysqli_query($conn, "SELECT * FROM $zroot.`$accbal` WHERE accode = '$maccode' $compcond ") or die(mysqli_error($conn));

        $opb = 0;
        $opbt = 0;
        while ($gobal = mysqli_fetch_array($baltmp)) {
            $opdc = $gobal["dc"];

            if ($opdc == 'D') {
                $opb = $gobal["openbal"] * -1;
            } else {
                $opb = $gobal["openbal"];
            }
            $opbt = $opbt + $opb;
        }


        $ctr = 0;
        $totrecp = 0;
        $fetopen = mysqli_fetch_array($getopen);
        $maccode = $fetopen[accode];
        $mname = $fetopen[name];

        $mopen = $opbt;

        $getacc = mysqli_query($conn, "SELECT SUM(amount) AS `openamt` FROM `$accdb` WHERE `accode` = '$maccode' and `date` < '$mfrmdate' and chqret != 'Y' $compcond ") or die(mysqli_error($conn));
        $fetacc = mysqli_fetch_array($getacc);
        $openacc = $fetacc[openamt];

        if ($openacc < 0) {
            $openacc = $openacc * -1;
        }
        $finopen = $mopen + $openacc;
        $finop = abs($finopen);

        $getacc2 = mysqli_query($conn, "SELECT SUM(amount) AS `openamt` FROM `$accdb` WHERE `accode` = '$maccode' and `date` <= '$mtodate' and chqret != 'Y' $compcond") or die(mysqli_error($conn));
        $fetacc2 = mysqli_fetch_array($getacc2);
        $openacc2 = $fetacc2[openamt];

        $finclos = $mopen + $openacc2;
        $fincl = abs($finclos);

        require("includes/dbconnect.php");
        mysqli_select_db($conn, $zdb);


        $contents .= "$mname,\n";
        $contents .= "Bills,\n";
        $contents .= "Date,Source,Voucher,Amount\n";



        $lop = 0;
        if ($finopen <= 0) {
            $ctr++;

            $contents .= ",";
            $contents .= ",";
            $contents .= "Opening" . ",";
            $contents .= $finop . "\n";
            $lop = 1;
            $totpay = $totpay + $finop;
        }
        if ($finopen > 0) {

            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= "\n";
        }

        $ctr2 = 0;
        $totpay = 0;
        while ($payment = mysqli_fetch_array($payqry)) {
            $source = $payment['source'];
            $vouchno = $payment['vouchno'];
            $vouchdt = $payment['vouchdt'];
            $payamt = abs($payment['amount']);


            $ctr2++;
            $contents .= $vouchdt . ",";
            $contents .= $source . ",";
            $contents .= $vouchno . ",";
            $contents .= $payamt . "\n";

            $totpay = $totpay + $payamt;
        }

        if ($lcount < $rcount) {
            $ext = $rcount - $lcount;
            for ($i = 0; $i < $ext; $i++) {
                $contents .= ",";
                $contents .= ",";
                $contents .= ",";
                $contents .= "\n";
            }
        }
        $contents .= ",";
        $contents .= ",";
        $contents .= "Total" . ",";
        $contents .= $totpay . "\n";

        if ($finclos > 0) {
            $ctr2++;

            $contents .= ",";
            $contents .= ",";
            $contents .= "Closing" . ",";
            $contents .= "$fincl" . "\n";
        } else {
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= "\n";
        }






        $contents .= "Receipts\n";
        $contents .= "Date, Source, Voucher, Amount\n";

        if ($finopen > 0) {
            $ctr++;
            $contents .= ",";
            $contents .= ",";
            $contents .= "Opening" . ",";
            $contents .= $finopen . "\n";

            $totrecp = $totrecp + $finopen;
        } else {
            if ($lop == 1) {
                $contents .= ",";
                $contents .= ",";
                $contents .= ",";
                $contents .= "\n";
            }
        }
        $ctr = 0;
        while ($receipt = mysqli_fetch_array($recqry)) {
            $source = $receipt[source];
            $vouchno = $receipt[vouchno];
            $vouchdt = $receipt[vouchdt];
            $recamt = abs($receipt[amount]);

            if ($source == 'BR') {
                $getbank = mysqli_query($conn, "Select date_format(recvdate, '%d-%m-%Y') as recvdate from `bankrecp$ztit` where `vouchno`='$vouchno' $compcond ") or die(mysqli_error($conn));
                $fetbank = mysqli_fetch_array($getbank);
                $vouchdt = $fetbank['recvdate'];
            }
            if ($source == 'CR') {
                $getcash = mysqli_query($conn, "Select date_format(recvdate, '%d-%m-%Y') as recvdate from `cashrecp$ztit` where `vouchno`='$vouchno' $compcond ") or die(mysqli_error($conn));
                $fetcash = mysqli_fetch_array($getcash);
                $vouchdt = $fetcash['recvdate'];
            }


            $ctr++;

            $contents .= $vouchdt . ",";
            $contents .= $source . ",";
            $contents .= $vouchno . ",";
            $contents .= $recamt . "\n";

            $totrecp = $totrecp + $recamt;
        }

        if ($rcount < $lcount) {
            $ext = $lcount - $rcount;
            for ($i = 0; $i < $ext; $i++) {
                $contents .= ",";
                $contents .= ",";
                $contents .= ",";
                $contents .= "\n";
            }
        }
        $contents .= ",";
        $contents .= ",";
        $contents .= "Total" . ",";
        $contents .= $totrecp . "\n";


        if ($finclos <= 0) {
            $ctr2++;
            $contents .= ",";
            $contents .= ",";
            $contents .= "Closing" . ",";
            $contents .= $fincl . "\n";
        } else {
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $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;
    }
}
?>