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

$maindb = "actran";

$formname = "Ledger Banya Register";
$file = "ledgerbanya_fr.php";

$zroot = 'cepheisy_msp_root';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$party = $_POST["party"];

$dtypecond = "";
$mscopecond = "";

$mfrmdate = $_POST["fdate"];
$mtodate = $_POST["tdate"];

if (!empty($party)) {
    $partycond = " AND `accode`='$party' ";
}
//    if (!empty($itemname)) {
//        $itemnamecond = " AND `itemname `='$itemname ' ";
//    }

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; inweg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; inwreg.php");
}

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$maccode = $party;

$getpartyname = mysqli_query($conn, "SELECT `name`, openbal, dc FROM `accmast` WHERE `accode`='$maccode'") or die(mysqli_error($conn));
$zname = mysqli_fetch_object($getpartyname);
$mname = $zname->name;
$mopbal = $zname->openbal;
$mopdc = $zname->dc;

if ($mopdc == "D") {
    $mopbal = $mopbal * - 1;
}

$getbal = mysqli_query($conn, "SELECT SUM(amount) as oamt FROM `actran` WHERE accode='$party' AND  date < '$mfrmdate'") or die(mysqli_error($conn));
$getamt = mysqli_fetch_array($getbal);
$oamt = $getamt["oamt"];

$mobal = $mopbal + $oamt;


