
-- ----------------------------------------
-- THIS FILE HAS BEEN GENERATED
-- DO NOT EDIT MANUALLY
-- ----------------------------------------


CREATE TABLE account_t (
  id serial not null primary key
  ,description varchar(128)     not null 
);

CREATE TABLE tenant_t (
  id serial not null primary key
  ,salutation varchar(128) 
  ,firstname varchar(128) 
  ,lastname varchar(128) 
  ,address1 varchar(128) 
  ,address2 varchar(128) 
  ,address3 varchar(128) 
  ,zip varchar(10) 
  ,city varchar(128) 
  ,phone1 varchar(64) 
  ,phone2 varchar(64) 
  ,iban varchar(64) 
  ,account integer     not null   references account_t (id) 
);

CREATE TABLE premise_t (
  id serial not null primary key
  ,description varchar(128) 
  ,street varchar(128)     not null 
  ,zip varchar(10)     not null 
  ,city varchar(128)     not null 
);

CREATE TABLE flat_t (
  id serial not null primary key
  ,description varchar(128) 
  ,premise integer   references premise_t (id) 
  ,area numeric(10,2)     not null 
  ,flat_no integer 
);

CREATE TABLE overhead_advance_t (
  id serial not null primary key
  ,description varchar(128) 
  ,amount numeric(10,4)     not null 
  ,startdate date 
  ,enddate date 
);

CREATE TABLE overhead_advance_flat_mapping_t (
  id serial not null primary key
  ,overhead_advance integer     not null   references overhead_advance_t (id) 
  ,flat integer     not null   references flat_t (id) 
);

CREATE TABLE parking_t (
  id serial not null primary key
  ,description varchar(128) 
  ,premise integer   references premise_t (id) 
);

CREATE TABLE commercial_premise_t (
  id serial not null primary key
  ,description varchar(128) 
  ,premise integer   references premise_t (id) 
);

CREATE TABLE tenancy_t (
  id serial not null primary key
  ,description varchar(128) 
  ,tenant integer     not null   references tenant_t (id) 
  ,flat integer   references flat_t (id) 
  ,parking integer   references parking_t (id) 
  ,commercial_premise integer   references commercial_premise_t (id) 
  ,startdate date     not null 
  ,enddate date 
  ,constraint tenancy_only_one_object check ((flat is not null and parking is null and commercial_premise is null) or (flat is null and parking is not null and commercial_premise is null) or (flat is null and parking is null and commercial_premise is not null))
);

CREATE TABLE fee_t (
  id serial not null primary key
  ,description varchar(128) 
  ,amount numeric(10,2)     not null 
  ,fee_type varchar(10)     not null 
  ,startdate date 
  ,enddate date 
  ,constraint fee_fee_type check (fee_type = 'per_area' or fee_type = 'total')
);

CREATE TABLE tenancy_fee_mapping_t (
  id serial not null primary key
  ,tenancy integer     not null   references tenancy_t (id) 
  ,fee integer     not null   references fee_t (id) 
);

CREATE TABLE account_entry_t (
  id serial not null primary key
  ,description varchar(128)     not null 
  ,account integer     not null   references account_t (id) 
  ,created_at timestamp     not null   default now() 
  ,amount numeric(10,2)     not null 
);



