<?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 = 'custvehicle';
$trandb = 'accmast';
$zroot = $_SESSION["zdbroot"];

$formname = 'Vehicle Wise Register';
$zroot = $_SESSION["zdir"];
$file = 'vehqry_fr.php';

$vehicleno = $_POST["vehicleno"];
//
$vehiclecond = "";
//
    if (!empty($vehicleno)) {
        $vehiclecond = "AND `vehicleno`='$vehicleno' ";
    }

    if (empty($vehicleno)) {
       browse(" Vehicle Cannot Be Empty");
       echo"<script>window.close();</script>"; 
    }



    $getpmain = mysqli_query($conn, "SELECT a.*, b.name, b.accode, b.custcode, b.add1 , b.add2 , b.city , b.country, b.pin , b.email FROM `$maindb` a, accmast b   
                           where a.accode = b.accode $vehiclecond order by b.name")or die(mysqli_error($conn));
//    print table($getpmain);
//    die();
    $get = mysqli_fetch_array($getpmain);

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: #4EB3C7;
        }
        .td{
            font-size: 15px;
            padding-right: 30px;
        }

    </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">
                        <div class="col-lg-6">    
                            <div class="row">
                                <table>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Customer Code</b></td>

                                        <td class="td"><?php echo $get['custcode']?> </td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Customer</b></td>

                                        <td class="td"><?php echo $get['name']?> </td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Address</b></td>

                                        <td class="td"><?php echo $get['add1']?></td>

                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Pin</b></td>

                                        <td class="td"><?php echo $get['pin']?></td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Country</b></td>

                                        <td class="td"><?php echo $get['country']?></td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Mobile No.</b></td>

                                        <td class="td"><?php echo $get['mobile']?></td>
                                        
                                    </tr>
                                    
                                    <tr>
                                        
                                        <td class="td"><b>Email</b></td>

                                        <td class="td"><?php echo $get['email']?></td>
                                        
                                    </tr>
                                    
                                </table>

                            </div>
                        </div>

                           <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                               <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:8%">Vehicle No.</th>
                                    <th style="width:8%">Description</th>
                                    <th style="width:8%">Product</th>
                                </tr>
                                </thead>
                                <tbody>
                                    
                        <?php
                             
                        mysqli_data_seek($getpmain, 0);

                        $ctr2 = 0;
                        while ($tran = mysqli_fetch_array($getpmain)) {

                            $ctr2++;
                            Echo "<tr>";
                            Echo "<td>$ctr2</td>";
                            Echo "<td>$tran[vehicleno]</td>";
                            Echo "<td>$tran[description]</td>";
                            Echo "<td>$tran[product]</td>";
                            Echo "</tr>";
                        }
                        ?>
                        </tbody>
                        </table>
                       
                    </div>
                </div>
            </div>
    <?php include("includes/linksfoot.php");
    ?>
    </body>
    </html>
            <?php
    }

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

            $dtye = $_POST["dtype"];

            if (!empty($dtype)) {
                $dtypecond = " AND `dtype`='$dtype' ";
            }

            if (empty($_POST["frmdt"])) {
                browse("From Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }
            if (empty($_POST["todt"])) {
                browse("To Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }

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

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

            $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
            $get = mysqli_num_rows($getpmain);

            $contents .= "Sr,Voucher,Date,Godown,Total Qty.\n";

            $ctr = 0;
            $totqty = 0;

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

                $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]'")or die(mysqli_error($conn));
                $godwn = mysqli_fetch_array($ggodwn);

                $ctr++;

                $contents .= $ctr . ",";
                $contents .= $pmain[vouchno] . ",";
                $contents .= $pmain[mydt] . ",";
                $contents .= $godwn[gcode] . ",";
                $contents .= $pmain[totqty] . "\n";

                $totqty = $totqty + $pmain[totqty];

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

                    $contents .= "Sr.,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Batch,Qty.,Remark\n";

                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                    $ctr2 = 0;
                    while ($tran = mysqli_fetch_array($gettran)) {

                        $ctr2++;

                        $contents .= $ctr2 . ",";
                        $contents .= $tran[barcode] . ",";
                        $contents .= $tran[itemcode] . ",";
                        $contents .= $tran[itemname] . ",";
                        $contents .= $tran[hsncode] . ",";
                        $contents .= $tran[gram] . ",";
                        $contents .= $tran[unit] . ",";
                        $contents .= $tran[batch] . ",";
                        $contents .= $tran[qty] . ",";
                        $contents .= $tran[remark] . "\n";

                        $trtotqty = $trtotqty + $tran[qty];
                    }

                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= " " . ",";
                    $contents .= "Total" . ",";
                    $contents .= $trtotqty . ",";
                    $contents .= " " . "\n";
                }
            }

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