<?php
class Natsubgroup extends MY_Controller
{
    var $db_onedev;
    public function index()
    {
        echo "AUTH API";
    }

    public function __construct()
    {
        parent::__construct();
        $this->db_onedev = $this->load->database("onedev", true);
    }

    // lookup nat_group
    function getNatGroup()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $sql = "SELECT Nat_GroupID,
                    Nat_GroupCode,
                    Nat_GroupName
                    FROM nat_group
                    WHERE Nat_GroupIsActive ='Y'";
            $qry = $this->db_onedev->query($sql, array());

        if (!$qry) {
            // $this->db->trans_rollback();
            $this->sys_error_db("Error get nat_group");
            exit;
        }
            $result = $qry->result_array();
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // lookup nat_sub_group
    function getNatSubGroup()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $sql = "SELECT 
                p.Nat_GroupName,
                c.Nat_SubGroupID,
                c.Nat_SubGroupNat_GroupID,
                c.Nat_SubGroupCode,
                c.Nat_SubGroupName,
                c.Nat_SubGroupLangName,
                c.Nat_SubGroupIsResult,
                c.Nat_SubGroupReportTitle,
                c.Nat_SubGroupCreated,
                c.Nat_SubGroupLastUpdated,
                c.Nat_SubGroupIsActive,
                CONCAT(p.Nat_GroupName,' - ',c.Nat_SubGroupName) as display
            FROM nat_subgroup AS c
            JOIN nat_group AS p
                ON c.Nat_SubGroupNat_GroupID = p.Nat_GroupID
                AND c.Nat_SubGroupIsActive = 'Y'
                AND p.Nat_GroupIsActive = 'Y'";
            $qry = $this->db_onedev->query($sql, array());

        if (!$qry) {
            // $this->db->trans_rollback();
            $this->sys_error_db("Error get nat_subgroup");
            exit;
        }
            $result = $qry->result_array();
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // ac pendapatan
    function getAcPendapatan()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $search = $prm['search'];
            // $where_search = "%%";
            $where_search_desc = '%'.$search.'%';
            $where_search_acc = "$search%";

            $sql = "SELECT 
                coaID,
                coaAccountNo,
                coaDescription,
                coaSubDescription,
                coaAccountType,
                coaSpecialAccountType,
                coaIsInput,
                coaReportSchedule,
                coaCurrencyCode,
                coaCashFlowCategory,
                coaCreated,
                coaLastUpdated,
                coaIsActive,
                coaLevel
            FROM coa
            WHERE coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            AND 
            (
                coaDescription LIKE '$where_search_desc'
                OR coaAccountNo LIKE '$where_search_acc'
            )";

            // echo $sql;
            // die();
            $qry = $this->db_onedev->query($sql, array());

        if (!$qry) {
            // $this->db->trans_rollback();
            $this->sys_error_db("Error get coa pendapatan");
            exit;
        }
            $result = $qry->result_array();
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // ac biaya
    function getAcBiaya()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $search = $prm['search'];
            // $where_search = "%%";
            $where_search_desc = '%'.$search.'%';
            $where_search_acc = "$search%";

            $sql = "SELECT 
                coaID,
                coaAccountNo,
                coaDescription,
                coaSubDescription,
                coaAccountType,
                coaSpecialAccountType,
                coaIsInput,
                coaReportSchedule,
                coaCurrencyCode,
                coaCashFlowCategory,
                coaCreated,
                coaLastUpdated,
                coaIsActive,
                coaLevel
            FROM coa
            WHERE coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            AND 
            (
                coaDescription LIKE '$where_search_desc'
                OR coaAccountNo LIKE '$where_search_acc'
            )";

            // echo $sql;
            // die();
            $qry = $this->db_onedev->query($sql, array());

        if (!$qry) {
            // $this->db->trans_rollback();
            $this->sys_error_db("Error get coa pendapatan");
            exit;
        }
            $result = $qry->result_array();
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // ac persediaan
    function getAcPersediaan()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $search = $prm['search'];
            // $where_search = "%%";
            $where_search_desc = '%'.$search.'%';
            $where_search_acc = "$search%";

