/*
		Firme 

UpdateFirme script

Martie 23, 2011
Version 1.0 
-------------------------------------
	- Se poate face inainte de Login.

*/

-- DECLARE SQL VARCHAR(max)
  
GO

/*Update#20110930.sql*/

--ALTER DATABASE [Firme]
--SET TRUSTWORTHY ON,
--    ENABLE_BROKER
--GO--

If db_id('Firme') Is Null
	Create Database Firme
GO

USE Firme
GO

ALTER DATABASE [Firme]
SET AUTO_CLOSE OFF, RECOVERY SIMPLE
GO

ALTER DATABASE [Firme]
COLLATE Latin1_General_CI_AS
GO

ALTER DATABASE [Firme]
SET ALLOW_SNAPSHOT_ISOLATION ON
GO

ALTER DATABASE [Firme]
SET TRUSTWORTHY OFF
GO

--ALTER DATABASE [Firme]
--MODIFY FILE (
--    NAME = [Firme_log], 
--    MAXSIZE = UNLIMITED )
--GO--

IF Object_id('Firme.dbo.soc') IS NULL
CREATE TABLE [dbo].[soc] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [CodSoc] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  [Denumire] varchar(40) COLLATE Latin1_General_CI_AS NOT NULL,
  [NumeDB] varchar(40) COLLATE Latin1_General_CI_AS NOT NULL,
  [Localitate] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [Judet] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [Sector] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  [Strada] varchar(30) COLLATE Latin1_General_CI_AS NULL,
  [NumarStrada] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  [CodPostal] varchar(16) COLLATE Latin1_General_CI_AS NULL,
  [Bloc] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [Scara] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [Etaj] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  [Apartament] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [Telefon] varchar(15) COLLATE Latin1_General_CI_AS NULL,
  [Fax] varchar(15) COLLATE Latin1_General_CI_AS NULL,
  [Email] varchar(50) COLLATE Latin1_General_CI_AS NULL,
  [CodFiscal] varchar(16) COLLATE Latin1_General_CI_AS NOT NULL,
  [RegistruComertului] varchar(16) COLLATE Latin1_General_CI_AS NOT NULL,
  [CodCaen] varchar(4) COLLATE Latin1_General_CI_AS NOT NULL,
  [Tarif] decimal(8, 3) DEFAULT 0.000 NOT NULL,
  [IsMicro] bit DEFAULT 0 NULL,
  [DepunereDeclaratii] varchar(16) COLLATE Latin1_General_CI_AS NOT NULL,
  [ModPlataTva] int NOT NULL,
  [NumePersoanaAutorizata] varchar(30) COLLATE Latin1_General_CI_AS NULL,
  [CapitalSocial] numeric(7, 0) DEFAULT 0 NULL,
  [Data_inc] date NULL,
  [Tip_Eval] tinyint DEFAULT 0 NULL,
  [Tip_Scad] tinyint DEFAULT 1 NULL,
  [Tip_Nir] tinyint DEFAULT 1 NULL,
  [Tip_Desc] tinyint DEFAULT 1 NULL,
  [Id_Numar] int DEFAULT 0 NULL,
  [Is_GV_Cum] bit DEFAULT 0 NULL,
  [Data_Prel] date NULL,
  [Data_PrelS] date NULL,
  [IsPretCuTvaBonFiscal] bit DEFAULT 1 NULL,
  [IsLastPriceCustomers] bit DEFAULT 1 NULL,
  [IsDataRap] bit DEFAULT 1 NULL,
  [iUserWorking] int NULL,
  [Is_On_G] bit DEFAULT 0 NULL,
  [Is_On_S] bit DEFAULT 1 NULL,
  [NC_Stock_WhichOperationDate] bit DEFAULT 1 NULL,
  [NC_Stock_Transfer] bit DEFAULT 1 NULL,
  [PreluatS] bit DEFAULT 0 NULL,
  [iSaveDays] int DEFAULT 7 NULL,
  [Tip_Decl] int DEFAULT 1 NULL,
  [PrenumePersoanaAutorizata] varchar(30) COLLATE Latin1_General_CI_AS NULL,
  [FunctiePersoanaAutorizata] varchar(20) COLLATE Latin1_General_CI_AS NULL,
  [cFooter] varchar(8000) COLLATE Latin1_General_CI_AS NULL,
  [SMTPServer] varchar(50) COLLATE Latin1_General_CI_AS NULL,
  [SMTPport] smallint NULL,
  [SMTPuseSSL] bit DEFAULT 0 NULL,
  [SMTPuserName] varchar(100) COLLATE Latin1_General_CI_AS NULL,
  [SMTPpassword] varchar(50) COLLATE Latin1_General_CI_AS NULL,
  [Sters] bit DEFAULT 0 NULL,
  [IsFactWithLogo] bit DEFAULT 0 NULL,
  [PunctLucru] varchar(254) COLLATE Latin1_General_CI_AS NULL,
  [Imp_Sold] bit DEFAULT 0 NULL,
  [Per_101] char(2) COLLATE Latin1_General_CI_AS NULL,
  [Tip_101] char(3) COLLATE Latin1_General_CI_AS NULL,
  [IsWithSoldTva] bit DEFAULT 0 NULL,
  [IntocmitBilant] varchar(50) COLLATE Latin1_General_CI_AS NULL,
  [CalitateIntocmitBilant] char(2) COLLATE Latin1_General_CI_AS NULL,
  [NumarExpert] char(15) COLLATE Latin1_General_CI_AS NULL,
  [TipFirma] char(2) COLLATE Latin1_General_CI_AS NULL,
  [FormaProprietate] char(2) COLLATE Latin1_General_CI_AS NULL,
  [LogoFactura] varbinary(max) NULL,
  [LogoHash] char(47) COLLATE Latin1_General_CI_AS NULL,
  [BackupAutomat] bit DEFAULT 0 NULL,
  CONSTRAINT [soc_pk] PRIMARY KEY CLUSTERED ([Id]),
  CONSTRAINT [soc_uq] UNIQUE ([Id])
)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.Soc','IsMicro') IS NULL 
	ALTER TABLE Firme.dbo.soc Add [IsMicro] bit DEFAULT 0 NULL
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsMicro'))
	EXEC sp_addextendedproperty 'MS_Description', N'Este MicroIntreprindere? 0=Nu;1=Da', 'schema', 'dbo', 'table', 'soc', 'column', 'IsMicro'
