------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonInvoices')
BEGIN
CREATE TABLE [dbo].[ut_MareonInvoices]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[UNR] char(20) DEFAULT ' '
  ,[SubUNR] char(20) DEFAULT ' '
  ,[MareonNo] char(20) DEFAULT ' '
  ,[InvoiceERP] char(30) DEFAULT ' '
  ,[InvoiceCM] char(20) DEFAULT ' '
  ,[JobERP] char(30) DEFAULT ' '
  ,[CraftsPN] char(10) DEFAULT ' '
  ,[DateInvoice] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Modify] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Status] char(1) DEFAULT ' '
  ,[Type] char(1) DEFAULT ' '
  ,[NrBsh] char(20) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[Datetopay] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Skonto] float DEFAULT 0
  ,[TextRN] integer DEFAULT 0
  ,[Canceltext] integer DEFAULT 0
  ,[Inspection] integer DEFAULT 0
  ,[Currency] char(3) DEFAULT ' '
  ,[DateDone] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[DateFwd] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[FPA] char(20) DEFAULT ' '
  ,[VAT] float DEFAULT 0
  ,[fpaPlus] float DEFAULT 0
  ,[fpaOff] float DEFAULT 0
  ,[revisedAmount] float DEFAULT 0
  ,[grossAmount] float DEFAULT 0
  ,[vatAmount] float DEFAULT 0
  ,[netAmount] float DEFAULT 0
  ,[wagesAmount] float DEFAULT 0
  ,[EStG35a] integer DEFAULT 0
  ,[UStG13b] tinyint DEFAULT 0
  ,[TenantPays] tinyint DEFAULT 0
  ,[Insured] tinyint DEFAULT 0
  ,[taxDed48] float DEFAULT 0
  ,[PrjDedSum] float DEFAULT 0
  ,[Barcode] char(50) DEFAULT ' '
  ,[PosNetto] float DEFAULT 0
  ,[PosTotal] float DEFAULT 0
  ,[PosWages] float DEFAULT 0
  ,[bshBrutto] float DEFAULT 0
  ,[bshAbzuege] float DEFAULT 0
  ,[bshSkBasis] float DEFAULT 0
  ,[bshSkBetrag] float DEFAULT 0
  ,[bshZahlBtr] float DEFAULT 0
  ,[TextPdf] integer DEFAULT 0
  ,[disDownload] tinyint DEFAULT 0
  ,[UZ] char(10) DEFAULT ' '
  ,[NrReserv] char(8) DEFAULT ' '
  ,[Contact] char(50) DEFAULT ' '
  ,[Kennzeich] integer DEFAULT 0
  ,[Historie] integer DEFAULT 0
  CONSTRAINT [Pk_MareonInvoices]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonInvoices_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonInvoices_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonInvoices_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonInvoices_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonInvoices_Keys')
  DROP TABLE [dbo].[ut_MareonInvoices_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [UNR] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [SubUNR] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'MareonNo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [MareonNo] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'InvoiceERP')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [InvoiceERP] char(30) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'InvoiceCM')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [InvoiceCM] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'JobERP')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [JobERP] char(30) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'CraftsPN')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [CraftsPN] char(10) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'DateInvoice')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [DateInvoice] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Modify')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Modify] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [HostDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Status] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Type')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Type] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [NrBsh] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [PrjNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [BelArt] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [BelNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Datetopay')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Datetopay] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Skonto')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Skonto] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'TextRN')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [TextRN] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Canceltext')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Canceltext] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Inspection')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Inspection] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Currency')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Currency] char(3) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'DateDone')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [DateDone] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'DateFwd')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [DateFwd] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [FPA] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'VAT')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [VAT] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'fpaPlus')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [fpaPlus] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'fpaOff')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [fpaOff] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'revisedAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [revisedAmount] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'grossAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [grossAmount] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'vatAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [vatAmount] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'netAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [netAmount] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'wagesAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [wagesAmount] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'EStG35a')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [EStG35a] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'UStG13b')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [UStG13b] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'TenantPays')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [TenantPays] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Insured')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Insured] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'taxDed48')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [taxDed48] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PrjDedSum')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [PrjDedSum] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Barcode')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Barcode] char(50) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PosNetto')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [PosNetto] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PosTotal')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [PosTotal] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PosWages')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [PosWages] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'bshBrutto')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [bshBrutto] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'bshAbzuege')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [bshAbzuege] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'bshSkBasis')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [bshSkBasis] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'bshSkBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [bshSkBetrag] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'bshZahlBtr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [bshZahlBtr] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'TextPdf')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [TextPdf] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'disDownload')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [disDownload] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'UZ')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [UZ] char(10) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'NrReserv')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [NrReserv] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Contact')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Contact] char(50) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Kennzeich')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Kennzeich] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Historie')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ADD [Historie] integer NOT NULL DEFAULT 0
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [UNR] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [SubUNR] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'MareonNo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [MareonNo] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'InvoiceERP')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [InvoiceERP] char(30)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'InvoiceCM')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [InvoiceCM] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'JobERP')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [JobERP] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'CraftsPN')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [CraftsPN] char(10)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [Status] char(1)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Type')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [Type] char(1)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [NrBsh] char(20)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [BelNr] char(8)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Currency')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [Currency] char(3)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [FPA] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Barcode')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [Barcode] char(50)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'UZ')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [UZ] char(10)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'NrReserv')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [NrReserv] char(8)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoices' and sc.[name] = 'Contact')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoices] ALTER COLUMN [Contact] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonInvoices')
  DROP VIEW [dbo].[MareonInvoices]
