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

$maindb = 'salemain';
$trandb = 'saletran';
$formname = "Pending Purchase";
$file = "pendpurchase_fr.php";

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

$myyear = $_SESSION[$myyear];

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

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

$salesman = $_POST['salesman'];
$supplier = $_POST['supplier'];

$mfrmdate = $_POST['frmdt'];
$mtodate = $_POST['todt'];

$salesmancond = "";
$suppliercond = "";

if (!empty($supplier)) {
    $suppliercond = "AND b.supplier='$supplier'";
}

if (!empty($salesman)) {
    $salesmancond = "AND b.salesman='$salesman'";
}


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

mysqli_query($conn, "drop temporary table if exists penddata") or die(mysqli_error($conn));
//mysqli_query($conn, "create temporary table penddata 
//                            SELECT a.*, b.supplier FROM saletran a, salepurch b WHERE  a.vouchno = b.salevouch and 
//                                       a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate'  and b.supplier = ''
//                                       order by a.vouchdt, a.vouchno, a.itemname, a.size ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table penddata 
                            SELECT * FROM saletran WHERE `vouchdt`>= '$mfrmdate'  AND  `vouchdt` <='$mtodate' 
                                and concat(vouchno,itemname,size) not in (Select concat(salevouch,itemname,size) from salepurch )
                                       order by vouchdt, vouchno, itemname, size ") or die(mysqli_error($conn));

//$getsupp = mysqli_query($conn, "SELECT * FROM penddata group by mansupp") or die(mysqli_error($conn));