GO

IF COL_LENGTH('Firme.Dbo.Soc','DepunereDeclaratii') IS NULL 
	ALTER TABLE Firme.dbo.soc Add [DepunereDeclaratii] varchar(16) COLLATE Latin1_General_CI_AS NOT NULL
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'DepunereDeclaratii'))
EXEC sp_addextendedproperty 'MS_Description', N'Lunar
Trimestrial
Semestrial', 'schema', 'dbo', 'table', 'soc', 'column', 'DepunereDeclaratii'
GO

IF COL_LENGTH('Firme.Dbo.Soc','ModPlataTva') IS NULL 
	ALTER TABLE Firme.dbo.soc Add [ModPlataTva] int Not NULL
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'ModPlataTva'))
EXEC sp_addextendedproperty 'MS_Description', N'1 - Plata Tva Lunar
2 - Plata Tva Trimestrial
3 - Neplator de TVA ', 'schema', 'dbo', 'table', 'soc', 'column', 'ModPlataTva'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'NumePersoanaAutorizata'))
EXEC sp_addextendedproperty 'MS_Description', N'Numele persoanei autorizate sa semneze pe declaratii', 'schema', 'dbo', 'table', 'soc', 'column', 'NumePersoanaAutorizata'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Data_inc'))
EXEC sp_addextendedproperty 'MS_Description', N'Data Ultimei Inchideri Contabile', 'schema', 'dbo', 'table', 'soc', 'column', 'Data_inc'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Eval'))
EXEC sp_addextendedproperty 'MS_Description', N'Evaluarea Marfurilor
4=Nu tin gestiuni Global valoric
1=Pret Vanzare cu Tva
2=Pret Vanzare fara Tva(vanzari en-gros)
3=Pret de vanzare pe gestiuni distincte

  Nu se poate lucra cu marfuri in cazul in care se foloseste evidenta global-valorica fara Gestiuni (1, 2).

  Daca se doreste sa se lucreze concomitent in sistem global-valoric si cantitativ-valoric, 
trebuie sa se treaca la evidenta global-valorica pe GESTIUNI DISTINCTE.(3)

 Daca se doreste sa se lucreze numai in sistem global-valoric (1,2), nu trebuie sa aiba Articol de tip Marfuri in ARTICOLE,  iar in documentele introduse trebuie lasat sa scrie utilizatorul ce doreste in textbox (de ex. "Marfa").', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Eval'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Scad'))
EXEC sp_addextendedproperty 'MS_Description', N'Tip de calcul al datei scadentei in facturi
1 = Include Sambetele si Duminicile
2 = Fara Sambete si Duminici', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Scad'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Nir'))
EXEC sp_addextendedproperty 'MS_Description', N'Daca in Form Numerotare este bifat Cel mai mare numar + 1 daca NU se introduce Manual.

Numerotare NIR-uri
1 = Cel mai mare numar + 1
2 = Numarul Anterior + 1
3 = Numarul Anterior + 1 cu renumerotarea numelor posterioare', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Nir'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Desc'))
EXEC sp_addextendedproperty 'MS_Description', N'Descarcare Gestiune
1 = FIFO
2 = LIFO
3 = CMP
4 = Manual
5 = Loturi (inca nefolosit)', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Desc'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Id_Numar'))
EXEC sp_addextendedproperty 'MS_Description', N'Numeratorul pentru societatea curenta', 'schema', 'dbo', 'table', 'soc', 'column', 'Id_Numar'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Is_GV_Cum'))
EXEC sp_addextendedproperty 'MS_Description', N'Foloseste Rulaje cumulate pentru Gestiunile Global Valorice
0 - Nu
1 - Da', 'schema', 'dbo', 'table', 'soc', 'column', 'Is_GV_Cum'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Data_Prel'))
EXEC sp_addextendedproperty 'MS_Description', N'Data Preluarii Contabile', 'schema', 'dbo', 'table', 'soc', 'column', 'Data_Prel'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Data_PrelS'))
EXEC sp_addextendedproperty 'MS_Description', N'Data Preluarii Stocurilor', 'schema', 'dbo', 'table', 'soc', 'column', 'Data_PrelS'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsPretCuTvaBonFiscal'))
EXEC sp_addextendedproperty 'MS_Description', N'Bonurile Fiscale din ''Iesiri'', se calculeaza plecand de la pretul cu TVA.

