<?php
session_start();
include_once("../../../../../lib/konfig.php");
include_once("../../../../../lib/function.php");

$mulai = $_GET['mulai'];
$hingga = $_GET['hingga'];
$sts =  $_GET['sts'];
$item =  $_GET['item_id'];

$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);

$having = "";
$akses = "";

if ($_SESSION['strDivision'] == "HQ")
    $akses = "";
elseif ($_SESSION['strJobPosition'] == "Purchasing" || $_SESSION['strJobPosition'] == "Accurate") {
    // $where=$where." AND strKdLPBJ like '".$_SESSION['strKdLokasi']."%' ";
    $akses = $akses . " ";
    $having = $having . "HAVING countDetails >= 1 AND
                        NOT (sumACCSPV = 0) AND
                        NOT (sumACCMGR = 0) AND
                        NOT (sumACCDIR = 0)";
} else {
    switch ($_SESSION['strJenisAkses']) {
        case "DIR":
            $akses = "";
            break;
        case "MGR":
            $akses = $akses . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "' OR strMailMGR='" . $_SESSION['strEmail'] . "')";
            break;
        case "SPV":
            $akses = $akses . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "')";
            break;
        case "STAFF":
            $akses = $akses . " AND employee_Email='" . $_SESSION['strEmail'] . "'";
            break;
    }
}

switch ($sts) {
    case "done":
        $where = " AND dtSubmitSPV > '1000-01-01' AND dtSubmitMGR > '1000-01-01' AND dtSubmitDIR > '1000-01-01' ";
        break;
    case "pending":
        $where = " AND (dtSubmitSPV = '1000-01-01' OR dtSubmitMGR = '1000-01-01' OR dtSubmitDIR = '1000-01-01')";
        break;
    case "all":
        $where = "";
        break;
}

switch ($_SESSION['strJenisAkses']) {
    case "DIR":
        $filter = "";
        break;
    case "MGR":
        $filter = $where . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "' OR strMailMGR='" . $_SESSION['strEmail'] . "')";
        break;
    case "SPV":
        $filter = $where . " AND (employee_Email='" . $_SESSION['strEmail'] . "' OR strMailSPV='" . $_SESSION['strEmail'] . "')";
        break;
    case "STAFF":
        $filter = $where . " AND employee_Email='" . $_SESSION['strEmail'] . "'";
        break;
}



?>

