-- DROP FUNCTION adempiere.tg_sk_bom_fg_i(numeric);
CREATE OR REPLACE FUNCTION adempiere.tg_sk_bom_fg_i(p_instance numeric)
RETURNS SETOF adempiere.tg_sk_bom_fg_t AS
$BODY$
/*
原設計是單頭就是一個月份..現在改成直接抓單身..只要確認最後一次產生的單頭且區間要 >= 列印起訖
CURRENT_LANG = en_US , zh_CN , zh_TW
set search_path=adempiere
select * from adempiere.tg_sk_bom_fg_t
-- "程式完成:: p_instance=1000000 ,p_Record_ID=0 ,p_DocDate=2013-01-01 - 2013-01-31 ,p_SK_NO=TG-G016 - TG-G016"
select * from adempiere.ad_pinstance where ad_pinstance_id=1000000
select * from adempiere.ad_pinstance_para where ad_pinstance_id = 1000000
DROP table adempiere.tg_sk_bom_fg_t CASCADE
SELECT * FROM adempiere.sk_fgbom_i(1000000)
*/
DECLARE
p RECORD;
q RECORD;
r RECORD;
v_message VARCHAR (2000);
ResultStr VARCHAR (2000);
p_Record_ID NUMERIC(10) := null;
p_AD_User_ID NUMERIC(10) := null;
p_AD_Process_ID NUMERIC(10) := null;
p_AD_Client_ID NUMERIC(10) := null;
p_AD_Org_ID NUMERIC(10) := null;
p_DocDate DATE;
p_DocDateTo DATE;
p_DocumentNo VARCHAR (200); --起迄
p_DocumentNoTo VARCHAR (200);
p_DateOrdered DATE; --起迄
p_DateOrderedTo DATE;
p_M_Product_ID NUMERIC(10) := 0; --起迄
p_M_ProductTo_ID NUMERIC(10) := 0;
p_C_BPartner_ID NUMERIC(10) := 0; --起迄
p_C_BPartnerTo_ID NUMERIC(10) := 0;
p_SK_NO VARCHAR (20); --起迄
p_SK_NOTo VARCHAR (20); --起迄
p_M_Product_Value VARCHAR (20); --起迄
p_M_ProductEnd_Value VARCHAR (20);
p_C_BPartner_Value VARCHAR (20); --起迄
p_C_BPartnerEnd_Value VARCHAR (20);
p_instance_id NUMERIC(10) := 0;
v_sk_no VARCHAR (20);
BEGIN
IF p_instance is null THEN
p_instance_id:=0;
ELSE
p_instance_id:=p_instance;
END IF;
v_message :='程式開始..';
IF p_instance_id > 0 THEN
BEGIN
ResultStr := 'p_instanceNotFound';
UPDATE adempiere.ad_pinstance
SET updated = NOW(),
isprocessing = 'Y',
result = 0,
errormsg = ResultStr
WHERE ad_pinstance_id = p_instance_id;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
END IF;
FOR p IN (
SELECT i.ad_pinstance_id, i.ad_process_id,
i.record_id, i.ad_user_id,
i.ad_client_id, i.ad_org_id,
pp.seqno, pp.parametername,
pp.p_string, pp.p_string_to,
pp.p_number, pp.p_number_to,
pp.p_date, pp.p_date_to,
pp.info, pp.info_to
FROM adempiere.ad_pinstance i
INNER JOIN adempiere.ad_pinstance_para pp ON pp.ad_pinstance_id=i.ad_pinstance_id
WHERE i.ad_pinstance_id=p_instance_id
ORDER BY pp.seqno
) LOOP
v_message := '讀取傳遞參數表';
p_Record_ID :=p.record_id;
p_AD_Process_ID:=p.ad_process_id;
p_AD_User_ID :=p.ad_user_id;
p_AD_Client_ID :=p.ad_client_id;
p_AD_Org_ID :=p.ad_org_id;
IF p.parametername = 'DocDate' THEN
p_DocDate :=p.p_date;
p_DocDateTo :=p.p_date_to;
ELSE IF p.parametername = 'SK_NO' THEN
p_SK_NO :=p.p_string;
p_SK_NOTo :=p.p_string_to;
END IF;
END IF;
END LOOP;
IF COALESCE(p_instance,1000000) = 1000000 THEN --OR p_Record_ID = 0 THEN
p_DocDate := '2013.01.01'::DATE;
p_DocDateTo := date_trunc('month',p_DocDate)+'1month'::interval-'1day'::interval;
FOR r IN (
--select * from adempiere.sk_fgjh
SELECT x.vcount , x.sd_skno
FROM (
SELECT COUNT(*) AS vcount ,sd_skno
FROM adempiere.sk_bom_fg
GROUP BY sd_skno
) x
ORDER BY vcount DESC LIMIT 1
) LOOP
-- v_message := '如果沒資料抓一筆來測試';
-- p_Record_ID := r.sk_fgjh_id;
p_SK_NO := r.sd_skno;
p_SK_NOTo := r.sd_skno;
END LOOP;
-- p_DocDate := '2013.01.01'::DATE;
-- p_DocDateTo := date_trunc('month',p_DocDate)+'1month'::interval-'1day'::interval;
-- p_SK_NO := '2Z-F8050-B070-1';
-- p_SK_NOTo := '2Z-F8050-B070-1';
END IF;
IF p_DocDateTo IS NULL AND p_DocDate IS NOT NULL THEN
p_DocDateTo := p_DocDate;
END IF;
IF p_SK_NOTo IS NULL AND p_SK_NO IS NOT NULL THEN
p_SK_NOTo := p_SK_NO;
END IF;
-- 1) TABLE
-- alter table adempiere.tg_sk_bom_fg_t add docdateto date
DELETE FROM adempiere.tg_sk_bom_fg_t WHERE ad_pinstance_id = p_instance;
v_sk_no := '';
-- 2) INSERT INOT TABLE SELECT
FOR r IN (
SELECT
-- ad_pinstance_id numeric(10,0) NOT NULL,
sk_bom_fg_id, -- numeric(10,0) NOT NULL,
sk_bom_fg_uu, -- character varying(36),
sp_date, -- date,
doc_no, -- character varying(40),
sd_skno, -- character varying(40),
exp_qty, -- numeric(14,4),
bomname, -- character varying(40),
bomvalue, -- character varying(40),
documentno, -- character varying(40),
sk_bom_id, -- numeric(10,0),
bomqty, -- numeric(14,4),
linename, -- character varying(40),
linevalue, -- character varying(40),
lineqty, -- numeric(14,4),
effectivedate, -- date,
expireddate, -- date,
sk_bomalternative_id, -- numeric(10,0),
alternativename, -- character varying(40),
ad_client_id, -- numeric(10,0) NOT NULL,
ad_org_id, -- numeric(10,0) NOT NULL,
isactive, -- character(1) NOT NULL DEFAULT 'Y'::bpchar,
created, -- timestamp without time zone NOT NULL DEFAULT now(),
createdby, -- numeric(10,0) NOT NULL,
updated, -- timestamp without time zone NOT NULL DEFAULT now(),
updatedby -- numeric(10,0) NOT NULL
FROM adempiere.sk_bom_fg
WHERE 1=1
-- AND sp_date <= p_DocDate --'2013-01-01' --
-- AND sp_date >= p_DocDateTo --'2013-01-31' --
AND sd_skno BETWEEN p_SK_NO AND p_SK_NOTo -- p_SK_NO := '2Z-F8050-B070-1';
-- AND sd_skno='1D-NGF899'
ORDER BY sd_skno, sp_date desc
--p_DocDate=2013-01-01 - 2013-01-31 ,p_SK_NO=1D-NGF899 - 1D-NGF899"
)LOOP
INSERT INTO adempiere.tg_sk_bom_fg_t(
ad_pinstance_id, -- numeric(10,0) NOT NULL,
sk_bom_fg_id, -- numeric(10,0) NOT NULL,
sk_bom_fg_uu, -- character varying(36),
sp_date, -- date,
doc_no, -- character varying(40),
sd_skno, -- character varying(40),
exp_qty, -- numeric(14,4),
bomname, -- character varying(40),
bomvalue, -- character varying(40),
documentno, -- character varying(40),
sk_bom_id, -- numeric(10,0),
bomqty, -- numeric(14,4),
linename, -- character varying(40),
linevalue, -- character varying(40),
lineqty, -- numeric(14,4),
effectivedate, -- date,
expireddate, -- date,
sk_bomalternative_id, -- numeric(10,0),
alternativename, -- character varying(40),
ad_client_id, -- numeric(10,0) NOT NULL,
ad_org_id, -- numeric(10,0) NOT NULL,
isactive, -- character(1) NOT NULL DEFAULT 'Y'::bpchar,
created, -- timestamp without time zone NOT NULL DEFAULT now(),
createdby, -- numeric(10,0) NOT NULL,
updated, -- timestamp without time zone NOT NULL DEFAULT now(),
updatedby -- numeric(10,0) NOT NULL
) VALUES (
p_instance_id, -- numeric(10,0) NOT NULL,
r.sk_bom_fg_id, -- numeric(10,0) NOT NULL,
r.sk_bom_fg_uu, -- character varying(36),
r.sp_date, -- date,
r.doc_no, -- character varying(40),
r.sd_skno, -- character varying(40),
r.exp_qty, -- numeric(14,4),
r.bomname, -- character varying(40),
r.bomvalue, -- character varying(40),
r.documentno, -- character varying(40),
r.sk_bom_id, -- numeric(10,0),
r.bomqty, -- numeric(14,4),
r.linename, -- character varying(40),
r.linevalue, -- character varying(40),
r.lineqty, -- numeric(14,4),
r.effectivedate, -- date,
r.expireddate, -- date,
r.sk_bomalternative_id, -- numeric(10,0),
r.alternativename, -- character varying(40),
r.ad_client_id, -- numeric(10,0) NOT NULL,
r.ad_org_id, -- numeric(10,0) NOT NULL,
r.isactive, -- character(1) NOT NULL DEFAULT 'Y'::bpchar,
r.created, -- timestamp without time zone NOT NULL DEFAULT now(),
r.createdby, -- numeric(10,0) NOT NULL,
r.updated, -- timestamp without time zone NOT NULL DEFAULT now(),
r.updatedby -- numeric(10,0) NOT NULL
);
END LOOP;
v_message :='程式完成::'||' p_instance='||p_instance||' ,p_Record_ID='||p_Record_ID
||' ,p_DocDate='||p_DocDate||' - '||p_DocDateTo||' ,p_SK_NO='||p_SK_NO||' - '||p_SK_NOTo;
IF p_instance_id > 0 THEN
BEGIN
UPDATE adempiere.ad_pinstance
SET updated = NOW(),
isprocessing = 'N',
result = 1,
errormsg = v_message
WHERE ad_pinstance_id = p_instance_id;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
END IF;
-- 3) RETURN
RETURN QUERY SELECT *
FROM adempiere.tg_sk_bom_fg_t
WHERE ad_pinstance_id = p_instance;
--raise notice 'Yo this is good! --> % %', SQLERRM, SQLSTATE;
EXCEPTION WHEN OTHERS THEN
v_message :='程式失敗::'||SQLERRM||' ,SQLSTATE='||SQLSTATE||' ,v_message='||v_message;
IF p_instance_id > 0 THEN
BEGIN
ResultStr := v_message;
UPDATE adempiere.ad_pinstance
SET updated = NOW(),
isprocessing = 'N',
result = 0,
errormsg = ResultStr
WHERE ad_pinstance_id = p_instance_id;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION adempiere.tg_sk_bom_fg_i(numeric)
OWNER TO postgres;
沒有留言:
張貼留言