0 - Nu
1 - Da', 'schema', 'dbo', 'table', 'soc', 'column', 'IsPretCuTvaBonFiscal'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsLastPriceCustomers'))
EXEC sp_addextendedproperty 'MS_Description', N'Preia in Facturile de Iesire,  ultimul pret de vanzare pt. clientului curent.', 'schema', 'dbo', 'table', 'soc', 'column', 'IsLastPriceCustomers'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsDataRap'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = NU afiseaza data in Rapoarte
1 = Afiseaza data in Rapoarte', 'schema', 'dbo', 'table', 'soc', 'column', 'IsDataRap'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'iUserWorking'))
EXEC sp_addextendedproperty 'MS_Description', N'Utilizatorul care lucreaza pe inregistrare', 'schema', 'dbo', 'table', 'soc', 'column', 'iUserWorking'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Is_On_G'))
EXEC sp_addextendedproperty 'MS_Description', N'Afisez numai articolele cu rulaj pe gestiunea selectata
0 - Afieaza Toate Articolele
1 - Afiseaza numai Articolele pentru gestiunea selectata', 'schema', 'dbo', 'table', 'soc', 'column', 'Is_On_G'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Is_On_S'))
EXEC sp_addextendedproperty 'MS_Description', N'Nu permit validarea cānd stocul devine negativ
0 - Permite 
1 - Nu permite ', 'schema', 'dbo', 'table', 'soc', 'column', 'Is_On_S'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'NC_Stock_WhichOperationDate'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = Data NC Inchidere Stocuri este Data Inchiderii de luna si Id-ul este cel Incrementat in Soc
1 = Data NC Inchidere Stocuri este Data Documentului Notei de Predare la care se refera Id-ul este cel al Notei de Predare (Recomandat)', 'schema', 'dbo', 'table', 'soc', 'column', 'NC_Stock_WhichOperationDate'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'NC_Stock_Transfer'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = NU se inregistreaza Transferurile intre Gestiuni
1 = Inregistreaza SI Transferurile intre Gestiuni  (Recomandat)', 'schema', 'dbo', 'table', 'soc', 'column', 'NC_Stock_Transfer'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'PreluatS'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = NU au fost preluate stocurile precedente
1 = Au fost preluate stocurile precedente', 'schema', 'dbo', 'table', 'soc', 'column', 'PreluatS'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'iSaveDays'))
EXEC sp_addextendedproperty 'MS_Description', N'Numarul de zile la care se face backup', 'schema', 'dbo', 'table', 'soc', 'column', 'iSaveDays'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Decl'))
EXEC sp_addextendedproperty 'MS_Description', N'Cand se depun Declaratiile 100,102
1 - Lunar
2 - Trimestrial
3 - Semestrial', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_Decl'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'cFooter'))
EXEC sp_addextendedproperty 'MS_Description', N'Textul Folosit In Garantie pt. Articole', 'schema', 'dbo', 'table', 'soc', 'column', 'cFooter'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Sters'))
EXEC sp_addextendedproperty 'MS_Description', N'Daca inregistrare este stearsa', 'schema', 'dbo', 'table', 'soc', 'column', 'Sters'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsFactWithLogo'))
EXEC sp_addextendedproperty 'MS_Description', N'Daca afiseaza Logo Firma pe raportul Facturi', 'schema', 'dbo', 'table', 'soc', 'column', 'IsFactWithLogo'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'PunctLucru'))
EXEC sp_addextendedproperty 'MS_Description', N'Adresa Punctului de Lucru', 'schema', 'dbo', 'table', 'soc', 'column', 'PunctLucru'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Imp_Sold'))
EXEC sp_addextendedproperty 'MS_Description', N'Se Bifeaza daca luna de inceput a programului este īn cursul anului, nu din 01/01.
Pentru afisarea corecta a datelor despre Venituri si Cheltuieli bifati la caseta de validare "Include Sold Impozit Preluat". (''891'')', 'schema', 'dbo', 'table', 'soc', 'column', 'Imp_Sold'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Per_101'))
EXEC sp_addextendedproperty 'MS_Description', N'AN
T3
T4', 'schema', 'dbo', 'table', 'soc', 'column', 'Per_101'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_101'))
EXEC sp_addextendedproperty 'MS_Description', N'103', 'schema', 'dbo', 'table', 'soc', 'column', 'Tip_101'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'soc', 'column', 'IsWithSoldTva'))
EXEC sp_addextendedproperty 'MS_Description', N'Cu preluarea automata a soldurilor periodei precedente.
Folosit la DecontTva', 'schema', 'dbo', 'table', 'soc', 'column', 'IsWithSoldTva'
GO

IF NOT EXISTS (SELECT * FROM Firme.sys.indexes WHERE object_id = OBJECT_ID('Firme.dbo.soc') AND Name = 'soc_uq2')
CREATE UNIQUE NONCLUSTERED INDEX [soc_uq2] ON [dbo].[soc]
  ([NumeDB])
WITH (
  PAD_INDEX = OFF,
  IGNORE_DUP_KEY = OFF,
  DROP_EXISTING = OFF,
  STATISTICS_NORECOMPUTE = OFF,
  SORT_IN_TEMPDB = OFF,
  ONLINE = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.Soc','Is_On_G') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Is_On_G] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Is_On_G]  bit NULL
GO

IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME='Soc' AND COLUMN_NAME='Is_On_G' AND COLUMN_DEFAULT IS NULL)
	ALTER TABLE [dbo].[Module] ADD DEFAULT 0 FOR [Is_On_G]
GO

IF COL_LENGTH('Firme.Dbo.Soc','Is_On_S') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Is_On_S] bit DEFAULT 1 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Is_On_S]  bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','NC_Stock_WhichOperationDate') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [NC_Stock_WhichOperationDate] bit DEFAULT 1 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [NC_Stock_WhichOperationDate]  bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','NC_Stock_Transfer') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [NC_Stock_Transfer] bit DEFAULT 1 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [NC_Stock_Transfer]  bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','PreluatS') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [PreluatS] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [PreluatS]  bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','iSaveDays') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [iSaveDays] int DEFAULT 7 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [iSaveDays]  int NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','iSaveDays') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [iSaveDays] int DEFAULT 7 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [iSaveDays]  int NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Tip_Decl') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Tip_Decl] int DEFAULT 1 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Tip_Decl]  int NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','PrenumePersoanaAutorizata') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [PrenumePersoanaAutorizata] varchar(30) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [PrenumePersoanaAutorizata]  varchar(30) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','FunctiePersoanaAutorizata') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [FunctiePersoanaAutorizata] varchar(20) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [FunctiePersoanaAutorizata]  varchar(20) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','cFooter') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [cFooter] varchar(8000) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [cFooter]  varchar(8000) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPServer') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [SMTPServer] varchar(50) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [SMTPServer]  varchar(50) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPport') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [SMTPport]  smallint NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [SMTPport] smallint NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPuseSSL') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [SMTPuseSSL] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [SMTPuseSSL] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPuserName') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [SMTPuserName]  varchar(100) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [SMTPuserName]  varchar(100) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPpassword') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [SMTPpassword]  varchar(50) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [SMTPpassword] varchar(50) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Sters') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Sters] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Sters] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','IsFactWithLogo') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [IsFactWithLogo] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [IsFactWithLogo] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','PunctLucru') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [PunctLucru] varchar(254) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [PunctLucru] varchar(254) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Imp_Sold') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Imp_Sold] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Imp_Sold] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Per_101') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Per_101] char(2) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Per_101] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Tip_101') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [Tip_101] char(3) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [Tip_101] char(3) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','IsWithSoldTva') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [IsWithSoldTva] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [IsWithSoldTva] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','IntocmitBilant') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [IntocmitBilant] varchar(50) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [IntocmitBilant] varchar(50) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','CalitateIntocmitBilant') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [CalitateIntocmitBilant] char(2) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [CalitateIntocmitBilant] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','NumarExpert') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [NumarExpert] char(15) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [NumarExpert] char(15) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','TipFirma') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [TipFirma] char(2) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [TipFirma] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','FormaProprietate') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [FormaProprietate] char(2) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [FormaProprietate] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','LogoFactura') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [LogoFactura] varbinary(max) NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [LogoFactura] varbinary(max) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','LogoHash') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [LogoHash] char(47) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [LogoHash] char(47) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','BackupAutomat') IS NULL 
	ALTER TABLE Firme.dbo.Soc ADD [BackupAutomat] bit DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Soc ALTER COLUMN [BackupAutomat] bit NULL
