-- 仮受注テーブル 追加

CREATE OR REPLACE FUNCTION f_order_detail_update(
    IN kubun             integer,

    IN _orderno          integer,
    IN _orderrowno       integer,
    IN _typediv          integer,
    IN _itemcd           integer,
    IN _structcd         integer,
    IN _stockcd          integer,
    IN _price            integer,
    IN _count            integer,

    IN upduser           character varying,
    IN pc                character varying,

    OUT errcd            integer,
    OUT errmsg           varchar) AS
$$
DECLARE
    rec      RECORD;
    cntcheck integer;
    --kubun    integer;
    title    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_detail (orderno, typediv, itemcd, structcd, stockcd, price, count, preorderflg, updusrnm, updpcnm)
         VALUES(_orderno, _typediv, _itemcd, _structcd, _stockcd, _price, _count, TRUE, upduser, pc);
        errcd := 1;
        errmsg := '完了';

    ELSIF kubun = 2 THEN
        -- 受注明細変更
        errcd := 1;
        errmsg := '完了';

    ELSIF kubun = 3 THEN
        -- 受注明細削除
        errcd := 1;
        errmsg := '完了';

    END IF;

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

