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

$maindb = 'pordmain';
$trandb = 'pordtran';

$formname = "Order Query ";
$file = "ordformreg_fr.php";


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);




if (isset($_POST["view"])) {
    $supp = $_POST["supp"];
    $branch = $_POST["company"];
    $item = $_POST["item"];
    $qty = $_POST["qty"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];



    $suppcond = "";
    $branchcond = "";
    $itemcond = "";
    $qtycond = "";


    if (!empty($supp)) {
        $suppcond = " AND a.`party` = '$supp' ";
    }

    if (!empty($branch)) {
        $suppcond = " AND a.`company`='$branch' ";
    }

    if (!empty($item)) {
        $itemcond = " AND b.`descrip` = '$item' ";
    }

    if (!empty($qty)) {
        $qtycond = " AND b.`qty`='$qty' ";
    }



    if (empty($frmdt)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; ordqueryrep.php");
    }
    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; ordqueryrep`.php");
    }
    $mfrmdate = $frmdt;
    $mtodate = $todt;


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

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt, b.* FROM `$maindb` a,$trandb b WHERE a.vouchno = b.vouchno
            and a.`vouchdt` >= '$frmdt' AND a.`vouchdt` <= '$todt' $suppcond $suppcond $itemcond $qtycond order by a.vouchno")or die(mysqli_error($conn));



    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
        .th1{
            background-color: #78909C;            
        }
        .th2{
            background-color: #85AFC3;            
        }
    </style>
    </head>
    <body>
        <div class="container">

            <?php
            include("includes/head_rep.php");
            ?>


            <div class="col-lg-12 col-sm-12 pad">
                <table style="width:100%;background: white;" class="table table-condensed table-hover table-striped table-bordered">
                    <thead class="th1">
                        <tr style="">
                            <th style="width:3%;">Sr</th>
                            <th style="width:7%;">Ord. No</th>
                            <th style="width:9%;">Order Date</th>
                            <th style="width:24%;">Party</th>
                            <th style="width:24%;">Company</th>
                            <th style="width:24%;">Item</th>
                            <th style="width:6%;" class='text-right'>Qty</th>
                            <th style="width:5%;">Unit</th>
                            <th style="width:6%;" class='text-right'>Rate</th>
                            <th style="width:6%;" class='text-right'>Amount</th>
                            <th style="width:5%;" class='text-right'>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = $totqty = $totrate = $totamt = 0;



                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $gparty = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[party]'")or die(mysqli_error($conn));
                            $sparty = mysqli_fetch_array($gparty);

                            $gcomp = mysqli_query($conn, "SELECT code, compname FROM `branch` where code='$pmain[company]'")or die(mysqli_error($conn));
                            $scomp = mysqli_fetch_array($gcomp);

                            $gitem = mysqli_query($conn, "SELECT code, name FROM `item` where code='$pmain[descrip]'")or die(mysqli_error($conn));
                            $sitem = mysqli_fetch_array($gitem);






                            $ctr++;

                            Echo "<tr>";
                            Echo "<td><b>$ctr</b></th>";
                            Echo "<td><b>$pmain[vouchno]</b></td>";
                            Echo "<td><b>$pmain[vouchdt]</b></td>";
                            Echo "<td><b>$sparty[name]</b></td>";
                            Echo "<td><b>$scomp[compname]</b></td>";
                            Echo "<td><b>$sitem[name]</b></td>";
                            Echo "<td class='text-right'><b>$pmain[qty]</b></td>";
                            Echo "<td><b>$pmain[unit]</b></td>";
                            Echo "<td class='text-right'><b>$pmain[rate]</b></td>";
                            Echo "<td class='text-right'><b>$pmain[amt]</b></td>";
                            Echo "<td class='text-center'><a href=\"pord.php?mode=edit&vouchno=$pmain[vouchno]\" target='_blank' >
                                    <i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></th>";
                            Echo "</tr>";
                            $totqty = $totqty + $pmain[qty];
                            $totrate = $totrate + $pmain[rate];
                            $totamt = $totamt + $pmain[amt];
                        }
                        Echo "<tr>";
                        Echo "<td><b></b></th>";
                        Echo "<td><b></b></th>";
                        Echo "<td><b></b></th>";
                        Echo "<td><b></b></th>";
                        Echo "<td><b></b></th>";
                        Echo "<td class='text-right text-danger'><b>Total</b></th>";
                        Echo "<td class='text-right text-danger'><b>$totqty</b></th>";
                        Echo "<td><b></b></th>";
                        Echo "<td class='text-right text-danger'><b>$totrate</b></th>";
                        Echo "<td class='text-right text-danger'><b>$totamt</b></th>";
                        Echo "<td><b></b></th>";

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




if (isset($_POST["subpdf"])) {
    $supp = $_POST["supp"];
    $branch = $_POST["company"];
    $item = $_POST["item"];
    $qty = $_POST["qty"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];



    $suppcond = "";
    $branchcond = "";
    $itemcond = "";
    $qtycond = "";


    if (!empty($supp)) {
        $suppcond = " AND a.`party` = '$supp' ";
    }

    if (!empty($branch)) {
        $suppcond = " AND a.`company`='$branch' ";
    }

    if (!empty($item)) {
        $itemcond = " AND b.`descrip` = '$item' ";
    }

    if (!empty($qty)) {
        $qtycond = " AND b.`qty`='$qty' ";
    }



    if (empty($frmdt)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; ordqueryrep.php");
    }
    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; ordqueryrep`.php");
    }
    $mfrmdate = $frmdt;
    $mtodate = $todt;


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

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt, b.* FROM `$maindb` a,$trandb b WHERE a.vouchno = b.vouchno
            and a.`vouchdt` >= '$frmdt' AND a.`vouchdt` <= '$todt' $suppcond $suppcond $itemcond $qtycond order by a.vouchno")or die(mysqli_error($conn));



    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



            //Position at 1.5 cm from bottom

            $this->SetY(-15);

            //Arial italic 8

            $this->SetFont('Arial', 'I', 8);

            //Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, 'BEST MARINE PVT LTD', 0);
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, '112 MINT ROAD FORT MARKET', 0);
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(200, 7, "Print - " . $formname, 0, 'R');
    $pdf->Cell(100, 7, "Report Date - " . Date("d-m-y"), 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(50, 10, 'From Date' . $mfrmdate, 0);
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, 'To Date' . $mtodate, 0);
    $pdf->Ln();