GO

/* ============================= DrFirme ============================= */

IF Object_id('Firme.dbo.DrFirme') IS NULL
CREATE TABLE [dbo].[DrFirme] (
  [IdUser] int NOT NULL,
  [IdSoc] int NULL,
  [CodSoc] varchar(10) COLLATE Latin1_General_CI_AS NOT NULL,
  [bAd] bit CONSTRAINT [DF__DrFirme__bAd__5DCAEF64] DEFAULT 0 NULL,
  [bEd] bit CONSTRAINT [DF__DrFirme__bEd__5EBF139D] DEFAULT 0 NULL,
  [bSt] bit CONSTRAINT [DF__DrFirme__bSt__5FB337D6] DEFAULT 0 NULL,
  [bPi] bit CONSTRAINT [DF__DrFirme__bPi__60A75C0F] DEFAULT 0 NULL,
  [bValidare] bit CONSTRAINT [DF__DrFirme__bValida__619B8048] DEFAULT 0 NULL,
  [IdProfil] int NULL,
  CONSTRAINT [DrFirme_fk] FOREIGN KEY ([IdSoc]) 
  REFERENCES [dbo].[soc] ([Id]) 
  ON UPDATE NO ACTION
  ON DELETE NO ACTION
)
ON [PRIMARY]
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'DrFirme', 'column', 'IdSoc'))
EXEC sp_addextendedproperty 'MS_Description', N'Id-ul Firmei', 'schema', 'dbo', 'table', 'DrFirme', 'column', 'IdSoc'
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','IdUser') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [IdUser]  int NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [IdUser] int NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','IdSoc') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [IdSoc] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [IdSoc] int NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','CodSoc') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [CodSoc] varchar(10) COLLATE Latin1_General_CI_AS NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [CodSoc] varchar(10) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','bAd') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [bAd] bit CONSTRAINT [DF__DrFirme__bAd__5DCAEF64] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [bAd] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','bEd') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [bEd] bit CONSTRAINT [DF__DrFirme__bEd__5EBF139D] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [bEd] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','bSt') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [bSt]  bit CONSTRAINT [DF__DrFirme__bSt__5FB337D6] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [bSt]  bit NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','bPi') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [bPi] bit CONSTRAINT [DF__DrFirme__bPi__60A75C0F] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [bPi] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','bValidare') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [bValidare] bit CONSTRAINT [DF__DrFirme__bValida__619B8048] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [bValidare] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.DrFirme','IdProfil') IS NULL 
	ALTER TABLE Firme.dbo.DrFirme ADD [IdProfil] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrFirme ALTER COLUMN [IdProfil] int NULL
GO

/* ============================= DrModule ============================= */

IF Object_id('Firme.dbo.DrModule') IS NULL
CREATE TABLE [dbo].[DrModule] (
  [IdUser] int NULL,
  [IdModul] int NOT NULL,
  [CodSoc] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  [IdSoc] int NULL,
  [IdProfil] int NULL
)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.DrModule','IdUser') IS NULL 
	ALTER TABLE Firme.dbo.DrModule ADD [IdUser] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrModule ALTER COLUMN [IdUser] int NULL
GO

IF COL_LENGTH('Firme.Dbo.DrModule','IdModul') IS NULL 
	ALTER TABLE Firme.dbo.DrModule ADD [IdModul] int NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.DrModule ALTER COLUMN [IdModul] int NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.DrModule','CodSoc') IS NULL 
	ALTER TABLE Firme.dbo.DrModule ADD [CodSoc] varchar(10) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.DrModule ALTER COLUMN [CodSoc] varchar(10) NULL
GO

IF COL_LENGTH('Firme.Dbo.DrModule','IdSoc') IS NULL 
	ALTER TABLE Firme.dbo.DrModule ADD [IdSoc] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrModule ALTER COLUMN [IdSoc] int NULL
GO

IF COL_LENGTH('Firme.Dbo.DrModule','IdProfil') IS NULL 
	ALTER TABLE Firme.dbo.DrModule ADD [IdProfil] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrModule ALTER COLUMN [IdProfil] int NULL
GO

/* ============================= DrProfile ============================= */

IF Object_id('dbo.DrProfile') IS NULL
CREATE TABLE [dbo].[DrProfile] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [Profil] int NULL,
  CONSTRAINT [PK__DrProfil__3214EC076B24EA82] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.DrProfile','Id') IS NULL 
	ALTER TABLE Firme.dbo.DrProfile ADD [Id] int IDENTITY(1, 1) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.DrProfile','Profil') IS NULL 
	ALTER TABLE Firme.dbo.DrProfile ADD [Profil] int NULL
ELSE	
	ALTER TABLE Firme.dbo.DrProfile ALTER COLUMN [Profil] int NULL
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'DrProfile', 'column', 'Profil'))
EXEC sp_addextendedproperty 'MS_Description', N'Id-ul din Profile', 'schema', 'dbo', 'table', 'DrProfile', 'column', 'Profil'
GO

/* ============================= Module ============================= */

IF Object_id('dbo.Module') IS NULL
CREATE TABLE [dbo].[Module] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [Modul] char(40) COLLATE Latin1_General_CI_AS NULL,
  [IdSuperior] int NULL,
  CONSTRAINT [PK__Module__3214EC071273C1CD] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'Module', 'column', 'IdSuperior'))
EXEC sp_addextendedproperty 'MS_Description', N'Id Modul Superior', 'schema', 'dbo', 'table', 'Module', 'column', 'IdSuperior'
GO

IF COL_LENGTH('Firme.Dbo.Module','Id') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [Id] int IDENTITY(1, 1) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.Module','Modul') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [Modul] char(40) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Module ALTER COLUMN [Modul] char(40) NULL
GO

IF COL_LENGTH('Firme.Dbo.Module','IdSuperior') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [IdSuperior] int NULL
ELSE	
	ALTER TABLE Firme.dbo.Module ALTER COLUMN [IdSuperior] int NULL
GO

/* ============================= Profile ============================= */

