<?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'];
$cabang =  $_GET['cabang'];

$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;
}

$cbg = "";
if (!$cabang == 0) {
    $cbg = " and left(HEAD.strKdLPBJ,2) = '" . $cabang . "' ";
}

?>

<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,
                            HEAD.stsDoneByAccurate,
                            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 
                    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 . $cbg . " 
                    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 . "
                    ";
        } 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.stsDoneByAccurate,
                            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 . $cbg . " 
                    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>";
                        $tombolEdit = "<a class='btn btn-outline-primary btn-sm' href='index2.php?menu=edit-lpbj&lpbj=" . $row['strKdLPBJ'] . "'>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>";
                        $tombolEdit = "<button type='button' class='btn btn-outline-primary btn-sm' onclick='selesai1(" . $row['ID'] . ")'>Done</button>";
                    }
                }

                //echo $row['sumACCDIR']; 
                //if ($row['dtSubmitAccurate'] == "1000-01-01" && $row['dtSubmitDIR'] <> "1000-01-01" && strtoupper($_SESSION['strJobPosition']) == "Accurate" && $row['sumACCDIR'] > 0)
                if ($row['dtSubmitAccurate'] == "" && $row['dtSubmitDIR'] <> "1000-01-01" && strtoupper($_SESSION['strJobPosition']) == "Accurate" && $row['sumACCDIR'] > 0)
                    //if ($row['dtSubmitAccurate'] == "" && $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>";
                    $tombolEdit = "<button type='button' class='btn btn-outline-primary btn-sm'>Edit 2</button>";
                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>";
                        $tombolEdit = "
                        <button type='button' class='btn btn-outline-primary btn-sm'>Edit 3</button>
                        <button type='button' class='btn btn-outline-primary btn-sm' onclick='selesai2(" . $row['ID'] . ")'>Done</button>";
                }
            }
                */
            $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='index2.php?menu=edit-lpbj&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-outline-primary btn-sm'>Edit</a>";
                    else
                        //$tombolEdit = "<a href='transaksi/persediaan/pemesanan/simpan.edit.pesan.barang.php?opt=done&id=" . $row['ID'] . "' class='btn btn-outline-primary btn-sm' target='_blank'>Done 1</a>";
                        $tombolEdit = "<button type='button' class='btn btn-outline-primary btn-sm' onclick='selesai1(" . $row['ID'] . ")'>Done</a>";
                }
                //echo $row['sumACCDIR']; 
                //if ($row['dtSubmitAccurate'] == "1000-01-01" && $row['dtSubmitDIR'] <> "1000-01-01" && strtoupper($_SESSION['strJobPosition']) == "ACCURATE" && $row['sumACCDIR'] > 0)
                if ($row['dtSubmitAccurate'] == "" && $row['dtSubmitDIR'] <> "1000-01-01" && $_SESSION['strJobPosition'] == "Accurate" && $row['sumACCDIR'] > 0)
                    $tombolEdit = "<a href='index2.php?menu=persetujuan-lpbj&edit=1&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-outline-primary btn-sm'>Edit</a>";
                else {
                    if (strtoupper($_SESSION['strJobPosition']) == "PURCHASING" && $row['stsDone'] <> 1 && $row['dtSubmitAccurate'] <> "1000-01-01" && $row['sumACC_Accurate'] > 0)
                        $tombolEdit = "<a href='index2.php?menu=edit-lpbj&lpbj=" . $row['strKdLPBJ'] . "' class='btn btn-outline-primary btn-sm'>Edit</a>
                                       <a href='transaksi/persediaan/pemesanan/simpan.edit.pesan.barang.php?opt=done&id=" . $row['ID'] . "' class='btn btn-outline-primary btn-sm'>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 style='white-space:nowrap'>" . $row['strFullNameDIR'] . " / " . $dtSubmitDIR . "</td>                                        
                    <td><div class='btn-group' role='group' aria-label='Basic outlined example'>
                        <button type='button' class='btn btn-outline-primary btn-sm' onclick='cetak(" . $row['ID'] . ")'>Preview</button>
                        " . $tombolEdit . "
                    </div>
                    </td>

                </tr>";
            $i++;
        }
        ?>
    </tbody>
</table>

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