<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include_once("../../../../../lib/phpmailer/Exception.php");
include_once("../../../../../lib/phpmailer/PHPMailer.php");
include_once("../../../../../lib/phpmailer/SMTP.php");
session_start();
include_once("../../../../../lib/konfig.php");
include_once("../../../../../lib/function.php");
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$MySQLi2 = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$MySQLi3 = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);

//if (isset($_POST['submit'])) {
$strIsiTable = "";
$i = 1;
$strBuffer = "";
$query = "SELECT D_LPBJ.ID, 
                   D_LPBJ.iJumlah, 
                   D_LPBJ.iJumlahStok, 
                   D_LPBJ.strKeteranganREQ,
                   D_LPBJ.strKeteranganDIR,
                   D_LPBJ.strKeteranganMGR,
                   D_LPBJ.strKeteranganSPV,
                   D_LPBJ.strKeteranganAccurate,
                   D_LPBJ.dtDibutuhkan, 
                   D_LPBJ.item_strUnit AS strUnit, 
                   ITEM.strKdItem, 
                   ITEM.strDeskripsi, 
                   DEPT.strNmDept, 
                   H_LPBJ.ID AS HEAD_ID, 
                   H_LPBJ.iLevelREQ, 
                   H_LPBJ.employee_Email, 
                   H_LPBJ.strFullNameDIR, 
                   H_LPBJ.strFullNameMGR, 
                   H_LPBJ.strFullNameSPV, 
                   H_LPBJ.strFullNameREQ, 
                   H_LPBJ.strMailDIR, 
                   H_LPBJ.strMailMGR, 
                   H_LPBJ.strMailSPV, 
                   STAFF.strKdLoc 
            FROM tpc_d_lpbj AS D_LPBJ 
            LEFT JOIN tpc_h_lpbj AS H_LPBJ 
                ON D_LPBJ.h_lpbj_ID=H_LPBJ.ID 
            LEFT JOIN item AS ITEM 
                ON D_LPBJ.item_ID = ITEM.ID 
            LEFT JOIN tpc_dept AS DEPT 
                ON DEPT.ID = D_LPBJ.dept_ID 
            LEFT JOIN employee AS STAFF 
                ON H_LPBJ.employee_Email=STAFF.Email 
            WHERE H_LPBJ.strKdLPBJ = '" . $_POST['strKdLPBJ'] . "' AND 
                  H_LPBJ.stsAktif = 1 AND 
                  D_LPBJ.stsAktif = 1 AND 
                  D_LPBJ.stsACCDIR > 0 AND 
                  D_LPBJ.stsACCMGR > 0 AND 
                  D_LPBJ.stsACCSPV > 0 
            ";

