<?php

class InventarisCoaMapping extends MY_Controller
{
    var $db;
    public function index()
    {
        echo "Inventaris COA Mapping API";
    }

    public function __construct()
    {
        parent::__construct();
    }


    ## QUERY ##
    public function getListCoa()
    {
        try {
            if (!$this->isLogin) {
                throw new Exception('Invalid token');
            }

            $para = $this->sys_input;
            $keyword = "%" . $para['keyword'] . "%";

            $sql = "SELECT
                    coaID,
                    coaAccountNo,
                    coaDescription
                FROM coa
                WHERE coaIsInput = 'Y'
                    AND (
                        coaDescription LIKE ?
                        OR coaAccountNo LIKE ?
                    )
                    AND coaIsActive = 'Y'
                LIMIT 15";
            $que = $this->db->query($sql, [$keyword, $keyword]);
            if (!$que) {
                throw new Exception('failed to query data inventaris gol', 1);
            }
            $data = $que->result_array();

            $this->sys_ok($data);
        } catch (Exception $e) {
            $msg = '[Error] ' . $e->getMessage();
            $code = $e->getCode();
            if ($code == 0) {
                $this->sys_error($msg);
            } else {
                $this->sys_error_db($msg);
            }
            exit;
        }
    }

    public function getListInventarisGol()
    {
        try {
            if (!$this->isLogin) {
                throw new Exception('Invalid token');
            }

            $sql = "SELECT
                M_InventarisGolID,
                M_InventarisGolCode,
                M_InventarisGolName
            FROM m_inventaris_gol gol
            WHERE gol.M_InventarisGolIsActive = 'Y'
            AND NOT EXISTS (
                SELECT 1
                FROM m_inventaris_coa_mapping m
                WHERE m.M_InventarisCoaMappingM_InventarisGolID = gol.M_InventarisGolID
                AND m.M_InventarisCoaMappingIsActive = 'Y'
            );";
            $que = $this->db->query($sql);
            if (!$que) {
                throw new Exception('failed to query data inventaris gol', 1);
            }
            $data = $que->result_array();

            $output = [
                'records' => $data,
                'total' => count($data)
            ];

            $this->sys_ok($output);
        } catch (Exception $e) {
            $msg = '[Error] ' . $e->getMessage();
            $code = $e->getCode();
            if ($code == 0) {
                $this->sys_error($msg);
            } else {
                $this->sys_error_db($msg);
            }
            exit;
        }
    }