//$pdf->SetMargins(1,1,1);
//$pdf->SetXY(5,5);
    $pdf->SetFont('Arial', 'BI', 8);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(177, 156, 217);
    $pdf->Cell(10, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(10, 8, 'Ord.No', 1, 0, '', 1);
    $pdf->Cell(20, 8, 'OrderDate', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Party', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Company', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Item', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Qty', 1, 0, '', 1);
    $pdf->Cell(100, 8, 'Unit', 1, 0, '', 1);
    $pdf->Cell(100, 8, 'Rate', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Amt', 1, 0, '', 1);
    $pdf->Ln();


    $ctr = $totqty = $totrate = $totamt = 0;



    while ($pmain = mysqli_fetch_array($getpmain)) {
        $gparty = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[party]'")or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $gcomp = mysqli_query($conn, "SELECT code, compname FROM `branch` where code='$pmain[company]'")or die(mysqli_error($conn));
        $scomp = mysqli_fetch_array($gcomp);

        $gitem = mysqli_query($conn, "SELECT code, name FROM `item` where code='$pmain[descrip]'")or die(mysqli_error($conn));
        $sitem = mysqli_fetch_array($gitem);
        $ctr++;

        $pdf->SetFont('Arial', '', 7);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(10, 8, $ctr, 1, 0);
        $pdf->Cell(10, 8, $pmain[vouchno], 1, 0);
        $pdf->Cell(20, 8, $pmain[vouchdt], 1, 0);
        $pdf->Cell(50, 8, $sparty[name], 1, 0);
        $pdf->Cell(50, 8, $scomp[compname], 1, 0);
        $pdf->Cell(50, 8, $sitem[name], 1, 0);
        $pdf->Cell(100, 8, $pmain[qty], 1, 0);
        $pdf->Cell(100, 8, $pmain[unit], 1, 0);
        $pdf->Cell(100, 8, $pmain[rate], 1, 0);
        $pdf->Cell(100, 8, $pmain[amt], 1, 0);
        $totqty = $totqty + $pmain[qty];
        $totrate = $totrate + $pmain[rate];
        $totamt = $totamt + $pmain[amt];
        $pdf->Ln();
    }


    $pdf->SetFont('Arial', 'BI', 8);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(177, 156, 217);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, "Total", 1, 0, '', 1);
    $pdf->Cell(30, 8, $totqty, 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Cell(30, 8, $totrate, 1, 0, '', 1);
    $pdf->Cell(30, 8, $totamt, 1, 0, '', 1);
    $pdf->Cell(30, 8, ".", 1, 0, '', 1);
    $pdf->Ln();


    $pdf->Output('OrderQuerry.pdf', 'D');
}

if (isset($_POST["excel"])) {
    $supp = $_POST["supp"];
    $branch = $_POST["company"];
    $item = $_POST["item"];
    $qty = $_POST["qty"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];



    $suppcond = "";
    $branchcond = "";
    $itemcond = "";
    $qtycond = "";


    if (!empty($supp)) {
        $suppcond = " AND a.`party` = '$supp' ";
    }

    if (!empty($branch)) {
        $branchcond = " AND a.`company`='$branch' ";
    }

    if (!empty($item)) {
        $itemcond = " AND b.`descrip` = '$item' ";
    }

    if (!empty($qty)) {
        $qtycond = " AND b.`qty`='$qty' ";
    }



    if (empty($frmdt)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; ordqueryrep.php");
    }
    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; ordqueryrep`.php");
    }
    $mfrmdate = $frmdt;
    $mtodate = $todt;


    $fdate = date("d-m-Y", strtotime($mfrmdate));
    $tdate = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt, b.* FROM `$maindb` a,$trandb b WHERE a.vouchno = b.vouchno
            and a.`vouchdt` >= '$frmdt' AND a.`vouchdt` <= '$todt' $suppcond $suppcond $itemcond $qtycond order by a.vouchno")or die(mysqli_error($conn));


    $contents .= "Sr.No,Order No,Order Date,Party,Company,Item,Qty,Unit,Rate,Amount,\n";

    $ctr = 1;
    $totqty = $totrate = $totamt = 0;



    while ($pmain = mysqli_fetch_array($getpmain)) {
        $gparty = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[party]'")or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $gcomp = mysqli_query($conn, "SELECT code, compname FROM `branch` where code='$pmain[company]'")or die(mysqli_error($conn));
        $scomp = mysqli_fetch_array($gcomp);

        $gitem = mysqli_query($conn, "SELECT code, name FROM `item` where code='$pmain[descrip]'")or die(mysqli_error($conn));
        $sitem = mysqli_fetch_array($gitem);

        $pvouchno = $pmain[vouchno];
        $pvouchdt = $pmain[vouchdt];
        $sname = $sparty[name];
        $scompname = $scomp[compname];
        $siname = $sitem[name];
        $pqty = $pmain[qty];
        $punit = $pmain[unit];
        $prate = $pmain[rate];
        $pamt = $pmain[amt];

        $contents .= $ctr . ",";
        $contents .= $pvouchno . ",";
        $contents .= $pvouchdt . ",";
        $contents .= $sname . ",";
        $contents .= $scompname . ",";
        $contents .= $siname . ",";
        $contents .= $pqty . ",";
        $contents .= $punit . ",";
        $contents .= $prate . ",";
        $contents .= $pamt . "\n";
        $ctr++;


        $totqty = $totqty + $pmain[qty];
        $totrate = $totrate + $pmain[rate];
        $totamt = $totamt + $pmain[amt];
    }

    $contents .= ",";
    $contents .= ",";
    $contents .= ",";
    $contents .= ",";
    $contents .= ",";
    $contents .= "Total" . ",";
    $contents .= $totqty . ",";
    $contents .= ",";
    $contents .= $totrate . ",";
    $contents .= $totamt . "\n";



    $contents = strip_tags($contents);


    header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    print $contents;
}
?>