﻿if not Exists(SELECT * FROM information_schema.tables where Table_name = 'Estate')
begin
	create table dbo.Estate
	(
		ID	int Primary key,
		Mat_ID	bigint references dbo.Matter(ID) not null,
		Code	int not null,
		Tarikh	int not null,
		Dom_ID_Type	int references dbo.domain(ID) not null,
		Dom_ID_Status	int references dbo.domain(ID) not null,
		Taf_ID	bigint references dbo.Tafsili(ID) null,
		Fname	nvarchar(200) null,
		Lname	nvarchar(200) null,
		Mobile	nvarchar(200) null,
		Tel	nvarchar(200) null,
		
		Dom_ID_Sale	int references dbo.domain(ID) not null, -- فروش
		Dom_ID_Mortgage	int references dbo.domain(ID) not null, -- رهن
		Dom_ID_Rent	int references dbo.domain(ID) not null, -- اجاره
		
		PriceSale	decimal(19,4),
		PriceMortgage	decimal(19,4),
		PriceRent	decimal(19,4),
		
		[Space]	int, -- مساحت
		Foundation	int, -- زیربنا
		Width	int, -- عرض
		CommericalMetrage	int, -- متراژ تجاری
		
		FloorTotalCount	int, -- تعداد کل طبقات
		FloorCount	int, -- طبقه
		UnitInFloorCount	int, -- تعداد واحد در طبقه
		SleepCount	int, -- تعداد خواب
		
		Pro_ID_View	int references dbo.otherProperties(ID) null, -- نما
		Pro_ID_Bottom	int references dbo.otherProperties(ID) null, -- کف
		Pro_ID_Cabinets	int references dbo.otherProperties(ID) null, -- کابینت
		Pro_ID_Ventilation	int references dbo.otherProperties(ID) null, -- تهویه
		Pro_ID_DocumentType	int references dbo.otherProperties(ID) null, -- نوع سند
		Pro_ID_EstateStatus	int references dbo.otherProperties(ID) null, -- وضعیت ملک
		Pro_ID_Direction	int references dbo.otherProperties(ID) null, -- جهت
		Pro_ID_FloorCovering	int references dbo.otherProperties(ID) null, -- کفپوش
		Pro_ID_KitchenService	int references dbo.otherProperties(ID) null, -- سرویس آشپزخانه
		Pro_ID_BuildingStatus	int references dbo.otherProperties(ID) null, -- وضعیت بنا
		BuildingAge	int , -- سن بنا
		
		Dom_ID_Store	int references dbo.domain(ID) not null, -- انباری
		Dom_ID_Parking	int references dbo.domain(ID) null, -- پارکینگ
		Dom_ID_Terrace	int references dbo.domain(ID) null, -- تراس
		StoreMeterage	float, -- متراژ انباری
		ParkingCount	int, -- تعداد پارکینگ
		TerraceMeterage	float, -- متراژ تراس
		
		Pro_ID_Water	int references dbo.otherProperties(ID) null, -- آب
		Pro_ID_Electricity	int references dbo.otherProperties(ID) null, -- برق
		Pro_ID_Gas	int references dbo.otherProperties(ID) null, -- گاز
		Pro_ID_Tel	int references dbo.otherProperties(ID) null, -- تلفن
		
		Pro_ID_City	int references dbo.otherProperties(ID) null, -- شهر
		Pro_ID_Area	int references dbo.otherProperties(ID) null, -- منظقه
		
		Address	nvarchar(1000) null,
		Description	nvarchar(1000) null
	)
end 
;
if not Exists(SELECT * FROM information_schema.tables where Table_name = 'EstateProperty')
begin
	create table dbo.EstateProperty
	(
		ID	int Primary key,
		Estate_ID	int references dbo.Estate(ID) not null,
		CSD_ID	int references dbo.ControlStructureDetail(ID) not null,
		Value	nvarchar(1000) null
	)
end
;
if not Exists(SELECT * FROM information_schema.tables where Table_name = 'EstateVisit')
begin
	create table dbo.EstateVisit
	(
		ID	int Primary key,
		Mat_ID	bigint references dbo.Matter(ID) not null,
		Estate_ID	int references dbo.Estate(ID) not null,
		Tarikh	int not null,
		Taf_ID	bigint references dbo.Tafsili(ID) null,
		Fname	nvarchar(200) null,
		Lname	nvarchar(200) null,
		Mobile	nvarchar(200) null,
		Tel	nvarchar(200) null,
		Dom_ID_Result	int references dbo.domain(ID) not null,
		Description	nvarchar(1000) null
	)
end
--drop table EstateProperty