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

$maindb = "sordmain";
$trandb = "sordtran";
$stkdb  = "sordsacn";

$formname = "Pending Sale Order Register";
$file = "pendslord_fr.php";

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

$party = $_POST["party"];
$product = $_POST["product"];
$color = $_POST['color'];
$dtype = $_POST['dtype'];    //  1 - summary  - ---2 - detail

$srcond = "";
$branch = trim($_POST["series"]);
$spltype = $_SESSION["spltype_" . $dbname];

if ($spltype != 1) {
    if (!empty($branch)) {
        $srcond = "and `series`='$branch'";
    }
}
$partycond = "";
$prodcond = "";
$colrcond = "";

$mfrmdate = $_POST["fdate"];
$mtodate  = $_POST["tdate"];

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

if (!empty($product)) {
    $prodcond = " AND `barcode`='$product' ";
}

if (!empty($color)) {
    $colrcond = " AND `color`='$color' ";
}

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

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

if ($dtype == 1) {
   $qry = mysqli_query($conn,"SELECT ordno, sum(qty) as sqty  from $stkdb where ordno != ''
                                    and ordno in (select vouchno from sordmain where vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' $partycond )
                                    $prodcond $colrcond group by ordno having sum(qty) > 0 order by ordno ") or die(mysqli_error($conn));
} else {
   $qry = mysqli_query($conn,"SELECT ordno, barcode, itemcode, color, sum(qty) as sqty from $stkdb where ordno != '' 
                                    and ordno in (select vouchno from sordmain where vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' $partycond )
                                    $prodcond $colrcond group by ordno, itemcode, color having sum(qty) > 0 order by ordno ") or die(mysqli_error($conn));
}


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

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .dtype{
            background-color: #C89595;
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    
            <?php if ($dtype == 1) { ?>
                    <table style="background: white;width: 100%" class="table table-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='5%'>Sale Order</th>
                                <th width='12%'>Date</th>
                                <th width='30%'>Party</th>
                                <th class="text-right" width='10%'>Qty</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;

                            while ($pmain = mysqli_fetch_array($qry)) {
                                $getitem2 = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as mydt,party FROM `sordmain` WHERE `vouchno`='$pmain[ordno]'")or die(mysqli_error($conn));
                                $item2 = mysqli_fetch_array($getitem2);
                                        
                                $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$item2[party]'")or die(mysqli_error($conn));
                                $fetp = mysqli_fetch_array($getparty);

                                $suppname = $fetp[name];
                                
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[ordno]</td>";
                                Echo "<td>$item2[mydt]</td>";
                                Echo "<td>$suppname</td>";
                                Echo "<td class='text-right'>$pmain[sqty]</td>";
                                Echo "</tr>";
                                $totamt = $totamt + $pmain[sqty];
                            }      
                                  $colr = "green";
                                echo "<tr> ";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td align='right' style='color:$colr;'><b>Total</b></td>";
                                echo "<td align='right' style='color:$colr;'><b>$totamt</b></td>";
                                echo "</tr>";
                                                           
                            }else{
                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='background: white;width: 100%' class='table table-striped table-condensed table-bordered table-hover'>";
                                    Echo "<thead class='mySearchTr' style='background-color:#C89595'>";
                                    Echo "<th width='5%;'class='dtype'>Sr</th>";
                                    Echo "<th width='4%' class='dtype'>Sale Order</th>";
                                    Echo "<th width='5%' class='dtype'>Date</th>";
                                    Echo "<th width='10%' class='dtype'>Party</th>";
                                    Echo "<th width='6%' class='dtype'>Barcode</th>";
                                    Echo "<th width='6%' class='dtype'>Item</th>";
                                    Echo "<th width='6%' class='dtype'>Color</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Qty</th>";
                                    Echo "</thead>";
                                    Echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;
                                    
                                    $getpmain3 = mysqli_query($conn, "SELECT a.*, b.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `sordmain` a, 
                                                                 `sordsacn` b where a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                                                  and a.vouchno = b.ordno $partycond order by b.ordno limit 0,1")or die(mysqli_error($conn));
                                    $fetp = mysqli_fetch_array($getpmain3);
                                    $tvouchno = $fetp['ordno'];

                                    while ($tran = mysqli_fetch_array($qry)) {
                                        $bvouchno = $tran['ordno'];
                                        
                                        $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);
                                        
                                        $getitem2 = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as mydt,party FROM `sordmain` WHERE `vouchno`='$tran[ordno]'")or die(mysqli_error($conn));
                                        $item2 = mysqli_fetch_array($getitem2);

                                        $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'")or die(mysqli_error($conn));
                                        $color = mysqli_fetch_array($getcolor);
                                        
                                        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$item2[party]'")or die(mysqli_error($conn));
                                        $supp = mysqli_fetch_array($getsupp);
                                        
                                        if ($bvouchno != $tvouchno) {
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1' ></td>";
                                        echo "<td style='color:$colr1'><b>Total</b></td>";
                                        echo "<td style='color:$colr1'class='text-right'><b>$totqty</b></td>";
                                        echo "</tr>";
                                        $totqty = 0;
                                        $tvouchno = $bvouchno;
                                    }
                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class=''>$tran[ordno]</td>";
                                        Echo "<td class=''>$item2[mydt]</td>";
                                        Echo "<td class=''>$supp[name]</td>";
                                        Echo "<td class=''>$tran[barcode]</td>";
                                        Echo "<td class=''>$item[name]</td>";
                                        Echo "<td class=''>$color[name]</td>";
                                        Echo "<td class='numeric'>$tran[sqty]</td>";
                                        Echo "</tr>";
                                        $totqty = $totqty + $tran[sqty];
                                        $ttotqty = $ttotqty + $totqty;
                                        echo "<tr> ";
                                    }
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1' ></td>";
                                        echo "<td style='color:$colr1'><b>Total</b></td>";
                                        echo "<td style='color:$colr1'class='text-right'><b>$totqty</b></td>";
                                        echo "</tr>";
                                        $totqty = 0;
                                        $tvouchno = $bvouchno;
                                        
                                        
                                        
                                        $colr1 = "blue";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1'</td>";
                                        echo "<td style='color:$colr1' ></td>";
                                        echo "<td style='color:$colr1'><b>Grand Total</b></td>";
                                        echo "<td style='color:$colr1'class='text-right'><b>$ttotqty</b></td>";
                                        echo "</tr>";
                                        Echo "</table>";
                                        Echo "</td>";
                                    }                            
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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


if (isset($_POST["excel"])) {
                    
            if ($dtype == 1) { 
                  
                $contents .= "Sr,Sale Order,Date,Party,Qty\n";
                              
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                      
                            while ($pmain = mysqli_fetch_array($qry)) {
                                $getitem2 = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as mydt,party FROM `sordmain` WHERE `vouchno`='$pmain[ordno]'")or die(mysqli_error($conn));
                                $item2 = mysqli_fetch_array($getitem2);
                                        
                                $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$item2[party]'")or die(mysqli_error($conn));
                                $fetp = mysqli_fetch_array($getparty);

                                $suppname = $fetp[name];
//                                browse($party[name]);
                                
                                $ctr++;
                                
                                $contents .= "$ctr" . ","; 
                                $contents .= "$pmain[ordno]" . ","; 
                                $contents .= "$item2[mydt]" . ","; 
                                $contents .= "$suppname" . ","; 
                                $contents .= "$pmain[sqty]" . "\n"; 
                               
                                $ttotamt = $ttotamt + $pmain[sqty];
                            }      
                                 
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "Total" . ",";
                                $contents .= "$ttotamt" . "\n";
                          
                                                           
                            }else{
                                    $contents .= "Sr,Sale Order,Date,Party,Barcode,Item,Color,Qty\n";

                                    $ctr2 = 0;
                                    $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;
                                    
                                    $getpmain3 = mysqli_query($conn, "SELECT a.*, b.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `sordmain` a, 
                                                                 `sordsacn` b where a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                                                  and a.vouchno = b.ordno $partycond order by b.ordno limit 0,1")or die(mysqli_error($conn));
                                    $fetp = mysqli_fetch_array($getpmain3);
                                    $tvouchno = $fetp['ordno'];

                                    while ($tran = mysqli_fetch_array($qry)) {
                                        $bvouchno = $tran['ordno'];
                                        
                                        $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);
                                        
                                        $getitem2 = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as mydt,party FROM `sordmain` WHERE `vouchno`='$tran[ordno]'")or die(mysqli_error($conn));
                                        $item2 = mysqli_fetch_array($getitem2);

                                        $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'")or die(mysqli_error($conn));
                                        $color = mysqli_fetch_array($getcolor);
                                        
                                        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$item2[party]'")or die(mysqli_error($conn));
                                        $supp = mysqli_fetch_array($getsupp);
                                        
                                        if ($bvouchno != $tvouchno) {
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "Total" . ",";
                                        $contents .= "$totqty" . "\n";
                                      
                                        $totqty = 0;
                                        $tvouchno = $bvouchno;
                                    }
                                        $ctr2++;
                                        
                                        $contents .= "$ctr2" . ",";
                                        $contents .= "$tran[ordno]" . ",";
                                        $contents .= "$item2[mydt]" . ",";
                                        $contents .= "$supp[name]" . ",";
                                        $contents .= "$tran[barcode]" . ",";
                                        $contents .= "$item[name]" . ",";
                                        $contents .= "$color[name]" . ",";
                                        $contents .= "$tran[sqty]" . "\n";
                                        
                                        $totqty = $totqty + $tran[sqty];
                                        $ttotqty = $ttotqty + $totqty;
                                    }
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "Total" . ",";
                                        $contents .= "$totqty" . "\n";
                                        $totqty = 0;
                                        $tvouchno = $bvouchno;
                                        
                                        $contents .= "" . ","; 
                                        $contents .= "" . ","; 
                                        $contents .= "" . ","; 
                                        $contents .= "" . ","; 
                                        $contents .= "" . ","; 
                                        $contents .= "" . ","; 
                                        $contents .= "Grand Total" . ","; 
                                        $contents .= "$ttotqty" . "\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;
    }
}
?>