IF NOT EXISTS (SELECT * FROM sys.objects WHERE type='U' and name = 'SIPARISLER') BEGIN
	CREATE TABLE SIPARISLER (
		SiparisId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
		Fk_CariId int NOT NULL,
		Fk_FaturaId int NOT NULL,
		CariKodu nvarchar(25) NOT NULL,
		PlasiyerKodu nvarchar(25) NOT NULL,
		PosNo smallint NOT NULL,
		TarihSaat datetime NOT NULL,
		AraToplam float NOT NULL,
		Iskonto1 float NOT NULL,
		Iskonto2 float NOT NULL,
		Iskonto3 float NOT NULL,
		Iskonto4 float NOT NULL,
		Iskonto5 float NOT NULL,
		Iskonto6 float NOT NULL,
		Vergi1 float NOT NULL,
		Vergi2 float NOT NULL,
		Vergi3 float NOT NULL,
		Vergi4 float NOT NULL,
		Vergi5 float NOT NULL,
		Vergi6 float NOT NULL,
		Vergi7 float NOT NULL,
		Vergi8 float NOT NULL,
		Vergi9 float NOT NULL,
		Vergi10 float NOT NULL,
		Meblag float NOT NULL,
		Synced bit NOT NULL,
		Vade int NOT NULL,
		Durum tinyint NOT NULL,
		Aciklama nvarchar(40) NULL,
		BelgeNo int NOT NULL,
		BasimSayisi smallint NOT NULL,
		DovizCinsi tinyint NOT NULL,
		DovizKuru float NOT NULL,
		PosHandle numeric(20, 0) NOT NULL,
		BelgeSeri nvarchar(10) NOT NULL,
		VergiNo nvarchar(15) NOT NULL,
		PlasiyerKodu2 nvarchar(25) NULL,
		SorumlulukMerkeziKodu nvarchar(25) NOT NULL,
		UUID nvarchar(50) NOT NULL,
		EvrakNoSeri nvarchar(20) NOT NULL,
		EvrakNoSira INT NOT NULL,
		SipTip INT NOT NULL,
	);

END

GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE type='U' and name = 'SIPARIS_DETAYLARI') BEGIN
	CREATE TABLE SIPARIS_DETAYLARI(
		SiparisDetayId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
		Fk_SiparisId int NOT NULL,
		Fk_StokId int NOT NULL,
		StokKodu nvarchar(25) NULL,
		SatirNo int NOT NULL,
		Miktar float NOT NULL,
		BirimFiyat float NOT NULL,
		Tutar float NOT NULL,
		Iskonto1 float NOT NULL,
		Iskonto2 float NOT NULL,
		Iskonto3 float NOT NULL,
		Iskonto4 float NOT NULL,
		Iskonto5 float NOT NULL,
		Iskonto6 float NOT NULL,
		Fk_VergiId int NOT NULL,
		Vergi float NOT NULL,
		Barkod nvarchar(25) NOT NULL,
		BarkodBirimi tinyint NOT NULL,
		BarkodFiyati float NOT NULL,
		BarkodKatsayisi float NOT NULL,
		BarkodMiktari float NOT NULL,
		Fk_OrjinalDovizCinsi tinyint NOT NULL,
		OrjinalDovizKuru tinyint NOT NULL
	);

END

GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE type='U' and name = 'SIPARIS_ODEMELERI') BEGIN
	CREATE TABLE SIPARIS_ODEMELERI(
		SiparisOdemeId int IDENTITY(1,1) NOT NULL,
		Fk_SiparisId int NOT NULL,
		Fk_OdemeTanimNo tinyint NOT NULL,
		Tutar float NOT NULL,
		ParaUstu float NOT NULL,
		CekNo nvarchar(12) NOT NULL,
		VadeTarihi datetime NOT NULL,
		Fk_BankaId int NOT NULL,
		BankaKodu nvarchar(25) NOT NULL,
		Kesideci nvarchar(30) NOT NULL
	);

END

GO

IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('FATURALAR') AND name='EvrakNoSira')
	ALTER TABLE FATURALAR ADD EvrakNoSira INT NOT NULL DEFAULT 0;
GO



IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('FATURALAR') AND name='Fk_SiparisId')
	ALTER TABLE FATURALAR ADD Fk_SiparisId INT NOT NULL DEFAULT 0;
GO

UPDATE FATURALAR SET EvrakNoSira = 0  WHERE EvrakNoSira IS NULL;
UPDATE FATURALAR SET Fk_SiparisId = 0  WHERE Fk_SiparisId IS NULL;
GO
