Add function to retrieve company address as JSON and update invoice procedures

- Created a new function `fds__getCompanyAddressJson` to return a company's postal address as a structured JSON object.
- Modified stored procedures `fds__createInvoice`, `fds__setInvoice`, and `fds__prepInvoice` to include a new parameter `@SendToAddressJson` for handling the address data.
- Updated the invoice table and user-defined types to accommodate the new `SendToAddressJson` field.
- Ensured that the address data is properly retrieved and stored in the invoice records.
This commit is contained in:
2026-07-18 18:01:24 +02:00
parent 5ccd85c38f
commit 628802db19
45 changed files with 1892 additions and 26 deletions
@@ -0,0 +1,41 @@
-- =============================================
-- Returns the recipient company's postal address as a structured JSON object
-- ({name, street, postalCode, city, state, country}) for the invoice editor's
-- structured address dialog (EN 16931 / eRechnung). Mirrors the location
-- resolution of [fds__getCompanyNameAddress] but keeps the fields separate
-- instead of composing them into one free-text string. See ADR 0012.
-- =============================================
CREATE FUNCTION [dbo].[fds__getCompanyAddressJson]
(
@companyid bigint
)
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @locationid bigint, @name nvarchar(255);
DECLARE @street nvarchar(255), @postal nvarchar(255), @city nvarchar(255), @state nvarchar(255), @country varchar(15);
SELECT TOP(1) @locationid = cy.[Location#ID], @name = cy.[name]
FROM [dbo].[mfr__companies] as cy WHERE cy.[id] = @companyid;
IF @locationid IS NULL
SELECT TOP(1) @locationid = l.[ID]
FROM [dbo].[mfr__#locations] as l
JOIN [dbo].[mfr__companies] as cy ON l.[Property] = 'Company:Location' AND l.[EntityId] = cy.[Id]
WHERE cy.[id] = @companyid;
SELECT TOP(1) @street = loc.[AddressString], @postal = loc.[Postal], @city = loc.[City],
@state = loc.[State], @country = loc.[Country]
FROM [dbo].[mfr__#locations] as loc WHERE loc.[id] = @locationid;
RETURN (
SELECT
ISNULL(@name, '') AS [name]
, ISNULL(@street, '') AS [street]
, ISNULL(@postal, '') AS [postalCode]
, ISNULL(@city, '') AS [city]
, ISNULL(@state, '') AS [state]
, ISNULL(@country, '') AS [country]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
END
@@ -17,7 +17,8 @@ CREATE PROCEDURE [dbo].[fds__createInvoice]
@ProvisionPeriod varchar(50),
@CustomValues nvarchar(max),
@authuser varchar(25),
@Id varchar(10) OUT
@Id varchar(10) OUT,
@SendToAddressJson nvarchar(max) = NULL
AS
BEGIN
SET NOCOUNT ON;
@@ -64,6 +65,7 @@ BEGIN
[IsCanceled] [bit] NULL,
[Replaces_InvId] [varchar](10) NULL,
[CustomValues] [nvarchar](max) NULL,
[SendToAddressJson] [nvarchar](max) NULL,
[DateSent] [datetime] NULL,
[UserSent] [varchar](25) NULL,
[DateFinalized] [datetime] NULL,
@@ -104,6 +106,7 @@ BEGIN
,[IsPayed]
,[IsSent]
,[CustomValues]
,[SendToAddressJson]
,[DateSent]
,[UserSent]
,[DateFinalized]
@@ -143,6 +146,7 @@ BEGIN
,0 --<IsPayed, bit,>
,0 --<IsSent, bit,>
,@CustomValues
,@SendToAddressJson
, NULL --[DateSent]
, NULL --[UserSent]
,NULL --<DateFinalized, datetime,>
@@ -45,6 +45,7 @@ BEGIN
,[IsCanceled]
,[Replaces_InvId]
,[CustomValues]
,[SendToAddressJson]
,[DateSent]
,[UserSent]
,[DateFinalized]
@@ -356,6 +356,7 @@ BEGIN
, [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)
, [invoiceaddressData] = [dbo].[fds__getCompanyAddressJson]((SELECT TOP(1) [id] 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'
@@ -18,7 +18,8 @@ CREATE PROCEDURE [dbo].[fds__setInvoice]
@ProvisionPeriod varchar(50),
@CustomValues nvarchar(max),
@authuser varchar(25),
@Id varchar(10) OUT
@Id varchar(10) OUT,
@SendToAddressJson nvarchar(max) = NULL
AS
BEGIN
SET NOCOUNT ON;
@@ -65,6 +66,7 @@ BEGIN
[IsCanceled] [bit] NULL,
[Replaces_InvId] [varchar](50) NULL,
[CustomValues] [nvarchar](max) NULL,
[SendToAddressJson] [nvarchar](max) NULL,
[DateSent] [datetime] NULL,
[UserSent] [varchar](25) NULL,
[DateFinalized] [datetime] NULL,
@@ -102,6 +104,7 @@ BEGIN
,0 --<IsPayed, bit,>
,0 --<IsSent, bit,>
,@CustomValues
,@SendToAddressJson
, NULL --[DateSent]
, NULL --[UserSent] [varchar](25) NULL,
,NULL --<DateFinalized, datetime,>
@@ -133,6 +136,7 @@ BEGIN
,[IsPayed]
,[IsSent]
,[CustomValues]
,[SendToAddressJson]
,[DateSent]
,[UserSent]
,[DateFinalized]
@@ -163,6 +167,7 @@ BEGIN
,[ProvisionPeriod] = SOURCE.[ProvisionPeriod]
,[ProvisionLocation] = SOURCE.[ProvisionLocation]
,[CustomValues] = SOURCE.[CustomValues]
,[SendToAddressJson] = SOURCE.[SendToAddressJson]
,[DateModified] = SOURCE.[DateModified]
,[UserModified] = SOURCE.[UserModified]
OUTPUT inserted.*
@@ -25,6 +25,7 @@
[IsCanceled] AS (CONVERT([bit],case when [DateCancelled] IS NULL then (0) else (1) end)),
[Replaces_InvId] VARCHAR (50) NULL,
[CustomValues] NVARCHAR (MAX) NULL,
[SendToAddressJson] NVARCHAR (MAX) NULL,
[DateSent] DATETIME NULL,
[UserSent] VARCHAR (25) NULL,
[DateFinalized] DATETIME NULL,
@@ -25,6 +25,7 @@
[IsCanceled] BIT NULL,
[Replaces_InvId] VARCHAR (50) NULL,
[CustomValues] NVARCHAR (MAX) NULL,
[SendToAddressJson] NVARCHAR (MAX) NULL,
[DateSent] DATETIME NULL,
[UserSent] VARCHAR (25) NULL,
[DateFinalized] DATETIME NULL,