IF Object_id('dbo.Profile') IS NULL
CREATE TABLE [dbo].[Profile] (
  [Id] int NOT NULL,
  [Profil] char(15) COLLATE Latin1_General_CI_AS NULL,
  CONSTRAINT [PK__Profile__3214EC0703317E3D] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.Profile','Id') IS NULL 
	ALTER TABLE Firme.dbo.Profile ADD [Id] int NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.Profile ALTER COLUMN [Id] int NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.Profile','Profil') IS NULL 
	ALTER TABLE Firme.dbo.Profile ADD [Profil] char(15) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Profile ALTER COLUMN [Profil] char(15) NULL
GO

/* ============================= UFirme ============================= */

IF Object_id('dbo.UFirme') IS NULL
CREATE TABLE [dbo].[UFirme] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [IdUser] int NULL,
  [IdSoc] int NULL,
  CONSTRAINT [PK__UFirme__3214EC0748CFD27E] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

--IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'UFirme', 'column', ''))
--EXEC sp_addextendedproperty 'MS_Description', N'Firmele pe care poate lucra un utilizator', 'schema', 'dbo', 'table', 'UFirme'
--GO--

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'UFirme', 'column', 'IdUser'))
EXEC sp_addextendedproperty 'MS_Description', N'Id User', 'schema', 'dbo', 'table', 'UFirme', 'column', 'IdUser'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'UFirme', 'column', 'IdSoc'))
EXEC sp_addextendedproperty 'MS_Description', N'Id Firma', 'schema', 'dbo', 'table', 'UFirme', 'column', 'IdSoc'
GO

IF COL_LENGTH('Firme.Dbo.UFirme','Id') IS NULL 
	ALTER TABLE Firme.dbo.UFirme ADD [Id] int IDENTITY(1, 1) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.UFirme','IdUser') IS NULL 
	ALTER TABLE Firme.dbo.UFirme ADD [IdUser] int NULL
ELSE	
	ALTER TABLE Firme.dbo.UFirme ALTER COLUMN [IdUser] int NULL
GO

IF COL_LENGTH('Firme.Dbo.UFirme','IdSoc') IS NULL 
	ALTER TABLE Firme.dbo.UFirme ADD [IdSoc] int NULL
ELSE	
	ALTER TABLE Firme.dbo.UFirme ALTER COLUMN [IdSoc] int NULL
GO

/* ============================= Utilizatori ============================= */

IF Object_id('dbo.Utilizatori') IS NULL
CREATE TABLE [dbo].[Utilizatori] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [Nume] char(20) COLLATE Latin1_General_CI_AS NOT NULL,
  [parola] char(10) COLLATE Latin1_General_CI_AS NULL,
  [Profil] int NULL,
  [sStationConnected] char(50) COLLATE Latin1_General_CI_AS NULL,
  [bAd] bit CONSTRAINT [DF__Utilizatori__bAd__117F9D94] DEFAULT 0 NULL,
  [bEd] bit CONSTRAINT [DF__Utilizatori__bEd__1273C1CD] DEFAULT 0 NULL,
  [bSt] bit CONSTRAINT [DF__Utilizatori__bSt__1367E606] DEFAULT 0 NULL,
  [bPi] bit CONSTRAINT [DF__Utilizatori__bPl__25869641] DEFAULT 0 NULL,
  [bValidare] bit CONSTRAINT [DF__Utilizato__bVali__267ABA7A] DEFAULT 0 NULL,
  [IsAdmin] bit CONSTRAINT [DF__Utilizato__IsAdm__2A4B4B5E] DEFAULT 0 NULL,
  [LastSoc] varchar(10) COLLATE Latin1_General_CI_AS NULL,
  CONSTRAINT [PK__Utilizat__3214EC077F60ED59] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'bPi'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = NU poate sa faca plati si incasari
1 = Poate sa faca plati si incasari', 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'bPi'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'bValidare'))
EXEC sp_addextendedproperty 'MS_Description', N'0 = Nu are dreptul sa faca Validari
1 = Poate face validari', 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'bValidare'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'IsAdmin'))
EXEC sp_addextendedproperty 'MS_Description', N'Daca este Admin', 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'IsAdmin'
GO

IF  Not EXISTS ( SELECT objtype FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'LastSoc'))
EXEC sp_addextendedproperty 'MS_Description', N'Ultima firma pe care a lucrat', 'schema', 'dbo', 'table', 'Utilizatori', 'column', 'LastSoc'
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','Id') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [Id] int IDENTITY(1, 1) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','Nume') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [Nume] char(20) COLLATE Latin1_General_CI_AS NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [Nume] char(20) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','parola') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [parola] char(10) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [parola] char(10) NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','Profil') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [Profil] int NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [Profil] int NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','sStationConnected') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [sStationConnected] char(50) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [sStationConnected] char(50) NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','bAd') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [bAd] bit CONSTRAINT [DF__Utilizatori__bAd__117F9D94] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [bAd] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','bEd') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [bEd] bit CONSTRAINT [DF__Utilizatori__bEd__1273C1CD] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [bEd] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','bSt') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [bSt] bit CONSTRAINT [DF__Utilizatori__bSt__1367E606] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [bSt] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','bPi') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [bPi] bit CONSTRAINT [DF__Utilizatori__bPl__25869641] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [bPi] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','bValidare') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [bValidare] bit CONSTRAINT [DF__Utilizato__bVali__267ABA7A] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [bValidare] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','IsAdmin') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [IsAdmin] bit CONSTRAINT [DF__Utilizato__IsAdm__2A4B4B5E] DEFAULT 0 NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [IsAdmin] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Utilizatori','LastSoc') IS NULL 
	ALTER TABLE Firme.dbo.Utilizatori ADD [LastSoc] varchar(10) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Utilizatori ALTER COLUMN [LastSoc] varchar(10) NULL
GO

/* ============================= ErrorLog ============================= */

IF Object_id('dbo.ErrorLog') IS NULL
CREATE TABLE [dbo].[ErrorLog] (
  [ErrorLogID] int IDENTITY(1, 1) NOT NULL,
  [ErrorTime] datetime CONSTRAINT [DF_ErrorLog_ErrorTime] DEFAULT getdate() NOT NULL,
  [UserName] sysname COLLATE Latin1_General_CS_AS NOT NULL,
  [ErrorNumber] int NOT NULL,
  [ErrorSeverity] int NULL,
  [ErrorState] int NULL,
  [ErrorProcedure] nvarchar(126) COLLATE Latin1_General_CS_AS NULL,
  [ErrorLine] int NULL,
  [ErrorMessage] nvarchar(4000) COLLATE Latin1_General_CS_AS NOT NULL,
  CONSTRAINT [PK_ErrorLog_ErrorLogID] PRIMARY KEY CLUSTERED ([ErrorLogID])
)
ON [PRIMARY]
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorLogID') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorLogID] int IDENTITY(1, 1) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorTime') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorTime] datetime CONSTRAINT [DF_ErrorLog_ErrorTime] DEFAULT getdate() NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorTime] datetime NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','UserName') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [UserName] sysname COLLATE Latin1_General_CS_AS NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [UserName] sysname NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorNumber') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorNumber] int NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorNumber] int NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorSeverity') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorSeverity] int NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorSeverity] int NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorState') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorState] int NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorState] int NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorProcedure') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorProcedure] nvarchar(126) COLLATE Latin1_General_CS_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorProcedure] nvarchar(126) NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorLine') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorLine] int NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorLine] int NULL
GO

