<?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);

$maindb = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$file = 'itemwisereg_fr.php';


$prodgrp = $_POST["prodgrp"];
$product = $_POST["prodcode"];
$prodlike = $_POST["prodlike"];
$ol = $_POST["ol"];
$dtype = $_POST["dtype"];
$repby = $_POST["repby"];

if ($repby == 2) {
$report_type = "Amt.";
} else {
$report_type = "Qty.";
}

$formname = "Product Sale Register (" . $report_type . ")";
$pgrpcond = '';
$prodcond = '';
$prodlikecond = '';
$prodcond2s = '';
$olcond = '';
if (!empty($prodgrp)) {
$pgrpcond = " AND `prodgrp` = '$prodgrp' ";
}
if (!empty($ol)) {
$olcond = " AND `type` = '$ol' ";
}
if (!empty($product)) {
$prodcond = " AND `itemcode` = '$product' ";
$prodcond2 = " AND `prodcode` = '$product' ";
}
if (!empty($prodlike)) {
$prodlikecond = " AND `name` LIKE '%$prodlike%' ";
}
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");
}

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

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

//if($repby == 2) {
//    $mainqry = mysqli_query($conn, "SELECT itemcode, gram, unit, barcode, vouchdt, vouchno, source, netamt,
//                            DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `transacn` WHERE `vouchdt` >= '$mfrmdate' 
//                            AND `vouchdt` <= '$mtodate' $prodcond and (`source` = 'SL' or source = 'SR' or source = 'SLDE' or source = 'SLEX') 
//                            and itemcode in (SELECT prodcode from product where prodcode != '' $pgrpcond $prodcond2 $olcond $prodlikecond) group by barcode")or die(mysqli_error($conn));
//}else{
$mainqry = mysqli_query($conn, "SELECT itemcode, gram, unit, barcode, vouchdt, vouchno, source, 
                            DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `transacn` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $prodcond and (`source` = 'SL' or source = 'SR' or source = 'SLDE' or source = 'SLEX') 
                            and itemcode in (SELECT prodcode from product where prodcode != '' $pgrpcond $prodcond2 $olcond $prodlikecond) group by barcode")or die(mysqli_error($conn));
//}


$getno = mysqli_num_rows($mainqry);
if (isset($_POST["view"])) {
include("includes/linkshead.php");
?>
<style type="text/css">
    #tabital{
        font-style: italic;
    }
    #font1{
        font-style: italic;
    }
    #boldfont{
        font-weight: bold; 
    }
    th{
        background: #26A69A;
    }
    .dth{
        background:#BABFC2;
    }
    .colr{
        background: #26A69A;
        font-size: 17px;
    }
    table tr td{
        font-size: 14px;
    }
    total{
        font-size: 16px;

    }

