<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$maindb = 'challan';
$formname = "Challan List";
$file = "challan.php";

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

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn,$zdb);
$zroot = $_SESSION["zdir"];


$partymod = $_POST['partymod'];
//browse($partymod);

$mainsize = $_POST['mainsize'];
$sizetype = $_POST['sizetype'];
$mmsize = $_POST['mmsize'];
$mainguage = $_POST['mainguage'];
$guagetype = $_POST['guagetype'];
$mmgauge = $_POST['mmgauge'];
$mainmksize = $_POST['mainmksize'];
$maketype = $_POST['maketype'];
$fts = $_POST['mainfts'];

$makesizemm = $_POST['makesizemm'];


$partymcond = "";


$mainsizecond = "";
$stypecond = "";
$mmsizecond = "";

$mainguagecond = "";
$gtypecond = "";
$mmguagecond = "";

$mainmkcond = "";
$mtypecond = "";
$makesizemmcond = "";
$ftscond = "";

if (!empty($partymod)) {
    $partymcond = "AND `party`='$partymod' ";
}

//browse($partymcond);
#################### SIZE ####################
if (!empty($mainsize)) {
    $mainsizecond = "AND `size`='$mainsize' ";
}

if (!empty($sizetype)) {
    $stypecond = "AND `stype`='$sizetype' ";
}
if (!empty($mmsize)) {
    $mmsizecond = "AND `sizemm`='$mmsize' ";
}

################### GUAGE ###############

if (!empty($mainguage)) {
    $mainguagecond = "AND `guage`='$guagtype' ";
}

if (!empty($guagetype)) {
    $gtypecond = "AND `gtype`='$guagetype' ";
}

if (!empty($mmgauge)) {
    $mmguagecond = "AND `guagemm`='$mmguagecond' ";
}

################ MAKE #######################

if (!empty($mainmksize)) {
    $mainmkcond = "AND `makesize`='$mainmksize' ";
}
if (!empty($maketype)) {
    $mtypecond = "AND `mktype`='$maketype' ";
}
if (!empty($makesizemm)) {
    $makesizemmcond = "AND `makemm`='$makesizemm' ";
}


if (!empty($fts)) {
    $ftscond = "AND `foot`='$ftscond' ";
}

$getdata = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE vouchno != '' $partymcond $mainsizecond $stypecond $mmsizecond $mainguagecond $gtypecond 
                                    $mmguagecond $mainmkcond $mtypecond  $makesizemmcond") or die(mysqli_error($conn));
//print table($getdata);

include("includes/linkshead.php");
?>
<style>
    @media print
    {    
        .no-print, .no-print *
        {
            display: none !important;
        }
    }
    .page {
        width: 297mm;
        min-height: 210mm;
        padding: 4mm;
        margin: 10mm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
        background: white;

    }
    @page {
        size: A4;
        margin: 0;
    }
    @media print {
        html, body {
            width: 297mm;
            height: 210mm;        
        }
        .page {
            margin: 0;
            border: initial;
            border-radius: initial;
            width: initial;
            min-height: initial;
            box-shadow: initial;
            background: initial;
            page-break-after: always;
        }
    }