            $sql = "SELECT 
                coaID,
                coaAccountNo,
                coaDescription,
                coaSubDescription,
                coaAccountType,
                coaSpecialAccountType,
                coaIsInput,
                coaReportSchedule,
                coaCurrencyCode,
                coaCashFlowCategory,
                coaCreated,
                coaLastUpdated,
                coaIsActive,
                coaLevel
            FROM coa
            WHERE coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            AND 
            (
                coaDescription LIKE '$where_search_desc'
                OR coaAccountNo LIKE '$where_search_acc'
            )";

            // echo $sql;
            // die();
            $qry = $this->db_onedev->query($sql, array());

        if (!$qry) {
            // $this->db->trans_rollback();
            $this->sys_error_db("Error get coa pendapatan");
            exit;
        }
            $result = $qry->result_array();
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // addData
    function addData()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $this->db_onedev->trans_begin();

            $a_nat_group = $prm['a_nat_group'];
            $a_nat_subgroup = $prm['a_nat_subgroup'];
            $a_coa_pendapatan = $prm['a_coa_pendapatan'];
            $a_coa_biaya = $prm['a_coa_biaya'];
            $a_coa_persediaan = $prm['a_coa_persediaan'];

            $MapNatSub_IsActive = 'Y'; 
            $MapNatSub_CreatedAt = date('Y-m-d H:i:s'); 
            $MapNatSub_LastUpdatedAt = date('Y-m-d H:i:s');

            // nat_group
            $MapNatSub_NatGroupID = isset($a_nat_group['Nat_GroupID']) ? $a_nat_group['Nat_GroupID'] : 0;

            // nat_subgroup
            $MapNatSub_NatSubGroupID = isset($a_nat_subgroup['Nat_SubGroupID']) ? $a_nat_subgroup['Nat_SubGroupID'] : 0;
            $MapNatSub_NatSubGroupCode = isset($a_nat_subgroup['Nat_SubGroupCode']) ? $a_nat_subgroup['Nat_SubGroupCode'] : '';

            // pendapatan
            $MapNatSub_PendapatanCoaID = isset($a_coa_pendapatan['coaID']) ? $a_coa_pendapatan['coaID'] : 0;
            $MapNatSub_PendapatanCoaAccountNo = isset($a_coa_pendapatan['coaAccountNo']) ? $a_coa_pendapatan['coaAccountNo'] : '';
            $MapNatSub_PendapatanCoaDescription = isset($a_coa_pendapatan['coaDescription']) ? $a_coa_pendapatan['coaDescription'] : '';

            // biaya
            $MapNatSub_BiayaCoaID = isset($a_coa_biaya['coaID']) ? $a_coa_biaya['coaID'] : 0;
            $MapNatSub_BiayaCoaAccountNo = isset($a_coa_biaya['coaAccountNo']) ? $a_coa_biaya['coaAccountNo'] : '';
            $MapNatSub_BiayaCoaDescription = isset($a_coa_biaya['coaDescription']) ? $a_coa_biaya['coaDescription'] : '';

            // persediaan
            $MapNatSub_PersediaanCoaID = isset($a_coa_persediaan['coaID']) ? $a_coa_persediaan['coaID'] : 0;
            $MapNatSub_PersediaanCoaAccountNo = isset($a_coa_persediaan['coaAccountNo']) ? $a_coa_persediaan['coaAccountNo'] : '';
            $MapNatSub_PersediaanCoaDescription = isset($a_coa_persediaan['coaDescription']) ? $a_coa_persediaan['coaDescription'] : '';

            // check jika sudah ada MapNatSub_NatGroupID AND MapNatSub_NatSubGroupID maka tidak boleh insert
            $sql_gn = "SELECT * 
            FROM nat_group 
            WHERE Nat_GroupID = '$MapNatSub_NatGroupID'
            AND Nat_GroupIsActive = 'Y'";
            
            $qry_gn = $this->db_onedev->query($sql_gn);
            if (!$qry_gn) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    // "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }
            $rows_gn = $qry_gn->result();
            $grup_name = "";
            $grup_name .= $rows_gn[0]->Nat_GroupName;

