----------1、建立批量促销商品表(DOC44PLU)
----------2、批量审核单据的触发器(DOC44UPDATEVALID)
----------4、建立满减促销主表和明细表(DOC58BF)(DOC58DT)
----------5、满减单据作废的存储过程(BLANKOUT58DOC)
----------6、满减促销主表删除的存储过程(DELETE58DOC)
----------7、满减促销明细表删除的存储过程(DELETE58DOCDT)
----------9、满减促销单失效触发器(DOC58UPDATE)
----------10、满减促销单审核触发器(DOC58UPDATEVALID)
----------11、满减促销单审核存储过程(VALID58DOC)
----------12、组合促销明细表增加组号(DOC43DT)
----------13、修改组合下传的函数(GETMMX43PACKET)
----------14、GETEBKDSC
----------15、GETMMXNO
----------16、修改调价单审核存储过程(VALID24DOC)
----------17、促销单导入存储过程(IMPCX)
----------18、下传视图(VWDOWNMMX)
----------19、下传视图(VWMMXPLU)
----------20、导入DTS_CX表存储过程(DTSPREPARE_CX)
----------21、日结时依次审核促销单据(DAYCLOSE2)
----------1、建立批量促销商品表(DOC44PLU)
IF EXISTS (SELECT * FROM DBO.SYSOBJECTS WHERE ID = OBJECT_ID(N'[DBO].[DOC44PLU]') AND OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1)
DROP TABLE [DBO].[DOC44PLU]
GO
CREATE TABLE [DBO].[DOC44PLU] (
[UID] [INT] IDENTITY (1, 1) NOT NULL ,
[DOCNO] [CHAR] (12) COLLATE CHINESE_PRC_CI_AS NOT NULL ,
[PLUID] [INT] NOT NULL ,
[ZSLPRC] [NUMERIC](12, 2) NULL
) ON [DOCDAT]
GO
ALTER TABLE [DBO].[DOC44PLU] ADD
CONSTRAINT [PK_DOC44PLU] PRIMARY KEY NONCLUSTERED
(
[UID]
) WITH FILLFACTOR = 90 ON [DOCDAT]
GO
ALTER TABLE [DBO].[DOC44PLU] ADD
CONSTRAINT [FK_DOC44DT_REF_18485_DOC44BF] FOREIGN KEY
(
[DOCNO]
) REFERENCES [DBO].[DOC44BF] (
[DOCNO]
),
CONSTRAINT [FK_DOC44DT_REF_18489_BASPLUCR] FOREIGN KEY
(
[PLUID]
) REFERENCES [DBO].[BASPLUCRTC] (
[PLUID]
)
GO
IF NOT EXISTS ( SELECT 1 FROM DICTABLE A WHERE A.CODE = 'DOC44PLU' )
BEGIN
EXEC SYSADDDICTABLE 'DOC44PLU','批量促销商品表', 0
EXEC SYSADDDICCOL 'DOC44PLU','UID','序号','INT','',1,'',''
EXEC SYSADDDICCOL 'DOC44PLU','DOCNO','凭证编号','VARCHAR(20)','',1,'',''
EXEC SYSADDDICCOL 'DOC44PLU','PLUID','商品信息','INT','',1,'',''
EXEC SYSADDDICCOL 'DOC44PLU','ZSLPRC','总部指导价','NUMERIC(12,2)','',1,'',''
END
GO
ALTER TABLE DOC44BF ALTER COLUMN PLUID INT NULL
GO
IF NOT EXISTS(SELECT * FROM SYSOBJECTS A, SYSCOLUMNS B WHERE A.ID=B.ID AND A.NAME='DOC44DT' AND B.NAME='ZSLPRC')
BEGIN
ALTER TABLE DOC44DT ADD ZSLPRC NUMERIC(12,2) NULL
END
GO
----------2、批量审核单据的触发器(DOC44UPDATEVALID)
--批量审核单据的触发器。
ALTER TRIGGER DOC44UPDATEVALID ON DOC44BF FOR UPDATE AS
BEGIN
DECLARE @ERROR INTEGER, @ERRMSG VARCHAR(255)
DECLARE @NOW DATETIME, @DOCNO CHAR(12)
DECLARE @PMNO INTEGER, @DSC VARCHAR(80)
--记录凭证名称
DECLARE @DOCNAME VARCHAR(60)
SELECT @DOCNAME = DBO.GETEBKDSC(4400 * 10000)
SELECT @NOW = DBO.MINDATE(GETDATE())
IF UPDATE(VALID) AND EXISTS(SELECT 1
FROM INSERTED A, DELETED B
WHERE A.DOCNO = B.DOCNO AND A.VALID = 1 AND B.VALID = 0 AND A.STAT = 0
)
BEGIN
SELECT TOP 1 @DSC = '['+PLUNO+']'+PLUNAME
FROM
(
SELECT PLUNO = A.PLUNO, PLUNAME = A.PLUNAME
FROM VWMMXPLU A, INSERTED C, DELETED D
WHERE A.PLUID = C.PLUID AND C.DOCNO = D.DOCNO AND C.VALID = 1 AND D.VALID = 0 AND C.STAT = 0
--当前审核的单据中同一商品促销的有效日期不能和其它的有效期重叠 2001.06.25 冯圣杰
-- AND A.DOCNO <> C.DOCNO AND ( (DBO.MINDATE(A.BDAT) >= DBO.MINDATE(C.BDAT) AND DBO.MINDATE(A.BDAT) <= DBO.MAXDATE(C.EDAT))
-- OR(DBO.MINDATE(C.BDAT) >= DBO.MINDATE(A.BDAT) AND DBO.MINDATE(C.BDAT) <= DBO.MAXDATE(A.EDAT))
-- OR(DBO.MAXDATE(A.EDAT) <= DBO.MAXDATE(C.EDAT))
-- )
--不允许两张预约的促销单据同时包含同一个商品。2001.10.16 冯圣杰
AND A.DOCNO <> C.DOCNO AND C.EDAT >= DBO.MINDATE(GETDATE())
)A
IF @DSC IS NOT NULL
BEGIN
ROLLBACK TRAN
RAISERROR('商品[%S]同时存在两种或两种以上的促销方式,审核失败.', 16, 1, @DSC)
RETURN
END
--先清楚过期促销单据的方案编号
-- UPDATE DOC44BF
-- SET PMNO = 0
-- WHERE PMNO > 1 AND EDAT < @NOW - 2
DECLARE VALIDMMX CURSOR FAST_FORWARD READ_ONLY FOR
SELECT A.DOCNO
FROM INSERTED A, DELETED B
WHERE A.DOCNO = B.DOCNO AND A.VALID = 1 AND B.VALID = 0
ORDER BY A.DOCNO
OPEN VALIDMMX
WHILE 1=1
BEGIN
FETCH NEXT FROM VALIDMMX INTO @DOCNO
IF @@FETCH_STATUS <> 0
BREAK
IF EXISTS(SELECT 1 FROM DOC44BF WHERE DOCNO = @DOCNO AND EDAT < @NOW)
SELECT @PMNO = 0
ELSE
SELECT @PMNO = DBO.GETMMXNO(@NOW)
--清除原被占用的PMNO
UPDATE DOC44BF
SET PMNO = 0
WHERE PMNO = @PMNO
UPDATE BASPLUPRC
SET MMNO = 0
FROM BASPLUPRC A
WHERE A.MMNO = @PMNO
--赋予新的PMNO
UPDATE DOC44BF
SET PMNO = @PMNO
WHERE DOCNO = @DOCNO
UPDATE BASPLUPRC
SET MMNO = @PMNO
FROM BASPLUPRC A, DOC44BF B, DOC44PLU C
WHERE B.DOCNO=C.DOCNO AND A.PLUID = C.PLUID AND B.DOCNO = @DOCNO
END
CLOSE VALIDMMX
DEALLOCATE VALIDMMX
END
END
GO
----------12、组合促销明细表增加组号(DOC43DT)
IF NOT EXISTS (SELECT * FROM sysobjects a, syscolumns b where a.id=b.id and a.name='DOC43DT' and b.name='ISZP')
ALTER TABLE DOC43DT ADD ISZP BIT DEFAULT(0)
GO
IF NOT EXISTS (SELECT * FROM sysobjects a, syscolumns b where a.id=b.id and a.name='DOC43DT' and b.name='GRPID')
ALTER TABLE DOC43DT ADD GRPID VARCHAR(12) DEFAULT('1')
GO
DELETE FROM DICCOLUMN WHERE CODE IN ('ISZP','GRPID') AND TABLEID IN (SELECT TABLEID FROM DICTABLE WHERE CODE='DOC43DT')
EXEC DBO.SYSADDDICCOL 'DOC43DT','ISZP','是否赠品','BIT','',0,'0',''
EXEC DBO.SYSADDDICCOL 'DOC43DT','GRPID','分组标识','VARCHAR(2)','',1,'0','0 分组0
1 分组1
2 分组2
3 分组3
4 分组4
5 分组5
6 分组6
7 分组7
8 分组8
9 分组9'
UPDATE DOC43DT SET ISZP=0,GRPID=0
GO
----------13、修改组合下传的函数(GETMMX43PACKET)
/****** 对象: USERDEFINEDFUNCTION [DBO].[GETMMX43PACKET] 脚本日期: 07/29/2010 13:05:13 ******/
--生成组合下传的包。158 = 13*10 + 2*10 + 8
--SELECT DBO.GETMMX43PACKET('128')
ALTER FUNCTION [DBO].[GETMMX43PACKET](@DOCNO CHAR(12))RETURNS VARCHAR(158)AS
BEGIN
DECLARE @BCDS VARCHAR(130),@PLUNO VARCHAR(13)
DECLARE @QTYS VARCHAR(20)
DECLARE @DISAMTS NUMERIC(8,2)
SELECT @BCDS = '', @QTYS = '', @DISAMTS = 0,@PLUNO=''
IF EXISTS(SELECT 1 FROM DOC43DT WHERE DOCNO = @DOCNO)
BEGIN
DECLARE PACKET CURSOR FAST_FORWARD READ_ONLY FOR
SELECT CAST(B.GRPID AS VARCHAR(1))+RIGHT(REPLICATE(' ', 12)+RIGHT('000000'+A.PLUNO,6),12) AS PLUNO
FROM VWDOWNPLU A, DOC43DT B
WHERE A.PLUID = B.PLUID AND B.DOCNO = @DOCNO
ORDER BY B.GRPID,A.PLUNO
OPEN PACKET
WHILE 1=1
BEGIN
FETCH NEXT FROM PACKET INTO @PLUNO
IF @@FETCH_STATUS <> 0
BREAK
SELECT @BCDS = @BCDS + @PLUNO
END
CLOSE PACKET
DEALLOCATE PACKET
IF NOT EXISTS (SELECT * FROM DOC43DT WHERE DOCNO=@DOCNO AND ISZP=1)
BEGIN
SELECT @DISAMTS = SUM(DISAMT)
FROM(
SELECT DISAMT = MAX(B.DISAMT)
FROM VWDOWNPLU A, DOC43DT B
WHERE A.PLUID = B.PLUID AND B.DOCNO = @DOCNO
GROUP BY B.GRPID
) A
END
SELECT @QTYS=@QTYS+CASE WHEN ISNULL(B.QTY,'00')='00' THEN '00' ELSE B.QTY END
FROM (SELECT 0 AS GRPID
UNION ALL
SELECT 1 AS GRPID
UNION ALL
SELECT 2 AS GRPID
UNION ALL
SELECT 3 AS GRPID
UNION ALL
SELECT 4 AS GRPID
UNION ALL
SELECT 5 AS GRPID
UNION ALL
SELECT 6 AS GRPID
UNION ALL
SELECT 7 AS GRPID
UNION ALL
SELECT 8 AS GRPID
UNION ALL
SELECT 9 AS GRPID
) A LEFT JOIN
( SELECT B.GRPID,
QTY = RIGHT(REPLICATE('0', 2)+ CONVERT(VARCHAR(2), CONVERT(INT, MAX(B.QTY))), 2)
FROM VWDOWNPLU A, DOC43DT B
WHERE A.PLUID = B.PLUID AND B.DOCNO = @DOCNO
GROUP BY B.GRPID
) B ON A.GRPID=B.GRPID
ORDER BY A.GRPID
END
SELECT @BCDS = @BCDS + REPLICATE('0', 13*10), @QTYS = @QTYS + REPLICATE('0', 2*10)
RETURN (LEFT(@BCDS, 13*10)+ LEFT(@QTYS, 2*10) + RIGHT(REPLICATE(' ', 8) + CONVERT(VARCHAR(8), @DISAMTS), 8))
END
GO
----------18、下传视图(VWDOWNMMX)
/****** 对象: VIEW [DBO].[VWDOWNMMX] 脚本日期: 07/26/2010 17:58:41 ******/
ALTER VIEW [DBO].[VWDOWNMMX]
AS
--循环促销
SELECT PMNO, MTYPE, MBDATE, MEDATE, MBTIME, METIME, MCYCLE, MLIMITCNT, M42, M43, M44, DOCNO
FROM
(
SELECT PMNO = A.PMNO,
MTYPE = CASE WHEN A.STAT = 1 OR CONVERT(CHAR(8),EDAT,112) < CONVERT(CHAR(8), GETDATE(), 112) THEN 0 ELSE 1 END,
MBDATE = CONVERT(CHAR(8), A.BDAT, 112),
MEDATE = CONVERT(CHAR(8), A.EDAT, 112),
MBTIME = SUBSTRING(A.BTIME, 1, 2)+':'+SUBSTRING(A.BTIME, 3, 2),
METIME = SUBSTRING(A.ETIME, 1, 2)+':'+SUBSTRING(A.ETIME, 3, 2),
MCYCLE = A.WCYCLE,
MLIMITCNT = A.MAXCNT,
M42 = RIGHT(REPLICATE('0', 2) + CONVERT(VARCHAR(2), CONVERT(INT, A.QTY)), 2) + RIGHT(REPLICATE('0', 8) + CONVERT(VARCHAR(8), A.DISAMT), 8),
M43 = REPLICATE('_', 13*10 + 2*10 + 8),
M44 = REPLICATE('_', 2*10 + 8*10),
DOCNO
FROM DOC42BF A
WHERE A.VALID = 1 AND PMNO > 0
)A
WHERE MTYPE > 0
-- UNION ALL
-- --满减促销
-- SELECT PMNO, MTYPE, MBDATE, MEDATE, MBTIME, METIME, MCYCLE, MLIMITCNT, M42, M43, M44, DOCNO
-- FROM
-- (
-- SELECT PMNO = A.PMNO,
-- MTYPE = CASE WHEN A.STAT = 1 OR CONVERT(CHAR(8),EDAT,112) < CONVERT(CHAR(8), GETDATE(), 112) THEN 0 ELSE 5 END,
-- MBDATE = CONVERT(CHAR(8), A.BDAT, 112),
-- MEDATE = CONVERT(CHAR(8), A.EDAT, 112),
-- MBTIME = SUBSTRING(A.BTIME, 1, 2)+':'+SUBSTRING(A.BTIME, 3, 2),
-- METIME = SUBSTRING(A.ETIME, 1, 2)+':'+SUBSTRING(A.ETIME, 3, 2),
-- MCYCLE = A.WCYCLE,
-- MLIMITCNT = A.MAXCNT,
-- M42 = REPLICATE('_', 2 + 8),
-- M43 = REPLICATE('_', 13*10 + 2*10 + 8),
-- M44 = REPLICATE('_', 2*10)
-- +RIGHT(REPLICATE('0', 8) + CONVERT(VARCHAR(8), CONVERT(VARCHAR(8), A.AMT)), 8) + RIGHT(REPLICATE('0', 8) + CONVERT(VARCHAR(8), A.DISAMT), 8)
-- +REPLICATE('_', 8 * 8),
--
-- DOCNO
-- FROM DOC58BF A
-- WHERE A.VALID = 1 AND PMNO > 0
-- )A
-- WHERE MTYPE > 0
-- --组合促销
-- UNION ALL
-- SELECT PMNO, MTYPE, MBDATE, MEDATE, MBTIME, METIME, MCYCLE, MLIMITCNT, M42, M43, M44, DOCNO
-- FROM
-- (
-- SELECT PMNO = A.PMNO,
-- MTYPE = 6,--CASE WHEN A.STAT = 1 OR CONVERT(CHAR(8),EDAT,112) < CONVERT(CHAR(8), GETDATE(), 112) THEN 0 ELSE 2 END,
-- MBDATE = CONVERT(CHAR(8), A.BDAT, 112),
-- MEDATE = CONVERT(CHAR(8), A.EDAT, 112),
-- MBTIME = SUBSTRING(A.BTIME, 1, 2)+':'+SUBSTRING(A.BTIME, 3, 2),
-- METIME = SUBSTRING(A.ETIME, 1, 2)+':'+SUBSTRING(A.ETIME, 3, 2),
-- MCYCLE = A.WCYCLE,
-- MLIMITCNT = A.MAXCNT,
-- M42 = REPLICATE('_', 2 + 8),
-- M43 = DBO.GETMMX43PACKET(A.DOCNO),
-- M44 = REPLICATE('_', 2*10 + 8*10),
-- DOCNO
-- FROM DOC43BF A
-- WHERE A.VALID = 1 AND PMNO > 0
-- )A
-- WHERE MTYPE > 0
--批量促销
UNION ALL
SELECT PMNO, MTYPE, MBDATE, MEDATE, MBTIME, METIME, MCYCLE, MLIMITCNT, M42, M43, M44, DOCNO
FROM
(
SELECT PMNO = A.PMNO,
MTYPE = CASE WHEN A.STAT = 1 OR CONVERT(CHAR(8),EDAT,112) < CONVERT(CHAR(8), GETDATE(), 112) THEN 0 ELSE 3 END,
MBDATE = CONVERT(CHAR(8), A.BDAT, 112),
MEDATE = CONVERT(CHAR(8), A.EDAT, 112),
MBTIME = SUBSTRING(A.BTIME, 1, 2)+':'+SUBSTRING(A.BTIME, 3, 2),
METIME = SUBSTRING(A.ETIME, 1, 2)+':'+SUBSTRING(A.ETIME, 3, 2),
MCYCLE = A.WCYCLE,
MLIMITCNT = A.MAXCNT,
M42 = REPLICATE('_', 2 + 8),
M43 = REPLICATE('_', 13*10 + 2*10 + 8),
M44 = DBO.GETMMX44PACKET(A.DOCNO),
DOCNO
FROM DOC44BF A
WHERE A.VALID = 1 AND PMNO > 0
)A
WHERE MTYPE > 0
--限时特卖
UNION ALL
SELECT PMNO, MTYPE, MBDATE, MEDATE, MBTIME, METIME, MCYCLE, MLIMITCNT, M42, M43, M44, DOCNO
FROM
(
SELECT PMNO = A.PMNO,
MTYPE = CASE WHEN A.STAT = 1 OR CONVERT(CHAR(8),EDAT,112) < CONVERT(CHAR(8), GETDATE(), 112) THEN 0 ELSE 4 END,
MBDATE = CONVERT(CHAR(8), A.BDAT, 112),
MEDATE = CONVERT(CHAR(8), A.EDAT, 112),
MBTIME = SUBSTRING(A.BTIME, 1, 2)+':'+SUBSTRING(A.BTIME, 3, 2),
METIME = SUBSTRING(A.ETIME, 1, 2)+':'+SUBSTRING(A.ETIME, 3, 2),
MCYCLE = A.WCYCLE,
MLIMITCNT = A.MAXCNT,
M42 = REPLICATE('_', 2 + 8),
M43 = REPLICATE('_', 13*10 + 2*10 + 8),
M44 = REPLICATE('_', 2*10 + 8*10),
DOCNO
FROM DOC45BF A
WHERE A.VALID = 1 AND PMNO > 0
)A
WHERE MTYPE > 0
GO
----------19、下传视图(VWMMXPLU)
ALTER VIEW VWMMXPLU
AS
--循环促销
SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 42, BDAT = A.BDAT, EDAT = A.EDAT
FROM DOC42BF A, DOC42DT B, BASPLUCRTC C
WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
-- --满减促销
-- UNION ALL
-- SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 58, BDAT = A.BDAT, EDAT = A.EDAT
-- FROM DOC58BF A, DOC58DT B, BASPLUCRTC C
-- WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
--组合促销
UNION ALL
SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 43, BDAT = A.BDAT, EDAT = A.EDAT
FROM DOC43BF A, DOC43DT B, BASPLUCRTC C
WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
--批量促销
UNION ALL
SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 44, BDAT = A.BDAT, EDAT = A.EDAT
FROM DOC44BF A, DOC44PLU B, BASPLUCRTC C
WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
--限时促销
UNION ALL
SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 45, BDAT = A.BDAT, EDAT = A.EDAT
FROM DOC45BF A, DOC45DT B, BASPLUCRTC C
WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
--快讯促销
UNION ALL
SELECT C.PLUNO, C.PLUNAME, C.PLUID, A.DOCNO, MTYPE = 47, BDAT = A.BDAT, EDAT = A.EDAT
FROM DOC47BF A, DOC47DT B, BASPLUCRTC C
WHERE A.DOCNO = B.DOCNO AND B.PLUID = C.PLUID AND A.VALID = 1 AND A.STAT = 0 AND A.EDAT >= DBO.MINDATE(GETDATE())
GO
DECLARE @UDICID INT
SELECT @UDICID=UDICID FROM DICUDIC WHERE CODE='basplu'
UPDATE b SET UDICID=@UDICID, UDICDISPLAYTYPE=2
FROM DICTABLE A, DICCOLUMN B
WHERE A.TABLEID=B.TABLEID AND A.CODE='DOC44PLU' AND B.CODE='pluid'
GO