GO
CREATE VIEW [dbo].[MareonInvoices] AS
  SELECT * FROM [dbo].[ut_MareonInvoices]
GO
CREATE TABLE [dbo].[ut_MareonInvoices_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(70) NOT NULL
  ,[Key01_Value] CHAR(30) NOT NULL
  ,[Key02_Value] CHAR(51) NOT NULL
  ,[Key03_Value] CHAR(90) NOT NULL
  CONSTRAINT [Pk_ut_MareonInvoices_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonInvoices_KeyDel] ON [dbo].[ut_MareonInvoices]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonInvoices_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_MareonInvoices_KeyUpd] ON [dbo].[ut_MareonInvoices]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_MareonInvoices_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_MareonInvoices_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Value],[Key03_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[MareonNo])+STR(RecId,10))
     ,UPPER(convert(char(20),[NrBsh])+STR(RecId,10))
     ,UPPER(convert(char(20),[UNR])+convert(char(1),[Status])+convert(char(20),[MareonNo])+STR(RecId,10))
     ,UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(30),[JobERP])+CONVERT(char(10),[DateInvoice],120)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_MareonInvoices_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[MareonNo])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(20),[NrBsh])+STR([RecId],10))
   ,[Key02_Value] = UPPER(convert(char(20),[UNR])+convert(char(1),[Status])+convert(char(20),[MareonNo])+STR([RecId],10))
   ,[Key03_Value] = UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(30),[JobERP])+CONVERT(char(10),[DateInvoice],120)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_MareonInvoices] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Add_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonInvoices] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonInvoices])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonInvoices] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Upd_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonInvoices]
  @RI integer
 ,@Parm_UNR char(20)
 ,@Parm_SubUNR char(20)
 ,@Parm_MareonNo char(20)
 ,@Parm_InvoiceERP char(30)
 ,@Parm_InvoiceCM char(20)
 ,@Parm_JobERP char(30)
 ,@Parm_CraftsPN char(10)
 ,@Parm_DateInvoice datetime
 ,@Parm_Modify datetime
 ,@Parm_HostDate datetime
 ,@Parm_Status char(1)
 ,@Parm_Type char(1)
 ,@Parm_NrBsh char(20)
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Datetopay datetime
 ,@Parm_Skonto float
 ,@Parm_TextRN integer
 ,@Parm_Canceltext integer
 ,@Parm_Inspection integer
 ,@Parm_Currency char(3)
 ,@Parm_DateDone datetime
 ,@Parm_DateFwd datetime
 ,@Parm_FPA char(20)
 ,@Parm_VAT float
 ,@Parm_fpaPlus float
 ,@Parm_fpaOff float
 ,@Parm_revisedAmount float
 ,@Parm_grossAmount float
 ,@Parm_vatAmount float
 ,@Parm_netAmount float
 ,@Parm_wagesAmount float
 ,@Parm_EStG35a integer
 ,@Parm_UStG13b tinyint
 ,@Parm_TenantPays tinyint
 ,@Parm_Insured tinyint
 ,@Parm_taxDed48 float
 ,@Parm_PrjDedSum float
 ,@Parm_Barcode char(50)
 ,@Parm_PosNetto float
 ,@Parm_PosTotal float
 ,@Parm_PosWages float
 ,@Parm_bshBrutto float
 ,@Parm_bshAbzuege float
 ,@Parm_bshSkBasis float
 ,@Parm_bshSkBetrag float
 ,@Parm_bshZahlBtr float
 ,@Parm_TextPdf integer
 ,@Parm_disDownload tinyint
 ,@Parm_UZ char(10)
 ,@Parm_NrReserv char(8)
 ,@Parm_Contact char(50)
 ,@Parm_Kennzeich integer
 ,@Parm_Historie integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonInvoices] SET
     [UNR] = RTRIM(@Parm_UNR)
    ,[SubUNR] = RTRIM(@Parm_SubUNR)
    ,[MareonNo] = RTRIM(@Parm_MareonNo)
    ,[InvoiceERP] = RTRIM(@Parm_InvoiceERP)
    ,[InvoiceCM] = RTRIM(@Parm_InvoiceCM)
    ,[JobERP] = RTRIM(@Parm_JobERP)
    ,[CraftsPN] = RTRIM(@Parm_CraftsPN)
    ,[DateInvoice] = @Parm_DateInvoice
    ,[Modify] = @Parm_Modify
    ,[HostDate] = @Parm_HostDate
    ,[Status] = RTRIM(@Parm_Status)
    ,[Type] = RTRIM(@Parm_Type)
    ,[NrBsh] = RTRIM(@Parm_NrBsh)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[Datetopay] = @Parm_Datetopay
    ,[Skonto] = @Parm_Skonto
    ,[TextRN] = @Parm_TextRN
    ,[Canceltext] = @Parm_Canceltext
    ,[Inspection] = @Parm_Inspection
    ,[Currency] = RTRIM(@Parm_Currency)
    ,[DateDone] = @Parm_DateDone
    ,[DateFwd] = @Parm_DateFwd
    ,[FPA] = RTRIM(@Parm_FPA)
    ,[VAT] = @Parm_VAT
    ,[fpaPlus] = @Parm_fpaPlus
    ,[fpaOff] = @Parm_fpaOff
    ,[revisedAmount] = @Parm_revisedAmount
    ,[grossAmount] = @Parm_grossAmount
    ,[vatAmount] = @Parm_vatAmount
    ,[netAmount] = @Parm_netAmount
    ,[wagesAmount] = @Parm_wagesAmount
    ,[EStG35a] = @Parm_EStG35a
    ,[UStG13b] = @Parm_UStG13b
    ,[TenantPays] = @Parm_TenantPays
    ,[Insured] = @Parm_Insured
    ,[taxDed48] = @Parm_taxDed48
    ,[PrjDedSum] = @Parm_PrjDedSum
    ,[Barcode] = RTRIM(@Parm_Barcode)
    ,[PosNetto] = @Parm_PosNetto
    ,[PosTotal] = @Parm_PosTotal
    ,[PosWages] = @Parm_PosWages
    ,[bshBrutto] = @Parm_bshBrutto
    ,[bshAbzuege] = @Parm_bshAbzuege
    ,[bshSkBasis] = @Parm_bshSkBasis
    ,[bshSkBetrag] = @Parm_bshSkBetrag
    ,[bshZahlBtr] = @Parm_bshZahlBtr
    ,[TextPdf] = @Parm_TextPdf
    ,[disDownload] = @Parm_disDownload
    ,[UZ] = RTRIM(@Parm_UZ)
    ,[NrReserv] = RTRIM(@Parm_NrReserv)
    ,[Contact] = RTRIM(@Parm_Contact)
    ,[Kennzeich] = @Parm_Kennzeich
    ,[Historie] = @Parm_Historie
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Sel_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonInvoices]
  @RI integer
