<?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 = "NEW LEDGER";

$grid = "ledger_new.php";
$cust = "accmast";
$ztit = $_SESSION["tit_" . $dbname];
$zroot = $_SESSION["zdir"];
$maindb = $cust;
$accdb = "actran";
mysqli_select_db($conn, $zdb);
$fseries = $_SESSION["series"];
$keycond = "";
$abal = 'accmtn';
$accbal = $abal . $ztit;
$compcond = '';

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


    $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;
    }
    $paytypecond = '';
    if (!empty($_POST["paytype"])) {
        $paytype = trim($_POST["paytype"]);
        $paytypecond = "AND `paytype`='$partytype'";
    }


    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));

//        $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);
        $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' ")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;
                }
            }
        </style>
        </head>
        <body>
            <?php
            require("includes/dbconnect.php");


            mysqli_select_db($conn, $zdb);
            ?>

            <div class="container">
                <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">
                                                <tr style="border:2px">
                                                    <th colspan="5" style="font-size: 16px">In</th>
                                                </tr>

                                                <tr style="border:2px;background:#93aebc">
                                                    <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'>$finopen</td>";
                                                    echo "</tr>";

                                                    $totrecp = $totrecp + $finopen;
                                                    $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']);
                                                    $colr = "";
                                                    $acname = '';

                                                    if ($source == 'BR') {
                                                        $url = "bankrecp.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "bankrecp_fr.php?vouchno=$vouchno";

                                                        $getbank = mysqli_query($conn, "Select date_format(vouchdt, '%d-%m-%Y') as recvdate,chqno from `bankrecp` where `vouchno`='$vouchno' $compcond $keycond") or die(mysqli_error($conn));
                                                        $fetbank = mysqli_fetch_array($getbank);
                                                        $vouchdt = $fetbank['recvdate'];

                                                        $getclr = mysqli_query($conn, "SELECT `clrdate` FROM `$accdb` WHERE `vouchno` = '$vouchno' and `source`='BR' $keycond") OR die(mysqli_error($conn));
                                                        $fetclr = mysqli_fetch_array($getclr);
                                                        $clrdate = $fetclr['clrdate'];
                                                        if ($clrdate == '0000-00-00') {
                                                            $colr = 'color:red';
                                                        }
                                                    }
                                                    
                                                    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' ") or die(mysqli_error($conn));
                                                        } else {
                                                            $getcash = mysqli_query($conn, "SELECT bankname as accode FROM `cashrecp` WHERE `vouchno`='$vouchno' ") 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 == 'FRAG') {
                                                        $url = "agentout.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "agentout_fr.php?vouchno=$vouchno";
                                                        $desc = "Payment From Agent";

                                                        $getcash = mysqli_query($conn, "Select party as accode from `agentout` where `vouchno`='$vouchno' ") 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'];
                                                    }
                                                   

                                                    $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:#b3d4e5'>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td class='text-right'>Total</td>";
                                                echo "<td class='text-right'><b>$totrecp</b></td>";
                                                echo "</tr>";

                                                if ($finclos <= 0) {
                                                    $ctr2++;
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td style='color:red' class='text-right'><b>Closing</b></td>";
                                                    echo "<td style='color:red' class='text-right'><b>$fincl</b></td>";
                                                    echo "</tr>";
                                                } else {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td>&nbsp;</td>";
                                                    echo "<td></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">

                                                <tr style="border:2px">
                                                    <th colspan="6" style="font-size: 16px">Out</th>
                                                </tr>

                                                <tr style="border:2px;background:#93aebc">
                                                    <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']);
                                                    $trcolr = "";

                                                    if ($source == 'SL') {
                                                        $url = "sale.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "sale_print.php?vouchno=$vouchno&mode=edit";

                                                        $mtransacdb = 'salemain';
                                                        $gdetail = mysqli_query($conn, "SELECT * FROM `$mtransacdb` WHERE vouchno='$vouchno' $keycond") or die(mysqli_error($conn));
                                                        $gdtl = mysqli_fetch_array($gdetail);
                                                        $manualno = $gdtl['manualno'];
                                                        $lotno = $gdtl['totalqty'];
//                                                        browse($lotno);
                                                        $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 == 'PYOU') {
                                                        $url = "payout.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "payout_fr.php?vouchno=$vouchno";
                                                        $desc = "Payment Out";

                                                        $mtransacdb = 'cashpay';

                                                        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 == 'CARR') {
                                                        $url = "carrierfees.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "";
                                                        $desc = "Carries Fees " ;
                                                        $contno = '';

                                                        $mtransacdb = 'carrierfees';

                                                        $getcash = mysqli_query($conn, "Select containerno from `carrierfees` where `vouchno`='$vouchno' ") or die(mysqli_error($conn));
                                                        $fetcash = mysqli_fetch_array($getcash);
                                                        $contno = $fetcash['containerno'];
                                                        
                                                        if ($contno != '') {
                                                            $desc = "Carries Fees - Container No. - ". trim($contno) ;
                                                        }
                                                    }


                                                    $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:#b3d4e5'>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td></td>";
                                                echo "<td class='text-right'>Total</td>";
                                                echo "<td class='text-right'><b>$totpay</b></td>";
                                                echo "</tr>";

                                                if ($finclos > 0) {
                                                    $ctr2++;
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td style='color:blue' class='text-right'><b>Closing</b></td>";
                                                    echo "<td style='color:blue' class='text-right'><b>$fincl</b></td>";
                                                    echo "</tr>";
                                                } else {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td>&nbsp;</td>";
                                                    echo "<td></td>";
                                                    echo "</tr>";
                                                }
                                                ?>     
                                            </tbody>
                                        </table>
                                    </td>
                                </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;
    }
}

?>