<?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 = 'prodwisesale_fr.php';

$prodgrp = $_POST["prodgrp"];
$product = $_POST["product"];
$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 = '';
if (!empty($prodgrp)) {
    $pgrpcond = " AND `prodgrp` = '$prodgrp' ";
}

if (!empty($product)) {
    $prodcond = " AND `itemcode` = '$product' ";
    $prodcond2 = " AND `code` = '$product' ";
}
if (!empty($prodlike)) {
    $prodlikecond = " AND `name` LIKE '%$prodlike%' ";
}
if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; prodwisesale.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; prodwisesale.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, 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 = 'SLVT')
                            and itemcode in (SELECT code from product where code != '' $pgrpcond $prodcond2 $prodlikecond) group by itemcode")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">

                    <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='7%' class="text-left">Unit</th>
                                <!--<th width='7%' class="text-right">Sale</th>-->
                                <!--<th width='7%' class="text-right">Online</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='7%' class="text-right">Total Sale Qty.</th>
                                    <th width='7%' class="text-right">Return</th>
                                    <th width='7%' 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)) {
                                
                                  mysqli_query($conn, "drop temporary table if exists itempl ") or die(mysqli_error($conn));
                                  mysqli_query($conn, "create temporary table itempl 
                                                               SELECT `barcode`,SUM(qty) AS qty, rate, SUM(amount) AS totalamt FROM `saletran` where 
                                                        `itemcode` = '$fmain[itemcode]'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate' and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate')") or die(mysqli_error($conn));
                                //   mysqli_query($conn, "drop temporary table if exists itempl ") or die(mysqli_error($conn));
                                //   mysqli_query($conn, "create temporary table itempl 
                                //                                SELECT `barcode`,SUM(qty) AS qty, rate, SUM(amount) AS totalamt FROM `saletran` where 
                                //                         `itemcode` = '$fmain[itemcode]'  and `vouchdt` >= '$mfrmdate' 
                                //                          AND `vouchdt` <= '$mtodate' and 
                                //                          vouchno in (select vouchno from salemain where `vouchdt` >= '$mfrmdate' 
                                //                          AND `vouchdt` <= '$mtodate' and canc_flag != 'C')
                                //                          UNION ALL    
                                //                          SELECT `barcode`,SUM(qty) AS qty, rate, SUM(amount) AS totalamt FROM `vattran` where 
                                //                         `itemcode` = '$fmain[itemcode]'  and `vouchdt` >= '$mfrmdate' 
                                //                          AND `vouchdt` <= '$mtodate' and 
                                //                          vouchno in (select vouchno from vatmain where `vouchdt` >= '$mfrmdate' 
                                //                          AND `vouchdt` <= '$mtodate' and canc_flag != 'C')") or die(mysqli_error($conn));
                                  
                                $gsalqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, rate, SUM(totalamt) AS totalamt FROM `itempl` group by barcode order by barcode ") or die(mysqli_error($conn));
                                $fsalqty = mysqli_fetch_array($gsalqty);
                                $locqty = abs($fsalqty['qty']);
                                $locamt = $fsalqty['totalamt'];

//                                $gonlsalqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(subtot) AS totalamt FROM `slonltran` where 
//                                                        `barcode` = '$fmain[barcode]'  and `vouchdt` >= '$mfrmdate' 
//                                                         AND `vouchdt` <= '$mtodate' and 
//                                                         vouchno in (select vouchno from slonlmain where `vouchdt` >= '$mfrmdate' 
//                                                         AND `vouchdt` <= '$mtodate' and canc_flag != 'C')") or die(mysqli_error($conn));
//                                $fonlsalqty = mysqli_fetch_array($gonlsalqty);
//                                $onlqty = abs($fonlsalqty['qty']);
//                                $onlamt = $fonlsalqty['totalamt'];


                                $gretnqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(subtot) AS totalamt  FROM `credtran` where 
                                                         `itemcode` = '$fmain[itemcode]' 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,stkunit from product where code = '$fmain[itemcode]' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($getitemname);
                                $itemname = $fitem['name'];
                                $stkunit = $fitem['stkunit'];
                                
                                $gunit = mysqli_query($conn, "SELECT code,name FROM $zroot.`unit` WHERE `code`='$stkunit'") or die(mysqli_error($conn));
                                $funit = mysqli_fetch_array($gunit);
                                
                                if ($retnqty['qty'] != '') {
                                    $retqty = $retnqty['qty'];
                                } else {
                                    $retqty = 0;
                                }
                                $salqty = $locqty + $deemqty + $expqty + $onlqty;
                                $totqty = $salqty - $retqty;
                                $totalamt = $locamt + $deemamt + $expamt + $onlamt;
                                $netamt = $totalamt - $retamt;
                                $ctr++;

                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
//                                Echo"<td>$fmain[itemcode]</td>";
                                Echo"<td>$itemname</td>";
                                Echo"<td>$funit[name]</td>";
                                if ($repby == 2) {
//                                    Echo"<td class='text-right'>$locamt</td>";
//                                    Echo"<td class='text-right'>$onlamt</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;
//                                    $grtotonlamt = $grtotonlamt + $onlamt;
                                    $grtotalamt = $grtotalamt + $totalamt;
                                    $grtotretamt = $grtotretamt + $retamt;
                                    $grtotnetamt = $grtotnetamt + $netamt;
                                } else {

//                                    Echo"<td class='text-right'>$locqty</td>";
//                                    Echo"<td class='text-right'>$onlqty</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;
                                    $grtotonl = $grtotonl + $onlqty;
                                    $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 `itemcode`='$fmain[itemcode]' and (`source` = 'SL' or `source` = 'SLVT' ) ") 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 `itemcode`='$fmain[itemcode]' 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 style='color: blue; font-size: 14px;'>";
                               Echo "<td ></td>";
                                Echo "<td ></td>";
//                                Echo "<td ></td>";
                                Echo "<td class='text-right '><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>$grtotlocamt</b></td>";
//                                Echo "<td class='numeric text-right'><b>$grtotonlamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$grtotalamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$grtotretamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$grtotnetamt</b></td>";
                                Echo "</tr>";


//                                $getcredother = mysqli_query($conn, "SELECT  SUM(taxableamt) as totamt FROM `credmain` WHERE `credtype`='O' and `vouchdt`>='$mfrmdate' AND `vouchdt`<='$mtodate'") or die(mysqli_error($conn));
//                                $fcredother = mysqli_fetch_array($getcredother);
//                                $scdiffamt = $fsaleother['totamt'] - $fcredother['totamt'];
                                $mtotlocamt = $grtotlocamt;
                                $mtotonlamt = $grtotonlamt;
                                $mtotalamt = $grtotalamt;
                                $mtotretamt = $grtotretamt;
                                $mtotnetamt = $grtotnetamt;

                                Echo "<tr style='background-color: #98F9F0; color: red'>";
                                Echo "<td class='text-right' colspan='2'><b>Grand Total</b></td>";
//                                Echo "<td class='numeric text-right'><b>$mtotlocamt</b></td>";
//                                Echo "<td class='numeric text-right'><b>$mtotonlamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$mtotalamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$mtotretamt</b></td>";
                                Echo "<td class='numeric text-right'><b>$mtotnetamt</b></td>";
                                Echo "</tr>";
                            } else {
                                Echo "<tr style='background-color: #98F9F0;'>";
                                Echo "<td class=''></td>";
                               Echo "<td class=''></td>";
//                                Echo "<td class=''></td>";
                                Echo "<td class='text-right '><b>Total</b></td>";
//                                Echo "<td class='numeric text-right '><b>$grtotloc</b></td>";
//                                Echo "<td class='numeric text-right '><b>$grtotonl</b></td>";
                                Echo "<td class='numeric text-right '><b>$grtotsalqty</b></td>";
                                Echo "<td class='numeric text-right '><b>$grtotretqty</b></td>";
                                Echo "<td class='numeric text-right '><b>$grtotqty</b></td>";
                                Echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

if (isset($_POST["excel"])) {
    $contents .= "Sr.,Product Name,";
    if ($repby == 2) {
        $contents .= "Total Sale Amt.,Total Return Amt.,Net Amt.\n";
    } else {
        $contents .= "Total Sale Qty.,Return,Net Qty.\n";
    }
    $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, rate, SUM(amount) AS totalamt FROM `saletran` where 
                                                        `itemcode` = '$fmain[itemcode]'  and `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate' and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$mfrmdate' 
                                                         AND `vouchdt` <= '$mtodate' and canc_flag != 'C')") or die(mysqli_error($conn));
        $fsalqty = mysqli_fetch_array($gsalqty);
        $locqty = abs($fsalqty['qty']);
        $locamt = $fsalqty['totalamt'];

//                                $gonlsalqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(subtot) AS totalamt FROM `slonltran` where 
//                                                        `barcode` = '$fmain[barcode]'  and `vouchdt` >= '$mfrmdate' 
//                                                         AND `vouchdt` <= '$mtodate' and 
//                                                         vouchno in (select vouchno from slonlmain where `vouchdt` >= '$mfrmdate' 
//                                                         AND `vouchdt` <= '$mtodate' and canc_flag != 'C')") or die(mysqli_error($conn));
//                                $fonlsalqty = mysqli_fetch_array($gonlsalqty);
//                                $onlqty = abs($fonlsalqty['qty']);
//                                $onlamt = $fonlsalqty['totalamt'];


        $gretnqty = mysqli_query($conn, "SELECT `barcode`,SUM(qty) AS qty, SUM(subtot) AS totalamt  FROM `credtran` where 
                                                         `itemcode` = '$fmain[itemcode]' 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 code = '$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 + $onlqty;
        $totqty = $salqty - $retqty;
        $totalamt = $locamt + $deemamt + $expamt + $onlamt;
        $netamt = $totalamt - $retamt;
        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$itemname" . ",";
        if ($repby == 2) {
            $contents .= "$totalamt" . ",";
            $contents .= "$retamt" . ",";
            $contents .= "$netamt" . "\n";


            $grtotlocamt = $grtotlocamt + $locamt;
//                                    $grtotonlamt = $grtotonlamt + $onlamt;
            $grtotalamt = $grtotalamt + $totalamt;
            $grtotretamt = $grtotretamt + $retamt;
            $grtotnetamt = $grtotnetamt + $netamt;
        } else {
            $contents .= "$salqty" . ",";
            $contents .= "$retqty" . ",";
            $contents .= "$totqty" . "\n";

            $grtotsalqty = $grtotsalqty + $salqty;
            $grtotretqty = $grtotretqty + $retqty;
            $grtotloc = $grtotloc + $locqty;
            $grtotonl = $grtotonl + $onlqty;
            $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 `itemcode`='$fmain[itemcode]' 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 `itemcode`='$fmain[itemcode]' 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 .= "Total" . ",";
        $contents .= $grtotlocamt . ",";
        $contents .= $grtotalamt . ",";
        $contents .= $grtotretamt . ",";
        $contents .= $grtotnetamt . "\n";

        $mtotlocamt = $grtotlocamt;
        $mtotonlamt = $grtotonlamt;
        $mtotalamt = $grtotalamt;
        $mtotretamt = $grtotretamt;
        $mtotnetamt = $grtotnetamt;

        $contents .= "Grand Total" . ",";
        $contents .= $mtotalamt . ",";
        $contents .= $mtotretamt . ",";
        $contents .= $mtotnetamt . "\n";
    } else {  


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