$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `date` >= '$mfrmdate' 
                                        AND `date` <= '$mtodate' and accode = '$party' order by date, vouchno, source desc ")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                       
                     <?php
                     echo "<td style='text-align:left;' >$mname</td>";
                     ?>
                    
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr</th>
                                <th style="width:8%">Date</th>
                                <th style="width:3%">Source</th>
                                <th style="width:6%">Voucher</th>
                                <th style="width:15%">Transaction</th>
                                <th style="width:30%">Description</th>
                                <th style="width:26%">Remarks</th>
                                <th style="width:9%" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $gtot = 0;

                            $vdate1 = strtotime($mfrmdate);
                            $vdate = date('d-m-Y', $vdate1);

                            $mdescrip = "Opening Balance";
 
                            $ctr++;
                            Echo "<tr id='boldfont'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$vdate</td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td>$mdescrip</td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'>" . number_format($mobal, 2) . "</td>";
                            Echo "</tr>";

                            $gtot = $gtot + $mobal;

                            Echo "<tr id='boldfont'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'>Balance</td>";
                            Echo "<td style='border-top:1px solid black; background-color:lightyellow' class='text-right'>" . number_format($gtot, 2) . "</td>";
                            Echo "</tr>";


                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $amount  = $pmain['amount'];
                                $msource = $pmain['source'];
                                $mvouch  = $pmain['vouchno'];

                                $getname = mysqli_query($conn, "SELECT accode,name FROM `accmast` where accode='$pmain[accode]' order by name")or die(mysqli_error($conn));
                                $fetname = mysqli_fetch_array($getname);

                                switch ($msource) {
                                    case "PUM";

                                        $head = "Purchase Material";
                                        $url = "purch.php?vouchno=$mvouch&mode=edit";
                                        $totdiscprov = $totdiscprov + $mamount;

                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "PU";
                                        $head = "Purchase";
                                        $url = "purch.php?vouchno=$mvouch&mode=edit";
                                        $totdiscprov = $totdiscprov + $mamount;

                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["billno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "PR";
                                        $head = "Purchase Return";
                                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
                                        $totdiscprov = $totdiscprov + $mamount;
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "REPG";
                                        $head = "Repair Given";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `repgivmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "SL";
                                        $head = "Sale";
//                                                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                             `remark1`, `remark2`, lesother, gstamt FROM `salemain`
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["invno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mdisc = $nqry["lesother"];
                                        $mgstamt = $nqry["gstamt"];
                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
//                                                                    }
                                        break;
                                    case "SLM";
                                        $head = "Sale / Material Given";
//                                                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                             `remark1`, `remark2`, lesother FROM `rawmain`
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["invno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mdisc = $nqry["lesother"];
                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
//                                                                    }
                                        break;

                                    case "SLTX";
                                        $head = "Sale - GST";
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        break;

                                    case "PUTX";
                                        $head = "Purchase - GST";
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        break;

                                    case "BRBP";
                                        $head = "Transfer - Customer to Karigar";
                                        $nqry11 = mysqli_query($conn, "SELECT party, karigar,acnum, remark1, remark2 FROM `custtokari`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $n1qry = mysqli_fetch_array($nqry11);
                                        $party = $n1qry["party"];
                                        $karigar = $n1qry["karigar"];
                                        $acnum = $n1qry["acnum"];
                                        if ($mgrpcode == '010') {
                                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$karigar'") or die(mysqli_error($conn));
                                        } else {
                                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$party'") or die(mysqli_error($conn));
                                        }

                                        $fetname = mysqli_fetch_array($getname);
                                        $showname = $fetname['name'];
                                        $head = $head . '( ' . $showname . '-' . $acnum . ' )';
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        $mnarr = trim($nlqry["remark1"]) . " " . trim($nlqry["remark2"]);
                                        
                                        break;

                                    case "SR";
                                        $head = "Sale Return";
                                        break;
                                    case "BR";
                                        $head = "Bank Receipt";
                                        $url = "bankrecp.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                                 remark1, remark2  FROM `bankrecp`
                                                                                 WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["chqno"];
                                            $mbilldt = $nqry["mbilldate"];
//                                                                        $mnarr = $nqry["narration"];

                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "BP";
                                        $head = "Bank Payment";
                                        $url = "bankpay.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate
                                                                        FROM `bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["chqno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                            $mchqdtl = "Bill No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "CR";
                                        $head = "Cash Receipt";
                                        $url = "cashrecp.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT * FROM `cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        }
                                        break;

                                    case "CP";
                                        $head = "Cash Payment";
                                        $url = "cashpay.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT * FROM `cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        }
                                        break;

                                    case "TR";
                                        $head = "Contra / Transfer";
                                        $url = "contra.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = $nqry["narration"];
                                        }
                                        break;

                                    case "JV";
                                        $head = "Journal Voucher";
                                        break;

                                    case "DG";
                                        $head = "Discount Given";
                                        break;

                                    case "DR";
                                        $head = "Discount Received";
                                        break;

                                    default :

                                        $head = "Other";
                                }


                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$msource</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$head</td>";
                                Echo "<td>$mchqdtl</td>";
                                Echo "<td>$mnarr</td>";
                                Echo "<td class='text-right'>" . number_format($amount, 2) . "</td>";
                                Echo "</tr>";

                                $gtot = $gtot + $amount;

                                Echo "<tr id='boldfont'>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td class='text-right'>Balance</td>";
                                Echo "<td style='border-top:1px solid black; background-color:lightyellow' class='text-right'>" . number_format($gtot, 2) . "</td>";
                                Echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

                       
                    $contents .= "$mname\n";
                     
                    
                               $contents .= "Sr,Date,Source,Voucher,Transaction,Description,Remarks,Amount\n";     
                             
                            $ctr = 0;
                            $totamt = 0;
                            $gtot = 0;

                            $vdate1 = strtotime($mfrmdate);
                            $vdate = date('d-m-Y', $vdate1);

                            $mdescrip = "Opening Balance";
 
                            
                            $ctr++;
                            
                            $contents .= "$ctr" . ",";
                            $contents .= "$vdate" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "$mdescrip" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "$mobal" . "\n";
                            
                         

                            $gtot = $gtot + $mobal;

                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Balance" . ",";
                            $contents .= "$gtot" . "\n";                           


                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $amount  = $pmain['amount'];
                                $msource = $pmain['source'];
                                $mvouch  = $pmain['vouchno'];

                                $getname = mysqli_query($conn, "SELECT accode,name FROM `accmast` where accode='$pmain[accode]' order by name")or die(mysqli_error($conn));
                                $fetname = mysqli_fetch_array($getname);

                                switch ($msource) {
                                    case "PUM";

                                        $head = "Purchase Material";
                                        $url = "purch.php?vouchno=$mvouch&mode=edit";
                                        $totdiscprov = $totdiscprov + $mamount;

                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "PU";
                                        $head = "Purchase";
                                        $url = "purch.php?vouchno=$mvouch&mode=edit";
                                        $totdiscprov = $totdiscprov + $mamount;

                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["billno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "PR";
                                        $head = "Purchase Return";
                                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
                                        $totdiscprov = $totdiscprov + $mamount;
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }

                                        break;
                                    case "REPG";
                                        $head = "Repair Given";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `repgivmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["invno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "SL";
                                        $head = "Sale";
//                                                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                             `remark1`, `remark2`, lesother, gstamt FROM `salemain`
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["invno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mdisc = $nqry["lesother"];
                                        $mgstamt = $nqry["gstamt"];
                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
//                                                                    }
                                        break;
                                    case "SLM";
                                        $head = "Sale / Material Given";
//                                                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                             `remark1`, `remark2`, lesother FROM `rawmain`
                                                                              WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["invno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mdisc = $nqry["lesother"];
                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
//                                                                    }
                                        break;

                                    case "SLTX";
                                        $head = "Sale - GST";
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        break;

                                    case "PUTX";
                                        $head = "Purchase - GST";
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        break;

                                    case "BRBP";
                                        $head = "Transfer - Customer to Karigar";
                                        $nqry11 = mysqli_query($conn, "SELECT party, karigar,acnum, remark1, remark2 FROM `custtokari`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $n1qry = mysqli_fetch_array($nqry11);
                                        $party = $n1qry["party"];
                                        $karigar = $n1qry["karigar"];
                                        $acnum = $n1qry["acnum"];
                                        if ($mgrpcode == '010') {
                                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$karigar'") or die(mysqli_error($conn));
                                        } else {
                                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$party'") or die(mysqli_error($conn));
                                        }

                                        $fetname = mysqli_fetch_array($getname);
                                        $showname = $fetname['name'];
                                        $head = $head . '( ' . $showname . '-' . $acnum . ' )';
                                        if ($prnnarr == 1) {
                                            $mnarr = "";
                                            $mchqdtl = "";
                                        }
                                        $mnarr = trim($nlqry["remark1"]) . " " . trim($nlqry["remark2"]);
                                        
                                        break;

                                    case "SR";
                                        $head = "Sale Return";
                                        break;
                                    case "BR";
                                        $head = "Bank Receipt";
                                        $url = "bankrecp.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                                 remark1, remark2  FROM `bankrecp`
                                                                                 WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["chqno"];
                                            $mbilldt = $nqry["mbilldate"];
//                                                                        $mnarr = $nqry["narration"];

                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "BP";
                                        $head = "Bank Payment";
                                        $url = "bankpay.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate
                                                                        FROM `bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mbill = $nqry["chqno"];
                                            $mbilldt = $nqry["mbilldate"];
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                            $mchqdtl = "Bill No. : " . + trim($mbill) . "  Dated : " . $mbilldt;
                                        }
                                        break;

                                    case "CR";
                                        $head = "Cash Receipt";
                                        $url = "cashrecp.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT * FROM `cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        }
                                        break;

                                    case "CP";
                                        $head = "Cash Payment";
                                        $url = "cashpay.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT * FROM `cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);
                                        }
                                        break;

                                    case "TR";
                                        $head = "Contra / Transfer";
                                        $url = "contra.php?vouchno=$mvouch&method=ledger";
                                        if ($prnnarr == 1) {
                                            $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = $nqry["narration"];
                                        }
                                        break;

                                    case "JV";
                                        $head = "Journal Voucher";
                                        break;

                                    case "DG";
                                        $head = "Discount Given";
                                        break;

                                    case "DR";
                                        $head = "Discount Received";
                                        break;

                                    default :

                                        $head = "Other";
                                }


                                $ctr++;
                                
                                $contents .= "$ctr" . ",";
                                $contents .= "$pmain[mydt]" . ",";
                                $contents .= "$msource" . ",";
                                $contents .= "$pmain[vouchno]" . ",";
                                $contents .= "$head" . ",";
                                $contents .= "$mchqdtl" . ",";
                                $contents .= "$mnarr" . ",";
                                $contents .= "$amount" . "\n";
                               

                                $gtot = $gtot + $amount;

                                
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "Balance" . ",";
                                $contents .= "$gtot" . "\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;
    }
}
?>