<?php
class Item 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);
    }

    // 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%" : "%%";
        $user = $this->sys_user;
        $regionalID = $user['S_RegionalID'];
        $branchCode = $user['M_BranchCode'];

       /* if (isset($branchCode)) {
            $join_branch = " JOIN m_branch ON M_BranchCode = '{$branchCode}'
            JOIN s_regional ON S_RegionalID = '{$regionalID}'";
            $filter_branch = " AND Fa_ItemS_RegionalID = '{$regionalID}'";
        }else{
            $join_branch = " JOIN s_regional ON S_RegionalID = '{$regionalID}'
            LEFT JOIN m_branch ON M_BranchS_RegionalID = '{$regionalID}'";
            $filter_branch = " AND Fa_ItemS_RegionalID = '{$regionalID}'";
        } */
        $join_branch = " JOIN s_regional ON S_RegionalID = '{$regionalID}'
        LEFT JOIN m_branch ON M_BranchS_RegionalID = '{$regionalID}'";
        $filter_branch = " AND Fa_ItemS_RegionalID = '{$regionalID}'";
        $number_limit = 10;
        $number_offset = ($prm["current_page"] > 0) ? ($prm["current_page"] - 1) * $number_limit : 0;

        $sql = "SELECT 
                    f.Fa_ItemID,
                    IFNULL(f.Fa_ItemDate, '') AS Fa_ItemDate,
                    IFNULL(f.Fa_ItemNumber, '') AS Fa_ItemNumber,
                    IFNULL(f.Fa_ItemType, '') AS Fa_ItemType,
                    IFNULL(f.Fa_ItemNote, '') AS Fa_ItemNote,
                    IFNULL(f.Fa_ItemM_DepartmentID, 0) AS Fa_ItemM_DepartmentID,
                    IFNULL(f.Fa_ItemBranchFaPercentID, 0) AS Fa_ItemBranchFaPercentID,
                    IFNULL(f.Fa_ItemM_ItemID, 0) AS Fa_ItemM_ItemID,
                    IFNULL(f.Fa_ItemQty, 0) AS Fa_ItemQty,
                    IFNULL(f.Fa_ItemPurchaseDate, '') AS Fa_ItemPurchaseDate,
                    IFNULL(f.Fa_ItemPurchasePrice, 0.00) AS Fa_ItemPurchasePrice,
                    IFNULL(f.Fa_ItemAcquisitionPrice, 0.00) AS Fa_ItemAcquisitionPrice,
                    IFNULL(f.Fa_ItemNetBookValue, 0.00) AS Fa_ItemNetBookValue,
                    IFNULL(f.Fa_ItemDeprePerMonth, 0.00) AS Fa_ItemDeprePerMonth,
                    IFNULL(f.Fa_ItemFa_ClassCoaID, 0) AS Fa_ItemFa_ClassCoaID,
                    IFNULL(f.Fa_ItemFa_ClassAccumDepreCoaID, 0) AS Fa_ItemFa_ClassAccumDepreCoaID,
                    IFNULL(f.Fa_ItemFa_ClassDepreCoaID, 0) AS Fa_ItemFa_ClassDepreCoaID,
                    IFNULL(f.Fa_ItemAcquisitionCoaID, 0) AS Fa_ItemAcquisitionCoaID,
                    IFNULL(f.Fa_ItemCurrentValue, 0.00) AS Fa_ItemCurrentValue,
                    IFNULL(f.Fa_ItemAccumDepre, 0.00) AS Fa_ItemAccumDepre,
                    IFNULL(f.Fa_ItemStatus, '') AS Fa_ItemStatus,
                    IFNULL(f.Fa_ItemUserID, 0) AS Fa_ItemUserID,
                    IFNULL(f.Fa_ItemCreated, CURRENT_TIMESTAMP) AS Fa_ItemCreated,
                    IFNULL(f.Fa_ItemLastUpdated, CURRENT_TIMESTAMP) AS Fa_ItemLastUpdated,
                    Fa_ItemInventarisNumber,

                M_ItemID,
                M_ItemCode,	
                M_ItemDesc,	
                M_ItemItem_CategoryID,
                M_ItemInventoryCode,
                M_ItemNat_GroupID,	
                M_ItemNat_SubGroupID,	
                M_ItemFa_ClassID,
                Fa_ClassID,	
                Fa_ClassName,	
                Fa_ClassFlagType,
                Fa_ClassCoaID,	
                Fa_ClassCoaAccountNo,
                Fa_ClassCoaDesc,
                Fa_ClassDepreCorp,
                Fa_ClassDepreGov,
                Fa_ClassAccumDepreCoaID,
                Fa_ClassAccumDepreCoaAccountNo,	
                Fa_ClassAccumDepreCoaDesc,	
                Fa_ClassDepreCoaID,	
                Fa_ClassAccumNote,	
                M_ItemM_InventarisGolID,	
                M_ItemPurchaseUOM,
                M_ItemBaseUOM,
                M_ItemItem_UnitID,
                Fa_ClassAgeAsset,
                    -- Data dari tabel coa untuk Class CoA
                    IFNULL(c_class.coaID, 0) AS ClassCoaID,
                    IFNULL(c_class.coaAccountNo, '') AS ClassCoaAccountNo,
                    IFNULL(c_class.coaDescription, '') AS ClassCoaDescription,

                    -- Data dari tabel coa untuk Accumulated Depreciation CoA
                    IFNULL(c_accum.coaID, 0) AS AccumDepreCoaID,
                    IFNULL(c_accum.coaAccountNo, '') AS AccumDepreCoaAccountNo,
                    IFNULL(c_accum.coaDescription, '') AS AccumDepreCoaDescription,

                    -- Data dari tabel coa untuk Depre
                    IFNULL(c_depre.coaID, 0) AS DepreCoaID,
                    IFNULL(c_depre.coaAccountNo, '') AS DepreCoaAccountNo,
                    IFNULL(c_depre.coaDescription, '') AS DepreCoaDescription,

                    M_DepartmentID,
                    M_DepartmentCode,	
                    M_DepartmentName,

                    IFNULL(c_acquisition.coaID, 0) AS AcquisitionCoaID,
                    IFNULL(c_acquisition.coaAccountNo, '') AS AcquisitionCoaAccountNo,
                    IFNULL(c_acquisition.coaDescription, '') AS AcquisitionCoaDescription,

                    M_BranchID,	
                    M_BranchS_RegionalID,
                    M_BranchCode,
                    M_BranchName,
                    S_RegionalID,	
                    S_RegionalName,
                    BranchFaPercentID,
                    BranchFaPercentType




                FROM fa_item f
                LEFT JOIN m_item ON M_ItemID = Fa_ItemM_ItemID
                LEFT JOIN fa_class ON Fa_ClassID = M_ItemFa_ClassID
                LEFT JOIN m_department ON M_DepartmentID = Fa_ItemM_DepartmentID
                LEFT JOIN branch_fa_percent ON BranchFaPercentID = Fa_ItemBranchFaPercentID
                LEFT JOIN coa c_class 
                    ON f.Fa_ItemFa_ClassCoaID = c_class.coaID 
                LEFT JOIN coa c_accum 
                    ON f.Fa_ItemFa_ClassAccumDepreCoaID = c_accum.coaID 
                LEFT JOIN coa c_depre 
                    ON f.Fa_ItemFa_ClassDepreCoaID = c_depre.coaID 
                LEFT JOIN coa c_acquisition 
                    ON f.Fa_ItemAcquisitionCoaID = c_acquisition.coaID
                $join_branch
                WHERE f.Fa_ItemIsActive = 'Y'
                    $filter_branch
                    AND (
                        f.Fa_ItemName LIKE CONCAT('%', '$search', '%')
                        OR f.Fa_ItemNumber LIKE CONCAT('%', '$search', '%')
                    )
                    GROUP BY f.Fa_ItemID
                ORDER BY f.Fa_ItemID DESC";
                //echo $this->db_onedev->last_query();

        $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 fa_item 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 fa_item 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());
    }
}

    // ac getAcAccumDepre
    function getAcAccumDepre()
    {
        $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 getAcCoa
    function getAcCoa()
    {
        $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);
        }
    }
    function getItem()
    {
        $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 
                M_ItemID,
                M_ItemCode,	
                M_ItemDesc,	
                M_ItemItem_CategoryID,
                M_ItemInventoryCode,
                M_ItemNat_GroupID,	
                M_ItemNat_SubGroupID,	
                M_ItemFa_ClassID,
                Fa_ClassID,	
                Fa_ClassName,	
                Fa_ClassFlagType,
                Fa_ClassCoaID,	
                Fa_ClassCoaAccountNo,
                Fa_ClassCoaDesc,
                Fa_ClassDepreCorp,
                Fa_ClassDepreGov,
                Fa_ClassAccumDepreCoaID,
                Fa_ClassAccumDepreCoaAccountNo,	
                Fa_ClassAccumDepreCoaDesc,	
                Fa_ClassDepreCoaID,	
                coaID,	
                coaAccountNo,	
                coaDescription,
                Fa_ClassAccumNote,	
                M_ItemM_InventarisGolID,	
                M_ItemPurchaseUOM,
                M_ItemBaseUOM,
                M_ItemItem_UnitID,
                Fa_ClassAgeAsset
            FROM m_item
            JOIN fa_class ON Fa_ClassID = M_ItemFa_ClassID
            LEFT JOIN coa ON coaID = Fa_ClassDepreCoaID
            WHERE M_ItemIsActive = 'Y'
            AND M_ItemItem_CategoryID = '3'
            AND 
            (
                M_ItemDesc LIKE '$where_search_desc'
                OR M_ItemCode 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);
        }
    }
    function getDepartment()
    {
        $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 *
            FROM m_department
            WHERE M_DepartmentIsActive = 'Y'
            AND 
            (
                M_DepartmentName LIKE '$where_search_desc'
                OR M_DepartmentCode 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);
        }
    }

    function getPresentase()
    {
        $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 *
            FROM branch_fa_percent
            WHERE BranchFaPercentIsActive = 'Y'
            AND 
            (
                BranchFaPercentType LIKE '$where_search_desc'

            )";

            // 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();
        $userid = $this->sys_user['M_UserID'];

        $pdSql = "SELECT `fn_numbering`('FAI') AS FAI";
            $exec = $this->db->query($pdSql, []);
            $pd = "";
            $dateNow = date('Y-m-d');

            if (!$exec) {
                $this->db->trans_rollback();
                $this->sys_error_db("payment voucher insert error", $this->db);
                exit;
            } else {
                $pd = $exec->result_array()[0]["FAI"];
            }

        // Extract input parameters
        $Fa_ItemNote = $prm['Fa_ItemNote'];
        $Fa_ItemM_DepartmentID = isset($prm['Fa_ItemM_DepartmentID']) ? $prm['Fa_ItemM_DepartmentID'] : 0;
        $Fa_ItemBranchFaPercentID = isset($prm['Fa_ItemBranchFaPercentID']) ? $prm['Fa_ItemBranchFaPercentID'] : 0;
        $Fa_ItemM_ItemID = isset($prm['Fa_ItemM_ItemID']) ? $prm['Fa_ItemM_ItemID'] : 0;
        $Fa_ItemQty = isset($prm['Fa_ItemQty']) ? $prm['Fa_ItemQty'] : 0;
        $Fa_ItemPurchasePrice = isset($prm['Fa_ItemPurchasePrice']) ? $prm['Fa_ItemPurchasePrice'] : 0.00;
        $Fa_ItemAcquisitionPrice = isset($prm['Fa_ItemAcquisitionPrice']) ? $prm['Fa_ItemAcquisitionPrice'] : 0.00;
        $Fa_ItemNetBookValue = isset($prm['Fa_ItemNetBookValue']) ? $prm['Fa_ItemNetBookValue'] : 0.00;
        $Fa_ItemDeprePerMonth = isset($prm['Fa_ItemDeprePerMonth']) ? $prm['Fa_ItemDeprePerMonth'] : 0.00;
        $Fa_ItemFa_ClassCoaID = isset($prm['Fa_ItemFa_ClassCoaID']) ? $prm['Fa_ItemFa_ClassCoaID'] : 0;
        $Fa_ItemFa_ClassAccumDepreCoaID = isset($prm['Fa_ItemFa_ClassAccumDepreCoaID']) ? $prm['Fa_ItemFa_ClassAccumDepreCoaID'] : 0;
        $Fa_ItemFa_ClassDepreCoaID = isset($prm['Fa_ItemFa_ClassDepreCoaID']) ? $prm['Fa_ItemFa_ClassDepreCoaID'] : 0;
        $Fa_ItemAcquisitionCoaID = isset($prm['Fa_ItemAcquisitionCoaID']) ? $prm['Fa_ItemAcquisitionCoaID'] : 0;
        $Fa_ItemCurrentValue = isset($prm['Fa_ItemCurrentValue']) ? $prm['Fa_ItemCurrentValue'] : 0.00;
        $Fa_ItemAccumDepre = isset($prm['Fa_ItemAccumDepre']) ? $prm['Fa_ItemAccumDepre'] : 0.00;
        $Fa_ItemStatus = isset($prm['Fa_ItemStatus']) ? $prm['Fa_ItemStatus'] : 'Draft';
        $Fa_ItemInventarisNumber = isset($prm['Fa_ItemInventarisNumber']) ? $prm['Fa_ItemInventarisNumber'] : '';
        $Fa_ItemS_RegionalID = isset($prm['Fa_ItemS_RegionalID']) ? $prm['Fa_ItemS_RegionalID'] : 0;
        $Fa_ItemM_BranchCode = isset($prm['Fa_ItemM_BranchCode']) ? $prm['Fa_ItemM_BranchCode'] : '';

        // Construct SQL query
        $sql = "INSERT INTO fa_item (
                    Fa_ItemS_RegionalID,	
                    Fa_ItemM_BranchCode,
                    Fa_ItemDate,
                    Fa_ItemNumber,
                    Fa_ItemType,
                    Fa_ItemNote,
                    Fa_ItemM_DepartmentID,
                    Fa_ItemBranchFaPercentID,
                    Fa_ItemM_ItemID,
                    Fa_ItemQty,
                    Fa_ItemPurchaseDate,
                    Fa_ItemPurchasePrice,
                    Fa_ItemAcquisitionPrice,
                    Fa_ItemNetBookValue,
                    Fa_ItemDeprePerMonth,
                    Fa_ItemFa_ClassCoaID,
                    Fa_ItemFa_ClassAccumDepreCoaID,
                    Fa_ItemFa_ClassDepreCoaID,
                    Fa_ItemAcquisitionCoaID,
                    Fa_ItemCurrentValue,
                    Fa_ItemAccumDepre,
                    Fa_ItemInventarisNumber,
                    Fa_ItemStatus,
                    Fa_ItemUserID,
                    Fa_ItemCreated,
                    Fa_ItemLastUpdated
                ) VALUES (
                    '$Fa_ItemS_RegionalID',
                    '$Fa_ItemM_BranchCode',
                    now(),
                    '$pd',
                    'ACCOUNT',
                    '$Fa_ItemNote',
                    '$Fa_ItemM_DepartmentID',
                    '$Fa_ItemBranchFaPercentID',
                    '$Fa_ItemM_ItemID',
                    '$Fa_ItemQty',
                    now(),
                    '$Fa_ItemPurchasePrice',
                    '$Fa_ItemAcquisitionPrice',
                    '$Fa_ItemNetBookValue',
                    '$Fa_ItemDeprePerMonth',
                    '$Fa_ItemFa_ClassCoaID',
                    '$Fa_ItemFa_ClassAccumDepreCoaID',
                    '$Fa_ItemFa_ClassDepreCoaID',
                    '$Fa_ItemAcquisitionCoaID',
                    '$Fa_ItemCurrentValue',
                    '$Fa_ItemAccumDepre',
                    '$Fa_ItemInventarisNumber',
                    '$Fa_ItemStatus',
                    '$userid',
                    NOW(),
                    NOW()
                )";

        // Execute query
        $qry = $this->db_onedev->query($sql);
        if (!$qry) {
            $this->db_onedev->trans_rollback();
            $error = array(
                "message" => $this->db_onedev->error()["message"],
                "sql" => $this->db_onedev->last_query()
            );
            $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;
            }

            // fa_item
            $Fa_ItemID = isset($prm['Fa_ItemID']) ? $prm['Fa_ItemID'] : 0;

            $sql_m = "SELECT * FROM fa_item where Fa_ItemID = '$Fa_ItemID'";

            $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();

            // coa start
            $Fa_ItemCoaID = isset($prm['Fa_ItemCoaID']) ? $prm['Fa_ItemCoaID'] : 0;
            $sql_u = "SELECT * FROM coa where coaID = '$Fa_ItemCoaID'
            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
                ]];
            }

            // coa end

            // accum depre start
            $Fa_ItemAccumDepreCoaID = $prm['Fa_ItemAccumDepreCoaID'];
            $sql_h = "SELECT * FROM coa where coaID = '$Fa_ItemAccumDepreCoaID'
            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
                ]];
            }
            // accum depre end

            

            $response = [
                "record_fa_item" => $rows_m,
                "records_coa" => $rows_u,
                "records_accum_depre" => $rows_h,
                "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();
            $userid = $this->sys_user['M_UserID'];
    
            $Fa_ItemID = $prm['Fa_ItemID'];
            $Fa_ItemNote = $prm['Fa_ItemNote'];
            $Fa_ItemM_DepartmentID = isset($prm['Fa_ItemM_DepartmentID']) ? $prm['Fa_ItemM_DepartmentID'] : 0;
            $Fa_ItemBranchFaPercentID = isset($prm['Fa_ItemBranchFaPercentID']) ? $prm['Fa_ItemBranchFaPercentID'] : 0;
        $Fa_ItemM_ItemID = isset($prm['Fa_ItemM_ItemID']) ? $prm['Fa_ItemM_ItemID'] : 0;
        $Fa_ItemQty = isset($prm['Fa_ItemQty']) ? $prm['Fa_ItemQty'] : 0;
        $Fa_ItemPurchasePrice = isset($prm['Fa_ItemPurchasePrice']) ? $prm['Fa_ItemPurchasePrice'] : 0.00;
        $Fa_ItemAcquisitionPrice = isset($prm['Fa_ItemAcquisitionPrice']) ? $prm['Fa_ItemAcquisitionPrice'] : 0.00;
        $Fa_ItemNetBookValue = isset($prm['Fa_ItemNetBookValue']) ? $prm['Fa_ItemNetBookValue'] : 0.00;
        $Fa_ItemDeprePerMonth = isset($prm['Fa_ItemDeprePerMonth']) ? $prm['Fa_ItemDeprePerMonth'] : 0.00;
        $Fa_ItemFa_ClassCoaID = isset($prm['Fa_ItemFa_ClassCoaID']) ? $prm['Fa_ItemFa_ClassCoaID'] : 0;
        $Fa_ItemFa_ClassAccumDepreCoaID = isset($prm['Fa_ItemFa_ClassAccumDepreCoaID']) ? $prm['Fa_ItemFa_ClassAccumDepreCoaID'] : 0;
        $Fa_ItemFa_ClassDepreCoaID = isset($prm['Fa_ItemFa_ClassDepreCoaID']) ? $prm['Fa_ItemFa_ClassDepreCoaID'] : 0;
        $Fa_ItemAcquisitionCoaID = isset($prm['Fa_ItemAcquisitionCoaID']) ? $prm['Fa_ItemAcquisitionCoaID'] : 0;
        $Fa_ItemCurrentValue = isset($prm['Fa_ItemCurrentValue']) ? $prm['Fa_ItemCurrentValue'] : 0.00;
        $Fa_ItemAccumDepre = isset($prm['Fa_ItemAccumDepre']) ? $prm['Fa_ItemAccumDepre'] : 0.00;
        $Fa_ItemStatus = isset($prm['Fa_ItemStatus']) ? $prm['Fa_ItemStatus'] : 'Draft';
        $Fa_ItemInventarisNumber = isset($prm['Fa_ItemInventarisNumber']) ? $prm['Fa_ItemInventarisNumber'] : '';
        $Fa_ItemS_RegionalID = isset($prm['Fa_ItemS_RegionalID']) ? $prm['Fa_ItemS_RegionalID'] : 0;
        $Fa_ItemM_BranchCode = isset($prm['Fa_ItemM_BranchCode']) ? $prm['Fa_ItemM_BranchCode'] : '';
    
            $sql = "UPDATE fa_item SET 
                    Fa_ItemNote = '{$Fa_ItemNote}',
                    Fa_ItemM_DepartmentID = '{$Fa_ItemM_DepartmentID}',
                    Fa_ItemBranchFaPercentID = '{$Fa_ItemBranchFaPercentID}',
                    Fa_ItemM_ItemID = '{$Fa_ItemM_ItemID}',
                    Fa_ItemQty = '{$Fa_ItemQty}',
                    Fa_ItemAcquisitionPrice = '{$Fa_ItemAcquisitionPrice}',
                    Fa_ItemNetBookValue = '{$Fa_ItemNetBookValue}',
                    Fa_ItemDeprePerMonth = '{$Fa_ItemDeprePerMonth}',
                    Fa_ItemFa_ClassCoaID = '{$Fa_ItemFa_ClassCoaID}',
                    Fa_ItemFa_ClassAccumDepreCoaID = '{$Fa_ItemFa_ClassAccumDepreCoaID}',
                    Fa_ItemFa_ClassDepreCoaID = '{$Fa_ItemFa_ClassDepreCoaID}',
                    Fa_ItemAcquisitionCoaID = '{$Fa_ItemAcquisitionCoaID}',
                    Fa_ItemCurrentValue = '{$Fa_ItemCurrentValue}',
                    Fa_ItemAccumDepre = '{$Fa_ItemAccumDepre}',
                    Fa_ItemInventarisNumber = '{$Fa_ItemInventarisNumber}',
                    Fa_ItemUserID = '{$userid}',
                    Fa_ItemLastUpdated = now(),
                    Fa_ItemS_RegionalID = '{$Fa_ItemS_RegionalID}',
                    Fa_ItemM_BranchCode = '{$Fa_ItemM_BranchCode}'
                WHERE Fa_ItemID = '$Fa_ItemID'
            ";
    
    
            // 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();
            $userid = $this->sys_user['M_UserID'];
    
            $Fa_ItemID = $prm['Fa_ItemID'];


            $sql = "UPDATE fa_item 
            SET 
                Fa_ItemIsActive = 'N',
                Fa_ItemLastUpdated = now(),
                Fa_ItemUserID = '$userid'
            WHERE Fa_ItemID = '$Fa_ItemID'";

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