</style>
</head>
<body>


    <div class="container" style="width:90%">
        <?php include("includes/head_rep.php"); ?>
        <div class="row">
            <div class="col-lg-12 pad">

                <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                    <thead class="mySearchTr">
                        <tr>
                            <th width='5%' >Sr</th>
                            <th width='6%' >Code</th>
                            <th width='40%'>Product Name</th>
                            <th width='6%' >Grammage</th>
                            <th width='7%' class="text-right">Local</th>
                            <th width='7%' class="text-right">Deemed</th>
                            <th width='7%' class="text-right">Export</th>
                            <? if ($repby == 2) { ?>
                            <th width='7%' class="text-right">Total Sale Amt.</th>    
                            <th width='7%' class="text-right">Total Return Amt.</th>    
                            <th width='7%' class="text-right">Net Amt.</th>    
                            <? } else {
                            ?>
                            <th width='8%' class="text-right">Total Sale Qty.</th>
                            <th width='7%' class="text-right">Return</th>
                            <th width='9%' class="text-right">Net Qty.</th>
                                <? } ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $grtotsalqty = $grtotretqty = $grtotqty = 0;
                        $grtotlocamt = $grtotdeeamt = $grtotexpamt = $grtotalamt = $grtotretamt = $grtotnetamt = 0;

                        while ($fmain = mysqli_fetch_array($mainqry)) {

                        $gsalqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SL'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
                        $fsalqty = mysqli_fetch_array($gsalqty);
                        $locqty = abs($fsalqty['qty']);
                        $locamt = $fsalqty['totalamt'];

                        $gdeeqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SLDE'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
                        $fdeeqty = mysqli_fetch_array($gdeeqty);
                        $deemqty = abs($fdeeqty['qty']);
                        $deemamt = $fdeeqty['totalamt'];

                        $gexpqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SLEX'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
                        $fexpqty = mysqli_fetch_array($gexpqty);
                        $expqty = abs($fexpqty['qty']);
                        $expamt = $fexpqty['totalamt'];

                        $gretnqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                         `barcode` = '$fmain[barcode]' AND `source`= 'SR' and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
                        $retnqty = mysqli_fetch_array($gretnqty);
                        $retqty = $retnqty['qty'];
                        $retamt = $retnqty['totalamt'];

                        $getitemname = mysqli_query($conn, "SELECT name from product where prodcode = '$fmain[itemcode]' ")or die(mysqli_error($conn));
                        $fitem = mysqli_fetch_array($getitemname);
                        $itemname = $fitem['name'];

                        if ($retnqty[qty] != '') {
                        $retqty = $retnqty[qty];
                        } else {
                        $retqty = 0;
                        }
                        $salqty = $locqty + $deemqty + $expqty;
                        $totqty = $salqty - $retqty;
                        $totalamt = $locamt + $deemamt + $expamt;
                        $netamt = $totalamt - $retamt;
                        $ctr++;

                        Echo"<tr>";
                        Echo"<td>$ctr</td>";
                        Echo"<td>$fmain[itemcode]</td>";
                        Echo"<td>$itemname</td>";
                        Echo"<td>$fmain[gram]</td>";
                        if ($repby == 2) {
                        Echo"<td class='text-right'>$locamt</td>";
                        Echo"<td class='text-right'>$deemamt</td>";
                        Echo"<td class='text-right'>$expamt</td>";
                        Echo"<td class='text-right'>$totalamt</td>";
                        Echo"<td class='text-right'>$retamt</td>";
                        Echo"<td class='text-right'>$netamt</td>";

                        $grtotlocamt = $grtotlocamt + $locamt;
                        $grtotdeeamt = $grtotdeeamt + $deemamt;
                        $grtotexpamt = $grtotexpamt + $expamt;
                        $grtotalamt = $grtotalamt + $totalamt;
                        $grtotretamt = $grtotretamt + $retamt;
                        $grtotnetamt = $grtotnetamt + $netamt;
                        } else {

                        Echo"<td class='text-right'>$locqty</td>";
                        Echo"<td class='text-right'>$deemqty</td>";
                        Echo"<td class='text-right'>$expqty</td>";
                        Echo"<td class='text-right'>$salqty</td>";
                        Echo"<td class='text-right'>$retqty</td>";
                        Echo"<td class='text-right'>$totqty</td>";

                        $grtotsalqty = $grtotsalqty + $salqty;
                        $grtotretqty = $grtotretqty + $retqty;
                        $grtotloc = $grtotloc + $locqty;
                        $grtotdee = $grtotdee + $deemqty;
                        $grtotexp = $grtotexp + $expqty;
                        $grtotqty = $grtotqty + $totqty;
                        }
                        Echo"</tr>";


                        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='mySearchTr2'>";
                        Echo "<th width='7%'  class='dth'>Sr</th>";
                        Echo "<th width='10%'  class='dth'>Date</th>";
                        Echo "<th width='10%'  class='dth'>Source</th>";
                        Echo "<th width='10%'  class='dth'>Voucher</th>";
                        Echo "<th width='33%'  class='dth'>Party Name</th>";
                        Echo "<th width='10%'  class='text-right dth'>Qty.</th>";
                        Echo "<th width='10%'  class='text-right dth'>Rate</th>";
                        Echo "<th width='10%'  class='text-right dth'>Amt.</th>";
                        Echo "</tr>";

                        $ctr2 = 0;
                        $totqty = 0;
                        $slqty = 0;
                        $totamt = 0;

                        $gettran = mysqli_query($conn, "SELECT `vouchno`, `source`, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt`, `accode`, `qty`, 
                                                            `rate` FROM `transacn` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                                            AND `barcode`='$fmain[barcode]' and (`source` = 'SL' or `source` = 'SLDE' or source = 'SLEX') ") or die(mysqli_error($conn));
                        while ($tran = mysqli_fetch_array($gettran)) {
                        $qty = abs($tran[qty]);



                        $getcust = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[accode]'") or die(mysqli_error($conn));
                        $customer = mysqli_fetch_array($getcust);

                        $amt = $qty * $tran[rate];



                        $ctr2++;
                        Echo "<tr>";
                        Echo "<td>$ctr2</td>";
                        Echo "<td>$tran[mydt]</td>";
                        Echo "<td>$tran[source]</td>";
                        Echo "<td>$tran[vouchno]</td>";
                        Echo "<td>$customer[name]</td>";
                        Echo "<td class='text-right'>$qty</td>";
                        Echo "<td class='text-right'>$tran[rate]</td>";
                        Echo "<td class='text-right'>$amt</td>";
                        Echo "</tr>";

                        $slqty = $slqty + $qty;
                        $totqty = $totqty + $qty;
                        $totamt = $totamt + $amt;
                        }

                        $getcred = mysqli_query($conn, "SELECT `vouchno`, `source`, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt`, `accode`, `qty`, 
                                                            `rate` FROM `transacn` WHERE `barcode`='$fmain[barcode]' and `source`='SR' ") or die(mysqli_error($conn));
                        $credtranno = mysqli_num_rows($getcred);

                        if ($credtranno > 0) {
                        $ctr3 = 0;
                        $credqty = 0;
                        while ($cred = mysqli_fetch_array($getcred)) {

                        $cdqty = abs($cred[qty]);

                        $gcust = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$cred[accode]'") or die(mysqli_error($conn));
                        $custm = mysqli_fetch_array($gcust);

                        $amount = $qty * $cred[rate];

                        $ctr3++;
                        Echo "<tr style='background-color:black'>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "</tr>";

                        Echo "<tr>";
                        Echo "<td>$ctr3</td>";
                        Echo "<td>$cred[mydt]</td>";
                        Echo "<td>$cred[source]</td>";
                        Echo "<td>$cred[vouchno]</td>";
                        Echo "<td>$custm[name]</td>";
                        Echo "<td class='text-right'>$cdqty</td>";
                        Echo "<td class='text-right'>$cred[rate]</td>";
                        Echo "<td class='text-right'>$amount</td>";
                        Echo "</tr>";

                        $credqty = $credqty + $cdqty;
                        }
                        $totqty = $slqty - $credqty;
                        }

                        Echo "<tr>";
                        Echo "<td class='dth'></td>";
                        Echo "<td class='dth'></td>";
                        Echo "<td class='dth'></td>";
                        Echo "<td class='dth'></td>";
                        Echo "<td class='text-right dth'>Total</td>";
                        Echo "<td class='text-right dth'>$totqty</td>";
                        Echo "<td class='text-right dth'></td>";
                        Echo "<td class='text-right dth'>$totamt</td>";
                        Echo "</tr>";
                        Echo "</table>";
                        }
                        }
                        if ($repby == 2) {
                        Echo "<tr>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='text-right colr'><b>Total</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotlocamt</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotdeeamt</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotexpamt</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotalamt</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotretamt</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotnetamt</b></td>";
                        Echo "</tr>";
                        } else {
                        Echo "<tr>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='colr'></td>";
                        Echo "<td class='text-right colr'><b>Total</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotloc</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotdee</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotexp</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotsalqty</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotretqty</b></td>";
                        Echo "<td class='numeric text-right colr '><b>$grtotqty</b></td>";
                        Echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div> 
        </div>
    </div>
    <?php include("includes/linksfoot.php");
    ?>
</body>
</html>
<?php
}

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

if ($repby == 2) {
$contents .= "Sr., Item Code, Name, Grammage, Unit, Total Sale Amt.,Total Return Amt., Net Amt.\n";
} else {
$contents .= "Sr., Item Code, Name, Grammage, Unit, Sale Qty., Ret. Qty., Total\n";
}

$ctr = 0;
$grtotsalqty = $grtotretqty = $grtotqty = 0;

while ($fmain = mysqli_fetch_array($mainqry)) {

$gsalqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SL'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
$fsalqty = mysqli_fetch_array($gsalqty);
$locqty = abs($fsalqty['qty']);
$locamt = $fsalqty['totalamt'];

$gdeeqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SLDE'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
$fdeeqty = mysqli_fetch_array($gdeeqty);
$deemqty = abs($fdeeqty['qty']);
$deemamt = $fdeeqty['totalamt'];

$gexpqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                        `barcode` = '$fmain[barcode]' AND `source`= 'SLEX'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
$fexpqty = mysqli_fetch_array($gexpqty);
$expqty = abs($fexpqty['qty']);
$expamt = $fexpqty['totalamt'];

$gretnqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(netamt) AS totalamt  FROM `transacn` where 
                                                         `barcode` = '$fmain[barcode]' AND `source`= 'SR' and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
$retnqty = mysqli_fetch_array($gretnqty);
$retqty = $retnqty['qty'];
$retamt = $retnqty['totalamt'];

$getitemname = mysqli_query($conn, "SELECT name from product where prodcode = '$fmain[itemcode]' ")or die(mysqli_error($conn));
$fitem = mysqli_fetch_array($getitemname);
$itemname = $fitem['name'];

if ($retnqty[qty] != '') {
$retqty = $retnqty[qty];
} else {
$retqty = 0;
}
$salqty = $locqty + $deemqty + $expqty;
$totqty = $salqty - $retqty;
$totalamt = $locamt + $deemamt + $expamt;
$netamt = $totalamt - $retamt;
$ctr++;





$contents .= $ctr. ",";
$contents .= $fmain['itemcode']. ",";
$contents .= $itemname. ",";
$contents .= $fmain['gram']. ",";
if ($repby == 2) {
$contents .= $locamt. ",";
$contents .= $deemamt. ",";
$contents .= $expamt. ",";
$contents .= $totalamt. ",";
$contents .= $retamt. ",";
$contents .= $netamt. "\n";

$grtotlocamt = $grtotlocamt + $locamt;
$grtotdeeamt = $grtotdeeamt + $deemamt;
$grtotexpamt = $grtotexpamt + $expamt;
$grtotalamt = $grtotalamt + $totalamt;
$grtotretamt = $grtotretamt + $retamt;
$grtotnetamt = $grtotnetamt + $netamt;
} else {

$contents .= $locqty. ",";
$contents .= $deemqty. ",";
$contents .= $expqty. ",";
$contents .= $salqty. ",";
$contents .= $retqty. ",";
$contents .= $totqty. "\n";

$grtotsalqty = $grtotsalqty + $salqty;
$grtotretqty = $grtotretqty + $retqty;
$grtotloc = $grtotloc + $locqty;
$grtotdee = $grtotdee + $deemqty;
$grtotexp = $grtotexp + $expqty;
$grtotqty = $grtotqty + $totqty;
}


$grtotsalqty = $grtotsalqty + $salqty;
$grtotretqty = $grtotretqty + $retqty;
$grtotqty = $grtotqty + $totqty;


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


$contents .= "Sr., Date, Source, Voucher, Party Name, Qty., Rate, Amt.,\n";


$ctr2 = 0;
$totqty = 0;
$slqty = 0;
$totamt = 0;

$gettran = mysqli_query($conn, "SELECT `vouchno`, `source`, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt`, `accode`, `qty`, 
                                                            `rate` FROM `transacn` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                                            AND `barcode`='$fmain[barcode]' and `source` = 'SL' ") or die(mysqli_error($conn));
while ($tran = mysqli_fetch_array($gettran)) {
$qty = abs($tran[qty]);



$getcust = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[accode]'") or die(mysqli_error($conn));
$customer = mysqli_fetch_array($getcust);

$amt = $qty * $tran[rate];



$ctr2++;

$contents .= $ctr2 . ",";
$contents .= $tran[mydt] . ",";
$contents .= $tran[source] . ",";
$contents .= $tran[vouchno] . ",";
$contents .= $customer[name] . ",";
$contents .= $qty . ",";
$contents .= $tran[rate] . ",";
$contents .= $amt . "\n";

$slqty = $slqty + $qty;
$totqty = $totqty + $qty;
$totamt = $totamt + $amt;
}

$getcred = mysqli_query($conn, "SELECT `vouchno`, `source`, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt`, `accode`, `qty`, 
                                                            `rate` FROM `transacn` WHERE `barcode`='$fmain[barcode]' and `source`='SR' ") or die(mysqli_error($conn));
$credtranno = mysqli_num_rows($getcred);

if ($credtranno > 0) {
$ctr3 = 0;
$credqty = 0;
while ($cred = mysqli_fetch_array($getcred)) {

$cdqty = abs($cred[qty]);

$gcust = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$cred[accode]'") or die(mysqli_error($conn));
$custm = mysqli_fetch_array($gcust);

$amount = $qty * $cred[rate];

$ctr3++;

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



$contents .= $ctr3 . ",";
$contents .= $cred[mydt] . ",";
$contents .= $cred[source] . ",";
$contents .= $cred[vouchno] . ",";
$contents .= $custm[name] . ",";
$contents .= $cdqty . ",";
$contents .= $cred[rate] . ",";
$contents .= $amount . "\n";


$credqty = $credqty + $cdqty;
}
$totqty = $slqty - $credqty;
}


$contents .= " " . ",";
$contents .= " " . ",";
$contents .= " " . ",";
$contents .= " " . ",";
$contents .= "Total" . ",";
$contents .= $totqty . ",";
$contents .= " " . ",";
$contents .= $totamt . "\n";
}
}


if ($repby == 2) {

$contents.='' . ",";
$contents.='' . ",";
$contents.='' . ",";
$contents.='Total'. ",";
$contents.=$grtotlocamt. ",";
$contents.=$grtotdeeamt. ",";
$contents.=$grtotexpamt. ",";
$contents.=$grtotalamt. ",";
$contents.=$grtotretamt. ",";
$contents.=$grtotnetamt. "\n";

} else {

$contents.='' . ",";
$contents.='' . ",";
$contents.='' . ",";

$contents.='Total' . ",";
$contents.=$grtotloc. ",";
$contents.=$grtotdee. ",";
$contents.=$grtotexp. ",";
$contents.=$grtotsalqty. ",";
$contents.=$grtotretqty. ",";
$contents.=$grtotqty. "\n";
}


$contents = strip_tags($contents);

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