<?php
class Natgroup 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);
        }
    }
    
    // ac uang muka
    function getAcUangMuka()
    {
        $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 hutang
    function getAcHutang()
    {
        $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 disc pendapatan
    function getAcDiscPendapatan()
    {
        $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 retur pendapatan
    function getAcReturPendapatan()
    {
        $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);
        }
    }

    // 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.MapNatGroup_ID, 0) AS MapNatGroup_ID,
                        IFNULL(m.MapNatGroup_NatGroupID, 0) AS MapNatGroup_NatGroupID,

                        -- Nat Group
                        IFNULL(g.Nat_GroupCode, '') AS Nat_GroupCode,
                        IFNULL(g.Nat_GroupName, '') AS Nat_GroupName,
    
                        -- Discount
                        IFNULL(m.MapNatGroup_Disc_coaID, 0) AS MapNatGroup_Disc_coaID,
                        IFNULL(m.MapNatGroup_Disc_coaAccNo, '') AS MapNatGroup_Disc_coaAccNo,
                        IFNULL(m.MapNatGroup_Disc_coaDesc, '') AS MapNatGroup_Disc_coaDesc,
                        IFNULL(c_disc.coaAccountNo, '') AS Disc_coaAccountNo,
                        IFNULL(c_disc.coaDescription, '') AS Disc_coaDescription,
    
                        -- Retur
                        IFNULL(m.MapNatGroup_Retur_coaID, 0) AS MapNatGroup_Retur_coaID,
                        IFNULL(m.MapNatGroup_Retur_coaAccNo, '') AS MapNatGroup_Retur_coaAccNo,
                        IFNULL(m.MapNatGroup_Retur_coaDesc, '') AS MapNatGroup_Retur_coaDesc,
                        IFNULL(c_retur.coaAccountNo, '') AS Retur_coaAccountNo,
                        IFNULL(c_retur.coaDescription, '') AS Retur_coaDescription,
    
                        -- DP
                        IFNULL(m.MapNatGroup_Dp_coaID, 0) AS MapNatGroup_Dp_coaID,
                        IFNULL(m.MapNatGroup_Dp_coaAccNo, '') AS MapNatGroup_Dp_coaAccNo,
                        IFNULL(m.MapNatGroup_Dp_coaDesc, '') AS MapNatGroup_Dp_coaDesc,
                        IFNULL(c_dp.coaAccountNo, '') AS Dp_coaAccountNo,
                        IFNULL(c_dp.coaDescription, '') AS Dp_coaDescription,
    
                        -- hutang
                        IFNULL(m.MapNatGroup_Debt_coaID, 0) AS MapNatGroup_Debt_coaID,
                        IFNULL(m.MapNatGroup_Debt_coaAccNo, '') AS MapNatGroup_Debt_coaAccNo,
                        IFNULL(m.MapNatGroup_Debt_coaDesc, '') AS MapNatGroup_Debt_coaDesc,
                        IFNULL(c_debt.coaAccountNo, '') AS Debt_coaAccountNo,
                        IFNULL(c_debt.coaDescription, '') AS Debt_coaDescription
                    FROM map_nat_group m
                    LEFT JOIN nat_group g 
                        ON m.MapNatGroup_NatGroupID = g.Nat_GroupID
                        AND g.Nat_GroupIsActive = 'Y'

                    LEFT JOIN coa c_disc 
                        ON m.MapNatGroup_Disc_coaID = c_disc.coaID
                        AND c_disc.coaIsInput = 'Y' 
                        AND c_disc.coaIsActive = 'Y'

                    LEFT JOIN coa c_retur 
                        ON m.MapNatGroup_Retur_coaID = c_retur.coaID
                        AND c_retur.coaIsInput = 'Y' 
                        AND c_retur.coaIsActive = 'Y'

                    LEFT JOIN coa c_dp 
                        ON m.MapNatGroup_Dp_coaID = c_dp.coaID
                        AND c_dp.coaIsInput = 'Y' 
                        AND c_dp.coaIsActive = 'Y'

                    LEFT JOIN coa c_debt 
                        ON m.MapNatGroup_Debt_coaID = c_debt.coaID
                        AND c_debt.coaIsInput = 'Y' 
                        AND c_debt.coaIsActive = 'Y'
                    WHERE m.MapNatGroup_IsActive = 'Y'
                    AND (
                        g.Nat_GroupName LIKE '$search' OR
                        m.MapNatGroup_Disc_coaAccNo LIKE '$search' OR
                        m.MapNatGroup_Disc_coaDesc LIKE '$search' OR
                        m.MapNatGroup_Retur_coaAccNo LIKE '$search' OR
                        m.MapNatGroup_Retur_coaDesc LIKE '$search' OR
                        m.MapNatGroup_Dp_coaAccNo LIKE '$search' OR
                        m.MapNatGroup_Dp_coaDesc LIKE '$search' OR
                        m.MapNatGroup_Debt_coaAccNo LIKE '$search' OR
                        m.MapNatGroup_Debt_coaDesc LIKE '$search'
                    )
                    ORDER BY m.MapNatGroup_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 coa 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 coa 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());
        }
    }

    // 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_coa_uang_muka = $prm['a_coa_uang_muka'];
            $a_coa_hutang = $prm['a_coa_hutang'];
            $a_coa_disc_pendapatan = $prm['a_coa_disc_pendapatan'];
            $a_coa_retur_pendapatan = $prm['a_coa_retur_pendapatan'];

            // defined variable
            $MapNatGroup_IsActive = "Y";
            $MapNatGroup_CreatedAt = date('Y-m-d H:i:s');
            $MapNatGroup_LastUpdatedAt = date('Y-m-d H:i:s');
            
            // nat_group
            $MapNatGroup_NatGroupID = isset($a_nat_group['Nat_GroupID']) ? $a_nat_group['Nat_GroupID'] : 0;
            
            // disc pendaptan
            $MapNatGroup_Disc_coaID = isset($a_coa_disc_pendapatan['coaID']) ? $a_coa_disc_pendapatan['coaID'] : 0;
            $MapNatGroup_Disc_coaAccNo = isset($a_coa_disc_pendapatan['coaAccountNo']) ? $a_coa_disc_pendapatan['coaAccountNo'] : '';
            $MapNatGroup_Disc_coaDesc = isset($a_coa_disc_pendapatan['coaDescription']) ? $a_coa_disc_pendapatan['coaDescription'] : '';

            // retur pendapatan
            $MapNatGroup_Retur_coaID = isset($a_coa_retur_pendapatan['coaID']) ? $a_coa_retur_pendapatan['coaID'] : 0;
            $MapNatGroup_Retur_coaAccNo = isset($a_coa_retur_pendapatan['coaAccountNo']) ? $a_coa_retur_pendapatan['coaAccountNo'] : '';
            $MapNatGroup_Retur_coaDesc = isset($a_coa_retur_pendapatan['coaDescription']) ? $a_coa_retur_pendapatan['coaDescription'] : '';

            // uang muka
            $MapNatGroup_Dp_coaID = isset($a_coa_uang_muka['coaID']) ? $a_coa_uang_muka['coaID'] : 0;
            $MapNatGroup_Dp_coaAccNo = isset($a_coa_uang_muka['coaAccountNo']) ? $a_coa_uang_muka['coaAccountNo'] : '';
            $MapNatGroup_Dp_coaDesc = isset($a_coa_uang_muka['coaDescription']) ? $a_coa_uang_muka['coaDescription'] : '';

            // hutang
            $MapNatGroup_Debt_coaID = isset($a_coa_hutang['coaID']) ? $a_coa_hutang['coaID'] : 0;
            $MapNatGroup_Debt_coaAccNo = isset($a_coa_hutang['coaAccountNo']) ? $a_coa_hutang['coaAccountNo'] : '';
            $MapNatGroup_Debt_coaDesc = isset($a_coa_hutang['coaDescription']) ? $a_coa_hutang['coaDescription'] : '';

            // check jika sudah ada MapNatGroup_NatGroupID maka tidak boleh insert
            $sql_gn = "SELECT * 
            FROM nat_group 
            WHERE Nat_GroupID = '$MapNatGroup_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;

            $sql_c = "SELECT * from map_nat_group WHERE MapNatGroup_NatGroupID = '$MapNatGroup_NatGroupID'
            AND MapNatGroup_IsActive = 'Y'";
            $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();
            if(count($rows_c) > 0){
                $this->db_onedev->trans_rollback();
                $this->sys_error("Group Name dengan nama $grup_name, sudah ada");
                exit;
            }

            // mulai insert
            $sql = "INSERT INTO map_nat_group (
                MapNatGroup_NatGroupID,
                MapNatGroup_Disc_coaID,
                MapNatGroup_Disc_coaAccNo,
                MapNatGroup_Disc_coaDesc,
                MapNatGroup_Retur_coaID,
                MapNatGroup_Retur_coaAccNo,
                MapNatGroup_Retur_coaDesc,
                MapNatGroup_Dp_coaID,
                MapNatGroup_Dp_coaAccNo,
                MapNatGroup_Dp_coaDesc,
                MapNatGroup_Debt_coaID,
                MapNatGroup_Debt_coaAccNo,
                MapNatGroup_Debt_coaDesc,
                MapNatGroup_IsActive,
                MapNatGroup_CreatedAt,
                MapNatGroup_LastUpdatedAt
            ) 
            VALUES (
                '$MapNatGroup_NatGroupID',
                '$MapNatGroup_Disc_coaID',
                '$MapNatGroup_Disc_coaAccNo',
                '$MapNatGroup_Disc_coaDesc',
                '$MapNatGroup_Retur_coaID',
                '$MapNatGroup_Retur_coaAccNo',
                '$MapNatGroup_Retur_coaDesc',
                '$MapNatGroup_Dp_coaID',
                '$MapNatGroup_Dp_coaAccNo',
                '$MapNatGroup_Dp_coaDesc',
                '$MapNatGroup_Debt_coaID',
                '$MapNatGroup_Debt_coaAccNo',
                '$MapNatGroup_Debt_coaDesc',
                '$MapNatGroup_IsActive',
                '$MapNatGroup_CreatedAt',
                '$MapNatGroup_LastUpdatedAt'
            )";

            // 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();
            $MapNatGroup_ID = $prm['MapNatGroup_ID'];
            $MapNatGroup_LastUpdatedAt = date('Y-m-d H:i:s');

            $sql = "UPDATE map_nat_group 
            SET 
                MapNatGroup_IsActive = 'N',
                MapNatGroup_LastUpdatedAt = '$MapNatGroup_LastUpdatedAt'
            WHERE MapNatGroup_ID = '$MapNatGroup_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);
        }
    }

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

            // map_nat_group
            // $MapNatGroup_ID = $prm['MapNatGroup_ID'];
            $MapNatGroup_ID = isset($prm['MapNatGroup_ID']) ? $prm['MapNatGroup_ID'] : 0;

            $sql_m = "SELECT * FROM map_nat_group where MapNatGroup_ID = '$MapNatGroup_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();

            // group start
            // $MapNatGroup_NatGroupID = $prm['MapNatGroup_NatGroupID'];

            $MapNatGroup_NatGroupID = isset($prm['MapNatGroup_NatGroupID']) ? $prm['MapNatGroup_NatGroupID'] : 0;
            $sql_g = "SELECT * FROM nat_group where Nat_GroupID = '$MapNatGroup_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();

            // empty rows_g
            if (empty($rows_m)) {
                $rows_m = [[
                    "MapNatGroup_ID"            => 0,
                    "MapNatGroup_NatGroupID"    => 0,
                    "MapNatGroup_Disc_coaID"    => 0,
                    "MapNatGroup_Disc_coaAccNo" => '',
                    "MapNatGroup_Disc_coaDesc"  => '',
                    "MapNatGroup_Retur_coaID"   => 0,
                    "MapNatGroup_Retur_coaAccNo"=> '',
                    "MapNatGroup_Retur_coaDesc" => '',
                    "MapNatGroup_Dp_coaID"      => 0,
                    "MapNatGroup_Dp_coaAccNo"   => '',
                    "MapNatGroup_Dp_coaDesc"    => '',
                    "MapNatGroup_Debt_coaID"    => 0,
                    "MapNatGroup_Debt_coaAccNo" => '',
                    "MapNatGroup_Debt_coaDesc"  => '',
                    "MapNatGroup_IsActive"      => 'Y',
                    "MapNatGroup_CreatedAt"     => '',
                    "MapNatGroup_LastUpdatedAt" => ''
                ]];
            }

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

            $qry_gx = $this->db_onedev->query($sql_gx);
            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_gx = $qry_gx->result_array();

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

            // group end

            // uang muka start
            // $MapNatGroup_Dp_coaID = $prm['MapNatGroup_Dp_coaID'];
            $MapNatGroup_Dp_coaID = isset($prm['MapNatGroup_Dp_coaID']) ? $prm['MapNatGroup_Dp_coaID'] : 0;
            $sql_u = "SELECT * FROM coa where coaID = '$MapNatGroup_Dp_coaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'
            ";

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

            $rows_u = $qry_u->result_array();

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

            // uang muka end

            // hutang start
            $MapNatGroup_Debt_coaID = $prm['MapNatGroup_Debt_coaID'];
            $sql_h = "SELECT * FROM coa where coaID = '$MapNatGroup_Debt_coaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'";

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

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

            // disc. pendapatan start
            $MapNatGroup_Disc_coaID = $prm['MapNatGroup_Disc_coaID'];
            $sql_d = "SELECT * FROM coa where coaID = '$MapNatGroup_Disc_coaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'";

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

            $rows_d = $qry_d->result_array();

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

            // retur start
            $MapNatGroup_Retur_coaID = $prm['MapNatGroup_Retur_coaID'];
            $sql_r = "SELECT * FROM coa where coaID = '$MapNatGroup_Retur_coaID'
            AND coaIsInput = 'Y' 
            AND coaIsActive = 'Y'";

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

            $rows_r = $qry_r->result_array();

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

            $response = [
                "records_group_all" => $rows_gx,
                "record_map_nat_group" => $rows_m,
                "records_group" => $rows_g,
                "records_uang_muka" => $rows_u,
                "records_hutang" => $rows_h,
                "records_disc" => $rows_d,
                "records_retur" => $rows_r,
                "sql" => $this->db_onedev->last_query()
            ];
            
            $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_coa_uang_muka = $prm['e_coa_uang_muka'];
            $e_coa_hutang = $prm['e_coa_hutang'];
            $e_coa_disc_pendapatan = $prm['e_coa_disc_pendapatan'];
            $e_coa_retur_pendapatan = $prm['e_coa_retur_pendapatan'];
            $MapNatGroup_ID = $prm['MapNatGroup_ID'];
    
            // defined variable
           
            $MapNatGroup_LastUpdatedAt = date('Y-m-d H:i:s');
            
            // // nat_group
            // $MapNatGroup_NatGroupID = $e_nat_group['Nat_GroupID'];
            
            // // disc pendaptan
            // $MapNatGroup_Disc_coaID = $e_coa_disc_pendapatan['coaID'];
            // $MapNatGroup_Disc_coaAccNo = $e_coa_disc_pendapatan['coaAccountNo'];
            // $MapNatGroup_Disc_coaDesc = $e_coa_disc_pendapatan['coaDescription'];
    
            // // retur pendapatan
            // $MapNatGroup_Retur_coaID = $e_coa_retur_pendapatan['coaID'];
            // $MapNatGroup_Retur_coaAccNo = $e_coa_retur_pendapatan['coaAccountNo'];
            // $MapNatGroup_Retur_coaDesc = $e_coa_retur_pendapatan['coaDescription'];
    
            // // uang muka
            // $MapNatGroup_Dp_coaID = $e_coa_uang_muka['coaID'];
            // $MapNatGroup_Dp_coaAccNo = $e_coa_uang_muka['coaAccountNo'];
            // $MapNatGroup_Dp_coaDesc = $e_coa_uang_muka['coaDescription'];
    
            // // hutang
            // $MapNatGroup_Debt_coaID = $e_coa_hutang['coaID'];
            // $MapNatGroup_Debt_coaAccNo = $e_coa_hutang['coaAccountNo'];
            // $MapNatGroup_Debt_coaDesc = $e_coa_hutang['coaDescription'];


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

            // disc pendapatan
            $MapNatGroup_Disc_coaID = isset($e_coa_disc_pendapatan['coaID']) ? $e_coa_disc_pendapatan['coaID'] : 0;
            $MapNatGroup_Disc_coaAccNo = isset($e_coa_disc_pendapatan['coaAccountNo']) ? $e_coa_disc_pendapatan['coaAccountNo'] : '';
            $MapNatGroup_Disc_coaDesc = isset($e_coa_disc_pendapatan['coaDescription']) ? $e_coa_disc_pendapatan['coaDescription'] : '';

            // retur pendapatan
            $MapNatGroup_Retur_coaID = isset($e_coa_retur_pendapatan['coaID']) ? $e_coa_retur_pendapatan['coaID'] : 0;
            $MapNatGroup_Retur_coaAccNo = isset($e_coa_retur_pendapatan['coaAccountNo']) ? $e_coa_retur_pendapatan['coaAccountNo'] : '';
            $MapNatGroup_Retur_coaDesc = isset($e_coa_retur_pendapatan['coaDescription']) ? $e_coa_retur_pendapatan['coaDescription'] : '';

            // uang muka
            $MapNatGroup_Dp_coaID = isset($e_coa_uang_muka['coaID']) ? $e_coa_uang_muka['coaID'] : 0;
            $MapNatGroup_Dp_coaAccNo = isset($e_coa_uang_muka['coaAccountNo']) ? $e_coa_uang_muka['coaAccountNo'] : '';
            $MapNatGroup_Dp_coaDesc = isset($e_coa_uang_muka['coaDescription']) ? $e_coa_uang_muka['coaDescription'] : '';

            // hutang
            $MapNatGroup_Debt_coaID = isset($e_coa_hutang['coaID']) ? $e_coa_hutang['coaID'] : 0;
            $MapNatGroup_Debt_coaAccNo = isset($e_coa_hutang['coaAccountNo']) ? $e_coa_hutang['coaAccountNo'] : '';
            $MapNatGroup_Debt_coaDesc = isset($e_coa_hutang['coaDescription']) ? $e_coa_hutang['coaDescription'] : '';
    
            $sql = "UPDATE map_nat_group 
            SET
                MapNatGroup_NatGroupID = '$MapNatGroup_NatGroupID',
                MapNatGroup_Disc_coaID = '$MapNatGroup_Disc_coaID',
                MapNatGroup_Disc_coaAccNo = '$MapNatGroup_Disc_coaAccNo',
                MapNatGroup_Disc_coaDesc = '$MapNatGroup_Disc_coaDesc',
                MapNatGroup_Retur_coaID = '$MapNatGroup_Retur_coaID',
                MapNatGroup_Retur_coaAccNo = '$MapNatGroup_Retur_coaAccNo',
                MapNatGroup_Retur_coaDesc = '$MapNatGroup_Retur_coaDesc',
                MapNatGroup_Dp_coaID = '$MapNatGroup_Dp_coaID',
                MapNatGroup_Dp_coaAccNo = '$MapNatGroup_Dp_coaAccNo',
                MapNatGroup_Dp_coaDesc = '$MapNatGroup_Dp_coaDesc',
                MapNatGroup_Debt_coaID = '$MapNatGroup_Debt_coaID',
                MapNatGroup_Debt_coaAccNo = '$MapNatGroup_Debt_coaAccNo',
                MapNatGroup_Debt_coaDesc = '$MapNatGroup_Debt_coaDesc',
                MapNatGroup_LastUpdatedAt = '$MapNatGroup_LastUpdatedAt'
            WHERE MapNatGroup_ID = '$MapNatGroup_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);
        }
    }
    
}
?>