<table id="tbllpbj1" class="display" style="width:100%">
    <thead>
        <tr>
            <th>No</th>
            <th>Kode LPBJ</th>
            <th>Requestor</th>
            <th>SPV</th>
            <th>MGR</th>
            <th>DIR</th>
            <th>Option</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
        switch ($sts) {
            case "done":
                $where = $where . " AND dtSubmitSPV > '1000-01-01' AND dtSubmitMGR > '1000-01-01' AND dtSubmitDIR > '1000-01-01' ";
                break;
            case "pending":
                $where = $where . " AND (dtSubmitSPV = '1000-01-01' OR dtSubmitMGR = '1000-01-01' OR dtSubmitDIR = '1000-01-01')";
                break;
            case "all":
                $where = $where . "";
                break;
        }

        if ($item <> "") {
            $query = "SELECT  HEAD.ID, 
                            HEAD.strKdLPBJ, 
                            HEAD.strFullNameREQ, 
                            HEAD.strFullNameSPV, 
                            HEAD.strFullNameMGR, 
                            HEAD.strFullNameDIR, 
                            HEAD.dtCreate, 
                            HEAD.dtSubmitSPV, 
                            HEAD.dtSubmitMGR, 
                            HEAD.dtSubmitDIR, 
                            HEAD.dtSubmitAccurate, 
                            HEAD.stsDone 
                    FROM tpc_h_lpbj AS HEAD 
                    LEFT JOIN tpc_d_lpbj AS DET 
                        ON HEAD.ID=DET.h_lpbj_ID 
                    LEFT JOIN item AS list_item 
                        ON DET.item_ID=list_item.ID 
                    WHERE DET.item_ID='" . $item . "' AND 
                        DET.stsAktif=1 AND
                        HEAD.stsAktif=1 AND 
                        list_item.stsAktif=1 AND 
                        (DATE_FORMAT(HEAD.dtCreate, '%Y-%m-%d') BETWEEN '$mulai' AND '$hingga') " . $where . $akses;
        } else {
            $query = "SELECT  HEAD.ID, 
                            HEAD.strKdLPBJ, 
                            HEAD.strFullNameREQ, 
                            HEAD.strFullNameSPV, 
                            HEAD.strFullNameMGR, 
                            HEAD.strFullNameDIR, 
                            HEAD.dtCreate, 
                            HEAD.dtSubmitSPV, 
                            HEAD.dtSubmitMGR, 
                            HEAD.dtSubmitDIR, 
                            HEAD.dtSubmitAccurate, 
                            HEAD.stsDone,
                            SUM(DET.stsACC_Accurate) AS sumACC_Accurate,
                            SUM(DET.stsACCDIR) AS sumACCDIR,
                            SUM(DET.stsACCMGR) AS sumACCMGR,
                            SUM(DET.stsACCSPV) AS sumACCSPV,
                            COUNT(DET.h_lpbj_ID) AS countDetails
                    FROM tpc_h_lpbj AS HEAD 
                    LEFT JOIN tpc_d_lpbj AS DET 
                        ON HEAD.ID=DET.h_lpbj_ID 
                    WHERE HEAD.stsAktif=1 AND 
                            DET.stsAktif=1 AND 
                        (DATE_FORMAT(HEAD.dtCreate, '%Y-%m-%d') BETWEEN '$mulai' AND '$hingga')    
                    " . $where . $akses . " 
                    GROUP BY HEAD.ID,
                            HEAD.strKdLPBJ,
                            HEAD.strFullNameREQ,
                            HEAD.strFullNameSPV,
                            HEAD.strFullNameMGR,
                            HEAD.strFullNameDIR,
                            HEAD.dtCreate,
                            HEAD.dtSubmitSPV,
                            HEAD.dtSubmitMGR,
                            HEAD.dtSubmitDIR,
                            HEAD.dtSubmitAccurate,
                            HEAD.stsDone 
                    " . $having . "
                    ";
        }




        $i = 1;
        //echo $query;
        //die($query);
        $result = $MySQLi->query($query);
        while ($row = $result->fetch_array()) {
            $tombolEdit = "";
            if ($row['dtSubmitSPV'] == "1000-01-01")
                $dtSubmitSPV = "Pending";
            else
                $dtSubmitSPV = $row['dtSubmitSPV'];

            if ($row['dtSubmitMGR'] == "1000-01-01")
                $dtSubmitMGR = "Pending";
            else
                $dtSubmitMGR = $row['dtSubmitMGR'];

            if ($row['dtSubmitDIR'] == "1000-01-01")
                $dtSubmitDIR = "Pending";
            else {
                $dtSubmitDIR = $row['dtSubmitDIR'];
                /*
                                                                if($_SESSION['usrLevel']>1 && $row['stsDoneByAccurate'] <> 1)
                                                                { 
                                                                    if($_SESSION['strJobPosition'] <> "Accurate")
                                                                        $tombolEdit="<a href='index.php?menu=edit-lpbj&lpbj=".$row['strKdLPBJ']."' class='btn btn-space btn-primary'>Edit</a>"; 
                                                                    else $tombolEdit="<a href='transaksi/persediaan/pemesanan/simpan.edit.pesan.barang.php?opt=done&id=".$row['ID']."' class='btn btn-space btn-primary' target='_blank'>Done</a>";
                                                                }
                                                                */ //echo $row['sumACCDIR']; 
                if ($row['dtSubmitAccurate'] == "1000-01-01" && $row['dtSubmitDIR'] <> "1000-01-01" && strtoupper($_SESSION['strJobPosition']) == "ACCURATE" && $row['sumACCDIR'] > 0)
                    $tombolEdit = "<a href='index.php?menu=persetujuan-lpbj&edit=1&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-space btn-primary'>Edit</a>";
                else {
                    if (strtoupper($_SESSION['strJobPosition']) == "PURCHASING" && $row['stsDone'] <> 1 && $row['dtSubmitAccurate'] <> "1000-01-01" && $row['sumACC_Accurate'] > 0)
                        $tombolEdit = "<a href='index.php?menu=edit-lpbj&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-space btn-primary'>Edit</a><a href='transaksi/persediaan/pemesanan/simpan.edit.pesan.barang.php?opt=done&id=" . $row['ID'] . "' class='btn btn-space btn-primary'>Done</a>";
                }
            }

            echo "<tr>
                    <td>" . $i . "</td>
                    <td>" . $row['strKdLPBJ'] . "</td>
                    <td>" . $row['strFullNameREQ'] . " / " . $row['dtCreate'] . "</td>
                    <td>" . $row['strFullNameSPV'] . " / " . $dtSubmitSPV . "</td>
                    <td>" . $row['strFullNameMGR'] . " / " . $dtSubmitMGR . "</td>
                    <td>" . $row['strFullNameDIR'] . " / " . $dtSubmitDIR . "</td>
                    <td>" . $tombolEdit . "<a href='transaksi/persediaan/pemesanan/print.pesan.barang.php?ID=" . $row['ID'] . "' class='btn btn-space btn-primary' target='_blank'>Print Preview</a></td>
                </tr>";
            $i++;
        }
        ?>
    </tbody>
</table>

<script>
    $(document).ready(function() {
        $('#tbllpbj1').DataTable({
            scrollX: true,
        });
    });
</script>