/*

    Open DBDiff 0.9.0.0
    http://opendbiff.codeplex.com/

    Script created by shu00\Stefan Hutter lokal on 28.04.2018 at 14:16:33.

    Created on:  SHU00
    Source:      dpm2018 on (local)
    Destination: dpm2018_1 on (local)

*/

EXEC sp_rename @objname = 'Tarif.[SSO-Nummer]', @newname = 'SSO_Nummer', @objtype = 'COLUMN'
EXEC sp_rename @objname = 'Leistung.[SSO-Nummer]', @newname = 'SSO_Nummer', @objtype = 'COLUMN'
EXEC sp_rename @objname = 'Privat.[E-Mail]', @newname = 'E_Mail', @objtype = 'COLUMN'
EXEC sp_rename @objname = 'Firma.[E-Mail]', @newname = 'E_Mail', @objtype = 'COLUMN'
go

ALTER TABLE Tarif 
ALTER COLUMN SSO_Nummer VARCHAR(255) 
go

Alter Table Leistung
Alter Column SSO_Nummer varchar(255)
go

Alter Table ESTyp add Default_rgtyp int
go
update estyp set default_rgtyp=1
go
update estyp set default_rgtyp=7 where nrestyp=7 or nrestyp=5
go  

DROP TABLE [dbo].[resttem3]
GO
UPDATE [dbo].[MELDUNG] SET [Nrmeldung] = 0 WHERE [Nrmeldung] IS NULL
GO
UPDATE [dbo].[TAGPLAN] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[OPTIONS] SET [NROPTION] = 0 WHERE [NROPTION] IS NULL
GO
UPDATE [dbo].[DEBITOR] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[TARBUCH] SET [Eintrag] = 0 WHERE [Eintrag] IS NULL
GO
UPDATE [dbo].[tmppatlab] SET [NRPRIVAT] = 0 WHERE [NRPRIVAT] IS NULL
GO
UPDATE [dbo].[TARIFE] SET [Nrtarifgruppe] = 0 WHERE [Nrtarifgruppe] IS NULL
GO
UPDATE [dbo].[LLFILES] SET [LLKEY] = '' WHERE [LLKEY] IS NULL
GO
UPDATE [dbo].[TARIF] SET [Nrtarif] = 0 WHERE [Nrtarif] IS NULL
GO
UPDATE [dbo].[TMPRCALL] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[SPRACHE] SET [Sprachcode] = '' WHERE [Sprachcode] IS NULL
GO
UPDATE [dbo].[BERECHT] SET [Nrberechtigung] = 0 WHERE [Nrberechtigung] IS NULL
GO
UPDATE [dbo].[TMPSORT] SET [Nummer] = 0 WHERE [Nummer] IS NULL
GO
UPDATE [dbo].[PRIVAT] SET [NRPRIVAT] = 0 WHERE [NRPRIVAT] IS NULL
GO
UPDATE [dbo].[Behandlu] SET [Nrbehandlung] = 0 WHERE [Nrbehandlung] IS NULL
GO
UPDATE [dbo].[PAKETPOS] SET [Nrpaketposition] = 0 WHERE [Nrpaketposition] IS NULL
GO
UPDATE [dbo].[STATUSBH] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[OPTIONS] SET [Mandant] = 0 WHERE [Mandant] IS NULL
GO
UPDATE [dbo].[BJOURNAL] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[STATTEXT] SET [Nrstatustext] = 0 WHERE [Nrstatustext] IS NULL
GO
UPDATE [dbo].[ORTE] SET [OrtNr] = 0 WHERE [OrtNr] IS NULL
GO
UPDATE [dbo].[TEXTE] SET [Mandant] = 0 WHERE [Mandant] IS NULL
GO
UPDATE [dbo].[FIRMA] SET [NRFIRMA] = 0 WHERE [NRFIRMA] IS NULL
GO
UPDATE [dbo].[FORMULAR] SET [Nrformular] = 0 WHERE [Nrformular] IS NULL
GO
UPDATE [dbo].[TEXTE] SET [Nrtext] = 0 WHERE [Nrtext] IS NULL
GO
UPDATE [dbo].[FData] SET [NrEintrag] = 0 WHERE [NrEintrag] IS NULL
GO
UPDATE [dbo].[TITEL] SET [NRTITEL] = 0 WHERE [NRTITEL] IS NULL
GO
UPDATE [dbo].[FJOURNAL] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[FIRMAAP] SET [NRFIRMAAP] = 0 WHERE [NRFIRMAAP] IS NULL
GO
UPDATE [dbo].[TEXTE] SET [Sprachcode] = '' WHERE [Sprachcode] IS NULL
GO
UPDATE [dbo].[FORMFELD] SET [Nrfeld] = 0 WHERE [Nrfeld] IS NULL
GO
UPDATE [dbo].[FUNKTION] SET [Nrfunktion] = 0 WHERE [Nrfunktion] IS NULL
GO
UPDATE [dbo].[ESTYP] SET [NRESTYP] = 0 WHERE [NRESTYP] IS NULL
GO
UPDATE [dbo].[KONTO] SET [Nrkonto] = 0 WHERE [Nrkonto] IS NULL
GO
UPDATE [dbo].[Tarifgrp] SET [Nrtarifgruppe] = 0 WHERE [Nrtarifgruppe] IS NULL
GO
UPDATE [dbo].[TMPKONTO] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[LEISTUNG] SET [Nrleistung] = 0 WHERE [Nrleistung] IS NULL
GO
UPDATE [dbo].[Faktura] SET [Nrfaktura] = 0 WHERE [Nrfaktura] IS NULL
GO
UPDATE [dbo].[TAXPUNKT] SET [Nrtaxpunkt] = 0 WHERE [Nrtaxpunkt] IS NULL
GO
UPDATE [dbo].[INDEXES] SET [NREINTRAG] = 0 WHERE [NREINTRAG] IS NULL
GO
UPDATE [dbo].[TARPAKET] SET [Nrpaket] = 0 WHERE [Nrpaket] IS NULL
GO
UPDATE [dbo].[FAKTPOS] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[Report] SET [Nrreport] = 0 WHERE [Nrreport] IS NULL
GO
UPDATE [dbo].[BARCODES] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[RECALL] SET [Nrrecall] = 0 WHERE [Nrrecall] IS NULL
GO
UPDATE [dbo].[ANSATZ] SET [Nransatz] = 0 WHERE [Nransatz] IS NULL
GO
UPDATE [dbo].[WORKLOG] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[ANREDE] SET [NRANREDE] = 0 WHERE [NRANREDE] IS NULL
GO
UPDATE [dbo].[ROLLENP] SET [Nrprivat] = 0 WHERE [Nrprivat] IS NULL
GO
UPDATE [dbo].[VDKonti] SET [NrPrivat] = 0 WHERE [NrPrivat] IS NULL
GO
UPDATE [dbo].[TMPUMSA] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[anzahlug] SET [Nranzahlung] = 0 WHERE [Nranzahlung] IS NULL
GO
UPDATE [dbo].[BARCODE] SET [Datum] = getdate() WHERE [Datum] IS NULL
GO
UPDATE [dbo].[RECHTEXT] SET [Nrrechnungstext] = 0 WHERE [Nrrechnungstext] IS NULL
GO
UPDATE [dbo].[BARCODE] SET [Bcnummer] = 0 WHERE [Bcnummer] IS NULL
GO
UPDATE [dbo].[VSKonti] SET [Nr] = 0 WHERE [Nr] IS NULL
GO
UPDATE [dbo].[BARCODE] SET [Mm] = 0 WHERE [Mm] IS NULL
GO
UPDATE [dbo].[USER] SET [User] = '' WHERE [User] IS NULL
GO
UPDATE [dbo].[BARCODE] SET [Hh] = 0 WHERE [Hh] IS NULL
GO
UPDATE [dbo].[RGTYP] SET [Nrrgtyp] = 0 WHERE [Nrrgtyp] IS NULL
GO
UPDATE [dbo].[BCFNKT] SET [Nrbcfunktion] = 0 WHERE [Nrbcfunktion] IS NULL
GO
UPDATE [dbo].[sortlei] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[Reports] SET [Nrreportgruppe] = 0 WHERE [Nrreportgruppe] IS NULL
GO
UPDATE [dbo].[reportdb] SET [Nreintrag] = 0 WHERE [Nreintrag] IS NULL
GO
UPDATE [dbo].[ABRTYP] SET [Nrabrechnungstyp] = 0 WHERE [Nrabrechnungstyp] IS NULL
GO
UPDATE [dbo].[TMPSORT] SET [Sortfeld2] = '' WHERE [Sortfeld2] IS NULL
GO
UPDATE [dbo].[BCPAGE] SET [Nrbcpage] = 0 WHERE [Nrbcpage] IS NULL
GO
UPDATE [dbo].[PUNKTE] SET [Nrtaxpunkte] = 0 WHERE [Nrtaxpunkte] IS NULL
GO
UPDATE [dbo].[TMPSORT] SET [Sortfeld3] = '' WHERE [Sortfeld3] IS NULL
GO
UPDATE [dbo].[BCART] SET [Nrbarcodeart] = 0 WHERE [Nrbarcodeart] IS NULL
GO
UPDATE [dbo].[TMPSORT] SET [Sortfeld4] = '' WHERE [Sortfeld4] IS NULL
GO
UPDATE [dbo].[Zahlung] SET [Nrzahlung] = 0 WHERE [Nrzahlung] IS NULL
GO
UPDATE [dbo].[TMPSORT] SET [Sortfeld1] = '' WHERE [Sortfeld1] IS NULL
GO
ALTER TABLE [dbo].[TAXPUNKT] ALTER COLUMN [Nrtaxpunkt] [int] NOT NULL
GO
ALTER TABLE [dbo].[TARPAKET] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Res] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[resttem2] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Reports] ALTER COLUMN [Nrreportgruppe] [int] NOT NULL
GO
ALTER TABLE [dbo].[TEXTE] ALTER COLUMN [Nrtext] [int] NOT NULL
GO
ALTER TABLE [dbo].[TEXTE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TEXTE] ALTER COLUMN [Mandant] [int] NOT NULL
GO
ALTER TABLE [dbo].[TEXTE] ALTER COLUMN [Sprachcode] [varchar] (1) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[TITEL] ALTER COLUMN [NRTITEL] [int] NOT NULL
GO
ALTER TABLE [dbo].[resttem1] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Reports] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TAXPUNKT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[STATUSBH] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[STATUSBH] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[STATTEXT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TARBUCH] ALTER COLUMN [Eintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[TAGPLAN] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TAGPLAN] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[sortlei] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[SPRACHE] ALTER COLUMN [Sprachcode] [varchar] (1) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[sortlei] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ROLLENP] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[STATTEXT] ALTER COLUMN [Nrstatustext] [int] NOT NULL
GO
ALTER TABLE [dbo].[SPRACHE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Tarifgrp] ALTER COLUMN [Nrtarifgruppe] [int] NOT NULL
GO
ALTER TABLE [dbo].[RGTYP] ALTER COLUMN [Nrrgtyp] [int] NOT NULL
GO
ALTER TABLE [dbo].[TARIFE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TARPAKET] ALTER COLUMN [Nrpaket] [int] NOT NULL
GO
ALTER TABLE [dbo].[Tarifgrp] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[resttemp] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TARBUCH] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[RGTYP] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ROLLENP] ALTER COLUMN [Nrprivat] [int] NOT NULL
GO
ALTER TABLE [dbo].[TARIFE] ALTER COLUMN [Nrtarifgruppe] [int] NOT NULL
GO
--ALTER TABLE [dbo].[TARIF] DROP COLUMN [SSO-Nummer]
--GO
ALTER TABLE [dbo].[TARIF] ADD 
--[SSO_Nummer] [varchar] (50) COLLATE Latin1_General_CI_AS NULL,
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TARIF] ALTER COLUMN [Nrtarif] [int] NOT NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Franken] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Rappen] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Vorauszahlung] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Frankenbetrag] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPSORT] ALTER COLUMN [Sortfeld2] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[TMPSORT] ALTER COLUMN [Sortfeld3] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[TMPSORT] ALTER COLUMN [Nummer] [int] NOT NULL
GO
ALTER TABLE [dbo].[TMPSORT] ALTER COLUMN [Sortfeld1] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total7000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total8000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total5000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total6000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total9000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Rabatt] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Rabattbetrag] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Ratenzuschlag] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Mahnzuschlag] [float] NULL
GO
ALTER TABLE [dbo].[TMPSORT] ALTER COLUMN [Sortfeld4] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[VSKonti] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[WORKLOG] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[VDKonti] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[VSKonti] ALTER COLUMN [Nr] [int] NOT NULL
GO
ALTER TABLE [dbo].[WORKLOG] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Zahlung_Sik_error] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ZJOURNAL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[pk] [int] IDENTITY (1,1) NOT NULL
GO
ALTER TABLE [dbo].[Zahlung] ALTER COLUMN [Nrzahlung] [int] NOT NULL
GO
ALTER TABLE [dbo].[Zahlung] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPUMSA] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpzaz] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPSORT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPUMSA] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[USER] ALTER COLUMN [User] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[USER] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[Userid] [int] IDENTITY (1,1) NOT NULL
GO
ALTER TABLE [dbo].[VDKonti] ALTER COLUMN [NrPrivat] [int] NOT NULL
GO
ALTER TABLE [dbo].[USER] ALTER COLUMN [Passwort] [varchar] (255) COLLATE Latin1_General_CI_AS NULL
GO
ALTER TABLE [dbo].[USER] ALTER COLUMN [Name] [varchar] (255) COLLATE Latin1_General_CI_AS NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 9000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Rabattwert] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 7000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 8000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Rabattbetrag] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Ratenzuschlag] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Mahnzuschlag] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Vorauszahlung] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Gesamttotal] [float] NULL
GO
ALTER TABLE [dbo].[tmpempf] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 1000] [float] NULL
GO
ALTER TABLE [dbo].[TITEL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpbesr] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 2000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 5000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 6000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 3000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Total 4000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Teilzahlung] [float] NULL
GO
ALTER TABLE [dbo].[tmprecalle] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmprecallw] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPRCALL] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[TMPRCALL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total3000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total4000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total1000] [float] NULL
GO
ALTER TABLE [dbo].[tmprechnung] ALTER COLUMN [Total2000] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmphaz] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [MahnungRateRestTotal] [float] NULL
GO
ALTER TABLE [dbo].[tmpfaktura] ALTER COLUMN [Taxpunktwert] [float] NULL
GO
ALTER TABLE [dbo].[TMPKONTO] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[tmppatlab] ALTER COLUMN [NRPRIVAT] [int] NOT NULL
GO
ALTER TABLE [dbo].[tmppatlab] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[TMPKONTO] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[tmpop] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Calendar] ALTER COLUMN [StartTime] [datetime] NULL
GO
ALTER TABLE [dbo].[FJOURNAL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FJOURNAL] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[BJOURNAL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FORMFELD] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FORMFELD] ALTER COLUMN [Nrfeld] [int] NOT NULL
GO
ALTER TABLE [dbo].[FIRMAAP] ALTER COLUMN [NRFIRMAAP] [int] NOT NULL
GO
--ALTER TABLE [dbo].[FIRMA] DROP COLUMN [E-Mail]
--GO
ALTER TABLE [dbo].[FIRMA] ADD 
--[E_Mail] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[GLN] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[Kanton] [varchar] (255) COLLATE Latin1_General_CI_AS NULL
GO
ALTER TABLE [dbo].[FIRMA] ALTER COLUMN [NRFIRMA] [int] NOT NULL
GO
ALTER TABLE [dbo].[Calendar] ALTER COLUMN [EndTime] [datetime] NULL
GO
ALTER TABLE [dbo].[Calendar] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FIRMAAP] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FORMULAR] ALTER COLUMN [Nrformular] [int] NOT NULL
GO
ALTER TABLE [dbo].[KONTO] ALTER COLUMN [Nrkonto] [int] NOT NULL
GO
ALTER TABLE [dbo].[BESRI] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[NrEintrag] [int] IDENTITY (1,1) NOT NULL,
[besrtnr] int null
GO
ALTER TABLE [dbo].[INDEXES] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BERECHT] ALTER COLUMN [Nrberechtigung] [int] NOT NULL
GO
ALTER TABLE [dbo].[KONTO] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BERECHT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FUNKTION] ALTER COLUMN [Nrfunktion] [int] NOT NULL
GO
ALTER TABLE [dbo].[BJOURNAL] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[FORMULAR] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[INDEXES] ALTER COLUMN [NREINTRAG] [int] NOT NULL
GO
ALTER TABLE [dbo].[BESRT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[Eintragnr] [int] IDENTITY (1,1) NOT NULL
GO
ALTER TABLE [dbo].[FUNKTION] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Rabattbetrag] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Rabattwert] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 9000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Gesamttotal] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Vorauszahlung] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 3000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 5000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 4000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 8000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 7000] [float] NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 6000] [float] NULL
GO
ALTER TABLE [dbo].[ESTYP] ALTER COLUMN [NRESTYP] [int] NOT NULL
GO
ALTER TABLE [dbo].[DEBITOR] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[DEBITOR] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[Faktura] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[CustDocs] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FData] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[FData] ALTER COLUMN [NrEintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[FAKTPOS] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 2000] [float] NULL
GO
ALTER TABLE [dbo].[ESTYP] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[Default_bei_KV] [bit] NULL,
[Default_bei_RG] [bit] NULL,
[Default_bei_KV_Dentotar] [bit] NULL,
[Default_bei_RG_Dentotar] [bit] NULL
GO
ALTER TABLE [dbo].[Faktura] ALTER COLUMN [Nrfaktura] [int] NOT NULL
GO
ALTER TABLE [dbo].[DesignFaktura] ALTER COLUMN [Total 1000] [float] NULL
GO
ALTER TABLE [dbo].[FAKTPOS] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[LEISTUNG] ALTER COLUMN [Nrleistung] [int] NOT NULL
GO
ALTER TABLE [dbo].[BARCODE] ALTER COLUMN [Datum] [date] NOT NULL
GO
ALTER TABLE [dbo].[BARCODE] ALTER COLUMN [Hh] [int] NOT NULL
GO
--ALTER TABLE [dbo].[PRIVAT] DROP COLUMN [E-Mail]
--GO
ALTER TABLE [dbo].[PRIVAT] ADD 
--[E_Mail] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[GLN] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[Kanton] [varchar] (2559) COLLATE Latin1_General_CI_AS NULL,
[ZSR] [varchar] (255) COLLATE Latin1_General_CI_AS NULL
GO
ALTER TABLE [dbo].[ANREDE] ALTER COLUMN [NRANREDE] [int] NOT NULL
GO
ALTER TABLE [dbo].[PUNKTE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[PUNKTE] ALTER COLUMN [Nrtaxpunkte] [int] NOT NULL
GO
ALTER TABLE [dbo].[PRIVAT] ALTER COLUMN [NRPRIVAT] [int] NOT NULL
GO
ALTER TABLE [dbo].[PAKETPOS] ALTER COLUMN [Nrpaketposition] [int] NOT NULL
GO
ALTER TABLE [dbo].[Absender] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BARCODES] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[BARCODE] ALTER COLUMN [Mm] [int] NOT NULL
GO
ALTER TABLE [dbo].[BARCODE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[PAKETPOS] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BARCODE] ALTER COLUMN [Bcnummer] [int] NOT NULL
GO
ALTER TABLE [dbo].[ANSATZ] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[anzahlug] ALTER COLUMN [Nranzahlung] [int] NOT NULL
GO
ALTER TABLE [dbo].[Report] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ANSATZ] ALTER COLUMN [Nransatz] [int] NOT NULL
GO
ALTER TABLE [dbo].[reportdb] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[reportdb] ALTER COLUMN [Nreintrag] [int] NOT NULL
GO
ALTER TABLE [dbo].[Report] ALTER COLUMN [Nrreport] [int] NOT NULL
GO
ALTER TABLE [dbo].[anzahlug] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[RECALL] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[RECALL] ALTER COLUMN [Nrrecall] [int] NOT NULL
GO
ALTER TABLE [dbo].[ANREDE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[RECHTEXT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[RECHTEXT] ALTER COLUMN [Nrrechnungstext] [int] NOT NULL
GO
ALTER TABLE [dbo].[MANDANT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BCPAGE] ALTER COLUMN [Nrbcpage] [int] NOT NULL
GO
ALTER TABLE [dbo].[BCPAGE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BCFNKT] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ABRTYP] ALTER COLUMN [Nrabrechnungstyp] [int] NOT NULL
GO
ALTER TABLE [dbo].[MELDUNG] ALTER COLUMN [Nrmeldung] [int] NOT NULL
GO
ALTER TABLE [dbo].[Behandlu] ALTER COLUMN [Nrbehandlung] [int] NOT NULL
GO
ALTER TABLE [dbo].[Behandlu] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL,
[Unfalldatum] [datetime] NULL,
[Datum_Kostengutsprache] [datetime] NULL,
[Diagnose] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[GLN_Liste] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[Kostengutsprache_Nummer] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[Behandlungsgrund] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[NrZuweiser] [int] NULL,
[NrGesetz] [int] NULL
GO
--ALTER TABLE [dbo].[LEISTUNG] DROP COLUMN [SSO-Nummer]
--GO
ALTER TABLE [dbo].[LEISTUNG] ADD 
--[SSO_Nummer] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[LLFILES] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[LLFILES] ALTER COLUMN [LLKEY] [varchar] (8) COLLATE Latin1_General_CI_AS NOT NULL
GO
ALTER TABLE [dbo].[ABRTYP] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BCFNKT] ALTER COLUMN [Nrbcfunktion] [int] NOT NULL
GO
ALTER TABLE [dbo].[BARCODES] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[BCART] ALTER COLUMN [Nrbarcodeart] [int] NOT NULL
GO
ALTER TABLE [dbo].[OPTIONS] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[MELDUNG] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ORTE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[ORTE] ALTER COLUMN [OrtNr] [int] NOT NULL
GO
ALTER TABLE [dbo].[OPTIONS] ALTER COLUMN [Mandant] [int] NOT NULL
GO
ALTER TABLE [dbo].[MONATE] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
ALTER TABLE [dbo].[OPTIONS] ALTER COLUMN [NROPTION] [int] NOT NULL
GO
ALTER TABLE [dbo].[BCART] ADD 
[erstellt_am] [datetime] NULL,
[mutiert_am] [datetime] NULL,
[mutierer] [int] NULL,
[aktiv] [bit] NULL
GO
CREATE TABLE [dbo].[_tmp_leistung]
(
	[Nrleistung] [int] NOT NULL,
	[Datum] [date] NULL,
	[Nrbehandlung] [int] NULL,
	[Mandant] [int] NULL,
	[Nrtarifgruppe] [int] NULL,
	[Nrtarif] [int] NULL,
	[SSO_Nummer] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Taxpunkte] [float] NULL,
	[Menge] [float] NULL,
	[Frankenprotaxpunkt] [float] NULL,
	[Absolut] [bit] NULL,
	[Total] [float] NULL,
	[Bezd] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Bezi] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Bezf] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[NrBehandler] [float] NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	[aktiv] [bit] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Auswertung]
