<?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 = "COMPANY IN & OUT";

$grid = "comp_in_out.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 = '';
    $paytypecond = '';

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

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

        $rcount = mysqli_num_rows($payqry);
        
        
        $recfea = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                               where feamt < 0 and chqret != 'Y' and source = 'PYIN'
                                               and date >= '$mfrmdate' and date <= '$mtodate' and type = 'R'  
                                               order by date, source, vouchno ") or die(mysqli_error($conn));
        $lfeacount = mysqli_num_rows($recfea);

        $payfea = mysqli_query($conn, " SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') AS vouchdt  FROM `$accdb`
                                               where feamt < 0 and chqret != 'Y' and (source = 'PYOU' or source = 'TOAG')
                                               and date >= '$mfrmdate' and date <= '$mtodate'  and type = 'R'   
                                               order by date, source, vouchno ") or die(mysqli_error($conn));

        $rfeacount = mysqli_num_rows($payfea);

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


            mysqli_select_db($conn, $zdb);
            ?>

            <div class="container">
<!--                  for inr   -->
                <div class="row">
                    <div class="col-lg-6" style="width:100%;">
                        <div class="box">
                            <?php
                            include("includes/head_rep.php");
                            ?>
                            
                            <h4 style="color:blue" colspan="2"><b>INR</b></h4>

                            <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' ") 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'];
                                                    }

                                                    $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 ($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>$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">

                                                <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' ") 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'  ") or die(mysqli_error($conn));
                                                        } else {
                                                            $getcash = mysqli_query($conn, "Select bankname as accode from `cashpay` 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'];

//                                                        $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 == 'TOAG') {
                                                        $url = "agentin.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "agentin_fr.php?vouchno=$vouchno";
                                                        $desc = "To Agent";

                                                        if ($grpcode == '002') {
                                                            $getcash = mysqli_query($conn, "Select b.name from `agentin` a, accmast b 
                                                                                                   where a.`vouchno`='$vouchno' and a.agent = b.accode  ") or die(mysqli_error($conn));
                                                        } else {
                                                            $getcash = mysqli_query($conn, "Select b.name from `agentin` 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'];
                                                    }
                                                    

//                                                    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 ($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>$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:#93aebc">
                                                <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:#93aebc">
                                                <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>

                
                
                <!--   for rmb   -->
                
<!--                <div class="row">
                    <div class="col-lg-6" style="width:100%;">
                        <div class="box">
                            <?php
                            $totpay = $totrecp = $diff = $lop = 0 ;
                            ?>
                                                     
                            <h4 style="color:#963634;background-color:#e3f2e1;line-height: 1.7;height:30px" colspan="2"><b>RMB</b></h4>

                            <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 ($receiptfea = mysqli_fetch_array($recfea)) {
                                                    $source = $receiptfea['source'];
                                                    $vouchno = $receiptfea['vouchno'];
                                                    $vouchdt = $receiptfea['vouchdt'];
                                                    $grpcode = $receiptfea['grpcode'];
                                                    $recamt = abs($receiptfea['feamt']);
                                                    $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' ") 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'];
                                                    }

                                                    $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 ($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>$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">

                                                <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 ($paymentfea = mysqli_fetch_array($payfea)) {
                                                    $source = $paymentfea['source'];
                                                    $vouchno = $paymentfea['vouchno'];
                                                    $vouchdt = $paymentfea['vouchdt'];
                                                    $payamt = abs($paymentfea['feamt']);
                                                    $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' ") 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'  ") or die(mysqli_error($conn));
                                                        } else {
                                                            $getcash = mysqli_query($conn, "Select bankname as accode from `cashpay` 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'];

//                                                        $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 == 'TOAG') {
                                                        $url = "agentin.php?vouchno=$vouchno&mode=edit";
                                                        $url2 = "agentin_fr.php?vouchno=$vouchno";
                                                        $desc = "To Agent";

                                                        if ($grpcode == '002') {
                                                            $getcash = mysqli_query($conn, "Select b.name from `agentin` a, accmast b 
                                                                                                   where a.`vouchno`='$vouchno' and a.agent = b.accode  ") or die(mysqli_error($conn));
                                                        } else {
                                                            $getcash = mysqli_query($conn, "Select b.name from `agentin` 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'];

                                                    }

//                                                    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 ($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>$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:#93aebc">
                                                <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:#93aebc">
                                                <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>-->
                
<!--                //////// end of rmb-->

            </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;
    }
}
?>