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

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

$zyear = $_SESSION["tit_" . $dbname];

$formname = "JV Book";
$maindb = "jvmain";
$trandb = "jvtran";

//$mpara = $_GET["para"];

$bgrp = "001";
$msource1 = "JV";
//    $msource2 = "BP";

$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

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

if (isset($_POST["submit"])) {

    $acond = "";

    if (!empty($_POST["accode"])) {

        $code = trim($_POST["accode"]);
        $acond = "AND b.`accode`='$code'";
    }
    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

    if ($mfrmdate > $mtodate) {

        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; jvbook.php");
        exit();
    }
    $gstcond = "";
    if (!empty($_POST['gsttype'])) {
        if ($_POST['gsttype'] == 1) {
            $gstcond = "";
        } else if ($_POST['gsttype'] == 2) {
            $gstcond = "and gst = 'Y'";
        } else if ($_POST['gsttype'] == 3) {
            $gstcond = "and gst = 'N'";
        }
    }


    $repname = "JV Book";

    $acc = mysqli_query($conn, "SELECT a.*,b.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `jvtran` b WHERE a.`vouchdt` >= '$mfrmdate' and
                        a.`vouchdt`<='$mtodate' and a.`vouchno` = b.`vouchno` $gstcond $acond group by a.vouchno") or die(mysqli_error($conn));
    include("includes/linkshead.php");
?>

    <body>
        <div class="container">
            <br>
            <?php include("includes/head_rep2.php");
            ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered table-hover" width='100%' border="0">
                        <tr style="color:black;background-color:grey;">
                            <th style="width : 9%">Date</th>
                            <th style="width : 7%">Voucher</th>
                            <th style="width : 27%">Description</th>
                            <th style="text-align:right; width : 10%">Debit</th>
                            <th style="text-align:right; width : 10%">Credit</th>
                            <th style="text-align:right; width : 13%">Total</th>
                        </tr>
                        <tbody>
                            <?php
                            $totcredit = 0;
                            $totdebit = 0;
                            while ($row = mysqli_fetch_array($acc)) {
                                $vdate = $row['mydt'];
                                $vouchno = $row['vouchno'];
                                $amount = $row['amount'];
                                $rem1 = $row['rem1'];
                                $rem2 = $row['rem2'];
                                $descrip = $rem1 . " " . $rem2;

                                echo "<tr class='mySearchTr clickable-row2' data-href='jv.php?vouchno=$vouchno&mode=edit'>";
                                echo "<td>$vdate</td>";
                                echo "<td>$vouchno</td>";
                                echo "<td>$descrip</td>";
                                echo "<td>&nbsp;</td>";
                                echo "<td>&nbsp;</td>";
                                echo "<td class='text-right'>$amount</td>";
                                echo "</tr>";

                                $nqry1 = mysqli_query($conn, "SELECT * FROM `jvtran` WHERE vouchno='$vouchno' ") or die(mysqli_error($conn));
                                $totcredit = $totdebit =  0;
                                while ($nqry = mysqli_fetch_array($nqry1)) {
                                    $accode = $nqry['accode'];
                                    $debit = $nqry['debit'];
                                    $credit = $nqry['credit'];

                                    $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast` WHERE accode='$accode'") or die(mysqli_error($conn));
                                    $acfet = mysqli_fetch_array($acqry1);
                                    $acname = $acfet["name"];

                                    echo "<tr class=' clickable-row2' data-href='jv.php?vouchno=$vouchno&mode=edit'>";
                                    echo "<td>&nbsp;</td>";
                                    echo "<td>&nbsp;</td>";
                                    echo "<td>$acname</td>";
                                    echo "<td class='text-right'>$debit</td>";
                                    echo "<td class='text-right'>$credit</td>";
                                    echo "<td>&nbsp;</td>";
                                    echo "</tr>";

                                    $totcredit = $totcredit + $credit;
                                    $totdebit = $totdebit + $debit;
                                }
                                echo "<tr class='mySearchTr2'>";
                                echo "<td>&nbsp;</td>";
                                echo "<td>&nbsp;</td>";
                                echo "<td class='text-right' style='font-weight:bold;font-size:15px'>Total</td>";
                                echo "<td class='text-right' style='font-weight:bold;font-size:15px'>$totdebit</td>";
                                echo "<td class='text-right' style='font-weight:bold;font-size:15px'>$totcredit</td>";
                                echo "<td>&nbsp;</td>";
                                echo "</tr>";
                                echo "<tr style='height:20px'>";
                                echo "</tr>";
                            }

                            echo "<tr class='mySearchTr'>";
                            echo "<td>&nbsp;</td>";
                            echo "<td>&nbsp;</td>";
                            echo "<td class='text-right' style='font-weight:bold;font-size:15px'>Total</td>";
                            echo "<td class='text-right' style='font-weight:bold;font-size:15px'>$totdebit</td>";
                            echo "<td class='text-right' style='font-weight:bold;font-size:15px'>$totcredit</td>";
                            echo "<td>&nbsp;</td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <?php
    include("includes/linksfoot.php");
}
if (isset($_POST["excel"])) {

    $acond = "";

    if (!empty($_POST["accode"])) {

        $code = trim($_POST["accode"]);
        $acond = "AND b.`accode`='$code'";
    }
    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

    if ($mfrmdate > $mtodate) {

        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; jvbook.php");
        exit();
    }
    $gstcond = "";
    if (!empty($_POST['gsttype'])) {
        if ($_POST['gsttype'] == 1) {
            $gstcond = "";
        } else if ($_POST['gsttype'] == 2) {
            $gstcond = "and gst = 'Y'";
        } else if ($_POST['gsttype'] == 3) {
            $gstcond = "and gst = 'N'";
        }
    }


    $repname = "JV Book";

    $acc = mysqli_query($conn, "SELECT a.*,b.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `jvtran` b WHERE a.`vouchdt` >= '$mfrmdate' and
                        a.`vouchdt`<='$mtodate' and a.`vouchno` = b.`vouchno` $gstcond $acond group by a.vouchno") or die(mysqli_error($conn));

    $contents .= "Date,Voucher,Description,Debit,Credit,Total\n";

    $totcredit = 0;
    $totdebit = 0;
    while ($row = mysqli_fetch_array($acc)) {
        $vdate = $row['mydt'];
        $vouchno = $row['vouchno'];
        $amount = $row['amount'];
        $rem1 = $row['rem1'];
        $rem2 = $row['rem2'];
        $descrip = $rem1 . " " . $rem2;

        $contents .= $vdate . ",";
        $contents .= $vouchno . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= $amount . "\n";

        $nqry1 = mysqli_query($conn, "SELECT * FROM `jvtran` WHERE vouchno='$vouchno' ") or die(mysqli_error($conn));
        while ($nqry = mysqli_fetch_array($nqry1)) {
            $accode = $nqry['accode'];
            $debit = $nqry['debit'];
            $credit = $nqry['credit'];

            $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast` WHERE accode='$accode'") or die(mysqli_error($conn));
            $acfet = mysqli_fetch_array($acqry1);
            $acname = $acfet["name"];

            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= $acname . ",";
            $contents .= $debit . ",";
            $contents .= $credit . ",";
            $contents .= "" . "\n";

            $totcredit = $totcredit + $credit;
            $totdebit = $totdebit + $debit;
        }
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= $totdebit . ",";
    $contents .= $totcredit . ",";
    $contents .= "" . "\n";

    $contents = strip_tags($contents);

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

    $acond = "";

    if (!empty($_POST["name"])) {

        $code = trim($_POST["name"]);
        $acond = "AND `accode`='$code'";
    }
    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

    if ($mfrmdate > $mtodate) {

        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; jvbook.php");
        exit();
    }
    $prntype = $_POST["dtype"];
    $prnnarr = $_POST["prinarr"];

    if ($prntype == 1) {
        $repname = "JV Book";
    } elseif ($prntype == 2) {
        $repname = "Bank Book - Only Debits";
    } else {
        $repname = "Bank Book - Only Credits";
    }
    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));

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

    class pdf extends fpdf
    {

        function Footer()
        {

            //Position at 1.5 cm from bottom

            $this->SetY(-15);

            //Arial italic 8

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

            //Page number

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

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

    $pdf->AliasNbPages();

    $pdf->AddPage();
    mysqli_select_db($conn, $zdb);
    $totbal = 0;

    while ($getac = mysqli_fetch_array($acc)) {
        $totbal = 0;
        $dtot = 0;
        $ctot = 0;
        $maccode = $getac["accode"];
        $mbankinit = $getac["bankinit"];
        $macname = $getac["name"];
        $mopbal = $getac["openbal"];
        $mopdc = $getac["dc"];

        if ($mopdc == "D") {
            $mopbal = $mopbal * -1;
        }
        $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE
                                                                  trnno != '001' AND bankinit='$mbankinit' AND
                                                                  source = '$msource1' and 
                                                                  vouchno+source IN (SELECT vouchno+source FROM $trandb WHERE 
                                                                  accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  AND source = '$msource1')
                                                                  ORDER BY date, source, vouchno ") or die(mysqli_error($conn));

        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' AND 
                                                           date < '$mfrmdate' and retdate != '' and retdate != '0000-00-00'") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);

        $amt = $getamt["amt"];
        $mobal = $mopbal + $amt;
        $mdebit = 0;
        $mcredit = 0;

        if ($mobal < 0) {
            $mdc = "DR";
            $mdebit = abs($mobal);
            $dtot = $dtot + $mdebit;
        } else {
            $mdc = "CR";
            $mcredit = abs($mobal);
            $ctot = $ctot + $mcredit;
        }
        $vdate1 = strtotime($mfrmdate);
        $vdate = date('d-m-Y', $vdate1);
        $mvouch = "";
        $mdescrip = "Opening Balance";
        $totbal = $totbal + $mobal;

        if ($totbal < 0) {
            $mdctot = "DR";
        } else {
            $mdctot = "CR";
        }
        include("includes/head_rep_pdf.php");
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetTextColor(000, 000, 000);
        $pdf->SetFillColor(239, 228, 176);
        $pdf->Cell(278, 7, strtoupper($macname), 1, 0, 'L', 1);
        $pdf->SetFont('Arial', '', 1, 0);

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

        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetTextColor(000, 000, 000);
        $pdf->SetFillColor(204, 204, 204);
        $pdf->Cell(40, 7, "Date", 1, 0, '', 1);
        $pdf->Cell(28, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(90, 7, "Description", 1, 0, '', 1);
        $pdf->Cell(40, 7, "Debit", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "Credit", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "Balance", 1, 0, 'R', 1);
        $pdf->SetFont('Arial', '', 1, 0);

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

        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(40, 7, $vdate, 1);
        $pdf->Cell(28, 7, $mvouch, 1);
        $pdf->Cell(90, 7, $mdescrip, 1);
        if ($prntype == 1) {
            $pdf->Cell(40, 7, $mdebit, 1, 0, 'R');
            $pdf->Cell(40, 7, $mcredit, 1, 0, 'R');
            $pdf->Cell(40, 7, abs($totbal) . $mdctot, 1, 0, 'R');
            $pdf->Ln();
        } elseif ($prntype == 2) {
            $pdf->Cell(40, 7, $mdebit, 1, 0, 'R');
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Ln();
        } else {
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Cell(40, 7, $mcredit, 1, 0, 'R');
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Ln();
        }

        while ($row = mysqli_fetch_assoc($ctran1)) {

            $vdate = $row["mydt"];
            $msource = $row["source"];
            $mvouch = $row["vouchno"];
            $mamount = $row["amount"];
            $daccode = $row["accode"];
            $mdebit = 0;
            $mcredit = 0;

            if ($mamount < 0) {
                $mdc = "DR";
                $mdebit = abs($mamount);
                $dtot = $dtot + $mdebit;
            } else {
                $mdc = "CR";
                $mcredit = abs($mamount);
                $ctot = $ctot + $mcredit;
            }
            $totbal = $totbal + $mamount;

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }
            $mdescrip = "";
            $mchqdtl = "";
            $mnarr = "";

            $nqry1 = mysqli_query($conn, "SELECT name FROM `accmast` WHERE accode = '$daccode'") or die(mysqli_error($conn));
            $nqry = mysqli_fetch_array($nqry1);

            $head = $nqry["name"];

            switch ($msource) {

                case "JV";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1
                                                                        FROM `$bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["remark1"];
                        $mchqdtl = "Cheque No. : " . +trim($mbill) . "Dated : " . $mbilldt;
                    }
                    break;

                case "BP";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1 
                                                                         FROM `$bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["remark1"];
                        $mchqdtl = "Cheque No. : " . +trim($mbill) . "Dated : " . $mbilldt;
                    }
                    break;

                case "CR";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `remark1` FROM `$cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = '';
                        $mbilldt = '';
                        $mnarr = $nqry["remark1"];
                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                    }
                    break;

                case "CP";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `remark1` FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry[""];
                        $mbilldt = $nqry[""];
                        $mnarr = $nqry["remark1"];
                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                    }
                    break;

                case "TR";
                    $head = "Contra / Transfer";
                    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;
                default:
                    $head = "Other";
            }
            if ($prnnarr == 1) {

                $pdf->SetFont('Arial', '', 10);
                $pdf->Cell(40, 7, $vdate, 1);
                $pdf->Cell(28, 7, $msource . $mvouch, 1);
                $pdf->Cell(90, 7, $head, 1);
                if ($prntype == 1) {
                    $pdf->Cell(40, 7, $mdebit, 1);
                    $pdf->Cell(40, 7, $mcredit, 1);
                    $pdf->Cell(40, 7, abs($totbal) . $mdctot, 1);
                    $pdf->Ln();
                } elseif ($prntype == 2) {
                    $pdf->Cell(40, 7, $mdebit, 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Ln();
                } else {
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Cell(40, 7, $mcredit, 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Ln();
                }
            } else {
                $pdf->SetFont('Arial', '', 10);
                $pdf->Cell(40, 7, $vdate, 1);
                $pdf->Cell(28, 7, $msource . $mvouch, 1);
                $pdf->Cell(90, 7, $head, 1);
                if ($prntype == 1) {
                    $pdf->Cell(40, 7, $mdebit, 1);
                    $pdf->Cell(40, 7, $mcredit, 1);
                    $pdf->Cell(40, 7, abs($totbal) . $mdctot, 1);
                    $pdf->Ln();
                } elseif ($prntype == 2) {
                    $pdf->Cell(40, 7, $mdebit, 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Ln();
                } else {
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Cell(40, 7, $mcredit, 1);
                    $pdf->Cell(40, 7, " ", 1);
                    $pdf->Ln();
                }
                $pdf->SetFont('Arial', '', 10);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(28, 7, " ", 1);
                if (!empty($mchqdtl)) {
                    $pdf->Cell(90, 7, $mchqdtl, 1);
                }
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Ln();

                $pdf->SetFont('Arial', '', 10);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(28, 7, " ", 1);
                if (!empty($mnarr)) {
                    $pdf->Cell(90, 7, $mnarr, 1);
                }
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Cell(40, 7, " ", 1);
                $pdf->Ln();
            }
        }
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(40, 7, " ", 1);
        $pdf->Cell(28, 7, " ", 1);
        $pdf->Cell(90, 7, "Total", 1, 0, 'R');
        if ($prntype == 1) {
            $pdf->Cell(40, 7, $dtot, 1, 0, 'R');
            $pdf->Cell(40, 7, $ctot, 1, 0, 'R');
            if ($totbal < 0) {
                $pdf->Cell(40, 7, abs($totbal) . $mdctot, 1, 0, 'R');
                $pdf->Ln();
            } else {
                $pdf->Cell(40, 7, abs($totbal) . $mdctot, 1, 0, 'R');
                $pdf->Ln();
            }
        } elseif ($prntype == 2) {
            $pdf->Cell(40, 7, $dtot, 1, 0, 'R');
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Cell(40, 7, " ", 1, 0, 'R');
            $pdf->Ln();
        } else {
            $pdf->Cell(40, 7, " ", 1);
            $pdf->Cell(40, 7, $ctot, 1, 0, 'R');
            $pdf->Cell(40, 7, " ", 1, 0, 'R');
            $pdf->Ln();
        }
    }
    $pdf->Output("JV.pdf", "D");
}

if (isset($_POST["tally"])) {
    include("includes/head_excel_rep.php");

    $getexcel = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` 
                WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                ORDER BY vouchdt ASC") or die(mysqli_error($conn));

    // $contents = "Voucher Date,Voucher Type Name,Voucher Number,Reference No.,Reference Date,Ledger Name,Ledger Amount,Ledger Amount Dr/Cr,Voucher Narration,Created By ,Created On, Created At,Normal,Verify,Optional\n";
    $contents = "Sr., Voucher Date,Voucher Type Name,Voucher Number,Ledger Name,Ledger Amount,Ledger Amount Dr/Cr,Voucher Narration,Created By ,Created On, Created At,Normal,Verify,Optional\n";

    $sr = 1;
    $prevvouch = 0;
    while ($pmain = mysqli_fetch_array($getexcel)) {
        
        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='{$pmain['vouchno']}'") or die(mysqli_error($conn));

        while ($tran = mysqli_fetch_array($gettran)) {
            $newvouch = $pmain['vouchno'];
            $amount = 0;
            if ($prevvouch == $newvouch) {
                $vouchtype = '';
                $vouchno = '';
                $mydt = '';
                } else {
                    $vouchno = $newvouch;
                    $prevvouch = $newvouch;
                    $mydt = $pmain['mydt'];
                    $vouchtype = 'Journal';
                $newline = $contents .= "\n";
            }
            if ($tran['debit'] > 0) {
                $amount = $tran['debit'];
                $drcr = 'Dr';
            } else {
                $amount = $tran['credit'];
                $drcr = 'Cr';
            }

            $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where `accode` ='$tran[accode]'") or die(mysqli_error($conn));
            $showparty = mysqli_fetch_array($getparty);
            $party = $showparty['name'];

            $contents .= "{$sr},";
            $contents .= "{$mydt},";
            $contents .= "{$vouchtype},";
            $contents .= "{$vouchno},";
            $contents .= "{$showparty['name']},";
            $contents .= "{$amount},";
            $contents .= "{$drcr},";
            $contents .= "\n";
            $newline;

            $sr++;
        }
    }

    $contents = strip_tags($contents);
    header("Content-Disposition: attachment; filename = $formname" . "_Tally Formnat" . date('d-m-Y') . " .csv");
    print $contents;
}
    ?>