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

$maindb = "salemain";
$trandb = "saletran";
$accdb = "actran";
$formname = "RM Daily Summary";
$file = "slipreg.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$dtye = $_POST["dtype"];
$name = $_POST["account"];
$shift = $_POST["shift"];
$product = $_POST["product"];
$para = $_POST["para"];

//$fromdate = $_POST["fromdate"];
//browse($fromdate);

$accountcond = "";
$custcodecond = "";
$productcond = "";


$shiftcond = " AND `shift`='$shift' ";
if ($shift == "") {
    browse("Shift Cannot be Empty");
    echo "<script>;"
    . "window.close();"
    . "</script>";
}

$getpdg = mysqli_query($conn, "SELECT * FROM `compyear`  where finyear= '$myyear'")or die(mysqli_error($conn));
$fetpdg = mysqli_fetch_array($getpdg);
$zprod1 = $fetpdg["prod1"];
$zprod2 = $fetpdg["prod2"];
$zprod3 = $fetpdg["prod3"];
$zprod4 = $fetpdg["prod4"];


mysqli_select_db($conn, $zdb);

$fromdate = $_POST["fromdate"];

//    $getpmain = mysqli_query($conn, "SELECT a.vouchno,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,a.shift,a.party,b.accode,b.name,b.custcode FROM `$maindb` a,accmast b where a.party= b.accode AND grpcode = '010' $accountcond ")or die(mysqli_error($conn));
if ($shift == 1) {
    $getacopen = mysqli_query($conn, "SELECT SUM(amount) as topen FROM `$accdb`  where accode = '3' and date < '$fromdate' $shiftcond")or die(mysqli_error($conn));
    $fetacopen = mysqli_fetch_array($getacopen);

    $topen = $fetacopen['topen'];
    $getopen = mysqli_query($conn, "SELECT openbal,dc FROM `accmast`  where accode = '3'")or die(mysqli_error($conn));
    $fetopen = mysqli_fetch_array($getopen);

    if ($fetopen['dc'] == "D") {
        $aopen = abs($fetopen['openbal']) * -1;
    } else {
        $aopen = abs($fetopen['openbal']);
    }
    $mopen = ($topen + $aopen) * -1;
} else {
    $getdlbal = mysqli_query($conn, "SELECT balance FROM `dailybal`  where trandate <= '$fromdate' AND shift < '$shift' ORDER BY trandate desc, shift")or die(mysqli_error($conn));
    $fetdlbal = mysqli_fetch_array($getdlbal);
    $mopen = $fetdlbal['balance'];
}


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: #6082BF;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:50%">Particulars</th>
                                    <th style="width:25%" class="text-right">Amount</th>
                                    <th style="width:25%"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;


                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>Opening Balance</td>";
                                Echo "<td  class='text-right'>$mopen</td>";
                                Echo "</tr>";

                                $getpd1 = mysqli_query($conn, "select * from prdmain where vouchdt = '$fromdate' $shiftcond")or die(mysqli_error($conn));
                                $fetpd1 = mysqli_fetch_array($getpd1);
                                $pcode = $fetpd1['code'];
                                $getsd1 = mysqli_query($conn, "select * from stkmain where vouchdt = '$fromdate' $shiftcond")or die(mysqli_error($conn));
                                $fetsd1 = mysqli_fetch_array($getsd1);
                                $stkcode = $fetsd1['vouchno'];

                                if ($zprod1 != "") {
                                    $getprod1 = mysqli_query($conn, "select * from pumpgrp where prodcode = '$zprod1' and  vouchno = '$fetpd1[vouchno]'")or die(mysqli_error($conn));
                                    $fetprod1 = mysqli_fetch_array($getprod1);

                                    $getpdname = mysqli_query($conn, "select * from product where prodcode = '$zprod1'")or die(mysqli_error($conn));
                                    $fetpdname = mysqli_fetch_array($getpdname);
                                    $pdname1 = $fetpdname['name'];

                                    $cashltr1 = $fetprod1['cashsale'];
                                    $cashamt1 = $fetprod1['cashamt'];
                                    if ($cashamt1 > 0) {
                                        $ctr = $ctr + 1;

                                        Echo "<tr id='boldfont'>";
                                        Echo "<td>$pdname1</td>";
                                        Echo "<td  class='text-right'>$cashamt1</td>";
                                        Echo "<td>$cashltr1 Ltrs.</td>";
                                        Echo "</tr>";
                                    }
                                }
                                if ($zprod2 != "") {
                                    $getprod2 = mysqli_query($conn, "select * from pumpgrp where prodcode = '$zprod2' and  vouchno = '$fetpd1[vouchno]'")or die(mysqli_error($conn));
                                    $fetprod2 = mysqli_fetch_array($getprod2);

                                    $getpdname2 = mysqli_query($conn, "select * from product where prodcode = '$zprod2'")or die(mysqli_error($conn));
                                    $fetpdname2 = mysqli_fetch_array($getpdname2);
                                    $pdname2 = $fetpdname2['name'];

                                    $cashltr2 = $fetprod2['cashsale'];
                                    $cashamt2 = $fetprod2['cashamt'];
                                    if ($cashamt2 > 0) {
                                        $ctr = $ctr + 1;

                                        Echo "<tr id='boldfont'>";
                                        Echo "<td>$pdname2</td>";
                                        Echo "<td  class='text-right'>$cashamt2</td>";
                                        Echo "<td>$cashltr2 Ltrs.</td>";
                                        Echo "</tr>";
                                    }
                                }
                                if ($zprod3 != "") {
                                    $getprod3 = mysqli_query($conn, "select * from pumpgrp where prodcode = '$zprod3' and  vouchno = '$fetpd1[vouchno]'")or die(mysqli_error($conn));
                                    $fetprod3 = mysqli_fetch_array($getprod3);

                                    $getpdname3 = mysqli_query($conn, "select * from product where prodcode = '$zprod3'")or die(mysqli_error($conn));
                                    $fetpdname3 = mysqli_fetch_array($getpdname3);
                                    $pdname3 = $fetpdname3['name'];

                                    $cashltr3 = $fetprod3['cashsale'];
                                    $cashamt3 = $fetprod3['cashamt'];
                                    if ($cashamt3 > 0) {
                                        $ctr = $ctr + 1;

                                        Echo "<tr id='boldfont'>";
                                        Echo "<td>$pdname3</td>";
                                        Echo "<td  class='text-right'>$cashamt3</td>";
                                        Echo "<td>$cashltr3 Ltrs.</td>";
                                        Echo "</tr>";
                                    }
                                }
                                if ($zprod4 != "") {
                                    $getprod4 = mysqli_query($conn, "select * from pumpgrp where prodcode = '$zprod4' and  vouchno = '$fetpd1[vouchno]'")or die(mysqli_error($conn));
                                    $fetprod4 = mysqli_fetch_array($getprod4);

                                    $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
                                    $fetpdname4 = mysqli_fetch_array($getpdname4);
                                    $pdname4 = $fetpdname4['name'];

                                    $cashltr4 = $fetprod4['cashsale'];
                                    $cashamt4 = $fetprod4['cashamt'];
                                    if ($cashamt4 > 0) {
                                        $ctr = $ctr + 1;

                                        Echo "<tr id='boldfont'>";
                                        Echo "<td>$pdname4</td>";
                                        Echo "<td  class='text-right'>$cashamt4</td>";
                                        Echo "<td>$cashltr4 Ltrs.</td>";
                                        Echo "</tr>";
                                    }
                                }


                                $getnoprod = mysqli_query($conn, "select sum(cashamt) as cashamt from stktran where prodcode NOT IN ('$zprod1','$zprod2','$zprod3','$zprod4') and vouchno = '$stkcode'")or die(mysqli_error($conn));
                                $fetnoprod = mysqli_fetch_array($getnoprod);