AS
SELECT
     [UNR]
    ,[SubUNR]
    ,[MareonNo]
    ,[InvoiceERP]
    ,[InvoiceCM]
    ,[JobERP]
    ,[CraftsPN]
    ,[DateInvoice]
    ,[Modify]
    ,[HostDate]
    ,[Status]
    ,[Type]
    ,[NrBsh]
    ,[PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[Datetopay]
    ,[Skonto]
    ,[TextRN]
    ,[Canceltext]
    ,[Inspection]
    ,[Currency]
    ,[DateDone]
    ,[DateFwd]
    ,[FPA]
    ,[VAT]
    ,[fpaPlus]
    ,[fpaOff]
    ,[revisedAmount]
    ,[grossAmount]
    ,[vatAmount]
    ,[netAmount]
    ,[wagesAmount]
    ,[EStG35a]
    ,[UStG13b]
    ,[TenantPays]
    ,[Insured]
    ,[taxDed48]
    ,[PrjDedSum]
    ,[Barcode]
    ,[PosNetto]
    ,[PosTotal]
    ,[PosWages]
    ,[bshBrutto]
    ,[bshAbzuege]
    ,[bshSkBasis]
    ,[bshSkBetrag]
    ,[bshZahlBtr]
    ,[TextPdf]
    ,[disDownload]
    ,[UZ]
    ,[NrReserv]
    ,[Contact]
    ,[Kennzeich]
    ,[Historie]
  FROM [dbo].[ut_MareonInvoices]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Del_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonInvoices]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonInvoices] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Reorg_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonInvoices]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonInvoices] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_MareonInvoices] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_MareonInvoices] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonInvoices')
  DROP PROCEDURE [dbo].[up_Append_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonInvoices]
  @Parm_UNR char(20)
 ,@Parm_SubUNR char(20)
 ,@Parm_MareonNo char(20)
 ,@Parm_InvoiceERP char(30)
 ,@Parm_InvoiceCM char(20)
 ,@Parm_JobERP char(30)
 ,@Parm_CraftsPN char(10)
 ,@Parm_DateInvoice datetime
 ,@Parm_Modify datetime
 ,@Parm_HostDate datetime
 ,@Parm_Status char(1)
 ,@Parm_Type char(1)
 ,@Parm_NrBsh char(20)
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_Datetopay datetime
 ,@Parm_Skonto float
 ,@Parm_TextRN integer
 ,@Parm_Canceltext integer
 ,@Parm_Inspection integer
 ,@Parm_Currency char(3)
 ,@Parm_DateDone datetime
 ,@Parm_DateFwd datetime
 ,@Parm_FPA char(20)
 ,@Parm_VAT float
 ,@Parm_fpaPlus float
 ,@Parm_fpaOff float
 ,@Parm_revisedAmount float
 ,@Parm_grossAmount float
 ,@Parm_vatAmount float
 ,@Parm_netAmount float
 ,@Parm_wagesAmount float
 ,@Parm_EStG35a integer
 ,@Parm_UStG13b tinyint
 ,@Parm_TenantPays tinyint
 ,@Parm_Insured tinyint
 ,@Parm_taxDed48 float
 ,@Parm_PrjDedSum float
 ,@Parm_Barcode char(50)
 ,@Parm_PosNetto float
 ,@Parm_PosTotal float
 ,@Parm_PosWages float
 ,@Parm_bshBrutto float
 ,@Parm_bshAbzuege float
 ,@Parm_bshSkBasis float
 ,@Parm_bshSkBetrag float
 ,@Parm_bshZahlBtr float
 ,@Parm_TextPdf integer
 ,@Parm_disDownload tinyint
 ,@Parm_UZ char(10)
 ,@Parm_NrReserv char(8)
 ,@Parm_Contact char(50)
 ,@Parm_Kennzeich integer
 ,@Parm_Historie integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonInvoices])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonInvoices] (
    RecId
   ,[UNR]
   ,[SubUNR]
   ,[MareonNo]
   ,[InvoiceERP]
   ,[InvoiceCM]
   ,[JobERP]
   ,[CraftsPN]
   ,[DateInvoice]
   ,[Modify]
   ,[HostDate]
   ,[Status]
   ,[Type]
   ,[NrBsh]
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[Datetopay]
   ,[Skonto]
   ,[TextRN]
   ,[Canceltext]
   ,[Inspection]
   ,[Currency]
   ,[DateDone]
   ,[DateFwd]
   ,[FPA]
   ,[VAT]
   ,[fpaPlus]
   ,[fpaOff]
   ,[revisedAmount]
   ,[grossAmount]
   ,[vatAmount]
   ,[netAmount]
   ,[wagesAmount]
   ,[EStG35a]
   ,[UStG13b]
   ,[TenantPays]
   ,[Insured]
   ,[taxDed48]
   ,[PrjDedSum]
   ,[Barcode]
   ,[PosNetto]
   ,[PosTotal]
   ,[PosWages]
   ,[bshBrutto]
   ,[bshAbzuege]
   ,[bshSkBasis]
   ,[bshSkBetrag]
   ,[bshZahlBtr]
   ,[TextPdf]
   ,[disDownload]
   ,[UZ]
   ,[NrReserv]
   ,[Contact]
   ,[Kennzeich]
   ,[Historie]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_UNR)
   ,RTRIM(@Parm_SubUNR)
   ,RTRIM(@Parm_MareonNo)
   ,RTRIM(@Parm_InvoiceERP)
   ,RTRIM(@Parm_InvoiceCM)
   ,RTRIM(@Parm_JobERP)
   ,RTRIM(@Parm_CraftsPN)
   ,@Parm_DateInvoice
   ,@Parm_Modify
   ,@Parm_HostDate
   ,RTRIM(@Parm_Status)
   ,RTRIM(@Parm_Type)
   ,RTRIM(@Parm_NrBsh)
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,@Parm_Datetopay
   ,@Parm_Skonto
   ,@Parm_TextRN
   ,@Parm_Canceltext
   ,@Parm_Inspection
   ,RTRIM(@Parm_Currency)
   ,@Parm_DateDone
   ,@Parm_DateFwd
   ,RTRIM(@Parm_FPA)
   ,@Parm_VAT
   ,@Parm_fpaPlus
   ,@Parm_fpaOff
   ,@Parm_revisedAmount
   ,@Parm_grossAmount
   ,@Parm_vatAmount
   ,@Parm_netAmount
   ,@Parm_wagesAmount
   ,@Parm_EStG35a
   ,@Parm_UStG13b
   ,@Parm_TenantPays
   ,@Parm_Insured
   ,@Parm_taxDed48
   ,@Parm_PrjDedSum
   ,RTRIM(@Parm_Barcode)
   ,@Parm_PosNetto
   ,@Parm_PosTotal
   ,@Parm_PosWages
   ,@Parm_bshBrutto
   ,@Parm_bshAbzuege
   ,@Parm_bshSkBasis
   ,@Parm_bshSkBetrag
   ,@Parm_bshZahlBtr
   ,@Parm_TextPdf
   ,@Parm_disDownload
   ,RTRIM(@Parm_UZ)
   ,RTRIM(@Parm_NrReserv)
   ,RTRIM(@Parm_Contact)
   ,@Parm_Kennzeich
   ,@Parm_Historie
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_MareonInvoices')
  DROP PROCEDURE [dbo].[up_TrSel_MareonInvoices]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonInvoices]