$getsupp = mysqli_query($conn, "SELECT a.vouchno,a.mansupp, b.name FROM penddata a left join  accmast b ON  a.mansupp = b.accode group by a.mansupp order by b.name ") or die(mysqli_error($conn));
//print table($getsupp);
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <table class="table table-bordered">
                <thead class="mySearchTr">
                    <tr style=" background-color: #8f4131;">
                        <th style="width:3%;">Sr. No.</th>
                        <th style="width:8%;">Date</th>
                        <th style="width:6%;">Sale</th>
                        <th style="width:10%;">Sales Executive</th>
                        <th style="width:17%;">Customer</th>
                        <th style="width:12%;">Description</th>
                        <th style="width:10%;">Code</th>
                        <th style="width:5%;">Size</th>
                        <th style="width:6%;" class='text-right'>Rate</th>
                        <th style="width:7%;" class='text-right'>Sale Amount</th>
                        <th style="width:5%;" class='text-right'>Qty.</th>
                        <th style="width:5%;" class='text-right'>Sq.Ft.</th>
                    </tr>
                </thead>

                <?php
                $ctr = 0;
                $gqty = 0;
                $gsqft = 0;
                $gpl = 0;


                while ($supp = mysqli_fetch_array($getsupp)) {

                    $vouchno = $supp['vouchno'];
                    $suppcode = $supp['mansupp'];
                    $suppname = $supp['name'];

                    echo "<tr style=' background-color: #8f4131;color:white;'>";
                    echo "<td colspan='12'>$suppname</td>";
                    echo "</tr>";
                    // browse($vouchno);

                    $ctr = 0;
                    $tpl = 0;
                    $tqty = 0;
                    $tsqft = 0;
                    $purvouch = 0;
                    $purparty = "";

                    $getvouch = mysqli_query($conn, "SELECT *, date_format(`vouchdt`, '%d-%m-%Y') as vouchdt FROM penddata WHERE `mansupp`= '$suppcode' ") or die(mysqli_error($conn));
                    while ($rowm = mysqli_fetch_array($getvouch)) {

                        $qty = $rowm['qty'];
                        $design = $rowm['sldesign'];
                        $sqft = $rowm['sqft'];
                        $rate = $rowm['rate'];
                        $cost = $rowm['cost'];
                        $saleamt = $rowm['amount'] - $rowm['itemdisc'] + $rowm['itemgst'];
                        $itemname = $rowm['itemname'];
                        $size = $rowm['size'];
                        $calby = $rowm['calby'];

                        $getdetail = mysqli_query($conn, "SELECT * FROM `salemain` WHERE  `vouchno`='$rowm[vouchno]' ") or die(mysqli_error($conn));
                        $fetdt = mysqli_fetch_array($getdetail);
                        $party = $fetdt['party'];
                        $staff = $fetdt['salesman'];

                        $partyname = '';
                        $getparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE  `accode`='$party' ") or die(mysqli_error($conn));
                        $fetparty = mysqli_fetch_array($getparty);
                        $partyname = $fetparty['name'];

                        $staffname = '';
                        $getstaff = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE  `accode`='$staff' ") or die(mysqli_error($conn));
                        $fetstaff = mysqli_fetch_array($getstaff);
                        $staffname = $fetstaff['name'];


                        $ctr++;
                        echo "<tr>";
                        echo "<td>$ctr</td>";
                        echo "<td>$rowm[vouchdt]</td>";
                        echo "<td>$rowm[vouchno]</td>";
                        echo "<td>$staffname</td>";
                        echo "<td>$partyname</td>";
                        echo "<td>$itemname</td>";
                        echo "<td>$design</td>";
                        echo "<td>$size</td>";
                        echo "<td class='text-right'>" . number_format($rate, 2) . "</td>";
                        echo "<td class='text-right'>" . number_format($saleamt, 2) . "</td>";
                        echo "<td class='text-right'>" . number_format($qty, 2) . "</td>";
                        echo "<td class='text-right'>" . number_format($sqft, 2) . "</td>";
                        echo "</tr>";


                        $tqty = $tqty + $qty;
                        $tsqft = $tsqft + $sqft;

                        $gqty = $gqty + $qty;
                        $gsqft = $gsqft + $sqft;
                    }

                    echo "<tr> ";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td  ></td>";
                    echo "<td class='text-right' style=' color: #006300;'><b>Total</b></td>";
                    echo "<td class='text-right' style=' color: #006300;'><b>" . number_format($tqty, 2) . "</b></td>";
                    echo "<td class='text-right' style=' color: #006300;'><b>" . number_format($tsqft, 2) . "</b></td>";
                    echo "</tr>";
                }
                echo "<tr> ";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td  ></td>";
                echo "<td class='text-right' style=' color: blue;'><b>Total</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>" . number_format($gqty, 2) . "</b></td>";
                echo "<td class='text-right' style=' color: blue;'><b>" . number_format($gsqft, 2) . "</b></td>";
                echo "</tr>";
                ?>
            </table>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}


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

    $contents .= "Sr. No., Date, Sale, Sales Executive, Customer, Item, Size, Rate, Sale Amount, Qty., Sq. Ft.\n";

    $ctr = 0;
    $gqty = 0;
    $gsqft = 0;
    $gpl = 0;


    while ($supp = mysqli_fetch_array($getsupp)) {

        $vouchno = $supp['vouchno'];
        $suppcode = $supp['mansupp'];
        $suppname = $supp['name'];

        $contents .= "$suppname" . "\n";

        $ctr = 0;
        $tpl = 0;
        $tqty = 0;
        $tsqft = 0;
        $purvouch = 0;
        $purparty = "";

        $getvouch = mysqli_query($conn, "SELECT * FROM penddata WHERE `mansupp`= '$suppcode' ") or die(mysqli_error($conn));
        while ($rowm = mysqli_fetch_array($getvouch)) {

            $qty = $rowm['qty'];
            $sqft = $rowm['sqft'];
            $rate = $rowm['rate'];
            $cost = $rowm['cost'];
            $saleamt = $rowm['amount'] - $rowm['itemdisc'] + $rowm['itemgst'];
            $itemname = $rowm['itemname'];
            $size = $rowm['size'];
            $calby = $rowm['calby'];

            $getdetail = mysqli_query($conn, "SELECT * FROM `salemain` WHERE  `vouchno`='$rowm[vouchno]' ") or die(mysqli_error($conn));
            $fetdt = mysqli_fetch_array($getdetail);
            $party = $fetdt['party'];
            $staff = $fetdt['salesman'];

            $partyname = '';
            $getparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE  `accode`='$party' ") or die(mysqli_error($conn));
            $fetparty = mysqli_fetch_array($getparty);
            $partyname = $fetparty['name'];

            $staffname = '';
            $getstaff = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE  `accode`='$staff' ") or die(mysqli_error($conn));
            $fetstaff = mysqli_fetch_array($getstaff);
            $staffname = $fetstaff['name'];


            $ctr++;
            $contents .= "$ctr" . ",";
            $contents .= "$rowm[vouchdt]" . ",";
            $contents .= "$rowm[vouchno]" . ",";
            $contents .= "$staffname" . ",";
            $contents .= "$partyname" . ",";
            $contents .= "$itemname" . ",";
            $contents .= "$size" . ",";
            $contents .= "$rate" . ",";
            $contents .= "$saleamt" . ",";
            $contents .= "$qty" . ",";
            $contents .= "$sqft" . "\n";

            $tqty = $tqty + $qty;
            $tsqft = $tsqft + $sqft;

            $gqty = $gqty + $qty;
            $gsqft = $gsqft + $sqft;
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$tqty" . ",";
        $contents .= "$tsqft" . "\n";
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$gqty" . ",";
    $contents .= "$gsqft" . "\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;
    }
}
?>