Files
Fuchs_Intranet/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions.sql
T
Stefan c98be7b23f
Playwright Tests / test (push) Has been cancelled
Enhance banking transaction data structure and validation
- Increased the length of the NameOfPayer field from NVARCHAR(60) to NVARCHAR(140) in the banking transactions function, table, temporary table, and user-defined type to accommodate longer names.
- Expanded the SepaRemittanceInformation field from VARCHAR(150) to VARCHAR(200) in the banking transactions function for more detailed remittance information.
- Modified the stored procedure fds__setBankingtransaction_done to return additional transaction data (ValueDate and Amount) along with the success flag for improved notification messaging.
- Added validation in MFRClientConfig constructor to ensure the provided URL is not null or empty, enhancing error handling for configuration settings.
2026-07-04 16:37:23 +02:00

88 lines
3.3 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_bankingtransactions]
(
@startdate date = NULL
,@enddate date = NULL
,@invoice_startdate date = NULL
,@invoice_enddate date = NULL
,@invoice_idlist [dbo].[fds__tt__idlist_vchar] READONLY
)
RETURNS @bankingtransactions TABLE
(
[taID] varchar(10)
,[AccountIdentification] varchar(50)
,[ValueDate] date
,[Amount] numeric(9,2)
,[AccountNumberOfPayer] varchar(30)
,[NameOfPayer] nvarchar(140)
,[SepaRemittanceInformation] varchar(200)
,[EndToEndReference] varchar(50)
,[manu] bit
,[InvId] varchar(15)
,[done_manually] bit
,[fds] bit
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
DECLARE @firstever date = '1900-01-01', @tomorrow date = DATEADD(dAy, 1, GETDATE());
SELECT @invoice_startdate = ISNULL(@invoice_startdate, @firstever), @invoice_enddate = ISNULL(@invoice_enddate, @tomorrow);
DECLARE @idlistempty bit = IIF(EXISTS (SELECT 0 FROM @invoice_idlist) ,0,1);
with bt as (
SELECT [taID],[AccountIdentification],[ValueDate],[Amount],AccountNumberOfPayer,NameOfPayer, SepaRemittanceInformation, EndToEndReference
FROM [dbo].[fds__bankingtransactions] as bt_
where (@startdate is null OR bt_.ValueDate >= @startdate)
AND (@enddate is null or bt_.ValueDate <= @enddate)
AND (bt_.[DebitCreditMark] in ('C')
AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE52301502000002091478') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%umbuchung%'))
AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE23501108006161606386') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%ebay%'))
AND NOT (ISNULL(bt_.[SepaRemittanceInformation],'') like ('%Umsatzsteuer%'))
AND ISNULL(bt_.AccountNumberOfPayer,'') <> ''
)
), bs0manu as (
SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(1 as bit)
FROM [dbo].[fds__bankingtransactions_settings] as bs
CROSS APPLY STRING_SPLIT(bs.assigned_invoice_id, ',') as spl
JOIN bt on bt.[taID] = bs.[taID]
WHERE bs.assigned_invoice_id is not null
), bs1 as (
SELECT * FROM bs0manu
UNION
SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(0 as bit)
FROM [dbo].[fds__bankingtransactions_settings] as bs
CROSS APPLY STRING_SPLIT(bs.auto_invoice_id, ',') as spl
JOIN bt on bt.[taID] = bs.[taID]
WHERE bs.auto_invoice_id is not null
and bt.[taID] NOT IN (SELECT [taID] FROM bs0manu)
), bs as (
SELECT [taID], [invid], manu = CAST(MAX(CAST([manu] as int)) as bit)
from bs1
WHERE @idlistempty = 1 OR bs1.[invid] in (SELECT [id] FROM @invoice_idlist)
GROUP BY [taID], [invid]
)
INSERT INTO @bankingtransactions
SELECT
bt.[taID]
,[AccountIdentification]
, bt.[ValueDate]
, [Amount]
,bt.AccountNumberOfPayer
,bt.NameOfPayer
,bt.SepaRemittanceInformation
,bt.EndToEndReference
,[manu]
,[invid]
,[done_manually] = IIF(ISNULL(bs2.[done_manually],'') <> '', 1,0)
,[fds] = IIF(fi.isExternal = 0, 1, 0)
FROM bt
JOIN bs ON bt.[taID] = bs.[taID]
LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[taID] = bs2.[taID]
LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate
RETURN
END