📄 m_product_trg.sql
字号:
CREATE OR REPLACE TRIGGER M_Product_Trg
AFTER INSERT OR UPDATE
ON M_Product
FOR EACH ROW
/*************************************************************************
* 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-2002 Jorg Janke, ComPiere, Inc. All Rights Reserved.
*************************************************************************
* $Id: M_Product_Trg.sql,v 1.5 2002/10/23 03:16:57 jjanke Exp $
***
* Title: Product new record
* Description:
* For Update
* - Cannot de-activate if product had active inventory (OnHand,..)
* For Insert
* - create default accounts and costing records
* - create translation row
* - create treenode
************************************************************************/
DECLARE
xTree_ID NUMBER;
xParent_ID NUMBER;
NextNo NUMBER;
ControlNo NUMBER;
CURSOR Cur_Defaults IS
SELECT *
FROM M_Product_Category_Acct d
WHERE d.M_Product_Category_ID=:new.M_Product_Category_ID;
BEGIN
-- Do not allow to de-activate products with OnHand Qty
IF (UPDATING) THEN
IF (:new.IsActive='N' AND :old.IsActive='Y') THEN
SELECT NVL(SUM(QtyOnHand)+SUM(QtyReserved)*.111+SUM(QtyOrdered)*999, 0)
INTO ControlNo
FROM M_Storage s WHERE s.M_Product_ID=:new.M_Product_ID;
IF (ControlNo <> 0) THEN
RAISE_APPLICATION_ERROR(-20400, 'Product has active Inventory');
END IF;
END IF;
RETURN;
END IF;
-- For all AcctSchema create ...
FOR cd IN Cur_Defaults LOOP
-- Account
INSERT INTO M_Product_Acct
(M_Product_ID, C_AcctSchema_ID,
AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
P_Revenue_Acct, P_Expense_Acct, P_Asset_Acct, P_COGS_Acct,
P_PurchasePriceVariance_Acct, P_InvoicePriceVariance_Acct,
P_TradeDiscountRec_Acct, P_TradeDiscountGrant_Acct)
VALUES
(:new.M_Product_ID, cd.C_AcctSchema_ID,
:new.AD_Client_ID, :new.AD_ORG_ID, 'Y', SysDate, :new.CreatedBy, SysDate, :new.UpdatedBy,
cd.P_Revenue_Acct, cd.P_Expense_Acct, cd.P_Asset_Acct, cd.P_COGS_Acct,
cd.P_PurchasePriceVariance_Acct, cd.P_InvoicePriceVariance_Acct,
cd.P_TradeDiscountRec_Acct, cd.P_TradeDiscountGrant_Acct);
-- Costing
INSERT INTO M_Product_Costing
(M_Product_ID, C_AcctSchema_ID,
AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
CurrentCostPrice, CostStandard, FutureCostPrice,
CostStandardPOQty,CostStandardPOAmt,CostStandardCumQty,CostStandardCumAmt,
CostAverage, CostAverageCumQty, CostAverageCumAmt,
PriceLastPO, PriceLastInv,
TotalInvQty, TotalInvAmt)
VALUES
(:new.M_Product_ID, cd.C_AcctSchema_ID,
:new.AD_Client_ID, :new.AD_ORG_ID, 'Y', SysDate, :new.CreatedBy, SysDate, :new.UpdatedBy,
0,0,0, 0,0,0,0, 0,0,0, 0,0, 0,0);
END LOOP;
-- Create Translation Row
INSERT INTO M_Product_Trl
(M_Product_ID, AD_Language, AD_Client_ID, AD_Org_ID,
IsActive, Created, CreatedBy, Updated, UpdatedBy,
Name, DocumentNote, IsTranslated)
SELECT :new.M_Product_ID, AD_Language, :new.AD_Client_ID, :new.AD_Org_ID,
:new.IsActive, :new.Created, :new.CreatedBy, :new.Updated, :new.UpdatedBy,
:new.Name, :new.DocumentNote, 'N'
FROM AD_Language
WHERE IsActive = 'Y' AND IsSystemLanguage = 'Y'
AND EXISTS (SELECT * FROM AD_Client
WHERE AD_Client_ID=:new.AD_Client_ID AND IsMultiLingualDocument='Y');
-- Create TreeNode --
-- get AD_Tree_ID + ParentID
SELECT c.AD_Tree_Product_ID, n.Node_ID INTO xTree_ID, xParent_ID
FROM AD_ClientInfo c, AD_TreeNodePR n
WHERE c.AD_Tree_Product_ID=n.AD_Tree_ID
AND n.Parent_ID IS NULL
AND c.AD_Client_ID=:new.AD_Client_ID;
-- DBMS_OUTPUT.PUT_LINE('Tree='||xTree_ID||' Node='||:new.M_Product_ID||' Parent='||xParent_ID);
-- Insert into TreeNode
INSERT INTO AD_TreeNodePR
(AD_Client_ID, AD_Org_ID,
IsActive, Created, CreatedBy, Updated, UpdatedBy,
AD_Tree_ID, Node_ID,
Parent_ID, SeqNo)
VALUES
(:new.AD_Client_ID, :new.AD_Org_ID,
:new.IsActive, :new.Created, :new.CreatedBy, :new.Updated, :new.UpdatedBy,
xTree_ID, :new.M_Product_ID,
xParent_ID, DECODE(:new.IsSummary, 'Y', 100, 999)); -- Summary Nodes first
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20001, 'M_Product InsertTrigger Error: No ClientInfo or parent TreeNode');
END M_Product_Trg;
/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -