Add unit tests for Fuchs_DataService and related components

- Introduced comprehensive unit tests for the Fuchs_DataService library, covering DATEV header formatting, CSV/XML generation, and FdsMfrClient construction.
- Implemented tests for FdsMfr.UpdateNeed parsing and FdsShared utility helpers, ensuring correct functionality and stability.
- Added tests for FdsConfig and FdsMfrClient to validate configuration resolution and client construction.

Document decisions on backend-authoritative invoice and reminder handling

- Created ADR 0008 to clarify that all invoice types and reminder stages are backend-authoritative during drafting and previewing.
- Established that all calculations and settings must be processed server-side, ensuring consistency between online editor and PDF outputs.

Define irreversible mutations for set-price modes in invoices

- Documented ADR 0009 to specify that the "Set mit Preis" and "Nur Set mit Preis" operations are irreversible mutations affecting service request blocks.
- Clarified that these operations are not display toggles but actual data changes, ensuring clear expectations for invoice handling.

Transition MFR ERP sync to in-process execution within the web app

- Created ADR 0010 to outline the migration of Fuchs_DataService from a standalone service to an in-process library within the Fuchs web application.
- Updated configuration and logging management to be handled by the host application, streamlining the sync process.

Add publish profile and periodic hosted service for job scheduling

- Introduced a publish profile for deployment to a specified folder.
- Implemented PeriodicHostedService to manage multiple independent jobs, including the MFR ERP sync, with configurable execution intervals.

Add dotnet-tools.json for EF Core CLI tools

