Files
Fuchs_Intranet/Fuchs_Database/dbo/Stored Procedures/fds__r_getBalanceByYearTopCustomer.sql
T
Stefan c741535aec
Playwright Tests / test (push) Canceled after 0s
feat: Implement mfr truncation fix and enhance invoice handling
- Added a new SQL script to address the "String or binary data would be truncated" error in mfr sync by widening the Note fields in service objects and companies to NVARCHAR(MAX).
- Updated the table types and associated stored procedures to accommodate the changes, ensuring compatibility with existing data.
- Queued a re-sync for previously rejected rows due to truncation issues.
- Created application logins for fuchs_fds and fuchs_web with placeholder passwords for initial setup.
- Introduced functions for invoice backdating and invoice date retrieval to streamline invoice processing.
- Developed a password generation function with customizable parameters for length and character types.
- Added a random number generation function to support password creation.
2026-09-30 16:56:08 +02:00

72 lines
2.5 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[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 100 Kunden für Jahr ' + CAST( @year as varchar(10))
,'Umsatz der Top 100 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] = [dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated])
,_i.CustomerId
,[customerName] = cy.Name
,[seq] = ROW_NUMBER() OVER (ORDER BY [dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated]))
FROM [dbo].[fds__invoices] as _i
JOIN [dbo].[mfr__companies] as cy on _i.CustomerId = cy.id
where YEAR([dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated])) = @year and _i.[isfinal] = 1
GROUP BY [dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated]), CustomerId, cy.name
), yr1 as (
SELECT
[year] = YEAR([dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated]))
, [total] = SUM( ISNULL(_i.[InvoiceBalance],0) )
FROM [dbo].[fds__invoices] as _i
WHERE _i.[isfinal] = 1
GROUP BY YEAR([dbo].[fds__fn_invoiceDate](_i.[DateFinalized], _i.[DateCreated]))
), yr as (
SELECT [year], [total] = SUM([total])
from yr1 GROUP BY [year]
)
SELECT TOP(100)
[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