IF COL_LENGTH('Firme.Dbo.ErrorLog','ErrorMessage') IS NULL 
	ALTER TABLE Firme.dbo.ErrorLog ADD [ErrorMessage] nvarchar(4000) COLLATE Latin1_General_CS_AS NOT NULL
ELSE	
	ALTER TABLE Firme.dbo.ErrorLog ALTER COLUMN [ErrorState] nvarchar(4000) NOT NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','IsWithSoldTva') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [IsWithSoldTva] bit NULL
GO

IF COL_LENGTH('Firme.Dbo.Module','IdSuperior') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [IdSuperior] int NULL
GO

IF COL_LENGTH('Firme.Dbo.Module','IdSuperior') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [IdSuperior] int NULL
GO

IF COL_LENGTH('Firme.Dbo.Module','Modul') IS NULL 
	ALTER TABLE Firme.dbo.Module ADD [Modul] char(40) COLLATE Latin1_General_CI_AS NULL
ELSE	
	ALTER TABLE Firme.dbo.Module ALTER COLUMN [Modul] char(40) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Tip_Decl') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [Tip_Decl] int NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','NumePersoanaAutorizata') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [NumePersoanaAutorizata] varchar(30) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','PrenumePersoanaAutorizata') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [PrenumePersoanaAutorizata] varchar(30) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','FunctiePersoanaAutorizata') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [FunctiePersoanaAutorizata] varchar(20) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','cFooter') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [cFooter] varchar(8000) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPServer') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [SMTPServer] varchar(50) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPport') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [SMTPport] smallint NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPuseSSL') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [SMTPuseSSL] bit DEFAULT 0 NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPuserName') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [SMTPuserName] varchar(100) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','SMTPpassword') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [SMTPpassword] varchar(50) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Sters') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [Sters] bit DEFAULT 0 NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','IsFactWithLogo') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [IsFactWithLogo] bit DEFAULT 0 NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','PunctLucru') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [PunctLucru] varchar(254) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Imp_Sold') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [Imp_Sold] bit DEFAULT 0 NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Per_101') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [Per_101] char(2) COLLATE Latin1_General_CI_AS NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','Tip_101') IS NULL 
	ALTER TABLE Firme.dbo.soc ADD [Tip_101] char(3) COLLATE Latin1_General_CI_AS NULL
GO

IF NOT EXISTS (SELECT * FROM Firme.sys.indexes WHERE object_id = OBJECT_ID('Firme.dbo.soc') AND Name = 'soc_uq2')
	CREATE UNIQUE NONCLUSTERED INDEX soc_uq2 ON Firme.dbo.soc (NumeDB) ON 'PRIMARY';
GO

-- sau 
--	ALTER TABLE [dbo].[soc]
--	ADD CONSTRAINT [soc_uq2] 
--	UNIQUE NONCLUSTERED ([NumeDB])
--	WITH (
--	  PAD_INDEX = OFF,
--	  IGNORE_DUP_KEY = OFF,
--	  STATISTICS_NORECOMPUTE = OFF,
--	  ALLOW_ROW_LOCKS = ON,
--	  ALLOW_PAGE_LOCKS = ON)
--	ON [PRIMARY]

-- Disable AutoClose
-- ALTER DATABASE [Firme] SET AUTO_CLOSE OFF
-- GO-- 
 
-- Mareste Firme de 2.25 la 4.25 MB
-- ALTER DATABASE [Firme] MODIFY FILE (NAME = [Firme],  SIZE = 4352 KB )
-- GO --
 
IF Object_id('uspPrintError') IS NOT NULL
  DROP PROC [uspPrintError]
GO

-- uspPrintError prints error information about the error that caused 
-- execution to jump to the CATCH block of a TRY...CATCH construct. 
-- Should be executed from within the scope of a CATCH block otherwise 
-- it will return without printing any error information.
CREATE PROCEDURE [uspPrintError] 
AS
BEGIN
    SET NOCOUNT ON;

    -- Print error information. 
     PRINT 
          ' Procedure: ' + ISNULL(ERROR_PROCEDURE(), '-') + 
          ', Line: ' + CONVERT(varchar(5), ERROR_LINE()) 
          + CHAR(13)+
          + CHAR(13)+          
          ISNULL(ERROR_MESSAGE(), '-'); 
    PRINT ERROR_MESSAGE();
END;
GO

IF Object_id('uspLogError') IS NOT NULL
  DROP PROC [uspLogError]
GO

-- uspLogError logs error information in the ErrorLog table about the 
-- error that caused execution to jump to the CATCH block of a 
-- TRY...CATCH construct. This should be executed from within the scope 
-- of a CATCH block otherwise it will return without inserting error 
-- information. 
CREATE PROCEDURE [uspLogError] 
    @ErrorLogID [int] = 0 OUTPUT -- contains the ErrorLogID of the row inserted
AS                               -- by uspLogError in the ErrorLog table
BEGIN
    SET NOCOUNT ON;

    -- Output parameter value of 0 indicates that error 
    -- information was not logged
    SET @ErrorLogID = 0;

    BEGIN TRY
        -- Return if there is no error information to log
        IF ERROR_NUMBER() IS NULL
            RETURN;

        -- Return if inside an uncommittable transaction.
        -- Data insertion/modification is not allowed when 
        -- a transaction is in an uncommittable state.
        IF XACT_STATE() = -1
        BEGIN
            PRINT 'Cannot log error since the current transaction is in an uncommittable state. ' 
                + 'Rollback the transaction before executing uspLogError in order to successfully log error information.';
            RETURN;
        END

        INSERT [dbo].[ErrorLog] 
            (
            [UserName], 
            [ErrorNumber], 
            [ErrorSeverity], 
            [ErrorState], 
            [ErrorProcedure], 
            [ErrorLine], 
            [ErrorMessage]
            ) 
        VALUES 
            (
            CONVERT(sysname, CURRENT_USER), 
            ERROR_NUMBER(),
            ERROR_SEVERITY(),
            ERROR_STATE(),
            ERROR_PROCEDURE(),
            ERROR_LINE(),
            ERROR_MESSAGE()
            );

        -- Pass back the ErrorLogID of the row inserted
        SET @ErrorLogID = @@IDENTITY;
    END TRY
    BEGIN CATCH
        PRINT 'An error occurred in stored procedure uspLogError: ';
        EXECUTE [dbo].[uspPrintError];
        RETURN -1;
    END CATCH
