<?php

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

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

    ## 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);
        }
    }

    ## QUERY ##
    public function getInvCoaMapping() {
        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 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);
        }
    }

    ## 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($insertID);
        } catch (Exception $exc) {
            $msg = $exc->getMessage();
            $this->sys_error($msg);
        }
    }

    ## QUERY ITEM ##
    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 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);
        }
    }
}