//                                $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
//                                $fetpdname4 = mysqli_fetch_array($getpdname4);
//                                $pdname4 = $fetpdname4['name'];
//                                $cashltr5 = $fetnoprod['cashsale'];
                                $cashamt5 = $fetnoprod['cashamt'];
                                if ($cashamt5 > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>OIL & ACCESSORIES</td>";
                                    Echo "<td  class='text-right'>$cashamt5</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getcontra = mysqli_query($conn, "select sum(amount) as tamt from contra where vouchdt = '$fromdate' $shiftcond and tobc in (select accode from accmast where grpcode = '002') ")or die(mysqli_error($conn));
                                $fetcontra = mysqli_fetch_array($getcontra);

//                                $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
//                                $fetpdname4 = mysqli_fetch_array($getpdname4);
//                                $pdname4 = $fetpdname4['name'];
//                                $cashltr5 = $fetnoprod['cashsale'];
                                $contamt = $fetcontra['tamt'];
                                if ($contamt > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>WITHDRAWAL FROM BANK</td>";
                                    Echo "<td  class='text-right'>$contamt</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getcashrecp = mysqli_query($conn, "select sum(amount) as tamt from cashrecp where vouchdt = '$fromdate' $shiftcond and trnflag = ''")or die(mysqli_error($conn));
                                $fetcashrecp = mysqli_fetch_array($getcashrecp);

//                                $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
//                                $fetpdname4 = mysqli_fetch_array($getpdname4);
//                                $pdname4 = $fetpdname4['name'];
//                                $cashltr5 = $fetnoprod['cashsale'];
                                $ocashamt = $fetcashrecp['tamt'];
                                if ($ocashamt > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>CASH RECEIPT OTHER THAN BANK</td>";
                                    Echo "<td  class='text-right'>$ocashamt</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $totin = $mopen + $cashamt1 + $cashamt2 + $cashamt3 + $cashamt4 + $cashamt5 + $contamt + $ocashamt;

                                Echo "<tr id='boldfont' style='color:green;' >";
                                Echo "<td class='text-right' style='font-size:20px;'><b>Total</b></td>";
                                Echo "<td  class='text-right' style='font-size:20px;'>$totin</td>";
                                Echo "<td></td>";
                                Echo "</tr>";


                                $getcashpay = mysqli_query($conn, "select sum(amount) as tamt from cashpay where vouchdt = '$fromdate' $shiftcond and party in (select accode from accmast where grpcode = '012')")or die(mysqli_error($conn));
                                $fetcashpay = mysqli_fetch_array($getcashpay);

//                                $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
//                                $fetpdname4 = mysqli_fetch_array($getpdname4);
//                                $pdname4 = $fetpdname4['name'];
//                                $cashltr5 = $fetnoprod['cashsale'];
                                $pcashamt = $fetcashpay['tamt'];
                                if ($pcashamt > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>TOTAL EXPENSE</td>";
                                    Echo "<td  class='text-right'>$pcashamt</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getcashpur = mysqli_query($conn, "select sum(amount) as tamt from cashpay where vouchdt = '$fromdate' and party in (select accode from accmast where grpcode = '004')")or die(mysqli_error($conn));
                                $fetcashpur = mysqli_fetch_array($getcashpur);

//                                $getpdname4 = mysqli_query($conn, "select * from product where prodcode = '$zprod4'")or die(mysqli_error($conn));
//                                $fetpdname4 = mysqli_fetch_array($getpdname4);
//                                $pdname4 = $fetpdname4['name'];
//                                $cashltr5 = $fetnoprod['cashsale'];
                                $cashpur = $fetcashpur['tamt'];
                                if ($cashpur > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>CASH PURCHASE</td>";
                                    Echo "<td  class='text-right'>$cashpur</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getcontra2 = mysqli_query($conn, "select sum(amount) as tamt from contra where vouchdt = '$fromdate' $shiftcond and frombc in (select accode from accmast where grpcode = '002') ")or die(mysqli_error($conn));
                                $fetcontra2 = mysqli_fetch_array($getcontra2);

                                $contamt2 = $fetcontra2['tamt'];
                                if ($contamt2 > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>DEPOSITED IN BANK</td>";
                                    Echo "<td  class='text-right'>$contamt2</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getcaca = mysqli_query($conn, "select sum(subtotal) as tamt from cacamain where vouchdt = '$fromdate' $shiftcond ")or die(mysqli_error($conn));
                                $fetcaca = mysqli_fetch_array($getcaca);

                                $cacaamt = $fetcaca['tamt'];
                                if ($cacaamt > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>CASH & CARRY</td>";
                                    Echo "<td  class='text-right'>$cacaamt</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }

                                $getocp = mysqli_query($conn, "select sum(amount) as tamt from cashpay where vouchdt = '$fromdate' and trnflag != 'D' and trnflag != 'C' $shiftcond and 
                                                               party not in (select accode from accmast where grpcode = '004' or grpcode = '012')")or die(mysqli_error($conn));
                                $fetocp = mysqli_fetch_array($getocp);

                                $ocpamt = $fetocp['tamt'];
                                if ($ocpamt > 0) {
                                    $ctr = $ctr + 1;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>OTHER CASH PAYMENT</td>";
                                    Echo "<td  class='text-right'>$ocpamt</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                }
                                $tcamt = 0;
                                $getcard = mysqli_query($conn, "SELECT owner, SUM(billamt) as total FROM cardsale WHERE vouchdt = '$fromdate' $shiftcond group BY owner")or die(mysqli_error($conn));
                                while ($fetcard = mysqli_fetch_array($getcard)) {

                                    $ccamt = $fetcard['total'];
                                    if ($ccamt > 0) {
                                        $ctr = $ctr + 1;
                                        $getpname = mysqli_query($conn, "select name from accmast where accode = '$fetcard[owner]'")or die(mysqli_error($conn));
                                        $fetpname = mysqli_fetch_array($getpname);

                                        Echo "<tr id='boldfont'>";
                                        Echo "<td>$fetpname[name]</td>";
                                        Echo "<td  class='text-right'>$ccamt</td>";
                                        Echo "<td></td>";
                                        Echo "</tr>";
                                        $tcamt = $tcamt + $ccamt;
                                    }
                                }

                                $totout = $pcashamt + $cashpur + $contamt2 + $ocpamt + $cacaamt + $tcamt;

                                Echo "<tr id='boldfont' style='color:red;' >";
                                Echo "<td class='text-right' style='font-size:20px;'><b>Total</b></td>";
                                Echo "<td  class='text-right' style='font-size:20px;'>$totout</td>";
                                Echo "<td></td>";
                                Echo "</tr>";
                                $gtot = $totin - $totout;
                                Echo "<tr id='boldfont' style='color:red;' >";
                                Echo "<td class='text-right' style='font-size:20px;'><b>Grand Total</b></td>";
                                Echo "<td  class='text-right' style='font-size:20px;'>$gtot</td>";
                                Echo "<td></td>";
                                Echo "</tr>";

                                mysqli_query($conn, "Delete from `dailybal` where trandate='$fromdate' and shift='$shift'");
                                mysqli_query($conn, "Insert  into dailybal( `trandate`, `balance`, `shift`) 
                                                                  Values ('$fromdate', '$gtot', '$shift')");


//                                detail part start
                                if ($para == "D") {
                                    Echo "<tr id='boldfont' style='color:red;height:25px' ></tr>";
                                    Echo "<tr id='boldfont' style='color:blue;'>";
                                    Echo "<td>Expense Details</td>";
                                    Echo "<td  class='text-right'>Amount</td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";

                                    $getcashpay = mysqli_query($conn, "select sum(amount) as tamt, party from cashpay where vouchdt = '$fromdate' $shiftcond and party in (select accode from accmast where grpcode = '012') group by party")or die(mysqli_error($conn));
                                    while ($fetcashpay = mysqli_fetch_array($getcashpay)) {

                                        $pcashamt = $fetcashpay['tamt'];
                                        if ($pcashamt > 0) {
                                            $ctr = $ctr + 1;
                                            $getpname = mysqli_query($conn, "select name from accmast where accode = '$fetcashpay[party]'")or die(mysqli_error($conn));
                                            $fetpname = mysqli_fetch_array($getpname);

                                            Echo "<tr id='boldfont'>";
                                            Echo "<td>$fetpname[name]</td>";
                                            Echo "<td  class='text-right'>$pcashamt</td>";
                                            Echo "<td></td>";
                                            Echo "</tr>";
                                        }
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <br>
                            <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th style="width:10%">Pump No.</th>
                                        <th style="width:20%">Product</th>

                                        <th style="width:10%" class="text-right">Opening</th>
                                        <th style="width:10%" class="text-right">Closing</th>
                                        <th style="width:10%" class="text-right">Sale</th>
                                        <th style="width:10%" class="text-right">Tank Total</th>
                                        <th style="width:10%" class="text-right">Tank</th>

                                    </tr>
                                </thead>
                                <?php
                                mysqli_query($conn, "Create temporary table pdent SELECT srno, pumpno, prodcode, prodname, opening, closing, sale FROM prdtran WHERE vouchno in 
                                                              (SELECT vouchno FROM prdmain WHERE vouchdt = '$fromdate' $shiftcond) ORDER BY pumpno")or die(mysqli_error($conn));


                                $getpmsale = mysqli_query($conn, "SELECT a.*, b.tankno, 0000000000.00 as cashamt FROM pdent a LEFT JOIN tanktran b ON a.pumpno = b.pumpno 
                                                              ORDER BY a.pumpno,b.tankno")or die(mysqli_error($conn));
                                $tsale = 0;
                                $ttankno = '';
                                $a1 = 0;


                                while ($fetpmsale = mysqli_fetch_array($getpmsale)) {

                                    $ptankno = $fetpmsale['tankno'];
                                    $msale = $fetpmsale['sale'];

                                    $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetpmsale[prodcode]'")or die(mysqli_error($conn));
                                    $fetpdname = mysqli_fetch_array($getpdname);
                                    echo "<tr>";
                                    echo "<td>$fetpmsale[pumpno]</td>";
                                    echo "<td>$fetpdname[name]</td>";
                                    echo "<td class='text-right'>$fetpmsale[opening]</td>";
                                    echo "<td class='text-right'>$fetpmsale[closing]</td>";
                                    echo "<td class='text-right'>$fetpmsale[sale]</td>";
                                    echo "<td class='text-right'></td>";
                                    echo "<td class='text-right'>$fetpmsale[tankno]</td>";
                                    echo "</tr>";
//                                $mtankno = $ttankno;
                                }
                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                            </table>

                            <br>
                            <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th style="width:10%">Product</th>
                                        <th style="width:10%" class="text-right">Total</th>
                                        <th style="width:10%" class="text-right">Account</th>
                                        <th style="width:10%" class="text-right">Testing</th>
                                        <th style="width:10%" class="text-right">Cash</th>
                                        <th style="width:10%" class="text-right">Cash Amount</th>

                                    </tr>
                                </thead>
                                <?php
                                $getpms = mysqli_query($conn, "SELECT a.*,b.prodcode,sum(b.sale) as total, sum(b.testing) as testing
                                                           from prdmain a, prdtran b where a.vouchno=b.vouchno and a.vouchdt = '$fromdate' and a.shift='$shift' 
                                                           group by b.prodcode ORDER BY b.pumpno")or die(mysqli_error($conn));


                                while ($fetpms = mysqli_fetch_array($getpms)) {

                                    $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetpms[prodcode]'")or die(mysqli_error($conn));
                                    $fetpdname = mysqli_fetch_array($getpdname);

                                    $getpdgrp = mysqli_query($conn, "select sum(credsale) as credsale, sum(cashsale) as cashsale, sum(cashamt) as cashamt from   pumpgrp
                                                                 where vouchno = '$fetpms[vouchno]' and prodcode = '$fetpms[prodcode]' group by prodcode")or die(mysqli_error($conn));
                                    $fetpdgrp = mysqli_fetch_array($getpdgrp);
                                    echo "<tr>";
                                    echo "<td>$fetpdname[name]</td>";
                                    echo "<td class='text-right'>$fetpms[total]</td>";
                                    echo "<td class='text-right'>$fetpdgrp[credsale]</td>";
                                    echo "<td class='text-right'>$fetpms[testing]</td>";
                                    echo "<td class='text-right'>$fetpdgrp[cashsale]</td>";
                                    echo "<td class='text-right'>$fetpdgrp[cashamt]</td>";
                                    echo "</tr>";
//                                $mtankno = $ttankno;
                                }
                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                            </table>

                            <br>
                            <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th style="width:25%">A/C Name</th>
                                        <th style="width:10%" class="">Vehicle</th>
                                        <th style="width:15%" class="">Product</th>
                                        <th style="width:5%" class="">Slip No.</th>
                                        <th style="width:7%" class="text-right">Qty.</th>
                                        <th style="width:7%" class="text-right">Rate</th>
                                        <th style="width:10%" class="text-right">Amount</th>

                                    </tr>
                                </thead>
                                <?php
                                $getcdsl = mysqli_query($conn, "SELECT b.srno, a.party, a.slipno, a.vehicleno, b.prodcode, b.qty, b.rate, b.amount, 00 as pumpno
                                                            FROM salemain a, saletran b WHERE a.vouchdt = '$fromdate' AND a.vouchno = b.vouchno $shiftcond")or die(mysqli_error($conn));
//                            print table($getcdsl);

                                while ($fetcdsl = mysqli_fetch_array($getcdsl)) {

                                    $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetcdsl[prodcode]'")or die(mysqli_error($conn));
                                    $fetpdname = mysqli_fetch_array($getpdname);
                                    $getacname = mysqli_query($conn, "select name from accmast where accode = '$fetcdsl[party]'")or die(mysqli_error($conn));
                                    $fetacname = mysqli_fetch_array($getacname);

//                                $getpdgrp = mysqli_query($conn, "select sum(credsale) as credsale, sum(cashsale) as cashsale, sum(cashamt) as cashamt from   pumpgrp
//                                                                 where vouchno = '$fetpms[vouchno]' and prodcode = '$fetpms[prodcode]' group by prodcode")or die(mysqli_error($conn));
//                                $fetpdgrp = mysqli_fetch_array($getpdgrp);
                                    echo "<tr>";
                                    echo "<td>$fetacname[name]</td>";
                                    echo "<td>$fetcdsl[vehicleno]</td>";
                                    echo "<td>$fetpdname[name]</td>";
                                    echo "<td>$fetcdsl[slipno]</td>";
                                    echo "<td class='text-right'>$fetcdsl[qty]</td>";
                                    echo "<td class='text-right'>$fetcdsl[rate]</td>";
                                    echo "<td class='text-right'>$fetcdsl[amount]</td>";
                                    echo "</tr>";
//                                $mtankno = $ttankno;
                                }
                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                                <tr>
                                    <th style="width:25%;background-color: white" colspan="4"></th>
                                    <th style="width:15%" class="">Product</th>
                                    <th style="width:7%" class="text-right">Qty.</th>
                                    <th style="width:10%" class="text-right">Amount</th>

                                </tr>
                                <?php
                                $getcdcn = mysqli_query($conn, "SELECT b.prodcode, sum(b.qty) as tqty, sum(b.amount) as tamount
                                                            FROM salemain a, saletran b WHERE a.vouchdt = '$fromdate'
                                                            AND a.vouchno = b.vouchno $shiftcond group by b.prodcode")or die(mysqli_error($conn));
                                while ($fetcdsl = mysqli_fetch_array($getcdcn)) {

                                    $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetcdsl[prodcode]'")or die(mysqli_error($conn));
                                    $fetpdname = mysqli_fetch_array($getpdname);
                                    echo "<tr>";
                                    echo "<td colspan='4'></td>";
                                    echo "<td>$fetpdname[name]</td>";
                                    echo "<td class='text-right'>$fetcdsl[tqty]</td>";
                                    echo "<td class='text-right'>$fetcdsl[tamount]</td>";
                                    echo "</tr>";
//                                $mtankno = $ttankno;
                                    $tqty = $tqty + $fetcdsl[tqty];
                                    $tamount = $tamount + $fetcdsl[tamount];
                                }
                                Echo "<tr id='boldfont' style='color:red;' >";
                                echo "<td colspan='4'></td>";

                                Echo "<td class='text-right' style='font-size:20px;'><b>Total</b></td>";
                                Echo "<td  class='text-right' style='font-size:20px;'>$tqty</td>";
                                Echo "<td  class='text-right' style='font-size:20px;'>$tamount</td>";
                                echo "</tr>";
                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                            }
                            ?>
                        </table>

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

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

    $contents .= "Sr,Voucher No.,Date,Vehicle No.,Account Name,Product,Shift\n";

    $ctr = 0;
    $totqty = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getname = mysqli_query($conn, "SELECT * FROM `accmast` where accode='$pmain[party]'")or die(mysqli_error($conn));
        $fetname = mysqli_fetch_array($getname);

        $nname = mysqli_query($conn, "SELECT * FROM `product` where code='$pmain[product]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($nname);

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[vehicleno]" . ",";
        $contents .= "$fetname[name]" . ",";
        $contents .= "$godwn[name]" . ",";
        $contents .= "$pmain[shift]" . "\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;
    }
}
?>