END;
GO

USE Master
GO

IF COL_LENGTH('Firme.Dbo.Soc','IntocmitBilant') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [IntocmitBilant] varchar(50) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','CalitateIntocmitBilant') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [CalitateIntocmitBilant] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','NumarExpert') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [NumarExpert] char(15) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','TipFirma') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [TipFirma] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','FormaProprietate') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [FormaProprietate] char(2) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','LogoFactura') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [LogoFactura] varbinary(max) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','LogoHash') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [LogoHash] char(47) NULL
GO

IF COL_LENGTH('Firme.Dbo.Soc','BackupAutomat') IS NULL 
	ALTER TABLE [Firme].[dbo].[soc] ADD [BackupAutomat] bit DEFAULT 0 NULL
GO

Use Firme
GO

If Exists ( Select * From Firme.sys.triggers Where Name = 'soc_tri' )
	Drop Trigger [dbo].[soc_tri]
GO

CREATE TRIGGER [dbo].[soc_tri] ON [dbo].[soc]
WITH EXECUTE AS CALLER
FOR INSERT
AS
	BEGIN
		DECLARE @Count int;
		SET @Count = @@ROWCOUNT;
		IF @Count = 0 
			RETURN;
		SET NOCOUNT ON;
		BEGIN TRY
			UPDATE Soc
			SET CodSoc = 
				(SELECT CAST(Id AS VarChar(10)) FROM inserted)
			 FROM Soc
				INNER JOIN Inserted ON Soc.Id = Inserted.Id
			 WHERE Soc.Id = Inserted.Id
		END TRY
		BEGIN CATCH
			EXECUTE [dbo].[uspPrintError];
			-- Rollback any active or uncommittable transactions before
			-- inserting information in the ErrorLog
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION;
			END
			EXECUTE [dbo].[uspLogError];
		END CATCH;
	END;
GO

IF Object_id('Firme.dbo.Module') IS NOT NULL
Drop Table [dbo].[Module]
GO

CREATE TABLE [Firme].[dbo].[Module] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [Modul] char(40) COLLATE Latin1_General_CI_AS NULL,
  [IdSuperior] int NULL,
  [Administrator] bit NULL,
  [Utilizator] bit NULL,
  [Altii] bit NULL,
  CONSTRAINT [PK__Module__3214EC071273C1CD] PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

EXEC sp_addextendedproperty 'MS_Description', N'Id Modul Superior', 'schema', 'dbo', 'table', 'Module', 'column', 'IdSuperior'
GO

TRUNCATE TABLE [Firme].[dbo].[Module]
GO