$result = $MySQLi->query($query);
while ($row = $result->fetch_array()) {
    $ID = $row['HEAD_ID'];
    $strMailDIR = $row['strMailDIR'];
    $strMailMGR = $row['strMailMGR'];
    $strMailSPV = $row['strMailSPV'];
    $strMailREQ = $row['employee_Email'];
    $strFullNameDIR = $row['strFullNameDIR'];
    $strFullNameMGR = $row['strFullNameMGR'];
    $strFullNameSPV = $row['strFullNameSPV'];
    $strFullNameREQ = $row['strFullNameREQ'];
    $iLevelREQ = $row['iLevelREQ'];
    $strKdLoc = $row['strKdLoc'];
    $stsACC = "";
    $set = "";
    if (isset($_POST['level'])) {
        if ($_POST['level'] == "DIR") {
            if ($_POST['stsACC' . $row['ID']] == 1) {
                $strKeteranganDIR = "DIR : OK. " . $_POST['strKeterangan' . $row['ID']];
                $stsACCFinal = "OK";
            } else {
                $strKeteranganDIR = "DIR : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                $stsACCFinal = "Tolak";
                $stsACC = $stsACC . " stsACC_Accurate = 0, strKeteranganAccurate='ACCURATE : Ditolak sebab sudah di tolak oleh Direktur', ";
            }

            if ($strMailDIR == $strMailMGR) {
                if ($_POST['stsACC' . $row['ID']] == 1)
                    $strKeteranganMGR = "MGR : OK. " . $_POST['strKeterangan' . $row['ID']];
                else $strKeteranganMGR = "MGR : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                $stsACC = "stsACCMGR = '" . $_POST['stsACC' . $row['ID']] . "', 
                            strKeteranganMGR = '" . $strKeteranganMGR . "', 
                            ";
            }

            if ($strMailDIR == $strMailSPV) {
                if ($_POST['stsACC' . $row['ID']] == 1)
                    $strKeteranganSPV = "SPV : OK. " . $_POST['strKeterangan' . $row['ID']];
                else $strKeteranganSPV = "SPV : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                $stsACC = $stsACC . " stsACCSPV = '" . $_POST['stsACC' . $row['ID']] . "', 
                            strKeteranganSPV = '" . $strKeteranganSPV . "', 
                            ";
            }

            $stsACC = $stsACC . " stsACCDIR = '" . $_POST['stsACC' . $row['ID']] . "', 
                        strKeteranganDIR = '" . $strKeteranganDIR . "', 
                        usrUpdate='" . $row['strMailDIR'] . "', 
                        dtUpdate=NOW() 
                        ";
            $strIsiTable = $strIsiTable . "<tr> 
                                <td align='center'>" . $row['strKdItem'] . "</td> 
                                <td align='center'>" . $row['strDeskripsi'] . "</td> 
                                <td align='center'>" . $row['strNmDept'] . "</td> 
                                <td align='center'>" . $row['iJumlah'] . "</td> 
                                <td align='center'>" . $row['strUnit'] . "</td> 
                                <td align='center'>" . $row['iJumlahStok'] . "</td> 
                                <td>" . $row['strKeteranganREQ'] . "<br>" . $row['strKeteranganSPV'] . "<br>" . $row['strKeteranganMGR'] . "<br>" . $strKeteranganDIR . "</td> 
                                <td align='center'>" . date("d/m/Y", strtotime($row['dtDibutuhkan'])) . "</td> 
                                <td align='center'>" . $stsACCFinal . "</td> 
                            </tr>";
            $strEmail = $row['strMailDIR'];
        } elseif ($_POST['level'] == "MGR") {
            if ($_POST['stsACC' . $row['ID']] == 1)
                $strKeteranganMGR = "MGR : OK. " . $_POST['strKeterangan' . $row['ID']];
            else {
                $strKeteranganMGR = "MGR : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                $stsACC = $stsACC . "stsACCDIR = 0, 
                                     strKeteranganDIR='DIR : Ditolak sebab sudah ditolak oleh Manager', 
                                     stsACC_Accurate = 0, 
                                     strKeteranganAccurate='ACCURATE : Ditolak sebab sudah di tolak oleh Direktur', 
                                     ";
            }

            if ($strMailMGR == $strMailSPV) {
                if ($_POST['stsACC' . $row['ID']] == 1)
                    $strKeteranganSPV = "SPV : OK. " . $_POST['strKeterangan' . $row['ID']];
                else {
                    $strKeteranganSPV = "SPV : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                }
                $stsACC = $stsACC . "stsACCSPV = '" . $_POST['stsACC' . $row['ID']] . "', 
                            strKeteranganSPV = '" . $strKeteranganSPV . "', 
                            ";
            }

            $stsACC = $stsACC . " stsACCMGR = '" . $_POST['stsACC' . $row['ID']] . "', 
                        strKeteranganMGR = '" . $strKeteranganMGR . "', 
                        usrUpdate='" . $row['strMailMGR'] . "', 
                        dtUpdate=NOW() 
                        ";
            $strEmail = $row['strMailMGR'];
        } elseif ($_POST['level'] == "SPV") {
            if ($_POST['stsACC' . $row['ID']] == 1)
                $strKeteranganSPV = "SPV : OK. " . $_POST['strKeterangan' . $row['ID']];
            else {
                $strKeteranganSPV = "SPV : Tolak. " . $_POST['strKeterangan' . $row['ID']];
                $stsACC = $stsACC . "stsACCMGR = 0, strKeteranganMGR='MGR : Ditolak sebab sudah ditolak oleh SPV', stsACCDIR = 0, strKeteranganDIR='DIR : Ditolak sebab sudah ditolak oleh Manager', stsACC_Accurate = 0, strKeteranganAccurate='ACCURATE : Ditolak sebab sudah di tolak oleh Direktur', ";
            }

            $stsACC = "stsACCSPV = '" . $_POST['stsACC' . $row['ID']] . "', 
                        strKeteranganSPV='" . $strKeteranganSPV . "', 
                        usrUpdate='" . $row['strMailSPV'] . "', 
                        dtUpdate=NOW() 
                        ";
            $strEmail = $row['strMailSPV'];
        }
    } else {
        $strEmail = $_SESSION['strEmail'];
        if (isset($_POST['stsACC'][$row['ID']])) {
            $strKeteranganAccurate = "ACCURATE : REJECT. " . $_POST['strKeterangan' . $row['ID']];
            $stsACC = "stsACC_Accurate=0, 
                        strKeteranganAccurate='" . $strKeteranganAccurate . "', 
                        usrUpdate='" . $_SESSION['strEmail'] . "', 
                        dtUpdate=NOW()
                        ";
        } else {
            $strKeteranganAccurate = "ACCURATE : OK. " . $_POST['strKeterangan' . $row['ID']];
            $stsACC = "stsACC_Accurate=1, 
                        strKeteranganAccurate='" . $strKeteranganAccurate . "', 
                        usrUpdate='" . $_SESSION['strEmail'] . "', 
                        dtUpdate=NOW() 
                        ";
        }
    }
    $query = "UPDATE tpc_d_lpbj 
                SET " . $stsACC . " 
                WHERE ID='" . $row['ID'] . "' 
                ";
    //die($query);
    $MySQLi2->query($query);
    $strBuffer  = str_replace('"', '`', str_replace("'", "`", $query)) . "<br>" . $strBuffer;
}

$query = "INSERT INTO tpc_log 
            SET strPCHostname='" . gethostbyaddr(getIPv4()) . "', 
                strIPv4='" . getIPv4() . "', 
                strPort='" . $_SERVER['REMOTE_PORT'] . "', 
                strTable='tpc_d_lpbj', 
                strQuery='" . $strBuffer . "', 
                strKeterangan='" . $_SESSION['strEmail'] . " melakukan persetujuan untuk LPBJ No : " . $_POST['strKdLPBJ'] . "', 
                dtCreate=NOW(), 
                usrCreate='" . $_SESSION['strEmail'] . "' 
            ";
$MySQLi->query($query);

if ($MySQLi->affected_rows == 1) {
    $strHyperlink = "http://" . $strNmDomain . "/home/index2.php?menu=persetujuan-lpbj&lpbj=" . $_POST['strKdLPBJ'];
    $strTable_v1 = "  <table cellspacing='0' cellpadding='0' border='0'>
                        <tr>
                            <td>No LPBJ</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $_POST['strKdLPBJ'] . "</td>
                        </tr>
                        <tr>
                            <td>Tanggal</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $_POST['dtCreate'] . "</td>
                        </tr>
                        <tr>
                            <td>Bagian/Divisi</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $_POST['Divisi'] . "</td>
                        </tr>
                        <tr>
                            <td>Peminta</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $strFullNameREQ . "</td>
                        </tr>
                        <tr>
                            <td>Supervisor</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $strFullNameSPV . "</td>
                        </tr>
                        <tr>
                            <td>Manager</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $strFullNameMGR . "</td>
                        </tr>
                        <tr>
                            <td>Direksi</td>
                            <td>&nbsp;:&nbsp;</td>
                            <td>" . $strFullNameDIR . "</td>
                        </tr>
                    </table><br>
                ";
    if (isset($_POST['level'])) {
        switch ($_POST['level']) {
            case "SPV":
                $where = $strMailMGR;
                $query = "SELECT Level
                                    FROM employee 
                                    WHERE Email='" . $where . "'
                                    ";
                $result3 = $MySQLi3->query($query);
                $row3 = $result3->fetch_array();
                switch ($row3['Level']) {
                    case 1:
                        $stsLevel = "DIR";
                        break;
                    case 2:
                        $stsLevel = "MGR";
                        break;
                    case 3:
                        $stsLevel = "SPV";
                        break;
                }

                $query = "UPDATE tpc_h_lpbj 
                                    SET dtSubmitSPV=NOW(), 
                                        dtUpdate=NOW(), 
                                        usrUpdate='" . $strEmail . "' 
                                    WHERE strKdLPBJ='" . $_POST['strKdLPBJ'] . "' 
                                    ";
                $MySQLi->query($query);
                $strHyperlink = $strHyperlink . "&session_data=" . $strMailMGR . "&level=" . $stsLevel;
                $query = "SELECT SUM(DET.stsACCSPV) AS stsACC 
                                    FROM tpc_d_lpbj AS DET 
                                    LEFT JOIN tpc_h_lpbj AS HEAD 
                                        ON HEAD.ID=DET.h_lpbj_ID 
                                    WHERE HEAD.strKdLPBJ='" . $_POST['strKdLPBJ'] . "' AND 
                                        HEAD.stsAktif=1 AND 
                                        DET.stsAktif=1 
                                    ";
                $result = $MySQLi->query($query);
                $row = $result->fetch_array();
                if ($row['stsACC'] > 0) {
                    $strBody = "Dear " . $strFullNameMGR . ", <br><br>

                                I already accepted the item that " . $strFullNameREQ . " want to purchase, please review the item imediately with click the link below :<br> 
                                <a href='" . $strHyperlink . "'>click me!</a><br><br>
                                " . $strTable_v1 . "
                                thats all i have to say, thank you for your attention.<br>
                                Best Regards,<br>
                                " . $strFullNameREQ . "
                                ";
                    $strFulNameMGR = "";
                    $strMailTo = [$strMailMGR . "#" . $strFulNameMGR];
                    $strMailCC = [];
                    $strMailBCC = [];
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailMGR, FILTER_VALIDATE_EMAIL))
                        alertpopup("Email Succesfully send, please follow up " . $strFullNameMGR . " to review the LPBJ", "", "../../../index2.php");
                } else {
                    $strBody = "Your Request has been rejected<br>
                                        Best Regards,<br>
                                        " . $strFullNameSPV;
                    $mail->setFrom($strEmailUsername, $strFullNameFrom);
                    $strMailTo = [$strMailREQ . "#" . $strFullNameREQ];
                    $strMailCC = [];
                    $strMailBCC = [];
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailREQ, FILTER_VALIDATE_EMAIL))
                        alertpopup("LPBJ Rejected and Email Succesfully send to requestor", "", "../../../index2.php");
                }
                break;
            case "MGR":
                if ($strMailMGR == $strMailSPV)
                    $set = "dtSubmitSPV=NOW(), ";

                $query = "UPDATE tpc_h_lpbj 
                                    SET " . $set . " dtSubmitMGR=NOW(), 
                                        dtUpdate=NOW(), 
                                        usrUpdate='" . $strEmail . "' 
                                    WHERE strKdLPBJ='" . $_POST['strKdLPBJ'] . "' 
                                    ";
                $MySQLi->query($query);
                $strHyperlink = $strHyperlink . "&session_data=" . $strMailDIR . "&level=DIR";
                $query = "SELECT SUM(DET.stsACCMGR) AS stsACC 
                                    FROM tpc_d_lpbj AS DET 
                                    LEFT JOIN tpc_h_lpbj AS HEAD 
                                        ON HEAD.ID=DET.h_lpbj_ID 
                                    WHERE HEAD.strKdLPBJ='" . $_POST['strKdLPBJ'] . "' AND 
                                        HEAD.stsAktif=1 AND 
                                        DET.stsAktif=1 
                                    ";
                $result = $MySQLi->query($query);
                $row = $result->fetch_array();
                if ($row['stsACC'] > 0) {
                    $strBody = "Dear " . $strFullNameDIR . ", <br><br>
                    
                                I already accepted the item that " . $strFullNameREQ . " want to purchase, please review the item imediately with click the link below :<br> 
                                <a href='" . $strHyperlink . "'>click me!</a><br><br>
                                " . $strTable_v1 . "
                                thats all i have to say, thank you for your attention.<br>
                                Best Regards,<br>
                                " . $strFullNameREQ . "
                                ";
                    $strMailTo = [$strMailDIR . "#" . $strFullNameDIR];
                    $strMailCC = [];
                    $strMailBCC = [];
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailDIR, FILTER_VALIDATE_EMAIL))
                        alertpopup("Email Succesfully send, please follow up " . $strFullNameDIR . " to review the LPBJ", "", "../../../index2.php");
                } else {
                    $strBody = "Your Request has been rejected<br>
                                        Best Regards,<br>
                                        " . $strFullNameMGR;
                    $strMailTo = [$strMailREQ . "#" . $strFullNameREQ];
                    $strMailCC = [];
                    $strMailBCC = [];
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailREQ, FILTER_VALIDATE_EMAIL))
                        alertpopup("LPBJ Rejected and Email Succesfully send to requestor", "", "../../../index2.php");
                }
                break;
            case "DIR":
                if ($strMailDIR == $strMailMGR)
                    $set = "dtSubmitMGR=NOW(), ";
                if ($strMailDIR == $strMailSPV)
                    $set = $set . " dtSubmitSPV=NOW(), ";

                $query = "UPDATE tpc_h_lpbj 
                                    SET " . $set . " dtSubmitDIR=NOW(), 
                                        dtUpdate=NOW(), 
                                        usrUpdate='" . $strEmail . "' 
                                    WHERE strKdLPBJ='" . $_POST['strKdLPBJ'] . "' 
                                    ";
                $MySQLi->query($query);
                $query = "SELECT SUM(DET.stsACCDIR) AS stsACC 
                                    FROM tpc_d_lpbj AS DET 
                                    LEFT JOIN tpc_h_lpbj AS HEAD 
                                        ON HEAD.ID=DET.h_lpbj_ID 
                                    WHERE HEAD.strKdLPBJ='" . $_POST['strKdLPBJ'] . "' AND 
                                        HEAD.stsAktif=1 AND 
                                        DET.stsAktif=1 
                                    ";
                $result = $MySQLi->query($query);
                $row = $result->fetch_array();
                if ($row['stsACC'] > 0) {
                    $strTable = "<table border='1' cellspacing='0' cellpadding='0'>
                                    <tr>
                                        <td>No</td>
                                        <td>Deskripsi</td>
                                        <td>Departement</td>
                                        <td>Jumlah</td>
                                        <td>Satuan</td>
                                        <td>Jumlah Stok</td>
                                        <td>Keterangan</td>
                                        <td>Prioritas</td>
                                        <td>Status</td>
                                    </tr>
                                    " . $strIsiTable . "
                                </table>
                                ";

                    list($strMailUserName, $strMailDomain) = explode("@", $strMailREQ);
                    $strMailPurchasing = trim("purchasing@" . $strMailDomain);
                    $strMailTo = [$strMailPurchasing . "#Purchasing"];
                    $strMailCC = [$strMailDIR . "#" . $strFullNameDIR];
                    $strMailBCC = [];
                    $query = "SELECT 	FullName, 
                                                Email 
                                        FROM employee 
                                        WHERE strKdLoc='" . $strKdLoc . "' AND 
                                            strJobPosition like '%Accurate%' 
                                        ";
                    $result = $MySQLi->query($query);
                    while ($row = $result->fetch_array()) {
                        $strMailTo[] = $row['Email'] . "#" . $row['FullName'];
                        $strFullNameAccurate = $row['FullName'];
                    }
                    $strBody = "Dear PURCHASING dan Accurate,<br><br>
                            
                                LPBJ ini telah disetujui, Berikut adalah barang yang ingin dibeli oleh :" . $strFullNameREQ . ", untuk segera dilakukan proses pembelian<br>
                                " . $strTable_v1 . "
                                " . $strTable . "<br><br>
                                Demikian yang dapat saya sampaikan, terima kasih.<br><br>
                                Best Regards,<br>
                                " . $strFullNameREQ;

                    switch ($iLevelREQ) {
                        case 3:
                            $strMailCC[] = $strMailMGR . "#" . $strFullNameMGR;
                            break;
                        case 4:
                            $strMailCC[] = $strMailMGR . "#" . $strFullNameMGR;
                            $strMailCC[] = $strMailSPV . "#" . $strFullNameSPV;
                            break;
                    }
                    $strMailCC[] = $strMailREQ . "#" . $strFullNameREQ;
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    // die($strHyperlink); 
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailREQ, FILTER_VALIDATE_EMAIL)) {
                        $query = "UPDATE tpc_h_lpbj 
                                            SET stsDone = 0 
                                            WHERE ID='" . $ID . "' 
                                            ";
                        $MySQLi->query($query);
                    }
                    alertpopup("Email berhasil terkirim, silahkan cek email anda beberapa menit kedepan", "", "../../../index2.php");
                } else {
                    $strBody = "Your Request has been rejected<br>
                                        Best Regards,<br>
                                        " . $strFullNameDIR;
                    $strMailTo = [$strMailREQ . "#" . $strFullNameREQ];
                    $strMailCC = [];
                    $strMailBCC = [];
                    $strSubject = "[LPBJ] Approval Request for LPBJ No :" . $_POST['strKdLPBJ'];
                    if (kirim_email($strMailTo, $strMailCC, $strMailBCC, $strSubject, $strBody) && filter_var($strMailREQ, FILTER_VALIDATE_EMAIL))
                        alertpopup("LPBJ Rejected and Email Succesfully send to requestor", "", "../../../index2.php");
                }
                break;
        }
    } else {
        if (isset($_POST['stsDone'][0])) {
            $query = "UPDATE tpc_h_lpbj 
                        SET dtSubmitAccurate=NOW(), 
                            strFullNameAccurate='" . str_replace("'", "`", str_replace('"', '`', $_SESSION['strNama'])) . "', 
                            strMailAccurate='" . $_SESSION['strEmail'] . "', 
                            usrUpdate='" . $_SESSION['strEmail'] . "', 
                            dtUpdate=NOW() 
                        WHERE strKdLPBJ='" . $_POST['strKdLPBJ'] . "' AND 
                              stsAktif=1
                        ";
            //die($query);
            $MySQLi->query($query);
        }
        alertpopup("LPBJ Berhasil di update, ", "", "../../../index2.php?menu=laporan-lpbj");
    }
}
//}