            // sub group
            
            $sql_sub = "SELECT * 
            FROM nat_subgroup 
            WHERE Nat_SubGroupID = '$MapNatSub_NatSubGroupID'
            AND Nat_SubGroupIsActive = 'Y'";
            
            $qry_sub = $this->db_onedev->query($sql_sub);
            if (!$qry_sub) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    // "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }
            $rows_sub = $qry_sub->result();
            $sub_grup_name = "";
            $sub_grup_name .= $rows_sub[0]->Nat_SubGroupName;

            $sql_c = "SELECT * from map_nat_subgroup
            where MapNatSub_IsActive = 'Y'
            AND MapNatSub_NatGroupID = '$MapNatSub_NatGroupID'
            AND MapNatSub_NatSubGroupID ='$MapNatSub_NatSubGroupID'";

            $qry_c = $this->db_onedev->query($sql_c);
            if (!$qry_c) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    // "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_c = $qry_c->result_array();
            if(count($rows_c) > 0) {
                $this->db_onedev->trans_rollback();
                $this->sys_error("Group Name dengan nama $grup_name, Sub Group dengan nama $sub_grup_name, sudah ada");
                exit;
            }

            // mulai insert
            $sql = "INSERT INTO map_nat_subgroup 
            (
                MapNatSub_NatGroupID,
                MapNatSub_NatSubGroupID,
                MapNatSub_NatSubGroupCode,
                MapNatSub_IsActive,
                MapNatSub_CreatedAt,
                MapNatSub_LastUpdatedAt,
                MapNatSub_PendapatanCoaID,
                MapNatSub_PendapatanCoaAccountNo,
                MapNatSub_PendapatanCoaDescription,
                MapNatSub_BiayaCoaID,
                MapNatSub_BiayaCoaAccountNo,
                MapNatSub_BiayaCoaDescription,
                MapNatSub_PersediaanCoaID,
                MapNatSub_PersediaanCoaAccountNo,
                MapNatSub_PersediaanCoaDescription
            ) 
            VALUES (
                '$MapNatSub_NatGroupID',
                '$MapNatSub_NatSubGroupID',
                '$MapNatSub_NatSubGroupCode',
                '$MapNatSub_IsActive',
                '$MapNatSub_CreatedAt',
                '$MapNatSub_LastUpdatedAt',
                '$MapNatSub_PendapatanCoaID',
                '$MapNatSub_PendapatanCoaAccountNo',
                '$MapNatSub_PendapatanCoaDescription',
                '$MapNatSub_BiayaCoaID',
                '$MapNatSub_BiayaCoaAccountNo',
                '$MapNatSub_BiayaCoaDescription',
                '$MapNatSub_PersediaanCoaID',
                '$MapNatSub_PersediaanCoaAccountNo',
                '$MapNatSub_PersediaanCoaDescription'
            )";

            // echo $sql;

            $qry = $this->db_onedev->query($sql);
            if (!$qry) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    // "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $this->db_onedev->trans_commit();
            $result = array("total" => 1);
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // search
    function search()
    {
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }
    
            $prm = $this->sys_input;
            $search = isset($prm["search"]) ? trim($prm["search"]) : '';
            $search = ($search !== '') ? "%$search%" : "%%";
    
            // $number_offset = ($prm["current_page"] > 0) ? ($prm["current_page"] - 1) * 20 : 0;
            // $number_limit = 2;
            $number_limit = 10;
            $number_offset = ($prm["current_page"] > 0) ? ($prm["current_page"] - 1) * $number_limit : 0;
    
