⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m_inventory_post.sql

📁 Java写的ERP系统
💻 SQL
字号:
CREATE OR REPLACE PROCEDURE M_Inventory_Post
(
	PInstance_ID			IN NUMBER
)
/*************************************************************************
 * The contents of this file are subject to the Compiere License.  You may
 * obtain a copy of the License at    http://www.compiere.org/license.html
 * Software is on an  "AS IS" basis,  WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for details. Code: Compiere ERP+CRM
 * Copyright (C) 1999-2001 Jorg Janke, ComPiere, Inc. All Rights Reserved.
 *************************************************************************
 * $Id: M_Inventory_Post.sql,v 1.3 2003/03/14 06:11:21 jjanke Exp $
 ***
 * Title:	Physical Inventory Post
 * Description:
 *	- Update Storage with correct QtyOnHand
 *	- Generate Transcation
 ************************************************************************/
AS
	--	Logistice
	ResultStr						VARCHAR2(2000);
	Message						VARCHAR2(2000);
	Record_ID						NUMBER;
	v_Result						NUMBER := 0;	--	failure
	
	IsProcessing					CHAR(1);
	IsProcessed					CHAR(1);
	NoProcessed					NUMBER := 0;
	--	Parameter
	CURSOR Cur_Parameter (PInstance NUMBER) IS
		SELECT i.Record_ID, p.ParameterName, p.P_String, p.P_Number, p.P_Date
		FROM AD_PInstance i, AD_PInstance_Para p
		WHERE i.AD_PInstance_ID=PInstance
		AND i.AD_PInstance_ID=p.AD_PInstance_ID(+)
		ORDER BY p.SeqNo;
	--	Parameter Variables
	InvDate							DATE;
	Client_ID						NUMBER (10);