    public function getListInventorygolMapping()
    {
        try {
            if (!$this->isLogin) {
                throw new Exception('invalid token');
            }

            $para = $this->sys_input;
            $keyword = "%" . $para['keyword'] . "%";

            $limit = 10;
            $offset = 0;
            if ($para['currpage'] > 0) {
                $offset = ($para['currpage'] - 1) * $limit;
            }

            $sql = "SELECT
                    M_InventarisGolID,
                    M_InventarisGolCode,
                    M_InventarisGolName,
                    M_InventarisCoaMappingID,
                    M_InventarisCoaMappingCoaInventarisID,
                    coaInv.coaAccountNo          AS CoaInventarisAccountNo,
                    coaInv.coaDescription        AS CoaInventarisDescription,
                    M_InventarisCoaMappingCoaHutangID,
                    coaHtg.coaAccountNo          AS CoaHutangAccountNo,
                    coaHtg.coaDescription        AS CoaHutangDescription,
                    M_InventarisCoaMappingCoaBebanPenyusutanID,
                    coaBbn.coaAccountNo          AS CoaBebanPenyusutanAccountNo,
                    coaBbn.coaDescription        AS CoaBebanPenyusutanDescription,
                    M_InventarisCoaMappingCoaAkumulasiPenyusutanID,
                    coaAkm.coaAccountNo          AS CoaAkumulasiPenyusutanAccountNo,
                    coaAkm.coaDescription        AS CoaAkumulasiPenyusutanDescription,
                    M_InventarisCoaMappingCoaLabaPelepasanID,
                    coaLab.coaAccountNo          AS CoaLabaPelepasanAccountNo,
                    coaLab.coaDescription        AS CoaLabaPelepasanDescription,
                    M_InventarisCoaMappingCoaRugiPelepasanID,
                    coaRug.coaAccountNo          AS CoaRugiPelepasanAccountNo,
                    coaRug.coaDescription        AS CoaRugiPelepasanDescription
                FROM m_inventaris_gol
                JOIN m_inventaris_coa_mapping
                    ON M_InventarisCoaMappingM_InventarisGolID = M_InventarisGolID
                    AND M_InventarisCoaMappingIsActive = 'Y'
                    AND M_InventarisGolIsActive = 'Y'
                    AND M_InventarisGolName LIKE ?
                LEFT JOIN coa AS coaInv
                    ON M_InventarisCoaMappingCoaInventarisID = coaInv.coaID
                LEFT JOIN coa AS coaHtg
                    ON M_InventarisCoaMappingCoaHutangID = coaHtg.coaID
                LEFT JOIN coa AS coaBbn
                    ON M_InventarisCoaMappingCoaBebanPenyusutanID = coaBbn.coaID
                LEFT JOIN coa AS coaAkm
                    ON M_InventarisCoaMappingCoaAkumulasiPenyusutanID = coaAkm.coaID
                LEFT JOIN coa AS coaLab
                    ON M_InventarisCoaMappingCoaLabaPelepasanID = coaLab.coaID
                LEFT JOIN coa AS coaRug
                    ON M_InventarisCoaMappingCoaRugiPelepasanID = coaRug.coaID
                WHERE M_InventarisGolIsActive = 'Y'";

            $sql_data = $sql . " ORDER BY M_InventarisGolID LIMIT ? OFFSET ? ";
            $que = $this->db->query($sql_data, [
                $keyword,
                $limit,
                $offset
            ]);
            if (!$que) {
                throw new Exception('failed to query data mapping coa inventaris golongan', 1);
            }

            $sql_total = "SELECT COUNT(*) AS total FROM ($sql) AS x";
            $que_total = $this->db->query($sql_total, [$keyword]);
            if (!$que_total) {
                throw new Exception('failed to get total rows data', 1);
            }

            $this->sys_ok([
                "records" => $que->result_array(),
                "total" => $que_total->row_array()['total']
            ]);
        } catch (Exception $e) {
            $msg = '[Error] ' . $e->getMessage();
            $code = $e->getCode();
            if ($code == 0) {
                $this->sys_error($msg);
            } else {
                $this->sys_error_db($msg);
            }
            exit;
        }
    }

    public function getInvCoaMappingDetail()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $para = $this->sys_input;

            $sql = "SELECT
                M_InventarisCoaMappingID,
                M_InventarisCoaMappingM_InventarisGolID,
                M_InventarisCoaMappingCoaInventarisID,
                M_InventarisCoaMappingCoaHutangID,
                M_InventarisCoaMappingCoaPembelianID,
                M_InventarisCoaMappingCoaBebanPenyusutanID,
                M_InventarisCoaMappingCoaAkumulasiPenyusutanID,
                M_InventarisCoaMappingCoaLabaPelepasanID,
                M_InventarisCoaMappingCoaRugiPelepasanID,
                M_InventarisCoaMappingCreatedUserID,
                M_InventarisCoaMappingCreated,
                M_InventarisCoaMappingLastUpdated
            FROM m_inventaris_coa_mapping
            WHERE M_InventarisCoaMappingIsActive = 'Y'
                AND M_InventarisCoaMappingID = ?";
            $query = $this->db->query($sql, [$para['M_InventarisCoaMappingID']]);
            if (!$query) {
                $this->sys_error_db("[Error] get data m_inventaris_coa_mapping");
                exit;
            }
            $data = $query->row_array();

            $this->sys_ok($data);
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    ## MUTATIONS ##
    public function createInvCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;
            $user = $this->sys_user;

