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

$maindb = 'challan';
$formname = "Challan Register";
$file = "chalreg.php";

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

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


$party = $_POST[party];
$fromdate = $_POST[fromdate];
$todate = $_POST[todate];
$dtype = $_POST[dtype];
mysqli_select_db($conn, $zdb);

if (empty($_POST["fromdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; chal_print.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; chal_print.php");
}
$partycond = '';
$dtypecond = '';


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

if ($dtype == '1') {
    $dtypecond = " AND `delv` !='d' ";
} elseif ($dtype == '2') {
    $dtypecond = " AND `delv` ='d' ";
} elseif ($dtype == '3') {
    $dtypecond = "";
}

$mfdt = date("d-m-Y", strtotime($fromdate));
$mtdt = date("d-m-Y", strtotime($todate));

$get = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as mydt , DATE_FORMAT(`delvdate`,'%d-%m-%Y') as  delvdate   
                                    FROM $maindb  WHERE  `vouchdt`>= '$fromdate'  AND  `vouchdt` <='$todate' $partycond $dtypecond  
                                    order by vouchdt, vouchno ASC") or die(mysqli_error($conn));
//print table($get);
//die();

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
        .td{
            font-size: 6px;
        }
        .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;        
            }
            .td{
                font-size: 6px important;
            }
            .page {
                margin: 0;
                border: initial;
                border-radius: initial;
                width: initial;
                min-height: initial;
                box-shadow: initial;
                background: initial;
                page-break-after: always;
            }
        }
        .table td{
            font-size: 10px;
        }
        .table th{
            font-size: 11px;
        }

    </style>
    </head>
    <body>
        <div class="page" >
            <label style="font-size:18px;">NEW STAR PIPE WORKS</label>

            <table  style="" class="table table-bordered  table-hover table-striped ">
                <thead class="mySearchTr">
                    <tr style=" background-color: #78909C;">
                        <th style="width:2%;">Sr.</th>
                        <th style="width:1%;"> </th>
                        <th style="width:6%;">Date</th>
                        <th style="width:3%;">P/R/D</th>
                        <th style="width:6%;">Delivery</th>
                        <th style="width:15%;">Party</th>
                        <th style="width:10%;">Description</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'>T(MM)</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: 5%" class='text-right'>Rate</th>
                        <th style="text-align:right;width: 6%" class='text-right'>Amount</th>
                        <th style="text-align:right;width: 3%" class='text-right'>GST%</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $fts = $amt = $ctr = 0;
                    $tnag  = $tfeet = $twt = $tamt = 0 ;
                    
                    while ($fdata = mysqli_fetch_array($get)) {
                        $ctr++;
                        $getparty = mysqli_query($conn, "SELECT  `name` FROM accmast WHERE  accode = '$fdata[party]' ") or die(mysqli_error($conn));
                        $fetparty = mysqli_fetch_array($getparty);
                        $partyname = $fetparty['name'];

                        echo "<tr> ";
                        echo "<td>$ctr</td>";
                        echo "<td>$fdata[mark]</td>";
                        echo "<td>$fdata[mydt]</td>";
                        echo "<td>$fdata[delv]</td>";
                        echo "<td>$fdata[delvdate]</td>";
                        echo "<td>$partyname</td>";
                        echo "<td>$fdata[descrip]</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'>$fgstperc[name]</td>";

                        echo "</tr>";

                        $tnag  = $tnag  + $fdata['nag'];
                        $tfeet = $tfeet + $fdata['totfeet'];
                        $twt   = $twt + $fdata['weight'];
                        $tamt  = $tamt + $fdata['amount'];

                    }
                    echo "<tr> ";
                    echo "<td colspan='7' class='text-right' style=' color: red;' colspan='2'><b>Grand Total</b></td>";
                    echo "<td class='text-right' style=' color: red;'><b>$tnag</b></td>";
                    echo "<td colspan='9' class='text-right' style=' color: red;'><b>$tfeet</b></td>";
                    echo "<td class='text-right' style=' color: red;'><b>$twt</b></td>";
                    echo "<td  ></td>";
                    echo "<td class='text-right' style=' color: red;' ><b>$tamt </b></td>";
                    echo "<td></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;
    }
}
?>