            $sql = "SELECT 
                        IFNULL(m.MapNatSub_ID, 0) AS MapNatSub_ID,
                        IFNULL(m.MapNatSub_NatGroupID, 0) AS MapNatSub_NatGroupID,
                        IFNULL(m.MapNatSub_NatSubGroupID, 0) AS MapNatSub_NatSubGroupID,
                        IFNULL(m.MapNatSub_NatSubGroupCode, '') AS MapNatSub_NatSubGroupCode,
                        IFNULL(m.MapNatSub_IsActive, '') AS MapNatSub_IsActive,
                        IFNULL(m.MapNatSub_CreatedAt, '') AS MapNatSub_CreatedAt,
                        IFNULL(m.MapNatSub_LastUpdatedAt, '') AS MapNatSub_LastUpdatedAt,
                        IFNULL(m.MapNatSub_PendapatanCoaID, 0) AS MapNatSub_PendapatanCoaID,
                        IFNULL(m.MapNatSub_PendapatanCoaAccountNo, '') AS MapNatSub_PendapatanCoaAccountNo,
                        IFNULL(m.MapNatSub_PendapatanCoaDescription, '') AS MapNatSub_PendapatanCoaDescription,
                        IFNULL(m.MapNatSub_BiayaCoaID, 0) AS MapNatSub_BiayaCoaID,
                        IFNULL(m.MapNatSub_BiayaCoaAccountNo, '') AS MapNatSub_BiayaCoaAccountNo,
                        IFNULL(m.MapNatSub_BiayaCoaDescription, '') AS MapNatSub_BiayaCoaDescription,
                        IFNULL(m.MapNatSub_PersediaanCoaID, 0) AS MapNatSub_PersediaanCoaID,
                        IFNULL(m.MapNatSub_PersediaanCoaAccountNo, '') AS MapNatSub_PersediaanCoaAccountNo,
                        IFNULL(m.MapNatSub_PersediaanCoaDescription, '') AS MapNatSub_PersediaanCoaDescription,
                        IFNULL(g.Nat_GroupID, 0) AS Nat_GroupID,
                        IFNULL(g.Nat_GroupName, '') AS Nat_GroupName,
                        IFNULL(g.Nat_GroupIsActive, '') AS Nat_GroupIsActive,
                        IFNULL(s.Nat_SubGroupID, 0) AS Nat_SubGroupID,
                        IFNULL(s.Nat_SubGroupCode, '') AS Nat_SubGroupCode,
                        IFNULL(s.Nat_SubGroupName, '') AS Nat_SubGroupName,
                        IFNULL(s.Nat_SubGroupLangName, '') AS Nat_SubGroupLangName,
                        IFNULL(s.Nat_SubGroupIsResult, '') AS Nat_SubGroupIsResult,
                        IFNULL(s.Nat_SubGroupReportTitle, '') AS Nat_SubGroupReportTitle,
                        IFNULL(s.Nat_SubGroupCreated, '') AS Nat_SubGroupCreated,
                        IFNULL(s.Nat_SubGroupLastUpdated, '') AS Nat_SubGroupLastUpdated,
                        IFNULL(s.Nat_SubGroupIsActive, '') AS Nat_SubGroupIsActive,
                        IFNULL(c_pendapatan.coaID, 0) AS PendapatanCoaID,
                        IFNULL(c_pendapatan.coaAccountNo, '') AS PendapatanCoaAccountNo,
                        IFNULL(c_pendapatan.coaDescription, '') AS PendapatanCoaDescription,
                        IFNULL(c_biaya.coaID, 0) AS BiayaCoaID,
                        IFNULL(c_biaya.coaAccountNo, '') AS BiayaCoaAccountNo,
                        IFNULL(c_biaya.coaDescription, '') AS BiayaCoaDescription,
                        IFNULL(c_persediaan.coaID, 0) AS PersediaanCoaID,
                        IFNULL(c_persediaan.coaAccountNo, '') AS PersediaanCoaAccountNo,
                        IFNULL(c_persediaan.coaDescription, '') AS PersediaanCoaDescription
                    FROM map_nat_subgroup m
                    LEFT JOIN nat_group g 
                        ON m.MapNatSub_NatGroupID = g.Nat_GroupID
                        AND g.Nat_GroupIsActive = 'Y'
                    LEFT JOIN nat_subgroup s
                        ON m.MapNatSub_NatSubGroupID = s.Nat_SubGroupID
                        AND s.Nat_SubGroupIsActive = 'Y'
                    LEFT JOIN coa c_pendapatan
                        ON m.MapNatSub_PendapatanCoaID = c_pendapatan.coaID
                        AND c_pendapatan.coaIsInput = 'Y'
                        AND c_pendapatan.coaIsActive = 'Y'
                    LEFT JOIN coa c_biaya
                        ON m.MapNatSub_BiayaCoaID = c_biaya.coaID
                        AND c_biaya.coaIsInput = 'Y'
                        AND c_biaya.coaIsActive = 'Y'
                    LEFT JOIN coa c_persediaan
                        ON m.MapNatSub_PersediaanCoaID = c_persediaan.coaID
                        AND c_persediaan.coaIsInput = 'Y'
                        AND c_persediaan.coaIsActive = 'Y'
                    WHERE m.MapNatSub_IsActive = 'Y'
                    AND (
                        g.Nat_GroupName LIKE '$search' OR
                        s.Nat_SubGroupName LIKE '$search' OR
                        m.MapNatSub_NatSubGroupCode LIKE '$search' OR
                        m.MapNatSub_PendapatanCoaAccountNo LIKE '$search' OR
                        m.MapNatSub_PendapatanCoaDescription LIKE '$search' OR
                        m.MapNatSub_BiayaCoaAccountNo LIKE '$search' OR
                        m.MapNatSub_BiayaCoaDescription LIKE '$search' OR
                        m.MapNatSub_PersediaanCoaAccountNo LIKE '$search' OR
                        m.MapNatSub_PersediaanCoaDescription LIKE '$search'
                    )
                    ORDER BY m.MapNatSub_ID ASC";