</style>
</head>
<body>
    <div class="page" >
        <?php include("includes/head_rep.php");
        ?>
        <label style="font-size:20px;">NEW STAR PIPE WORKS</label>
        <table  style="" class="table table-bordered   table-striped " width="250%">
            <thead class="mySearchTr">
                <tr style=" background-color: #64ccfc;">
                    <th style="width:1%;">SrL</th>
                    <th style="width:30%;">Date</th>
                    <th style="width:15%;">Delv.Date</th>
                    <th style="width:25%;">Party Name</th>
                    <th style="width:2%;"class='text-right'>Nags</th>
                    <th style="width:4%;" class='text-right'>Size(MM)</th>
                    <th style="width:4%;" class='text-right'>SWGS</th>
                    <th style="width:3%;" class='text-right'>Make(MM)</th>
                    <th style="width:2%;">Dia</th>
                    <th style="width:2%">A/P</th>
                    <th style="width:3%">Wld</th>
                    <th style="width:3%">STRT</th>
                    <th style="width:3%" class='text-right'>FTS</th>
                    <th style="width:4%" class='text-right'>T.FTS</th>
                    <th style="width:4%" class='text-right'>Weight</th>
                    <th style="text-align:right;width: 6%" class='text-right'>Rate</th>
                    <th style="text-align:right;width: 6%" class='text-right'>Amount</th>
                    <th style="text-align:right;width: 6%" class='text-right'>GST%</th>
                    <th style="text-align:right;width: 6%" class='text-right'>GST Amt</th>
                    <th style="text-align:right;width: 6%" class='text-right'>Bill Amt.</th>
                </tr>
            </thead>
            <?php
            $tname = '';
            $ctr = 0;
            $totfts = 0;
            $totwgt = 0;
            $totamt = 0;
            $totgstamt = 0;
            $totbill = 0;
            while ($fdata = mysqli_fetch_array($getdata)) {
                ?>
                <tbody>
                    <?php
                    $getparty = mysqli_query($conn,"SELECT `name` FROM `accmast` WHERE `accode`='$fdata[party]' ")or die(mysqli_error($conn));
                    $fparty = mysqli_fetch_array($getparty);
                    $partyname = $fparty['name'];
                    
                    $delvdt = $fdata[delvdate];
                    $vouchdt = $fdata[vouchdt];
                    $delvdate = date("d-m-Y", strtotime($delvdt));
                    $voucherdt = date("d-m-Y", strtotime($vouchdt));
                    $gstperc = $fdata[gstperc];
                    
                    $totalgst = $ftdata[totgst1] + $fdata[totgst5] + $fdata[totgst12] + $fdata[totgst18] + $fdata[totgst28] + $fdata[cgst1] +
                    $fdata[sgst1] + $fdata[igst1] + $fdata[cgst5] + $fdata[sgst5] + $fdata[igst5] +  $fdata[cgst12] + $fdata[sgst12] + $fdata[igst12] + $fdata[cgst18] + $fdata[sgst18] + 
                    $fdata[igst18]  + $fdata[cgst28] + $fdata[sgst28] + $fdata[igst28];         
                    
                    
                    $billamt = $fdata[billamt];
                    
                    
                    $ctr++;
                    echo "<tr > ";
                    echo "<td>$ctr</td>";
                    echo "<td>$voucherdt</td>";
                    echo "<td>$delvdate</td>";
                    echo "<td>$partyname</td>";
                    echo "<td class='text-right'>$fdata[nag]</td>";
                    echo "<td class='text-right'>$fdata[sizemm]</td>";
                    echo "<td class='text-right'>$fdata[guagemm]</td>";
                    echo "<td class='text-right'>$fdata[makemm]</td>";
                    echo "<td class='text-right'>$fdata[dia]</td>";
                    echo "<td>$fdata[ap]</td>";
                    echo "<td>$fdata[weld]</td>";
                    echo "<td>$fdata[straight]</td>";
                    echo "<td class='text-right'>$fdata[foot]</td>";
                    echo "<td class='text-right'>$fdata[totfeet]</td>";
                    echo "<td class='text-right'>$fdata[weight]</td>";
                    echo "<td class='text-right'>$fdata[rate]</td>";
                    echo "<td class='text-right'>$fdata[amount]</td>";
                    echo "<td class='text-right'>$gstperc</td>";
                    echo "<td class='text-right'>$totalgst</td>";
                    echo "<td class='text-right'>$fdata[billamt]</td>";
                    echo "</tr>";
                    $totwgt = $totwgt + $fdata[weight]; 
                    $totfts = $totfts + $fdata[foot];
                    $totalf = $totalf + $fdata[totfeet];
                    $totamt = $totamt + $fdata[amount];
                    $totgstamt = $totgstamt + $totalgst;
                    $totbill = $totbill + $fdata[billamt];
                }

                echo "<tr> ";
                echo "<td colspan='12' class='text-right' style=' color: red;'><b>Grand Total</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totfts</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totalf</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totwgt</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b></b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totamt</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b></b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totgstamt</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>$totbill</b></td>";
                echo "</tr>";
                ?> 
            </tbody>

        </table>

    </div>

    <?php
    include("includes/linksfoot.php");
    ?>
</body>
</html>
<?php
//}
//
//if (isset($_POST["excel"])) {
//
//    $contents .= "NEW STAR PIPE WORKS" . "\n";
//    while ($row = mysqli_fetch_array($gparty)) {
//        $ctr = 0;
//        $get = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt , DATE_FORMAT(`delvdate`,'%d-%m-%Y') as  delvdate   
//                                        FROM $maindb  WHERE  party = '$row[accode]' and `vouchdt`>= '$fromdate'  
//                                        AND  `vouchdt` <='$todate' $dtypecond  order by vouchno") or die(mysqli_error($conn));
//        $getno = mysqli_num_rows($get);
//
//
//        if ($getno > 0) {
//
//            $contents .= "Party - " . ",";
//            $contents .= "$row[name]" . "\n";
//
//
//            $contents .= "Sr., Date, P/R/D, Delv.Date, Description, Nags, Size(MM), T(MM), Make(MM), Dia, A/P, Wld, STRT, FTS, T.FTS, Weight, Rate, Amount\n";
//        }
//        $tname = '';
//        $fts = $amt = $ctr = 0;
//        while ($fdata = mysqli_fetch_array($get)) {
//            $ctr2++;
//
//            $contents .= "$ctr2" . ",";
//            $contents .= "$fdata[vouchdt]" . ",";
//            $contents .= "$fdata[delv]" . ",";
//            $contents .= "$fdata[delvdate]" . ",";
//            $contents .= "$fdata[descrip]" . ",";
//            $contents .= "$fdata[nag]" . ",";
//            $contents .= "$fdata[sizemm]" . ",";
//            $contents .= "$fdata[guagemm]" . ",";
//            $contents .= "$fdata[makemm]" . ",";
//            $contents .= "$fdata[dia]" . ",";
//            $contents .= "$fdata[ap]" . ",";
//            $contents .= "$fdata[weld]" . ",";
//            $contents .= "$fdata[straight]" . ",";
//            $contents .= "$fdata[foot]" . ",";
//            $contents .= "$fdata[totfeet]" . ",";
//            $contents .= "$fdata[weight]" . ",";
//            $contents .= "$fdata[rate]" . ",";
//            $contents .= "$fdata[amount]" . "\n";
//
//            $fts = $fts + $fdata[totfeet];
//            $amt = $amt + $fdata[amount];
//        }
//        if ($getno > 0) {
//
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "Total" . ",";
//            $contents .= "" . ",";
//            $contents .= "$fts" . ",";
//            $contents .= "" . ",";
//            $contents .= "" . ",";
//            $contents .= "$amt" . "\n";
//        }
//        $totfts = $totfts + $fts;
//        $totamt = $totamt + $amt;
//    }
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "GRAND TOTAL" . ",";
//    $contents .= "" . ",";
//    $contents .= "$totfts" . ",";
//    $contents .= "" . ",";
//    $contents .= "" . ",";
//    $contents .= "$totamt" . "\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;
//    }
//}
?>