
CREATE OR REPLACE FUNCTION f_import_ex_stock_detail(
 IN itemcode integer,
 IN structcode integer,

 IN _exhibitcount integer,
 IN _condition integer,
 IN _sellingprice integer,
 IN _souryouprc integer,
 IN _stockstate integer,
 IN usr character varying,

 OUT errcd integer, OUT errmsg character varying) AS
$BODY$

DECLARE
    item integer;
    struct integer;

    exhibit integer;
    selling integer;
    souryou integer;
BEGIN
    IF usr IS NULL THEN
        errmsg := '更新ユーザが指定されていません。';
        errcd := -10;
        RETURN;
    END IF;

    SELECT itemcd INTO item FROM t_item_basic_attr WHERE itemcd = itemcode;
    IF item IS NULL THEN
        errcd := -10;
        errmsg := '指定された品目コードは品目基本属性にありません';
        RETURN;
    END IF;

    SELECT structcd INTO struct FROM t_item_struct WHERE itemcd = itemcode AND structcd = structcode;
    IF struct IS NULL THEN
        errcd := -10;
        errmsg := '指定された構成コードは品目構成にありません';
        RETURN;
    END IF;

    -- チェック
    IF _condition IS NULL THEN
        errcd := -10;
        errmsg := 'コンディションが不正です';
        RETURN;
    END IF;
    IF _stockstate IS NULL THEN
        errcd := -10;
        errmsg := '在庫状態が不正です';
        RETURN;
    END IF;

    IF _exhibitcount IS NULL THEN
        exhibit := 0;
    ELSE
        exhibit := _exhibitcount;
    END IF;

    IF _sellingprice IS NULL THEN
        selling := 0;
    ELSE
        selling := _sellingprice;
    END IF;

    IF _souryouprc IS NULL THEN
        souryou := 0;
    ELSE
        souryou := _souryouprc;
    END IF;


    INSERT INTO t_ex_stock_detail(itemcd, structcd, exhibitcount, condition, sellingprice, souryouprc, stockstate, updusrnm)
        values(itemcode, structcode, exhibit, _condition, selling, souryou, _stockstate, usr);

    errcd := 1;
END;

$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

