<?php
class CoaMapInventaris extends MY_Controller
{
    var $db;
    public function index()
    {
        echo "coa map inventaris";
    }

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

    public function InsertMapCoaInventaris()
    {
        try {
            if (!$this->isLogin) {
                throw new Exception(message: 'invalid token', code: 1);
            }
            $para = $this->sys_input;
            $user = $this->sys_user;

            $this->db->trans_begin();
            $sql = "INSERT INTO coa_map_inventaris (
                    CoaMapInventarisM_ItemID,
                    CoaMapInventarisHutangCoaID,
                    CoaMapInventarisHutangCoaNo,
                    CoaMapInventarisHutangCoaDesc,
                    CoaMapInventarisCreated,
                    CoaMapInventarisUserID
                ) VALUES (?,?,?,?,NOW(),?)";
            $que = $this->db->query($sql, [
                $para['itemID'],
                $para['HutangCoaID'],
                $para['HutangCoaNo'],
                $para['HutangCoaDesc'],
                $user['M_UserID']
            ]);
            if (!$que) {
                $this->db->trans_rollback();
                throw new Exception(message: 'failed insert data', code: 2);
            }

            $this->db->trans_commit();
            $this->sys_ok("OK");
        } catch (Exception $e) {
            $message = "[Error] " . $e->getMessage();
            $code = $e->getCode();
            if ($code == 1) {
                $this->sys_error($message);
            } else {
                $this->sys_error_db($message);
            }
            exit;
        }
    }
}
