/**
 *	Cleanup Data
 */


--	Copy Data from PriceListVersion_Para to DiscountSchema
DECLARE
	CURSOR Cur_PLV IS
		SELECT DISTINCT M_PriceList_Version_ID FROM M_PriceList_Para;
	v_Name				VARCHAR(60);
	v_CLIENT_ID			NUMBER(10);
	v_ORG_ID			NUMBER(10);
	v_VALIDFROM			DATE;
	v_DiscountSchema_ID	NUMBER(10);
BEGIN
	FOR plv IN Cur_PLV LOOP
		SELECT	Name, AD_CLIENT_ID, AD_ORG_ID, VALIDFROM
		  INTO	v_Name, v_CLIENT_ID, v_ORG_ID, v_VALIDFROM
	   	FROM 	M_PriceList_Version
		WHERE	M_PriceList_Version_ID=plv.M_PriceList_Version_ID;
		--
		DBMS_OUTPUT.PUT_LINE(v_Name);
		AD_Sequence_Next('M_DiscountSchema', v_Client_ID, v_DiscountSchema_ID);
		INSERT INTO M_DISCOUNTSCHEMA
			(M_DISCOUNTSCHEMA_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
			CREATED, CREATEDBY, UPDATED, UPDATEDBY,
			NAME, DESCRIPTION, VALIDFROM,
			DISCOUNTTYPE, SCRIPT, FLATDISCOUNT, 
			ISQUANTITYBASED, CUMULATIVELEVEL, PROCESSING)
		VALUES
			(v_DISCOUNTSCHEMA_ID, v_CLIENT_ID, v_ORG_ID, 'Y',
			SysDate, 0, SysDate, 0,
			v_NAME, NULL, v_VALIDFROM,
			'P', NULL, NULL, 
			'Y', NULL, NULL);
		--
		DECLARE
			CURSOR Cur_Para IS
				SELECT	*
				FROM 	M_PriceList_Para
				WHERE	M_PriceList_Version_ID=plv.M_PriceList_Version_ID;
			v_NextNo			NUMBER(10);
		BEGIN
			FOR p in CUR_Para LOOP
				AD_Sequence_Next('M_DiscountSchemaLine', v_Client_ID, v_NextNo);
				INSERT INTO M_DISCOUNTSCHEMALINE
					(M_DISCOUNTSCHEMALINE_ID, AD_CLIENT_ID, AD_ORG_ID,
					ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
					M_DISCOUNTSCHEMA_ID, SEQNO,
					M_PRODUCT_CATEGORY_ID, C_BPARTNER_ID, M_PRODUCT_ID,
					CONVERSIONRATETYPE, CONVERSIONDATE,
					LIST_BASE, LIST_ADDAMT, LIST_DISCOUNT, LIST_ROUNDING, 
					LIST_MINAMT, LIST_MAXAMT,
					STD_BASE, STD_ADDAMT, STD_DISCOUNT, STD_ROUNDING,
					STD_MINAMT, STD_MAXAMT, LIMIT_BASE, LIMIT_ADDAMT, 
					LIMIT_DISCOUNT, LIMIT_ROUNDING, LIMIT_MINAMT, LIMIT_MAXAMT)
			 	VALUES
					(v_NextNo, p.AD_CLIENT_ID, p.AD_ORG_ID,
					p.ISACTIVE, p.CREATED, p.CREATEDBY, p.UPDATED, p.UPDATEDBY,
					v_DISCOUNTSCHEMA_ID, p.SEQNO,
					p.M_PRODUCT_CATEGORY_ID, p.C_BPARTNER_ID, p.M_PRODUCT_ID,
					p.CONVERSIONRATETYPE, p.CONVERSIONDATE,
					p.LIST_BASE, p.LIST_ADDAMT, p.LIST_DISCOUNT, p.LIST_ROUNDING, 
					p.LIST_MINAMT, p.LIST_MAXAMT,
					p.STD_BASE, p.STD_ADDAMT, p.STD_DISCOUNT, p.STD_ROUNDING,
					p.STD_MINAMT, p.STD_MAXAMT, p.LIMIT_BASE, p.LIMIT_ADDAMT, 
					p.LIMIT_DISCOUNT, p.LIMIT_ROUNDING, p.LIMIT_MINAMT, p.LIMIT_MAXAMT);
			END LOOP;
	 	END;
	END LOOP;	--	PLV
END;