Set Identity_Insert Module On
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (1, N'Preluari', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (2, N'Preluare Solduri', 1, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (3, N'Preluare Stocuri', 1, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (4, N'Import Date', 1, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (5, N'Export Date', 1, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (6, N'Nomenclatoare', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (7, N'Furnizori', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (9, N'Clienti', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (10, N'Conturi', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (11, N'Conturi la Banci', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (12, N'Conturi in Numerar', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (13, N'Conturi Avansuri Trezorerie', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (14, N'Gestiuni', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (15, N'Tipuri Articole', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (16, N'Articole', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (17, N'Agenti', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (18, N'Delegati', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (19, N'Reprezentanti Facturare', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (20, N'Categorii', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (21, N'Valute', 6, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (22, N'Documente', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (23, N'Intrari', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (24, N'Intrari Valuta', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (25, N'Iesiri', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (26, N'Iesiri Avize', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (27, N'Iesiri Valuta', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (28, N'Trezorerie', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (29, N'Note Contabile', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (30, N'Cecuri BO', 22, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (31, N'Inchideri Contabile', 22, 1, 0, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (32, N'Stocuri', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (33, N'Transferuri Gestiuni', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (34, N'Note Predare', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (35, N'Bonuri Consum', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (36, N'Dare in Folosinta Obiecte Inventar', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (37, N'Schimbare Preturi', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (38, N'Operatii Stocuri', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (39, N'Reglari Stocuri', 32, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (40, N'MIjloace Fixe', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (41, N'Imobilizari', 40, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (42, N'Amortizari', 40, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (43, N'Salarii', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (44, N'Fise Salariati', 43, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (45, N'State', 43, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (46, N'Configurare', 43, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (48, N'Rapoarte', 0, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (49, N'Balante Contabile', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (50, N'Fise Cont', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (51, N'Registru Jurnal', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (52, N'Registru Inventar', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (53, N'Jurnale Tva', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (54, N'Situatie Aprovizionari Vanzari', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (55, N'Situatie Parteneri', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (56, N'Fise de Magazie', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (57, N'Balanta Stocuri', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (58, N'Raport de Gestiune', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (59, N'Bonuri Consum', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (60, N'Obiecte de Inventar', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (61, N'Productie', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (62, N'Bilant', 48, 1, 1, 1)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (63, N'Contracte si Comenzi', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (64, N'Comenzi Terti', 63, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (65, N'Situatie Comenzi Terti', 63, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (66, N'Contracte Clienti', 63, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (67, N'Cheltuieli si Venituri Inreg in Avans', 63, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (68, N'Comenzi Transport', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (69, N'Comenzi', 68, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (70, N'Masini', 68, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (71, N'Soferi', 68, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (72, N'Parc Auto', 68, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (73, N'Garantii Soferi', 68, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (74, N'Setari si Administrare', 0, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (75, N'Firme', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (76, N'Setari Firma', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (77, N'Numerotare Documente', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (78, N'Schimbare Conturi si Articole', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (79, N'Perioada Fiscala', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (80, N'Verificari', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (81, N'Utilizatori Conectati', 74, 1, 1, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (82, N'Backup si Restore', 0, 1, 0, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (83, N'Backup', 82, 1, 0, 0)
GO

INSERT INTO [Firme].[dbo].[Module] ([Id], [Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
VALUES (84, N'Restore', 82, 1, 0, 0)
GO

Set Identity_Insert Module OFF
GO

/********************************************/
/*Update#20111001.sql*/
GO

IF Object_id('Firme.dbo.Version') IS NULL
Create Table[dbo].[Version] (
  [Id] int IDENTITY(1, 1) NOT NULL,
  [iVersion] int CONSTRAINT [DF_Version_iVersion] DEFAULT 20110101 NOT NULL,
  [iVersionC] int DEFAULT 20110101 NOT NULL,
  PRIMARY KEY CLUSTERED ([Id])
)
ON [PRIMARY]
GO

Select * From Firme..Version
If @@ROWCOUNT = 0
	Insert Into Firme..Version (iVersion) Values (20110101)
GO

/********************************************/
/*Update#20111108.sql*/
GO

If Not Exists (Select Id From Firme.Dbo.Module Where Modul = 'Situatie Contracte Clienti' And IdSuperior = 63)
	Begin
		Insert Into  [Firme].[dbo].[Module] ([Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
		Values (N'Situatie Contracte Clienti', 63, 1, 0, 0)
	End
GO

If Not Exists (Select Id From Firme.Dbo.Module Where Modul = 'Conturi La Banci Lei' And IdSuperior = 11)
	Begin
		Insert Into  [Firme].[dbo].[Module] ([Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
		Values (N'Conturi La Banci Lei', 11, 1, 0, 0)
	End
GO

If Not Exists (Select Id From Firme.Dbo.Module Where Modul = 'Conturi La Banci Valuta' And IdSuperior = 11)
	Begin
		Insert Into  [Firme].[dbo].[Module] ([Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
		Values (N'Conturi La Banci Valuta', 11, 1, 0, 0)
	End
GO

/********************************************/
/*Update#20111202.sql*/
GO

USE Firme
GO

If Exists(Select * From sys.sysindexes Where Name = 'soc_idx')
	Drop Index [soc_idx] On [dbo].[soc]
GO

CREATE NONCLUSTERED INDEX [soc_idx] ON [dbo].[soc]
  ([CodSoc])
WITH (
  PAD_INDEX = OFF,
  DROP_EXISTING = OFF,
  STATISTICS_NORECOMPUTE = OFF,
  SORT_IN_TEMPDB = OFF,
  ONLINE = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON)
GO

If Exists(Select * From sys.sysindexes Where Name = 'DrFirme_idx')
	Drop Index [DrFirme_idx] On [dbo].[DrFirme]
GO

CREATE NONCLUSTERED INDEX [DrFirme_idx] ON [dbo].[DrFirme]
  ([CodSoc])
WITH (
  PAD_INDEX = OFF,
  DROP_EXISTING = OFF,
  STATISTICS_NORECOMPUTE = OFF,
  SORT_IN_TEMPDB = OFF,
  ONLINE = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON)
GO

If Exists(Select * From sys.sysindexes Where Name = 'DrFirme_idx2')
	Drop Index [DrFirme_idx2] On [dbo].[DrFirme]
GO

CREATE NONCLUSTERED INDEX [DrFirme_idx2] ON [dbo].[DrFirme]
  ([IdUser])
WITH (
  PAD_INDEX = OFF,
  DROP_EXISTING = OFF,
  STATISTICS_NORECOMPUTE = OFF,
  SORT_IN_TEMPDB = OFF,
  ONLINE = OFF,
  ALLOW_ROW_LOCKS = ON,
  ALLOW_PAGE_LOCKS = ON)
GO

If Not Exists (Select Id From Firme.Dbo.Module Where Modul = 'Alerte' And IdSuperior = 74)
	Begin
		Insert Into  [Firme].[dbo].[Module] ([Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
		Values (N'Alerte', 74, 1, 1, 0)
	End
GO

/********************************************/

/*Update#20120111.sql*/
GO

If Not Exists (Select Id From Firme.Dbo.Module Where Modul = 'Cheltuieli Transport' And IdSuperior = 63)
	Begin
		Insert Into  [Firme].[dbo].[Module] ([Modul], [IdSuperior], [Administrator], [Utilizator], [Altii])
		Values (N'Cheltuieli Transport', 68, 1, 1, 0)
	End
GO

Delete From NoteFacturi 
Where Not Exists (
	Select IdUnic As IdNota From Intrari Where IsNull(Sters, 0) = 0  And IdUnic = NoteFacturi.IdNota Union All
    Select IdUnic As IdNota From Import  Where IsNull(Sters, 0) = 0  And IdUnic = NoteFacturi.IdNota Union All
    Select IdUnic As IdNota From ImportTransport  Where IsNull(Sters, 0) = 0 And IdUnic = NoteFacturi.IdNota Union All
    Select IdUnic As IsNota From Iesiri  Where IsNull(Anulat, 0) = 0 And IdUnic = NoteFacturi.IdNota Union All
    Select IdUnic As IdNota From Export  Where IsNull(Anulat, 0) = 0 And IdUnic = NoteFacturi.IdNota Union All
    Select Id_Nota As IdNota From Registru Where IsNull(Sters, 0) = 0 And Id_Nota = NoteFacturi.IdNota
)
GO

Delete From NoteFacturiValuta 
Where Not Exists (
	Select IdUnic As IdNota From Intrari Where IsNull(Sters, 0) = 0  And IdUnic = NoteFacturiValuta.IdNota Union All
    Select IdUnic As IdNota From Import  Where IsNull(Sters, 0) = 0  And IdUnic = NoteFacturiValuta.IdNota Union All
    Select IdUnic As IdNota From ImportTransport  Where IsNull(Sters, 0) = 0 And IdUnic = NoteFacturiValuta.IdNota Union All
    Select IdUnic As IsNota From Iesiri  Where IsNull(Anulat, 0) = 0 And IdUnic = NoteFacturiValuta.IdNota Union All
    Select IdUnic As IdNota From Export  Where IsNull(Anulat, 0) = 0 And IdUnic = NoteFacturiValuta.IdNota Union All
    Select Id_Nota As IdNota From Registru Where IsNull(Sters, 0) = 0 And Id_Nota = NoteFacturiValuta.IdNota
)
GO

/********************************************/

