<?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="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;
                                                $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'];
                                                    $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'>$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;
                                                    $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>$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="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;
                                                $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'];
                                                    $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'>$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;
                                                    $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>$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="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;
                                                $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'>$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;
                                                    $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>$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="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;
                                                $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'];
                                                    $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'>$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;
                                                    $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>$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 ($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;

            $contents.= ",,,,,,,Purchase\n";
            $contents.=  ",Amount,Discount,Taxable Amount, GST, BillAmt.\n";
            
                $contents.=  $fpurraw['amt'] . ","
                $contents.=  $fpurraw['discount'] .  ",";
                $contents.=  $fpurraw['taxable'] . ",";
                $contents.=  $fpurraw['totgst'] . ",";
                $contents.=  $fpurraw['netamt'] . "\n";
            
            $contents.= "Purchase Raw Material" . ",";
            $contents.= $fpurraw['amt'] . ",";
            $contents.= $fpurraw['discount'] . ",";
            $contents.= $fpurraw['taxable'] . ",";
            $contents.= $fpurraw['totgst'] . ",";
            $contents.= $fpurraw['netamt'] . "\n";

            $contents.= "Purchase Packaging" . ",";
            $contents.= $fpurpack['amt'] . ",";
            $contents.= $fpurpack['discount'] . ",";
            $contents.= $fpurpack['taxable'] . ",";
            $contents.= $fpurpack['totgst'] . ",";
            $contents.= $fpurpack['netamt'] . "\n";

            $contents.= "Purchase Other" . ",";
            $contents.= $fpuroth['amt'] . ",";
            $contents.= $fpuroth['discount'] . ",";
            $contents.= $fpuroth['taxable'] . ",";
            $contents.= $fpuroth['totgst'] . ",";
            $contents.= $fpuroth['netamt'] . "\n";

            $contents.= "Purchase Expense" . ",";
            $contents.= $fpurexp['amt'] . ",";
            $contents.= $fpurexp['discount'] . ",";
            $contents.= $fpurexp['taxable'] . ",";
            $contents.= $fpurexp['totgst'] . ",";
            $contents.= $fpurexp['netamt'] . "\n";

            $contents.= "Total Purchase" . ",";
            $contents.= $totamt . ",";
            $contents.= $totdiscamt . ",";
            $contents.= $tottaxable . ",";
            $contents.= $totgst . ",";
            $contents.= $totnetamt . ",";

            $contents.= "Returns\n";
            $contents.= ",Amount,Discount,Taxable Amount, GST, Bill Amt.\n";


            $contents.= "Goods Returns" . ",";
            $contents.= $fdebitnoteg['amt'] . ",";
            $contents.= $fdebitnoteg['discount'] . ",";
            $contents.= $fdebitnoteg['taxable'] . ",";
            $contents.= $fdebitnoteg['totgst'] . ",";
            $contents.= $fdebitnoteg['netamt'] . "\n";

            $contents.= "Others" . ",";
            $contents.= $fdebitnoteo['amt'] . ",";
            $contents.= $fdebitnoteo['discount'] . ",";
            $contents.= $fdebitnoteo['taxable'] . ",";
            $contents.= $fdebitnoteo['totgst'] . ",";
            $contents.= $fdebitnoteo['netamt'] . "\n";

            $contents.= "Total Purchase Return" . ",";
            $contents.= $totretamt . ",";
            $contents.= $totretdiscamt . ",";
            $contents.= $totrettaxableamt . ",";
            $contents.= $totretgst . ",";
            $contents.= $totretnetamt . "\n";

            $contents.= "Total" . ",";
            $contents.= $grtotamt . ",";
            $contents.= $grtotdisc . ",";
            $contents.= $grtottaxable . ",";
            $contents.= $grtotgst . ",";
            $contents.= $grtotnetamt . "\n";


        <? } else {
                    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) {
                                $contents.= "Raw Material - Purchase Register\n";

                                $contents.= $getno . ","; 

                                     if ($ptype != "") { 
                                        $contents.= "Pay Type" . ",";
                                        $contents.= $paytype . ",";
                             } 
                             if ($ptype != "") { 
$contents.= "Sr.,Voucher, Invno, Date, Godown, Party Name, GST No,Pay Type, Subtotal,Disc Amt,GST 3%,GST %5,GST %12,GST %18,GST %28,Net Amount";
                             } 
                             else {
$contents.= "Sr.,Voucher, Invno, Date, Godown, Party Name, GST No,Subtotal,Disc Amt,GST 3%,GST %5,GST %12,GST %18,GST %28,Net Amount"; 
                            }       
                                            $ctr = 0;
                                            $totamt = 0;
                                            $totdiscamt = 0;
                                            $rtottaxable = 0;
                                            $rtotgst3 = 0;
                                            $rtotgst5 = 0;
                                            $rtotgst12 = 0;
                                            $rtotgst18 = 0;
                                            $rtotgst28 = 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'];
                                                $billamt = $pmain['billamt'];
                                                $subtot = $pmain['grossamt'];
                                                $totdisc = $pmain['discamt'];
                                                $taxable = $pmain['taxableamt'];

                                                $ctr++;
                                                $contents.= $ctr . ",";
                                                $contents.= $voucher . ",";
                                                $contents.= $invno . ",";
                                                $contents.= $mydt . ",";
                                                $contents.= $party . ",";
                                                $contents.= $gstno . ",";
                                                if ($ptype != "") {
                                                    $contents.= $ptyped . ",";
                                                }
                                                $contents.= $subtot . ",";
                                                $contents.= $totdisc . ",";
                                                $contents.= $taxable . ",";
                                                $contents.= $gst3 . ",";
                                                $contents.= $gst5 . ",";
                                                $contents.= $gst12 . ",";
                                                $contents.= $gst28 . ",";
                                                $contents.= $billamt . "\n";


                                                $rgrsubtot = $rgrsubtot + $subtot;
                                                $rgrtotdis = $rgrtotdis + $totdisc;
                                                $rgrtottaxable = $rgrtottaxable + $taxable;
                                                $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) {
$contents.= "Sr.,Item Name, HSN Code, Unit, Rate, Amount, Disc%, Disc Amt, Taxable Amount,GST %5,GST %12,GST %18,GST %28,Net Amount";
                                                    if ($gsttype == 1) {
                                                        $contents. = "IGST Rate,";
                                                        $contents. = "IGST Amount,";
                                                    } elseif ($gsttype == 2) {
                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "SGST Rate";
                                                        $contents.= "SGST Amount";
                                                    } elseif ($gsttype == '') {
                                                        
                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "SGST Rate";
                                                        $contents.= "SGST Amount";
                                                        $contents.= "IGST Rate";
                                                        $contents.= "IGST Amount";
                                                    }
                                                    $contents.= "\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++;
                                                        Echo "<tr>";
                                                        $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.= ",,,,," . ",";
                                            if ($ptype != "") {
                                                $contents.= "" . ",";
                                            }
                                            $contents.= $rgrsubtot . ",";
                                            $contents.= $rgrsubdis . ",";
                                            $contents.= $rgrtottaxable . ",";
                                            $contents.= $rgrtotgst3 . ",";
                                            $contents.= $rgrtotgst5 . ",";
                                            $contents.= $rgrtotgst12 . ",";
                                            $contents.= $rgrtotgst18 . ",";
                                            $contents.= $rgrtotgst28 . ",";
                                            $contents.= $rgrtotbillamt . ",";
                        }
                    }

                    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) {
                           $contents.= "Packing Material - Purchase Register\n";
                           $contents.= "Total No. Of Record , $getno2\n";

                            if ($ptype != "") { 
                            
                           $contents.= "Pay Type :" . ",";
                           $contents.= $paytype . ",";
                           
                        } 
                         if ($ptype != "") { 
                            
$contents.= "Sr, Voucher, Inovice No, Date, Godown, Party Name, GST No,Pay Type, Subtotal, Disc Amt, Taxable, GST 3, GST 5, GST 12, GST 18, GST 28,Net Amount\n";
                            } 
                            else {
                                $contents.= "Sr, Voucher, Inovice No, Date, Godown, Party Name, GST No, Subtotal, Disc Amt, Taxable, GST 3, GST 5, GST 12, GST 18, GST 28,Net Amount\n";
                            }
                                            <?php
                                            $pctr = 0;
                                            $ptotamt = 0;
                                            $ptotdiscamt = 0;
                                            $ptottaxable = 0;
                                            $ptotgst3 = 0;
                                            $ptotgst5 = 0;
                                            $ptotgst12 = 0;
                                            $ptotgst18 = 0;
                                            $ptotgst28 = 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'];
                                                $billamt = $pmain['billamt'];
                                                $subtot = $pmain['grossamt'];
                                                $totdisc = $pmain['discamt'];
                                                $taxable = $pmain['taxableamt'];

                                                $pctr++;

                                                $contents.= $pctr . ",";
                                                $contents.= $voucher . ",";
                                                $contents.= $invno . ",";
                                                $contents.= $mydt . ",";
                                                $contents.= $gcode . ",";
                                                $contents.= $party . ",";
                                                $contents.= $gstno . ",";
                                                if ($ptype != "") {
                                                    $contents.= $ptyped . ",";
                                                }
                                                $contents.= $subtot . ",";
                                                $contents.= $totdisc . ",";
                                                $contents.= $taxable . ",";
                                                $contents.= $gst3 . ",";
                                                $contents.= $gst5 . ",";
                                                $contents.= $gst12 . ",";
                                                $contents.= $gst18 . ",";
                                                $contents.= $gst28 . ",";
                                                $contents.= $billamt . "\n";

                                                $pgrsubtot = $pgrsubtot + $subtot;
                                                $pgrtotdis = $pgrtotdis + $totdisc;
                                                $pgrtottaxable = $pgrtottaxable + $taxable;
                                                $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) {
                                                    $contents.= "Sr, Item Name, HSN Code, Unit, QTy., Rate, Amount, Disc%, Disc Amt, Taxable Amt, Total\n";
                                                    if ($gsttype == 1) {
                                                        $contents.= "IGST Rate";
                                                        $contents.= "IGST Amt";
                                                    } elseif ($gsttype == 2) {
                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "SGST Rate";
                                                        $contents.= "SGST Amount";
                                                    } elseif ($gsttype == '') {
                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "IGST Rate";
                                                        $contents.= "IGST Amount";
                                                    }
//
//
                                                    $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++;
                                                        $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] . "\n"; 
                                                        if ($gsttype == 1) {
                                                            $contents.= $tran[discamt] . ","; 
                                                            $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[igstamt] . "\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.= ",,,Total\n"; 


                                                    $cotents.= $ptotqty . ",";
                                                    $cotents.= "," . ",";
                                                    $cotents.= $ptotamount . ",";
                                                    $cotents.= "," . ",";
                                                    $cotents.= $ptotdiscount . ",";
                                                    $cotents.= $ptotsubtot . ",";
                                                    if ($gsttype == 1) {
                                                        $cotents.= "," . ",";
                                                        $cotents.= $ptotigstamt . "\n";
                                                    } elseif ($gsttype == 2) {
                                                        $cotents.= "," . ",";
                                                        $cotents.= $ptotcgstamt . ",";
                                                        $cotents.= "," . ",";
                                                        $cotents.= $ptotsgstamt . ",";
                                                    } elseif ($gsttype == '') {
                                                        $cotents.= "," . ",";
                                                        $cotents.= $ptotcgstamt . ",";
                                                        $cotents.= "," . ",";
                                                        $cotents.= "$ptotsgstamt" . ",";
                                                        $cotents.= "," . ",";
                                                        $cotents.= $ptotigstamt . ",";
                                                    }
                                                    $cotents.= $ptotamount . "\n";
                                                }
                                            }
                                            $contents.= ",,,,,," . ",";
                                            if ($ptype != "") {
                                                
                                                $contents.= " " . ",";
                                                Echo "<td></td>";

                                            }

                                            $contents.= "Total" . ",";
                                            $contents.= $pgrsubtot . ",";
                                            $contents.= $pgrtotdis . ",";
                                            $contents.= $pgrtottaxable . ",";
                                            $contents.= $pgrtotgst3 . ",";
                                            $contents.= $pgrtotgst5 . ",";
                                            $contents.= $pgrtotgst12 . ",";
                                            $contents.= $pgrtotgst18 . ",";
                                            $contents.= $pgrtotgst28 . ",";
                                            $contents.= $pgrtotbillamt . "\n";
                        }
                    }

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

                            $contents.= "Other Material - Purchase Register\n";
                            $contents.= "Total No Of Records , $getno3\n";
                                     if ($ptype != "") { 
                                        
                                        $contents.= "Pay Type" . ",";
                                        $contents.= $paytype . "";
                                     } 
                                     if ($ptype != "") { 
                                    $contents.= "Sr., Voucher, Invoice No, Date, Godown, Party Name, GST No, Pay Type, Sub Total, Disc Amt.,Taxable, GST 3%, GST 5%, GST12%, GST18, GST28, Net Amt\n";
                                }
                                else{
                                    $contents.= "Sr., Voucher, Invoice No, Date, Godown, Party Name, GST No, Sub Total, Disc Amt.,Taxable, GST 3%, GST 5%, GST12%, GST18, GST28, Net Amt\n";
                                } 
                                            <?php
                                            $octr = 0;
                                            $ototamt = 0;
                                            $ototdiscamt = 0;
                                            $otottaxable = 0;
                                            $ototgst3 = 0;
                                            $ototgst5 = 0;
                                            $ototgst12 = 0;
                                            $ototgst18 = 0;
                                            $ototgst28 = 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>";
                                                $contents.= $octr . ",";
                                                $contents.= $voucher . ",";
                                                $contents.= $invno . ",";
                                                $contents.= $mydt . ",";
                                                $contents.= $gcode . ",";
                                                $contents.= $party . ",";
                                                $contents.= $gstno . ",";
                                                if ($ptype != "") {
                                                    $contents.= $ptyped . ",";
                                                }
                                                $contents.= $subtot . ",";
                                                $contents.= $totdisc . ",";
                                                $contents.= $taxable . ",";
                                                $contents.= $gst3 . ",";
                                                $contents.= $gst5 . ",";
                                                $contents.= $gst12 . ",";
                                                $contents.= $gst18 . ",";
                                                $contents.= $gst28 . ",";
                                                $contents.= $billamt . "\n";
                                                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;
                                                $ogrtotbillamt = $ogrtotbillamt + $billamt;

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


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

                                                        $contents.="IGST Rate";
                                                        $contents.="IGST Amount";
                                                    } elseif ($gsttype == 2) {

                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "SGST Rate";
                                                        $contents.= "SGST Amount";
                                                    } elseif ($gsttype == '') {
                                                        
                                                        $contents.= "CGST Rate";
                                                        $contents.= "CGST Amount";
                                                        $contents.= "SGST Rate";
                                                        $contents.= "SGST Amount";
                                                        $contents.= "IGST Rate";
                                                        $contents.= "IGST Amount";
                                                    }
                                                    $contents.= "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] ."\n";
                                                        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.= ",,,,Total" . ",";
                                                    $contents.= "" . ",";
                                                    $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.= $totigstamt .",";
                                                    }
                                                    $contents.= $ototamount .",";
                                                }
                                            }
                                            $contents.= ",,,,,," .",";
                                            if ($ptype != "") {
                                                $contents.= "" . ",";
                                            }
                                            $contents.= "Total" . ",";
                                            $contents.= $ogrsubtot . ",";
                                            $contents.= $ogrtotdis . ",";
                                            $contents.= $ogrtottaxable . ",";
                                            $contents.= $ogrtotgst3 . ",";
                                            $contents.= $ogrtotgst5 . ",";
                                            $contents.= $ogrtotgst12 . ",";
                                            $contents.= $ogrtotgst18 . ",";
                                            $contents.= $ogrtotgst28 . ",";
                                            $contents.= $ogrtotgst28 . ",";
                                            $contents.= $ogrtotbillamt . ",";
                        }
                    }

                    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) {
                                $contents.= "Purchase Expense - Purchase Register\n"
                                $contents.="Total No Of Records :,$getno4\n";
                                     if ($ptype != "") { 
                                        $contents.= "Pay Type" . ",";
                                        $contents.="$paytype" . ",";
                                     }
                                      if ($ptype != "") { 
                                     $contents.= "Sr, Voucher, Invoice No, Date, Godown, Party Name, GST No,Pay Type, Sub Total, Disc Amt, Taxable, GST 3, GST 5,GST 12, GST 18, GST 28, Net Amount\n";
                                     } 
                                     else {
                                        $contents.= "Sr, Voucher, Invoice No, Date, Godown, Party Name, GST No, Sub Total, Disc Amt, Taxable, GST 3, GST 5,GST 12, GST 18, GST 28, Net Amount\n";
                                     }
                                            <?php
                                            $pctr = 0;
                                            $ptotamt = 0;
                                            $ptotdiscamt = 0;
                                            $ptottaxable = 0;
                                            $ptotgst3 = 0;
                                            $ptotgst5 = 0;
                                            $ptotgst12 = 0;
                                            $ptotgst18 = 0;
                                            $ptotgst28 = 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'];
                                                $billamt = $pmain['billamt'];
                                                $subtot = $pmain['grossamt'];
                                                $totdisc = $pmain['discamt'];
                                                $taxable = $pmain['taxableamt'];

                                                $pctr++;
                                             
                                                $contents.= $pctr . ",";
                                                $contents.= $voucher . ",";
                                                $contents.= $invno . ",";
                                                $contents.= $mydt . ",";
                                                $contents.= $gcode . ",";
                                                $contents.= $party . ",";
                                                $contents.= $gstno . ",";
                                                if ($ptype != "") {
                                                    $contents.= $ptyped . ",";
                                                }
                                                $contents.= $subtot . ",";
                                                $contents.= $totdisc . ",";
                                                $contents.= $taxable . ",";
                                                $contents.= $gst3 . ",";
                                                $contents.= $gst5 . ",";
                                                $contents.= $gst12 . ",";
                                                $contents.= $gst18 . ",";
                                                $contents.= $gst28 . ",";
                                                $contents.= $billamt . "\n";


                                                $pgrsubtot = $pgrsubtot + $subtot;
                                                $pgrtotdis = $pgrtotdis + $totdisc;
                                                $pgrtottaxable = $pgrtottaxable + $taxable;
                                                $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) {
                                                    $contents.= ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n";
                                                    $contents.= "Sr. Item Name, HSN Code, Unit, QTy., Rate, Amount, Disc%, Disc Amt, Taxable Amount";
                                                    if ($gsttype == 1) {
                                                    $contents.= "IGST Rate";    
                                                    $contents.= "IGST Amount";    
                                                    } elseif ($gsttype == 2) {
                                                        $contents.= "CGST Rate";    
                                                        $contents.= "CGST Amt";    
                                                        $contents.= "SGST Rate";    
                                                        $contents.= "SGST Amount";    
                                                    } elseif ($gsttype == '') {
                                                        
                                                        $contents.= "CGST Rate";    
                                                        $contents.= "CGST Amt";    
                                                        $contents.= "SGST Rate";    
                                                        $contents.= "SGST Amount";  
                                                        $contents.= "IGST Rate";  
                                                        $contents.= "IGST Amount";  
                                                        Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                                    }
                                                    $contents.= "Total" . ",";
//
//
                                                    $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++;
                                                        $contents.= $ctr2 . ",";
                                                        $contents.= $tran[name] . ",";
                                                        $contents.= $tran[hsncode] . ",";
                                                        $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";

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

                                                    $contents.= ",,," . ",";
                                                    $contents.= "Total" . ",";
                                                    $contents.= "," . ",";
                                                    $contents.= "," . ",";
                                                    $contents.= $etotamtount . ",";
                                                    $contents.= "," . ",";
                                                    $contents.= $etotdiscount . ",";
                                                    $contents.= $etotsubtot . ",";
                                                    if ($gsttype == 1) {
                                                        $contents.= "," . ",";
                                                        $contents.= $etotigstamt . ",";
                                                    } elseif ($gsttype == 2) {
                                                        $contents.= "," . ",";
                                                        $contents.= $etotcgstamt . ",";
                                                        $contents.= "," . ",";
                                                        $contents.= $etotsgstamt . ",";
                                                    } elseif ($gsttype == '') {
                                                        $contents.= "," . ",";
                                                        $contents.= $etotcgstamt . ",";
                                                        $contents.= "," . ",";
                                                        $contents.= $etotsgstamt . ",";
                                                        $contents.= "," . ",";
                                                        $contents.= $etotigstamt . ",";
                                                    }
                                                    $contents.= $ptotamount . ",";
                                                }
                                            }
                                            $contents.= ",,,,,," . ","; 
                                            if ($ptype != "") {
                                            $contents.= "," . ",";
                                        }
                                            $contents.= "Total" . ","; 
                                            $contents.=  $pgrsubtot . ","; 
                                            $contents.=  $pgrtotdis . ","; 
                                            $contents.=  $pgrtottaxable . ","; 
                                            $contents.=  $pgrtotgst3 . ","; 
                                            $contents.=  $pgrtotgst5 . ","; 
                                            $contents.=  $pgrtotgst12 . ","; 
                                            $contents.=  $pgrtotgst18 . ","; 
                                            $contents.=  $pgrtotgst28 . ","; 
                                            $contents.=  $pgrtotbillamt . "m"; 
                    }
                }
            }

            if ($supplier == '') {
             
                        $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));
                       $contents.= "Entries Other Than Purchase\n";

                       $contents.= "Source, Amounnt\n";

                            $sctr = 0;
                            while ($fsource = mysqli_fetch_array($sourceqry)) {
                                $famt = $fsource['amt'] * -1;
                                $sctr++;
                                ?>
                                $contents.= $sctr . ",";    
                                $contents.= $fsource['source'] . ",";    
                                $contents.= $famt . "\n";    
                                $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));
                                ?>
                                $contents.= "Sr, Date, Vouchno, Amount\n"
                                
                                 $scrt2 = 0;
                                while ($ftran = mysqli_fetch_array($tranqry)) {

                                    $sctr2++;
                                    ?>
                                    $contents.= $sctr2 . ",";
                                    $contents.= $ftran['date'] . ",";
                                    $contents.= $ftran['vouchno'] . ",";
                                    $contents.= $ftran['amt'] . "\n";
                                }
                                $totamt = $totamt + $famt;
                            }
                            $contents.= ",,,Total " . ",";
                            $contents.= $totamt . "\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;
    }
}
?>