BEGIN
	--  Update AD_PInstance
	DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing');
	ResultStr := 'PInstanceNotFound';
	UPDATE AD_PInstance
	SET Created = SysDate,
		IsProcessing = 'Y'
	WHERE AD_PInstance_ID=PInstance_ID;
	COMMIT;

	--	Get Parameters
	ResultStr := 'ReadingParameters';
	FOR p IN Cur_Parameter (PInstance_ID) LOOP
		Record_ID := p.Record_ID;
	END LOOP;	--	Get Parameter
	DBMS_OUTPUT.PUT_LINE('  Record_ID=' || Record_ID);

	ResultStr := 'ReadingInventory';
	BEGIN
		SELECT	MovementDate, Processing, Processed, AD_Client_ID
		  INTO	InvDate, IsProcessing, IsProcessed, Client_ID
		FROM	M_Inventory
		WHERE	M_Inventory_ID = Record_ID
		FOR UPDATE;
	EXCEPTION WHEN OTHERS THEN
		Message := '@SaveErrorRowNotFound@';
		GOTO END_PROCESSING;
	END;

	IF (IsProcessing = 'Y') THEN
		Message := '@OtherProcessActive@';
		GOTO END_PROCESS;
	END IF;
	IF (IsProcessed = 'Y') THEN
		Message := '@AlreadyPosted@';
		GOTO END_PROCESSING;
	END IF;

	--	Check for products in multiple lines
	ResultStr := 'CheckMultipleProducts';
	UPDATE	M_InventoryLine l
	  SET	IsActive='N'
	WHERE	M_Inventory_ID = Record_ID
	  AND	(M_Product_ID, M_Locator_ID) IN 
				(SELECT M_Product_ID, M_Locator_ID FROM M_InventoryLine ll
				WHERE l.M_Inventory_ID=ll.M_Inventory_ID
				GROUP BY M_Product_ID, M_Locator_ID HAVING COUNT(*) > 1); 
	IF (SQL%ROWCOUNT <> 0) THEN
		Message := '@InventoryProductMultiple@';
		GOTO END_PROCESSING;
	END IF;

	--	Start Processing ------------------------------------------------------
	ResultStr := 'LockingInventory';
	UPDATE	M_Inventory
	  SET	Processing = 'Y',
			Processed = 'Y'
	WHERE	M_Inventory_ID = Record_ID;
	COMMIT;			

	/**
	 *	Update Storage
	 */
	-- Create missing zero qty record to update later
	INSERT INTO M_Storage
		(M_Product_ID, M_Locator_ID,
		AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
		QtyOnHand, QtyReserved, QtyOrdered)
	SELECT	l.M_Product_ID, l.M_Locator_ID,
		loc.AD_Client_ID, loc.AD_Org_ID, 'Y', SysDate, 0, SysDate, 0,
		0, 0, 0
	FROM	M_Locator loc, M_InventoryLine l
	WHERE	loc.M_Locator_ID=l.M_Locator_ID
	 AND	l.M_Inventory_ID = Record_ID 
	 AND NOT EXISTS 
			(SELECT * FROM M_Storage s 
			WHERE s.M_Product_ID=l.M_Product_ID
			  AND s.M_Locator_ID=l.M_Locator_ID);

	Message := '@Inserted@=' || SQL%ROWCOUNT;

	ResultStr := 'UpdateStorage';
	UPDATE	M_Storage s
	  SET	QtyOnHand = (SELECT QtyOnHand + QtyCount - QtyBook FROM M_InventoryLine l
					WHERE l.M_Inventory_ID = Record_ID
					  AND l.M_Product_ID = s.M_Product_ID
					  AND l.M_Locator_ID = s.M_Locator_ID
					  AND l.IsActive = 'Y'),
			DateLastInventory = InvDate,
			Updated = SysDate,
			UpdatedBy = 0
	WHERE EXISTS	(SELECT * FROM M_InventoryLine l
					WHERE l.M_Inventory_ID = Record_ID
					  AND l.M_Product_ID = s.M_Product_ID
					  AND l.M_Locator_ID = s.M_Locator_ID
					  AND l.IsActive = 'Y');

	Message := Message || ', @Updated@=' || SQL%ROWCOUNT;
	
	/**
	 *	Create Transaction
	 */
	DECLARE
		CURSOR Cur_InvLine	IS
			SELECT	*
			FROM	M_InventoryLine
			WHERE	M_Inventory_ID = Record_ID
			ORDER BY Line;
		NextNo					NUMBER;
	BEGIN
		FOR il IN Cur_InvLine LOOP
		AD_Sequence_Next('M_Transaction', Client_ID, NextNo);
		INSERT INTO M_Transaction
			(M_Transaction_ID,
			AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
			MovementType, M_Locator_ID, M_Product_ID,
			MovementDate, MovementQty,
			M_InventoryLine_ID)
		VALUES
			(NextNo,
			il.AD_Client_ID, il.AD_Org_ID, 'Y', SysDate, 0, SysDate, 0,
			'I+', il.M_Locator_ID, il.M_Product_ID,
			InvDate, il.QtyCount-il.QtyBook,
			il.M_InventoryLine_ID);
		END LOOP;
	END;
	v_Result := 1;		-- success
	
	--	End Processing --------------------------------------------------------
<<END_PROCESSING>>
	ResultStr := 'UnLockingInventory';
	UPDATE	M_Inventory
	  SET	Processing = 'N'
	WHERE	M_Inventory_ID = Record_ID;
	COMMIT;

<<END_PROCESS>>
	--  Update AD_PInstance
	DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished');
	DBMS_OUTPUT.PUT_LINE(Message);
	UPDATE	AD_PInstance
	SET Updated = SysDate,
		IsProcessing = 'N',
		Result = v_Result,					-- success
		ErrorMsg = Message
	WHERE	AD_PInstance_ID=PInstance_ID;
	COMMIT;
	RETURN;

EXCEPTION
	WHEN  OTHERS THEN
		DBMS_OUTPUT.PUT_LINE(ResultStr || ': ' || SQLERRM);
		DBMS_OUTPUT.PUT_LINE(Message);
		ResultStr := ResultStr || ':' || SUBSTR(SQLERRM,1,1978-LENGTH(ResultStr));
		--
		ROLLBACK;
		--
		UPDATE	AD_PInstance
		SET Updated = SysDate,
			IsProcessing = 'N',
			Result = 0,				-- failure
			ErrorMsg = ResultStr
		WHERE	AD_PInstance_ID=PInstance_ID;
		UPDATE	M_Inventory
		  SET	Processed = 'N',
				Processing = 'N'
		WHERE	M_Inventory_ID = Record_ID;
		--
		COMMIT;
		RETURN;

END M_Inventory_Post;
/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -