2005.11.02_02/UPGRADE_DETAIL
1. 纠正: [先进先出]中入库数量处理的错误。
--
2005.11.02_02/SQL1
UPDATE DomainDefine SET EMask = '!0000.99.99;1;' WHERE Upper(DomainName) = 'TLISTDATE'
---
ALTER PROCEDURE LIST_SET_FIFO_INPRICE
(
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(15),
  P_MAININTNO INTEGER
)
AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  IF (NOT (p_AddFlag = 1)) THEN                       /* 先进先出时, 不允许取消进出仓 */
    EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC;

  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE SALEGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5

      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE PANGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE ASSGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)

          INTO :m_InPrice;
        UPDATE BUYGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'USEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM USEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE USEGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'STKINGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM STKINGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo  /* 此处 m_Qty 前面应加负号 */
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE STKINGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END
END
--
2005.11.02_01/UPGRADE_DETAIL
1. 修改: 时段促销支持分组定义;
--
2005.11.02_01/SQL1
ALTER TABLE SpecList ADD GroupNoList VARCHAR(200) DEFAULT ''
---
UPDATE SpecList SET GroupNoList = ''
---
ALTER TABLE Client ADD GroupNo VARCHAR(20) DEFAULT ''
---
UPDATE Client SET GroupNo = ''
---
DELETE FROM FieldDefine WHERE ( UPPER(FieldName) = UPPER('GroupNo') )
  AND ( UPPER(TableName) = UPPER('Client') )
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('GroupNo', 'Client', '分组号', '6')
---
DELETE FROM FieldDefine WHERE ( UPPER(FieldName) = UPPER('GroupNoList') )
  AND ( UPPER(TableName) = UPPER('SpecList') )
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('GroupNoList', 'SpecList', '分组列表', '40')
--
2005.11.02_01/SQL2
DECLARE EXTERNAL FUNCTION zs_CheckATList
   CSTRING(20), CSTRING(256), CHAR
   RETURNS INTEGER BY VALUE
  ENTRY_POINT "zsudf_CheckATList"  MODULE_NAME "zsudf"
---
Create PROCEDURE GetSpecListGoodsIntNoEx
(
  P_GoodsIntNo Integer,
  P_Date TimeStamp,
  P_Time TimeStamp,
  P_CmpyName Varchar(12)
)
RETURNS
(
  R_IntNo INTEGER
)
AS
DECLARE VARIABLE m_GroupNo varchar(20);
DECLARE VARIABLE m_Separator Char(1);
BEGIN
  SELECT ALLTRIM(GroupNo) FROM Client
    WHERE (CmpyName = :P_CmpyName) AND (ClientFlag = 0) INTO :m_GroupNo;

  m_Separator = ';';
  R_IntNo = null;
  FOR
    SELECT d.IntNo FROM SpecList m, SpecGds d
      WHERE (m.UpdateAcc = '*')
        AND (m.DateTimeTo+1 > :P_Date) AND (m.DateTimeFrom <= :P_Date)
        AND (m.DailyTimeTo >= :P_Time) AND (m.DailyTimeFrom <= :P_Time)
        AND (m.IntNo = d.MainIntNo) AND (d.GoodsIntNo = :P_GoodsIntNo)
        AND ( (m.GroupNoList = '') OR (ZS_CheckATList(:m_GroupNo, m.GroupNoList, :m_Separator) = 1) )
        ORDER BY m.ListDate Desc, m.ListNo DESC
      INTO :R_IntNo
    DO
    BEGIN
      SUSPEND;
      EXIT; /* 只取最新的一条记录 */
    END
END
--
2005.10.19_01/UPGRADE_DETAIL
1. 增加: 单据的锁定日期;
--
2005.10.19_01/SQL1
CREATE GENERATOR LOCK_IN
---
SET GENERATOR LOCK_IN TO 0
---
CREATE GENERATOR LOCK_OUT
---
SET GENERATOR LOCK_OUT TO 0
---
CREATE GENERATOR LOCK_MOVE
---
SET GENERATOR LOCK_MOVE TO 0
---
CREATE GENERATOR LOCK_ASS
---
SET GENERATOR LOCK_ASS TO 0
---
CREATE GENERATOR LOCK_STKINLIST
---
SET GENERATOR LOCK_STKINLIST TO 0
---
CREATE GENERATOR LOCK_USELIST
---
SET GENERATOR LOCK_USELIST TO 0
---
CREATE GENERATOR LOCK_PAN
---
SET GENERATOR LOCK_PAN TO 0
---
CREATE GENERATOR LOCK_REAL
---
SET GENERATOR LOCK_REAL TO 0
---
CREATE GENERATOR LOCK_INBK
---
SET GENERATOR LOCK_INBK TO 0
---
CREATE GENERATOR LOCK_OUTBK
---
SET GENERATOR LOCK_OUTBK TO 0
---
CREATE GENERATOR LOCK_BANKMOVE
---
SET GENERATOR LOCK_BANKMOVE TO 0
---
CREATE GENERATOR LOCK_BANKS
---
SET GENERATOR LOCK_BANKS TO 0
---
CREATE GENERATOR LOCK_INTRANS
---
SET GENERATOR LOCK_INTRANS TO 0
---
CREATE GENERATOR LOCK_OUTTRANS
---
SET GENERATOR LOCK_OUTTRANS TO 0
---
CREATE GENERATOR LOCK_DOUT
---
SET GENERATOR LOCK_DOUT TO 0
---
CREATE GENERATOR LOCK_BAO
---
SET GENERATOR LOCK_BAO TO 0
---
CREATE GENERATOR LOCK_PRESETLIST
---
SET GENERATOR LOCK_PRESETLIST TO 0
---
CREATE GENERATOR LOCK_PRESENDLIST
---
SET GENERATOR LOCK_PRESENDLIST TO 0
---
CREATE GENERATOR LOCK_SPECLIST
---
SET GENERATOR LOCK_SPECLIST TO 0
---
CREATE GENERATOR LOCK_CPLIST
---
SET GENERATOR LOCK_CPLIST TO 0
--
2005.10.19_01/SQL2
DECLARE EXTERNAL FUNCTION ZS_TimeStamp2UnixTime
   TimeStamp
   RETURNS INTEGER BY VALUE
  ENTRY_POINT "zsudf_TimeStamp2UnixTime"  MODULE_NAME "zsudf"
---
CREATE EXCEPTION Msg_Lock_In_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_In_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_In_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Out_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Out_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Out_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Move_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Move_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Move_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Ass_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Ass_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Ass_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_StkInList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_StkInList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_StkInList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_UseList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_UseList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_UseList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Pan_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Pan_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Pan_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Real_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Real_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Real_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_InBK_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_InBK_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_InBk_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_OutBk_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_OutBk_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_OutBk_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_BankMove_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_BankMove_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_BankMove_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Banks_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Banks_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Banks_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_InTrans_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_InTrans_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_InTrans_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_OutTrans_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_OutTrans_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_OutTrans_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_DOut_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_DOut_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_DOut_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_Bao_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_Bao_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_Bao_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_PreSetList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_PreSetList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_PreSetList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_PreSendList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_PreSendList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_PreSendList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_SpecList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_SpecList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_SpecList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE EXCEPTION Msg_Lock_CPList_CanNot_Ins "帐务锁定日及之前的单据不能被添加"
---
CREATE EXCEPTION Msg_Lock_CPList_CanNot_Del "帐务锁定日及之前的单据不能被删除"
---
CREATE EXCEPTION Msg_Lock_CPList_CanNot_Upd "帐务锁定日及之前的单据不能被修改"
---
CREATE PROCEDURE Check_Lock_ByDate_Int(
P_ListDate TimeStamp,
P_ListFlag SmallInt,
P_CheckType SmallInt
)
AS
DECLARE VARIABLE m_ListDate_Int Integer;
DECLARE VARIABLE m_LockDate_Int Integer;
BEGIN
  m_ListDate_Int = ZS_TimeStamp2UnixTime(:P_ListDate);
  m_LockDate_Int = 0;

  IF (:P_ListFlag = 0)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_IN, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_In_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_In_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_In_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 1)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_OUT, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Out_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Out_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Out_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 2)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_MOVE, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Move_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Move_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Move_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 3)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_ASS, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Ass_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Ass_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Ass_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 4)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_STKINLIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_StkInList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_StkInList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_StkInList_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 5)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_USELIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_UseList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_UseList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_UseList_CanNot_Upd;
    END
    EXIT;
  END

  IF (:P_ListFlag = 6)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_PAN, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Pan_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Pan_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Pan_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 7)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_REAL, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Real_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Real_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Real_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 8)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_INBK, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_InBk_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_InBk_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_InBk_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 9)  THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_OUTBK, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_OutBk_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_OutBk_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_OutBk_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 10) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_BANKMOVE, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_BankMove_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_BankMove_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_BankMove_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 11) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_BANKS, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Banks_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Banks_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Banks_CanNot_Upd;
    END
    EXIT;
  END

  IF (:P_ListFlag = 12) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_INTRANS, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_InTrans_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_InTrans_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_InTrans_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 13) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_OUTTRANS, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_OutTrans_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_OutTrans_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_OutTrans_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 14) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_DOUT, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_DOut_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_DOut_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_DOut_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 15) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_BAO, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_Bao_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_Bao_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_Bao_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 16) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_PRESETLIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_PreSetList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_PreSetList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_PreSetList_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 17) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_PRESENDLIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_PreSendList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_PreSendList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_PreSendList_CanNot_Upd;
    END
    EXIT;
  END

  IF (:P_ListFlag = 18) THEN
  BEGIN
    m_LockDate_Int = Gen_ID(LOCK_SPECLIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_SpecList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_SpecList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_SpecList_CanNot_Upd;
    END
    EXIT;
  END
  IF (:P_ListFlag = 19) THEN
  BEGIN
   m_LockDate_Int = Gen_ID(LOCK_CPLIST, 0);
    IF (:m_LockDate_Int >= :m_ListDate_Int) THEN
    BEGIN
      IF (P_CheckType = 0) THEN EXCEPTION Msg_Lock_CPList_CanNot_Ins;
      IF (P_CheckType = 1) THEN EXCEPTION Msg_Lock_CPList_CanNot_Del;
      IF (P_CheckType = 2) THEN EXCEPTION Msg_Lock_CPList_CanNot_Upd;
    END
    EXIT;
  END
END
---
/*0*/
CREATE TRIGGER BUYGDS_BEF_INS_LOCK FOR BUYGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 0);
  END
END
---
CREATE TRIGGER BUYGDS_BEF_DEL_LOCK FOR BUYGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 1);
  END
END
---
CREATE TRIGGER BUYGDS_BEF_UPD_LOCK FOR BUYGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 2);
  END
END
---
CREATE TRIGGER INPAY_BEF_INS_LOCK FOR INPAY BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 0);
  END
END
---
CREATE TRIGGER INPAY_BEF_DEL_LOCK FOR INPAY BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 1);
  END
END
---
CREATE TRIGGER INPAY_BEF_UPD_LOCK FOR INPAY BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 0, 2);
  END
END
---
CREATE TRIGGER INLIST_BEF_INS_LOCK FOR INLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 0, 0);
END
---
CREATE TRIGGER INLIST_BEF_DEL_LOCK FOR INLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 0, 1);
END
---
CREATE TRIGGER INLIST_BEF_UPD_LOCK FOR INLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 0, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 0, 2);
END
---
/*1*/
CREATE TRIGGER SALEGDS_BEF_INS_LOCK FOR SALEGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 0);
  END
END
---
CREATE TRIGGER SALEGDS_BEF_DEL_LOCK FOR SALEGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 1);
  END
END
---
CREATE TRIGGER SALEGDS_BEF_UPD_LOCK FOR SALEGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 2);
  END
END
---
CREATE TRIGGER OUTPAY_BEF_INS_LOCK FOR OUTPAY BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 0);
  END
END
---
CREATE TRIGGER OUTPAY_BEF_DEL_LOCK FOR OUTPAY BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 1);
  END
END
---
CREATE TRIGGER OUTPAY_BEF_UPD_LOCK FOR OUTPAY BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 1, 2);
  END
END
---
CREATE TRIGGER OUTLIST_BEF_INS_LOCK FOR OUTLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 1, 0);
END
---
CREATE TRIGGER OUTLIST_BEF_DEL_LOCK FOR OUTLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 1, 1);
END
---
CREATE TRIGGER OUTLIST_BEF_UPD_LOCK FOR OUTLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 1, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 1, 2);
END
---
/*2*/
CREATE TRIGGER MOVEGDS_BEF_INS_LOCK FOR MOVEGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM MoveList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 2, 0);
  END
END
---
CREATE TRIGGER MOVEGDS_BEF_DEL_LOCK FOR MOVEGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM MoveList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 2, 1);
  END
END
---
CREATE TRIGGER MOVEGDS_BEF_UPD_LOCK FOR MOVEGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM MoveList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 2, 2);
  END
END
---
CREATE TRIGGER MOVELIST_BEF_INS_LOCK FOR MOVELIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 2, 0);
END
---
CREATE TRIGGER MOVELIST_BEF_DEL_LOCK FOR MOVELIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 2, 1);
END
---
CREATE TRIGGER MOVELIST_BEF_UPD_LOCK FOR MOVELIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 2, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 2, 2);
END
---
/*3*/
CREATE TRIGGER ASSGDS_BEF_INS_LOCK FOR ASSGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM AssList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 3, 0);
  END
END
---
CREATE TRIGGER ASSGDS_BEF_DEL_LOCK FOR ASSGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM AssList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 3, 1);
  END
END
---
CREATE TRIGGER ASSGDS_BEF_UPD_LOCK FOR ASSGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM AssList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 3, 2);
  END
END
---
CREATE TRIGGER ASSLIST_BEF_INS_LOCK FOR ASSLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 3, 0);
END
---
CREATE TRIGGER ASSLIST_BEF_DEL_LOCK FOR ASSLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 3, 1);
END
---
CREATE TRIGGER ASSLIST_BEF_UPD_LOCK FOR ASSLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 3, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 3, 2);
END
---
/*4*/
CREATE TRIGGER STKINGDS_BEF_INS_LOCK FOR STKINGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM StkInList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 4, 0);
  END
END
---
CREATE TRIGGER STKINGDS_BEF_DEL_LOCK FOR STKINGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM StkInList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 4, 1);
  END
END
---
CREATE TRIGGER STKINGDS_BEF_UPD_LOCK FOR STKINGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM StkInList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 4, 2);
  END
END
---
CREATE TRIGGER STKINLIST_BEF_INS_LOCK FOR STKINLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 4, 0);
END
---
CREATE TRIGGER STKINLIST_BEF_DEL_LOCK FOR STKINLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 4, 1);
END
---
CREATE TRIGGER STKINLIST_BEF_UPD_LOCK FOR STKINLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 4, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 4, 2);
END
---
/*5*/
CREATE TRIGGER USEGDS_BEF_INS_LOCK FOR USEGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM UseList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 5, 0);
  END
END
---
CREATE TRIGGER USEGDS_BEF_DEL_LOCK FOR USEGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM UseList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 5, 1);
  END
END
---
CREATE TRIGGER USEGDS_BEF_UPD_LOCK FOR USEGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM UseList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 5, 2);
  END
END
---
CREATE TRIGGER USELIST_BEF_INS_LOCK FOR USELIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 5, 0);
END
---
CREATE TRIGGER USELIST_BEF_DEL_LOCK FOR USELIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 5, 1);
END
---
CREATE TRIGGER USELIST_BEF_UPD_LOCK FOR USELIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 5, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 5, 2);
END
---
/*6*/
CREATE TRIGGER PANGDS_BEF_INS_LOCK FOR PANGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PanList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 6, 0);
  END
END
---
CREATE TRIGGER PANGDS_BEF_DEL_LOCK FOR PANGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM PanList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 6, 1);
  END
END
---
CREATE TRIGGER PANGDS_BEF_UPD_LOCK FOR PANGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PanList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 6, 2);
  END
END
---
CREATE TRIGGER PANLIST_BEF_INS_LOCK FOR PANLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 6, 0);
END
---
CREATE TRIGGER PANLIST_BEF_DEL_LOCK FOR PANLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 6, 1);
END
---
CREATE TRIGGER PANLIST_BEF_UPD_LOCK FOR PANLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 6, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 6, 2);
END
---
/*7*/
CREATE TRIGGER REALGDS_BEF_INS_LOCK FOR REALGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM RealList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 7, 0);
  END
END
---
CREATE TRIGGER REALGDS_BEF_DEL_LOCK FOR REALGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM RealList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 7, 1);
  END
END
---
CREATE TRIGGER REALGDS_BEF_UPD_LOCK FOR REALGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM RealList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 7, 2);
  END
END
---
CREATE TRIGGER REALLIST_BEF_INS_LOCK FOR REALLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 7, 0);
END
---
CREATE TRIGGER REALLIST_BEF_DEL_LOCK FOR REALLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 7, 1);
END
---
CREATE TRIGGER REALLIST_BEF_UPD_LOCK FOR REALLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 7, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 7, 2);
END
---
/*8*/
CREATE TRIGGER BUYGDSBK_BEF_INS_LOCK FOR BUYGDSBK BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 8, 0);
  END
END
---
CREATE TRIGGER BUYGDSBK_BEF_DEL_LOCK FOR BUYGDSBK BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM InListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 8, 1);
  END
END
---
CREATE TRIGGER BUYGDSBK_BEF_UPD_LOCK FOR BUYGDSBK BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 8, 2);
  END
END
---
CREATE TRIGGER INLISTBK_BEF_INS_LOCK FOR INLISTBK BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 8, 0);
END
---
CREATE TRIGGER INLISTBK_BEF_DEL_LOCK FOR INLISTBK BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 8, 1);
END
---
CREATE TRIGGER INLISTBK_BEF_UPD_LOCK FOR INLISTBK BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 8, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 8, 2);
END
---
/*9*/
CREATE TRIGGER SALEGDSBK_BEF_INS_LOCK FOR SALEGDSBK BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 9, 0);
  END
END
---
CREATE TRIGGER SALEGDSBK_BEF_DEL_LOCK FOR SALEGDSBK BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM OutListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 9, 1);
  END
END
---
CREATE TRIGGER SALEGDSBK_BEF_UPD_LOCK FOR SALEGDSBK BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutListBk WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 9, 2);
  END
END
---
CREATE TRIGGER OUTLISTBK_BEF_INS_LOCK FOR OUTLISTBK BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 9, 0);
END
---
CREATE TRIGGER OUTLISTBK_BEF_DEL_LOCK FOR OUTLISTBK BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 9, 1);
END
---
CREATE TRIGGER OUTLISTBK_BEF_UPD_LOCK FOR OUTLISTBK BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 9, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 9, 2);
END
---
/*10*/
CREATE TRIGGER BANKMOVE_BEF_INS_LOCK FOR BANKMOVE BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 10, 0);
END
---
CREATE TRIGGER BANKMOVE_BEF_DEL_LOCK FOR BANKMOVE BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 10, 1);
END
---
CREATE TRIGGER BANKMOVE_BEF_UPD_LOCK FOR BANKMOVE BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 10, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 10, 2);
END
---
/*11*/
CREATE TRIGGER BANKS_BEF_INS_LOCK FOR BANKS BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 11, 0);
END
---
CREATE TRIGGER BANKS_BEF_DEL_LOCK FOR BANKS BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 11, 1);
END
---
CREATE TRIGGER BANKS_BEF_UPD_LOCK FOR BANKS BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 11, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 11, 2);
END
---
/*12*/
CREATE TRIGGER BUYGDST_BEF_INS_LOCK FOR BUYGDST BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 12, 0);
  END
END
---
CREATE TRIGGER BUYGDST_BEF_DEL_LOCK FOR BUYGDST BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM InListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 12, 1);
  END
END
---
CREATE TRIGGER BUYGDST_BEF_UPD_LOCK FOR BUYGDST BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM InListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 12, 2);
  END
END
---
CREATE TRIGGER INLISTT_BEF_INS_LOCK FOR INLISTT BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 12, 0);
END
---
CREATE TRIGGER INLISTT_BEF_DEL_LOCK FOR INLISTT BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 12, 1);
END
---
CREATE TRIGGER INLISTT_BEF_UPD_LOCK FOR INLISTT BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 12, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 12, 2);
END
---
/*13*/
CREATE TRIGGER SALEGDST_BEF_INS_LOCK FOR SALEGDST BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 13, 0);
  END
END
---
CREATE TRIGGER SALEGDST_BEF_DEL_LOCK FOR SALEGDST BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM OutListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 13, 1);
  END
END
---
CREATE TRIGGER SALEGDST_BEF_UPD_LOCK FOR SALEGDST BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM OutListT WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 13, 2);
  END
END
---
CREATE TRIGGER OUTLISTT_BEF_INS_LOCK FOR OUTLISTT BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 13, 0);
END
---
CREATE TRIGGER OUTLISTT_BEF_DEL_LOCK FOR OUTLISTT BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 13, 1);
END
---
CREATE TRIGGER OUTLISTT_BEF_UPD_LOCK FOR OUTLISTT BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 13, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 13, 2);
END
---
/*14*/
CREATE TRIGGER DSALEGDS_BEF_INS_LOCK FOR DSALEGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM DOutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 14, 0);
  END
END
---
CREATE TRIGGER DSALEGDS_BEF_DEL_LOCK FOR DSALEGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM DOutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 14, 1);
  END
END
---
CREATE TRIGGER DSALEGDS_BEF_UPD_LOCK FOR DSALEGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM DOutList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 14, 2);
  END
END
---
CREATE TRIGGER DOUTLIST_BEF_INS_LOCK FOR DOUTLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 14, 0);
END
---
CREATE TRIGGER DOUTLIST_BEF_DEL_LOCK FOR DOUTLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 14, 1);
END
---
CREATE TRIGGER DOUTLIST_BEF_UPD_LOCK FOR DOUTLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 14, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 14, 2);
END
---
/*15*/
CREATE TRIGGER BAOGDS_BEF_INS_LOCK FOR BAOGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM BaoList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 15, 0);
  END
END
---
CREATE TRIGGER BAOGDS_BEF_DEL_LOCK FOR BAOGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM BaoList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 15, 1);
  END
END
---
CREATE TRIGGER BAOGDS_BEF_UPD_LOCK FOR BAOGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM BaoList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 15, 2);
  END
END
---
CREATE TRIGGER BAOLIST_BEF_INS_LOCK FOR BAOLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 15, 0);
END
---
CREATE TRIGGER BAOLIST_BEF_DEL_LOCK FOR BAOLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 15, 1);
END
---
CREATE TRIGGER BAOLIST_BEF_UPD_LOCK FOR BAOLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 15, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 15, 2);
END
---
/*16*/
CREATE TRIGGER PRESETGDS_BEF_INS_LOCK FOR PRESETGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PreSetList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 16, 0);
  END
END
---
CREATE TRIGGER PRESETGDS_BEF_DEL_LOCK FOR PRESETGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM PreSetList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 16, 1);
  END
END
---
CREATE TRIGGER PRESETGDS_BEF_UPD_LOCK FOR PRESETGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PreSetList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 16, 2);
  END
END
---
CREATE TRIGGER PRESETLIST_BEF_INS_LOCK FOR PRESETLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 16, 0);
END
---
CREATE TRIGGER PRESETLIST_BEF_DEL_LOCK FOR PRESETLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 16, 1);
END
---
CREATE TRIGGER PRESETLIST_BEF_UPD_LOCK FOR PRESETLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 16, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 16, 2);
END
---
/*17*/
CREATE TRIGGER PRESENDGDS_BEF_INS_LOCK FOR PRESENDGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PreSendList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 17, 0);
  END
END
---
CREATE TRIGGER PRESENDGDS_BEF_DEL_LOCK FOR PRESENDGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM PreSendList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 17, 1);
  END
END
---
CREATE TRIGGER PRESENDGDS_BEF_UPD_LOCK FOR PRESENDGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM PreSendList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 17, 2);
  END
END
---
CREATE TRIGGER PRESENDLIST_BEF_INS_LOCK FOR PRESENDLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 17, 0);
END
---
CREATE TRIGGER PRESENDLIST_BEF_DEL_LOCK FOR PRESENDLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 17, 1);
END
---
CREATE TRIGGER PRESENDLIST_BEF_UPD_LOCK FOR PRESENDLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 17, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 17, 2);
END
---
/*18*/
CREATE TRIGGER SPECGDS_BEF_INS_LOCK FOR SPECGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM SpecList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 18, 0);
  END
END
---
CREATE TRIGGER SPECGDS_BEF_DEL_LOCK FOR SPECGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM SpecList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 18, 1);
  END
END
---
CREATE TRIGGER SPECGDS_BEF_UPD_LOCK FOR SPECGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM SpecList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 18, 2);
  END
END
---
CREATE TRIGGER SPECLIST_BEF_INS_LOCK FOR SPECLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 18, 0);
END
---
CREATE TRIGGER SPECLIST_BEF_DEL_LOCK FOR SPECLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 18, 1);
END
---
CREATE TRIGGER SPECLIST_BEF_UPD_LOCK FOR SPECLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 18, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 18, 2);
END
---
/*19*/
CREATE TRIGGER CPGDS_BEF_INS_LOCK FOR CPGDS BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM CPList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 19, 0);
  END
END
---
CREATE TRIGGER CPGDS_BEF_DEL_LOCK FOR CPGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = Old.MainIntNo;
  FOR SELECT ListDate FROM CPList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 19, 1);
  END
END
---
CREATE TRIGGER CPGDS_BEF_UPD_LOCK FOR CPGDS BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  m_IntNo = New.MainIntNo;
  FOR SELECT ListDate FROM CPList WHERE (IntNo = :m_IntNo) INTO :m_ListDate DO
  BEGIN
    EXECUTE PROCEDURE Check_Lock_ByDate_Int(:m_ListDate, 19, 2);
  END
END
---
CREATE TRIGGER CPLIST_BEF_INS_LOCK FOR CPLIST BEFORE INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 19, 0);
END
---
CREATE TRIGGER CPLIST_BEF_DEL_LOCK FOR CPLIST BEFORE DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 19, 1);
END
---
CREATE TRIGGER CPLIST_BEF_UPD_LOCK FOR CPLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(Old.ListDate, 19, 2);
  EXECUTE PROCEDURE Check_Lock_ByDate_Int(New.ListDate, 19, 2);
END
--
2005.09.30_01/UPGRADE_DETAIL
1. 刷新: 库存警示存量字段的显示;
--
2005.09.30_01/SQL1
DELETE FROM FieldDefine WHERE (UPPER(TableName) = '')
  AND (UPPER(FieldName) = UPPER('QRemainMin'))
---
DELETE FROM FieldDefine WHERE (UPPER(TableName) = '')
  AND (UPPER(FieldName) = UPPER('QRemainMax'))
---
DELETE FROM FieldDefine WHERE (UPPER(TableName) = '')
  AND (UPPER(FieldName) = UPPER('QtySafety'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMin', '', '最低存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMax', '', '最高存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QtySafety', '', '安全存量', '8', 'TQty')
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainIn'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainIn', '', '调入仓现存量', '8', 'TQty')
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainMinIn'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainMaxIn'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QtySafetyIn'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMinIn', '', '调入仓最低存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMaxIn', '', '调入仓最高存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QtySafetyIn', '', '调入仓安全存量', '8', 'TQty')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSMININ')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSSAFETYIN')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QSAFETYMININ')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QMOREMAXIN')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLessMinIn', '调入仓最低应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLessSafetyIn', '调入仓安全应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QSafetyMinIn', '调入仓合理应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QMoreMaxIn', '调入仓超出数量', 'TQty')
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainOut'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainOut', '', '调出仓现存量', '8', 'TQty')
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainMinOut'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QRemainMaxOut'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('QtySafetyOut'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMinOut', '', '调出仓最低存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMaxOut', '', '调出仓最高存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QtySafetyOut', '', '调出仓安全存量', '8', 'TQty')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSMINOUT')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSSAFETYOUT')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QSAFETYMINOUT')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QMOREMAXOUT')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLessMinOut', '调出仓最低应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLessSafetyOut', '调出仓安全应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QSafetyMinOut', '调出仓合理应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QMoreMaxOut', '调出仓超出数量', 'TQty')
--
2005.09.15_01/UPGRADE_DETAIL
1. 新增: 房台号表;
2. 修改: 销售主表, 增加字段[开台时间][结束时间]。
--
2005.09.15_01/SQL1
ALTER TABLE SaleGds ADD SaleTime TimeStamp
---
ALTER TABLE OutList ADD OpenTime TimeStamp,
  ADD CloseTime TimeStamp
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PeopleCount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PeopleCount', '', '容量', '8')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('UsePeopleCount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('UsePeopleCount', '', '消费人数', '8')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('TableNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('TableNo', '', '房台号', 'TCmpyNo')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('TableName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('TableName', '', '房台名称', 'TComment')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('SortOrder')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('SortOrder', '', '排序', 'TComment')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('Status')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, Dwidth)
  VALUES ('Status', '', '房台状态', '4')
---
/*第四步， 新增时的内号号生成子*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2005.09.15_01/SQL2
/*第五步，处理生成子*/
CREATE GENERATOR INTNO_reTableList
---
SET GENERATOR INTNO_reTableList TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINLIST')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOLST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTPAY')           THEN BEGIN r_NewIntNo = Gen_ID(IntNo_OutPay, 10); SUSPEND; EXIT; END
    IF (P_TableName='INPAY')            THEN BEGIN r_NewIntNo = Gen_ID(IntNo_InPay, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESETGDS')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESETGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESETLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESETLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESENDGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESENDGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESENDLIST')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESENDLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='RETABLELIST')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_reTableList, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2005.09.15_01/SQL3
CREATE TABLE reTableList (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,

  TableNo TCmpyNo,
  TableName TComment,
  PeopleCount TComment,
  UsePeopleCount TComment,
  SortOrder TComment,
  Remark TRemark,
  LRemark2 VarChar(200),
  TypeIntNo Integer,
  DepIntNo Integer,
  UseCount Integer,
  TotalUseCount Integer,
  Amount TAmount,
  TodayAmount TAmount,
  TotalAmount TAmount,
  OutListIntNo Integer,
  Status Integer,
  MasterIntNo Integer,

  OPERNO TOPERNO,
  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,

  CONSTRAINT reTableList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_reTableList_OutList FOREIGN KEY (OutListIntNo) REFERENCES OutList (INTNO)
    ON UPDATE NO ACTION ON DELETE SET NULL
)
---
CREATE INDEX IDX_ReTableList_TableName ON reTableList(TableName);
---
CREATE UNIQUE INDEX IDX_ReTableList_TableNo ON reTableList(TableNo);
--
2005.09.15_01/SQL4
/*房台表 触发器， 用于初始化默认值*/
CREATE TRIGGER reTableList_BEF_INS_SET_DEF FOR reTableList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.TableNo is NULL) THEN new.TableNo = '';
  IF (new.TableName is NULL) THEN new.TableName = '';
  IF (new.PeopleCount is NULL) THEN new.PeopleCount = '';
  IF (new.UsePeopleCount is NULL) THEN new.UsePeopleCount = '';
  IF (new.SortOrder is NULL) THEN new.SortOrder = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';

  IF (new.TypeIntNo is NULL) THEN new.TypeIntNo = 0;
  IF (new.DepIntNo is NULL) THEN new.DepIntNo = 0;
  IF (new.UseCount is NULL) THEN new.UseCount = 0;
  IF (new.TotalUseCount is NULL) THEN new.TotalUseCount = 0;
  IF (new.Amount is NULL) THEN new.Amount = 0.0;
  IF (new.TodayAmount is NULL) THEN new.TodayAmount = 0.0;
  IF (new.TotalAmount is NULL) THEN new.TotalAmount = 0.0;

  IF (new.Status is NULL) THEN new.Status = 0;
  IF (new.MasterIntNo is NULL) THEN new.MasterIntNo = 0;
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
CREATE TRIGGER reTableList_BEF_UPD_SET_DEF FOR reTableList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.TableNo is NULL) THEN new.TableNo = '';
  IF (new.TableName is NULL) THEN new.TableName = '';
  IF (new.PeopleCount is NULL) THEN new.PeopleCount = '';
  IF (new.UsePeopleCount is NULL) THEN new.UsePeopleCount = '';
  IF (new.SortOrder is NULL) THEN new.SortOrder = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';

  IF (new.TypeIntNo is NULL) THEN new.TypeIntNo = 0;
  IF (new.DepIntNo is NULL) THEN new.DepIntNo = 0;
  IF (new.UseCount is NULL) THEN new.UseCount = 0;
  IF (new.TotalUseCount is NULL) THEN new.TotalUseCount = 0;
  IF (new.Amount is NULL) THEN new.Amount = 0.0;
  IF (new.TodayAmount is NULL) THEN new.TodayAmount = 0.0;
  IF (new.TotalAmount is NULL) THEN new.TotalAmount = 0.0;

  IF (new.Status is NULL) THEN new.Status = 0;
  IF (new.MasterIntNo is NULL) THEN new.MasterIntNo = 0;
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;

  IF (new.OutListIntNo = -1) THEN new.OutListIntNo = null;
END
---
/*赠品设置属于基本资料需要同步  三个主表同步用的触发*/
CREATE TRIGGER reTableList_BEF_INS_EVENTLOG FOR reTableList BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER reTableList_BEF_UPD_EVENTLOG FOR reTableList BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER reTableList_AFT_DEL_EVENTLOG FOR reTableList AFTER DELETE POSITION 1
AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('reTableList', OLD.INTNO);
END
--
2005.09.10_01/UPGRADE_DETAIL
1. 新增: 赠品设置主/明细表;
2. 新增: 赠品领用主/明细表;
3. 新增: 单位资料表字段[领用积分];
4. 新增: 出库主表字段[赠品领用单号];
--
2005.09.10_01/SQL1
/*修改单位资料 增加领用积分*/
ALTER TABLE Client ADD PointUsed NUMERIC(15, 4) DEFAULT 0 NOT NULL
---
/*修改出库表 增加赠品单号*/
ALTER TABLE UseList ADD PreSendListNo TListNo
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('TotalPoint')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('TotalPoint', '', '总积分', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('RemainPoint')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('RemainPoint', '', '剩余积分', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FieldName) = UPPER('GdsPay'))
  AND (UPPER(TABLENAME) = UPPER('PreSendList'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('GdsPay', 'PreSendList', '货值', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PreSendListNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PreSendListNo', '', '赠品领用单号', 'TListNo')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PointUsed')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PointUsed', '', '领用积分', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PrePoint')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PrePoint', '', '积分', 'TPrice')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PrePoint1')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PrePoint1', '', '积分2', 'TPrice')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FieldName) = UPPER('PrePoint2')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PrePoint2', '', '积分3', 'TPrice')
---
/*第一步 检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc VARChar(1);
BEGIN
  SUSPEND;
END
---
/*第二步， 检查单号是否存在*/
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  SUSPEND;
END
---
/*第三步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  SUSPEND;
END
---
/*第四步， 生成子*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2005.09.10_01/SQL2
/*增加换购积分 设置默认值的2个触发器*/
CREATE TRIGGER CLIENT_BEF_INS_SET_DEF2 FOR CLIENT BEFORE INSERT POSITION 1 AS
BEGIN
  IF (new.PointUsed is NULL) THEN new.PointUsed = 0.0;
END
---
CREATE TRIGGER CLIENT_BEF_UPD_SET_DEF2 FOR CLIENT BEFORE UPDATE POSITION 1 AS
BEGIN
  IF (new.PointUsed is NULL) THEN new.PointUsed = 0.0;
END
---
/*第五步，处理生成子*/
CREATE GENERATOR INTNO_PRESETGDS
---
SET GENERATOR INTNO_PRESETGDS TO 10
---
CREATE GENERATOR INTNO_PRESETLIST
---
SET GENERATOR INTNO_PRESETLIST TO 10
---
CREATE GENERATOR INTNO_PRESENDGDS
---
SET GENERATOR INTNO_PRESENDGDS TO 10
---
CREATE GENERATOR INTNO_PRESENDLIST
---
SET GENERATOR INTNO_PRESENDLIST TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINLIST')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOLST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTPAY')           THEN BEGIN r_NewIntNo = Gen_ID(IntNo_OutPay, 10); SUSPEND; EXIT; END
    IF (P_TableName='INPAY')            THEN BEGIN r_NewIntNo = Gen_ID(IntNo_InPay, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESETGDS')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESETGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESETLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESETLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESENDGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESENDGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRESENDLIST')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRESENDLIST, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2005.09.10_01/SQL3
/*建立赠品设置主表*/
CREATE TABLE PRESETLIST (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,

  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO   TLISTNO,
  COMMENT TCOMMENT,
  DATETIMEFROM DATE NOT NULL,
  DATETIMETO DATE NOT NULL,
  DAILYTIMEFROM DATE NOT NULL,
  DAILYTIMETO DATE NOT NULL,

  OPERNO TOPERNO,
  REMARK TREMARK,
  LREMARK2 VARCHAR(120),
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  CONTRACTNO CHAR(20),
  INVOICENO CHAR(20),

  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,
  CONSTRAINT PRESETLIST_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT PRESETLIST_UNIQUE_1 UNIQUE (LISTNO)
)
---
/*建立赠品主表索引, 日期及备注*/
CREATE INDEX IDX_PRESETLIST_LISTDATE ON PRESETLIST(LISTDATE);
---
CREATE INDEX IDX_PRESETLIST_COMMENT ON PRESETLIST(COMMENT);
---
/*建立赠品领用主表*/
CREATE TABLE PRESENDLIST (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,

  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  CMPYNAME TCMPYNAME,

  GDSPAY TAMOUNT,
  INAMOUNT TAMOUNT,
  PointUsed TAMOUNT,

  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  LREMARK2 VARCHAR(120),
  CONTRACTNO CHAR(20),
  INVOICENO CHAR(20),
  GOODSGDSPAY NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,
  CONSTRAINT PRESENDLIST_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT PRESENDLIST_UNIQUE_1 UNIQUE (LISTNO)
)
---
/*建立赠品领用主表索引, 名称及日期*/
CREATE INDEX IDX_PRESENDLIST_LISTDATE ON PRESENDLIST(LISTDATE);
---
CREATE INDEX IDX_PRESENDLIST_CMPYNAME ON PRESENDLIST(CMPYNAME);
--
2005.09.10_01/SQL4
/*建赠品设置明细表*/
CREATE TABLE PRESETGDS
(
  INTNO	TINTNO,
  MAININTNO	TINTNO,
  GOODSINTNO	TGOODSINTNO,

  STOCKINTNO TINTNO,
  PREPOINT TAMOUNT,
  PREPOINT1 TAMOUNT,
  PREPOINT2 TAMOUNT,

  REMARK TREMARK,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  QUALITYSTATUS VARCHAR(20),

  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,

  CONSTRAINT PRESETGDS_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_PRESETGDS_LIST FOREIGN KEY (MAININTNO) REFERENCES PRESETLIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_PRESETGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT FK_PRESETGDS_STKNAME FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE
)
---
CREATE ASCENDING INDEX IDX_PRESETGDS_MAININTNO ON PRESETGDS (MAININTNO)
---
/*建赠品领用明细表*/
CREATE TABLE PRESENDGDS
(
  INTNO	TINTNO,
  MAININTNO	TINTNO,
  GOODSINTNO	TGOODSINTNO,
  STOCKINTNO TINTNO,

  UNITID TGOODSUNITID,
  QTY TQTY,
  PRICE TPRICE,
  DISCOUNT TDISCOUNT,
  AMOUNT TAMOUNT,
  INPRICE TPRICE,
  INAMOUNT TAMOUNT,

  PREPOINT TAMOUNT,

  REMARK TREMARK,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  QUALITYSTATUS VARCHAR(20),

  TAXRATE NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  GOODSAMOUNT NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  TAXPAYMENT NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,

  CONSTRAINT PRESENDGDS_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_PRESENDGDS_LIST FOREIGN KEY (MAININTNO) REFERENCES PRESENDLIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_PRESENDGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT FK_PRESENDGDS_STKNAME FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE
)
---
CREATE ASCENDING INDEX IDX_PRESENDGDS_MAININTNO ON PRESENDGDS (MAININTNO)
--
2005.09.10_01/SQL5
/*赠品设置主表的触发部分, 设置备注*/
CREATE TRIGGER PRESETLIST_BEF_INS_SET_DEF FOR PRESETLIST BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.FLAG1 is NULL) THEN new.FLAG1 = 0;
  IF (new.FLAG2 is NULL) THEN new.FLAG2 = 0;
  IF (new.FLAG3 is NULL) THEN new.FLAG3 = 0;
  IF (new.FLAG4 is NULL) THEN new.FLAG4 = 0;
  IF (new.PRINTCOUNT is NULL) THEN new.PRINTCOUNT = 0;

  IF (new.COMMENT is NULL) THEN new.COMMENT = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.CONTRACTNO is NULL) THEN new.CONTRACTNO = '';
  IF (new.INVOICENO is NULL) THEN new.INVOICENO = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
CREATE TRIGGER PRESETLIST_BEF_UPD_SET_DEF FOR PRESETLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.FLAG1 is NULL) THEN new.FLAG1 = 0;
  IF (new.FLAG2 is NULL) THEN new.FLAG2 = 0;
  IF (new.FLAG3 is NULL) THEN new.FLAG3 = 0;
  IF (new.FLAG4 is NULL) THEN new.FLAG4 = 0;
  IF (new.PRINTCOUNT is NULL) THEN new.PRINTCOUNT = 0;

  IF (new.COMMENT is NULL) THEN new.COMMENT = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.CONTRACTNO is NULL) THEN new.CONTRACTNO = '';
  IF (new.INVOICENO is NULL) THEN new.INVOICENO = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
/*赠品设置主表的触发部分 删除主表时删除明细表*/
CREATE TRIGGER PRESETLIST_BEF_DEL_DEL_DETAIL FOR PRESETLIST BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM PRESETGDS WHERE MAININTNO = OLD.INTNO;
END
---
/*赠品设置属于基本资料需要同步  三个主表同步用的触发*/
CREATE TRIGGER PRESETLIST_BEF_INS_EVENTLOG FOR PRESETLIST BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PRESETLIST_BEF_UPD_EVENTLOG FOR PRESETLIST BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PRESETLIST_AFT_DEL_EVENTLOG FOR PRESETLIST AFTER DELETE POSITION 1
AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('PreSetList', OLD.INTNO);
END
---
/*赠品设置主表删除时, 如果单据已生效, 则不允许删除(除非正在做帐务清除)*/
CREATE TRIGGER PRESETLIST_BEF_DEL_CHK_UPACC FOR PRESETLIST BEFORE DELETE POSITION 2
AS
BEGIN
  IF ((OLD.UPDATEACC = '*') AND (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---
/*赠品设置 主表修改时检查是不同时修改日期及生效标志*/
CREATE TRIGGER PRESETLIST_BEF_UPD_CHK_UPACC FOR PRESETLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF ((old.UPDATEACC <> new.UpdateAcc) and (old.LISTDATE <> new.LISTDATE)) THEN
    EXCEPTION CANNOT_CHANGE_BOTH_ACC_LISTDATE;
END
---
/*赠品设置 插入时检查单号是否已经存在*/
CREATE TRIGGER PRESETLIST_BEF_INS_CHK_LISTNO FOR PRESETLIST BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (29, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
/*赠品设置明细表的触发部分, 设置备注*/
CREATE TRIGGER PRESETGDS_BEF_INS_SET_DEF FOR PRESETGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.PREPOINT is NULL) THEN new.PREPOINT = 0.0;
  IF (new.PREPOINT1 is NULL) THEN new.PREPOINT1 = 0.0;
  IF (new.PREPOINT2 is NULL) THEN new.PREPOINT2 = 0.0;

  IF (new.QUALITYSTATUS is NULL) THEN new.QUALITYSTATUS = '';
  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
CREATE TRIGGER PRESETGDS_BEF_UPD_SET_DEF FOR PRESETGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.PREPOINT is NULL) THEN new.PREPOINT = 0.0;
  IF (new.PREPOINT1 is NULL) THEN new.PREPOINT1 = 0.0;
  IF (new.PREPOINT2 is NULL) THEN new.PREPOINT2 = 0.0;

  IF (new.QUALITYSTATUS is NULL) THEN new.QUALITYSTATUS = '';
  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
/*赠品设置明细表的3个同步触发*/
CREATE TRIGGER PRESETGDS_AFT_DEL_EVENTLOG FOR PRESETGDS AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('PRESETGDS', old.IntNo);
END
---
CREATE TRIGGER PRESETGDS_BEF_INS_EVENTLOG FOR PRESETGDS BEFORE INSERT POSITION 10 AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PRESETGDS_BEF_UPD_EVENTLOG FOR PRESETGDS BEFORE UPDATE POSITION 10 AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
/*赠品设置明细表 货号不允许空也不允许重复*/
CREATE EXCEPTION MSG_PRESETGDS_GoodsNo_Not_Null "赠品设置明细中货号不能为空"
---
CREATE EXCEPTION MSG_PRESETGDS_GoodsNo_DIFF "赠品设置明细中货号不能重复"
---
CREATE PROCEDURE CHECK_PRESETGDS_GOODSNO_DIFF(
p_MainIntNo Integer,
p_GoodsIntNo Integer
)AS
DECLARE VARIABLE m_RecCount Integer;
BEGIN
  m_RecCount = 0;
  FOR
    SELECT COUNT(*) FROM PreSetGds
      WHERE (MainIntNo = :p_MainIntNo AND GoodsIntNo = :p_GoodsIntNO) INTO :m_RecCount
  DO
    BEGIN
      IF (m_RecCount > 0) THEN EXCEPTION MSG_PRESETGDS_GoodsNo_DIFF;
    END
END
---
CREATE TRIGGER PRESETGDS_BEF_UPD_1 FOR PRESETGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.GoodsIntNo is null) then EXCEPTION MSG_PRESETGDS_GoodsNo_Not_Null;
    IF (new.GoodsIntNo <> old.GoodsIntNo) THEN
      EXECUTE PROCEDURE CHECK_PRESETGDS_GOODSNO_DIFF(new.MainIntNo, new.GoodsIntNo);
  END
END
---
CREATE TRIGGER PRESETGDS_BEF_INS_1 FOR PRESETGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.GoodsIntNo is null) then EXCEPTION MSG_PRESETGDS_GoodsNo_Not_Null;
  EXECUTE PROCEDURE CHECK_PRESETGDS_GOODSNO_DIFF(new.MainIntNo, new.GoodsIntNo);
END
---
/*赠品设置明细表 存储过程, 用于取得某赠品的信息**/
CREATE PROCEDURE GETPRESETGDS_INFO (
  P_GOODSINTNO INTEGER,
  P_DATE DATE,
  P_TIME DATE
) RETURNS (
  R_StockIntNo INTEGER,
  R_PrePoint NUMERIC(15, 4),
  R_PrePoint1 NUMERIC(15, 4),
  R_PrePoint2 NUMERIC(15, 4)
) AS
BEGIN
  R_STOCKINTNO = 0;
  FOR
    SELECT d.StockIntNo, d.PrePoint, d.PrePoint1, d.PrePoint2
      FROM PreSetList m, PreSetGds d
      WHERE (m.UpdateAcc = '*')
        AND (m.DateTimeTo+1 > :P_Date) AND (m.DateTimeFrom <= :P_Date)
        AND (m.DailyTimeTo >= :P_Time) AND (m.DailyTimeFrom <= :P_Time)
        AND (m.IntNo = d.MainIntNo) AND (d.GoodsIntNo = :P_GoodsIntNo)
        ORDER BY m.ListDate Desc, m.ListNo DESC
      INTO :R_StockIntNo, :R_PrePoint, :R_PrePoint1, :R_PrePoint2
    DO
    BEGIN
      SUSPEND;
      EXIT; /* 只取最新的一条记录 */
    END
END
---
/*赠品领用主表的触发部分, 设置备注*/
CREATE TRIGGER PRESENDLIST_BEF_INS_SET_DEF FOR PRESENDLIST BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.FLAG1 is NULL) THEN new.FLAG1 = 0;
  IF (new.FLAG2 is NULL) THEN new.FLAG2 = 0;
  IF (new.FLAG3 is NULL) THEN new.FLAG3 = 0;
  IF (new.FLAG4 is NULL) THEN new.FLAG4 = 0;
  IF (new.PRINTCOUNT is NULL) THEN new.PRINTCOUNT = 0;
  IF (new.CONTRACTNO is NULL) THEN new.CONTRACTNO = '';
  IF (new.INVOICENO is NULL) THEN new.INVOICENO = '';

  IF (new.PointUsed is NULL) THEN new.PointUsed = 0.0;
  IF (new.GDSPAY is NULL) THEN new.GDSPAY = 0.0;
  IF (new.INAMOUNT is NULL) THEN new.INAMOUNT = 0.0;
  IF (new.GOODSGDSPAY is NULL) THEN new.GOODSGDSPAY = 0.0;

  IF (new.CMPYNAME is NULL) THEN new.CMPYNAME = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
CREATE TRIGGER PRESENDLIST_BEF_UPD_SET_DEF FOR PRESENDLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.FLAG1 is NULL) THEN new.FLAG1 = 0;
  IF (new.FLAG2 is NULL) THEN new.FLAG2 = 0;
  IF (new.FLAG3 is NULL) THEN new.FLAG3 = 0;
  IF (new.FLAG4 is NULL) THEN new.FLAG4 = 0;
  IF (new.PRINTCOUNT is NULL) THEN new.PRINTCOUNT = 0;
  IF (new.CONTRACTNO is NULL) THEN new.CONTRACTNO = '';
  IF (new.INVOICENO is NULL) THEN new.INVOICENO = '';

  IF (new.PointUsed is NULL) THEN new.PointUsed = 0.0;
  IF (new.GDSPAY is NULL) THEN new.GDSPAY = 0.0;
  IF (new.INAMOUNT is NULL) THEN new.INAMOUNT = 0.0;
  IF (new.GOODSGDSPAY is NULL) THEN new.GOODSGDSPAY = 0.0;

  IF (new.CMPYNAME is NULL) THEN new.CMPYNAME = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';

  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
/*赠品领用主表的触发部分 删除主表时删除明细表*/
CREATE TRIGGER PRESENDLIST_BEF_DEL_DEL_DETAIL FOR PRESENDLIST BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM PRESENDGDS WHERE MAININTNO = OLD.INTNO;
END
---
/*赠品领用主表删除时, 如果单据已生效, 则不允许删除(除非正在做帐务清除)*/
CREATE TRIGGER PRESENDLIST_BEF_DEL_CHK_UPACC FOR PRESENDLIST BEFORE DELETE POSITION 2
AS
BEGIN
  IF ((OLD.UPDATEACC = '*') AND (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---
/*赠品领用 主表修改时检查是不同时修改日期及生效标志*/
CREATE TRIGGER PRESENDLIST_BEF_UPD_CHK_UPACC FOR PRESENDLIST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF ((old.UPDATEACC <> new.UpdateAcc) and (old.LISTDATE <> new.LISTDATE)) THEN
    EXCEPTION CANNOT_CHANGE_BOTH_ACC_LISTDATE;
END
---
/*赠品领用 插入时检查单号是否已经存在*/
CREATE TRIGGER PRESENDLIST_BEF_INS_CHK_LISTNO FOR PRESENDLIST BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (30, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
/*赠品领用 插入时检查生效状态*/
CREATE TRIGGER PRESENDLIST_BEF_INS_UPDATEACC FOR PRESENDLIST BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc = '*') THEN EXCEPTION CANNOT_SET_UPDATEACC_WHILE_NEW;
END
---
/*赠品领用 生效时检查货号是否已经结清， 因为生效时会生成出库单影响库存量*/
CREATE TRIGGER PRESENDLIST_BEF_UPD_UPDATEACC FOR PRESENDLIST BEFORE UPDATE POSITION 2 AS
DECLARE VARIABLE m_Changeable SMALLINT;
BEGIN /*做帐务清除时不处理*/
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1) AND (new.UPDATEACC <> old.UPDATEACC)) THEN
    EXECUTE PROCEDURE LIST_CHECK_STK_CHANGEABLE ('PRESENDLIST', new.IntNo, old.ListDate, 1)
      RETURNING_VALUES :m_Changeable;
END
---
/*赠品领用 单位名称为空时不允许生效*/
CREATE EXCEPTION MSG_PRESEND_CmpyName_Not_Null "赠品领用中单位名称不能为空!"
---
/*赠品领用 单位名称为空时不允许生效*/
CREATE TRIGGER PRESENDLIST_BEF_UPD_CMPYNAME FOR PRESENDLIST BEFORE UPDATE POSITION 3 AS
BEGIN
  IF (((OLD.UpdateAcc = '') OR (NEW.UpdateAcc IS NULL))
    AND (NEW.UpdateAcc = '*') AND ((NEW.CmpyName = '') OR (NEW.CmpyName IS NULL)))THEN
    EXCEPTION MSG_PRESEND_CmpyName_Not_Null;
END
---
/*赠品领用 当客户的剩余积分不能小于0*/
CREATE EXCEPTION Msg_PreSend_RemainPoint_Zero "赠品领用生效时, 客户的乘余积分不能小于0!"
---
CREATE PROCEDURE CHECK_CMPY_REMAINPOINT(
  P_CmpyName VARCHAR(12),
  P_PointUsed NUMERIC(15, 4)
) RETURNS (
  R_BUPDATE SMALLINT,
  R_REMAINPOINT NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_PrePoint NUMERIC(15, 4);
DECLARE VARIABLE m_TotalAmount NUMERIC(15, 4);
DECLARE VARIABLE m_AmountFactor NUMERIC(15, 4);
DECLARE VARIABLE m_PointUsed NUMERIC(15, 4);
DECLARE VARIABLE m_RemainPoint NUMERIC(15, 4);
BEGIN
  r_bUpdate = 1;
  P_PointUsed = Round(:P_PointUsed, 4);
  /*本单增加的积分*/
  IF (:P_PointUsed > 0) THEN
  FOR SELECT c.PrevPoint, c.TotalAmount, c.AmountFactor, c.PointUsed
    FROM Client c WHERE (c.ClientFlag = 0) AND (c.CmpyName = :P_CmpyName)
  INTO :m_PrePoint, :m_TotalAmount, :m_AmountFactor, :m_PointUsed DO
  BEGIN
    IF ((:m_AmountFactor = 0) OR (:m_AmountFactor IS NULL)) THEN m_AmountFactor = 1;

    m_RemainPoint = :m_PrePoint + :m_TotalAmount / :m_AmountFactor
      - :m_PointUsed - :P_PointUsed;
    m_RemainPoint = Round(:m_RemainPoint, 4);
    r_RemainPoint = :m_RemainPoint;
    IF (:m_RemainPoint < 0) THEN r_bUpdate = 0;
  END
  SUSPEND;
END
---
/*排在上一个异常触发之后*/
CREATE TRIGGER PRESENDLIST_BEF_UPD_REMAINPOINT FOR PRESENDLIST BEFORE UPDATE POSITION 4 AS
DECLARE VARIABLE m_bUpdate SMALLINT;
DECLARE VARIABLE m_RemainPoint NUMERIC(15, 4);
BEGIN
  /*生效时做检查, 取消生效时不做检查*/
  IF (((OLD.UpdateAcc = '') OR (OLD.UpdateAcc IS NULL)) AND (NEW.UpdateAcc = '*'))THEN
  BEGIN
    m_bUpdate = 1;
    EXECUTE PROCEDURE CHECK_CMPY_REMAINPOINT(new.CmpyName, new.PointUsed)
      RETURNING_VALUES :m_bUpdate, :m_RemainPoint;
    IF (:m_bUpdate = 0) THEN EXCEPTION Msg_PreSend_RemainPoint_Zero;
  END
END
---
/*赠品领用 生效及取消生效时更新客户资料中的积分 存储过程-1*/
CREATE PROCEDURE UPDATE_CLIENT_PointUsed (
  P_CMPYNAME VARCHAR(12),
  P_CLIENTFLAG SMALLINT,
  P_PointUsed NUMERIC(15, 4)
)  AS
BEGIN
  IF (p_CmpyName = '') THEN EXIT;
  Update CLIENT SET
    PointUsed = PointUsed + :P_PointUsed
    WHERE (CmpyName = :p_CmpyName) and (ClientFlag = :p_ClientFlag);
END
---
/*赠品领用 生效及取消生效时更新单位资料中的积分 触发器-1*/
CREATE TRIGGER PRESENDLIST_AFT_UPD_PointUsed FOR PRESENDLIST AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_PointUsed NUMERIC(15, 4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    /*生效时增加积分*/
    IF ((NEW.UpdateAcc = '*') AND (OLD.UpdateAcc = '')) THEN
    BEGIN
      m_PointUsed = NEW.PointUsed;
      EXECUTE PROCEDURE UPDATE_CLIENT_PointUsed(NEW.CmpyName, 0, :m_PointUsed);
    END

    /*取消生效时减少积分*/
    IF ((NEW.UpdateAcc = '') AND (OLD.UpdateAcc = '*')) THEN
    BEGIN
      m_PointUsed = -(OLD.PointUsed);
      EXECUTE PROCEDURE UPDATE_CLIENT_PointUsed(OLD.CmpyName, 0, :m_PointUsed);
    END
  END
END
---
/*赠品领用明细表 更新主表的积分 存储过程*/
CREATE PROCEDURE UPDATE_PRESENDLIST_AMOUNT
(
  P_GdsPay NUMERIC(15, 4),
  P_InAmount NUMERIC(15, 4),
  P_PrePoint NUMERIC(15, 4),
  P_GoodsGdsPay NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)
AS
BEGIN
  UPDATE PRESENDLIST SET GdsPay = GdsPay + (:P_GdsPay*:Direct),
    InAmount = InAmount + (:P_InAmount*:Direct),
    PointUsed = PointUsed + (:P_PrePoint*:Direct),
    GoodsGdsPay = GoodsGdsPay + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
/*赠品领用明细表 更新主表的积分 触发器共3-1个*/
CREATE TRIGGER PRESENDGDS_AFT_DEL_SET_MASTER FOR PRESENDGDS AFTER DELETE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_PRESENDLIST_AMOUNT(OLD.Amount, OLD.InAmount,
      OLD.PrePoint, OLD.GoodsAmount, -1, old.MAININTNO);
  END
END
---
/*赠品领用明细表 更新主表的积分 触发器共3-2个*/
CREATE TRIGGER PRESENDGDS_AFT_INS_SET_MASTER FOR PRESENDGDS AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_PRESENDLIST_AMOUNT(NEW.Amount, NEW.InAmount,
    NEW.PrePoint, NEW.GoodsAmount, 1, NEW.MAININTNO);
END
---
/*赠品领用明细表 更新主表的积分 触发器共3-3个*/
CREATE TRIGGER PRESENDGDS_AFT_UPD_SET_MASTER FOR PRESENDGDS AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_Amount NUMERIC(15,4);
DECLARE VARIABLE m_InAmount NUMERIC(15,4);
DECLARE VARIABLE m_PrePoint NUMERIC(15,4);
DECLARE VARIABLE m_GoodsAmount NUMERIC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_Amount = NEW.Amount - OLD.Amount;
    m_InAmount = NEW.InAmount - OLD.InAmount;
    m_PrePoint = NEW.PrePoint - OLD.PrePoint;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;

    EXECUTE PROCEDURE UPDATE_PRESENDLIST_AMOUNT(:m_Amount, :m_InAmount,
      :m_PrePoint, :m_GoodsAmount, 1, OLD.MAININTNO);
  END
END
---
/*赠品领用明细表的触发部分, 插入设置备注*/
CREATE TRIGGER PRESENDGDS_BEF_INS_SET_DEF FOR PRESENDGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.PREPOINT is NULL) THEN new.PREPOINT = 0.0;
  IF (new.TAXRATE is NULL) THEN new.TAXRATE = 0.0;
  IF (new.GOODSAMOUNT is NULL) THEN new.GOODSAMOUNT = 0.0;
  IF (new.TAXPAYMENT is NULL) THEN new.TAXPAYMENT = 0.0;

  IF (new.QUALITYSTATUS is NULL) THEN new.QUALITYSTATUS = '';
  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
/*赠品领用明细表的触发部分, 更新设置备注*/
CREATE TRIGGER PRESENDGDS_BEF_UPD_SET_DEF FOR PRESENDGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.PREPOINT is NULL) THEN new.PREPOINT = 0.0;
  IF (new.TAXRATE is NULL) THEN new.TAXRATE = 0.0;
  IF (new.GOODSAMOUNT is NULL) THEN new.GOODSAMOUNT = 0.0;
  IF (new.TAXPAYMENT is NULL) THEN new.TAXPAYMENT = 0.0;

  IF (new.QUALITYSTATUS is NULL) THEN new.QUALITYSTATUS = '';
  IF (new.UPDATEUCT is NULL) THEN new.UPDATEUCT = 0;
  IF (new.INSERTUCT is NULL) THEN new.INSERTUCT = 0;
END
---
/*赠品领用明细表 主表已经生效明细表不允许被修改 共3个*/
CREATE TRIGGER PRESENDGDS_BEF_DEL_CHK_UPACC FOR PRESENDGDS BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('PRESENDLIST', old.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
CREATE TRIGGER PRESENDGDS_BEF_INS_CHK_UPACC FOR PRESENDGDS BEFORE INSERT POSITION 1 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('PRESENDLIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
CREATE TRIGGER PRESENDGDS_BEF_UPD_CHK_UPACC FOR PRESENDGDS BEFORE UPDATE POSITION 0 AS DECLARE
VARIABLE m_Changeable SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (NOT ((new.GoodsIntNo is NULL) AND (not (old.GoodsIntNo is NULL))
      AND (new.PrePoint=old.PrePoint) AND (new.Qty=old.Qty)
      AND (new.UnitId=old.UnitId)))
      THEN
        EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('PRESENDLIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
  END
END
--
2005.09.10_01/SQL6
/*第一步 检查主表是否已经出仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc Char(1);
BEGIN
  r_Changeable = 1; IF (GEN_ID(INTNO_DataInit, 0) = 1) THEN EXIT;
  IF (p_ListName = 'INLIST') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OUTLIST') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'MOVELIST') THEN
    SELECT UpdateAcc From MOVELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PANLIST') THEN
    SELECT UpdateAcc From PANLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'REALLIST') THEN
    SELECT UpdateAcc From REALLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'ASSLIST') THEN
    SELECT UpdateAcc From ASSLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'USELIST') THEN
    SELECT UpdateAcc From USELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'STKINLIST') THEN
    SELECT UpdateAcc From STKINLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'INPAY') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OutPay') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PRESETLIST') THEN
    SELECT UpdateAcc From PRESETLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PRESENDLIST') THEN
    SELECT UpdateAcc From PRESENDLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;

  IF (m_UpdateAcc = '*') THEN R_CHANGEABLE = 0;
  ELSE R_CHANGEABLE = 1;
  IF (R_CHANGEABLE = 0) THEN IF (P_RAISEEXCEPTION = 1) THEN EXCEPTION MSG_MASTER_UPDATEACC_LOCK;
END
---
/* 第二步，检查单号是否存在 以及修改相应的存储过程 */
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN

    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 18) THEN  /* 时段促销 */
  BEGIN
    SELECT COUNT(*) FROM SpecList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 20) THEN  /* 出库 */
  BEGIN
    SELECT COUNT(*) FROM USEList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 23) THEN  /* 入库 */
  BEGIN
    SELECT COUNT(*) FROM STKINList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 29) THEN  /* 赠品设置 */
  BEGIN
    SELECT COUNT(*) FROM PRESETLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 30) THEN  /* 赠品领用 */
  BEGIN
    SELECT COUNT(*) FROM PRESENDLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END

  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
---
/*第三步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  IF (TBLNAME = 'INLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM BUYGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'OUTLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM SALEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'MOVELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM MOVEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'PANLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM PANGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'ASSLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM ASSGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'USELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM USEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'STKINLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM STKINGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'PRESENDLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM PRESENDGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  SUSPEND;
END
--
2005.08.15_01/UPGRADE_DETAIL
1. 修改: 进销存数量统计表, 当生产日期/有效期为0时, 设置为NULL。
--
2005.08.15_01/SQL1
ALTER PROCEDURE GETSTOCKIOLIST_2
(
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
)
RETURNS
(
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
)
AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountUse,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,

          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 20;
  FOR  /* 出库 */
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM USEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END

  WorkMode = 23;
  FOR  /* 入库 */
    SELECT M.IntNo From STKINLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, u.Factor*d.Qty as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM STKINGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (ExpireDate_Int = 0) THEN ExpireDate_Int = NULL;
        IF (ManufactDate_Int = 0) THEN ManufactDate_Int = NULL;
        SUSPEND;
      END
END
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('ProfitRate')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth, DFormat)
  VALUES ('', 'ProfitRate', '毛利率', '10', '0.0000;;')
--
2005.08.01_01/UPGRADE_DETAIL
1. 更新: 收付款模块的现收现付款;
2. 增加: 合理应补字段;
3. 更改: 应补数量1为最低应补, 应补数量2为安全应补;
--
2005.08.01_01/SQL1
UPDATE OutList SET PayAmount = ActPay
  WHERE ((ROUND(PayAmount, 4) = 0) AND ((Flag3 = 0) OR (Flag3 IS NULL)));
---
UPDATE InList SET PayAmount = ActPay
  WHERE ((ROUND(PayAmount, 4) = 0) AND ((Flag3 = 0) OR (Flag3 IS NULL)));
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSMIN')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QLESSSAFETY')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'QSAFETYMIN')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLESSMIN', '最低应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QLESSSAFETY', '安全应补', 'TQty')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('QSafetyMin', '合理应补', 'TQty')
--
2005.07.07_01/UPGRADE_DETAIL
1. 增加: 付款表、收款表;
2. 销售、进货增加字段: 结算金额;
--
2005.07.07_01/SQL1
ALTER PROCEDURE UPDATE_INLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_Flag3 SmallInt;
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE UPDATE_OUTLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_INAMOUNT NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_Flag3 SmallInt;
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN
  SUSPEND;
END
---
/*检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'OUTLISTNO')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'INLISTNO')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'PAYAMOUNT')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'ACTPAY')
  AND (UPPER(TABLENAME) = 'INPAY')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'ACTPAY')
  AND (UPPER(TABLENAME) = 'OUTPAY')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'PAYREMAIN')
---
DELETE FROM FIELDDEFINE WHERE (UPPER(FIELDNAME) = 'SUMPAYAMOUNT')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('OutListNo', '销售单号', 'TListNo')
---
Insert INTO FieldDefine (FieldName, DLabel, DomainName)
  VALUES ('InListNo', '进货单号', 'TListNo')
---
Insert INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PayAmount', 'OutPay', '收款金额', 'TAmount')
---
Insert INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('PayAmount', 'InPay', '付款金额', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('OutList', 'PayAmount', '已结算金额', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('InList', 'PayAmount', '已结算金额', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('InPay', 'ActPay', '现付款', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('OutPay', 'ActPay', '现收款', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'PayRemain', '未结算金额', 'TAmount')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'SumPayAmount', '已结算金额', 'TAmount')
--
2005.07.07_01/SQL2
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc Char(1);
BEGIN
  r_Changeable = 1; IF (GEN_ID(INTNO_DataInit, 0) = 1) THEN EXIT;
  IF (p_ListName = 'INLIST') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OUTLIST') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'MOVELIST') THEN
    SELECT UpdateAcc From MOVELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PANLIST') THEN
    SELECT UpdateAcc From PANLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'REALLIST') THEN
    SELECT UpdateAcc From REALLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'ASSLIST') THEN
    SELECT UpdateAcc From ASSLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'USELIST') THEN
    SELECT UpdateAcc From USELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'STKINLIST') THEN
    SELECT UpdateAcc From STKINLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'INPAY') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OutPay') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (m_UpdateAcc = '*') THEN R_CHANGEABLE = 0;
  ELSE R_CHANGEABLE = 1;
  IF (R_CHANGEABLE = 0) THEN IF (P_RAISEEXCEPTION = 1) THEN EXCEPTION MSG_MASTER_UPDATEACC_LOCK;
END
---
CREATE GENERATOR IntNo_OutPay;
---
CREATE GENERATOR IntNo_InPay;
---
SET GENERATOR IntNo_OutPay TO 10
---
SET GENERATOR IntNo_InPay TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINLIST')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOLST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTPAY')           THEN BEGIN r_NewIntNo = Gen_ID(IntNo_OutPay, 10); SUSPEND; EXIT; END
    IF (P_TableName='INPAY')            THEN BEGIN r_NewIntNo = Gen_ID(IntNo_InPay, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2005.07.07_01/SQL3
CREATE TABLE OutPay
(
  IntNo TIntNo,
  MainIntNo TIntNo,
  OutIntNo TIntNo,
  OutListNo TListNo,
  PayAmount TAmount,
  REMARK	TREMARK,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,

  CONSTRAINT OutPay_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_OutPay_LST FOREIGN KEY (MAININTNO) REFERENCES OUTLIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE
)
---
CREATE TABLE InPay
(
  IntNo TIntNo,
  MainIntNo TIntNo,
  InIntNo TIntNo,
  InListNo TListNo,
  PayAmount TAmount,
  REMARK	TREMARK,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,

  CONSTRAINT InPay_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_InPay_LST FOREIGN KEY (MAININTNO) REFERENCES INLIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE
)
---
ALTER TABLE OutList ADD PayAmount NUMERIC(15, 4)
---
UPDATE OutList SET PayAmount = 0
---
ALTER TABLE InList ADD PayAmount NUMERIC(15, 4)
---
UPDATE InList SET PayAmount = 0
--
2005.07.07_01/SQL4
/* 更新总表INLIST数据的1个存储过程3个触发器用ListNo*/
CREATE PROCEDURE UPDATE_INLIST_PayAmount (
  P_PAYAMOUNT NUMERIC(15, 4),
  P_DIRECT SMALLINT,
  P_ListNo VARCHAR(20)
)  AS
BEGIN
  IF ((:P_ListNo = '') OR (:P_ListNo IS NULL)) THEN EXIT;
  UPDATE INLIST SET PayAmount = PayAmount + (:P_PAYAMOUNT*:P_DIRECT) WHERE ListNo = :P_ListNo;
END
---
CREATE TRIGGER InPay_AFT_DEL_SET_MASTER FOR InPay AFTER DELETE POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
    EXECUTE PROCEDURE UPDATE_INLIST_PayAmount(old.PAYAMOUNT, -1, old.InListNo);
END
---
CREATE TRIGGER InPay_AFT_INS_SET_MASTER FOR InPay AFTER INSERT POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE UPDATE_INLIST_PayAmount(new.PAYAMOUNT, 1, new.InListNo);
END
---
CREATE TRIGGER InPay_AFT_UPD_SET_MASTER FOR InPay AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN /* 更新总表相关数据 */
  m_PayAmount = new.PAYAMOUNT - old.PAYAMOUNT;
  EXECUTE PROCEDURE UPDATE_INLIST_PayAmount(:m_PayAmount, 1, old.InListNo);
END
---
/* 更新总表INLIST数据的1个存储过程3个触发器用MainIntNo*/
CREATE PROCEDURE Update_InPay_Master (
  P_PAYAMOUNT NUMERIC(15, 4),
  P_DIRECT SMALLINT,
  P_MAININTNO INTEGER
)  AS
BEGIN
  IF ((:P_MAININTNO = 0) OR (:P_MAININTNO IS NULL)) THEN EXIT;
  UPDATE INLIST SET PayAmount = PayAmount + (:P_PAYAMOUNT*:P_DIRECT) WHERE IntNo = :P_MainIntNo;
END
---
CREATE TRIGGER InPay_AFT_DEL_SET_MASTER_1 FOR InPay AFTER DELETE POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE Update_InPay_Master(old.PAYAMOUNT, -1, old.MainIntNo);
END
---
CREATE TRIGGER InPay_AFT_INS_SET_MASTER_1 FOR InPay AFTER INSERT POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE Update_InPay_Master(new.PAYAMOUNT, 1, new.MainIntNo);
END
---
CREATE TRIGGER InPay_AFT_UPD_SET_MASTER_1 FOR InPay AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN /* 更新总表相关数据 */
  m_PayAmount = new.PAYAMOUNT - old.PAYAMOUNT;
  EXECUTE PROCEDURE Update_InPay_Master(:m_PayAmount, 1, old.MainIntNo);
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许删除 */
CREATE TRIGGER InPay_BEF_DEL_CHK_UPACC FOR InPay BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
    EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('INPAY', old.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许新增 */
CREATE TRIGGER InPay_BEF_INS_CHK_UPACC FOR InPay BEFORE INSERT POSITION 1 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('INPAY', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许修改 */
CREATE TRIGGER InPay_BEF_UPD_CHK_UPACC FOR InPay BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('INPAY', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 更新总表OUTLIST数据的1个存储过程3个触发器用ListNo*/
CREATE PROCEDURE UPDATE_OUTLIST_PayAMOUNT (
  P_PayAmount NUMERIC(15, 4),
  P_DIRECT SMALLINT,
  P_ListNo VARCHAR(20)
)  AS
BEGIN
  IF ((:P_ListNo = '') OR (:P_ListNo IS NULL)) THEN EXIT;
  UPDATE OUTLIST SET PayAmount = PayAmount + (:P_PayAmount*:P_DIRECT) WHERE ListNo = :P_ListNo;
END
---
CREATE TRIGGER OutPay_AFT_DEL_SET_MASTER FOR OutPay AFTER DELETE POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
    EXECUTE PROCEDURE UPDATE_OUTLIST_PayAMOUNT(old.PayAmount, -1, old.OutListNo);
END
---
CREATE TRIGGER OutPay_AFT_INS_SET_MASTER FOR OutPay AFTER INSERT POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE UPDATE_OUTLIST_PayAMOUNT(new.PayAmount, 1, new.OutListNo);
END
---
CREATE TRIGGER OutPay_AFT_UPD_SET_MASTER FOR OutPay AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN /* 更新总表相关数据 */
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_PayAmount = new.PayAmount - old.PayAmount;
    EXECUTE PROCEDURE UPDATE_OUTLIST_PayAMOUNT(:m_PayAmount, 1, old.OutListNo);
  END
END
---
/* 更新总表OUTLIST数据的1个存储过程3个触发器用MainIntNo*/
CREATE PROCEDURE Update_OutPay_Master (
  P_PayAmount NUMERIC(15, 4),
  P_DIRECT SMALLINT,
  P_MAININTNO INTEGER
)  AS
BEGIN
  IF ((:P_MainIntNo = 0) OR (:P_MainIntNo IS NULL)) THEN EXIT;
  UPDATE OUTLIST SET PayAmount = PayAmount + (:P_PayAmount*:P_DIRECT) WHERE IntNo = :P_MainIntNo;
END
---
CREATE TRIGGER OutPay_AFT_DEL_SET_MASTER_1 FOR OutPay AFTER DELETE POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE Update_OutPay_Master(old.PayAmount, -1, old.MainIntNo);
END
---
CREATE TRIGGER OutPay_AFT_INS_SET_MASTER_1 FOR OutPay AFTER INSERT POSITION 0 AS
BEGIN /* 更新总表相关数据 */
  EXECUTE PROCEDURE Update_OutPay_Master(new.PayAmount, 1, new.MainIntNo);
END
---
CREATE TRIGGER OutPay_AFT_UPD_SET_MASTER_1 FOR OutPay AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN /* 更新总表相关数据 */
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_PayAmount = new.PayAmount - old.PayAmount;
    EXECUTE PROCEDURE Update_OutPay_Master(:m_PayAmount, 1, old.MainIntNo);
  END
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许删除 */
CREATE TRIGGER OutPay_BEF_DEL_CHK_UPACC FOR OutPay BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('OutPay', old.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许新增 */
CREATE TRIGGER OutPay_BEF_INS_CHK_UPACC FOR OutPay BEFORE INSERT POSITION 1 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('OutPay', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 检查主表是否已生效, 如果已生效, 则不允许修改 */
CREATE TRIGGER OutPay_BEF_UPD_CHK_UPACC FOR OutPay BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('OutPay', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
ALTER PROCEDURE UPDATE_INLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_Flag3 SmallInt;
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN
  m_FLag3 = 0;
  m_PayAmount = 0;
  SELECT Flag3 FROM InList WHERE (IntNo = :MainIntNo) INTO m_Flag3;
  IF ((:m_Flag3 = 0) OR (:m_Flag3 IS NULL)) THEN m_PayAmount = :p_ACTPAY*:Direct;

  UPDATE INLIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    ACTPAY = ACTPAY + (:p_ACTPAY*:Direct),
    PayAmount = PayAmount + :m_PayAmount,
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
ALTER PROCEDURE UPDATE_OUTLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_INAMOUNT NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_Flag3 SmallInt;
DECLARE VARIABLE m_PayAmount NUMERIC(15, 4);
BEGIN
  m_FLag3 = 0;
  m_PayAmount = 0;
  SELECT Flag3 FROM OutList WHERE (IntNo = :MainIntNo) INTO m_Flag3;
  IF ((:m_Flag3 = 0) OR (:m_Flag3 IS NULL)) THEN m_PayAmount = :p_ACTPAY*:Direct;

  UPDATE OUTLIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    ACTPAY = ACTPAY + (:p_ACTPAY*:Direct),
    PayAmount = PayAmount + :m_PayAmount,
    PROFIT = PROFIT + (:p_GDSPAY - :p_INAMOUNT)*:Direct,
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
--
2005.06.22_01/UPGRADE_DETAIL
1. 增加: 取得帐期报表的存储过程。
--
2005.06.22_01/SQL1
CREATE PROCEDURE GET_DAYLIMIT_QUERY_2 (
  P_TODAY TIMESTAMP,
  P_CLIENTFLAG SMALLINT,
  P_OVERDAYLIMIT SMALLINT
) RETURNS (
  R_INTNO INTEGER,
  R_ACTPAY NUMERIC(15, 4),
  R_GDSPAY NUMERIC(15, 4),
  R_ALLACTPAY NUMERIC(15, 4),
  R_ALLGDSPAY NUMERIC(15, 4),
  R_LASTREMAINDATE TIMESTAMP,
  R_LASTREMAINNO VARCHAR(20),
  R_LASTACTDATE TIMESTAMP,
  R_LASTACTNO VARCHAR(20),
  R_REMAINDATE INTEGER,
  r_LastActPay NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(20);
DECLARE VARIABLE m_DayLimit SMALLINT;
DECLARE VARIABLE m_NewDay TIMESTAMP;
DECLARE VARIABLE m_SumActPay NUMERIC(15, 4);
DECLARE VARIABLE m_SumGdsPay NUMERIC(15, 4);
DECLARE VARIABLE m_AllActPay NUMERIC(15, 4);
DECLARE VARIABLE m_AllGdsPay NUMERIC(15, 4);
DECLARE VARIABLE m_RemainActPay NUMERIC(15, 4);
DECLARE VARIABLE m_ListDate TIMESTAMP;
DECLARE VARIABLE m_ListNo VARCHAR(20);
DECLARE VARIABLE m_GdsPay NUMERIC(15, 4);
DECLARE VARIABLE m_ActPay NUMERIC(15, 4);
BEGIN
  FOR SELECT IntNo, CmpyName, DayLimit FROM Client WHERE (ClientFlag = :p_ClientFlag)
    ORDER BY CmpyName INTO :m_IntNo, :m_CmpyName, :m_DayLimit DO
  BEGIN
    r_IntNo = 0;
    r_ActPay = 0.00;
    r_GdsPay = 0.00;
    m_SumActPay = 0.00;
    m_SumGdsPay = 0.00;
    m_AllActPay = 0.00;
    m_AllGdsPay = 0.00;
    r_LastRemainDate = NULL;
    r_LastRemainNo   = '';
    r_LastActDate    = NULL;
    r_LastActNo      = '';
    r_RemainDate = - :m_DayLimit;
    m_NewDay = :p_ToDay - :m_DayLimit; /*检查的日期*/

    IF (:m_DayLimit <> 0) THEN
    BEGIN
      /* 第一步, 先取得客户的所有的货款及付款 */
      SELECT Round(r_SumActPay, 8), Round(r_SumGdsPay, 8)
        FROM CHECK_DAYLIMIT(:m_CmpyName, :p_ClientFlag, '2049.12.31')
        INTO :m_AllActPay, :m_AllGdsPay;

      /* 第二步, 如果货款<付款则表示该客户需要检查帐期的付款情况*/
      IF (:m_AllActPay - :m_AllGdsPay < 0.00) THEN
      BEGIN
        m_RemainActPay = :m_AllActPay;

        IF (:p_ClientFlag = 0) THEN /*销售*/
        FOR SELECT m.ListDate, m.ListNo, Round(m.ActPay, 8), Round(m.GdsPay, 8)
          FROM OUTLIST m WHERE (m.CmpyName = :m_CmpyName)
          ORDER BY m.ListDate INTO :m_ListDate, :m_ListNo, :m_ActPay, :m_GdsPay DO
        BEGIN
          /*某单的货款=0 不用处理, 继续下一条*/
          IF (:m_GdsPay <> 0.00) THEN
          BEGIN
            m_RemainActPay = :m_RemainActPay - :m_Gdspay;
            IF ((:m_RemainActPay < 0.00) AND (:r_LastRemainDate IS NULL)) THEN
            BEGIN
              r_LastRemainDate = :m_ListDate; /*返回日期*/
              r_LastRemainNo   = :m_ListNo;   /*返回未结款日单号*/
            END
          END
          IF (:m_ActPay > 0.00) THEN
          BEGIN
            r_LastActDate = :m_ListDate; /*返回近付款日*/
            r_LastActNo   = :m_ListNo;   /*返回最后付款单号*/
          END
        END

        IF (:p_ClientFlag = 1) THEN /*进货*/
        FOR SELECT m.ListDate, m.ListNo, Round(m.ActPay, 8), Round(m.GdsPay, 8)
          FROM INLIST m WHERE (m.CmpyName = :m_CmpyName)
          ORDER BY m.ListDate INTO :m_ListDate, :m_ListNo, :m_ActPay, :m_GdsPay DO
        BEGIN
          /*某单的货款=0 不用处理, 继续下一条*/
          IF (:m_GdsPay <> 0.00) THEN
          BEGIN
            m_RemainActPay = :m_RemainActPay - :m_Gdspay;
            IF ((:m_RemainActPay < 0.00) AND (:r_LastRemainDate IS NULL)) THEN
            BEGIN
              r_LastRemainDate = :m_ListDate; /*返回日期*/
              r_LastRemainNo   = :m_ListNo;   /*返回未结款日单号*/
            END
          END
          IF (:m_ActPay > 0.00) THEN
          BEGIN
            r_LastActDate = :m_ListDate; /*返回近付款日*/
            r_LastActNo   = :m_ListNo;   /*返回最后付款单号*/
          END
        END

        /*检查是否返回所有的记录还是返回超过帐期的记录*/
        IF (:p_OverDayLimit <> 1) THEN r_IntNo = :m_IntNo;
        r_AllActPay = :m_AllActPay;
        r_AllGdsPay = :m_AllGdsPay;
        r_RemainDate = CSTR_TO_DBL(:p_ToDay - :m_DayLimit - :r_LastRemainDate);
      END

      /*返回帐期前欠款*/
      SELECT Round(r_SumActPay, 8), Round(r_SumGdsPay, 8)
        FROM CHECK_DAYLIMIT(:m_CmpyName, :p_ClientFlag, :m_NewDay)
        INTO :m_SumActPay, :m_SumGdsPay;
      IF (:m_SumActPay - :m_SumGdsPay < 0.00) THEN
      BEGIN
        r_IntNo  = :m_IntNo;  /*只有超过帐期的记录被返回*/
        r_GdsPay = :m_SumGdsPay;
        r_ActPay = :m_SumActPay;
      END
      /*付款小于货款就不能通过检查*/
    END
    SUSPEND;
  END
END
--
2005.05.28_01/UPGRADE_DETAIL
1. 调整: 进销存统计报表支持自定义内容及批号;
--
2005.05.28_01/SQL1
/*进销存数量统计报表精确到自定义内容_批号_效期_取明细*/
CREATE PROCEDURE GETSTOCKIOLIST_2(
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountUse,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,

          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 20;
  FOR  /* 出库 */
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM USEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;

  WorkMode = 23;
  FOR  /* 入库 */
    SELECT M.IntNo From STKINLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, u.Factor*d.Qty as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManufactDate_Int
      FROM STKINGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, ManufactDate_Int
      DO SUSPEND;
END
---
/*进销存数量统计报表精确到自定义内容_批号_效期_根据明细合并*/
CREATE PROCEDURE GETSTOCKIOSUM_2(
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR
    Select GoodsIntNo, StockIntNo,
      Sum(QtyIn)-Sum(QtyOut)+Sum(QtyMoveIn)-Sum(QtyMoveOut)+Sum(QtyPan)+
      Sum(QtyAssIn-QtyAssOut)-Sum(QtyUse)+Sum(QtyStkIn) as QInitRemain,
      Sum(AmountIn)-Sum(AmountOut)+Sum(AmountMoveIn)-Sum(AmountMoveOut)+Sum(AmountPan)+
      Sum(AmountAssIn-AmountAssOut)-Sum(AmountUse)+Sum(AmountStkIn) as InitAmount,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      BatchNo, ExpireDate_Int, ManufactDate_Int
      From GetStockIOList_2('0001.1.1', :p_DateFrom-1)
      Group by GoodsIntNo, StockIntNo,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        BatchNo, ExpireDate_Int, ManufactDate_Int
    UNION ALL
    Select GoodsIntNo, StockIntNo,
      0.0 as QInitRemain,
      0.0 as InitAmount,
      Sum(QtyIn) as QtyIn, Sum(QtyOut) as QtyOut, Sum(QtyMoveIn) as QtyMoveIn,
      Sum(QtyMoveOut) as QtyMoveOut, Sum(QtyPan) as QtyPan,
      Sum(QtyAssIn) as QtyAssIn, Sum(QtyAssOut) as QtyAssOut, Sum(QtyUse) as QtyUse, Sum(QtyStkIn) as QtyStkIn,
      Sum(AmountIn) as AmountIn, Sum(AmountOut) as AmountOut, Sum(AmountMoveIn) as AmountMoveIn,
      Sum(AmountMoveOut) as AmountMoveOut, Sum(AmountPan) as AmountPan,
      Sum(AmountAssIn) as AmountAssIn, Sum(AmountAssOut) as AmountAssOut,
      Sum(AmountUse) as AmountUse, Sum(AmountStkIn) as AmountStkIn,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      BatchNo, ExpireDate_Int, ManufactDate_Int
      From GetStockIOList_2(:p_DateFrom, :p_DateTo)
      Group by GoodsIntNo, StockIntNo,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        BatchNo, ExpireDate_Int, ManufactDate_Int

        INTO :GoodsIntNo, :StockIntNo, :QInitRemain, :InitAmount,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
          :BatchNo, :ExpireDate_Int, :ManufactDate_Int
    DO SUSPEND;
END
--
2005.05.21_01/UPGRADE_DETAIL
1. 调整: 获得类别信息的机制;
2. 调整: 取交班资料存储过程;
--
2005.05.21_01/SQL1
ALTER PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  GOODSINTNO INTEGER,
  UNITID INTEGER,
  STOCKINTNO INTEGER,
  BANKINTNO INTEGER,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80),
  ISSPECPRICE SMALLINT,
  StoreNo VARCHAR(80)
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
BEGIN
  FOR SELECT
    d.GoodsIntNo,
    d.UnitId,
    d.StockIntNo,
    d.BankIntNo,
    d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,
    d.AMOUNT,
    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5,
    d.isSpecPrice,
    d.StoreNo
    FROM Salegds d, StkName, BankName
    WHERE StkName.IntNo = d.StockIntNo and d.BankIntNo = BankName.IntNo
    INTO :GoodsIntNo, :UnitId, :StockIntNo, :BankIntNo,
      :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
      :isSpecPrice, :StoreNo
    DO
    BEGIN
      IF (GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN
        SELECT GoodsNo FROM Stklst Where IntNo = :GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo = :GoodsIntNo and u.UnitId = :UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN CustomValue2 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
---
ALTER TABLE TYPEDEFINE
  ADD FullTypeNo VARCHAR(20),
  ADD FullTypeName VARCHAR(80)
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GdsTypeNo')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)
  VALUES ('', 'GdsTypeNo', '类别代码', 'GdsTypeNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('FullTypeNo'))
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)
  VALUES ('', 'FullTypeNo', '类别代码', 'GdsTypeNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'TYPENO')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)
  VALUES ('', 'TypeNo', '子类别码', 8)
---
DELETE From FieldDefine Where (Upper(FieldName) = 'TYPENAME')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)
  VALUES ('', 'TypeName', '子类别名', 'GdsTypeName')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('FullTypeName'))
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)
  VALUES ('', 'FullTypeName', '类别全名', 'GdsFullTypeName')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'TYPELEVEL')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel)
  VALUES ('', 'TypeLevel', '级别')
---
CREATE PROCEDURE SET_FULL_TYPEINFO
AS
DECLARE VARIABLE m_IntNo INTEGER;
DECLARE VARIABLE m_MasterIntNo INTEGER;
DECLARE VARIABLE m_TypeNo VARCHAR(20);
DECLARE VARIABLE m_TypeName VARCHAR(20);
DECLARE VARIABLE m_FullTypeNo VARCHAR(20);
DECLARE VARIABLE m_FullTypeName VARCHAR(80);
BEGIN
  FOR SELECT IntNo, MasterIntNo, TypeNo, TypeName FROM TypeDefine
    INTO :m_IntNo, :m_MasterIntNo, :m_TypeNo, :m_TypeName DO
  BEGIN
    m_FullTypeNo = ALLTRIM(:m_TypeNo);
    m_FullTypeName = ALLTRIM(:m_TypeName);
    WHILE (NOT (:m_MasterIntNo = 0)) DO
    BEGIN
      SELECT MasterIntNo, TypeNo, TypeName FROM TypeDefine WHERE (IntNo = :m_MasterIntNO)
        INTO :m_MasterIntNo, :m_TypeNo, :m_TypeName;
      m_FullTypeNo = ALLTRIM(:m_TypeNo) || ALLTRIM(:m_FullTypeNo);
      m_FullTypeName = ALLTRIM(:m_TypeName) ||'\'|| ALLTRIM(:m_FullTypeName);
    END
    UPDATE TypeDefine SET FullTypeNo = :m_FullTypeNo, FullTypeName = :m_FullTypeName
      WHERE (IntNo = :m_IntNo);
  END
END
---
EXECUTE PROCEDURE SET_FULL_TYPEINFO
--
2005.04.26_01/UPGRADE_DETAIL
1. 新增: 配方定义的操作员等字段;
2. 新增: 店内码主/明细表;
--
2005.04.26_01/SQL1
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = 'STORENO' AND UPPER(TABLENAME) = 'CLIENT'
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = 'STORENO' AND UPPER(TABLENAME) = 'STORENOLST'
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = 'STORENO' AND UPPER(TABLENAME) = 'STORENOGDS'
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo', 'Client', '店内码表代号', 'TPriceNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo', 'StoreNoLst', '店内码表代号', 'TPriceNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo', 'StoreNoGds', '店内码表代号', 'TPriceNo')
--
2005.04.26_01/SQL2
ALTER TABLE FORMULA
  ADD LISTTIME TTIME,
  ADD OPERNO TOPERNO,
  ADD PERSONDEAL TPERSONNAME,
  ADD DEPARTMENT TDEPARTMENT,
  ADD LREMARK2 VARCHAR(120)
---
/*客户中的店内码表*/
ALTER TABLE Client ADD StoreNo TPriceNo
---
CREATE GENERATOR INTNO_StoreNoLst;
---
CREATE GENERATOR INTNO_StoreNoGds;
---
SET GENERATOR INTNO_StoreNoLst TO 10
---
SET GENERATOR INTNO_StoreNoGds TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINLIST')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOLST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STORENOGDS')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STORENOGDS, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2005.04.26_01/SQL3
/*建立主表*/
CREATE TABLE STORENOLST (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  STORENO TPRICENO,
  COMMENT TCOMMENT,
  CLIENTFLAG TCLIENTFLAG,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  LREMARK2 VARCHAR(120),
  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,

  CONSTRAINT STORENOLST_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT STORENOLST_UNIQUE_1 UNIQUE (STORENO, CLIENTFLAG)
)
--
2005.04.26_01/SQL4
/*建明细表*/
CREATE TABLE StoreNoGds
(
  INTNO	TINTNO,
  MAININTNO	TINTNO,
  GOODSINTNO	TGOODSINTNO,
  STORENO1 VARCHAR(80),
  STORENO2 VARCHAR(80),
  STORENO3 VARCHAR(80),
  REMARK	TREMARK,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
  CUSTOMINTNO1	TCUSTOMINTNO,
  CUSTOMINTNO2	TCUSTOMINTNO,
  CUSTOMINTNO3	TCUSTOMINTNO,
  CUSTOMINTNO4	TCUSTOMINTNO,
  CUSTOMINTNO5	TCUSTOMINTNO,
  MANUFACTDATE_INT	TSMALLINTDATE,
  EXPIREDATE_INT	TSMALLINTDATE,
  BATCHNO	TBATCHNO,

  CONSTRAINT STORENOGDS_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_STORENOGDS_LST FOREIGN KEY (MAININTNO) REFERENCES STORENOLST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_STORENOGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE
)
---
CREATE ASCENDING INDEX IDX_STORENOGDS_MAININTNO ON STORENOGDS (MAININTNO)
---
CREATE UNIQUE ASCENDING INDEX IDX_STORENOGDS_1 ON STORENOGDS (MAININTNO, GOODSINTNO)
--
2005.04.26_01/SQL5
/*主表的触发部分, 设置备注*/
CREATE TRIGGER STORENOLST_BEF_INS_SET_DEF FOR STORENOLST BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.STORENO is NULL) THEN new.STORENO = '';
  IF (new.COMMENT is NULL) THEN new.COMMENT = '';
  IF (new.CLIENTFLAG is NULL) THEN new.CLIENTFLAG = 0;
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
END
---
CREATE TRIGGER STORENOLST_BEF_UPD_SET_DEF FOR STORENOLST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.UPDATEACC is NULL) THEN new.UPDATEACC = '';
  IF (new.STORENO is NULL) THEN new.STORENO = '';
  IF (new.COMMENT is NULL) THEN new.COMMENT = '';
  IF (new.CLIENTFLAG is NULL) THEN new.CLIENTFLAG = 0;
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
END
---
/*主表的触发部分 删除主表时删除明细表*/
CREATE TRIGGER StoreNoLst_BEF_DEL_DEL_DETAIL FOR StoreNoLst
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM STORENOGds WHERE MAININTNO = old.intno;
END
---
/*主表删除时, 如果单据已生效, 则不允许删除(除非正在做帐务清除)*/
CREATE TRIGGER STORENOLst_BEF_DEL_CHK_UPACC FOR STORENOLST
ACTIVE BEFORE DELETE POSITION 2
AS
BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---
/*三个主表同步用的触发*/
CREATE TRIGGER StoreNoLst_BEF_INS_EVENTLOG FOR StoreNoLst
ACTIVE BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoLst_BEF_UPD_EVENTLOG FOR StoreNoLst
ACTIVE BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoLst_AFT_DEL_EVENTLOG FOR StoreNoLst
ACTIVE AFTER DELETE POSITION 1
AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('StoreNoIn', Old.IntNo);
END
---
/*主表生效时根据明细记录更新相应的跟踪表*/
CREATE PROCEDURE STORENOLST_UPDATEACC_STORENO(
p_StoreNo CHAR(20), p_IntNo Integer, p_ClientFlag SmallInt
)AS
DECLARE VARIABLE m_CmpyName CHAR(20);
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StoreNo1 VARCHAR(80);
DECLARE VARIABLE m_StoreNo2 VARCHAR(80);
DECLARE VARIABLE m_StoreNo3 VARCHAR(80);
BEGIN
  m_CmpyName = '';
  FOR
    SELECT CmpyName FROM Client WHERE (ClientFlag = :p_ClientFlag) AND (StoreNo = :p_StoreNo) INTO :m_CmpyName DO
    BEGIN
      m_GoodsIntNo = 0; m_StoreNo1 = ''; m_StoreNo2 = ''; m_StoreNo3 = '';
      FOR SELECT GoodsIntNo, StoreNo1, StoreNo2, StoreNo3 From StoreNoGds  WHERE MainIntNo = :p_IntNo
        INTO :m_GoodsIntNo, :m_StoreNo1, :m_StoreNo2, :m_StoreNo3 DO
        BEGIN
          IF (:p_ClientFlag = 1) THEN
          BEGIN
            UPDATE StoreNoIn Set StoreNo1 = :m_StoreNo1, StoreNo2 = :m_StoreNo2, StoreNo3 = :m_StoreNo3
              Where (GoodsIntNo = :m_GoodsIntNo) AND (CmpyName = :m_CmpyName);
          END
          IF (:p_ClientFlag = 0) THEN
          BEGIN
            UPDATE StoreNoOut Set StoreNo1 = :m_StoreNo1, StoreNo2 = :m_StoreNo2, StoreNo3 = :m_StoreNo3
              Where (GoodsIntNo = :m_GoodsIntNo) AND (CmpyName = :m_CmpyName);
          END
        END
    END
END
---
/*主表生效时更新相应的店内码跟踪*/
CREATE TRIGGER STORENOLST_BEF_UPD_ACC FOR STORENOLST BEFORE UPDATE POSITION 1 AS
BEGIN
  /*不允许空, 并且确保是生效*/ 
  IF (NOT ((new.StoreNo IS NULL) or (new.StoreNo = ''))) THEN
  BEGIN
    IF (((Old.UpdateAcc IS NULL) OR (old.UpdateAcc = '')) AND (new.UpdateAcc = '*')) THEN 
    BEGIN
      EXECUTE PROCEDURE STORENOLST_UPDATEACC_STORENO(new.StoreNo, new.IntNo, new.ClientFlag);
    END
  END
END
---
/*明细表的触发部分, 设置备注*/
CREATE TRIGGER STORENOGDS_BEF_INS_SET_DEF FOR STORENOGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.STORENO1 is NULL) THEN new.STORENO1 = '';
  IF (new.STORENO2 is NULL) THEN new.STORENO2 = '';
  IF (new.STORENO3 is NULL) THEN new.STORENO3 = '';
END
---
CREATE TRIGGER STORENOGDS_BEF_UPD_SET_DEF FOR STORENOGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.STORENO1 is NULL) THEN new.STORENO1 = '';
  IF (new.STORENO2 is NULL) THEN new.STORENO2 = '';
  IF (new.STORENO3 is NULL) THEN new.STORENO3 = '';
END
---
/*明细表的3个同步触发*/
CREATE TRIGGER StoreNoGds_AFT_DEL_EVENT_LOG FOR StoreNoGds AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('STORENOGDS', old.IntNo);
END
---
CREATE TRIGGER StoreNoGds_BEF_INS_EVENTLOG FOR StoreNoGds BEFORE INSERT POSITION 10 AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoGds_BEF_UPD_EVENTLOG FOR StoreNoGds BEFORE UPDATE POSITION 10 AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE EXCEPTION MSG_StoreNoGds_GoodsNo_Not_Null "店内码明细中货号不能为空"
---
CREATE EXCEPTION MSG_StoreNoGds_GoodsNo_DIFF "店内码明细中货号不能重复"
---
CREATE PROCEDURE CHECK_StoreNoGds_GOODSNO_DIFF(
p_MainIntNo Integer,
p_GoodsIntNo Integer
)AS
DECLARE VARIABLE m_RecCount Integer;
BEGIN
  m_RecCount = 0;
  FOR
    Select COUNT(*) from StoreNoGds
      Where (MainIntNo = :p_MainIntNo AND GoodsIntNo = :p_GoodsIntNO) INTO :m_RecCount
  DO
    BEGIN
      IF (m_RecCount > 0) THEN EXCEPTION MSG_StoreNoGds_GoodsNo_DIFF;
    END
END
---
CREATE TRIGGER StoreNoGds_BEF_UPD_1 FOR StoreNoGds BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.GoodsIntNo is null) then EXCEPTION MSG_StoreNoGds_GoodsNo_Not_Null;
    IF (new.GoodsIntNo <> old.GoodsIntNo) THEN
      EXECUTE PROCEDURE CHECK_StoreNoGds_GOODSNO_DIFF(new.MainIntNo, new.GoodsIntNo);
  END
END
---
CREATE TRIGGER StoreNoGds_BEF_INS_1 FOR StoreNoGds BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.GoodsIntNo is null) then EXCEPTION MSG_StoreNoGds_GoodsNo_Not_Null;
  EXECUTE PROCEDURE CHECK_StoreNoGds_GOODSNO_DIFF(new.MainIntNo, new.GoodsIntNo);
END
---
/*单位资料中设置STORENO默认值*/
CREATE TRIGGER CLIENT_BEF_INS_SET_DEF1 FOR CLIENT BEFORE INSERT POSITION 1 AS
BEGIN
  IF (new.StoreNo is NULL) THEN new.StoreNo = '';
END
---
/*单位资料中设置STORENO默认值*/
CREATE TRIGGER CLIENT_BEF_UPD_SET_DEF1 FOR CLIENT BEFORE UPDATE POSITION 1 AS
BEGIN
  IF (new.StoreNo is NULL) THEN new.StoreNo = '';
END
--
2005.04.15_01/UPGRADE_DETAIL
1. 新增: 配方定义的生效标志。
--
2005.04.15_01/SQL1
ALTER TABLE FORMULA ADD UpdateAcc TUPDATEFLAG DEFAULT '' NOT NULL
--
2005.03.29_01/UPGRADE_DETAIL
1. 新增: 对销售/进货店内码的支持。
--
2005.03.29_01/SQL5
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER,
  QREMAINNEW DOUBLE PRECISION,
  QREMAINOLD DOUBLE PRECISION
) AS
BEGIN
  FOR
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse+d.QtyStkIn) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse+d.AmountStkIn) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int,
    0.00 AS QRemainNew,
    SUM(-d.QtyIn+d.QtyOut-d.QtyMoveIn+d.QtyMoveOut-d.QtyPan-d.QtyAssIn+d.QtyAssOut+d.QtyUse-d.QtyStkIn) AS QRemainOld
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int,
      SUM(d.Qty*u.Factor) AS QRemainNew,
      0.00 AS QRemainOld
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo

      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int,
      0.00 AS QRemainNew,
      SUM(d.QRemain) AS QRemainOld
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
          ,:ManuFactDate_Int, :ExpireDate_Int, :QRemainNew, :QRemainOld
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
--
2005.03.29_01/SQL_PREPARE1
ALTER PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  GOODSINTNO INTEGER,
  UNITID INTEGER,
  STOCKINTNO INTEGER,
  BANKINTNO INTEGER,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80),
  ISSPECPRICE SMALLINT,
  StoreNo VARCHAR(80)
) AS
BEGIN
  SUSPEND;
END
--
2005.03.29_01/SQL1
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('StoreNo')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('StoreNo1')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('StoreNo2')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('StoreNo3')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo', '', '店内码', 'TGoodsNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo1', '', '店内码1', 'TGoodsNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo2', '', '店内码2', 'TGoodsNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('StoreNo3', '', '店内码3', 'TGoodsNo')
---
CREATE GENERATOR INTNO_USEStoreNoIN;
---
CREATE GENERATOR INTNO_USEStoreNoOUT;
---
SET GENERATOR INTNO_USEStoreNoIN TO 0
---
SET GENERATOR INTNO_USEStoreNoOUT TO 0
---
CREATE GENERATOR INTNO_StoreNoIN;
---
CREATE GENERATOR INTNO_StoreNoOUT;
---
SET GENERATOR INTNO_StoreNoIN TO 10
---
SET GENERATOR INTNO_StoreNoOUT TO 10
---
ALTER TABLE BuyGds ADD StoreNo VarChar(80)
---
ALTER TABLE SaleGds ADD StoreNo VarChar(80)
---
ALTER TABLE BuyGdsBK ADD StoreNo VarChar(80)
---
ALTER TABLE SaleGdsBK ADD StoreNo VarChar(80)
--
2005.03.29_01/SQL2
CREATE TABLE StoreNoIn(
  INTNO TINTNO,
  CMPYNAME TCMPYNAME,
  GOODSINTNO TINTNO,
  LISTDATE TDATE,
  StoreNo1 VarChar(80),
  StoreNo2 VarChar(80),
  StoreNo3 VarChar(80),
  INSERTUCT INTEGER,
  UPDATEUCT INTEGER,
  KEYVALUE COMPUTED BY (GoodsIntNo || '+-*/' || ALLTRIM(CmpyName)),
CONSTRAINT StoreNoIN_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT StoreNoIN_UNIQUE_1 UNIQUE (CMPYNAME, GOODSINTNO)
)
---
CREATE TABLE StoreNoOut(
  INTNO TINTNO,
  CMPYNAME TCMPYNAME,
  GOODSINTNO TINTNO,
  LISTDATE TDATE,
  StoreNo1 VarChar(80),
  StoreNo2 VarChar(80),
  StoreNo3 VarChar(80),
  INSERTUCT INTEGER,
  UPDATEUCT INTEGER,
  KEYVALUE COMPUTED BY (GoodsIntNo || '+-*/' || ALLTRIM(CmpyName)),
CONSTRAINT StoreNoOUT_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT StoreNoOUT_UNIQUE_1 UNIQUE (CMPYNAME, GOODSINTNO)
)
--
2005.03.29_01/SQL3
/*生成内部号*/
CREATE TRIGGER CREATE_INTNO_StoreNoIN FOR StoreNoIN BEFORE INSERT POSITION 0 AS
BEGIN
  NEW.INTNO = GEN_ID(INTNO_StoreNoIN, 10);
END
---
/*生成内部号*/
CREATE TRIGGER CREATE_INTNO_StoreNoOUT FOR StoreNoOUT BEFORE INSERT POSITION 0 AS
BEGIN
  NEW.INTNO = GEN_ID(INTNO_StoreNoOUT, 10);
END
---
/*三个同步用的触发*/
CREATE TRIGGER StoreNoIN_BEF_INS_EVENTLOG FOR StoreNoIN
ACTIVE BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoIN_BEF_UPD_EVENTLOG FOR StoreNoIN
ACTIVE BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoIN_AFT_DEL_EVENTLOG FOR StoreNoIN
ACTIVE AFTER DELETE POSITION 1
AS
DECLARE VARIABLE m_KeyValue VARCHAR(255);
BEGIN
  m_KeyValue = old.KeyValue;
  EXECUTE PROCEDURE SYS_LOGDELETE('StoreNoIn', m_KeyValue);
END
---
/*三个同步用的触发*/
CREATE TRIGGER StoreNoOUT_BEF_INS_EVENTLOG FOR StoreNoOUT
ACTIVE BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoOUT_BEF_UPD_EVENTLOG FOR StoreNoOUT
ACTIVE BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StoreNoOUT_AFT_DEL_EVENTLOG FOR StoreNoOUT
ACTIVE AFTER DELETE POSITION 1
AS
DECLARE VARIABLE m_KeyValue VARCHAR(255);
BEGIN
  m_KeyValue = old.KeyValue;
  EXECUTE PROCEDURE SYS_LOGDELETE('StoreNoOut', m_KeyValue);
END
---
/*进货明细保存退出时更新对应的店内码跟踪*/
CREATE PROCEDURE StoreNoIN_UPDATE (
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_StoreNo VarChar(80),
  P_UnitId SmallInt,
  P_TableType SmallInt
)  AS
DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TIMESTAMP;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;

  IF (P_TableType = 0) THEN
  FOR SELECT CmpyName, ListDate From INLIST WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  IF (P_TableType = 1) THEN
  FOR SELECT CmpyName, ListDate From INLISTBK WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM StoreNoIn WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (:P_UnitId = 0) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoIn(GoodsIntNo, CmpyName,  ListDate, StoreNo1)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoIN SET ListDate = :m_ListDate, StoreNo1 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END

  IF (:P_UnitId = 1) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoIn(GoodsIntNo, CmpyName,  ListDate, StoreNo2)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoIN SET ListDate = :m_ListDate, StoreNo2 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END

  IF (:P_UnitId = 2) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoIn(GoodsIntNo, CmpyName,  ListDate, StoreNo3)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoIN SET ListDate = :m_ListDate, StoreNo3 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END
END
---
/*销售明细保存退出时更新对应的店内码跟踪*/
CREATE PROCEDURE StoreNoOut_UPDATE (
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_StoreNo VarChar(80),
  P_UnitId SmallInt,
  P_TableType SmallInt
)  AS
DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TIMESTAMP;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;

  IF (P_TableType = 0) THEN
  FOR SELECT CmpyName, ListDate FROM OutList WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  IF (P_TableType = 1) THEN
  FOR SELECT CmpyName, ListDate FROM OutListBK WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM StoreNoOut WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (:P_UnitId = 0) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoOut(GoodsIntNo, CmpyName,  ListDate, StoreNo1)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoOut SET ListDate = :m_ListDate, StoreNo1 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END

  IF (:P_UnitId = 1) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoOut(GoodsIntNo, CmpyName,  ListDate, StoreNo2)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoOut SET ListDate = :m_ListDate, StoreNo2 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END

  IF (:P_UnitId = 2) THEN
  BEGIN
    IF (m_reccount = 0) THEN
      INSERT INTO StoreNoOut(GoodsIntNo, CmpyName,  ListDate, StoreNo3)
        VALUES (:p_GoodsIntNo, :m_CmpyName, :m_ListDate, :p_StoreNo);
    ELSE
      UPDATE StoreNoOut SET ListDate = :m_ListDate, StoreNo3 = :p_StoreNo
        WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
  END
END
--
2005.03.29_01/SQL4
/*插入后判断是否更新店内码*/
CREATE TRIGGER BUYGDS_AFT_INS_StoreNoIN FOR BUYGDS
ACTIVE AFTER INSERT POSITION 1
AS
BEGIN
  /*插入时先判断是否全部为空, 有内容继续处理*/
  IF ( (GEN_ID(IntNo_UseStoreNoIn, 0) <> 0)
    AND (NOT ((new.StoreNo = '') OR (new.StoreNo IS NULL)) ) ) THEN
  BEGIN
    EXECUTE PROCEDURE StoreNoIN_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 0);
  END
END
---
/*修改时判断是否更新店内码*/
CREATE TRIGGER BUYGDS_AFT_UPD_StoreNoIN FOR BUYGDS
ACTIVE AFTER UPDATE POSITION 1
AS
BEGIN
  IF ((GEN_ID(IntNo_UseStoreNoIn, 0) <> 0)
    AND (GEN_ID(INTNO_DataInit, 0) <> 1)
    ) THEN   /*启用店内码就继续*/
  BEGIN
    IF ((new.GoodsIntNo <> old.GoodsIntNo) or (new.StoreNo <> old.StoreNo) or (new.UnitId <> old.UnitId)) THEN
    BEGIN
      EXECUTE PROCEDURE StoreNoIN_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 0);
    END
  END
END
---
/*插入后判断是否更新店内码*/
CREATE TRIGGER BUYGDSBK_AFT_INS_StoreNoIN FOR BUYGDSBK
ACTIVE AFTER INSERT POSITION 1
AS
BEGIN
  /*插入时先判断是否全部为空, 有内容继续处理*/
  IF ((GEN_ID(IntNo_UseStoreNoIn, 0) <> 0) 
    AND (NOT ((new.StoreNo = '') OR (new.StoreNo IS NULL)) ) ) THEN
  BEGIN
    EXECUTE PROCEDURE StoreNoIN_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 1);
  END
END
---
/*修改时判断是否更新店内码*/
CREATE TRIGGER BUYGDSBK_AFT_UPD_StoreNoIN FOR BUYGDSBK
ACTIVE AFTER UPDATE POSITION 1
AS
BEGIN
  IF ((GEN_ID(IntNo_UseStoreNoIn, 0) <> 0) AND (GEN_ID(INTNO_DataInit, 0) <> 1)
    ) THEN   /*启用店内码就继续*/
  BEGIN
    IF ((new.GoodsIntNo <> old.GoodsIntNo) or (new.StoreNo <> old.StoreNo) or (new.UnitId <> old.UnitId)) THEN
    BEGIN
      EXECUTE PROCEDURE StoreNoIN_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 1);
    END
  END
END
---
/*插入后判断是否更新店内码*/
CREATE TRIGGER SALEGDS_AFT_INS_StoreNoOUT FOR SaleGds
ACTIVE AFTER INSERT POSITION 1
AS
BEGIN
  /*插入时先判断是否全部为空, 有内容继续处理*/
  IF ((GEN_ID(IntNo_UseStoreNoOut, 0) <> 0) 
    AND (NOT ((new.StoreNo = '') OR (new.StoreNo IS NULL)) ) ) THEN
  BEGIN
    EXECUTE PROCEDURE StoreNoOut_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 0);
  END
END
---
/*修改时判断是否更新店内码*/
CREATE TRIGGER SALEGDS_AFT_UPD_StoreNoOUT FOR SaleGds
ACTIVE AFTER UPDATE POSITION 1
AS
BEGIN
  IF ((GEN_ID(IntNo_UseStoreNoOut, 0) <> 0) AND (GEN_ID(INTNO_DataInit, 0) <> 1)
    ) THEN   /*启用店内码就继续*/
  BEGIN
    IF ((new.GoodsIntNo <> old.GoodsIntNo) or (new.StoreNo <> old.StoreNo) or (new.UnitId <> old.UnitId)) THEN
    BEGIN
      EXECUTE PROCEDURE StoreNoOut_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 0);
    END
  END
END
---
/*插入后判断是否更新店内码*/
CREATE TRIGGER SALEGDSBK_AFT_INS_StoreNoOUT FOR SaleGdsBK
ACTIVE AFTER INSERT POSITION 1
AS
BEGIN
  /*插入时先判断是否全部为空, 有内容继续处理*/
  IF ((GEN_ID(IntNo_UseStoreNoOut, 0) <> 0) 
    AND (NOT ((new.StoreNo = '') OR (new.StoreNo IS NULL)) ) ) THEN
  BEGIN
    EXECUTE PROCEDURE StoreNoOut_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 1);
  END
END
---
/*修改时判断是否更新店内码*/
CREATE TRIGGER SALEGDSBK_AFT_UPD_StoreNoOUT FOR SaleGdsBK
ACTIVE AFTER UPDATE POSITION 1
AS
BEGIN
  IF ((GEN_ID(IntNo_UseStoreNoOut, 0) <> 0) AND (GEN_ID(INTNO_DataInit, 0) <> 1)
    ) THEN   /*启用店内码就继续*/
  BEGIN
    IF ((new.GoodsIntNo <> old.GoodsIntNo) or (new.StoreNo <> old.StoreNo) or (new.UnitId <> old.UnitId)) THEN
    BEGIN
      EXECUTE PROCEDURE StoreNoOut_UPDATE(new.MainIntNo, new.GoodsIntNo, new.StoreNo, new.UnitId, 1);
    END
  END
END
---
/*检查某店内码是否存在于进货明细中, 用于删除店内码表记录的提示*/
CREATE PROCEDURE Check_BuyGds_Use_StoreNo(
  P_StoreNo1 VarChar(80),
  P_StoreNo2 VarChar(80),
  P_StoreNo3 VarChar(80)
)
RETURNS(r_Count Integer)
AS
BEGIN
  r_Count = 0;
  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo1 = '') OR (:P_StoreNo1 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGds WHERE (StoreNo = :P_StoreNo1) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo2 = '') OR (:P_StoreNo2 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGds WHERE (StoreNo = :P_StoreNo2) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo3 = '') OR (:P_StoreNo3 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGds WHERE (StoreNo = :P_StoreNo3) INTO :r_Count;
  SUSPEND;
END
---
/*检查某店内码是否存在于进货明细中, 用于删除店内码表记录的提示*/
CREATE PROCEDURE Check_BuyGdsBk_Use_StoreNo(
  P_StoreNo1 VarChar(80),
  P_StoreNo2 VarChar(80),
  P_StoreNo3 VarChar(80)
)
RETURNS(r_Count Integer)
AS
BEGIN
  r_Count = 0;
  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo1 = '') OR (:P_StoreNo1 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGdsBk WHERE (StoreNo = :P_StoreNo1) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo2 = '') OR (:P_StoreNo2 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGdsBk WHERE (StoreNo = :P_StoreNo2) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo3 = '') OR (:P_StoreNo3 IS NULL)))) THEN
    SELECT COUNT(*) FROM BuyGdsBk WHERE (StoreNo = :P_StoreNo3) INTO :r_Count;
  SUSPEND;
END
---
/*检查某店内码是否存在于进货明细中, 用于删除店内码表记录的提示*/
CREATE PROCEDURE Check_SaleGds_Use_StoreNo(
  P_StoreNo1 VarChar(80),
  P_StoreNo2 VarChar(80),
  P_StoreNo3 VarChar(80)
)
RETURNS(r_Count Integer)
AS
BEGIN
  r_Count = 0;
  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo1 = '') OR (:P_StoreNo1 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGds WHERE (StoreNo = :P_StoreNo1) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo2 = '') OR (:P_StoreNo2 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGds WHERE (StoreNo = :P_StoreNo2) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo3 = '') OR (:P_StoreNo3 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGds WHERE (StoreNo = :P_StoreNo3) INTO :r_Count;
  SUSPEND;
END
---
/*检查某店内码是否存在于进货明细中, 用于删除店内码表记录的提示*/
CREATE PROCEDURE Check_SaleGdsBk_Use_StoreNo(
  P_StoreNo1 VarChar(80),
  P_StoreNo2 VarChar(80),
  P_StoreNo3 VarChar(80)
)
RETURNS(r_Count Integer)
AS
BEGIN
  r_Count = 0;
  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo1 = '') OR (:P_StoreNo1 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGdsBk WHERE (StoreNo = :P_StoreNo1) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo2 = '') OR (:P_StoreNo2 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGdsBk WHERE (StoreNo = :P_StoreNo2) INTO :r_Count;

  IF ((:r_Count = 0) AND (NOT ((:P_StoreNo3 = '') OR (:P_StoreNo3 IS NULL)))) THEN
    SELECT COUNT(*) FROM SaleGdsBk WHERE (StoreNo = :P_StoreNo3) INTO :r_Count;
  SUSPEND;
END
---
CREATE PROCEDURE Get_StoreNoIn(
  P_CmpyName VARCHAR(12),
  P_GoodsIntNo Integer
)
RETURNS(r_StoreNo1 VarChar(80), r_StoreNo2 VarChar(80), r_StoreNo3 VarChar(80))
AS
BEGIN
  r_StoreNo1 = ''; r_StoreNo2 = ''; r_StoreNo3 = '';
  IF (GEN_ID(INTNO_UseStoreNoIN, 0) = 1) THEN
  BEGIN
    SELECT StoreNo1, StoreNo2, StoreNo3 FROM StoreNoIn
      WHERE (CmpyName = :P_CmpyName) AND (GoodsIntNo = :P_GoodsIntNo)
      INTO :r_StoreNo1, :r_StoreNo2, :r_StoreNo3;
  END
  SUSPEND;
END
---
CREATE PROCEDURE Get_StoreNoOut(
  P_CmpyName VARCHAR(12),
  P_GoodsIntNo Integer
)
RETURNS(r_StoreNo1 VarChar(80), r_StoreNo2 VarChar(80), r_StoreNo3 VarChar(80))
AS
BEGIN
  r_StoreNo1 = ''; r_StoreNo2 = ''; r_StoreNo3 = '';
  IF (GEN_ID(INTNO_UseStoreNoOut, 0) = 1) THEN
  BEGIN
    SELECT StoreNo1, StoreNo2, StoreNo3 FROM StoreNoOut
      WHERE (CmpyName = :P_CmpyName) AND (GoodsIntNo = :P_GoodsIntNo)
      INTO :r_StoreNo1, :r_StoreNo2, :r_StoreNo3;
  END
  SUSPEND;
END
---
/*公共部分*/
/*货号被删除时, 删除相应的记录*/
CREATE TRIGGER STKLST_BEFORE_DEL_DEL_DETAIL FOR STKLST BEFORE DELETE POSITION 10 AS
BEGIN
  DELETE FROM STKLSTS WHERE GoodsIntNo = old.intno;
  DELETE FROM MGOODSNO WHERE GoodsIntNo = old.intno;
  DELETE FROM PRICEIN WHERE GoodsIntNo = old.intno;
  DELETE FROM PRICEOUT WHERE GoodsIntNo = old.intno;
  DELETE FROM StoreNoIn WHERE GoodsIntNo = old.intno;
  DELETE FROM StoreNoOut WHERE GoodsIntNo = old.intno;
END
---
/*客户简称被更改时现新店内码跟踪中的客户简称*/
CREATE TRIGGER CLIENT_AFT_UPD_SET_StoreNo FOR CLIENT AFTER UPDATE POSITION 2 AS
BEGIN
  IF ((OLD.CmpyName <> NEW.CmpyName) AND (GEN_ID(IntNo_UseStoreNoIn, 0) = 1))THEN
  BEGIN
    IF (NEW.ClientFlag = 0) THEN
    BEGIN
      UPDATE StoreNoOut SET CmpyName = NEW.CmpyName WHERE (CmpyName = OLD.CmpyName);
    END
    IF (NEW.ClientFlag = 1) THEN
    BEGIN
      UPDATE StoreNoIn SET CmpyName = NEW.CmpyName WHERE (CmpyName = OLD.CmpyName);
    END
  END
END
---
ALTER PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  GOODSINTNO INTEGER,
  UNITID INTEGER,
  STOCKINTNO INTEGER,
  BANKINTNO INTEGER,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80),
  ISSPECPRICE SMALLINT,
  StoreNo VARCHAR(80)
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
BEGIN
  FOR SELECT
    d.GoodsIntNo,
    d.UnitId,
    d.StockIntNo,
    d.BankIntNo,
    d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,
    d.AMOUNT,
    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5,
    d.isSpecPrice,
    d.StoreNo
    FROM Salegds d, StkName, BankName
    WHERE StkName.IntNo = d.StockIntNo and d.BankIntNo = BankName.IntNo
    INTO :GoodsIntNo, :UnitId, :StockIntNo, :BankIntNo,
      :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
      :isSpecPrice, :StoreNo
    DO
    BEGIN
      IF (GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN
        SELECT GoodsNo FROM Stklst Where IntNo = :GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo = :GoodsIntNo and u.UnitId = :UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN
        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
--
2005.03.20_01/UPGRADE_DETAIL
1. 新增: 入库主/明细表;
--
2005.03.20_01/SQL1
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('SumAmountStkIn')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('SumAmountStkIn', '', '总金额', 'TSumAmount')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'STKINLIST' AND UPPER(FIELDNAME) = Upper('GdsPay')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QtyStkIn')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('AmountStkIn')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('QtyStkIn', '', '入库数量', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('AmountStkIn', '', '入库金额', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('GdsPay', 'STKINLIST', '金额', 'TAmount')
---
/*第负一步, 库存商品明细及进销存数量统计表*/
/*共八个存储过程GETSTOCKIO, GETSTOCKIOEX, GETSTOCKIOEXB*/
/*共八个存储过程GETSTOCKIOLIST, GETSTOCKIOLISTEXB, GETSTOCKIOLIST*/
/*共八个存储过程GET_PAN_FROM_REAL, GET_PAN_FROM_REAL_EXB*/
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS

BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOSUM (
  P_DATEFROM TIMESTAMP,

  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),

  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
/*第零步 检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc VARChar(1);
BEGIN
  SUSPEND;
END
---
/*第一步， 检查单号是否存在*/
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  SUSPEND;
END
---
/*第二步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  SUSPEND;
END
---
/*第三步， 处理先入先出法的问题*/
ALTER PROCEDURE LIST_SET_FIFO_INPRICE (

  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(15),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  SUSPEND;
END
---
/*第四步， 进出仓时， 相应的库存量变化*/
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN
  SUSPEND;
END
---
/*第四-1步， 生成子*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2005.03.20_01/SQL2
/*第五步，处理生成子*/
CREATE GENERATOR INTNO_STKINGDS
---
SET GENERATOR INTNO_STKINGDS TO 10
---
CREATE GENERATOR INTNO_STKINLIST
---
SET GENERATOR INTNO_STKINLIST TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINLIST')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKINGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKINGDS, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
---
/*第六步， 新建出库表及相关的索引字段等*/
CREATE TABLE STKINLIST (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  COMMENT TCOMMENT,
  GDSPAY TAMOUNT,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  LREMARK2 VARCHAR(120),
  GOODSGDSPAY	NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  CONSTRAINT STKINList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT STKINList_UNIQUE_1 UNIQUE (LISTNO)
)
---
/*建立主表索引*/
CREATE INDEX IDX_STKINLIST_LISTDATE ON STKINLIST(LISTDATE);
---
/*第七步  建出库明细表及主键约束*/
CREATE TABLE STKINGDS(
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO INTEGER,
  UNITID TGOODSUNITID,
  STOCKINTNO TINTNO,
  QTY TQTY,
  PRICE TPRICE,
  DISCOUNT TDISCOUNT,
  AMOUNT TAMOUNT,
  REMARK TREMARK,
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  TAXRATE	NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  GOODSAMOUNT	NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  TAXPAYMENT	NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  CONSTRAINT STKINGDS_PRIMARY_KEY PRIMARY KEY (INTNO),

  CONSTRAINT FK_STKINGDS_STKINLIST FOREIGN KEY (MAININTNO) REFERENCES STKINLIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,

  CONSTRAINT FK_STKINGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE,

  CONSTRAINT FK_STKINGDS_STKNAME FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO)
    ON UPDATE CASCADE ON DELETE SET NULL
)
---
/*建立明细表索引*/
CREATE INDEX IDX_STKINGDS_MAININTNO ON STKINGDS(MAININTNO);
--
2005.03.20_01/SQL3
/*第八步建立主表触发， 用于新增记录的初始化*/
CREATE TRIGGER STKINList_BEF_INS_SET_DEF FOR STKINList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
---
CREATE TRIGGER STKINList_BEF_UPD_SET_DEF FOR STKINList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
---
/*第九步， 建立主表的相应触发 新增记录时, 不允许UpdateAcc标志为(*)*/
CREATE TRIGGER STKINLIST_BEF_INS_UPDATEACC FOR STKINLIST
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (new.UpdateAcc = '*') THEN
    EXCEPTION CANNOT_SET_UPDATEACC_WHILE_NEW;
END
---
/*检查单号是否存在, 如存在, 则激活Exception*/
CREATE TRIGGER STKINLIST_BEF_INS_CHK_LISTNO FOR STKINLIST
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (23, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
/*同时修改进出仓标志及日期的提示*/
CREATE TRIGGER STKINLIST_BEF_UPD_CHK_UPACC FOR STKINLIST
ACTIVE BEFORE UPDATE POSITION 0
 AS
BEGIN
  IF ((old.UPDATEACC <> new.UpdateAcc) and (old.LISTDATE <> new.LISTDATE)) THEN
    EXCEPTION CANNOT_CHANGE_BOTH_ACC_LISTDATE;
END
---
/*进出仓时， 检查货号是否已结清*/
CREATE TRIGGER STKINLIST_BEF_UPD_UPDATEACC FOR STKINLIST
ACTIVE BEFORE UPDATE POSITION 3
AS DECLARE VARIABLE m_Changeable SMALLINT;
BEGIN
  IF (new.UPDATEACC <> old.UPDATEACC) THEN
    EXECUTE PROCEDURE LIST_CHECK_STK_CHANGEABLE ('STKINLIST', new.IntNo, old.ListDate, 1)
      RETURNING_VALUES :m_Changeable;
END
---
/*若当前启用先入先出法， 则处理相关的单价， 金额*/
CREATE TRIGGER STKINLIST_BEF_UPD_SETINPRICE FOR STKINLIST
ACTIVE BEFORE UPDATE POSITION 3
AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_Amount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'STKINGDS', old.IntNo);
      SELECT SUM(Amount) FROM STKINgds WHERE MainIntNo = old.IntNo INTO :m_Amount;
      IF (m_Amount IS NULL) THEN m_Amount = 0;
      new.Gdspay = m_Amount;
    END
  END
END
---
/*执行进仓/取消进仓的操作,  处理相关的库存量*/
CREATE TRIGGER STKINLIST_AFT_UPD_UPDATEACC FOR STKINLIST
ACTIVE AFTER UPDATE POSITION 2
AS
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'STKINGDS', old.IntNo);
    END
  END
END
---
/*如果单据已出仓, 则不允许删除(除非正在做帐务清除)*/
CREATE TRIGGER STKINLIST_BEF_DEL_CHK_UPACC FOR STKINLIST
ACTIVE BEFORE DELETE POSITION 2
AS
BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_OUT_CANNOT_DELETE;
END
---
/*主表被删除， 明细表自动被删除*/
CREATE TRIGGER STKINLIST_BEF_DEL_DEL_DETAIL FOR STKINLIST
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM STKINGDS WHERE MAININTNO = old.intno;
END
--
2005.03.20_01/SQL4
/* 检查是否已出仓, 如果已出仓, 则不允许新增 */
CREATE TRIGGER STKINGDS_BEF_INS_CHK_UPACC FOR STKINGDS
ACTIVE BEFORE INSERT POSITION 1
AS DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('STKINLIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/* 检查是否已出仓, 如果已出仓, 则不允许修改 */
CREATE TRIGGER STKINGDS_BEF_UPD_CHK_UPACC FOR STKINGDS
ACTIVE BEFORE UPDATE POSITION 0
AS DECLARE
VARIABLE m_Changeable SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (not ((new.GoodsIntNo is NULL) and (not (old.GoodsIntNo is NULL))
      and (new.Qty=old.Qty) and (new.Price=old.Price)
      and (new.Discount=old.Discount) ))
      THEN
        EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('STKINLIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
  END
END
---
/* 检查是否已出仓, 如果已出仓, 则不允许删除 */
CREATE TRIGGER STKINGDS_BEF_DEL_CHK_UPACC FOR STKINGDS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('STKINLIST', old.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/*更新主表的货款  存储过程*/
CREATE PROCEDURE UPDATE_STKINLIST_AMOUNT
(
  P_GDSPAY NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)
AS
BEGIN
  UPDATE STKINLIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
/*更新主表的货款 -插入后-  触发器*/
CREATE TRIGGER STKINGDS_AFT_INS_SET_MASTER FOR STKINGDS
ACTIVE AFTER INSERT POSITION 0
AS
BEGIN
  EXECUTE PROCEDURE UPDATE_STKINLIST_AMOUNT(new.AMOUNT, new.GoodsAmount, 1, new.MAININTNO);
END
---
/*更新主表的货款 -修改后-  触发器*/
CREATE TRIGGER STKINGDS_AFT_UPD_SET_MASTER FOR STKINGDS
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.AMOUNT - old.AMOUNT;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_STKINLIST_AMOUNT(:m_amount, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
/*更新主表的货款 -删除后-  触发器*/
CREATE TRIGGER STKINGDS_AFT_DEL_SET_MASTER FOR STKINGDS
ACTIVE AFTER DELETE POSITION 0
AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_STKINLIST_AMOUNT(old.AMOUNT, old.GoodsAmount, -1, old.MAININTNO);

  END
END
--
2005.03.20_01/SQL5
/*第零步, 检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc VARChar(1);
BEGIN
  r_Changeable = 1; IF (GEN_ID(INTNO_DataInit, 0) = 1) THEN EXIT;
  IF (p_ListName = 'INLIST') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OUTLIST') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'MOVELIST') THEN
    SELECT UpdateAcc From MOVELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PANLIST') THEN
    SELECT UpdateAcc From PANLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'REALLIST') THEN
    SELECT UpdateAcc From REALLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'ASSLIST') THEN
    SELECT UpdateAcc From ASSLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'USELIST') THEN
    SELECT UpdateAcc From USELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'STKINLIST') THEN
    SELECT UpdateAcc From STKINLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (m_UpdateAcc = '*') THEN R_CHANGEABLE = 0;
  ELSE R_CHANGEABLE = 1;
  IF (R_CHANGEABLE = 0) THEN IF (P_RAISEEXCEPTION = 1) THEN EXCEPTION MSG_MASTER_UPDATEACC_LOCK;
END

---
/* 第一步，检查单号是否存在 以及修改相应的存储过程 */
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN

    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 18) THEN  /* 时段促销 */
  BEGIN
    SELECT COUNT(*) FROM SpecList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 20) THEN  /* 出库 */
  BEGIN
    SELECT COUNT(*) FROM USEList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 23) THEN  /* 入库 */
  BEGIN
    SELECT COUNT(*) FROM STKINList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
---
/*第二步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  IF (TBLNAME = 'INLIST') THEN

  BEGIN
    FOR SELECT GOODSINTNO FROM BUYGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'OUTLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM SALEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'MOVELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM MOVEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'PANLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM PANGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'ASSLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM ASSGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'USELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM USEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'STKINLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM STKINGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  SUSPEND;
END
---
/*第三步， 处理先入先出法的成本问题*/
ALTER PROCEDURE LIST_SET_FIFO_INPRICE (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(15),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  IF (NOT (p_AddFlag = 1)) THEN                       /* 先进先出时, 不允许取消进出仓 */
    EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC;

  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE SALEGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5

      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE PANGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE ASSGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)

          INTO :m_InPrice;
        UPDATE BUYGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'USEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM USEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE USEGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'STKINGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM STKINGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE STKINGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END
END
---
/*第四步， 进出仓时， 相应的库存量变化*/
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN

  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'USEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM USEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'STKINGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM STKINGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
END
--
2005.03.20_01/SQL6
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),

  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  /* 发货 */
  FOR
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 7;
  /* 差额盘点 */
  FOR
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 12;
  /* 组装与拆解 */
  FOR
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      d.InAmount as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END


  WorkMode = 3;
  /* 调拨入 */
  FOR
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 3;
  /* 调拨出 */
  FOR
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 1;
  /* 进货 */
  FOR
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 20;
  /* 出库 */
  FOR
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, Amount as AmountUse, 0.0 as AmountStkIn
      FROM UseGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;

  WorkMode = 23;
  /* 入库 */
  FOR
    SELECT M.IntNo From STKINLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, u.Factor*d.Qty as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, Amount as AmountStkIn
      FROM STKINGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
      DO SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),

  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountUse,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,

          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT, 0.0 as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 20;
  FOR  /* 出库 */
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM USEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;

  WorkMode = 23;
  FOR  /* 入库 */
    SELECT M.IntNo From STKINLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, u.Factor*d.Qty as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse, 0.0 as AmountStkIn,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5, d.BatchNo
      FROM STKINGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse, :AmountStkIn,
          :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :BatchNo
      DO SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      FROM BUYGDS d, InList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      FROM SALEGDS d, Outlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM PANGDS d, PanList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM ASSGDS d, AssList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      FROM USEGDS d, Uselist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 入库 */
    SELECT 23, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      FROM STKINGDS d, STKINlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d, Inlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM USEGDS d, USELIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 入库 */
    SELECT 23, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM STKINGDS d, STKINLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
  DO
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---

ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),

  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(120),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d, INLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM USEGDS d, USELIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 入库 */
    SELECT 23, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM STKINGDS d, STKINLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
      :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
  BEGIN
    IF (BatchNo = '') THEN BatchNo = NULL;
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOSUM (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  QTYSTKIN NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  AMOUNTSTKIN NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR
    Select GoodsIntNo, StockIntNo,
      Sum(QtyIn)-Sum(QtyOut)+Sum(QtyMoveIn)-Sum(QtyMoveOut)+Sum(QtyPan)+
      Sum(QtyAssIn-QtyAssOut)-Sum(QtyUse)+Sum(QtyStkIn) as QInitRemain,
      Sum(AmountIn)-Sum(AmountOut)+Sum(AmountMoveIn)-Sum(AmountMoveOut)+Sum(AmountPan)+
      Sum(AmountAssIn-AmountAssOut)-Sum(AmountUse)+Sum(AmountStkIn) as InitAmount,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse, 0.0 as QtyStkIn,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse, 0.0 as AmountStkIn
      From GetstockIolist('0001.1.1', :p_DateFrom-1)
      Group by GoodsIntNo, StockIntNo
    UNION ALL
    Select GoodsIntNo, StockIntNo,
      0.0 as QInitRemain,
      0.0 as InitAmount,
      Sum(QtyIn) as QtyIn, Sum(QtyOut) as QtyOut, Sum(QtyMoveIn) as QtyMoveIn,
      Sum(QtyMoveOut) as QtyMoveOut, Sum(QtyPan) as QtyPan,
      Sum(QtyAssIn) as QtyAssIn, Sum(QtyAssOut) as QtyAssOut, Sum(QtyUse) as QtyUse, Sum(QtyStkIn) as QtyStkIn,
      Sum(AmountIn) as AmountIn, Sum(AmountOut) as AmountOut, Sum(AmountMoveIn) as AmountMoveIn,
      Sum(AmountMoveOut) as AmountMoveOut, Sum(AmountPan) as AmountPan,
      Sum(AmountAssIn) as AmountAssIn, Sum(AmountAssOut) as AmountAssOut,
      Sum(AmountUse) as AmountUse, Sum(AmountStkIn) as AmountStkIn
      From GetstockIolist(:p_DateFrom, :p_DateTo)
      Group by GoodsIntNo, StockIntNo
        INTO :GoodsIntNo, :StockIntNo, :QInitRemain, :InitAmount,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, :QtyStkIn,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse, :AmountStkIn
    DO SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut) as InAmount
    FROM GETSTOCKIOLIST(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
    FROM Stklsts d
    GROUP BY d.GoodsIntNo, d.StockIntNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse+d.QtyStkIn) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse+d.AmountStkIn) as InAmount
    FROM GETSTOCKIOLIST(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
    FROM Stklsts d
    GROUP BY d.GoodsIntNo, d.StockIntNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS
BEGIN
FOR
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse+d.QtyStkIn) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse+d.AmountStkIn) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
          ,:ManuFactDate_Int, :ExpireDate_Int
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
---
/*货号被删除时, 将货号代入备注*/
CREATE TRIGGER STKLST_BEFORE_DEL_SAVE_GOODSNO FOR STKLST BEFORE DELETE POSITION 10 AS
BEGIN
  UPDATE SALEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE SALEGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE MOVEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE PANGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE REALGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE ASSGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE USEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE STKINGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
END
--
2005.02.19_01/UPGRADE_DETAIL
1. 修改类别表, 用于记录该类别的最后一个货号;
2. 新增存储过程, 用于获取该类别的最后一个货号;
3. 售价跟踪：开始支持前台售价跟踪。
--
2005.02.19_01/SQL1
CREATE TRIGGER STKLSTSX_BEF_INS_CHECKQTY FOR STKLSTSX 
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (GEN_ID(INTNO_NEGQTY, 0) = 1) THEN
  BEGIN
    IF (ROUND(NEW.QREMAIN, 4) < 0) then EXCEPTION MSG_QREMAIN_NOT_NEGATIVE;
  END
END
---
CREATE TRIGGER STKLSTSX_BEF_UPD_CHECKQTY FOR STKLSTSX
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF (GEN_ID(INTNO_NEGQTY, 0) = 1) THEN
  BEGIN
    IF (ROUND(NEW.QREMAIN, 4) < 0) then EXCEPTION MSG_QREMAIN_NOT_NEGATIVE;
  END
END
---
ALTER TABLE TypeDefine
  ADD MaxGoodsNo INTEGER DEFAULT 0
--
2005.02.19_01/SQL2
/*新建2个存储过程, 第一个用于获得最大一个货号; 第二个用于检查货号是否已经存在*/
CREATE PROCEDURE GetMaxGoodsNo(p_TypeIntNo INTEGER)
  RETURNS(RMaxGoodsNo INTEGER)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  SELECT IntNo, MaxGoodsNo FROM TypeDefine WHERE (IntNo = :p_TypeIntNo)
    INTO :m_GoodsIntNo, :RMaxGoodsNo;
  IF (RMaxGoodsNo IS NULL) THEN RMaxGoodsNo = 0;
  RMaxGoodsNo = :RMaxGoodsNo + 1;

  /* 只有找到了记录，才需要更新 */
  IF (m_GoodsIntNo IS NOT NULL) THEN
    UPDATE TypeDefine SET MaxGoodsNo = :RMaxGoodsNo WHERE (IntNo = :p_TypeIntNo);
  SUSPEND;
END
---
CREATE PROCEDURE CheckMaxGoodsNoExists(p_GoodsNo VARCHAR(20))
  RETURNS(RGoodsNoCount INTEGER)
AS
BEGIN
  RGoodsNoCount = 0;
  SELECT COUNT(*) FROM Stklst WHERE (GoodsNo = :p_GoodsNo) INTO :RGoodsNoCount;
  SUSPEND;
END
---
ALTER TABLE PriceOut ADD InsertUct Integer
---
ALTER TABLE PriceOut ADD UpdateUct Integer
---
ALTER TABLE PriceIn ADD InsertUct Integer
---
ALTER TABLE PriceIn ADD UpdateUct Integer
---
UPDATE PriceOut SET InsertUCT = zs_GetUnixTime();
---
UPDATE PriceIn SET InsertUCT = zs_GetUnixTime();
---
ALTER TABLE PriceOut ADD KeyValue COMPUTED BY (GoodsIntNo || '+-*/' || ALLTRIM(CmpyName))
---
ALTER TABLE PriceIn ADD KeyValue COMPUTED BY (GoodsIntNo || '+-*/' || ALLTRIM(CmpyName))
---
CREATE TRIGGER PriceOut_BEFORE_INSERT_EVENTLOG FOR PriceOut BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();

END
---
CREATE TRIGGER PriceOut_BEFORE_UPDATE_EVENTLOG FOR PriceOut BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PriceOut_AFTER_DELETE_EVENT_LOG FOR PriceOut AFTER DELETE POSITION 1 
AS
DECLARE VARIABLE m_KeyValue VARCHAR(255);
BEGIN
  m_KeyValue = old.KeyValue;
  EXECUTE PROCEDURE SYS_LOGDELETE('PriceOut', m_KeyValue);
END
---
CREATE TRIGGER PriceIn_BEFORE_INSERT_EVENTLOG FOR PriceIn BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PriceIn_BEFORE_UPDATE_EVENTLOG FOR PriceIn BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER PriceIn_AFTER_DELETE_EVENT_LOG FOR PriceIn AFTER DELETE POSITION 1
AS
DECLARE VARIABLE m_KeyValue VARCHAR(255);
BEGIN
  m_KeyValue = old.KeyValue;
  EXECUTE PROCEDURE SYS_LOGDELETE('PriceIn', m_KeyValue);
END
---
CREATE TRIGGER CLIENT_AFT_UPD_SET_PRICEOUT_IN FOR Client AFTER UPDATE POSITION 2
AS
BEGIN
  /*客户简称被更改时现新价格跟踪中的客户简称*/
  /*欠一个条件, 系统选项中是否使用价格跟踪*/
  IF ((OLD.CmpyName <> NEW.CmpyName) AND (NEW.PriceTrace = 1)) THEN
  BEGIN
    IF (NEW.ClientFlag = 0) THEN
    BEGIN
      UPDATE PriceOut SET CmpyName = NEW.CmpyName WHERE (CmpyName = OLD.CmpyName);
    END
    IF (NEW.ClientFlag = 1) THEN
    BEGIN
      UPDATE PriceIn SET CmpyName = NEW.CmpyName WHERE (CmpyName = OLD.CmpyName);
    END
  END
END
--
2005.01.20_01/UPGRADE_DETAIL
1. 新增表: 库存量警示主/明细表；
2. 库存清单：新增字段[近供应商2～5]；
3. 自定义组：新增[删除触发器]用于数据同步；
4. 仓库名称：新增字段[警示表号]。
--
2005.01.20_01/SQL1
/* 近供应商被更新时, 处理供应商2-5 */
ALTER PROCEDURE INLIST_UPDATE_PRICEUPIN
(
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_CMPYNAME VARCHAR(12)
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_Discount Dec(15,4);
DECLARE VARIABLE m_AssignResult SmallInt;
DECLARE VARIABLE m_CompanyUpIn CHAR(12);
BEGIN
  SUSPEND;
END
--
2005.01.20_01/SQL2
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('CurrentPoint')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CurrentPoint', '', '当前积分', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('CompanyUpIn2')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('CompanyUpIn3')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('CompanyUpIn4')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('CompanyUpIn5')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('PriceUpIn2')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('PriceUpIn3')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('PriceUpIn4')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('PriceUpIn5')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('DateUpIn2')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('DateUpIn3')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('DateUpIn4')
---
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('DateUpIn5')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CompanyUpIn2', '', '近供应商2', 'TCmpyName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CompanyUpIn3', '', '近供应商3', 'TCmpyName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CompanyUpIn4', '', '近供应商4', 'TCmpyName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CompanyUpIn5', '', '近供应商5', 'TCmpyName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PriceUpIn2', '', '近进价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PriceUpIn3', '', '近进价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PriceUpIn4', '', '近进价4', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PriceUpIn5', '', '近进价5', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('DateUpIn2', '', '近进货日2', 'TDate')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('DateUpIn3', '', '近进货日3', 'TDate')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('DateUpIn4', '', '近进货日4', 'TDate')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('DateUpIn5', '', '近进货日5', 'TDate')
---
ALTER TABLE Stklst
  Add CompanyUpIn2 TCMPYNAME,
  Add CompanyUpIn3 TCMPYNAME,
  Add CompanyUpIn4 TCMPYNAME,
  Add CompanyUpIn5 TCMPYNAME,
  Add PriceUpIn2   TPrice,
  Add PriceUpIn3   TPrice,
  Add PriceUpIn4   TPrice,
  Add PriceUpIn5   TPrice,
  Add DateUpIn2    TDate,
  Add DateUpIn3    TDate,
  Add DateUpIn4    TDate,
  Add DateUpIn5    TDate
--
2005.01.20_01/SQL3
/*新增时, 初始化 设置默认值*/
CREATE TRIGGER STKLST_BEF_INS_SET_DEF_4 FOR STKLST BEFORE INSERT POSITION 0 AS
BEGIN
  IF (New.CompanyUpIn2 IS NULL) THEN New.CompanyUpIn2 = '';
  IF (New.CompanyUpIn3 IS NULL) THEN New.CompanyUpIn3 = '';
  IF (New.CompanyUpIn4 IS NULL) THEN New.CompanyUpIn4 = '';
  IF (New.CompanyUpIn5 IS NULL) THEN New.CompanyUpIn5 = '';
  IF (New.PriceUpIn2 IS NULL) THEN New.PriceUpIn2 = 0.00;
  IF (New.PriceUpIn3 IS NULL) THEN New.PriceUpIn3 = 0.00;
  IF (New.PriceUpIn4 IS NULL) THEN New.PriceUpIn4 = 0.00;
  IF (New.PriceUpIn5 IS NULL) THEN New.PriceUpIn5 = 0.00;
  IF (New.DateUpIn2 IS NULL) THEN New.DateUpIn2 = '1900.01.01';
  IF (New.DateUpIn3 IS NULL) THEN New.DateUpIn3 = '1900.01.01';
  IF (New.DateUpIn4 IS NULL) THEN New.DateUpIn4 = '1900.01.01';
  IF (New.DateUpIn5 IS NULL) THEN New.DateUpIn5 = '1900.01.01';
END
---
/*被修改时 设置默认值*/
CREATE TRIGGER STKLST_BEF_UPD_SET_DEF_4 FOR STKLST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (New.CompanyUpIn2 IS NULL) THEN New.CompanyUpIn2 = '';
  IF (New.CompanyUpIn3 IS NULL) THEN New.CompanyUpIn3 = '';
  IF (New.CompanyUpIn4 IS NULL) THEN New.CompanyUpIn4 = '';
  IF (New.CompanyUpIn5 IS NULL) THEN New.CompanyUpIn5 = '';
  IF (New.PriceUpIn2 IS NULL) THEN New.PriceUpIn2 = 0.00;
  IF (New.PriceUpIn3 IS NULL) THEN New.PriceUpIn3 = 0.00;
  IF (New.PriceUpIn4 IS NULL) THEN New.PriceUpIn4 = 0.00;
  IF (New.PriceUpIn5 IS NULL) THEN New.PriceUpIn5 = 0.00;
  IF (New.DateUpIn2 IS NULL) THEN New.DateUpIn2 = '1900.01.01';
  IF (New.DateUpIn3 IS NULL) THEN New.DateUpIn3 = '1900.01.01';
  IF (New.DateUpIn4 IS NULL) THEN New.DateUpIn4 = '1900.01.01';
  IF (New.DateUpIn5 IS NULL) THEN New.DateUpIn5 = '1900.01.01';
END
---
/* 近供应商被更新时, 处理供应商2-5 */
ALTER PROCEDURE INLIST_UPDATE_PRICEUPIN (
  P_MAININTNO INTEGER,
  P_LISTDATE DATE,
  P_CMPYNAME VARCHAR(12)
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_Discount Dec(15,4);

DECLARE VARIABLE m_MinDateUpIn TimeStamp;
DECLARE VARIABLE m_MinDateFlag SmallInt;
DECLARE VARIABLE m_MinFlag SmallInt;

DECLARE VARIABLE m_CompanyUpIn CHAR(12);
DECLARE VARIABLE m_CompanyUpIn2 CHAR(12);
DECLARE VARIABLE m_CompanyUpIn3 CHAR(12);
DECLARE VARIABLE m_CompanyUpIn4 CHAR(12);
DECLARE VARIABLE m_CompanyUpIn5 CHAR(12);

DECLARE VARIABLE m_DateUpIn TimeStamp;
DECLARE VARIABLE m_DateUpIn2 TimeStamp;
DECLARE VARIABLE m_DateUpIn3 TimeStamp;
DECLARE VARIABLE m_DateUpIn4 TimeStamp;
DECLARE VARIABLE m_DateUpIn5 TimeStamp;

DECLARE VARIABLE m_PriceUpIn Dec(15,4);
DECLARE VARIABLE m_PriceUpIn2 Dec(15,4);
DECLARE VARIABLE m_PriceUpIn3 Dec(15,4);
DECLARE VARIABLE m_PriceUpIn4 Dec(15,4);
DECLARE VARIABLE m_PriceUpIn5 Dec(15,4);
BEGIN
  IF (:p_ListDate < '1900.01.01') THEN EXIT; /*非法的日期值*/
  FOR SELECT GoodsIntNo, UnitId, Price, Discount FROM BUYGDS
    WHERE (MainIntNo = :p_MainIntNo) AND ( Qty > 0 )
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price, :m_Discount
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END

      SELECT CompanyUpIn, CompanyUpIn2, CompanyUpIn3, CompanyUpIn4, CompanyUpIn5,
        DateUpIn, DateUpIn2, DateUpIn3, DateUpIn4, DateUpIn5,
        PriceUpIn, PriceUpIn2, PriceUpIn3, PriceUpIn4, PriceUpIn5
        FROM Stklst WHERE (IntNo = :m_GoodsIntNo)
        INTO :m_CompanyUpIn, :m_CompanyUpIn2, :m_CompanyUpIn3, :m_CompanyUpIn4, :m_CompanyUpIn5,
          :m_DateUpIn, :m_DateUpIn2, :m_DateUpIn3, :m_DateUpIn4, :m_DateUpIn5,
          :m_PriceUpIn, :m_PriceUpIn2, :m_PriceUpIn3, :m_PriceUpIn4, :m_PriceUpIn5;

      IF (:m_CompanyUpIn IS NULL) THEN m_CompanyUpIn = '';
      IF (:m_CompanyUpIn2 IS NULL) THEN m_CompanyUpIn2 = '';
      IF (:m_CompanyUpIn3 IS NULL) THEN m_CompanyUpIn3 = '';
      IF (:m_CompanyUpIn4 IS NULL) THEN m_CompanyUpIn4 = '';
      IF (:m_CompanyUpIn5 IS NULL) THEN m_CompanyUpIn5 = '';

      IF (:m_DateUpIn IS NULL) THEN m_DateUpIn = '1900.01.01';
      IF (:m_DateUpIn2 IS NULL) THEN m_DateUpIn2 = '1900.01.01';
      IF (:m_DateUpIn3 IS NULL) THEN m_DateUpIn3 = '1900.01.01';
      IF (:m_DateUpIn4 IS NULL) THEN m_DateUpIn4 = '1900.01.01';
      IF (:m_DateUpIn5 IS NULL) THEN m_DateUpIn5 = '1900.01.01';

      IF (:m_PriceUpIn IS NULL) THEN m_PriceUpIn = 0.00;
      IF (:m_PriceUpIn2 IS NULL) THEN m_PriceUpIn2 = 0.00;
      IF (:m_PriceUpIn3 IS NULL) THEN m_PriceUpIn3 = 0.00;
      IF (:m_PriceUpIn4 IS NULL) THEN m_PriceUpIn4 = 0.00;
      IF (:m_PriceUpIn5 IS NULL) THEN m_PriceUpIn5 = 0.00;

      m_MinFlag = 0;                 /*初始化值为0, 表示没有更新点*/
      m_MinDateUpIn = '2999.12.31';  /*初始化为最大日期*/

      /*寻找更新点, 寻找方法:比较供应商名称,当空/相同+日期限制, 则应该是相对更新点*/
      IF ((:m_CompanyUpIn = '') OR ((:m_CompanyUpIn = :p_CmpyName) AND
        (:m_DateUpIn <= :p_ListDate))) THEN m_MinFlag = 1;
      IF ((:m_MinFlag = 0) AND ((:m_CompanyUpIn2 = '') OR ((:m_CompanyUpIn2 = :p_CmpyName) AND
        (:m_DateUpIn2 <= :p_ListDate)))) THEN m_MinFlag = 2;
      IF ((:m_MinFlag = 0) AND ((:m_CompanyUpIn3 = '') OR ((:m_CompanyUpIn3 = :p_CmpyName) AND
        (:m_DateUpIn3 <= :p_ListDate)))) THEN m_MinFlag = 3;
      IF ((:m_MinFlag = 0) AND ((:m_CompanyUpIn4 = '') OR ((:m_CompanyUpIn4 = :p_CmpyName) AND
        (:m_DateUpIn4 <= :p_ListDate)))) THEN m_MinFlag = 4;
      IF ((:m_MinFlag = 0) AND ((:m_CompanyUpIn5 = '') OR ((:m_CompanyUpIn5 = :p_CmpyName) AND
        (:m_DateUpIn5 <= :p_ListDate)))) THEN m_MinFlag = 5;

      IF (:m_MinFlag = 0) THEN /*找不到更新点,只有一种情况就是存在5个供应商,或是日期条件无法满足,*/
      BEGIN       /*如果是第六个,需要比较存在5个的日期谁老!*/
        IF ((:m_DateUpIn < :m_MinDateUpIn) AND (:m_DateUpIn <= :p_ListDate)) THEN
        BEGIN
          m_MinDateUpIn = :m_DateUpIn;
          m_MinFlag = 1;
        END
        IF ((:m_DateUpIn2 < :m_MinDateUpIn) AND (:m_DateUpIn2 <= :p_ListDate)) THEN
        BEGIN
          m_MinDateUpIn = :m_DateUpIn2;
          m_MinFlag = 2;
        END
        IF ((:m_DateUpIn3 < :m_MinDateUpIn) AND (:m_DateUpIn3 <= :p_ListDate)) THEN
        BEGIN
          m_MinDateUpIn = :m_DateUpIn3;
          m_MinFlag = 3;
        END
        IF ((:m_DateUpIn4 < :m_MinDateUpIn) AND (:m_DateUpIn4 <= :p_ListDate)) THEN
        BEGIN
          m_MinDateUpIn = :m_DateUpIn4;
          m_MinFlag = 4;
        END
        IF ((:m_DateUpIn5 < :m_MinDateUpIn) AND (:m_DateUpIn5 <= :p_ListDate)) THEN
        BEGIN
          m_MinDateUpIn = :m_DateUpIn5;
          m_MinFlag = 5;
        END
      END

      IF (:m_MinFlag >= 1) THEN
      BEGIN
        m_MinDateFlag = 0;
        /*根据更新日期与确认的相对更新点, 继续寻找绝对更新点*/
        IF (:m_DateUpIn5 <= :p_ListDate) THEN m_MinDateFlag = 5;
        IF (:m_DateUpIn4 <= :p_ListDate) THEN m_MinDateFlag = 4;
        IF (:m_DateUpIn3 <= :p_ListDate) THEN m_MinDateFlag = 3;
        IF (:m_DateUpIn2 <= :p_ListDate) THEN m_MinDateFlag = 2;
        IF (:m_DateUpIn <= :p_ListDate) THEN m_MinDateFlag = 1;

        IF ((:m_MinFlag - 4 >= 1) AND (:m_MinDateFlag <= 4)) THEN
        BEGIN  /*把4与5更换*/
          UPDATE STKLST SET PriceUpIn5 = :m_PriceUpIn4,
            CompanyUpIn5 = :m_CompanyUpIn4, DateUpIn5 = :m_DateUpIn4
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF ((:m_MinFlag - 3 >= 1) AND (:m_MinDateFlag <= 3)) THEN
        BEGIN /*把3与4更换*/
          UPDATE STKLST SET PriceUpIn4 = :m_PriceUpIn3,
            CompanyUpIn4 = :m_CompanyUpIn3, DateUpIn4 = :m_DateUpIn3
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF ((:m_MinFlag - 2 >= 1) AND (:m_MinDateFlag <= 2)) THEN
        BEGIN /*把2与3更换*/
          UPDATE STKLST SET PriceUpIn3 = :m_PriceUpIn2,
            CompanyUpIn3 = :m_CompanyUpIn2, DateUpIn3 = :m_DateUpIn2
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF ((:m_MinFlag - 1 >= 1) AND (:m_MinDateFlag <= 1)) THEN
        BEGIN /*把1与2更换*/
          UPDATE STKLST SET PriceUpIn2 = :m_PriceUpIn,
            CompanyUpIn2 = :m_CompanyUpIn, DateUpIn2 = :m_DateUpIn
          WHERE (IntNo = :m_GoodsIntNo);
        END

        IF (:m_MinDateFlag = 5) THEN
        BEGIN   /*直接更新m_MinDateFlag对应的列*/
          UPDATE STKLST SET PriceUpIn5 = :m_Price*:m_Discount,
            CompanyUpIn5 = :p_CmpyName, DateUpIn5 = :p_ListDate
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF (:m_MinDateFlag = 4) THEN
        BEGIN   /*直接更新m_MinDateFlag对应的列*/
          UPDATE STKLST SET PriceUpIn4 = :m_Price*:m_Discount,
            CompanyUpIn4 = :p_CmpyName, DateUpIn4 = :p_ListDate
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF (:m_MinDateFlag = 3) THEN
        BEGIN   /*直接更新m_MinDateFlag对应的列*/
          UPDATE STKLST SET PriceUpIn3 = :m_Price*:m_Discount,
            CompanyUpIn3 = :p_CmpyName, DateUpIn3 = :p_ListDate
          WHERE (IntNo = :m_GoodsIntNo);
        END
        IF (:m_MinDateFlag = 2) THEN
        BEGIN   /*直接更新m_MinDateFlag对应的列*/
          UPDATE STKLST SET PriceUpIn2 = :m_Price*:m_Discount,
            CompanyUpIn2 = :p_CmpyName, DateUpIn2 = :p_ListDate
          WHERE (IntNo = :m_GoodsIntNo);
        END

        IF (:m_MinDateFlag = 1) THEN
        BEGIN   /*直接更新m_MinDateFlag对应的列*/
          UPDATE STKLST SET PriceUpIn = :m_Price*:m_Discount,
            CompanyUpIn = :p_CmpyName, DateUpIn = :p_ListDate
          WHERE (IntNo = :m_GoodsIntNo);
        END
      END /* m_MinFlag = 0 表示不符合条件*/
    END
END
--
2005.01.20_01/SQL4
DELETE FROM FieldDefine WHERE UPPER(FieldName) = 'QTYDEFINENO'
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('QtyDefineNo', '', '警示表号', '8')
---
DELETE FROM FieldDefine WHERE (UPPER(TableName) = 'STKQTYDETAIL')
  AND (UPPER(FieldName) = 'QRemainMin')
---
DELETE FROM FieldDefine WHERE (UPPER(TableName) = 'STKQTYDETAIL')
  AND (UPPER(FieldName) = 'QRemainMax')
---
DELETE FROM FieldDefine WHERE (UPPER(TableName) = 'STKQTYDETAIL')
  AND (UPPER(FieldName) = 'QtySafety')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMin', 'StkQtyDetail', '最低存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QRemainMax', 'StkQtyDetail', '最高存量', '8', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('QtySafety', 'StkQtyDetail', '安全存量', '8', 'TQty')
---
/*第一步， 修改内部号生成*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2005.01.20_01/SQL5
/*第二步，处理生成子*/
CREATE GENERATOR INTNO_StkQtyList
---
SET GENERATOR INTNO_StkQtyList TO 10
---
CREATE GENERATOR INTNO_StkQtyDetail
---
SET GENERATOR INTNO_StkQtyDetail TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END

    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END

    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYLIST')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyList, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKQTYDETAIL')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_StkQtyDetail, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
---
/*第三步， 新建主表及相关的索引字段等*/
CREATE TABLE StkQtyList (
  IntNo  TINTNO,
  QtyDefineNo Char(12) DEFAULT '' NOT NULL,
  Comment TComment,
  Persondeal TPersonName,
  Department TDepartment,
  OperNo ToperNo,
  Remark TRemark,
  LRemark2 VarChar(120),
  UpdateUCT Integer,
  InsertUCT Integer,
  CONSTRAINT StkQtyList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT StkQtyList_UNIQUE_1 UNIQUE (QtyDefineNo)
)
---
CREATE INDEX IdxStkQtyList_QtyDefineNo ON StkQtyList(QtyDefineNo);
--
2005.01.20_01/SQL6
ALTER TABLE StkName
  ADD QtyDefineNo Char(12) DEFAULT ''
---
/*第四步  建明细表及主键约束*/
CREATE TABLE StkQtyDetail(
  IntNo TIntNo,
  MainIntNo TIntNo,
  GoodsIntNo TIntNo,
  QRemainMin TQty,
  QRemainMax TQty,
  QtySafety TQty,
  Remark TRemark,
  UpdateUCT Integer,
  InsertUCT Integer,
  CONSTRAINT StkQtyDetail_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_StkQtyDetail_StkQtyList FOREIGN KEY (MAININTNO) REFERENCES StkQtyList(INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_StkQtyDetail_Stklst FOREIGN KEY (GoodsIntNo) REFERENCES Stklst(IntNo)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT StkQtyDetail_UNIQUE_1 UNIQUE (MainIntNo, GoodsIntNo)
)
---
CREATE INDEX IdxSQDetial_MainIntNo ON StkQtyDetail(MainIntNo);
--
2005.01.20_01/SQL7
/*第五步 建立主表触发， 用于新增记录的初始化*/
CREATE TRIGGER StkQtyList_BEF_INS_SET_DEF FOR StkQtyList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.IntNo IS NULL) THEN new.IntNo = 0;
  IF (new.QtyDefineNo IS NULL) THEN new.QtyDefineNo = '';
  IF (new.Comment IS NULL)     THEN new.Comment = '';
  IF (new.Persondeal IS NULL)  THEN new.Persondeal = '';
  IF (new.Department IS NULL)  THEN new.Department = '';
  IF (new.OperNo IS NULL)      THEN new.OperNo = '';
  IF (new.Remark IS NULL)      THEN new.Remark = '';
  IF (new.LRemark2 IS NULL)    THEN new.LRemark2 = '';
  IF (new.UpdateUCT IS NULL)   THEN new.UpdateUCT = 0;
  IF (new.InsertUCT IS NULL)   THEN new.InsertUCT = 0;
END
---
CREATE TRIGGER StkQtyList_BEF_UPD_SET_DEF FOR StkQtyList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.IntNo IS NULL) THEN new.IntNo = 0;
  IF (new.QtyDefineNo IS NULL) THEN new.QtyDefineNo = '';
  IF (new.Comment IS NULL)     THEN new.Comment = '';
  IF (new.Persondeal IS NULL)  THEN new.Persondeal = '';
  IF (new.Department IS NULL)  THEN new.Department = '';
  IF (new.OperNo IS NULL)      THEN new.OperNo = '';
  IF (new.Remark IS NULL)      THEN new.Remark = '';
  IF (new.LRemark2 IS NULL)    THEN new.LRemark2 = '';
  IF (new.UpdateUCT IS NULL)   THEN new.UpdateUCT = 0;
  IF (new.InsertUCT IS NULL)   THEN new.InsertUCT = 0;
END
---
/*第六步 建立明细表触发， 用于新增记录的初始化*/
CREATE TRIGGER StkQtyDetail_BEF_INS_SET_DEF FOR StkQtyDetail BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.IntNo IS NULL)      THEN new.IntNo = 0;
  IF (new.MainIntNo  IS NULL) THEN new.MainIntNo = 0;
  IF (new.GoodsIntNo IS NULL) THEN new.GoodsIntNo = 0;
  IF (new.QRemainMin IS NULL) THEN new.QRemainMin = 0.00;
  IF (new.QRemainMax IS NULL) THEN new.QRemainMax = 0.00;
  IF (new.QtySafety IS NULL)  THEN new.QtySafety = 0.00;
  IF (new.Remark IS NULL)     THEN new.Remark = '';
  IF (new.UpdateUCT IS NULL)  THEN new.UpdateUCT = 0;
  IF (new.InsertUCT IS NULL)  THEN new.InsertUCT = 0;
END
---
CREATE TRIGGER StkQtyDetail_BEF_UPD_SET_DEF FOR StkQtyDetail BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.IntNo IS NULL)      THEN new.IntNo = 0;
  IF (new.MainIntNo  IS NULL) THEN new.MainIntNo = 0;
  IF (new.GoodsIntNo IS NULL) THEN new.GoodsIntNo = 0;
  IF (new.QRemainMin IS NULL) THEN new.QRemainMin = 0.00;
  IF (new.QRemainMax IS NULL) THEN new.QRemainMax = 0.00;
  IF (new.QtySafety IS NULL)  THEN new.QtySafety = 0.00;
  IF (new.Remark IS NULL)     THEN new.Remark = '';
  IF (new.UpdateUCT IS NULL)  THEN new.UpdateUCT = 0;
  IF (new.InsertUCT IS NULL)  THEN new.InsertUCT = 0;
END
---
/*第七步 主表被删除， 明细表自动被删除*/
CREATE TRIGGER StkQtyList_BEF_DEL_DEL_DETAIL FOR StkQtyList
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM StkQtyDetail WHERE MainIntNo = old.IntNo;
END
---
/*第八步 新增6个触发器 用于同步*/
CREATE TRIGGER StkQtyList_BEF_INS_EVENTLOG FOR StkQtyList BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StkQtyList_BEF_UPD_EVENTLOG FOR StkQtyList BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StkQtyList_AFT_DEL_EVENTLOG FOR StkQtyList AFTER DELETE POSITION 1 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('StkQtyList', old.IntNo);
END
---
CREATE TRIGGER StkQtyDetail_BEF_INS_EVENTLOG FOR StkQtyDetail BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StkQtyDetail_BEF_UPD_EVENTLOG FOR StkQtyDetail BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER StkQtyDetail_AFT_DEL_EVENTLOG FOR StkQtyDetail AFTER DELETE POSITION 1 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('StkQtyDetail', old.IntNo);
END
---
/*第九步 仓库名称触发*/
CREATE TRIGGER StkQtyList_AFT_UPD_SET_StkName FOR StkQtyList AFTER UPDATE POSITION 2 AS
BEGIN
  UPDATE StkName SET QtyDefineNo = new.QtyDefineNo
    WHERE QtyDefineNo = old.QtyDefineNo;
END
---
CREATE TRIGGER StkQtyList_AFT_DEL_SET_StkName FOR StkQtyList AFTER DELETE POSITION 2 AS
BEGIN
  IF (old.QtyDefineNo <> '') THEN
  BEGIN
    UPDATE StkName SET QtyDefineNo = ''
      WHERE QtyDefineNo = old.QtyDefineNo;
  END
END
---
/*第十步 获取分仓中的警示表号, 再获得相应的警示量*/
CREATE PROCEDURE GetStkQtyDetailQRemain(p_StockIntNo Integer, p_GoodsIntNo Integer)
RETURNS(r_QRemainMin DEC(15,4), r_QRemainMax DEC(15, 4), r_QtySafety DEC(15, 4))
AS
  DECLARE VARIABLE m_QtyDefineNo Char(12);
  DECLARE VARIABLE m_MainIntNo Integer;
BEGIN
  /*先初始化*/
  r_QRemainMax = 0.00;
  r_QRemainMax = 0.00;
  r_QtySafety  = 0.00;
  m_QtyDefineNo= '';
  m_MainIntNo  = 0;

  /*查找警示表号*/
  SELECT QtyDefineNo FROM StkName WHERE (IntNo = :p_StockIntNo)
    INTO :m_QtyDefineNo;
  IF ((:m_QtyDefineNo = '') OR (:m_QtyDefineNo IS NULL)) THEN
  BEGIN
    SUSPEND;
    EXIT;
  END

  /*查主表内部号*/
  SELECT IntNo FROM StkQtyList WHERE (QtyDefineNo = :m_QtyDefineNo)
    INTO :m_MainIntNo;
  IF (:m_MainIntNo = 0) THEN
  BEGIN
    SUSPEND;
    EXIT;
  END

  /*返回值*/
  FOR SELECT QRemainMin, QRemainMax, QtySafety FROM StkQtyDetail
    WHERE ((MainIntNo =:m_MainIntNo) AND (GoodsIntNo = :p_GoodsIntNo))
    INTO :r_QRemainMin, :r_QRemainMax, :r_QtySafety DO
  BEGIN
    SUSPEND;
  END
END
---
/*新增颜色尺码组删除触发器各一个*/
CREATE TRIGGER CGList_AFT_DEL_EVENTLOG FOR CustomGrpList AFTER DELETE POSITION 1 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('CustomGrpList', old.IntNo);
END
---
CREATE TRIGGER CGDetail_AFT_DEL_EVENTLOG FOR CustomGrpDetail AFTER DELETE POSITION 1 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('CustomGrpDetail', old.IntNo);
END
---
CREATE TRIGGER STKLST_BEFORE_DEL_SAVE_GOODSNO FOR STKLST BEFORE DELETE POSITION 10 AS
BEGIN
  UPDATE SALEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE SALEGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE MOVEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE PANGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE REALGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE ASSGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE USEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
END
--
2005.01.15_01/UPGRADE_DETAIL
1. 库存清单: 增加字段[类别内部号]；
2. 调整关于类别定义的存贮过程。
--
2005.01.15_01/SQL1
ALTER TABLE Stklst
  ADD TypeIntNo Integer DEFAULT NULL
---
ALTER TABLE STKLST ADD CONSTRAINT FK_STKLST_TYPEDEFINE
  FOREIGN KEY (TypeIntNo)
  REFERENCES TypeDefine (IntNo)
  ON UPDATE CASCADE
  ON DELETE SET NULL
---
ALTER PROCEDURE GET_FULL_TYPEINFO
RETURNS (
  INTNO INTEGER,
  FULLTYPENO VARCHAR(20),
  TYPENAME VARCHAR(20),
  FULLTYPENAME VARCHAR(80)
) AS
BEGIN
  FOR SELECT IntNo, TypeNo, TypeName FROM TypeDefine
    INTO :IntNo, :FullTypeNo, :TypeName DO
  BEGIN
    FullTypeName = TypeName;
    IF (IntNo <> 0) THEN
      EXECUTE PROCEDURE Get_Full_TypeInfo_By_IntNo(:IntNo)
        RETURNING_VALUES :FULLTYPENO, :TypeName, :FullTypeName;
    FULLTYPENO = ALLTRIM(FULLTYPENO);
    TYPENAME = ALLTRIM(TYPENAME);
    FULLTYPENAME = ALLTRIM(FULLTYPENAME);
    SUSPEND;
  END
END
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('FullTypeNo')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('TypeName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('FullTypeNo', '', '类别代码', 'TGdsType')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('TypeName', '', '类别名称', '12')
--
2005.01.05_01/UPGRADE_DETAIL
1. 资金户头/客户/供应商资料:[结帐日期]设置默认值；
2. 客户帐：增加支持字段[换算]。
--
2005.01.05_01/SQL1
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('CustomGrpName')) AND (UPPER(TableName) = UPPER('CustomGrpList'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('SumAmountIn')) AND (UPPER(TableName) = UPPER('STKLST'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('SumQtyIn')) AND (UPPER(TableName) = UPPER('STKLST'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('CustomGrpName', 'CUSTOMGRPLIST', '组名称', '12')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName, Hidden)
  VALUES ('SumAmountIn', 'STKLST', '总成本', 'TAmount', '1')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName, Hidden)
  VALUES ('SumQtyIn', 'STKLST', '总进货量', 'TQty', '1')
---
CREATE TRIGGER BANKNAME_BEF_INS_SETDEF_1 FOR BANKNAME
ACTIVE BEFORE INSERT POSITION 1
AS
BEGIN
  new.S_Date = '1900-01-01';
END
---
CREATE TRIGGER CLIENT_BEF_INS_SETDEF_1 FOR CLIENT
ACTIVE BEFORE INSERT POSITION 1
AS
BEGIN
  new.S_Date = '1900-01-01';
END
---
ALTER PROCEDURE QUERY_CMPYNAME_DETAIL (
  P_CMPYNAME CHAR(12),
  P_DATEFROM DATE,
  P_CLIENTFLAG SMALLINT
) RETURNS (
  INTNO INTEGER,
  LISTDATE DATE,
  GOODSINTNO INTEGER,
  GOODSNO CHAR(20),
  GOODS CHAR(80),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  NETPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  BANKID CHAR(8),
  ACTPAY NUMERIC(15, 4),
  TMPREMAIN NUMERIC(15, 4),
  LISTNO CHAR(14),
  REMARK CHAR(20),
  MAINREMARK CHAR(20),
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO CHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER,
  UNITID SMALLINT
) AS    
BEGIN
  IF (p_ClientFLag = 1) THEN /* 供应商 */

  BEGIN
    FOR
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit,
      StkName.StockId, d.Qty, d.Price, d.Price, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 
      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag, 
      d.CustomIntNo1,  d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      d.BatchNo, d.ManuFactDate_Int, d.ExpireDate_Int, d.UnitId
      From Buygds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)  
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)  
      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)  
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo) 
      LEFT OUTER JOIN InList m On (m.IntNo = d.MainIntNo)  
      Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :InPrice, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
         , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
         :BatchNo, :ManuFactDate_Int, :ExpireDate_Int, :UnitId
    DO SUSPEND;
  END
  ELSE  /* 客户 */
  BEGIN
    FOR
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit, 
      StkName.StockId, d.Qty, d.Price, d.InPrice, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 
      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag 
      , d.CustomIntNo1,  d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ManuFactDate_Int, d.ExpireDate_Int, d.UnitId
      From Salegds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)
      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo)
      LEFT OUTER JOIN OutList m On (m.IntNo = d.MainIntNo)  
      Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :InPrice, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
         , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
         :BatchNo, :ManuFactDate_Int, :ExpireDate_Int, :UnitId
    DO SUSPEND;
  END
END
--
2004.12.10_01/UPGRADE_DETAIL
1. 增加字段: [换算A/换算B]。
--
2004.12.10_01/SQL1
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('AltUnitA'))
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('AltUnitB'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('AltUnitA', '', '换算A', 'TAltUnit1')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('AltUnitB', '', '换算B', 'TAltUnit1')
---
DECLARE EXTERNAL FUNCTION ZS_GETALTUNITA
   CSTRING(8), CSTRING(8), CSTRING(8), INTEGER,
   DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, CSTRING(60)
   RETURNS CSTRING(255)
  ENTRY_POINT "zsudf_GetAltUnitA"  MODULE_NAME "zsudf"
---
DECLARE EXTERNAL FUNCTION ZS_GETALTUNITB
   CSTRING(8), CSTRING(8), CSTRING(8), INTEGER,
   DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, CSTRING(60)
   RETURNS CSTRING(255)
  ENTRY_POINT "zsudf_GetAltUnitB"  MODULE_NAME "zsudf"
--
2004.12.03_01/UPGRADE_DETAIL
1. 颜色尺码组: 支持分时模式前后台同步。
2. 客户/供应商资料: 新增[新建操作员]。
--
2004.12.03_01/SQL1
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = UPPER('OperNo'))
  AND (UPPER(TableName) = UPPER('Client'))
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('OPERNO', 'CLIENT', '新建操作员', '8', 'TOPERNO')
---
ALTER TABLE Client ADD OPERNO VARCHAR(10)
---
ALTER TABLE CustomGrpList ADD InsertUct Integer
---
ALTER TABLE CustomGrpList ADD UpdateUct Integer
---
ALTER TABLE CustomGrpDetail ADD InsertUct Integer
---
ALTER TABLE CustomGrpDetail ADD UpdateUct Integer
--
2004.12.03_01/SQL2
CREATE TRIGGER CGList_BEF_INS_EVENTLOG FOR CustomGrpList BEFORE INSERT POSITION 1
AS
BEGIN

  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER CGList_BEF_UPD_EVENTLOG FOR CustomGrpList BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER CGDetail_BEF_INS_EVENTLOG FOR CustomGrpDetail BEFORE INSERT POSITION 1
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER CGDetail_BEF_UPD_EVENTLOG FOR CustomGrpDetail BEFORE UPDATE POSITION 1
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
--
2004.11.16_01/UPGRADE_DETAIL
1. 调价表: 支持[调整自定进价时，同时更新最新进价]。
--
2004.11.16_01/SQL1
CREATE GENERATOR INTNO_CPLISTUPDATEPRICEUPIN
---
SET GENERATOR INTNO_CPLISTUPDATEPRICEUPIN TO 0
---
ALTER PROCEDURE UPDATE_CPGDS_BYACC (
  P_FLAG SMALLINT,
  P_INTNO INTEGER
)  AS
DECLARE VARIABLE m_INPRICEDEF DEC(15,4);         /*自定进价*/
DECLARE VARIABLE m_PRICEUPIN DEC(15,4);          /*最新进价*/
DECLARE VARIABLE m_STANDARDOUTPRICE DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE DEC(15,4); /*优惠批发价*/
DECLARE VARIABLE m_GoodsIntNo Integer;           /*货号*/
DECLARE VARIABLE m_UnitId Integer;               /*单位ID*/

DECLARE VARIABLE m_STANDARDOUTPRICE1 DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE1 DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE1 DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE1 DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE1 DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE1 DEC(15,4); /*优惠批发价*/

DECLARE VARIABLE m_STANDARDOUTPRICE2 DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE2 DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE2 DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE2 DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE2 DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE2 DEC(15,4); /*优惠批发价*/
BEGIN
  IF (p_Flag = 1) then                           /* 生效 */
  BEGIN
    /* 取第一单位的旧价格 */
    FOR
      SELECT stk.INPRICEDEF, stk.STANDARDOUTPRICE, stk.NORMALOUTPRICE, stk.PREFENTIALPRICE,
        stk.MINOUTPRICE, stk.PFASTANDARDPRICE, stk.PFAPREFENTIALPRICE,

        stk.STANDARDOUTPRICE1, stk.NORMALOUTPRICE1, stk.PREFENTIALPRICE1,
        stk.MINOUTPRICE1, stk.PFASTANDARDPRICE1, stk.PFAPREFENTIALPRICE1,

        stk.STANDARDOUTPRICE2, stk.NORMALOUTPRICE2, stk.PREFENTIALPRICE2,
        stk.MINOUTPRICE2, stk.PFASTANDARDPRICE2, stk.PFAPREFENTIALPRICE2, d.GoodsIntNo
      FROM CPGDS d, STKLST stk
      WHERE (d.MainIntNo = :p_IntNo and d.GoodsIntNo = stk.IntNo)
      INTO :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE CPGDS Set o_INPRICEDEF = :m_INPRICEDEF, o_STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          o_NORMALOUTPRICE = :m_NORMALOUTPRICE, o_PREFENTIALPRICE = :m_PREFENTIALPRICE,
          o_MINOUTPRICE = :m_MINOUTPRICE, o_PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          o_PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

          o_STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, o_NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
          o_PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   o_MINOUTPRICE1 = :m_MINOUTPRICE1,
          o_PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, o_PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

          o_STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, o_NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
          o_PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   o_MINOUTPRICE2 = :m_MINOUTPRICE2,
          o_PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, o_PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2

        WHERE (MainIntNo = :p_IntNo and GoodsIntNo = :m_GoodsIntNo); /*  and UnitId = 0 */
      END
      /* IF (m_UnitId = 1) THEN */
      /* IF (m_UnitId = 2) THEN */

    /* 取新的价格, 更新到库存清单中 */
    FOR
      SELECT n_INPRICEDEF, n_STANDARDOUTPRICE, n_NORMALOUTPRICE, n_PREFENTIALPRICE,
        n_MINOUTPRICE, n_PFASTANDARDPRICE, n_PFAPREFENTIALPRICE,

        n_STANDARDOUTPRICE1, n_NORMALOUTPRICE1, n_PREFENTIALPRICE1,

        n_MINOUTPRICE1, n_PFASTANDARDPRICE1, n_PFAPREFENTIALPRICE1,

        n_STANDARDOUTPRICE2, n_NORMALOUTPRICE2, n_PREFENTIALPRICE2,
        n_MINOUTPRICE2, n_PFASTANDARDPRICE2, n_PFAPREFENTIALPRICE2, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        IF (GEN_ID(INTNO_CPLISTUPDATEPRICEUPIN, 0) = 1) THEN
        BEGIN /*自动更新库存清单中的最新进价*/
          UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, PRICEUPIN = :m_INPRICEDEF,
            STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
            NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
            MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
            PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

            STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
            PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   MINOUTPRICE1 = :m_MINOUTPRICE1,
            PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

            STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
            PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   MINOUTPRICE2 = :m_MINOUTPRICE2,
            PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2
          WHERE (IntNo = :m_GoodsIntNo);
        END
        ELSE
        BEGIN
          UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
            NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
            MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
            PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

            STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
            PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   MINOUTPRICE1 = :m_MINOUTPRICE1,
            PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

            STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
            PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   MINOUTPRICE2 = :m_MINOUTPRICE2,
            PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2
          WHERE (IntNo = :m_GoodsIntNo);
        END
      END
  END
  IF (p_Flag = -1) then                          /* 取消生效 */
  BEGIN
    /* 将旧的价格更新回到库存清单中 */
    FOR
      SELECT O_INPRICEDEF, O_STANDARDOUTPRICE, O_NORMALOUTPRICE, O_PREFENTIALPRICE,
        O_MINOUTPRICE, O_PFASTANDARDPRICE, O_PFAPREFENTIALPRICE,

        O_STANDARDOUTPRICE1, O_NORMALOUTPRICE1, O_PREFENTIALPRICE1,
        O_MINOUTPRICE1, O_PFASTANDARDPRICE1, O_PFAPREFENTIALPRICE1,

        O_STANDARDOUTPRICE2, O_NORMALOUTPRICE2, O_PREFENTIALPRICE2,
        O_MINOUTPRICE2, O_PFASTANDARDPRICE2, O_PFAPREFENTIALPRICE2, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
          MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

          STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
          PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   MINOUTPRICE1 = :m_MINOUTPRICE1,
          PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

          STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
          PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   MINOUTPRICE2 = :m_MINOUTPRICE2,
          PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2
        WHERE (IntNo = :m_GoodsIntNo);
      END
  END
END
--
2004.10.30_01/UPGRADE_DETAIL
1. 直运商品调拨明细单:　增加字段, 支持自定义内容;
2. 商品销售明细单(含运费):　增加字段, 支持自定义内容;

3. 商品进货明细单(含运费):　增加字段, 支持自定义内容;
4. 进货明细表:  增加字段, 验收结论/验收人
--
2004.10.30_01/SQL1
ALTER TABLE BuyGds
  ADD CheckPerson CHAR(10) DEFAULT '',
  ADD CheckConclusion CHAR(10) DEFAULT ''
---
ALTER TABLE DSaleGds
  ADD CustomIntNo1 TCUSTOMINTNO,
  ADD CustomIntNo2 TCUSTOMINTNO,
  ADD CustomIntNo3 TCUSTOMINTNO,
  ADD CustomIntNo4 TCUSTOMINTNO,
  ADD CustomIntNo5 TCUSTOMINTNO

---
ALTER TABLE BuyGdsT

  ADD CustomIntNo1 TCUSTOMINTNO,
  ADD CustomIntNo2 TCUSTOMINTNO,
  ADD CustomIntNo3 TCUSTOMINTNO,
  ADD CustomIntNo4 TCUSTOMINTNO,
  ADD CustomIntNo5 TCUSTOMINTNO
---
ALTER TABLE SaleGdsT
  ADD CustomIntNo1 TCUSTOMINTNO,
  ADD CustomIntNo2 TCUSTOMINTNO,
  ADD CustomIntNo3 TCUSTOMINTNO,
  ADD CustomIntNo4 TCUSTOMINTNO,
  ADD CustomIntNo5 TCUSTOMINTNO
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('CheckPerson')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('CheckConclusion')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('CheckPerson', '', '验收人', '8')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('CheckConclusion', '', '验收结论', '8')
--
2004.10.12_01/UPGRADE_DETAIL
1. 库存清单: 增加第二、第三单位的价格及换算关系，增加商品启用标志；
2. 调价表中: 增加第二、第三单位的价格(新旧各6个字段);
3. 时段促销表: 增加第二、第三单位的价格及折扣;
4. 售价/进价明细表: 增加第二、第三单位的价格及折扣;
5. 前台支持分布式盘点;
6. 差额盘点明细表: 增加库存量(原)、库存量(新)字段。
--
2004.10.12_01/SQL0
ALTER PROCEDURE PUT_UNPUTDATA (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14)
)  AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
/* 调价表的价格 */
ALTER PROCEDURE UPDATE_CPGDS_BYACC (
  P_FLAG SMALLINT,
  P_INTNO INTEGER
)  AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER,
  QREMAINNEW DOUBLE PRECISION,
  QREMAINOLD DOUBLE PRECISION
) AS
BEGIN
  SUSPEND;
END
--
2004.10.12_01/SQL1
ALTER PROCEDURE PUT_UNPUTDATA (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14)
)  AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF (P_WorkMode = 1) THEN  /* 进货 */
  BEGIN
    UPDATE INLIST SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM INLIST WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN
    UPDATE OUTLIST SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM OUTLIST WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    UPDATE PANLIST SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM PANLIST WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    UPDATE RealList SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM RealList WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    UPDATE ASSLIST SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM ASSLIST WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    UPDATE MOVELIST SET UPDATEACC = ' ' WHERE LISTNO = :p_ListNo;
    DELETE FROM MOVELIST WHERE LISTNO = :p_ListNo;
    SUSPEND;
  END
END
---
ALTER TABLE MGOODSNO ADD CONSTRAINT MGOODSNO_UNIQUE_1 UNIQUE (GOODSINTNO, OTHERNO, UNITID)
---
ALTER TABLE PanGds
  ADD QReMainOld TQty,
  ADD QRemainNew TQty
---
ALTER TABLE CPGds
  ADD N_STANDARDOUTPRICE1 TPRICE,
  ADD N_NORMALOUTPRICE1 TPRICE,
  ADD N_PREFENTIALPRICE1 TPRICE,
  ADD N_MINOUTPRICE1 TPRICE,
  ADD N_PFASTANDARDPRICE1 TPRICE,
  ADD N_PFAPREFENTIALPRICE1 TPRICE,

  ADD N_STANDARDOUTPRICE2 TPRICE,
  ADD N_NORMALOUTPRICE2 TPRICE,
  ADD N_PREFENTIALPRICE2 TPRICE,
  ADD N_MINOUTPRICE2 TPRICE,
  ADD N_PFASTANDARDPRICE2 TPRICE,
  ADD N_PFAPREFENTIALPRICE2 TPRICE,

  ADD O_STANDARDOUTPRICE1 TPRICE,
  ADD O_NORMALOUTPRICE1 TPRICE,
  ADD O_PREFENTIALPRICE1 TPRICE,
  ADD O_MINOUTPRICE1 TPRICE,
  ADD O_PFASTANDARDPRICE1 TPRICE,
  ADD O_PFAPREFENTIALPRICE1 TPRICE,

  ADD O_STANDARDOUTPRICE2 TPRICE,
  ADD O_NORMALOUTPRICE2 TPRICE,
  ADD O_PREFENTIALPRICE2 TPRICE,
  ADD O_MINOUTPRICE2 TPRICE,
  ADD O_PFASTANDARDPRICE2 TPRICE,
  ADD O_PFAPREFENTIALPRICE2 TPRICE
---
/* 时段促销*/
ALTER TABLE SpecGds
  ADD Price    TPrice,
  ADD Discount TDiscount
---
/* 售价明细表中增加第二、第三单位的价格及拆扣*/
ALTER TABLE PriceLst
  ADD Price1    TPrice,
  ADD Discount1 TDiscount,
  ADD Price2    TPrice,
  ADD Discount2 TDiscount
---
/* 库存表中增加第二、第三单位的价格及换算关系共14个字段 */
ALTER TABLE Stklst
  ADD Unit2             TGoodsUnit,
  ADD Unit2XChg         TUnitXChg,

  ADD StandardOutPrice1 TPrice,
  ADD NormalOutPrice1   TPrice,
  ADD PrefentialPrice1  TPrice,
  ADD MinOutPrice1      TPrice,
  ADD PFAStandardPrice1   TPrice,
  ADD PFAPrefentialPrice1 TPrice,

  ADD StandardOutPrice2 TPrice,
  ADD NormalOutPrice2   TPrice,
  ADD PrefentialPrice2  TPrice,
  ADD MinOutPrice2      TPrice,
  ADD PFAStandardPrice2   TPrice,
  ADD PFAPrefentialPrice2 TPrice,
  ADD InUse SMALLINT DEFAULT 1 NOT NULL
---
/* 盘点明细表*/
DELETE FROM FIELDDEFINE WHERE UPPER(TABLENAME) = UPPER('RealList')
  AND UPPER(FIELDNAME) = Upper('Flag3')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QReMainOld')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QReMainNew')
---
/*调价明细表*/
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_STANDARDOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_NORMALOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PREFENTIALPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_MINOUTPRICE1')

---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFASTANDARDPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFAPREFENTIALPRICE1')
---
/* O_2 */
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_STANDARDOUTPRICE2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_NORMALOUTPRICE2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PREFENTIALPRICE2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_MINOUTPRICE2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFASTANDARDPRICE2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFAPREFENTIALPRICE2')
---
/* N_1 */
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_STANDARDOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_NORMALOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PREFENTIALPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_MINOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFASTANDARDPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFAPREFENTIALPRICE1')
---
/* N_2 */
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_STANDARDOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_NORMALOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PREFENTIALPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_MINOUTPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFASTANDARDPRICE1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFAPREFENTIALPRICE1')
---
/*售价明细表*/
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Price1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Price2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Discount1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Discount2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('NetPrice')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('NetPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('NetPrice2')
---
/* 库存清单 */
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('InUse')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Unit2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('Unit2XChg')
---
/*删除第二单位的六个售价字段定义*/
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('StandardOutPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('NormalOutPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PrefentialPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('MinOutPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PFAStandardPrice1')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PFAPrefentialPrice1')
---
/*删除第三单位的六个售价字段定义*/
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('StandardOutPrice2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('NormalOutPrice2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PrefentialPrice2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('MinOutPrice2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PFAStandardPrice2')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('PFAPrefentialPrice2')
---
/* 盘点明细表*/
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('Flag3', 'RealList', '传送', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('QReMainOld', '', '库存量(原)', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('QReMainNew', '', '库存量(新)', 'TQty')
---
/* O_1 */
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_STANDARDOUTPRICE1', '', '原标准售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_NORMALOUTPRICE1', '', '原一般售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PREFENTIALPRICE1', '', '原优惠售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_MINOUTPRICE1', '', '原最低售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFASTANDARDPRICE1', '', '原批发价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFAPREFENTIALPRICE1', '', '原优惠批发价二', 'TPrice')
---
/* O_2 */
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_STANDARDOUTPRICE2', '', '原标准售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_NORMALOUTPRICE2', '', '原一般售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PREFENTIALPRICE2', '', '原优惠售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_MINOUTPRICE2', '', '原最低售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFASTANDARDPRICE2', '', '原批发价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFAPREFENTIALPRICE2', '', '原优惠批发价三', 'TPrice')
---
/* N_1 */
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_STANDARDOUTPRICE1', '', '新标准售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_NORMALOUTPRICE1', '', '新一般售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PREFENTIALPRICE1', '', '新优惠售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_MINOUTPRICE1', '', '新最低售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFASTANDARDPRICE1', '', '新批发价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFAPREFENTIALPRICE1', '', '新优惠批发价二', 'TPrice')
---
/* N_2 */
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_STANDARDOUTPRICE2', '', '新标准售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_NORMALOUTPRICE2', '', '新一般售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PREFENTIALPRICE2', '', '新优惠售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_MINOUTPRICE2', '', '新最低售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFASTANDARDPRICE2', '', '新批发价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFAPREFENTIALPRICE2', '', '新优惠批发价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Price1', '', '单价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Price2', '', '单价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Discount1', '', '折二', 'TDiscount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Discount2', '', '折三', 'TDiscount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('NetPrice', '', '净售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('NetPrice1', '', '净售价二', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('NetPrice2', '', '净售价三', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('InUse', '', '启用', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Unit2', '', '第三单位', 'TGoodsUnit')
---
UPDATE FieldDefine SET Dlabel = '单位', DWidth = '4', DomainName = 'TGoodsUnit', Hidden = '0'
WHERE UPPER(FieldName) = Upper('Unit0')
---
UPDATE FieldDefine SET Dlabel = '第二单位', DWidth = '4', DomainName = 'TGoodsUnit', Hidden = '0'
WHERE UPPER(FieldName) = Upper('Unit1')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('UNIT2XCHG', 'STKLST', '换算三', '7', 'TUnitXChg')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Unit2XChg', '', '换算三', 'TQty')
---
/*增加第二单位的六个售价字段定义*/
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('StandardOutPrice1',   '', '标准售价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('NormalOutPrice1',     '', '一般售价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PrefentialPrice1',    '', '优惠售价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('MinOutPrice1',        '', '最低售价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PFAStandardPrice1',   '', '批发价2', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PFAPrefentialPrice1', '', '优惠批发价2', 'TPrice')
---
/*增加第三单位的六个售价字段定义*/
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('StandardOutPrice2',   '', '标准售价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('NormalOutPrice2',     '', '一般售价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PrefentialPrice2',    '', '优惠售价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('MinOutPrice2',        '', '最低售价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PFAStandardPrice2',   '', '批发价3', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('PFAPrefentialPrice2', '', '优惠批发价3', 'TPrice')
--
2004.10.12_01/SQL2

/*处理库存表被插入时更新单位表*/
CREATE TRIGGER STKLST_AFTER_INSERT_1 FOR STKLST AFTER INSERT POSITION 1 AS
BEGIN
  DELETE FROM UnitList WHERE GoodsIntNo = new.IntNo;
  INSERT INTO UnitList (GoodsIntNo, UnitId, GoodsUnit, Factor, isBase)
    VALUES (new.IntNo, 0, new.Unit0, 1, 1);
  INSERT INTO UnitList (GoodsIntNo, UnitId, GoodsUnit, Factor, isBase)
    VALUES (new.IntNo, 1, new.Unit1, new.Unit0Xchg/new.Unit1Xchg, 0);
  INSERT INTO UnitList (GoodsIntNo, UnitId, GoodsUnit, Factor, isBase)
    VALUES (new.IntNo, 2, new.Unit2, new.Unit0Xchg/new.Unit2XChg, 0);
END
---
/*处理库存表被修改时更新单位表*/
CREATE TRIGGER STKLST_AFTER_UPDATE_1 FOR STKLST AFTER UPDATE POSITION 1 AS
BEGIN
  IF ((new.unit0 <> old.unit0)
    or (new.unit0Xchg <> old.unit0Xchg)) THEN
    UPDATE UNITLIST SET GoodsUnit = new.unit0, Factor = 1
      WHERE (goodsIntNo=old.IntNo) and (UnitId=0);
  IF ((new.unit1 <> old.unit1)
    or (new.unit1Xchg <> old.unit1Xchg)
    or (new.unit0Xchg <> old.Unit0Xchg)) THEN
    UPDATE UNITLIST SET GoodsUnit = new.unit1, Factor = new.Unit0Xchg/new.Unit1Xchg
      WHERE (goodsIntNo=old.IntNo) and (UnitId=1);
  IF ((new.unit2 <> old.unit2)
    or (new.unit2Xchg <> old.unit2Xchg)
    or (new.unit0Xchg <> old.Unit0Xchg)) THEN
    UPDATE UNITLIST SET GoodsUnit = new.unit2,
      Factor = new.Unit0Xchg/new.Unit2XChg
      WHERE (goodsIntNo=old.IntNo) and (UnitId=2);
  /* 当单位换算系数被修改时, 自动结清商品 */
  IF ((new.unit2Xchg <> old.unit2Xchg) or (new.unit1Xchg <> old.unit1Xchg)
      or (new.unit0Xchg <> old.Unit0Xchg)) THEN
    EXECUTE PROCEDURE STKLST_SET_LIMIT(new.IntNo, new.S_Date);
END
---
/*新增记录时设置默认值*/
CREATE TRIGGER STKLST_BEF_INS_SET_DEF_3 FOR STKLST BEFORE INSERT POSITION 0 AS
BEGIN
  IF(New.InUse IS NULL)                 THEN New.InUse = 1;
  IF(New.Unit2 IS NULL)                 THEN New.Unit2 = '';
  IF(New.Unit2XChg IS NULL)             THEN New.Unit2XChg = 0.00;

  IF(New.StandardOutPrice1 IS NULL)     THEN New.StandardOutPrice1 = 0.00;
  IF(New.NormalOutPrice1 IS NULL)       THEN New.NormalOutPrice1 = 0.00;
  IF(New.PrefentialPrice1 IS NULL)      THEN New.PrefentialPrice1 = 0.00;
  IF(New.MinOutPrice1 IS NULL)          THEN New.MinOutPrice1 = 0.00;
  IF(New.PFAStandardPrice1 IS NULL)     THEN New.PFAStandardPrice1 = 0.00;
  IF(New.PFAPrefentialPrice1 IS NULL)   THEN New.PFAPrefentialPrice1 = 0.00;

  IF(New.StandardOutPrice2 IS NULL)     THEN New.StandardOutPrice2 = 0.00;
  IF(New.NormalOutPrice2 IS NULL)       THEN New.NormalOutPrice2 = 0.00;
  IF(New.PrefentialPrice2 IS NULL)      THEN New.PrefentialPrice2 = 0.00;
  IF(New.MinOutPrice2 IS NULL)          THEN New.MinOutPrice2 = 0.00;
  IF(New.PFAStandardPrice2 IS NULL)     THEN New.PFAStandardPrice2 = 0.00;
  IF(New.PFAPrefentialPrice2 IS NULL)   THEN New.PFAPrefentialPrice2 = 0.00;
END
---
/*修改记录时设置默认值*/
CREATE TRIGGER STKLST_BEF_UPD_SET_DEF_3 FOR STKLST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF(New.InUse IS NULL)                 THEN New.InUse = 1;
  IF(New.Unit2 IS NULL)                 THEN New.Unit2 = '';
  IF(New.Unit2XChg IS NULL)             THEN New.Unit2XChg = 0.00;

  IF(New.StandardOutPrice1 IS NULL)     THEN New.StandardOutPrice1 = 0.00;
  IF(New.NormalOutPrice1 IS NULL)       THEN New.NormalOutPrice1 = 0.00;
  IF(New.PrefentialPrice1 IS NULL)      THEN New.PrefentialPrice1 = 0.00;
  IF(New.MinOutPrice1 IS NULL)          THEN New.MinOutPrice1 = 0.00;
  IF(New.PFAStandardPrice1 IS NULL)     THEN New.PFAStandardPrice1 = 0.00;
  IF(New.PFAPrefentialPrice1 IS NULL)   THEN New.PFAPrefentialPrice1 = 0.00;

  IF(New.StandardOutPrice2 IS NULL)     THEN New.StandardOutPrice2 = 0.00;
  IF(New.NormalOutPrice2 IS NULL)       THEN New.NormalOutPrice2 = 0.00;
  IF(New.PrefentialPrice2 IS NULL)      THEN New.PrefentialPrice2 = 0.00;
  IF(New.MinOutPrice2 IS NULL)          THEN New.MinOutPrice2 = 0.00;
  IF(New.PFAStandardPrice2 IS NULL)     THEN New.PFAStandardPrice2 = 0.00;
  IF(New.PFAPrefentialPrice2 IS NULL)   THEN New.PFAPrefentialPrice2 = 0.00;
END
---
CREATE PROCEDURE GET_PRICE_DEFINE2 (
  P_PRICENO CHAR(8),
  P_GOODSNO VARCHAR(20),
  P_CLIENTFLAG SMALLINT,
  P_UnitId SMALLINT /* 取得哪个单位的价格 */
) RETURNS (
  R_PRICEINDEX SMALLINT,
  R_DEFAULTDISCOUNT DOUBLE PRECISION,
  R_GOODSUNIT VARCHAR(4),
  R_DISCOUNT DOUBLE PRECISION,
  R_PRICE DOUBLE PRECISION
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR SELECT IntNo, PriceIndex, DefaultDiscount FROM PRICENO
    WHERE (ClientFlag = :P_ClientFlag) and (PriceNo = :P_PriceNo)
    INTO :m_IntNo, :R_PriceIndex, :R_DefaultDiscount
    DO
    BEGIN
      IF (:P_UnitId = 0) THEN /*第一单位*/
      BEGIN
        SELECT u.GOODSUNIT, d.Discount, d.Price From PriceLst d
          LEFT OUTER JOIN UNITLIST u On (u.GOODSINTNO = d.GoodsIntNo) and (u.UNITID = d.UnitId)
          LEFT OUTER JOIN STKLST stk On (stk.IntNo = d.GoodsIntNo)
          WHERE d.MainIntNo = :m_IntNo and stk.GoodsNo = :P_GoodsNo
          INTO :R_GOODSUNIT, :R_Discount, :R_Price;
        IF (R_GOODSUNIT is NULL) THEN
         SELECT Unit0 From Stklst Where GoodsNo = :P_GoodsNo INTO :R_GOODSUNIT;
      END

      IF (:P_UnitId = 1) THEN /*第二单位*/
      BEGIN
        SELECT u.GOODSUNIT, d.Discount1, d.Price1 From PriceLst d
          LEFT OUTER JOIN UNITLIST u On (u.GOODSINTNO = d.GoodsIntNo) and (u.UNITID = 1)
          LEFT OUTER JOIN STKLST stk On (stk.IntNo = d.GoodsIntNo)
          WHERE d.MainIntNo = :m_IntNo and stk.GoodsNo = :P_GoodsNo
          INTO :R_GOODSUNIT, :R_Discount, :R_Price;
        IF (R_GOODSUNIT is NULL) THEN
         SELECT Unit1 From Stklst Where GoodsNo = :P_GoodsNo INTO :R_GOODSUNIT;
      END

      IF (:P_UnitId = 2) THEN /*第三单位*/
      BEGIN
        SELECT u.GOODSUNIT, d.Discount2, d.Price2 From PriceLst d
          LEFT OUTER JOIN UNITLIST u On (u.GOODSINTNO = d.GoodsIntNo) and (u.UNITID = 2)
          LEFT OUTER JOIN STKLST stk On (stk.IntNo = d.GoodsIntNo)
          WHERE d.MainIntNo = :m_IntNo and stk.GoodsNo = :P_GoodsNo
          INTO :R_GOODSUNIT, :R_Discount, :R_Price;
        IF (R_GOODSUNIT is NULL) THEN
         SELECT Unit2 From Stklst Where GoodsNo = :P_GoodsNo INTO :R_GOODSUNIT;
      END
    END
  SUSPEND;
END
---
ALTER PROCEDURE UPDATE_CPGDS_BYACC (
  P_FLAG SMALLINT,
  P_INTNO INTEGER
)  AS
DECLARE VARIABLE m_INPRICEDEF DEC(15,4);         /*自定进价*/
DECLARE VARIABLE m_STANDARDOUTPRICE DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE DEC(15,4); /*优惠批发价*/
DECLARE VARIABLE m_GoodsIntNo Integer;           /*货号*/
DECLARE VARIABLE m_UnitId Integer;               /*单位ID*/

DECLARE VARIABLE m_STANDARDOUTPRICE1 DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE1 DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE1 DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE1 DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE1 DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE1 DEC(15,4); /*优惠批发价*/

DECLARE VARIABLE m_STANDARDOUTPRICE2 DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE2 DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE2 DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE2 DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE2 DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE2 DEC(15,4); /*优惠批发价*/
BEGIN
  IF (p_Flag = 1) then                           /* 生效 */
  BEGIN
    /* 取第一单位的旧价格 */
    FOR
      SELECT stk.INPRICEDEF, stk.STANDARDOUTPRICE, stk.NORMALOUTPRICE, stk.PREFENTIALPRICE,
        stk.MINOUTPRICE, stk.PFASTANDARDPRICE, stk.PFAPREFENTIALPRICE, 

        stk.STANDARDOUTPRICE1, stk.NORMALOUTPRICE1, stk.PREFENTIALPRICE1,
        stk.MINOUTPRICE1, stk.PFASTANDARDPRICE1, stk.PFAPREFENTIALPRICE1,

        stk.STANDARDOUTPRICE2, stk.NORMALOUTPRICE2, stk.PREFENTIALPRICE2,
        stk.MINOUTPRICE2, stk.PFASTANDARDPRICE2, stk.PFAPREFENTIALPRICE2, d.GoodsIntNo
      FROM CPGDS d, STKLST stk
      WHERE (d.MainIntNo = :p_IntNo and d.GoodsIntNo = stk.IntNo)
      INTO :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE CPGDS Set o_INPRICEDEF = :m_INPRICEDEF, o_STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          o_NORMALOUTPRICE = :m_NORMALOUTPRICE, o_PREFENTIALPRICE = :m_PREFENTIALPRICE,
          o_MINOUTPRICE = :m_MINOUTPRICE, o_PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          o_PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

          o_STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, o_NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
          o_PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   o_MINOUTPRICE1 = :m_MINOUTPRICE1,
          o_PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, o_PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

          o_STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, o_NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
          o_PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   o_MINOUTPRICE2 = :m_MINOUTPRICE2,
          o_PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, o_PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2

        WHERE (MainIntNo = :p_IntNo and GoodsIntNo = :m_GoodsIntNo); /*  and UnitId = 0 */
      END
      /* IF (m_UnitId = 1) THEN */
      /* IF (m_UnitId = 2) THEN */

    /* 取新的价格, 更新到库存清单中 */
    FOR
      SELECT n_INPRICEDEF, n_STANDARDOUTPRICE, n_NORMALOUTPRICE, n_PREFENTIALPRICE,
        n_MINOUTPRICE, n_PFASTANDARDPRICE, n_PFAPREFENTIALPRICE,

        n_STANDARDOUTPRICE1, n_NORMALOUTPRICE1, n_PREFENTIALPRICE1,
        n_MINOUTPRICE1, n_PFASTANDARDPRICE1, n_PFAPREFENTIALPRICE1,

        n_STANDARDOUTPRICE2, n_NORMALOUTPRICE2, n_PREFENTIALPRICE2,
        n_MINOUTPRICE2, n_PFASTANDARDPRICE2, n_PFAPREFENTIALPRICE2, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
          MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

          STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
          PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   MINOUTPRICE1 = :m_MINOUTPRICE1,
          PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

          STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
          PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   MINOUTPRICE2 = :m_MINOUTPRICE2,
          PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2
        WHERE (IntNo = :m_GoodsIntNo);
      END
  END
  IF (p_Flag = -1) then                          /* 取消生效 */
  BEGIN
    /* 将旧的价格更新回到库存清单中 */
    FOR
      SELECT O_INPRICEDEF, O_STANDARDOUTPRICE, O_NORMALOUTPRICE, O_PREFENTIALPRICE,
        O_MINOUTPRICE, O_PFASTANDARDPRICE, O_PFAPREFENTIALPRICE,

        O_STANDARDOUTPRICE1, O_NORMALOUTPRICE1, O_PREFENTIALPRICE1,
        O_MINOUTPRICE1, O_PFASTANDARDPRICE1, O_PFAPREFENTIALPRICE1,

        O_STANDARDOUTPRICE2, O_NORMALOUTPRICE2, O_PREFENTIALPRICE2,
        O_MINOUTPRICE2, O_PFASTANDARDPRICE2, O_PFAPREFENTIALPRICE2, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,

        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE,

        :m_STANDARDOUTPRICE1, :m_NORMALOUTPRICE1, :m_PREFENTIALPRICE1,
        :m_MINOUTPRICE1, :m_PFASTANDARDPRICE1, :m_PFAPREFENTIALPRICE1,

        :m_STANDARDOUTPRICE2, :m_NORMALOUTPRICE2, :m_PREFENTIALPRICE2,
        :m_MINOUTPRICE2, :m_PFASTANDARDPRICE2, :m_PFAPREFENTIALPRICE2, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
          MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE,

          STANDARDOUTPRICE1 = :m_STANDARDOUTPRICE1, NORMALOUTPRICE1 = :m_NORMALOUTPRICE1,
          PREFENTIALPRICE1 = :m_PREFENTIALPRICE1,   MINOUTPRICE1 = :m_MINOUTPRICE1,
          PFASTANDARDPRICE1 = :m_PFASTANDARDPRICE1, PFAPREFENTIALPRICE1 = :m_PFAPREFENTIALPRICE1,

          STANDARDOUTPRICE2 = :m_STANDARDOUTPRICE2, NORMALOUTPRICE2 = :m_NORMALOUTPRICE2,
          PREFENTIALPRICE2 = :m_PREFENTIALPRICE2,   MINOUTPRICE2 = :m_MINOUTPRICE2,
          PFASTANDARDPRICE2 = :m_PFASTANDARDPRICE2, PFAPREFENTIALPRICE2 = :m_PFAPREFENTIALPRICE2
        WHERE (IntNo = :m_GoodsIntNo);
      END
  END
END
---
DECLARE EXTERNAL FUNCTION ZS_GETUNITXCHGSTR2
   CSTRING(8), CSTRING(8), CSTRING(8),
   DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION
   RETURNS CSTRING(26)
  ENTRY_POINT "zsudf_GetUnitXchgStr2"  MODULE_NAME "zsudf"
---
DECLARE EXTERNAL FUNCTION ZS_GETALTUNIT2
   CSTRING(8), CSTRING(8), CSTRING(8), INTEGER,
   DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, CSTRING(60)
   RETURNS CSTRING(255)
  ENTRY_POINT "zsudf_GetAltUnit2"  MODULE_NAME "zsudf"
---
CREATE PROCEDURE UPDAET_UnitList_Def AS
DECLARE VARIABLE m_UnitListIntNo INTEGER;
DECLARE VARIABLE m_GoodsIntNo INTEGER;
DECLARE VARIABLE m_Unit2 CHAR(20);
DECLARE VARIABLE m_Unit0XChg DECIMAL (15,4);
DECLARE VARIABLE m_Unit1XChg DECIMAL (15,4);
DECLARE VARIABLE m_Unit2XChg DECIMAL (15,4);
BEGIN
  m_UnitListIntNo = 0;
  FOR
    SELECT IntNo, Unit2, Unit0XChg, Unit1XChg, Unit2XChg FROM Stklst
      INTO :m_GoodsIntNo, :m_Unit2, :m_Unit0XChg, :m_Unit1XChg, :m_Unit2XChg DO
    BEGIN
      SELECT IntNo FROM UnitList
        WHERE (GoodsIntno = :m_GoodsIntNo AND UnitId = 2) INTO :m_UnitListIntNo;
      IF (:m_UnitListIntNo > 0) THEN
      BEGIN
        UPDATE UnitList SET GoodsUnit = :m_Unit2,
          Factor = :m_Unit0Xchg/:m_Unit2XChg, isBase = 0
          WHERE (IntNo = :m_UnitListIntNo);
      END
      ELSE
      BEGIN
        INSERT INTO UnitList (GoodsIntNo, UnitId, GoodsUnit, Factor, isBase)
          VALUES (:m_GoodsIntNo, 2, :m_Unit2, :m_Unit0Xchg/:m_Unit2XChg, 0);
      END
    END
END
---
CREATE VIEW VIEW_POS_REALLIST (
  INTNO,
  UPDATEACC,
  PRINTCOUNT,
  FLAG1,
  FLAG2,
  FLAG3,
  FLAG4,
  LISTDATE,
  LISTTIME,
  LISTNO,
  COMMENT,
  GDSPAY,
  INAMOUNT,
  PANDIANNO,
  LISTTYPE,
  PERSONDEAL,
  REMARK,
  OPERNO,
  LREMARK2
) AS
Select m.IntNo, ' ' as UpdateAcc, m.PrintCount, m.Flag1, m.Flag2, m.Flag3, m.Flag4,
  m.ListDate, m.ListTime, m.ListNo, m.Comment, m.Gdspay, m.InAmount, m.PanDianNo,
  m.ListType, m.PersonDeal, m.Remark, m.OperNo, m.LRemark2
  FROM RealList m WHERE m.UpdateAcc = '*'
---
CREATE PROCEDURE V_POS_RealGds  RETURNS (
  INTNO INTEGER,
  MAININTNO INTEGER,
  GOODSINTNO INTEGER,
  UNITID INTEGER,
  STOCKINTNO INTEGER,
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80)
) AS 
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
BEGIN
  FOR SELECT d.GoodsIntNo, d.UnitId, d.StockIntNo, d.INTNO, d.MAININTNO, d.QTY, d.PRICE, d.INPRICE,
    d.AMOUNT, d.INAMOUNT, d.REMARK, d.UPDATEFLAG, StkName.StockId, d.MANUFACTDATE_INT, d.EXPIREDATE_INT,
    d.BATCHNO, d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
    FROM RealGds d, StkName
    LEFT OUTER JOIN RealList m ON (m.IntNo = d.MainIntNo) and (m.UpdateAcc = '*')
    WHERE StkName.IntNo = d.StockIntNo
    INTO :GoodsIntNo, :UnitId, :StockIntNo, :IntNo, :MainIntNo, :Qty, :Price, :InPrice,
         :Amount, :InAmount, :Remark, :UpdateFlag, :StockId, :ManuFactDate_Int, :ExpireDate_Int,
         :BatchNo, :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
    DO
    BEGIN
      IF (GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN
        SELECT GoodsNo FROM Stklst Where IntNo = :GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo = :GoodsIntNo and u.UnitId = :UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN
        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER,
  QREMAINNEW DOUBLE PRECISION,
  QREMAINOLD DOUBLE PRECISION
) AS
BEGIN
FOR
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int,
    0.00 AS QRemainNew,
    SUM(-d.QtyIn+d.QtyOut-d.QtyMoveIn+d.QtyMoveOut-d.QtyPan-d.QtyAssIn+d.QtyAssOut+d.QtyUse) AS QRemainOld
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int,
      SUM(d.Qty*u.Factor) AS QRemainNew,
      0.00 AS QRemainOld
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo

      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int,
      0.00 AS QRemainNew,
      SUM(d.QRemain) AS QRemainOld
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
          ,:ManuFactDate_Int, :ExpireDate_Int, :QRemainNew, :QRemainOld
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
--
/*更新第三单位换算*/
2004.10.12_01/SQL3
UPDATE STKLST SET Unit2 = Unit1, Unit2XChg = Unit1XChg
--
/*在单位表中生成第三单位*/
2004.10.12_01/SQL4
EXECUTE PROCEDURE UPDAET_UnitList_Def
--
2004.10.12_01/SQL5
Update SpecGds set Price = price1, Discount = Discount1
---
Update SpecGds set Price1 = 0, Discount1 = 1, Price2 = 0, Discount2 = 1
--
2004.09.13_01/UPGRADE_DETAIL
1. 修改订单到货数量的设置方式，使其支持自定义内容及批号;
--
2004.09.13_01/SQL1
/* 创建销售订单明细表的一个存储过程 */
CREATE PROCEDURE UPDATE_SaleGDSBK_DELIVERQTY (
  Qty NUMERIC(15, 4),
  Direct SMALLINT,
  BookListNo CHAR(14),
  GoodsIntNo INTEGER,
  UnitId SMALLINT,
  CustomIntNo1 INTEGER,
  CustomIntNo2 INTEGER,
  CustomIntNo3 INTEGER,
  CustomIntNo4 INTEGER,
  CustomIntNo5 INTEGER,
  ManuFactDate_Int SMALLINT,
  ExpireDate_Int SMALLINT,
  BatchNo CHAR(20)

)  AS
DECLARE VARIABLE M_IntNo INTEGER;
DECLARE VARIABLE SaleGds_Factor NUMERIC(15, 4);
DECLARE VARIABLE SaleGdsBk_Factor NUMERIC(15, 4);
BEGIN
  IF (GoodsIntNo IS NULL) THEN EXIT;
  SELECT IntNo FROM OutListBK WHERE ListNo = :BookListNo INTO :M_IntNo;
  IF (M_IntNo IS NULL) THEN EXIT;
  SELECT u.Factor FROM UnitList u
    WHERE ((u.GoodsIntNo = :GoodsIntNo) AND (u.UnitId = :UnitId))
      INTO :SaleGds_Factor;
  /* 先不处理自定义内容及批号 */
  FOR SELECT u.Factor From UnitList u, SaleGdsBk d
    WHERE (d.MainIntNo = :M_IntNo) AND (d.GoodsIntNo = :GoodsIntNo) AND
        (u.GoodsIntNo=d.GoodsIntNo AND u.UnitId=d.UnitId)
       AND ((CustomIntNo1 = :CustomIntNo1) OR (CustomIntNo1 IS NULL AND :CustomIntNo1 IS NULL))
       AND ((CustomIntNo2 = :CustomIntNo2) OR (CustomIntNo2 IS NULL AND :CustomIntNo2 IS NULL))
       AND ((CustomIntNo3 = :CustomIntNo3) OR (CustomIntNo3 IS NULL AND :CustomIntNo3 IS NULL))
       AND ((CustomIntNo4 = :CustomIntNo4) OR (CustomIntNo4 IS NULL AND :CustomIntNo4 IS NULL))
       AND ((CustomIntNo5 = :CustomIntNo5) OR (CustomIntNo5 IS NULL AND :CustomIntNo5 IS NULL))
       AND ((ManufactDate_Int = :ManufactDate_Int) OR (ManufactDate_Int IS NULL AND :ManufactDate_Int IS NULL))
       AND ((ExpireDate_Int = :ExpireDate_Int) OR (ExpireDate_Int IS NULL AND :ExpireDate_Int IS NULL))
       AND ((BatchNo = :BatchNo) OR (BatchNo IS NULL AND :BatchNo IS NULL))
    INTO :SaleGdsBk_Factor DO
    UPDATE SaleGdsBK SET DeliverQty = DeliverQty + (:Qty*:SaleGds_Factor/:SaleGdsBk_Factor*:Direct)
      WHERE (MainIntNo = :M_IntNo) AND (GoodsIntNo = :GoodsIntNo)
       AND ((CustomIntNo1 = :CustomIntNo1) OR (CustomIntNo1 IS NULL AND :CustomIntNo1 IS NULL))
       AND ((CustomIntNo2 = :CustomIntNo2) OR (CustomIntNo2 IS NULL AND :CustomIntNo2 IS NULL))
       AND ((CustomIntNo3 = :CustomIntNo3) OR (CustomIntNo3 IS NULL AND :CustomIntNo3 IS NULL))
       AND ((CustomIntNo4 = :CustomIntNo4) OR (CustomIntNo4 IS NULL AND :CustomIntNo4 IS NULL))
       AND ((CustomIntNo5 = :CustomIntNo5) OR (CustomIntNo5 IS NULL AND :CustomIntNo5 IS NULL))
       AND ((ManufactDate_Int = :ManufactDate_Int) OR (ManufactDate_Int IS NULL AND :ManufactDate_Int IS NULL))
       AND ((ExpireDate_Int = :ExpireDate_Int) OR (ExpireDate_Int IS NULL AND :ExpireDate_Int IS NULL))
       AND ((BatchNo = :BatchNo) OR (BatchNo IS NULL AND :BatchNo IS NULL));
  /* 如果两个CustomIntNo都为NULL, 则认为是同一货号可以进行数量合并 */
  /* 040913处理支持自定义内容及批号 */
END
---
/* 创建销售明细表三个触发器 */
CREATE TRIGGER SALEGDS_AFTER_Upd_DELIVERQTY FOR SALEGDS AFTER Update POSITION 0 AS
BEGIN
  /* 040913处理支持自定义内容及批号 */
  if ((old.goodsintno <> new.goodsintno) OR (old.qty <>new.qty)
   OR (old.UnitId <> new.UnitId) OR (old.CustomIntNo1 <> new.CustomIntNo1)
   OR (old.CustomIntNo2 <> new.CustomIntNo2) OR (old.CustomIntNo3 <> new.CustomIntNo3)
   OR (old.CustomIntNo4 <> new.CustomIntNo4) OR (old.CustomIntNo5 <> new.CustomIntNo5)
   OR (old.ManuFactDate_Int <> new.ManuFactDate_Int)
   OR (old.ExpireDate_Int <> old.ExpireDate_Int)
   OR (old.BatchNo <> new.BatchNo)) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(old.Qty, -1, old.BookListNo,
      old.GoodsIntNo, old.UnitId, old.CustomIntNo1, old.CustomIntNo2, old.CustomIntNo3,
      old.CustomIntNo4, old.CustomIntNo5, old.ManuFactDate_Int, old.ExpireDate_Int, old.BatchNo);

    EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(new.Qty, 1, new.BookListNo,
      new.GoodsIntNo, new.UnitId, new.CustomIntNo1, new.CustomIntNo2, new.CustomIntNo3,
      new.CustomIntNo4, new.CustomIntNo5, new.ManuFactDate_Int, new.ExpireDate_Int, new.BatchNo);
  END
END
---
CREATE TRIGGER SALEGDS_AFTER_Ins_DELIVERQTY FOR SALEGDS AFTER Insert POSITION 0 AS
BEGIN
  /* 040913处理支持自定义内容及批号 */
  EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(new.Qty, 1, new.BookListNo,
    new.GoodsIntNo, new.UnitId, new.CustomIntNo1, new.CustomIntNo2, new.CustomIntNo3,
    new.CustomIntNo4, new.CustomIntNo5, new.ManuFactDate_Int, new.ExpireDate_Int, new.BatchNo);
END
---
CREATE TRIGGER SALEGDS_AFTER_Del_DELIVERQTY FOR SALEGDS AFTER Delete POSITION 0 AS
BEGIN
  /* 040913处理支持自定义内容及批号 */
  EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(old.Qty, -1, old.BookListNo,
    old.GoodsIntNo, old.UnitId, old.CustomIntNo1, old.CustomIntNo2, old.CustomIntNo3,
    old.CustomIntNo4, old.CustomIntNo5, old.ManuFactDate_Int, old.ExpireDate_Int, old.BatchNo);
END
---

/* 创建进货订单明细表的一个存储过程 */
CREATE PROCEDURE UPDATE_BuyGDSBK_DELIVERQTY (
  QTY NUMERIC(15, 4),
  Direct SMALLINT,
  BookListNo CHAR(14),
  GoodsIntNo INTEGER,
  UnitId SMALLINT,
  CustomIntNo1 INTEGER,
  CustomIntNo2 INTEGER,
  CustomIntNo3 INTEGER,
  CustomIntNo4 INTEGER,
  CustomIntNo5 INTEGER,
  ManuFactDate_Int SMALLINT,
  ExpireDate_Int SMALLINT,
  BatchNo CHAR(20)
)  AS
DECLARE VARIABLE M_IntNo Integer;
DECLARE VARIABLE BuyGds_Factor NUMERIC(15, 4);
DECLARE VARIABLE BuyGdsBk_Factor NUMERIC(15, 4);
BEGIN
  if (GoodsIntNo IS NULL) THEN exit;
  SELECT IntNo from InListBK WHERE ListNo = :BookListNo INTO :M_IntNo;
  IF (M_IntNo IS NULL) THEN exit;
  SELECT u.Factor from UnitList u
    WHERE ((u.GoodsIntNo = :GoodsIntNo) AND (u.UnitId = :UnitId))
      into :BuyGds_Factor;

  FOR SELECT u.Factor From UnitList u, BuyGdsBk d
    WHERE (d.MainIntNo = :M_IntNo) AND (d.GoodsIntNo = :GoodsIntNo) AND
        (u.GoodsIntNo=d.GoodsIntNo AND u.UnitId=d.UnitId)
       AND ((CustomIntNo1 = :CustomIntNo1) OR (CustomIntNo1 IS NULL AND :CustomIntNo1 IS NULL))
       AND ((CustomIntNo2 = :CustomIntNo2) OR (CustomIntNo2 IS NULL AND :CustomIntNo2 IS NULL))
       AND ((CustomIntNo3 = :CustomIntNo3) OR (CustomIntNo3 IS NULL AND :CustomIntNo3 IS NULL))
       AND ((CustomIntNo4 = :CustomIntNo4) OR (CustomIntNo4 IS NULL AND :CustomIntNo4 IS NULL))
       AND ((CustomIntNo5 = :CustomIntNo5) OR (CustomIntNo5 IS NULL AND :CustomIntNo5 IS NULL))
       AND ((ManufactDate_Int = :ManufactDate_Int) OR (ManufactDate_Int IS NULL AND :ManufactDate_Int IS NULL))
       AND ((ExpireDate_Int = :ExpireDate_Int) OR (ExpireDate_Int IS NULL AND :ExpireDate_Int IS NULL))
       AND ((BatchNo = :BatchNo) OR (BatchNo IS NULL AND :BatchNo IS NULL))
      Into :BuyGdsBk_Factor DO
  UPDATE BuyGdsBK SET DeliverQty = DeliverQty + (:Qty*:BuyGds_Factor/:BuyGdsBk_Factor*:Direct)
    WHERE (MainIntNo = :M_IntNo) AND (GoodsIntNo = :GoodsIntNo)
      AND ((CustomIntNo1 = :CustomIntNo1) OR (CustomIntNo1 IS NULL AND :CustomIntNo1 IS NULL))
      AND ((CustomIntNo2 = :CustomIntNo2) OR (CustomIntNo2 IS NULL AND :CustomIntNo2 IS NULL))
      AND ((CustomIntNo3 = :CustomIntNo3) OR (CustomIntNo3 IS NULL AND :CustomIntNo3 IS NULL))
      AND ((CustomIntNo4 = :CustomIntNo4) OR (CustomIntNo4 IS NULL AND :CustomIntNo4 IS NULL))
      AND ((CustomIntNo5 = :CustomIntNo5) OR (CustomIntNo5 IS NULL AND :CustomIntNo5 IS NULL))
      AND ((ManufactDate_Int = :ManufactDate_Int) OR (ManufactDate_Int IS NULL AND :ManufactDate_Int IS NULL))

      AND ((ExpireDate_Int = :ExpireDate_Int) OR (ExpireDate_Int IS NULL AND :ExpireDate_Int IS NULL))
      AND ((BatchNo = :BatchNo) OR (BatchNo IS NULL AND :BatchNo IS NULL));
  /* 如果两个CustomIntNo都为NULL, 则认为是同一货号可以进行数量合并 */
  /* 040913处理支持自定义内容及批号 */
END
---
/* 创建进货订单明细表三个触发器 */
CREATE TRIGGER BuyGDS_AFTER_Upd_DELIVERQTY FOR BuyGDS AFTER Update POSITION 0 AS
BEGIN
  /* 040913处理支持自定义内容及批号 */
  if ((old.goodsintno <> new.goodsintno) OR (old.qty <>new.qty)
    OR (old.UnitId <> new.UnitId) OR (old.CustomIntNo1 <> new.CustomIntNo1)
    OR (old.CustomIntNo2 <> new.CustomIntNo2) OR (old.CustomIntNo3 <> new.CustomIntNo3)
    OR (old.CustomIntNo4 <> new.CustomIntNo4) OR (old.CustomIntNo5 <> new.CustomIntNo5)
    OR (old.ManuFactDate_Int <> new.ManuFactDate_Int)
    OR (old.ExpireDate_Int <> old.ExpireDate_Int)
    OR (old.BatchNo <> new.BatchNo)) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(old.Qty, -1, old.BookListNo,
      old.GoodsIntNo, old.UnitId, old.CustomIntNo1, old.CustomIntNo2, old.CustomIntNo3,
      old.CustomIntNo4, old.CustomIntNo5, old.ManuFactDate_Int, old.ExpireDate_Int, old.BatchNo);

    EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(new.Qty, 1, new.BookListNo,
      new.GoodsIntNo, new.UnitId, new.CustomIntNo1, new.CustomIntNo2, new.CustomIntNo3,
      new.CustomIntNo4, new.CustomIntNo5, new.ManuFactDate_Int, new.ExpireDate_Int, new.BatchNo);
  END
END
---
CREATE TRIGGER BuyGDS_AFTER_Ins_DELIVERQTY FOR BuyGDS AFTER Insert POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(new.Qty, 1, new.BookListNo,
    new.GoodsIntNo, new.UnitId, new.CustomIntNo1, new.CustomIntNo2, new.CustomIntNo3,
    new.CustomIntNo4, new.CustomIntNo5, new.ManuFactDate_Int, new.ExpireDate_Int, new.BatchNo);
END
---
CREATE TRIGGER BuyGDS_AFTER_Del_DELIVERQTY FOR BuyGDS AFTER Delete POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(old.Qty, -1, old.BookListNo,
    old.GoodsIntNo, old.UnitId, old.CustomIntNo1, old.CustomIntNo2, old.CustomIntNo3,
    old.CustomIntNo4, old.CustomIntNo5, old.ManuFactDate_Int, old.ExpireDate_Int, old.BatchNo);
END
--
2004.08.18_01/UPGRADE_DETAIL
1. 新增: 颜色尺码主/明细表;
--
2004.08.18_01/SQL1
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('CUSTOMGRPNAME1')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('CUSTOMGRPNAME1', '', '颜色组', '12')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('CUSTOMGRPNAME2')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('CUSTOMGRPNAME2', '', '尺码组', '12')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('FIELDNO')
  AND UPPER(TableName) = UPPER('CUSTOMGRPLIST')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('FIELDNO', 'CUSTOMGRPLIST', '颜色尺码类型', '12')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('CUSTOMGRPNAME')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('CUSTOMGRPNAME', '', '组名称', '20')
---
DELETE FROM FieldDefine WHERE UPPER(FieldName) = UPPER('UserGroup')
  AND Upper(TableName) = Upper('BankName')

---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('UserGroup', 'BankName', '分组', '12')
---
/*第一步， 生成子*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2004.08.18_01/SQL2
/*第三步，处理生成子*/
CREATE GENERATOR INTNO_CustomGrpList
---
SET GENERATOR INTNO_CustomGrpList TO 10
---
CREATE GENERATOR INTNO_CustomGrpDetail
---
SET GENERATOR INTNO_CustomGrpDetail TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END

    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END

    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPLIST')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpList, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMGRPDETAIL')  THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CustomGrpDetail, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
---
/*第三步， 新建主表及相关的索引字段等*/
CREATE TABLE CustomGrpList (
  IntNo  TINTNO,
  FieldNo SMALLINT DEFAULT 0 NOT NULL,
  CustomGrpName CHAR(20) DEFAULT '' NOT NULL,
  Remark TRemark,
  CONSTRAINT CustomGrpList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT CustomGrpList_UNIQUE_1 UNIQUE (FieldNo, CustomGrpName)
)
---
CREATE INDEX IdxCustomGrpList_FieldNo ON CustomGrpList(FieldNo);
--
2004.08.18_01/SQL3
/*第四步  建明细表及主键约束*/
CREATE TABLE CustomGrpDetail(
  IntNo TINTNO,
  MainIntNo TINTNO,
  CustomIntNo TINTNO,
  Remark TRemark,

  CONSTRAINT GrpDetial_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_GrpD_CustomGrpList FOREIGN KEY (MAININTNO) REFERENCES CustomGrpList(INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_GrpD_CustomValues FOREIGN KEY (CustomIntNo) REFERENCES CustomValues(IntNo)
    ON UPDATE NO ACTION ON DELETE CASCADE
)
---
CREATE INDEX IdxC_GrpDetial_MainIntNo ON CustomGrpDetail(MainIntNo);
--
2004.08.18_01/SQL4
/*第五步 建立主表触发， 用于新增记录的初始化*/
CREATE TRIGGER C_GrpList_BEF_INS_SET_DEF FOR CustomGrpList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.IntNo is NULL) THEN new.IntNo = 0;
  IF (new.FieldNo is NULL) THEN new.FieldNo = 0;
  IF (new.CustomGrpName is NULL) THEN new.CustomGrpName = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
END
---
CREATE TRIGGER C_GrpList_BEF_UPD_SET_DEF FOR CustomGrpList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.IntNo is NULL) THEN new.IntNo = 0;
  IF (new.FieldNo is NULL) THEN new.FieldNo = 0;
  IF (new.CustomGrpName is NULL) THEN new.CustomGrpName = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
END
---
/*第六步 建立明细表触发， 用于新增记录的初始化*/
CREATE TRIGGER C_GrpDetail_BEF_INS_SET_DEF FOR CustomGrpDetail BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.IntNo is NULL) THEN new.IntNo = 0;
  IF (new.MainIntNo  is NULL) THEN new.MainIntNo = 0;
  IF (new.CustomIntNo is NULL) THEN new.CustomIntNo = 0;
  IF (new.Remark is NULL) THEN new.Remark = '';
END
---
CREATE TRIGGER C_GrpDetail_BEF_UPD_SET_DEF FOR CustomGrpDetail BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.IntNo is NULL) THEN new.IntNo = 0;
  IF (new.MainIntNo  is NULL) THEN new.MainIntNo = 0;
  IF (new.CustomIntNo is NULL) THEN new.CustomIntNo = 0;
  IF (new.Remark is NULL) THEN new.Remark = '';
END
---
/*第七步 主表被删除， 明细表自动被删除*/
CREATE TRIGGER C_GrpList_BEF_DEL_DEL_DETAIL FOR CustomGrpList
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM CustomGrpDetail WHERE MainIntNo = old.IntNo;
END
---
/*第八步 更改库存清单, 加上字段*/
Alter Table Stklst
  Add CustomGrpName1 Char(20)   Default '',
  Add CustomGrpName2 Char(20)   Default ''
---
CREATE PROCEDURE UPDATE_Stk_GrpName(
  p_GrpNameFlag SmallInt,
  p_GrpValue Char(20),
  p_GrpName Char(20)
)  AS
BEGIN
  IF (:p_GrpNameFlag = 1) THEN
  BEGIN
    UPDATE Stklst SET CustomGrpName1 = :p_GrpValue
      WHERE CustomGrpName1 = :p_GrpName;
  END

  IF (:p_GrpNameFlag = 2) THEN
  BEGIN
    UPDATE Stklst SET CustomGrpName2 = :p_GrpValue
      WHERE CustomGrpName2 = :p_GrpName;
  END
END
---
CREATE TRIGGER C_GrpList_AFT_UPD_SET_STK FOR CustomGrpList AFTER UPDATE POSITION 0 AS
BEGIN
  IF (OLD.CustomGrpName <> NEW.CustomGrpName) THEN
    EXECUTE PROCEDURE UPDATE_Stk_GrpName(NEW.FieldNo, NEW.CustomGrpName, OLD.CustomGrpName);
END
---
CREATE TRIGGER C_GrpList_AFT_DEL_SET_STK FOR CustomGrpList AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_Stk_GrpName(OLD.FieldNo, '', OLD.CustomGrpName);
END
---
ALTER TABLE BankName ADD UserGroup Char(12) Default '' NOT NULL
---
CREATE TRIGGER BANKNAME_BEFORE_INSERT_SET_DEF FOR BANKNAME BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.BankName is NULL) THEN new.BankName = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.BankAccNo is NULL) THEN new.BankAccNo = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
---
CREATE TRIGGER BANKNAME_BEFORE_UPDATE_SET_DEF FOR BANKNAME BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.BankName is NULL) THEN new.BankName = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.BankAccNo is NULL) THEN new.BankAccNo = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
--
2004.06.20_01/UPGRADE_DETAIL
1. 库存清单新增字段以支持[外贸行业版]
--
2004.06.20_01/SQL1
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('CmpyBoss')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('CmpyContact')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('CmpyFName')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsEng')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsCode')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackSizeLong')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackSizeWidth')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackSizeHeight')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackSizeUnit')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackVolumeCBM')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackVolumeCUFT')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsSizeLength')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsSizeWidth')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsSizeHeight')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackNetWeight')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackGrossWeight')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('InnerQty')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('MasterQty')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PackMode')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('MinOrderQtyByPack')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CmpyBoss', '', '负责人', 'TPERSONNAME')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CmpyContact', '', '联系人', 'TCONTACT')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('CmpyFName', '', '公司全称', 'TCmpyFName')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('GoodsEng', '', '品名(英文)', 'TGoods')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
 VALUES ('GoodsCode', '', '厂家编号', 'TGoodsNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('PackSizeLength', '', '外箱长', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('PackSizeWidth', '', '外箱宽', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('PackSizeHeight', '', '外箱高', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('PackSizeUnit', '', '计量单位', '8')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
 VALUES ('PackVolumeCBM', '', '立方米')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
 VALUES ('PackVolumeCUFT', '', '立方英尺')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('GoodsSizeLength', '', '产品长', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('GoodsSizeHeight', '', '产品高', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('GoodsSizeWidth', '', '产品宽', '6')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
 VALUES ('PackNetWeight', '', '净重')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
 VALUES ('PackGrossWeight', '', '毛重')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('InnerQty', '', '内箱数量', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('MasterQty', '', '外箱数量', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('PackMode', '', '包装方式', '8')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
 VALUES ('MinOrderQtyByPack', '', '起订量', '6')
---
/*默认单位数值为0 表示  单位0表示厘米 1表示英寸 */
Alter Table Stklst
  Add GoodsEng            VarChar(80)   Default '' not null,
  Add GoodsCode           VarChar(20)   Default '' not null,
  Add PackSizeLength      Dec(15, 4)    Default 0.00 not null,
  Add PackSizeWidth       Dec(15, 4)    Default 0.00 not null,
  Add PackSizeHeight      Dec(15, 4)    Default 0.00 not null,

  Add PackSizeUnit        SmallInt      Default 0 not null,

  Add PackVolumeCBM       Dec(15, 4)    Default 0.00 not null,
  Add PackVolumeCUFT      Dec(15, 4)    Default 0.00 not null,

  Add GoodsSizeLength     Dec(15, 4)    Default 0.00 not null,
  Add GoodsSizeWidth      Dec(15, 4)    Default 0.00 not null,
  Add GoodsSizeHeight     Dec(15, 4)    Default 0.00 not null,

  Add PackNetWeight       Dec(15, 4)    Default 0.00 not null,
  Add PackGrossWeight     Dec(15, 4)    Default 0.00 not null,

  Add InnerQty            Integer       Default 0 not null,
  Add MasterQty           Integer       Default 0 not null,

  Add PackMode            VarChar(20)   Default '' not null,
  Add MinOrderQtyByPack   Integer       Default 0 not null
---
/*新增时, 初始化 设置默认值*/
CREATE TRIGGER STKLST_BEF_INS_SET_DEF_2 FOR STKLST BEFORE INSERT POSITION 0 AS
BEGIN
  if (New.GoodsEng is null) then New.GoodsEng = '';
  if (New.GoodsCode is null) then New.GoodsCode = '';

  if (New.PackSizeLength is null) then New.PackSizeLength = 0.00;
  if (New.PackSizeWidth is null) then New.PackSizeWidth = 0.00;
  if (New.PackSizeHeight is null) then New.PackSizeHeight = 0.00;
  if (New.PackSizeUnit is null) then New.PackSizeUnit = 0;

  if (New.PackVolumeCBM is null)  then New.PackVolumeCBM  = 0.00;
  if (New.PackVolumeCUFT is null) then New.PackVolumeCUFT = 0.00;

  if (New.GoodsSizeLength is null) then New.GoodsSizeLength = 0.00;
  if (New.GoodsSizeWidth is null)  then New.GoodsSizeWidth  = 0.00;
  if (New.GoodsSizeHeight is null) then New.GoodsSizeHeight = 0.00;

  if (New.PackNetWeight is null) then New.PackNetWeight = 0.00;
  if (New.PackGrossWeight is null) then New.PackGrossWeight = 0.00;

  if (New.InnerQty is null) then New.InnerQty = 0;
  if (New.MasterQty is null) then New.MasterQty = 0;

  if (New.PackMode is null) then New.PackMode = '';
  if (New.MinOrderQtyByPack is null) then New.MinOrderQtyByPack = 0;
END
---
/*被修改时 设置默认值*/
CREATE TRIGGER STKLST_BEF_UPD_SET_DEF_2 FOR STKLST BEFORE UPDATE POSITION 0 AS
BEGIN
  if (New.GoodsEng is null) then New.GoodsEng = '';
  if (New.GoodsCode is null) then New.GoodsCode = '';

  if (New.PackSizeLength is null) then New.PackSizeLength = 0.00;
  if (New.PackSizeWidth is null) then New.PackSizeWidth = 0.00;
  if (New.PackSizeHeight is null) then New.PackSizeHeight = 0.00;
  if (New.PackSizeUnit is null) then New.PackSizeUnit = 0;

  if (New.PackVolumeCBM is null)  then New.PackVolumeCBM  = 0.00;
  if (New.PackVolumeCUFT is null) then New.PackVolumeCUFT = 0.00;

  if (New.GoodsSizeLength is null) then New.GoodsSizeLength = 0.00;
  if (New.GoodsSizeWidth is null)  then New.GoodsSizeWidth  = 0.00;
  if (New.GoodsSizeHeight is null) then New.GoodsSizeHeight = 0.00;

  if (New.PackNetWeight is null) then New.PackNetWeight = 0.00;
  if (New.PackGrossWeight is null) then New.PackGrossWeight = 0.00;

  if (New.InnerQty is null) then New.InnerQty = 0;
  if (New.MasterQty is null) then New.MasterQty = 0;

  if (New.PackMode is null) then New.PackMode = '';
  if (New.MinOrderQtyByPack is null) then New.MinOrderQtyByPack = 0;
END
--
2004.05.30_01/UPGRADE_DETAIL
1. 新增复制操作员套账权限
--
2004.05.30_01/SQL1
CREATE PROCEDURE SYS_COPY_OPERACC
(
  OPERNO VARCHAR(10),
  OLDACCNO CHAR(1),
  NEWACCNO CHAR(1)
)
AS
DECLARE VARIABLE m_AccNo VARCHAR(20);
DECLARE VARIABLE m_Actived SmallInt;
DECLARE VARIABLE m_ItemNo VARCHAR(6);
DECLARE VARIABLE m_AccessCode VarChar(40);
DECLARE VARIABLE m_Prompt VarChar(40);
DECLARE VARIABLE m_RRun SmallInt;
DECLARE VARIABLE m_RInsert SmallInt;
DECLARE VARIABLE m_RDelete SmallInt;
DECLARE VARIABLE m_RUpdate SmallInt;
DECLARE VARIABLE m_RPrint SmallInt;
DECLARE VARIABLE m_RCopy SmallInt;
DECLARE VARIABLE m_RSetup SmallInt;
DECLARE VARIABLE m_RInput SmallInt;
DECLARE VARIABLE m_ROutput SmallInt;
DECLARE VARIABLE m_CheckSum VarChar(40);
BEGIN
  m_ACCNO = NewAccNo;
  FOR
    SELECT ACTIVED, ITEMNO, ACCESS_CODE, PROMPT,
      RRUN, RINSERT, RDELETE, RUPDATE, RPRINT, RCOPY, RSETUP, RINPUT, ROUTPUT, CHECKSUM
      FROM ACCESS WHERE (OPERNO = :OperNo) AND (AccNo = :OldAccNo)
      INTO
      :m_ACTIVED, :m_ITEMNO, :m_ACCESSCODE, :m_PROMPT,
      :m_RRUN, :m_RINSERT, :m_RDELETE, :m_RUPDATE, :m_RPRINT, :m_RCOPY, :m_RSETUP, :m_RINPUT, :m_ROUTPUT, :m_CHECKSUM
      DO
      INSERT INTO ACCESS (OperNo, ACCNO, ACTIVED, ITEMNO, ACCESS_CODE, PROMPT,
        RRUN, RINSERT, RDELETE, RUPDATE, RPRINT, RCOPY, RSETUP, RINPUT, ROUTPUT, CHECKSUM)
        VALUES (:OperNo, :m_ACCNO, :m_ACTIVED, :m_ITEMNO, :m_ACCESSCODE, :m_PROMPT,
         :m_RRUN, :m_RINSERT, :m_RDELETE, :m_RUPDATE, :m_RPRINT, :m_RCOPY, :m_RSETUP, :m_RINPUT, :m_ROUTPUT, :m_CHECKSUM);

END
--
2004.05.20_01/UPGRADE_DETAIL
1. 组装/拆装进仓时能更新自定进价/最新进价/近进货日
2. 新增出库字段定义
--
2004.05.20_01/SQL1
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('SumAmountUse')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('SumAmountUse', '', '出库金额', 'TAmount')
---
CREATE GENERATOR INTNO_ASS_CALC_INPRICE
---
SET GENERATOR INTNO_ASS_CALC_INPRICE TO 0
---
CREATE GENERATOR INTNO_ASS_CHK_DIRECT_ONLY
---
SET GENERATOR INTNO_ASS_CHK_DIRECT_ONLY TO 0
---
CREATE GENERATOR INTNO_ASSUPDATEINPRICEDEF
---
SET GENERATOR INTNO_ASSUPDATEINPRICEDEF TO 0
---
CREATE GENERATOR INTNO_ASSUPDATEPRICEUPIN
---
SET GENERATOR INTNO_ASSUPDATEPRICEUPIN TO 0
---
/*20040517为当组装/折装时,数量>0时把价格写入最新进价及更新近进货日*/
CREATE PROCEDURE ASSLIST_UPDATE_INPRICEDEF
(
  P_MAININTNO INTEGER
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
BEGIN
  FOR SELECT GoodsIntNo, UnitId, InPrice FROM ASSGDS
    WHERE (MainIntNo = :p_MainIntNo) and (Qty > 0)
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END
/*更新自定进价*/
      UPDATE STKLST SET InPriceDef = :m_Price
        WHERE (IntNo = :m_GoodsIntNo);
    END
END
---
CREATE PROCEDURE ASSLIST_UPDATE_PRICEUPIN
(
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP
)
AS

DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
BEGIN
  FOR SELECT GoodsIntNo, UnitId, InPrice FROM ASSGDS
    WHERE (MainIntNo = :p_MainIntNo) and (Qty > 0)
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END
/*更新最新进价及近进货日*/
      UPDATE STKLST SET PriceUpIn = :m_Price,
        DateUpIn = :p_ListDate
        WHERE (IntNo = :m_GoodsIntNo) and
          ((DateUpIn <= :p_ListDate) or (DateUpIn is NULL));
    END
END
---
CREATE TRIGGER ASSLIST_AFTER_UPDATE_UPDATEACC FOR ASSLIST
ACTIVE AFTER UPDATE POSITION 0
AS
/*执行进仓/取消进仓的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'ASSGDS', old.IntNo);
      /* 如果是进仓, 则更新最新进价/自定进价/近进货日 */
      IF (m_Flag = 1) THEN
      BEGIN
        IF (GEN_ID(INTNO_ASSUPDATEINPRICEDEF, 0) = 1) THEN
          EXECUTE PROCEDURE ASSLIST_UPDATE_INPRICEDEF(old.IntNo);
        IF (GEN_ID(INTNO_AssUpdatePriceUpIn, 0) = 1) THEN
          EXECUTE PROCEDURE ASSLIST_UPDATE_PRICEUPIN(old.IntNo, new.ListDate);
      END
    END
  END
END
--
2004.05.20_01/SQL2
CREATE EXCEPTION MSG_ASSGDS_DIRECT_Not_REPEAT "组装/拆解明细表中成品只能有一种"
---
CREATE PROCEDURE CHECK_ASSGDS_DIRECT_MUST_ONLY(
p_MainIntNo Integer
)AS
DECLARE VARIABLE m_RecCount Integer;
BEGIN
  m_RecCount = 0;
  FOR
    Select COUNT(*) from AssGdS
      Where (MainIntNo = :p_MainIntNo AND DIRECT = 0)
      INTO :m_RecCount
  DO
    BEGIN
      IF (m_RecCount > 1) THEN EXCEPTION MSG_ASSGDS_DIRECT_Not_REPEAT;
    END
END
---
CREATE PROCEDURE ASSGDS_UPDATE_DIRECT_INPRICE(
p_MainIntNo Integer
)AS
DECLARE VARIABLE m_MAmount Dec(15,4);
DECLARE VARIABLE m_Qty Dec(15,4);
BEGIN
  m_MAmount = 0; /*原料金额初始化*/
  m_Qty     = 0; /*成品数量初始化*/
  SELECT Sum(InAmount) FROM ASSGDS
    Where (MainIntNo = :p_MainIntNo AND DIRECT = 1)
    INTO :m_MAmount;

  SELECT Sum(Qty) FROM ASSGDS
    Where (MainIntNo = :p_MainIntNo AND DIRECT = 0)
    INTO :m_Qty;

  IF (Round(m_Qty, 4) <> 0) THEN
  BEGIN
    UPDATE Assgds SET InPrice = ROUND(-(:m_MAmount)/:m_Qty, 8),
      InAmount = InPrice*Qty Where (MainIntNo = :p_MainIntNo AND DIRECT = 0);
  END
END
---
CREATE TRIGGER ASSLIST_BEF_UPDATEACC_1 FOR ASSLIST
ACTIVE BEFORE UPDATE POSITION 2
AS /*执行进仓/取消进仓的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  /*进仓前自动计算进价及成本*/
  IF (GEN_ID(INTNO_ASS_CALC_INPRICE, 0) = 1) THEN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1) AND (GEN_ID(IntNo_PriceFIFO, 0) <> 1)) THEN
  BEGIN  /*账务清队及先入先出法时不起作用*/
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      IF (m_Flag = 1) THEN   /*如果是进仓则检查是否需要判断*/
      BEGIN
        IF (GEN_ID(INTNO_ASS_CHK_DIRECT_ONLY, 0) = 1) THEN
          EXECUTE PROCEDURE CHECK_ASSGDS_DIRECT_MUST_ONLY(old.IntNo);
        EXECUTE PROCEDURE ASSGDS_UPDATE_DIRECT_INPRICE(old.IntNo);
        New.InAmount = 0;
      END
    END
  END
END
--
2004.05.20_01/SQL3
CREATE VIEW VIEW_GET_CLIENT_BIRTHDAY (
  INTNO,
  CMPYNO,
  CMPYNAME,
  GDSPAY,
  ACTPAY,
  INITREMAIN,
  TELE,
  FAX,
  PRICENO,
  CMPYBOSS,
  CMPYCONTACT,
  ADDRESS,
  TAXNO,
  LREMARK,
  CMPYFNAME,
  CMPYBANKNAME,
  CMPYBANKNO,
  CLIENTFLAG,
  PERSONDEAL,
  CLIENTTYPE,
  BIRTHDAY,
  SEX,
  PERSONCHARACTER,
  BirthdayDate0,
  BirthdayDate1
) AS
SELECT c.IntNo, c.CmpyNo, c.CmpyName, c.Gdspay, c.Actpay, c.InitRemain, c.Tele,
c.Fax, c.PriceNo, c.CmpyBoss, c.CmpyContact, c.Address, c.TaxNo, c.LRemark, c.CmpyFName,
c.CmpyBankName, c.CmpyBankNo, c.ClientFlag, c.PersonDeal, c.ClientType,
c.Birthday, c.Sex, c.PersonCharacter,
zs_GetBirthdayDate(c.Birthday, 0),
zs_GetBirthdayDate(c.Birthday, 1)
FROM Client c
--
2004.04.30_01/UPGRADE_DETAIL
1. 新增: 仓库出库主/明细表;
--
2004.04.30_01/SQL1
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = Upper('SumAmoumtUse')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('SumAmoumtUse', '', '总金额', 'TSumAmount')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'USELIST' AND UPPER(FIELDNAME) = Upper('Department')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'USELIST' AND UPPER(FIELDNAME) = Upper('GdsPay')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'PANGDS' AND UPPER(FIELDNAME) = Upper('Price')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'REALGDS' AND UPPER(FIELDNAME) = Upper('Price')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QtyUse')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('AmountUse')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Department', 'UseList', '接收部门', 'TDepartment')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('QtyUse', '', '出库数量', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('AmountUse', '', '出库金额', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('GdsPay', 'USELIST', '金额', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Price', 'PANGDS', '参考售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('Price', 'REALGDS', '参考售价', 'TPrice')
---
/*第负一步, 库存商品明细及进销存数量统计表*/
/*共八个存储过程GETSTOCKIO, GETSTOCKIOEX, GETSTOCKIOEXB*/
/*共八个存储过程GETSTOCKIOLIST, GETSTOCKIOLISTEXB, GETSTOCKIOLIST*/
/*共八个存储过程GET_PAN_FROM_REAL, GET_PAN_FROM_REAL_EXB*/
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOSUM (
  P_DATEFROM TIMESTAMP,

  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),

  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  SUSPEND;
END
---
/*第零步 检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc VARChar(1);
BEGIN
  SUSPEND;
END
---
/*第一步， 检查单号是否存在*/
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT

)
AS
BEGIN
  SUSPEND;
END
---
/*第二步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  SUSPEND;
END
---
/*第三步， 处理先入先出法的问题*/
ALTER PROCEDURE LIST_SET_FIFO_INPRICE (

  P_ADDFLAG SMALLINT,

  P_GDSTABLE VARCHAR(15),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  SUSPEND;
END
---
/*第四步， 进出仓时， 相应的库存量变化*/
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN
  SUSPEND;
END
---
/*第四-1步， 生成子*/
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2004.04.30_01/SQL2
/*第五步，处理生成子*/
CREATE GENERATOR INTNO_USEGDS
---
SET GENERATOR INTNO_USEGDS TO 10
---
CREATE GENERATOR INTNO_USELIST
---
SET GENERATOR INTNO_USELIST TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='USELIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='USEGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_USEGDS, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
---
/*第六步， 新建出库表及相关的索引字段等*/
CREATE TABLE USELIST (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  COMMENT TCOMMENT,
  GDSPAY TAMOUNT,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  LREMARK2 VARCHAR(120),
  GOODSGDSPAY	NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  CONSTRAINT USEList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT USEList_UNIQUE_1 UNIQUE (LISTNO)
)
---
/*建立主表索引*/
CREATE INDEX IDX_USELIST_LISTDATE ON USELIST(LISTDATE);
---
/*第七步  建出库明细表及主键约束*/
CREATE TABLE USEGDS(
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO INTEGER,
  UNITID TGOODSUNITID,
  STOCKINTNO TINTNO,
  QTY TQTY,
  PRICE TPRICE,
  DISCOUNT TDISCOUNT,
  AMOUNT TAMOUNT,
  REMARK TREMARK,
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  TAXRATE	NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  GOODSAMOUNT	NUMERIC(15, 4) DEFAULT 0 NOT NULL,
  TAXPAYMENT	NUMERIC(15, 4) DEFAULT 0 NOT NULL,

  CONSTRAINT USEGDS_PRIMARY_KEY PRIMARY KEY (INTNO),

  CONSTRAINT FK_USEGDS_USELIST FOREIGN KEY (MAININTNO) REFERENCES USELIST (INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,

  CONSTRAINT FK_USEGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE,

  CONSTRAINT FK_USEGDS_STKNAME FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO)
    ON UPDATE CASCADE ON DELETE SET NULL
)
---
/*建立明细表索引*/
CREATE INDEX IDX_USEGDS_MAININTNO ON USEGDS(MAININTNO);
--
2004.04.30_01/SQL3
/*第八步建立主表触发， 用于新增记录的初始化*/
CREATE TRIGGER USEList_BEF_INS_SET_DEF FOR USEList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
---
CREATE TRIGGER USEList_BEF_UPD_SET_DEF FOR USEList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
---
/*第九步， 建立主表的相应触发 新增记录时, 不允许UpdateAcc标志为(*)*/
CREATE TRIGGER USELIST_BEFORE_INSERT_UPDATEACC FOR USELIST
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (new.UpdateAcc = '*') THEN
    EXCEPTION CANNOT_SET_UPDATEACC_WHILE_NEW;
END
---
/*检查单号是否存在, 如存在, 则激活Exception*/
CREATE TRIGGER USELIST_BEFORE_INS_CHK_LISTNO FOR USELIST
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (20, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
/*同时修改进出仓标志及日期的提示*/
CREATE TRIGGER USELIST_BEFORE_UPDATE_CHK_UPACC FOR USELIST
ACTIVE BEFORE UPDATE POSITION 0
 AS
BEGIN
  IF ((old.UPDATEACC <> new.UpdateAcc) and (old.LISTDATE <> new.LISTDATE)) THEN
    EXCEPTION CANNOT_CHANGE_BOTH_ACC_LISTDATE;
END
---
/*进出仓时， 检查货号是否已结清*/
CREATE TRIGGER USELIST_BEFORE_UPDATE_UPDATEACC FOR USELIST
ACTIVE BEFORE UPDATE POSITION 3
AS DECLARE VARIABLE m_Changeable SMALLINT;
BEGIN
  IF (new.UPDATEACC <> old.UPDATEACC) THEN
    EXECUTE PROCEDURE LIST_CHECK_STK_CHANGEABLE ('USELIST', new.IntNo, old.ListDate, 1)
      RETURNING_VALUES :m_Changeable;
END
---
/*若当前启用先入先出法， 则处理相关的单价， 金额*/
CREATE TRIGGER USELIST_BEF_UPD_ACC_SETINPRICE FOR USELIST
ACTIVE BEFORE UPDATE POSITION 3
AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_Amount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'USEGDS', old.IntNo);
      SELECT SUM(Amount) FROM Usegds WHERE MainIntNo = old.IntNo INTO :m_Amount;
      IF (m_Amount IS NULL) THEN m_Amount = 0;
      new.Gdspay = m_Amount;
    END
  END
END
---
/*执行进仓/取消进仓的操作,  处理相关的库存量*/
CREATE TRIGGER USELIST_AFTER_UPDATE_UPDATEACC FOR USELIST
ACTIVE AFTER UPDATE POSITION 2
AS
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'USEGDS', old.IntNo);
    END
  END
END
---
/*如果单据已出仓, 则不允许删除(除非正在做帐务清除)*/
CREATE TRIGGER USELIST_BEFORE_DELETE_CHK_UPACC FOR USELIST

ACTIVE BEFORE DELETE POSITION 2
AS
BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_OUT_CANNOT_DELETE;
END
---
/*主表被删除， 明细表自动被删除*/
CREATE TRIGGER USELIST_BEFORE_DEL_DEL_DETAIL FOR USELIST
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM USEGDS WHERE MAININTNO = old.intno;
END
--
2004.04.30_01/SQL4
/* 检查是否已出仓, 如果已出仓, 则不允许新增 */
CREATE TRIGGER USEGDS_BEFORE_INSERT_CHK_UPACC FOR USEGDS
ACTIVE BEFORE INSERT POSITION 1
AS DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('USELIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;

END
---
/* 检查是否已出仓, 如果已出仓, 则不允许修改 */
CREATE TRIGGER USEGDS_BEFORE_UPDATE_CHK_UPACC FOR USEGDS
ACTIVE BEFORE UPDATE POSITION 0
AS DECLARE
VARIABLE m_Changeable SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (not ((new.GoodsIntNo is NULL) and (not (old.GoodsIntNo is NULL))

      and (new.Qty=old.Qty) and (new.Price=old.Price)

      and (new.Discount=old.Discount) ))
      THEN
        EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('USELIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
  END
END
---
/* 检查是否已出仓, 如果已出仓, 则不允许删除 */
CREATE TRIGGER USEGDS_BEFORE_DELETE_CHK_UPACC FOR USEGDS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE m_Changeable SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('USELIST', old.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
END
---
/*更新主表的货款  存储过程*/
CREATE PROCEDURE UPDATE_USELIST_AMOUNT
(
  P_GDSPAY NUMERIC(15, 4),
  P_GOODSGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)
AS
BEGIN
  UPDATE USELIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
/*更新主表的货款 -插入后-  触发器*/
CREATE TRIGGER USEGDS_AFTER_INSERT_SET_MASTER FOR USEGDS
ACTIVE AFTER INSERT POSITION 0
AS
BEGIN
  EXECUTE PROCEDURE UPDATE_USELIST_AMOUNT(new.AMOUNT, new.GoodsAmount, 1, new.MAININTNO);
END
---
/*更新主表的货款 -修改后-  触发器*/
CREATE TRIGGER USEGDS_AFTER_UPDATE_SET_MASTER FOR USEGDS
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.AMOUNT - old.AMOUNT;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_USELIST_AMOUNT(:m_amount, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
/*更新主表的货款 -删除后-  触发器*/
CREATE TRIGGER USEGDS_AFTER_DELETE_SET_MASTER FOR USEGDS
ACTIVE AFTER DELETE POSITION 0
AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_USELIST_AMOUNT(old.AMOUNT, old.GoodsAmount, -1, old.MAININTNO);
  END
END
--
2004.04.30_01/SQL5
/*第零步, 检查主表是否已进仓*/
ALTER PROCEDURE CHECK_LIST_UPDATEACC (
  P_LISTNAME CHAR(12),
  P_MAININTNO INTEGER,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_UpdateAcc VARChar(1);
BEGIN
  r_Changeable = 1; IF (GEN_ID(INTNO_DataInit, 0) = 1) THEN EXIT;
  IF (p_ListName = 'INLIST') THEN
    SELECT UpdateAcc From INLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'OUTLIST') THEN
    SELECT UpdateAcc From OUTLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'MOVELIST') THEN
    SELECT UpdateAcc From MOVELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'PANLIST') THEN
    SELECT UpdateAcc From PANLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'REALLIST') THEN
    SELECT UpdateAcc From REALLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'ASSLIST') THEN
    SELECT UpdateAcc From ASSLIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (p_ListName = 'USELIST') THEN
    SELECT UpdateAcc From USELIST WHERE INTNO = :p_MainIntNo INTO :m_UpdateAcc;
  IF (m_UpdateAcc = '*') THEN R_CHANGEABLE = 0;
  ELSE R_CHANGEABLE = 1;
  IF (R_CHANGEABLE = 0) THEN IF (P_RAISEEXCEPTION = 1) THEN EXCEPTION MSG_MASTER_UPDATEACC_LOCK;
END

---
/* 第一步，检查单号是否存在 以及修改相应的存储过程 */
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN

    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 18) THEN  /* 时段促销 */
  BEGIN
    SELECT COUNT(*) FROM SpecList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 20) THEN  /* 出库 */
  BEGIN
    SELECT COUNT(*) FROM USEList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
---
/*第二步， 检查货号是否已结清*/
ALTER PROCEDURE LIST_CHECK_STK_CHANGEABLE (
  TBLNAME VARCHAR(20),
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_CHANGEABLE SMALLINT
) AS
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  IF (TBLNAME = 'INLIST') THEN

  BEGIN
    FOR SELECT GOODSINTNO FROM BUYGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'OUTLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM SALEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'MOVELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM MOVEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'PANLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM PANGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN

      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'ASSLIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM ASSGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  IF (TBLNAME = 'USELIST') THEN
  BEGIN
    FOR SELECT GOODSINTNO FROM USEGDS WHERE MAININTNO = :p_MainIntNo
      INTO :m_GoodsIntNo DO
    BEGIN
      EXECUTE PROCEDURE STKLST_CHECK_LIMIT (:m_GoodsIntNo, :p_ListDate, :p_raiseException)
        RETURNING_VALUES :r_Changeable;
      IF (r_Changeable = 0) THEN
        IF (p_RaiseException = 1) THEN
          EXCEPTION STKLST_LIMIT_NOT_CHANGEABLE;
    END
  END
  SUSPEND;
END
---
/*第三步， 处理先入先出法的成本问题*/
ALTER PROCEDURE LIST_SET_FIFO_INPRICE (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(15),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  IF (NOT (p_AddFlag = 1)) THEN                       /* 先进先出时, 不允许取消进出仓 */
    EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC;

  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE SALEGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5

      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE PANGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE ASSGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE BUYGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'USEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM USEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE USEGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END
END
---
/*第四步， 进出仓时， 相应的库存量变化*/
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN

  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'USEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM USEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
END
--
2004.04.30_01/SQL6
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),

  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  /* 发货 */
  FOR
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

  WorkMode = 7;
  /* 差额盘点 */
  FOR
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

  WorkMode = 12;
  /* 组装与拆解 */
  FOR
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  /* 调拨入 */
  FOR
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

  WorkMode = 3;
  /* 调拨出 */
  FOR
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

  WorkMode = 1;
  /* 进货 */
  FOR
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      ,0.0 as AmountUse
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

  WorkMode = 20;
  /* 出库 */
  FOR
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, Amount as AmountUse
      FROM UseGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
          ,:AmountUse
      DO SUSPEND;

END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),

  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  AMOUNTUSE NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;

  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut,

      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as QtyUse,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,0.0 as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 20;
  FOR  /* 出库 */
    SELECT M.IntNo From USELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, u.Factor*d.Qty as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT, d.Amount as AmountUse
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT, :AmountUse
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      FROM BUYGDS d, InList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      FROM SALEGDS d, Outlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM PANGDS d, PanList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM ASSGDS d, AssList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      FROM USEGDS d, Uselist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d, Inlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL

    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM USEGDS d, USELIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
  DO
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---

ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),

  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d, INLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m

      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 出库 */
    SELECT 20, d.GoodsIntNo, m.ListDate, m.Department, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, -d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM USEGDS d, USELIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
  BEGIN
    IF (BatchNo = '') THEN BatchNo = NULL;
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOSUM (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  QTYUSE NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),

  AMOUNTUSE NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR
    Select GoodsIntNo, StockIntNo,
      Sum(QtyIn)-Sum(QtyOut)+Sum(QtyMoveIn)-Sum(QtyMoveOut)+Sum(QtyPan)+
      Sum(QtyAssIn-QtyAssOut)-Sum(QtyUse) as QInitRemain,
      Sum(AmountIn)-Sum(AmountOut)+Sum(AmountMoveIn)-Sum(AmountMoveOut)+Sum(AmountPan)+
      Sum(AmountAssIn-AmountAssOut)-Sum(AmountUse) as InitAmount,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut, 0.0 as QtyUse,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut, 0.0 as AmountUse
      From GetstockIolist('0001.1.1', :p_DateFrom-1)
      Group by GoodsIntNo, StockIntNo
    UNION ALL
    Select GoodsIntNo, StockIntNo,
      0.0 as QInitRemain,
      0.0 as InitAmount,
      Sum(QtyIn) as QtyIn, Sum(QtyOut) as QtyOut, Sum(QtyMoveIn) as QtyMoveIn,
      Sum(QtyMoveOut) as QtyMoveOut, Sum(QtyPan) as QtyPan,
      Sum(QtyAssIn) as QtyAssIn, Sum(QtyAssOut) as QtyAssOut, Sum(QtyUse) as QtyUse,
      Sum(AmountIn) as AmountIn, Sum(AmountOut) as AmountOut, Sum(AmountMoveIn) as AmountMoveIn,
      Sum(AmountMoveOut) as AmountMoveOut, Sum(AmountPan) as AmountPan,
      Sum(AmountAssIn) as AmountAssIn, Sum(AmountAssOut) as AmountAssOut, Sum(AmountUse) as AmountUse
      From GetstockIolist(:p_DateFrom, :p_DateTo)
      Group by GoodsIntNo, StockIntNo
        INTO :GoodsIntNo, :StockIntNo, :QInitRemain, :InitAmount,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut, :QtyUse, 
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut, :AmountUse
    DO SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut) as InAmount
    FROM GETSTOCKIOLIST(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
    FROM Stklsts d
    GROUP BY d.GoodsIntNo, d.StockIntNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER
) AS
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse) as InAmount
    FROM GETSTOCKIOLIST(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
    FROM Stklsts d
    GROUP BY d.GoodsIntNo, d.StockIntNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
---
ALTER PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS
BEGIN
FOR
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut-d.QtyUse) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut-d.AmountUse) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
          ,:ManuFactDate_Int, :ExpireDate_Int
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
---
/*货号被删除时, 将货号代入备注*/
CREATE TRIGGER STKLST_BEFORE_DEL_SAVE_GOODSNO FOR STKLST BEFORE DELETE POSITION 10 AS
BEGIN
  UPDATE SALEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE SALEGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE MOVEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE PANGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE REALGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE ASSGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE USEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
END
--
2004.04.10_01/UPGRADE_DETAIL
1. 优化: 分时模式下, 后台即使修改货号, 也能正常并入前台销售数据;
2. 调整: SpecList的触发器;
--
2004.04.10_01/SQL2
CREATE TRIGGER SpecList_BEF_INS_SET_DEF FOR SpecList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';

  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
---
CREATE TRIGGER SpecList_BEF_UPD_SET_DEF FOR SpecList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
  IF (new.LREMARK2 is NULL) THEN new.LREMARK2 = '';
  IF (new.Comment is NULL) THEN new.Comment = '';
END
--
2004.04.10_01/SQL1
ALTER PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  GoodsIntNo Integer,
  UnitId Integer,
  StockIntNo Integer,
  BankIntNo Integer,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80),
  ISSPECPRICE SMALLINT
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
BEGIN
  FOR SELECT
    d.GoodsIntNo,
    d.UnitId,
    d.StockIntNo,
    d.BankIntNo,
    d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,
    d.AMOUNT,

    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5,
    d.isSpecPrice
    FROM Salegds d, StkName, BankName
    WHERE StkName.IntNo = d.StockIntNo and d.BankIntNo = BankName.IntNo
    INTO :GoodsIntNo, :UnitId, :StockIntNo, :BankIntNo,
      :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      ,:isSpecPrice
    DO
    BEGIN
      IF (GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN
        SELECT GoodsNo FROM Stklst Where IntNo = :GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo = :GoodsIntNo and u.UnitId = :UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN
        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
--
2004.03.28_01/UPGRADE_DETAIL
1. 调整: 时段促销表, 处理成主明细表结构;
2. 调整: 扩充[代号]表的[内容]字段的宽度到120;
--
2004.03.28_01/SQL1
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS

(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  SUSPEND;
END
--
2004.03.28_01/SQL2
/*第一步， 创建生成子， 并修改生成子， 使支持时段促销明细表*/
CREATE GENERATOR INTNO_SPECGDS
---
SET GENERATOR INTNO_SPECGDS TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECGDS, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2004.03.28_01/SQL3
/*第二步， 新建时段促销表及相关的索引字段等*/
CREATE TABLE SPECLIST (
  INTNO TINTNO,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  COMMENT TCOMMENT,
  UPDATEACC TUPDATEFLAG,
  DATETIMEFROM DATE NOT NULL,
  DATETIMETO DATE NOT NULL,
  DAILYTIMEFROM DATE NOT NULL,
  DAILYTIMETO DATE NOT NULL,

  OPERNO TOPERNO,
  PRINTCOUNT SMALLINT,
  REMARK TREMARK,
  LREMARK2 VARCHAR(120),
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  UPDATEUCT INTEGER,

  INSERTUCT INTEGER,

  CONSTRAINT SpecList_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT SpecList_UNIQUE_1 UNIQUE (LISTNO)
)
---
/*建立主表索引*/
CREATE INDEX IDX_SPECLIST_LISTDATE ON SPECLIST(LISTDATE);
---
/*第三步  建时段促销明细表及主键约束：在同一张促销单中，不能同时出现相同的货号*/
CREATE TABLE SPECGDS(
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO TINTNO,
  Price1        TPrice,
  Price2        TPrice,
  Price3        TPrice,
  Discount1     TDiscount,
  Discount2     TDiscount,
  Discount3     TDiscount,
  REMARK TREMARK,

  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,
  CONSTRAINT SPECGDS_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT SPECGDS_UNIQUE_1 UNIQUE (MainIntNo, GoodsIntNo),
  CONSTRAINT FK_SPECGDS_SPECLIST FOREIGN KEY (MAININTNO) REFERENCES SPECLIST(INTNO)
    ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT FK_SPECGDS_STKLST FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
    ON UPDATE CASCADE ON DELETE CASCADE
)
---
/*建立明细表索引*/
CREATE INDEX IDX_SPECGDS_MAININTNO ON SPECGDS(MAININTNO);
---
/*第四步建立主表触发， 用于新增记录的初始化*/
CREATE TRIGGER SpecList_BEF_INS_SET_DEF FOR SpecList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
CREATE TRIGGER SpecList_BEF_UPD_SET_DEF FOR SpecList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
/*第五步，建立主表触发，用于处理前后台数据传输*/
CREATE TRIGGER SpecList_BEF_UPD_EVENTLOG FOR SpecList BEFORE UPDATE POSITION 10 AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SpecList_BEF_INS_EVENTLOG FOR SpecList BEFORE INSERT POSITION 10 AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SpecList_AFT_DEL_EVENT_LOG FOR SpecList AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('SPECLIST', old.IntNo);
END
---
/*第六步，建立明细表触发，用于处理前后台数据传输*/
CREATE TRIGGER SPECGDS_BEF_UPD_EVENTLOG FOR SpecGds BEFORE UPDATE POSITION 10 AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SPECGDS_BEF_INS_EVENTLOG FOR SpecGds BEFORE INSERT POSITION 10 AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SPECGDS_AFT_DEL_EVENT_LOG FOR SpecGds AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('SPECGDs', old.IntNo);
END
---
CREATE TRIGGER SpecGds_BEF_INS_SET_DEF FOR SpecGds BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.REMARK is NULL) THEN new.REMARK = '';
END
---
CREATE TRIGGER SpecGds_BEF_UPD_SET_DEF FOR SpecGds BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.REMARK is NULL) THEN new.REMARK = '';
END
---
/*第七步， 建立明细表触发， 用于处理 货号不能为空及不能重复*/
CREATE EXCEPTION MSG_SPECGDS_GoodsNo_Not_Null "促销明细表中货号不能为空"
---
CREATE EXCEPTION MSG_SPECGDS_GoodsNo_MUST_DIFF "促销明细表中货号不能重复"
---
CREATE PROCEDURE CHECK_SPECGDS_GOODSNO_MUST_DIFF(
p_MainIntNo Integer,
p_GoodsIntNo Integer
)AS
DECLARE VARIABLE m_RecCount Integer;
BEGIN
  m_RecCount = 0;
  FOR
    Select COUNT(*) from SPECGdS
      Where (MainIntNo = :p_MainIntNo AND GoodsIntNo = :p_GoodsIntNO)
      INTO :m_RecCount
  DO
    BEGIN
      IF (m_RecCount > 0) THEN EXCEPTION MSG_SPECGDS_GoodsNo_MUST_DIFF;
    END
END
---
CREATE TRIGGER SPECGDS_BEFORE_UPDATE_1 FOR SPECGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.GoodsIntNo is null) then EXCEPTION MSG_SPECGDS_GoodsNo_Not_Null;
    IF (new.GoodsIntNo <> old.GoodsIntNo) THEN
      EXECUTE PROCEDURE CHECK_SPECGDS_GoodsNo_MUST_DIFF(new.MainIntNo, new.GoodsIntNo);
  END
END
---
CREATE TRIGGER SPECGDS_BEFORE_INSERT_1 FOR SPECGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.GoodsIntNo is null) then EXCEPTION MSG_SPECGDS_GoodsNo_Not_Null;
  EXECUTE PROCEDURE CHECK_SPECGDS_GoodsNo_MUST_DIFF(new.MainIntNo, new.GoodsIntNo);
END
---
/* 第八步， 创建触发器 检查单号是否存在 以及修改相应的存储过程 */ 
CREATE TRIGGER SpecList_BEF_INS_CHK_LISTNO FOR SpecList
ACTIVE BEFORE INSERT POSITION 0
AS
/*检查单号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (18, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN

    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END

  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 18) THEN  /* 时段促销 */
  BEGIN
    SELECT COUNT(*) FROM SpecList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
---
/*第九步，创建存储过程， 根据货号及当前日期时间获得该货号的促销价格*/
Create PROCEDURE GetSpecListGoodsIntNo
(
  P_GoodsIntNo Integer,
  P_Date TimeStamp,
  P_Time TimeStamp
)
RETURNS
(
  R_IntNo INTEGER
)
AS
BEGIN
  R_IntNo = null;
  FOR
    SELECT d.IntNo FROM SpecList m, SpecGds d
      WHERE (m.UpdateAcc = '*')
        AND (m.DateTimeTo+1 > :P_Date) AND (m.DateTimeFrom <= :P_Date)
        AND (m.DailyTimeTo >= :P_Time) AND (m.DailyTimeFrom <= :P_Time)
        AND (m.IntNo = d.MainIntNo) AND (d.GoodsIntNo = :P_GoodsIntNo)
        ORDER BY m.ListDate Desc, m.ListNo DESC
      INTO :R_IntNo
    DO
    BEGIN
      SUSPEND;
      EXIT; /* 只取最新的一条记录 */
    END
END
--
2004.03.28_01/SQL4
/*扩充[代号]表的[内容]字段的宽度, */
Alter Table Phrase Add tempField VarCHAR(120) DEFAULT '' NOT NULL
---
UpDate Phrase Set tempField = PhraseText
---
Alter Table Phrase Drop PhraseText
---
Alter Table Phrase Add PhraseText VarChar(120) Default '' not Null
---
Update Phrase Set PhraseText = tempField
---
Alter Table Phrase Drop tempField
---
UPDATE FieldDefine SET DWIDTH = null, FSize = 120 where Upper(FieldName) = Upper('PhraseText')
---
UPDATE DomainDefine SET FieldSize = 120 where Upper(DomainName) = Upper('TPHRASETEXT')
--
2004.03.24_01/UPGRADE_DETAIL
1. 调整: 后台并入前台交班数据时, 能够保留原始单据出仓状态;(不强制出仓);
--
2004.03.24_01/SQL1
CREATE VIEW VIEW_POS_OUTLIST2 (
  INTNO,
  UPDATEACC,
  PRINTCOUNT,
  FLAG1,
  FLAG2,
  FLAG3,
  FLAG4,
  LISTDATE,
  LISTTIME,
  LISTNO,
  CMPYNAME,
  GDSPAY,
  ACTPAY,
  PROFIT,
  PERSONDEAL,
  DEPARTMENT,
  REMARK,
  OPERNO,
  LREMARK2
) AS
Select m.IntNo, m.UpdateAcc, m.PrintCount, m.Flag1, m.Flag2, m.Flag3, m.Flag4,
  m.ListDate, m.ListTime, m.ListNo, m.CmpyName,
  m.Gdspay, m.Actpay, m.Profit,
  m.PersonDeal, m.Department, m.Remark, m.OperNo, m.LRemark2
  FROM OUTLIST m
--
2004.03.09_01/UPGRADE_DETAIL
1. 增加选项: 可设置[库存量不允许为负数];
--
2004.03.09_01/SQL1
CREATE GENERATOR INTNO_NEGQTY
---
SET GENERATOR INTNO_NEGQTY TO 0
---
CREATE EXCEPTION MSG_QREMAIN_NOT_NEGATIVE "库存量不允许为负数"
---
CREATE TRIGGER STKLSTS_BEF_INS_CheckQty FOR STKLSTS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_NEGQTY, 0) = 1) THEN
  BEGIN
    IF (ROUND(NEW.QREMAIN, 4) < 0) then EXCEPTION MSG_QREMAIN_NOT_NEGATIVE;
  END
END

---
CREATE TRIGGER STKLSTS_BEF_UPD_CheckQty FOR STKLSTS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_NEGQTY, 0) = 1) THEN
  BEGIN
    IF (ROUND(NEW.QREMAIN, 4) < 0) then EXCEPTION MSG_QREMAIN_NOT_NEGATIVE;

  END
END
--
2004.02.28_01/UPGRADE_DETAIL
1. 修正[供应商帐/客户帐]余额更新的问题;
--
2004.02.12_01/UPGRADE_DETAIL
1. 销售明细表增加字段[促销标志]
2. 增加[时段促销]表
--
2004.02.12_01/SQL6
CREATE PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80),
  ISSPECPRICE SMALLINT
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;

DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId Integer;
BEGIN
  FOR SELECT d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,
    d.AMOUNT,
    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    d.GoodsIntNo, d.UnitId,
    StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5,
    d.isSpecPrice
    FROM Salegds d, StkName, BankName
    WHERE StkName.IntNo = d.StockIntNo and d.BankIntNo = BankName.IntNo
    INTO :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :m_GoodsIntNo, :m_UnitId, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      ,:isSpecPrice
    DO
    BEGIN
      IF (m_GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN

        SELECT GoodsNo FROM Stklst Where IntNo = :m_GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo=:m_GoodsIntNo and u.UnitId=:m_UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN
        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
--
2004.02.12_01/SQL5
CREATE GENERATOR INTNO_SPECLIST
---
SET GENERATOR INTNO_SPECLIST TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    IF (P_TableName='SPECLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SPECLIST, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2004.02.12_01/SQL4
Create PROCEDURE GetSpecListGoodsIntNo
(
  P_GoodsIntNo Integer,
  P_Date TimeStamp,
  P_Time TimeStamp
)
RETURNS
(
  R_IntNo INTEGER
)
AS
BEGIN
  R_IntNo = null;
  FOR
    Select IntNo From SpecList
    Where (UpdateAcc = '*') and (GoodsIntNo = :P_GoodsIntNo)
    and (DateTimeTo+1 > :P_Date) and (DateTimeFrom <= :P_Date)
    and (DailyTimeTo >= :P_Time) and (DailyTimeFrom <= :P_Time)

    order by ListDate Desc, ListNo Desc
    InTo :R_IntNo
    DO
    BEGIN
      SusPend;
      EXIT;
    END
END
--
2004.02.12_01/SQL3
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DateTimeFrom')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DateTimeTo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DailyTimeFrom')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DailyTimeTo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('Price1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('Discount1')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('IsSpecPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DFormat, DLabel, DWidth, EMask)
 VALUES ('DateTimeFrom', '', 'YYYY.MM.DD', '开始日期', '10', '!9999.99.99;1;_')
---
INSERT INTO FieldDefine (FieldName, TableName, DFormat, DLabel, DWidth, EMask)
 VALUES ('DateTimeTo', '', 'YYYY.MM.DD', '结束日期', '10', '!9999.99.99;1;_')
---
INSERT INTO FieldDefine (FieldName, TableName, DFORMAT, DLabel, DWIDTH, EMASK)
  VALUES ('DailyTimeFrom', '', 'HH:MM:SS', '开始时间', '8', '!90:00:00;1;_')
---
INSERT INTO FieldDefine (FieldName, TableName, DFORMAT, DLabel, DWIDTH, EMASK)
  VALUES ('DailyTimeTo', '', 'HH:MM:SS', '结束时间', '8', '!90:00:00;1;_')
---

INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('Price1', '', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('Discount1', '', 'TDiscount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWIDTH)
  VALUES ('IsSpecPrice', '', '促销', '2')
--
2004.02.12_01/SQL1
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  SUSPEND;
END
--
2004.02.12_01/SQL2
Alter Table Salegds Add IsSpecPrice SmallInt Default 0 Not Null
/*销售明细表增加 IsSpecPrice 促销标志， 默认为0即非促销商品， 1为促销商品*/
---
/*建立时段触销表*/
Create Table SpecList
(
  INTNO	TINTNO,
  LISTDATE	TLISTDATE,
  LISTTIME	TLISTTIME,
  LISTNO	TLISTNO,
  UPDATEACC	TUPDATEFLAG,
  Disabled	SMALLINT Default 0 Not Null ,
  DateTimeFrom  TimeStamp Not Null,
  DateTimeTo    TimeStamp Not Null,
  DailyTimeFrom TimeStamp Not Null,
  DailyTimeTo   TimeStamp Not Null,
  GOODSINTNO	INTEGER,
  Price1        TPrice,
  Price2        TPrice,
  Price3        TPrice,
  Discount1     TDiscount,
  Discount2     TDiscount,
  Discount3     TDiscount,
  QtyLimit      TQty,
  QtySold       TQty,
  OPERNO	TOPERNO,
  PRINTCOUNT	SMALLINT,
  REMARK	TREMARK,
  PERSONDEAL	TPERSONNAME,
  DEPARTMENT	TDEPARTMENT,
  UPDATEUCT INTEGER,
  INSERTUCT INTEGER,
CONSTRAINT SpecList_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT SpecList_UNIQUE_1 UNIQUE (LISTNO),
CONSTRAINT FK_SpecList_STK FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO)
  ON UPDATE CASCADE
  ON DELETE CASCADE
)
---
CREATE TRIGGER SpecList_BEF_INS_SET_DEF FOR SpecList BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
CREATE TRIGGER SpecList_BEF_UPD_SET_DEF FOR SpecList BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.UpdateAcc is NULL) THEN new.UpdateAcc = '';
  IF (new.OPERNO is NULL) THEN new.OPERNO = '';
  IF (new.REMARK is NULL) THEN new.REMARK = '';
  IF (new.PERSONDEAL is NULL) THEN new.PERSONDEAL = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
CREATE TRIGGER SpecList_BEF_UPD_EVENTLOG FOR SpecList BEFORE UPDATE POSITION 10 AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SpecList_BEF_INS_EVENTLOG FOR SpecList BEFORE INSERT POSITION 10 AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER SpecList_AFT_DEL_EVENT_LOG FOR SpecList AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('SPECLIST', old.IntNo);
END
---
CREATE TRIGGER SpecList_BEF_INS_CHK_LISTNO FOR SpecList
ACTIVE BEFORE INSERT POSITION 0
AS
/*检查单号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (18, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
ALTER PROCEDURE CHECK_LISTNO_EXISTS
(
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
)
RETURNS
(
  R_LISTCOUNT SMALLINT
)
AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN

    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */

  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 18) THEN  /* 时段促销 */
  BEGIN
    SELECT COUNT(*) FROM SpecList WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
--
2004.02.11_01/UPGRADE_DETAIL
1. 修正[积分价格表]的问题;
--
2004.02.11_01/SQL1
ALTER PROCEDURE GET_PRICENO_FROM_SUMPOINT (
  P_CLIENTFLAG SMALLINT,
  P_PREVAMOUNT NUMERIC(15, 4),
  P_TOTALAMOUNT NUMERIC(15, 4),
  P_AMOUNTFACTOR NUMERIC(15, 4)
) RETURNS (
  R_PRICENO CHAR(8)
) AS
DECLARE VARIABLE m_SumPoint DEC(15,4);
DECLARE VARIABLE m_AmountLevelSumPoint DEC(15,4);
BEGIN
  r_PriceNo = null;
  if ((p_AmountFactor is null) or (p_AmountFactor = 0))then p_AmountFactor = 1;
  m_SumPoint = p_PrevAmount + p_TotalAmount/p_AmountFactor;
  FOR SELECT SumPoint, PriceNo FROM AmountLevel
    WHERE (ClientFlag = :p_ClientFlag) AND (SumPoint<=:m_SumPoint)
    ORDER BY SumPoint DESC
    INTO :m_AmountLevelSumPoint, :r_PriceNo
    DO
    BEGIN
      SUSPEND;
      EXIT;
    END
END
---
CREATE TRIGGER CLIENT_BEF_UPDATE_SET_PRICENO FOR CLIENT BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_PriceNo CHAR(8);
BEGIN
  IF ( (new.AutoPriceNo = 1)
    AND (new.PriceNo = old.PriceNo)
    AND
     ((new.TotalAmount <> old.TotalAmount)
      or (new.PrevPoint <> old.PrevPoint)
      or (new.AmountFactor <> old.AmountFactor)
      ) ) THEN
  BEGIN
    EXECUTE PROCEDURE Get_PriceNo_From_SumPoint(new.ClientFlag, new.PrevPoint, new.TotalAmount, new.AmountFactor)
      RETURNING_VALUES :m_PriceNo;
    IF (m_PriceNo is not null) THEN
      new.PriceNo = m_PriceNO;
  END
END
--
2004.02.06_01/UPGRADE_DETAIL
1. 调整内部号的生成方式;
--
2004.02.06_01/SQL2
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  SUSPEND;
END
--
2004.02.06_01/SQL1
CREATE EXCEPTION MSG_GENERATOR_NOT_DEFINE "内部号生成器未定义";

---
CREATE GENERATOR INTNO_CREATEPROC
---
SET GENERATOR INTNO_CREATEPROC TO 10
---
ALTER PROCEDURE GETNEWINTNO (
  P_TABLENAME VARCHAR(20)
) RETURNS (
  R_NEWINTNO INTEGER
) AS
BEGIN
  P_TABLENAME = UPPER(P_TABLENAME);
  BEGIN
    /* 调用频度较高的表的比较放在前面, 有利于提高速度 */
    IF (P_TableName='BUYGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVEGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICELST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICELST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKS, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALGDS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALGDS, 10); SUSPEND; EXIT; END

    IF (P_TableName='AMOUNTLEVEL')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_AMOUNTLEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='ASSLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ASSLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKMOVE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKMOVE, 10); SUSPEND; EXIT; END
    IF (P_TableName='BANKNAME')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BANKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='BAOLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BAOLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDSBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='BUYGDST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_BUYGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CLIENT')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CLIENT, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPGDS')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='CPLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CPLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CUSTOMVALUES')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CUSTOMVALUES, 10); SUSPEND; EXIT; END
    IF (P_TableName='DEPARTMENTS')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DEPARTMENTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='DOUTLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DOUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='DSALEGDS')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_DSALEGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='EMPLOYEE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_EMPLOYEE, 10); SUSPEND; EXIT; END
    IF (P_TableName='ERRORLOG')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_ERRORLOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULA')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULA, 10); SUSPEND; EXIT; END
    IF (P_TableName='FORMULADETAIL')    THEN BEGIN r_NewIntNo = Gen_ID(INTNO_FORMULADETAIL, 10); SUSPEND; EXIT; END
    IF (P_TableName='GRIDSETTING')      THEN BEGIN r_NewIntNo = Gen_ID(INTNO_GRIDSETTING, 10); SUSPEND; EXIT; END

    IF (P_TableName='INLIST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTBK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='INLISTT')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_INLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='LISTNO')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_LISTNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MGOODSNO')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MGOODSNO, 10); SUSPEND; EXIT; END
    IF (P_TableName='MOVELIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_MOVELIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='NOTE')             THEN BEGIN r_NewIntNo = Gen_ID(INTNO_NOTE, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='OUTLISTT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_OUTLISTT, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANGDS')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANGDS, 10); SUSPEND; EXIT; END
    IF (P_TableName='PANLIST')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PANLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHONEBOK')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHONEBOK, 10); SUSPEND; EXIT; END
    IF (P_TableName='PHRASE')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PHRASE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICE')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEIN')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEIN, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICENO')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICENO, 10); SUSPEND; EXIT; END
    IF (P_TableName='PRICEOUT')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_PRICEOUT, 10); SUSPEND; EXIT; END
    IF (P_TableName='REALLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_REALLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDSBK')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDSBK, 10); SUSPEND; EXIT; END
    IF (P_TableName='SALEGDST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SALEGDST, 10); SUSPEND; EXIT; END
    IF (P_TableName='SETUP')            THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SETUP, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLST')           THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLST, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTS')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTS, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKLSTSX')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKLSTSX, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKNAME')          THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKNAME, 10); SUSPEND; EXIT; END
    IF (P_TableName='STKPRICE')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_STKPRICE, 10); SUSPEND; EXIT; END
    IF (P_TableName='SYSDELETELOG')     THEN BEGIN r_NewIntNo = Gen_ID(INTNO_SYSDELETELOG, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPEDEFINE')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPEDEFINE, 10); SUSPEND; EXIT; END
    IF (P_TableName='TYPELEVEL')        THEN BEGIN r_NewIntNo = Gen_ID(INTNO_TYPELEVEL, 10); SUSPEND; EXIT; END
    IF (P_TableName='UNITLIST')         THEN BEGIN r_NewIntNo = Gen_ID(INTNO_UNITLIST, 10); SUSPEND; EXIT; END
    IF (P_TableName='CREATEPROC')       THEN BEGIN r_NewIntNo = Gen_ID(INTNO_CREATEPROC, 10); SUSPEND; EXIT; END
    EXCEPTION MSG_GENERATOR_NOT_DEFINE;
  END
END
--
2004.01.11_01/UPGRADE_DETAIL
1. 增加字段及设置定义
2. 加入医药版所需的字段及税率税额等;
3. 资金帐中增加字段[生效]
--
2004.01.11_01/SQL7
ALTER TABLE Banks ADD UPDATEACC TUPDATEFLAG
--
2004.01.11_01/SQL6
ALTER TABLE Client ADD IDNO Char(20)
---
ALTER TABLE Client ADD FactoryAuthorNo Char(20)
---
ALTER TABLE Client ADD BusinessAuthorNo Char(20)
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('IDNO')
  AND Upper(DomainName) = Upper('TPERSONID')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('FactoryAuthorNo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('BusinessAuthorNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('IDNO', '', 'TPERSONID')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('FactoryAuthorNo', '', '生产许可证', 'FactoryAuthorNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('BusinessAuthorNo', '', '经营许可证', 'BusinessAuthorNo')
--
2004.01.11_01/SQL5
ALTER PROCEDURE CLIENT_SET_LIMIT (
  P_CLIENTFLAG SMALLINT,
  P_CMPYNAME CHAR(12),
  P_S_DATE DATE
)  AS

BEGIN
  SUSPEND;
END
--
2004.01.11_01/SQL4
CREATE PROCEDURE UPDATE_CLIENT_PAYMENT (
  P_CMPYNAME VARCHAR(12),
  P_CLIENTFLAG SMALLINT,
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_GoodsGDSPAY NUMERIC(15, 4)
)  AS
BEGIN
  IF (p_CmpyName = '') THEN EXIT;
  Update CLIENT SET
    Gdspay = Gdspay + :p_Gdspay,
    GoodsGdspay = GoodsGdspay + :p_GoodsGdspay,
    Actpay = Actpay + :p_Actpay,
    TotalAmount = TotalAmount + :p_Gdspay
    WHERE (CmpyName = :p_CmpyName) and (ClientFlag = :p_ClientFlag);
END
---
CREATE TRIGGER INLIST_AFTER_UPDATE_SET_CLIENT FOR INLIST AFTER UPDATE POSITION 0 AS
/*更新客户帐中对应的单位的应收/应付款*/
DECLARE VARIABLE m_gdspay NUMERIC(15, 4);
DECLARE VARIABLE m_actpay NUMERIC(15, 4);
DECLARE VARIABLE m_Goodsgdspay NUMERIC(15, 4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.CmpyName = old.CmpyName) THEN
    BEGIN
      /* 单位名称未被修改, 只需修改原单位的相应资料 */
      m_gdspay = new.Gdspay-old.GDSPAY;
      m_actpay = new.Actpay-old.ACTPAY;
      m_Goodsgdspay = new.GoodsGdspay-old.GoodsGDSPAY;
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 1, m_gdspay, m_actpay, m_Goodsgdspay);
    END
    ELSE
    BEGIN
      /* 单位名称已被修改, 必须修改二个单位的相应资料 */
      m_gdspay = -old.gdspay;
      m_actpay = -old.actpay;
      m_Goodsgdspay = -old.Goodsgdspay;
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (old.CmpyName, 1, m_gdspay, m_Actpay, m_Goodsgdspay);
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 1, new.gdspay, new.Actpay, new.Goodsgdspay);
    END
  END
END
---
CREATE TRIGGER INLIST_AFTER_INSERT_SET_CLIENT FOR INLIST AFTER INSERT POSITION 0 AS
/*更新客户帐中对应的单位的应收/应付款*/
BEGIN
  EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 1, new.Gdspay, new.Actpay, new.GoodsGdspay);
END
---
CREATE TRIGGER INLIST_AFTER_DELETE_SET_CLIENT FOR INLIST AFTER DELETE POSITION 0 AS
/*更新客户帐中对应的单位的应收/应付款*/
DECLARE VARIABLE m_gdspay NUMERIC(15, 4);
DECLARE VARIABLE m_actpay NUMERIC(15, 4);
DECLARE VARIABLE m_Goodsgdspay NUMERIC(15, 4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_gdspay = -old.GDSPAY;
    m_actpay = -old.ACTPAY;
    m_Goodsgdspay = -old.GoodsGDSPAY;
    EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (old.CmpyName, 1, m_Gdspay, m_Actpay, m_Goodsgdspay);
  END
END
---
CREATE TRIGGER OUTLIST_AFTER_DELETE_SET_CLNT FOR OUTLIST BEFORE DELETE POSITION 0 AS
DECLARE VARIABLE m_gdspay NUMERIC(15, 4);
DECLARE VARIABLE m_actpay NUMERIC(15, 4);
DECLARE VARIABLE m_Goodsgdspay NUMERIC(15, 4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_gdspay = -old.GDSPAY;
    m_actpay = -old.ACTPAY;
    m_Goodsgdspay = -old.GoodsGDSPAY;
    EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (old.CmpyName, 0, m_Gdspay, m_Actpay, m_Goodsgdspay);
  END
END
---
CREATE TRIGGER OUTLIST_AFTER_INSERT_SET_CLNT FOR OUTLIST AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 0, new.Gdspay, new.Actpay, new.GoodsGdspay);
END
---
CREATE TRIGGER OUTLIST_AFTER_UPDATE_SET_CLNT FOR OUTLIST AFTER UPDATE POSITION 0 AS
/*更新客户帐中对应的单位的应收/应付款*/
DECLARE VARIABLE m_gdspay NUMERIC(15, 4);
DECLARE VARIABLE m_actpay NUMERIC(15, 4);
DECLARE VARIABLE m_Goodsgdspay NUMERIC(15, 4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.CmpyName = old.CmpyName) THEN
    BEGIN
      /* 单位名称未被修改, 只需修改原单位的相应资料 */
      m_gdspay = new.Gdspay-old.GDSPAY;
      m_actpay = new.Actpay-old.ACTPAY;
      m_Goodsgdspay = new.GoodsGdspay-old.GoodsGDSPAY;
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 0, m_gdspay, m_actpay, m_Goodsgdspay);
    END
    ELSE
    BEGIN
      /* 单位名称已被修改, 必须修改二个单位的相应资料 */

      m_gdspay = -old.gdspay;
      m_actpay = -old.actpay;
      m_Goodsgdspay = -old.Goodsgdspay;
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (old.CmpyName, 0, m_gdspay, m_Actpay, m_GoodsGdspay);
      EXECUTE PROCEDURE UPDATE_CLIENT_PAYMENT (new.CmpyName, 0, new.gdspay, new.Actpay, new.GoodsGdspay);
    END
  END
END
---
ALTER PROCEDURE CLIENT_SET_LIMIT (
  P_CLIENTFLAG SMALLINT,
  P_CMPYNAME CHAR(12),
  P_S_DATE DATE
)  AS
DECLARE VARIABLE m_b_gdspay DECIMAL (15,4);
DECLARE VARIABLE m_b_actpay DECIMAL (15,4);
DECLARE VARIABLE m_a_gdspay DECIMAL (15,4);
DECLARE VARIABLE m_a_actpay DECIMAL (15,4);
DECLARE VARIABLE m_TotalAmount DECIMAL (15,4);
DECLARE VARIABLE m_GoodsGdspay DECIMAL (15,4);
BEGIN
  IF (P_CLIENTFLAG = 0) THEN
  BEGIN
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM OUTLIST
      WHERE (LISTDATE <= :P_S_DATE) AND (CMPYNAME = :p_CmpyName)
      INTO :m_b_gdspay, :m_b_actpay;
    SELECT SUM(GDSPAY), SUM(ACTPAY), SUM(GoodsGdsPay) FROM OUTLIST
      WHERE (LISTDATE > :P_S_DATE) AND (CMPYNAME = :p_CmpyName)
      INTO :m_a_gdspay, :m_a_actpay, :m_GoodsGdsPay;
    SELECT SUM(Gdspay) FROM OutList
      WHERE (CmpyName = :p_CmpyName) INTO :m_TotalAmount;
  END
  ELSE
  BEGIN
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM INLIST
      WHERE (LISTDATE <= :P_S_DATE) AND (CMPYNAME = :p_CmpyName)
      INTO :m_b_gdspay, :m_b_actpay;
    SELECT SUM(GDSPAY), SUM(ACTPAY), SUM(GoodsGdsPay) FROM INLIST
      WHERE (LISTDATE > :P_S_DATE) AND (CMPYNAME = :p_CmpyName)
      INTO :m_a_gdspay, :m_a_actpay, :m_GoodsGdsPay;
    SELECT SUM(Gdspay) FROM INLIST
      WHERE (CmpyName = :p_CmpyName) INTO :m_TotalAmount;
  END
  IF (m_b_gdspay is NULL)    THEN m_b_gdspay = 0;
  IF (m_b_actpay is NULL)    THEN m_b_actpay = 0;
  IF (m_a_gdspay is NULL)    THEN m_a_gdspay = 0;
  IF (m_a_actpay is NULL)    THEN m_a_actpay = 0;
  IF (m_TotalAmount is NULL) THEN m_TotalAmount = 0;
  IF (m_GoodsGdsPay is NULL)   THEN m_GoodsGdsPay = 0;
  UPDATE CLIENT SET INITREMAIN = CREATE_RM + :m_b_gdspay - :m_b_actpay,
    GDSPAY = :m_a_gdspay, ACTPAY = :m_a_actpay,
      TotalAmount = :m_TotalAmount, GoodsGdsPay = :m_GoodsGdsPay
    WHERE (CmpyName = :p_CmpyName) and (ClientFlag = :p_ClientFlag);
  SUSPEND;
END
--
2004.01.11_01/SQL3
CREATE PROCEDURE UPDATE_OUTLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_INAMOUNT NUMERIC(15, 4),
  P_GoodsGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE OUTLIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    ACTPAY = ACTPAY + (:p_ACTPAY*:Direct),
    PROFIT = PROFIT + (:p_GDSPAY - :p_INAMOUNT)*:Direct,
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
CREATE TRIGGER SALEGDS_BEFORE_UPDATE_CHK_UPACC FOR SALEGDS BEFORE UPDATE POSITION 0 AS DECLARE
VARIABLE m_Changeable SmallInt;
/* 检查是否已出仓, 如果已出仓, 则不允许删除 */
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (not ((new.GoodsIntNo is NULL) and (not (old.GoodsIntNo is NULL))
      and (new.Qty=old.Qty) and (new.Price=old.Price)
      and (new.InPrice=old.InPrice)
      and (new.Discount=old.Discount) ))
      THEN
        EXECUTE PROCEDURE CHECK_LIST_UPDATEACC('OUTLIST', new.MAININTNO, 1) RETURNING_VALUES :m_Changeable;
    IF (new.Actpay is NULL) THEN new.Actpay = 0;
  END
END
---
CREATE TRIGGER SALEGDS_AFTER_UPDATE_SET_MASTER FOR SALEGDS AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_actpay DEC(15,4);
DECLARE VARIABLE m_Inamount DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.AMOUNT - old.AMOUNT;
    m_actpay = new.ACTPAY - old.ACTPAY;
    m_Inamount = new.INAMOUNT - old.INAMOUNT;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_OUTLIST_AMOUNT(:m_amount, :m_actpay, :m_Inamount, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
CREATE TRIGGER SALEGDS_AFTER_DELETE_SET_MASTER FOR SALEGDS AFTER DELETE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_OUTLIST_AMOUNT(old.AMOUNT, old.ACTPAY, old.INAMOUNT, old.GoodsAmount, -1, old.MAININTNO);
  END
END
---
CREATE TRIGGER SALEGDS_AFTER_INSERT_SET_MASTER FOR SALEGDS AFTER INSERT POSITION 0 AS

BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLIST_AMOUNT(new.AMOUNT, new.ACTPAY, new.INAMOUNT, new.GoodsAmount, 1, new.MAININTNO);
END
---
CREATE PROCEDURE UPDATE_INLIST_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4),
  P_GoodsGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE INLIST SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    ACTPAY = ACTPAY + (:p_ACTPAY*:Direct),
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
  SUSPEND;
END
---
CREATE TRIGGER BUYGDS_AFTER_DELETE_SET_MASTER FOR BUYGDS AFTER DELETE POSITION 0 AS /* 更新总表的相关数据 */
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    EXECUTE PROCEDURE UPDATE_INLIST_AMOUNT(old.AMOUNT, old.ACTPAY, old.GoodsAmount, -1, old.MAININTNO);
  END
END
---
CREATE TRIGGER BUYGDS_AFTER_INSERT_SET_MASTER FOR BUYGDS AFTER INSERT POSITION 0 AS /* 更新总表相关数据 */
BEGIN
  EXECUTE PROCEDURE UPDATE_INLIST_AMOUNT(new.AMOUNT, new.ACTPAY, new.GoodsAmount, 1, new.MAININTNO);
END
---
CREATE TRIGGER BUYGDS_AFTER_UPDATE_SET_MASTER FOR BUYGDS AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_actpay DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.AMOUNT - old.AMOUNT;
    m_actpay = new.ACTPAY - old.ACTPAY;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_INLIST_AMOUNT(:m_amount, :m_actpay, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
CREATE PROCEDURE UPDATE_INLISTBK_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_GoodsGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE INLISTBK SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
END
---
CREATE TRIGGER BUYGDSBK_AFTER_DELETE_1 FOR BUYGDSBK AFTER DELETE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
    EXECUTE PROCEDURE UPDATE_INLISTBK_AMOUNT(old.Amount, old.GoodsAmount, -1, old.MAININTNO);
END
---
CREATE TRIGGER BUYGDSBK_AFTER_UPDATE_1 FOR BUYGDSBK AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.Amount - old.Amount;
    m_Goodsamount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_INLISTBK_AMOUNT(:m_Amount, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
CREATE TRIGGER BUYGDSBK_AFTER_INSERT_1 FOR BUYGDSBK AFTER INSERT POSITION 0 AS
BEGIN

  EXECUTE PROCEDURE UPDATE_INLISTBK_AMOUNT(new.Amount, new.GoodsAmount, 1, new.MAININTNO);
END
---
CREATE PROCEDURE UPDATE_OUTLISTBK_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  P_GoodsGDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE OUTLISTBK SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct),
    GoodsGDSPAY = GoodsGDSPAY + (:p_GoodsGDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
END
---
CREATE TRIGGER SALEGDSBK_AFTER_DELETE_1 FOR SALEGDSBK AFTER DELETE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
    EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(old.Amount, old.GoodsAmount, -1, old.MAININTNO);
END
---
CREATE TRIGGER SALEGDSBK_AFTER_UPDATE_1 FOR SALEGDSBK AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_GoodsAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    m_amount = new.Amount - old.Amount;
    m_GoodsAmount = new.GoodsAmount - old.GoodsAmount;
    EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(:m_Amount, :m_GoodsAmount, 1, old.MAININTNO);
  END
END
---
CREATE TRIGGER SALEGDSTBK_AFTER_INSERT_1 FOR SALEGDSBK AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(new.Amount, new.GoodsAmount, 1, new.MAININTNO);
END
--
2004.01.11_01/SQL2
UPDATE BuyGdsBk Set TaxRate = 0, GoodsAmount = Amount, TaxPayment = 0
---
UPDATE SaleGdsBk Set TaxRate = 0, GoodsAmount = Amount, TaxPayment = 0
---
UPDATE SaleGds Set TaxRate = 0, GoodsAmount = Amount, TaxPayment = 0
---
UPDATE BuyGds Set TaxRate = 0, GoodsAmount = Amount, TaxPayment = 0
---
UPDATE InList Set GoodsGdsPay = GdsPay
---
UPDATE OutList Set GoodsGdsPay = GdsPay
---
UPDATE InListBk Set GoodsGdsPay = GdsPay
---
UPDATE OutListBk Set GoodsGdsPay = GdsPay
---
UPDATE Client Set GoodsGdsPay = GdsPay
--
2004.01.11_01/SQL1
ALTER TABLE InList ADD ContractNo Char(20)
---
ALTER TABLE InList ADD InvoiceNo Char(20)
---
ALTER TABLE InList ADD GoodsGdspay Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE OutList ADD ContractNo Char(20)
---
ALTER TABLE OutList ADD InvoiceNo Char(20)
---
ALTER TABLE OutList ADD GoodsGdspay Dec(15, 4) DEFAULT 0 NOT NULL

---
ALTER TABLE InListBk ADD ContractNo Char(20)
---
ALTER TABLE InListBk ADD InvoiceNo Char(20)
---
ALTER TABLE InListBk ADD GoodsGdspay Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE OutListBk ADD ContractNo Char(20)
---
ALTER TABLE OutListBk ADD InvoiceNo Char(20)
---
ALTER TABLE OutListBk ADD GoodsGdspay Dec(15, 4) DEFAULT 0 NOT NULL

---
ALTER TABLE BuyGds ADD QualityStatus VarChar(20)
---
ALTER TABLE BuyGds ADD TaxRate Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE BuyGds ADD GoodsAmount Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE BuyGds ADD TaxPayment Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE BuyGdsBk ADD QualityStatus VarChar(20)
---
ALTER TABLE BuyGdsBk ADD TaxRate Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE BuyGdsBk ADD GoodsAmount Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE BuyGdsBk ADD TaxPayment Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGds ADD QualityStatus VarChar(20)
---
ALTER TABLE SaleGds ADD TaxRate Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGds ADD GoodsAmount Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGds ADD TaxPayment Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGdsBk ADD QualityStatus VarChar(20)
---
ALTER TABLE SaleGdsBk ADD TaxRate Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGdsBk ADD GoodsAmount Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE SaleGdsBk ADD TaxPayment Dec(15, 4) DEFAULT 0 NOT NULL
---
ALTER TABLE Stklst ADD TradeMark Char(20)
---
ALTER TABLE Stklst ADD QualityStandard Char(40)
---
ALTER TABLE Stklst ADD MedType Char(12)
---
ALTER TABLE Client ADD GoodsGdsPay Dec(15, 4) DEFAULT 0 NOT NULL
/* 定义字段名称 */
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('ConTractNo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('InvoiceNo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsGdsPay')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('ConTractNo', '', '合同号', 'ConTractNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('InvoiceNo', '', '发票号', 'InvoiceNo')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('GoodsGdsPay', '', '货价总额', 'TAmount')
---
DELETE FROM DOMAINDEFINE WHERE UPPER(DOMAINNAME) = UPPER('TTaxRate')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QualityStatus')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('TaxRate')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('GoodsAmount')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('TaxPayment')
---
INSERT INTO DomainDefine (DomainName, DLabel, DFormat, DWidth)
 VALUES ('TTaxRate', '税率', '##;;', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('QualityStatus', '', '质量状况', 'QualityStatus')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('TaxRate', '', 'TTaxRate')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('GoodsAmount', '', '货价', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('TaxPayment', '', '税额', 'TAmount')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('TradeMark')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('QualityStandard')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('MedType')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('TradeMark', '', '注册商标', 'TradeMark')
---

INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('QualityStandard', '', '质量标准', 'QualityStandard')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('MedType', '', '剂型', 'MedType')
--
2003.12.31_01/UPGRADE_DETAIL
1. 当[客户名称]存在于[客户资料]中时, 售价跟踪才起作用;
2. 当[供应商]存在于[供应商资料]中时, 进价跟踪才起作用;
3. 当[进货数量>0]时, 才更新[最新进价/近进货日];
4. 当[销售数量>0]时, 才更新[近发货日];
5. 处理分库存清单中, 允许删除库存量为零(但总仓不为零时)的商品的错误;
--
2003.12.31_01/SQL1
/*20031230修改为当客户名不存在于客户资料中时不跟踪*/
ALTER PROCEDURE PRICEOUT_UPDATE (
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_PRICE DOUBLE PRECISION,
  P_DISCOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT
)  AS
DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_OutUseUpPrice VARCHAR(5);
DECLARE VARIABLE m_PriceTrace INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;

  FOR SELECT CmpyName, ListDate From OUTLIST WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO
    IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  m_PriceTrace = NULL;
  SELECT PriceTrace FROM CLIENT WHERE (CMPYNAME = :m_CmpyName) and (ClientFlag = 0) INTO :m_PriceTrace;
  IF ((m_PriceTrace = 0) OR (m_PriceTrace is NULL)) THEN EXIT;

  FOR SELECT Upper(DataString) FROM SETUP WHERE (Upper(DataName) LIKE '%OUTUSEUPPRICE') AND (UserGroup = '')
  INTO :m_OutUseUpPrice
    DO
    IF ((m_OutUseUpPrice = 'FALSE') OR (m_OutUseUpPrice is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM PRICEOUT WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (m_reccount = 0) THEN
    INSERT INTO PRICEOUT (GoodsIntNo, CmpyName, Price, Discount, ListDate, UnitId)
      VALUES (:p_GoodsIntNo, :m_CmpyName, :p_Price, :p_Discount, :m_ListDate, :p_UnitId);
  ELSE
    UPDATE PRICEOUT SET Price = :p_Price, Discount = :p_Discount, ListDate = :m_ListDate, UnitId = :p_UnitId
      WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
END
---
/*20031230修改为当供应商不存在于供应商资料中时不跟踪*/
ALTER PROCEDURE PRICEIN_UPDATE (
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_PRICE DOUBLE PRECISION,
  P_DISCOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT
)  AS
DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_UseUpPrice VARCHAR(5);
DECLARE VARIABLE m_PriceTrace INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  FOR SELECT CmpyName, ListDate From INLIST WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO
    IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  m_PriceTrace = NULL;
  SELECT PriceTrace FROM CLIENT WHERE (CMPYNAME = :m_CmpyName) and (ClientFlag = 1) INTO :m_PriceTrace;
    IF ((m_PriceTrace = 0) OR (m_PriceTrace is NULL)) THEN EXIT;

  FOR SELECT Upper(DataString) FROM SETUP WHERE (Upper(DataName) LIKE '%INUSEUPPRICE') AND (UserGroup = '')
  INTO :m_UseUpPrice
    DO
    IF ((m_UseUpPrice = 'FALSE') OR (m_UseUpPrice is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM PRICEIN WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (m_reccount = 0) THEN
    INSERT INTO PRICEIN (GoodsIntNo, CmpyName, Price, Discount, ListDate, UnitId)
      VALUES (:p_GoodsIntNo, :m_CmpyName, :p_Price, :p_Discount, :m_ListDate, :p_UnitId);
  ELSE
    UPDATE PRICEIN SET Price = :p_Price, Discount = :p_Discount, ListDate = :m_ListDate, UnitId = :p_UnitId
      WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
END
---
/*20031230删除掉以前没有客户名称的售价跟踪*/
CREATE PROCEDURE DELETE_PRICEOUT_UPDATE
AS
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_RecCount INTEGER;
BEGIN
  m_CmpyName = '';
  FOR SELECT CmpyName FROM PriceOut INTO :m_CmpyName
  DO
  BEGIN
    m_RecCount = 0;
    SELECT COUNT(*) FROM Client WHERE (CmpyName = :m_CmpyName) AND (ClientFlag = 0) INTO :m_RecCount;
    IF (m_RecCount = 0) THEN DELETE FROM PriceOut WHERE (CmpyName = :m_CmpyName);
  END
END
---
/*20031230删除掉以前没有供应商名称的进价跟踪*/
CREATE PROCEDURE DELETE_PRICEIN_UPDATE
AS
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_RecCount INTEGER;
BEGIN
  m_CmpyName = '';
  FOR SELECT CmpyName FROM PriceIn INTO :m_Cmpyname
  DO
  BEGIN
    m_RecCount = 0;
    SELECT COUNT(*) FROM Client WHERE (CmpyName = :m_Cmpyname) AND (ClientFlag = 1) INTO :m_RecCount;
    IF (m_RecCount = 0) THEN DELETE FROM PriceIn WHERE (CmpyName = :m_Cmpyname);
  END

END
---
/*20031230修改为当进货数<0时不把该价格写入最新进价及近进货日*/
ALTER PROCEDURE INLIST_UPDATE_PRICEUPIN
(
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_CMPYNAME VARCHAR(12)
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_Discount Dec(15,4);
BEGIN
  FOR SELECT GoodsIntNo, UnitId, Price, Discount FROM BUYGDS
    WHERE (MainIntNo = :p_MainIntNo) AND ( Qty > 0 )
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price, :m_Discount
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END
      UPDATE STKLST SET PriceUpIn = :m_Price*:m_Discount,
        CompanyUpIn = :p_CmpyName,
        DateUpIn = :p_ListDate
        WHERE (IntNo = :m_GoodsIntNo) and
          ((DateUpIn <= :p_ListDate) or (DateUpIn is NULL));
    END
END
---
ALTER PROCEDURE OUTLIST_UPDATE_PRICEUPOUT
(
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_CMPYNAME VARCHAR(12)
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_Discount Dec(15,4);
BEGIN
  FOR SELECT GoodsIntNo, UnitId, Price, Discount FROM SALEGDS
    WHERE (MainIntNo = :p_MainIntNo) and (Qty > 0)
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price, :m_Discount
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END
      UPDATE STKLST SET PriceUpOut = :m_Price*:m_Discount,
        DateUpOut = :p_ListDate
        WHERE (IntNo = :m_GoodsIntNo) and
          ((DateUpOut <= :p_ListDate) or (DateUpOut is NULL));
    END
END
--
2003.12.31_01/SQL2
CREATE TRIGGER STKLST_BEFORE_DEL_SAVE_GOODSNO FOR STKLST
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  UPDATE SALEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE SALEGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE BUYGDSBK SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE MOVEGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE PANGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE REALGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
  UPDATE ASSGDS SET GoodsIntNo = NUll, REMARK = old.GoodsNo WHERE GoodsIntNo = old.IntNo;
END
--
2003.11.27_01/UPGRADE_DETAIL
1. 更正组装商品的库存金额计算错误;
--
2003.11.27_01/SQL1
ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE DATE,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,

  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d, INLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
  BEGIN
    IF (BatchNo = '') THEN BatchNo = NULL;
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),

  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      FROM BUYGDS d, InList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      FROM SALEGDS d, Outlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM PANGDS d, PanList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM ASSGDS d, AssList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      FROM MoveGDS d, MoveList m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d, Inlist m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)

    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        AND (m.IntNo = d.MainIntNo)
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
  DO
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
    END
    Suspend;
  END
END
--
2003.11.21_01/UPGRADE_DETAIL
1. 优化统计报表速度;
--
2003.11.21_01/SQL1
CREATE PROCEDURE GETSTOCKIOLIST_NULL (
  P_DATEFROM DATE,
  P_DATETO DATE
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  /* 发货 */
  FOR
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM SaleGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 7;
  /* 差额盘点 */
  FOR
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, d.Qty as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM PanGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 12;
  /* 组装与拆解 */
  FOR
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AmountAssIn, 0.0 as AmountAssOut
      FROM ASSGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  /* 调拨入 */
  FOR
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 3;
  /* 调拨出 */
  FOR
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 1;
  /* 进货 */
  FOR
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM BUYGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, AmountAssOut
      DO SUSPEND;

END
---
CREATE PROCEDURE GETSTOCKIOLISTEXB_NULL (
  P_DATEFROM DATE,
  P_DATETO DATE
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')


    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM PanGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM ASSGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;

          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo

      FROM MOVEGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,

          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM BUYGDS d
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo is null)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

END
---
CREATE PROCEDURE GETSTOCKIOSUM_NULL (
  P_DATEFROM DATE,
  P_DATETO DATE
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4)
) AS  
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR
    Select GoodsIntNo, StockIntNo,
      Sum(QtyIn)-Sum(QtyOut)+Sum(QtyMoveIn)-Sum(QtyMoveOut)+Sum(QtyPan)+
      Sum(QtyAssIn-QtyAssOut) as QInitRemain,
      Sum(AmountIn)-Sum(AmountOut)+Sum(AmountMoveIn)-Sum(AmountMoveOut)+Sum(AmountPan)+
      Sum(AmountAssIn-AmountAssOut) as InitAmount,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      From GetstockIolist_Null('0001.1.1', :p_DateFrom-1)

      Group by GoodsIntNo, StockIntNo
    UNION ALL
    Select GoodsIntNo, StockIntNo,
      0.0 as QInitRemain,
      0.0 as InitAmount,
      Sum(QtyIn) as QtyIn, Sum(QtyOut) as QtyOut, Sum(QtyMoveIn) as QtyMoveIn,
      Sum(QtyMoveOut) as QtyMoveOut, Sum(QtyPan) as QtyPan,
      Sum(QtyAssIn) as QtyAssIn, Sum(QtyAssOut) as QtyAssOut,
      Sum(AmountIn) as AmountIn, Sum(AmountOut) as AmountOut, Sum(AmountMoveIn) as AmountMoveIn,
      Sum(AmountMoveOut) as AmountMoveOut, Sum(AmountPan) as AmountPan,
      Sum(AmountAssIn) as AmountAssIn, Sum(AmountAssOut) as AmountAssOut
      From GetstockIolist_NULL(:p_DateFrom, :p_DateTo)
      Group by GoodsIntNo, StockIntNo
        INTO :GoodsIntNo, :StockIntNo, :QInitRemain, :InitAmount,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
    DO SUSPEND;
END
---
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),

  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  /* 发货 */
  FOR
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 7;
  /* 差额盘点 */
  FOR
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 12;
  /* 组装与拆解 */
  FOR
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AmountAssIn, 0.0 as AmountAssOut
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  /* 调拨入 */
  FOR
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 3;
  /* 调拨出 */
  FOR
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 1;
  /* 进货 */

  FOR
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, AmountAssOut
      DO SUSPEND;

END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),

  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

END
--
2003.11.17_01/UPGRADE_DETAIL
1. 修改进销存数量统计报表,使其支持空货号;
2. 增加调价主表及明细表;
3. 设定字段定义及相关存储过程;
4. 调价单明细表中货号不能为空并且货号/单位的组合必须是唯一;
--
2003.11.17_01/SQL5
ALTER TABLE SalegdsBK Add InPrice TPrice
---
ALTER TABLE SalegdsBK Add InAmount TAmount
--
2003.11.17_01/SQL1
DELETE FROM FIELDDEFINE WHERE UPPER(FIELDNAME) = 'SUMAMOUNT'
---
INSERT INTO FIELDDEFINE (TABLENAME, DLABEL, FIELDNAME, DOMAINNAME)
  VALUES ('', '货款总额', 'SUMAMOUNT', 'TSUMAMOUNT')
--
2003.11.17_01/SQL2
CREATE GENERATOR INTNO_CPLIST
---
CREATE GENERATOR INTNO_CPGDS;
---
CREATE TABLE CPList (  /*建主表*/
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,

  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  COMMENT TCOMMENT,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  LREMARK2 VARCHAR(120),
  CONSTRAINT CPLIST_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT CPLIST_UNIQUE_1 UNIQUE (LISTNO)
)
---
CREATE TABLE CPGDS(  /*建明细表*/
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO TINTNO,
  UNITID TGOODSUNITID,
  n_InPriceDef TPRICE,          /*自定进价*/
  n_StandardOutPrice TPRICE,    /*标准售价*/
  n_NormalOutPrice TPRICE,      /*一般售价*/
  n_PrefentialPrice TPRICE,     /*优惠售价*/
  n_MinOutPrice TPRICE,         /*最低售价*/
  n_PFAStandardPrice TPRICE,    /*批发价*/
  n_PFAPrefentialPrice TPRICE,  /*优惠批发价*/
  O_InPriceDef TPRICE,          /*自定进价1*/
  o_StandardOutPrice TPRICE,    /*标准售价2*/
  o_NormalOutPrice TPRICE,      /*一般售价3*/
  o_PrefentialPrice TPRICE,     /*优惠售价4*/
  o_MinOutPrice TPRICE,         /*最低售价5*/
  o_PFAStandardPrice TPRICE,    /*批发价6*/
  o_PFAPrefentialPrice TPRICE,  /*优惠批发价7*/
  REMARK TREMARK,
  CONSTRAINT CPGDS_PRIMARY_KEY PRIMARY KEY (INTNO),
  CONSTRAINT FK_CPGDS_CPLIST FOREIGN KEY (MAININTNO)
    REFERENCES CPLIST(INTNO)
    ON UPDATE NO ACTION
    ON DELETE CASCADE,
  CONSTRAINT FK_CPGDS_STKLST FOREIGN KEY (GOODSINTNO)
    REFERENCES STKLST (INTNO)

    ON UPDATE CASCADE
    ON DELETE CASCADE,
  CONSTRAINT CPGDS_UNIQUE_1 UNIQUE (MainIntNo, GoodsIntNo, UnitId)
)
---
CREATE INDEX IDX_CPLIST_LISTDATE ON CPLIST(LISTDATE);
---
CREATE INDEX IDX_CPGDS_MAININTNO ON CPGDS(MAININTNO);
---
/*删除字段定义*/
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_INPRICEDEF')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_STANDARDOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_NORMALOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PREFENTIALPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_MINOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFASTANDARDPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('O_PFAPREFENTIALPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_INPRICEDEF')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_STANDARDOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_NORMALOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PREFENTIALPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_MINOUTPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFASTANDARDPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('N_PFAPREFENTIALPRICE')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('COMMENT')
---     /*建立字段定义*/
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_INPRICEDEF', '', '原自定进价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_STANDARDOUTPRICE', '', '原标准售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_NORMALOUTPRICE', '', '原一般售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PREFENTIALPRICE', '', '原优惠售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_MINOUTPRICE', '', '原最低售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFASTANDARDPRICE', '', '原批发价', 'TPrice')
---

INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('O_PFAPREFENTIALPRICE', '', '原优惠批发价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_INPRICEDEF', '', '新自定进价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_STANDARDOUTPRICE', '', '新标准售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_NORMALOUTPRICE', '', '新一般售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PREFENTIALPRICE', '', '新优惠售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_MINOUTPRICE', '', '新最低售价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFASTANDARDPRICE', '', '新批发价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('N_PFAPREFENTIALPRICE', '', '新优惠批发价', 'TPrice')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DomainName)
  VALUES ('COMMENT', '', '摘要', 'TCOMMENT')
--
2003.11.17_01/SQL3
CREATE PROCEDURE UPDATE_CPGDS_BYACC(
p_Flag SmallInt,
p_IntNo Integer
) AS
DECLARE VARIABLE m_INPRICEDEF DEC(15,4);         /*自定进价*/
DECLARE VARIABLE m_STANDARDOUTPRICE DEC(15,4);   /*标准售价*/
DECLARE VARIABLE m_NORMALOUTPRICE DEC(15,4);     /*一般售价*/
DECLARE VARIABLE m_PREFENTIALPRICE DEC(15,4);    /*优惠售价*/
DECLARE VARIABLE m_MINOUTPRICE DEC(15,4);        /*最低售价*/
DECLARE VARIABLE m_PFASTANDARDPRICE DEC(15,4);   /*批发价*/
DECLARE VARIABLE m_PFAPREFENTIALPRICE DEC(15,4); /*优惠批发价*/
DECLARE VARIABLE m_GoodsIntNo Integer;           /*货号*/
DECLARE VARIABLE m_UnitId Integer;               /*单位ID*/
begin
  IF (p_Flag = 1) then                           /* 生效 */
  BEGIN
    /* 取第一单位的旧价格 */
    FOR
      SELECT stk.INPRICEDEF, stk.STANDARDOUTPRICE, stk.NORMALOUTPRICE, stk.PREFENTIALPRICE,
        stk.MINOUTPRICE, stk.PFASTANDARDPRICE, stk.PFAPREFENTIALPRICE, d.GoodsIntNo
      FROM CPGDS d, STKLST stk
      WHERE (d.MainIntNo = :p_IntNo and d.GoodsIntNo = stk.IntNo)
      INTO :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE CPGDS Set o_INPRICEDEF = :m_INPRICEDEF, o_STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          o_NORMALOUTPRICE = :m_NORMALOUTPRICE, o_PREFENTIALPRICE = :m_PREFENTIALPRICE,

          o_MINOUTPRICE = :m_MINOUTPRICE, o_PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          o_PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE
        WHERE (MainIntNo = :p_IntNo and GoodsIntNo = :m_GoodsIntNo); /*  and UnitId = 0 */
      END
      /* IF (m_UnitId = 1) THEN */
      /* IF (m_UnitId = 2) THEN */

    /* 取新的价格, 更新到库存清单中 */
    FOR
      SELECT n_INPRICEDEF, n_STANDARDOUTPRICE, n_NORMALOUTPRICE, n_PREFENTIALPRICE,
        n_MINOUTPRICE, n_PFASTANDARDPRICE, n_PFAPREFENTIALPRICE, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
          MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE
        WHERE (IntNo = :m_GoodsIntNo);
      END
  END
  IF (p_Flag = -1) then                          /* 取消生效 */
  BEGIN
    /* 将旧的价格更新回到库存清单中 */
    FOR
      SELECT O_INPRICEDEF, O_STANDARDOUTPRICE, O_NORMALOUTPRICE, O_PREFENTIALPRICE,
        O_MINOUTPRICE, O_PFASTANDARDPRICE, O_PFAPREFENTIALPRICE, GoodsIntNo FROM CPGDS
      WHERE (MainIntNo = :p_IntNo)
      InTo :m_INPRICEDEF, :m_STANDARDOUTPRICE, :m_NORMALOUTPRICE, :m_PREFENTIALPRICE,
        :m_MINOUTPRICE, :m_PFASTANDARDPRICE, :m_PFAPREFENTIALPRICE, :m_GoodsIntNo
    DO
      BEGIN
        UPDATE Stklst Set INPRICEDEF = :m_INPRICEDEF, STANDARDOUTPRICE = :m_STANDARDOUTPRICE,
          NORMALOUTPRICE = :m_NORMALOUTPRICE, PREFENTIALPRICE = :m_PREFENTIALPRICE,
          MINOUTPRICE = :m_MINOUTPRICE, PFASTANDARDPRICE = :m_PFASTANDARDPRICE,
          PFAPREFENTIALPRICE = :m_PFAPREFENTIALPRICE
        WHERE (IntNo = :m_GoodsIntNo);
      END
  END
END
---
CREATE TRIGGER CPLIST_AFTER_UPDATE_UPDATEACC FOR CPLIST AFTER UPDATE POSITION 0 AS
/*执行生效/取消生效的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE UPDATE_CPGDS_BYACC(m_Flag, old.IntNo);
    END
  END
END
--
2003.11.17_01/SQL4
CREATE EXCEPTION MSG_CPGDS_GoodsNo_Not_Null "调价单货号不能为空"
---
CREATE EXCEPTION MSG_CPGDS_GoodsNo_MUST_DIFF "调价单(货号+单位)不能重复"
---
CREATE PROCEDURE CHECK_CPGDS_GOODSNO_MUST_DIFF(
p_MainIntNo Integer,
p_GoodsIntNo Integer,
p_UnitId SmallInt
)AS
DECLARE VARIABLE m_RecCount Integer;
BEGIN
  m_RecCount = 0;
  FOR
    Select COUNT(*) from CPGdS
      Where (MainIntNo = :p_MainIntNo AND GoodsIntNo = :p_GoodsIntNO and UnitId = :p_UnitId)
      INTO :m_RecCount
  DO
    BEGIN
      IF (m_RecCount > 0) THEN EXCEPTION MSG_CPGDS_GoodsNo_MUST_DIFF;
    END
END
---
CREATE TRIGGER CPGDS_BEFORE_UPDATE_1 FOR CPGDS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.GoodsIntNo is null) then EXCEPTION MSG_CPGDS_GoodsNo_Not_Null;
    IF (new.GoodsIntNo <> old.GoodsIntNo) THEN
      EXECUTE PROCEDURE CHECK_CPGDS_GoodsNo_MUST_DIFF(new.MainIntNo, new.GoodsIntNo, new.UnitId);

  END
END
---
CREATE TRIGGER CPGDS_BEFORE_INSERT_1 FOR CPGDS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.GoodsIntNo is null) then EXCEPTION MSG_CPGDS_GoodsNo_Not_Null;
  EXECUTE PROCEDURE CHECK_CPGDS_GoodsNo_MUST_DIFF(new.MainIntNo, new.GoodsIntNo, new.UnitId);
END
--
2003.10.19_02/UPGRADE_DETAIL
1. 调整多种收款方式;
2. 增加[进价平均值];
3. 会员资料: 增加[办卡日期][交易总额][积分权重];
--
2003.10.19_02/SQL2
ALTER TABLE AmountLevel ADD SumPoint Dec(15,4) DEFAULT 0 NOT NULL
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('SumPoint')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('SumPoint', '', '总积分', 'Amount', 'TAmount')
---
UPDATE AmountLevel Set SumPoint = SumAmount
--
2003.10.19_02/SQL3
CREATE EXCEPTION
  MSG_AMOUNTLEVEL_POINT_EXISTS "积分已存在, 不能重复"

---
CREATE PROCEDURE CHECK_AMOUNTLEVEL_POINT (
  P_CLIENTFLAG SMALLINT,
  P_SUMPOINT NUMERIC(15, 4)
)  AS
DECLARE VARIABLE m_Count Integer;
BEGIN
  m_Count = 0;
  SELECT COUNT(*) FROM AmountLevel
    WHERE (ClientFlag = :p_ClientFlag) AND (SumPoint = :p_SumPoint)
    INTO :m_Count;
  IF (m_Count > 0) THEN
    EXCEPTION MSG_AMOUNTLEVEL_POINT_EXISTS;
END
---
CREATE PROCEDURE GET_PRICENO_FROM_SUMPOINT (

  P_CLIENTFLAG SMALLINT,
  P_PREVAMOUNT NUMERIC(15, 4),
  P_TOTALAMOUNT NUMERIC(15, 4),
  P_AMOUNTFACTOR NUMERIC(15, 4)
) RETURNS (
  R_PRICENO CHAR(8)
) AS


DECLARE VARIABLE m_SumPoint DEC(15,4);
DECLARE VARIABLE m_AmountLevelSumPoint DEC(15,4);
BEGIN
  r_PriceNo = '';
  if ((p_AmountFactor is null) or (p_AmountFactor = 0))then p_AmountFactor = 1;
  m_SumPoint = p_PrevAmount + p_TotalAmount/p_AmountFactor;
  FOR SELECT SumPoint, PriceNo FROM AmountLevel
    WHERE ClientFlag = :p_ClientFlag
    ORDER BY SumPoint DESC
    INTO :m_AmountLevelSumPoint, :r_PriceNo
    DO
    BEGIN
      IF (m_SumPoint >= m_AmountLevelSumPoint) THEN
      BEGIN
        SUSPEND;
        EXIT;
      END
    END
END
---
CREATE TRIGGER AMOUNTLEVEL_BEF_INS_CHK_POINT FOR AMOUNTLEVEL BEFORE INSERT POSITION 0 AS
/*检查SumPoint否存在, 如存在, 则激活Exception*/
BEGIN
  EXECUTE PROCEDURE CHECK_AMOUNTLEVEL_POINT(new.ClientFlag, new.SumPoint);
END
---
CREATE TRIGGER AMOUNTLEVEL_BEF_UPD_CHK_POINT FOR AMOUNTLEVEL BEFORE UPDATE POSITION 0 AS
/*如果SumPoint/ClientFlag被改变, 检查SumPoint是否存在, 如存在, 则激活Exception*/
BEGIN
  IF ((new.SumPoint <> old.SumPoint) or (new.ClientFLag <> old.ClientFlag)) THEN
    EXECUTE PROCEDURE CHECK_AMOUNTLEVEL_POINT(new.ClientFlag, new.SumPoint);
END
---
CREATE TRIGGER CLIENT_BEF_UPDATE_SET_PRICENO FOR CLIENT BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_PriceNo CHAR(8);
BEGIN
  IF ( (new.AutoPriceNo = 1)
    AND (new.PriceNo = old.PriceNo)
    AND
     ((new.TotalAmount <> old.TotalAmount)
      or (new.PrevPoint <> old.PrevPoint)
      or (new.AmountFactor <> old.AmountFactor)
      ) ) THEN
  BEGIN
    EXECUTE PROCEDURE Get_PriceNo_From_SumPoint(new.ClientFlag, new.PrevPoint, new.TotalAmount, new.AmountFactor)
      RETURNING_VALUES :m_PriceNo;
    new.PriceNo = m_PriceNO;
  END
END
---
CREATE PROCEDURE APPLY_AMOUNTLEVEL (
  P_CLIENTFLAG SMALLINT,
  P_CMPYNO CHAR(8)
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_PrevPoint DEC(15,4);
DECLARE VARIABLE m_TotalAmount DEC(15,4);
DECLARE VARIABLE m_AmountFactor DEC(15,4);
DECLARE VARIABLE m_PriceNo CHAR(8);
BEGIN
    FOR SELECT IntNo, PrevPoint, TotalAmount, AmountFactor FROM Client
      WHERE (ClientFlag = :p_ClientFlag) AND (AutoPriceNo = 1)
        AND ( (:p_CmpyNo = '') OR ((:p_CmpyNo > '') AND (CmpyNo = :p_CmpyNo)) )
      INTO :m_IntNo, :m_PrevPoint, :m_TotalAmount, :m_AmountFactor
      DO
      BEGIN
        SELECT r_PriceNo FROM Get_PriceNo_From_SumPoint(:p_ClientFlag, :m_PrevPoint, :m_TotalAmount, :m_AmountFactor)
          INTO :m_PriceNo;
        IF (m_PriceNo IS NULL) THEN m_PriceNo = '';
        UPDATE Client SET PriceNo = :m_PriceNo WHERE IntNo = :m_IntNo;

      END
END
---
ALTER TABLE AmountLevel ADD CONSTRAINT AmountLevel_UNIQUE_2 UNIQUE (SumPoint, ClientFlag)
--
2003.10.19_02/SQL_CANCEL
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PrevPoint')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('CardDate')
---
ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE UPDATE_CLIENT_PAYMENT (
  P_CMPYNAME VARCHAR(12),

  P_CLIENTFLAG SMALLINT,
  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4)
)  AS
BEGIN
  SUSPEND;
END
--
2003.10.19_02/SQL1
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('PrevPoint', '', '初始积分', 'Amount', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('TotalAmount', '', '交易总额', 'Amount', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth, DomainName)
  VALUES ('AmountFactor', '', '积分权重', 'Amount', 'TAmount')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DWidth, DFormat, EMask)
  VALUES ('', 'CardDate', '办卡日期', '10', 'YYYY.MM.DD', '!0000.99.99;1;')
---
ALTER TABLE Client ADD PrevPoint Dec(15,4) DEFAULT 0
---
ALTER TABLE Client ADD TotalAmount Dec(15,4) DEFAULT 0
---
ALTER TABLE Client ADD AmountFactor Dec(15,4) DEFAULT 1
---
ALTER TABLE Client ADD CardDate TIMESTAMP
---
ALTER TABLE Stklst ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklst ADD SumQtyIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklst ADD AvgInPrice DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklsts ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklsts ADD SumQtyIn DEC(15,4)  DEFAULT 0
---
ALTER TABLE Stklsts ADD AvgInPrice DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD SumQtyIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD AvgInPrice DEC(15,4) DEFAULT 0
---
UPDATE Client SET PrevPoint = 0, TotalAmount = 0, AmountFactor = 1
---
CREATE PROCEDURE STOCK_UPDATE_ITEMX_D (
  P_DIRECT SMALLINT,
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,

  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER,
  P_EXPIREDATE_INT SMALLINT,
  P_BATCHNO VARCHAR(20),
  P_MANUFACTDATE_INT SMALLINT,
  P_GDSTABLE VARCHAR(10)
)  AS
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_OldExpireDate_Int SmallInt;
DECLARE VARIABLE m_OldManuFactDate_Int SmallInt;
DECLARE VARIABLE m_QtyIn Dec(15,4);
DECLARE VARIABLE m_QtyOut Dec(15,4);
DECLARE VARIABLE m_SumQtyIn Dec(15,4);
DECLARE VARIABLE m_SumAmountIn Dec(15,4);
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END

  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END

  m_QtyIn = 0; m_QtyOut = 0;
  IF (P_Direct > 0) THEN                         /* 属于正方向的操作 */
    m_QtyIn = p_Qty;
  ELSE
    m_QtyOut = -p_Qty;

  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  IF (p_BatchNo = '') THEN p_BatchNo = NULL;
  IF (p_ExpireDate_Int is NULL) THEN p_ExpireDate_Int = 0;
  IF (p_ManuFactDate_Int is NULL) THEN p_ManuFactDate_Int = 0;

  m_IntNo = NULL;

  /* 处理先进先出的批次 */
  IF ((p_Qty > 0) AND (GEN_ID(IntNo_PriceFIFO,0) = 1)) THEN
  BEGIN
    INSERT INTO StkPrice (GoodsIntNo, StockIntNo, QRemain, Amount, BatchNo,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 )
      VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount, :p_BatchNo,
        :p_CIntNo1, :p_CIntNo2, :p_CIntNo3, :p_CIntNo4, :p_CIntNo5
      );
  END

  IF (P_GDSTABLE = 'SALEGDS') THEN
  BEGIN
    m_SumQtyIn    = 0;
    m_SumAmountIn = 0;
  END
  ELSE
  BEGIN
    m_SumQtyIn    = p_Qty;
    m_SumAmountIn = p_Amount;
  END

  BEGIN
    m_OldExpireDate_Int = 0;
    m_OldManuFactDate_Int = 0;
    SELECT INTNO, ExpireDate_Int, ManuFactDate_Int FROM STKLSTSX
      WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)
        and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
        and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
        and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
        and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
        and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
        and ((BatchNo = :p_BatchNo) or (:p_BatchNo is NULL and BatchNo is NULL))
    INTO :m_IntNo, :m_OldExpireDate_Int, m_OldManuFactDate_Int;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int, QtyIn, QtyOut
        , SumQtyIn, SumAmountIn)
        VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount,
          :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5,
          :P_ExpireDate_Int, :P_BatchNo, :p_ManuFactDate_Int, :m_QtyIn, :m_QtyOut
          , :m_SumQtyIn, :m_SumAmountIn);
    ELSE
    BEGIN
      IF (m_OldExpireDate_Int > p_ExpireDate_Int) THEN     /* 原有效期较大, 保留该日期 */
        p_ExpireDate_Int = m_OldExpireDate_Int;
      IF (m_OldManuFactDate_Int > p_ManuFactDate_Int) THEN /* 原生产日期较大, 保留该日期 */
        p_ManuFactDate_Int = m_OldManuFactDate_Int;
      UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount,
        ExpireDate_Int = :p_ExpireDate_Int, ManuFactDate_Int = :p_ManuFactDate_Int,
        QtyIn = QtyIn + :m_QtyIn, QtyOut = QtyOut + :m_QtyOut,
        SumQtyIn    = SumQtyIn + :m_SumQtyIn,
        SumAmountIn = SumAmountIn + :m_SumAmountIn
        WHERE IntNo = :m_IntNo;
    END
  END
END
---
ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_AddFlag SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
BEGIN

  m_AddFlag = -p_AddFlag;
  FOR SELECT GoodsIntNo, Qty, Amount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int
         From MOVEGDS
    WHERE MainIntNo = :p_MainIntNo
    INTO :m_GoodsIntNo, :m_qty, :m_Amount, :m_UnitId,
         :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
         :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(1, p_AddFlag, m_GoodsIntNo, p_StockIntNoTo, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
          m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, 'MOVEGDS');
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(-1, m_AddFlag, m_GoodsIntNo, p_StockIntNoFrom, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
          m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, 'MOVEGDS');
      END
END
---
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN

  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,

            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int, p_gdsTable);

      END
  END
END
---
ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,

  GOODSINTNO INTEGER,
  LISTDATE DATE,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,

  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d, INLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d, OUTLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d, PANLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d, ASSLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

        and (m.IntNo = d.MainIntNo)

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)
    UNION ALL

    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d, MoveLIST m
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
        and (m.IntNo = d.MainIntNo)

    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
  BEGIN
    IF (BatchNo = '') THEN BatchNo = NULL;
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
      InAmount = -InAmount;

    END
    Suspend;
  END
END
---
CREATE PROCEDURE GET_ALL_StockIn
RETURNS (
  GOODSINTNO INTEGER,
  BATCHNO VARCHAR(20),
  STOCKINTNO INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  QTY Dec(15,4),
  Amount Dec(15,4)
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT d.GoodsIntNo, d.StockIntNo, d.BatchNo,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      Sum(d.Qty*u.Factor), Sum(d.Amount)
      FROM BUYGDS d, UnitList u, INLIST m
      WHERE (m.UpdateAcc = '*') AND ( (u.GoodsIntNo = d.GoodsIntNo) AND (u.UnitId=d.UnitId) )
        AND (m.IntNo = d.MainIntNo)
      GROUP BY d.GoodsIntNo, d.StockIntNo, d.BatchNo,
        d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

    Union All
    /* 差额盘点 */
    SELECT d.GoodsIntNo, d.StockIntNo, d.BatchNo,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      Sum(d.Qty*u.Factor), Sum(d.InAmount)
      FROM PanGds d, UnitList u, PANLIST m

      WHERE (m.UpdateAcc = '*') AND ( (u.GoodsIntNo = d.GoodsIntNo) AND (u.UnitId=d.UnitId) )
        AND (m.IntNo = d.MainIntNo)
      GROUP BY d.GoodsIntNo, d.StockIntNo, d.BatchNo,
        d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

    UNION ALL
    /* 组装与拆解 */
    SELECT d.GoodsIntNo, d.StockIntNo, d.BatchNo,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      Sum(d.Qty*u.Factor), Sum(d.InAmount)
      FROM AssGds d, UnitList u, ASSLIST m
      WHERE (m.UpdateAcc = '*') AND ( (u.GoodsIntNo = d.GoodsIntNo) AND (u.UnitId=d.UnitId) )
        AND (m.IntNo = d.MainIntNo)
      GROUP BY d.GoodsIntNo, d.StockIntNo, d.BatchNo,
        d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

    UNION ALL
    /* 调拨入 */
    SELECT d.GoodsIntNo, m.StockIntNoTo, d.BatchNo,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,

      Sum(d.Qty*u.Factor), Sum(d.Amount)
      FROM MoveGds d, UnitList u, MOVELIST m
      WHERE (m.UpdateAcc = '*') AND ( (u.GoodsIntNo = d.GoodsIntNo) AND (u.UnitId=d.UnitId) )
        AND (m.IntNo = d.MainIntNo)
      GROUP BY d.GoodsIntNo, m.StockIntNoTo, d.BatchNo,
        d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

    UNION ALL
    /* 调拨出 */
    SELECT d.GoodsIntNo, m.StockIntNoFrom, d.BatchNo,
      d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      Sum(-d.Qty*u.Factor), Sum(-d.Amount)
      FROM MoveGds d, UnitList u, MOVELIST m
      WHERE (m.UpdateAcc = '*') AND ( (u.GoodsIntNo = d.GoodsIntNo) AND (u.UnitId=d.UnitId) )
        AND (m.IntNo = d.MainIntNo)
      GROUP BY d.GoodsIntNo, m.StockIntNoFrom, d.BatchNo,
        d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

    INTO :GoodsIntNo, :StockIntNo, :BatchNo
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :Qty, :Amount
  DO
  BEGIN
    IF (BatchNo = '') THEN BatchNo = NULL;
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo2 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo3 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo4 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo5 = NULL;
    Suspend;
  END
END
---
CREATE PROCEDURE STK_CALC_AVGINPRICE AS
DECLARE VARIABLE m_GOODSINTNO INTEGER;
DECLARE VARIABLE m_BATCHNO VARCHAR(20);
DECLARE VARIABLE m_STOCKINTNO INTEGER;
DECLARE VARIABLE m_CUSTOMINTNO1 INTEGER;
DECLARE VARIABLE m_CUSTOMINTNO2 INTEGER;
DECLARE VARIABLE m_CUSTOMINTNO3 INTEGER;
DECLARE VARIABLE m_CUSTOMINTNO4 INTEGER;
DECLARE VARIABLE m_CUSTOMINTNO5 INTEGER;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
BEGIN
  UPDATE StklstsX SET SumQtyIn = 0, SumAmountIn = 0;
  UPDATE Stklsts SET SumQtyIn = 0, SumAmountIn = 0;
  UPDATE Stklst SET SumQtyIn = 0, SumAmountIn = 0;
  FOR SELECT d.GoodsIntNo, d.StockIntNo, d.BatchNo,
    d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5,
      Sum(d.Qty), Sum(d.Amount)
        FROM GET_ALL_StockIn d
        GROUP BY d.GoodsIntNo, d.StockIntNo, d.BatchNo,
          d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
  INTO :m_GOODSINTNO, :m_STOCKINTNO, :m_BATCHNO, :m_CUSTOMINTNO1, :m_CUSTOMINTNO2, :m_CUSTOMINTNO3,
       :m_CUSTOMINTNO4, :m_CUSTOMINTNO5, :m_Qty, :m_Amount
  DO
  BEGIN
    UPDATE STKLSTSX SET SumQtyIn = :m_Qty, SumAmountIn = :m_Amount
      WHERE (GoodsIntNo = :m_GoodsIntNo) and (StockIntNo = :m_StockIntNo)
        and ((CustomIntNo1 = :m_CustomIntNo1) or (:m_CustomIntNo1 is NULL and CustomIntNo1 is NULL))
        and ((CustomIntNo2 = :m_CustomIntNo2) or (:m_CustomIntNo2 is NULL and CustomIntNo2 is NULL))
        and ((CustomIntNo3 = :m_CustomIntNo3) or (:m_CustomIntNo3 is NULL and CustomIntNo3 is NULL))
        and ((CustomIntNo4 = :m_CustomIntNo4) or (:m_CustomIntNo4 is NULL and CustomIntNo4 is NULL))
        and ((CustomIntNo5 = :m_CustomIntNo5) or (:m_CustomIntNo5 is NULL and CustomIntNo5 is NULL))
        and ((BatchNo = :m_BatchNo) or (:m_BatchNo is NULL and BatchNo is NULL));
  END
END
---
UPDATE FieldDefine SET DLabel = '移动加权平均'
  WHERE (UPPER(FieldName) = Upper('OrgInPrice'))
    OR ( (UPPER(TableName) = 'STKLSTS') AND (UPPER(FieldName) = 'INPRICE') )
    OR ( (UPPER(TableName) = 'STKLST') AND (UPPER(FieldName) = 'INPRICE') )
---
UPDATE FieldDefine SET DLabel = '期初成本均价'
  WHERE UPPER(FIELDNAME) = 'INITPRICE'
---
CREATE TRIGGER STKLSTSX_BEFORE_INSERT_1 FOR STKLSTSX BEFORE INSERT POSITION 0 AS
BEGIN
  IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
  IF (Round(new.SumQtyIn,8) <> 0) THEN new.AvgInPrice = new.SumAmountIn / new.SumQtyIn;
END
---
CREATE TRIGGER STKLSTSX_BEFORE_UPDATE_1 FOR STKLSTSX BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
  IF ((new.SumAmountIn <> old.SumAmountIn) or (new.SumQtyIn <> old.SumQtyIn)) THEN
    IF (Round(new.SumQtyIn,8) <> 0) THEN new.AvgInPrice = new.SumAmountIn / new.SumQtyIn;
END
---
CREATE TRIGGER STKLSTS_BEFORE_INSERT_1 FOR STKLSTS BEFORE INSERT POSITION 0 AS

BEGIN
  IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
  IF (Round(new.SumQtyIn,8) <> 0) THEN new.AvgInPrice = new.SumAmountIn / new.SumQtyIn;
END
---
CREATE TRIGGER STKLSTS_BEFORE_UPDATE_1 FOR STKLSTS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
  IF ((new.SumAmountIn <> old.SumAmountIn) or (new.SumQtyIn <> old.SumQtyIn)) THEN
    IF (Round(new.SumQtyIn,8) <> 0) THEN new.AvgInPrice = new.SumAmountIn / new.SumQtyIn;
END
---
CREATE TRIGGER STKLST_BEFORE_UPDATE_1 FOR STKLST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
  IF ((new.SumAmountIn <> old.SumAmountIn) or (new.SumQtyIn <> old.SumQtyIn)) THEN
    IF (Round(new.SumQtyIn,8) <> 0) THEN new.AvgInPrice = new.SumAmountIn / new.SumQtyIn;
END
---
CREATE TRIGGER STKLSTSX_AFTER_INSERT_1 FOR STKLSTSX AFTER INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
  BEGIN
    m_IntNo = NULL;
    SELECT IntNo FROM STKLSTS WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo)
      INTO :m_IntNo;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTS (GoodsIntNo, StockIntNo, QRemain, Amount,
        qInitRemain, InitAmount, QtyIn, QtyOut, SumQtyIn, SumAmountIn)
        VALUES (new.GoodsIntNo, new.StockIntNo, new.qRemain, new.Amount,
        new.qInitRemain, new.InitAmount, new.QtyIn, new.QtyOut
        , new.SumQtyIn, new.SumAmountIn);
    ELSE
      UPDATE STKLSTS SET QRemain = QRemain + new.QRemain, Amount = AMOUNT + new.Amount,
        qInitRemain = qInitRemain + new.qInitRemain, InitAmount = InitAmount + new.InitAmount,
        QtyOut = QtyOut + new.QtyOut, QtyIn = QtyIn + new.QtyIn,
        SumQtyIn    = SumQtyIn + new.SumQtyIn,
        SumAmountIn = SumAmountIn + new.SumAmountIn
        WHERE (IntNo = :m_IntNo);
  END
END
---
CREATE TRIGGER STKLSTSX_AFTER_UPDATE_1 FOR STKLSTSX AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
  BEGIN
    m_IntNo = NULL;
    SELECT IntNo FROM STKLSTS WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo)
      INTO :m_IntNo;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTS (GoodsIntNo, StockIntNo, QRemain, Amount,
        qInitRemain, InitAmount, QtyIn, QtyOut,
        SumQtyIn, SumAmountIn)
        VALUES (new.GoodsIntNo, new.StockIntNo, new.qRemain, new.Amount,
        new.qInitRemain, new.InitAmount, new.QtyIn, new.QtyOut,
        new.SumQtyIn, new.SumAmountIn);

    ELSE
    BEGIN
      IF ((new.QRemain <> old.QRemain) or (new.AMOUNT <> old.Amount)) THEN
      BEGIN
        UPDATE STKLSTS SET QRemain = QRemain + new.QRemain - old.QRemain,
          Amount = AMOUNT + new.Amount - old.Amount
          WHERE (IntNo = :m_IntNo);
      END
      IF ((new.QInitRemain <> old.QInitRemain) or (new.InitAMOUNT <> old.InitAmount)) THEN
      BEGIN
        UPDATE STKLSTS SET QInitRemain = QInitRemain + new.QInitRemain - old.QInitRemain,
          InitAmount = InitAMOUNT + new.InitAmount - old.InitAmount
          WHERE (IntNo = :m_IntNo);
      END
      IF ((new.QtyIn <> old.QtyIn) or (new.QtyOut <> old.QtyOut)) THEN
      BEGIN
        UPDATE STKLSTS SET QtyIn = QtyIn + new.QtyIn - old.QtyIn,
          QtyOut = QtyOut + new.QtyOut - old.QtyOut
          WHERE (IntNo = :m_IntNo);
      END
      IF ((new.SumQtyIn <> old.SumQtyIn) or (new.SumAmountIn <> old.SumAmountIn)) THEN
      BEGIN
        UPDATE STKLSTS SET SumQtyIn = SumQtyIn + new.SumQtyIn - old.SumQtyIn,
          SumAmountIn = SumAmountIn + new.SumAmountIn - old.SumAmountIn
          WHERE (IntNo = :m_IntNo);
      END
    END
  END
END
---
CREATE TRIGGER STKLSTS_AFTER_INSERT_1 FOR STKLSTS AFTER INSERT POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    UPDATE STKLST SET QRemain = QRemain + new.QRemain,
      Amount = AMOUNT + new.Amount,
      QInitRemain = QInitRemain + new.QInitRemain,
      InitAmount = InitAMOUNT + new.InitAmount,
      SumQtyIn    = SumQtyIn + new.SumQtyIn,
      SumAmountIn = SumAmountIn + new.SumAmountIn
      WHERE INTNO = new.GoodsIntNo;
  END
END
---
CREATE TRIGGER STKLSTS_AFTER_UPDATE_1 FOR STKLSTS AFTER UPDATE POSITION 0 AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ((new.QRemain <> old.QRemain) or (new.AMOUNT <> old.Amount)) THEN
      UPDATE STKLST SET QRemain = QRemain + new.QRemain - old.QRemain,
        Amount = AMOUNT + new.Amount - old.Amount
        WHERE INTNO = old.GoodsIntNo;

    IF ((new.QInitRemain <> old.QInitRemain) or (new.InitAMOUNT <> old.InitAmount)) THEN
      UPDATE STKLST SET QInitRemain = QInitRemain + new.QInitRemain - old.QInitRemain,
        InitAmount = InitAMOUNT + new.InitAmount - old.InitAmount
        WHERE INTNO = old.GoodsIntNo;

    IF ((new.SumQtyIn <> old.SumQtyIn) or (new.SumAmountIn <> old.SumAmountIn)) THEN
      UPDATE STKLST SET SumQtyIn = SumQtyIn + new.SumQtyIn - old.SumQtyIn,
        SumAmountIn = SumAmountIn + new.SumAmountIn - old.SumAmountIn
        WHERE (IntNo = old.GoodsIntNo);
  END
END
---
CREATE PROCEDURE V_POS_SALEGDS  RETURNS (
  INTNO INTEGER,
  MAININTNO INTEGER,
  ACTPAY NUMERIC(15, 4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  INAMOUNT NUMERIC(15, 4),
  REMARK VARCHAR(20),
  UPDATEFLAG SMALLINT,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SMALLINT,
  EXPIREDATE_INT SMALLINT,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80)
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId Integer;
BEGIN
  FOR SELECT d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,
    d.AMOUNT,
    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    d.GoodsIntNo, d.UnitId,
    StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5
    FROM Salegds d, StkName, BankName
    WHERE StkName.IntNo = d.StockIntNo and d.BankIntNo = BankName.IntNo
    INTO :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :m_GoodsIntNo, :m_UnitId, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
    DO

    BEGIN
      IF (m_GoodsIntNo is NULL) THEN
      BEGIN
        GoodsNo = '';
        GoodsUnit = '';
      END
      ELSE
      BEGIN
        SELECT GoodsNo FROM Stklst Where IntNo = :m_GoodsIntNo INTO :GoodsNo;
        SELECT GoodsUnit FROM UnitList u
          WHERE (u.GoodsIntNo=:m_GoodsIntNo and u.UnitId=:m_UnitId)
          INTO :GoodsUnit;
      END
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN
        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
---
CREATE TRIGGER CLIENT_BEFORE_INSERT_SET_DEF FOR CLIENT BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.CmpyBankName is NULL) THEN new.CmpyBankName = '';
  IF (new.CmpyBankNo is NULL) THEN new.CmpyBankNo = '';
  IF (new.CmpyBoss is NULL) THEN new.CmpyBoss = '';
  IF (new.CmpyContact is NULL) THEN new.CmpyContact = '';
  IF (new.City is NULL) THEN new.City = '';
  IF (new.Province is NULL) THEN new.Province = '';
  IF (new.ClientType is NULL) THEN new.ClientType = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.TaxNo is NULL) THEN new.TaxNo = '';
  IF (new.Fax is NULL) THEN new.Fax = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.CmpyFName is NULL) THEN new.CmpyFName = '';
  IF (new.PrevPoint is NULL) THEN new.PrevPoint = 0;
  IF (new.TotalAmount is NULL) THEN new.TotalAmount = 0;
  IF ((new.AmountFactor is NULL) or (new.AmountFactor = 0)) THEN new.AmountFactor = 1;
END
---
CREATE TRIGGER CLIENT_BEFORE_UPDATE_SET_DEF FOR CLIENT BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.CmpyBankName is NULL) THEN new.CmpyBankName = '';
  IF (new.CmpyBankNo is NULL) THEN new.CmpyBankNo = '';
  IF (new.CmpyBoss is NULL) THEN new.CmpyBoss = '';
  IF (new.CmpyContact is NULL) THEN new.CmpyContact = '';
  IF (new.City is NULL) THEN new.City = '';
  IF (new.Province is NULL) THEN new.Province = '';
  IF (new.ClientType is NULL) THEN new.ClientType = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.TaxNo is NULL) THEN new.TaxNo = '';
  IF (new.Fax is NULL) THEN new.Fax = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.CmpyFName is NULL) THEN new.CmpyFName = '';
  IF (new.PrevPoint is NULL) THEN new.PrevPoint = 0;
  IF (new.TotalAmount is NULL) THEN new.TotalAmount = 0;
  IF ((new.AmountFactor is NULL) or (new.AmountFactor = 0)) THEN new.AmountFactor = 1;
END
---
ALTER PROCEDURE UPDATE_CLIENT_PAYMENT (
  P_CMPYNAME VARCHAR(12),
  P_CLIENTFLAG SMALLINT,

  P_GDSPAY NUMERIC(15, 4),
  P_ACTPAY NUMERIC(15, 4)
)  AS
BEGIN
  IF (p_CmpyName = '') THEN EXIT;
  Update CLIENT SET
    Gdspay = Gdspay + :p_Gdspay,
    Actpay = Actpay + :p_Actpay,
    TotalAmount = TotalAmount + :p_Gdspay
    WHERE (CmpyName = :p_CmpyName) and (ClientFlag = :p_ClientFlag);
END
---
CREATE PROCEDURE CLIENT_CALC_TOTALAMOUNT AS
DECLARE VARIABLE m_SumGdspay Dec(15,4);
DECLARE VARIABLE m_CmpyName Char(12);
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR SELECT IntNo, CmpyName FROM Client WHERE (ClientFlag = 0)
    INTO :m_IntNo, :m_CmpyName
  DO
  BEGIN
    SELECT SUM(Gdspay) FROM Outlist WHERE (CmpyName = :m_CmpyName) INTO :m_SumGdspay;
    IF (m_SumGdspay is NULL) THEN m_SumGdspay = 0;
    UPDATE Client SET TotalAmount = :m_SumGdspay
      WHERE (IntNo = :m_IntNo);
  END

  FOR SELECT IntNo, CmpyName FROM Client WHERE (ClientFlag = 1)
     INTO :m_IntNo, :m_CmpyName
  DO
  BEGIN
    SELECT SUM(Gdspay) FROM Inlist WHERE (CmpyName = :m_CmpyName) INTO :m_SumGdspay;
    IF (m_SumGdspay is NULL) THEN m_SumGdspay = 0;
    UPDATE Client SET TotalAmount = :m_SumGdspay
      WHERE (IntNo = :m_IntNo);
  END
END
--
2003.10.06_03/UPGRADE_DETAIL
1. 客户资料: 增加标志[前台可见], [经营规模];
2. 职员资料: 增加[学历];
3. 进货/销售总表: 增加[备注2]
4. 库存资料: 增加[进价平均值];
5. 增加表: [先入先出]库存表, 以支持[先入先出法]计算利润;
--
2003.10.06_03/SQL_CANCEL
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('PosVisible')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('BusinessSize')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('Edu_Level')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('LRemark2')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('SumAmountIn')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('SumQtyIn')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('AvgInPrice')
---
ALTER PROCEDURE STOCK_UPDATE_ITEMX_D (
  P_DIRECT SMALLINT,
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER,
  P_EXPIREDATE_INT SMALLINT,
  P_BATCHNO VARCHAR(20),
  P_MANUFACTDATE_INT SMALLINT
)  AS
BEGIN
  SUSPEND;
END
--
2003.10.06_03/SQL4
UPDATE FieldDefine SET DLabel = '客户名称' WHERE DLabel = '受货客户'
---
UPDATE FieldDefine SET DLabel = '供应商名称' WHERE DLabel = '供货客户'
---
CREATE VIEW VIEW_POS_OUTLIST (
  INTNO,
  UPDATEACC,
  PRINTCOUNT,
  FLAG1,
  FLAG2,
  FLAG3,

  FLAG4,
  LISTDATE,
  LISTTIME,
  LISTNO,
  CMPYNAME,
  GDSPAY,
  ACTPAY,
  PROFIT,
  PERSONDEAL,
  DEPARTMENT,
  REMARK,
  OPERNO,
  LREMARK2
) AS
Select m.IntNo, '*' as UpdateAcc, m.PrintCount, m.Flag1, m.Flag2, m.Flag3, m.Flag4,
  m.ListDate, m.ListTime, m.ListNo, m.CmpyName,
  m.Gdspay, m.Actpay, m.Profit,
  m.PersonDeal, m.Department, m.Remark, m.OperNo, m.LRemark2
  FROM OUTLIST m
---
CREATE PROCEDURE MOVELIST_SET_FIFO_INPRICE(
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
) AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
*/
BEGIN
  IF (NOT (p_AddFlag = 1)) THEN                       /* 先进先出时, 不允许取消进出仓 */
    EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC;

  /* 处理调出的正数记录 */
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MOVEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)

      INTO :m_IntNo, :m_GoodsIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :P_StockIntNoFrom, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE MOVEGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  /* 处理调入的负数记录 */
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MOVEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :P_StockIntNoTo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE MOVEGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END
END
---

CREATE TRIGGER MOVELIST_BEF_UPD_ACC_SETINPRICE FOR MOVELIST BEFORE UPDATE POSITION 3 AS
/* 执行进仓/取消进仓的操作前, 自动设置价格 */
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_Amount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE MOVELIST_SET_FIFO_INPRICE(m_Flag, old.IntNo, new.StockIntNoFrom, new.StockIntNoTo);
      SELECT SUM(Amount) FROM Movegds WHERE MainIntNo = old.IntNo INTO :m_Amount;
      IF (m_Amount IS NULL) THEN m_Amount = 0;
      new.Gdspay = m_Amount;
    END
  END
END
---
CREATE PROCEDURE MOVELIST_INSERT_RECORD (
  INTNO INTEGER,
  LISTDATE DATE,
  LISTTIME DATE,
  LISTNO CHAR(14),
  STOCKIDFROM CHAR(4),
  STOCKIDTO CHAR(4),
  PERSONDEAL CHAR(12),
  REMARK CHAR(20),
  OPERNO CHAR(10),
  LREMARK2 varCHAR(120)
)  AS
DECLARE VARIABLE m_StockIntNoFrom Integer;
DECLARE VARIABLE m_StockIntNoTo Integer;
BEGIN
  SELECT IntNo FROM STKNAME WHERE STOCKID = :StockIdFrom INTO :m_StockIntNoFrom;
  SELECT IntNo FROM STKNAME WHERE STOCKID = :StockIdTo INTO :m_StockIntNoTo;
  IF ((m_StockIntNoFrom is NULL) or (m_StockIntNoTo is NULL)) THEN
    EXCEPTION MSG_STOCKID_NOT_EXISTS;
  INSERT INTO MOVELIST (IntNo, ListDate, ListTime, ListNo, StockIntNoFrom, StockIntNoTo, PersonDeal, Remark, OperNo, LRemark2)
    VALUES (:IntNo, :ListDate, :ListTime, :ListNo, :m_StockIntNoFrom, :m_StockIntNoTo, :PersonDeal, :Remark, :OperNo, :LRemark2);
  SUSPEND;
END
--
2003.10.06_03/SQL3
Create Procedure Get_DayLimit(  /*输入公司名返回帐期*/
  p_CmpyName VARChar(12),          /*公司名称*/
  p_ClientFlag SmallInt         /*公司类别*/
) Returns(
  r_DayLimit SmallInt            /*帐期*/
) as

begin
  Select DayLimit from Client Where (CmpyName = :p_CmpyName) and (ClientFlag = :p_ClientFlag)
    Into :r_DayLimit;

  Suspend;
end;
---
Create Procedure Check_DayLimit(  /*输入公司名及新帐期日得到货款合计及付款合计*/
  p_CmpyName VARChar(12),           /*公司名称*/
  p_ClientFlag SmallInt,         /*公司类别*/
  p_NewDate TimeStamp            /*新帐期日*/
) Returns(
  r_SumActpay Dec(15,4),         /*付款合计*/
  r_SumGdspay Dec(15,4)          /*货款合计*/
)
AS
begin
  if (p_ClientFlag = 0) then
  begin
    Select Sum(Actpay) from OutList
      Where CmpyName = :p_CmpyName Into :r_SumActpay;  /*得到所有付款*/
    Select Sum(Gdspay) from OutList                    /*得到所有货款*/
      Where (CmpyName = :p_CmpyName) and (ListDate <= :p_NewDate) Into :r_SumGdspay;
  end
  else
  begin
    Select Sum(Actpay) from InList
      Where CmpyName = :p_CmpyName Into :r_SumActpay;  /*得到所有付款*/
    Select Sum(Gdspay) from InList                    /*得到所有货款*/
      Where (CmpyName = :p_CmpyName) and (ListDate <= :p_NewDate) Into :r_SumGdspay;
  end
  IF (r_SumGdspay is NULL) THEN r_SumGdspay = 0;
  IF (r_SumActpay is NULL) THEN r_SumActpay = 0;
  Suspend;
end
--
2003.10.06_03/SQL2
CREATE GENERATOR INTNO_PriceFIFO
---
SET GENERATOR INTNO_PriceFIFO TO 0
---
ALTER PROCEDURE STOCK_UPDATE_ITEMX_D (
  P_DIRECT SMALLINT,
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER,
  P_EXPIREDATE_INT SMALLINT,
  P_BATCHNO VARCHAR(20),
  P_MANUFACTDATE_INT SMALLINT
)  AS
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_OldExpireDate_Int SmallInt;
DECLARE VARIABLE m_OldManuFactDate_Int SmallInt;
DECLARE VARIABLE m_QtyIn Dec(15,4);
DECLARE VARIABLE m_QtyOut Dec(15,4);
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END

  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END


  m_QtyIn = 0; m_QtyOut = 0;
  IF (P_Direct > 0) THEN                         /* 属于正方向的操作 */
    m_QtyIn = p_Qty;
  ELSE
    m_QtyOut = -p_Qty;

  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  IF (p_BatchNo = '') THEN p_BatchNo = NULL;
  IF (p_ExpireDate_Int is NULL) THEN p_ExpireDate_Int = 0;
  IF (p_ManuFactDate_Int is NULL) THEN p_ManuFactDate_Int = 0;

  m_IntNo = NULL;

  /* 处理先进先出的批次 */
  IF ((p_Qty > 0) AND (GEN_ID(IntNo_PriceFIFO,0) = 1)) THEN
  BEGIN
    INSERT INTO StkPrice (GoodsIntNo, StockIntNo, QRemain, Amount, BatchNo,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 )
      VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount, :p_BatchNo,
        :p_CIntNo1, :p_CIntNo2, :p_CIntNo3, :p_CIntNo4, :p_CIntNo5
      );
  END

  BEGIN
    m_OldExpireDate_Int = 0;
    m_OldManuFactDate_Int = 0;
    SELECT INTNO, ExpireDate_Int, ManuFactDate_Int FROM STKLSTSX
      WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)
        and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
        and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
        and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
        and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
        and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
        and ((BatchNo = :p_BatchNo) or (:p_BatchNo is NULL and BatchNo is NULL))
    INTO :m_IntNo, :m_OldExpireDate_Int, m_OldManuFactDate_Int;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int, QtyIn, QtyOut)
        VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount,
          :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5,
          :P_ExpireDate_Int, :P_BatchNo, :p_ManuFactDate_Int, :m_QtyIn, :m_QtyOut);
    ELSE
    BEGIN
      IF (m_OldExpireDate_Int > p_ExpireDate_Int) THEN     /* 原有效期较大, 保留该日期 */
        p_ExpireDate_Int = m_OldExpireDate_Int;
      IF (m_OldManuFactDate_Int > p_ManuFactDate_Int) THEN /* 原生产日期较大, 保留该日期 */
        p_ManuFactDate_Int = m_OldManuFactDate_Int;
      UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount,
        ExpireDate_Int = :p_ExpireDate_Int, ManuFactDate_Int = :p_ManuFactDate_Int,
        QtyIn = QtyIn + :m_QtyIn, QtyOut = QtyOut + :m_QtyOut
        WHERE IntNo = :m_IntNo;
    END
  END
END
---
CREATE TRIGGER CREATE_INTNO_STKPRICE FOR STKPRICE BEFORE INSERT POSITION 0 AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((new.IntNo is NULL) OR (new.IntNo = 0)) THEN
  BEGIN
    NEW.INTNO = GEN_ID(INTNO_STKPRICE, 1);
  END
END
---
CREATE TRIGGER STKPRICE_BEFORE_UPDATE_1 FOR STKPRICE BEFORE UPDATE POSITION 0 AS
BEGIN
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
END
---
CREATE TRIGGER STKPRICE_BEFORE_INSERT_1 FOR STKPRICE BEFORE INSERT POSITION 0 AS
BEGIN
  IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
END
---
CREATE EXCEPTION FIFO_NOT_Enough_Qty
  "先进先出时, 出库量不能大于商品存量"
---
CREATE EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC
  "先进先出时, 不允许取消进出仓"
---
CREATE PROCEDURE StkPrice_FIFO_CheckOut(
  P_GoodsIntNo Integer,
  P_StockIntNo Integer,
  p_Qty DEC(15,4),
  p_BatchNo VARCHAR(20),
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER
) RETURNS (
  R_InPrice DEC(15,4)
) AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_QRemain DEC(15,4);
DECLARE VARIABLE m_Amount DEC(15,4);
DECLARE VARIABLE m_InPrice DEC(15,4);
DECLARE VARIABLE m_Amount_Dec_Total DEC(15,4);
DECLARE VARIABLE m_Amount_DEC DEC(15,4);
DECLARE VARIABLE m_Qty_DEC DEC(15,4);
DECLARE VARIABLE m_Qty_Save DEC(15,4);
BEGIN
  p_Qty = Round(p_Qty, 8);

  IF (p_Qty = 0) THEN EXIT;             /* 出库数量为零, 无须处理, 直接退出 */

  m_Qty_Save = p_Qty;

  IF (ALLTRIM(p_BatchNo) = '') THEN p_BatchNo = NULL;
  IF (P_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (P_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (P_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (P_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (P_CIntNo5 = 0) THEN p_CIntNo5 = NULL;

  m_Amount_Dec_Total = 0;

  FOR SELECT IntNo, InPrice, QRemain, Amount FROM StkPrice
    WHERE (GoodsIntNo = :p_GoodsIntNo and StockIntNo = :p_StockIntNo)
      AND ( (BatchNo = :p_BatchNo) or ( (:p_BatchNo is NULL) and (BatchNo is NULL) ) )
      AND ( (CustomIntNo1 = :p_CIntNo1) or ( (:p_CIntNo1 is NULL) and (CustomIntNo1 is NULL) ) )
      AND ( (CustomIntNo2 = :p_CIntNo2) or ( (:p_CIntNo2 is NULL) and (CustomIntNo2 is NULL) ) )
      AND ( (CustomIntNo3 = :p_CIntNo3) or ( (:p_CIntNo3 is NULL) and (CustomIntNo3 is NULL) ) )
      AND ( (CustomIntNo4 = :p_CIntNo4) or ( (:p_CIntNo4 is NULL) and (CustomIntNo4 is NULL) ) )
      AND ( (CustomIntNo5 = :p_CIntNo5) or ( (:p_CIntNo5 is NULL) and (CustomIntNo5 is NULL) ) )
    ORDER BY IntNo
    INTO :m_IntNo, :m_InPrice, :m_QRemain, :m_Amount
    DO
    BEGIN
      IF (p_Qty = 0) THEN                   /* 出库数量已处理完, 退出 */
      BEGIN
        r_InPrice = m_Amount_Dec_Total / m_Qty_Save;
        SUSPEND; EXIT;
      END

      IF (p_Qty > m_QRemain) THEN           /* 库存数量少于要出库的数量, 直接减少该批次的数量及金额 */
      BEGIN
        m_Qty_Dec = m_QRemain;
        m_Amount_Dec = m_Amount;
      END
      ELSE                                  /* 库存数量大于要出库的数量, 减少所需的数量, 金额为(平均价*减少的数量) */
      BEGIN
        m_Qty_Dec = p_Qty;
        m_Amount_Dec = m_InPrice * m_Qty_Dec;
      END
      p_Qty = p_Qty - m_Qty_DEC;
      m_Amount_Dec_Total = m_Amount_Dec_Total + m_Amount_Dec;

      UPDATE StkPrice SET QRemain = QRemain - :m_Qty_Dec,
        Amount = Amount - :m_Amount_Dec
        WHERE IntNo = :m_IntNo;
    END

  IF ( (Round(p_Qty,8) > 0) ) THEN
    EXCEPTION FIFO_NOT_Enough_Qty;

  IF (p_Qty = 0) THEN                   /* 出库数量已处理完, 退出 */
  BEGIN
    r_InPrice = m_Amount_Dec_Total / m_Qty_Save;
    SUSPEND; EXIT;
  END
END
---
CREATE PROCEDURE LIST_SET_FIFO_INPRICE(
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(15),
  P_MainIntNo Integer
) AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_InPrice Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_Factor Dec(15,4);

DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
/*
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;

*/
BEGIN
  IF (NOT (p_AddFlag = 1)) THEN                       /* 先进先出时, 不允许取消进出仓 */

    EXCEPTION FIFO_NOT_ALLOW_CANCEL_UPDATEACC;

  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty > 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO

      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u

            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, :m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE SALEGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE PANGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor)
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE ASSGDS SET InPrice = (:m_InPrice/:m_Factor),
          InAmount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END

  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT d.IntNo, d.GoodsIntNo, d.StockIntNo, d.Qty, d.UnitId, d.BatchNo
        ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d

      WHERE (MainIntNo = :p_MainIntNo) AND (d.Qty < 0)
      INTO :m_IntNo, :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_UnitId, :m_BatchNo
        ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF (m_UnitId = 0) THEN
          m_Factor = 1;
        ELSE
          SELECT u.Factor FROM UnitList u
            WHERE (u.GoodsIntNo=:m_GoodsIntNo) AND (u.UnitId=:m_UnitId)
            INTO :m_Factor;
        SELECT r_InPrice
          FROM StkPrice_FIFO_CheckOut(:m_GoodsIntNo, :m_StockIntNo, -:m_Qty*:m_Factor, :m_BatchNo
            ,:m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5)
          INTO :m_InPrice;
        UPDATE BUYGDS SET Price = (:m_InPrice/:m_Factor),
          Amount = :m_Qty * (:m_InPrice/:m_Factor) * Discount
          WHERE IntNo = :m_IntNo;
      END
  END
END
---
CREATE TRIGGER OUTLIST_BEF_UPD_ACC_SETINPRICE FOR OUTLIST BEFORE UPDATE POSITION 3
AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_InAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'SALEGDS', old.IntNo);
      SELECT SUM(InAmount) FROM Salegds WHERE MainIntNo = old.IntNo INTO :m_InAmount;
      IF (m_InAmount IS NULL) THEN m_InAmount = 0;
      new.Profit = new.Gdspay - m_InAmount;
    END
  END
END
---
CREATE TRIGGER INLIST_BEF_UPD_ACC_SETINPRICE FOR INLIST BEFORE UPDATE POSITION 3 AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_Amount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'BUYGDS', old.IntNo);
      SELECT SUM(Amount) FROM Buygds WHERE MainIntNo = old.IntNo INTO :m_Amount;
      IF (m_Amount IS NULL) THEN m_Amount = 0;
      new.Gdspay = m_Amount;
    END
  END
END
---
CREATE TRIGGER PANLIST_BEF_UPD_ACC_SETINPRICE FOR PANLIST BEFORE UPDATE POSITION 3 AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_InAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'PANGDS', old.IntNo);
      SELECT SUM(InAmount) FROM PANgds WHERE MainIntNo = old.IntNo INTO :m_InAmount;
      IF (m_InAmount IS NULL) THEN m_InAmount = 0;
      new.InAmount = m_InAmount;
    END
  END
END
---
CREATE TRIGGER ASSLIST_BEF_UPD_ACC_SETINPRICE FOR ASSLIST BEFORE UPDATE POSITION 3 AS
DECLARE VARIABLE m_Flag SmallInt;
DECLARE VARIABLE m_InAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ( (new.UPDATEACC <> old.UPDATEACC)
          AND (GEN_ID(IntNo_PriceFIFO, 0) = 1)
       ) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE LIST_SET_FIFO_INPRICE(m_Flag, 'ASSGDS', old.IntNo);
      SELECT SUM(InAmount) FROM ASSgds WHERE MainIntNo = old.IntNo INTO :m_InAmount;
      IF (m_InAmount IS NULL) THEN m_InAmount = 0;
      new.InAmount = m_InAmount;
    END
  END
END
--
2003.10.06_03/SQL1
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('PosVisible', '', '前台可见', '4')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
  VALUES ('BusinessSize', '', '规模')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel)
  VALUES ('Edu_Level', '', '学历')
---
INSERT INTO FieldDefine (FieldName, TableName, DLabel, DWidth)
  VALUES ('LRemark2', '', '附加信息', '40')
---
CREATE GENERATOR INTNO_StkPrice;
---
ALTER TABLE Client ADD PosVisible SmallInt DEFAULT 1
---
UPDATE Client SET PosVisible = 1 WHERE ClientFlag = 0
---
UPDATE Client SET PosVisible = 0 WHERE ClientFlag = 1
---
ALTER TABLE Client ADD BusinessSize CHAR(20) DEFAULT ""
---
ALTER TABLE Employee ADD Edu_Level CHAR(16) DEFAULT ""
---
ALTER TABLE OutList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE InList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE MoveList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE PanList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE RealList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE AssList ADD LRemark2 VARCHAR(120)
---
ALTER TABLE Stklst ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklst ADD SumQtyIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklst ADD AvgInPrice DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklsts ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklsts ADD SumQtyIn DEC(15,4)  DEFAULT 0
---
ALTER TABLE Stklsts ADD AvgInPrice DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD SumAmountIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD SumQtyIn DEC(15,4) DEFAULT 0
---
ALTER TABLE Stklstsx ADD AvgInPrice DEC(15,4) DEFAULT 0

---
INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('SumAmountIn', '', 'TAmount')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName)
  VALUES ('SumQtyIn', '', 'TQty')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName, DLabel)
  VALUES ('AvgInPrice', '', 'TPrice', '进价平均值')
---
CREATE TABLE StkPrice (
  INTNO TINTNO,
  GOODSINTNO TGOODSINTNO,
  STOCKINTNO TINTNO,
  Amount TAmount,
  QRemain TQty,
  InPrice TPrice,
  BatchNo TBatchNo,
  CustomIntNo1 Integer,
  CustomIntNo2 Integer,
  CustomIntNo3 Integer,
  CustomIntNo4 Integer,
  CustomIntNo5 Integer,
CONSTRAINT StkPrice_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_STKPRICE_1 ON STKPRICE(GoodsIntNo,StockIntNo,BatchNo);
---
ALTER TABLE StkPrice ADD CONSTRAINT FK_StkPrice_STKLST
  FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO)
  ON UPDATE NO ACTION ON DELETE CASCADE
---
ALTER TABLE StkPrice ADD CONSTRAINT FK_StkPrice_STOCKID
  FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO)
  ON UPDATE CASCADE ON DELETE CASCADE
--
2003.09.10_01/UPGRADE_DETAIL
1、新增订单功能，包括进货订单，及发货订单；
2、在进货主表、进货明细表、发货主表、发货明细表中新增字段“订单号”;
--
2003.09.10_01/SQL_CANCEL
ALTER PROCEDURE CHECK_LISTNO_EXISTS (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_LISTCOUNT SMALLINT
) AS
BEGIN
  SUSPEND;
END
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DeliverDate')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DeliverQty')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('Gdspay')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('BankId')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('AmountBook')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('BookListNo')
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = '' AND UPPER(FIELDNAME) = Upper('DoneAcc')
--
2003.09.10_01/SQL1
/*   创建四个表    */
CREATE GENERATOR INTNO_OutLISTBK
---
CREATE TABLE OUTLISTBK (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,

  DoneAcc SmallInt,
  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  CMPYNAME TCMPYNAME,
  GDSPAY TAMOUNT,
  ACTPAY TAMOUNT,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  BANKINTNO TINTNO,
  AMOUNTBOOK TAMOUNT,
CONSTRAINT OUTLISTBK_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT OUTLISTBK_UNIQUE_1 UNIQUE (LISTNO)
)
---
ALTER TABLE OUTLISTBK ADD CONSTRAINT FK_OUTLISTBK_BankName FOREIGN KEY (BankINTNO)
  REFERENCES BANKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
---
CREATE INDEX IDX_OutLISTBK_LISTDATE ON OutLISTBK(LISTDATE);
---
CREATE GENERATOR INTNO_SALEGDSBK;
---
CREATE TABLE SALEGDSBK (
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO TGOODSINTNO,
  BANKINTNO TINTNO,
  UNITID TGOODSUNITID,

  ACTPAY TAMOUNT,
  STOCKINTNO TINTNO,
  QTY TQTY,
  PRICE TPRICE,
  InPrice TPrice,
  InAmount TAmount,
  DISCOUNT TDISCOUNT,
  AMOUNT TAMOUNT,
  REMARK TREMARK,
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  DELIVERDATE DATE,
  DeliverQty TQty,
CONSTRAINT SALEGDSBK_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_SALEGDSBK_MAININTNO ON SALEGDSBK(MAININTNO);
---
ALTER TABLE SALEGDSBK ADD CONSTRAINT FK_SALEGDSBK_OUTLISTBK FOREIGN KEY (MAININTNO)
  REFERENCES OUTLISTBK (INTNO)
  ON UPDATE NO ACTION
  ON DELETE CASCADE;
---
ALTER TABLE SALEGDSBK ADD CONSTRAINT FK_SALEGDSBK_STKLST FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO)
  ON UPDATE CASCADE;
---
ALTER TABLE SALEGDSBK ADD CONSTRAINT FK_SALEGDSBK_STKNAME FOREIGN KEY (STOCKINTNO)
  REFERENCES STKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
---
CREATE GENERATOR INTNO_InLISTBK;
---
CREATE TABLE InLISTBK (
  INTNO TINTNO,
  UPDATEACC TUPDATEFLAG,
  DoneAcc SmallInt,

  FLAG1 SMALLINT,
  FLAG2 SMALLINT,
  FLAG3 SMALLINT,
  FLAG4 SMALLINT,
  PRINTCOUNT SMALLINT,
  LISTDATE TLISTDATE,
  LISTTIME TLISTTIME,
  LISTNO TLISTNO,
  CMPYNAME TCMPYNAME,
  GDSPAY TAMOUNT,
  ACTPAY TAMOUNT,
  PERSONDEAL TPERSONNAME,
  DEPARTMENT TDEPARTMENT,
  REMARK TREMARK,
  OPERNO TOPERNO,
  BANKINTNO TINTNO,
  AMOUNTBOOK TAMOUNT,
CONSTRAINT INLISTBK_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT INLISTBK_UNIQUE_1 UNIQUE (LISTNO)
);
---
ALTER TABLE INLISTBK ADD CONSTRAINT FK_INLISTBK_BankName FOREIGN KEY (BankINTNO)
  REFERENCES BANKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
---
CREATE INDEX IDX_INLISTBK_LISTDATE ON INLISTBK(LISTDATE);
---
CREATE GENERATOR INTNO_BUYGDSBK;
---
CREATE TABLE BuyGDSBK (
  INTNO TINTNO,
  MAININTNO TINTNO,
  GOODSINTNO TGOODSINTNO,
  BANKINTNO TINTNO,
  UNITID TGOODSUNITID,
  ACTPAY TAMOUNT,
  STOCKINTNO TINTNO,
  QTY TQTY,
  PRICE TPRICE,
  DISCOUNT TDISCOUNT,
  AMOUNT TAMOUNT,
  REMARK TREMARK,
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 TCUSTOMINTNO,
  CUSTOMINTNO2 TCUSTOMINTNO,
  CUSTOMINTNO3 TCUSTOMINTNO,
  CUSTOMINTNO4 TCUSTOMINTNO,
  CUSTOMINTNO5 TCUSTOMINTNO,
  MANUFACTDATE_INT TSMALLINTDATE,
  EXPIREDATE_INT TSMALLINTDATE,
  BATCHNO TBATCHNO,
  DELIVERDATE DATE,
  DeliverQty TQty,
CONSTRAINT BUYGDSBK_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_BUYGDSBK_MAININTNO ON BUYGDSBK(MAININTNO);
---
ALTER TABLE BUYGDSBK ADD CONSTRAINT FK_BUYGDSBK_INLISTBK FOREIGN KEY (MAININTNO)
  REFERENCES INLISTBK (INTNO)
  ON UPDATE NO ACTION
  ON DELETE CASCADE;
---
ALTER TABLE BUYGDSBK ADD CONSTRAINT FK_BUYGDSBK_STKLST FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO)
  ON UPDATE CASCADE;
---
ALTER TABLE BUYGDSBK ADD CONSTRAINT FK_BUYGDSBK_STKNAME FOREIGN KEY (STOCKINTNO)
  REFERENCES STKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
--
2003.09.10_01/SQL2

/* 修改四个表结构, 保存订单号 */
alter table buygds  add BookListNo TListNo
---
alter table Salegds add BookListNo TListNo
---
alter table OutList add BookListNo TListNo
---
alter table InList  add BookListNo TListNo

/* 对新字段进行定义，定义其中文名 */
---
DELETE FROM FieldDefine WHERE Upper(TableName) = 'SALEGDSBK' and (Upper(FieldName) = 'INPRICE')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName, Hidden)
  VALUES ('InPrice', 'SaleGdsBK', 'TPrice', 1)
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = 'TPRINTCOUNT'
---
INSERT INTO DomainDefine (DomainName, DLabel, DFormat, DWidth, FieldTypeName)
 VALUES ('TPrintCount', 'P', '##;;', '2', 'SmallInt')
---
DELETE FROM FieldDefine WHERE Upper(TableName) = 'SALEGDSBK' and (Upper(FieldName) = 'INAMOUNT')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName, Hidden)
  VALUES ('InAmount', 'SaleGdsBK', 'TAmount', 1)
---
DELETE FROM DomainDefine WHERE UPPER(DomainName) = 'TLISTTIME'
---
INSERT INTO DomainDefine (DomainName, DFORMAT, DLabel, DWIDTH, EMASK)
  VALUES ('TListTime', 'HH:MM:SS', '时间', '8', '!90:00:00;1;_')
---
DELETE FROM FieldDefine WHERE (UPPER(FieldName) = 'LISTTIME') and (TableName = '')
---
INSERT INTO FieldDefine (FieldName, TableName, DomainName, Hidden)
  VALUES ('ListTime', '', 'TListTime', 1)
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DWidth, DFormat, EMask)
  VALUES ('', 'DELIVERDATE', '预到货日', '10', 'YYYY.MM.DD', '!0000.99.99;1;')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'GdsPay', '货款', 'TAmount')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'BankId', 'B', 'TBankId')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'AmountBook', '订金', 'TAmount')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'BookListNo', '订单号', 'TListNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel, DomainName)
  VALUES ('', 'DeliverQty', '到货数量', 'TQty')
---
INSERT INTO FieldDefine (TableName, FieldName, dLabel, Dwidth)
  values ('', 'DoneAcc', '完成', 4)
--
2003.09.10_01/SQL3
ALTER PROCEDURE CHECK_LISTNO_EXISTS (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),

  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_LISTCOUNT SMALLINT
) AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN
    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 16) THEN  /* 销售订单 */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 17) THEN  /* 进货订单 */
  BEGIN
    SELECT COUNT(*) FROM INLISTBK WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
---
CREATE PROCEDURE GET_Assgds_Qty_Sum(
  ListNo Char(14)
)
RETURNS (
  Qty_Finish NUMERIC(15, 4),
  Qty_Material NUMERIC(15, 4)
)
AS
BEGIN
  SELECT Sum(d.Qty) From AssGds d, AssList m
    WHERE (m.ListNo = :Listno) and (m.IntNo = d.MainIntNo)
      and (d.Direct = 0)
    INTO Qty_Finish;
  IF (Qty_Finish IS NULL) THEN Qty_Finish = 0;
  SELECT Sum(d.Qty) From AssGds d, AssList m
    WHERE (m.ListNo = :Listno) and (m.IntNo = d.MainIntNo)
      and (d.Direct = 1)
    INTO Qty_Material;
  IF (Qty_Material IS NULL) THEN Qty_Material = 0;
  SUSPEND;
END
---
/* 创建销售订单总表存储过程，用于修改生成的金额 */
CREATE PROCEDURE UPDATE_OUTLISTBK_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE OUTLISTBK SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
END
/* SALEGDSBK 对应的3个触发器 */
---
CREATE TRIGGER SALEGDSBK_AFTER_DELETE_1 FOR SALEGDSBK AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(old.Amount, -1, old.MAININTNO);
END
---
CREATE TRIGGER SALEGDSTBK_AFTER_INSERT_1 FOR SALEGDSBK AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(new.Amount, 1, new.MAININTNO);
END
---
CREATE TRIGGER SALEGDSBK_AFTER_UPDATE_1 FOR SALEGDSBK AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
BEGIN
  m_amount = new.Amount - old.Amount;
  EXECUTE PROCEDURE UPDATE_OUTLISTBK_AMOUNT(:m_amount, 1, old.MAININTNO);
END
---
/* 创建进货订单总表存储过程，用于修改明细表中生成的金额 */
CREATE PROCEDURE UPDATE_InLISTBK_AMOUNT (
  P_GDSPAY NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE INLISTBK SET GDSPAY = GDSPAY + (:p_GDSPAY*:Direct)
    WHERE INTNO = :MainIntNo;
END
---
/* 创建 BuygdsBK 对应的触发器 */
CREATE TRIGGER BuyGDSBK_AFTER_DELETE_1 FOR BuyGDSBK AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_InLISTBK_AMOUNT(old.Amount, -1, old.MAININTNO);
END
---
CREATE TRIGGER BuyGDSTBK_AFTER_INSERT_1 FOR BuyGDSBK AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_InLISTBK_AMOUNT(new.Amount, 1, new.MAININTNO);
END
---
CREATE TRIGGER BuyGDSBK_AFTER_UPDATE_1 FOR BuyGDSBK AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
BEGIN
  m_amount = new.Amount - old.Amount;
  EXECUTE PROCEDURE UPDATE_InLISTBK_AMOUNT(:m_amount, 1, old.MAININTNO);
END

---
/* 创建销售订单明细表的一个存储过程 */
CREATE PROCEDURE UPDATE_SaleGDSBK_DELIVERQTY (
  QTY NUMERIC(15, 4),
  DIRECT SMALLINT,
  BOOKLISTNO CHAR(14),
  GOODSINTNO INTEGER,
  unitid smallint
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE SaleGds_Factor NUMERIC(15, 4);
DECLARE VARIABLE SaleGdsBk_Factor NUMERIC(15, 4);
BEGIN
  if (GoodsIntNo is null) then exit;
  select IntNo from OutListBK where ListNo = :BookListNo INTO :m_IntNo;
  IF (m_IntNo is NULL) THEN exit;
  select u.Factor from UnitList u
    where ((u.GoodsIntNo = :GoodsIntNo) and (u.UnitId = :UnitId))
      into :SaleGds_Factor;
  /* 先不处理自定义内容及批号 */
  FOR SELECT u.Factor From UnitList u, SaleGdsBk d
    where (d.MainIntNo = :m_IntNo) and (d.GoodsIntNo = :GoodsIntNo) and
        (u.GoodsIntNo=d.GoodsIntNo and u.UnitId=d.UnitId)
    INTO :SaleGdsBk_Factor DO
    UPDATE SaleGdsBK SET DeliverQty = DeliverQty + (:Qty*:SaleGds_Factor/:SaleGdsBk_Factor*:Direct)
      WHERE (MainIntNo = :m_IntNo) and (GoodsIntNo = :GoodsIntNo);
END
---
/* 创建销售明细表三个触发器 */
CREATE TRIGGER SALEGDS_AFTER_Upd_DELIVERQTY FOR SALEGDS AFTER Update POSITION 0 AS
BEGIN
  if ((old.goodsintno <> new.goodsintno) or (old.qty <>new.qty) or (old.unitid <> new.UnitId) ) then
  begin
    EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(old.Qty, -1, old.BookListNo, old.GoodsIntNo, old.unitid);
    EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(new.Qty, 1, new.BookListNo, new.GoodsIntNo, new.unitid);
  end
END
---
CREATE TRIGGER SALEGDS_AFTER_Ins_DELIVERQTY FOR SALEGDS AFTER Insert POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(new.Qty, 1, new.BookListNo, new.GoodsIntNo, new.unitid);
END
---
CREATE TRIGGER SALEGDS_AFTER_Del_DELIVERQTY FOR SALEGDS AFTER Delete POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_SaleGdsBK_DeliverQty(old.Qty, -1, old.BookListNo, old.GoodsIntNo, Old.unitid);
END
---

/* 创建销售订单明细表的一个存储过程 */
CREATE PROCEDURE UPDATE_BuyGDSBK_DELIVERQTY (
  QTY NUMERIC(15, 4),
  DIRECT SMALLINT,
  BOOKLISTNO CHAR(14),
  GOODSINTNO INTEGER,
  unitid smallint
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE BuyGds_Factor NUMERIC(15, 4);
DECLARE VARIABLE BuyGdsBk_Factor NUMERIC(15, 4);
BEGIN

  if (GoodsIntNo is null) then exit;
  select IntNo from InListBK where ListNo = :BookListNo INTO :m_IntNo;
  IF (m_IntNo is NULL) THEN exit;
  select u.Factor from UnitList u

    where ((u.GoodsIntNo = :GoodsIntNo) and (u.UnitId = :UnitId))
      into :BuyGds_Factor;
  select u.Factor From UnitList u, BuyGdsBk d
    where (d.MainIntNo = :m_IntNo) and (d.GoodsIntNo = :GoodsIntNo) and
        (u.GoodsIntNo=d.GoodsIntNo and u.UnitId=d.UnitId)
      Into :BuyGdsBk_Factor;
  UPDATE BuyGdsBK SET DeliverQty = DeliverQty + (:Qty*:BuyGds_Factor/:BuyGdsBk_Factor*:Direct)
    WHERE (MainIntNo = :m_IntNo) and (GoodsIntNo = :GoodsIntNo);
END
---
/* 创建进货订单明细表三个触发器 */
CREATE TRIGGER BuyGDS_AFTER_Upd_DELIVERQTY FOR BuyGDS AFTER Update POSITION 0 AS
BEGIN
  if ((old.goodsintno <> new.goodsintno) or (old.qty <>new.qty) or (old.unitid <> new.UnitId) ) then
  begin
    EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(old.Qty, -1, old.BookListNo, old.GoodsIntNo, old.unitid);
    EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(new.Qty, 1, new.BookListNo, new.GoodsIntNo, new.unitid);
  end
END
---
CREATE TRIGGER BuyGDS_AFTER_Ins_DELIVERQTY FOR BuyGDS AFTER Insert POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(new.Qty, 1, new.BookListNo, new.GoodsIntNo, new.unitid);
END
---
CREATE TRIGGER BuyGDS_AFTER_Del_DELIVERQTY FOR BuyGDS AFTER Delete POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_BuyGdsBK_DeliverQty(old.Qty, -1, old.BookListNo, old.GoodsIntNo, Old.unitid);
END
---
/* 创建存储过程根据到货数量,判断是否完成,以更新完成标志 */
CREATE PROCEDURE UPDATE_InListBk_DoneAcc (
  MainIntNo  NUMERIC(15, 4)
) AS
DECLARE VARIABLE m_Done Integer;
BEGIN
  SELECT Count(IntNo) FROM BuyGdsBk
    WHERE ((MainIntNo = :MainIntNo) AND (Round(Qty,8) > Round(DeliverQty,8)))
    INTO :m_Done;
  IF (m_Done = 0) THEN
    UPDATE InListBk SET DoneAcc = 1 WHERE (IntNo = :MainIntNo) AND ((DoneAcc <> 1) OR (DoneAcc is NULL));
  ELSE
    UPDATE InListBk SET DoneAcc = 0 WHERE (IntNo = :MainIntNo) AND (DoneAcc = 1);
END
---
/* 创建存储过程根据到货数量,判断是否完成,以更新完成标志 */
CREATE PROCEDURE UPDATE_OutListBk_DoneAcc (
 MainIntNo  NUMERIC(15, 4)
)  AS
DECLARE VARIABLE m_Done Integer;
BEGIN
  SELECT Count(IntNo) FROM SaleGdsBk
    WHERE ((MainIntNo = :MainIntNo) AND (Round(Qty,8) > Round(DeliverQty,8)))
    INTO :m_Done;
  IF (m_Done = 0) THEN
    UPDATE OutListBk SET DoneAcc = 1 WHERE (IntNo = :MainIntNo) AND ((DoneAcc <> 1) OR (DoneAcc is NULL));
  ELSE
    UPDATE OutListBk SET DoneAcc = 0 WHERE (IntNo = :MainIntNo) AND (DoneAcc = 1);
END
---
/* 创建订单明细表的六个触发器用于本身到货数量被更改时,刷新主表的完成标志 */
CREATE TRIGGER SaleGdsBk_AFTER_Upd_DELIVERQTY FOR SaleGDSBK AFTER UPdate POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OutListBk_DoneAcc(old.MainIntNo);
END
---
CREATE TRIGGER SaleGdsBk_AFTER_Del_DELIVERQTY FOR SaleGDSBK AFTER Delete POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OutListBk_DoneAcc(old.MainIntNo);
END
---
CREATE TRIGGER SaleGdsBk_AFTER_Ins_DELIVERQTY FOR SaleGDSBK AFTER Insert POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OutListBk_DoneAcc(new.MainIntNo);
END
---
CREATE TRIGGER BuyGdsBk_AFTER_Upd_DELIVERQTY FOR BuyGDSBK AFTER UPdate POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_InListBk_DoneAcc(old.MainIntNo);
END
---
CREATE TRIGGER BuyGdsBk_AFTER_Del_DELIVERQTY FOR BuyGDSBK AFTER Delete POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_InListBk_DoneAcc(old.MainIntNo);
END

---
CREATE TRIGGER BuyGdsBk_AFTER_Ins_DELIVERQTY FOR BuyGDSBK AFTER Insert POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_InListBk_DoneAcc(new.MainIntNo);
END
--
2003.08.16_01/UPGRADE_DETAIL
调整装拆单: 将数量由合一的[装拆]改为[装拆入][装拆出];
--
2003.08.16_01/SQL1
CREATE TRIGGER STKLST_BEFORE_UPDATE_1 FOR STKLST BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTS_BEFORE_UPDATE_1 FOR STKLSTS BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTSX_BEFORE_UPDATE_1 FOR STKLSTSX BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.QRemain <> old.QRemain) THEN
    new.QRemain = Round(new.QRemain, 8);
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
DELETE FROM FieldDefine WHERE ( (Upper(FieldName) = Upper('QtyAssIn'))
  OR (Upper(FieldName) = Upper('QtyAssOut'))
  OR (Upper(FieldName) = Upper('AmountAssOut'))
  OR (Upper(FieldName) = Upper('AmountAssIn')) )

  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'QtyAssIn', 'TQty', '装拆进')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'QtyAssOut', 'TQty', '装拆出')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'AmountAssIn', 'TAmount', '装拆进金额')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'AmountAssOut', 'TAmount', '装拆出金额')
---
ALTER PROCEDURE GETSTOCKIO (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      FROM BUYGDS d
      LEFT OUTER JOIN INLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      FROM SALEGDS d
      LEFT OUTER JOIN OUTLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM PANGDS d
      LEFT OUTER JOIN PANLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      FROM ASSGDS d
      LEFT OUTER JOIN ASSLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
      InAmount = -InAmount;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,

      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d
      LEFT OUTER JOIN INLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      LEFT OUTER JOIN OUTLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      LEFT OUTER JOIN PANLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      LEFT OUTER JOIN ASSLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
      InAmount = -InAmount;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOEXB (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE TIMESTAMP,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  EXPIREDATE_INT SMALLINT,
  MANUFACTDATE_INT SMALLINT
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d
      LEFT OUTER JOIN INLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d
      LEFT OUTER JOIN OUTLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d
      LEFT OUTER JOIN PANLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d
      LEFT OUTER JOIN ASSLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
  BEGIN
    IF ((WorkMode = 12) AND (Round(QtyIn, 8) < 0)) THEN
    BEGIN
      QtyOut = - QtyIn;
      QtyIn = 0;
      InAmount = -InAmount;
    END
    Suspend;
  END
END
---
ALTER PROCEDURE GETSTOCKIOLIST (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4)
) AS 
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  /* 发货 */
  FOR
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 7;
  /* 差额盘点 */
  FOR
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 12;
  /* 组装与拆解 */
  FOR
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AmountAssIn, 0.0 as AmountAssOut
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  /* 调拨入 */
  FOR
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m

      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 3;
  /* 调拨出 */
  FOR
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
      DO SUSPEND;

  WorkMode = 1;
  /* 进货 */

  FOR
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, AmountAssOut
      DO SUSPEND;

END
---
ALTER PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS   
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      d.InAmount as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')

    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      u.Factor*d.Qty as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      d.InAmount as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO
      BEGIN
        IF (Round(QtyAssIn, 8) < 0) THEN
        BEGIN
          QtyAssOut = - QtyAssIn;
          AmountAssOut = - AmountAssIn;
          QtyAssIn = 0;
          AmountAssIn = 0;
        END
        SUSPEND;
      END

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,   0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN, 0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,

          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn,  0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m
      WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AMOUNTASSIN,  0.0 as AMOUNTASSOUT
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

      ,d.BatchNo
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AMOUNTASSIN,  :AMOUNTASSOUT
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

END
---

CREATE PROCEDURE GETSTOCKIOSUM (
  P_DATEFROM TIMESTAMP,
  P_DATETO TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QINITREMAIN NUMERIC(15, 4),
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASSIN NUMERIC(15, 4),
  QTYASSOUT NUMERIC(15, 4),
  INITAMOUNT NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASSIN NUMERIC(15, 4),
  AMOUNTASSOUT NUMERIC(15, 4)
) AS 
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  FOR
    Select GoodsIntNo, StockIntNo,
      Sum(QtyIn)-Sum(QtyOut)+Sum(QtyMoveIn)-Sum(QtyMoveOut)+Sum(QtyPan)+
      Sum(QtyAssIn-QtyAssOut) as QInitRemain,
      Sum(AmountIn)-Sum(AmountOut)+Sum(AmountMoveIn)-Sum(AmountMoveOut)+Sum(AmountPan)+
      Sum(AmountAssIn-AmountAssOut) as InitAmount,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan,
      0.0 as QtyAssIn, 0.0 as QtyAssOut,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut,
      0.0 as AmountPan,
      0.0 as AmountAssIn, 0.0 as AmountAssOut
      From GetstockIolist('0001.1.1', :p_DateFrom-1)
      Group by GoodsIntNo, StockIntNo
    UNION ALL
    Select GoodsIntNo, StockIntNo,
      0.0 as QInitRemain,
      0.0 as InitAmount,
      Sum(QtyIn) as QtyIn, Sum(QtyOut) as QtyOut, Sum(QtyMoveIn) as QtyMoveIn,

      Sum(QtyMoveOut) as QtyMoveOut, Sum(QtyPan) as QtyPan,
      Sum(QtyAssIn) as QtyAssIn, Sum(QtyAssOut) as QtyAssOut,
      Sum(AmountIn) as AmountIn, Sum(AmountOut) as AmountOut, Sum(AmountMoveIn) as AmountMoveIn,
      Sum(AmountMoveOut) as AmountMoveOut, Sum(AmountPan) as AmountPan,
      Sum(AmountAssIn) as AmountAssIn, Sum(AmountAssOut) as AmountAssOut
      From GetstockIolist(:p_DateFrom, :p_DateTo)
      Group by GoodsIntNo, StockIntNo
        INTO :GoodsIntNo, :StockIntNo, :QInitRemain, :InitAmount,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan,
          :QtyAssIn, :QtyAssOut,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan,
          :AmountAssIn, :AmountAssOut
    DO SUSPEND;
END
---
CREATE PROCEDURE GET_PAN_FROM_REAL (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,

  REALFLAG INTEGER
) AS
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut) as InAmount
    FROM GETSTOCKIOLIST(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
    FROM Stklsts d
    GROUP BY d.GoodsIntNo, d.StockIntNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
---
CREATE PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS    
BEGIN
FOR
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAssIn-d.QtyAssOut) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAssIn-d.AmountAssOut) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5

      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
          ,:ManuFactDate_Int, :ExpireDate_Int
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
--
2003.08.03_01/UPGRADE_DETAIL
1. 客户帐/供应商帐: 增加[批号][自定义内容]的显示;
2. 客户帐: 增加[进价]的显示;
--
2003.08.03_01/SQL1
CREATE PROCEDURE QUERY_CMPYNAME_DETAIL (
  P_CMPYNAME CHAR(12),
  P_DATEFROM TIMESTAMP,
  P_CLIENTFLAG SMALLINT
) RETURNS (
  INTNO INTEGER,
  LISTDATE TIMESTAMP,
  GOODSINTNO INTEGER,
  GOODSNO CHAR(20),
  GOODS CHAR(80),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  QTY NUMERIC(15, 4),
  PRICE NUMERIC(15, 4),
  INPRICE NUMERIC(15, 4),
  DISCOUNT NUMERIC(15, 4),
  NETPRICE NUMERIC(15, 4),
  AMOUNT NUMERIC(15, 4),
  BANKID CHAR(8),
  ACTPAY NUMERIC(15, 4),
  TMPREMAIN NUMERIC(15, 4),
  LISTNO CHAR(14),
  REMARK CHAR(20),
  MAINREMARK CHAR(20),
  UPDATEFLAG SMALLINT,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO CHAR(20),
  MANUFACTDATE_INT INTEGER,
  EXPIREDATE_INT INTEGER
) AS   
BEGIN
  IF (p_ClientFLag = 1) THEN /* 供应商 */
  BEGIN
    FOR
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit,
      StkName.StockId, d.Qty, d.Price, d.Price, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 





      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag 
      , d.CustomIntNo1,  d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5 
      , d.BatchNo, d.ManuFactDate_Int, d.ExpireDate_Int
      From Buygds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)  
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)  
      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)  
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo) 
      LEFT OUTER JOIN InList m On (m.IntNo = d.MainIntNo)  
      Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :InPrice, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
         , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
         :BatchNo, :ManuFactDate_Int, :ExpireDate_Int
    DO SUSPEND;
  END
  ELSE  /* 客户 */
  BEGIN
    FOR
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit, 
      StkName.StockId, d.Qty, d.Price, d.InPrice, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 
      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag 
      , d.CustomIntNo1,  d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ManuFactDate_Int, d.ExpireDate_Int
      From Salegds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)
      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo)
      LEFT OUTER JOIN OutList m On (m.IntNo = d.MainIntNo)  
      Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :InPrice, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
         , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5,
         :BatchNo, :ManuFactDate_Int, :ExpireDate_Int
    DO SUSPEND;
  END
END
--
2003.07.29_01/UPGRADE_DETAIL
1. 增加: 前台复制后台基本资料时, 能够复制[职员资料];
--
2003.07.29_01/SQL1
ALTER TABLE EMPLOYEE ADD INSERTUCT INTEGER
---
ALTER TABLE EMPLOYEE ADD UPDATEUCT INTEGER
---
CREATE TRIGGER EMPLOYEE_BEF_INSERT_EVENTLOG FOR EMPLOYEE
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER EMPLOYEE_BEF_UPDATE_EVENTLOG FOR EMPLOYEE
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER EMPLOYEE_BEFORE_INSERT_SET_DEF FOR EMPLOYEE BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.PERSONNO is NULL) THEN new.PERSONNO = '';
  IF (new.PERSONNAME is NULL) THEN new.PERSONNAME = '';
  IF (new.PERSONTEL is NULL) THEN new.PERSONTEL = '';
  IF (new.PERSONPAGE is NULL) THEN new.PERSONPAGE = '';
  IF (new.BIRTHDATE is NULL) THEN new.BIRTHDATE = '';
  IF (new.LREMARK is NULL) THEN new.LREMARK = '';
  IF (new.MPHONE is NULL) THEN new.MPHONE = '';
  IF (new.IDNO is NULL) THEN new.IDNO = '';
  IF (new.ADDRESS is NULL) THEN new.ADDRESS = '';
  IF (new.JOBPOSITION is NULL) THEN new.JOBPOSITION = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
CREATE TRIGGER EMPLOYEE_BEFORE_UPDATE_SET_DEF FOR EMPLOYEE BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.PERSONNO is NULL) THEN new.PERSONNO = '';
  IF (new.PERSONNAME is NULL) THEN new.PERSONNAME = '';
  IF (new.PERSONTEL is NULL) THEN new.PERSONTEL = '';
  IF (new.PERSONPAGE is NULL) THEN new.PERSONPAGE = '';
  IF (new.BIRTHDATE is NULL) THEN new.BIRTHDATE = '';
  IF (new.LREMARK is NULL) THEN new.LREMARK = '';
  IF (new.MPHONE is NULL) THEN new.MPHONE = '';
  IF (new.IDNO is NULL) THEN new.IDNO = '';
  IF (new.ADDRESS is NULL) THEN new.ADDRESS = '';
  IF (new.JOBPOSITION is NULL) THEN new.JOBPOSITION = '';
  IF (new.DEPARTMENT is NULL) THEN new.DEPARTMENT = '';
END
---
UPDATE EMPLOYEE SET INSERTUCT = zs_GetUnixTime() - 100
--
2003.07.24_01/UPGRADE_DETAIL
1. 增加: 前台复制后台基本资料时, 能够复制[积分价格表];
--
2003.07.24_01/SQL1
CREATE TRIGGER AMOUNTLEVEL_BEF_INSERT_SET_DEF FOR AMOUNTLEVEL BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
END
---
CREATE TRIGGER AMOUNTLEVEL_BEF_UPDAET_SET_DEF FOR AMOUNTLEVEL BEFORE UPDATE POSITION 0 AS

BEGIN
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
END
--
2003.07.17_01/UPGRADE_DETAIL
1. 增加: 表AmountLevel, 用于记录积分对应的价格表;
2. 当客户的累计货款改变时, 自动设置该客户的价格表代号;
   (当该客户使用[积分价格表]时);
--
2003.07.17_01/SQL2
CREATE GENERATOR INTNO_AmountLevel
--
2003.07.17_01/SQL1
ALTER TABLE CLIENT ADD AutoPriceNo SmallInt DEFAULT 0
---
CREATE TABLE AmountLevel
(
  INTNO		TINTNO,
  SumAmount 	Dec(15,4) NOT NULL,
  PriceNo 	TPriceNo,
  LRemark	TLRemark,
  ClientFlag 	TCLIENTFLAG,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
CONSTRAINT AmountLevel_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE TRIGGER AmountLevel_BEF_INSERT_EVENTLOG FOR AmountLevel
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN

  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER AmountLevel_BEF_UPDATE_EVENTLOG FOR AmountLevel
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE EXCEPTION MSG_AMOUNTLEVEL_AMOUNT_EXISTS "积分金额已存在, 不能重复"
---
CREATE PROCEDURE CHECK_AMOUNTLVEL_Amount(
  p_ClientFlag SmallInt,
  p_SumAmount DEC(15,4)
)
AS
DECLARE VARIABLE m_Count Integer;
BEGIN
  m_Count = 0;
  SELECT COUNT(*) FROM AmountLevel
    WHERE (ClientFlag = :p_ClientFlag) AND (SumAmount = :p_SumAmount)
    INTO :m_Count;
  IF (m_Count > 0) THEN

    EXCEPTION MSG_AMOUNTLEVEL_AMOUNT_EXISTS;
END
---



CREATE TRIGGER AMOUNTLEVEL_BEF_INS_CHK_AMOUNT FOR AMOUNTLEVEL BEFORE INSERT POSITION 0 AS
/*检查SumAmount是否存在, 如存在, 则激活Exception*/
BEGIN
  EXECUTE PROCEDURE CHECK_AMOUNTLVEL_Amount(new.ClientFlag, new.SumAmount);
END
---
CREATE TRIGGER AMOUNTLEVEL_BEF_UPD_CHK_AMOUNT FOR AMOUNTLEVEL BEFORE UPDATE POSITION 0 AS
/*如果SumAmount/ClientFlag被改变, 检查SumAmount是否存在, 如存在, 则激活Exception*/
BEGIN
  IF ((new.SumAmount <> old.SumAmount) or (new.ClientFLag <> old.ClientFlag)) THEN
    EXECUTE PROCEDURE CHECK_AMOUNTLVEL_Amount(new.ClientFlag, new.SumAmount);
END
---
CREATE PROCEDURE Get_PriceNo_From_SumAmount(
  p_ClientFlag SmallInt,
  p_SumAmount DEC(15,4)
) RETURNS (
  r_PriceNo Char(8)
)
AS
DECLARE VARIABLE m_SumAmount DEC(15,4);
BEGIN
  r_PriceNo = '';
  FOR SELECT SumAmount, PriceNo FROM AmountLevel
    WHERE ClientFlag = :p_ClientFlag
    ORDER BY SumAmount DESC
    INTO :m_SumAmount, :r_PriceNo
    DO
    BEGIN
      IF (p_SumAmount >= m_SumAmount) THEN
      BEGIN
        SUSPEND;
        EXIT;
      END
    END
END
---
CREATE PROCEDURE APPLY_AMOUNTLEVEL (
  p_ClientFlag SmallInt,
  p_CmpyNo CHAR(8)
)  AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_Gdspay DEC(15,4);
DECLARE VARIABLE m_PriceNo CHAR(8);
BEGIN
  FOR SELECT IntNo, Gdspay FROM Client
    WHERE (ClientFlag = :p_ClientFlag) AND (AutoPriceNo = 1)
      AND ( (:p_CmpyNo = '') OR ((:p_CmpyNo > '') AND (CmpyNo = :p_CmpyNo)) )
    INTO :m_IntNo, :m_Gdspay
    DO
    BEGIN
      SELECT r_PriceNo FROM Get_PriceNo_From_SumAmount(:p_ClientFlag, :m_Gdspay)
        INTO :m_PriceNo;
      IF (m_PriceNo IS NULL) THEN m_PriceNo = '';
      UPDATE Client SET PriceNo = :m_PriceNo WHERE IntNo = :m_IntNo;
    END
END
---
CREATE TRIGGER CLIENT_BEF_UPDATE_SET_PRICENO FOR CLIENT
ACTIVE BEFORE UPDATE POSITION 0 AS
DECLARE VARIABLE m_PriceNo CHAR(8);
BEGIN
  IF ((new.AutoPriceNo = 1) AND (new.Gdspay <> old.Gdspay) AND (new.PriceNo = old.PriceNo)) THEN
  BEGIN
    EXECUTE PROCEDURE Get_PriceNo_From_SumAmount(new.ClientFlag, new.Gdspay) RETURNING_VALUES :m_PriceNo;
    new.PriceNo = m_PriceNO;
  END
END
--
2003.07.16_01/UPGRADE_DETAIL
1. 客户/供应商资料: 增加(性别/生日/性格描述)字段;
2. 增加: 生日提醒功能;

   注意:
   本次升级需要版本为 1.4.5 的支持文件 ZSUDF.DLL;
--
2003.07.16_01/SQL1
ALTER TABLE CLIENT ADD BirthDay TimeStamp
---
ALTER TABLE CLIENT ADD Sex SmallInt
---
ALTER TABLE CLIENT ADD PersonCharacter CHAR(80)
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = Upper('TPersonCharacter')
---
INSERT INTO DomainDefine (DomainName, DLabel, DWidth)

  VALUES ('TPersonCharacter', '性格描述', '40')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('PersonCharacter'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'PersonCharacter', 'TPersonCharacter')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('Birthday'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Birthday', 'TBirthday')
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = Upper('TBirthday')
---
INSERT INTO DomainDefine (DomainName, DLabel, DWidth, DFormat, EMask)
  VALUES ('TBirthday', '出生日期', '10', 'YYYY.MM.DD', '!0000.99.99;1;')
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = Upper('TSex')
---
INSERT INTO DomainDefine (DomainName, DLabel, DWidth)
  VALUES ('TSex', '性别', '4')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('Sex'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Sex', 'TSex')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('BirthdayDate'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'BirthdayDate', 'TBirthdayDate')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('BirthdayDate0'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'BirthdayDate0', 'TBirthdayDate', '今年生日')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('BirthdayDate1'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'BirthdayDate1', 'TBirthdayDate', '明年生日')
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = Upper('TBirthdayDate')
---
INSERT INTO DomainDefine (DomainName, DLabel, DWidth, DFormat, EMask)
  VALUES ('TBirthdayDate', '生日', '10', 'YYYY.MM.DD', '!0000.99.99;1;')
---
DELETE FROM DomainDefine WHERE Upper(DomainName) = Upper('TAutoPriceNo')
---
INSERT INTO DomainDefine (DomainName, DLabel, DWidth)
  VALUES ('TAutoPriceNo', '使用积分价格表', '10')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName) = Upper('AutoPriceNo'))
  AND (TableName = '')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'AutoPriceNo', 'TAutoPriceNo')
---

DECLARE EXTERNAL FUNCTION ZS_GETBIRTHDAYDATE
   DATE, INTEGER
   RETURNS DATE
  ENTRY_POINT "zsudf_GetBirthdayDate"  MODULE_NAME "zsudf"
---
CREATE VIEW VIEW_GET_CLIENT_BIRTHDAY (
  INTNO,
  CMPYNO,
  CMPYNAME,
  GDSPAY,
  ACTPAY,
  INITREMAIN,
  TELE,
  FAX,
  PRICENO,
  CMPYBOSS,
  CMPYCONTACT,
  ADDRESS,
  TAXNO,
  LREMARK,
  CMPYFNAME,
  CMPYBANKNAME,
  CMPYBANKNO,
  CLIENTFLAG,
  PERSONDEAL,
  CLIENTTYPE,
  BIRTHDAY,
  SEX,
  PERSONCHARACTER,
  BirthdayDate0,
  BirthdayDate1
) AS
SELECT c.IntNo, c.CmpyNo, c.CmpyName, c.Gdspay, c.Actpay, c.InitRemain, c.Tele,
c.Fax, c.PriceNo, c.CmpyBoss, c.CmpyContact, c.Address, c.TaxNo, c.LRemark, c.CmpyFName,
c.CmpyBankName, c.CmpyBankNo, c.ClientFlag, c.PersonDeal, c.ClientType,
c.Birthday, c.Sex, c.PersonCharacter,
zs_GetBirthdayDate(c.Birthday, 0),
zs_GetBirthdayDate(c.Birthday, 1)
FROM Client c

--
2003.07.10_02/UPGRADE_DETAIL
1. 调整平均进价的计算方法(当库存量接近零时);
2. 增加资金帐明细记录保存时对单号的判断:
   * 资金帐明细中直接录入的记录, 单号不允许重复;

   * 其他收付款生成的资金帐明细记录, 单号允许重复;

3. 客户名称被修改后, 自动重新计算新名称的货款/欠款等;
--
2003.07.10_02/SQL1
UPDATE FieldDefine SET DLABEL = '供应商' WHERE (DLabel like '供货客户%')
---
CREATE TRIGGER STKLST_BEFORE_UPDATE_1 FOR STKLST
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTS_BEFORE_INSERT_1 FOR STKLSTS 
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTS_BEFORE_UPDATE_1 FOR STKLSTS 
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTSX_BEFORE_INSERT_1 FOR STKLSTSX 
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;

END
---
CREATE TRIGGER STKLSTSX_BEFORE_UPDATE_1 FOR STKLSTSX 
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (Round(new.QRemain,8) <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (Round(new.QINITRemain,8) <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE EXCEPTION MSG_BANKS_LISTNO_EMPTY "流水号不能为空"
---
CREATE EXCEPTION MSG_BANKS_LISTNO_EXISTS "同类型的流水号不能重复"
---
CREATE PROCEDURE CHECK_BANKS_LISTNO_EXISTS (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_LISTCOUNT SMALLINT
) AS 
BEGIN
  R_ListCount = 0;
  SELECT COUNT(*) FROM BANKS WHERE (ListNo = :p_ListNo) AND (WORKMODE = :p_WorkMode)
    INTO :R_ListCount;
  IF (p_WorkMode = 4) THEN   /* wmBanks */
  BEGIN
    IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN

      EXCEPTION MSG_BANKS_LISTNO_EXISTS;
  END
END
---
CREATE TRIGGER BANKS_BEFORE_INSERT_CHK_LISTNO FOR BANKS BEFORE INSERT POSITION 0 AS
/*检查流水号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  IF ( (new.ListNo = '') OR (new.ListNo is NULL) ) THEN
    EXCEPTION MSG_BANKS_LISTNO_EMPTY;
  EXECUTE PROCEDURE CHECK_BANKS_LISTNO_EXISTS (new.WorkMode, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
CREATE TRIGGER BANKS_BEFORE_UPDATE_CHK_LISTNO FOR BANKS BEFORE UPDATE POSITION 0 AS
/*如果流水号被改变, 则检查新的流水号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  IF (new.ListNo <> old.ListNo) THEN
  BEGIN
    IF ( (new.ListNo = '') OR (new.ListNo is NULL) ) THEN
      EXCEPTION MSG_BANKS_LISTNO_EMPTY;
    EXECUTE PROCEDURE CHECK_BANKS_LISTNO_EXISTS (new.WorkMode, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
  END
END
---
CREATE TRIGGER CLIENT_AFTER_UPDATE_SET_LIMIT FOR CLIENT AFTER UPDATE POSITION 0 AS
/* 执行结帐操作 */
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ((old.S_DATE <> new.S_Date) 
         or (old.Create_RM <> new.Create_RM)
         or (old.CmpyName <> new.CmpyName)
       ) THEN
      EXECUTE PROCEDURE CLIENT_SET_LIMIT (new.CLIENTFLAG, new.CmpyName, new.S_Date);
  END
END
--
2003.06.26_01/UPGRADE_DETAIL
1. 调整医药版分布式盘点生效操作，使之能处理生产日期及有效期;
--
2003.06.26_01/SQL1
CREATE PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(20),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20),
  ManuFactDate_Int Integer,
  ExpireDate_Int Integer
) AS  
BEGIN

FOR

  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAss) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAss) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,Max(d.ManuFactDate_Int)+0 as ManuFactDate_Int
      ,Max(d.ExpireDate_Int)+0 as ExpireDate_Int
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL

  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      ,0 as ManuFactDate_Int, 0 as ExpireDate_Int
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo

          ,:ManuFactDate_Int, :ExpireDate_Int
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
--
2003.06.19_01/UPGRADE_DETAIL
1. 调整医药版进仓操作，使之能支持生产日期为空或有效期为空的记录;
--
2003.06.19_01/SQL1
ALTER PROCEDURE STOCK_UPDATE_ITEMX_D (
  P_DIRECT SMALLINT,
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER,
  P_EXPIREDATE_INT SMALLINT,
  P_BATCHNO VARCHAR(20),
  P_MANUFACTDATE_INT SMALLINT
)  AS    
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_OldExpireDate_Int SmallInt;
DECLARE VARIABLE m_OldManuFactDate_Int SmallInt;
DECLARE VARIABLE m_QtyIn Dec(15,4);
DECLARE VARIABLE m_QtyOut Dec(15,4);
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END

  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END

  m_QtyIn = 0; m_QtyOut = 0;
  IF (P_Direct > 0) THEN                         /* 属于正方向的操作 */
    m_QtyIn = p_Qty;
  ELSE
    m_QtyOut = -p_Qty;

  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  IF (p_BatchNo = '') THEN p_BatchNo = NULL;
  IF (p_ExpireDate_Int is NULL) THEN p_ExpireDate_Int = 0;
  IF (p_ManuFactDate_Int is NULL) THEN p_ManuFactDate_Int = 0;

  m_IntNo = NULL;

  BEGIN
    m_OldExpireDate_Int = 0;
    m_OldManuFactDate_Int = 0;

    SELECT INTNO, ExpireDate_Int, ManuFactDate_Int FROM STKLSTSX
      WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)
        and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
        and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
        and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
        and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
        and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
        and ((BatchNo = :p_BatchNo) or (:p_BatchNo is NULL and BatchNo is NULL))
    INTO :m_IntNo, :m_OldExpireDate_Int, m_OldManuFactDate_Int;
    IF (m_IntNo is NULL) THEN

      INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int, QtyIn, QtyOut)
        VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount,
          :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5,
          :P_ExpireDate_Int, :P_BatchNo, :p_ManuFactDate_Int, :m_QtyIn, :m_QtyOut);
    ELSE
    BEGIN
      IF (m_OldExpireDate_Int > p_ExpireDate_Int) THEN     /* 原有效期较大, 保留该日期 */
        p_ExpireDate_Int = m_OldExpireDate_Int;
      IF (m_OldManuFactDate_Int > p_ManuFactDate_Int) THEN /* 原生产日期较大, 保留该日期 */
        p_ManuFactDate_Int = m_OldManuFactDate_Int;
      UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount,
        ExpireDate_Int = :p_ExpireDate_Int, ManuFactDate_Int = :p_ManuFactDate_Int,
        QtyIn = QtyIn + :m_QtyIn, QtyOut = QtyOut + :m_QtyOut
        WHERE IntNo = :m_IntNo;
    END
  END
END
--
2003.06.15_02/UPGRADE_DETAIL
1. 调整出仓操作，使能支持近发货日;
--
2003.06.15_02/SQL1
CREATE PROCEDURE V_POS_SALEGDS
RETURNS (
  INTNO Integer,
  MAININTNO Integer,
  ACTPAY Dec(15,4),
  QTY Dec(15,4),
  PRICE Dec(15,4),
  DISCOUNT Dec(15,4),
  INPRICE Dec(15,4),
  AMOUNT Dec(15,4),
  INAMOUNT Dec(15,4),
  REMARK VARCHAR(20),
  UPDATEFLAG SmallInt,
  GOODSNO VARCHAR(20),
  GOODSUNIT CHAR(4),
  STOCKID CHAR(4),
  BANKID CHAR(8),
  MANUFACTDATE_INT SmallInt,


  EXPIREDATE_INT SmallInt,
  BATCHNO VARCHAR(20),
  CUSTOMVALUE1 VARCHAR(80),
  CUSTOMVALUE2 VARCHAR(80),
  CUSTOMVALUE3 VARCHAR(80),
  CUSTOMVALUE4 VARCHAR(80),
  CUSTOMVALUE5 VARCHAR(80)
) AS
DECLARE VARIABLE CustomIntNo1 Integer;
DECLARE VARIABLE CustomIntNo2 Integer;
DECLARE VARIABLE CustomIntNo3 Integer;
DECLARE VARIABLE CustomIntNo4 Integer;
DECLARE VARIABLE CustomIntNo5 Integer;
BEGIN
  FOR SELECT d.INTNO,
    d.MAININTNO,
    d.ACTPAY,
    d.QTY,
    d.PRICE,
    d.DISCOUNT,
    d.INPRICE,

    d.AMOUNT,
    d.INAMOUNT,
    d.REMARK,
    d.UPDATEFLAG,
    stklst.GoodsNo, u.GoodsUnit, StkName.StockId, BankName.BankId,
    d.MANUFACTDATE_INT,
    d.EXPIREDATE_INT,
    d.BATCHNO,
    d.CustomIntNo1,
    d.CustomIntNo2,
    d.CustomIntNo3,
    d.CustomIntNo4,
    d.CustomIntNo5
    FROM Salegds d, stklst, UnitList u, StkName, BankName
    WHERE stklst.IntNo = d.GoodsIntNo and StkName.IntNo = d.StockIntNo
      and u.GoodsIntNo = d.GoodsIntNo and u.UnitId = d.UnitId
      and d.BankIntNo = BankName.IntNo
    INTO :IntNo, :MainIntNo, :Actpay, :Qty, :Price, :Discount, :InPrice, :Amount, :InAmount,
      :Remark, :UpdateFlag, :GoodsNo, :GoodsUnit, :StockId, :BankId,
      :ManuFactDate_Int, :ExpireDate_Int, :BatchNo,
      :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5

    DO
    BEGIN
      IF (CustomIntNo1 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=1) AND (IntNo=:CustomIntNo1)
          INTO :CustomValue1;
      IF (CustomIntNo2 is NULL) THEN
        CustomValue1 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=2) AND (IntNo=:CustomIntNo2)
          INTO :CustomValue2;
      IF (CustomIntNo3 is NULL) THEN

        CustomValue3 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=3) AND (IntNo=:CustomIntNo3)
          INTO :CustomValue3;
      IF (CustomIntNo4 is NULL) THEN
        CustomValue4 = '';
      ELSE

        SELECT StrValue FROM CustomValues WHERE (FieldNo=4) AND (IntNo=:CustomIntNo4)
          INTO :CustomValue4;
      IF (CustomIntNo5 is NULL) THEN
        CustomValue5 = '';
      ELSE
        SELECT StrValue FROM CustomValues WHERE (FieldNo=5) AND (IntNo=:CustomIntNo5)
          INTO :CustomValue5;
      SUSPEND;
    END
END
---
CREATE PROCEDURE OUTLIST_UPDATE_PRICEUPOUT
(
  P_MAININTNO INTEGER,
  P_LISTDATE TIMESTAMP,
  P_CMPYNAME VARCHAR(12)
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_Price Dec(15,4);
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_Discount Dec(15,4);
BEGIN
  FOR SELECT GoodsIntNo, UnitId, Price, Discount FROM SALEGDS WHERE MainIntNo = :p_MainIntNo
    INTO :m_GoodsIntNo, :m_UnitId, :m_Price, :m_Discount
    DO
    BEGIN
      IF (m_UnitId > 0) THEN
      BEGIN
        SELECT Factor From UnitList WHERE (GoodsIntNo = :m_GoodsIntNo)
          AND (UnitId = :m_UnitId) INTO :m_Factor;
        m_Price = m_Price / m_Factor;
      END
      UPDATE STKLST SET PriceUpOut = :m_Price*:m_Discount,
        DateUpOut = :p_ListDate
        WHERE (IntNo = :m_GoodsIntNo) and
          ((DateUpOut <= :p_ListDate) or (DateUpOut is NULL));
    END
END
---
CREATE TRIGGER OUTLIST_AFTER_UPDATE_UPDATEACC FOR OUTLIST AFTER UPDATE POSITION 2 AS
/*执行进仓/取消进仓的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc)
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'SALEGDS', old.IntNo);
      /* 如果是出仓(且非退货), 则更新最新售价/近发货日 */
      IF ((m_Flag = 1) and (new.Flag1 <> 1)) THEN
        EXECUTE PROCEDURE OUTLIST_UPDATE_PRICEUPOUT(old.IntNo, new.ListDate, new.CmpyName);
    END
  END
END
--
2003.06.13_01/UPGRADE_DETAIL
1. 调整分布式盘点生效的Procedure,
   纠正：
   1)未使用批号时分布式盘点的计算错误;
   2)非全局盘点不能处理数量为0的货号。
--
2003.06.13_01/SQL1
CREATE PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE TIMESTAMP
) RETURNS (
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS 
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAss) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAss) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
  DO
  IF ((Qty <> 0) or (InAmount <> 0) or (RealFlag = 1)) THEN
  BEGIN
    IF (CustomIntNo1 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo2 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo3 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo4 = 0) THEN CustomIntNo1 = NULL;
    IF (CustomIntNo5 = 0) THEN CustomIntNo1 = NULL;
    IF (BatchNo = '') THEN BatchNo = NULL;
    SUSPEND;
  END
END
--
2003.06.01_01/UPGRADE_DETAIL
1. 增加PROCEDURE使分布式盘点支持批号/自定义内容;
2. 调整TypeDefine表的BeforeInsert/BeforeUpdate
   的Trigger, 使之支持数据同步;
--
2003.06.01_01/SQL1
CREATE TRIGGER TYPEDEFINE_BEF_INSERT_SET_DEF FOR TYPEDEFINE BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.LRemark is NULL) THEN new.LRemark = '';
END
---
CREATE TRIGGER TYPEDEFINE_BEF_UPDATE_SET_DEF FOR TYPEDEFINE BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.LRemark is NULL) THEN new.LRemark = '';
END
---
CREATE PROCEDURE GETSTOCKIOLISTEXB (
  P_DATEFROM DATE,
  P_DATETO DATE
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTYIN NUMERIC(15, 4),
  QTYOUT NUMERIC(15, 4),
  QTYMOVEIN NUMERIC(15, 4),
  QTYMOVEOUT NUMERIC(15, 4),
  QTYPAN NUMERIC(15, 4),
  QTYASS NUMERIC(15, 4),
  AMOUNTIN NUMERIC(15, 4),
  AMOUNTOUT NUMERIC(15, 4),
  AMOUNTMOVEIN NUMERIC(15, 4),
  AMOUNTMOVEOUT NUMERIC(15, 4),
  AMOUNTPAN NUMERIC(15, 4),
  AMOUNTASS NUMERIC(15, 4),
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS 
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  WorkMode = 2;
  FOR  /* 发货 */
    SELECT M.IntNo From OUTLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, u.Factor*d.Qty as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan, 0.0 as QtyAss,
      0.0 as AmountIn, d.InAmount as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan, 0.0 as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM SaleGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 7;
  FOR  /* 差额盘点 */
    SELECT M.IntNo From PANLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, u.Factor*d.Qty as QtyPan, 0.0 as QtyAss,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, d.InAmount as AmountPan, 0.0 as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM PanGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 12;
  FOR /* 组装与拆解 */
    SELECT M.IntNo From ASSLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan, u.Factor*d.Qty as QtyAss,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan, d.InAmount as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM ASSGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨入 */
    SELECT M.IntNo, m.StockIntNoTo From MOVELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, u.Factor*d.Qty as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan, 0.0 as QtyAss,
      0.0 as AmountIn, 0.0 as AmountOut, d.Amount as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan, 0.0 as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 3;
  FOR  /* 调拨出 */
    SELECT M.IntNo, m.StockIntNoFrom From MOVELIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo, :StockIntNo
    DO FOR SELECT d.GoodsIntNo,
      0.0 as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, u.Factor*d.Qty as QtyMoveOut, 0.0 as QtyPan, 0.0 as QtyAss,
      0.0 as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, d.Amount as AmountMoveOut, 0.0 as AmountPan, 0.0 as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM MOVEGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

  WorkMode = 1;
  FOR /* 进货 */
    SELECT M.IntNo From INLIST m WHERE (m.ListDate>=:p_DateFrom and m.ListDate<=:p_DateTo) and (m.UpdateAcc = '*')
    INTO :m_IntNo
    DO FOR SELECT d.GoodsIntNo, d.StockIntNo,
      u.Factor*d.Qty as QtyIn, 0.0 as QtyOut, 0.0 as QtyMoveIn, 0.0 as QtyMoveOut, 0.0 as QtyPan, 0.0 as QtyAss,
      d.Amount as AmountIn, 0.0 as AmountOut, 0.0 as AmountMoveIn, 0.0 as AmountMoveOut, 0.0 as AmountPan, 0.0 as AmountAss
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
      FROM BUYGDS d, UnitList u
      WHERE (d.MainIntNo=:m_IntNo) and (d.GoodsIntNo=u.GoodsIntNo) and (u.UnitId=d.UnitId)
        INTO :GoodsIntNo, :StockIntNo,
          :QtyIn, :QtyOut, :QtyMoveIn, :QtyMoveOut, :QtyPan, :QtyAss,
          :AmountIn, :AmountOut, :AmountMoveIn, :AmountMoveOut, :AmountPan, :AmountAss
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
      DO SUSPEND;

END
---
CREATE PROCEDURE GET_PAN_FROM_REAL_EXB (
  P_PANDIANNO VARCHAR(10),
  P_PANDIANDATE DATE
) RETURNS (

  GOODSINTNO INTEGER,
  STOCKINTNO INTEGER,
  QTY DOUBLE PRECISION,
  INAMOUNT DOUBLE PRECISION,
  REALFLAG INTEGER,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BATCHNO VARCHAR(20)
) AS 
BEGIN
FOR
  Select 1 as RealFlag, d.goodsIntNo, d.stockIntNo, sum(d.Qty*u.Factor) as Qty,
    Sum(d.InAmount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    From Reallist m, Realgds d, UnitList u
    Where (m.IntNo = d.MainIntNo) and (d.GoodsIntNo = u.GoodsIntNo) and (d.UnitId = u.UnitId)
    and (m.PanDianNo = :p_PanDianNo)
    group by d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(d.QtyIn-d.QtyOut+d.QtyMoveIn-d.QtyMoveOut+d.QtyPan+d.QtyAss) as Qty,
    Sum(d.AmountIn-d.AmountOut+d.AmountMoveIn-d.AmountMoveOut+d.AmountPan+d.AmountAss) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    FROM GETSTOCKIOLISTExB(:p_PanDianDate, '3000.1.1') d
    Group By d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  UNION ALL
  SELECT 0 as RealFlag, d.GoodsIntNo, d.StockIntNo,
    Sum(-d.QRemain) as Qty, Sum(-d.Amount) as InAmount
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
    FROM StklstsX d
    GROUP BY d.GoodsIntNo, d.StockIntNo
      ,d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      ,d.BatchNo
  INTO :RealFlag, :GoodsIntNo, :StockIntNo, :Qty, :InAmount
          ,:CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
          ,:BatchNo
  DO
  IF ((Qty <> 0) or (InAmount <> 0)) THEN SUSPEND;
END
--
2003.06.01_01_SYS/UPGRADE_DETAIL

1. 调整OPER表的BeforeInsert的Trigger, 使之支持数据
   同步;
--
2003.06.01_01_SYS/SQL1
CREATE TRIGGER OPER_BEFORE_INSERT_SET_DEF FOR OPER BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.OperName is NULL) THEN new.OperName = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
---
CREATE TRIGGER OPER_BEFORE_UPDATE_SET_DEF FOR OPER BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.OperName is NULL) THEN new.OperName = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
--

2003.05.30_01/UPGRADE_DETAIL
1. 调整STKLSTS/STKLSTSX的INSERT的Trigger, 使之自
   动计算平均进价;
--
2003.05.30_01/SQL1
CREATE TRIGGER STKLSTSX_BEFORE_INSERT_1 FOR STKLSTSX BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.QRemain <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (new.QINITRemain <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---
CREATE TRIGGER STKLSTS_BEFORE_INSERT_1 FOR STKLSTS BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.QRemain <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF (new.QINITRemain <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;

END
--
2003.05.29_02/UPGRADE_DETAIL
1. 调整SETUP/STKLSTSX表的BeforeInsert的Trigger, 使
   之支持数据同步;
2. 新增PROCEDURE: GetStockIOEXB
   (取仓库帐明细记录/含批号);
3. 修改商品结清的PROCEDURE, 使之支持批号的处理;
4. 对所有商品进行结清操作, 以适应新的数据库结构。
--
2003.05.29_02/SQL2
EXECUTE PROCEDURE STKLST_FORCE_SETLIMIT
--
2003.05.29_02/SQL1
CREATE TRIGGER SETUP_BEFORE_UPDATE_SET_DEF FOR SETUP BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.DataString is NULL) THEN new.DataString = '';
  IF (new.Descript is NULL) THEN new.Descript = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
---
CREATE TRIGGER SETUP_BEFORE_INSERT_SET_DEF FOR SETUP BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.DataString is NULL) THEN new.DataString = '';
  IF (new.Descript is NULL) THEN new.Descript = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
---
CREATE TRIGGER CREATE_INTNO_STKLSTSX FOR STKLSTSX BEFORE INSERT POSITION 0 AS
BEGIN

  IF ((new.IntNo is NULL) or (new.IntNo = 0)) THEN
    NEW.INTNO = GEN_ID(INTNO_STKLSTSX, 10);
END
---
CREATE PROCEDURE GETSTOCKIOEXB (

  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,
  GOODSINTNO INTEGER,
  LISTDATE DATE,
  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,
  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER,
  BatchNo Varchar(20),
  ExpireDate_Int SmallInt,
  ManuFactDate_Int SmallInt
) AS
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM BUYGDS d
      LEFT OUTER JOIN INLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM SALEGDS d
      LEFT OUTER JOIN OUTLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,

      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM PANGDS d

      LEFT OUTER JOIN PANLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM ASSGDS d

      LEFT OUTER JOIN ASSLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      , d.BatchNo, d.ExpireDate_Int, d.ManuFactDate_Int
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
      , :BatchNo, :ExpireDate_Int, :ManuFactDate_Int
  DO
    Suspend;
END
---
CREATE PROCEDURE STKLST_SET_LIMITEX (
  P_GOODSINTNO INTEGER,
  P_S_DATE TIMESTAMP
)  AS
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_QtyIn DEC(15,4);
DECLARE VARIABLE m_QtyOut DEC(15,4);
DECLARE VARIABLE m_InAmount DEC(15,4);
DECLARE VARIABLE m_Qty DEC(15,4);
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_id Integer;
DECLARE VARIABLE m_qInitRemain DEC(15,4);
DECLARE VARIABLE m_InitAmount DEC(15,4);
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) = 1) THEN EXIT;

  m_id = GEN_ID(INTNO_DataInit, 0);
  IF (m_id <> 1) THEN
  BEGIN
    m_id = GEN_ID(INTNO_DataInit, -m_id);
    m_id = GEN_ID(INTNO_DataInit, 1);
  END
  /* SET GENERATOR IntNo_DataInit to 1;  */      /* 关闭 STKLSTSX'S Trigger */

  /* ------------------------------------------------------------------------ */
  /* 开始处理 STKLSTSX 表 */
  /* 做标志 = 未处理, 因为 USED = 1 表示该记录不被使用 */
  UPDATE STKLSTSX SET USED = 1, qRemain = 0, Amount = 0,
    qInitRemain = 0, InitAmount = 0
    WHERE GoodsIntNo = :p_GoodsIntNo;

  /* 读取期末库存量 */
  FOR SELECT sio.CustomIntNo1, sio.CustomIntNo2, sio.CustomIntNo3,
    sio.CustomIntNo4, sio.CustomIntNo5,
    sio.BatchNo, Max(sio.ExpireDate_Int), Max(sio.ManuFactDate_Int), sio.StockIntNo,
    Sum(sio.QtyIn*u.Factor), Sum(sio.QtyOut*u.Factor), Sum(sio.InAmount)
    FROM GETSTOCKIOEXB(:p_GoodsIntNo) sio, UNITLIST u
    WHERE (u.GOODSINTNO = sio.GoodsIntNo) and (u.UNITID = sio.UnitId)
    GROUP BY sio.CustomIntNo1, sio.CustomIntNo2, sio.CustomIntNo3,
      sio.CustomIntNo4, sio.CustomIntNo5, sio.BatchNo, sio.StockIntNo
    INTO :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
         :m_BatchNo, :m_ExpireDate_Int, :m_ManuFactDate_Int, :m_StockIntNo,
         :m_QtyIn, :m_QtyOut, :m_InAmount

    DO
    BEGIN
      IF (m_QtyIn is Null) THEN m_QtyIn = 0;
      IF (m_QtyOut is Null) THEN m_QtyOut = 0;
      IF (m_InAmount is Null) THEN m_InAmount = 0;
      m_Qty = m_QtyIn - m_QtyOut;

      m_IntNo = NULL;
      SELECT INTNO FROM STKLSTSX
        WHERE (GoodsIntNo = :P_GoodsIntNo) and (StockIntNo = :m_StockIntNo)
          and ((CustomIntNo1 = :m_CIntNo1) or (:m_CIntNo1 is NULL and CustomIntNo1 is NULL))
          and ((CustomIntNo2 = :m_CIntNo2) or (:m_CIntNo2 is NULL and CustomIntNo2 is NULL))
          and ((CustomIntNo3 = :m_CIntNo3) or (:m_CIntNo3 is NULL and CustomIntNo3 is NULL))
          and ((CustomIntNo4 = :m_CIntNo4) or (:m_CIntNo4 is NULL and CustomIntNo4 is NULL))
          and ((CustomIntNo5 = :m_CIntNo5) or (:m_CIntNo5 is NULL and CustomIntNo5 is NULL))
          and ((BatchNo = :m_BatchNo) or (:m_BatchNo is NULL and BatchNo is NULL))
      INTO :m_IntNo;

      IF (m_IntNo IS NULL) THEN
        INSERT INTO STKLSTSX (USED, GoodsIntNo, StockIntNo, QRemain, Amount,
          CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
          ExpireDate_Int, BatchNo, ManuFactDate_Int, QtyIn, QtyOut,
          qInitRemain, InitAmount)
          VALUES (0, :P_GoodsIntNo, :m_StockIntNo, :m_Qty, :m_InAmount,
            :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
            :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int, :m_QtyIn, :m_QtyOut,
            0, 0);
      ELSE
      BEGIN
        UPDATE STKLSTSX SET USED = 0, QREMAIN = :m_Qty, Amount = :m_InAmount,
          ExpireDate_Int = :m_ExpireDate_Int, ManuFactDate_Int = :m_ManuFactDate_Int,
          QtyIn = :m_QtyIn, QtyOut = :m_QtyOut, qInitRemain = 0, InitAmount = 0
          WHERE IntNo = :m_IntNo;
      END
    END

  /* 读取期初库存量 */
  FOR SELECT sio.CustomIntNo1, sio.CustomIntNo2, sio.CustomIntNo3,
    sio.CustomIntNo4, sio.CustomIntNo5,

    sio.BatchNo, sio.StockIntNo,
    Sum(sio.QtyIn*u.Factor), Sum(sio.QtyOut*u.Factor), Sum(sio.InAmount)
    FROM GETSTOCKIOEXB(:p_GoodsIntNo) sio, UNITLIST u
    WHERE (u.GOODSINTNO = sio.GoodsIntNo) and (u.UNITID = sio.UnitId)
      AND (sio.ListDate <= :p_S_Date)

    GROUP BY sio.CustomIntNo1, sio.CustomIntNo2, sio.CustomIntNo3,
      sio.CustomIntNo4, sio.CustomIntNo5, sio.BatchNo, sio.StockIntNo
    INTO :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
         :m_BatchNo, :m_StockIntNo,
         :m_QtyIn, :m_QtyOut, :m_InAmount
    DO
    BEGIN
      IF (m_QtyIn is Null) THEN m_QtyIn = 0;
      IF (m_QtyOut is Null) THEN m_QtyOut = 0;
      IF (m_InAmount is Null) THEN m_InAmount = 0;
      m_Qty = m_QtyIn - m_QtyOut;

      m_IntNo = NULL;
      SELECT INTNO FROM STKLSTSX
        WHERE (GoodsIntNo = :P_GoodsIntNo) and (StockIntNo = :m_StockIntNo)
          and ((CustomIntNo1 = :m_CIntNo1) or (:m_CIntNo1 is NULL and CustomIntNo1 is NULL))
          and ((CustomIntNo2 = :m_CIntNo2) or (:m_CIntNo2 is NULL and CustomIntNo2 is NULL))
          and ((CustomIntNo3 = :m_CIntNo3) or (:m_CIntNo3 is NULL and CustomIntNo3 is NULL))
          and ((CustomIntNo4 = :m_CIntNo4) or (:m_CIntNo4 is NULL and CustomIntNo4 is NULL))
          and ((CustomIntNo5 = :m_CIntNo5) or (:m_CIntNo5 is NULL and CustomIntNo5 is NULL))
          and ((BatchNo = :m_BatchNo) or (:m_BatchNo is NULL and BatchNo is NULL))
      INTO :m_IntNo;

      UPDATE STKLSTSX SET qInitRemain = :m_Qty, InitAmount = :m_InAmount
          WHERE IntNo = :m_IntNo;
    END
  /* 删除所有 USED = 1 的记录 */
  DELETE FROM STKLSTSX WHERE (USED = 1) AND (GoodsIntNo = :p_GoodsIntNo);
  /* STKLSTSX 表 处理完毕 */
  /* ------------------------------------------------------------------------ */

  /* ------------------------------------------------------------------------ */
  /* 开始处理 STKLSTS 表 */
  /* 做标志 = 未处理, 因为 USED = 1 表示该记录不被使用 */
  UPDATE STKLSTS SET USED = 1, qRemain = 0, Amount = 0,
    qInitRemain = 0, InitAmount = 0

    WHERE GoodsIntNo = :p_GoodsIntNo;

  FOR
    SELECT Sum(qRemain), Sum(Amount), Sum(qInitRemain), Sum(InitAmount), SUM(QtyIn), SUM(QtyOut),
    StockIntNo
    FROM STKLSTSX
    WHERE (GoodsIntNo = :p_GoodsIntNo)
    GROUP BY StockIntNo
    INTO :m_Qty, :m_InAmount, :m_qInitRemain, :m_InitAmount, :m_QtyIn, :m_QtyOut, :m_StockIntNo
    DO
    BEGIN
      m_IntNo = NULL;
      IF (m_Qty is Null) THEN m_Qty = 0;
      IF (m_InAmount is Null) THEN m_InAmount = 0;
      IF (m_qInitRemain is Null) THEN m_qInitRemain = 0;
      IF (m_InitAmount is Null) THEN m_InitAmount = 0;
      IF (m_QtyIn is Null) THEN m_QtyIn = 0;
      IF (m_QtyOut is Null) THEN m_QtyOut = 0;
      SELECT IntNo FROM Stklsts
        WHERE (GoodsIntNo = :p_GoodsIntNo) AND (StockIntNo = :m_StockIntNo)
        INTO :m_IntNo;
      IF (m_IntNo IS NULL) THEN
        INSERT INTO Stklsts (USED, GoodsIntNo, StockIntNo,
          qRemain, Amount, qInitRemain, InitAmount, QtyIn, QtyOut)
          VALUES (0, :p_GoodsIntNo, :m_StockIntNo,
          :m_Qty, :m_InAmount, :m_qInitRemain, :m_InitAmount, :m_QtyIn, :m_QtyOut);
      ELSE
        UPDATE Stklsts SET USED = 0, qRemain = :m_Qty, Amount = :m_InAmount,
          qInitRemain = :m_qInitRemain, InitAmount = :m_InitAmount,
          QtyIn = :m_QtyIn, QtyOut = :m_QtyOut
          WHERE IntNo = :m_IntNo;
    END

  /* 删除所有 USED = 1 的记录 */
  DELETE FROM STKLSTS WHERE (USED = 1) AND (GoodsIntNo = :p_GoodsIntNo);
  /* STKLSTS 表 处理完毕 */
  /* ------------------------------------------------------------------------ */

  /* ------------------------------------------------------------------------ */
  /* 开始处理 STKLST 表 */
    SELECT Sum(qRemain), Sum(Amount), Sum(qInitRemain), Sum(InitAmount)
    FROM STKLSTS
    WHERE (GoodsIntNo = :p_GoodsIntNo)
    INTO :m_Qty, :m_InAmount, :m_qInitRemain, :m_InitAmount;
    BEGIN
      IF (m_Qty is Null) THEN m_Qty = 0;
      IF (m_InAmount is Null) THEN m_InAmount = 0;
      IF (m_qInitRemain is Null) THEN m_qInitRemain = 0;
      IF (m_InitAmount is Null) THEN m_InitAmount = 0;
      UPDATE Stklst SET qRemain = :m_Qty, Amount = :m_InAmount,
          qInitRemain = :m_qInitRemain, InitAmount = :m_InitAmount
          WHERE IntNo = :p_GoodsIntNo;
    END
  /* STKLST 表 处理完毕 */
  /* ------------------------------------------------------------------------ */

  m_id = GEN_ID(INTNO_DataInit, 0);
  IF (m_id <> 0) THEN
    m_id = GEN_ID(INTNO_DataInit, -m_id);
  /* SET GENERATOR IntNo_DataInit to 0;  */      /* 开启 STKLSTSX'S Trigger */
END
---
CREATE TRIGGER STKLST_AFTER_UPDATE_SETLIMIT FOR STKLST AFTER UPDATE POSITION 0 AS
/* 执行结帐操作 */
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  IF (old.S_DATE <> new.S_Date) THEN
    EXECUTE PROCEDURE STKLST_SET_LIMITEX (old.IntNo, new.S_Date);
END
---
CREATE PROCEDURE STKLST_FORCE_SETLIMIT
AS
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_S_Date TimeStamp;
BEGIN
  FOR SELECT IntNo, S_Date FROM Stklst INTO :m_IntNo, :m_S_Date DO
    EXECUTE PROCEDURE STKLST_SET_LIMITEX (m_IntNo, m_S_Date);
END
--

2003.05.29_01/UPGRADE_DETAIL
1. 调整分仓库库存清单记录生成的机制:
   原来: 新增商品或仓库代号时自动生成;
   现在: 只有该商品+仓库代号的记录被修改时,
         才生成分仓库记录;
--
2003.05.29_01/SQL0
ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS
BEGIN
  SUSPEND;
END
---
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
BEGIN
  SUSPEND;
END
--
2003.05.29_01/SQL1
CREATE TRIGGER CREATE_INTNO_STKLSTS FOR STKLSTS 
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((new.IntNo is NULL) OR (new.IntNo = 0)) THEN
  BEGIN
    NEW.INTNO = GEN_ID(INTNO_STKLSTS, 10);
    /*
    EXECUTE PROCEDURE GETNEWINTNO ('STKLSTS') RETURNING_VALUES :m_IntNo;
    new.IntNo = m_IntNo;
    */
  END
END
---
CREATE TRIGGER STKNAME_AFTER_INSERT_EVENT FOR STKNAME 
ACTIVE AFTER INSERT POSITION 0
AS
BEGIN

  EXECUTE PROCEDURE SEND_MSG_STKNAME_CHANGE;
END

---
CREATE TRIGGER STKLSTS_AFTER_INSERT_1 FOR STKLSTS
ACTIVE AFTER INSERT POSITION 0
AS 

BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    UPDATE STKLST SET QRemain = QRemain + new.QRemain,
      Amount = AMOUNT + new.Amount,
      QInitRemain = QInitRemain + new.QInitRemain,
      InitAmount = InitAMOUNT + new.InitAmount
      WHERE INTNO = new.GoodsIntNo;
  END
END
---
CREATE TRIGGER STKLSTS_AFTER_UPDATE_1 FOR STKLSTS
ACTIVE AFTER UPDATE POSITION 0
AS 
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ((new.QRemain <> old.QRemain) or (new.AMOUNT <> old.Amount)) THEN
      UPDATE STKLST SET QRemain = QRemain + new.QRemain - old.QRemain, 
        Amount = AMOUNT + new.Amount - old.Amount
        WHERE INTNO = old.GoodsIntNo;
    IF ((new.QInitRemain <> old.QInitRemain) or (new.InitAMOUNT <> old.InitAmount)) THEN
      UPDATE STKLST SET QInitRemain = QInitRemain + new.QInitRemain - old.QInitRemain,
        InitAmount = InitAMOUNT + new.InitAmount - old.InitAmount
        WHERE INTNO = old.GoodsIntNo;
  END
END
---
CREATE TRIGGER STKLSTSX_AFTER_INSERT_1 FOR STKLSTSX
ACTIVE AFTER INSERT POSITION 0
AS 
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
  BEGIN
    m_IntNo = NULL;
    SELECT IntNo FROM STKLSTS WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo)
      INTO :m_IntNo;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTS (GoodsIntNo, StockIntNo, QRemain, Amount, 
        qInitRemain, InitAmount, QtyIn, QtyOut)
        VALUES (new.GoodsIntNo, new.StockIntNo, new.qRemain, new.Amount, 
        new.qInitRemain, new.InitAmount, new.QtyIn, new.QtyOut);
    ELSE
      UPDATE STKLSTS SET QRemain = QRemain + new.QRemain, Amount = AMOUNT + new.Amount,
        qInitRemain = qInitRemain + new.qInitRemain, InitAmount = InitAmount + new.InitAmount,
        QtyOut = QtyOut + new.QtyOut, QtyIn = QtyIn + new.QtyIn
        WHERE (IntNo = :m_IntNo);
  END
END
---
CREATE TRIGGER STKLSTSX_AFTER_UPDATE_1 FOR STKLSTSX
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
  BEGIN

    m_IntNo = NULL;
    SELECT IntNo FROM STKLSTS WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo)
      INTO :m_IntNo;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTS (GoodsIntNo, StockIntNo, QRemain, Amount,
        qInitRemain, InitAmount, QtyIn, QtyOut)
        VALUES (new.GoodsIntNo, new.StockIntNo, new.qRemain, new.Amount,
        new.qInitRemain, new.InitAmount, new.QtyIn, new.QtyOut);
    ELSE
    BEGIN
      IF ((new.QRemain <> old.QRemain) or (new.AMOUNT <> old.Amount)) THEN
      BEGIN
        UPDATE STKLSTS SET QRemain = QRemain + new.QRemain - old.QRemain,
          Amount = AMOUNT + new.Amount - old.Amount
          WHERE (IntNo = :m_IntNo);
      END
      IF ((new.QInitRemain <> old.QInitRemain) or (new.InitAMOUNT <> old.InitAmount)) THEN
      BEGIN
        UPDATE STKLSTS SET QInitRemain = QInitRemain + new.QInitRemain - old.QInitRemain,
          InitAmount = InitAMOUNT + new.InitAmount - old.InitAmount
          WHERE (IntNo = :m_IntNo);
      END
      IF ((new.QtyIn <> old.QtyIn) or (new.QtyOut <> old.QtyOut)) THEN
      BEGIN
        UPDATE STKLSTS SET QtyIn = QtyIn + new.QtyIn - old.QtyIn,
          QtyOut = QtyOut + new.QtyOut - old.QtyOut
          WHERE (IntNo = :m_IntNo);
      END
    END
  END
END
---
CREATE PROCEDURE STOCK_UPDATE_ITEMX_D (
  P_DIRECT SMALLINT,
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,

  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER,
  P_EXPIREDATE_INT SMALLINT,
  P_BATCHNO VARCHAR(20),
  P_MANUFACTDATE_INT SMALLINT
)  AS 
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
DECLARE VARIABLE m_OldExpireDate_Int SmallInt;
DECLARE VARIABLE m_OldManuFactDate_Int SmallInt;
DECLARE VARIABLE m_QtyIn Dec(15,4);
DECLARE VARIABLE m_QtyOut Dec(15,4);
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END

  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END

  m_QtyIn = 0; m_QtyOut = 0;
  IF (P_Direct > 0) THEN                         /* 属于正方向的操作 */
    m_QtyIn = p_Qty;
  ELSE
    m_QtyOut = -p_Qty;

  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  IF (p_BatchNo = '') THEN p_BatchNo = NULL;
  m_IntNo = NULL;

  BEGIN
    m_OldExpireDate_Int = 0;
    m_OldManuFactDate_Int = 0;
    SELECT INTNO, ExpireDate_Int, ManuFactDate_Int FROM STKLSTSX
      WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)
        and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
        and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
        and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
        and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
        and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
        and ((BatchNo = :p_BatchNo) or (:p_BatchNo is NULL and BatchNo is NULL))
    INTO :m_IntNo, :m_OldExpireDate_Int, m_OldManuFactDate_Int;
    IF (m_IntNo is NULL) THEN
      INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int, QtyIn, QtyOut)

        VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount,
          :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5,
          :P_ExpireDate_Int, :P_BatchNo, :p_ManuFactDate_Int, :m_QtyIn, :m_QtyOut);
    ELSE
    BEGIN
      IF (m_OldExpireDate_Int > p_ExpireDate_Int) THEN     /* 原有效期较大, 保留该日期 */
        p_ExpireDate_Int = m_OldExpireDate_Int;
      IF (m_OldManuFactDate_Int > p_ManuFactDate_Int) THEN /* 原生产日期较大, 保留该日期 */
        p_ManuFactDate_Int = m_OldManuFactDate_Int;
      UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount,
        ExpireDate_Int = :p_ExpireDate_Int, ManuFactDate_Int = :p_ManuFactDate_Int,
        QtyIn = QtyIn + :m_QtyIn, QtyOut = QtyOut + :m_QtyOut
        WHERE IntNo = :m_IntNo;
    END
  END

END
---
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);

DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
DECLARE VARIABLE m_Direct SmallInt;
BEGIN
  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    m_Direct = -1;
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      FROM PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,

            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    m_Direct = 1;
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
      ExpireDate_Int, BatchNo, ManuFactDate_Int
      From ASSGDS
      WHERE MainIntNo = :p_MainIntNo

      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
           :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(m_Direct, p_AddFlag,
            m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
            m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
      END
  END
END
---
ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_AddFlag SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
DECLARE VARIABLE m_ExpireDate_Int SmallInt;
DECLARE VARIABLE m_BatchNo Varchar(20);
DECLARE VARIABLE m_ManuFactDate_Int SmallInt;
BEGIN

  m_AddFlag = -p_AddFlag;
  FOR SELECT GoodsIntNo, Qty, Amount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5,
        ExpireDate_Int, BatchNo, ManuFactDate_Int
         From MOVEGDS
    WHERE MainIntNo = :p_MainIntNo
    INTO :m_GoodsIntNo, :m_qty, :m_Amount, :m_UnitId,
         :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5,
         :m_ExpireDate_Int, :m_BatchNo, :m_ManuFactDate_Int
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(1, p_AddFlag, m_GoodsIntNo, p_StockIntNoTo, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,
          m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX_D(-1, m_AddFlag, m_GoodsIntNo, p_StockIntNoFrom, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5,

          m_ExpireDate_Int, m_BatchNo, m_ManuFactDate_Int);
      END
END
--
2003.05.22_03/UPGRADE_DETAIL
1. 调整厂家的显示长度;
--
2003.05.22_03/SQL1
DELETE From DomainDefine Where (Upper(DomainName) = Upper('TLVendor'))
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth)
  VALUES ('TLVendor', '厂家', 'Vendor')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('Vendor'))
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)

  VALUES ('', 'Vendor', 'TLVendor')
---
DELETE From DomainDefine Where (Upper(DomainName) = Upper('TAuthorizeNo'))
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth)
  VALUES ('TAuthorizeNo', '批准文号', 'AuthorizeNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('AuthorizeNo'))
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'AuthorizeNo', 'TAuthorizeNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('ValidateDays'))
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, DWidth)

  VALUES ('', 'ValidateDays', '有效期(天)', 6)
--
2003.05.22_02/UPGRADE_DETAIL
1. 调整库存总表结构:
   厂家: 由 12 位升级到 40 位;
   规格: 由 12 位升级到 20 位;
--
2003.05.22_02/SQL1
ALTER TABLE STKLST ADD GoodsSpec VARCHAR(20)
---
ALTER TABLE STKLST ADD Vendor VARCHAR(40)
---
UPDATE STKLST SET GoodsSpec = GoodsSpec2
---
UPDATE STKLST SET Vendor = Vendor2
---
ALTER TABLE STKLST DROP GoodsSpec2
---
ALTER TABLE STKLST DROP Vendor2
---
CREATE TRIGGER STKLST_BEFORE_INSERT_SET_DEF FOR STKLST
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (new.Unit0 is NULL) THEN new.Unit0 = 0;
  IF (new.Unit1 is NULL) THEN new.Unit1 = 0;
  IF (new.GoodsSpec is NULL) THEN new.GoodsSpec = '';
  IF (new.Vendor is NULL) THEN new.Vendor = '';

  IF (new.CompanyUPIn is NULL) THEN new.CompanyUPIn = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.AltGoodsNo is NULL) THEN new.AltGoodsNo = '';
  IF (new.PhotoFileName is NULL) THEN new.PhotoFileName = '';
  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.GdsTypeNo is NULL) THEN new.GdsTypeNo = '';
END

---
CREATE TRIGGER STKLST_BEFORE_UPDATE_SET_DEF FOR STKLST 
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF (new.Unit0 is NULL) THEN new.Unit0 = 0;
  IF (new.Unit1 is NULL) THEN new.Unit1 = 0;
  IF (new.GoodsSpec is NULL) THEN new.GoodsSpec = '';
  IF (new.Vendor is NULL) THEN new.Vendor = '';
  IF (new.CompanyUPIn is NULL) THEN new.CompanyUPIn = '';
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.AltGoodsNo is NULL) THEN new.AltGoodsNo = '';
  IF (new.PhotoFileName is NULL) THEN new.PhotoFileName = '';
  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.GdsTypeNo is NULL) THEN new.GdsTypeNo = '';
END
---
DECLARE EXTERNAL FUNCTION ZS_GetUnitXchgStr
   CSTRING(8), CSTRING(8), DOUBLE PRECISION, DOUBLE PRECISION
   RETURNS CSTRING(26)
  ENTRY_POINT "zsudf_GetUnitXchgStr"  MODULE_NAME "zsudf"
--

2003.05.22_01/UPGRADE_DETAIL
1. 修改数据库结构, 所有的有关GoodsIntNo的表
   增加字段: 生产日期/有效期/生产批号;
2. 调整表: StklstsX 用于记录不同批号的商品的
   库存量;
--
2003.05.22_01/SQL1
DELETE From DomainDefine Where (Upper(DomainName) = Upper('TManufactDate'))
---
DELETE From DomainDefine Where (Upper(DomainName) = Upper('TExpireDate'))
---
DELETE From DomainDefine Where (Upper(DomainName) = Upper('TBatchNo'))
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth)
  VALUES ('TBatchNo', '批号', 'BatchNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('ManufactDate'))
---
Insert INTO FieldDefine (TableName, FieldName, DomainName, DLabel, DWidth, EMask)
  VALUES ('', 'ManufactDate', 'TDate', '生产日期', 10, '!0000.99.99;1;')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('ExpireDate'))
---
Insert INTO FieldDefine (TableName, FieldName, DomainName, DLabel, DWidth, EMask)
  VALUES ('', 'ExpireDate', 'TDate', '有效期至', 10, '!0000.99.99;1;')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('BatchNo'))
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'BatchNo', 'TBatchNo')
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('ManufactDate_INT'))
---
Insert INTO FieldDefine (TableName, FieldName, Hidden)
  VALUES ('', 'ManufactDate_INT', 1)
---
DELETE From FieldDefine Where (Upper(FieldName) = Upper('ExpireDate_INT'))
---
Insert INTO FieldDefine (TableName, FieldName, Hidden)

  VALUES ('', 'ExpireDate_INT', 1)
---
DECLARE EXTERNAL FUNCTION ZS_SmallInt2Date
   Integer
   RETURNS DATE
  ENTRY_POINT "zsudf_SmallInt2Date"  MODULE_NAME "zsudf"
---
DECLARE EXTERNAL FUNCTION ZS_DATE2SMALLINT
   DATE
   RETURNS INTEGER BY VALUE
  ENTRY_POINT "zsudf_Date2SmallInt"  MODULE_NAME "zsudf"
---
CREATE DOMAIN TSmallIntDate AS SMALLINT
---
CREATE DOMAIN TBatchNo VARCHAR(20)
---
CREATE DOMAIN TBatchNo_NOTNULL VARCHAR(20) DEFAULT "" NOT NULL
---
CREATE DOMAIN TAuthorizeNo VARCHAR(30)
---

ALTER TABLE STKLST DROP XchgStr
---
ALTER TABLE STKLST DROP DATEPRODUCE
---
ALTER TABLE STKLST DROP DateLimit
---
ALTER TABLE STKLST DROP QTYIN
---
ALTER TABLE STKLST DROP QTYOUT
---
ALTER TABLE STKLST DROP DATEUPIN2
---
ALTER TABLE STKLST DROP DATEUPIN3
---
ALTER TABLE STKLST DROP PRICEUPIN2
---
ALTER TABLE STKLST DROP PRICEUPIN3
---
ALTER TABLE STKLST DROP COMPANYUPIN2
---
ALTER TABLE STKLST DROP COMPANYUPIN3
---
ALTER TABLE STKLST ADD PriceNotDiscount SmallInt DEFAULT 0
---
ALTER TABLE STKLST ADD AuthorizeNo TAuthorizeNo
---
ALTER TABLE STKLST ADD ValidateDays SmallInt
---
ALTER TABLE STKLST ADD GoodsSpec2 VARCHAR(20)
---
ALTER TABLE STKLST ADD Vendor2 VARCHAR(40)
---
UPDATE STKLST SET GoodsSpec2 = GoodsSpec
---
UPDATE STKLST SET Vendor2 = Vendor
---
ALTER TABLE STKLST DROP GoodsSpec
---
ALTER TABLE STKLST DROP Vendor
---

ALTER TABLE STKLSTS Add QTYIN DEC(15,4)
---
ALTER TABLE STKLSTS Add QTYOUT DEC(15,4)
---


ALTER TABLE STKLSTSX Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE STKLSTSX Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE STKLSTSX Add BATCHNO TBATCHNO
---
ALTER TABLE STKLSTSX Add QTYIN DEC(15,4)
---
ALTER TABLE STKLSTSX Add QTYOUT DEC(15,4)
---

ALTER TABLE Assgds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Assgds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Assgds Add BATCHNO TBATCHNO
---
ALTER TABLE Assgds DROP BATCHID
---

ALTER TABLE Buygds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Buygds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Buygds Add BATCHNO TBATCHNO
---
ALTER TABLE Buygds DROP BATCHID
---

ALTER TABLE DSALEGDS DROP BATCHID
---
ALTER TABLE SALEGDST DROP BATCHID
---
ALTER TABLE BUYGDST DROP BATCHID
---
ALTER TABLE FormulaDetail Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE FormulaDetail Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE FormulaDetail Add BATCHNO TBATCHNO
---

ALTER TABLE Movegds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Movegds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Movegds Add BATCHNO TBATCHNO
---
ALTER TABLE Movegds DROP BATCHID
---

ALTER TABLE Pangds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Pangds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Pangds Add BATCHNO TBATCHNO
---
ALTER TABLE Pangds DROP BATCHID
---

ALTER TABLE Pricelst Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Pricelst Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Pricelst Add BATCHNO TBATCHNO
---

ALTER TABLE Realgds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Realgds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Realgds Add BATCHNO TBATCHNO
---
ALTER TABLE Realgds DROP BATCHID
---

ALTER TABLE Salegds Add ManufactDate_INT TSmallIntDate
---
ALTER TABLE Salegds Add ExpireDate_INT TSmallIntDate
---
ALTER TABLE Salegds Add BATCHNO TBATCHNO
---
ALTER TABLE Salegds DROP BATCHID
---

CREATE VIEW VIEW_POS_SALEGDS (
  INTNO,
  MAININTNO,
  ACTPAY,
  QTY,
  PRICE,
  DISCOUNT,
  INPRICE,
  AMOUNT,
  INAMOUNT,
  REMARK,
  UPDATEFLAG,
  GOODSNO,
  GOODSUNIT,


  STOCKID,
  BANKID,
  MANUFACTDATE_INT,
  EXPIREDATE_INT,
  BATCHNO,
  CustomValue1, CustomValue2, CustomValue3, CustomValue4, CustomValue5
) AS

Select
  d.INTNO,
  d.MAININTNO,
  d.ACTPAY,
  d.QTY,
  d.PRICE,
  d.DISCOUNT,
  d.INPRICE,
  d.AMOUNT,
  d.INAMOUNT,
  d.REMARK,
  d.UPDATEFLAG,
  stklst.GoodsNo, u.GoodsUnit, StkName.StockId, BankName.BankId,
  d.MANUFACTDATE_INT,
  d.EXPIREDATE_INT,
  d.BATCHNO,
  cv1.StrValue as CustomValue1, cv2.StrValue as CustomValue2,
  cv3.StrValue as CustomValue3, cv4.StrValue as CustomValue4,
  cv5.StrValue as CustomValue5
  From Salegds d, Stklst, UnitList u, StkName, BankName

  LEFT OUTER JOIN CustomValues cv1 On (cv1.IntNo=d.CustomIntNo1)
  LEFT OUTER JOIN CustomValues cv2 On (cv2.IntNo=d.CustomIntNo2)
  LEFT OUTER JOIN CustomValues cv3 On (cv3.IntNo=d.CustomIntNo3)
  LEFT OUTER JOIN CustomValues cv4 On (cv4.IntNo=d.CustomIntNo4)
  LEFT OUTER JOIN CustomValues cv5 On (cv5.IntNo=d.CustomIntNo5)
  Where stklst.IntNo = d.GoodsIntNo and StkName.IntNo = d.StockIntNo

    and u.GoodsIntNo = d.GoodsIntNo and u.UnitId = d.UnitId
    and d.BankIntNo = BankName.IntNo
--
2003.05.24_02_SYS/UPGRADE_DETAIL
1. 调整OPER表的BeforeInsert的Trigger, 使之支持数据
   同步;
--
2003.05.24_02_SYS/SQL1
CREATE TRIGGER OPER_BEFORE_INSERT_SET_DEF FOR OPER BEFORE INSERT POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.OperName is NULL) THEN new.OperName = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';

END
---
CREATE TRIGGER OPER_BEFORE_UPDATE_SET_DEF FOR OPER BEFORE UPDATE POSITION 0 AS
BEGIN
  IF (new.Remark is NULL) THEN new.Remark = '';
  IF (new.OperName is NULL) THEN new.OperName = '';
  IF (new.UserGroup is NULL) THEN new.UserGroup = '';
END
--
2003.05.15_03/UPGRADE_DETAIL
1. 修改取单位转换的FUNCITON, 使之支持较长的位数;
2. 增加类别全名/完整代号的长度检查等;
   类别代码全长不能超过20位;
   类别全名长度不能超过80位(含分隔符\);
3. 定义类别名称/类别全名的显示方式;

--
2003.05.15_03/SQL1
DELETE From DomainDefine Where (Upper(DomainName) = 'TGOODSTYPEFULLNAME')
---
DELETE From DomainDefine Where (Upper(DomainName) = 'TGOODSFULLTYPENAME')
---
DELETE From DomainDefine Where (Upper(DomainName) = 'TGDSFULLTYPENAME')
---
DELETE From DomainDefine Where (Upper(DomainName) = 'TGDSTYPEFULLNAME')
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth, Hidden)
  VALUES ('TGdsFullTypeName', '类别全称', 'GdsFullTypeName', 0)
---
DELETE From DomainDefine Where (Upper(DomainName) = 'TGOODSTYPENAME')
---

DELETE From DomainDefine Where (Upper(DomainName) = 'TGDSTYPENAME')
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth, Hidden)
  VALUES ('TGdsTypeName', '子类别名', 'GdsTypeName', 0)
---
DELETE From FieldDefine Where (Upper(FieldName) = 'GDSFULLTYPENAME')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'GDSTYPEFULLNAME')
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'GdsFullTypeName', 'TGDSFULLTYPENAME')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'GDSTYPENAME')
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'GdsTypeName', 'TGDSTYPENAME')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'GDSTYPELEVEL')

---
Insert INTO FieldDefine (TableName, FieldName, DLabel)
  VALUES ('', 'GdsTypeLevel', '级别')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'SUMFACTOR')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, Hidden)
  VALUES ('', 'SumFactor', '统计系数', 1)
---
DELETE From FieldDefine Where (Upper(FieldName) = 'GRIDCOLOR')
---
Insert INTO FieldDefine (TableName, FieldName, DLabel, Hidden)
  VALUES ('', 'GridColor', '颜色', 1)
---
DECLARE EXTERNAL FUNCTION ZS_GETALTUNIT1
   CSTRING(8), CSTRING(8), INTEGER, DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, CSTRING(60)
   RETURNS CSTRING(255)
  ENTRY_POINT "zsudf_GetAltUnit1"  MODULE_NAME "zsudf"
---

CREATE EXCEPTION
  MSG_TYPENAME_TOO_LONG "类别全名不能超过80个字符(含分隔符\)"
---
CREATE EXCEPTION
  MSG_TYPENO_TOO_LONG "类别完整代号不能超过20个字符"
---
CREATE PROCEDURE CHECK_FULL_TYPENO (
  p_MasterIntNo Integer,
  p_TypeNo VARCHAR(20),
  p_CreateException SmallInt
) RETURNS (
  r_FullTypeNoLength Integer
) AS
DECLARE VARIABLE m_MasterFullTypeNo VARCHAR(20);
DECLARE VARIABLE m_MasterTypeName VARCHAR(20);
DECLARE VARIABLE m_MasterFullTypeName VARCHAR(200);
BEGIN
  EXECUTE PROCEDURE GET_FULL_TYPEINFO_BY_INTNO (:p_MasterIntNo)
    RETURNING_VALUES (:m_MasterFullTypeNo, :m_MasterTypeName, :m_MasterFullTypeName);
  r_FullTypeNoLength = LEN(ALLTRIM(p_TypeNo))+LEN(ALLTRIM(m_MasterFullTypeNo));
  IF ((r_FullTypeNoLength > 20) AND (p_CreateException = 1)) THEN
    EXCEPTION MSG_TYPENO_TOO_LONG;
END;
---
CREATE PROCEDURE CHECK_FULL_TYPENAME (
  p_MasterIntNo Integer,
  p_TypeName VARCHAR(20),
  p_CreateException SmallInt
) RETURNS (
  r_FullTypeNameLength Integer
) AS
DECLARE VARIABLE m_MasterFullTypeNo VARCHAR(20);
DECLARE VARIABLE m_MasterTypeName VARCHAR(20);
DECLARE VARIABLE m_MasterFullTypeName VARCHAR(200);
BEGIN

  EXECUTE PROCEDURE GET_FULL_TYPEINFO_BY_INTNO (:p_MasterIntNo)
    RETURNING_VALUES (:m_MasterFullTypeNo, :m_MasterTypeName, :m_MasterFullTypeName);
  /* 类别名称之间有分隔符, 长度应加1 */
  r_FullTypeNameLength = LEN(ALLTRIM(p_TypeName))+LEN(ALLTRIM(m_MasterFullTypeName))+1;
  IF ((r_FullTypeNameLength > 80) AND (p_CreateException = 1)) THEN
    EXCEPTION MSG_TYPENAME_TOO_LONG;
END;
---
CREATE TRIGGER TypeDefine_BEF_INSERT_CHECK FOR TypeDefine
  ACTIVE BEFORE INSERT POSITION 10
AS
DECLARE VARIABLE m_Length Integer;
BEGIN
  EXECUTE PROCEDURE CHECK_FULL_TYPENO(new.MasterIntNo, new.TypeNo, 1)
    RETURNING_VALUES :m_Length;
  EXECUTE PROCEDURE CHECK_FULL_TYPENAME(new.MasterIntNo, new.TypeName, 1)
    RETURNING_VALUES :m_Length;
END
---
CREATE TRIGGER TypeDefine_BEF_UPDATE_CHECK FOR TypeDefine
  ACTIVE BEFORE UPDATE POSITION 10
AS
DECLARE VARIABLE m_Length Integer;
BEGIN
  EXECUTE PROCEDURE CHECK_FULL_TYPENO(new.MasterIntNo, new.TypeNo, 1)
    RETURNING_VALUES :m_Length;
  EXECUTE PROCEDURE CHECK_FULL_TYPENAME(new.MasterIntNo, new.TypeName, 1)
    RETURNING_VALUES :m_Length;
END
---
CREATE TRIGGER TYPEDEFINE_AFTER_DEL_EVENT_LOG FOR TYPEDEFINE AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('TYPEDEFINE', old.IntNo);
END
---
CREATE TRIGGER TYPELEVEL_AFTER_DEL_EVENT_LOG FOR TYPELEVEL AFTER DELETE POSITION 10 AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('TYPELEVEL', old.IntNo);
END
--
2003.05.15_02/UPGRADE_DETAIL
1. 重新定义TypeDefine的索引;
--
2003.05.15_02/SQL1
ALTER TABLE TypeDefine ADD CONSTRAINT TypeDefine_UNIQUE_1
  UNIQUE (MasterIntNo, TypeNo)
--
2003.05.15_01/UPGRADE_DETAIL
1. 修改分类表的数据结构;
2. 增加分类表相关的PROCEDURE;
--
2003.05.15_01/SQL1
ALTER TABLE TypeDefine ADD MasterIntNO_Copy INTEGER
---
UPDATE TypeDefine SET MasterIntNo_Copy = MasterIntNo
---
UPDATE TypeDefine SET MasterIntNo_Copy = 0 WHERE MasterIntNo_Copy IS NULL
---
ALTER TABLE TypeDefine DROP MasterIntNO
---
ALTER TABLE TypeDefine ADD MasterIntno INTEGER NOT NULL
---
UPDATE TypeDefine SET MasterIntNo = MasterIntNo_Copy
---
ALTER TABLE TypeDefine DROP MasterIntNo_Copy
---
CREATE PROCEDURE GET_FULL_TYPEINFO_BY_INTNO (
  P_INTNO INTEGER
) RETURNS (
  FULLTYPENO VARCHAR(20),
  TYPENAME VARCHAR(20),
  FULLTYPENAME VARCHAR(80)
) AS
DECLARE VARIABLE m_MasterIntNo INTEGER;
DECLARE VARIABLE m_MasterTypeNo Char(20);
DECLARE VARIABLE m_MasterTypeName Char(80);
BEGIN
  m_MasterIntNo = 0;
  m_MasterTypeNo = '';
  m_MasterTypeName = '';
  FullTypeNo = '';
  FullTypeName = '';
  FOR SELECT MasterIntNo, TypeNo, TypeName FROM TypeDefine WHERE IntNo = :p_IntNo
    INTO :m_MasterIntNo, :FullTypeNo, :TypeName DO
  BEGIN
    FullTypeName = TypeName;
    IF (m_MasterIntNo <> 0) THEN
    BEGIN
      EXECUTE PROCEDURE Get_Full_TypeInfo_By_IntNo(:m_MasterIntNo)
        RETURNING_VALUES :m_MasterTypeNo, :m_MasterTypeName, :FullTypeName;
      FullTypeNo = ALLTRIM(m_MasterTypeNo) || ALLTRIM(FullTypeNo);
      FullTypeName = ALLTRIM(FullTypeName) || '\' || ALLTRIM(TypeName);
    END

  END
  SUSPEND;
END
---
CREATE PROCEDURE GET_FULL_TYPEINFO RETURNS (
  INTNO INTEGER,
  FULLTYPENO VARCHAR(20),
  TYPENAME VARCHAR(20),
  FULLTYPENAME VARCHAR(80)
) AS
BEGIN
  FOR SELECT IntNo, TypeNo, TypeName FROM TypeDefine

    INTO :IntNo, :FullTypeNo, :TypeName DO
  BEGIN
    FullTypeName = TypeName;
    IF (IntNo <> 0) THEN
      EXECUTE PROCEDURE Get_Full_TypeInfo_By_IntNo(:IntNo)
        RETURNING_VALUES :FULLTYPENO, :TypeName, :FullTypeName;
    SUSPEND;
  END
END
---
--
2003.05.12_01/UPGRADE_DETAIL
1. 新增商品多级分类的功能;
--
2003.05.12_01/SQL1
CREATE TABLE TypeLevel
(
  INTNO	TINTNO,
  TypeLevel	SmallInt NOT NULL,
  TypeLength	SmallInt,
  LRemark	TLRemark,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
CONSTRAINT TypeLevel_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT TypeLevel_UNIQUE_1 UNIQUE (TypeLevel)
);
---
CREATE GENERATOR INTNO_TypeLevel
---
CREATE TRIGGER TypeLevel_BEF_INSERT_EVENTLOG FOR TypeLevel
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();

END
---
CREATE TRIGGER TypeLevel_BEF_UPDATE_EVENTLOG FOR TypeLevel
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TABLE TypeDefine
(
  INTNO	TINTNO,
  TypeLevel	SmallInt NOT NULL,
  TypeNo	Char(20) NOT NULL,
  TypeName	Char(20),
  LRemark	TLRemark,
  MasterIntNo	Integer,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
CONSTRAINT TypeDefine_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT TypeDefine_UNIQUE_1 UNIQUE (TypeLevel,TypeNo)
);
---
CREATE GENERATOR INTNO_TypeDefine
---
CREATE TRIGGER TypeDefine_BEF_INSERT_EVENTLOG FOR TypeDefine
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER TypeDefine_BEF_UPDATE_EVENTLOG FOR TypeDefine
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE PROCEDURE TypeLevelCheckUpdate (
  P_TypeLevel SMALLINT,
  P_TypeLength SmallInt,
  P_LRemark Char(40)
)  AS 
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  m_IntNo = NULL;
  FOR SELECT IntNo From TypeLevel WHERE (TypeLevel = :p_TypeLevel)
    INTO :m_IntNo DO
    BEGIN
      UPDATE TypeLevel SET TypeLength = :p_TypeLength, LRemark = :p_LRemark
        WHERE IntNo = :m_IntNo;
    END
  IF (m_IntNo is NULL) THEN
  BEGIN
    EXECUTE PROCEDURE GETNEWINTNO('TYPELEVEL') RETURNING_VALUES :m_IntNo;
    INSERT INTO TypeLevel (IntNo, TypeLevel, TypeLength, LRemark)
      VALUES (:m_IntNo, :p_TypeLevel, :p_TypeLength, :p_LRemark);
  END
END
---

CREATE PROCEDURE GET_FULL_TYPENO_BY_INTNO (
  P_INTNO INTEGER
) RETURNS (
  FULLTYPENO VARCHAR(20)
) AS
DECLARE VARIABLE m_MasterIntNo INTEGER;
DECLARE VARIABLE m_MasterTypeNo Char(20);
BEGIN
  m_MasterIntNo = 0;
  m_MasterTypeNo = '';
  FullTypeNo = '';
  FOR SELECT MasterIntNo, TypeNo FROM TypeDefine WHERE IntNo = :p_IntNo
    INTO :m_MasterIntNo, :FullTypeNo DO
  BEGIN
    IF (not (m_MasterIntNo = 0)) THEN
    BEGIN
      EXECUTE PROCEDURE Get_Full_TypeNo_By_IntNo(:m_MasterIntNo) RETURNING_VALUES :m_MasterTypeNo;
      FullTypeNo = ALLTRIM(m_MasterTypeNo) || ALLTRIM(FullTypeNo);
    END
  END
  SUSPEND;
END
---
CREATE PROCEDURE GET_FULL_TYPENO  RETURNS (
  INTNO INTEGER,
  FULLTYPENO VARCHAR(20)
) AS
DECLARE VARIABLE m_MasterIntNo INTEGER;
DECLARE VARIABLE m_MasterTypeNo Char(20);
BEGIN
  FOR SELECT IntNo, TypeNo FROM TypeDefine INTO :IntNo, :FullTypeNo DO
  BEGIN
    IF (IntNo <> 0) THEN
      EXECUTE PROCEDURE Get_Full_TypeNo_By_IntNo(:IntNo) RETURNING_VALUES :FULLTYPENO;
    SUSPEND;
  END
END
--
2003.05.09_01/UPGRADE_DETAIL
1. 开启单据类型的标志;
--
2003.05.09_01/SQL1
DELETE From DomainDefine Where (Upper(DomainName) = 'TLISTFLAG2')
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth, Hidden, DFormat)
  VALUES ('TListFlag2', 'T', 1, 0, '##;; ')
---
DELETE From FieldDefine Where (Upper(FieldName) = 'FLAG2')
---
Insert INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Flag2', 'TListFlag2')
--
2003.05.02_01/UPGRADE_DETAIL
1. 增加进价跟踪表;
--
2003.05.02_01/SQL1
ALTER PROCEDURE PRICEOUT_UPDATE
(
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_PRICE DOUBLE PRECISION,
  P_DISCOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT
)
AS
DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_OutUseUpPrice VARCHAR(5);
DECLARE VARIABLE m_PriceTrace INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TimeStamp;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  FOR SELECT CmpyName, ListDate From OUTLIST WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO
    IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  FOR SELECT PriceTrace FROM CLIENT WHERE (CMPYNAME = :m_CmpyName) and (ClientFlag = 0) INTO :m_PriceTrace
    DO
    IF ((m_PriceTrace = 0) OR (m_PriceTrace is NULL)) THEN EXIT;

  FOR SELECT Upper(DataString) FROM SETUP WHERE (Upper(DataName) LIKE '%OUTUSEUPPRICE') AND (UserGroup = '')
  INTO :m_OutUseUpPrice
    DO
    IF ((m_OutUseUpPrice = 'FALSE') OR (m_OutUseUpPrice is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM PRICEOUT WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (m_reccount = 0) THEN
    INSERT INTO PRICEOUT (GoodsIntNo, CmpyName, Price, Discount, ListDate, UnitId)
      VALUES (:p_GoodsIntNo, :m_CmpyName, :p_Price, :p_Discount, :m_ListDate, :p_UnitId);
  ELSE
    UPDATE PRICEOUT SET Price = :p_Price, Discount = :p_Discount, ListDate = :m_ListDate, UnitId = :p_UnitId
      WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
END
---
CREATE TABLE PRICEIN
(
  INTNO	TINTNO,
  CMPYNAME	TCMPYNAME,
  GOODSINTNO	TINTNO,
  LISTDATE	TDATE,
  PRICE	TPRICE,
  DISCOUNT	TDISCOUNT,
  UNITID	TGOODSUNITID,
CONSTRAINT PRICEIN_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT PRICEIN_UNIQUE_1 UNIQUE (CMPYNAME, GOODSINTNO)
)
---
CREATE GENERATOR INTNO_PRICEIN
---
CREATE PROCEDURE PRICEIN_UPDATE
(
  P_MAININTNO INTEGER,
  P_GOODSINTNO INTEGER,
  P_PRICE DOUBLE PRECISION,
  P_DISCOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT
)
AS

DECLARE VARIABLE m_reccount INTEGER;
DECLARE VARIABLE m_UseUpPrice VARCHAR(5);
DECLARE VARIABLE m_PriceTrace INTEGER;
DECLARE VARIABLE m_CmpyName VARCHAR(12);
DECLARE VARIABLE m_ListDate TimeStamp;

BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  FOR SELECT CmpyName, ListDate From INLIST WHERE IntNo = :p_MainIntNo INTO :m_CmpyName, :m_ListDate
    DO
    IF ((m_CmpyName = '') or (m_CmpyName is NULL)) THEN EXIT;

  FOR SELECT PriceTrace FROM CLIENT WHERE (CMPYNAME = :m_CmpyName) and (ClientFlag = 1) INTO :m_PriceTrace
    DO

    IF ((m_PriceTrace = 0) OR (m_PriceTrace is NULL)) THEN EXIT;

  FOR SELECT Upper(DataString) FROM SETUP WHERE (Upper(DataName) LIKE '%INUSEUPPRICE') AND (UserGroup = '')
  INTO :m_UseUpPrice
    DO
    IF ((m_UseUpPrice = 'FALSE') OR (m_UseUpPrice is NULL)) THEN EXIT;

  SELECT COUNT(*) FROM PRICEIN WHERE (CMPYNAME = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo) INTO :m_reccount;
  IF (m_reccount = 0) THEN
    INSERT INTO PRICEIN (GoodsIntNo, CmpyName, Price, Discount, ListDate, UnitId)
      VALUES (:p_GoodsIntNo, :m_CmpyName, :p_Price, :p_Discount, :m_ListDate, :p_UnitId);
  ELSE
    UPDATE PRICEIN SET Price = :p_Price, Discount = :p_Discount, ListDate = :m_ListDate, UnitId = :p_UnitId
      WHERE (CmpyName = :m_CmpyName) and (GoodsIntNo = :p_GoodsIntNo);
END
---
CREATE TRIGGER CREATE_INTNO_PRICEIN FOR PRICEIN 
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  NEW.INTNO = GEN_ID(INTNO_PRICEIN, 10);
END
---
CREATE TRIGGER BUYGDS_AFTER_INSERT_PRICEIN FOR BUYGDS 
ACTIVE AFTER INSERT POSITION 1

AS
BEGIN
  IF (new.UpdateFlag = 0) THEN
    EXECUTE PROCEDURE PRICEIN_UPDATE(new.MainIntNo, new.GoodsIntNo, new.Price, new.Discount, new.UnitId);
END
---
CREATE TRIGGER BUYGDS_AFTER_UPDATE_PRICEIN FOR BUYGDS 
ACTIVE AFTER UPDATE POSITION 1
AS
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UpdateFlag = 0) THEN
    IF ( (new.GoodsIntNo <> old.GoodsIntNo)
      or (new.Price <> old.Price)
      or (new.Discount <> old.Discount)
      or (new.UnitId <> old.UnitId)
      ) THEN
      EXECUTE PROCEDURE PRICEIN_UPDATE(old.MainIntNo, new.GoodsIntNo, new.Price, new.Discount, new.UnitId);
  END
END
---
CREATE TRIGGER STKLST_BEFORE_DEL_DEL_DETAIL FOR STKLST
ACTIVE BEFORE DELETE POSITION 10
AS
BEGIN
  DELETE FROM STKLSTS WHERE GoodsIntNo = old.intno;

  DELETE FROM MGOODSNO WHERE GoodsIntNo = old.intno;
  DELETE FROM PRICEIN WHERE GoodsIntNo = old.intno;
  DELETE FROM PRICEOUT WHERE GoodsIntNo = old.intno;
END
--
2003.05.01_01/UPGRADE_DETAIL
1. 修改行业版进出仓的Procedure, 使之可以处理
   货号为空的记录;
--
2003.05.01_01/SQL1
ALTER PROCEDURE STOCK_UPDATE_ITEMX (
  P_ADDFLAG SMALLINT,
  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER
)  AS                 
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF (p_GoodsIntNo is NULL) THEN EXIT;
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END
  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END
  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;
  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  m_IntNo = NULL;
  SELECT INTNO FROM STKLSTSX
    WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)
      and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
      and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
      and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
      and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
      and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
    INTO :m_IntNo;
  IF (m_IntNo is NULL) THEN
    INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount, 
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5)

      VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount, 
        :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5);
  ELSE
    UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount
      WHERE IntNo = :m_IntNo;
END
--
2003.04.29_01/UPGRADE_DETAIL
1. 调整根据串号查找相应记录的Procedure;
--
2003.04.29_01/SQL1
ALTER PROCEDURE GETGOODSFROMCUSTOMINTNO (
  P_FIELDNO INTEGER,
  P_CUSTOMINTNO INTEGER
) RETURNS (
  INTNO INTEGER,
  GOODSNO CHAR(20),
  STOCKID CHAR(4),
  CUSTOMVALUE1 CHAR(120),
  CUSTOMVALUE2 CHAR(120),
  CUSTOMVALUE3 CHAR(120),
  CUSTOMVALUE4 CHAR(120),
  CUSTOMVALUE5 CHAR(120),
  QREMAIN NUMERIC(15, 4)
) AS        
DECLARE VARIABLE CustomIntNo1 INTEGER;
DECLARE VARIABLE CustomIntNo2 INTEGER;
DECLARE VARIABLE CustomIntNo3 INTEGER;
DECLARE VARIABLE CustomIntNo4 INTEGER;
DECLARE VARIABLE CustomIntNo5 INTEGER;
BEGIN
  IF (p_FieldNo = 3) THEN
  BEGIN
    FOR SELECT stk.IntNo, stk.GoodsNo, StkName.StockId, 
      StkX.CustomIntNo1, 

      StkX.CustomIntNo2, 
      StkX.CustomIntNo3, 
      StkX.CustomIntNo4, 
      StkX.CustomIntNo5, StkX.qRemain 
      FROM StklstsX StkX, Stklst stk, StkName
      WHERE (StklstsX.CustomIntNo3 = :p_CustomIntNo) and (StklstsX.GoodsIntNo = stk.IntNo)
        AND (StkX.StockIntNo = StkName.IntNo)
      INTO :IntNo, :GoodsNo, :StockId, :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5, :qRemain DO
      BEGIN
        CUSTOMVALUE1 = NULL;  CUSTOMVALUE2 = NULL;  CUSTOMVALUE3 = NULL;  CUSTOMVALUE4 = NULL;  CUSTOMVALUE5 = NULL;
        IF (NOT (CustomIntNo1 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo1 INTO :CustomValue1;
        IF (NOT (CustomIntNo2 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo2 INTO :CustomValue2;
        IF (NOT (CustomIntNo3 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo3 INTO :CustomValue3;
        IF (NOT (CustomIntNo4 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo4 INTO :CustomValue4;
        IF (NOT (CustomIntNo5 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo5 INTO :CustomValue5;
        SUSPEND;
      END
  END
END
--
2003.04.28_01/UPGRADE_DETAIL
1. 新增部门表: Departments;
2. 设置部门表的数据项的显示;
3. 设置部门结帐的Procedure;
4. 设置CustomValues表的Trigger, 为数据同步
   做准备;
--
2003.04.28_01/SQL1
DELETE FROM FIELDDEFINE WHERE (TABLENAME='') AND (UPPER(FIELDNAME) = 'DEP_NO')
---
INSERT INTO FIELDDEFINE (TableName, FieldName, DomainName)
  VALUES ('', 'Dep_No', 'TDep_No')
---
DELETE FROM DOMAINDEFINE WHERE (UPPER(DomainName) = 'TDEP_NO')
---
INSERT INTO DOMAINDEFINE (DomainName, DLabel, DWidth)
  VALUES ('TDep_No', '代号', '12')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME='') AND (UPPER(FIELDNAME) = 'FAX')
---

INSERT INTO FIELDDEFINE (TableName, FieldName, DomainName)
  VALUES ('', 'FAX', 'TFAX')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME='') AND (UPPER(FIELDNAME) = 'ADDRESS')
---
INSERT INTO FIELDDEFINE (TableName, FieldName, DomainName)
  VALUES ('', 'ADDRESS', 'TADDRESS')
---
/* Domain definitions */
CREATE DOMAIN TDEP_NO AS CHAR(12)
	 DEFAULT '' NOT NULL;
---
CREATE DOMAIN TDEP_FNAME AS VARCHAR(50)
	 DEFAULT '' NOT NULL;
---
CREATE DOMAIN TDEP_FLAG AS SMALLINT
	 DEFAULT 0 NOT NULL;
---
CREATE DOMAIN TDEP_TYPE AS CHAR(12)
	 DEFAULT '' NOT NULL;
---
CREATE TABLE DEPARTMENTS
(
  INTNO	TINTNO,
  DEP_NO	TDEP_NO,
  DEPARTMENT    TDEPARTMENT,
  S_DATE	TLIMITDATE NOT NULL,
  GDSPAY	TAMOUNT,
  ACTPAY	TAMOUNT,
  INITREMAIN	TAMOUNT,
  TELE	TTELE,
  FAX	TFAX,
  PRICENO	TPRICENO,
  AMOUNTWARNING	TAMOUNT,
  AMOUNTLIMIT	TAMOUNT,
  DAYLIMIT	TACCDAYLIMIT,
  DEP_BOSS	TPERSONNAME,

  DEP_CONTACT	TCONTACT,
  ADDRESS	TADDRESS,
  TAXNO	TTAXNO,
  LREMARK	TLREMARK,
  DEP_FNAME	TDEP_FNAME,
  DEP_BANKNAME	TBANKNAME,
  DEP_BANKNO	TBANKACCNO,
  CREATE_RM	TAMOUNT,
  DEP_FLAG	TDEP_FLAG,
  AMOUNTTOPAY	TAMOUNT,
  PINYINCODE	TPINYINCODE,
  CITY	TCITY,
  PROVINCE	TPROVINCE,
  DEP_TYPE	TDEP_TYPE,
  PERSONDEAL	TPERSONNAME,
  PRICETRACE	SMALLINT DEFAULT 0 NOT NULL,
  WEBURL	VARCHAR(40),

  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
  REPORTCAPTION	VARCHAR(40),
CONSTRAINT DEP_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT DEP_UNIQUE_1 UNIQUE (DEPARTMENT, DEP_FLAG),
CONSTRAINT DEP_UNIQUE_2 UNIQUE (DEP_NO, DEP_FLAG)
);

/* Triggers only will work for SQL triggers */
---
CREATE TRIGGER DEP_BEFORE_INSERT_SET_DEF FOR DEPARTMENTS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.Dep_BankName is NULL) THEN new.Dep_BankName = '';
  IF (new.Dep_BankNo is NULL) THEN new.Dep_BankNo = '';
  IF (new.Dep_Boss is NULL) THEN new.Dep_Boss = '';
  IF (new.Dep_Contact is NULL) THEN new.Dep_Contact = '';
  IF (new.City is NULL) THEN new.City = '';
  IF (new.Province is NULL) THEN new.Province = '';
  IF (new.Dep_Type is NULL) THEN new.Dep_Type = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.TaxNo is NULL) THEN new.TaxNo = '';
  IF (new.Fax is NULL) THEN new.Fax = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.Dep_FName is NULL) THEN new.Dep_FName = '';
END
---
CREATE TRIGGER DEP_BEFORE_INSERT_EVENTLOG FOR DEPARTMENTS
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER DEP_BEFORE_UPDATE_SET_DEF FOR DEPARTMENTS
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN

  IF (new.PinYinCode is NULL) THEN new.PinYinCode = '';
  IF (new.LRemark is NULL) THEN new.LRemark = '';
  IF (new.PriceNo is NULL) THEN new.PriceNo = '';
  IF (new.Dep_BankName is NULL) THEN new.Dep_BankName = '';
  IF (new.Dep_BankNo is NULL) THEN new.Dep_BankNo = '';
  IF (new.Dep_Boss is NULL) THEN new.Dep_Boss = '';
  IF (new.Dep_Contact is NULL) THEN new.Dep_Contact = '';
  IF (new.City is NULL) THEN new.City = '';
  IF (new.Province is NULL) THEN new.Province = '';
  IF (new.Dep_Type is NULL) THEN new.Dep_Type = '';
  IF (new.PersonDeal is NULL) THEN new.PersonDeal = '';
  IF (new.Address is NULL) THEN new.Address = '';
  IF (new.TaxNo is NULL) THEN new.TaxNo = '';
  IF (new.Fax is NULL) THEN new.Fax = '';
  IF (new.Tele is NULL) THEN new.Tele = '';
  IF (new.Dep_FName is NULL) THEN new.Dep_FName = '';
END
---
CREATE TRIGGER DEP_BEFORE_UPDATE_EVENTLOG FOR DEPARTMENTS
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER DEP_AFTER_DELETE_EVENT_LOG FOR DEPARTMENTS
ACTIVE AFTER DELETE POSITION 10
AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('DEPARTMENTS', old.IntNo);
END
---
CREATE PROCEDURE Department_SET_LIMIT (
  P_Dep_FLAG SMALLINT,
  P_Dep_NAME CHAR(12),
  P_S_DATE TIMESTAMP
)  AS
DECLARE VARIABLE m_b_gdspay DECIMAL (15,4);
DECLARE VARIABLE m_b_actpay DECIMAL (15,4);
DECLARE VARIABLE m_a_gdspay DECIMAL (15,4);
DECLARE VARIABLE m_a_actpay DECIMAL (15,4);
BEGIN
  IF (P_Dep_FLAG = 0) THEN
  BEGIN
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM OUTLIST
      WHERE (LISTDATE <= :P_S_DATE) AND (DEPARTMENT = :P_Dep_NAME)
      INTO :m_b_gdspay, :m_b_actpay;
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM OUTLIST
      WHERE (LISTDATE > :P_S_DATE) AND (DEPARTMENT = :P_Dep_NAME)
      INTO :m_a_gdspay, :m_a_actpay;
  END
  ELSE
  BEGIN
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM INLIST
      WHERE (LISTDATE <= :P_S_DATE) AND (DEPARTMENT = :P_Dep_NAME)
      INTO :m_b_gdspay, :m_b_actpay;
    SELECT SUM(GDSPAY), SUM(ACTPAY) FROM INLIST
      WHERE (LISTDATE > :P_S_DATE) AND (DEPARTMENT = :P_Dep_NAME)
      INTO :m_a_gdspay, :m_a_actpay;
  END
  IF (m_b_gdspay is NULL) THEN m_b_gdspay = 0;
  IF (m_b_actpay is NULL) THEN m_b_actpay = 0;
  IF (m_a_gdspay is NULL) THEN m_a_gdspay = 0;

  IF (m_a_actpay is NULL) THEN m_a_actpay = 0;
  UPDATE Departments SET INITREMAIN = CREATE_RM + :m_b_gdspay - :m_b_actpay,
    GDSPAY = :m_a_gdspay, ACTPAY = :m_a_actpay
    WHERE (DEPARTMENT = :P_Dep_NAME) and (Dep_Flag = :p_Dep_Flag);
END
---
CREATE TRIGGER DEP_AFTER_UPDATE_SET_LIMIT FOR DEPARTMENTS
ACTIVE AFTER UPDATE POSITION 0
AS
/* 执行结帐操作 */
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF ((old.S_DATE <> new.S_Date) or (old.Create_RM <> new.Create_RM)) THEN
      EXECUTE PROCEDURE CLIENT_SET_LIMIT (new.DEP_FLAG, old.Department, new.S_Date);
  END
END
---
CREATE TRIGGER DEP_AFTER_INSERT_SET_LIMIT FOR DEPARTMENTS
ACTIVE AFTER INSERT POSITION 10
AS
BEGIN
  EXECUTE PROCEDURE CLIENT_SET_LIMIT(new.DEP_FLAG, new.Department, '1900/1/1');
END
---
ALTER TABLE CUSTOMVALUES ADD InsertUCT INTEGER
---
ALTER TABLE CUSTOMVALUES ADD UpdateUCT INTEGER
---
CREATE TRIGGER CV_BEFORE_INSERT_EVENTLOG FOR CUSTOMVALUES 
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER CV_BEFORE_UPDATE_EVENTLOG FOR CUSTOMVALUES
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER CV_AFTER_DELETE_EVENT_LOG FOR CUSTOMVALUES
ACTIVE AFTER DELETE POSITION 10
AS
BEGIN
  EXECUTE PROCEDURE SYS_LOGDELETE('CUSTOMVALUES', old.IntNo);
END
--
2003.04.27_03/UPGRADE_DETAIL
1. 开启部门数据项的显示;
--
2003.04.27_03/SQL1
INSERT INTO FIELDDEFINE (TableName, FieldName, DomainName)
  VALUES ('', 'Department', 'TDepartment')
---
UPDATE FIELDDEFINE SET Hidden = 0 WHERE UPPER(DomainName) = 'TDEPARTMENT'
--
2003.04.27_02_SYS/UPGRADE_DETAIL
1. 修改OPER结构, 增加[分组]的项目;
--
2003.04.27_02_SYS/SQL1
CREATE DOMAIN TUserGroup AS CHAR(12) DEFAULT '' NOT NULL
--
2003.04.27_02_SYS/SQL2
ALTER TABLE OPER ADD UserGroup TUserGroup
--
2003.04.27_01/UPGRADE_DETAIL
1. 修正进出仓的方法, 使之适用与各类行业版;
2. 修改SETUP表结构, 使之可处理不同的分组;
3. 设置数据项分组的显示方式;
4. 设置SETUP分组内容初始化方式;
--
2003.04.27_01/SQL1
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
BEGIN
  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
/*
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
*/
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
/*

        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
*/
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
/*
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
*/
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId,
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
/*
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
*/

          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
END
---

ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS  
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_AddFlag SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
BEGIN
  m_AddFlag = -p_AddFlag;
  FOR SELECT GoodsIntNo, Qty, Amount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From MOVEGDS
    WHERE MainIntNo = :p_MainIntNo
    INTO :m_GoodsIntNo, :m_qty, :m_Amount, :m_UnitId,
         :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, p_StockIntNoTo, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
        EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(m_AddFlag, m_GoodsIntNo, p_StockIntNoFrom, m_Qty, m_Amount, m_UnitId,
          m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
END
---
CREATE DOMAIN TUserGroup AS CHAR(12) DEFAULT '' NOT NULL
---
DROP INDEX SETUP_INDEX_DATANAME 
---
ALTER TABLE SETUP ADD UserGroup TUserGroup
---
ALTER TABLE SETUP
  DROP CONSTRAINT SETUP_UNIQUE_1
---
ALTER TABLE SETUP
  ADD CONSTRAINT SETUP_UNIQUE_1
  UNIQUE (DATANAME,USERGROUP)
---
DELETE FROM DOMAINDEFINE WHERE UPPER(DomainName) = 'TUSERGROUP'
---
INSERT INTO DOMAINDEFINE (DomainName, DLabel, DWidth)
  VALUES ('TUserGroup', '分组', '12')
---
DELETE FROM FIELDDEFINE WHERE TableName = '' AND UPPER(DomainName) = 'TUSERGROUP'
---
INSERT INTO FIELDDEFINE (TableName, FieldName, DomainName)
  VALUES ('', 'UserGroup', 'TUserGroup')
---
CREATE PROCEDURE CheckSetupUserGroup(
  pUserGroup VARChar(12)
) AS
DECLARE VARIABLE DataName VARCHAR(40);
DECLARE VARIABLE DataTYPE VARCHAR(1);
DECLARE VARIABLE DATASTRING VARCHAR(100);
DECLARE VARIABLE DESCRIPT VARCHAR(40);
DECLARE VARIABLE INTNO INTEGER;
BEGIN
  FOR SELECT DATANAME, DATATYPE, DATASTRING, DESCRIPT
    FROM SETUP WHERE USERGROUP = ''
    INTO :DataName, :DataType, :DataString, :Descript
    DO
    BEGIN
      INTNO = NULL;
      SELECT INTNO FROM SETUP WHERE
        (DATANAME = :DataName) AND (USERGROUP = :pUserGroup)
      INTO :IntNo;
      IF (IntNo is NULL) THEN
      BEGIN
        SELECT R_NEWINTNO FROM GETNEWINTNO('SETUP') INTO :IntNo;
        INSERT INTO SETUP
          (INTNO, DATANAME, DATATYPE, DATASTRING, DESCRIPT, USERGROUP)
          VALUES (:IntNo, :DataName, :DataType, :DataString, :Descript, :pUserGroup);
      END
    END
END
--
2003.04.13_01/UPGRADE_DETAIL
1. 修改数据库结构, 所有的有关GoodsIntNo的表
   增加字段: CustomIntNo1-5;
2. 增加表: CustomValues 用于记录不同的自定义
   内容;
3. 增加表: StklstsX 用于记录不同的自定义项目
   组合的商品的库存量;
--
2003.04.13_01/SQL1
CREATE DOMAIN TCustomINTNO AS INTEGER DEFAULT NULL;
---

ALTER TABLE Assgds Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE Assgds Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE Assgds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Assgds Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Assgds Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Buygds Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE Buygds Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE Buygds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Buygds Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Buygds Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Salegds Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE Salegds Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE Salegds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Salegds Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Salegds Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE FormulaDetail Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE FormulaDetail Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE FormulaDetail Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE FormulaDetail Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE FormulaDetail Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Movegds Add CustomIntNo1 TCustomINTNO

---
ALTER TABLE Movegds Add CustomIntNo2 TCustomINTNO
---

ALTER TABLE Movegds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Movegds Add CustomIntNo4 TCustomINTNO
---


ALTER TABLE Movegds Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Pangds Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE Pangds Add CustomIntNo2 TCustomINTNO
---

ALTER TABLE Pangds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Pangds Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Pangds Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Pricelst Add CustomIntNo1 TCustomINTNO

---
ALTER TABLE Pricelst Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE Pricelst Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Pricelst Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Pricelst Add CustomIntNo5 TCustomINTNO
---

ALTER TABLE Realgds Add CustomIntNo1 TCustomINTNO
---
ALTER TABLE Realgds Add CustomIntNo2 TCustomINTNO
---
ALTER TABLE Realgds Add CustomIntNo3 TCustomINTNO
---
ALTER TABLE Realgds Add CustomIntNo4 TCustomINTNO
---
ALTER TABLE Realgds Add CustomIntNo5 TCustomINTNO
---

Create Domain TCustomNo CHAR(12)
---
Create Domain TCustomString CHAR(120) NOT NULL
---
CREATE GENERATOR INTNO_CustomValues;
---
CREATE TABLE CustomValues
(
  INTNO	TINTNO,
  FieldNo Integer NOT NULL,
  CustomNo TCustomNo,
  StrValue TCustomString,
CONSTRAINT CValues_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT CValues_UNIQUE_2 UNIQUE (FieldNo, StrValue)
)
--
2003.04.13_01/SQL2
CREATE TABLE STKLSTSx
(
  INTNO	TINTNO,
  STOCKINTNO	TINTNO,
  GOODSINTNO	TINTNO,
  QREMAIN	TQTY,
  INPRICE	TPRICE,
  AMOUNT	TAMOUNT,
  INITPRICE	TPRICE,
  QINITREMAIN	TQTY,
  INITAMOUNT	TAMOUNT,
  USED	SMALLINT,
  UPDATEUCT	INTEGER,
  INSERTUCT	INTEGER,
  CUSTOMINTNO1  INTEGER,
  CUSTOMINTNO2  INTEGER,
  CUSTOMINTNO3  INTEGER,
  CUSTOMINTNO4  INTEGER,
  CUSTOMINTNO5  INTEGER,
CONSTRAINT STKLSTSX_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
ALTER TABLE STKLSTSX ADD CONSTRAINT FK_STKLSTSX_STKLST 

  FOREIGN KEY (GOODSINTNO) REFERENCES STKLST (INTNO) 

  ON UPDATE NO ACTION ON DELETE CASCADE
---
ALTER TABLE STKLSTSX ADD CONSTRAINT FK_STKLSTSX_STOCKID 
  FOREIGN KEY (STOCKINTNO) REFERENCES STKNAME (INTNO) 
  ON UPDATE CASCADE ON DELETE CASCADE
---
CREATE GENERATOR INTNO_STKLSTSX
---
CREATE TRIGGER CREATE_INTNO_STKLSTSX FOR STKLSTSX 
ACTIVE BEFORE INSERT POSITION 0

AS
BEGIN
  NEW.INTNO = GEN_ID(INTNO_STKLSTSX, 10);
END
---
CREATE TRIGGER STKLSTSX_BEFORE_INSERT_EVENTLOG FOR STKLSTSX 
ACTIVE BEFORE INSERT POSITION 10
AS
BEGIN
  new.InsertUCT = zs_GetUnixTime();
END
---

CREATE TRIGGER STKLSTSX_BEFORE_UPDATE_1 FOR STKLSTSX 
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
  IF ((new.AMOUNT <> old.AMOUNT) or (new.QREMAIN <> old.QREMAIN)) THEN
    IF (new.QRemain <> 0) THEN new.InPrice = new.Amount / new.QRemain;
  IF ((new.INITAMOUNT <> old.INITAMOUNT) or (new.QINITREMAIN <> old.QINITREMAIN)) THEN
    IF (new.QINITRemain <> 0) THEN new.InitPrice = new.InitAmount / new.QInitRemain;
END
---

CREATE TRIGGER STKLSTSX_BEFORE_UPDATE_EVENTLOG FOR STKLSTSX 
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  new.UpdateUCT = zs_GetUnixTime();
END
---
CREATE TRIGGER STKLSTSX_AFTER_UPDATE_1 FOR STKLSTSX
ACTIVE AFTER UPDATE POSITION 0
AS
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)
    and ((new.QRemain <> old.QRemain) or (new.AMOUNT <> old.Amount))) THEN
  BEGIN
    UPDATE STKLSTS SET QRemain = QRemain + new.QRemain, Amount = AMOUNT + new.Amount
      WHERE (GoodsIntNo = old.GoodsIntNo) and (StockIntNo = old.StockIntNo);
    UPDATE STKLSTS SET QRemain = QRemain - old.QRemain, Amount = AMOUNT - old.Amount
      WHERE (GoodsIntNo = old.GoodsIntNo) and (StockIntNo = old.StockIntNo);
  END
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)
    and ((new.QInitRemain <> old.QInitRemain) or (new.InitAMOUNT <> old.InitAmount))) THEN
  BEGIN
    UPDATE STKLSTS SET QInitRemain = QInitRemain + new.QInitRemain,
      InitAmount = InitAMOUNT + new.InitAmount
      WHERE (GoodsIntNo = old.GoodsIntNo) and (StockIntNo = old.StockIntNo);
    UPDATE STKLSTS SET QInitRemain = QInitRemain - old.QInitRemain,
      InitAmount = InitAMOUNT - old.InitAmount
      WHERE (GoodsIntNo = old.GoodsIntNo) and (StockIntNo = old.StockIntNo);
  END
END
---
CREATE TRIGGER STKLSTSX_AFTER_INSERT_1 FOR STKLSTSX AFTER INSERT POSITION 0 AS 
BEGIN
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1) 
    and ((new.QRemain <> 0) or (new.AMOUNT <> 0))) THEN
  BEGIN
    UPDATE STKLSTS SET QRemain = QRemain + new.QRemain, Amount = AMOUNT + new.Amount
      WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo);
  END
  IF ((GEN_ID(INTNO_DataInit, 0) <> 1)
    and ((new.QInitRemain <> 0) or (new.InitAMOUNT <> 0))) THEN
  BEGIN
    UPDATE STKLSTS SET QInitRemain = QInitRemain + new.QInitRemain,
      InitAmount = InitAMOUNT + new.InitAmount
      WHERE (GoodsIntNo = new.GoodsIntNo) and (StockIntNo = new.StockIntNo);

  END
END
--
2003.04.13_01/SQL3
CREATE PROCEDURE STOCK_UPDATE_ITEMX (
  P_ADDFLAG SMALLINT,

  P_GOODSINTNO INTEGER,
  P_STOCKINTNO INTEGER,
  P_QTY DOUBLE PRECISION,
  P_AMOUNT DOUBLE PRECISION,
  P_UNITID SMALLINT,
  P_CINTNO1 INTEGER,
  P_CINTNO2 INTEGER,
  P_CINTNO3 INTEGER,
  P_CINTNO4 INTEGER,
  P_CINTNO5 INTEGER
)  AS
DECLARE VARIABLE m_Factor Dec(15,4);
DECLARE VARIABLE m_IntNo Integer;
BEGIN
  IF (P_UNITID <> 0) THEN
  BEGIN
    SELECT FACTOR FROM UNITLIST WHERE GOODSINTNO = :p_GoodsIntNo and UnitId = :p_UnitId INTO :m_Factor;
    p_Qty = p_Qty * m_Factor;
  END
  IF (p_AddFlag = -1) THEN
  BEGIN p_Qty = -p_Qty; p_Amount = -p_Amount; END
  IF (p_CIntNo1 = 0) THEN p_CIntNo1 = NULL;
  IF (p_CIntNo2 = 0) THEN p_CIntNo2 = NULL;

  IF (p_CIntNo3 = 0) THEN p_CIntNo3 = NULL;
  IF (p_CIntNo4 = 0) THEN p_CIntNo4 = NULL;
  IF (p_CIntNo5 = 0) THEN p_CIntNo5 = NULL;
  SELECT INTNO FROM STKLSTSX
    WHERE (GoodsIntNo = :p_GoodsIntNo) and (StockIntNo = :p_StockIntNo)

      and ((CustomIntNo1 = :P_CIntNo1) or (:p_CIntNo1 is NULL and CustomIntNo1 is NULL))
      and ((CustomIntNo2 = :P_CIntNo2) or (:p_CIntNo2 is NULL and CustomIntNo2 is NULL))
      and ((CustomIntNo3 = :P_CIntNo3) or (:p_CIntNo3 is NULL and CustomIntNo3 is NULL))
      and ((CustomIntNo4 = :P_CIntNo4) or (:p_CIntNo4 is NULL and CustomIntNo4 is NULL))
      and ((CustomIntNo5 = :P_CIntNo5) or (:p_CIntNo5 is NULL and CustomIntNo5 is NULL))
    INTO :m_IntNo;
  IF (m_IntNo is NULL) THEN
    INSERT INTO STKLSTSX (GoodsIntNo, StockIntNo, QRemain, Amount, 
      CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5)
      VALUES (:p_GoodsIntNo, :p_StockIntNo, :p_Qty, :p_Amount, 
        :P_CIntNo1, :P_CIntNo2, :P_CIntNo3, :P_CIntNo4, :P_CIntNo5);
  ELSE
    UPDATE STKLSTSX SET QREMAIN = QREMAIN + :p_Qty, Amount = Amount + :p_Amount
      WHERE IntNo = :m_IntNo;
END
---
ALTER PROCEDURE STOCK_UPDATE_LIST (
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
BEGIN
  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and 
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,

            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN

  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and 
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId,
           :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and 
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
        ELSE
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
  END
END
---
ALTER PROCEDURE STOCK_UPDATE_MOVELIST (
  P_ADDFLAG SMALLINT,
  P_MAININTNO INTEGER,
  P_STOCKINTNOFROM INTEGER,
  P_STOCKINTNOTO INTEGER
)  AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
DECLARE VARIABLE m_AddFlag SmallInt;
DECLARE VARIABLE m_CIntNo1 Integer;
DECLARE VARIABLE m_CIntNo2 Integer;
DECLARE VARIABLE m_CIntNo3 Integer;
DECLARE VARIABLE m_CIntNo4 Integer;
DECLARE VARIABLE m_CIntNo5 Integer;
BEGIN
  m_AddFlag = -p_AddFlag;
  FOR SELECT GoodsIntNo, Qty, Amount, UnitId, 
        CustomIntNo1, CustomIntNo2, CustomIntNo3, CustomIntNo4, CustomIntNo5 From MOVEGDS
    WHERE MainIntNo = :p_MainIntNo
    INTO :m_GoodsIntNo, :m_qty, :m_Amount, :m_UnitId,
         :m_CIntNo1, :m_CIntNo2, :m_CIntNo3, :m_CIntNo4, :m_CIntNo5
      DO
      BEGIN
        IF ( (m_CIntNo1 is NULL) and (m_CIntNo2 is NULL) and (m_CIntNo3 is NULL) and 
             (m_CIntNo4 is NULL) and (m_CIntNo5 is NULL) ) THEN
          EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, p_StockIntNoTo, m_Qty, m_Amount, m_UnitId);
        ELSE
          EXECUTE PROCEDURE STOCK_UPDATE_ITEMX(m_AddFlag, m_GoodsIntNo, p_StockIntNoFrom, m_Qty, m_Amount, m_UnitId,
            m_CIntNo1, m_CIntNo2, m_CIntNo3, m_CIntNo4, m_CIntNo5);
      END
END
---
CREATE PROCEDURE GETSTOCKIOEX (
  P_GOODSINTNO INTEGER
) RETURNS (
  WORKMODE SMALLINT,

  GOODSINTNO INTEGER,
  LISTDATE DATE,

  CMPYNAME CHAR(12),
  UNITID SMALLINT,
  STOCKINTNO INTEGER,
  QTYIN DOUBLE PRECISION,
  QTYOUT DOUBLE PRECISION,
  PRICE DOUBLE PRECISION,
  INPRICE DOUBLE PRECISION,

  LISTNO CHAR(14),
  REMARK CHAR(20),
  INAMOUNT DOUBLE PRECISION,
  CUSTOMINTNO1 INTEGER,
  CUSTOMINTNO2 INTEGER,
  CUSTOMINTNO3 INTEGER,
  CUSTOMINTNO4 INTEGER,
  CUSTOMINTNO5 INTEGER
) AS 
BEGIN
  FOR
    /* 进货 */
    SELECT 1, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price as InPrice, m.ListNo,
      d.Remark, d.Amount as InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM BUYGDS d
      LEFT OUTER JOIN INLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 发货 */
    SELECT 2, d.GoodsIntNo, m.ListDate, m.CmpyName, d.UnitId, d.StockIntNo,
      0.0 as QtyIn, d.Qty as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      -d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM SALEGDS d
      LEFT OUTER JOIN OUTLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 差额盘点 */
    SELECT 7, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM PANGDS d
      LEFT OUTER JOIN PANLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 组装与拆解 */
    SELECT 12, d.GoodsIntNo, m.ListDate, m.COMMENT, d.UnitId, d.StockIntNo,

      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.InPrice, m.ListNo, d.Remark,
      d.InAmount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM ASSGDS d
      LEFT OUTER JOIN ASSLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')

    UNION ALL
    /* 调拨入 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoTo,
      d.Qty as QtyIn, 0.0 as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    UNION ALL
    /* 调拨出 */
    SELECT 3, d.GoodsIntNo, m.ListDate, '            ' as CmpyName, d.UnitId, m.StockIntNoFrom,
      0.00 as QtyIn, d.Qty as QtyOut, d.Price, d.Price, m.ListNo, d.Remark,
      -d.Amount
      , d.CustomIntNo1, d.CustomIntNo2, d.CustomIntNo3, d.CustomIntNo4, d.CustomIntNo5
      FROM MoveGDS d
      LEFT OUTER JOIN MoveLIST m On (m.IntNo = d.MainIntNo)
      WHERE (d.GoodsIntNo = :p_GoodsIntNo) and (m.UpdateAcc = '*')
    INTO :WorkMode, :GoodsIntNo, :ListDate, :CmpyName, :UnitId, :StockIntNo, :QtyIn,
      :QtyOut, :Price, :InPrice, :ListNo, :Remark, :InAmount
      , :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5
  DO
    Suspend;
END
---
CREATE PROCEDURE GETGOODSFROMCUSTOMINTNO (
  P_FIELDNO INTEGER,
  P_CUSTOMINTNO INTEGER
) RETURNS (
  INTNO INTEGER,
  GOODSNO CHAR(20),
  CUSTOMVALUE1 CHAR(120),
  CUSTOMVALUE2 CHAR(120),
  CUSTOMVALUE3 CHAR(120),
  CUSTOMVALUE4 CHAR(120),
  CUSTOMVALUE5 CHAR(120)
) AS    
DECLARE VARIABLE CustomIntNo1 INTEGER;
DECLARE VARIABLE CustomIntNo2 INTEGER;
DECLARE VARIABLE CustomIntNo3 INTEGER;


DECLARE VARIABLE CustomIntNo4 INTEGER;
DECLARE VARIABLE CustomIntNo5 INTEGER;
BEGIN
  CUSTOMVALUE1 = NULL;
  CUSTOMVALUE2 = NULL;
  CUSTOMVALUE3 = NULL;
  CUSTOMVALUE4 = NULL;
  CUSTOMVALUE5 = NULL;
  IF (p_FieldNo = 3) THEN
  BEGIN
    FOR SELECT stk.IntNo, stk.GoodsNo, 
      StkX.CustomIntNo1, 
      StkX.CustomIntNo2, 
      StkX.CustomIntNo3, 
      StkX.CustomIntNo4, 
      StkX.CustomIntNo5 
      FROM StklstsX StkX, Stklst stk
      WHERE (StklstsX.CustomIntNo3 = :p_CustomIntNo) and (StklstsX.GoodsIntNo = stk.IntNo)
      INTO :IntNo, :GoodsNo, :CustomIntNo1, :CustomIntNo2, :CustomIntNo3, :CustomIntNo4, :CustomIntNo5 DO
      BEGIN
        IF (NOT (CustomIntNo1 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo1 INTO :CustomValue1;
        IF (NOT (CustomIntNo2 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo2 INTO :CustomValue2;
        IF (NOT (CustomIntNo3 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo3 INTO :CustomValue3;
        IF (NOT (CustomIntNo4 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo4 INTO :CustomValue4;
        IF (NOT (CustomIntNo5 is NULL)) THEN SELECT StrValue FROM CustomValues WHERE IntNo = :CustomIntNo5 INTO :CustomValue5;
        SUSPEND;
      END
  END
END
--
2003.04.13_01/SQL4
DELETE FROM FieldDefine WHERE (TableName='') AND
  (   Upper(FieldName) = 'CUSTOMINTNO1'
   or Upper(FieldName) = 'CUSTOMINTNO2'
   or Upper(FieldName) = 'CUSTOMINTNO3'
   or Upper(FieldName) = 'CUSTOMINTNO4'
   or Upper(FieldName) = 'CUSTOMINTNO5' )
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'CustomIntNo1', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'CustomIntNo2', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'CustomIntNo3', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'CustomIntNo4', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'CustomIntNo5', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel)
  VALUES ('', 'CustomNo', '代号')
--
2003.04.09_01/UPGRADE_DETAIL
1. 修改资金户头资料: 增加字段[使用];
   实现: 资金帐中能够隐藏不再使用的户头资料;
--
2003.04.09_01/SQL1
ALTER TABLE BANKNAME ADD Actived SmallInt Default 1
---
UPDATE BANKNAME SET ACTIVED = 1
---
DELETE FROM FieldDefine WHERE (TableName = 'BANKNAME')
  AND (UPPER(FieldName) = 'ACTIVED')
---
INSERT INTO FieldDefine (TableName, FieldName, DLabel)
  VALUES ('BANKNAME', 'Actived', '使用')
--
2003.04.07_01/UPGRADE_DETAIL
1. 增加过程: POS_CALC_STKLST 用于前台数据同步
--
2003.04.07_01/SQL1
CREATE PROCEDURE POS_CALC_STKLST
AS
DECLARE VARIABLE m_qInitRemain DEC(15,4);
DECLARE VARIABLE m_InitAmount DEC(15,4);
DECLARE VARIABLE m_QRemain DEC(15,4);
DECLARE VARIABLE m_Amount DEC(15,4);
DECLARE VARIABLE m_GoodsIntNo Integer;
BEGIN
  FOR SELECT GoodsIntNo, Sum(qInitRemain), Sum(InitAmount), Sum(QRemain), Sum(Amount) FROM STKLSTS
    GROUP BY GoodsIntNo
    INTO :m_GoodsIntNo, :m_QInitRemain, :m_InitAmount, :m_QRemain, :m_Amount
    DO
      UPDATE STKLST SET qInitRemain = :m_qInitRemain,
        InitAmount = :m_InitAmount,
        qRemain = :m_QRemain,
        Amount = :m_Amount
        WHERE IntNo = :m_GoodsIntNo;
END
--
2003.03.26_01/UPGRADE_DETAIL
1. 增加查询总资产的Procedure;
--
2003.03.26_01/SQL1
CREATE PROCEDURE GET_ASSET
 RETURNS (
  BanksAmount NUMERIC(15, 4),
  StklstAmount NUMERIC(15, 4),
  ClientAmount NUMERIC(15, 4),
  VendorAmount NUMERIC(15, 4),
  StklstOutListInAmount NUMERIC(15, 4),
  StklstInListInAmount NUMERIC(15, 4)
) AS
BEGIN
  SELECT Sum(BankIn+InitRemain-BankOut) FROM BANKNAME
    INTO BanksAmount;
  SELECT Sum(Amount) FROM Stklst

    INTO :StklstAmount;
  SELECT Sum(Gdspay-Actpay) FROM Outlist m  /* 应收款 */
    INTO :ClientAmount;
  SELECT Sum(Gdspay-Actpay) FROM InList m   /* 应付款 */
    INTO :VendorAmount;
  SELECT Sum(Gdspay-Profit) FROM OutList m
    WHERE (m.UpdateAcc = '') or (m.UpdateAcc is NULL)
    INTO :StklstOutListInAmount;
  SELECT Sum(Gdspay) FROM InList m
    WHERE (m.UpdateAcc = '') or (m.UpdateAcc is NULL)
    INTO :StklstInListInAmount;
  SUSPEND;
END
--
2003.03.25_01/UPGRADE_DETAIL
1. 增加销售/直运表的字段;
2. 设定相关的显示内容;
--
2003.03.25_01/SQL1
ALTER TABLE DOUTLIST ADD OUTLISTINTNO2 TINTNO
---
ALTER TABLE OUTLISTT ADD OUTLISTINTNO2 TINTNO
---
CREATE PROCEDURE GET_Net_Profit (
  P_DATEFROM DATE,
  P_DATETO DATE
) RETURNS (
  SaleAmount DEC(15,4),
  SaleInAmount DEC(15,4),
  PanAmountMore Dec(15,4),
  PanAmountLess Dec(15,4),
  TotalCharge Dec(15,4),
  NetProfit Dec(15,4)
) AS
BEGIN
  SELECT Sum(Gdspay), Sum(Gdspay)-Sum(Profit) FROM OUTLIST m
    WHERE m.ListDate >= :p_DateFrom and m.ListDate <= :p_DateTo
    INTO :SaleAmount, :SaleInAmount;
  SELECT Sum(InAmount) FROM PANLIST m
    WHERE (m.ListDate >= :p_DateFrom and m.ListDate <= :p_DateTo)
      AND (m.InAmount > 0)
    INTO :PanAmountMore;
  SELECT Sum(InAmount) FROM PANLIST m
    WHERE (m.ListDate >= :p_DateFrom and m.ListDate <= :p_DateTo)
      AND (m.InAmount < 0)

    INTO :PanAmountLess;
  SELECT Sum(BankOut)-Sum(BankIn) FROM BANKS d
    WHERE (d.ListDate >= :p_DateFrom and d.ListDate <= :p_DateTo)
      AND (d.Type1 = "费用")
    INTO :TotalCharge;
  NetProfit = SaleAmount-SaleInAmount+PanAmountMore-PanAmountLess-TotalCharge;
  SUSPEND;
END
--
2003.03.25_01/SQL2
DELETE FROM FieldDefine WHERE Upper(FieldName) = 'LISTTIME' and TableName = ''
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'ListTime', 'TListTime')
---
DELETE FROM FieldDefine WHERE (TableName = '') AND (
  (Upper(FieldName) = 'INLISTINTNO')
  or (Upper(FieldName) = 'OUTLISTINTNO')
  or (Upper(FieldName) = 'OUTLISTINTNO2')
  or (Upper(FieldName) = 'TRANSINTNO')
  or (Upper(FieldName) = 'STOREINTNO')
  or (Upper(FieldName) = 'PANLISTINTNO')
  )
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'INLISTINTNO', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'OUTLISTINTNO', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'OUTLISTINTNO2', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)

  VALUES ('', 'TRANSINTNO', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'STOREINTNO', 'TIntNo')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'PANLISTINTNO', 'TIntNo')
--
2003.03.08_01/UPGRADE_DETAIL
1. 新建含运费的销售/进货/直运表(共6个);
2. 设定相关的显示内容;

--
2003.03.08_01/SQL10
CREATE Procedure Query_CmpyName_Detail (
  p_CmpyName CHAR(12),
  p_DateFrom Date,
  p_ClientFlag SmallInt
) RETURNS (
 IntNo Integer,
 ListDate Date,
 GoodsIntNo Integer,
 GoodsNo CHAR(20),
 Goods CHAR(80),
 GoodsUnit CHAR(4),
 StockId CHAR(4),
 Qty Dec(15,4),
 Price Dec(15,4),
 Discount Dec(15,4),
 NetPrice Dec(15,4),
 Amount Dec(15,4),
 BankId CHAR(8),
 Actpay Dec(15,4),
 tmpRemain Dec(15,4),
 ListNo CHAR(14),
 Remark CHAR(20),
 MainRemark CHAR(20),
 UpdateFlag SmallInt
) AS

BEGIN
  IF (p_ClientFLag = 1) THEN /* 供应商 */
  BEGIN
    FOR
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit,
      StkName.StockId, d.Qty, d.Price, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 
      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag 
      From Buygds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)  
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)  
      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)  
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo) 
      LEFT OUTER JOIN InList m On (m.IntNo = d.MainIntNo)  Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
    DO SUSPEND;
  END
  ELSE  /* 客户 */

  BEGIN
    FOR 
    SELECT d.IntNo, m.ListDate, d.GoodsIntNo, stk.GoodsNo, stk.Goods, u.GoodsUnit, 

      StkName.StockId, d.Qty, d.Price, d.Discount, d.Price*d.Discount as NetPrice, d.Amount, 
      b.BankId, d.Actpay, d.Amount as tmpRemain, m.ListNo, d.Remark, m.Remark as MainRemark, d.UpdateFlag 
      From Salegds d  
      LEFT OUTER JOIN Stklst stk On (d.GoodsIntNo = stk.IntNo)
      LEFT OUTER JOIN UnitList u On (u.GoodsIntNo = d.GoodsIntNo) and (d.UnitId = u.UnitId)

      LEFT OUTER JOIN BankName b On (b.IntNo = d.BankIntNo)
      LEFT OUTER JOIN StkName On (StkName.IntNo = d.StockIntNo)
      LEFT OUTER JOIN OutList m On (m.IntNo = d.MainIntNo)  Where (m.CmpyName = :P_CmpyName) and (m.ListDate > :P_DateFrom)
    INTO :IntNo, :ListDate, :GoodsIntNo, :GoodsNo, :Goods, :GoodsUnit,
         :StockId, :Qty, :Price, :Discount, :NetPrice, :Amount,
         :BankId, :Actpay, :tmpRemain, :ListNo, :Remark, :MainRemark, :UpdateFlag
    DO SUSPEND;
  END
END
---
DELETE FROM FieldDefine WHERE (TableName='' and Upper(FieldName)='MAINREMARK')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName, DLabel)
  VALUES ('', 'MainRemark', 'TRemark', '主单备注')
---
Alter table Client Add ReportCaption Varchar(40)
---
DELETE FROM DomainDefine WHERE (Upper(DomainName)='TREPORTCAPTION')
---
Insert INTO DomainDefine (DomainName, DLabel, DWidth, Hidden)
  VALUES ('TReportCaption', '报表标题', 40, 0)
---
DELETE FROM FieldDefine WHERE (TableName='' and Upper(FieldName)='REPORTCAPTION')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'ReportCaption', 'TReportCaption')
---
DELETE FROM DomainDefine WHERE (Upper(DomainName)='TTYPE3')
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TType3', '摘要三', 0, 'TYPE3')
---


DELETE FROM FieldDefine WHERE (Upper(FieldName)='TYPE3')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Type3', 'TType3')
---
DELETE FROM DomainDefine WHERE (Upper(DomainName)='TTYPE2')
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TType2', '摘要二', 0, 'TYPE2')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName)='TYPE2')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Type2', 'TType2')
---
DELETE FROM DomainDefine WHERE (Upper(DomainName)='TTYPE1')
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TType1', '摘要一', 0, 'TYPE1')
---
DELETE FROM FieldDefine WHERE (Upper(FieldName)='TYPE1')
---
INSERT INTO FieldDefine (TableName, FieldName, DomainName)
  VALUES ('', 'Type1', 'TType1')
--
2003.03.08_01/SQL_CANCEL
ALTER PROCEDURE CHECK_LISTNO_EXISTS (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_LISTCOUNT SMALLINT
) AS
BEGIN
END
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'ASSGDS' AND UPPER(FIELDNAME) = 'QTYDELTA'
---
DELETE FROM FIELDDEFINE WHERE TABLENAME = 'ASSGDS' AND UPPER(FIELDNAME) = 'QREMAIN'
---
ALTER TABLE BANKS DROP TYPE3
---
DROP DOMAIN TTYPE3
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'UPDATEACC'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'UPDATEFLAG'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'ASSGDS') AND UPPER(FIELDNAME) = 'QREMAIN'
---
DELETE FROM DOMAINDEFINE WHERE UPPER(DOMAINNAME) = 'TCMPYVENDOR'
  OR UPPER(DOMAINNAME) = 'TCMPYCLIENT'
  OR UPPER(DOMAINNAME) = 'TCMPYTRANS'
  OR UPPER(DOMAINNAME) = 'TCMPYSTORE'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CMPYVENDOR'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CMPYCLIENT'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CMPYTRANS'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CMPYSTORE'

---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'PERSONVENDOR'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'PERSONCLIENT'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'PERSONTRANS'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'PERSONSTORE'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CITYFROM'
---

DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'CITYTO'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = '') AND UPPER(FIELDNAME) = 'SHIPNO'
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'DOUTLIST')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'OUTLISTT')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'INLISTT')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'BUYGDST')
---

DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'SALEGDST')
---
DELETE FROM FIELDDEFINE WHERE (TABLENAME = 'DSALEGDS')
---
--
2003.03.08_01/SQL9
CREATE PROCEDURE Get_StkFlag_From_UpdateAcc (
  p_OldUpdateAcc VarChar(1),
  p_NewUpdateAcc VarChar(1)
) RETURNS (
  R_Flag SMALLINT
) AS
BEGIN
  IF (((p_OldUpdateAcc=' ') or (p_OldUpdateAcc='') or (p_OldUpdateAcc is NULL))
    and (p_NewUpdateAcc='*')) THEN
  BEGIN
    R_Flag = 1;
    EXIT;
  END
  IF (((p_NewUpdateAcc=' ') or (p_NewUpdateAcc='') or (p_NewUpdateAcc is NULL))
    and (p_OldUpdateAcc='*')) THEN
  BEGIN
    R_Flag = -1;
    EXIT;
  END
  R_Flag = 0;
END
---
CREATE TRIGGER INLIST_AFTER_UPDATE_UPDATEACC FOR INLIST 
ACTIVE AFTER UPDATE POSITION 2
AS /*执行进仓/取消进仓的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN 
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc) 
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'BUYGDS', old.IntNo);
      /* 如果是进仓, 则更新最新进价 */
      IF ((m_Flag = 1) and (new.Flag1 <> 1)) THEN
        EXECUTE PROCEDURE INLIST_UPDATE_PRICEUPIN(old.IntNo, new.ListDate, new.CmpyName);
    END
  END

END
---
CREATE TRIGGER OUTLIST_AFTER_UPDATE_UPDATEACC FOR OUTLIST 
ACTIVE AFTER UPDATE POSITION 2
AS 
/*执行进仓/取消进仓的操作*/
DECLARE VARIABLE m_Flag SmallInt;
BEGIN
  IF (GEN_ID(INTNO_DataInit, 0) <> 1) THEN
  BEGIN
    IF (new.UPDATEACC <> old.UPDATEACC) THEN
    BEGIN
      EXECUTE PROCEDURE Get_StkFlag_From_UpdateAcc(Old.UpdateAcc, New.UpdateAcc) 
        RETURNING_VALUES :m_Flag;
      EXECUTE PROCEDURE STOCK_UPDATE_LIST (m_Flag, 'SALEGDS', old.IntNo);
    END
  END
END
---
ALTER PROCEDURE STOCK_UPDATE_LIST 
(
  P_ADDFLAG SMALLINT,
  P_GDSTABLE VARCHAR(10),
  P_MAININTNO INTEGER
)
AS
DECLARE VARIABLE m_GoodsIntNo Integer;
DECLARE VARIABLE m_StockIntNo Integer;
DECLARE VARIABLE m_Qty Dec(15,4);
DECLARE VARIABLE m_Amount Dec(15,4);
DECLARE VARIABLE m_UnitId SmallInt;
BEGIN
  IF (p_AddFlag=0) THEN EXIT;
  IF (p_GdsTable = 'BUYGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, Amount, UnitId From BUYGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId
      DO

      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
      END
  END
  IF (p_GdsTable = 'SALEGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, -Qty, -InAmount, UnitId From SALEGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
      END
  END
  IF (p_GdsTable = 'PANGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId From PANGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
      END
  END

  IF (p_GdsTable = 'ASSGDS') THEN
  BEGIN
    FOR SELECT GoodsIntNo, StockIntNo, Qty, InAmount, UnitId From ASSGDS
      WHERE MainIntNo = :p_MainIntNo
      INTO :m_GoodsIntNo, :m_StockIntNo, :m_qty, :m_Amount, :m_UnitId
      DO
      BEGIN
        EXECUTE PROCEDURE STOCK_UPDATE_ITEM(p_AddFlag, m_GoodsIntNo, m_StockIntNo, m_Qty, m_Amount, m_UnitId);
      END
  END
END
---
UPDATE DomainDefine SET DLabel = '内容' WHERE upper(DomainName) = 'TPHRASETEXT'
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'ASSGDS', 'QtyDelta', 'TQty', 0, '最终存量', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'ASSGDS', 'QRemain', 'TQty', 0, '现存量', null)
---
CREATE DOMAIN TTYPE3 VARCHAR(20)
---
ALTER TABLE BANKS ADD TYPE3 TTYPE3
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TType3', '摘要三', 0, 'TYPE3')
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN)
  VALUES (0, '', 'Type3', 'TType3', 0)
--
2003.03.08_01/SQL8
ALTER PROCEDURE CHECK_LISTNO_EXISTS (
  P_WORKMODE SMALLINT,
  P_LISTNO VARCHAR(14),
  P_RAISEEXCEPTION SMALLINT
) RETURNS (
  R_LISTCOUNT SMALLINT
) AS
BEGIN
  R_ListCount = 0;
  IF (p_WorkMode = 1) THEN   /* 进货 */
  BEGIN
    SELECT COUNT(*) FROM INLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 2) THEN  /* 发货 */
  BEGIN
    SELECT COUNT(*) FROM OUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 7) THEN  /* 差额盘点 */
  BEGIN
    SELECT COUNT(*) FROM PANLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 12) THEN  /* 组装与拆解 */
  BEGIN
    SELECT COUNT(*) FROM ASSLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 3) THEN  /* 调拨 */
  BEGIN
    SELECT COUNT(*) FROM MOVELIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END

  IF (P_WorkMode = 5) THEN  /* 报价单 */
  BEGIN
    SELECT COUNT(*) FROM BAOLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 6) THEN  /* 资金调拨 */
  BEGIN
    SELECT COUNT(*) FROM BANKMOVE WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END

  IF (P_WorkMode = 9) THEN  /* 分布式盘点 */
  BEGIN
    SELECT COUNT(*) FROM REALLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 13) THEN  /* 直运商品销售 */
  BEGIN
    SELECT COUNT(*) FROM DOUTLIST WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 14) THEN  /* 销售(含运费) */
  BEGIN
    SELECT COUNT(*) FROM OUTLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF (P_WorkMode = 15) THEN  /* 进货(含运费) */
  BEGIN
    SELECT COUNT(*) FROM INLISTT WHERE LISTNO = :p_ListNo INTO :R_ListCount;
  END
  IF ((R_ListCount > 0) AND (p_RaiseException = 1)) THEN
    EXCEPTION MSG_LISTNO_ALREADY_EXISTS;
END
--
2003.03.08_01/SQL7
/* INLISTT & BUYGDST */
CREATE TRIGGER INLISTT_BEFORE_DEL_CHK_UPACC FOR INLISTT BEFORE DELETE POSITION 2 AS
/*如果单据已生效, 则不允许删除(除非正在做帐务清除)*/
BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---
CREATE TRIGGER INLISTT_BEFORE_DEL_DEL_DETAIL FOR INLISTT BEFORE DELETE POSITION 10 AS

BEGIN
  DELETE FROM BUYGDST WHERE MAININTNO = old.intno;
END
---
CREATE TRIGGER INLISTT_BEFORE_INS_CHK_LISTNO FOR INLISTT BEFORE INSERT POSITION 0 AS
/*检查单号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (15, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
CREATE PROCEDURE UPDATE_INLISTT_AMOUNT (
  P_AmountStore NUMERIC(15, 4),
  P_AMOUNTIN NUMERIC(15, 4),
  P_AMOUNTTRANS NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS 
BEGIN
  UPDATE INLISTT SET AmountStore = AmountStore + (:p_AmountStore*:Direct),
    AmountIn = AmountIn + (:p_AmountIn)*:Direct,
    AmountTrans = AmountTrans + (:p_AmountTrans)*:Direct
    WHERE INTNO = :MainIntNo;
END
---
CREATE TRIGGER BUYGDST_AFTER_DELETE_1 FOR BUYGDST AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_INLISTT_AMOUNT(old.AmountStore, old.AmountIn, old.AmountTrans, -1, old.MAININTNO);
END
---
CREATE TRIGGER BUYGDST_AFTER_INSERT_1 FOR BUYGDST AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_INLISTT_AMOUNT(new.AmountStore, new.AmountIn, new.AmountTrans, 1, new.MAININTNO);
END
---
CREATE TRIGGER BUYGDST_AFTER_UPDATE_1 FOR BUYGDST AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_AmountStore DEC(15,4);
DECLARE VARIABLE m_amountIn DEC(15,4);
DECLARE VARIABLE m_amountTrans DEC(15,4);
BEGIN
  m_AmountStore = new.AmountStore - old.AmountStore;
  m_amountIn = new.AmountIn - old.AmountIn;
  m_amountTrans = new.AmountTrans - old.AmountTrans;
  EXECUTE PROCEDURE UPDATE_INLISTT_AMOUNT(:m_AmountStore, :m_amountIn, :m_AmountTrans, 1, old.MAININTNO);
END
--
2003.03.08_01/SQL6
/* OUTLISTT & SALEGDST */
CREATE TRIGGER OUTLISTT_BEFORE_DEL_CHK_UPACC FOR OUTLISTT BEFORE DELETE POSITION 2 AS
/*如果单据已生效, 则不允许删除(除非正在做帐务清除)*/


BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN

    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---

CREATE TRIGGER OUTLISTT_BEFORE_DEL_DEL_DETAIL FOR OUTLISTT BEFORE DELETE POSITION 10 AS
BEGIN
  DELETE FROM SALEGDST WHERE MAININTNO = old.intno;
END
---
CREATE TRIGGER OUTLISTT_BEFORE_INS_CHK_LISTNO FOR OUTLISTT BEFORE INSERT POSITION 0 AS
/*检查单号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (14, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
CREATE PROCEDURE UPDATE_OUTLISTT_AMOUNT (
  P_AMOUNTOUT NUMERIC(15, 4),
  P_AMOUNTIN NUMERIC(15, 4),
  P_AMOUNTTRANS NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS 
BEGIN
  UPDATE OUTLISTT SET AmountOut = AmountOut + (:p_AmountOut*:Direct),
    AmountIn = AmountIn + (:p_AmountIn)*:Direct,
    AmountTrans = AmountTrans + (:p_AmountTrans)*:Direct
    WHERE INTNO = :MainIntNo;
END
---
CREATE TRIGGER SALEGDST_AFTER_DELETE_1 FOR SALEGDST AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLISTT_AMOUNT(old.Amount, old.InAmount, old.AmountTrans, -1, old.MAININTNO);
END
---
CREATE TRIGGER SALEGDST_AFTER_INSERT_1 FOR SALEGDST AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_OUTLISTT_AMOUNT(new.Amount, new.InAmount, new.AmountTrans, 1, new.MAININTNO);
END
---
CREATE TRIGGER SALEGDST_AFTER_UPDATE_1 FOR SALEGDST AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amount DEC(15,4);
DECLARE VARIABLE m_amountIn DEC(15,4);
DECLARE VARIABLE m_amountTrans DEC(15,4);
BEGIN
  m_amount = new.Amount - old.Amount;
  m_amountIn = new.InAmount - old.InAmount;
  m_amountTrans = new.AmountTrans - old.AmountTrans;
  EXECUTE PROCEDURE UPDATE_OUTLISTT_AMOUNT(:m_amount, :m_amountIn, :m_AmountTrans, 1, old.MAININTNO);
END
--
2003.03.08_01/SQL5
/* DOUTLIST & DSALEGDS */
CREATE EXCEPTION
  MSG_UPDATED_DONE_CANNOT_DELETE
  "单据已生效, 不允许删除"
---
CREATE TRIGGER DOUTLIST_BEFORE_DEL_CHK_UPACC FOR DOUTLIST BEFORE DELETE POSITION 2 AS
/*如果单据已生效, 则不允许删除(除非正在做帐务清除)*/
BEGIN
  IF ((old.UPDATEACC = '*') and (GEN_ID(INTNO_DataInit, 0) <> 1)) THEN
    EXCEPTION MSG_UPDATED_DONE_CANNOT_DELETE;
END
---
CREATE TRIGGER DOUTLIST_BEFORE_DEL_DEL_DETAIL FOR DOUTLIST BEFORE DELETE POSITION 10 AS
BEGIN
  DELETE FROM DSALEGDS WHERE MAININTNO = old.intno;
END
---
CREATE TRIGGER DOUTLIST_BEFORE_INS_CHK_LISTNO FOR DOUTLIST BEFORE INSERT POSITION 0 AS
/*检查单号是否存在, 如存在, 则激活Exception*/
DECLARE VARIABLE m_ListCount SmallInt;
BEGIN
  EXECUTE PROCEDURE CHECK_LISTNO_EXISTS (13, new.ListNo, 1) RETURNING_VALUES :m_ListCount;
END
---
CREATE PROCEDURE UPDATE_DOUTLIST_AMOUNT (
  P_AMOUNTOUT NUMERIC(15, 4),
  P_AMOUNTIN NUMERIC(15, 4),
  P_AMOUNTTRANS NUMERIC(15, 4),
  DIRECT SMALLINT,
  MAININTNO INTEGER
)  AS
BEGIN
  UPDATE DOUTLIST SET AmountOut = AmountOut + (:p_AmountOut*:Direct),
    AmountIn = AmountIn + (:p_AmountIn)*:Direct,
    AmountTrans = AmountTrans + (:p_AmountTrans)*:Direct


    WHERE INTNO = :MainIntNo;
END
---
CREATE TRIGGER DSALEGDS_AFTER_DELETE_1 FOR DSALEGDS AFTER DELETE POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_DOUTLIST_AMOUNT(old.AmountOut, old.AmountIn, old.AmountTrans, -1, old.MAININTNO);
END
---
CREATE TRIGGER DSALEGDS_AFTER_INSERT_1 FOR DSALEGDS AFTER INSERT POSITION 0 AS
BEGIN
  EXECUTE PROCEDURE UPDATE_DOUTLIST_AMOUNT(new.AmountOut, new.AmountIn, new.AmountTrans, 1, new.MAININTNO);
END
---
CREATE TRIGGER DSALEGDS_AFTER_UPDATE_1 FOR DSALEGDS AFTER UPDATE POSITION 0 AS
DECLARE VARIABLE m_amountOut DEC(15,4);
DECLARE VARIABLE m_amountIn DEC(15,4);
DECLARE VARIABLE m_amountTrans DEC(15,4);
BEGIN

  m_amountOut = new.AmountOut - old.AmountOut;
  m_amountIn = new.AmountIn - old.AmountIn;
  m_amountTrans = new.AmountTrans - old.AmountTrans;
  EXECUTE PROCEDURE UPDATE_DOUTLIST_AMOUNT(:m_amountOut, :m_amountIn, :m_AmountTrans, 1, old.MAININTNO);
END
--
2003.03.08_01/SQL4
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DWIDTH)
  VALUES (0, '', 'UPDATEACC', 0, '√', 1)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DWIDTH)
  VALUES (0, '', 'UPDATEFLAG', 1, '临时价', 2)
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TCmpyVendor', '供应商', 0, 12)
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TCmpyClient', '客户', 0, 12)
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TCmpyTrans', '承运商', 0, 12)
---
INSERT INTO DOMAINDEFINE (DOMAINNAME, DLABEL, HIDDEN, DWIDTH) VALUES
  ('TCmpyStore', '货场名称', 0, 12)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, '', 'CmpyVendor', 'TCmpyVendor', 0, null, null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, '', 'CmpyClient', 'TCmpyClient', 0, null, null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)

  VALUES (0, '', 'CmpyTrans', 'TCmpyTrans', 0, null, null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, '', 'CmpyStore', 'TCmpyStore', 0, null, null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'PERSONCLIENT', 0, '经手人(销)', 'TPerson', null)
---

INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'PERSONVENDOR', 0, '经手人(进)', 'TPerson', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'PERSONTRANS', 0, '经手人(运)', 'TPerson', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'PERSONSTORE', 0, '经手人(仓)', 'TPerson', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'CITYFROM', 0, '起点', 'TCity', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'CITYTO', 0, '终点', 'TCity', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, '', 'SHIPNO', 0, '车船号', 'TShipNo', 12)
---
/* DSALEGDS */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'QtyIn', 'TQty', 0, '数量(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'QtyOut', 'TQty', 0, '数量(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'QtyTrans', 'TQty', 0, '数量(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'QtyStore', 'TQty', 0, '数量(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'PriceIn', 'TPrice', 0, '单价(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'PriceOut', 'TPrice', 0, '单价(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'PriceTrans', 'TPrice', 0, '单价(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'PriceStore', 'TPrice', 0, '单价(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'DiscountIn', 'TDiscount', 0, '折扣(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'DiscountOut', 'TDiscount', 0, '折扣(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'DiscountTrans', 'TDiscount', 0, '折扣(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'DiscountStore', 'TDiscount', 0, '折扣(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'AmountIn', 'TAmount', 0, '金额(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'AmountOut', 'TAmount', 0, '金额(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'AmountTrans', 'TAmount', 0, '金额(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'DSALEGDS', 'AmountStore', 'TAmount', 0, '金额(仓)', null)
---
/* SALEGDST */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'QtyIn', 'TQty', 0, '数量(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'QtyOut', 'TQty', 0, '数量(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'QtyTrans', 'TQty', 0, '数量(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'QtyStore', 'TQty', 0, '数量(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'PriceIn', 'TPrice', 0, '单价(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'PriceOut', 'TPrice', 0, '单价(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'PriceTrans', 'TPrice', 0, '单价(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'PriceStore', 'TPrice', 0, '单价(仓)', null)
---

INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'DiscountIn', 'TDiscount', 0, '折扣(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'DiscountOut', 'TDiscount', 0, '折扣(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'DiscountTrans', 'TDiscount', 0, '折扣(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'DiscountStore', 'TDiscount', 0, '折扣(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'AmountIn', 'TAmount', 0, '金额(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'AmountOut', 'TAmount', 0, '金额(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'AmountTrans', 'TAmount', 0, '金额(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'SALEGDST', 'AmountStore', 'TAmount', 0, '金额(仓)', null)
---

/* BUYGDST */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'QtyIn', 'TQty', 0, '数量(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)

  VALUES (0, 'BUYGDST', 'QtyOut', 'TQty', 0, '数量(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'QtyTrans', 'TQty', 0, '数量(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'QtyStore', 'TQty', 0, '数量(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'PriceIn', 'TPrice', 0, '单价(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'PriceOut', 'TPrice', 0, '单价(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'PriceTrans', 'TPrice', 0, '单价(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'PriceStore', 'TPrice', 0, '单价(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'DiscountIn', 'TDiscount', 0, '折扣(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'DiscountOut', 'TDiscount', 0, '折扣(销)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'DiscountTrans', 'TDiscount', 0, '折扣(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'DiscountStore', 'TDiscount', 0, '折扣(仓)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'AmountIn', 'TAmount', 0, '金额(进)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'AmountOut', 'TAmount', 0, '金额(销)', null)
---

INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'AmountTrans', 'TAmount', 0, '金额(运)', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, DomainName, HIDDEN, DLabel, DWIDTH)
  VALUES (0, 'BUYGDST', 'AmountStore', 'TAmount', 0, '金额(仓)', null)
---

/* DOUTLIST */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'DOUTLIST', 'AMOUNTOUT', 0, '销售金额', 'TAmount', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'DOUTLIST', 'AMOUNTIN', 0, '进货金额', 'TAmount', null)
---

INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'DOUTLIST', 'AMOUNTTRANS', 0, '运费', 'TAmount', null)
---
/* OUTLISTT */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'OUTLISTT', 'AMOUNTOUT', 0, '销售金额', 'TAmount', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'OUTLISTT', 'AMOUNTIN', 0, '成本', 'TAmount', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'OUTLISTT', 'AMOUNTTRANS', 0, '运费', 'TAmount', null)
---
/* INLISTT */
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'INLISTT', 'AMOUNTSTORE', 0, '码头-装卸费', 'TAmount', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'INLISTT', 'AMOUNTIN', 0, '进货金额', 'TAmount', null)
---
INSERT INTO FIELDDEFINE (DIS, TABLENAME, FIELDNAME, HIDDEN, DLabel, DomainName, DWidth)
  VALUES (0, 'INLISTT', 'AMOUNTTRANS', 0, '运费', 'TAmount', null)
--
2003.03.08_01/SQL3

CREATE GENERATOR INTNO_OutLISTT;
---
CREATE TABLE OutLISTT
(
  INTNO	TINTNO,
  UPDATEACC	TUPDATEFLAG,

  FLAG1	SMALLINT,
  FLAG2	SMALLINT,
  FLAG3	SMALLINT,
  FLAG4	SMALLINT,
  PRINTCOUNT	SMALLINT,
  LISTDATE	TLISTDATE,
  LISTTIME	TLISTTIME,

  LISTNO	TLISTNO,
  CMPYTrans	TCMPYNAME,
  CMPYClient	TCMPYNAME,
  AmountIn	TAMOUNT,
  AmountOut	TAMOUNT,
  AmountTrans	TAMOUNT,
  PERSONTrans	TPERSONNAME,
  PERSONClient	TPERSONNAME,
  DEPARTMENT	TDEPARTMENT,
  CityFrom      TCity,
  CityTo        TCity,
  ShipNo        TShipNo,
  OutListIntNo  TIntNO,
  TransIntNo    TIntNo,
  REMARK	TREMARK,
  OPERNO	TOPERNO,
CONSTRAINT OutLISTT_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT OutLISTT_UNIQUE_1 UNIQUE (LISTNO)
);
---
CREATE INDEX IDX_OutLISTT_LISTDATE ON OutLISTT(LISTDATE);
---
CREATE GENERATOR INTNO_SALEGDST;
---
CREATE TABLE SALEGDST 

(
  INTNO	TINTNO,
  MAININTNO	TINTNO,
  GOODSINTNO	INTEGER,
  UNITID	TGOODSUNITID,
  STOCKINTNO    TINTNO,

  QTY   	TQTY,
  PRICE  	TPRICE,
  DISCOUNT	TDISCOUNT,
  AMOUNT	TAMOUNT,

  InPRICE	TPRICE,
  InAMOUNT	TAMOUNT,

  QTYTRANS 	TQTY,
  PRICETRANS	TPRICE,
  DiscountTrans	TDISCOUNT,
  AMOUNTTRANS	TAMOUNT,

  REMARK	TREMARK,
  UPDATEFLAG	SMALLINT,
  BATCHID	TINTNO,
CONSTRAINT SALEGDST_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_SALEGDST_MAININTNO ON SALEGDST(MAININTNO);
---
ALTER TABLE SALEGDST ADD CONSTRAINT FK_SALEGDST_OUTLISTT FOREIGN KEY (MAININTNO) 
  REFERENCES OUTLISTT (INTNO) 
  ON UPDATE NO ACTION 
  ON DELETE CASCADE;
---
ALTER TABLE SALEGDST ADD CONSTRAINT FK_SALEGDST_STKLST FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO) 
  ON UPDATE CASCADE;
---
ALTER TABLE SALEGDST ADD CONSTRAINT FK_SALEGDST_STKNAME FOREIGN KEY (STOCKINTNO)
  REFERENCES STKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
--
2003.03.08_01/SQL2
CREATE GENERATOR INTNO_INLISTT;
---
CREATE TABLE INLISTT
(
  INTNO	TINTNO,
  UPDATEACC	TUPDATEFLAG,
  FLAG1	SMALLINT,
  FLAG2	SMALLINT,
  FLAG3	SMALLINT,
  FLAG4	SMALLINT,

  PRINTCOUNT	SMALLINT,
  LISTDATE	TLISTDATE,
  LISTTIME	TLISTTIME,
  LISTNO	TLISTNO,

  CMPYVendor	TCMPYNAME,
  CMPYTrans	TCMPYNAME,
  CMPYStore	TCMPYNAME,

  AmountIn	TAMOUNT,
  AmountTrans	TAMOUNT,
  AmountStore	TAMOUNT,

  PERSONVendor	TPERSONNAME,
  PERSONTrans	TPERSONNAME,
  PERSONStore	TPERSONNAME,


  DEPARTMENT	TDEPARTMENT,
  CityFrom      TCity,
  CityTo        TCity,
  ShipNo        TShipNo,
  
  InListIntNo   TIntNo,
  TransIntNo    TIntNo,
  StoreIntNo    TIntNO,
  PanListIntNo  TIntNo,

  REMARK	TREMARK,
  OPERNO	TOPERNO,
CONSTRAINT INLISTT_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT INLISTT_UNIQUE_1 UNIQUE (LISTNO)
);
---
CREATE INDEX IDX_INLISTT_LISTDATE ON INLISTT(LISTDATE);
---
CREATE GENERATOR INTNO_BUYGDST;
---
CREATE TABLE BUYGDST 
(
  INTNO	TINTNO,
  MAININTNO	TINTNO,

  GOODSINTNO	INTEGER,
  UNITID	TGOODSUNITID,
  StockIntNo    TIntNo,

  QTYIN 	TQTY,
  PRICEIN	TPRICE,
  DISCOUNTIN	TDISCOUNT,
  AMOUNTIN	TAMOUNT,

  QTYSTORE	TQTY,
  PRICESTORE	TPRICE,
  DISCOUNTSTORE	TDISCOUNT,
  AMOUNTSTORE	TAMOUNT,

  QTYTRANS 	TQTY,
  PRICETRANS	TPRICE,
  DISCOUNTTRANS	TDISCOUNT,
  AMOUNTTRANS	TAMOUNT,

  REMARK	TREMARK,
  UPDATEFLAG	SMALLINT,
  BATCHID	TINTNO,

CONSTRAINT BUYGDST_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_BUYGDST_MAININTNO ON BUYGDST(MAININTNO);
---
ALTER TABLE BUYGDST ADD CONSTRAINT FK_BUYGDST_INLISTT FOREIGN KEY (MAININTNO)
  REFERENCES INLISTT (INTNO)
  ON UPDATE NO ACTION
  ON DELETE CASCADE;
---
ALTER TABLE BUYGDST ADD CONSTRAINT FK_BUYGDST_STKLST FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO)
  ON UPDATE CASCADE;
---
ALTER TABLE BUYGDST ADD CONSTRAINT FK_BUYGDST_STKNAME FOREIGN KEY (STOCKINTNO)
  REFERENCES STKNAME (INTNO)
  ON UPDATE CASCADE
  ON DELETE SET NULL;
--
2003.03.08_01/SQL1
Create Domain TShipNo VARCHAR(20)
---
CREATE GENERATOR INTNO_DOUTLIST;
---
CREATE TABLE DOutLIST
(
  INTNO	TINTNO,
  UPDATEACC	TUPDATEFLAG,

  FLAG1	SMALLINT,
  FLAG2	SMALLINT,
  FLAG3	SMALLINT,

  FLAG4	SMALLINT,
  PRINTCOUNT	SMALLINT,
  LISTDATE	TLISTDATE,
  LISTTIME	TLISTTIME,
  LISTNO	TLISTNO,
  CMPYVendor	TCMPYNAME,
  CMPYTrans	TCMPYNAME,
  CMPYClient	TCMPYNAME,
  AmountIn	TAMOUNT,
  AmountOut	TAMOUNT,
  AmountTrans	TAMOUNT,
  PERSONVendor	TPERSONNAME,
  PERSONTrans	TPERSONNAME,
  PERSONClient	TPERSONNAME,
  DEPARTMENT	TDEPARTMENT,
  CityFrom      TCity,
  CityTo        TCity,
  ShipNo        TShipNo,
  OutListIntNo  TIntNO,
  InListIntNo   TIntNo,
  TransIntNo    TIntNo,
  REMARK	TREMARK,
  OPERNO	TOPERNO,
CONSTRAINT DOUTLIST_PRIMARY_KEY PRIMARY KEY (INTNO),
CONSTRAINT DOUTLIST_UNIQUE_1 UNIQUE (LISTNO)
);
---
CREATE INDEX IDX_DOUTLIST_LISTDATE ON DOUTLIST(LISTDATE);
---
CREATE GENERATOR INTNO_DSALEGDS;
---
CREATE TABLE DSALEGDS 
(
  INTNO	TINTNO,
  MAININTNO	TINTNO,
  GOODSINTNO	INTEGER,
  UNITID	TGOODSUNITID,

  QTYIN 	TQTY,
  PRICEIN	TPRICE,
  DISCOUNTIN	TDISCOUNT,
  AMOUNTIN	TAMOUNT,

  QTYOUT	TQTY,
  PRICEOUT	TPRICE,
  DISCOUNTOUT	TDISCOUNT,
  AMOUNTOUT	TAMOUNT,

  QTYTRANS 	TQTY,
  PRICETRANS	TPRICE,
  DISCOUNTTRANS	TDISCOUNT,
  AMOUNTTRANS	TAMOUNT,

  REMARK	TREMARK,
  UPDATEFLAG	SMALLINT,
  BATCHID	TINTNO,
CONSTRAINT DSALEGDS_PRIMARY_KEY PRIMARY KEY (INTNO)
);
---
CREATE INDEX IDX_DSALEGDS_MAININTNO ON DSALEGDS(MAININTNO);
---
ALTER TABLE DSALEGDS ADD CONSTRAINT FK_DSALEGDS_DOUTLIST FOREIGN KEY (MAININTNO)
  REFERENCES DOUTLIST (INTNO)
  ON UPDATE NO ACTION
  ON DELETE CASCADE;
---
ALTER TABLE DSALEGDS ADD CONSTRAINT FK_DSALEGDS_STKLST FOREIGN KEY (GOODSINTNO)
  REFERENCES STKLST (INTNO)
  ON UPDATE CASCADE;
--
2003.02.15_02/UPGRADE_DETAIL

1. 调整OutList的Trigger(允许更新PrintCount);
2. 调整InList的Trigger(允许更新PrintCount);
--
2003.02.15_02/SQL1
CREATE TRIGGER OUTLIST_BEFORE_UPDATE_CHK_CLNT FOR OUTLIST BEFORE UPDATE POSITION 0 AS

/*检查单据--客户是否已结帐, 如果已结帐, 则激活Exception*/
DECLARE VARIABLE M_CHANGEABLE SmallInt;
BEGIN
  IF ( (new.UpdateAcc<>old.UpdateAcc)
    or (new.Flag1<>old.Flag1)
    or (new.ListTime<>old.ListTime)
    or (new.CmpyName<>old.CmpyName)
    or (new.Gdspay<>old.Gdspay)
    or (new.Actpay<>old.Actpay)
    or (new.PersonDeal<>old.PersonDeal)
    or (new.Department<>old.Department)
    or (new.Remark<>old.Remark)
    or (new.OperNo<>old.OperNo)

    or (new.Profit<>old.Profit)
    ) THEN

  BEGIN
    EXECUTE PROCEDURE VALID_LIST_CHANGE (0, old.CMPYNAME, old.ListDate);
    IF ((old.CMPYNAME<>new.CMPYNAME) or (old.LISTDATE<>new.LISTDATE)) THEN
      EXECUTE PROCEDURE VALID_LIST_CHANGE (0, new.CMPYNAME, new.ListDate);
  END
END
---
CREATE TRIGGER INLIST_BEFORE_UPDATE_CHK_CLIENT FOR INLIST BEFORE UPDATE POSITION 0 AS
/*检查单据--客户是否已结帐, 如果已结帐, 则激活Exception*/
DECLARE VARIABLE M_CHANGEABLE SmallInt;
BEGIN
  IF ( (new.UpdateAcc<>old.UpdateAcc)
    or (new.Flag1<>old.Flag1)
    or (new.ListTime<>old.ListTime)
    or (new.CmpyName<>old.CmpyName)
    or (new.Gdspay<>old.Gdspay)
    or (new.Actpay<>old.Actpay)
    or (new.PersonDeal<>old.PersonDeal)
    or (new.Department<>old.Department)
    or (new.Remark<>old.Remark)
    or (new.OperNo<>old.OperNo)
    ) THEN
  BEGIN
    EXECUTE PROCEDURE VALID_LIST_CHANGE (1, old.CMPYNAME, old.ListDate);
    IF ((old.CMPYNAME<>new.CMPYNAME) or (old.LISTDATE<>new.LISTDATE)) THEN
      EXECUTE PROCEDURE VALID_LIST_CHANGE (1, new.CMPYNAME, new.ListDate);
  END
END
--
2003.02.15_01_SYS/UPGRADE_DETAIL
1. 调整权限表Oper/Access的Trigger;
--
2003.02.15_01_SYS/SQL1
CREATE TRIGGER CREATE_INTNO_ACCESS FOR ACCESS BEFORE INSERT POSITION 0 AS
BEGIN
  IF ((new.IntNo is NULL) or (new.IntNo = 0)) THEN NEW.INTNO = GEN_ID(INTNO_ACCESS, 1);
END
---
CREATE TRIGGER CREATE_INTNO_OPER FOR OPER BEFORE INSERT POSITION 0 AS
BEGIN
  IF ((new.IntNo is NULL) or (new.IntNo = 0)) THEN NEW.INTNO = GEN_ID(INTNO_OPER, 1);
END
---
DELETE FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME='INTNO_OPERATOR'
--










 
 
 