- Included dotnet-tools.json to manage the version of dotnet-ef for Entity Framework Core migrations and commands.
This commit is contained in:
Stefan
2026-07-16 13:34:23 +02:00
parent f724b9b59d
commit 49e3ed2673
102 changed files with 2501 additions and 8438 deletions
-25
View File
@@ -357,31 +357,6 @@
<Build Include="dbo\Stored Procedures\fds__admin_removeconflicttables.sql" />
<Build Include="dbo\Stored Procedures\fds__admin_logdebug.sql" />
<Build Include="dbo\Stored Procedures\fds__admin_getReportCatalog.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__setInvoicePayed.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__setBankingtransaction_autoAssigns.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__r_getBalanceTrendByYear.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__r_getBalanceTrendByMonth.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__r_getBalanceByYearTopMaterial.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__r_getBalanceByYearTopCustomer.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__r_getBalanceByMonth.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__prepStorno_recreate.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__prepReminder.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__lookupReminders.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getRequests_list2.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getRequests_list.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getRequest_details.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getReportDocument.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getReminder.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getInvRequestItems.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getInvoices_list2.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getInvoices_list_vario.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getInvoiceReminder.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getDatevExports.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getBankingtransfers_questionable.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__getBankingtransfers.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__createStorno_simple.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__createReminder.sql" />
<Build Include="dbo\Stored Procedures\backup__fds__createCredit_simple.sql" />
<Build Include="dbo\Stored Procedures\_BackupAndClearInvoiceFile.sql" />
<Build Include="dbo\User Defined Types\json_data_maxU.sql" />
<Build Include="dbo\User Defined Types\hash_256.sql" />
@@ -1,264 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__createCredit_simple]
@InvId varchar(20)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__createCredit_simple' ,@authuser , 'invid: ' + ISNULL(@InvId, ''));
DECLARE @now datetime = GETDATE();
DECLARE @today date = CAST(@now as date);
DECLARE @newid varchar(10) = [dbo].[fds__fn_invoice_id]();
DECLARE @inv_DocumentName varchar(50), @inv_DateFinalized datetime, @inv_vat numeric(5,2), @inv_net numeric(10,3);
IF EXISTS (SELECT 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1)
BEGIN
SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateFinalized], @inv_vat = [InvoiceVAT_1], @inv_net = 1
FROM [dbo].[fds__invoices] WHERE [id] = @InvId
INSERT INTO [dbo].[fds__invoices]
([Id]
,[Version]
,[InvoiceId]
,[InvoiceType]
,[InvoiceTitle]
,[DocumentName]
,[InvoiceBalance]
,[InvoiceBalance_net]
,[InvoiceVAT_net1]
,[InvoiceVAT_1]
,[InvoiceVAT_net2]
,[InvoiceVAT_2]
,[PaymentTerm]
,[DueDate]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus]
,[IsPayed]
,[IsSent]
,[Replaces_InvId]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCancelled]
,[UserCancelled]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified])
SELECT
[Id] = @newid
,[Version] = 0
,[InvoiceId] = NULL
,[InvoiceType] = 'g'
,[InvoiceTitle] = 'Gutschrift'
,[DocumentName] = NULL
,[InvoiceBalance] = (@inv_net * -1) * (1 + 0.01 * ISNULL(@inv_vat,19))
,[InvoiceBalance_net] = (@inv_net * -1)
,[InvoiceVAT_net1] = (@inv_net * -1) * (0.01 * ISNULL(@inv_vat,19))
,[InvoiceVAT_1] = @inv_net
,[InvoiceVAT_net2] = null
,[InvoiceVAT_2] = null
,[PaymentTerm]
,[DueDate] = @today
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus] = NULL
,[IsPayed] = 0
,[IsSent] = 0
,[Replaces_InvId] = @InvId
,[CustomValues]
,[DateSent] = NULL
,[UserSent] = @authuser
,[DateFinalized] = NULL
,[UserFinalized] = NULL
,[DateCancelled] = NULL
,[UserCancelled] = NULL
,[DateCreated] = @now
,[UserCreated] = @authuser
,[DateModified] = @now
,[UserModified] = @authuser
FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1;
END
ELSE IF EXISTS (SELECT 0 FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice'))
BEGIN
SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateOfCreation]
, @inv_vat = CASE WHEN ISNULL([InvoiceBalanceNetto], 0) <> 0 THEN (([InvoiceBalance] / [InvoiceBalanceNetto]) - 1)*100 ELSE (SELECT TOP(1) TRY_CAST([value] as numeric(5,2)) FROM [dbo].[fds__admin_settings] as st where st.[type] = 'defaults' and st.[key] = 'vat') END
, @inv_net = 1 --[InvoiceBalanceNetto]
FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
INSERT INTO [dbo].[fds__invoices]
([Id]
,[Version]
,[InvoiceId]
,[InvoiceType]
,[InvoiceTitle]
,[DocumentName]
,[InvoiceBalance]
,[InvoiceBalance_net]
,[InvoiceVAT_net1]
,[InvoiceVAT_1]
,[InvoiceVAT_net2]
,[InvoiceVAT_2]
,[PaymentTerm]
,[DueDate]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus]
,[IsPayed]
,[IsSent]
,[Replaces_InvId]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCancelled]
,[UserCancelled]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified])
SELECT
[Id] = @newid
,[Version] = 0
,[InvoiceId] = NULL
,[InvoiceType] = 'g'
,[InvoiceTitle] = 'Gutschrift'
,[DocumentName] = NULL
,[InvoiceBalance] = (@inv_net * -1) * (1 + 0.01 * ISNULL(@inv_vat,19))
,[InvoiceBalance_net] = (@inv_net * -1)
,[InvoiceVAT_net1] = (@inv_net * -1) * (0.01 * ISNULL(@inv_vat,19))
,[InvoiceVAT_1] = @inv_vat
,[InvoiceVAT_net2] = NULL
,[InvoiceVAT_2] = NULL
,[PaymentTerm] = '10wd'
,[DueDate] = _i.DueDate
,[CustomerId] = (SELECT TOP(1) [partnerid] from [mfr__*PartnerSet] as ps where ps.[Property] = 'Invoice:Customer' and ps.[EntityId] = _i.[id])
,[SendToAddress] = NULL
,[SendToEmail] = NULL
,[ProvisionPeriod] = null
,[ProvisionLocation] = NULL
,[PaymentStatus] = NULL
,[IsPayed] = 0
,[IsSent] = 0
,[Replaces_InvId] = @InvId
,[CustomValues] = null
,[DateSent] = NULL
,[UserSent] = @authuser
,[DateFinalized] = NULL
,[UserFinalized] = NULL
,[DateCancelled] = NULL
,[UserCancelled] = NULL
,[DateCreated] = @now
,[UserCreated] = @authuser
,[DateModified] = @now
,[UserModified] = @authuser
FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
UPDATE [dbo].[fds__invoices]
SET [SendToAddress] = [dbo].[fds__getCompanyNameAddress]([CustomerId]), [SendToEmail] = [dbo].[fds__getCompanyEmail]([CustomerId])
WHERE [Id] = @newid;
END
-- continue creating storno
DECLARE @inv_srq [dbo].[fds__tt__invoice_servicerequests];
INSERT INTO [dbo].[fds__invoice_servicerequests]
([InvId]
,[mfr__servicerequest]
,[title]
,[value_net]
,[SortOrder])
OUTPUT
inserted.* INTO @inv_srq
SELECT
[InvId] = [id]
,NULL --<mfr__servicerequest, bigint,>
,'Gutschrift'
,(@inv_net * -1)
,0
FROM [dbo].[fds__invoices] WHERE [id] = @newid;
INSERT INTO [dbo].[fds__invoice_items]
([InvId]
,[InvRqId]
,[mfr__item]
,[reference_InvId]
,[Type]
,[Position]
,[Quantity]
,[Text]
,[value]
,[value_total]
,[vat]
,[value_service]
,[det]
,[SortOrder])
SELECT
@newid --<InvId, varchar(10),>
,[id]--<InvRqId, bigint,>
,NULL --<mfr__item, bigint,>
,@InvId --<reference_InvId, varchar(20),>
,'CreditInvoice' --<Type, nvarchar(255),>
,NULL --<Position, varchar(5),>
,'1' ---<Quantity, nvarchar(25),>
,[Text] = '<p>' + 'Gutschrift zu der ' + REPLACE(@inv_DocumentName, '.pdf','') + ' vom ' + FORMAT(@inv_dateFinalized, 'dd.MM.yyyy', 'de-de') + '</p>'
,[value] = (@inv_net * -1)
,[value_total] = (@inv_net * -1)
,[vat] = @inv_vat
,0 --<value_service, numeric(10,3),>
,1 --<det, bit,>
,1 --<SortOrder, tinyint,>)
FROM @inv_srq;
INSERT INTO [dbo].[fds__invoice_details]
([InvId]
,[InvoiceService_net]
,[InvoiceService_VAT]
,[AllocatedTo_InvId]
,[StornoTo_InvId])
VALUES(
@newid
,NULL --[InvoiceService_net]
,NULL --[InvoiceService_VAT]
,NULL --[AllocatedTo_InvId]
,@InvId --[StornoTo_InvId]
);
EXECUTE [dbo].[fds__getInvoice] @newid, @authuser;
END
@@ -1,120 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__createReminder]
@InvId varchar(15)
, @type varchar(3)
, @amount numeric(10,3)
, @amount_payed numeric(10,3)
, @SendToAddress [nvarchar](1000)
, @SendToEmail [nvarchar](255)
, @subject nvarchar(255)
, @text nvarchar(2000)
, @authuser varchar(25)
, @Id varchar(10) OUT
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 2
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__createReminder' ,@authuser , 'invid: ' + ISNULL(@invid, ''));
DECLARE @now datetime = GETUTCDATE();
DECLARE @today date = @now;
DECLARE @newid varchar(10) = [dbo].[fds__fn_reminder_id]();
DECLARE @OUT [dbo].[fds__tt__reminder_core];
DECLARE @CustomerId bigint = dbo.fds__fn_invoice_customerid(@InvId);
INSERT INTO [dbo].[fds__reminder]
([Id]
,[version]
,[InvId]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[type]
,[amount]
,[amount_payed]
,[subject]
,[text]
,[IsSent]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified]
,[file])
OUTPUT inserted.*
INTO @OUT
VALUES
(@newid
,0 --version
,@InvId
,@CustomerId
,@SendToAddress
,@SendToEmail
,@type
,@amount
,@amount_payed
,@subject
,@text
,0 --IsSent, bit,>
,null --<CustomValues, nvarchar(max),>
,NULL --<DateSent, datetime,>
,NULL --[UserSent]
,NULL --<DateFinalized, datetime,>
,NULL --<UserFinalized, varchar(25),>
,@now
,@authuser
,@now
,@authuser
,NULL);
SELECT TOP(1) @Id = [id] FROM @out;
With inv1 as(
select TOP(1) * FROM [dbo].[fds__invoices] WHERE [id] = @InvId
), inv as(
SELECT [Id] = cast([id] as varchar(15))
,[Invoiceid]
,[DateFinalized]
,[InvoiceBalance]
FROM inv1
UNION
SELECT [Id] = cast([id] as varchar(15))
,[Invoiceid]
,[DateFinalized] = [DateOfCreation]
,[InvoiceBalance]
FROM [dbo].[mfr__invoices] as mfri
WHERE [id] = TRY_CAST(@invId as bigint) AND NOT EXISTS( SELECT 0 FROM inv1 ) --will only be used, if id not present in fds__invoices
)
SELECT TOP(1)
o.*
, [InvoiceId] = inv.[InvoiceId]
, [InvoiceDate] = inv.[DateFinalized]
, [hasFile] = CAST ( CASE WHEN o.[file] is null THEN 0 ELSE 1 END as bit)
, [UserNameFinalized] = [dbo].[fis_admin_getUserName_byID](o.[UserFinalized])
, [UserEmailFinalized] = [dbo].[fis_admin_getUserEmail_byID](o.[UserFinalized])
from @out as o
join inv on o.[invid] = inv.[id];
END
@@ -1,263 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__createStorno_simple]
@InvId varchar(20)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__createStorno_simple' ,@authuser , 'invid: ' + ISNULL(@InvId, ''));
DECLARE @now datetime = GETDATE();
DECLARE @today date = CAST(@now as date);
DECLARE @newid varchar(10) = [dbo].[fds__fn_invoice_id]();
DECLARE @inv_DocumentName varchar(50), @inv_DateFinalized datetime, @inv_vat numeric(5,2), @inv_net numeric(10,3);
IF EXISTS (SELECT 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1)
BEGIN
SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateFinalized], @inv_vat = [InvoiceVAT_1], @inv_net = [invoicebalance_net] FROM [dbo].[fds__invoices] WHERE [id] = @InvId
INSERT INTO [dbo].[fds__invoices]
([Id]
,[Version]
,[InvoiceId]
,[InvoiceType]
,[InvoiceTitle]
,[DocumentName]
,[InvoiceBalance]
,[InvoiceBalance_net]
,[InvoiceVAT_net1]
,[InvoiceVAT_1]
,[InvoiceVAT_net2]
,[InvoiceVAT_2]
,[PaymentTerm]
,[DueDate]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus]
,[IsPayed]
,[IsSent]
,[Replaces_InvId]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCancelled]
,[UserCancelled]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified])
SELECT
[Id] = @newid
,[Version] = 0
,[InvoiceId] = NULL
,[InvoiceType] = 'c'
,[InvoiceTitle] = 'Stornorechnung'
,[DocumentName] = NULL
,[InvoiceBalance] = ([InvoiceBalance] * -1)
,[InvoiceBalance_net] = ([InvoiceBalance_net] *-1)
,[InvoiceVAT_net1] = ([InvoiceVAT_net1] * -1)
,[InvoiceVAT_1]
,[InvoiceVAT_net2] = ([InvoiceVAT_net2] * -1)
,[InvoiceVAT_2]
,[PaymentTerm]
,[DueDate] = @today
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus] = NULL
,[IsPayed] = 0
,[IsSent] = 0
,[Replaces_InvId] = @InvId
,[CustomValues]
,[DateSent] = NULL
,[UserSent] = NULL
,[DateFinalized] = NULL
,[UserFinalized] = NULL
,[DateCancelled] = NULL
,[UserCancelled] = NULL
,[DateCreated] = @now
,[UserCreated] = @authuser
,[DateModified] = @now
,[UserModified] = @authuser
FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1;
END
ELSE IF EXISTS (SELECT 0 FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice'))
BEGIN
SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateOfCreation]
, @inv_vat = CASE WHEN ISNULL([InvoiceBalanceNetto], 0) <> 0 THEN (([InvoiceBalance] / [InvoiceBalanceNetto]) - 1)*100 ELSE (SELECT TOP(1) TRY_CAST([value] as numeric(5,2)) FROM [dbo].[fds__admin_settings] as st where st.[type] = 'defaults' and st.[key] = 'vat') END
, @inv_net = [InvoiceBalanceNetto]
FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
INSERT INTO [dbo].[fds__invoices]
([Id]
,[Version]
,[InvoiceId]
,[InvoiceType]
,[InvoiceTitle]
,[DocumentName]
,[InvoiceBalance]
,[InvoiceBalance_net]
,[InvoiceVAT_net1]
,[InvoiceVAT_1]
,[InvoiceVAT_net2]
,[InvoiceVAT_2]
,[PaymentTerm]
,[DueDate]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[ProvisionPeriod]
,[ProvisionLocation]
,[PaymentStatus]
,[IsPayed]
,[IsSent]
,[Replaces_InvId]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCancelled]
,[UserCancelled]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified])
SELECT
[Id] = @newid
,[Version] = 0
,[InvoiceId] = NULL
,[InvoiceType] = 'c'
,[InvoiceTitle] = 'Stornorechnung'
,[DocumentName] = NULL
,[InvoiceBalance] = ([InvoiceBalance] * -1)
,[InvoiceBalance_net] = ([InvoiceBalanceNetto] *-1)
,[InvoiceVAT_net1] = ((ISNULL([InvoiceBalance],0) - ISNULL([InvoiceBalanceNetto],0)) * -1)
,[InvoiceVAT_1] = @inv_vat
,[InvoiceVAT_net2] = NULL
,[InvoiceVAT_2] = NULL
,[PaymentTerm] = '10wd'
,[DueDate] = _i.DueDate
,[CustomerId] = (SELECT TOP(1) [partnerid] from [mfr__*PartnerSet] as ps where ps.[Property] = 'Invoice:Customer' and ps.[EntityId] = _i.[id])
,[SendToAddress] = NULL
,[SendToEmail] = NULL
,[ProvisionPeriod] = null
,[ProvisionLocation] = NULL
,[PaymentStatus] = NULL
,[IsPayed] = 0
,[IsSent] = 0
,[Replaces_InvId] = @InvId
,[CustomValues] = null
,[DateSent] = NULL
,[UserSent] = NULL
,[DateFinalized] = NULL
,[UserFinalized] = NULL
,[DateCancelled] = NULL
,[UserCancelled] = NULL
,[DateCreated] = @now
,[UserCreated] = @authuser
,[DateModified] = @now
,[UserModified] = @authuser
FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
UPDATE [dbo].[fds__invoices]
SET [SendToAddress] = [dbo].[fds__getCompanyNameAddress]([CustomerId]), [SendToEmail] = [dbo].[fds__getCompanyEmail]([CustomerId])
WHERE [Id] = @newid;
END
-- continue creating storno
DECLARE @inv_srq [dbo].[fds__tt__invoice_servicerequests];
INSERT INTO [dbo].[fds__invoice_servicerequests]
([InvId]
,[mfr__servicerequest]
,[title]
,[value_net]
,[SortOrder])
OUTPUT
inserted.* INTO @inv_srq
SELECT TOP(1)
[InvId] = [id]
,NULL --<mfr__servicerequest, bigint,>
,'Storno'
,0
,0
FROM [dbo].[fds__invoices] WHERE [id] = @newid;
INSERT INTO [dbo].[fds__invoice_items]
([InvId]
,[InvRqId]
,[mfr__item]
,[reference_InvId]
,[Type]
,[Position]
,[Quantity]
,[Text]
,[value]
,[value_total]
,[vat]
,[value_service]
,[det]
,[SortOrder])
SELECT
@newid --<InvId, varchar(10),>
,[id]--<InvRqId, bigint,>
,NULL --<mfr__item, bigint,>
,@InvId --<reference_InvId, varchar(20),>
,'CanceledInvoice' --<Type, nvarchar(255),>
,NULL --<Position, varchar(5),>
,'1' ---<Quantity, nvarchar(25),>
,[Text] = '<p>' + 'Storno der ' + REPLACE(@inv_DocumentName, '.pdf','') + ' vom ' + FORMAT(@inv_dateFinalized, 'dd.MM.yyyy', 'de-de') + '</p>'
,[value] = (@inv_net * -1)
,[value_total] = (@inv_net * -1)
,[vat] = @inv_vat
,0 --<value_service, numeric(10,3),>
,1 --<det, bit,>
,1 --<SortOrder, tinyint,>)
FROM @inv_srq;
INSERT INTO [dbo].[fds__invoice_details]
([InvId]
,[InvoiceService_net]
,[InvoiceService_VAT]
,[AllocatedTo_InvId]
,[StornoTo_InvId])
VALUES(
@newid
,NULL --[InvoiceService_net]
,NULL --[InvoiceService_VAT]
,NULL --[AllocatedTo_InvId]
,@InvId --[StornoTo_InvId]
);
EXECUTE [dbo].[fds__getInvoice] @newid, @authuser;
END
@@ -1,51 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getBankingtransfers]
@mode varchar(1) = 'm'
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_bam', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getBankingtransfers_questionable' ,@authuser , '');
DECLARE @startdate date, @enddate date, @today date = GETDATE();
DECLARE @true bit = 1, @false bit = 0;
SET @mode = LOWER(@mode);
DECLARE @invidlist [dbo].[fds__tt__idlist_vchar];
--output admin
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode, [title] = 'Zahlungen (neueste zuerst)'
,[note] = 'Letzes Buchungsdatum: ' + FORMAT((SELECT MAX([valuedate]) from [fds__bankingtransactions]), 'dd.MM.yy');
select
ic.[taID]
--inv.InvoiceId,
--[IsPayed],
, ic.fds
, InvoiceId = ISNULL(fi.[invoiceid], mi.[invoiceid])
, [ValueDate] = FORMAT(ic.[ValueDate], 'dd.MM.yy')
, [InvoiceBalance] = FORMAT((ISNULL(fi.[InvoiceBalance],0.0) + ISNULL(mi.[InvoiceBalance],0.0)), '0.00€', 'de')
, [Amount] = FORMAT(ic.[amount], '0.00€', 'de')
, [Skonto] = CAST(CASE WHEN ic.[amount] between (0.965 * (ISNULL(fi.[InvoiceBalance],0.0) + ISNULL(mi.[InvoiceBalance],0.0))) and (0.975 * (ISNULL(fi.[InvoiceBalance],0.0) + ISNULL(mi.[InvoiceBalance],0.0))) THEN 1 ELSE 0 END as bit)
, [Deviation] = FORMAT(CASE WHEN ISNULL((ISNULL(fi.[InvoiceBalance],0.0) + ISNULL(mi.[InvoiceBalance],0.0)),0) > 9 THEN ic.[amount] / (ISNULL(fi.[InvoiceBalance],0.0) + ISNULL(mi.[InvoiceBalance],0.0)) ELSE NULL END , '0.00%')
--, ic.*
,ic.AccountNumberOfPayer
,ic.NameOfPayer
,ic.SepaRemittanceInformation
,ic.EndToEndReference
, [order] = ROW_NUMBER() OVER (ORDER BY [valuedate] DESC)
FROM
[dbo].[fds__fn_bankingtransactions](null, null, @startdate, @enddate, @invidlist) as ic
LEFT JOIN [dbo].[fds__invoices] as fi on ic.[fds] = 1 and ic.[invid] = fi.id
LEFT JOIN [dbo].[mfr__invoices] as mi on ic.[fds] = 0 and ic.[invid] = mi.id
END
@@ -1,60 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getBankingtransfers_questionable]
@mode varchar(1) = 'm'
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_bam', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getBankingtransfers_questionable' ,@authuser , '');
DECLARE @startdate date, @enddate date, @today date = GETDATE();
DECLARE @true bit = 1, @false bit = 0;
SET @mode = LOWER(@mode);
--output admin
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode, [title] = 'Auffällige Zahlungen (neueste zuerst)'
,[note] = 'Letzes Buchungsdatum: ' + FORMAT((SELECT MAX([valuedate]) from [fds__bankingtransactions]), 'dd.MM.yy');
with inv as (
SELECT [fds] = CAST(1 as bit), [InvId] = CAST([Id] as varchar(25)), [InvoiceId], [InvoiceBalance] = CAST([InvoiceBalance] as numeric(10,2)), [ispayed] FROM [dbo].[fds__invoices] as i
UNION
SELECT [fds] = CAST(0 as bit), [InvId] = CAST([Id] as varchar(25)), [InvoiceId], [InvoiceBalance] = CAST([InvoiceBalance] as numeric(10,2)), [ispayed] = CAST(CASE WHEN [InvoiceState] = 'eIsCancelled' THEN 1 WHEN [InvoiceState] = 'eIsPaid' THEN 1 ELSE 0 END as bit) FROM [dbo].[mfr__invoices] as i
)
select
ic.[taID]
--inv.InvoiceId,
--[IsPayed],
, inv.fds
, ic.InvoiceId
, [ValueDate] = FORMAT(ic.[ValueDate], 'dd.MM.yy')
, [InvoiceBalance] = FORMAT(inv.[InvoiceBalance], '0.00€', 'de') + ISNULL( CASE WHEN id2.[invid] is not null then (SELECT TOP(1) FORMAT(inv2.[InvoiceBalance], '0.00€', 'de') FROM [dbo].[fds__invoices] as inv2 where inv2.Id = id2.[invid]) ELSE '' END ,'')
, [Amount] = FORMAT(ic.[amount], '0.00€', 'de')
, [Skonto] = CAST(CASE WHEN ic.[amount] between (0.965 * inv.[InvoiceBalance]) and (0.975 * inv.[InvoiceBalance]) THEN 1 ELSE 0 END as bit)
, [Deviation] = FORMAT(CASE WHEN ISNULL(inv.[InvoiceBalance],0) > 9 THEN ic.[amount] / inv.[InvoiceBalance] ELSE NULL END , '0.00%')
--, ic.*
,ic.AccountNumberOfPayer
,ic.NameOfPayer
,ic.SepaRemittanceInformation
,ic.EndToEndReference
, [order] = ROW_NUMBER() OVER (ORDER BY [valuedate] DESC)
FROM
[dbo].[fds__getInvoiceCredits] (null, null) as ic
LEFT JOIN inv on inv.[InvoiceId] = ic.[invoiceid]
LEFT JOIN [dbo].[fds__invoice_details] as id2 ON inv.InvId = id2.[stornoto_invid]
WHERE
((ISNULL(inv.ispayed,0) = 0 and fds = 1) OR
ic.[amount] not between (inv.InvoiceBalance - 0.99) and (inv.InvoiceBalance + 0.99) OR
ic.invoiceid is null oR
inv.invoiceid is null
) AND NOT EXISTS (SELECT 0 FROM [dbo].[fds__bankingtransactions_settings] as bs WHERE bs.[taID] = ic.[taID] AND (ISNULL(bs.[done_manually],'') <> '' OR ISNULL(bs.[assigned_invoice_id],'') <> ''))
END
@@ -1,634 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getDatevExports]
@tgtdate date
,@mode varchar(1) = 'm'
,@files bit
,@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
DECLARE @startdate date, @enddate date;
SET @mode = LOWER(@mode);
If @mode = 'm'
BEGIN
SELECT @startdate = [dbo].[date_monthfirst](@tgtdate)
,@enddate = [dbo].[date_monthend](@tgtdate);
END
ELSE If @mode = 'w'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst](@tgtdate)
,@enddate = [dbo].[date_weekend](@tgtdate);
END
DECLARE @contraaccounts as table([account] varchar(10), [vat] numeric(5,2));
insert into @contraaccounts
SELECT [account] = [key], [vat] = TRY_PARSE([value2] as numeric(5,2) USING 'en-US') FROM [dbo].[fds__admin_settings] WHERE [type] = 'contra_account'
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode
, [beraternummer] = 11653
, [mandantennummer] = 62321
, [WJ-Beginn] = DATEFROMPARTS(YEAR(@tgtDate), 1, 1)
, [Sachkontenlänge] = ISNULL((SELECT MAX(LEN([account])) FROM @contraaccounts), 4)
;
DECLARE @inv TABLE([Id] varchar(20), [InvoiceID] varchar(255), [URI] nvarchar(255), [DocumentName] nvarchar(255), [DateOfCreation] date, [fds] bit, [file_guid] varchar(36));
--INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0 FROM [dbo].[mfr__reports] where [IsInvoice] = 1 AND CAST([DateOfCreation] as date) between @startdate AND @enddate;
INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0, [file_guid] FROM [dbo].[mfr__invoices] where CAST([DateOfCreation] as date) between @startdate AND @enddate and [FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview','') ;
INSERT INTO @inv SELECT [Id],[invoiceid],[URI] = null,[DocumentName],[DateOfCreation] = [DateCreated],[fds] = 1, [file_guid] FROM [dbo].[fds__invoices] where CAST([DateCreated] as date) between @startdate AND @enddate and [isfinal] = 1;
SELECT
inv.[Id],inv.[InvoiceID],inv.[URI],inv.[DocumentName],inv.[fds]
, [file] = CASE WHEN @files = 1 then finv.[file] ELSE NULL END
, inv.[file_guid], [DateOfCreation]
--,[order] = ROW_NUMBER() OVER (ORDER BY [DateOfCreation] ASC)
FROM @inv as inv LEFT JOIN [dbo].[fds__invoices] as finv on inv.[id] = finv.[id];
DECLARE @SReq TABLE ([id] bigint, [DateOfCreation] datetime, [name] nvarchar(255), [invoice.Id] varchar(15), [customer.ExternalId] bigint, [customer.Id] bigint, [ExternalId] nvarchar(255), [fds] bit);
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[mfr__servicerequests] as s JOIN @inv as i on s.[InvoiceId] like ('%' + i.[InvoiceId] + '%') LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 0;
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[fds__invoice_servicerequests] as irq
JOIN @inv as i on irq.[InvId] = i.[Id]
LEFT JOIN [dbo].[mfr__servicerequests] as s ON irq.[mfr__servicerequest] = s.[id]
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 1;
With itm as (
SELECT
[net_itemsum] = CAST( SUM((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL(vat.[vat], 19.0) * 0.01))
, sreq.[invoice.Id]
,[fds] = 0
FROM [dbo].[mfr__items] as itm
JOIN @sreq as sreq ON itm.[ServiceRequestId] = SReq.[id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
WHERE [fds] = 0
GROUP BY sreq.[invoice.Id]
UNION
SELECT
[net_itemsum] = CAST( SUM(ISNULL([value_total],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM(ISNULL([value_total],0) * (1 + (ISNULL([vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL([vat], 19.0) * 0.01))
, [invoice.Id] = inv.[id] --sreq.[invoice.Id]
, [fds] = 1
FROM [dbo].[fds__invoice_items] as itm
--JOIN @sreq as sreq ON itm.[InvRqId] = SReq.[id]
JOIN @inv as inv ON itm.[invid] = inv.[id] and [fds] = 1
WHERE [fds] = 1
GROUP BY inv.[id] --sreq.[invoice.Id]
),sc as(
SELECT s.[invoice.Id], [fds], [#] = COUNT(s.[id]) FROM @SReq as s GROUP BY s.[invoice.Id], [fds]
), inv as(
--select
-- [Id] = CAST(iv.[Id] as varchar(20))
-- ,[InvoiceId] = iv.[InvoiceId]
-- ,[requestcount] = sc.[#]
-- ,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
-- ,[balance] = ABS([invoiceBalance])
-- ,[EINZELPOS_brutto] = itm.bo_itemsum
-- ,[EINZELPOS_netto] = itm.net_itemsum
-- ,[vf]
-- ,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
-- ,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
-- ,[contra_account] = ca.[account]
-- ,[Belegdatum] = iv.[DateOfCreation]
-- ,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
-- ,iv.[DateOfCreation]
-- from [dbo].[mfr__reports] as iv
-- LEFT JOIN itm on iv.Id = itm.[invoice.Id]
-- LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
-- LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
-- LEFT JOIN contra_accounts as ca ON ISNULL(vat.[vat], 19.0) = ca.[vat]
-- where iv.Id in (SELECT [Id] FROM @inv Where fds = 0)
--UNION
select
[Id] = CAST(iv.[Id] as varchar(20))
,[InvoiceId] = iv.[InvoiceId]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
,[balance] = ABS([invoiceBalance])
,[EINZELPOS_brutto] = itm.bo_itemsum
,[EINZELPOS_netto] = itm.net_itemsum
,[vf]
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
,[contra_account] = ca.[account]
,[Belegdatum] = iv.[DateOfCreation]
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
,iv.[DateOfCreation]
,[file_guid]
from [dbo].[mfr__invoices] as iv
LEFT JOIN itm on iv.Id = itm.[invoice.Id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
LEFT JOIN @contraaccounts as ca ON ISNULL(vat.[vat], ISNULL([dbo].[fds__fn_vatfrombalances]([invoiceBalance], [invoiceBalancenetto]),19.0)) = ca.[vat]
where iv.Id in (SELECT [Id] FROM @inv Where fds = 0)
UNION
select
iv.[Id]
,[InvoiceId] = iv.[InvoiceId]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
,[balance] = ABS([invoiceBalance])
,[EINZELPOS_brutto] = itm.bo_itemsum
,[EINZELPOS_netto] = itm.net_itemsum
,[vf]
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
,[customer] = (SELECT TOP(1) [ExternalId] FROM [dbo].[mfr__companies] as c WHERE c.[Id] = iv.[CustomerId])
,[contra_account] = ca.[account]
,[Belegdatum] = iv.[DateCreated]
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
,[DateOfCreation] = iv.[DateCreated]
,[file_guid]
from [dbo].[fds__invoices] as iv
LEFT JOIN itm on iv.Id = itm.[invoice.Id]
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 1
LEFT JOIN @contraaccounts as ca ON ISNULL(iv.[InvoiceVAT_1], 19.0) = ca.[vat]
where iv.Id in (SELECT [Id] FROM @inv Where fds = 1)
)
select
[Umsatz (ohne Soll/Haben-Kz)] = ABS([balance])--CAST(FORMAT(ABS([balance]), '0.00', 'de') as varchar(13))
--,[EINZELPOS_brutto]
--,[EINZELPOS_netto]
--,vf
,[Soll/Haben-Kennzeichen] = [dbo].[ott_quote_255](UPPER([deb_cred]))
,[WKZ Umsatz] = [dbo].[ott_quote_255]('')
,[Kurs] = NULL
,[Basis-Umsatz] = NULL
,[WKZ Basis-Umsatz] = [dbo].[ott_quote_255]('')
,[Konto] = CAST(iv.[customer] as bigint) --CAST(iv.[customer] as varchar(9)) --(SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] ORDER BY SReq.DateOfCreation ASC)
,[Gegenkonto (ohne BU-Schlüssel)] = CAST([contra_account] as bigint )--CAST([contra_account] as varchar(9))]
,[BU-Schlüssel] = [dbo].[ott_quote_255]('')
,[Belegdatum] = FORMAT(iv.[Belegdatum], 'ddMM')
,[Belegfeld 1] = [dbo].[ott_quote_255](iv.[InvoiceId])
,[Belegfeld 2] = [dbo].[ott_quote_255]([dbo].[strings_removeLeading0_255](FORMAT(iv.[Belegdatum], 'ddMMyy')))
,[Skonto] = [dbo].[ott_quote_255]('')
,[Buchungstext] = [dbo].[ott_quote_255](CAST(ISNULL(LEFT( TRIM(CAST((SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id]) as varchar(255)) ), 60) ,'') as varchar(60)))
,[Postensperre] = NULL
,[Diverse Adressnummer] = [dbo].[ott_quote_255]('')
,[Geschäftspartnerbank] = null
,[Sachverhalt] = null
,[Zinssperre] = null
,[Beleglink] = [dbo].[ott_quote_255](CASE WHEN [file_guid] is null THEN NULL
ELSE CAST( 'BEDI "' + LOWER(CAST([file_guid] as varchar(36))) + '"' as varchar(50))
end)
--,[order] = ROW_NUMBER() OVER (ORDER BY iv.[DateOfCreation] ASC)]
,[Beleginfo - Art 1] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 1] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 2] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 2] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 3] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 3] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 4] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 4] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 5] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 5] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 6] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 6] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 7] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 7] = [dbo].[ott_quote_255]('')
,[Beleginfo - Art 8] = [dbo].[ott_quote_255]('')
,[Beleginfo - Inhalt 8] = [dbo].[ott_quote_255]('')
,[KOST1 - Kostenstelle] = [dbo].[ott_quote_255]('')
,[KOST2 - Kostenstelle] = [dbo].[ott_quote_255]('')
,[Kost-Menge] = null
,[EU-Land u. UStID (Bestimmung)] = [dbo].[ott_quote_255]('')
,[EU-Steuersatz (Bestimmung)] = null
,[Abw. Versteuerungsart] = [dbo].[ott_quote_255]('')
,[Sachverhalt L+L] = null
,[Funktionsergänzung L+L] = null
,[BU 49 Hauptfunktionstyp] = null
,[BU 49 Hauptfunktionsnummer] = null
,[BU 49 Funktionsergänzung] = null
,[Zusatzinformation - Art 1] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 1] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 2] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 2] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 3] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 3] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 4] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 4] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 5] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 5] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 6] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 6] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 7] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 7] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 8] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 8] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 9] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 9] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 10] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 10] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 11] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 11] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 12] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 12] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 13] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 13] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 14] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 14] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 15] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 15] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 16] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 16] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 17] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 17] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 18] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 18] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 19] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 19] = [dbo].[ott_quote_255]('')
,[Zusatzinformation - Art 20] = [dbo].[ott_quote_255]('')
,[Zusatzinformation- Inhalt 20] = [dbo].[ott_quote_255]('')
,[Stück] = null
,[Gewicht] = null
,[Zahlweise] = null
,[Forderungsart] = [dbo].[ott_quote_255]('')
,[Veranlagungsjahr] = null
,[Zugeordnete Fälligkeit] = null
,[Skontotyp] = null
,[Auftragsnummer] = [dbo].[ott_quote_255]('')
,[Buchungstyp] = [dbo].[ott_quote_255]('')
,[USt-Schlüssel (Anzahlungen)] = null
,[EU-Land (Anzahlungen)] = [dbo].[ott_quote_255]('')
,[Sachverhalt L+L (Anzahlungen)] = null
,[EU-Steuersatz (Anzahlungen)] = null
,[Erlöskonto (Anzahlungen)] = null
,[Herkunft-Kz] = [dbo].[ott_quote_255]('')
,[Buchungs GUID] = [dbo].[ott_quote_255]('')
,[KOST-Datum] = null
,[Mandatsreferenz] = [dbo].[ott_quote_255]('')
,[Skontosperre] = null
,[Gesellschaftername] = [dbo].[ott_quote_255]('')
,[Beteiligtennummer] = null
,[Identifikationsnummer] = [dbo].[ott_quote_255]('')
,[Zeichnernummer] = [dbo].[ott_quote_255]('')
,[Postensperre bis] = null
,[Bezeichnung SoBil-Sachverhalt] = [dbo].[ott_quote_255]('')
,[Kennzeichen SoBil-Buchung] = [dbo].[ott_quote_255]('')
,[Festschreibung] = 1
,[Leistungsdatum] = null
,[Datum Zuord. Steuerperiode] = null
,[Fälligkeit] = null
,[Generalumkehr (GU)] = 0
,[Steuersatz] = null
,[Land] = null
--,[Abrechnungsreferenz]
--,[BVV-Position]
--,[EU-Land u. UStID (Ursprung)]
--,[EU-Steuersatz (Ursprung)]
from inv as iv
order by TRY_CONVERT(integer, SUBSTRING(iv.[InvoiceId], 2,4)), TRY_CONVERT(integer, RIGHT(iv.[InvoiceId], 4)), iv.[DateOfCreation] ASC;
WIth debs as (
SELECT ROW_NUMBER() OVER (PARTITION BY [customer.Id] ORDER BY SReq.DateOfCreation ASC) as [#]
, [customer.ExternalId]
, [customer.Id]
FROM @SReq as sreq
), dd as (
SELECT distinct cy.* FROM [mfr__companies] as cy WHERE EXISTS ( SELECT * FROM debs WHERE debs.[customer.Id] = cy.[id] AND debs.[#] = 1)
), cc as (
SELECT ROW_NUMBER() OVER (PARTITION BY dd.[id] ORDER BY CASE WHEN dd.[MainContactId] = c.[id] THEN 0 ELSE 1 END) as '#'
, c.*
, [company_id] = dd.[id]
, [company_name] = dd.[Name]
, [isCompany] = CAST (
CASE WHEN ( not dd.[name] like '%c[/]o %'
AND (
dd.[Name] like '%mbH%'
OR dd.[Name] like '%Ltd%'
OR dd.[Name] like '% gbr%' OR dd.[Name] like 'gbr %'
OR dd.[Name] like '% OHG%'
OR (REPLACE(dd.[Name], '|' , ' ') + ' ') like '% AG %'
OR (REPLACE(dd.[Name], '|' , ' ') + ' ') like '% KG %'
OR (REPLACE(dd.[Name], '|' , ' ') + ' ') like '% SA %'
OR (REPLACE(dd.[Name], '|' , ' ') + ' ') like '% UG %'
OR dd.[Name] like '% eV%' or dd.[Name] like '%e[.]V[.]%'
OR dd.[Name] like '%Gesellschaft%'
OR dd.[Name] like '%Gemeinde%'
OR dd.[Name] like '%Verwaltung%'
OR dd.[Name] like '%Stadtwerke%'
OR dd.[Name] like '%tagesstätte%'
OR dd.[Name] like '%schützenverein%'
OR dd.[Name] like '%akademie%'
OR dd.[Name] like '%universität%'
OR dd.[Name] like '%schule%'
OR dd.[Name] like '%spedition%'
OR dd.[Name] like '%immobilien%'
OR dd.[Name] like '%krankenkasse%'
OR dd.[Name] like '% holding %'
OR dd.[Name] like '% Bautenschutz %'
OR dd.[Name] like '%dienstleistung%'
OR dd.[Name] like '%weinlokal%'
OR dd.[Name] like '%restaurant%'
OR dd.[Name] like '% hotel%'
OR dd.[Name] like '%kanzlei%'
OR dd.[Name] like '%praxis%'
OR dd.[Name] like '%großhandel%'
OR dd.[Name] like '%tischlerei%'
OR dd.[Name] like '%haustechnik%'
OR dd.[Name] like '%holztechnik%'
OR dd.[Name] like '%heizungsbau%'
OR dd.[Name] like '%bautenschutz%'
OR dd.[Name] like '%tischlerei%'
OR dd.[Name] like '%architekt%'
OR dd.[Name] like '%sanitär%'
OR dd.[Name] like '%pafümerie%'
OR dd.[Name] like '%bauamt%'
OR dd.[Name] like '%gebäudereinigung%'
OR dd.[Name] like '%handwerksmeister%'
OR dd.[Name] like '%fachbetrieb%'
OR dd.[Name] like '%ausbau und montagen%'
OR dd.[Name] like '%verband %'
OR dd.[Name] like '%vermietung%'
OR dd.[Name] like '%Bad [&] Heizung%'
OR dd.[Name] like '%Bad[&]Wärme%'
OR dd.[Name] like '% Düsseldorf%'
OR dd.[Name] like '%niederlassung%'
OR dd.[Name] like 'zentrum für %'
OR dd.[Name] like '% Generalvertretung %'
))
THEN 1 ELSE 0 END
as bit)
FROM [dbo].[mfr__contacts] as c JOIN dd on c.[CompanyId] = dd.[Id] OR dd.[MainContactId] = c.[id]
)
SELECT
[Konto] = CAST(dd.[ExternalId] as bigint) --CAST(dd.[ExternalId] as varchar(9))
,[Name (Adressatentyp Unternehmen)] = [dbo].[ott_quote_255](CAST(LEFT(CAST(CASE WHEN cc.[isCompany] = 0 THEN '' ELSE REPLACE(REPLACE(REPLACE(ISNULL( cc.[company_name] ,''), CHAR(13), ' '), CHAR(10), ' '), ' ', ' ') END as varchar(255)), 50) as varchar(50)))
,[Unternehmensgegenstand] = [dbo].[ott_quote_255]('')
,[Name (Adressattyp natürl. Person)] = [dbo].[ott_quote_255](CAST(LEFT(CAST(CASE WHEN cc.[isCompany] = 1 THEN '' ELSE REPLACE(REPLACE(REPLACE(ISNULL( cc.[LastName] ,''), CHAR(13), ' '), CHAR(10), ' '), ' ', ' ') END as varchar(255)), 30) as varchar(30)))
,[Vorname (Adressattyp natürl. Person)] = [dbo].[ott_quote_255](CAST(LEFT(CAST(CASE WHEN cc.[isCompany] = 1 THEN '' ELSE REPLACE(REPLACE(REPLACE(ISNULL( cc.[FirstName] ,''), CHAR(13), ' '), CHAR(10), ' '), ' ', ' ') END as varchar(255)), 30) as varchar(30)))
,[Name (Adressattyp keine Angabe)] = [dbo].[ott_quote_255]('')
,[Adressattyp] = [dbo].[ott_quote_255](CAST(CASE WHEN cc.[isCompany] = 1 THEN 2 ELSE 1 END as varchar(1)))
,[Kurzbezeichnung] = [dbo].[ott_quote_255]('')
,[EU-Land] = [dbo].[ott_quote_255]('')
,[EU-UStID] = [dbo].[ott_quote_255]('')
,[Anrede] = [dbo].[ott_quote_255]('') --CASE WHEN LOWER(LEFT(cc.[Gender],1)) = 'm' THEN 'Herrn' WHEN LOWER(LEFT(cc.[Gender],1)) = 'w' THEN 'Frau' ELSE '' END
,[Titel/Akad. Grad] = [dbo].[ott_quote_255]('')
,[Adelstitel] = [dbo].[ott_quote_255]('')
,[Namensvorsatz] = [dbo].[ott_quote_255]('')
,[Adressart] = [dbo].[ott_quote_255](CAST( CASE WHEN ISNULL(l.[AddressString],'') like '%postfach%' THEN 'PF' ELSE 'STR' END as varchar(3)))
,[Straße] = [dbo].[ott_quote_255](CAST(LEFT(CAST(CASE WHEN ISNULL(l.[AddressString],'') like '%postfach%' THEN '' ELSE ISNULL(l.[AddressString],'') END as varchar(255)),36) as varchar(36)))
,[Postfach] = [dbo].[ott_quote_255](CAST(LEFT(CAST(CASE WHEN ISNULL(l.[AddressString],'') like '%postfach%' THEN TRIM(SUBSTRING(ISNULL(l.[AddressString],''), PATINDEX('%postfach%', ISNULL(l.[AddressString],'')) + 8 , 12)) ELSE '' END as varchar(255)),36) as varchar(36)))
,[Postleitzahl] = [dbo].[ott_quote_255](CAST(l.[Postal] as varchar(10)))
,[Ort] = [dbo].[ott_quote_255](CAST(l.City as varchar(30)))
,[Land] = [dbo].[ott_quote_255](CAST(REPLACE(UPPER(ISNULL(l.[Country], '')),'KE','') as varchar(2)))
,[Versandzusatz] = [dbo].[ott_quote_255]('')
,[Adresszusatz] = [dbo].[ott_quote_255]('')
,[Abweichende Anrede] = [dbo].[ott_quote_255]('')
,[Abw. Zustellbezeichnung 1] = [dbo].[ott_quote_255]('')
,[Abw. Zustellbezeichnung 2] = [dbo].[ott_quote_255]('')
,[Kennz. Korrespondenzadresse] = Cast(1 as int)
,[Adresse Gültig von] = [dbo].[ott_quote_255]('')
,[Adresse Gültig bis] = [dbo].[ott_quote_255]('')
,[Telefon] = [dbo].[ott_quote_255](CAST(ISNULL(cc.[Telephone], dd.[SupportTelephone]) as varchar(60)))
,[Bemerkung (Telefon)] = [dbo].[ott_quote_255]('')
,[Telefon GL] = [dbo].[ott_quote_255]('')
,[Bemerkung (Telefon GL)] = [dbo].[ott_quote_255]('')
,[E-Mail] = [dbo].[ott_quote_255](CAST( CASE WHEN ISNULL(cc.[Email], dd.[SupportMail]) like '%[@]%' THEN REPLACE(LOWER(ISNULL(cc.[Email], dd.[SupportMail])), ' ', '') ELSE NULL END as varchar(60)))
,[Bemerkung (E-Mail)] = [dbo].[ott_quote_255]('')
,[Internet] = [dbo].[ott_quote_255]('')
,[Bemerkung (Internet)] = [dbo].[ott_quote_255]('')
,[Fax] = [dbo].[ott_quote_255]('')
,[Bemerkung (Fax)] = [dbo].[ott_quote_255]('')
,[Sonstige] = [dbo].[ott_quote_255]('')
,[Bemerkung (Sonstige)] = [dbo].[ott_quote_255]('')
,[Bankleitzahl 1] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 1] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 1] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 1] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 1] = [dbo].[ott_quote_255]('')
,[IBAN1 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 1] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 1] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 1] = [dbo].[ott_quote_255]('')
,[Bankverb 1 Gültig von] = null
,[Bankverb 1 Gültig bis] = null
,[Bankleitzahl 2] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 2] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 2] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 2] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 2] = [dbo].[ott_quote_255]('')
,[IBAN2 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 2] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 2] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 2] = [dbo].[ott_quote_255]('')
,[Bankverb 2 Gültig von] = null
,[Bankverb 2 Gltig bis] = null
,[Bankleitzahl 3] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 3] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 3] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 3] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 3] = [dbo].[ott_quote_255]('')
,[IBAN3 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 3] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 3] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 3] = [dbo].[ott_quote_255]('')
,[Bankverb 3 Gültig von] = null
,[Bankverb 3 Gültig bis] = null
,[Bankleitzahl 4] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 4] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 4] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 4] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 4] = [dbo].[ott_quote_255]('')
,[IBAN4 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 4] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 4] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 4] = [dbo].[ott_quote_255]('')
,[Bankverb 4 Gültig von] = null
,[Bankverb 4 Gültig bis] = null
,[Bankleitzahl 5] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 5] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 5] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 5] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 5] = [dbo].[ott_quote_255]('')
,[IBAN5 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 5] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 5] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 5] = [dbo].[ott_quote_255]('')
,[Bankverb 5 Gültig von] = null
,[Bankverb 5 Gültig bis] = null
,[Leerfeld] = null
,[Briefanrede] = [dbo].[ott_quote_255]('')
,[Grußformel] = [dbo].[ott_quote_255]('')
,[Kundennummer] = [dbo].[ott_quote_255]('')
,[Steuernummer] = [dbo].[ott_quote_255]('')
,[Sprache] = null
,[Ansprechpartner] = [dbo].[ott_quote_255]('')
,[Vertreter] = [dbo].[ott_quote_255]('')
,[Sachbearbeiter] = [dbo].[ott_quote_255]('')
,[Diverse-Konto] = null
,[Ausgabeziel] = null
,[Währungssteuerung] = [dbo].[ott_quote_255]('')
,[Kreditlimit (Debitor)] = null
,[Zahlungsbedingung] = null
,[Fälligkeit in Tagen (Debitor)] = null
,[Skonto in Prozent (Debitor)] = null
,[Kreditoren-Ziel 1 Tg.] = null
,[Kreditoren-Skonto 1 %] = null
,[Kreditoren-Ziel 2 Tg.] = null
,[Kreditoren-Skonto 2 %] = null
,[Kreditoren-Ziel 3 Brutto Tg.] = null
,[Kreditoren-Ziel 4 Tg.] = null
,[Kreditoren-Skonto 4 %] = null
,[Kreditoren-Ziel 5 Tg.] = null
,[Kreditoren-Skonto 5 %] = null
,[Mahnung] = null
,[Kontoauszug] = null
,[Mahntext 1] = null
,[Mahntext 2] = null
,[Mahntext 3] = null
,[Kontoauszugstext] = null
,[Mahnlimit Betrag] = null
,[Mahnlimit %] = null
,[Zinsberechnung] = null
,[Mahnzinssatz 1] = null
,[Mahnzinssatz 2] = null
,[Mahnzinssatz 3] = null
,[Lastschrift] = [dbo].[ott_quote_255]('')
,[Verfahren] = [dbo].[ott_quote_255]('')
,[Mandantenbank] = null
,[Zahlungsträger] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 1] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 2] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 3] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 4] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 5] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 6] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 7] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 8] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 9] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 10] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 11] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 12] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 13] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 14] = [dbo].[ott_quote_255]('')
,[Indiv. Feld 15] = [dbo].[ott_quote_255]('')
,[Abweichende Anrede (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Adressart (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Straße (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Postfach (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Postleitzahl (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Ort (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Land (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Versandzusatz (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Adresszusatz (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Abw. Zustellbezeichnung 1 (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Abw. Zustellbezeichnung 2 (Rechnungsadresse)] = [dbo].[ott_quote_255]('')
,[Adresse Gültig von (Rechnungsadresse)] = null
,[Adresse Gültig bis (Rechnungsadresse)] = null
,[Bankleitzahl 6] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 6] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 6] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 6] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 6] = [dbo].[ott_quote_255]('')
,[IBAN6 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 6] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 6] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 6] = [dbo].[ott_quote_255]('')
,[Bankverb 6 Gültig von] = null
,[Bankverb 6 Gültig bis] = null
,[Bankleitzahl 7] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 7] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 7] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 7] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 7] = [dbo].[ott_quote_255]('')
,[IBAN7 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 7] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 7] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 7] = [dbo].[ott_quote_255]('')
,[Bankverb 7 Gültig von] = null
,[Bankverb 7 Gültig bis] = null
,[Bankleitzahl 8] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 8] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 8] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 8] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 8] = [dbo].[ott_quote_255]('')
,[IBAN8 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 8] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 8] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 8] = [dbo].[ott_quote_255]('')
,[Bankverb 8 Gültig von] = null
,[Bankverb 8 Gültig bis] = null
,[Bankleitzahl 9] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 9] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 9] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 9] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 9] = [dbo].[ott_quote_255]('')
,[IBAN9 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 9] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 9] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 9] = [dbo].[ott_quote_255]('')
,[Bankverb 9 Gültig von] = null
,[Bankverb 9 Gültig bis] = null
,[Bankleitzahl 10] = [dbo].[ott_quote_255]('')
,[Bankbezeichnung 10] = [dbo].[ott_quote_255]('')
,[Bank-Kontonummer 10] = [dbo].[ott_quote_255]('')
,[Länderkennzeichen 10] = [dbo].[ott_quote_255]('')
,[IBAN-Nr. 10] = [dbo].[ott_quote_255]('')
,[IBAN10 korrekt] = [dbo].[ott_quote_255]('')
,[SWIFT-Code 10] = [dbo].[ott_quote_255]('')
,[Abw. Kontoinhaber 10] = [dbo].[ott_quote_255]('')
,[Kennz. Hauptbankverb. 10] = [dbo].[ott_quote_255]('')
,[Bankverb 10 Gültig von] = null
,[Bankverb 10 Gültig bis] = null
,[Nummer Fremdsystem] = [dbo].[ott_quote_255]('')
,[Insolvent] = null
,[Mandatsreferenz 1] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 2] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 3] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 4] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 5] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 6] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 7] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 8] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 9] = [dbo].[ott_quote_255]('')
,[Mandatsreferenz 10] = [dbo].[ott_quote_255]('')
,[Verknüpftes OPOS-Konto] = null
,[Mahnsperre bis] = null
,[Lastschriftsperre bis] = null
,[Zahlungssperre bis] = null
,[gebührenberechnung] = null
,[Mahngebühr 1] = null
,[Mahngebühr 2] = null
,[Mahngebühr 3] = null
,[Pauschalenberechnung] = null
,[Verzugspauschale 1] = null
,[Verzugspauschale 2] = null
,[Verzugspauschale 3] = null
,[Alternativer Suchname] = null
,[Status] = null
,[Anschrift manuell geändert (Korrespondenzadresse)] = null
,[Anschrift individuell (Korrespondenzadresse)] = null
,[Anschrift manuell geändert (Rechnungsadresse)] = null
,[Anschrift individuell (Rechnungsadresse)] = null
,[Fristberechnung bei Debitor] = null
,[Mahnfrist 1] = null
,[Mahnfrist 2] = null
,[Mahnfrist 3] = null
,[Letzte Frist] = null
FROM dd JOIN cc on cc.[#] = 1 AND cc.[company_id] = dd.[id]
LEFT JOIN [dbo].[mfr__#locations] as l ON l.[EntityID] = dd.[id] and l.Property = 'Company:Location'
END
@@ -1,57 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getInvRequestItems]
@invoiceid varchar(20)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @idbigint bigint = TRY_CAST(@invoiceid as bigint);
DECLARE @SReq TABLE ([id] bigint, [DateOfCreation] datetime, [name] nvarchar(255), [invoice.Id] varchar(20), [customer.ExternalId] bigint, [customer.Id] bigint, [mfr] bit);
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = TRY_CAST(i.[Id] as varchar(20)), [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], [mfr] = 1
FROM [dbo].[mfr__servicerequests] as s JOIN [mfr__reports] as i on i.[id] = @idbigint and s.[InvoiceId] like ('%' + i.[InvoiceId] + '%') LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
where i.[id] = @idbigint
UNION
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], [mfr] = 0
FROM [dbo].[mfr__servicerequests] as s
JOIN [dbo].[fds__invoice_servicerequests] as iq ON s.[id] = iq.[mfr__servicerequest]
JOIN [dbo].[fds__invoices] as i on iq.[InvId] = i.[id] AND i.[id] = @invoiceid
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
where i.[id] = @invoiceid
;
SELECT
[id],[DateOfCreation],[name],[invoice.Id],[customer.ExternalId],[customer.Id], [mfr]
,[order] = ROW_NUMBER() OVER (ORDER BY [DateOfCreation])
FROM @SReq;
SELECT
itm.[Id]
, [net_pos] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN '' ELSE FORMAT( CAST( (ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0) as numeric(7,2)) , '#0.00 €','de') END
, [bo_pos] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN '' ELSE FORMAT( CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2)) , '#0.00 €','de') END
, [vat] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN null ELSE FORMAT(ISNULL(vat.[vat], 19.0), '#0.0','de') + '%' END
, itm.[ServiceRequestId]
, itm.[SortOrder]
, itm.[Type]
, itm.[NameOrNumber]
, sreq.[invoice.Id]
, [order] = ROW_NUMBER() OVER (PARTITION BY itm.[ServiceRequestId] ORDER BY itm.[SortOrder])
FROM [dbo].[mfr__items] as itm
JOIN @sreq as sreq ON itm.[ServiceRequestId] = SReq.[id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
;
END
@@ -1,43 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getInvoiceReminder]
@InvId varchar(15)
, @include_drafts bit = 0
,@authuser varchar(25)
AS
BEGIN
SET NOCOUNT ON;
With inv as (
SELECT TOP(1)
[Id] = CAST([id] as varchar(15)), [InvoiceId], [DocumentName]
FROM [dbo].[fds__invoices]
WHERE [id] = @InvId
), inv2 as(
SELECT * FROM inv
UNION
SELECT TOP(1)
[Id] = CAST([id] as varchar(15)), [InvoiceId], [DocumentName]
FROM [dbo].[mfr__invoices] as mi
WHERE NOT EXISTS( SELECT 0 FROM inv )
AND TRY_CAST(@InvId as bigint) is not null
AND mi.[id] = TRY_CAST(@InvId as bigint)
)
SELECT rem.[Id]
,rem.[DateSent]
,[status] = CAST(CASE WHEN rem.[isSent] = 1 THEN 'versandt' WHEN rem.[isfinal] = 1 THEN 'fertig' WHEN rem.isfinal = 0 THEN 'Entwurf' ELSE '' END AS varchar(50))
,rem.[subject]
,rem.[DocumentName]
,rem.[amount_open]
,rem.[InvId]
,[InvoiceId] = inv2.[InvoiceId]
--,[InvoiceDocumentName] = inv2.[DocumentName]
,[hasFile] = cast( IIF(rem.[file] is null, 0, 1) as bit)
FROM [dbo].[fds__reminder] as rem
LEFT JOIN inv2 on rem.[InvId] = inv2.[Id]
WHERE [invId] = @InvId;
END
@@ -1,285 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getInvoices_list2]
@tgtdate date
,@mode varchar(1) = 'm'
, @include_drafts bit = 0
, @search varchar(100)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getInvoices_list' ,@authuser , @search);
DECLARE @startdate date, @enddate date, @today date = GETDATE();
DECLARE @true bit = 1, @false bit = 0;
DECLARE @searchmode as varchar(1) = CASE WHEN SUBSTRING(ISNULL(@search, '') + ' ', 2,1) <> ':' THEN '' ELSE LOWER(LEFT(@search,1 )) END;
DECLARE @customerfilter TABLE ([CustomerID] bigint);
IF @searchmode <> ''
BEGIN
SELECT @mode = 's', @search = SUBSTRING(@search,3, 1000);
IF @searchmode = 'c' AND LEN(@search) > 3
BeGIN
INSERT INTO @customerfilter SELECT [id] FROM [dbo].[mfr__companies] WHERE [Name] like ('%' + @search + '%');
END
END
ELSE
SET @mode = LOWER(@mode);
If @mode = 'm'
BEGIN
SELECT @startdate = [dbo].[date_monthfirst](@tgtdate)
,@enddate = [dbo].[date_monthend](@tgtdate);
END
ELSE If @mode = 'w'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst](@tgtdate)
,@enddate = [dbo].[date_weekend](@tgtdate);
END;
--Reminder settings
DECLARE @stage1 varchar(10), @stage2 varchar(10), @stage3 varchar(10);
SELECT @stage1 = [stage1], @stage2 = [stage2], @stage3 = [stage3] FROM [dbo].[fds__admin_reminderSettings]();
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode
, [title] = 'Rechnungsübersicht für ' + CASE WHEN @mode = 'm' THEN 'MONAT ' + FORMAT(@startdate, 'MMM yyyy')
WHEN @mode = 'w' THEN 'WOCHE ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' - ' + FORMAT(@enddate, 'dd.MM.')
WHEN @searchmode = 'i' THEN 'Suche nach Rechnungsnummer ''' + @search + ''''
WHEN @searchmode = 'c' THEN 'Suche nach Kunde ''' + @search + ''''
ELSE '' END
,[ust_options] = '19,0%;16,0%;0,0%';
DECLARE @inv TABLE([Id] varchar(15), [InvoiceID] varchar(255), [URI] nvarchar(255), [DocumentName] nvarchar(255), [DateOfCreation] date, [fds] bit);
--INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0 FROM [dbo].[mfr__reports] where [IsInvoice] = 1 AND CAST([DateOfCreation] as date) between @startdate AND @enddate;
INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0 FROM [dbo].[mfr__invoices]
where (CASE WHEN @searchmode = 'i' THEN
(CASE WHEN [InvoiceID] = @search OR [InvoiceID] like ('R20[0-9][0-9][-]' + @search) THEN 1 ELSE 0 END)
ELSE
(CASE WHEN CAST([DateOfCreation] as date) between @startdate AND @enddate and (ISNULL(@include_drafts,0) = 1 OR [FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview','')) THEN 1 ELSE 0 END)
END) = 1;
INSERT INTO @inv SELECT [Id],[invoiceid],[URI] = null,[DocumentName],[DateOfCreation] = [DateCreated],[fds] = 1 FROM [dbo].[fds__invoices]
where (CASE WHEN @searchmode = 'i' THEN
(CASE WHEN [InvoiceID] = @search OR [InvoiceID] like ('R20[0-9][0-9][-]' + @search) THEN 1 ELSE 0 END)
WHEN @searchmode = 'c' THEN
(CASE WHEN [CustomerId] IN (SELECT [CustomerId] fROM @customerfilter) THEN 1 ELSE 0 END)
ELSE
(CASE WHEN CAST([DateCreated] as date) between @startdate AND @enddate and (ISNULL(@include_drafts,0) = 1 OR [isfinal] = 1) THEN 1 ELSE 0 END)
END) = 1;
DECLARE @SReq TABLE ([id] bigint, [DateOfCreation] datetime, [name] nvarchar(255), [invoice.Id] varchar(15), [customer.ExternalId] bigint, [customer.Id] bigint, [ExternalId] nvarchar(255), [fds] bit);
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[mfr__servicerequests] as s
JOIN dbo.[mfr__*PartnerSet] as p on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices'
JOIN @inv as i on i.fds = 0 and p.[PartnerId] = i.[id] --s.[InvoiceId] like ('%' + i.[InvoiceId] + '%')
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 0;
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[fds__invoice_servicerequests] as irq
JOIN @inv as i on irq.[InvId] = i.[Id]
LEFT JOIN [dbo].[mfr__servicerequests] as s ON irq.[mfr__servicerequest] = s.[id]
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 1;
With itm as (
SELECT
[net_itemsum] = CAST( SUM((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL(vat.[vat], 19.0) * 0.01))
, sreq.[invoice.Id]
,[fds] = 0
FROM [dbo].[mfr__items] as itm
JOIN @sreq as sreq ON itm.[ServiceRequestId] = SReq.[id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
WHERE [fds] = 0
GROUP BY sreq.[invoice.Id]
UNION
SELECT
[net_itemsum] = CAST( SUM(ISNULL([value_total],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM(ISNULL([value_total],0) * (1 + (ISNULL([vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL([vat], 19.0) * 0.01))
, [invoice.Id] = inv.[id] --sreq.[invoice.Id]
, [fds] = 1
FROM [dbo].[fds__invoice_items] as itm
--JOIN @sreq as sreq ON itm.[InvRqId] = SReq.[id]
JOIN @inv as inv ON itm.[invid] = inv.[id] and [fds] = 1
WHERE [fds] = 1
GROUP BY inv.[id] --sreq.[invoice.Id]
),sc as(
SELECT s.[invoice.Id], [fds], [#] = COUNT(s.[id]) FROM @SReq as s GROUP BY s.[invoice.Id], [fds]
),contra_accounts as(
SELECT [account] = [key], [vat] = TRY_PARSE([value2] as numeric(5,2) USING 'en-US') FROM [dbo].[fds__admin_settings] WHERE [type] = 'contra_account'
), inv as(
--select
-- [Id] = CAST(iv.[Id] as varchar(15))
-- ,[InvoiceId] = iv.[InvoiceId]
-- ,[InvoiceType] = CAST( CASE WHEN LEFT([documentname],5) = 'absch' THEN 'i'
-- WHEN LEFT([documentname],5) = 'storn' THEN 'c'
-- ELSE 'r'
-- END
-- as char(1))
-- ,[DocumentName]
-- ,[requestcount] = sc.[#]
-- ,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
-- ,[balance] = FORMAT(ABS([invoiceBalance]), '#0.00', 'de') + ' €'
-- ,[EINZELPOS_brutto] = FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
-- ,[EINZELPOS_netto] = FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
-- ,[vat] = FORMAT(ISNULL(vat.[vat], 19.0), '#0.0', 'de') + ' %'
-- ,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
-- ,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
-- ,[contra_account] = ca.[account]
-- ,[Belegdatum] = FORMAT(iv.[DateOfCreation], 'dd.MM.yy')
-- ,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
-- ,iv.[DateOfCreation]
-- ,iv.[DueDate]
-- ,[PaymentTerm] = @stage1
-- ,ivi.[PaymentStatus]
-- ,[IsPayed] = CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL(ivi.[IsPayed],0) END as bit)
-- ,[IsCanceled]
-- ,[isSent] = @true
-- ,[isFinal] = @true
-- ,[hasFile] = CAST( CASE wHEN ISNULL([URI], '') <> '' THEN 1 ELSE 0 END as bit)
-- ,[fds] = @false
-- from [dbo].[mfr__reports] as iv
-- LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on iv.[id] = ivi.[invid]
-- LEFT JOIN itm on iv.Id = itm.[invoice.Id]
-- LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
-- LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
-- LEFT JOIN contra_accounts as ca ON ISNULL(vat.[vat], 19.0) = ca.[vat]
-- where iv.Id in (SELECT [Id] FROM @inv Where fds = 0)
--UNION
select
[Id] = CAST(iv.[Id] as varchar(15))
,[InvoiceId] = iv.[InvoiceId]
,[InvoiceType] = CAST( CASE WHEN LEFT([documentname],5) = 'absch' THEN 'i'
WHEN LEFT([documentname],5) = 'storn' THEN 'c'
ELSE 'r'
END
as char(1))
,[DocumentName]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = TRY_CAST(iv.id as varchar(50)))
,[balance] = FORMAT([invoiceBalance], '#0.00', 'de') + ''
,[EINZELPOS_brutto] = NULL --FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
,[EINZELPOS_netto] = NULL --FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
,[vat] = FORMAT(ISNULL(vat.[vat], ISNULL([dbo].[fds__fn_vatfrombalances]([invoiceBalance], [invoiceBalancenetto]) ,19.0)), '#0.0', 'de') + ' %'
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
--,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE TRY_CAST(iv.id as varchar(50)) = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
,[contra_account] = ca.[account]
,[Belegdatum] = FORMAT(iv.[DateOfCreation], 'dd.MM.yy')
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE TRY_CAST(iv.id as varchar(50)) = sreq.[invoice.Id])
,iv.[DateOfCreation]
,iv.[DueDate]
,[PaymentTerm] = @stage1
,ivi.[PaymentStatus]
,[IsPayed] = CAST(CASE WHEN ISNULL(ivi.isPayed,0) = 1 THEN 1 WHEN [InvoiceState] = 'eIsCancelled' THEN 1 WHEN [InvoiceState] = 'eIsPaid' THEN 1 ELSE 0 END as bit)
,[IsCanceled] = CAST(CASE WHEN [InvoiceState] = 'eIsCancelled' THEN 1 ELSE 0 END as bit)
,[isSent] = @true
,[isFinal] = @true
,[hasFile] = CAST( CASE wHEN ISNULL([URI], '') <> '' THEN 1 ELSE 0 END as bit)
,[fds] = @false
,[customer_id] = ps.[PartnerId]
from [dbo].[mfr__invoices] as iv
LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on iv.[id] = ivi.[invid]
LEFT JOIN itm on TRY_CAST(iv.id as varchar(50)) = itm.[invoice.Id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
LEFT JOIN contra_accounts as ca ON ISNULL(vat.[vat], 19.0) = ca.[vat]
LEFT JOIN [dbo].[mfr__*PartnerSet] as ps ON iv.[Id] = ps.[EntityId] AND ps.[Property] = 'Invoice:Customer'
where TRY_CAST(iv.id as varchar(50)) in (SELECT [Id] FROM @inv Where fds = 0)
UNION
select
iv.[Id]
,[InvoiceId] = iv.[InvoiceId] + ISNULL((CASE WHEN iv.InvoiceType = 'c' THEN (SELECT TOP(1) ' >> ' + i2.[InvoiceId] FROM [dbo].[fds__invoices] as i2 JOIN [dbo].[fds__invoice_details] as id2 on i2.[Id] = id2.[StornoTo_InvId] where id2.[InvId] = iv.[id] and i2.[IsFinal] = 1) ELSE '' END), '')
,[InvoiceType]
,[DocumentName]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
,[balance] = FORMAT([invoiceBalance], '#0.00', 'de') + ''
,[EINZELPOS_brutto] = NULL --FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
,[EINZELPOS_netto] = NULL --FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
,[vat] = FORMAT(ISNULL(iv.[InvoiceVAT_1], 19.0), '#0.0', 'de') + ' %'
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
--,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
,[contra_account] = ca.[account]
,[Belegdatum] = FORMAT(iv.[DateCreated], 'dd.MM.yy')
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
,[DateOfCreation] = iv.[DateCreated]
,[DueDate]
,[PaymentTerm] = ISNULL([PaymentTerm], @stage1)
,[PaymentStatus]
,[IsPayed] = CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL([IsPayed],0) END as bit)
,[IsCanceled]
,[isSent]
,[isFinal]
,[hasFile] = CAST( CASE wHEN [file] is not null and [DocumentNAme] is not null THEN 1 ELSE 0 END as bit)
,[fds] = @true
,[customer_id] = iv.[CustomerId]
from [dbo].[fds__invoices] as iv
LEFT JOIN itm on iv.Id = itm.[invoice.Id]
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 1
LEFT JOIN contra_accounts as ca ON ISNULL(iv.[InvoiceVAT_1], 19.0) = ca.[vat]
where iv.Id in (SELECT [Id] FROM @inv Where fds = 1)
), reminder as (
SELECT [InvId], [lastReminder] = MAX([DateFinalized]), [#] = COUNT(*) FROM [dbo].[fds__reminder] WHERE [IsFinal] = 1 AND [invId] IN (SELECT [Id] FROM @inv) GROUP BY [InvId]
)
select
inv.*
,[reminderstatus] = CASE WHEN [ispayed] = 1 THEN 0
WHEN [dbo].[fds__fn_isInvoiceCancelled](inv.[Id]) = 1 THEN 0
WHEN DueDate is null THEN 0
WHEN [deb_cred] = 'H' THEN 0
WHEN @stage3 <> '' AND rem.[#] = 2 AND @today > [dbo].[date_addcustom]([lastReminder], @stage3) THEN 3
WHEN @stage2 <> '' AND rem.[#] = 1 AND @today > [dbo].[date_addcustom]([lastReminder], @stage2) THEN 2
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, [DateOfCreation]), ISNULL([PaymentTerm],@stage1)) THEN 1
ELSE 0 END
,[invstatus] = CASE WHEN [IsPayed] = 1 AND [isCanceled] = 0 THEN 'pyd'
WHEN [isCanceled] = 1 OR [dbo].[fds__fn_isInvoiceCancelled](inv.[Id]) = 1 THEN 'cc'
WHEN [isFinal] = 0 THEN 'dft'
WHEN [isFinal] = 1 AND [isSent] = 0 THEN 'uns'
WHEN [DueDate] is null THEN 'op'
WHEN [deb_cred] = 'H' THEN ''
WHEN @today >= [DueDate] THEN 'due'
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, [DateOfCreation]), @stage1) THEN 'ovd'
WHEN ISNULL(rem.[#],0) > 0 THEN 'rem'
END
,[reminder] = rem.[#]
,[CustomerName] = c.[name]
,[order] = CASE WHEN @include_drafts = 1 THEN
ROW_NUMBER() OVER (ORDER BY inv.[DateOfCreation] ASC)
ELSE
ROW_NUMBER() OVER (ORDER BY TRY_CAST(SUBSTRING([invoiceid], 2,4) AS int), TRY_CAST(SUBSTRING([invoiceid], 7,4) AS int), inv.[DateOfCreation] ASC)
END
,[Payment] = CASE WHEN [paymentstatus] = 'a' THEN 'vollst.'
WHEN [paymentstatus] = 'p' THEN FORMAT(ISNULL([dbo].[fds__fn_InvoicePaymentAmount](inv.[Id]),0.0), '0.00€', 'de')
ELSE '' END
FROM inv
left JOIN reminder as rem ON inv.[Id] = rem.[InvId] --inv.[id] is already converted to varchar
LEFT JOIN [dbo].[mfr__companies] as c ON inv.[customer_id] = c.[id]
WHERE (CASE WHEN @searchmode = 'c' THEN
(CASE WHEN inv.[customer_id] IN (SELECT [CustomerId] fROM @customerfilter) THEN 1 ELSE 0 END)
ELSE 1 END) = 1
order by inv.[DateOfCreation] ASC;
END
@@ -1,293 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getInvoices_list_vario]
@tgtdate date
,@mode varchar(1) = 'm'
, @includes varchar(50)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @include_drafts bit = [dbo].[ott_csv_contains](@includes, 'd');
DECLARE @only_drafts bit = 0, @only_unpayed bit = 0, @only_reminded bit = 0, @only_akonto bit = 0, @only_storno bit = 0, @only_remindedunpayed bit = 0;
DECLARE @includetext varchar(50) = '';
IF @includes = '#d'
SELECT @includetext = ' (nur Entwürfe) ', @include_drafts = 1, @only_drafts = 1, @only_reminded = 0, @only_unpayed = 0, @only_akonto = 0, @only_storno = 0;
ELSE IF @includes = 'all'
SELECT @includetext = ' (alle) ', @include_drafts = 1, @only_drafts = 0, @only_reminded = 0, @only_unpayed = 0, @only_akonto = 0, @only_storno = 0;
ELSE IF @includes = '#r'
SELECT @includetext = ' (nur angemahnte) ', @include_drafts = 0, @only_drafts = 0, @only_reminded = 1, @only_unpayed = 0, @only_akonto = 0, @only_storno = 0;
ELSE IF @includes = '#u'
SELECT @includetext = ' (nur unbezahlte) ', @include_drafts = 0, @only_drafts = 0, @only_reminded = 0, @only_unpayed = 1, @only_akonto = 0, @only_storno = 0;
ELSE IF @includes = '#a'
SELECT @includetext = ' (nur Akonto) ', @include_drafts = 0, @only_drafts = 0, @only_reminded = 0, @only_unpayed = 0, @only_akonto = 1, @only_storno = 0;
ELSE IF @includes = '#c'
SELECT @includetext = ' (nur Storno) ', @include_drafts = 0, @only_drafts = 0, @only_reminded = 0, @only_unpayed = 0, @only_akonto = 0, @only_storno = 1;
ELSE IF @includes = '#ru'
SELECT @includetext = ' (nur gemahnt + unbez. + nicht storniert) ', @include_drafts = 0, @only_drafts = 0, @only_reminded = 0, @only_unpayed = 0, @only_akonto = 0, @only_storno = 0, @only_remindedunpayed = 1;
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getInvoices_list' ,@authuser , '');
DECLARE @startdate date, @enddate date, @today date = GETDATE();
DECLARE @true bit = 1, @false bit = 0;
SET @mode = LOWER(@mode);
If @mode = 'm'
BEGIN
SELECT @startdate = [dbo].[date_monthfirst](@tgtdate)
,@enddate = [dbo].[date_monthend](@tgtdate);
END
ELSE If @mode = 'w'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst](@tgtdate)
,@enddate = [dbo].[date_weekend](@tgtdate);
END
ELSE If @mode = 'a'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst]('2000-01-01')
,@enddate = [dbo].[date_weekend](DATEADD(year, 1, @today));
END
--Reminder settings
DECLARE @stage1 varchar(10), @stage2 varchar(10), @stage3 varchar(10);
SELECT @stage1 = [stage1], @stage2 = [stage2], @stage3 = [stage3] FROM [dbo].[fds__admin_reminderSettings]();
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode
, [title] = 'Rechnungsübersicht ' + @includetext + ' für ' + CASE WHEN @mode = 'm' THEN 'MONAT ' + FORMAT(@startdate, 'MMM yyyy')
WHEN @mode = 'w' THEN 'WOCHE ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' - ' + FORMAT(@enddate, 'dd.MM.')
WHEN @mode = 'a' THEN 'den gesamten Zeitraum'
ELSE '' END
,[ust_options] = '19,0%;16,0%;0,0%';
DECLARE @inv TABLE([Id] varchar(15), [InvoiceID] varchar(255), [URI] nvarchar(255), [DocumentName] nvarchar(255), [DateOfCreation] date, [fds] bit);
--INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0 FROM [dbo].[mfr__reports] where [IsInvoice] = 1 AND CAST([DateOfCreation] as date) between @startdate AND @enddate;
INSERT INTO @inv SELECT [Id],[invoiceid],[URI],[DocumentName],[DateOfCreation],[fds] = 0 FROM [dbo].[mfr__invoices]
where CAST([DateOfCreation] as date) between @startdate AND @enddate
and (ISNULL(@include_drafts,0) = 1 OR [FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview',''))
and (@only_drafts = 0 OR ISNULL(InvoiceId,'') in ('Preview',''))
and (@only_akonto = 0 OR [FileType] = 'PdfPartialInvoice')
and (@only_storno = 0 OR [FileType] = 'PdfCancelInvoice')
and ((@only_unpayed = 0 AND @only_remindedunpayed = 0) OR [InvoiceState] not IN ('eIsCancelled', 'eIsPaid'));
INSERT INTO @inv SELECT [Id],[invoiceid],[URI] = null,[DocumentName],[DateOfCreation] = [DateCreated],[fds] = 1 FROM [dbo].[fds__invoices]
where CAST([DateCreated] as date) between @startdate AND @enddate
and (ISNULL(@include_drafts,0) = 1 OR [isfinal] = 1)
and (@only_drafts = 0 OR [isfinal] = 0)
and (@only_akonto = 0 OR [InvoiceType] = 'i')
and (@only_storno = 0 OR [InvoiceType] = 'c')
and ((@only_unpayed = 0 AND @only_remindedunpayed = 0) OR (IsPayed = 0 AND IsCanceled = 0));
DECLARE @SReq TABLE ([id] bigint, [DateOfCreation] datetime, [name] nvarchar(255), [invoice.Id] varchar(15), [customer.ExternalId] bigint, [customer.Id] bigint, [ExternalId] nvarchar(255), [fds] bit);
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[mfr__servicerequests] as s
JOIN dbo.[mfr__*PartnerSet] as p on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices'
JOIN @inv as i on i.fds = 0 and p.[PartnerId] = i.[id] --s.[InvoiceId] like ('%' + i.[InvoiceId] + '%')
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 0;
INSERT INTO @SReq
SELECT s.[Id], s.[DateOfCreation], s.[name], [invoice.Id] = i.[Id], [customer.ExternalId] = cy.ExternalId , [customer.Id] = cy.[id], s.[ExternalId], [fds]
FROM [dbo].[fds__invoice_servicerequests] as irq
JOIN @inv as i on irq.[InvId] = i.[Id]
LEFT JOIN [dbo].[mfr__servicerequests] as s ON irq.[mfr__servicerequest] = s.[id]
LEFT JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE i.fds = 1;
With itm as (
SELECT
[net_itemsum] = CAST( SUM((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL(vat.[vat], 19.0) * 0.01))
, sreq.[invoice.Id]
,[fds] = 0
FROM [dbo].[mfr__items] as itm
JOIN @sreq as sreq ON itm.[ServiceRequestId] = SReq.[id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
WHERE [fds] = 0
GROUP BY sreq.[invoice.Id]
UNION
SELECT
[net_itemsum] = CAST( SUM(ISNULL([value_total],0)) as numeric(12,2))
, [bo_itemsum] = CAST( SUM(ISNULL([value_total],0) * (1 + (ISNULL([vat], 19.0) * 0.01))) as numeric(12,2))
, [vf] = MAX(1 + (ISNULL([vat], 19.0) * 0.01))
, [invoice.Id] = inv.[id] --sreq.[invoice.Id]
, [fds] = 1
FROM [dbo].[fds__invoice_items] as itm
--JOIN @sreq as sreq ON itm.[InvRqId] = SReq.[id]
JOIN @inv as inv ON itm.[invid] = inv.[id] and [fds] = 1
WHERE [fds] = 1
GROUP BY inv.[id] --sreq.[invoice.Id]
),sc as(
SELECT s.[invoice.Id], [fds], [#] = COUNT(s.[id]) FROM @SReq as s GROUP BY s.[invoice.Id], [fds]
),contra_accounts as(
SELECT [account] = [key], [vat] = TRY_PARSE([value2] as numeric(5,2) USING 'en-US') FROM [dbo].[fds__admin_settings] WHERE [type] = 'contra_account'
), inv as(
--select
-- [Id] = CAST(iv.[Id] as varchar(15))
-- ,[InvoiceId] = iv.[InvoiceId]
-- ,[InvoiceType] = CAST( CASE WHEN LEFT([documentname],5) = 'absch' THEN 'i'
-- WHEN LEFT([documentname],5) = 'storn' THEN 'c'
-- ELSE 'r'
-- END
-- as char(1))
-- ,[DocumentName]
-- ,[requestcount] = sc.[#]
-- ,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
-- ,[balance] = FORMAT(ABS([invoiceBalance]), '#0.00', 'de') + ' €'
-- ,[EINZELPOS_brutto] = FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
-- ,[EINZELPOS_netto] = FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
-- ,[vat] = FORMAT(ISNULL(vat.[vat], 19.0), '#0.0', 'de') + ' %'
-- ,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
-- ,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
-- ,[contra_account] = ca.[account]
-- ,[Belegdatum] = FORMAT(iv.[DateOfCreation], 'dd.MM.yy')
-- ,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
-- ,iv.[DateOfCreation]
-- ,iv.[DueDate]
-- ,[PaymentTerm] = @stage1
-- ,ivi.[PaymentStatus]
-- ,[IsPayed] = CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL(ivi.[IsPayed],0) END as bit)
-- ,[IsCanceled]
-- ,[isSent] = @true
-- ,[isFinal] = @true
-- ,[hasFile] = CAST( CASE wHEN ISNULL([URI], '') <> '' THEN 1 ELSE 0 END as bit)
-- ,[fds] = @false
-- from [dbo].[mfr__reports] as iv
-- LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on iv.[id] = ivi.[invid]
-- LEFT JOIN itm on iv.Id = itm.[invoice.Id]
-- LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
-- LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
-- LEFT JOIN contra_accounts as ca ON ISNULL(vat.[vat], 19.0) = ca.[vat]
-- where iv.Id in (SELECT [Id] FROM @inv Where fds = 0)
--UNION
select
[Id] = CAST(iv.[Id] as varchar(15))
,[InvoiceId] = iv.[InvoiceId]
,[InvoiceType] = CAST( CASE WHEN LEFT([documentname],5) = 'absch' THEN 'i'
WHEN LEFT([documentname],5) = 'storn' THEN 'c'
ELSE 'r'
END
as char(1))
,[DocumentName]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = TRY_CAST(iv.id as varchar(50)))
,[balance] = FORMAT([invoiceBalance], '#0.00', 'de') + ''
,[EINZELPOS_brutto] = NULL --FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
,[EINZELPOS_netto] = NULL --FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
,[vat] = FORMAT(ISNULL(vat.[vat], ISNULL([dbo].[fds__fn_vatfrombalances]([invoiceBalance], [invoiceBalancenetto]) ,19.0)), '#0.0', 'de') + ' %'
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE TRY_CAST(iv.id as varchar(50)) = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
,[contra_account] = ca.[account]
,[Belegdatum] = FORMAT(iv.[DateOfCreation], 'dd.MM.yy')
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE TRY_CAST(iv.id as varchar(50)) = sreq.[invoice.Id])
,iv.[DateOfCreation]
,iv.[DueDate]
,[PaymentTerm] = @stage1
,ivi.[PaymentStatus]
,[IsPayed] = CAST(CASE WHEN ISNULL(ivi.isPayed,0) = 1 THEN 1 WHEN [InvoiceState] = 'eIsCancelled' THEN 1 WHEN [InvoiceState] = 'eIsPaid' THEN 1 ELSE 0 END as bit)
,[IsCanceled] = CAST(CASE WHEN [InvoiceState] = 'eIsCancelled' THEN 1 ELSE 0 END as bit)
,[isSent] = @true
,[isFinal] = @true
,[hasFile] = CAST( CASE wHEN ISNULL([URI], '') <> '' THEN 1 ELSE 0 END as bit)
,[fds] = @false
,[customer_id] = ps.[PartnerId]
from [dbo].[mfr__invoices] as iv
LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on iv.[id] = ivi.[invid]
LEFT JOIN itm on TRY_CAST(iv.id as varchar(50)) = itm.[invoice.Id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON iv.[Id] = vat.[EntityId] and vat.[EntityType] = 'report'
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 0
LEFT JOIN contra_accounts as ca ON ISNULL(vat.[vat], 19.0) = ca.[vat]
LEFT JOIN [dbo].[mfr__*PartnerSet] as ps ON iv.[Id] = ps.[EntityId] AND ps.[Property] = 'Invoice:Customer'
where TRY_CAST(iv.id as varchar(50)) in (SELECT [Id] FROM @inv Where fds = 0)
UNION
select
iv.[Id]
,[InvoiceId] = iv.[InvoiceId] + ISNULL((CASE WHEN iv.InvoiceType = 'c' THEN (SELECT TOP(1) ' >> ' + i2.[InvoiceId] FROM [dbo].[fds__invoices] as i2 JOIN [dbo].[fds__invoice_details] as id2 on i2.[Id] = id2.[StornoTo_InvId] where id2.[InvId] = iv.[id] and i2.[IsFinal] = 1) ELSE '' END), '')
,[InvoiceType]
,[DocumentName]
,[requestcount] = sc.[#]
,[request] = (SELECT TOP(1) s.[ExternalId] FROM @SReq as s WHERE s.[invoice.Id] = iv.id)
,[balance] = FORMAT([invoiceBalance], '#0.00', 'de') + ''
,[EINZELPOS_brutto] = NULL --FORMAT(itm.bo_itemsum, '#0.00', 'de') + ' €'
,[EINZELPOS_netto] = NULL --FORMAT(itm.net_itemsum, '#0.00', 'de') + ' €'
,[vat] = FORMAT(ISNULL(iv.[InvoiceVAT_1], 19.0), '#0.0', 'de') + ' %'
,[deb_cred] = CASE WHEN [invoiceBalance] >= 0 THEN 'S' ELSE 'H' END
,[customer] = (SELECT TOP(1) [customer.ExternalId] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id] and [customer.ExternalId] is not null ORDER BY SReq.DateOfCreation ASC)
,[contra_account] = ca.[account]
,[Belegdatum] = FORMAT(iv.[DateCreated], 'dd.MM.yy')
,[Buchungstext] = (SELECT TOP(1) SReq.[name] FROM @SReq as sreq WHERE iv.[Id] = sreq.[invoice.Id])
,[DateOfCreation] = iv.[DateCreated]
,[DueDate]
,[PaymentTerm] = ISNULL([PaymentTerm], @stage1)
,[PaymentStatus]
,[IsPayed] = CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL([IsPayed],0) END as bit)
,[IsCanceled] = CAST(IIF(ISNULL([IsCanceled],0) = 1 OR [dbo].[fds__fn_isInvoiceCancelled](iv.[Id]) = 1, 1,0) as bit)
,[isSent]
,[isFinal]
,[hasFile] = CAST( CASE wHEN [file] is not null and [DocumentNAme] is not null THEN 1 ELSE 0 END as bit)
,[fds] = @true
,[customer_id] = iv.[CustomerId]
from [dbo].[fds__invoices] as iv
LEFT JOIN itm on iv.Id = itm.[invoice.Id]
LEFT JOIN sc on iv.[id] = sc.[invoice.Id] and sc.[fds] = 1
LEFT JOIN contra_accounts as ca ON ISNULL(iv.[InvoiceVAT_1], 19.0) = ca.[vat]
where iv.Id in (SELECT [Id] FROM @inv Where fds = 1)
), reminder as (
SELECT [InvId], [lastReminder] = MAX([DateFinalized]), [#] = COUNT(*) FROM [dbo].[fds__reminder] WHERE [isFinal] = 1 AND [invId] IN (SELECT [Id] FROM @inv) GROUP BY [InvId]
)
select
inv.*
,[reminderstatus] = CASE WHEN [ispayed] = 1 THEN 0
WHEN [iscanceled] = 1 THEN 0
WHEN DueDate is null THEN 0
WHEN [deb_cred] = 'H' THEN 0
WHEN @stage3 <> '' AND rem.[#] = 2 AND @today > [dbo].[date_addcustom]([lastReminder], @stage3) THEN 3
WHEN @stage2 <> '' AND rem.[#] = 1 AND @today > [dbo].[date_addcustom]([lastReminder], @stage2) THEN 2
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, [DateOfCreation]), ISNULL([PaymentTerm],@stage1)) THEN 1
ELSE 0 END
,[invstatus] = CASE WHEN [IsPayed] = 1 AND [isCanceled] = 0 THEN 'pyd'
WHEN [isCanceled] = 1 THEN 'cc'
WHEN [isFinal] = 0 THEN 'dft'
WHEN [isFinal] = 1 AND [isSent] = 0 THEN 'uns'
WHEN [DueDate] is null THEN 'op'
WHEN [deb_cred] = 'H' THEN ''
WHEN @today >= [DueDate] THEN 'due'
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, [DateOfCreation]), @stage1) THEN 'ovd'
WHEN ISNULL(rem.[#],0) > 0 THEN 'rem'
END
,[reminder] = rem.[#]
,[CustomerName] = c.[name]
,[order] = CASE WHEN @include_drafts = 1 THEN
ROW_NUMBER() OVER (ORDER BY inv.[DateOfCreation] ASC)
ELSE
ROW_NUMBER() OVER (ORDER BY TRY_CAST(SUBSTRING([invoiceid], 2,4) AS int), TRY_CAST(SUBSTRING([invoiceid], 7,4) AS int), inv.[DateOfCreation] ASC)
END
,[Payment] = IIF(py.[amount] is null,'',FORMAT(ISNULL(py.[amount],0.0), '0.00€', 'de') + IIF(py.[manu] = 1, ' man.',''))
/*CASE WHEN [paymentstatus] = 'a' THEN 'vollst.'
WHEN [paymentstatus] = 'p' THEN FORMAT(ISNULL([dbo].[fds__fn_InvoicePaymentAmount](inv.[Id]),0.0), '0.00€', 'de')
ELSE '' END*/
FROM inv
left JOIN reminder as rem ON inv.[Id] = rem.[InvId] --inv.[id] is already converted to varchar
LEFT JOIN [dbo].[mfr__companies] as c ON inv.[customer_id] = c.[id]
LEFT JOIN [dbo].[fds__fn_bankingtransactions_perInvoice](@startdate, @enddate) as py ON py.[invid] = inv.[Id]
WHERE ((@only_reminded = 0 AND @only_remindedunpayed = 0) OR ISNULL(rem.[#],0) > 0)
AND (CASE WHEN @only_unpayed = 0 AND @only_remindedunpayed = 0 THEN 1 WHEN [IsCanceled] = 1 THEN 0 ELSE 1 END) = 1
order by inv.[DateOfCreation] ASC;
END
@@ -1,96 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getReminder]
@Id varchar(8),
@includefile bit,
@authuser varchar(25)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 1
THROW 60000, N'not authorized', 1;
DECLARE @rem [dbo].[fds__tt__reminder_core];
INSERT INTO @rem
SELECT TOP(1) [Id]
,[Version]
,[DocumentName]
,[InvId]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[type]
,[amount]
,[amount_payed]
,[amount_open]
,[subject]
,[text]
,[IsSent]
,[IsFinal]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified]
,[file] = CASE WHEN ISNULL(@includefile,0) = 1 THEN [file] else NULL END -- do not return file if not explicitly requested
FROM [dbo].[fds__reminder]
WHERE [Id] = @Id AND @Id is not null;
DECLARE @InvId varchar(15) = (SELECT TOP(1) [InvId] fROM @rem);
---output
--admin
SELECT TOP(1)
[today]
, [sender] = N'Sebastian Fuchs GmbH & Co. KG ● Germaniastraße 15 ● 40223 Düsseldorf'
, [CustomerId] = rem.[CustomerId]
, [type] = rem.[type]
FROM (VALUES(CAST(GETDATE() as date)))base ([today]) cross JOIN @rem as rem;
--rem
With inv1 as(
select TOP(1) * FROM [dbo].[fds__invoices] WHERE [id] = @InvId
), inv as(
SELECT [Id] = cast([id] as varchar(15))
,[Invoiceid]
,[DateFinalized]
,[InvoiceBalance]
,[DocumentName]
,[file]
FROM inv1
UNION
SELECT [Id] = cast([id] as varchar(15))
,[Invoiceid]
,[DateFinalized] = [DateOfCreation]
,[InvoiceBalance]
,[DocumentName]
,[file] = NULL
FROM [dbo].[mfr__invoices] as mfri
WHERE [id] = TRY_CAST(@invId as bigint) AND NOT EXISTS( SELECT 0 FROM inv1 ) --will only be used, if id not present in fds__invoices
)
SELECT TOP(1) o.*
,inv.[InvoiceId]
,[InvoiceDate] = inv.[DateFinalized]
,[InvoiceFileName] = inv.[DocumentName]
,[InvoiceFile] = CASE WHEN ISNULL(@includefile,0) = 1 THEN inv.[file] else NULL END -- do not return file if not explicitly requested
,[hasFile] = CAST ( CASE WHEN o.[file] is null THEN 0 ELSE 1 END as bit)
,[UserNameFinalized] = [dbo].[fis_admin_getUserName_byID](o.[UserFinalized])
,[UserEmailFinalized] = [dbo].[fis_admin_getUserEmail_byID](o.[UserFinalized])
from @rem as o
join inv on o.[invid] = inv.[id];
END
@@ -1,26 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getReportDocument]
@reportid varchar(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @idbigint bigint = TRY_CAST(@reportid as bigint);
SELECT TOP(1)
[Id] = CAST([Id] as varchar(20)),[InvoiceID],[URI],[DocumentName],[isInvoice] = 1,[DateOfCreation],[fds] = CAST( 0 as bit),[file] = CAST( null as varbinary(max))
,[order] = ROW_NUMBER() OVER (ORDER BY [DateOfCreation] ASC)
FROM [dbo].[mfr__invoices] WHERE [id] = @idbigint and @idbigint is not null
UNION
SELECT TOP(1)
[Id] = CAST([Id] as varchar(20)),[InvoiceID],[URI] = '',[DocumentName],[isInvoice] = 1,[DateOfCreation] = [DateCreated],[fds] = CAST( 1 as bit), [file]
,[order] = ROW_NUMBER() OVER (ORDER BY [DateCreated] ASC)
FROM [dbo].[fds__invoices] WHERE [id] = @reportid AND [isFinal] = 1;
;
END
@@ -1,422 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getRequest_details]
@servicerequestid bigint
, @mode varchar(3)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_req', @authuser) < 1
THROW 60000, N'not authorized', 1;
DECLARE @today date = GETDATE();
DECLARE @SReq TABLE (
[Id] [bigint] NOT NULL,
[Name] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[InvoiceId] [nvarchar](255) NULL,
[ClosedAt] [datetime] NULL,
[ReleasedAt] [datetime] NULL,
[WorkDoneAt] [datetime] NULL,
[TargetTimeInMinutes] [bigint] NOT NULL,
[DateModified] [datetime] NULL,
[DateOfCreation] [datetime] NULL,
[DueDateRangeStart] [datetime] NULL,
[DueDateRangeEnd] [datetime] NULL,
[PortalLink] [nvarchar](255) NULL,
[CostCenterId] [bigint] NULL,
[Description] [nvarchar](max) NULL,
[State] [nvarchar](255) NULL,
[CustomValues] [nvarchar](max) NOT NULL,
[CurrentOwnerId] [bigint] NULL,
[CustomerId] [bigint] NULL,
[ParentServiceRequestId] [bigint] NULL,
[ParentExtenalId] [nvarchar](255) NULL,
[ParentWorkDoneAt] [date] NULL,
[ParentInvoiceId] [nvarchar](255) NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NOT NULL,
[IsTemplate] [bit] NOT NULL,
[IsTemplateMobile] [bit] NOT NULL,
[CreateFromServiceRequestTemplateId] [bigint] NOT NULL,
[Type] [nvarchar](255) NULL,
[Summary] nvarchar(255) NULL,
[isChild] bit NULL,
[baseorder] int,
[provision] varchar(255) NULL,
[open] bit NULL
);
insert into @SReq
SELECT
[Id]
,[Name]
,[ExternalId]
,[InvoiceId]
,[ClosedAt]
,[ReleasedAt]
,[WorkDoneAt]
,[TargetTimeInMinutes]
,[DateModified]
,[DateOfCreation]
,[DueDateRangeStart]
,[DueDateRangeEnd]
,[PortalLink]
,[CostCenterId]
,[Description]
,[State]
,[CustomValues]
,[CurrentOwnerId]
,[CustomerId]
,[ParentServiceRequestId] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then [id] ELSE [ParentServiceRequestId] END
,[ParentExtenalId] = cast(CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then null ELSE '?' END as nvarchar(255))
,[ParentWorkDoneAt] = cast(NULL as datetime)
,[ParentInvoiceId] = cast(NULL as nvarchar(255))
,[Location#ID]
,[Version]
,[IsTemplate]
,[IsTemplateMobile]
,[CreateFromServiceRequestTemplateId]
,[Type]
,[Summary] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[baseorder] = 0
,[provision] = NULL
,[open] = 1
FROM [dbo].[mfr__servicerequests] as s
WHERE [id] = @servicerequestid;
INSERT INTO @SReq
SELECT s.[Id]
,s.[Name]
,s.[ExternalId]
,s.[InvoiceId]
,s.[ClosedAt]
,s.[ReleasedAt]
,s.[WorkDoneAt]
,s.[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,s.[DueDateRangeStart]
,s.[DueDateRangeEnd]
,s.[PortalLink]
,s.[CostCenterId]
,s.[Description]
,s.[State]
,s.[CustomValues]
,s.[CurrentOwnerId]
,s.[CustomerId]
,s.[ParentServiceRequestId]
,[ParentExtenalId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[ExternalId] ELSE NULL END
,[ParentWorkDoneAt] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[WorkDoneAt] ELSE NULL END
,[ParentInvoiceId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[InvoiceId] ELSE NULL END
,s.[Location#ID]
,s.[Version]
,s.[IsTemplate]
,s.[IsTemplateMobile]
,s.[CreateFromServiceRequestTemplateId]
,s.[Type]
,[Summary] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[baseorder] = 0
, [provision] = null
,[open] = 1
from [dbo].[mfr__servicerequests] as s JOIN @SReq as q ON (s.[ParentServiceRequestId] = q.[id] OR q.[ParentServiceRequestId] = s.[id]) --parents or childs
WHERE NOT EXISTS (SELECT * FROM @SReq as s2 WHERE s2.[id] = s.[Id])
;
-- need to use CTE, because row_number is not available with update statements
WITH q as (
SELECT [id], [baseorder] = (ROW_NUMBER() OVER (ORDER BY ISNULL([ParentWorkDoneAt], [WorkDoneAt]), ISNULL([ParentServiceRequestId],[id])) * 100)
FROM @SReq as s
WHERE [ischild] = 0
)
UPDATE s SET
[baseorder] = q.baseorder
FROM @SReq as s JOIN q ON s.[id] = q.[id];
-- need to use CTE, because row_number is not available with update statements
WITH q as (
SELECT s.[id]
, q.[ExternalId], q.[WorkDoneAt], q.[InvoiceId]
, [baseorder] = q.[baseorder] + ROW_NUMBER() OVER (PARTITION BY q.[id] ORDER BY s.[dateofcreation])
FROM @SReq as s JOIN @SReq as q ON s.[ParentServiceRequestId] = q.[id]
WHERE s.[ischild] = 1
)
UPDATE s SET
[ParentExtenalId] = q.[ExternalId]
,[ParentWorkDoneAt] = q.[WorkDoneAt]
,[ParentInvoiceId] = q.[InvoiceId]
,[baseorder] = q.[baseorder]
FROM @SReq as s JOIN q ON s.[Id] = q.[id];
DECLARE @inv TABLE ([Id] bigint, [Invoices] varchar(1000), [isOpen] bit);
With fdsinv as(
SELECT [Id] = fdsr.[mfr__servicerequest], fdsi.[InvoiceId], fdsi.[InvoiceType], [isCanceled] = ISNULL(fdsi.[isCanceled],0)
FROM [dbo].[fds__invoice_servicerequests] as fdsr
JOIN [dbo].[fds__invoices] as fdsi on fdsr.[invid] = fdsi.[id]
JOIN @SReq as s ON fdsr.[mfr__servicerequest] = s.[id]
WHERE [isFinal] = 1 AND fdsr.[mfr__servicerequest] is not null
), fi as(
SELECT [Id], [isOpen] = MIN(CASE WHEN ISNULL([invoicetype],'r') = 'r' AND [isCanceled] = 0 THEN 0 ELSE 1 END) FROM fdsinv GROUP BY [Id]
)
INSERT INTO @inv
SELECT [Id]
, [invoices] = ISNULL(STUFF( (SELECT CHAR(10) + [InvoiceId] + ' (' + [InvoiceTYpe] + ')' FROM fdsinv as f2 WHERE f2.[id] = fi.[Id] FOR XML PATH, TYPE).value('.[1]','nvarchar(max)') ,1,1,'') , '')
, [isOpen]
FROM fi;
UPDATE s
SET s.[invoiceid] = [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid], inv.[Invoices])
,[open] = 1 --CASE WHEN s.[workdoneat] is not null AND [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid],'') = '' AND ISNULL(inv.[isOpen],1) = 1 THEN 1 ELSE 0 END
FROM @SReq as s LEFT JOIN @inv as inv on s.[id] = inv.[id];
DECLARE @step TABLE (
[Id] [bigint] NOT NULL,
[MobileId] [bigint] NULL,
[Version] [int] NOT NULL,
[Name] [nvarchar](255) NULL,
[IsDone] [bit] NOT NULL,
[HasError] [bit] NOT NULL,
[TrackingId] [nvarchar](255) NULL,
[Type] [nvarchar](255) NULL,
[SortOrder] [int] NOT NULL,
[Data] [nvarchar](max) NULL,
[DateModifiedOffline] [datetime] NULL,
[ServiceRequestId] [bigint] NULL,
[Description] [nvarchar](max) NULL,
[Comment] [nvarchar](max) NULL,
[InternalComment] [nvarchar](max) NULL,
[ServiceObjectId] [bigint] NULL,
[StepListTemplateId] [bigint] NULL,
[ParentId] [bigint] NULL,
[order] int NOT NULL
);
DECLARE @company TABLE (
[Id] [bigint] NOT NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NOT NULL,
[IsPhysicalPerson] [bit] NOT NULL,
[IsOwner] [bit] NOT NULL,
[IsEmailInvoicingActive] [bit] NOT NULL,
[IsSupplier] [bit] NOT NULL,
[MappingId] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[Name] [nvarchar](255) NULL,
[Note] [nvarchar](255) NULL,
[SupportTelephone] [nvarchar](255) NULL,
[SupportFax] [nvarchar](255) NULL,
[SupportMail] [nvarchar](255) NULL,
[QuickSearch] [nvarchar](255) NULL,
[DateModified] [datetime] NOT NULL,
[CustomValues] [nvarchar](max) NOT NULL,
[MainContactId] [bigint] NOT NULL,
[address] nvarchar(1000) null
);
INSERT INTO @company SELECT DISTINCT cy.*, [address] = [dbo].[fds__getCompanyAddress](cy.[id]) FROM [mfr__companies] as cy JOIN @SReq as s on cy.id = s.[CustomerId];
DECLARE @companyids [dbo].[fds__tt__idlist];
INSERT INTO @companyids SELECT DISTINCT [id] FROM @company;
/** OUTPUT + get other types **/
IF @mode in ('r','ov', '','f', 'ful')
BEGIN
WITH app as(
SELECT [start] = MIN(CAST(CASE WHEN a.[StartDateTime] is null THEN s.[workdoneat] WHEN CAST(a.[StartDateTime] as date) > s.[workdoneat] THEN CAST(a.[StartDateTime] as date) ELSE s.[workdoneat] END as date))
, [end] = MAX(CAST(CASE WHEN a.[EndDateTime] is null THEN s.[workdoneat] WHEN CAST(a.[EndDateTime] as date) > s.[workdoneat] THEN CAST(a.[EndDateTime] as date) ELSE s.[workdoneat] END as date))
, [CustomerId] = MAX (s.customerid)
, [ParentServiceRequestId] = ISNULL(MIN(CASE WHEN [isChild] = 1 then null else s.[Id] END), MAX([ParentServiceRequestId])) --make sure only one returned
FROM [dbo].[mfr__appointments] as a
RIGHT JOIN @SReq as s on a.[ServiceRequestId] = s.[Id] AND a.[State] in ('IsWorkDone')
)
SELECT TOP(1)
[today]
, [provisionstart] = CASe WHEN [start] = [end] THEN NULL ELSE [start] END
, [provisionend] = [end]
, [sender] = N'Sebastian Fuchs GmbH & Co. KG ● Germaniastraße 15 ● 40223 Düsseldorf'
, [paymentterms] = N'10wd'
, [invoiceemail] = (SELECT TOP(1) [SupportMail] FROM @company where IsEmailInvoicingActive = 1)
, [invoiceaddress] = (SELECT TOP(1) CONCAT([name], CHAR(10), [address]) FROM @company ORDER BY IsEmailInvoicingActive DESC)
, [tax_servicerefund] = 0.2
, [CustomerId] = [CustomerId]
, [search] = @servicerequestid
, [parent] = app.[ParentServiceRequestId]
FROM (VALUES(CAST(GETDATE() as date)))base ([today]) CROSS JOIN app;
SELECT
*
,[order] = ROW_NUMBER() OVER (ORDER BY [baseorder])
FROM @SReq;
END
IF @mode in ('i','ov')
BEGIN
SELECT
itm.[Id]
, [net_pos] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN '' ELSE FORMAT( CAST( (ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0) as numeric(7,2)) , '#0.00 €','de') END
, [bo_pos] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN '' ELSE FORMAT( CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2)) , '#0.00 €','de') END
, [vat] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN null ELSE FORMAT(ISNULL(vat.[vat], 19.0), '#0.0','de') + '%' END
, itm.[ServiceRequestId]
, itm.[SortOrder]
, itm.[Type]
, itm.[NameOrNumber]
, itm.[Note]
, [order] = (s.[baseorder] * 100) + ROW_NUMBER() OVER (PARTITION BY itm.[ServiceRequestId] ORDER BY itm.[SortOrder])
FROM [dbo].[mfr__items] as itm
JOIN @SReq as s ON itm.ServiceRequestId = s.[Id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
;
END
ELSE IF @mode in ('f', 'ful')
BEGIN
SELECT
itm.[Id]
, [net] = ISNULL([Price],0)
, [quantityhours] = ISNULL([quantityhours],1)
, itm.[UnitString]
, [net_val] = CAST( (ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0) as numeric(7,2))
, [bo_val] = CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2))
, [vat] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN null ELSE FORMAT(ISNULL(vat.[vat], 19.0), '#0.0','de') + '%' END
, [vat_val] = CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * ((ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2))
, itm.[ServiceRequestId]
, itm.[SortOrder]
, itm.[Type]
, itm.[NameOrNumber]
, itm.[Note]
, [order] = (s.[baseorder] * 100) + ROW_NUMBER() OVER (PARTITION BY itm.[ServiceRequestId] ORDER BY itm.[SortOrder], itm.[Id])
FROM [dbo].[mfr__items] as itm
JOIN @SReq as s ON itm.ServiceRequestId = s.[Id]
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item'
;
END
IF @mode in ('f', 'ful')
BEGIN
INSERT INTO @step
SELECT
stp.*
, [order] = (s.[baseorder] * 100) + ROW_NUMBER() OVER (PARTITION BY stp.[ServiceRequestId] ORDER BY stp.[SortOrder], stp.[Id])
FROM [dbo].[mfr__steps] as stp JOIN @SReq as s ON stp.ServiceRequestId = s.[Id];
SELECT * from @step;
SELECT
cc.[Id]
, [CompanyName] = cc.[name]
, ct.[Gender]
, ct.[FirstName]
, ct.[LastName]
, ct.[Email]
, cc.[address]
FROM [dbo].[fds__getCompanysContacts](@companyids) as ct join @company as cc on cc.[Id] = ct.[CyId];
SELECT DISTINCT
[ref] = 'Einsatzort ' + so.[name]
,[address] = [dbo].[fds__fn_combineAddress] (so.[Name], loc.[AddressString], null
,loc.[Postal]
,loc.[City]
,loc.[State]
,loc.[Country])
FROM @SReq as s
LEFT JOIN [mfr__*PartnerSet] as p ON p.property = 'ServiceRequest:ServiceObjects' and p.[EntityId] = s.[id]
LEFT JOIN [mfr__serviceobjects] as so ON so.id = p.[PartnerId]
LEFT JOIN [mfr__*PartnerSet] as po ON p.property = 'ServiceObject:Location' and po.[EntityId] = so.[Id]
LEFT join [dbo].[mfr__#locations] as loc ON (loc.[id] = so.[Location#ID] OR po.[PartnerId] = loc.[Id] OR (so.[Id] = loc.[EntityId] and loc.Property = 'ServiceObject:Location'))
WHERE so.id is not null and loc.[id] is not null
UNION
SELECT DISTINCT
[ref] = 'Auftragsort ' + [name]
,[address] = [dbo].[fds__fn_combineAddress] (loc.[AddressString], null, null
,loc.[Postal]
,loc.[City]
,loc.[State]
,loc.[Country])
FROM @SReq as s
LEFT JOIN [mfr__*PartnerSet] as p ON p.property = 'ServiceRequest:Location' and p.[EntityId] = s.[id]
join [dbo].[mfr__#locations] as loc ON (s.[Location#ID] = loc.[id] OR p.[PartnerId] = loc.[Id] OR (s.[Id] = loc.[EntityId] and loc.Property = 'ServiceRequest:Location'))
WHERE loc.[id] is not null
UNION
SELECT DISTINCT [ref] = 'Kunde'
, [address]
FROM @company;
END
--get invoices
--Reminder settings
DECLARE @stage1 varchar(10), @stage2 varchar(10), @stage3 varchar(10);
SELECT @stage1 = [stage1], @stage2 = [stage2], @stage3 = [stage3] FROM [dbo].[fds__admin_reminderSettings]();
With inv as(
SELECT [id] = CAST(ir.invid as varchar(20)), [isChild] = CAST(MIN(CAST(s.[isChild] as tinyint)) as BIT), [baseorder] = MIN(s.[baseorder]), [sopen] = CAST( MIN( CAST(s.[open] as tinyint) ) as bit)
FROM [dbo].[fds__invoice_servicerequests] as ir
JOIN @sreq as s ON ir.[mfr__servicerequest] = s.[id]
GROUP by ir.invid
UNION
SELECT [id] = CAST(i.[id] as varchar(20)), [isChild] = CAST(MIN(CAST(s.[isChild] as tinyint)) as BIT), [baseorder] = MIN(s.[baseorder]), [sopen] = CAST( MIN( CAST(s.[open] as tinyint) ) as bit)
FROM [dbo].[mfr__servicerequests] as ms JOIN @sreq as s on ms.[id] = s.[id]
JOIN [mfr__invoices] as i on ms.[InvoiceId] like ('%' + i.[InvoiceId] + '%')
GROUP by i.[id]
),reminder as (
SELECT [InvId], [lastReminder] = MAX([DateSent]), [#] = COUNT(*) FROM [dbo].[fds__reminder] WHERE [isSent] = 1 AND [invId] IN (SELECT [Id] FROM inv) GROUP BY [InvId]
)
SELECT [Id] = CAST(i.[Id] as varchar(20))
,i.[DateCreated]
,[isFinal]
,[isSent]
,i.[DocumentName]
,[invstatus] = CASE WHEN [IsPayed] = 1 AND [isCanceled] = 0 THEN 'pyd'
WHEN [isCanceled] = 1 OR [dbo].[fds__fn_isInvoiceCancelled](inv.[Id]) = 1 THEN 'cc'
WHEN [isFinal] = 0 THEN 'dft'
WHEN [isFinal] = 1 AND [isSent] = 0 THEN 'uns'
WHEN [DueDate] is null THEN 'op'
WHEN [invoiceBalance] < 0 THEN ''
WHEN @today >= [DueDate] THEN 'due'
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, i.[DateCreated]), @stage1) THEN 'ovd'
WHEN ISNULL(rem.[#],0) > 0 THEN 'rem'
END
,[order] = ROW_NUMBER() OVER ( ORDER BY inv.[isChild], inv.[baseorder], i.[isFinal] DESC, i.[datecreated] ASC )
FROM [dbo].[fds__invoices] as i
JOIN inv on i.[Id] = inv.[Id]
LEFT JOIN reminder as rem on inv.id = rem.[InvId]
WHERE (inv.[sopen] = 1 OR i.[isfinal] = 1) --- don't show any draft invoices for final tasks;
UNION
SELECT [Id] = CAST(i.[Id] as varchar(20))
,[DateCreated] = i.[DateOfCreation]
,[isFinal] = 1
,[isSent] = 1
,i.[DocumentName]
,[invstatus] = CASE WHEN [invoicestate] in ('eIsPaid') THEN 'pyd'
WHEN [invoicestate] in ('eIsCancelled') THEN 'cc'
--WHEN [isFinal] = 0 THEN 'dft'
--WHEN [isFinal] = 1 AND [isSent] = 0 THEN 'uns'
WHEN [DueDate] is null THEN 'op'
WHEN @today >= [DueDate] THEN 'due'
WHEN ISNULL(rem.[#],0) > 0 THEN 'rem'
WHEN @stage1 <> '' AND ISNULL(rem.[#],0) = 0 AND @today > [dbo].[date_addcustom](ISNULL(DueDate, i.[DateofCreation]), @stage1) THEN 'ovd'
END
,[order] = ROW_NUMBER() OVER ( ORDER BY inv.[isChild], inv.baseorder, i.[dateofcreation] ASC)
FROM [dbo].[mfr__invoices] as i
JOIN inv on TRY_CAST(i.[Id] as varchar(20)) = inv.[Id]
LEFT JOIN reminder as rem on inv.id = rem.[InvId];
END
@@ -1,278 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getRequests_list]
@tgtdate date
,@mode varchar(1) = ''
,@completed bit = 1
,@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_req', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getRequests_list' ,@authuser , '');
DECLARE @startdate date, @enddate date, @today date = GETDATE();
SET @mode = ISNULL(LOWER(@mode),'');
If @mode in ('r') -- r = open recent
BEGIN
SET @tgtdate = @today;
SELECT @startdate = DATEADD(week, -4, [dbo].[date_weekfirst](@tgtdate))
,@enddate = @today;
END
else If @mode in ('m') -- r = open recent
BEGIN
SELECT @startdate = [dbo].[date_monthfirst](@tgtdate)
,@enddate = [dbo].[date_monthend](@tgtdate);
END
ELSE If @mode = 'w'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst](@tgtdate)
,@enddate = [dbo].[date_weekend](@tgtdate);
END
DECLARE @openmode as bit = CASE WHEN LOWER(LEFT(ISNULL(@mode, ''),1)) IN ('o', 'r') THEN 1 ELSE 0 END;
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode
, [title] = CASE WHEN @openmode = 1 AND @mode = 'r' THEN 'Übersicht der offenen Aufträge (letzte 4 Wochen seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ')'
WHEN @openmode = 1 THEN 'Übersicht der offenen Aufträge'
ELSE 'Auftragsübersicht für ' + CASE WHEN @mode = 'm' THEN 'MONAT ' + FORMAT(@startdate, 'MMM yyyy')
WHEN @mode = 'w' THEN 'WOCHE ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' - ' + FORMAT(@enddate, 'dd.MM.')
ELSE '' END
END
, [note] = CASE WHEN @openmode = 1 and @mode = 'r' THEN 'Es werden alle Aufträge angezeigt, die selbst offen sind und seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' erstellt wurden, oder deren Folgeauftrag offen ist und/oder seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' erstellt wurden.'
WHEN @openmode = 1 THEN 'Es werden alle Aufträge angezeigt, die selbst oder ein Folgeeauftrag offen ist.'
ELSE '' END;
DECLARE @SReq TABLE (
[Id] [bigint] NOT NULL,
[Name] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[InvoiceId] varchar(1000) NULL,
[ClosedAt] [datetime] NULL,
[ReleasedAt] [datetime] NULL,
[WorkDoneAt] [datetime] NULL,
[TargetTimeInMinutes] [bigint] NULL,
[DateModified] [datetime] NULL,
[DateOfCreation] [datetime] NULL,
[DueDateRangeStart] [datetime] NULL,
[DueDateRangeEnd] [datetime] NULL,
[PortalLink] [nvarchar](255) NULL,
[CostCenterId] [bigint] NULL,
[Description] [nvarchar](max) NULL,
[State] [nvarchar](255) NULL,
[CustomValues] [nvarchar](max) NULL,
[CurrentOwnerId] [bigint] NULL,
[CustomerId] [bigint] NULL,
[ParentServiceRequestId] [bigint] NULL,
[ParentExtenalId] [nvarchar](255) NULL,
[ParentWorkDoneAt] [date] NULL,
[ParentInvoiceId] varchar(1000) NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NULL,
[IsTemplate] [bit] NULL,
[IsTemplateMobile] [bit] NULL,
[CreateFromServiceRequestTemplateId] [bigint] NULL,
[Type] [nvarchar](255) NULL,
[CustomerName] [nvarchar](255) NULL,
[isChild] bit NULL,
[open] bit,
[baseorder] int
);
DECLARE @inv TABLE ([Id] bigint, [Invoices] varchar(1000), [isOpen] bit);
With fdsinv as(
SELECT [Id] = fdsr.[mfr__servicerequest], fdsi.[InvoiceId], fdsi.[InvoiceType], [isCanceled] = ISNULL(fdsi.[isCanceled],0) FROM [dbo].[fds__invoice_servicerequests] as fdsr
JOIN [dbo].[fds__invoices] as fdsi on fdsr.[invid] = fdsi.[id]
WHERE [isFinal] = 1 AND fdsr.[mfr__servicerequest] is not null
), fi as(
SELECT [Id], [isOpen] = MIN(CASE WHEN ISNULL([invoicetype],'r') = 'r' AND [isCanceled] = 0 THEN 0 ELSE 1 END) FROM fdsinv GROUP BY [Id]
)
INSERT INTO @inv
SELECT [Id]
, [invoices] = ISNULL(STUFF( (SELECT CHAR(10) + [InvoiceId] + ' (' + [InvoiceTYpe] + ')' FROM fdsinv as f2 WHERE f2.[id] = fi.[Id] FOR XML PATH, TYPE).value('.[1]','nvarchar(max)') ,1,1,'') , '')
, [isOpen]
FROM fi;
insert into @SReq
SELECT
s.[Id]
,[Name]
,[ExternalId]
,[InvoiceId] = [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid], inv.[Invoices])
,[ClosedAt]
,[ReleasedAt]
,[WorkDoneAt]
,[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,[DueDateRangeStart]
,[DueDateRangeEnd]
,[PortalLink]
,[CostCenterId]
,[Description]
,[State]
,s.[CustomValues]
,[CurrentOwnerId]
,s.[CustomerId]
,[ParentServiceRequestId]
,[ParentExtenalId] = cast(CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then null ELSE '?' END as nvarchar(255))
,[ParentWorkDoneAt] = cast(NULL as datetime)
,[ParentInvoiceId] = cast(NULL as nvarchar(255))
,[Location#ID]
,s.[Version]
,[IsTemplate]
,[IsTemplateMobile]
,[CreateFromServiceRequestTemplateId]
,[Type]
,[CustomerName] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[open] = CASE WHEN s.[workdoneat] is not null AND [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid],'') = '' AND ISNULL(inv.[isOpen],1) = 1 THEN 1 ELSE 0 END
,[baseorder] = 0
FROM [dbo].[mfr__servicerequests] as s -- parent and childs;
LEFT JOIN @inv as inv on inv.[Id] = s.[Id]
WHERE
([type] in ('IsProject', 'IsMaintenance')
AND
(CASE WHEN @openmode = 1 AND @mode = 'r' THEN
( CASE WHEN
[workdoneat] is not null
--AND ISNULL(s.[invoiceid],'') = ''
AND ISNULL([State], '') <> 'Closed'
--AND ISNULL(inv.[isOpen],1) = 1
AND NOT EXISTS (SELECT * FROM [dbo].[fds__custom_servicerequest] as sr where sr.[EntityID] = s.[id] and sr.[hidden] = 1)
AND (CAST([DateOfCreation] as date) BETWEEN @startdate AND @today OR CAST([DateModified] as date) BETWEEN @startdate AND @today)
AND EXISTS (SELECT 0 FROM [dbo].[mfr__*PartnerSet] as ps JOIN [dbo].[mfr__tags] as tg on ps.Property = 'ServiceRequest:Tags' and tg.Id = ps.[PartnerId] where EntityId = s.[id] and ps.partnerid = 16034758659 and [Type] = 'ServiceRequest')
THEN 1 ELSE 0 END)
WHEN @openmode = 1 AND @mode <> 'r' THEN
( CASE WHEN
[workdoneat] is not null
AND ISNULL(s.[invoiceid],'') = ''
AND ISNULL([State], '') <> 'Closed'
AND ISNULL(inv.[isOpen],1) = 1
AND NOT EXISTS (SELECT * FROM [dbo].[fds__custom_servicerequest] as sr where sr.[EntityID] = s.[id] and sr.[hidden] = 1)
THEN 1 ELSE 0 END)
WHEN @openmode = 0 THEN
( CASE WHEN CAST([DateOfCreation] as date) BETWEEN @startdate AND @enddate THEN 1 ELSE 0 END)
ELSE 0 END
) = 1);
INSERT INTO @SReq
SELECT DISTINCT
ISNULL(s.[Id], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN q.[ParentServiceRequestId] ELSE NULL END)
,ISNULL(s.[Name], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN ' - - unbekannt - - ' ELSE NULL END)
,s.[ExternalId]
,[InvoiceId] = [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid], inv.[Invoices])
,s.[ClosedAt]
,s.[ReleasedAt]
,s.[WorkDoneAt]
,s.[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,s.[DueDateRangeStart]
,s.[DueDateRangeEnd]
,s.[PortalLink]
,s.[CostCenterId]
,s.[Description]
,s.[State]
,s.[CustomValues]
,s.[CurrentOwnerId]
,s.[CustomerId]
,s.[ParentServiceRequestId]
,[ParentExtenalId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[ExternalId] ELSE NULL END
,[ParentWorkDoneAt] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[WorkDoneAt] ELSE NULL END
,[ParentInvoiceId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[InvoiceId] ELSE NULL END
,s.[Location#ID]
,s.[Version]
,s.[IsTemplate]
,s.[IsTemplateMobile]
,s.[CreateFromServiceRequestTemplateId]
,s.[Type]
,[CustomerName] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[open] = CASE WHEN s.[workdoneat] is not null AND [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid],'') = '' AND ISNULL(inv.[isOpen],1) = 1 THEN 1 ELSE 0 END
,[baseorder] = 0
from [dbo].[mfr__servicerequests] as s RIGHT JOIN @SReq as q ON (s.[ParentServiceRequestId] = q.[id] OR q.[ParentServiceRequestId] = s.[id]) --parents or childs
LEFT JOIN @inv as inv on inv.[Id] = s.[Id]
WHERE
(s.[type] in ('IsProject', 'IsMaintenance')
AND NOT EXISTS (SELECT * FROM @SReq as s2 WHERE s2.[id] = s.[Id])
AND ISNULL(s.[Id], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN q.[ParentServiceRequestId] ELSE NULL END) is not null
)
;
WITH q as (
SELECT [id], [baseorder] = (ROW_NUMBER() OVER (ORDER BY ISNULL([ParentWorkDoneAt], [WorkDoneAt]), CASE WHEN ISNULL([ParentServiceRequestId],0) = 0 THEN [id] ELSE [ParentServiceRequestId] END) * 100)
FROM @SReq as s
WHERE [ischild] = 0
)
UPDATE s SET
[baseorder] = q.baseorder
,[ParentServiceRequestId] = CASE WHEN s.isChild = 0 THEN s.[Id] ELSE s.[ParentServiceRequestId] END
FROM @SReq as s JOIN q ON s.[id] = q.[id];
UPDATE s SET
[CustomerName] = c.[name]
FROM @SReq as s JOIN [mfr__companies] as c ON s.[CustomerId] = c.[id];
WITH q as (
SELECT s.[id]
, q.[ExternalId], q.[WorkDoneAt], q.[InvoiceId]
, [baseorder] = q.[baseorder] + ROW_NUMBER() OVER (PARTITION BY q.[id] ORDER BY s.[dateofcreation], s.[Id])
FROM @SReq as s JOIN @SReq as q ON s.[ParentServiceRequestId] = q.[id]
WHERE s.[ischild] = 1
)
UPDATE s SET
[ParentExtenalId] = q.[ExternalId]
,[ParentWorkDoneAt] = q.[WorkDoneAt]
,[ParentInvoiceId] = q.[InvoiceId]
,[baseorder] = q.[baseorder]
FROM @SReq as s JOIN q ON s.[Id] = q.[id];
---OUTPUT
WITH tgs as(
SELECT ps.[EntityId], tg.* FROM [dbo].[mfr__tags] as tg JOIN [dbo].[mfr__*PartnerSet] as ps ON ps.[Property] = 'ServiceRequest:Tags' and tg.[Id] = ps.[PartnerId] AND ISNULL(tg.[Name],'') <> '' and [Type] = 'ServiceRequest'
WHERE tg.Id in (16288120843
,15842181123
,16034758659
,16050520064
,16783966210
,16802480128
,16830398466
,19650019330
,19670532096
,18925977600)
AND EXISTS (SELECT 0 FROM @SReq as _s where _s.Id = ps.[EntityId])
)
SELECT
s.*
,[tags] = STUFF( (SELECT ',' + TRIM([Name]) FROM tgs where tgs.[EntityId] = s.[Id] order by tgs.[name] FOR XML PATH, TYPE).value('.[1]','varchar(255)'), 1,1,'')
FROM @SReq as s order by baseorder
SELECT
[requestID] = s.[Id]
,r.*
,[order] = ROW_NUMBER() OVER (PARTITION BY s.[Id] ORDER BY r.[dateofcreation] ASC, r.[Id])
FROM [dbo].[mfr__invoices] as r
JOIN [dbo].[mfr__*PartnerSet] as ps ON ps.[Property] like 'ServiceRequest:Invoices' and r.[id] = ps.PartnerId
JOIN @SReq as s ON s.[Id] = ps.[EntityId]
ORDER BY s.[Id], r.[dateofcreation] ASC;
END
@@ -1,308 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__getRequests_list2]
@tgtdate date
,@mode varchar(1) = ''
,@completed bit = 1
, @search varchar(100)
,@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_req', @authuser) < 1
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__getRequests_list' ,@authuser , @search);
DECLARE @startdate date, @enddate date, @today date = GETDATE();
DECLARE @searchmode as varchar(1) = CASE WHEN SUBSTRING(ISNULL(@search, '') + ' ', 2,1) <> ':' THEN '' ELSE LOWER(LEFT(@search,1 )) END;
IF @searchmode <> ''
BEGIN
SELECT @mode = 's', @search = SUBSTRING(@search,3, 1000);
END
ELSE
SET @mode = LOWER(@mode);
If @mode in ('r') -- r = open recent
BEGIN
SET @tgtdate = @today;
SELECT @startdate = DATEADD(week, -4, [dbo].[date_weekfirst](@tgtdate))
,@enddate = @today;
END
else If @mode in ('m') -- r = open recent
BEGIN
SELECT @startdate = [dbo].[date_monthfirst](@tgtdate)
,@enddate = [dbo].[date_monthend](@tgtdate);
END
ELSE If @mode = 'w'
BEGIN
SELECT @startdate = [dbo].[date_weekfirst](@tgtdate)
,@enddate = [dbo].[date_weekend](@tgtdate);
END
DECLARE @openmode as bit = CASE WHEN LOWER(LEFT(ISNULL(@mode, ''),1)) IN ('o', 'r') THEN 1 ELSE 0 END;
SELECT [startdate] = @startdate, [enddate] = @enddate, [mode] = @mode
, [title] = CASE WHEN @searchmode = 'n' THEN 'Übersicht der Aufträge mit der Auftragsnummer ' + @search
WHEN @openmode = 1 AND @mode = 'r' THEN 'Übersicht der offenen Aufträge (letzte 4 Wochen seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ')'
WHEN @openmode = 1 THEN 'Übersicht der offenen Aufträge'
ELSE 'Auftragsübersicht für ' + CASE WHEN @mode = 'm' THEN 'MONAT ' + FORMAT(@startdate, 'MMM yyyy')
WHEN @mode = 'w' THEN 'WOCHE ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' - ' + FORMAT(@enddate, 'dd.MM.')
ELSE '' END
END
, [note] = CASE WHEN @searchmode = 'n' THEN 'Es wird nur der Auftrag mit der Nummer ' + @search + ' angezeigt'
WHEN @openmode = 1 and @mode = 'r' THEN 'Es werden alle Aufträge angezeigt, die selbst offen sind und seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' erstellt wurden, oder deren Folgeauftrag offen ist und/oder seit ' + FORMAT(@startdate, 'dd.MM.yyyy') + ' erstellt wurden.'
WHEN @openmode = 1 THEN 'Es werden alle Aufträge angezeigt, die selbst oder ein Folgeeauftrag offen ist.'
ELSE '' END;
DECLARE @SReq TABLE (
[Id] [bigint] NOT NULL,
[Name] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[InvoiceId] varchar(1000) NULL,
[ClosedAt] [datetime] NULL,
[ReleasedAt] [datetime] NULL,
[WorkDoneAt] [datetime] NULL,
[TargetTimeInMinutes] [bigint] NULL,
[DateModified] [datetime] NULL,
[DateOfCreation] [datetime] NULL,
[DueDateRangeStart] [datetime] NULL,
[DueDateRangeEnd] [datetime] NULL,
[PortalLink] [nvarchar](255) NULL,
[CostCenterId] [bigint] NULL,
[Description] [nvarchar](max) NULL,
[State] [nvarchar](255) NULL,
[CustomValues] [nvarchar](max) NULL,
[CurrentOwnerId] [bigint] NULL,
[CustomerId] [bigint] NULL,
[ParentServiceRequestId] [bigint] NULL,
[ParentExtenalId] [nvarchar](255) NULL,
[ParentWorkDoneAt] [date] NULL,
[ParentInvoiceId] varchar(1000) NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NULL,
[IsTemplate] [bit] NULL,
[IsTemplateMobile] [bit] NULL,
[CreateFromServiceRequestTemplateId] [bigint] NULL,
[Type] [nvarchar](255) NULL,
[CustomerName] [nvarchar](255) NULL,
[isChild] bit NULL,
[open] bit,
[baseorder] int
);
DECLARE @inv TABLE ([Id] bigint, [Invoices] varchar(1000), [isOpen] bit);
With fdsinv as(
SELECT [Id] = fdsr.[mfr__servicerequest], fdsi.[InvoiceId], fdsi.[InvoiceType], [isCanceled] = ISNULL(fdsi.[isCanceled],0) FROM [dbo].[fds__invoice_servicerequests] as fdsr
JOIN [dbo].[fds__invoices] as fdsi on fdsr.[invid] = fdsi.[id]
WHERE [isFinal] = 1 AND fdsr.[mfr__servicerequest] is not null
), fi as(
SELECT [Id], [isOpen] = MIN(CASE WHEN ISNULL([invoicetype],'r') = 'r' AND [isCanceled] = 0 THEN 0 ELSE 1 END) FROM fdsinv GROUP BY [Id]
)
INSERT INTO @inv
SELECT [Id]
, [invoices] = ISNULL(STUFF( (SELECT CHAR(10) + [InvoiceId] + ' (' + [InvoiceTYpe] + ')' FROM fdsinv as f2 WHERE f2.[id] = fi.[Id] FOR XML PATH, TYPE).value('.[1]','nvarchar(max)') ,1,1,'') , '')
, [isOpen]
FROM fi;
WITH s as (
SELECT * FROM [dbo].[mfr__servicerequests] as _s
WHERE (CASE
WHEN @searchmode = 'n' THEN
( CASE WHEN [ExternalId] = @search OR [ExternalId] like ('_[-]' + @search) THEN 1 ELSE 0 END )
WHEN @openmode = 1 AND @mode = 'r' THEN
( CASE WHEN
[workdoneat] is not null
--AND ISNULL(s.[invoiceid],'') = ''
AND ISNULL([State], '') <> 'Closed'
--AND ISNULL(inv.[isOpen],1) = 1
AND NOT EXISTS (SELECT * FROM [dbo].[fds__custom_servicerequest] as sr where sr.[EntityID] = _s.[id] and sr.[hidden] = 1)
AND (CAST([DateOfCreation] as date) BETWEEN @startdate AND @today OR CAST([DateModified] as date) BETWEEN @startdate AND @today)
AND EXISTS (SELECT 0 FROM [dbo].[mfr__*PartnerSet] as ps JOIN [dbo].[mfr__tags] as tg on ps.Property = 'ServiceRequest:Tags' and tg.Id = ps.[PartnerId] where EntityId = _s.[id] and ps.partnerid = 16034758659 and [Type] = 'ServiceRequest')
THEN 1 ELSE 0 END)
WHEN @openmode = 1 AND @mode <> 'r' THEN -- will be covered later because invoice info is included
1
--( CASE WHEN
-- [workdoneat] is not null
-- AND ISNULL(s.[invoiceid],'') = ''
-- AND ISNULL([State], '') <> 'Closed'
-- AND ISNULL(inv.[isOpen],1) = 1
-- AND NOT EXISTS (SELECT * FROM [dbo].[fds__custom_servicerequest] as sr where sr.[EntityID] = s.[id] and sr.[hidden] = 1)
-- THEN 1 ELSE 0 END)
WHEN @openmode = 0 THEN
( CASE WHEN CAST([DateOfCreation] as date) BETWEEN @startdate AND @enddate THEN 1 ELSE 0 END)
ELSE 0 END
) = 1
)
insert into @SReq
SELECT
s.[Id]
,[Name]
,[ExternalId]
,[InvoiceId] = [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid], inv.[Invoices])
,[ClosedAt]
,[ReleasedAt]
,[WorkDoneAt]
,[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,[DueDateRangeStart]
,[DueDateRangeEnd]
,[PortalLink]
,[CostCenterId]
,[Description]
,[State]
,s.[CustomValues]
,[CurrentOwnerId]
,s.[CustomerId]
,[ParentServiceRequestId]
,[ParentExtenalId] = cast(CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then null ELSE '?' END as nvarchar(255))
,[ParentWorkDoneAt] = cast(NULL as datetime)
,[ParentInvoiceId] = cast(NULL as nvarchar(255))
,[Location#ID]
,s.[Version]
,[IsTemplate]
,[IsTemplateMobile]
,[CreateFromServiceRequestTemplateId]
,[Type]
,[CustomerName] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[open] = CASE WHEN s.[workdoneat] is not null AND [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid],'') = '' AND ISNULL(inv.[isOpen],1) = 1 THEN 1 ELSE 0 END
,[baseorder] = 0
FROM s -- parent and childs;
LEFT JOIN @inv as inv on inv.[Id] = s.[Id]
WHERE
([type] in ('IsProject', 'IsMaintenance')
AND
(CASE
WHEN @openmode = 1 AND @mode <> 'r' THEN
( CASE WHEN
[workdoneat] is not null
AND ISNULL(s.[invoiceid],'') = ''
AND ISNULL([State], '') <> 'Closed'
AND ISNULL(inv.[isOpen],1) = 1
AND NOT EXISTS (SELECT * FROM [dbo].[fds__custom_servicerequest] as sr where sr.[EntityID] = s.[id] and sr.[hidden] = 1)
THEN 1 ELSE 0 END)
ELSE 1 END -- heres the difference, because main filter is applied in CTE
) = 1);
INSERT INTO @SReq
SELECT DISTINCT
ISNULL(s.[Id], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN q.[ParentServiceRequestId] ELSE NULL END)
,ISNULL(s.[Name], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN ' - - unbekannt - - ' ELSE NULL END)
,s.[ExternalId]
,[InvoiceId] = [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid], inv.[Invoices])
,s.[ClosedAt]
,s.[ReleasedAt]
,s.[WorkDoneAt]
,s.[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,s.[DueDateRangeStart]
,s.[DueDateRangeEnd]
,s.[PortalLink]
,s.[CostCenterId]
,s.[Description]
,s.[State]
,s.[CustomValues]
,s.[CurrentOwnerId]
,s.[CustomerId]
,s.[ParentServiceRequestId]
,[ParentExtenalId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[ExternalId] ELSE NULL END
,[ParentWorkDoneAt] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[WorkDoneAt] ELSE NULL END
,[ParentInvoiceId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[InvoiceId] ELSE NULL END
,s.[Location#ID]
,s.[Version]
,s.[IsTemplate]
,s.[IsTemplateMobile]
,s.[CreateFromServiceRequestTemplateId]
,s.[Type]
,[CustomerName] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[open] = CASE WHEN s.[workdoneat] is not null AND [dbo].[fds__fn_InvoiceIDs_mfr](s.[invoiceid],'') = '' AND ISNULL(inv.[isOpen],1) = 1 THEN 1 ELSE 0 END
,[baseorder] = 0
from [dbo].[mfr__servicerequests] as s RIGHT JOIN @SReq as q ON (s.[ParentServiceRequestId] = q.[id] OR q.[ParentServiceRequestId] = s.[id]) --parents or childs
LEFT JOIN @inv as inv on inv.[Id] = s.[Id]
WHERE
(s.[type] in ('IsProject', 'IsMaintenance')
AND NOT EXISTS (SELECT * FROM @SReq as s2 WHERE s2.[id] = s.[Id])
AND ISNULL(s.[Id], CASE WHEN ISNULL(q.[ParentServiceRequestId],0) > 0 THEN q.[ParentServiceRequestId] ELSE NULL END) is not null
)
;
WITH q as (
SELECT [id], [baseorder] = (ROW_NUMBER() OVER (ORDER BY ISNULL([ParentWorkDoneAt], [WorkDoneAt]), CASE WHEN ISNULL([ParentServiceRequestId],0) = 0 THEN [id] ELSE [ParentServiceRequestId] END) * 100)
FROM @SReq as s
WHERE [ischild] = 0
)
UPDATE s SET
[baseorder] = q.baseorder
,[ParentServiceRequestId] = CASE WHEN s.isChild = 0 THEN s.[Id] ELSE s.[ParentServiceRequestId] END
FROM @SReq as s JOIN q ON s.[id] = q.[id];
UPDATE s SET
[CustomerName] = c.[name]
FROM @SReq as s JOIN [mfr__companies] as c ON s.[CustomerId] = c.[id];
WITH q as (
SELECT s.[id]
, q.[ExternalId], q.[WorkDoneAt], q.[InvoiceId]
, [baseorder] = q.[baseorder] + ROW_NUMBER() OVER (PARTITION BY q.[id] ORDER BY s.[dateofcreation], s.[Id])
FROM @SReq as s JOIN @SReq as q ON s.[ParentServiceRequestId] = q.[id]
WHERE s.[ischild] = 1
)
UPDATE s SET
[ParentExtenalId] = q.[ExternalId]
,[ParentWorkDoneAt] = q.[WorkDoneAt]
,[ParentInvoiceId] = q.[InvoiceId]
,[baseorder] = q.[baseorder]
FROM @SReq as s JOIN q ON s.[Id] = q.[id];
---OUTPUT
WITH tgs as(
SELECT ps.[EntityId], tg.* FROM [dbo].[mfr__tags] as tg JOIN [dbo].[mfr__*PartnerSet] as ps ON ps.[Property] = 'ServiceRequest:Tags' and tg.[Id] = ps.[PartnerId] AND ISNULL(tg.[Name],'') <> '' and [Type] = 'ServiceRequest'
WHERE tg.Id in (16288120843
,15842181123
,16034758659
,16050520064
,16783966210
,16802480128
,16830398466
,19650019330
,19670532096
,18925977600)
AND EXISTS (SELECT 0 FROM @SReq as _s where _s.Id = ps.[EntityId])
)
SELECT
s.*
,[tags] = STUFF( (SELECT ',' + TRIM([Name]) FROM tgs where tgs.[EntityId] = s.[Id] order by tgs.[name] FOR XML PATH, TYPE).value('.[1]','varchar(255)'), 1,1,'')
FROM @SReq as s order by baseorder
SELECT
[requestID] = s.[Id]
,r.*
,[order] = ROW_NUMBER() OVER (PARTITION BY s.[Id] ORDER BY r.[dateofcreation] ASC, r.[Id])
FROM [dbo].[mfr__invoices] as r
JOIN [dbo].[mfr__*PartnerSet] as ps ON ps.[Property] like 'ServiceRequest:Invoices' and r.[id] = ps.PartnerId
JOIN @SReq as s ON s.[Id] = ps.[EntityId]
ORDER BY s.[Id], r.[dateofcreation] ASC;
END
@@ -1,113 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__lookupReminders]
@InvId varchar(15)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 2
THROW 60000, N'not authorized', 1;
ELSE IF NOT EXISTS (select 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId) AND NOT EXISTS (select 0 FROM [dbo].[mfr__invoices] WHERE [id] = TRY_CAST(@InvId as bigint))
THROW 60000, N'invoice not found', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__lookupReminders' ,@authuser , (SELECT * FROM (VALUES(@InvId, @authuser)) as z ([InvId],[authuser]) FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER));
DECLARE @REM TABLE (
[Id] [varchar](10) NOT NULL,
[Version] [int] NOT NULL,
[DocumentName] [varchar](100) NULL,
[InvId] [varchar](15) NOT NULL,
[CustomerId] [bigint] NULL,
[SendToAddress] [nvarchar](1000) NULL,
[SendToEmail] [nvarchar](255) NULL,
[type] [varchar](3) NOT NULL,
[amount] [numeric](10, 3) NULL,
[amount_payed] [numeric](10, 3) NULL,
[amount_open] [numeric](10, 3) NULL,
[subject] [nvarchar](255) NULL,
[text] [nvarchar](2000) NULL,
[IsSent] [bit] NOT NULL,
[IsFinal] [bit] NULL,
[CustomValues] [nvarchar](max) NULL,
[DateSent] [datetime] NULL,
[UserSent] [varchar](25) NULL,
[DateFinalized] [datetime] NULL,
[UserFinalized] [varchar](25) NULL,
[DateCreated] [datetime] NOT NULL,
[UserCreated] [varchar](25) NOT NULL,
[DateModified] [datetime] NOT NULL,
[UserModified] [varchar](25) NOT NULL,
[order] int
);
INSERT INTO @REM
([Id]
,[Version]
,[DocumentName]
,[InvId]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[type]
,[amount]
,[amount_payed]
,[subject]
,[text]
,[IsSent]
,[IsFinal]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified]
,[order])
SELECT [Id]
,[Version]
,[DocumentName]
,[InvId]
,[CustomerId]
,[SendToAddress]
,[SendToEmail]
,[type]
,[amount]
,[amount_payed]
,[subject]
,[text]
,[IsSent]
,[IsFinal]
,[CustomValues]
,[DateSent]
,[UserSent]
,[DateFinalized]
,[UserFinalized]
,[DateCreated]
,[UserCreated]
,[DateModified]
,[UserModified]
, [order] = ROW_NUMBER() OVER (ORDER BY DateFinalized ASC)
FROM [dbo].[fds__reminder] WHERE [invid] = @InvId AND [IsFinal] = 1;
DECLARE @last varchar(1) = ISNULL(( SELECT TOP(1) [type] FROM @rem ORDER BY [DateFinalized] DESC), '');
DECLARE @newnum int = CASE WHEN @last IN('l','f') THEN (SELECT COUNT(*) FROM @REM) + 1
WHEN @last = 'm' THEN ISNULL( (SELECT TOP(1) TRY_CAST(LEFT([type],1) as int) FROM @rem ORDER BY [DateFinalized] DESC) , (SELECT COUNT(*) FROM @REM) + 1)
ELSE 1
END;
---output
SELECT
[type] = CAST( CASE WHEN @last = 'f' THEN 'm' WHEN @last = 'm' THEN 'l' ELSE 'f' END as varchar(1))
,[level] = @newnum;
SELECT [subject], [amount], [amount_payed], [DateFinalized] FROM @REM;
END
@@ -1,86 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__prepReminder]
@InvId varchar(15)
, @authuser varchar(100)
, @type varchar(1)
, @level int
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 2
THROW 60000, N'not authorized', 1;
ELSE IF ISNULL(@type,'') NOT IN ('f','m','l') OR ISNULL(@level, 0) < 1
THROW 60000, N'inputs not valid', 1;
ELSE IF NOT EXISTS (select 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId) AND NOT EXISTS (select 0 FROM [dbo].[mfr__invoices] WHERE [id] = TRY_CAST(@InvId as bigint))
THROW 60000, N'invoice not found', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__prepReminder' ,@authuser , (SELECT * FROM (VALUES(@InvId, @authuser, @type, @level)) as z ([InvId],[authuser],[type],[level]) FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER));
DECLARE @now datetime = GETDATE();
DECLARE @today date = CAST(@now as date);
--output
With inv1 as(
select TOP(1) * FROM [dbo].[fds__invoices] WHERE [id] = @InvId
), inv as(
SELECT [Id] = cast([id] as varchar(15))
,[PaymentTerm]
,[SendToemail]
,[SendToAddress]
,[CustomerId]
,[Invoiceid]
,[DateFinalized]
,[InvoiceBalance]
FROM inv1
UNION
SELECT [Id] = cast([id] as varchar(15))
,[PaymentTerm] = '10wd'
,[SendToEmail] = sy.[SupportMail]
,[SendToAddress] = (select TOP(1) sy.[name] + CHAR(10) + l.[addressString] + CHAR(10) + l.[postal] + ' ' + l.[city] + CHAR(10) + l.[country] from [dbo].[mfr__#locations] as l where l.[EntityId] = sy.[CustomerId])
,[CustomerId]
,[Invoiceid]
,[DateFinalized] = [DateOfCreation]
,[InvoiceBalance]
FROM [dbo].[mfr__invoices] as mfri
LEFT JOIN (SELECT TOP(1) [invid] = p.[PartnerId], s.[CustomerId], cy.[supportmail], cy.[Name]
FROM dbo.[mfr__*PartnerSet] as p
JOIN [dbo].[mfr__servicerequests] as s on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices'
JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
WHERE p.PartnerId = TRY_CAST(@invId as bigint)) as sy on mfri.Id = sy.[invid]
WHERE [id] = TRY_CAST(@invId as bigint) AND NOT EXISTS( SELECT 0 FROM inv1 ) --will only be used, if id not present in fds__invoices
AND [Filetype] not in ('PdfCancelInvoice')
AND [invoiceState] not in ('eIsCancelled','eIsPaid')
)
SELECT TOP(1)
[today] = @today
, [invid] = inv.[id]
, [sender] = N'Sebastian Fuchs GmbH & Co. KG ● Germaniastraße 15 ● 40223 Düsseldorf'
, [paymentterms] = [PaymentTerm]
, [invoiceemail] = [SendToemail]
, [invoiceaddress] = [SendToAddress]
, [CustomerId] = [CustomerId]
, [subject] = CASE WHEN @type = 'f' THEN N'Zahlungserinnerung'
WHEN @type = 'm' THEN FORMAT(@level,'0','de-de') + N'. Mahnung'
WHEN @type = 'l' THEN N'Letzte außergerichtliche Mahnung'
ELSE N'Zahlungserinnerung'
END
, [type] = @type
, [invoiceid] = inv.[Invoiceid]
, [invoicedate] = inv.[DateFinalized]
, [amount] = inv.[InvoiceBalance]
, [amount_payed] = CAST( ISNULL([dbo].[fds__fn_InvoicePaymentAmount](inv.[Id]),0.0) as numeric(10,2))
FROM inv;
END
@@ -1,459 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__prepStorno_recreate]
@InvId varchar(20)
, @authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__prepStorno_recreate' ,@authuser , '');
DECLARE @today date = GETDATE();
DECLARE @type char(1) = 'r';
DECLARE @selReq as TABLE ([id] bigint);
IF EXISTS (SELECT 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1)
BEGIN
INSERT INTO @selReq SELECT DISTINCT irq.[mfr__servicerequest] from [dbo].[fds__invoice_servicerequests] as irq where InvId = @InvId;
END
ELSE IF EXISTS (SELECT 0 FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice'))
BEGIN
INSERT INTO @selReq SELECT DISTINCT ps.[PartnerId] from [dbo].[mfr__invoices] as _i JOIN [mfr__*PartnerSet] as ps ON _i.[Id] = ps.[EntityId] and ps.[Property] = 'Invoice:SourceServiceRequest' where TRY_CAST(_i.Id as varchar(20)) = @InvId and EXISTS ( SELECT 0 FROM [dbo].[mfr__servicerequests] as _s WHERE _s.[Id] = ps.[PartnerId]);
END
DECLARE @SReq TABLE (
[Id] [bigint] NULL,
[Name] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[InvoiceId] [nvarchar](255) NULL,
[ClosedAt] [datetime] NULL,
[ReleasedAt] [datetime] NULL,
[WorkDoneAt] [datetime] NULL,
[TargetTimeInMinutes] [bigint] NULL,
[DateModified] [datetime] NULL,
[DateOfCreation] [datetime] NULL,
[DueDateRangeStart] [datetime] NULL,
[DueDateRangeEnd] [datetime] NULL,
[PortalLink] [nvarchar](255) NULL,
[CostCenterId] [bigint] NULL,
[Description] [nvarchar](max) NULL,
[State] [nvarchar](255) NULL,
[CustomValues] [nvarchar](max) NULL,
[CurrentOwnerId] [bigint] NULL,
[CustomerId] [bigint] NULL,
[ParentServiceRequestId] [bigint] NULL,
[ParentExtenalId] [nvarchar](255) NULL,
[ParentWorkDoneAt] [date] NULL,
[ParentInvoiceId] [nvarchar](255) NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NULL,
[IsTemplate] [bit] NULL,
[IsTemplateMobile] [bit] NULL,
[CreateFromServiceRequestTemplateId] [bigint] NULL,
[Type] [nvarchar](255) NULL,
[Summary] nvarchar(255) NULL,
[isChild] bit NULL,
[baseorder] int,
[provision] varchar(255) NULL
);
insert into @SReq
SELECT
[Id]
,[Name]
,[ExternalId]
,[InvoiceId]
,[ClosedAt]
,[ReleasedAt]
,[WorkDoneAt]
,[TargetTimeInMinutes]
,[DateModified]
,[DateOfCreation]
,[DueDateRangeStart]
,[DueDateRangeEnd]
,[PortalLink]
,[CostCenterId]
,[Description]
,[State]
,[CustomValues]
,[CurrentOwnerId]
,[CustomerId]
,[ParentServiceRequestId] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then [id] ELSE s.[ParentServiceRequestId] END
,[ParentExtenalId] = cast(CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then null ELSE '?' END as nvarchar(255))
,[ParentWorkDoneAt] = cast(NULL as datetime)
,[ParentInvoiceId] = cast(NULL as nvarchar(255))
,[Location#ID]
,[Version]
,[IsTemplate]
,[IsTemplateMobile]
,[CreateFromServiceRequestTemplateId]
,[Type]
,[Summary] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[baseorder] = 0
,[provision] = NULL
FROM [dbo].[mfr__servicerequests] as s
WHERE [id] IN (SELECT [Id] from @selReq);
IF @type <> 'i' -- for interim invoices, only one selected servicerequest is allowed -> childs and parents are ignored
BEGIN
INSERT INTO @SReq
SELECT s.[Id]
,s.[Name]
,s.[ExternalId]
,s.[InvoiceId]
,s.[ClosedAt]
,s.[ReleasedAt]
,s.[WorkDoneAt]
,s.[TargetTimeInMinutes]
,s.[DateModified]
,s.[DateOfCreation]
,s.[DueDateRangeStart]
,s.[DueDateRangeEnd]
,s.[PortalLink]
,s.[CostCenterId]
,s.[Description]
,s.[State]
,s.[CustomValues]
,s.[CurrentOwnerId]
,s.[CustomerId]
,s.[ParentServiceRequestId]
,[ParentExtenalId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[ExternalId] ELSE NULL END
,[ParentWorkDoneAt] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[WorkDoneAt] ELSE NULL END
,[ParentInvoiceId] = CASE WHEN s.[ParentServiceRequestId] = q.[id] THEN q.[InvoiceId] ELSE NULL END
,s.[Location#ID]
,s.[Version]
,s.[IsTemplate]
,s.[IsTemplateMobile]
,s.[CreateFromServiceRequestTemplateId]
,s.[Type]
,[Summary] = NULL
,[isChild] = CASE WHEN ISNULL(s.[ParentServiceRequestId],0) = 0 then 0 ELSE 1 END
,[baseorder] = 0
, [provision_start] = null
from [dbo].[mfr__servicerequests] as s JOIN @SReq as q ON (s.[ParentServiceRequestId] = q.[id] OR q.[ParentServiceRequestId] = s.[id]) --parents or childs
WHERE NOT EXISTS (SELECT * FROM @SReq as s2 WHERE s2.[id] = s.[Id])
END;
WITH q as (
SELECT [id], [baseorder] = (ROW_NUMBER() OVER (ORDER BY ISNULL([ParentWorkDoneAt], [WorkDoneAt]), ISNULL([ParentServiceRequestId],[id])) * 100)
FROM @SReq as s
WHERE [ischild] = 0
)
UPDATE s SET
[baseorder] = q.baseorder
FROM @SReq as s JOIN q ON s.[id] = q.[id];
WITH q as (
SELECT s.[id]
, q.[ExternalId], q.[WorkDoneAt], q.[InvoiceId]
, [baseorder] = q.[baseorder] + ROW_NUMBER() OVER (PARTITION BY q.[id] ORDER BY s.[dateofcreation])
FROM @SReq as s JOIN @SReq as q ON s.[ParentServiceRequestId] = q.[id]
WHERE s.[ischild] = 1
)
UPDATE s SET
[ParentExtenalId] = q.[ExternalId]
,[ParentWorkDoneAt] = q.[WorkDoneAt]
,[ParentInvoiceId] = q.[InvoiceId]
,[baseorder] = q.[baseorder]
FROM @SReq as s JOIN q ON s.[Id] = q.[id];
IF @type <> 'i'
BEGIN
DELETE s FROM @SReq as s WHERE NOT EXISTS(SELECT * FROM @selReq as sel where sel.[id] = s.[Id])
END
DECLARE @step [dbo].[fds__tt__mfr_steps];
DECLARE @itm [dbo].[fds__tt__mfr_items];
DECLARE @company TABLE (
[Id] [bigint] NOT NULL,
[Location#ID] [bigint] NULL,
[Version] [int] NOT NULL,
[IsPhysicalPerson] [bit] NOT NULL,
[IsOwner] [bit] NOT NULL,
[IsEmailInvoicingActive] [bit] NOT NULL,
[IsSupplier] [bit] NOT NULL,
[MappingId] [nvarchar](255) NULL,
[ExternalId] [nvarchar](255) NULL,
[Name] [nvarchar](255) NULL,
[Note] [nvarchar](255) NULL,
[SupportTelephone] [nvarchar](255) NULL,
[SupportFax] [nvarchar](255) NULL,
[SupportMail] [nvarchar](255) NULL,
[QuickSearch] [nvarchar](255) NULL,
[DateModified] [datetime] NOT NULL,
[CustomValues] [nvarchar](max) NOT NULL,
[MainContactId] [bigint] NOT NULL,
[address] nvarchar(1000) null
);
INSERT INTO @company SELECT DISTINCT cy.*, [address] = [dbo].[fds__getCompanyAddress](cy.[id]) FROM [mfr__companies] as cy JOIN @SReq as s on cy.id = s.[CustomerId];
DECLARE @companyids [dbo].[fds__tt__idlist];
INSERT INTO @companyids SELECT DISTINCT [id] FROM @company;
INSERT INTO @itm
SELECT itm.*, [baseorder] = s.[baseorder], [order] = ROW_NUMBER() OVER (PARTITION BY [ServiceRequestId] ORDER BY [SortOrder], itm.[Id])
from [dbo].[mfr__items] as itm JOIN @SReq as s ON itm.ServiceRequestId = s.[Id];
DECLARE @NUM_of_int_Invoices int = 0;
-- Add virtual items for intermediate invoices
INSERT INTO @itm
SELECT
[Id] = -2
,[Version] = 0
,[QuantityHours] = 1
,[PlannedQuantityHours] = 1
,[ItemTypeId] = NULL
,[ItemNumber] = NULL
,[TrackingId] = NULL
,[Manufacture] = NULL
,[Price] = inv.InvoiceBalance_net * -1.0 --without VAT
,[Costs] = inv.InvoiceBalance_net * -1.0 --without VAT
,[NameOrNumber] = REPLACE(inv.DocumentName, '.pdf','')
,[CustomValues] = NULL
,[Note] = '<p>' + 'Anrechnung der ' + REPLACE(inv.DocumentName, '.pdf','') + ' vom ' + FORMAT(inv.[dateFinalized], 'dd.MM.yyyy', 'de-de') + '</p>'
,[ExternalId] = NULL
,[Discount] = NULL
,[VAT] = inv.InvoiceVAT_1
,[IsManual] = 1
,[SortOrder] = ROW_NUMBER() OVER (PARTITION BY srq.[mfr__servicerequest] ORDER BY inv.[dateFinalized], inv.DateCreated)
,[Type] = 'PartialInvoice'
,[ServiceRequestId] = -2 --s.[id]
,[ServiceObjectId] = Null
,[CreatorId] = NULL
,[UnitId] = 15825600513
,[UnitString] = 'Pauschal'
,[baseorder] = s.[baseorder], [order] = 10000 + ROW_NUMBER() OVER (PARTITION BY srq.[mfr__servicerequest] ORDER BY inv.[dateFinalized], inv.DateCreated)
from [dbo].[fds__invoices] as inv
JOIN [dbo].[fds__invoice_servicerequests] as srq ON inv.[Id] = srq.[InvId] AND inv.[InvoiceType] = 'i' --important
JOIN @SReq as s ON srq.[mfr__servicerequest] = s.[Id]
WHERE inv.[InvoiceType] = 'i' --important;
AND ISNULL(inv.[isFinal],0) = 1 AND ISNULL(inv.[IsCanceled],0) = 0
AND [dbo].[fds__fn_IntermediateIsAllocatedToOther](inv.[id],@InvId) = 0; --inonly those intermediates that are not already allocated to any other (than the to be replaced Inv);
IF EXISTS (SELECT * FROM @itm WHERE [ServiceRequestId] = -2)
BEGIN
-- Add virtual request
INSERT INTO @SReq ([Id]
,[Name]
,[Type]
,[isChild]
,[baseorder])
VALUES(-2
,'Abschlagszahlungen'
,'vrii'
,0
,999);
SET @type = 'f';
END;
-- Add virtual item (to front) for (to-be) replaced invoice
INSERT INTO @itm
SELECT TOP(1) --can only be one
[Id] = -2
,[Version] = 0
,[QuantityHours] = 1
,[PlannedQuantityHours] = 1
,[ItemTypeId] = NULL
,[ItemNumber] = NULL
,[TrackingId] = NULL
,[Manufacture] = NULL
,[Price] = inv.InvoiceBalance_net * -1.0 --without VAT
,[Costs] = inv.InvoiceBalance_net * -1.0 --without VAT
,[NameOrNumber] = REPLACE(inv.DocumentName, '.pdf','')
,[CustomValues] = NULL
,[Note] = '<p>' + 'Storno der ' + REPLACE(inv.DocumentName, '.pdf','') + ' vom ' + FORMAT(inv.[dateFinalized], 'dd.MM.yyyy', 'de-de') + '</p>'
,[ExternalId] = NULL
,[Discount] = NULL
,[VAT] = inv.InvoiceVAT_1
,[IsManual] = 1
,[SortOrder] = 1
,[Type] = 'CanceledInvoice'
,[ServiceRequestId] = -3 --s.[id]
,[ServiceObjectId] = Null
,[CreatorId] = NULL
,[UnitId] = 15825600513
,[UnitString] = 'Pauschal'
,[baseorder] = 0, [order] = 1
from [dbo].[fds__invoices] as inv
WHERE ISNULL(inv.[isFinal],0) = 1 AND ISNULL(inv.[IsCanceled],0) = 0
AND inv.[Id] = @InvId;
IF EXISTS (SELECT * FROM @itm WHERE [ServiceRequestId] = -3)
BEGIN
-- Add virtual request
INSERT INTO @SReq ([Id]
,[Name]
,[Type]
,[isChild]
,[baseorder])
VALUES(-3
,'Storno'
,'vrci'
,0
,-999);
END;
/** OUTPUT + get other types **/
WITH app as(
SELECT [start] = MIN(CAST(CASE WHEN a.[StartDateTime] is null THEN s.[workdoneat] WHEN CAST(a.[StartDateTime] as date) > s.[workdoneat] THEN CAST(a.[StartDateTime] as date) ELSE s.[workdoneat] END as date))
, [end] = MAX(CAST(CASE WHEN a.[EndDateTime] is null THEN s.[workdoneat] WHEN CAST(a.[EndDateTime] as date) > s.[workdoneat] THEN CAST(a.[EndDateTime] as date) ELSE s.[workdoneat] END as date))
, [CustomerId] = MIN (s.customerid)
FROM [dbo].[mfr__appointments] as a
RIGHT JOIN @SReq as s on a.[ServiceRequestId] = s.[Id] AND a.[State] in ('IsWorkDone')
)
SELECT TOP(1)
[today]
, [provisionstart] = CASe WHEN @type = 'i' THEN NULL WHEN [start] = [end] THEN NULL ELSE [start] END
, [provisionend] = CASe WHEN @type = 'i' THEN @today ELSE [end] END -- Abschlagsrechnungen immer mit Tagesdatum
, [provisionlocation] = ISNULL((SELECT TOP(1) [provisionlocation] FROM [dbo].[fds__invoices] where [Id] = @InvId and ISNULL([provisionlocation],'') <>''),N'')
, [sender] = N'Sebastian Fuchs GmbH & Co. KG ● Germaniastraße 15 ● 40223 Düsseldorf'
, [paymentterms] = ISNULL((SELECT TOP(1) [PaymentTerm] FROM [dbo].[fds__invoices] where [Id] = @InvId and ISNULL([PaymentTerm],'') <>''),N'10wd')
, [invoiceemail] = ISNULL((SELECT TOP(1) [SendToemail] FROM [dbo].[fds__invoices] where [Id] = @InvId and ISNULL([sendToEmail],'') <>''),(SELECT TOP(1) [SupportMail] FROM @company where IsEmailInvoicingActive = 1))
, [invoiceaddress] = ISNULL((SELECT TOP(1) [SendToAddress] FROM [dbo].[fds__invoices] where [Id] = @InvId and ISNULL([SendToAddress],'') <>''),(SELECT TOP(1) CONCAT([name], CHAR(10), [address]) FROM @company ORDER BY IsEmailInvoicingActive DESC))
, [tax_servicerefund] = 0.2
, [CustomerId] = [CustomerId]
, [invoicetitle] = CASE WHEN @type = 'i' THEN (CASE WHEN @NUM_of_int_Invoices > 0 THEN CAST((@NUM_of_int_Invoices + 1) as varchar(3)) + '. ' ELSE '' END) + 'Abschlagsrechnung'
WHEN @type = 'f' THEN 'Schlussrechnung' WHEN @type = 'c' THEN 'Stornorechnung' ELSE 'Rechnung' END
, [type] = @type
FROM (VALUES(CAST(GETDATE() as date)))base ([today]) CROSS JOIN app;
SELECT
*
,[order] = ROW_NUMBER() OVER (ORDER BY [baseorder])
FROM @SReq;
IF @type <> 'i'
BEGIN
SELECT
itm.[Id]
, [net] = ISNULL([Price],0)
, [quantityhours] = ISNULL([quantityhours],1)
, itm.[UnitString]
, [net_val] = CAST( (ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0) as numeric(7,2))
, [bo_val] = CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * (1 + (ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2))
, [vat] = CASE WHEN LOWER(itm.[type]) in ('Title','Text') THEN null ELSE FORMAT(ISNULL(vat.[vat], 19.0), '#0.0','de') + '%' END
, [vat_val] = CAST( ((ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0)) * ((ISNULL(vat.[vat], 19.0) * 0.01)) as numeric(7,2))
, itm.[ServiceRequestId]
, itm.[SortOrder]
, itm.[Type]
, itm.[NameOrNumber]
, itm.[Note]
, [order] = (itm.[baseorder] * 100) + itm.[order]
FROM @itm as itm
LEFT JOIN [dbo].[fds__custom_vat] as vat ON itm.[Id] = vat.[EntityId] and vat.[EntityType] = 'item';
END
ELSE
BEGIN
WITH citm as (
SELECT itm.[ServiceRequestId], [net_val] = CAST( SUM( (ISNULL([Price],0) * ISNULL([quantityhours],1)) - ISNULL([discount],0) ) as numeric(7,2))
FROM @itm as itm
WHERE itm.[Type] = 'Material'
GROUP BY itm.[ServiceRequestId]
)
SELECT
[Id] = -1
, [net] = 1000.0
, [quantityhours] = 1
, [UnitString] = 'Pauschal'
, [net_val] = CAST( 1000.0 as numeric(7,2))
, [bo_val] = CAST( 1000.0 * 1.19 as numeric(7,2))
, [vat] = FORMAT( 19.0, '#0.0','de') + '%'
, [vat_val] = CAST( 1000.0 * 0.19 as numeric(7,2))
, [ServiceRequestId] = s.[id]
, [SortOrder] = 0
, [Type] = 'PartialInvoice'
, [NameOrNumber] = 'Abschlagsbetrag für bereits erbrachte Arbeiten, Dienstleistungen, Materiallieferungen und getätigte Bestellvorgänge zum oben genannten Bauvorhaben'
, [Note] = ''
, [order] = 1
FROM @SReq as s;
END
INSERT INTO @step
SELECT
stp.*
, s.[baseorder]
, [order] = (s.[baseorder] * 100) + ROW_NUMBER() OVER (PARTITION BY stp.[ServiceRequestId] ORDER BY stp.[SortOrder], stp.[Id])
FROM [dbo].[mfr__steps] as stp JOIN @SReq as s ON stp.ServiceRequestId = s.[Id];
SELECT * from @step;
SELECT
cc.[Id]
, [CompanyName] = cc.[name]
, ct.[Gender]
, ct.[FirstName]
, ct.[LastName]
, ct.[Email]
, cc.[address]
FROM [dbo].[fds__getCompanysContacts](@companyids) as ct join @company as cc on cc.[Id] = ct.[CyId];
SELECT DISTINCT
[ref] = 'Einsatzort ' + so.[name]
,[address] = [dbo].[fds__fn_combineAddress] (so.[Name], loc.[AddressString], null
,loc.[Postal]
,loc.[City]
,loc.[State]
,loc.[Country])
FROM @SReq as s
LEFT JOIN [mfr__*PartnerSet] as p ON p.property = 'ServiceRequest:ServiceObjects' and p.[EntityId] = s.[id]
LEFT JOIN [mfr__serviceobjects] as so ON so.id = p.[PartnerId]
LEFT JOIN [mfr__*PartnerSet] as po ON p.property = 'ServiceObject:Location' and po.[EntityId] = so.[Id]
LEFT join [dbo].[mfr__#locations] as loc ON (loc.[id] = so.[Location#ID] OR po.[PartnerId] = loc.[Id] OR (so.[Id] = loc.[EntityId] and loc.Property = 'ServiceObject:Location'))
WHERE so.id is not null and loc.[id] is not null
UNION
SELECT DISTINCT
[ref] = 'Auftragsort ' + [name]
,[address] = [dbo].[fds__fn_combineAddress] (loc.[AddressString], null, null
,loc.[Postal]
,loc.[City]
,loc.[State]
,loc.[Country])
FROM @SReq as s
LEFT JOIN [mfr__*PartnerSet] as p ON p.property = 'ServiceRequest:Location' and p.[EntityId] = s.[id]
join [dbo].[mfr__#locations] as loc ON (s.[Location#ID] = loc.[id] OR p.[PartnerId] = loc.[Id] OR (s.[Id] = loc.[EntityId] and loc.Property = 'ServiceRequest:Location'))
WHERE loc.[id] is not null
UNION
SELECT DISTINCT [ref] = 'Kunde'
, [address]
FROM @company;
END
@@ -1,61 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__r_getBalanceByMonth]
@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @today date = GETDATE()
DECLARE @tomorrow date = DATEADD(DAY,1,@today);
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];
--Admin Table
INSERT INTO @Admin
VALUES (
'bbm'
,'Umsatz nach Monaten'
,'Umsatz nach Monaten'
,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
,''
,'table' --typ
, '' --settings
, ''
, 0)
SELECT * FROM @Admin;
WITH inv as (
SELECT
i.[Dateofcreation]
,i.[InvoiceBalance]
,i.[InvoiceBalanceNetto]
FROM [dbo].[mfr__invoices] as i
where --i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and
i.[FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview','')
UNION
SELECT
[Dateofcreation] = i.[DateCreatEd]
,i.[InvoiceBalance]
,[InvoiceBalanceNetto] = i.[InvoiceBalance_net]
FROM [dbo].[fds__invoices] as i
WHERE i.[isFinal] = 1
)
SELECT
[Monat] = FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de')
, [ Umsätze] = FORMAT( SUM( ISNULL(inv.[InvoiceBalance],0) ), '#,0.00 €', 'de')
, [style: Umsätze] = 'text-align: right'
, [ Umsätze_netto] = FORMAT( SUM( ISNULL(inv.[InvoiceBalanceNetto],0) ), '#,0.00 €', 'de')
, [style: Umsätze_netto] = 'text-align: right'
FROM inv
GROUP BY FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de');
END
@@ -1,91 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__r_getBalanceByYearTopCustomer]
@year int,
@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @today date = GETDATE()
DECLARE @tomorrow date = DATEADD(DAY,1,@today);
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];
--Admin Table
INSERT INTO @Admin
VALUES (
'bbm'
,'Umsatz der Top 10 Kunden für Jahr ' + CAST( @year as varchar(10))
,'Umsatz der Top 10 Kunden pro Jahr ' + CAST( @year as varchar(10))
,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
,''
,'table' --typ
, '' --settings
, ''
, 0);
SELECT * FROM @Admin;
WITH inv as(
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST(_i.[Dateofcreation] as date)
,srq.CustomerId
,[customerName] = cy.Name
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST(_i.[Dateofcreation] as date))
FROM [dbo].[mfr__invoices] as _i
JOIN [dbo].[mfr__servicerequests] as srq on srq.InvoiceId like ('%' + _i.[InvoiceId] + '%')
JOIN [dbo].[mfr__companies] as cy on srq.CustomerId = cy.id
where _i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and YEAR(_i.[Dateofcreation]) = @year
GROUP BY CAST(_i.[Dateofcreation] as date), CustomerId, cy.name
UNION
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST(_i.[DateCreated] as date)
,_i.CustomerId
,[customerName] = cy.Name
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST(_i.[DateCreated] as date))
FROM [dbo].[fds__invoices] as _i
JOIN [dbo].[mfr__companies] as cy on _i.CustomerId = cy.id
where YEAR(_i.[DateCreated]) = @year and _i.[isfinal] = 1
GROUP BY CAST(_i.[DateCreated] as date), CustomerId, cy.name
), yr1 as (
SELECT
[year] = YEAR( inv.[Dateofcreation] )
, [total] = SUM( ISNULL(inv.[InvoiceBalance],0) )
FROM [dbo].[mfr__invoices] as inv
where inv.[invoicestate] in('eIsSent','eIsPaid','eIsOpen')
GROUP BY YEAR( inv.[Dateofcreation])
UNION
SELECT
[year] = YEAR( _i.[DateCreated] )
, [total] = SUM( ISNULL(_i.[InvoiceBalance],0) )
FROM [dbo].[fds__invoices] as _i
WHERE _i.[isfinal] = 1
GROUP BY YEAR( _i.[DateCreated])
), yr as (
SELECT [year], [total] = SUM([total])
from yr1 GROUP BY [year]
)
SELECT TOP(10)
[Jahr] = CAST( YEAR( inv.[Dateofcreation] ) as varchar(4))
, [Name d. Kunden] = [customerName]
, [ Umsätze] = FORMAT( SUM( ISNULL(inv.[InvoiceBalance],0) ), '#,0.00 €', 'de')
, [style: Umsätze] = 'text-align: right'
, [% Anteil] = CASE WHEN yr.[total] > 0 THEN FORMAT( SUM( ISNULL(inv.[InvoiceBalance],0) ) / [total], '0 %', 'de') ELSE NULL END
, [style:% Anteil] = 'text-align: right'
, [order] = ROW_NUMBER() OVER ( ORDER By SUM( ISNULL(inv.[InvoiceBalance],0) ) DESC)
FROM inv LEFT JOIN yr on YEAR( inv.[Dateofcreation] ) = yr.[year]
GROUP BY YEAR( inv.[Dateofcreation] ), yr.[total], [customerName];
END
@@ -1,90 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__r_getBalanceByYearTopMaterial]
@year int,
@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @today date = GETDATE()
DECLARE @tomorrow date = DATEADD(DAY,1,@today);
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];
--Admin Table
INSERT INTO @Admin
VALUES (
'bbm'
,'Umsatz der Top 10 Materialien für Jahr ' + CAST( @year as varchar(10))
,'Umsatz der Top 10 Materialien für Jahr ' + CAST( @year as varchar(10))
,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
,''
,'table' --typ
, '' --settings
, ''
, 0);
SELECT * FROM @Admin;
WITH inv as(
select
[itmPrice] = SUM( (ISNULL(itm.[price],0) * ISNULL(itm.[QuantityHours],0) ) -ISNULL(itm.[discount],0) )
,[Dateofcreation] = CAST(_i.[Dateofcreation] as date)
,[material] = itm.NameOrNumber
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST(_i.[Dateofcreation] as date))
FROM [dbo].[mfr__invoices] as _i
JOIN [dbo].[mfr__servicerequests] as srq on srq.InvoiceId like ('%' + _i.[InvoiceId] + '%')
JOIN [dbo].[mfr__items] as itm on srq.Id = itm.[ServiceRequestId] and itm.[Type] like 'Material'
where _i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and YEAR(_i.[Dateofcreation]) = @year
GROUP BY CAST(_i.[Dateofcreation] as date), itm.NameOrNumber
UNION
SELECT
[itmPrice] = SUM( ISNULL(itm.[value_total],0) )
,[Dateofcreation] = CAST(_i.[DateCreated] as date)
,[material] = itm.[text]
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST(_i.[DateCreated] as date))
FROM [dbo].[fds__invoices] as _i
JOIN [fds__invoice_items] as itm on itm.[invid] = _i.[id] AND itm.[Type] like 'Material'
where YEAR(_i.[DateCreated]) = @year and _i.[isfinal] = 1
GROUP BY CAST(_i.[DateCreated] as date), itm.[text]
), yr1 as (
SELECT
[year] = YEAR( inv.[Dateofcreation] )
, [total] = SUM( ISNULL(inv.[InvoiceBalance],0) )
FROM [dbo].[mfr__invoices] as inv
where inv.[invoicestate] in('eIsSent','eIsPaid','eIsOpen')
GROUP BY YEAR( inv.[Dateofcreation])
UNION
SELECT
[year] = YEAR( _i.[DateCreated] )
, [total] = SUM( ISNULL(_i.[InvoiceBalance],0) )
FROM [dbo].[fds__invoices] as _i
WHERE _i.[isfinal] = 1
GROUP BY YEAR( _i.[DateCreated])
), yr as (
SELECT [year], [total] = SUM([total])
from yr1 GROUP BY [year]
)
SELECT TOP(10)
[Jahr] = CAST( YEAR( inv.[Dateofcreation] ) as varchar(4))
, [Material] = [material]
, [ Umsätze] = FORMAT( SUM( ISNULL(inv.[itmPrice],0) ), '#,0.00 €', 'de')
, [style: Umsätze] = 'text-align: right'
, [% Anteil] = CASE WHEN yr.[total] > 0 THEN FORMAT( SUM( ISNULL(inv.[itmPrice],0) ) / [total], '0 %', 'de') ELSE NULL END
, [style:% Anteil] = 'text-align: right'
, [order] = ROW_NUMBER() OVER ( ORDER By SUM( ISNULL(inv.[itmPrice],0) ) DESC)
FROM inv LEFT JOIN yr on YEAR( inv.[Dateofcreation] ) = yr.[year]
GROUP BY YEAR( inv.[Dateofcreation] ), yr.[total], [material];
END
@@ -1,72 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__r_getBalanceTrendByMonth]
@year int
,@month int,
@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @today date = GETDATE()
DECLARE @tomorrow date = DATEADD(DAY,1,@today);
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
set @month = ISNULL(@month, Month(@today));
set @year= ISNULL(@year, Year(@year));
DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];
--Admin Table
INSERT INTO @Admin
VALUES (
'bbm'
,'UmsatzTrend für Monat ' + FORMAT(@Month, '00') + ' / ' + FORMAT(@year, '0000')
,'UmsatzTrend für Monat ' + FORMAT(@Month, '00') + ' / ' + FORMAT(@year, '0000')
,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
,''
,'chart' --typ
, '{ "charttype": "line", "x1_label_angle": -90, "color": "e0301e", "linewidth": 2, "valuelabels": false, "marker": "none", "colorpalette": "00B050;00B050;00B050", "x1_column": "date", "y1_column": "Value", "y1_title": "Actuals", "x1_title": "", "series_column": "series", "legend_position": "bottom", "show_datalabel": true, "datalabelfontsizescale": 0.6, "height": 600, "width": 1200, "x1_labelformat": "ddd, dd/MM"}' --settings
, ''
, 0)
SELECT * FROM @Admin;
with inv as(
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST([Dateofcreation] as date)
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST([Dateofcreation] as date))
FROM [dbo].[mfr__invoices] as _i
where _i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and month([Dateofcreation]) = @Month and YEAR([Dateofcreation]) = @year
GROUP BY CAST([Dateofcreation] as date)
UNION
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST([Datecreated] as date)
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST([Datecreated] as date))
FROM [dbo].[fds__invoices] as _i where _i.IsFinal = 1 and month([Datecreated]) = @Month and YEAR([Datecreated]) = @year
GROUP BY CAST([Datecreated] as date)
)
SELECT
--[Monat] = FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de')
--,[Umsatz] = FORMAT( ISNULL(inv.[InvoiceBalance],0) , '#0.00', 'de')
[series] = 'Umsatz'
,[date] = CAST(inv.[Dateofcreation] as date)
,[value] = CAST( SUM( ISNULL(inv.[InvoiceBalance],0) ) OVER (ORDER BY [seq]) as numeric(9,2))
FROM inv
ORDER BY [seq]
END
@@ -1,71 +0,0 @@

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__r_getBalanceTrendByYear]
@year int,
@authuser varchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @today date = GETDATE()
DECLARE @tomorrow date = DATEADD(DAY,1,@today);
IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
THROW 60000, N'not authorized', 1;
set @year= ISNULL(@year, Year(@year));
DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];
--Admin Table
INSERT INTO @Admin
VALUES (
'bbm'
,'UmsatzTrend für Jahr ' + FORMAT(@year, '0000')
,'UmsatzTrend für Jahr ' + FORMAT(@year, '0000')
,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
,''
,'chart' --typ
, '{ "charttype": "line", "x1_label_angle": -90, "color": "e0301e", "linewidth": 2, "valuelabels": false, "marker": "none", "colorpalette": "00B050;00B050;00B050", "x1_column": "date", "y1_column": "Value", "y1_title": "Actuals", "x1_title": "", "series_column": "series", "legend_position": "bottom", "show_datalabel": true, "datalabelfontsizescale": 0.6, "height": 600, "width": 1200, "x1_labelformat": "ddd, dd/MM"}' --settings
, ''
, 0)
SELECT * FROM @Admin;
with inv as(
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST([Dateofcreation] as date)
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST([Dateofcreation] as date))
FROM [dbo].[mfr__invoices] as _i
where _i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and YEAR([Dateofcreation]) = @year
GROUP BY CAST([Dateofcreation] as date)
UNION
select
[InvoiceBalance] = SUM( ISNULL(_i.[InvoiceBalance],0) )
,[Dateofcreation] = CAST([Datecreated] as date)
,[seq] = ROW_NUMBER() OVER (ORDER BY CAST([Datecreated] as date))
FROM [dbo].[fds__invoices] as _i where _i.IsFinal = 1 and YEAR([Datecreated]) = @year
GROUP BY CAST([Datecreated] as date)
)
SELECT
--[Monat] = FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de')
--,[Umsatz] = FORMAT( ISNULL(inv.[InvoiceBalance],0) , '#0.00', 'de')
[series] = 'Umsatz'
,[date] = CAST(inv.[Dateofcreation] as date)
,[value] = CAST( SUM( ISNULL(inv.[InvoiceBalance],0) ) OVER (ORDER BY [seq]) as numeric(9,2))
FROM inv
ORDER BY [seq]
END
@@ -1,70 +0,0 @@

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__setBankingtransaction_autoAssigns]
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__setBankingtransaction_autoAssigns' ,'', '');
with
icr as (
SELECT * FROM [dbo].[fds__getInvoiceCredits] (null, null)
), ic as (
SELECT
icr.[uid]
, icr.[InvoiceId]
, [InvID] = CAST(i.[Id] as varchar(15))
, icr.amount
, [auto] = CAST(1 as bit)
FROM icr
JOIN [dbo].[fds__invoices] as i on icr.[InvoiceId] = i.[InvoiceId] and i.[InvoiceId] is not null and i.[IsFinal] = 1
UNION
SELECT
icr.[uid]
, icr.[InvoiceId]
, [InvID] = CAST(i.[Id] as varchar(15))
, icr.amount
, [auto] = CAST(1 as bit)
FROM icr
JOIN [dbo].[mfr__invoices] as i on icr.[InvoiceId] = i.[InvoiceId] and i.[InvoiceId] is not null
--UNION
--SELECT
-- bt.[uid]
-- , i.[InvoiceId]
-- , [InvID] = i.[Id]
-- , bt.[amount]
-- , [auto] = CAST(0 as bit)
--FROM [dbo].[fds__bankingtransactions] as bt JOIN [dbo].[fds__bankingtransactions_settings] as bs ON bt.uid = bs.[banking_uid] AND ISNULL(bs.[assigned_invoice_id], '') <> ''
-- JOIN [dbo].[fds__invoices] as i on bs.[assigned_invoice_id] = i.[Id] and i.[IsFinal] = 1
), ic2 as (
SELECT [banking_uid] = ic.[uid]
, [amount] = SUM(ISNULL(ic.[amount], 0.0))
, [InvID_csv] = STRING_AGG(ic.[InvID], ',')
from ic
WHERE ic.[InvID] is not null
GROUP BY ic.[uid]
)
--select * from ic2
MERGE [dbo].[fds__bankingtransactions_settings] as TARGET
USING ic2 as SOURCE ON TARGET.[banking_uid] = SOURCE.[banking_uid]
WHEN NOT MATCHED BY TARGET THEN
INSERT
([banking_uid]
,[auto_invoice_id])
VALUES
(SOURCE.[banking_uid]
,SOURCE.[InvID_csv])
WHEN MATCHED THEN
UPDATE SET [auto_invoice_id] = [dbo].[ott_merge_csv](TARGET.[auto_invoice_id],SOURCE.[InvID_csv]);
END
@@ -1,46 +0,0 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__setInvoicePayed]
@Id varchar(15),
@authuser varchar(25)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @now datetime = GETUTCDATE();
IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
THROW 60000, N'not authorized', 1;
INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
VALUES ('fds__setInvoicePayed' ,@authuser , (SELECT * FROM (VALUES(@Id, @authuser)) as z ([id],[authuser]) FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER));
UPDATE [dbo].[fds__invoices] SET [IsPayed] = 1, [paymentstatus] = 'm', [DateModified] = @now, [UserModified] = @authuser, [version] = (ISNULL([version],0) + 1)
OUTPUT inserted.*
WHERE [Id] = @Id AND @Id is not null and [IsPayed] = 0;
IF TRY_CAST(@id as bigint) is not null and EXISTS(SELECT 0 FROM [dbo].[mfr__invoices] as i where i.[id] = TRY_CAST(@id as bigint))
BEGIN
MERGE [dbo].[fds__custom_invoiceinfo] as TARGET
USING (VALUES
(TRY_CAST(@id as bigint)
,CAST('m' as char(1))
,CAST(1 as bit))) as SOURCE([InvId],[PaymentStatus],[isPayed])
ON TARGET.[InvID] = SOURCE.[InvID]
WHEN MATCHED THEN
UPDATE SET TARGET.[PaymentStatus] = SOURCE.[PaymentStatus]
,TARGET.[isPayed] = SOURCE.[isPayed]
WHEN NOT MATCHED THEN
INSERT ([InvId],[PaymentStatus],[isPayed],[file])
VALUES(
SOURCE.[InvId]
,SOURCE.[PaymentStatus]
,SOURCE.[isPayed]
,NULL
);
END
END
@@ -209,14 +209,12 @@ BEGIN
itm.*
, [baseorder] = s.[baseorder]
, [order] = ROW_NUMBER() OVER (PARTITION BY itm.[ServiceRequestId] ORDER BY itm.[SortOrder], itm.[Id])
, [SetItmID] = CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) > 0 THEN NULL
ELSE MAX(CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) > 0 THEN itm.[Id] ELSE NULL END)
, [SetItmID] = MAX(CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) = 0 THEN itm.[Id] ELSE NULL END)
OVER (
PARTITION BY itm.[ServiceRequestId]
ORDER BY itm.[SortOrder], itm.[Id]
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
END
FROM [dbo].[mfr__items] as itm
JOIN @SReq as s ON itm.ServiceRequestId = s.[Id]
WHERE itm.[Type] <> 'PartialInvoice';
@@ -207,14 +207,12 @@ BEGIN
SELECT itm.*
, [baseorder] = s.[baseorder]
, [order] = ROW_NUMBER() OVER (PARTITION BY itm.[ServiceRequestId] ORDER BY itm.[SortOrder], itm.[Id])
, [SetItmID] = CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) > 0 THEN NULL
ELSE MAX(CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) > 0 THEN itm.[Id] ELSE NULL END)
, [SetItmID] = MAX(CASE WHEN itm.[Type] = 'Set' AND ISNULL(itm.[Price], 0) = 0 THEN itm.[Id] ELSE NULL END)
OVER (
PARTITION BY itm.[ServiceRequestId]
ORDER BY itm.[SortOrder], itm.[Id]
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
END
FROM [dbo].[mfr__items] as itm
JOIN @SReq as s ON itm.ServiceRequestId = s.[Id];