CREATE OR REPLACE FUNCTION f_order_title_update(
    IN kubun             integer,

    IN _orderno          integer,
    IN _ordermember      integer,
    IN _sendmailaddr     varchar,
    IN _sendbuyer     varchar,
    IN _sendpostalcd     varchar,
    IN _sendprefcd       integer,
    IN _sendaddr1        varchar,
    IN _sendaddr2        varchar,
    IN _sendtelno1       varchar,
    IN _addressee        varchar,
    IN _takkyubin      integer,
    IN _note               varchar,

    IN upduser           character varying,
    IN pc                character varying,

    OUT errcd            integer,
    OUT errmsg           varchar,
    OUT code             integer) AS
$$

DECLARE
    rec      RECORD;
    cntcheck integer;
    title    integer;
    in_kubun integer;
BEGIN
    --
    -- パラメータチェック
    --

    -- 処理区分
    IF kubun IS NULL OR kubun NOT IN (1,2,3) THEN
        RAISE EXCEPTION '処理区分が不正です';
    END IF;

    IF kubun = 1 THEN
        -- 受注見出し追加
        INSERT INTO t_order_title (orderdate, ordermember, sendmailaddr, sendbuyer, sendpostalcd, sendprefcd, sendaddr1, sendaddr2, sendtelno1, addressee, takkyubin, note, preorderflg, orderprocessflg, updusrnm, updpcnm)
         VALUES(CURRENT_TIMESTAMP, _ordermember, _sendmailaddr, _sendbuyer, _sendpostalcd, _sendprefcd, _sendaddr1, _sendaddr2, _sendtelno1, _addressee, _takkyubin, _note, TRUE, TRUE, upduser, pc);
        errcd := 1;
        errmsg := '完了';
        code := lastval();

    ELSIF kubun = 2 THEN
        -- 受注見出し変更
        errcd := 1;
        errmsg := '完了';

    ELSIF kubun = 3 THEN
        -- 受注見出し削除
        errcd := 1;
        errmsg := '完了';

    END IF;

    RETURN;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;