            $sql = "INSERT INTO m_inventaris_coa_mapping (
                M_InventarisCoaMappingM_InventarisGolID,
                M_InventarisCoaMappingCoaInventarisID,
                M_InventarisCoaMappingCoaHutangID,
                M_InventarisCoaMappingCoaPembelianID,
                M_InventarisCoaMappingCoaBebanPenyusutanID,
                M_InventarisCoaMappingCoaAkumulasiPenyusutanID,
                M_InventarisCoaMappingCoaLabaPelepasanID,
                M_InventarisCoaMappingCoaRugiPelepasanID,
                M_InventarisCoaMappingCreatedUserID,
                M_InventarisCoaMappingCreated,
                M_InventarisCoaMappingLastUpdated
            ) VALUES (?,?,?,?,?,?,?,?,?,NOW(),NOW())";
            $query = $this->db->query($sql, [
                $para['M_InventarisGolID'],
                $para['CoaInventarisID'],
                $para['CoaHutangID'],
                $para['CoaPembelianID'],
                $para['CoaBebanPenyusutanID'],
                $para['CoaAkumulasiPenyusutanID'],
                $para['CoaLabaPelepasanID'],
                $para['CoaRugiPelepasanID'],
                $user['M_UserID']
            ]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] insert into table m_inventaris_coa_mapping");
                exit;
            }

            $insertID = $this->db->insert_id();

            $this->db->trans_commit();
            $this->sys_ok($insertID);
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    public function editInvCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;
            $user = $this->sys_user;

            $sql = "UPDATE m_inventaris_coa_mapping SET
                M_InventarisCoaMappingM_InventarisGolID = ?,
                M_InventarisCoaMappingCoaInventarisID = ?,
                M_InventarisCoaMappingCoaHutangID = ?,
                M_InventarisCoaMappingCoaPembelianID = ?,
                M_InventarisCoaMappingCoaBebanPenyusutanID = ?,
                M_InventarisCoaMappingCoaAkumulasiPenyusutanID = ?,
                M_InventarisCoaMappingCoaLabaPelepasanID = ?,
                M_InventarisCoaMappingCoaRugiPelepasanID = ?,
                M_InventarisCoaMappingLastUpdated = NOW()
            WHERE M_InventarisCoaMappingIsActive = 'Y'
                AND M_InventarisCoaMappingID = ?";
            $query = $this->db->query($sql, [
                $para['M_InventarisGolID'],
                $para['CoaInventarisID'],
                $para['CoaHutangID'],
                $para['CoaPembelianID'],
                $para['CoaBebanPenyusutanID'],
                $para['CoaAkumulasiPenyusutanID'],
                $para['CoaLabaPelepasanID'],
                $para['CoaRugiPelepasanID'],
                $para['M_InventarisCoaMappingID']
            ]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] update table m_inventaris_coa_mapping");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok("[Success] update data inventaris coa mapping");
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    public function deleteInvCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;

            $sql = "UPDATE m_inventaris_coa_mapping SET
                M_InventarisCoaMappingIsActive = 'N',
                M_InventarisCoaMappingLastUpdated = NOW()
            WHERE M_InventarisCoaMappingIsActive = 'Y'
                AND M_InventarisCoaMappingID = ?";
            $query = $this->db->query($sql, [$para['M_InventarisCoaMappingID']]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] soft delete data m_inventaris_coa_mapping");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok("[Success] delete data inventaris coa mapping");
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }


    ## QUERY ITEM ##
    public function getListItemInventaris()
    {
        try {
            if (!$this->isLogin) {
                throw new Exception('Invalid token');
            }

            $para = $this->sys_input;

            $sql = "SELECT
                    M_ItemID,
                    M_ItemCode,
                    M_ItemDesc,
                    M_ItemM_InventarisGolID AS itemGolID,
                    IFNULL(M_InventarisItemCoaMappingID, 0) AS itemCoaMapID,
                    M_InventarisItemCoaMappingCoaInventarisID,
                    coaInv.coaAccountNo          AS CoaInventarisAccountNo,
                    coaInv.coaDescription        AS CoaInventarisDescription,
                    M_InventarisItemCoaMappingCoaHutangID,
                    coaHtg.coaAccountNo          AS CoaHutangAccountNo,
                    coaHtg.coaDescription        AS CoaHutangDescription,
                    M_InventarisItemCoaMappingCoaBebanPenyusutanID,
                    coaBbn.coaAccountNo          AS CoaBebanPenyusutanAccountNo,
                    coaBbn.coaDescription        AS CoaBebanPenyusutanDescription,
                    M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID,
                    coaAkm.coaAccountNo          AS CoaAkumulasiPenyusutanAccountNo,
                    coaAkm.coaDescription        AS CoaAkumulasiPenyusutanDescription,
                    M_InventarisItemCoaMappingCoaLabaPelepasanID,
                    coaLab.coaAccountNo          AS CoaLabaPelepasanAccountNo,
                    coaLab.coaDescription        AS CoaLabaPelepasanDescription,
                    M_InventarisItemCoaMappingCoaRugiPelepasanID,
                    coaRug.coaAccountNo          AS CoaRugiPelepasanAccountNo,
                    coaRug.coaDescription        AS CoaRugiPelepasanDescription
                FROM m_item
                LEFT JOIN m_inventaris_item_coa_mapping 
                    ON M_ItemID = M_InventarisItemCoaMappingM_ItemID
                    AND M_InventarisItemCoaMappingIsActive = 'Y'
                LEFT JOIN coa AS coaInv
                    ON M_InventarisItemCoaMappingCoaInventarisID = coaInv.coaID
                LEFT JOIN coa AS coaHtg
                    ON M_InventarisItemCoaMappingCoaHutangID = coaHtg.coaID
                LEFT JOIN coa AS coaBbn
                    ON M_InventarisItemCoaMappingCoaBebanPenyusutanID = coaBbn.coaID
                LEFT JOIN coa AS coaAkm
                    ON M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID = coaAkm.coaID
                LEFT JOIN coa AS coaLab
                    ON M_InventarisItemCoaMappingCoaLabaPelepasanID = coaLab.coaID
                LEFT JOIN coa AS coaRug
                    ON M_InventarisItemCoaMappingCoaRugiPelepasanID = coaRug.coaID
                WHERE M_ItemItem_CategoryID = 2
                    AND M_ItemM_InventarisGolID = ?
                    AND M_ItemIsActive = 'Y'";
            $que = $this->db->query($sql, [
                $para['golID']
            ]);
            if (!$que) {
                throw new Exception('failed to query data inventaris gol', 1);
            }
            $data = $que->result_array();

            $output = [
                'records' => $data,
                'total' => count($data)
            ];

            $this->sys_ok($output);
        } catch (Exception $e) {
            $msg = '[Error] ' . $e->getMessage();
            $code = $e->getCode();
            if ($code == 0) {
                $this->sys_error($msg);
            } else {
                $this->sys_error_db($msg);
            }
            exit;
        }
    }

    public function getInvItemCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $para = $this->sys_input;

            $sql = "SELECT
                M_InventarisItemCoaMappingID,
                M_InventarisItemCoaMappingM_ItemID,
                M_InventarisItemCoaMappingCoaInventarisID,
                M_InventarisItemCoaMappingCoaHutangID,
                M_InventarisItemCoaMappingCoaPembelianID,
                M_InventarisItemCoaMappingCoaBebanPenyusutanID,
                M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID,
                M_InventarisItemCoaMappingCoaLabaPelepasanID,
                M_InventarisItemCoaMappingCoaRugiPelepasanID,
                M_InventarisItemCoaMappingCreatedUserID,
                M_InventarisItemCoaMappingCreated,
                M_InventarisItemCoaMappingLastUpdated
            FROM m_inventaris_item_coa_mapping
            WHERE M_InventarisItemCoaMappingIsActive = 'Y'
                AND M_InventarisItemCoaMappingID = ?";
            $query = $this->db->query($sql, [$para['M_InventarisItemCoaMappingID']]);
            if (!$query) {
                $this->sys_error_db("[Error] get data m_inventaris_item_coa_mapping");
                exit;
            }
            $data = $query->row_array();

            $this->sys_ok($data);
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    ## MUTATIONS ITEM ##
    public function createInvItemCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;
            $user = $this->sys_user;

            $sql = "INSERT INTO m_inventaris_item_coa_mapping (
                M_InventarisItemCoaMappingM_ItemID,
                M_InventarisItemCoaMappingCoaInventarisID,
                M_InventarisItemCoaMappingCoaHutangID,
                M_InventarisItemCoaMappingCoaPembelianID,
                M_InventarisItemCoaMappingCoaBebanPenyusutanID,
                M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID,
                M_InventarisItemCoaMappingCoaLabaPelepasanID,
                M_InventarisItemCoaMappingCoaRugiPelepasanID,
                M_InventarisItemCoaMappingCreatedUserID,
                M_InventarisItemCoaMappingCreated,
                M_InventarisItemCoaMappingLastUpdated
            ) VALUES (?,?,?,?,?,?,?,?,?,NOW(),NOW())";
            $query = $this->db->query($sql, [
                $para['M_ItemID'],
                $para['CoaInventarisID'],
                $para['CoaHutangID'],
                $para['CoaPembelianID'],
                $para['CoaBebanPenyusutanID'],
                $para['CoaAkumulasiPenyusutanID'],
                $para['CoaLabaPelepasanID'],
                $para['CoaRugiPelepasanID'],
                $user['M_UserID']
            ]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] insert into table m_inventaris_item_coa_mapping");
                exit;
            }

            $insertID = $this->db->insert_id();

            $this->db->trans_commit();
            $this->sys_ok("[Success] insert coa item");
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    public function editInvItemCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;
            $user = $this->sys_user;

            $sql = "UPDATE m_inventaris_item_coa_mapping SET
                M_InventarisItemCoaMappingM_ItemID = ?,
                M_InventarisItemCoaMappingCoaInventarisID = ?,
                M_InventarisItemCoaMappingCoaHutangID = ?,
                M_InventarisItemCoaMappingCoaPembelianID = ?,
                M_InventarisItemCoaMappingCoaBebanPenyusutanID = ?,
                M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID = ?,
                M_InventarisItemCoaMappingCoaLabaPelepasanID = ?,
                M_InventarisItemCoaMappingCoaRugiPelepasanID = ?,
                M_InventarisItemCoaMappingLastUpdated = NOW()
            WHERE M_InventarisItemCoaMappingIsActive = 'Y'
                AND M_InventarisItemCoaMappingID = ?";
            $query = $this->db->query($sql, [
                $para['M_ItemID'],
                $para['CoaInventarisID'],
                $para['CoaHutangID'],
                $para['CoaPembelianID'],
                $para['CoaBebanPenyusutanID'],
                $para['CoaAkumulasiPenyusutanID'],
                $para['CoaLabaPelepasanID'],
                $para['CoaRugiPelepasanID'],
                $para['M_InventarisItemCoaMappingID']
            ]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] update table m_inventaris_item_coa_mapping");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok("[Success] update data inventaris item coa mapping");
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    public function deleteInvItemCoaMapping()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("invalid token");
                exit;
            }

            $this->db->trans_begin();

            $para = $this->sys_input;

            $sql = "UPDATE m_inventaris_item_coa_mapping SET
                M_InventarisItemCoaMappingIsActive = 'N',
                M_InventarisItemCoaMappingLastUpdated = NOW()
            WHERE M_InventarisItemCoaMappingIsActive = 'Y'
                AND M_InventarisItemCoaMappingID = ?";
            $query = $this->db->query($sql, [$para['M_InventarisItemCoaMappingID']]);
            if (!$query) {
                $this->db->trans_rollback();
                $this->sys_error_db("[Error] soft delete data m_inventaris_item_coa_mapping");
                exit;
            }

            $this->db->trans_commit();
            $this->sys_ok("[Success] delete data inventaris item coa mapping");
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }
}