            $sql_total = "SELECT count(*) as total FROM ($sql) as x";
            $qry_total = $this->db_onedev->query($sql_total);

            $totalCount = 0;
            $totalPage = 0;
            if ($qry_total) {
                $totalCount = $qry_total->result_array()[0]["total"];
                $totalPage = ceil($totalCount / $number_limit);
            } else {
                $this->sys_error_db("select map_nat_subgroup count error", $this->db_onedev);;
                exit;
            }

            $sql_select = $sql . " LIMIT $number_limit OFFSET $number_offset";
            
            $qry = $this->db_onedev->query($sql_select);
            if ($qry) {
                $rows = $qry->result_array();
            } else {
                $this->db_onedev->trans_rollback();
                $this->sys_error_db("select map_nat_subgroup error", $this->db_onedev);
                exit;
            }
    
            $response = [
                "total" => $totalPage,
                "totalFilter" => $totalCount,
                "records" => $rows,
                "sql" => $this->db_onedev->last_query()
            ];
            
            $this->sys_ok($response);
        } catch (Exception $exc) {
            $this->sys_error($exc->getMessage());
        }
    }
    
    // get_by_id
    function get_by_id()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            // map_nat_subgroup
            $MapNatSub_ID = isset($prm['MapNatSub_ID']) ? $prm['MapNatSub_ID'] : 0;

            $sql_m = "SELECT * FROM map_nat_subgroup where MapNatSub_ID = '$MapNatSub_ID'";

            $qry_m = $this->db_onedev->query($sql_m);
            if (!$qry_m) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_m = $qry_m->result_array();

            // empty rows_m
            if (empty($rows_m)) {
                $rows_m = [[
                    "MapNatSub_ID"                   => 0,
                    "MapNatSub_NatGroupID"           => 0,
                    "MapNatSub_NatSubGroupID"        => 0,
                    "MapNatSub_NatSubGroupCode"      => '',
                    "MapNatSub_IsActive"             => 'Y',
                    "MapNatSub_CreatedAt"            => '',
                    "MapNatSub_LastUpdatedAt"        => '',
                    "MapNatSub_PendapatanCoaID"      => 0,
                    "MapNatSub_PendapatanCoaAccountNo" => '',
                    "MapNatSub_PendapatanCoaDescription" => '',
                    "MapNatSub_BiayaCoaID"           => 0,
                    "MapNatSub_BiayaCoaAccountNo"    => '',
                    "MapNatSub_BiayaCoaDescription"  => '',
                    "MapNatSub_PersediaanCoaID"      => 0,
                    "MapNatSub_PersediaanCoaAccountNo" => '',
                    "MapNatSub_PersediaanCoaDescription" => ''
                ]];
            }

            // nat group
            $MapNatSub_NatGroupID = isset($prm['MapNatSub_NatGroupID']) ? $prm['MapNatSub_NatGroupID'] : 0;
            $sql_g = "SELECT * FROM nat_group where Nat_GroupID = '$MapNatSub_NatGroupID'";

            $qry_g = $this->db_onedev->query($sql_g);
            if (!$qry_g) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_g = $qry_g->result_array();

            if (empty($rows_g)) {
                $rows_g = [[
                    "Nat_GroupID"        => 0,
                    "Nat_GroupCode"      => '',
                    "Nat_GroupName"      => '',
                    "Nat_GroupCreated"   => '',
                    "Nat_GroupLastUpdated"=> '',
                    "Nat_GroupIsActive"  => 'Y'
                ]];
            }

            $sql_gx = "SELECT * FROM nat_group where Nat_GroupIsActive = 'Y'";

            $qry_gx = $this->db_onedev->query($sql_gx);
            if (!$qry_gx) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_gx = $qry_gx->result_array();

            // nat sub group
            $MapNatSub_NatSubGroupID = isset($prm['MapNatSub_NatSubGroupID']) ? $prm['MapNatSub_NatSubGroupID'] : 0;
            $sql_sg = "SELECT c.*,
            CONCAT(p.Nat_GroupName,' - ',c.Nat_SubGroupName) as display
            FROM nat_subgroup c 
            JOIN nat_group AS p
                ON c.Nat_SubGroupNat_GroupID = p.Nat_GroupID
                AND c.Nat_SubGroupIsActive = 'Y'
                AND p.Nat_GroupIsActive = 'Y'
            where Nat_SubGroupID = '$MapNatSub_NatSubGroupID'";

            $qry_sg = $this->db_onedev->query($sql_sg);
            if (!$qry_sg) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_sg = $qry_sg->result_array();

            if (empty($rows_sg)) {
                $rows_sg = [[
                    "Nat_SubGroupID"          => 0,
                    "Nat_SubGroupNat_GroupID" => 0,
                    "Nat_SubGroupCode"        => '',
                    "Nat_SubGroupName"        => '',
                    "Nat_SubGroupLangName"    => '',
                    "Nat_SubGroupIsResult"    => 'N',
                    "Nat_SubGroupReportTitle" => '',
                    "Nat_SubGroupCreated"     => '',
                    "Nat_SubGroupLastUpdated" => '',
                    "Nat_SubGroupIsActive"    => 'Y',
                    "display" => ''
                ]];
            }

            $sql_sgx = "SELECT c.*,
            CONCAT(p.Nat_GroupName,' - ',c.Nat_SubGroupName) as display
            FROM nat_subgroup c 
            JOIN nat_group AS p
                ON c.Nat_SubGroupNat_GroupID = p.Nat_GroupID
                AND c.Nat_SubGroupIsActive = 'Y'
                AND p.Nat_GroupIsActive = 'Y'";

            $qry_sgx = $this->db_onedev->query($sql_sgx);
            if (!$qry_sgx) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_sgx = $qry_sgx->result_array();

            // pendapatan
            $MapNatSub_PendapatanCoaID = isset($prm['MapNatSub_PendapatanCoaID']) ? $prm['MapNatSub_PendapatanCoaID'] : 0;
            $sql_p = "SELECT * FROM coa where coaID = '$MapNatSub_PendapatanCoaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            ";

            $qry_p = $this->db_onedev->query($sql_p);
            if (!$qry_p) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_p = $qry_p->result_array();

            if (empty($rows_p)) {
                $rows_p = [[
                    "coaID"                  => 0,
                    "coaAccountNo"           => '',
                    "coaDescription"         => '',
                    "coaSubDescription"      => '',
                    "coaAccountType"         => '',
                    "coaSpecialAccountType"  => '',
                    "coaIsInput"             => 'N',
                    "coaReportSchedule"      => '',
                    "coaCurrencyCode"        => '',
                    "coaCashFlowCategory"    => '',
                    "coaCreated"             => '',
                    "coaLastUpdated"         => '',
                    "coaIsActive"            => 'Y',
                    "coaLevel"               => 1
                ]];
            }

            // biaya
            $MapNatSub_BiayaCoaID = isset($prm['MapNatSub_BiayaCoaID']) ? $prm['MapNatSub_BiayaCoaID'] : 0;
            $sql_b = "SELECT * FROM coa where coaID = '$MapNatSub_BiayaCoaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            ";

            $qry_b = $this->db_onedev->query($sql_b);
            if (!$qry_b) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_b = $qry_b->result_array();

            if (empty($rows_b)) {
                $rows_b = [[
                    "coaID"                  => 0,
                    "coaAccountNo"           => '',
                    "coaDescription"         => '',
                    "coaSubDescription"      => '',
                    "coaAccountType"         => '',
                    "coaSpecialAccountType"  => '',
                    "coaIsInput"             => 'N',
                    "coaReportSchedule"      => '',
                    "coaCurrencyCode"        => '',
                    "coaCashFlowCategory"    => '',
                    "coaCreated"             => '',
                    "coaLastUpdated"         => '',
                    "coaIsActive"            => 'Y',
                    "coaLevel"               => 1
                ]];
            }

            // psd
            $MapNatSub_PersediaanCoaID = isset($prm['MapNatSub_PersediaanCoaID']) ? $prm['MapNatSub_PersediaanCoaID'] : 0;
            $sql_psd = "SELECT * FROM coa where coaID = '$MapNatSub_PersediaanCoaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            ";

            $qry_psd = $this->db_onedev->query($sql_psd);
            if (!$qry_psd) {
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $this->db_onedev->last_query()
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $rows_psd = $qry_psd->result_array();

            if (empty($rows_psd)) {
                $rows_psd = [[
                    "coaID"                  => 0,
                    "coaAccountNo"           => '',
                    "coaDescription"         => '',
                    "coaSubDescription"      => '',
                    "coaAccountType"         => '',
                    "coaSpecialAccountType"  => '',
                    "coaIsInput"             => 'N',
                    "coaReportSchedule"      => '',
                    "coaCurrencyCode"        => '',
                    "coaCashFlowCategory"    => '',
                    "coaCreated"             => '',
                    "coaLastUpdated"         => '',
                    "coaIsActive"            => 'Y',
                    "coaLevel"               => 1
                ]];
            }

            // response
            $response = [
                "record_map_nat_subgroup" => $rows_m,
                "records_group" => $rows_g,
                "records_group_all" => $rows_gx,
                "records_sub_group" => $rows_sg,
                "records_sub_group_all" => $rows_sgx,
                "records_pendapatan" => $rows_p,
                "records_biaya" => $rows_b,
                "records_persediaan" => $rows_psd,
                "sql" => $sql_sg
            ];
            
            $this->sys_ok($response);

        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // editData
    function editData()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }
    
            $this->db_onedev->trans_begin();
            $e_nat_group = $prm['e_nat_group'];
            $e_nat_subgroup = $prm['e_nat_subgroup'];
            $e_coa_biaya = $prm['e_coa_biaya'];
            $e_coa_pendapatan = $prm['e_coa_pendapatan'];
            $e_coa_persediaan = $prm['e_coa_persediaan'];
         
            $MapNatSub_ID = $prm['MapNatSub_ID'];
    
            // defined variable
           
            $MapNatSub_LastUpdatedAt = date('Y-m-d H:i:s');

            // nat_group
            $MapNatSub_NatGroupID = isset($e_nat_group['Nat_GroupID']) ? $e_nat_group['Nat_GroupID'] : 0;

            // nat sub grup
            $MapNatSub_NatSubGroupID = isset($e_nat_subgroup['Nat_SubGroupID']) ? $e_nat_subgroup['Nat_SubGroupID'] : 0;
            $MapNatSub_NatSubGroupCode = isset($e_nat_subgroup['Nat_SubGroupCode']) ? $e_nat_subgroup['Nat_SubGroupCode'] : '';

            // pendapatan
            $MapNatSub_PendapatanCoaID = isset($e_coa_pendapatan['coaID']) ? $e_coa_pendapatan['coaID'] : 0;
            $MapNatSub_PendapatanCoaAccountNo = isset($e_coa_pendapatan['coaAccountNo']) ? $e_coa_pendapatan['coaAccountNo'] : '';
            $MapNatSub_PendapatanCoaDescription = isset($e_coa_pendapatan['coaDescription']) ? $e_coa_pendapatan['coaDescription'] : '';

            // biaya
            $MapNatSub_BiayaCoaID = isset($e_coa_biaya['coaID']) ? $e_coa_biaya['coaID'] : 0;
            $MapNatSub_BiayaCoaAccountNo = isset($e_coa_biaya['coaAccountNo']) ? $e_coa_biaya['coaAccountNo'] : '';
            $MapNatSub_BiayaCoaDescription = isset($e_coa_biaya['coaDescription']) ? $e_coa_biaya['coaDescription'] : '';

            // persediaan
            $MapNatSub_PersediaanCoaID = isset($e_coa_persediaan['coaID']) ? $e_coa_persediaan['coaID'] : 0;
            $MapNatSub_PersediaanCoaAccountNo = isset($e_coa_persediaan['coaAccountNo']) ? $e_coa_persediaan['coaAccountNo'] : '';
            $MapNatSub_PersediaanCoaDescription = isset($e_coa_persediaan['coaDescription']) ? $e_coa_persediaan['coaDescription'] : '';
    
            $sql = "UPDATE map_nat_subgroup 
            SET
                MapNatSub_NatGroupID = '$MapNatSub_NatGroupID',
                MapNatSub_NatSubGroupID = '$MapNatSub_NatSubGroupID',
                MapNatSub_NatSubGroupCode = '$MapNatSub_NatSubGroupCode',
                MapNatSub_IsActive = 'Y',
                MapNatSub_LastUpdatedAt = '$MapNatSub_LastUpdatedAt',
                MapNatSub_PendapatanCoaID = '$MapNatSub_PendapatanCoaID',
                MapNatSub_PendapatanCoaAccountNo = '$MapNatSub_PendapatanCoaAccountNo',
                MapNatSub_PendapatanCoaDescription = '$MapNatSub_PendapatanCoaDescription',
                MapNatSub_BiayaCoaID = '$MapNatSub_BiayaCoaID',
                MapNatSub_BiayaCoaAccountNo = '$MapNatSub_BiayaCoaAccountNo',
                MapNatSub_BiayaCoaDescription = '$MapNatSub_BiayaCoaDescription',
                MapNatSub_PersediaanCoaID = '$MapNatSub_PersediaanCoaID',
                MapNatSub_PersediaanCoaAccountNo = '$MapNatSub_PersediaanCoaAccountNo',
                MapNatSub_PersediaanCoaDescription = '$MapNatSub_PersediaanCoaDescription'
            WHERE MapNatSub_ID = '$MapNatSub_ID'";
    
            // echo $sql;
    
            $qry = $this->db_onedev->query($sql);
            if (!$qry) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }
    
            $this->db_onedev->trans_commit();
            $result = array("total" => 1);
            $this->sys_ok($result);
    
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    // deleteData
    function deleteData()
    {
        $prm = $this->sys_input;
        try {
            if (! $this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $this->db_onedev->trans_begin();
            $MapNatSub_ID = $prm['MapNatSub_ID'];
            $MapNatSub_LastUpdatedAt = date('Y-m-d H:i:s');

            $sql = "UPDATE map_nat_subgroup 
            SET 
                MapNatSub_IsActive = 'N',
                MapNatSub_LastUpdatedAt = '$MapNatSub_LastUpdatedAt'
            WHERE MapNatSub_ID = '$MapNatSub_ID'";

            $qry = $this->db_onedev->query($sql);
            if (!$qry) {
                $this->db_onedev->trans_rollback();
                $error = array(
                    "message" => $this->db_onedev->error()["message"],
                    "sql" => $last_qry
                );
                $this->sys_error_db($error, $this->db_onedev);
                exit;
            }

            $this->db_onedev->trans_commit();
            $result = array("total" => 1);
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }
}
?>