Files
Fuchs_Intranet/Fuchs_Database/dbo/Functions/fds__getCompanyContacts.sql
T
Stefan 10ecdfa2e4 Add Fuchs_Database SSDT project (schema source of truth)
Adds the SQL Server Data Tools project for the fuchs_fds database — tables,
table types, functions and stored procedures that the backend calls (e.g.
fds__getInvoice, fds__merge_bankingtransactions, fds__tt__bankingtransactions,
fds__admin_getReportCatalog, fis_* auth). Build/model caches (bin, obj,
*.dbmdl, *.jfm, *.user) are git-ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 14:50:54 +02:00

31 lines
1.1 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fds__getCompanyContacts]
(
@companyid bigint
)
RETURNS TABLE
AS
RETURN
(
WITH ps as (
SELECT [contactid] = p.[EntityId] from [mfr__*PartnerSet] as p WHERE p.property = 'Contact:Company' and p.[partnerId] = @companyid
UNION
SELECT [contactid] = p.[PartnerId] from [mfr__*PartnerSet] as p WHERE p.property = 'Company:Contacts' and p.[EntityId] = @companyid
)
SELECT ct.*
,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
FROM [dbo].[mfr__contacts] as ct
JOIN [dbo].[mfr__companies] as cy ON ct.[CompanyId] = cy.[id]
WHERE ct.CompanyId = @companyid
UNION
SELECT ct.*
,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
FROM [dbo].[mfr__contacts] as ct
JOIN ps ON ct.[id] = ps.[contactid]
JOIN [dbo].[mfr__companies] as cy ON cy.[id] = @companyid
)