122 lines
18 KiB
Transact-SQL
122 lines
18 KiB
Transact-SQL
/****** Object: StoredProcedure [CDN].[my_ImpDM_Src] Script Date: 29.07.2021 11:19:03 ******/
|
|||
|
|
SET ANSI_NULLS ON
|
||
|
|
GO
|
||
|
|
SET QUOTED_IDENTIFIER ON
|
||
|
|
GO
|
||
|
|
|
||
|
|
/*
|
||
|
|
---- 2019.03.11 - KC: zmiana w treści opisy pozycji noty oraz treści noty: dla zakupu i sprzedaży: kontrahent + opis z pozycji dekretu,
|
||
|
|
dla wewnętrznych - opis z pozycji dekretu. Z opisu pozycji dekretu źródłowego wyciąć numer dokumentu, gdy jest poza nim jakiś opis.
|
||
|
|
2019.03.20 - KC: Powiązanie dokumentów z dekretami poprzez opis zapisu księgowego zamiast opisu pozycji zapisu
|
||
|
|
2019.03.21 - KC: powiązanie dokumentów z dekretami poprzez CDN.Zrodla. Przyspieszenie pobierania danych.
|
||
|
|
2021.07.29 - KC: wybór dokumentów źródłowych wedługf daty magazynowej a nie według pola TrN_RokMiesiac
|
||
|
|
*/
|
||
|
|
ALTER PROCEDURE [CDN].[my_ImpDM_Src]
|
||
|
|
(
|
||
|
|
@SP int, -- 1 - uwzględniać WZ, WZE i ich korekty oraz KK
|
||
|
|
@ZAK int, -- 1 - uwzględniać PZ, PZI i ich korekty
|
||
|
|
@WEW int, -- 1 - uwzględniać RW, PW i ich korekty
|
||
|
|
@RM int, -- TrN_RokMiesiac
|
||
|
|
@Rok int, -- Rok
|
||
|
|
@Miesiac int, -- Miesiąc
|
||
|
|
@Konto varchar(200), -- numer konta
|
||
|
|
@Import int --flaga importu: 0 - niezaimportowane, 1 - zaimportowane, 2 - wszystkie
|
||
|
|
)
|
||
|
|
AS
|
||
|
|
SET NOCOUNT ON;
|
||
|
|
|
||
|
|
set @Konto = @Konto + '%'
|
||
|
|
/*
|
||
|
|
declare @TN table(Dokument varchar(255), TrNNumer int, TrNSeria varchar(10), RokMiesiac int, TrN_GIDTyp int, TrN_GIDNumer int, TrN_Zaksiegowano int, DataDok int, Magazyn varchar(30), Kontrahent varchar(50), NagOpis varchar(80), DokKoryg varchar(40))
|
||
|
|
insert into @TN
|
||
|
|
select CDN.NazwaObiektu(TrN_GIDTyp, TrN_GIDNumer, 0, 2) as Dokument, TrN_TrNNumer, TrN_TrNSeria, TrN_RokMiesiac as RokMiesiac, TrN_GIDTyp, TrN_GIDNumer, TrN_Zaksiegowano, TrN_Data3, isnull(MAG_Kod, ''), isnull(Kontrahent, ''), isnull(Opis, ''), dokkor
|
||
|
|
from (select TrN_GIDTyp, TrN_GIDNumer, TrN_RokMiesiac, TrN_TrNNumer, TrN_TrNSeria, TrN_Zaksiegowano, TrN_Data3, MAG_Kod, Kontrahent = Knt_Akronim, Opis = SUBSTRING(TnO_Opis, 1, 80),
|
||
|
|
case when TrN_GIDTyp in (1624, 1625, 1497, 1498, 2009, 2013) or TrN_VatKorekta > 0 then cdn.NazwaObiektu(TrN_ZwrTyp, TrN_ZwrNumer, 0, 2) else '' end as dokkor
|
||
|
|
from CDN.TraNag with (nolock) left outer join CDN.Magazyny on TrN_MagDNumer = MAG_GIDNumer and TrN_MagDTyp = MAG_GIDTyp
|
||
|
|
left outer join CDN.KntKarty on TrN_KntNumer = Knt_GIDNumer and TrN_KntTyp = Knt_GIDTyp
|
||
|
|
left outer join CDN.TrNOpisy on TrN_GIDNumer = TnO_TrnNumer
|
||
|
|
where TrN_Zaksiegowano > 0 and TrN_RokMiesiac = @RM and
|
||
|
|
( case when @WEW > 0 then case TrN_GIDTyp when 1616 then 1 when 1624 then 1 when 1617 then 1 when 1625 then 1 else 0 end else 0 end > 0 or
|
||
|
|
case when @ZAK > 0 then case TrN_GIDTyp when 1489 then 1 when 1497 then 1 when 1490 then 1 when 1498 then 1 else 0 end else 0 end > 0 or
|
||
|
|
case when @SP > 0 then case TrN_GIDTyp when 2001 then 1 when 2005 then 1 when 2009 then 1 when 2013 then 1 when 2003 then 1 else 0 end else 0 end > 0)) TTT
|
||
|
|
|
||
|
|
declare @DZV table(DZK_Opis varchar(200), DEL_Opis varchar(200), DZK_Rok int, DZK_Miesiac int, DZK_Dzien int, DZK_DokumentZrodlowy varchar(60), DZK_GIDNumer int, DZK_Dziennik varchar(5), CT_Kwota decimal(21,2), DT_Kwota decimal(21,2), DT_DataCentrali int)
|
||
|
|
insert into @DZV
|
||
|
|
select distinct DZK_Opis, DEL_Opis, DZK_Rok, DZK_Miesiac, DZK_Dzien, DZK_DokumentZrodlowy, DZK_GIDNumer, DZK_Dziennik, CT_Kwota = DZK_SumaCT, DT_Kwota = DZK_SumaDT, DT.DT_DataCentrali
|
||
|
|
FROM CDN.Dziennik inner join CDN.DziennikElem on DZK_GIDNumer = DEL_GIDNumer
|
||
|
|
inner join CDN.Dekrety DT on DZK_GIDNumer=DT.DT_GIDNumer AND DT.DT_DC=1
|
||
|
|
INNER JOIN CDN.Dekrety AS CT ON DZK_GIDNumer=CT.DT_GIDNumer AND CT.DT_DC=2
|
||
|
|
where DZK_Rok = @Rok and DZK_Miesiac = @Miesiac
|
||
|
|
and (DT.DT_Konto like case when @Konto > '' then @Konto else DT.DT_Konto end or CT.DT_Konto like case when @Konto > '' then @Konto else CT.DT_Konto end)
|
||
|
|
and (case when isNull(DT.DT_DataCentrali, 0) > 0 then 1 else 0 end = case @Import when 0 then 0 when 1 then 1 end or @Import = 2)
|
||
|
|
|
||
|
|
SELECT Lp = ROW_NUMBER() OVER(ORDER BY TrNSeria, TrNNumer),
|
||
|
|
Dokument,
|
||
|
|
DZK_Opis = LTrim(case when TrN_GIDTyp in (1616, 1624, 1617, 1625)
|
||
|
|
then case when Replace(DZK_Opis, Dokument, '') = '' then Dokument else Replace(DZK_Opis, Dokument, '') end -- PW/RW
|
||
|
|
else Kontrahent + '; ' + case when Replace(DZK_Opis, Dokument, '') = '' then Dokument else Replace(DZK_Opis, Dokument, '') end end), -- reszta
|
||
|
|
RokMiesiac,
|
||
|
|
DataWyst = CDN.TsToDate(CDN.Data(DZK_Rok, DZK_Miesiac, DZK_Dzien), 0),
|
||
|
|
DataOper = CDN.TsToDate(DataDok, 0),
|
||
|
|
DokZr = DZK_DokumentZrodlowy,
|
||
|
|
Dekret = DZK_GIDNumer,
|
||
|
|
DZK_Dziennik,
|
||
|
|
SumaCT = CT_Kwota,
|
||
|
|
SumaDT = DT_Kwota,
|
||
|
|
DataImportu = CDN.TsToDate(coalesce(DT_DataCentrali, 0), 1),
|
||
|
|
Magazyn,
|
||
|
|
Kontrahent,
|
||
|
|
Opis = LTrim(case when Replace(DEL_Opis, Dokument, '') = '' then Dokument else Replace(DEL_Opis, Dokument, '') end),
|
||
|
|
DokKoryg
|
||
|
|
FROM @TN inner join @DZV on substring(DZK_Opis, 1, len(Dokument)) = Dokument
|
||
|
|
order by 1
|
||
|
|
*/
|
||
|
|
|
||
|
|
SELECT Lp = ROW_NUMBER() OVER(ORDER BY TrN_GIDTyp, TrNSeria, TrNNumer),
|
||
|
|
Dokument,
|
||
|
|
DZK_Opis = LTrim(case when TrN_GIDTyp in (1616, 1624, 1617, 1625)
|
||
|
|
then case when Replace(DZK_Opis, Dokument, '') = '' then Dokument else Replace(DZK_Opis, Dokument, '') end -- PW/RW
|
||
|
|
else Kontrahent + '; ' + case when Replace(DZK_Opis, Dokument, '') = '' then Dokument else Replace(DZK_Opis, Dokument, '') end end), -- reszta
|
||
|
|
RokMiesiac,
|
||
|
|
DataWyst = CDN.TsToDate(CDN.Data(DZK_Rok, DZK_Miesiac, DZK_Dzien), 0),
|
||
|
|
DataOper = CDN.TsToDate(DataDok, 0),
|
||
|
|
DokZr = DZK_DokumentZrodlowy,
|
||
|
|
Dekret = DZK_GIDNumer,
|
||
|
|
DZK_Dziennik,
|
||
|
|
SumaCT = CT_Kwota,
|
||
|
|
SumaDT = DT_Kwota,
|
||
|
|
DataImportu = CDN.TsToDate(coalesce(DT_DataCentrali, 0), 1),
|
||
|
|
Magazyn,
|
||
|
|
Kontrahent,
|
||
|
|
Opis = LTrim(case when Replace(DEL_Opis, Dokument, '') = '' then Dokument else Replace(DEL_Opis, Dokument, '') end),
|
||
|
|
DokKoryg
|
||
|
|
from
|
||
|
|
(select distinct DZK_Opis, DEL_Opis, DZK_Rok, DZK_Miesiac, DZK_Dzien, DZK_DokumentZrodlowy, DZK_GIDNumer, DZK_Dziennik, CT_Kwota = DZK_SumaCT, DT_Kwota = DZK_SumaDT, DT.DT_DataCentrali,
|
||
|
|
Dokument = CDN.NazwaObiektu(TrN_GIDTyp, TrN_GIDNumer, 0, 2),
|
||
|
|
TrNNumer = TrN_TrNNumer,
|
||
|
|
TrNSeria = TrN_TrNSeria,
|
||
|
|
RokMiesiac = TrN_RokMiesiac,
|
||
|
|
TrN_GIDTyp, TrN_Zaksiegowano,
|
||
|
|
DataDok = TrN_Data3,
|
||
|
|
Magazyn = isnull(MAG_Kod, TrN_CechaOpis),
|
||
|
|
Kontrahent = isnull(Knt_Akronim, ''),
|
||
|
|
NagOpis = isnull(SUBSTRING(TnO_Opis, 1, 80), ''),
|
||
|
|
DokKoryg = case when TrN_GIDTyp in (1624, 1625, 1497, 1498, 2009, 2013) or TrN_VatKorekta > 0 then cdn.NazwaObiektu(TrN_ZwrTyp, TrN_ZwrNumer, 0, 2) else '' end
|
||
|
|
FROM CDN.Dziennik with (nolock) inner join CDN.DziennikElem with (nolock) on DZK_GIDNumer = DEL_GIDNumer
|
||
|
|
inner join CDN.Dekrety AS DT with (nolock) on DZK_GIDNumer=DT.DT_GIDNumer AND DT.DT_DC=1
|
||
|
|
INNER JOIN CDN.Dekrety AS CT with (nolock) ON DZK_GIDNumer=CT.DT_GIDNumer AND CT.DT_DC=2
|
||
|
|
inner join CDN.Zrodla on DZK_GIDTyp = ZRO_DTTyp and DZK_GIDNumer = ZRO_DTNumer and DZK_GIDLp = ZRO_DTLp
|
||
|
|
inner join CDN.TraNag on TrN_GIDTyp = ZRO_TRNTyp and TrN_GIDNumer = ZRO_TRNNumer and TrN_GIDLp = ZRO_TRNLp
|
||
|
|
left outer join CDN.Magazyny with (nolock) on TrN_MagDNumer = MAG_GIDNumer and TrN_MagDTyp = MAG_GIDTyp
|
||
|
|
left outer join CDN.KntKarty with (nolock) on TrN_KntNumer = Knt_GIDNumer and TrN_KntTyp = Knt_GIDTyp
|
||
|
|
left outer join CDN.TrNOpisy with (nolock) on TrN_GIDNumer = TnO_TrnNumer
|
||
|
|
where DZK_Rok = @Rok and DZK_Miesiac = @Miesiac
|
||
|
|
and (DT.DT_Konto like case when @Konto > '' then @Konto else DT.DT_Konto end or CT.DT_Konto like case when @Konto > '' then @Konto else CT.DT_Konto end)
|
||
|
|
and (case when isNull(DT.DT_DataCentrali, 0) > 0 then 1 else 0 end = case @Import when 0 then 0 when 1 then 1 end or @Import = 2)
|
||
|
|
and Year(CDN.TsToDate(TrN_DataMag, 0)) = @Rok and Month(CDN.TsToDate(TrN_DataMag, 0)) = @Miesiac --and TrN_RokMiesiac = @RM
|
||
|
|
and (case when @WEW > 0 then case TrN_GIDTyp when 1616 then 1 when 1624 then 1 when 1617 then 1 when 1625 then 1 else 0 end else 0 end > 0 or
|
||
|
|
case when @ZAK > 0 then case TrN_GIDTyp when 1489 then 1 when 1497 then 1 when 1490 then 1 when 1498 then 1 else 0 end else 0 end > 0 or
|
||
|
|
case when @SP > 0 then case TrN_GIDTyp when 2001 then 1 when 2005 then 1 when 2009 then 1 when 2013 then 1 when 2003 then 1 else 0 end else 0 end > 0)) TT
|
||
|
|
order by 1
|
||
|
|
|
||
|
|
�
|