AS
  SELECT   RecId,
      LTRIM(RTRIM([UNR]))
      +LTRIM(RTRIM([SubUNR]))
      +LTRIM(RTRIM([MareonNo]))
      +LTRIM(RTRIM([InvoiceERP]))
      +LTRIM(RTRIM([InvoiceCM]))
      +LTRIM(RTRIM([JobERP]))
      +LTRIM(RTRIM([CraftsPN]))
      +LTRIM(RTRIM([Status]))
      +LTRIM(RTRIM([Type]))
      +LTRIM(RTRIM([NrBsh]))
      +LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Currency]))
      +LTRIM(RTRIM([FPA]))
      +LTRIM(RTRIM([Barcode]))
      +LTRIM(RTRIM([UZ]))
      +LTRIM(RTRIM([NrReserv]))
      +LTRIM(RTRIM([Contact]))
    AS Data
  FROM     [dbo].[ut_MareonInvoices]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonInvoices_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonInvoices_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonInvoices_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonInvoices_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_MareonInvoices_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonInvoices_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonInvoices_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_MareonInvoices_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_MareonInvoices_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_MareonInvoices_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_MareonInvoices_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_MareonInvoices_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

CREATE INDEX [Ik_Key01] ON [dbo].[ut_MareonInvoices_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_First_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_Last_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_Prior_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_Next_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_Next50_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_FindL_MareonInvoices_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonInvoices_Key01')
  DROP PROCEDURE [dbo].[up_FindF_MareonInvoices_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_MareonInvoices_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonInvoices_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonInvoices_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_MareonInvoices_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_MareonInvoices_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_MareonInvoices_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_MareonInvoices_Keys]
      WHERE [Key01_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key01_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_MareonInvoices_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

CREATE INDEX [Ik_Key02] ON [dbo].[ut_MareonInvoices_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_First_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_Last_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_Prior_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_Next_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_Next50_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_FindL_MareonInvoices_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonInvoices_Key02')
  DROP PROCEDURE [dbo].[up_FindF_MareonInvoices_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_MareonInvoices_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonInvoices_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonInvoices_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_MareonInvoices_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_MareonInvoices_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_MareonInvoices_Key02] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key02_Value] AS Key_Value FROM [dbo].[ut_MareonInvoices_Keys]
      WHERE [Key02_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key02_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_MareonInvoices_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

CREATE INDEX [Ik_Key03] ON [dbo].[ut_MareonInvoices_Keys]
( [Key03_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_First_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_Last_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_Prior_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_Next_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_Next50_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_FindL_MareonInvoices_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonInvoices_Key03')
  DROP PROCEDURE [dbo].[up_FindF_MareonInvoices_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_MareonInvoices_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonInvoices_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys] ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonInvoices_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_MareonInvoices_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_MareonInvoices_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_MareonInvoices_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_MareonInvoices_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_MareonInvoices_Key03] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key03_Value] AS Key_Value FROM [dbo].[ut_MareonInvoices_Keys]
      WHERE [Key03_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key03_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_MareonInvoices_Key03] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonInvoices_Keys]
  WHERE [Key03_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key03_Value] DESC
GO