(
	[AuswertungNr] [int] NOT NULL,
	[ES_Typ] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Folge_Report] [int] NULL,
	[Folgereport_Sep_Druckjob] [bit] NULL,
	[Bezeichnung] [varchar] (50) COLLATE Latin1_General_CI_AS NULL,
	[Drucker] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Papierschacht] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Aktiv] [bit] NULL,
	[Erstellt_am] [datetime] NULL,
	[Mutiert_am] [datetime] NULL,
	[Mutierer] [int] NULL,
	[Report] [bit] NULL,
	[Filename] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Excel_Report] [bit] NULL,
	[SQL] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[SQLType] [varchar] (50) COLLATE Latin1_General_CI_AS NULL,
	[Beschreibung] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[Connectionstring_Subreport] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Reportdatei] [image] NULL,
	[Gruppe] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Design_Funktion] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Kopien] [int] NULL CONSTRAINT [DF_Auswertung_Kopeien] DEFAULT ((1)),
	CONSTRAINT [PK_Auswertung] PRIMARY KEY CLUSTERED
	(
		[AuswertungNr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[__Log]
(
	[_nreintrag] [int] IDENTITY (1,1) NOT NULL,
	[Logeintrag] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	CONSTRAINT [PK___Log] PRIMARY KEY CLUSTERED
	(
		[_nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Dentotar_Parameter]
(
	[EintragNr] [int] NOT NULL,
	[Beschreibung] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Wert] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	[aktiv] [bit] NULL,
	CONSTRAINT [PK_Dentotar_Parameter] PRIMARY KEY CLUSTERED
	(
		[EintragNr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[DentoTar]
(
	[KapitelLeistung] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Level] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[KapitelNrNeu] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[LeistungNrNeu] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[LeistungD] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[LeistungF] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[LeistungI] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[InterpretationD] [varchar] (max) COLLATE Latin1_General_CI_AS NULL,
	[InterpretationF] [varchar] (max) COLLATE Latin1_General_CI_AS NULL,
	[InterpretationI] [varchar] (max) COLLATE Latin1_General_CI_AS NULL,
	[Abrechnungseinheit] [nvarchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[BetragInCHF] [nvarchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Taxpunkte_UV_MV_IV] [float] NULL,
	[PP_Min] [float] NULL,
	[PP_Privat] [float] NULL,
	[PP_Max] [float] NULL,
	[Gueltig_Von] [datetime] NULL,
	[Gueltig_Bis] [datetime] NULL,
	[pk] [int] IDENTITY (1,1) NOT NULL,
	CONSTRAINT [PK_DentoTar] PRIMARY KEY CLUSTERED
	(
		[pk] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[_tmp]
(
	[NRSQL] [int] NOT NULL,
	[SQL] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[Beschreibung] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[spalten]
(
	[SpalteNr] [int] IDENTITY (1,1) NOT NULL,
	[Tabelle] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL,
	[Tabellenspalte] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL,
	[Spalte] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL,
	[Breite] [int] NOT NULL,
	[Reihenfolge] [int] NOT NULL,
	[TipText] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL,
	[Nummer_Format] [varchar] (255) COLLATE Latin1_General_CI_AS NOT NULL,
	[Erstellt_am] [datetime] NOT NULL,
	[Mutiert_am] [datetime] NOT NULL,
	[Mutierer] [int] NOT NULL,
	[aktiv] [bit] NULL,
	[readonly] [bit] NULL,
	[AlsHacken] [bit] NULL,
	CONSTRAINT [PK_spalten] PRIMARY KEY CLUSTERED
	(
		[SpalteNr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[SQL_Statements]
(
	[NRSQL] [int] NOT NULL,
	[SQL] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[Beschreibung] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	CONSTRAINT [PK_SQL_Statements] PRIMARY KEY CLUSTERED
	(
		[NRSQL] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[status]
(
	[NrStatus] [int] NOT NULL,
	[Status] [varchar] (45) COLLATE Latin1_General_CI_AS NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	[aktiv] [bit] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Ortsverzeichnis]
(
	[PLZ] [float] NULL,
	[Ort] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Kanton] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[KT] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[Land] [varchar] (255) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Gesetz]
(
	[NrGesetz] [int] IDENTITY (1,1) NOT NULL,
	[Gesetz] [varchar] (255) COLLATE Latin1_General_CI_AS NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	[aktiv] [bit] NULL,
	CONSTRAINT [PK_Gesetz] PRIMARY KEY CLUSTERED
	(
		[NrGesetz] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[KG]
(
	[NrKG] [int] IDENTITY (1,1) NOT NULL,
	[NrPrivat] [int] NULL,
	[Datum] [datetime] NULL,
	[Beschreibung] [varchar] (4096) COLLATE Latin1_General_CI_AS NULL,
	[Bemerkung] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[aktiv] [bit] NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	CONSTRAINT [PK_KG] PRIMARY KEY CLUSTERED
	(
		[NrKG] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Log]
(
	[nreintrag] [int] IDENTITY (1,1) NOT NULL,
	[ts] [datetime] NULL,
	[Eintrag] [varchar] (1024) COLLATE Latin1_General_CI_AS NULL,
	[Logtype] [int] NULL
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [idx_nrprivat] ON [dbo].[Behandlu]
(
	[Nrpatient] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_vz] ON [dbo].[Zahlung]
(
	[aktiv] ASC,
	[Vorauszahlung] ASC,
	[Nrbehandlung] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_debitor] ON [dbo].[Zahlung]
(
	[Nrdebitor] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_nrfaktura] ON [dbo].[Zahlung]
(
	[Nrfaktura] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_nrbehandlung] ON [dbo].[LEISTUNG]
(
	[Nrbehandlung] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_behandlung] ON [dbo].[Faktura]
(
	[Nrbehandlung] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_debitor] ON [dbo].[Faktura]
(
	[Nrdebitor] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE VIEW [dbo].[View_Zahnarzt]
AS
SELECT     TOP (100) PERCENT NRPRIVAT, NAME + ' ' + VORNAME + ', ' + PLZ + ' ' + ORT AS Concat_Name
FROM         dbo.privat
WHERE     (ZAHNARZT = 1)
ORDER BY Concat_Name
GO
CREATE FUNCTION [dbo].[Modulus10] (@num INT)
RETURNS INT
AS
BEGIN
DECLARE @result INT;
DECLARE @snum VARCHAR(100);
SET @snum = CAST(@num AS VARCHAR (100)); -- converts num to a string value
WITH Partials(n, odd, even)
AS
(
SELECT LEN(@snum)+2 AS n, -- Ignores the first row by starting at offset + 2 to make the code more readable
 0 AS odd,
 0 AS even
UNION ALL
SELECT n - 2, -- "loop" counter
(CAST(COALESCE(SUBSTRING(@snum, n - 2 , 1),'0') AS INT) -- Extracting the n'th odd digit
* 2 / CAST (10 AS INT))
+
(CAST(COALESCE(SUBSTRING(@snum, n - 2, 1),'0') AS INT) -- Extracting the n'th odd digit
* 2 % 10) AS odd,
CAST (COALESCE (SUBSTRING(@snum, n - 3, 1), '0') AS INT) AS even -- Extracting the n'th even digit
FROM Partials
WHERE n >= 1)
SELECT @result =(CASE (SUM(odd + even) % 10)
 WHEN 0 THEN (SUM(odd + even) % 10) ELSE 10 -(SUM(odd + even) % 10)
 END)
FROM Partials;
RETURN(@result)
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_gesetz]
(

@behandlungsnr INT
)
RETURNS varchar(255)
AS
BEGIN

    DECLARE @gesetz VARCHAR(255)
	DECLARE @nr INT
    SELECT @nr=ISNULL(nrgesetz,0) FROM behandlu WHERE Nrbehandlung=@behandlungsnr
	IF @nr=0 BEGIN
       SET @gesetz=''
	END ELSE BEGIN
       SELECT @gesetz=gesetz FROM gesetz WHERE nrgesetz=@nr
	END	
    RETURN @gesetz
    

END
GO
CREATE FUNCTION [dbo].[RoundToFiveOrZero](@inputValue MONEY)
RETURNS MONEY
AS BEGIN
  RETURN ROUND(@inputvalue*2,2)/2
  --RETURN FLOOR((@inputValue + 0.05) * 20.0) / 20.0
END
GO
CREATE VIEW [dbo].[View_Spalten]
AS
SELECT     SpalteNr, Tabelle, Tabellenspalte, Spalte, Breite, Reihenfolge, TipText, Nummer_Format, Erstellt_am, Mutiert_am, Mutierer, aktiv, readonly, AlsHacken
FROM         dbo.spalten
GO
CREATE VIEW [dbo].[View_Versicherung]
AS
SELECT     TOP (100) PERCENT NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
WHERE     (VS = 1)
ORDER BY Concat_Name
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_offener_betrag]
(
	-- Add the parameters for the function here
	@fakturanr int
)
RETURNS float
AS
BEGIN
	 DECLARE @RESULT FLOAT
	 DECLARE @result1 float
     SELECT @result=total FROM faktura WHERE nrfaktura=@fakturanr
	 SELECT  @RESULT1=ISNULL(SUM(Betrag), 0) 
FROM            dbo.Zahlung
WHERE        (Status <> 9) AND (Nrfaktura = @fakturanr) AND (CAST(aktiv AS int) = 1)
RETURN @RESULT-@RESULt1
END
GO
CREATE function [dbo].[GetRoundedValue](@inputValue MONEY)
returns MONEY
as begin
  return (ROUND(@inputValue*2,1)/2)
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_name]
(
@keyvalue int
)
RETURNS VARCHAR(255)
AS
BEGIN
   DECLARE @res VARCHAR(255)
   IF @keyvalue < 50000 BEGIN
      SELECT @res=LTRIM(STR(nrprivat))+' '+name+' '+vorname+', '+plz+' '+ort FROM privat WHERE NRPRIVAT=@keyvalue
   END ELSE BEGIN
      SELECT @res=LTRIM(STR(nrfirma))+' '+name1+', '+plz+' '+ort FROM dbo.FIRMA WHERE NRFIRMA=@keyvalue
   END
   RETURN @res
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[Get_Totale]
(
	-- Add the parameters for the function here
	@typ INT,
	@nrbehanlung int
)
RETURNS FLOAT
AS
BEGIN
     DECLARE @res FLOAT	
	 IF @typ=1 begin-- SSO-Leistungen
	    SELECT @res=ISNULL(SUM(total),0) FROM dbo.LEISTUNG WHERE SUBSTRING(SSO_Nummer,1,1)='4' AND aktiv=1 AND Nrbehandlung=@nrbehanlung
	    --SELECT @res=ISNULL(SUM(total),0) FROM leistung WHERE SSO_Nummer>3999 AND SSO_Nummer<5000 AND aktiv=1 AND Nrbehandlung=@nrbehanlung
	 END
	 IF @typ=2 BEGIN -- Medikamente
 	    SELECT @res=ISNULL(SUM(total),0) FROM dbo.LEISTUNG WHERE (SUBSTRING(SSO_Nummer,1,1)='5' OR SUBSTRING(SSO_Nummer,1,1)='6' OR SUBSTRING(SSO_Nummer,1,1)='7' ) AND aktiv=1 AND Nrbehandlung=@nrbehanlung
	    --SELECT @res=ISNULL(SUM(total),0) FROM leistung WHERE SSO_Nummer>4999 AND SSO_Nummer<8000 AND aktiv=1 AND Nrbehandlung=@nrbehanlung
     END
	 IF @typ=3 BEGIN -- Labor
 	    SELECT @res=ISNULL(SUM(total),0) FROM dbo.LEISTUNG WHERE (SUBSTRING(SSO_Nummer,1,1)='8') AND aktiv=1 AND Nrbehandlung=@nrbehanlung
 	    --SELECT @res=ISNULL(SUM(total),0) FROM leistung WHERE SSO_Nummer>7999 AND SSO_Nummer<9000 AND aktiv=1 AND Nrbehandlung=@nrbehanlung
     END
	 IF @typ=4 BEGIN -- Rabatt
	    DECLARE @rabatgesamt INT
		DECLARE @rabatt float
		DECLARE @sum float
		SELECT @rabatgesamt=RabattGesamt,@rabatt=Rabatt FROM dbo.Behandlu WHERE nrbehandlung=@nrbehanlung
		  
		IF @rabatgesamt=1 BEGIN
		   SET @sum=dbo.Get_Totale(1,@nrbehanlung)+dbo.Get_Totale(2,@nrbehanlung)+dbo.Get_Totale(3,@nrbehanlung)
		   SET @res=dbo.getroundedvalue(@sum/100*@rabatt)
		END ELSE BEGIN
		   SELECT @sum=dbo.Get_Totale(1,@nrbehanlung)
		   SET @res=dbo.getroundedvalue(@sum/100*@rabatt)
        end
     END
     IF @typ=5 BEGIN -- Vorauszahlungen
	    SELECT @res=ISNULL(SUM(betrag),0) FROM zahlung WHERE Vorauszahlung=1 AND aktiv=1 AND Nrbehandlung=@nrbehanlung
	 END
     IF @typ=6 BEGIN
        SET @sum=dbo.Get_Totale(1,@nrbehanlung)+dbo.Get_Totale(2,@nrbehanlung)+dbo.Get_Totale(3,@nrbehanlung)-dbo.Get_Totale(4,@nrbehanlung)
	 end
     RETURN @res

END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_totalblock]
(
	-- Add the parameters for the function here
	@typ INT,
	@nrbehanlung INT
)
RETURNS VARCHAR(1024)
AS
BEGIN
	DECLARE @res VARCHAR(1024)
	DECLARE @res2 VARCHAR(1024)
	DECLARE @sum float
	declare @crlf varchar(2)
    SET @crlf  = char(13) + char(10)
	SET @res=''
	SET @res2=''

	SELECT @sum=ISNULL(dbo.get_totale(1,@nrbehanlung),0)
	IF @sum<>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'Total Leistungen'
	   SET @res2=@res2+STR(@sum,10,2)
	END
    SELECT @sum=ISNULL(dbo.get_totale(2,@nrbehanlung),0)
	IF @sum<>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'Medikamente und Diverses'
	   SET @res2=@res2+STR(@sum,10,2)
	END
	SELECT @sum=ISNULL(dbo.get_totale(3,@nrbehanlung),0)
	IF @sum<>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'Labor'
	   SET @res2=@res2+STR(@sum,10,2)
	END
	DECLARE @rate FLOAT
    SELECT @rate=ratenzuschlag FROM dbo.Behandlu WHERE Nrbehandlung=@nrbehanlung
	IF @rate>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'Ratenzuschlag'
	   SET @res2=@res2+STR(@rate,10,2)
	end
	SELECT @sum=ISNULL(dbo.get_totale(4,@nrbehanlung),0)
	IF @sum<>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'./. Rabatt'
	   SET @res2=@res2+STR(@sum,10,2)
	END
	SELECT @sum=ISNULL(dbo.get_totale(5,@nrbehanlung),0)
	IF @sum<>0 BEGIN
       IF @res<>'' BEGIN
	      SET @res=@res+@crlf
		  SET @res2=@res2+@crlf
	   end
	   SET @res=@res+'./. Vorauszahlunagen'
	   SET @res2=@res2+STR(@sum,10,2)
	END
	IF @typ=1 RETURN @res
	IF @typ=2 RETURN @res2
	RETURN ''
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_FakturaNr]
(
)
RETURNS int
AS
BEGIN
	DECLARE @nrfaktura varchar(255)
    SELECT TOP 1 @nrfaktura=nrfaktura FROM faktura ORDER BY nrfaktura DESC
    IF YEAR(GETDATE())=LEFT(@nrfaktura,4) BEGIN
       SET @nrfaktura=@nrfaktura+1
	END ELSE BEGIN
       SET @nrfaktura=LTRIM(RTRIM(STR(YEAR(GETDATE()))))+'000001'
	end
	RETURN @nrfaktura


END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_adresse_direkt]
(
	@keyvalue INT,
	@einzeilig INT=0
)
RETURNS varchar(255)
AS
BEGIN
	DECLARE @Adresstype int
	DECLARE @Nrprivat int
	DECLARE @res VARCHAR(255)
	DECLARE @res2 VARCHAR(255)
	DECLARE @garant INT
	DECLARE @ansprechpartner INT
    declare @adresse varchar(255)
    DECLARE @crlf varchar(2)
    SET @crlf  = char(13) + char(10)
	IF @einzeilig=1 SET @crlf = ' '

	SET @garant=@keyvalue
    SET @nrprivat=@keyvalue
IF @garant > 0 AND @garant > 49000  GOTO Firmenadresse   

	SET @adresse=''
    SELECT @res=dbo.anrede.Anrede_D FROM dbo.anrede INNER JOIN dbo.privat ON dbo.anrede.NRANREDE = dbo.privat.NRANREDE 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	SET @adresse=@res

	SELECT @res=ISNULL(dbo.titel.titel_d,'') FROM dbo.Titel INNER JOIN dbo.privat ON dbo.titel.nrtitel = dbo.privat.nrtitel 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
  
    SELECT @res= vorname+' '+name FROM privat WHERE NRPRIVAT=@Nrprivat
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
  
    SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res

    SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
 
    SELECT @res= STRASSE FROM privat WHERE NRPRIVAT=@Nrprivat
	IF @res<>'' SET @adresse=@adresse+@crlf+@res

    SELECT @res=plz+' '+ort FROM privat WHERE NRPRIVAT=@Nrprivat 
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=@adresse
	GOTO ende

firmenadresse:
    SET @adresse=''
	SET @res=''
	SELECT @adresse=name1 FROM firma WHERE NRFIRMA=@garant
	SELECT @res=ISNULL(name2,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(dbo.ANREDE.Anrede_D +' '+ dbo.firmaap.NAME,'') FROM  dbo.ANREDE LEFT OUTER JOIN  dbo.FIRMAAP ON dbo.ANREDE.NRANREDE = dbo.FIRMAAP.NRANREDE 
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(strasse,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(POSTFACH,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(plz+' '+ort,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=@adresse



   --SET @res=''
   --IF @zeilenr=1 BEGIN
   --   SELECT @res=name1 FROM firma WHERE nrfirma=@garant
   --END
   --IF @ZeileNr=2 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=dbo.ANREDE.Anrede_D +' '+ dbo.firmaap.NAME FROM  dbo.ANREDE LEFT OUTER JOIN  dbo.FIRMAAP ON dbo.ANREDE.NRANREDE = dbo.FIRMAAP.NRANREDE 
   --   END ELSE begin   
   --      SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
	  --end
   --END  
   --IF @ZeileNr=3 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
	  --END ELSE BEGIN
   --      SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
   --   END
   --end 
   --IF @ZeileNr=4 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
	  --END ELSE BEGIN
	  --   SELECT @res2=ISNULL(postfach,'') FROM firma WHERE NRFIRMA=@garant
		 --IF @res2='' SELECT @res=plz + ' '+ort FROM dbo.FIRMA WHERE NRFIRMA=@garant ELSE SET @res=@res2   
   --   END
   --end 
   --IF @ZeileNr=5 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=plz + ' '+ort FROM dbo.FIRMA WHERE NRFIRMA=@garant 
	  --END ELSE BEGIN
   --      SELECT @res=ISNULL(Postfach,'') FROM firma WHERE NRFIRMA=@garant
   --   END
   --end 

   --RETURN @res
ende:
   RETURN @res
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_adresse]
(
	@nrbehandlung INT,
	@einzeilig INT=0
)
RETURNS varchar(255)
AS
BEGIN
	DECLARE @Adresstype int
	DECLARE @Nrprivat int
	DECLARE @res VARCHAR(255)
	DECLARE @res2 VARCHAR(255)
	DECLARE @garant INT
	DECLARE @ansprechpartner INT
    declare @adresse varchar(255)
    DECLARE @crlf varchar(2)
    SET @crlf  = char(13) + char(10)
	IF @einzeilig=1 SET @crlf = ' '


	SELECT @nrprivat=Nrpatient,@garant=ISNULL(Nrgarant,0),@ansprechpartner=Nransprechpartner FROM behandlu WHERE Nrbehandlung=@nrbehandlung
	IF @garant > 0 AND @garant < 50000 BEGIN
	   SET @Nrprivat=@garant
	end
    
IF @garant > 0 AND @garant > 49000  GOTO Firmenadresse   

	SET @adresse=''
	SET @res=''
    SELECT @res=dbo.anrede.Anrede_D FROM dbo.anrede INNER JOIN dbo.privat ON dbo.anrede.NRANREDE = dbo.privat.NRANREDE 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	SET @adresse=@res
	SET @res=''
	SELECT @res=ISNULL(dbo.titel.titel_d,'') FROM dbo.Titel INNER JOIN dbo.privat ON dbo.titel.nrtitel = dbo.privat.nrtitel 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=''
  
    SELECT @res= vorname+' '+name FROM privat WHERE NRPRIVAT=@Nrprivat
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=''
  
    SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=''

    SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=''
 
    SELECT @res= STRASSE FROM privat WHERE NRPRIVAT=@Nrprivat
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=''

    SELECT @res=plz+' '+ort FROM privat WHERE NRPRIVAT=@Nrprivat 
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=@adresse
	GOTO ende

firmenadresse:
    SET @adresse=''
	SET @res=''
	SELECT @adresse=name1 FROM firma WHERE NRFIRMA=@garant
	SELECT @res=ISNULL(name2,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(dbo.ANREDE.Anrede_D +' '+ dbo.firmaap.NAME,'') FROM  dbo.ANREDE LEFT OUTER JOIN  dbo.FIRMAAP ON dbo.ANREDE.NRANREDE = dbo.FIRMAAP.NRANREDE 
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(strasse,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(POSTFACH,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SELECT @res=ISNULL(plz+' '+ort,'') FROM dbo.FIRMA WHERE NRFIRMA=@garant
	IF @res<>'' SET @adresse=@adresse+@crlf+@res
	SET @res=@adresse



   --SET @res=''
   --IF @zeilenr=1 BEGIN
   --   SELECT @res=name1 FROM firma WHERE nrfirma=@garant
   --END
   --IF @ZeileNr=2 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=dbo.ANREDE.Anrede_D +' '+ dbo.firmaap.NAME FROM  dbo.ANREDE LEFT OUTER JOIN  dbo.FIRMAAP ON dbo.ANREDE.NRANREDE = dbo.FIRMAAP.NRANREDE 
   --   END ELSE begin   
   --      SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
	  --end
   --END  
   --IF @ZeileNr=3 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
	  --END ELSE BEGIN
   --      SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
   --   END
   --end 
   --IF @ZeileNr=4 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
	  --END ELSE BEGIN
	  --   SELECT @res2=ISNULL(postfach,'') FROM firma WHERE NRFIRMA=@garant
		 --IF @res2='' SELECT @res=plz + ' '+ort FROM dbo.FIRMA WHERE NRFIRMA=@garant ELSE SET @res=@res2   
   --   END
   --end 
   --IF @ZeileNr=5 BEGIN
   --   IF @ansprechpartner<>0 BEGIN
   --      SELECT @res=plz + ' '+ort FROM dbo.FIRMA WHERE NRFIRMA=@garant 
	  --END ELSE BEGIN
   --      SELECT @res=ISNULL(Postfach,'') FROM firma WHERE NRFIRMA=@garant
   --   END
   --end 

   --RETURN @res
ende:
   RETURN @res
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[Get_Dentotar_Parameter]
(
	@Paramnr int,
	@behandlungnr int
	
)
RETURNS VARCHAR(255)
AS
BEGIN
    DECLARE @result VARCHAR(255)
	SET @result=''
    DECLARE @key INT
	declare @beh varchar(255)

	SELECT @result=wert FROM dentotar_parameter WHERE eintragnr=@Paramnr
	IF charindex('&Behandler&',@result)>-1 BEGIN
	   SELECT @key = nrbehandler FROM behandlu WHERE Nrbehandlung=@behandlungnr
	   SELECT @beh=name+' '+vorname FROM privat WHERE nrprivat=@key
	   SELECT @result=REPLACE(@result,'&Behandler&',@beh)
	END
	IF charindex('&Behandler1&',@result)>-1 BEGIN
	   SELECT @key = nrbehandler FROM behandlu WHERE Nrbehandlung=@behandlungnr
	   SELECT @beh=name+' '+vorname FROM privat WHERE nrprivat=1
	   SELECT @result=REPLACE(@result,'&Behandler1&',@beh)
	END
    IF charindex('&GLN-Empfaenger&',@result) > 0 BEGIN
		DECLARE @nrprivat int
		declare @garant int    
		SELECT @nrprivat=Nrpatient,@garant=ISNULL(Nrgarant,-1)FROM behandlu WHERE Nrbehandlung=@behandlungnr
	    IF @garant > 49999 begin
	       SELECT @result=ISNULL(gln,'') FROM firma WHERE nrfirma=@garant
		END ELSE BEGIN
	       SELECT @result=ISNULL(gln,'') FROM privat WHERE nrprivat=@nrprivat
		END
	end    
	

    RETURN @result
    

END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[get_adresszeile]
(
	@ZeileNr INT,
	@nrbehandlung int
)
RETURNS varchar(255)
AS
BEGIN
	DECLARE @Adresstype int
	DECLARE @Nrprivat int
	DECLARE @res VARCHAR(255)
	DECLARE @res2 VARCHAR(255)
	DECLARE @garant INT
	DECLARE @ansprechpartner INT
    
	SELECT @nrprivat=Nrpatient,@garant=ISNULL(Nrgarant,0),@ansprechpartner=Nransprechpartner FROM behandlu WHERE Nrbehandlung=@nrbehandlung
	IF @garant > 0 AND @garant < 50000 BEGIN
	   SET @Nrprivat=@garant
	end
    
	IF @garant > 0 AND @garant > 49000  GOTO Firmenadresse   

	IF @zeilenr=1 BEGIN
       SELECT @res=dbo.anrede.Anrede_D FROM dbo.anrede INNER JOIN dbo.privat ON dbo.anrede.NRANREDE = dbo.privat.NRANREDE 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	END
    IF @zeilenr=2 BEGIN
	   SELECT @res=ISNULL(dbo.titel.titel_d,'') FROM dbo.Titel INNER JOIN dbo.privat ON dbo.titel.nrtitel = dbo.privat.nrtitel 
	       WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	   SELECT @res2= vorname+' '+name FROM privat WHERE NRPRIVAT=@Nrprivat
	   IF @res<>'' SET @res=@res+' '+@res2 ELSE SET @res=@res2

    END
    IF @zeilenr=3 BEGIN
	   SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	   IF @res='' SELECT @res= STRASSE FROM privat WHERE NRPRIVAT=@Nrprivat
	END
    IF @zeilenr=4 BEGIN
	   SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	   IF @res<>'' BEGIN
	      SELECT @res= STRASSE FROM privat WHERE NRPRIVAT=@Nrprivat
	   END ELSE BEGIN
	      SELECT @res=plz+' '+ort FROM privat WHERE NRPRIVAT=@Nrprivat
       END
	END
	IF @zeilenr=5 BEGIN
	   SELECT @res=ISNULL(COADRESSE,'') FROM privat WHERE (dbo.privat.NRPRIVAT = @Nrprivat)
	   IF @res<>'' BEGIN
	      SELECT @res=plz+' '+ort FROM privat WHERE NRPRIVAT=@Nrprivat
	   END ELSE BEGIN
	      SET @res=''
       END
	END
	RETURN @res
	GOTO ende

firmenadresse:
   SET @res=''
   IF @zeilenr=1 BEGIN
      SELECT @res=name1 FROM firma WHERE nrfirma=@garant
   END
   IF @ZeileNr=2 BEGIN
      IF @ansprechpartner<>0 BEGIN
         SELECT @res=dbo.ANREDE.Anrede_D +' '+ dbo.firmaap.NAME FROM  dbo.ANREDE LEFT OUTER JOIN  dbo.FIRMAAP ON dbo.ANREDE.NRANREDE = dbo.FIRMAAP.NRANREDE 
      END ELSE begin   
         SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
	  end
   END  
   IF @ZeileNr=3 BEGIN
      IF @ansprechpartner<>0 BEGIN
         SELECT @res=ISNULL(name2,'') FROM firma WHERE NRFIRMA=@garant
      END ELSE begin   
         SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
	  end
   END  
   IF @ZeileNr=4 BEGIN
      IF @ansprechpartner<>0 BEGIN
         SELECT @res=ISNULL(Strasse,'') FROM firma WHERE NRFIRMA=@garant
      END ELSE begin   
         SELECT @res=ISNULL(Postfach,'') FROM firma WHERE NRFIRMA=@garant
	  end
   END  
   IF @ZeileNr=5 BEGIN
      IF @ansprechpartner<>0 BEGIN
         SELECT @res=ISNULL(Postfach,'') FROM firma WHERE NRFIRMA=@garant
      END ELSE begin   
         SELECT @res=ISNULL(PLZ,'') +' ' + isnull(ort,'') FROM firma WHERE NRFIRMA=@garant
	  end
   END  


   RETURN @res
ende:
   RETURN @res
END
GO
CREATE VIEW [dbo].[View_Rechnung]
AS
SELECT        dbo.Behandlu.Nrbehandlung, dbo.Behandlu.Behandlungsbeginn, dbo.Behandlu.Behandlungsende, dbo.PRIVAT.NAME + ' ' + dbo.PRIVAT.VORNAME AS Patient, 
                         privat_1.NAME + ' ' + privat_1.VORNAME AS Behandler, dbo.Behandlu.Taxpunktwert, dbo.Behandlu.Rabatt, dbo.Behandlu.Total - dbo.Get_Totale(5, dbo.Behandlu.Nrbehandlung) AS Total, 
                         dbo.Behandlu.Rechnungsbemerkung, dbo.Behandlu.Ratenzuschlag, dbo.Behandlu.Andrucken_Taxpunkte, dbo.Behandlu.Anzahlung, dbo.LEISTUNG.Nrleistung, dbo.LEISTUNG.Datum, 
                         dbo.LEISTUNG.SSO_Nummer, dbo.LEISTUNG.Taxpunkte, dbo.LEISTUNG.Menge, dbo.LEISTUNG.Frankenprotaxpunkt, dbo.LEISTUNG.Absolut, dbo.LEISTUNG.Total AS Leistungstotal, dbo.LEISTUNG.Bezd, 
                         dbo.get_adresszeile(1, dbo.Behandlu.Nrbehandlung) AS AdressZeile1, dbo.get_adresszeile(2, dbo.Behandlu.Nrbehandlung) AS AdressZeile2, dbo.get_adresszeile(3, dbo.Behandlu.Nrbehandlung) 
                         AS AdressZeile3, dbo.get_adresszeile(4, dbo.Behandlu.Nrbehandlung) AS AdressZeile4, dbo.get_adresszeile(5, dbo.Behandlu.Nrbehandlung) AS AdressZeile5, dbo.Get_Totale(1, dbo.Behandlu.Nrbehandlung) 
                         AS Total_SSO, dbo.Get_Totale(2, dbo.Behandlu.Nrbehandlung) AS Total_Medikamente, dbo.Get_Totale(3, dbo.Behandlu.Nrbehandlung) AS Total_Labor, dbo.Get_Totale(4, dbo.Behandlu.Nrbehandlung) 
                         AS RabattTotal, dbo.Get_Totale(5, dbo.Behandlu.Nrbehandlung) AS Vorauszahlung, dbo.get_totalblock(1, dbo.Behandlu.Nrbehandlung) AS TotalTextblock, dbo.get_totalblock(2, dbo.Behandlu.Nrbehandlung) 
                         AS TotalNumBlock
FROM            dbo.Behandlu INNER JOIN
                         dbo.PRIVAT ON dbo.Behandlu.Nrpatient = dbo.PRIVAT.NRPRIVAT INNER JOIN
                         dbo.PRIVAT AS privat_1 ON dbo.Behandlu.Nrbehandler = privat_1.NRPRIVAT INNER JOIN
                         dbo.LEISTUNG ON dbo.Behandlu.Nrbehandlung = dbo.LEISTUNG.Nrbehandlung
WHERE        (dbo.LEISTUNG.aktiv = 1)
GO
CREATE VIEW [dbo].[View_Garant]
AS
SELECT     TOP (100) PERCENT NRPRIVAT, NAME + ' ' + VORNAME + ', ' + PLZ + ' ' + ORT AS Concat_Name
FROM         dbo.privat
WHERE     (GARANT = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_Fuersorge]
AS
SELECT     TOP (100) PERCENT NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
WHERE     (FS = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_GesVertreter]
AS
SELECT     TOP (100) PERCENT NRPRIVAT, NAME + ' ' + VORNAME + ', ' + PLZ + ' ' + ORT AS Concat_Name
FROM         dbo.privat
WHERE     (GESVERTRETER = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_IV]
AS
SELECT     TOP (100) PERCENT NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
WHERE     (IV = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_Hausarzt]
AS
SELECT     TOP (100) PERCENT NRPRIVAT, NAME + ' ' + VORNAME + ', ' + PLZ + ' ' + ORT AS Concat_Name
FROM         dbo.privat
WHERE     (HAUSARZT = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_Arbeitgeber]
AS
SELECT     TOP (100) PERCENT NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
WHERE     (AG = 1)
ORDER BY NAME1
GO
CREATE VIEW [dbo].[view_abgeschlossene_Behandlungen]
AS
SELECT        TOP (100) PERCENT dbo.Behandlu.Nrbehandlung, dbo.get_name(dbo.Behandlu.Nrpatient) AS Patient, ISNULL(dbo.get_name(dbo.Behandlu.Nrgarant), '') AS Garant, dbo.Behandlu.Behandlungsbeginn, 
                         dbo.Behandlu.Behandlungsende, dbo.Behandlu.Nrestyp, dbo.Behandlu.Anzahlraten AS Raten
FROM            dbo.Behandlu INNER JOIN
                         dbo.PRIVAT ON dbo.Behandlu.Nrpatient = dbo.PRIVAT.NRPRIVAT
WHERE        (dbo.Behandlu.Status = 2) AND (dbo.Behandlu.aktiv = 1)
ORDER BY dbo.Behandlu.Nrbehandlung
GO
CREATE VIEW [dbo].[View_Behandler]
AS
SELECT     TOP (100) PERCENT NRPRIVAT, NAME + ' ' + VORNAME + ', ' + PLZ + ' ' + ORT AS Concat_Name
FROM         dbo.privat
WHERE     (BEHANDLER = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_Firma]
AS
SELECT     NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
GO
CREATE VIEW [dbo].[View_Behandlungsuebersicht]
AS
SELECT        dbo.behandlu.Nrbehandlung, dbo.statusbh.Bezeichnung, dbo.behandlu.aktiv, dbo.privat.NAME AS Behandler, dbo.behandlu.Behandlungsbeginn, dbo.behandlu.Behandlungsende, dbo.behandlu.Rabatt, 
                         dbo.behandlu.Total, dbo.behandlu.Taxpunktwert, dbo.behandlu.Nrpatient, dbo.behandlu.Status
FROM            dbo.behandlu INNER JOIN
                         dbo.statusbh ON dbo.behandlu.Status = dbo.statusbh.Nreintrag LEFT OUTER JOIN
                         dbo.privat ON dbo.behandlu.Nrbehandler = dbo.privat.NRPRIVAT
WHERE        (dbo.behandlu.aktiv = 1)
GO
CREATE VIEW [dbo].[View_Leistungen_mit_Datum]
AS
SELECT        1 AS Typ, dbo.LEISTUNG.Datum, dbo.LEISTUNG.SSO_Nummer, dbo.LEISTUNG.Bezd, dbo.LEISTUNG.Taxpunkte, SUM(dbo.LEISTUNG.Menge) AS Expr2, dbo.LEISTUNG.Frankenprotaxpunkt, CONVERT(int, dbo.LEISTUNG.Absolut) 
                         AS Absolut, SUM(dbo.LEISTUNG.Total) AS Leistungstotal, dbo.Behandlu.Nrbehandlung
FROM            dbo.Behandlu INNER JOIN
                         dbo.LEISTUNG ON dbo.Behandlu.Nrbehandlung = dbo.LEISTUNG.Nrbehandlung
WHERE        (dbo.LEISTUNG.aktiv = 1)
GROUP BY dbo.LEISTUNG.SSO_Nummer, dbo.LEISTUNG.Taxpunkte, dbo.LEISTUNG.Bezd, CONVERT(int, dbo.LEISTUNG.Absolut), dbo.LEISTUNG.Frankenprotaxpunkt, dbo.LEISTUNG.Datum, dbo.Behandlu.Nrbehandlung
GO
CREATE VIEW [dbo].[View_RacallPatient]
AS
SELECT     dbo.recall.Nrrecall, dbo.recall.Nrprivat, dbo.recall.Mandant, dbo.recall.Nrbehandler, dbo.recall.Zazrecall, dbo.recall.Datum, dbo.recall.Zeitvon, dbo.recall.Zeitbis, 
                      dbo.recall.Bemerkung, dbo.recall.Gedruckt, dbo.recall.erstellt_am, dbo.recall.mutiert_am, dbo.recall.mutierer, dbo.recall.aktiv, 
                      dbo.privat.NAME + ' ' + dbo.privat.VORNAME + ', ' + dbo.privat.PLZ + ' ' + dbo.privat.ORT AS Expr1
FROM         dbo.recall LEFT OUTER JOIN
                      dbo.privat ON dbo.recall.Nrbehandler = dbo.privat.NRPRIVAT
GO
CREATE VIEW [dbo].[View_Offene_Rechnungen]
AS
SELECT        TOP (100) PERCENT dbo.DEBITOR.Nreintrag, dbo.Faktura.Nrfaktura, dbo.DEBITOR.Faelligam, dbo.DEBITOR.Betrag, dbo.DEBITOR.Status, dbo.get_adresse_direkt(dbo.DEBITOR.Nrpatient, 1) AS Patient, 
                         ISNULL(dbo.get_adresse_direkt(dbo.DEBITOR.Nrdebitor, 1), '') AS Debitor, dbo.DEBITOR.Nrpatient, dbo.get_offener_betrag(dbo.Faktura.Nrfaktura) AS Offener_Betrag, dbo.DEBITOR.Nrdebitor
FROM            dbo.DEBITOR INNER JOIN
                         dbo.Faktura ON dbo.DEBITOR.Nrfaktura = dbo.Faktura.Nrfaktura
WHERE        (dbo.DEBITOR.Status <> 9) AND (dbo.DEBITOR.Betrag <> 0) AND (dbo.DEBITOR.aktiv = 1) AND (dbo.get_offener_betrag(dbo.Faktura.Nrfaktura) > 0)
ORDER BY dbo.DEBITOR.Faelligam DESC
GO
CREATE VIEW [dbo].[View_Leistungen_ohne_Datum]
AS
SELECT        1 AS Typ, dbo.LEISTUNG.SSO_Nummer, dbo.LEISTUNG.Bezd, dbo.LEISTUNG.Taxpunkte, SUM(dbo.LEISTUNG.Menge) AS Expr2, dbo.LEISTUNG.Frankenprotaxpunkt, CONVERT(int, dbo.LEISTUNG.Absolut) AS Absolut, 
                         SUM(dbo.LEISTUNG.Total) AS Leistungstotal, dbo.Behandlu.Nrbehandlung
FROM            dbo.Behandlu INNER JOIN
                         dbo.LEISTUNG ON dbo.Behandlu.Nrbehandlung = dbo.LEISTUNG.Nrbehandlung
WHERE        (dbo.LEISTUNG.aktiv = 1)
GROUP BY dbo.LEISTUNG.SSO_Nummer, dbo.LEISTUNG.Taxpunkte, dbo.LEISTUNG.Bezd, CONVERT(int, dbo.LEISTUNG.Absolut), dbo.LEISTUNG.Frankenprotaxpunkt, dbo.Behandlu.Nrbehandlung
GO
CREATE VIEW [dbo].[View_Recall]
AS
SELECT     dbo.recall.Nrrecall, dbo.recall.Nrprivat, dbo.recall.Mandant, dbo.recall.Nrbehandler, dbo.recall.Zazrecall, dbo.recall.Datum, dbo.recall.Zeitvon, dbo.recall.Zeitbis, 
                      dbo.recall.Bemerkung, dbo.recall.Gedruckt, dbo.recall.erstellt_am, dbo.recall.mutiert_am, dbo.recall.mutierer, dbo.recall.aktiv, 
                      dbo.privat.NAME + ' ' + dbo.privat.VORNAME + ', ' + dbo.privat.PLZ + ' ' + dbo.privat.ORT AS Concat_Name
FROM         dbo.recall LEFT OUTER JOIN
                      dbo.privat ON dbo.recall.Nrbehandler = dbo.privat.NRPRIVAT
GO
CREATE VIEW [dbo].[View_Krankenkasse]
AS
SELECT     TOP (100) PERCENT NRFIRMA, NAME1 AS Concat_Name
FROM         dbo.firma
WHERE     (KK = 1)
ORDER BY Concat_Name
GO
CREATE VIEW [dbo].[View_Leistungen_Dentotar]
AS
SELECT        dbo.LEISTUNG.Datum, CASE WHEN dbo.dentotar.leistungnrneu <> '' THEN 222 ELSE 0 END AS Tarif, CASE WHEN len(dbo.LEISTUNG.SSO_Nummer) 
                         < 5 THEN 'P.' + dbo.leistung.sso_nummer ELSE dbo.leistung.sso_nummer END AS Tarifziffer, dbo.LEISTUNG.Bezd AS Tarifziffer_Beschreibung, 0 AS Bezugsziffer, 1 AS SI, 0 AS St, dbo.LEISTUNG.Menge AS Anzahl, 
                         CASE WHEN absolut = 1 THEN frankenprotaxpunkt ELSE dbo.LEISTUNG.Taxpunkte END AS TPAL_Preis, dbo.Behandlu.Nrbehandlung, 1 AS TPW_AL, '' AS TP_TL, '' AS f_TL, '' AS TPW_TL, 1 AS A, 1 AS V, 1 AS P, 
                         CASE WHEN len(leistungnrneu) > 6 OR
                         len(dbo.leistung.sso_nummer) < 5 THEN 1 ELSE 0 END AS M, dbo.LEISTUNG.Total AS Betrag
FROM            dbo.Behandlu INNER JOIN
                         dbo.LEISTUNG ON dbo.Behandlu.Nrbehandlung = dbo.LEISTUNG.Nrbehandlung LEFT OUTER JOIN
                         dbo.DentoTar ON dbo.LEISTUNG.SSO_Nummer = dbo.DentoTar.LeistungNrNeu
WHERE        (dbo.LEISTUNG.aktiv = 1)
GO
ALTER TABLE [dbo].[reportdb] ADD CONSTRAINT [PK_reportdb] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ZJOURNAL] ADD CONSTRAINT [PK_ZJOURNAL] PRIMARY KEY CLUSTERED
	(
		[pk] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ANREDE] ADD CONSTRAINT [PK_ANREDE] PRIMARY KEY CLUSTERED
	(
		[NRANREDE] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ABRTYP] ADD CONSTRAINT [PK_ABRTYP] PRIMARY KEY CLUSTERED
	(
		[Nrabrechnungstyp] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Zahlung] ADD CONSTRAINT [PK_Zahlung] PRIMARY KEY CLUSTERED
	(
		[Nrzahlung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[WORKLOG] ADD CONSTRAINT [PK_WORKLOG] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TARPAKET] ADD CONSTRAINT [PK_TARPAKET] PRIMARY KEY CLUSTERED
	(
		[Nrpaket] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[INDEXES] ADD CONSTRAINT [PK_INDEXES] PRIMARY KEY CLUSTERED
	(
		[NREINTRAG] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TAXPUNKT] ADD CONSTRAINT [PK_TAXPUNKT] PRIMARY KEY CLUSTERED
	(
		[Nrtaxpunkt] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FUNKTION] ADD CONSTRAINT [PK_FUNKTION] PRIMARY KEY CLUSTERED
	(
		[Nrfunktion] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tarifgrp] ADD CONSTRAINT [PK_Tarifgrp] PRIMARY KEY CLUSTERED
	(
		[Nrtarifgruppe] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[LEISTUNG] ADD CONSTRAINT [PK_LEISTUNG] PRIMARY KEY CLUSTERED
	(
		[Nrleistung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TARIF] ADD CONSTRAINT [PK_TARIF] PRIMARY KEY CLUSTERED
	(
		[Nrtarif] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[KONTO] ADD CONSTRAINT [PK_KONTO] PRIMARY KEY CLUSTERED
	(
		[Nrkonto] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TARIFE] ADD CONSTRAINT [PK_TARIFE] PRIMARY KEY CLUSTERED
	(
		[Nrtarifgruppe] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Report] ADD CONSTRAINT [PK_Report] PRIMARY KEY CLUSTERED
	(
		[Nrreport] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FIRMAAP] ADD CONSTRAINT [PK_FIRMAAP] PRIMARY KEY CLUSTERED
	(
		[NRFIRMAAP] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FData] ADD CONSTRAINT [PK_FData] PRIMARY KEY CLUSTERED
	(
		[NrEintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FIRMA] ADD CONSTRAINT [PK_FIRMA] PRIMARY KEY CLUSTERED
	(
		[NRFIRMA] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TITEL] ADD CONSTRAINT [PK_TITEL] PRIMARY KEY CLUSTERED
	(
		[NRTITEL] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FORMFELD] ADD CONSTRAINT [PK_FORMFELD] PRIMARY KEY CLUSTERED
	(
		[Nrfeld] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FORMULAR] ADD CONSTRAINT [PK_FORMULAR] PRIMARY KEY CLUSTERED
	(
		[Nrformular] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FJOURNAL] ADD CONSTRAINT [PK_FJOURNAL] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TEXTE] ADD CONSTRAINT [PK_TEXTE] PRIMARY KEY CLUSTERED
	(
		[Nrtext] ASC,
		[Mandant] ASC,
		[Sprachcode] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PRIVAT] ADD CONSTRAINT [PK_PRIVAT] PRIMARY KEY CLUSTERED
	(
		[NRPRIVAT] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ROLLENP] ADD CONSTRAINT [PK_ROLLENP] PRIMARY KEY CLUSTERED
	(
		[Nrprivat] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PAKETPOS] ADD CONSTRAINT [PK_PAKETPOS] PRIMARY KEY CLUSTERED
	(
		[Nrpaketposition] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[sortlei] ADD CONSTRAINT [PK_sortlei] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PUNKTE] ADD CONSTRAINT [PK_PUNKTE] PRIMARY KEY CLUSTERED
	(
		[Nrtaxpunkte] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[RECHTEXT] ADD CONSTRAINT [PK_RECHTEXT] PRIMARY KEY CLUSTERED
	(
		[Nrrechnungstext] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Reports] ADD CONSTRAINT [PK_Reports] PRIMARY KEY CLUSTERED
	(
		[Nrreportgruppe] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[RGTYP] ADD CONSTRAINT [PK_RGTYP] PRIMARY KEY CLUSTERED
	(
		[Nrrgtyp] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[RECALL] ADD CONSTRAINT [PK_RECALL] PRIMARY KEY CLUSTERED
	(
		[Nrrecall] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TAGPLAN] ADD CONSTRAINT [PK_TAGPLAN] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[MELDUNG] ADD CONSTRAINT [PK_MELDUNG] PRIMARY KEY CLUSTERED
	(
		[Nrmeldung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[LLFILES] ADD CONSTRAINT [PK_LLFILES] PRIMARY KEY CLUSTERED
	(
		[LLKEY] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TARBUCH] ADD CONSTRAINT [PK_TARBUCH] PRIMARY KEY CLUSTERED
	(
		[Eintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[STATUSBH] ADD CONSTRAINT [PK_STATUSBH] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ORTE] ADD CONSTRAINT [PK_ORTE] PRIMARY KEY CLUSTERED
	(
		[OrtNr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SPRACHE] ADD CONSTRAINT [PK_SPRACHE] PRIMARY KEY CLUSTERED
	(
		[Sprachcode] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[OPTIONS] ADD CONSTRAINT [PK_OPTIONS] PRIMARY KEY CLUSTERED
	(
		[NROPTION] ASC,
		[Mandant] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[STATTEXT] ADD CONSTRAINT [PK_STATTEXT] PRIMARY KEY CLUSTERED
	(
		[Nrstatustext] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BESRT] ADD CONSTRAINT [PK_BESRT] PRIMARY KEY CLUSTERED
	(
		[Eintragnr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FAKTPOS] ADD CONSTRAINT [PK_FAKTPOS] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ESTYP] ADD CONSTRAINT [PK_ESTYP] PRIMARY KEY CLUSTERED
	(
		[NRESTYP] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Behandlu] ADD CONSTRAINT [PK_Behandlu] PRIMARY KEY CLUSTERED
	(
		[Nrbehandlung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BERECHT] ADD CONSTRAINT [PK_BERECHT] PRIMARY KEY CLUSTERED
	(
		[Nrberechtigung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BESRI] ADD CONSTRAINT [PK_BESRI] PRIMARY KEY CLUSTERED
	(
		[NrEintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Calendar] ADD CONSTRAINT [PK_Calendar] PRIMARY KEY CLUSTERED
	(
		[ItemID] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TMPRCALL] ADD CONSTRAINT [PK_TMPRCALL] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[DEBITOR] ADD CONSTRAINT [PK_DEBITOR] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BJOURNAL] ADD CONSTRAINT [PK_BJOURNAL] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TMPKONTO] ADD CONSTRAINT [PK_TMPKONTO] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tmppatlab] ADD CONSTRAINT [PK_tmppatlab] PRIMARY KEY CLUSTERED
	(
		[NRPRIVAT] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BCPAGE] ADD CONSTRAINT [PK_BCPAGE] PRIMARY KEY CLUSTERED
	(
		[Nrbcpage] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[anzahlug] ADD CONSTRAINT [PK_anzahlug] PRIMARY KEY CLUSTERED
	(
		[Nranzahlung] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[USER] ADD CONSTRAINT [PK_USER] PRIMARY KEY CLUSTERED
	(
		[Userid] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BARCODE] ADD CONSTRAINT [PK_BARCODE] PRIMARY KEY CLUSTERED
	(
		[Bcnummer] ASC,
		[Datum] ASC,
		[Hh] ASC,
		[Mm] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[VDKonti] ADD CONSTRAINT [PK_VDKonti] PRIMARY KEY CLUSTERED
	(
		[NrPrivat] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[VSKonti] ADD CONSTRAINT [PK_VSKonti] PRIMARY KEY CLUSTERED
	(
		[Nr] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ANSATZ] ADD CONSTRAINT [PK_ANSATZ] PRIMARY KEY CLUSTERED
	(
		[Nransatz] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Faktura] ADD CONSTRAINT [PK_Faktura] PRIMARY KEY CLUSTERED
	(
		[Nrfaktura] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BCART] ADD CONSTRAINT [PK_BCART] PRIMARY KEY CLUSTERED
	(
		[Nrbarcodeart] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TMPSORT] ADD CONSTRAINT [PK_TMPSORT] PRIMARY KEY CLUSTERED
	(
		[Nummer] ASC,
		[Sortfeld1] ASC,
		[Sortfeld2] ASC,
		[Sortfeld3] ASC,
		[Sortfeld4] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BARCODES] ADD CONSTRAINT [PK_BARCODES] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BCFNKT] ADD CONSTRAINT [PK_BCFNKT] PRIMARY KEY CLUSTERED
	(
		[Nrbcfunktion] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TMPUMSA] ADD CONSTRAINT [PK_TMPUMSA] PRIMARY KEY CLUSTERED
	(
		[Nreintrag] ASC
	) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY  = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[rechnung_buchen_for_Delete]
    @type INT,
	@Fakturanr VARCHAR(255),
	@Hauptfaktura VARCHAR(255),
	@rate INT,
	@behandlungsnummer VARCHAR(255)
AS
BEGIN
   DECLARE @nrdebitor INT
   DECLARE @nrhauptfaktura VARCHAR(255)

   DECLARE @nrbehandler INT
   DECLARE @nrgarant INT
   DECLARE @behandlungsbeginn DATETIME
   DECLARE @behandlungsende DATETIME
   DECLARE @nrestyp INT
   DECLARE @anschrift VARCHAR(255)
   DECLARE @faelligam DATETIME
   DECLARE @total FLOAT
   DECLARE @rabatt FLOAT
   DECLARE @vorauszahlungen FLOAT
   DECLARE @ratenzuschlag FLOAT
   DECLARE @fallnr VARCHAR(255)
   DECLARE @mahnbetrag FLOAT
   DECLARE @taxpunktwert FLOAT
   DECLARE @rechnungsbemekrung VARCHAR(255)
   DECLARE @zahlungsbedingungen VARCHAR(255)
   DECLARE @andrucken_taxpunkte BIT
   DECLARE @nrpatient INT
   DECLARE @faelligkeitstage int
   SELECT @nrpatient=Nrpatient,@nrgarant=nrgarant,@nrbehandler=Nrbehandler,@behandlungsbeginn=Behandlungsbeginn,@behandlungsende=Behandlungsende,
          @nrestyp=Nrestyp,@total=Total,@rabatt=Rabatt,@ratenzuschlag=Ratenzuschlag,@fallnr=Fallnummer,@taxpunktwert=Taxpunktwert,
		  @rechnungsbemekrung=Rechnungsbemerkung,@andrucken_taxpunkte=Andrucken_Taxpunkte
   FROM dbo.Behandlu WHERE Nrbehandlung=@behandlungsnummer

  
    SELECT @anschrift=dbo.get_adresse(@behandlungsnummer,1)
	SELECT @faelligkeitstage=inhalt FROM dbo.OPTIONS WHERE NROPTION=2000
	SET @faelligam=DATEADD(dd,@faelligkeitstage,GETDATE())

    INSERT dbo.Faktura
          ( Nrfaktura ,
            Nrbehandler ,
            Nrhauptfaktura ,
            Mandant ,
            Nrbehandlung ,
            Nrdebitor ,
            Debitorsprachcode ,
            Behandlungsbeginn ,
            Behandlungsende ,
            Nransatz ,
            Nrpatient ,
            Patientzeile ,
            Nrestyp ,
            Anschriftz1 ,
            Anschriftz2 ,
            Anschriftz3 ,
            Anschriftz4 ,
            Anschriftz5 ,
            Anschriftz6 ,
            Fallnummer ,
            Datum ,
            Faelligam ,
            Ratennummer ,
            Total ,
            Rabatt ,
            Vorauszahlungen ,
            Ratenzuschlag ,
            Mahnbetrag ,
            Taxpunktwert ,
            Rechnungsbemerkung ,
            Zahlungsbedingungen ,
            Andrucken_taxpunkte ,
            Status ,
            Statusdatum ,
            MAHNSTOP ,
            erstellt_am ,
            mutiert_am ,
            mutierer ,
            aktiv
          )
  VALUES  ( @Fakturanr , -- Nrfaktura - int
            @nrbehandler , -- Nrbehandler - int
            @Hauptfaktura , -- Nrhauptfaktura - int
            1 , -- Mandant - int
            @behandlungsnummer , -- Nrbehandlung - int
            @nrgarant, -- Nrdebitor - int
            'D' , -- Debitorsprachcode - varchar(1)
            @behandlungsbeginn , -- Behandlungsbeginn - date
            @behandlungsende , -- Behandlungsende - date
            0 , -- Nransatz - int
            @nrpatient , -- Nrpatient - int
            '' , -- Patientzeile - varchar(60)
            @nrestyp , -- Nrestyp - int
            '' , -- Anschriftz1 - varchar(40)
            '' , -- Anschriftz2 - varchar(40)
            '' , -- Anschriftz3 - varchar(40)
            '' , -- Anschriftz4 - varchar(40)
            '' , -- Anschriftz5 - varchar(40)
            '' , -- Anschriftz6 - varchar(40)
            '' , -- Fallnummer - varchar(40)
            GETDATE() , -- Datum - date
            @faelligam , -- Faelligam - date
            0 , -- Ratennummer - int
            0.0 , -- Total - float
            @total , -- Rabatt - float
            0.0 , -- Vorauszahlungen - float
            @ratenzuschlag , -- Ratenzuschlag - float
            0.0 , -- Mahnbetrag - float
            @taxpunktwert , -- Taxpunktwert - float
            @rechnungsbemekrung , -- Rechnungsbemerkung - varchar(255)
            @zahlungsbedingungen , -- Zahlungsbedingungen - varchar(255)
            @andrucken_taxpunkte , -- Andrucken_taxpunkte - bit
            1 , -- Status - int
            GETDATE() , -- Statusdatum - date
			0, -- Mahnstop
            GETDATE() , -- erstellt_am - datetime
            GETDATE() , -- mutiert_am - datetime
            1 , -- mutierer - int
            1  -- aktiv - bit
          )
   
          UPDATE behandlu SET status=2,mutiert_am=GETDATE() WHERE Nrbehandlung=@behandlungsnummer
		   
		 
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_fakturierung]
	
	@param1 varchar(255)='',
	@param2 varchar(255)='',
	@param3 varchar(255)=''
AS
BEGIN
	declare @nrfaktura int
	declare @nrbehandlu int
	declare @preview int
	if @param1='' set @nrfaktura=0 else set @nrfaktura=@param1
	if @param2='' set @nrbehandlu=0 else set @nrbehandlu=@param2
	if @param3='' set @preview=0 else set @preview=@param3

	if @nrfaktura=0 begin
	   select top 1 @nrfaktura=nrfaktura from faktura order by nrfaktura desc
	end
	
	select @nrfaktura as NrFaktura,* from view_rechnung where Nrbehandlung=@nrbehandlu
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_copy_behandlung]
	@behandlungsnr VARCHAR(255),
	@typ VARCHAR(255)
AS
BEGIN
   DECLARE @nbhnr int
   SELECT * INTO #tmp FROM behandlu WHERE Nrbehandlung=@behandlungsnr
   SELECT TOP 1 @nbhnr=nrbehandlung+1 FROM dbo.Behandlu ORDER by Nrbehandlung DESC
   UPDATE #tmp SET Nrbehandlung=@nbhnr

   SELECT * into #tmp2 FROM leistung WHERE Nrbehandlung=@behandlungsnr
   UPDATE #tmp2 SET Nrbehandlung=@nbhnr

   DECLARE @lnr INT
   DECLARE @nlnr INT
   
   SELECT TOP 1 @nlnr = nrleistung+1 FROM leistung ORDER BY nrleistung DESC
   DECLARE xc CURSOR FOR SELECT nrleistung FROM #tmp2
   OPEN xc
   FETCH NEXT FROM xc INTO @lnr
   WHILE @@FETCH_STATUS=0 begin
      UPDATE #tmp2 SET Nrleistung=@nlnr WHERE Nrleistung=@lnr
	  SET @nlnr=@nlnr+1
      FETCH NEXT FROM xc INTO @lnr
   END
   CLOSE xc 
   DEALLOCATE xc

   IF @typ=1 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=4 
   END
   IF @typ=2 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=1 
   END
   IF @typ=3 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=1 
   END
   IF @typ=4 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=4 
   END

   INSERT dbo.Behandlu SELECT * FROM #tmp
   insert dbo.leistung SELECT * FROM #tmp2
   DROP TABLE #tmp
   DROP TABLE #tmp2
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[adm_update_tables]
AS
BEGIN
    declare @tbl varchar(255)
    declare xc cursor for
	SELECT sobjects.name
	FROM sysobjects sobjects
	WHERE sobjects.xtype = 'U'
	order by name
	open xc
	fetch next from xc into @tbl
	while @@FETCH_STATUS=0 begin
	   print '-- Starte ' + @tbl + ' ---------------------------'
	   print 'alter table [' + @tbl +'] add erstellt_am datetime'
	   print 'alter table [' + @tbl +'] add mutiert_am datetime'
	   print 'alter table [' + @tbl +'] add mutierer int'
	   print 'alter table [' + @tbl +'] add aktiv bit'
	   print 'go'
	   print 'update [' +@tbl +'] set erstellt_am=getdate(),mutiert_am=getdate(),mutierer=1,aktiv=1'
	   print 'go'
	   print '-- Ende ' + @tbl + ' ---------------------------'
	fetch next from xc into @tbl
	end 
	close xc
	deallocate xc

ALTER TABLE firma ADD GLN VARCHAR(255)
ALTER TABLE privat ADD GLN VARCHAR(255)
ALTER TABLE privat ADD Kanton VARCHAR(255)
ALTER TABLE firma ADD Kanton varchar(255)
ALTER TABLE privat ADD Kantron VARCHAR(255)
UPDATE firma SET kanton=''
UPDATE privat SET kanton=''
ALTER TABLE behandlu ADD Unfalldatum DATETIME
ALTER TABLE behandlu ADD Datum_Kostengutsprache DATETIME
ALTER TABLE behandlu ADD Diagnose VARCHAR(255)
ALTER TABLE behandlu ADD GLN_Liste VARCHAR(255)
ALTER Table behandlu ADD Kostengutsprache_Nummer varchar(255)
UPDATE behandlu SET diagnose=''
UPDATE behandlu SET gln_liste=''
UPDATE behandlu SET kostengutsprache_Nummer=''
UPDATE behandlu SET Datum_Kostengutsprache=null
UPDATE behandlu SET unfalldatum=null

declare @nr int
DECLARE @plz VARCHAR(255)
DECLARE @kt VARCHAR(255)
DECLARE xc CURSOR FOR
   SELECT nrfirma, plz FROM dbo.FIRMA 
OPEN xc
FETCH NEXT FROM xc INTO @nr,@plz
WHILE @@FETCH_STATUS=0 BEGIN
   IF ISNUMERIC(@plz)=1 begin
   SELECT @kt=ISNULL(kt,'') FROM ortsverzeichnis WHERE plz=@plz
   IF @kt<>'' UPDATE firma SET kanton=@kt WHERE nrfirma=@nr
   END
   FETCH NEXT FROM xc INTO @nr,@plz
END
CLOSE xc
DEALLOCATE xc


DECLARE xc CURSOR FOR
   SELECT nrprivat, plz FROM dbo.privat 
OPEN xc
FETCH NEXT FROM xc INTO @nr,@plz
WHILE @@FETCH_STATUS=0 BEGIN
   IF ISNUMERIC(@plz)=1 begin
   SELECT @kt=ISNULL(kt,'') FROM ortsverzeichnis WHERE plz=@plz
   IF @kt<>'' UPDATE privat SET kanton=@kt WHERE nrprivat=@nr
   END
   FETCH NEXT FROM xc INTO @nr,@plz
END
CLOSE xc
DEALLOCATE xc
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[copy_behandlung]
	@behandlugnsnr VARCHAR(255),
	@typ VARCHAR(255)
AS
BEGIN
   DECLARE @nbhnr int
   SELECT * INTO #tmp FROM behandlu WHERE Nrbehandlung=@behandlugnsnr
   SELECT TOP 1 @nbhnr=nrbehandlung+1 FROM dbo.Behandlu ORDER by Nrbehandlung DESC
   
   SELECT * into #tmp2 FROM leistung WHERE Nrbehandlung=@behandlugnsnr
   UPDATE #tmp2 SET Nrbehandlung=@nbhnr

   DECLARE @lnr INT
   DECLARE @nlnr INT
   
   DECLARE xc CURSOR FOR SELECT nrleistung FROM #tmp2
   OPEN xc
   FETCH NEXT FROM xc INTO @lnr
   WHILE @@FETCH_STATUS=0 begin
      SELECT TOP 1 @nlnr = nrleistung+1 FROM leistung ORDER BY nrleistung DESC
      UPDATE #tmp2 SET Nrleistung=@nlnr WHERE Nrleistung=@lnr
      FETCH NEXT FROM xc INTO @lnr
   END
   CLOSE xc 
   DEALLOCATE xc

   IF @typ=1 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=4 
   END
   IF @typ=2 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=1 
   END
   IF @typ=3 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=1 
   END
   IF @typ=4 begin
      UPDATE #tmp SET erstellt_am=GETDATE(),mutiert_am=GETDATE(),[status]=4 
   END



   

	   

  


END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_verbuchen_for_Delete] 
	-- Add the parameters for the stored procedure here
    @behandlungsnr VARCHAR(255) ,
    @ratenr INT ,
    @nrhauptfaktura INT ,
    @idatum VARCHAR(255),
	@betrag float
AS
    BEGIN
        DECLARE @datum DATETIME;
        IF @idatum = ''
            SET @datum = GETDATE();
        ELSE
            SET @datum = @idatum;
        DECLARE @nrfaktura INT; 
        SET @nrfaktura = dbo.get_FakturaNr();

        DECLARE @nrpatient INT;
        DECLARE @nrgarant INT;
        DECLARE @nrbehandler INT;
        DECLARE @mandant INT;
        DECLARE @behandlungsbegin DATETIME;
        DECLARE @behandlungsende DATETIME;
        DECLARE @total FLOAT;
        DECLARE @rabatt FLOAT;
        DECLARE @vorauszahlung FLOAT;
        DECLARE @ratenzuschlag FLOAT;
        DECLARE @tpwert FLOAT;
        SELECT  @vorauszahlung = ISNULL(SUM(Betrag), 0)
        FROM    dbo.Zahlung
        WHERE   ( CAST(aktiv AS INT) = 1 )
                AND ( CAST(Vorauszahlung AS INT) = 1 )
                AND ( Nrbehandlung = 123 );
        SELECT  @nrpatient = Nrpatient ,
                @nrgarant = Nrgarant ,
                @nrbehandler = Nrbehandler ,
                @mandant = Mandant ,
                @behandlungsbegin = Behandlungsbeginn ,
                @behandlungsende = Behandlungsende ,
                @total = Total ,
                @rabatt = Rabatt ,
                @ratenzuschlag = Ratenzuschlag ,
                @tpwert = Taxpunktwert
        FROM    dbo.Behandlu
        WHERE   Nrbehandlung = @behandlungsnr;
        IF @nrhauptfaktura = 0
            SET @nrhauptfaktura = @nrfaktura;
        IF @nrgarant = 0
            SET @nrgarant = @nrpatient;
        IF @nrhauptfaktura <> @nrfaktura
            BEGIN
                SET @rabatt = 0;
                SET @ratenzuschlag = 0;
                SET @vorauszahlung = 0;
            END;
           
        INSERT  dbo.Faktura
                ( Nrfaktura ,
                  Nrbehandler ,
                  Nrhauptfaktura ,
                  Mandant ,
                  Nrbehandlung ,
                  Nrdebitor ,
                  Debitorsprachcode ,
                  Behandlungsbeginn ,
                  Behandlungsende ,
                  Nransatz ,
                  Nrpatient ,
                  Patientzeile ,
                  Nrestyp ,
                  Anschriftz1 ,
                  Anschriftz2 ,
                  Anschriftz3 ,
                  Anschriftz4 ,
                  Anschriftz5 ,
                  Anschriftz6 ,
                  Fallnummer ,
                  Datum ,
                  Faelligam ,
                  Ratennummer ,
                  Total ,
                  Rabatt ,
                  Vorauszahlungen ,
                  Ratenzuschlag ,
                  Mahnbetrag ,
                  Taxpunktwert ,
                  Rechnungsbemerkung ,
                  Zahlungsbedingungen ,
                  Andrucken_taxpunkte ,
                  Status ,
                  Statusdatum ,
                  Mahndatum1 ,
                  Mahnfaelligkeit1 ,
                  Mahngebuehr1 ,
                  Mahndatum2 ,
                  Mahnfaelligkeit2 ,
                  Mahngebuehr2 ,
                  Mahndatum3 ,
                  Mahnfaelligkeit3 ,
                  Mahngebuehr3 ,
                  MAHNSTOP ,
                  erstellt_am ,
                  mutiert_am ,
                  mutierer ,
                  aktiv
	            )
        VALUES  ( @nrfaktura , -- Nrfaktura - int
                  @nrbehandler , -- Nrbehandler - int
                  @nrhauptfaktura , -- Nrhauptfaktura - int
                  @mandant , -- Mandant - int
                  @behandlungsnr , -- Nrbehandlung - int
                  @nrgarant , -- Nrdebitor - int
                  '' , -- Debitorsprachcode - varchar(1)
                  @behandlungsbegin , -- Behandlungsbeginn - date
                  @behandlungsende , -- Behandlungsende - date
                  0 , -- Nransatz - int
                  @nrpatient , -- Nrpatient - int
                  '' , -- Patientzeile - varchar(60)
                  0 , -- Nrestyp - int
                  '' , -- Anschriftz1 - varchar(40)
                  '' , -- Anschriftz2 - varchar(40)
                  '' , -- Anschriftz3 - varchar(40)
                  '' , -- Anschriftz4 - varchar(40)
                  '' , -- Anschriftz5 - varchar(40)
                  '' , -- Anschriftz6 - varchar(40)
                  '' , -- Fallnummer - varchar(40)
                  GETDATE() , -- Datum - date
                  DATEADD(DAY, 30, @datum) , -- Faelligam - date
                  @ratenr , -- Ratennummer - int
                  @betrag , -- Total - float
                  @rabatt , -- Rabatt - float
                  @vorauszahlung , -- Vorauszahlungen - float
                  @ratenzuschlag , -- Ratenzuschlag - float
                  0.0 , -- Mahnbetrag - float
                  @tpwert , -- Taxpunktwert - float
                  '' , -- Rechnungsbemerkung - varchar(255)
                  '' , -- Zahlungsbedingungen - varchar(255)
                  NULL , -- Andrucken_taxpunkte - bit
                  0 , -- Status - int
                  NULL , -- Statusdatum - date
                  NULL , -- Mahndatum1 - date
                  NULL , -- Mahnfaelligkeit1 - date
                  0.0 , -- Mahngebuehr1 - float
                  NULL , -- Mahndatum2 - date
                  NULL , -- Mahnfaelligkeit2 - date
                  0.0 , -- Mahngebuehr2 - float
                  NULL , -- Mahndatum3 - date
                  NULL , -- Mahnfaelligkeit3 - date
                  0.0 , -- Mahngebuehr3 - float
                  NULL , -- MAHNSTOP - bit
                  GETDATE() , -- erstellt_am - datetime
                  GETDATE() , -- mutiert_am - datetime
                  1 , -- mutierer - int
                  1  -- aktiv - bit
	            );

        DECLARE @nreintrag INT;
        SELECT TOP 1
                @nreintrag = Nreintrag + 1
        FROM    dbo.DEBITOR
        ORDER BY Nreintrag DESC;
        INSERT  dbo.DEBITOR
                ( Nreintrag ,
                  Mandant ,
                  Nrbehandler ,
                  Nrdebitor ,
                  Nrfaktura ,
                  Nrpatient ,
                  Betrag ,
                  Faelligam ,
                  Status ,
                  Statusdatum ,
                  erstellt_am ,
                  mutiert_am ,
                  mutierer ,
                  aktiv
				 )
        VALUES  ( @nreintrag , -- Nreintrag - int
                  @mandant , -- Mandant - int
                  @nrbehandler , -- Nrbehandler - int
                  @nrgarant , -- Nrdebitor - int
                  @nrfaktura , -- Nrfaktura - int
                  @nrpatient , -- Nrpatient - int
                  @total , -- Betrag - float
                  GETDATE() , -- Faelligam - date
                  0 , -- Status - int
                  GETDATE() , -- Statusdatum - date
                  GETDATE() , -- erstellt_am - datetime
                  GETDATE() , -- mutiert_am - datetime
                  1 , -- mutierer - int
                  1  -- aktiv - bit
				 );

        SELECT TOP 1
                @nreintrag = Nreintrag + 1
        FROM    dbo.FJOURNAL
        ORDER BY Nreintrag DESC;

        INSERT  dbo.FJOURNAL
                ( Nreintrag ,
                  Mandant ,
                  Datum ,
                  Nrfaktura ,
                  Nrdebitor ,
                  Debitor ,
                  Patient ,
                  Betrag ,
                  Storno ,
                  erstellt_am ,
                  mutiert_am ,
                  mutierer ,
                  aktiv
						
                )
        VALUES  ( @nreintrag , -- Nreintrag - int
                  @mandant , -- Mandant - int
                  GETDATE() , -- Datum - date
                  @nrfaktura , -- Nrfaktura - int
                  @nrgarant , -- Nrdebitor - int
                  '' , -- Debitor - varchar(50)
                  '' , -- Patient - varchar(50)
                  @total , -- Betrag - float
                  '' , -- Storno - varchar(1)
                  GETDATE() , -- erstellt_am - datetime
                  GETDATE() , -- mutiert_am - datetime
                  1 , -- mutierer - int
                  1  -- aktiv - bit
						
                );

        IF @nrfaktura = @nrhauptfaktura
            BEGIN
                UPDATE  dbo.Behandlu
                SET     Status = 3
                WHERE   Nrbehandlung = Nrbehandlung;

            END;
    END;
GO
CREATE PROCEDURE [dbo].[sp_update_spalten]
	@tablename varchar(255)
AS
begin
	declare @col varchar(30)
	declare @dbkey int
	declare @ierrorcode int
	declare @spaltenkey INT
    DECLARE @i INT
    SET @i=0
	declare xcursor cursor for
		SELECT column_name
		from	INFORMATION_SCHEMA.Columns
		where	table_name = @tablename
	open xcursor
	FETCH NEXT FROM xcursor into @col
	WHILE @@FETCH_STATUS = 0 BEGIN
		set @spaltenkey=@dbkey
		insert into spalten (tabelle,tabellenspalte,spalte,breite,Reihenfolge,readonly,aktiv,erstellt_am, mutiert_am,mutierer,alsHacken,TipText,Nummer_Format) values(@tablename,@col,@col,50,@i,0,1,getdate(),getdate(),-1,0,'','')
		SET @i=@i+1
	FETCH NEXT FROM xcursor into @col
	end   
	close xcursor
	deallocate xcursor
end
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_zahlung_storno]
   @zahlungnr int
AS
BEGIN
     UPDATE zahlung SET aktiv=1,status=9, statusdatum=GETDATE(),mutiert_am=GETDATE(),mutierer=1 WHERE Nrzahlung=@zahlungnr
   DECLARE @fakturanr VARCHAR(255)
   declare @betrag float
   SELECT @fakturanr=ISNULL(nrfaktura,''),@betrag=Betrag FROM zahlung WHERE nrzahlung=@zahlungnr
   IF @fakturanr<>'' BEGIN
      UPDATE debitor SET betrag=betrag+@betrag WHERE Nrfaktura=@fakturanr
	  SELECT @betrag=betrag FROM debitor WHERE Nrfaktura=@fakturanr
	  IF @betrag>0 begin
         UPDATE debitor SET status=0, statusdatum=GETDATE(),mutiert_am=GETDATE() WHERE Nrfaktura=@fakturanr
	     
	  end
   end
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_verrechnungsstatus]
    @fnkt INT ,
    @behandlungsnr VARCHAR(255)
AS
    BEGIN
        IF @fnkt = 1
            BEGIN
                DECLARE @nrfaktura INT;
                DECLARE @fakturiert INT;
                DECLARE @zahlungen INT;
				DECLARE @Hauptfaktura INT;
				SELECT  @nrfaktura = ISNULL(Nrfaktura, -1), @Hauptfaktura=ISNULL(Nrhauptfaktura,-1)
                FROM    Faktura
                WHERE   Nrbehandlung = @behandlungsnr
                        AND aktiv = 1
                        AND Status <> 9;
                SELECT  @fakturiert = COUNT(*)
                FROM    Faktura
                WHERE   Nrbehandlung = @behandlungsnr
                        AND aktiv = 1
                        AND Status <> 9;
                SELECT  @zahlungen = COUNT(*)
                FROM    Zahlung
                WHERE   (Nrfaktura = @nrfaktura OR Nrfaktura=@Hauptfaktura)
                        AND aktiv = 1
                        AND Status <> 9;

                SELECT  @fakturiert AS fakturiert ,
                        @zahlungen AS Zahlungen;
            END;
        IF @fnkt = 2
            BEGIN
			    DECLARE xc CURSOR FOR
				   SELECT nrfaktura FROM faktura WHERE Nrbehandlung=@behandlungsnr
				OPEN xc
				FETCH NEXT FROM xc into @nrfaktura
				WHILE @@FETCH_STATUS=0 BEGIN
                
                   UPDATE faktura SET status=9, aktiv=0, mutiert_am=GETDATE(),mutierer=1 WHERE Nrfaktura=@nrfaktura
	   			   UPDATE debitor SET status=9, aktiv=0, mutiert_am=GETDATE(),mutierer=1 WHERE Nrfaktura=@nrfaktura
				   FETCH NEXT FROM xc into @nrfaktura
                END
                CLOSE xc
				DEALLOCATE xc
				
            END;
    
    END;
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_update_dentotar]
AS
BEGIN
	SET NOCOUNT ON;
    DECLARE @datum varchar(255)
	SET @datum=LTRIM(RTRIM(YEAR(GETDATE())))+LTRIM(RTRIM(MONTH(GETDATE())))+LTRIM(RTRIM(DAY(GETDATE())))
	SET @datum=@datum+LTRIM(RTRIM(DATEPART(HOUR,GETDATE())))+LTRIM(RTRIM(DATEPART(MINUTE,GETDATE())))+LTRIM(RTRIM(DATEPART(SECOND,GETDATE())))
	PRINT @datum

	DECLARE @xsql VARCHAR(255)
	SET @xsql='Select * into Dentotar_'+@datum+' from dentotar'
	EXEC(@xsql)
	TRUNCATE TABLE dbo.DentoTar
END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_recalc_leistungen]
@nrbehandlung INT,
@taxpunktwert FLOAT
as
BEGIN
   UPDATE leistung SET Frankenprotaxpunkt=@taxpunktwert WHERE Absolut=0 AND Nrbehandlung=@nrbehandlung
   UPDATE leistung SET Total=dbo.RoundToFiveOrZero(menge*Taxpunkte*Frankenprotaxpunkt) WHERE absolut=0 AND Nrbehandlung=@nrbehandlung

END
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_get_fakturadata]
    @behandlungsnr INT ,
    @typ INT ,
    @betrag FLOAT = 0 ,
    @rate INT = 0,
	@verbuchen INT = 0,
	@kopie INT = 0,
	@ifakturanr INT = 0
AS
    BEGIN
	    --INSERT __log (logeintrag) VALUES(@kopie)
	    DECLARE @rgdatum DATETIME
		SET @rgdatum=GETDATE()
		IF @kopie = 1 BEGIN
           SELECT @rgdatum=erstellt_am FROM faktura WHERE Nrfaktura=@ifakturanr
		end
        DECLARE @type INT;
        DECLARE @Tabelle_ohne_datum TABLE
            (
              [Typ] [INT] NULL ,
              [SSO_Nummer] [VARCHAR](255) NULL ,
              [Bezd] [VARCHAR](255) NULL ,
              [Taxpunkte] [FLOAT] NULL ,
              [Menge] [FLOAT] NULL ,
              [Frankenprotaxpunkt] [FLOAT] NULL ,
              [Absolut] [INT] NULL ,
              [Leistungstotal] [FLOAT] NULL ,
              [NrBehandlung] INT
            );
        DECLARE @Tabelle_mit_datum TABLE
            (
              [Typ] [INT] NULL ,
              [Datum] [DATE] NULL ,
              [SSO_Nummer] [VARCHAR](255) NULL ,
              [Bezd] [VARCHAR](255) NULL ,
              [Taxpunkte] [FLOAT] NULL ,
              [Menge] [FLOAT] NULL ,
              [Frankenprotaxpunkt] [FLOAT] NULL ,
              [Absolut] [BIT] NULL ,
              [Leistungstotal] [FLOAT] NULL ,
              [nrbehandlu] INT
            );
        DECLARE @lr_tabelle_mit_Datum TABLE
            (
              pk INT NOT NULL
                     IDENTITY(1, 1) ,
              [L_Datum] [DATE] NULL ,
              [L_SSO_Nummer] [VARCHAR](255) NULL ,
              [L_Bezd] [VARCHAR](255) NULL ,
              [L_Taxpunkte] [FLOAT] NULL ,
              [L_Menge] [FLOAT] NULL ,
              [L_Frankenprotaxpunkt] [FLOAT] NULL ,
              [L_Absolut] [BIT] NULL ,
              [L_Leistungstotal] [FLOAT] NULL ,
              [R_Datum] [DATE] NULL ,
              [R_SSO_Nummer] [VARCHAR](255) NULL ,
              [R_Bezd] [VARCHAR](255) NULL ,
              [R_Taxpunkte] [FLOAT] NULL ,
              [R_Menge] [FLOAT] NULL ,
              [R_Frankenprotaxpunkt] [FLOAT] NULL ,
              [R_Absolut] [BIT] NULL ,
              [R_Leistungstotal] [FLOAT] NULL ,
              PRIMARY KEY ( pk )
            );

        DECLARE @lr_tabelle_dentotar TABLE
            (
              pk INT NOT NULL
                     IDENTITY(1, 1) ,
              datum DATE NULL ,
              tarif VARCHAR(255) ,
              tarifziffer VARCHAR(255) NULL ,
              tariftext VARCHAR(255) NULL ,
              bezugsziffer VARCHAR(255) NULL ,
              si VARCHAR(255) NULL ,
              st VARCHAR(255) NULL ,
              anzahl INT NULL ,
              tpal_preis DECIMAL(10, 2) NULL ,
              f_al DECIMAL(10, 2) NULL ,
              tpw_al DECIMAL(10, 2) NULL ,
              f_tl DECIMAL(10, 2) NULL ,
              tpw_tl DECIMAL(10, 2) NULL ,
              a INT NULL ,
              v INT NULL ,
              p INT NULL ,
              m INT NULL ,
              betrag DECIMAL(10, 2) NULL PRIMARY KEY ( pk )
            );


        DECLARE @lr_tabelle_ohne_Datum TABLE
            (
              pk INT NOT NULL
                     IDENTITY(1, 1) ,
              [L_SSO_Nummer] [VARCHAR](255) NULL ,
              [L_Bezd] [VARCHAR](255) NULL ,
              [L_Taxpunkte] [FLOAT] NULL ,
              [L_Menge] [FLOAT] NULL ,
              [L_Frankenprotaxpunkt] [FLOAT] NULL ,
              [L_Absolut] [BIT] NULL ,
              [L_Leistungstotal] [FLOAT] NULL ,
              [R_SSO_Nummer] [VARCHAR](255) NULL ,
              [R_Bezd] [VARCHAR](255) NULL ,
              [R_Taxpunkte] [FLOAT] NULL ,
              [R_Menge] [FLOAT] NULL ,
              [R_Frankenprotaxpunkt] [FLOAT] NULL ,
              [R_Absolut] [BIT] NULL ,
              [R_Leistungstotal] [FLOAT] NULL ,
              PRIMARY KEY ( pk )
            );

        DECLARE @Nrbehandlung AS INT ,
            @Behandlungsbeginn AS DATE ,
            @Behandlungsende AS DATE ,
            @Patient AS VARCHAR(51) ,
            @Behandler AS VARCHAR(51) ,
            @Taxpunktwert AS FLOAT ,
            @Rabatt AS FLOAT ,
            @Total AS FLOAT ,
            @Rechnungsbemerkung AS VARCHAR(255) ,
            @Ratenzuschlag AS FLOAT ,
            @Andrucken_Taxpunkte AS BIT ,
            @Anzahlung AS FLOAT ,
            @ESTyp INT ,
            @Nrleistung AS INT ,
            @Datum AS DATE ,
            @SSO_Nummer AS VARCHAR(255) ,
            @Taxpunkte AS FLOAT ,
            @Menge AS FLOAT ,
            @Frankenprotaxpunkt AS FLOAT ,
            @Absolut AS BIT ,
            @Leistungstotal AS FLOAT ,
            @Bezd AS VARCHAR(255) ,
            @@AdressZeile1 AS VARCHAR(255) ,
            @AdressZeile2 AS VARCHAR(255) ,
            @AdressZeile3 AS VARCHAR(255) ,
            @AdressZeile4 AS VARCHAR(255) ,
            @AdressZeile5 AS VARCHAR(255) ,
            @Total_SSO AS FLOAT ,
            @Total_Medikamente AS FLOAT ,
            @Total_Labor AS FLOAT ,
            @RabattTotal AS FLOAT ,
            @Vorauszahlung AS FLOAT ,
            @TotalTextblock AS VARCHAR(1024) ,
            @TotalNumBlock AS VARCHAR(1024) ,
            @Anzahlraten INT ,
            @Ungeraterateamstart INT ,
            @i INT ,
            @franken VARCHAR(255) ,
            @rappen VARCHAR(255) ,
            @totalstring VARCHAR(255) ,
            @faelligkeit DATETIME ,
            @hauptfaktura VARCHAR(255);
			

        DECLARE @esdaten TABLE
            (
              [Vs_konto] [VARCHAR](20) NULL ,
              [Vs_belegart] [VARCHAR](2) NULL ,
              [Vs_teilnehmernummer] [VARCHAR](20) NULL ,
              [Vs_besrid] [VARCHAR](20) NULL ,
              [Bankzeile1] [VARCHAR](50) NULL ,
              [Bankzeile2] [VARCHAR](50) NULL ,
              [Bankzeile3] [VARCHAR](50) NULL ,
              [ZuGunstenVon1] [VARCHAR](50) NULL ,
              [ZuGunstenVon2] [VARCHAR](50) NULL ,
              [ZuGunstenVon3] [VARCHAR](50) NULL ,
              [ZuGunstenVon4] [VARCHAR](50) NULL ,
              [Total_Franken] FLOAT ,
              [Nur_Franken] INT ,
              [Nur_Rappen] INT ,
              Referenzzeile1 VARCHAR(255) ,
              referenzzeile2 VARCHAR(255) ,
              fakturanr VARCHAR(255) ,
              rate INT ,
              zahlungsfrist INT ,
              faelligkeit DATETIME ,
              hauptfaktura VARCHAR(255)
            );

        IF @typ = 1
            BEGIN-- Kontoinformationen / ES_Daten
                DECLARE @manr INT;
                DECLARE @totalbetrag FLOAT;
                DECLARE @rc INT;
                DECLARE @FAKTURANR INT;
                DECLARE @vz FLOAT;
                SELECT  @vz = dbo.Get_Totale(5, @behandlungsnr);
                SET @vz = 0;
                SET @rc = 0;
				IF @kopie=1 Or @ifakturanr<>0 BEGIN
                   SET @fakturanr=@ifakturanr
				END ELSE begin
                   SET @FAKTURANR = dbo.get_FakturaNr();
				end
                IF @betrag <> 0
                    SET @totalbetrag = @betrag;
                SELECT  @manr = Nrbehandler ,
                        @totalbetrag = Total ,
                        @Anzahlraten = Anzahlraten ,
                        @Ungeraterateamstart = Ungeraterateamstart
                FROM    Behandlu
                WHERE   Nrbehandlung = @behandlungsnr;
                SET @totalbetrag = dbo.GetRoundedValue(@totalbetrag - @vz);
                SELECT  @rc = COUNT(*)
                FROM    dbo.VDKonti
                WHERE   NrPrivat = @manr;
                PRINT @totalbetrag;
                IF @rc = 0
                    SET @manr = 1;
                PRINT FLOOR(@totalbetrag); 
                PRINT ( @totalbetrag - FLOOR(@totalbetrag) ) * 100;


                DECLARE @temp FLOAT;
                DECLARE @temp1 FLOAT;
                DECLARE @temp2 FLOAT;
                DECLARE @temp3 VARCHAR(255);
                DECLARE @tage_zahlungsfrist INT;
                SELECT  @tage_zahlungsfrist = INHALT
                FROM    dbo.OPTIONS
                WHERE   NROPTION = 2000;

                IF @Anzahlraten > 1
                    BEGIN
                        SET @temp = FLOOR(FLOOR(@totalbetrag) / @Anzahlraten); -- Rate ohne Nachkommastellen
                        SET @temp1 = @temp * @Anzahlraten; 
                        SET @temp2 = @temp + ( @totalbetrag - @temp1 ); -- ungerate rate
                        IF @rate <= 1
                            BEGIN
                                IF @Ungeraterateamstart = 1
                                    BEGIN
                                        SET @totalbetrag = @temp2;
                                    END;
                                ELSE
                                    BEGIN
                                        SET @totalbetrag = @temp;
                                    END;	
                                SET @hauptfaktura = @FAKTURANR;
                                SET @faelligkeit = DATEADD(dd,
                                                           @tage_zahlungsfrist,
                                                           GETDATE());
                                SET @faelligkeit = DATEADD(dd,
                                                           @tage_zahlungsfrist,
                                                           @rgdatum);

                            END;
                        ELSE
                            BEGIN
                                IF @Ungeraterateamstart = 1
                                    AND @rate = @Anzahlraten
                                    BEGIN
                                        SET @totalbetrag = @temp2;
                                    END;
                                ELSE
                                    BEGIN
                                        SET @totalbetrag = @temp;
                                    END;
                                SELECT TOP 1
                                        @hauptfaktura = Nrhauptfaktura
                                FROM    Faktura
                                ORDER BY Nrfaktura DESC;
                                SET @faelligkeit = DATEADD(dd,
                                                           @tage_zahlungsfrist
                                                           * @rate, GETDATE());
                                SET @faelligkeit = DATEADD(dd,
                                                           @tage_zahlungsfrist
                                                           * @rate, @rgdatum);

                            END;

                    END;
                ELSE
                    BEGIN
                        SET @hauptfaktura = @FAKTURANR;
                        SET @faelligkeit = DATEADD(dd, @tage_zahlungsfrist,
                                                   GETDATE());
                        SET @faelligkeit = DATEADD(dd, @tage_zahlungsfrist,
                                                   @rgdatum);

                    END;
                SET @totalstring = CONVERT(NUMERIC(10, 2), @totalbetrag);
                SET @franken = SUBSTRING(@totalstring, 1,
                                         CHARINDEX('.', @totalstring) - 1);
                SET @rappen = SUBSTRING(@totalstring,
                                        CHARINDEX('.', @totalstring) + 1, 2);
			    WHILE LEN(@rappen)<2 BEGIN
                   SET @rappen='0'+LTRIM(RTRIM(STR(@rappen)))
				end
				   
				               

                PRINT @franken;
                PRINT @rappen;
                PRINT @totalstring;
                INSERT  @esdaten
                        SELECT  Vs_konto ,
                                Vs_belegart ,
                                Vs_teilnehmernummer ,
                                Vs_besrid ,
                                Bankzeile1 ,
                                Bankzeile2 ,
                                Bankzeile3 ,
                                ZuGunstenVon1 ,
                                ZuGunstenVon2 ,
                                ZuGunstenVon3 ,
                                ZuGunstenVon4 ,
                                @totalbetrag ,
                                @franken ,--FLOOR(@totalbetrag) ,
                                @rappen ,--( @totalbetrag - FLOOR(@totalbetrag) ) * 100 ,
                                '                                            ' ,
                                '                                            ' ,
                                @FAKTURANR ,
                                @rate ,
                                @tage_zahlungsfrist ,
                                @faelligkeit ,
                                @hauptfaktura
                        FROM    dbo.VDKonti
                        WHERE   NrPrivat = @manr;

						


               SELECT  *
                FROM    @esdaten;


            END;

        DECLARE @mitdatum INT;
        SELECT  @mitdatum = MitDatum
        FROM    Behandlu
        WHERE   Nrbehandlung = @behandlungsnr;


        IF @typ = 2
            BEGIN -- Empfängerdaten / Allg Behandlung / Totale
			    DECLARE @absenderort AS VARCHAR(255)
				SELECT TOP 1  @absenderort = absenderort FROM absender
				SET @absenderort=@absenderort + ', '+CONVERT(VARCHAR(10),@rgdatum,104)
                SELECT  1 AS Typ ,
				        @rgdatum AS Rechnungsdatum,
                        @mitdatum AS MitDatum ,
                        dbo.Behandlu.Nrbehandlung ,
                        dbo.Behandlu.Behandlungsbeginn ,
                        dbo.Behandlu.Behandlungsende ,
                        dbo.PRIVAT.NAME + ' ' + dbo.PRIVAT.VORNAME AS Patient ,
                        dbo.PRIVAT.NAME AS Patient_Name ,
                        dbo.PRIVAT.VORNAME AS Patient_Vorname ,
                        dbo.PRIVAT.STRASSE AS Patient_Strasse ,
                        dbo.PRIVAT.PLZ AS Patient_PLZ ,
                        dbo.PRIVAT.ORT AS Patient_Ort ,
                        dbo.PRIVAT.Kanton AS Patient_Kanton ,
                        dbo.PRIVAT.AHVNR AS Patient_AHVNr ,
                        CASE WHEN dbo.PRIVAT.GESCHLECHT = 0 THEN 'M'
                             ELSE 'W'
                        END AS Patient_Geschlecht ,
                        CONVERT(VARCHAR(10),dbo.PRIVAT.GEBDAT,104) AS Patient_Geburtsdatum ,
                        dbo.PRIVAT.GLN AS Patient_GLNr ,
                        privat_1.NAME + ' ' + privat_1.VORNAME AS Behandler ,
                        dbo.Behandlu.Taxpunktwert ,
                        dbo.Behandlu.Rabatt ,
                        dbo.Behandlu.Total ,
                        dbo.Behandlu.Rechnungsbemerkung ,
                        dbo.Behandlu.Ratenzuschlag ,
                        dbo.Behandlu.Andrucken_Taxpunkte ,
                        dbo.Behandlu.Anzahlung ,
                        dbo.Behandlu.Nrestyp ,
                        dbo.Behandlu.Fallnummer ,
                        dbo.Behandlu.Anzahlraten ,
						dbo.Behandlu.Unfalldatum,
						dbo.behandlu.Datum_Kostengutsprache,
						dbo.behandlu.Kostengutsprache_Nummer,
						dbo.behandlu.Behandlungsgrund,
						dbo.behandlu.Diagnose,
						dbo.behandlu.GLN_Liste,
                        dbo.get_adresszeile(1, dbo.Behandlu.Nrbehandlung) AS AdressZeile1 ,
                        dbo.get_adresszeile(2, dbo.Behandlu.Nrbehandlung) AS AdressZeile2 ,
                        dbo.get_adresszeile(3, dbo.Behandlu.Nrbehandlung) AS AdressZeile3 ,
                        dbo.get_adresszeile(4, dbo.Behandlu.Nrbehandlung) AS AdressZeile4 ,
                        dbo.get_adresszeile(5, dbo.Behandlu.Nrbehandlung) AS AdressZeile5 ,
                        dbo.Get_Dentotar_Parameter(11,
                                                   dbo.Behandlu.Nrbehandlung) AS GLN_Empfaenger ,
                        dbo.get_adresse(dbo.Behandlu.Nrbehandlung, 0) AS Empfaenger ,
                        dbo.Get_Totale(1, dbo.Behandlu.Nrbehandlung) AS Total_SSO ,
                        dbo.Get_Totale(2, dbo.Behandlu.Nrbehandlung) AS Total_Medikamente ,
                        dbo.Get_Totale(3, dbo.Behandlu.Nrbehandlung) AS Total_Labor ,
                        dbo.Get_Totale(4, dbo.Behandlu.Nrbehandlung) AS RabattTotal ,
                        dbo.Get_Totale(5, dbo.Behandlu.Nrbehandlung) AS Vorauszahlung ,
                        dbo.get_totalblock(1, dbo.Behandlu.Nrbehandlung) AS TotalTextblock ,
                        dbo.get_totalblock(2, dbo.Behandlu.Nrbehandlung) AS TotalNumBlock ,
                        dbo.Get_Dentotar_Parameter(1,
                                                   dbo.Behandlu.Nrbehandlung) AS Rechnungstitel ,
                        dbo.Get_Dentotar_Parameter(2,
                                                   dbo.Behandlu.Nrbehandlung) AS Dokument_Identifikation ,
                        dbo.Get_Dentotar_Parameter(3,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_GLN ,
                        dbo.Get_Dentotar_Parameter(4,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_ZSR ,
                        dbo.Get_Dentotar_Parameter(5,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_Name ,
                        dbo.Get_Dentotar_Parameter(6,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_Adresse ,
                        dbo.Get_Dentotar_Parameter(15,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_Tel ,
                        dbo.Get_Dentotar_Parameter(16,
                                                   dbo.Behandlu.Nrbehandlung) AS RG_Steller_Fax ,
						(SELECT ISNULL(GLN,'') FROM privat WHERE nrprivat=behandlu.Nrbehandler) AS Leistungserbringer_GSN,
						(SELECT ISNULL(ZSR,'') FROM privat WHERE nrprivat=behandlu.Nrbehandler) AS Leistungserbringer_ZSR,
						(SELECT name+' '+vorname FROM privat WHERE nrprivat=behandlu.Nrbehandler) AS Leistungserbringer_Name,
                        --dbo.Get_Dentotar_Parameter(7,
                        --                           dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_GSN ,
                        --dbo.Get_Dentotar_Parameter(8,
                        --                           dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_ZSR ,
                        --dbo.Get_Dentotar_Parameter(9,
                        --                           dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_Name ,
                        dbo.Get_Dentotar_Parameter(10,
                                                   dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_Adresse ,
                        dbo.Get_Dentotar_Parameter(12,
                                                   dbo.Behandlu.Nrbehandlung) AS Verguetungsart ,
                        --dbo.Get_Dentotar_Parameter(13,
                        --                           dbo.Behandlu.Nrbehandlung) AS Gesetz ,
						dbo.get_gesetz(dbo.Behandlu.Nrbehandlung) AS Gesetz,
                        dbo.Get_Dentotar_Parameter(14,
                                                   dbo.Behandlu.Nrbehandlung) AS Rolle_Ort ,
                        dbo.Get_Dentotar_Parameter(17,
                                                   dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_Tel ,
                        dbo.Get_Dentotar_Parameter(18,
                                                   dbo.Behandlu.Nrbehandlung) AS Leistungserbringer_Fax,
						CASE WHEN @kopie=0 THEN 'nein' ELSE 'ja' END AS Kopie,
						@absenderort AS Absenderort_Datum
                INTO    #tmp1
                FROM    dbo.Behandlu
                        INNER JOIN dbo.PRIVAT ON dbo.Behandlu.Nrpatient = dbo.PRIVAT.NRPRIVAT
                        INNER JOIN dbo.PRIVAT AS privat_1 ON dbo.Behandlu.Nrbehandler = privat_1.NRPRIVAT
						
                WHERE   Behandlu.Nrbehandlung = @behandlungsnr;
                        
                SELECT  *
                FROM    #tmp1;
            END;
        
       
        IF @typ = 3
            OR @typ = 4
            BEGIN -- Leistungen
                
                DECLARE @pk INT;
                IF @mitdatum = 1
                    BEGIN -- Nach Datum
						    
                        INSERT  @Tabelle_mit_datum
                                SELECT  *
                                FROM    View_Leistungen_mit_Datum
                                WHERE   Nrbehandlung = @behandlungsnr
                                ORDER BY Datum ,
                                        SSO_Nummer;
                        SET @i = 1;
                        DECLARE xc CURSOR
                        FOR
                            SELECT  *
                            FROM    @Tabelle_mit_datum
                            ORDER BY Datum ,
                                    [SSO_Nummer];
                        OPEN xc;
                        FETCH NEXT FROM xc INTO @type, @Datum, @SSO_Nummer,
                            @Bezd, @Taxpunkte, @Menge, @Frankenprotaxpunkt,
                            @Absolut, @Leistungstotal, @Nrbehandlung;
                        WHILE @@FETCH_STATUS = 0
                            BEGIN
                                IF @i = 1
                                    BEGIN
                                        INSERT  @lr_tabelle_mit_Datum
                                                ( L_Datum ,
                                                  L_SSO_Nummer ,
                                                  L_Bezd ,
                                                  L_Taxpunkte ,
                                                  L_Menge ,
                                                  L_Frankenprotaxpunkt ,
                                                  L_Absolut ,
                                                  L_Leistungstotal ,
                                                  R_Datum ,
                                                  R_SSO_Nummer ,
                                                  R_Bezd ,
                                                  R_Taxpunkte ,
                                                  R_Menge ,
                                                  R_Frankenprotaxpunkt ,
                                                  R_Absolut ,
                                                  R_Leistungstotal
                                                )
                                        VALUES  ( @Datum , -- L_Datum - date
                                                  @SSO_Nummer , -- L_SSO_Nummer - int
                                                  @Bezd , -- L_Bezd - varchar(255)
                                                  @Taxpunkte , -- L_Taxpunkte - float
                                                  @Menge , -- L_Menge - float
                                                  @Frankenprotaxpunkt , -- L_Frankenprotaxpunkt - float
                                                  @Absolut , -- L_Absolut - bit
                                                  @Leistungstotal , -- L_Leistungstotal - float
                                                  NULL , -- R_Datum - date
                                                  NULL , -- R_SSO_Nummer - int
                                                  CONVERT(VARCHAR(255), '') , -- R_Bezd - varchar(255)
                                                  NULL , -- R_Taxpunkte - float
                                                  NULL , -- R_Menge - float
                                                  NULL , -- R_Frankenprotaxpunkt - float
                                                  NULL , -- R_Absolut - bit
                                                  NULL  -- R_Leistungstotal - float
                                                );
                                   
                                        SET @i = @i + 1;
                                    END;
                                ELSE
                                    BEGIN
                                        SELECT TOP 1
                                                @pk = pk
                                        FROM    @lr_tabelle_mit_Datum
                                        ORDER BY pk DESC;
                                        UPDATE  @lr_tabelle_mit_Datum
                                        SET     R_Datum = @Datum ,
                                                R_SSO_Nummer = @SSO_Nummer ,
                                                R_Bezd = @Bezd ,
                                                R_Taxpunkte = @Taxpunkte ,
                                                R_Menge = @Menge ,
                                                R_Frankenprotaxpunkt = @Frankenprotaxpunkt ,
                                                R_Absolut = @Absolut ,
                                                R_Leistungstotal = @Leistungstotal
                                        WHERE   pk = @pk;
                                        SET @i = 1;
                                    END;
                                FETCH NEXT FROM xc INTO @type, @Datum,
                                    @SSO_Nummer, @Bezd, @Taxpunkte, @Menge,
                                    @Frankenprotaxpunkt, @Absolut,
                                    @Leistungstotal, @Nrbehandlung;
                            END; 
                        CLOSE xc;
                        DEALLOCATE xc;
                        IF @typ = 4
                            BEGIN
                                PRINT 'a0';
                                SELECT  *
                                FROM    @lr_tabelle_mit_Datum;
                            END;
                        ELSE
                            BEGIN
                                SELECT  *
                                FROM    @Tabelle_mit_datum;
                            END;
  
                    END;
                ELSE
                    BEGIN -- ohne Datum
					
                        INSERT  @Tabelle_ohne_datum
                                SELECT  *
                                FROM    View_Leistungen_ohne_Datum
                                WHERE   Nrbehandlung = @behandlungsnr
                                ORDER BY SSO_Nummer;
                        SET @i = 1;
			
                        DECLARE xc CURSOR
                        FOR
                            SELECT  *
                            FROM    @Tabelle_ohne_datum
                            ORDER BY [SSO_Nummer];
                        OPEN xc;
                        FETCH NEXT FROM xc INTO @type, @SSO_Nummer, @Bezd,
                            @Taxpunkte, @Menge, @Frankenprotaxpunkt, @Absolut,
                            @Leistungstotal, @Nrbehandlung;
                        WHILE @@FETCH_STATUS = 0
                            BEGIN
                                IF @i = 1
                                    BEGIN
                                        INSERT  @lr_tabelle_ohne_Datum
                                                ( L_SSO_Nummer ,
                                                  L_Bezd ,
                                                  L_Taxpunkte ,
                                                  L_Menge ,
                                                  L_Frankenprotaxpunkt ,
                                                  L_Absolut ,
                                                  L_Leistungstotal ,
                                                  R_SSO_Nummer ,
                                                  R_Bezd ,
                                                  R_Taxpunkte ,
                                                  R_Menge ,
                                                  R_Frankenprotaxpunkt ,
                                                  R_Absolut ,
                                                  R_Leistungstotal
                                                )
                                        VALUES  ( @SSO_Nummer , -- L_SSO_Nummer - int
                                                  @Bezd , -- L_Bezd - varchar(255)
                                                  @Taxpunkte , -- L_Taxpunkte - float
                                                  @Menge , -- L_Menge - float
                                                  @Frankenprotaxpunkt , -- L_Frankenprotaxpunkt - float
                                                  @Absolut , -- L_Absolut - bit
                                                  @Leistungstotal , -- L_Leistungstotal - float
                                                  NULL , -- R_SSO_Nummer - int
                                                  CONVERT(VARCHAR(255), '') , -- R_Bezd - varchar(255)
                                                  NULL , -- R_Taxpunkte - float
                                                  NULL , -- R_Menge - float
                                                  NULL , -- R_Frankenprotaxpunkt - float
                                                  NULL , -- R_Absolut - bit
                                                  NULL  -- R_Leistungstotal - float
                                                );
                                        SET @i = @i + 1;
										
                                    END;
                                ELSE
                                    BEGIN
									
                                        SELECT TOP 1
                                                @pk = pk
                                        FROM    @lr_tabelle_ohne_Datum
                                        ORDER BY pk DESC;
                                        PRINT @pk;
                                        UPDATE  @lr_tabelle_ohne_Datum
                                        SET     R_SSO_Nummer = @SSO_Nummer ,
                                                R_Bezd = @Bezd ,
                                                R_Taxpunkte = @Taxpunkte ,
                                                R_Menge = @Menge ,
                                                R_Frankenprotaxpunkt = @Frankenprotaxpunkt ,
                                                R_Absolut = @Absolut ,
                                                R_Leistungstotal = @Leistungstotal
                                        WHERE   pk = @pk;
                                        SET @i = 1;
                                    END;
                                FETCH NEXT FROM xc INTO @type, @SSO_Nummer,
                                    @Bezd, @Taxpunkte, @Menge,
                                    @Frankenprotaxpunkt, @Absolut,
                                    @Leistungstotal, @Nrbehandlung;
                            END; 
                        CLOSE xc;
                        DEALLOCATE xc;
                        IF @typ = 4
                            BEGIN
                                SELECT  '' AS Datum ,
                                        *
                                FROM    @lr_tabelle_ohne_Datum;
                            END;
                        ELSE
                            BEGIN
                                SELECT  '' AS Datum ,
                                        *
                                FROM    @Tabelle_ohne_datum;
                            END;

                    END;
            END;

        IF @typ = 5
            BEGIN 
                SELECT  *
                FROM    View_Leistungen_Dentotar
                WHERE   Nrbehandlung = @behandlungsnr;
            END;
            
				  
                
    END;
GO
Create PROCEDURE [dbo].[sp_sysobjects_select] AS
select * from sysobjects where xtype='U' order by name
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_rechnung_buchen]
    @type INT ,
    @Fakturanr VARCHAR(255) ,
    @Hauptfaktura VARCHAR(255) ,
    @rate INT ,
    @behandlungsnummer VARCHAR(255),
	@betrag AS FLOAT	
AS
    BEGIN
        DECLARE @nrdebitor INT;
        DECLARE @nrhauptfaktura VARCHAR(255);

        DECLARE @nrbehandler INT;
        DECLARE @nrgarant INT;
        DECLARE @behandlungsbeginn DATETIME;
        DECLARE @behandlungsende DATETIME;
        DECLARE @nrestyp INT;
        DECLARE @anschrift VARCHAR(255);
        DECLARE @faelligam DATETIME;
        DECLARE @total FLOAT;
        DECLARE @rabatt FLOAT;
        DECLARE @vorauszahlungen FLOAT;
        DECLARE @ratenzuschlag FLOAT;
        DECLARE @fallnr VARCHAR(255);
        DECLARE @mahnbetrag FLOAT;
        DECLARE @taxpunktwert FLOAT;
        DECLARE @rechnungsbemekrung VARCHAR(255);
        DECLARE @zahlungsbedingungen VARCHAR(255);
        DECLARE @andrucken_taxpunkte BIT;
        DECLARE @nrpatient INT;
        DECLARE @faelligkeitstage INT;
        SELECT  @nrpatient = Nrpatient ,
                @nrgarant = Nrgarant ,
                @nrbehandler = Nrbehandler ,
                @behandlungsbeginn = Behandlungsbeginn ,
                @behandlungsende = Behandlungsende ,
                @nrestyp = Nrestyp ,
                @total = Total ,
                @rabatt = Rabatt ,
                @ratenzuschlag = Ratenzuschlag ,
                @fallnr = Fallnummer ,
                @taxpunktwert = Taxpunktwert ,
                @rechnungsbemekrung = Rechnungsbemerkung ,
                @andrucken_taxpunkte = Andrucken_Taxpunkte
        FROM    dbo.Behandlu
        WHERE   Nrbehandlung = @behandlungsnummer;

		IF @rate > 1 BEGIN
           SELECT TOP 1 @Hauptfaktura=dbo.Faktura.nrfaktura FROM faktura WHERE nrbehandlung=@behandlungsnummer ORDER BY mutiert_am
		END
        
        IF @nrgarant = 0
            SET @nrgarant = @nrpatient;

        SELECT  @anschrift = dbo.get_adresse(@behandlungsnummer,1);
        SELECT  @faelligkeitstage = INHALT
        FROM    dbo.OPTIONS
        WHERE   NROPTION = 2000;
		IF @rate=0 SET @rate=1
        SET @faelligam = DATEADD(dd, @faelligkeitstage*@rate, GETDATE());

        INSERT  dbo.Faktura
                ( Nrfaktura ,
                  Nrbehandler ,
                  Nrhauptfaktura ,
                  Mandant ,
                  Nrbehandlung ,
                  Nrdebitor ,
                  Debitorsprachcode ,
                  Behandlungsbeginn ,
                  Behandlungsende ,
                  Nransatz ,
                  Nrpatient ,
                  Patientzeile ,
                  Nrestyp ,
                  Anschriftz1 ,
                  Anschriftz2 ,
                  Anschriftz3 ,
                  Anschriftz4 ,
                  Anschriftz5 ,
                  Anschriftz6 ,
                  Fallnummer ,
                  Datum ,
                  Faelligam ,
                  Ratennummer ,
                  Total ,
                  Rabatt ,
                  Vorauszahlungen ,
                  Ratenzuschlag ,
                  Mahnbetrag ,
                  Taxpunktwert ,
                  Rechnungsbemerkung ,
                  Zahlungsbedingungen ,
                  Andrucken_taxpunkte ,
                  Status ,
                  Statusdatum ,
                  MAHNSTOP ,
                  erstellt_am ,
                  mutiert_am ,
                  mutierer ,
                  aktiv
                )
        VALUES  ( @Fakturanr , -- Nrfaktura - int
                  @nrbehandler , -- Nrbehandler - int
                  @Hauptfaktura , -- Nrhauptfaktura - int
                  1 , -- Mandant - int
                  @behandlungsnummer , -- Nrbehandlung - int
                  @nrgarant , -- Nrdebitor - int
                  'D' , -- Debitorsprachcode - varchar(1)
                  @behandlungsbeginn , -- Behandlungsbeginn - date
                  @behandlungsende , -- Behandlungsende - date
                  0 , -- Nransatz - int
                  @nrpatient , -- Nrpatient - int
                  '' , -- Patientzeile - varchar(60)
                  @nrestyp , -- Nrestyp - int
                  '' , -- Anschriftz1 - varchar(40)
                  '' , -- Anschriftz2 - varchar(40)
                  '' , -- Anschriftz3 - varchar(40)
                  '' , -- Anschriftz4 - varchar(40)
                  '' , -- Anschriftz5 - varchar(40)
                  '' , -- Anschriftz6 - varchar(40)
                  '' , -- Fallnummer - varchar(40)
                  GETDATE() , -- Datum - date
                  @faelligam , -- Faelligam - date
                  @rate , -- Ratennummer - int
                  @betrag , -- Total - float
                  @rabatt , -- Rabatt - float
                  @vorauszahlungen , -- Vorauszahlungen - float
                  @ratenzuschlag , -- Ratenzuschlag - float
                  @mahnbetrag , -- Mahnbetrag - float
                  @taxpunktwert , -- Taxpunktwert - float
                  @rechnungsbemekrung , -- Rechnungsbemerkung - varchar(255)
                  @zahlungsbedingungen , -- Zahlungsbedingungen - varchar(255)
                  @andrucken_taxpunkte , -- Andrucken_taxpunkte - bit
                  0 , -- Status - int
                  GETDATE() , -- Statusdatum - date
                  0 , -- Mahnstop
                  GETDATE() , -- erstellt_am - datetime
                  GETDATE() , -- mutiert_am - datetime
                  1 , -- mutierer - int
                  1  -- aktiv - bit
                );
   
        UPDATE  Behandlu
        SET     Status = 3 ,
                mutiert_am = GETDATE()
        WHERE   Nrbehandlung = @behandlungsnummer;
		
		DECLARE @nreintrag INT
		SELECT TOP 1 @nreintrag = nreintrag+1 FROM dbo.DEBITOR ORDER BY Nreintrag desc
		INSERT dbo.DEBITOR
		        ( Nreintrag ,
		          Mandant ,
		          Nrbehandler ,
		          Nrdebitor ,
		          Nrfaktura ,
		          Nrpatient ,
		          Betrag ,
		          Faelligam ,
		          Status ,
		          Statusdatum ,
		          erstellt_am ,
		          mutiert_am ,
		          mutierer ,
		          aktiv
		        )
		VALUES  ( @nreintrag , -- Nreintrag - int
		          1 , -- Mandant - int
		          @nrbehandler , -- Nrbehandler - int
		          @nrgarant, -- Nrdebitor - int
		          @Fakturanr , -- Nrfaktura - int
		          @nrpatient , -- Nrpatient - int
		          @betrag , -- Betrag - float
		          @faelligam , -- Faelligam - date
		          2 , -- Status - int
		          GETDATE() , -- Statusdatum - date
		          GETDATE() , -- erstellt_am - datetime
		          GETDATE() , -- mutiert_am - datetime
		          2 , -- mutierer - int
		          1  -- aktiv - bit
		        )
		 
    END;
GO
