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

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

$file = 'purrawreg.php';

$supplier = $_POST["supplier"];
$p = $_GET["p"];
$godown = $_POST["godown"];
$ptype = $_POST["ptype"];
$pchtype = $_POST["pchtype"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

if (!empty($supplier)) {
    $suppcond = " AND `party` = '$supplier' ";
    $suppcond2 = " AND `accode` = '$supplier' ";
}
if (!empty($p)) {
    $pcond = " AND `party` = '$p' ";
}
if (!empty($godown)) {
    $godowncond = " AND `godown` = '$godown' ";
}
if (!empty($ptype)) {
    $ptypecond = " AND `paidby`= '$ptype'";
}

if ($ptype == "C") {
    $paytype = "CASH";
} else {
    $paytype = "CREDIT";
}

if ($_REQUEST['pchtype'] == "R") {
    $pctype = "Raw Material";
} elseif ($_REQUEST['pchtype'] == "P") {
    $pctype = "Packing Material";
} else {
    $pctype = "Other";
}
$formname = $pctype . " " . 'Purchase Register';

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}


if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
}

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

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

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
} else {
    $gstcond = "";
}

if (isset($_POST["view"]) || ($_GET["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="tablerep">
            <?
            if ($dtype == 3) {
                $purraw = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst3+totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt from rawmain 
                                      WHERE `mattype` = 'R' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ") or die(mysqli_error($conn));
                $fpurraw = mysqli_fetch_array($purraw);

                $purpack = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst3+totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt from rawmain 
                                      WHERE `mattype` = 'P' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ") or die(mysqli_error($conn));
                $fpurpack = mysqli_fetch_array($purpack);


                $puroth = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst3+totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt from puotmain 
                                      WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ") or die(mysqli_error($conn));
                $fpuroth = mysqli_fetch_array($puroth);


                $purexp = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt) AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst3+totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt from purexpmain 
                                      WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ") or die(mysqli_error($conn));
                $fpurexp = mysqli_fetch_array($purexp);


                $debitnoteg = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst3 + totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt  
                                      from debmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and credtype = 'G' ") or die(mysqli_error($conn));
                $fdebitnoteg = mysqli_fetch_array($debitnoteg);

                $debitnoteo = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                        SUM(totgst3 + totgst5+totgst12+totgst18+totgst28)  AS totgst ,SUM(billamt) AS netamt from debmain WHERE 
                                        `vouchdt` >= '$mfrmdate'  AND `vouchdt` <= '$mtodate' and credtype = 'O' ") or die(mysqli_error($conn));
                $fdebitnoteo = mysqli_fetch_array($debitnoteo);


                $totamt = $fpurraw['amt'] + $fpurpack['amt'] + $fpuroth['amt'] + $fpurexp['amt'];
                $totdiscamt = $fpurraw['discount'] + $fpurpack['discount'] + $fpuroth['discount'] + $fpurexp['discount'];
                $tottaxble = $fpurraw['taxable'] + $fpurpack['taxable'] + $fpuroth['taxable'] + $fpurexp['taxable'];
                $totgst = $fpurraw['totgst'] + $fpurpack['totgst'] + $fpuroth['totgst'] + $fpurexp['totgst'];

                $totnetamt = $fpurraw['netamt'] + $fpurpack['netamt'] + $fpuroth['netamt'] + $fpurexp['netamt'];

                $totretamt = $fdebitnoteg['amt'] + $fdebitnoteo['amt'];
                $totretdiscamt = $fdebitnoteg['discount'] + $fdebitnoteo['discoubt'];
                $totrettaxableamt = $fdebitnoteg['taxable'] + $fdebitnoteo['taxable'];
                $totretgst = $fdebitnoteg['totgst'] + $fdebitnoteo['totgst'];
                $totretnetamt = $fdebitnoteg['netamt'] + $fdebitnoteo['netamt'];

                $grtotamt = $totamt - $totretamt;
                $grtotdisc = $totdiscamt - $totretdiscamt;
                $grtottaxable = $tottaxble - $totrettaxableamt;
                $grtotgst = $totgst - $totretgst;
                $grtotnetamt = $totnetamt - $totretnetamt;
                ?>
                <div class="container" style="width:60%">
                    <?php include("includes/head_rep2.php");
                    ?>
                    <div class="row">
                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                            <tr>
                                <td colspan='7'><h5>PURCHASE</h5></td>
                            </tr>
                            <tr class="mySearchTr">
                                <th width="25%" class='text-right'></th>
                                <th width="10%" class='text-right'>Amount</th>
                                <th width="10%" class='text-right'>Discount</th>
                                <th width="15%" class='text-right'>Taxable Amount</th>
                                <th width="15%" class='text-right'>GST</th>
                                <th width="15%" class='text-right'>Bill Amt.</th>
                            </tr>
                            <tr>
                                <td>Purchase Raw Material</td>
                                <td class='text-right'><?= $fpurraw['amt']; ?></td>
                                <td class='text-right'><?= $fpurraw['discount'] ?></td>
                                <td class='text-right'><?= $fpurraw['taxable'] ?></td>
                                <td class='text-right'><?= $fpurraw['totgst'] ?></td>
                                <td class='text-right'><?= $fpurraw['netamt'] ?></td>
                            </tr>
                            <tr>
                                <td>Purchase Packing</td>
                                <td class='text-right'><?= $fpurpack['amt'] ?></td>
                                <td class='text-right'><?= $fpurpack['discount']; ?></td>
                                <td class='text-right'><?= $fpurpack['taxable']; ?></td>
                                <td class='text-right'><?= $fpurpack['totgst']; ?></td>
                                <td class='text-right'><?= $fpurpack['netamt']; ?></td>
                            </tr>
                            <tr>
                                <td>Purchase Other</td>
                                <td class='text-right'><?= $fpuroth['amt'] ?></td>
                                <td class='text-right'><?= $fpuroth['discount']; ?></td>
                                <td class='text-right'><?= $fpuroth['taxable']; ?></td>
                                <td class='text-right'><?= $fpuroth['totgst']; ?></td>
                                <td class='text-right'><?= $fpuroth['netamt']; ?></td>
                            </tr>
                            <tr>
                                <td>Purchase Expense</td>
                                <td class='text-right'><?= $fpurexp['amt']; ?></td>
                                <td class='text-right'><?= $fpurexp['discount'] ?></td>
                                <td class='text-right'><?= $fpurexp['taxable']; ?></td>
                                <td class='text-right'><?= $fpurexp['totgst'] ?></td>
                                <td class='text-right'><?= $fpurexp['netamt']; ?></td>
                            </tr>
                            <tr class="bold" style="color:#26A69A">
                                <td><b>Total Purchase</b></td>
                                <td class='text-right'><b><?= $totamt; ?></b></td>
                                <td class='text-right'><b><?= $totdiscamt ?></b></b></td>
                                <td class='text-right'><b><?= $tottaxble; ?></b></td>
                                <td class='text-right'><b><?= $totgst ?></b></td>
                                <td class='text-right'><b><?= $totnetamt; ?></b></td>
                            </tr>
                            <tr>
                                <td colspan='7'><h5>RETURNS</h5></td>
                            </tr>
                            <tr style="background-color:#26A69A">
                                <th class='text-right'></th>
                                <th class='text-right'>Amount</th>
                                <th class='text-right' >Discount</th>
                                <th class='text-right' >Taxable Amount</th>
                                <th class='text-right'>GST</th>
                                <th class='text-right'>Bill Amt.</th>
                            </tr>
                            <tr>
                                <td>Goods Return</td>
                                <td class='text-right'><?= $fdebitnoteg['amt'] ?></td>
                                <td class='text-right' ><?= $fdebitnoteg['discount'] ?></td>
                                <td class='text-right' ><?= $fdebitnoteg['taxable'] ?></td>
                                <td class='text-right'><?= $fdebitnoteg['totgst'] ?></td>
                                <td class='text-right'><?= $fdebitnoteg['netamt'] ?></td>
                            </tr>
                            <tr>
                                <td>Others</td>
                                <td class='text-right'><?= $fdebitnoteo['amt'] ?></td>
                                <td class='text-right' ><?= $fdebitnoteo['discount'] ?></td>
                                <td class='text-right'><?= $fdebitnoteo['taxable'] ?></td>
                                <td class='text-right'><?= $fdebitnoteo['totgst'] ?></td>
                                <td class='text-right'><?= $fdebitnoteo['netamt'] ?></td>
                            </tr>
                            <tr class="bold" style="color:red">
                                <td class="f17">Total Purchase Return</td>
                                <td class='text-right'><?= $totretamt; ?></td>
                                <td class='text-right' ><?= $totretdiscamt; ?></td>
                                <td class='text-right'><?= $totrettaxableamt; ?></td>
                                <td class='text-right'><?= $totretgst; ?></td>
                                <td class='text-right'><?= $totretnetamt; ?></td>
                            </tr>
                            <tr style="color:blue" class="bold">
                                <td style="width:15%">Total</td>
                                <td style="width:10%" class='text-right'><?= $grtotamt; ?></td>
                                <td style="width:10%" class='text-right'><?= $grtotdisc; ?></td>
                                <td style="width:15%;color: #ff00cc;" class='text-right' ><?= $grtottaxable; ?></td>
                                <td style="width:15%" class='text-right'><?= $grtotgst; ?></td>
                                <td style="width:15%" class='text-right'><?= $grtotnetamt; ?></td>
                            </tr>
                        </table>
                    </div>
                </div>


            <? } else {
                ?>
                <div class="container-fluid">
                    <?php include("includes/head_rep2.php"); ?>
                    <div class="row">
                        <?
                        if ($pchtype == "" || $pchtype == "R") {

                            $getpmain1 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' and mattype = 'R' $suppcond $godowncond $ptypecond $pchtypecond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));

                            $getno = mysqli_num_rows($getpmain1);

                            if ($getno > 0) {
                                ?>
                                <div class="row">
                                    <div class="col-lg-12 pad">
                                        <table class="table table-condesed table-bordered">
                                            <tr class="mySearchTr">
                                                <th width="100%" class="h4" style="color:white"><b>Raw Material - Purchase Register</b></th>
                                            </tr>
                                            <tr>
                                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno; ?></b>
                                                </td>
                                            </tr>
                                        </table>
                                        <?php if ($ptype != "") { ?>
                                            <table width="100%" class="table" style="background-color: #98F9F0">
                                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                                            </table>
                                        <?php } ?>
                                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th width="2%;">Sr</th>
                                                    <th width="4%;">Voucher</th>
                                                    <th width="5%;">Inv. No.</th>
                                                    <th width="6%;">Date</th>
                                                    <!--<th width="5%;">Godown</th>-->
                                                    <th width="19%;">Party Name</th>                                
                                                    <th width="8%;">GST No.</th>                                
                                                    <?php if ($ptype != "") { ?>
                                                        <th width="7%;">Pay type</th>
                                                    <?php } ?>
                                                    <th width="6%;" class="text-right">Sub Total</th>
                                                    <th width="5%;" class="text-right">Disc Amt.</th>
                                                    <th width="6%;" class="text-right">Taxable</th>
                                                    <th width="5%;" class="text-right">GST 3%</th>
                                                    <th width="5%;" class="text-right">GST 5%</th>
                                                    <th width="5%;" class="text-right">GST 12%</th>
                                                    <th width="5%;" class="text-right">GST 18%</th>
                                                    <th width="5%;" class="text-right">GST 28%</th>
                                                    <th width="5%;" class="text-right">R.OFF</th>
                                                    <th width="8%;" class="text-right">Net Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $ctr = 0;
                                                $totamt = 0;
                                                $totdiscamt = 0;
                                                $rtottaxable = 0;
                                                $rtotgst3 = 0;
                                                $rtotgst5 = 0;
                                                $rtotgst12 = 0;
                                                $rtotgst18 = 0;
                                                $rtotgst28 = 0;
                                                $rroundoff = 0;
                                                $rtotpackamt = $rtotstamt = $rtotothamt = $rtotbillamt = 0;
                                                $pchtypecond = " AND `mattype`= 'R'";

                                                $getpmain_r = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond $pchtypecond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                                                while ($pmain = mysqli_fetch_array($getpmain_r)) {
                                                    if ($pmain['paidby'] == "C") {
                                                        $ptyped = "CASH";
                                                    } else {
                                                        $ptyped = "CREDIT";
                                                    }


                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);

                                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
                                                    $trow = mysqli_fetch_array($mown);

                                                    $voucher = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $party = $sparty['name'];
                                                    $gstno = $sparty['gstno'];
                                                    $gst3 = $pmain['totgst3'];
                                                    $gst5 = $pmain['totgst5'];
                                                    $gst12 = $pmain['totgst12'];
                                                    $gst18 = $pmain['totgst18'];
                                                    $gst28 = $pmain['totgst28'];
                                                    $roundoff = $pmain['roundoff'];
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $taxable = $pmain['taxableamt'];

                                                    $ctr++;
                                                    Echo"<tr data-href='purchase_mi.php?mode=edit&vouchno=$voucher&type=R' class='clickable-row2' [target]='_blank'>";
                                                    Echo"<td>$ctr</td>";
                                                    Echo"<td>$voucher</td>";
                                                    Echo"<td>$invno</td>";
                                                    Echo"<td>$mydt</td>";
//                        Echo"<td>$gcode</td>";
                                                    Echo"<td>$party</td>";
                                                    Echo"<td>$gstno</td>";
                                                    if ($ptype != "") {
                                                        Echo"<td>$ptyped</td>";
                                                    }
                                                    Echo"<td class='text-right'>$subtot</td>";
                                                    Echo"<td class='text-right'>$totdisc</td>";
                                                    Echo"<td class='text-right'>$taxable</td>";
                                                    Echo"<td class='text-right'>$gst3</td>";
                                                    Echo"<td class='text-right'>$gst5</td>";
                                                    Echo"<td class='text-right'>$gst12</td>";
                                                    Echo"<td class='text-right'>$gst18</td>";
                                                    Echo"<td class='text-right'>$gst28</td>";
                                                    Echo"<td class='text-right'>$roundoff</td>";
                                                    Echo"<td class='text-right'>$billamt</td>";
                                                    Echo"</tr>";


                                                    $rgrsubtot = $rgrsubtot + $subtot;
                                                    $rgrtotdis = $rgrtotdis + $totdisc;
                                                    $rgrtottaxable = $rgrtottaxable + $taxable;
                                                    $rgrtotgst3 = $rgrtotgst3 + $gst3;
                                                    $rgrtotgst5 = $rgrtotgst5 + $gst5;
                                                    $rgrtotgst12 = $rgrtotgst12 + $gst12;
                                                    $rgrtotgst18 = $rgrtotgst18 + $gst18;
                                                    $rgrtotgst28 = $rgrtotgst28 + $gst28;
                                                    $rgrroundoff = $rgrroundoff + $roundoff;
                                                    $rgrtotbillamt = $rgrtotbillamt + $billamt;

                                                    $rtotpackamt = $rtotpackamt + $pmain['packamt'];
                                                    $rtotstamt = $rtotstamt + $pmain['stamt'];
                                                    $rtotothamt = $rtotothamt + $pmain['otheramt'];
                                                    $rtotbillamt = $rtotbillamt + $pmain['billamt'];


                                                    if ($_POST["dtype"] == 2) {

                                                        Echo "<tr style='height:10px;'>";
                                                        Echo "<td colspan='25'>";
                                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                                        Echo "<thead>";
                                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                                        Echo "<th style='width:5%;'>Sr</th>";
                                                        Echo "<th style='width:15%;' class=''>Item Name</th>";
                                                        Echo "<th style='width:7%;' class=''>HSN Code</th>";
                                                        Echo "<th style='width:5%;' class=''>Unit</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Disc%</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Disc. Amt.</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 1) {
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        }
                                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                                        Echo "</tr>";
                                                        Echo "<thead>";
                                                        Echo "<tr>";
//
//
                                                        $ctr2 = 0;
                                                        $rtotqty = 0;
                                                        $rtotrate = 0;
                                                        $rtotamount = $rtotdiscount = $rtotsubtot = $rtotcgstamt = $rtotsgstamt = $rtotigstamt = 0;

                                                        $gettran = mysqli_query($conn, "SELECT * FROM `rawtran` WHERE `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                                        while ($tran = mysqli_fetch_array($gettran)) {

                                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                                            $showitem = mysqli_fetch_array($getitem);

                                                            $ctr2++;
                                                            Echo "<tr>";
                                                            Echo "<td>$ctr2</td>";
                                                            Echo "<td>$tran[itemname]</td>";
                                                            Echo "<td>$tran[hsncode]</td>";
                                                            Echo "<td>$tran[unit]</td>";
                                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "<td class='text-right'>$tran[discperc]</td>";
                                                            Echo "<td class='text-right'>$tran[discamt]</td>";
                                                            Echo "<td class='text-right'>$tran[subtot]</td>";
                                                            if ($gsttype == 1) {
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            } elseif ($gsttype == 2) {
                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                            } elseif ($gsttype == '') {

                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            }
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "</tr>";

                                                            $rtotqty = $rtotqty + $tran['qty'];
                                                            $rtotamount = $rtotamount + $tran['amount'];
                                                            $rtotdiscount = $rtotdiscount + $tran['discamt'];
                                                            $rtotsubtot = $rtotsubtot + $tran['subtot'];
                                                            $rtotcgstamt = $rtotcgstamt + $tran['cgstamt'];
                                                            $rtotsgstamt = $rtotsgstamt + $tran['sgstamt'];
                                                            $rtotigstamt = $rtotigstamt + $tran['igstamt'];
                                                        }

                                                        Echo "<tr>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td>Total</td>";
                                                        Echo "<td class='text-right'>$rtotqty</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$rtotamount</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$rtotdiscount</td>";
                                                        Echo "<td class='text-right'>$rtotsubtot</td>";
                                                        if ($gsttype == 1) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotigstamt</td>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotsgstamt</td>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotsgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$rtotigstamt</td>";
                                                        }
                                                        Echo "<td class='text-right'>$rtotamount</td>";
                                                        Echo "</tr>";
                                                        Echo "</table>";
                                                    }
                                                }
                                                Echo "<tr style='background-color: #98F9F0'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
//                        Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                if ($ptype != "") {
                                                    Echo "<td></td>";
                                                }
                                                Echo "<td class='text-right'><b>Total</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrsubtot</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotdis</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtottaxable</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotgst3</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotgst5</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotgst12</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotgst18</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotgst28</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrroundoff</b></td>";
                                                Echo "<td class='numeric text-right'><b>$rgrtotbillamt</b></td>";
                                                Echo "</tr>";
                                                ?>
                                            </tbody>
                                        </table>
                                    </div> 
                                </div>
                                <?php
                            }
                        }

                        if ($pchtype == "" || $pchtype == "P") {

                            $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' and mattype = 'P' $suppcond $godowncond $ptypecond  $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                            $getno2 = mysqli_num_rows($getpmain2);


                            if ($getno2 > 0) {
                                ?>
                                <div class="row">
                                    <div class="col-lg-12 pad">
                                        <table class="table table-condesed table-bordered">
                                            <tr class="mySearchTr">
                                                <!--9221292520-->
                                                <th width="100%" class="h4" style="color:white"><b>Packing Material - Purchase Register</b></th>
                                            </tr>
                                            <tr>
                                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                                </td>
                                            </tr>
                                        </table>
                                        <?php if ($ptype != "") { ?>
                                            <table width="100%" class="table" style="background-color: #98F9F0">
                                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                                            </table>
                                        <?php } ?>
                                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th width="3%;">Sr</th>
                                                    <th width="4%;">Voucher</th>
                                                    <th width="6%;">Invoice No.</th>
                                                    <th width="6%;">Date</th>
                                                    <th width="5%;">Godown</th>
                                                    <th width="19%;">Party Name</th>                                
                                                    <th width="8%;">GST NO.</th>                                
                                                    <?php if ($ptype != "") { ?>
                                                        <th width="7%;">Pay type</th>
                                                    <?php } ?>
                                                    <th width="6%;" class="text-right">Sub Total</th>
                                                    <th width="5%;" class="text-right">Disc Amt.</th>
                                                    <th width="5%;" class="text-right">Taxable</th>
                                                    <th width="5%;" class="text-right">GST 3%</th>
                                                    <th width="5%;" class="text-right">GST 5%</th>
                                                    <th width="5%;" class="text-right">GST 12%</th>
                                                    <th width="5%;" class="text-right">GST 18%</th>
                                                    <th width="5%;" class="text-right">GST 28%</th>
                                                    <th width="5%;" class="text-right">R.OFF</th>
                                                    <th width="7%;" class="text-right">Net Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $pctr = 0;
                                                $ptotamt = 0;
                                                $ptotdiscamt = 0;
                                                $ptottaxable = 0;
                                                $ptotgst3 = 0;
                                                $ptotgst5 = 0;
                                                $ptotgst12 = 0;
                                                $ptotgst18 = 0;
                                                $ptotgst28 = 0;
                                                $proundoff = 0;
                                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                                $pchtypecond = " AND `mattype`= 'P'";

                                                $getpmain_p = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond $pchtypecond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                                                while ($pmain = mysqli_fetch_array($getpmain_p)) {
                                                    if ($pmain['paidby'] == "C") {
                                                        $ptyped = "CASH";
                                                    } else {
                                                        $ptyped = "CREDIT";
                                                    }


                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);

                                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
                                                    $trow = mysqli_fetch_array($mown);

                                                    $voucher = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $party = $sparty['name'];
                                                    $gstno = $sparty['gstno'];
                                                    $gst3 = $pmain['totgst3'];
                                                    $gst5 = $pmain['totgst5'];
                                                    $gst12 = $pmain['totgst12'];
                                                    $gst18 = $pmain['totgst18'];
                                                    $gst28 = $pmain['totgst28'];
                                                    $roundoff = $pmain['roundoff'];
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $taxable = $pmain['taxableamt'];

                                                    $pctr++;
                                                    Echo"<tr data-href='purchase_mi.php?mode=edit&vouchno=$voucher&type=P' class='clickable-row2' [target]='_blank'>";
                                                    Echo"<td>$pctr</td>";
                                                    Echo"<td>$voucher</td>";
                                                    Echo"<td>$invno</td>";
                                                    Echo"<td>$mydt</td>";
                                                    Echo"<td>$gcode</td>";
                                                    Echo"<td>$party</td>";
                                                    Echo"<td>$gstno</td>";
                                                    if ($ptype != "") {
                                                        Echo"<td>$ptyped</td>";
                                                    }
                                                    Echo"<td class='text-right'>$subtot</td>";
                                                    Echo"<td class='text-right'>$totdisc</td>";
                                                    Echo"<td class='text-right'>$taxable</td>";
                                                    Echo"<td class='text-right'>$gst3</td>";
                                                    Echo"<td class='text-right'>$gst5</td>";
                                                    Echo"<td class='text-right'>$gst12</td>";
                                                    Echo"<td class='text-right'>$gst18</td>";
                                                    Echo"<td class='text-right'>$gst28</td>";
                                                    Echo"<td class='text-right'>$roundoff</td>";
                                                    Echo"<td class='text-right'>$billamt</td>";
                                                    Echo"</tr>";



                                                    $pgrsubtot = $pgrsubtot + $subtot;
                                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                                    $pgrtottaxable = $pgrtottaxable + $taxable;
                                                    $pgrtotgst3 = $pgrtotgst3 + $gst3;
                                                    $pgrtotgst5 = $pgrtotgst5 + $gst5;
                                                    $pgrtotgst12 = $pgrtotgst12 + $gst12;
                                                    $pgrtotgst18 = $pgrtotgst18 + $gst18;
                                                    $pgrtotgst28 = $pgrtotgst28 + $gst28;
                                                    $pgrroundoff = $pgrroundoff + $roundoff;
                                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                                    $ptotpackamt = $ptotpackamt + $pmain['packamt'];
                                                    $ptotstamt = $ptotstamt + $pmain['stamt'];
                                                    $ptotothamt = $ptotothamt + $pmain['otheramt'];
                                                    $ptotbillamt = $ptotbillamt + $pmain['billamt'];


                                                    if ($_POST["dtype"] == 2) {

                                                        Echo "<tr style='height:10px;'>";
                                                        Echo "<td colspan='25'>";
                                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                                        Echo "<thead>";
                                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                                        Echo "<th style='width:5%;'>Sr</th>";
                                                        Echo "<th style='width:15%;' class=''>Item Name</th>";
                                                        Echo "<th style='width:7%;' class=''>HSN Code</th>";
                                                        Echo "<th style='width:5%;' class=''>Unit</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Disc%</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Disc. Amt.</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 1) {
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        }
                                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                                        Echo "</tr>";
                                                        Echo "<thead>";
                                                        Echo "<tr>";
//
//
                                                        $pctr2 = 0;
                                                        $ptotqty = 0;
                                                        $ptotrate = 0;
                                                        $ptotamount = $ptotdiscount = $ptotsubtot = $ptotcgstamt = $ptotsgstamt = $ptotigstamt = 0;

                                                        $gettran = mysqli_query($conn, "SELECT * FROM `rawtran` WHERE `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                                        while ($tran = mysqli_fetch_array($gettran)) {

                                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                                            $showitem = mysqli_fetch_array($getitem);

                                                            $ctr2++;
                                                            Echo "<tr>";
                                                            Echo "<td>$ctr2</td>";
                                                            Echo "<td>$tran[itemname]</td>";
                                                            Echo "<td>$tran[hsncode]</td>";
                                                            Echo "<td>$tran[unit]</td>";
                                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "<td class='text-right'>$tran[discperc]</td>";
                                                            Echo "<td class='text-right'>$tran[discamt]</td>";
                                                            Echo "<td class='text-right'>$tran[subtot]</td>";
                                                            if ($gsttype == 1) {
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            } elseif ($gsttype == 2) {
                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                            } elseif ($gsttype == '') {

                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            }
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "</tr>";

                                                            $ptotqty = $ptotqty + $tran['qty'];
                                                            $ptotamount = $ptotamount + $tran['amount'];
                                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                                            $ptotsubtot = $ptotsubtot + $tran['subtot'];
                                                            $ptotcgstamt = $ptotcgstamt + $tran['cgstamt'];
                                                            $ptotsgstamt = $ptotsgstamt + $tran['sgstamt'];
                                                            $ptotigstamt = $ptotigstamt + $tran['igstamt'];
                                                        }

                                                        Echo "<tr>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td>Total</td>";
                                                        Echo "<td class='text-right'>$ptotqty</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$ptotamount</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$ptotdiscount</td>";
                                                        Echo "<td class='text-right'>$ptotsubtot</td>";
                                                        if ($gsttype == 1) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotigstamt</td>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotsgstamt</td>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotsgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ptotigstamt</td>";
                                                        }
                                                        Echo "<td class='text-right'>$ptotamount</td>";
                                                        Echo "</tr>";
                                                        Echo "</table>";
                                                    }
                                                }
                                                Echo "<tr style='background-color: #98F9F0'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                if ($ptype != "") {
                                                    Echo "<td></td>";
                                                }
                                                Echo "<td class='text-right'><b>Total</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrsubtot</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotdis</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtottaxable</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst3</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst5</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst12</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst18</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst28</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrroundoff</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                                Echo "</tr>";
                                                ?>
                                            </tbody>
                                        </table>
                                    </div> 
                                </div>

                                <?php
                            }
                        }

                        if ($pchtype == "" || $pchtype == "O") {

                            $getpmain3 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `puotmain` WHERE `vouchdt` >= '$mfrmdate'             
                                        AND `vouchdt` <= '$mtodate'  $suppcond $godowncond $ptypecond  $gstcond $pcond order by vouchdt,vouchno ")or die(mysqli_error($conn));

                            $getno3 = mysqli_num_rows($getpmain3);

                            if ($getno3 > 0) {
                                ?>

                                <div class="row">
                                    <div class="col-lg-12 pad">
                                        <table class="table table-condesed table-bordered">
                                            <tr class="mySearchTr">
                                                <!--9221292520-->
                                                <th width="100%" class="h4" style="color:white"><b>Other Material - Purchase Register</b></th>
                                            </tr>
                                            <tr>
                                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno3; ?></b>
                                                </td>
                                            </tr>
                                        </table>
                                        <?php if ($ptype != "") { ?>
                                            <table width="100%" class="table" style="background-color: #98F9F0">
                                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                                            </table>
                                        <?php } ?>
                                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th width="3%;">Sr</th>
                                                    <th width="4%;">Voucher</th>
                                                    <th width="6%;">Invoice NO.</th>
                                                    <th width="6%;">Date</th>
                                                    <th width="5%;">Godown</th>
                                                    <th width="19%;">Party Name</th>                               
                                                    <th width="8%;">GST No.</th>                               
                                                    <?php if ($ptype != "") { ?>
                                                        <th width="7%;">Pay type</th>
                                                    <?php } ?>
                                                    <th width="6%;" class="text-right">Sub Total</th>
                                                    <th width="5%;" class="text-right">Disc Amt.</th>
                                                    <th width="5%;" class="text-right">Taxable</th>
                                                    <th width="5%;" class="text-right">GST 3%</th>
                                                    <th width="5%;" class="text-right">GST 5%</th>
                                                    <th width="5%;" class="text-right">GST 12%</th>
                                                    <th width="5%;" class="text-right">GST 18%</th>
                                                    <th width="5%;" class="text-right">GST 28%</th>
                                                    <th width="5%;" class="text-right">R.OFF</th>
                                                    <th width="7%;" class="text-right">Net Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $octr = 0;
                                                $ototamt = 0;
                                                $ototdiscamt = 0;
                                                $otottaxable = 0;
                                                $ototgst3 = 0;
                                                $ototgst5 = 0;
                                                $ototgst12 = 0;
                                                $ototgst18 = 0;
                                                $ototgst28 = 0;
                                                $oroundoff = 0;
                                                $ototpackamt = $ototstamt = $ototothamt = $ototbillamt = 0;

                                                $getpmain_o = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `puotmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond  $gstcond $pcond order by vouchdt,vouchno ")or die(mysqli_error($conn));


                                                while ($pmain = mysqli_fetch_array($getpmain_o)) {
                                                    if ($pmain['paidby'] == "C") {
                                                        $ptyped = "CASH";
                                                    } else {
                                                        $ptyped = "CREDIT";
                                                    }


                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);

                                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
                                                    $trow = mysqli_fetch_array($mown);

                                                    $voucher = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $party = $sparty['name'];
                                                    $gstno = $sparty['gstno'];
                                                    if ($oms == 1) {
                                                        if ($pmain['igstrate'] == 3.00) {
                                                            $gst3 = $pmain['igstamt'];
                                                        }
                                                        if ($pmain['igstrate'] == 5.00) {
                                                            $gst5 = $pmain['igstamt'];
                                                        }
                                                        if ($pmain['igstrate'] == 12.00) {
                                                            $gst12 = $pmain['igstamt'];
                                                        }
                                                        if ($pmain['igstrate'] == 18.00) {
                                                            $gst18 = $pmain['igstamt'];
                                                        }
                                                        if ($pmain['igstrate'] == 28.00) {
                                                            $gst28 = $pmain['igstamt'];
                                                        }
                                                    } else {
                                                        if ($pmain['cgstrate'] == 0.00) {
                                                            $gst0 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                        if ($pmain['cgstrate'] == 1.50) {
                                                            $gst3 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                        if ($pmain['cgstrate'] == 2.50) {
                                                            $gst5 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                        if ($pmain['cgstrate'] == 6.00) {
                                                            $gst12 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                        if ($pmain['cgstrate'] == 9.00) {
                                                            $gst18 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                        if ($pmain['cgstrate'] == 14.00) {
                                                            $gst28 = $pmain['cgstamt'] + $pmain['sgstamt'];
                                                        }
                                                    }
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $taxable = $pmain['taxableamt'];

                                                    $octr++;
                                                    Echo"<tr>";
                                                    Echo"<td>$octr</td>";
                                                    Echo"<td>$voucher</td>";
                                                    Echo"<td>$invno</td>";
                                                    Echo"<td>$mydt</td>";
                                                    Echo"<td>$gcode</td>";
                                                    Echo"<td>$party</td>";
                                                    Echo"<td>$gstno</td>";
                                                    if ($ptype != "") {
                                                        Echo"<td>$ptyped</td>";
                                                    }
                                                    Echo"<td class='text-right'>$subtot</td>";
                                                    Echo"<td class='text-right'>$totdisc</td>";
                                                    Echo"<td class='text-right'>$taxable</td>";
                                                    Echo"<td class='text-right'>$gst3</td>";
                                                    Echo"<td class='text-right'>$gst5</td>";
                                                    Echo"<td class='text-right'>$gst12</td>";
                                                    Echo"<td class='text-right'>$gst18</td>";
                                                    Echo"<td class='text-right'>$gst28</td>";
                                                    Echo"<td class='text-right'>$roundoff</td>";
                                                    Echo"<td class='text-right'>$billamt</td>";
                                                    Echo"</tr>";



                                                    $ogrsubtot = $ogrsubtot + $subtot;
                                                    $ogrtotdis = $ogrtotdis + $totdisc;
                                                    $ogrtottaxable = $ogrtottaxable + $taxable;
                                                    $ogrtotgst3 = $ogrtotgst3 + $gst3;
                                                    $ogrtotgst5 = $ogrtotgst5 + $gst5;
                                                    $ogrtotgst12 = $ogrtotgst12 + $gst12;
                                                    $ogrtotgst18 = $ogrtotgst18 + $gst18;
                                                    $ogrtotgst28 = $ogrtotgst28 + $gst28;
                                                    $ogrroundoff = $ogrroundoff + $roundoff;
                                                    $ogrtotbillamt = $ogrtotbillamt + $billamt;

                                                    $ototpackamt = $ototpackamt + $pmain['packamt'];
                                                    $ototstamt = $ototstamt + $pmain['stamt'];
                                                    $ototothamt = $ototothamt + $pmain['otheramt'];
                                                    $ototbillamt = $ototbillamt + $pmain['billamt'];


                                                    if ($_POST["dtype"] == 2) {

                                                        Echo "<tr style='height:10px;'>";
                                                        Echo "<td colspan='25'>";
                                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                                        Echo "<thead>";
                                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                                        Echo "<th style='width:5%;'>Sr</th>";
                                                        Echo "<th style='width:15%;' class=''>Item Name</th>";
                                                        Echo "<th style='width:7%;' class=''>HSN Code</th>";
                                                        Echo "<th style='width:5%;' class=''>Unit</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Disc%</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Disc. Amt.</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 1) {
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        }
                                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                                        Echo "</tr>";
                                                        Echo "<thead>";
                                                        Echo "<tr>";
// 
//
                                                        $ctr2 = 0;
                                                        $ototqty = 0;
                                                        $ototrate = 0;
                                                        $ototamount = $ototdiscount = $ototsubtot = $ototcgstamt = $ototsgstamt = $ototigstamt = 0;

                                                        $gettran = mysqli_query($conn, "SELECT * FROM `puottran` WHERE `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                                        while ($tran = mysqli_fetch_array($gettran)) {

                                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                                            $showitem = mysqli_fetch_array($getitem);

                                                            $ctr2++;
                                                            Echo "<tr>";
                                                            Echo "<td>$ctr2</td>";
                                                            Echo "<td>$tran[itemname]</td>";
                                                            Echo "<td>$tran[hsncode]</td>";
                                                            Echo "<td>$tran[unit]</td>";
                                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "<td class='text-right'>$tran[discperc]</td>";
                                                            Echo "<td class='text-right'>$tran[discamt]</td>";
                                                            Echo "<td class='text-right'>$tran[subtot]</td>";
                                                            if ($gsttype == 1) {
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            } elseif ($gsttype == 2) {
                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                            } elseif ($gsttype == '') {

                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            }
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "</tr>";

                                                            $ototqty = $ototqty + $tran['qty'];
                                                            $ototamount = $ototamount + $tran['amount'];
                                                            $ototdiscount = $ototdiscount + $tran['discamt'];
                                                            $ototsubtot = $ototsubtot + $tran['subtot'];
                                                            $ototcgstamt = $ototcgstamt + $tran['cgstamt'];
                                                            $ototsgstamt = $ototsgstamt + $tran['sgstamt'];
                                                            $ototigstamt = $ototigstamt + $tran['igstamt'];
                                                        }

                                                        Echo "<tr>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td>Total</td>";
                                                        Echo "<td class='text-right'>$ototqty</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$ototamount</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$ototdiscount</td>";
                                                        Echo "<td class='text-right'>$ototsubtot</td>";
                                                        if ($gsttype == 1) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ototigstamt</td>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ototcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ototsgstamt</td>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ototcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$ototsgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$totigstamt</td>";
                                                        }
                                                        Echo "<td class='text-right'>$ototamount</td>";
                                                        Echo "</tr>";
                                                        Echo "</table>";
                                                    }
                                                }
                                                Echo "<tr style='background-color: #98F9F0'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                if ($ptype != "") {
                                                    Echo "<td></td>";
                                                }
                                                Echo "<td class='text-right'><b>Total</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrsubtot</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotdis</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtottaxable</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotgst3</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotgst5</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotgst12</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotgst18</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotgst28</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrroundoff</b></td>";
                                                Echo "<td class='numeric text-right'><b>$ogrtotbillamt</b></td>";
                                                Echo "</tr>";
                                                ?>
                                            </tbody>
                                        </table>
                                    </div> 
                                </div>
                                <?
                            }
                        }

                        if ($pchtype == "" || $pchtype == "E") {

                            $getpmain4 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `purexpmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate'  $suppcond $godowncond $ptypecond  $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                            $getno4 = mysqli_num_rows($getpmain4);


                            if ($getno4 > 0) {
                                ?>
                                <div class="row">
                                    <div class="col-lg-12 pad">
                                        <table class="table table-condesed table-bordered">
                                            <tr class="mySearchTr">
                                                <!--9221292520-->
                                                <th width="100%" class="h4" style="color:white"><b>Purchase Expense - Purchase Register</b></th>
                                            </tr>
                                            <tr>
                                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno4; ?></b>
                                                </td>
                                            </tr>
                                        </table>
                                        <?php if ($ptype != "") { ?>
                                            <table width="100%" class="table" style="background-color: #98F9F0">
                                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                                            </table>
                                        <?php } ?>
                                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th width="3%;">Sr</th>
                                                    <th width="4%;">Voucher</th>
                                                    <th width="6%;">Invoice No.</th>
                                                    <th width="6%;">Date</th>
                                                    <th width="5%;">Godown</th>
                                                    <th width="19%;">Party Name</th>                                
                                                    <th width="8%;">GST NO.</th>                                
                                                    <?php if ($ptype != "") { ?>
                                                        <th width="7%;">Pay type</th>
                                                    <?php } ?>
                                                    <th width="6%;" class="text-right">Sub Total</th>
                                                    <th width="5%;" class="text-right">Disc Amt.</th>
                                                    <th width="5%;" class="text-right">Taxable</th>
                                                    <th width="5%;" class="text-right">GST 3%</th>
                                                    <th width="5%;" class="text-right">GST 5%</th>
                                                    <th width="5%;" class="text-right">GST 12%</th>
                                                    <th width="5%;" class="text-right">GST 18%</th>
                                                    <th width="5%;" class="text-right">GST 28%</th>
                                                    <th width="5%;" class="text-right">R.OFF</th>
                                                    <th width="7%;" class="text-right">Net Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $pctr = 0;
                                                $ptotamt = 0;
                                                $ptotdiscamt = 0;
                                                $ptottaxable = 0;
                                                $ptotgst3 = 0;
                                                $ptotgst5 = 0;
                                                $ptotgst12 = 0;
                                                $ptotgst18 = 0;
                                                $ptotgst28 = 0;
                                                $proundoff = 0;
                                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                                $pchtypecond = " AND `mattype`= 'E'";

                                                $getpmain_e = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `purexpmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                                                while ($pmain = mysqli_fetch_array($getpmain_e)) {
                                                    if ($pmain['paidby'] == "C") {
                                                        $ptyped = "CASH";
                                                    } else {
                                                        $ptyped = "CREDIT";
                                                    }


                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);

                                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
                                                    $trow = mysqli_fetch_array($mown);

                                                    $voucher = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $party = $sparty['name'];
                                                    $gstno = $sparty['gstno'];
                                                    $gst3 = $pmain['totgst3'];
                                                    $gst5 = $pmain['totgst5'];
                                                    $gst12 = $pmain['totgst12'];
                                                    $gst18 = $pmain['totgst18'];
                                                    $gst28 = $pmain['totgst28'];
                                                    $roundoff = $pmain['roundoff'];
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $taxable = $pmain['taxableamt'];

                                                    $pctr++;
                                                    Echo"<tr data-href='purchase_exp.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                                    Echo"<td>$pctr</td>";
                                                    Echo"<td>$voucher</td>";
                                                    Echo"<td>$invno</td>";
                                                    Echo"<td>$mydt</td>";
                                                    Echo"<td>$gcode</td>";
                                                    Echo"<td>$party</td>";
                                                    Echo"<td>$gstno</td>";
                                                    if ($ptype != "") {
                                                        Echo"<td>$ptyped</td>";
                                                    }
                                                    Echo"<td class='text-right'>$subtot</td>";
                                                    Echo"<td class='text-right'>$totdisc</td>";
                                                    Echo"<td class='text-right'>$taxable</td>";
                                                    Echo"<td class='text-right'>$gst3</td>";
                                                    Echo"<td class='text-right'>$gst5</td>";
                                                    Echo"<td class='text-right'>$gst12</td>";
                                                    Echo"<td class='text-right'>$gst18</td>";
                                                    Echo"<td class='text-right'>$gst28</td>";
                                                    Echo"<td class='text-right'>$roundoff</td>";
                                                    Echo"<td class='text-right'>$billamt</td>";
                                                    Echo"</tr>";


                                                    $pgrsubtot = $pgrsubtot + $subtot;
                                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                                    $pgrtottaxable = $pgrtottaxable + $taxable;
                                                    $pgrtotgst3 = $pgrtotgst3 + $gst3;
                                                    $pgrtotgst5 = $pgrtotgst5 + $gst5;
                                                    $pgrtotgst12 = $pgrtotgst12 + $gst12;
                                                    $pgrtotgst18 = $pgrtotgst18 + $gst18;
                                                    $pgrtotgst28 = $pgrtotgst28 + $gst28;
                                                    $pgrroundoff = $pgrroundoff + $roundoff;
                                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                                    $ptotpackamt = $ptotpackamt + $pmain['packamt'];
                                                    $ptotstamt = $ptotstamt + $pmain['stamt'];
                                                    $ptotothamt = $ptotothamt + $pmain['otheramt'];
                                                    $ptotbillamt = $ptotbillamt + $pmain['billamt'];

                                                    if ($_POST["dtype"] == 2) {

                                                        Echo "<tr style='height:10px;'>";
                                                        Echo "<td colspan='25'>";
                                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                                        Echo "<thead>";
                                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                                        Echo "<th style='width:5%;'>Sr</th>";
                                                        Echo "<th style='width:15%;' class=''>Item Name</th>";
                                                        Echo "<th style='width:7%;' class=''>HSN Code</th>";
                                                        Echo "<th style='width:5%;' class=''>Unit</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                                        Echo "<th style='width:4%;' class='text-right'>Disc%</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Disc. Amt.</th>";
                                                        Echo "<th style='width:8%;' class='text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 1) {
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                        }
                                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                                        Echo "</tr>";
                                                        Echo "<thead>";
                                                        Echo "<tr>";
//
//
                                                        $pctr2 = 0;
                                                        $ptotqty = 0;
                                                        $ptotrate = 0;
                                                        $ptotamount = $ptotdiscount = $ptotsubtot = $ptotcgstamt = $ptotsgstamt = $ptotigstamt = 0;

                                                        $gettran = mysqli_query($conn, "SELECT * FROM `purexptran` WHERE `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                                        while ($tran = mysqli_fetch_array($gettran)) {

                                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `expense`='$tran[accode]'") or die(mysqli_error($conn));
                                                            $showitem = mysqli_fetch_array($getitem);

                                                            $ctr2++;
                                                            Echo "<tr>";
                                                            Echo "<td>$ctr2</td>";
                                                            Echo "<td>$tran[name]</td>";
                                                            Echo "<td>$tran[hsncode]</td>";
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "<td class='text-right'>$tran[discperc]</td>";
                                                            Echo "<td class='text-right'>$tran[discamt]</td>";
                                                            Echo "<td class='text-right'>$tran[subtot]</td>";
                                                            if ($gsttype == 1) {
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            } elseif ($gsttype == 2) {
                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                            } elseif ($gsttype == '') {

                                                                Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[sgstamt]</td>";
                                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                                            }
                                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                                            Echo "</tr>";

                                                            $etotamount = $etotamount + $tran['amount'];
                                                            $etotdiscount = $etotdiscount + $tran['discamt'];
                                                            $etotsubtot = $etotsubtot + $tran['subtot'];
                                                            $etotcgstamt = $etotcgstamt + $tran['cgstamt'];
                                                            $etotsgstamt = $etotsgstamt + $tran['sgstamt'];
                                                            $etotigstamt = $etotigstamt + $tran['igstamt'];
                                                        }

                                                        Echo "<tr>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td>Total</td>";
                                                        Echo "<td class='text-right'></td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$etotamount</td>";
                                                        Echo "<td></td>";
                                                        Echo "<td class='text-right'>$etotdiscount</td>";
                                                        Echo "<td class='text-right'>$etotsubtot</td>";
                                                        if ($gsttype == 1) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotigstamt</td>";
                                                        } elseif ($gsttype == 2) {
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotsgstamt</td>";
                                                        } elseif ($gsttype == '') {

                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotcgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotsgstamt</td>";
                                                            Echo "<td></td>";
                                                            Echo "<td class='text-right'>$etotigstamt</td>";
                                                        }
                                                        Echo "<td class='text-right'>$ptotamount</td>";
                                                        Echo "</tr>";
                                                        Echo "</table>";
                                                    }
                                                }
                                                Echo "<tr style='background-color: #98F9F0'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                if ($ptype != "") {
                                                    Echo "<td></td>";
                                                }
                                                Echo "<td class='text-right'><b>Total</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrsubtot</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotdis</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtottaxable</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst3</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst5</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst12</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst18</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotgst28</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrroundoff</b></td>";
                                                Echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                                Echo "</tr>";
                                                ?>
                                            </tbody>
                                        </table>
                                    </div> 
                                </div>
                            </div>

                            <?php
                        }
                    }
                }

                if ($supplier == '') {
                    ?>
                    <div class="container" style="width: 60%;">
                        <div class="row">
                            <?php
                            $sourceqry = mysqli_query($conn, "SELECT source, sum(amount) as amt from actran where `date` >= '$mfrmdate' 
                                        AND `date` <= '$mtodate'  and grpcode = '004' and source != 'PUPK' AND source != 'PURW'
                                        AND source != 'PUO' AND source != 'PRPK' AND source != 'PRRW' AND source != 'PUEX' ")or die(mysqli_error($conn));
                            ?>

                            <table class="table table-bordered table-condensed">
                                <tr class="mySearchTr">
                                    <th class="h4" style="color:white"><b>Entries Other Than Purchase</b></th>
                                </tr>
                            </table>

                            <table class="table table-bordered table-condensed">
                                <tr class="mySearchTr">
                                    <th>Sr</th>
                                    <th colspan="2">Source</th>
                                    <th class="text-right">Amount</th>
                                </tr>
                                <?
                                $sctr = 0;
                                while ($fsource = mysqli_fetch_array($sourceqry)) {
                                    $famt = $fsource['amt'] * -1;
                                    $sctr++;
                                    ?>
                                    <tr style="background-color: white ;">
                                        <td><?php echo $sctr; ?></td>
                                        <td colspan="2"><?php echo $fsource['source']; ?></td>
                                        <td class="text-right"><?php echo $famt; ?></td>
                                    </tr>
                                    <?
                                    $tranqry = mysqli_query($conn, "SELECT DATE_FORMAT(`date`, '%d-%m-%Y') as date , source, vouchno, sum(amount) as amt from actran where `date` >= '$mfrmdate' 
                                        AND `date` <= '$mtodate'  and grpcode = '004' and source = '$fsource[source]' group by vouchno")or die(mysqli_error($conn));
                                    ?>
                                    <tr class="mySearchTr">
                                        <th>Sr</th>
                                        <th>Date</th>
                                        <th>Vouchno</th>
                                        <th class="text-right">Amount</th>
                                    </tr>
                                    <? $scrt2 = 0; ?>
                                    <?
                                    while ($ftran = mysqli_fetch_array($tranqry)) {

                                        $sctr2++;
                                        ?>
                                        <tr style="background-color: white ;">
                                            <td><?php echo $sctr2; ?></td>
                                            <td><?php echo $ftran['date'] ?></td>
                                            <td><?php echo $ftran['vouchno'] ?></td>
                                            <td class="text-right"><?php echo abs($ftran['amt']) ?></td>
                                        </tr>
                                        <?
                                    }
                                    $totamt = $totamt + $famt;
                                }
                                ?>

                                <tr style="background-color: #98F9F0" >
                                    <td class="text-right" colspan="3">Total</td>
                                    <td class="text-right" colspan=""><?php echo $totamt; ?></td>
                                </tr>
                            </table>

                        </div>
                    </div>
                <? } ?>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

    if ($pchtype == "" || $pchtype == "R") {


        $contents .= ",,,,,Raw Material - Purchase Register\n";
        $contents .= "Sr.,Voucher, Invoice No., Date, Godown, Party Name, GST No., Pay Type, Sub Total, Disc Amt., Taxable, ";
        $contents .= "Taxable on 0%, GST 0%, Taxable on 3%, GST 3%, Taxable on 5%, GST 5%, Taxable on 12%, GST 12%, ";
        $contents .= "Taxable on 18%, GST 18%,Taxable on 28%, GST 28%, ";
        $contents .= "Taxable IGST on 0%, IGST 0%, Taxable IGST on 3%, IGST 3%, Taxable IGST on 5%, IGST 5%, Taxable IGST on 12%, IGST 12%, ";
        $contents .= "Taxable IGST on 18%, IGST 18%, Taxable IGST on 28%, IGST 28%, Net Amount\n";

        $ctr = 0;
        $totamt = 0;
        $totdiscamt = 0;
        $tottaxable = 0;

        $rgrtotgst0 = $rgrtotgst3 = $rgrtotgst5 = $rgrtotgst12 = $rgrtotgst18 = $rgrtotgst28 = 0;
        $rgrtotgst0i = $rgrtotgst3i = $rgrtotgst5i = $rgrtotgst12i = $rgrtotgst18i = $rgrtotgst28i = 0;
        $totpackamt = $totstamt = $totothamt = $totbillamt = 0;
        $grtottaxbl0 = $grtottaxbl3 = $grtottaxbl5 = $grtottaxbl12 = $grtottaxbl8 = $grtottaxbl28 = 0;
        $pchtypecond = " AND `mattype`= 'R'";

        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond $pchtypecond $gstcond $pcond ")or die(mysqli_error($conn));


        while ($pmain = mysqli_fetch_array($getpmain)) {
            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else {
                $ptyped = "CREDIT";
            }

            $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
            $sgodw = mysqli_fetch_array($ggodw);

            $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
            $trow = mysqli_fetch_array($mown);

            $vouchno = $pmain['vouchno'];
            $oms = $pmain['oms'];
            $invno = $pmain['invno'];
            $mydt = $pmain['mydt'];
            $gcode = $sgodw['gcode'];
            $party = $sparty['name'];
            $gstno = $sparty['gstno'];
            $gst3 = $pmain['totgst3'];
            $gst5 = $pmain['totgst5'];
            $gst12 = $pmain['totgst12'];
            $gst18 = $pmain['totgst18'];
            $gst28 = $pmain['totgst28'];
            $roundoff = $pmain['roundoff'];
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totdisc = $pmain['discamt'];
            $taxable = $pmain['taxableamt'];
            $ctr++;


            if ($oms == 1) {
                $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate = 0.00 ")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT sum(subtot) as subtot3, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate = 3.00 ")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, igstrate from  rawtran   
                                       where vouchno = '$vouchno' and igstrate=5.00 ")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, igstrate from  rawtran  
                                            where vouchno = '$vouchno' and igstrate = 12.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, igstrate from  rawtran  
                                            where vouchno = '$vouchno' and igstrate=18.00 ")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate=28.00 ")or die(mysqli_error($conn));
            } else {
                $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 0.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT sum(subtot) as subtot3, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 1.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 2.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, cgstrate from  rawtran  
                                            where vouchno = '$vouchno' and cgstrate = 6.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, cgstrate from  rawtran  
                                       where vouchno = '$vouchno' and cgstrate=9.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, cgstrate from  rawtran  
                                       where vouchno = '$vouchno' and cgstrate=14.00 group by cgstrate")or die(mysqli_error($conn));
            }


            $taxable0 = mysqli_fetch_array($taxbl0);
            $taxable3 = mysqli_fetch_array($taxbl3);
            $taxable5 = mysqli_fetch_array($taxbl5);
            $taxable12 = mysqli_fetch_array($taxbl12);
            $taxable18 = mysqli_fetch_array($taxbl18);
            $taxable28 = mysqli_fetch_array($taxbl28);



            $contents .= $ctr . ",";
            $contents .= $vouchno . ",";
            $contents .= $invno . ",";
            $contents .= $mydt . ",";
            $contents .= $gcode . ",";
            $contents .= $party . ",";
            $contents .= $gstno . ",";
            $contents .= $ptyped . ",";
            $contents .= $subtot . ",";
            $contents .= $totdisc . ",";
            $contents .= $taxable . ",";

            if ($oms == 1) {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $taxable0['subtot0'] . ",";
            $contents .= $gst0 . ",";
            $contents .= $taxable3['subtot3'] . ",";
            $contents .= $gst3 . ",";
            $contents .= $taxable5['subtot5'] . ",";
            $contents .= $gst5 . ",";
            $contents .= $taxable12['subtot12'] . ",";
            $contents .= $gst12 . ",";
            $contents .= $taxable18['subtot18'] . ",";
            $contents .= $gst18 . ",";
            $contents .= $taxable28['subtot28'] . ",";
            $contents .= $gst28 . ",";

            if ($oms == 1) {
                
            } else {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $billamt . "\n";



            $rgrsubtot = $rgrsubtot + $subtot;
            $rgrtotdis = $rgrtotdis + $totdisc;
            $rgrtottaxable = $rgrtottaxable + $taxable;

            if ($oms == 1) {
                $grtottaxbl0i = $grtottaxbl0i + $taxable0['subtot0'];
                $grtottaxbl3i = $grtottaxbl3i + $taxable3['subtot3'];
                $grtottaxbl5i = $grtottaxbl5i + $taxable5['subtot5'];
                $grtottaxbl12i = $grtottaxbl12i + $taxable12['subtot12'];
                $grtottaxbl18i = $grtottaxbl18i + $taxable18['subtot18'];
                $grtottaxbl28i = $grtottaxbl28i + $taxable28['subtot28'];

                $rgrtotgst0i = $rgrtotgst0i + $gst0;
                $rgrtotgst3i = $rgrtotgst3i + $gst3;
                $rgrtotgst5i = $rgrtotgst5i + $gst5;
                $rgrtotgst12i = $rgrtotgst12i + $gst12;
                $rgrtotgst18i = $rgrtotgst18i + $gst18;
                $rgrtotgst28i = $rgrtotgst28i + $gst28;
            } else {
                $grtottaxbl0 = $grtottaxbl0 + $taxable0['subtot0'];
                $grtottaxbl3 = $grtottaxbl3 + $taxable3['subtot3'];
                $grtottaxbl5 = $grtottaxbl5 + $taxable5['subtot5'];
                $grtottaxbl12 = $grtottaxbl12 + $taxable12['subtot12'];
                $grtottaxbl18 = $grtottaxbl18 + $taxable18['subtot18'];
                $grtottaxbl28 = $grtottaxbl28 + $taxable28['subtot28'];

                $rgrtotgst0 = $rgrtotgst0 + $gst0;
                $rgrtotgst3 = $rgrtotgst3 + $gst3;
                $rgrtotgst5 = $rgrtotgst5 + $gst5;
                $rgrtotgst12 = $rgrtotgst12 + $gst12;
                $rgrtotgst18 = $rgrtotgst18 + $gst18;
                $rgrtotgst28 = $rgrtotgst28 + $gst28;
            }

            $rgrtotbillamt = $rgrtotbillamt + $billamt;

            $rtotpackamt = $rtotpackamt + $pmain['packamt'];
            $rtotstamt = $rtotstamt + $pmain['stamt'];
            $rtotothamt = $rtotothamt + $pmain['otheramt'];
            $rtotbillamt = $rtotbillamt + $pmain['billamt'];


            if ($_POST["dtype"] == 2) {

                if ($gsttype == 1) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, IGST Rate,IGST Amount,Total\n";
                } elseif ($gsttype == 2) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount,Total\n";
                } elseif ($gsttype == '') {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount, IGST Rate, IGST Amount,Total\n";
                }

                $ctr2 = 0;
                $rtotqty = 0;
                $rtotrate = 0;
                $rtotamount = $rtotdiscount = $rtotsubtot = $rtotcgstamt = $rtotsgstamt = $rtotigstamt = 0;

                $gettran = mysqli_query($conn, "SELECT * FROM `rawtran` WHERE `vouchno` = '$pmain[vouchno]' ") or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

                    $getitem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` WHERE `code` = '$tran[itemcode]'") or die(mysqli_error($conn));
                    $showitem = mysqli_fetch_array($getitem);

                    $ctr2++;
                    $contents .= $ctr2 . ",";
                    $contents .= $tran[itemname] . ",";
                    $contents .= $tran[hsncode] . ",";
                    $contents .= $tran[unit] . ",";
                    $contents .= $tran[qty] . ",";
                    $contents .= $tran[rate] . ",";
                    $contents .= $tran[amount] . ",";
                    $contents .= $tran[discperc] . ",";
                    $contents .= $tran[discamt] . ",";
                    $contents .= $tran[subtot] . ",";
                    if ($gsttype == 1) {
                        $contents .= $tran[igstrate] . ",";
                        $contents .= $tran[igstamt] . ",";
                    } elseif ($gsttype == 2) {
                        $contents .= $tran[cgstrate] . ",";
                        $contents .= $tran[cgstamt] . ",";
                        $contents .= $tran[sgstrate] . ",";
                        $contents .= $tran[sgstamt] . ",";
                    } elseif ($gsttype == '') {

                        $contents .= $tran[cgstrate] . ",";
                        $contents .= $tran[cgstamt] . ",";
                        $contents .= $tran[sgstrate] . ",";
                        $contents .= $tran[sgstamt] . ",";
                        $contents .= $tran[igstrate] . ",";
                        $contents .= $tran[igstamt] . ",";
                    }
                    $contents .= $tran[amount] . "\n";

                    $rtotqty = $rtotqty + $tran['qty'];
                    $rtotamount = $rtotamount + $tran['amount'];
                    $rtotdiscount = $rtotdiscount + $tran['discamt'];
                    $rtotsubtot = $rtotsubtot + $tran['subtot'];
                    $rtotcgstamt = $rtotcgstamt + $tran['cgstamt'];
                    $rtotsgstamt = $rtotsgstamt + $tran['sgstamt'];
                    $rtotigstamt = $rtotigstamt + $tran['igstamt'];
                }


                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $rtotqty . ",";
                $contents .= "" . ",";
                $contents .= $rtotamount . ",";
                $contents .= "" . ",";
                $contents .= $rtotdiscount . ",";
                $contents .= $rtotsubtot . ",";
                if ($gsttype == 1) {
                    $contents .= "" . ",";
                    $contents .= $rtotigstamt . ",";
                } elseif ($gsttype == 2) {
                    $contents .= "" . ",";
                    $contents .= $rtotcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $rtotsgstamt . ",";
                } elseif ($gsttype == '') {

                    $contents .= "" . ",";
                    $contents .= $rtotcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $rtotsgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $rtotigstamt . ",";
                }
                $contents .= $rtotamount . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= $rgrsubtot . ",";
        $contents .= $rgrtotdis . ",";
        $contents .= $rgrtottaxable . ",";

        $contents .= $grtottaxbl0 . ",";
        $contents .= $rgrtotgst0 . ",";
        $contents .= $grtottaxbl3 . ",";
        $contents .= $rgrtotgst3 . ",";
        $contents .= $grtottaxbl5 . ",";
        $contents .= $rgrtotgst5 . ",";
        $contents .= $grtottaxbl12 . ",";
        $contents .= $rgrtotgst12 . ",";
        $contents .= $grtottaxbl18 . ",";
        $contents .= $rgrtotgst18 . ",";
        $contents .= $grtottaxbl28 . ",";
        $contents .= $rgrtotgst28 . ",";

        $contents .= $grtottaxbl0i . ",";
        $contents .= $rgrtotgst0i . ",";
        $contents .= $grtottaxbl3i . ",";
        $contents .= $rgrtotgst3i . ",";
        $contents .= $grtottaxbl5i . ",";
        $contents .= $rgrtotgst5i . ",";
        $contents .= $grtottaxbl12i . ",";
        $contents .= $rgrtotgst12i . ",";
        $contents .= $grtottaxbl18i . ",";
        $contents .= $rgrtotgst18i . ",";
        $contents .= $grtottaxbl28i . ",";
        $contents .= $rgrtotgst28i . ",";

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


    if ($pchtype == "" || $pchtype == "P") {


        $contents .= ",,,,,Packing Material - Purchase Register\n";
        $contents .= "Sr.,Voucher, Invoice No., Date, Godown, Party Name, GST No., Pay Type, Sub Total, Disc Amt., Taxable, ";
        $contents .= "Taxable on 0%, GST 0%,Taxable on 3%,GST 3%,Taxable on 5%, GST 5%, Taxable on 12%, GST 12%, ";
        $contents .= "Taxable on 18%, GST 18%,Taxable on 28%, GST 28%, ";
        $contents .= "Taxable IGST on 0%, IGST 0%, Taxable IGST on 3%, IGST 3%, Taxable IGST on 5%, IGST 5%, Taxable IGST on 12%, IGST 12%, ";
        $contents .= "Taxable IGST on 18%, IGST 18%, Taxable IGST on 28%, IGST 28%, Net Amount\n";

        $ctr = 0;
        $totamt = 0;
        $totdiscamt = 0;
        $ptottaxable = 0;

        $ptotgst0 = $ptotgst3 = $ptotgst5 = $ptotgst12 = $ptotgst18 = $ptotgst28 = 0;
        $ptotgst0i = $ptotgst3i = $ptotgst5i = $ptotgst12i = $ptotgst18i = $ptotgst28i = 0;

        $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
        $pgrtottaxbl0 = $pgrtottaxbl3 = $pgrtottaxbl5 = $pgrtottaxbl12 = $pgrtottaxbl8 = $pgrtottaxbl28 = 0;
        $pchtypecond = " AND `mattype`= 'P'";

        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond $pchtypecond $gstcond $pcond ")or die(mysqli_error($conn));


        while ($pmain = mysqli_fetch_array($getpmain)) {
            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else {
                $ptyped = "CREDIT";
            }

            $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
            $sgodw = mysqli_fetch_array($ggodw);

            $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
            $trow = mysqli_fetch_array($mown);

            $vouchno = $pmain['vouchno'];
            $oms = $pmain['oms'];
            $invno = $pmain['invno'];
            $mydt = $pmain['mydt'];
            $gcode = $sgodw['gcode'];
            $party = $sparty['name'];
            $gstno = $sparty['gstno'];
            $gst3 = $pmain['totgst3'];
            $gst5 = $pmain['totgst5'];
            $gst12 = $pmain['totgst12'];
            $gst18 = $pmain['totgst18'];
            $gst28 = $pmain['totgst28'];
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totdisc = $pmain['discamt'];
            $taxable = $pmain['taxableamt'];
            $ctr++;


            if ($oms == 1) {
                $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate = 0.00 ")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT sum(subtot) as subtot3, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate = 3.00 ")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, igstrate from  rawtran   
                                       where vouchno = '$vouchno' and igstrate=5.00 ")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, igstrate from  rawtran  
                                            where vouchno = '$vouchno' and igstrate = 12.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, igstrate from  rawtran  
                                            where vouchno = '$vouchno' and igstrate=18.00 ")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, igstrate from  rawtran  
                                       where vouchno = '$vouchno' and igstrate=28.00 ")or die(mysqli_error($conn));
            } else {
                $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 0.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT sum(subtot) as subtot3, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 1.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, cgstrate from  rawtran   
                                       where vouchno = '$vouchno' and cgstrate = 2.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, cgstrate from  rawtran  
                                            where vouchno = '$vouchno' and cgstrate = 6.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, cgstrate from  rawtran  
                                       where vouchno = '$vouchno' and cgstrate=9.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, cgstrate from  rawtran  
                                       where vouchno = '$vouchno' and cgstrate=14.00 group by cgstrate")or die(mysqli_error($conn));
            }


            $taxable0 = mysqli_fetch_array($taxbl0);
            $taxable3 = mysqli_fetch_array($taxbl3);
            $taxable5 = mysqli_fetch_array($taxbl5);
            $taxable12 = mysqli_fetch_array($taxbl12);
            $taxable18 = mysqli_fetch_array($taxbl18);
            $taxable28 = mysqli_fetch_array($taxbl28);

            $contents .= $ctr . ",";
            $contents .= $vouchno . ",";
            $contents .= $invno . ",";
            $contents .= $mydt . ",";
            $contents .= $gcode . ",";
            $contents .= $party . ",";
            $contents .= $gstno . ",";
            $contents .= $ptyped . ",";
            $contents .= $subtot . ",";
            $contents .= $totdisc . ",";
            $contents .= $taxable . ",";

            if ($oms == 1) {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $taxable0['subtot0'] . ",";
            $contents .= $gst0 . ",";
            $contents .= $taxable3['subtot3'] . ",";
            $contents .= $gst3 . ",";
            $contents .= $taxable5['subtot5'] . ",";
            $contents .= $gst5 . ",";
            $contents .= $taxable12['subtot12'] . ",";
            $contents .= $gst12 . ",";
            $contents .= $taxable18['subtot18'] . ",";
            $contents .= $gst18 . ",";
            $contents .= $taxable28['subtot28'] . ",";
            $contents .= $gst28 . ",";

            if ($oms == 1) {
                
            } else {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $billamt . "\n";


            $pgrsubtot = $pgrsubtot + $subtot;
            $pgrtotdis = $pgrtotdis + $totdisc;
            $pgrtottaxable = $pgrtottaxable + $taxable;

            if ($oms == 1) {
                $pgrtottaxbl0i = $pgrtottaxbl0i + $taxable0['subtot0'];
                $pgrtottaxbl3i = $pgrtottaxbl3i + $taxable3['subtot3'];
                $pgrtottaxbl5i = $pgrtottaxbl5i + $taxable5['subtot5'];
                $pgrtottaxbl12i = $pgrtottaxbl12i + $taxable12['subtot12'];
                $pgrtottaxbl18i = $pgrtottaxbl18i + $taxable18['subtot18'];
                $pgrtottaxbl28i = $pgrtottaxbl28i + $taxable28['subtot28'];

                $pgrtotgst0i = $pgrtotgst0i + $gst0;
                $pgrtotgst3i = $pgrtotgst3i + $gst3;
                $pgrtotgst5i = $pgrtotgst5i + $gst5;
                $pgrtotgst12i = $pgrtotgst12i + $gst12;
                $pgrtotgst18i = $pgrtotgst18i + $gst18;
                $pgrtotgst28i = $pgrtotgst28i + $gst28;
            } else {
                $pgrtottaxbl0 = $pgrtottaxbl0 + $taxable0['subtot0'];
                $pgrtottaxbl3 = $pgrtottaxbl3 + $taxable3['subtot3'];
                $pgrtottaxbl5 = $pgrtottaxbl5 + $taxable5['subtot5'];
                $pgrtottaxbl12 = $pgrtottaxbl12 + $taxable12['subtot12'];
                $pgrtottaxbl18 = $pgrtottaxbl18 + $taxable18['subtot18'];
                $pgrtottaxbl28 = $pgrtottaxbl28 + $taxable28['subtot28'];

                $pgrtotgst0 = $pgrtotgst0 + $gst0;
                $pgrtotgst3 = $pgrtotgst3 + $gst3;
                $pgrtotgst5 = $pgrtotgst5 + $gst5;
                $pgrtotgst12 = $pgrtotgst12 + $gst12;
                $pgrtotgst18 = $pgrtotgst18 + $gst18;
                $pgrtotgst28 = $pgrtotgst28 + $gst28;
            }
            $pgrtotbillamt = $pgrtotbillamt + $billamt;

            $ptotpackamt = $ptotpackamt + $pmain['packamt'];
            $ptotstamt = $ptotstamt + $pmain['stamt'];
            $ptotothamt = $ptotothamt + $pmain['otheramt'];
            $ptotbillamt = $ptotbillamt + $pmain['billamt'];


            if ($_POST["dtype"] == 2) {
                if ($gsttype == 1) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, IGST Rate,IGST Amount,Total\n";
                } elseif ($gsttype == 2) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount,Total\n";
                } elseif ($gsttype == '') {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount, IGST Rate, IGST Amount,Total\n";
                }


                $ctr2 = 0;
                $rtotqty = 0;
                $rtotrate = 0;
                $rtotamount = $rtotdiscount = $rtotsubtot = $rtotcgstamt = $rtotsgstamt = $rtotigstamt = 0;

                $gettran = mysqli_query($conn, "SELECT * FROM `rawtran` WHERE `vouchno` = '$pmain[vouchno]' ") or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

                    $getitem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` WHERE `code` = '$tran[itemcode]'") or die(mysqli_error($conn));
                    $showitem = mysqli_fetch_array($getitem);

                    $ctr2++;
                    $contents .= $ctr2 . ",";
                    $contents .= $tran['itemname'] . ",";
                    $contents .= $tran['hsncode'] . ",";
                    $contents .= $tran['unit'] . ",";
                    $contents .= $tran['qty'] . ",";
                    $contents .= $tran['rate'] . ",";
                    $contents .= $tran['amount'] . ",";
                    $contents .= $tran['discperc'] . ",";
                    $contents .= $tran['discamt'] . ",";
                    $contents .= $tran['subtot'] . ",";
                    if ($gsttype == 1) {
                        $contents .= $tran['igstrate'] . ",";
                        $contents .= $tran['igstamt'] . ",";
                    } elseif ($gsttype == 2) {
                        $contents .= $tran['cgstrate'] . ",";
                        $contents .= $tran['cgstamt'] . ",";
                        $contents .= $tran['sgstrate'] . ",";
                        $contents .= $tran['sgstamt'] . ",";
                    } elseif ($gsttype == '') {

                        $contents .= $tran['cgstrate'] . ",";
                        $contents .= $tran['cgstamt'] . ",";
                        $contents .= $tran['sgstrate'] . ",";
                        $contents .= $tran['sgstamt'] . ",";
                        $contents .= $tran['igstrate'] . ",";
                        $contents .= $tran['igstamt'] . ",";
                    }
                    $contents .= $tran['amount'] . "\n";

                    $ptotqty = $ptotqty + $tran['qty'];
                    $ptotamount = $ptotamount + $tran['amount'];
                    $ptotdiscount = $ptotdiscount + $tran['discamt'];
                    $ptotsubtot = $ptotsubtot + $tran['subtot'];
                    $ptotcgstamt = $ptotcgstamt + $tran['cgstamt'];
                    $ptotsgstamt = $ptotsgstamt + $tran['sgstamt'];
                    $ptotigstamt = $ptotigstamt + $tran['igstamt'];
                }


                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $ptotqty . ",";
                $contents .= "" . ",";
                $contents .= $ptotamount . ",";
                $contents .= "" . ",";
                $contents .= $ptotdiscount . ",";
                $contents .= $ptotsubtot . ",";
                if ($gsttype == 1) {
                    $contents .= "" . ",";
                    $contents .= $ptotigstamt . ",";
                } elseif ($gsttype == 2) {
                    $contents .= "" . ",";
                    $contents .= $ptotcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ptotsgstamt . ",";
                } elseif ($gsttype == '') {

                    $contents .= "" . ",";
                    $contents .= $ptotcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ptotsgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ptotigstamt . ",";
                }
                $contents .= $ptotamount . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= $pgrsubtot . ",";
        $contents .= $pgrtotdis . ",";
        $contents .= $pgrtottaxable . ",";

        $contents .= $pgrtottaxbl0 . ",";
        $contents .= $pgrtotgst0 . ",";
        $contents .= $pgrtottaxbl3 . ",";
        $contents .= $pgrtotgst3 . ",";
        $contents .= $pgrtottaxbl5 . ",";
        $contents .= $pgrtotgst5 . ",";
        $contents .= $pgrtottaxbl12 . ",";
        $contents .= $pgrtotgst12 . ",";
        $contents .= $pgrtottaxbl18 . ",";
        $contents .= $pgrtotgst18 . ",";
        $contents .= $pgrtottaxbl28 . ",";
        $contents .= $pgrtotgst28 . ",";

        $contents .= $pgrtottaxbl0i . ",";
        $contents .= $pgrtotgst0i . ",";
        $contents .= $pgrtottaxbl3i . ",";
        $contents .= $pgrtotgst3i . ",";
        $contents .= $pgrtottaxbl5i . ",";
        $contents .= $pgrtotgst5i . ",";
        $contents .= $pgrtottaxbl12i . ",";
        $contents .= $pgrtotgst12i . ",";
        $contents .= $pgrtottaxbl18i . ",";
        $contents .= $pgrtotgst18i . ",";
        $contents .= $pgrtottaxbl28i . ",";
        $contents .= $pgrtotgst28i . ",";

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


    if ($pchtype == "" || $pchtype == "O") {

        $contents .= ",,,,,Other - Purchase Register\n";
        $contents .= "Sr.,Voucher, Invoice No., Date, Godown, Party Name, GST No., Pay Type, Sub Total, Disc Amt., Taxable, ";
        $contents .= "Taxable on 0%, GST 0%, Taxable on 3%, GST 3%, Taxable on 5%, GST 5%, Taxable on 12%, GST 12%, ";
        $contents .= "Taxable on 18%, GST 18%,Taxable on 28%, GST 28%, ";
        $contents .= "Taxable IGST on 0%, IGST 0%, Taxable IGST on 3%, IGST 3%, Taxable IGST on 5%, IGST 5%, Taxable IGST on 12%, IGST 12%, ";
        $contents .= "Taxable IGST on 18%, IGST 18%, Taxable IGST on 28%, IGST 28%, Net Amount\n";

        $ctr = 0;
        $totamt = 0;
        $totdiscamt = 0;
        $otottaxable = 0;

        $ototgst0 = $ototgst3 = $ototgst5 = $ototgst12 = $ototgst18 = $ototgst28 = 0;
        $ototgst0i = $ototgst3i = $ototgst5i = $ototgst12i = $ototgst18i = $ototgst28i = 0;

        $ototpackamt = $ototstamt = $ototothamt = $ototbillamt = 0;
        $ogrtottaxbl0 = $ogrtottaxbl3 = $ogrtottaxbl5 = $ogrtottaxbl12 = $ogrtottaxbl18 = $ogrtottaxbl28 = 0;
        $ogrtottaxbl0i = $ogrtottaxbl3i = $ogrtottaxbl5i = $ogrtottaxbl12i = $ogrtottaxbl18i = $ogrtottaxbl28i = 0;

        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `puotmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $ptypecond  $gstcond $pcond ") or die(mysqli_error($conn));

        while ($pmain = mysqli_fetch_array($getpmain)) {
            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else {
                $ptyped = "CREDIT";
            }

            $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='009'  ") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
            $sgodw = mysqli_fetch_array($ggodw);

            $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
            $trow = mysqli_fetch_array($mown);

            $vouchno = $pmain['vouchno'];
            $oms = $pmain['oms'];
            $invno = $pmain['invno'];
            $mydt = $pmain['mydt'];
            $gcode = $sgodw['gcode'];
            $party = $sparty['name'];
            $gstno = $sparty['gstno'];
            if ($oms == 1) {
                if ($pmain['igstrate'] == 0.00) {
                    $gst0 = $pmain['igstamt'];
                }
                if ($pmain['igstrate'] == 3.00) {
                    $gst3 = $pmain['igstamt'];
                }
                if ($pmain['igstrate'] == 5.00) {
                    $gst5 = $pmain['igstamt'];
                }
                if ($pmain['igstrate'] == 12.00) {
                    $gst12 = $pmain['igstamt'];
                }
                if ($pmain['igstrate'] == 18.00) {
                    $gst18 = $pmain['igstamt'];
                }
                if ($pmain['igstrate'] == 28.00) {
                    $gst28 = $pmain['igstamt'];
                }
            } else {
                if ($pmain['cgstrate'] == 0.00) {
                    $gst0 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
                if ($pmain['cgstrate'] == 1.50) {
                    $gst3 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
                if ($pmain['cgstrate'] == 2.50) {
                    $gst5 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
                if ($pmain['cgstrate'] == 6.00) {
                    $gst12 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
                if ($pmain['cgstrate'] == 9.00) {
                    $gst18 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
                if ($pmain['cgstrate'] == 14.00) {
                    $gst28 = $pmain['cgstamt'] + $pmain['sgstamt'];
                }
            }
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totdisc = $pmain['discamt'];
            $taxable = $pmain['taxableamt'];
            $ctr++;


            if ($oms == 1) {
                $taxbl0 = mysqli_query($conn, "SELECT taxableamt as subtot0, igstrate from  puotmain  
                                       where vouchno = '$vouchno' and igstrate = 0.00 ")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT taxableamt as subtot3, igstrate from  puotmain  
                                       where vouchno = '$vouchno' and igstrate = 3.00 ")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT taxableamt as subtot5, igstrate from  puotmain   
                                       where vouchno = '$vouchno' and igstrate=5.00 ")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT taxableamt as subtot12, igstrate from  puotmain  
                                            where vouchno = '$vouchno' and igstrate = 12.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT taxableamt as subtot18, igstrate from  puotmain  
                                            where vouchno = '$vouchno' and igstrate=18.00 ")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT taxableamt as subtot28, igstrate from  puotmain  
                                       where vouchno = '$vouchno' and igstrate=28.00 ")or die(mysqli_error($conn));
            } else {
                $taxbl0 = mysqli_query($conn, "SELECT taxableamt as subtot0, cgstrate from  puotmain   
                                       where vouchno = '$vouchno' and cgstrate = 0.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl3 = mysqli_query($conn, "SELECT taxableamt as subtot3, cgstrate from  puotmain   
                                       where vouchno = '$vouchno' and cgstrate = 1.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl5 = mysqli_query($conn, "SELECT taxableamt as subtot5, cgstrate from  puotmain   
                                       where vouchno = '$vouchno' and cgstrate = 2.50 group by cgstrate")or die(mysqli_error($conn));

                $taxbl12 = mysqli_query($conn, "SELECT taxableamt as subtot12, cgstrate from  puotmain  
                                            where vouchno = '$vouchno' and cgstrate = 6.00 ")or die(mysqli_error($conn));

                $taxbl18 = mysqli_query($conn, "SELECT taxableamt as subtot18, cgstrate from  puotmain  
                                       where vouchno = '$vouchno' and cgstrate=9.00 group by cgstrate")or die(mysqli_error($conn));

                $taxbl28 = mysqli_query($conn, "SELECT taxableamt as subtot28, cgstrate from  puotmain  
                                       where vouchno = '$vouchno' and cgstrate=14.00 group by cgstrate")or die(mysqli_error($conn));
            }


            $taxable0 = mysqli_fetch_array($taxbl0);
            $taxable3 = mysqli_fetch_array($taxbl3);
            $taxable5 = mysqli_fetch_array($taxbl5);
            $taxable12 = mysqli_fetch_array($taxbl12);
            $taxable18 = mysqli_fetch_array($taxbl18);
            $taxable28 = mysqli_fetch_array($taxbl28);


            $contents .= $ctr . ",";
            $contents .= $vouchno . ",";
            $contents .= $invno . ",";
            $contents .= $mydt . ",";
            $contents .= $gcode . ",";
            $contents .= $party . ",";
            $contents .= $gstno . ",";
            $contents .= $ptyped . ",";
            $contents .= $subtot . ",";
            $contents .= $totdisc . ",";
            $contents .= $taxable . ",";

            if ($oms == 1) {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $taxable0['subtot0'] . ",";
            $contents .= $gst0 . ",";
            $contents .= $taxable3['subtot3'] . ",";
            $contents .= $gst3 . ",";
            $contents .= $taxable5['subtot5'] . ",";
            $contents .= $gst5 . ",";
            $contents .= $taxable12['subtot12'] . ",";
            $contents .= $gst12 . ",";
            $contents .= $taxable18['subtot18'] . ",";
            $contents .= $gst18 . ",";
            $contents .= $taxable28['subtot28'] . ",";
            $contents .= $gst28 . ",";

            if ($oms == 1) {
                
            } else {
                $contents .= ",,,,,,,,,,,,";
            }

            $contents .= $billamt . "\n";

            $ogrsubtot = $ogrsubtot + $subtot;
            $ogrtotdis = $ogrtotdis + $totdisc;
            $ogrtottaxable = $ogrtottaxable + $taxable;

            if ($oms == 1) {
                $ogrtottaxbl0i = $ogrtottaxbl0i + $taxable0['subtot0'];
                $ogrtottaxbl3i = $ogrtottaxbl3i + $taxable3['subtot3'];
                $ogrtottaxbl5i = $ogrtottaxbl5i + $taxable5['subtot5'];
                $ogrtottaxbl12i = $ogrtottaxbl12i + $taxable12['subtot12'];
                $ogrtottaxbl18i = $ogrtottaxbl18i + $taxable18['subtot18'];
                $ogrtottaxbl28i = $ogrtottaxbl28i + $taxable28['subtot28'];

                $ogrtotgst3i = $ogrtotgst3i + $gst3;
                $ogrtotgst5i = $ogrtotgst5i + $gst5;
                $ogrtotgst12i = $ogrtotgst12i + $gst12;
                $ogrtotgst18i = $ogrtotgst18i + $gst18;
                $ogrtotgst28i = $ogrtotgst28i + $gst28;
            } else {
                $ogrtottaxbl0 = $ogrtottaxbl0 + $taxable0['subtot0'];
                $ogrtottaxbl3 = $ogrtottaxbl3 + $taxable3['subtot3'];
                $ogrtottaxbl5 = $ogrtottaxbl5 + $taxable5['subtot5'];
                $ogrtottaxbl12 = $ogrtottaxbl12 + $taxable12['subtot12'];
                $ogrtottaxbl18 = $ogrtottaxbl18 + $taxable18['subtot18'];
                $ogrtottaxbl28 = $ogrtottaxbl28 + $taxable28['subtot28'];

                $ogrtotgst3 = $ogrtotgst3 + $gst3;
                $ogrtotgst5 = $ogrtotgst5 + $gst5;
                $ogrtotgst12 = $ogrtotgst12 + $gst12;
                $ogrtotgst18 = $ogrtotgst18 + $gst18;
                $ogrtotgst28 = $ogrtotgst28 + $gst28;
            }

            $ogrtotbillamt = $ogrtotbillamt + $billamt;

            $ototpackamt = $ototpackamt + $omain['packamt'];
            $ototstamt = $ototstamt + $omain['stamt'];
            $ototothamt = $ototothamt + $omain['otheramt'];
            $ototbillamt = $ototbillamt + $omain['billamt'];


            if ($_POST["dtype"] == 2) {

                if ($gsttype == 1) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, IGST Rate,IGST Amount,Total\n";
                } elseif ($gsttype == 2) {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount,Total\n";
                } elseif ($gsttype == '') {
                    $contents .= "Sr, Item Name, HSN Code, Unit, Qty., Rate, Amount, Disc%, Disc. Amt., Taxable Amount, CGST Rate, CGST Amount, SGST Rate, SGST Amount, IGST Rate, IGST Amount,Total\n";
                }


                $ctr2 = 0;
                $ototqty = 0;
                $ototrate = 0;
                $ototamount = $ototdiscount = $ototsubtot = $ototcgstamt = $ototsgstamt = $ototigstamt = 0;

                $gettran = mysqli_query($conn, "SELECT * FROM `puottran` WHERE `vouchno` = '$pmain[vouchno]' ") or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

                    $getitem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` WHERE `code` = '$tran[itemcode]'") or die(mysqli_error($conn));
                    $showitem = mysqli_fetch_array($getitem);

                    $ctr2++;
                    $contents .= $ctr2 . ",";
                    $contents .= $tran['itemname'] . ",";
                    $contents .= $tran['hsncode'] . ",";
                    $contents .= $tran['unit'] . ",";
                    $contents .= $tran['qty'] . ",";
                    $contents .= $tran['rate'] . ",";
                    $contents .= $tran['amount'] . ",";
                    $contents .= $tran['discperc'] . ",";
                    $contents .= $tran['discamt'] . ",";
                    $contents .= $tran['subtot'] . ",";
                    if ($gsttype == 1) {
                        $contents .= $tran['igstrate'] . ",";
                        $contents .= $tran['igstamt'] . ",";
                    } elseif ($gsttype == 2) {
                        $contents .= $tran['cgstrate'] . ",";
                        $contents .= $tran['cgstamt'] . ",";
                        $contents .= $tran['sgstrate'] . ",";
                        $contents .= $tran['sgstamt'] . ",";
                    } elseif ($gsttype == '') {

                        $contents .= $tran['cgstrate'] . ",";
                        $contents .= $tran['cgstamt'] . ",";
                        $contents .= $tran['sgstrate'] . ",";
                        $contents .= $tran['sgstamt'] . ",";
                        $contents .= $tran['igstrate'] . ",";
                        $contents .= $tran['igstamt'] . ",";
                    }
                    $contents .= $tran['amount'] . "\n";

                    $ototqty = $ototqty + $tran['qty'];
                    $ototamount = $ototamount + $tran['amount'];
                    $ototdiscount = $ototdiscount + $tran['discamt'];
                    $ototsubtot = $ototsubtot + $tran['subtot'];
                    $ototcgstamt = $ototcgstamt + $tran['cgstamt'];
                    $ototsgstamt = $ototsgstamt + $tran['sgstamt'];
                    $ototigstamt = $ototigstamt + $tran['igstamt'];
                }


                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $ototqty . ",";
                $contents .= "" . ",";
                $contents .= $ototamount . ",";
                $contents .= "" . ",";
                $contents .= $ototdiscount . ",";
                $contents .= $ototsubtot . ",";
                if ($gsttype == 1) {
                    $contents .= "" . ",";
                    $contents .= $ototigstamt . ",";
                } elseif ($gsttype == 2) {
                    $contents .= "" . ",";
                    $contents .= $ototcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ototsgstamt . ",";
                } elseif ($gsttype == '') {

                    $contents .= "" . ",";
                    $contents .= $ototcgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ototsgstamt . ",";
                    $contents .= "" . ",";
                    $contents .= $ototigstamt . ",";
                }
                $contents .= $ototamount . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= $ogrsubtot . ",";
        $contents .= $ogrtotdis . ",";
        $contents .= $ogrtottaxable . ",";

        $contents .= $ogrtottaxbl0 . ",";
        $contents .= $ogrtotgst0 . ",";
        $contents .= $ogrtottaxbl3 . ",";
        $contents .= $ogrtotgst3 . ",";
        $contents .= $ogrtottaxbl5 . ",";
        $contents .= $ogrtotgst5 . ",";
        $contents .= $ogrtottaxbl12 . ",";
        $contents .= $ogrtotgst12 . ",";
        $contents .= $ogrtottaxbl8 . ",";
        $contents .= $ogrtotgst18 . ",";
        $contents .= $ogrtottaxbl28 . ",";
        $contents .= $ogrtotgst28 . ",";

        $contents .= $ogrtottaxbl0i . ",";
        $contents .= $ogrtotgst0i . ",";
        $contents .= $ogrtottaxbl3i . ",";
        $contents .= $ogrtotgst3i . ",";
        $contents .= $ogrtottaxbl5i . ",";
        $contents .= $ogrtotgst5i . ",";
        $contents .= $ogrtottaxbl12i . ",";
        $contents .= $ogrtotgst12i . ",";
        $contents .= $ogrtottaxbl18i . ",";
        $contents .= $ogrtotgst18i . ",";
        $contents .= $ogrtottaxbl28i . ",";
        $contents .= $ogrtotgst28i . ",";

        $contents .= $ogrtotbillamt . "\n";
    }
    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>