CREATE TABLE enapo_ppas_distribution_invoice (
	id varchar,
	preceding_id varchar NULL,
	customer_id varchar,
	customer_company_id varchar NULL,
	customer_name varchar NULL,
	customer_contract_account_id varchar NULL,
	customer_address_street varchar NULL,
	customer_address_house_number varchar NULL,
	customer_address_house_org_number varchar NULL,
	customer_address_city varchar NULL,
	customer_address_city_part varchar NULL,
	customer_address_post_code varchar NULL,
	customer_address_country varchar NULL,
	customer_address_ruian_id int8 NULL,
	facts_doc_date date NULL,
	facts_net_date date NULL,
	facts_billing_transaction varchar NULL,
	facts_to_pay_amount numeric NULL,
	facts_currency varchar NULL,
	facts_price_brutto numeric NULL,
	is_canceled bool NULL,
	canceled_reason varchar NULL,
	eic varchar NULL, -- installations_points_of_delivery 
	place_id varchar NULL, -- installations_installation
	installations_billing_class varchar NULL,
	installations_measurement_type varchar NULL,
	installations_contract_contract_id varchar NULL,
	installations_contract_move_in_date date NULL,
	installations_contract_move_out_date date NULL,
	installations_tdd_class varchar NULL,
	installations_address_street varchar NULL,
	installations_address_house_number varchar NULL,
	installations_address_house_org_number varchar NULL,
	installations_address_city varchar NULL,
	installations_address_city_part varchar NULL,
	installations_address_post_code varchar NULL,
	installations_address_country varchar NULL,
	installations_address_ruian_id int8 NULL,
	created_at timestamptz DEFAULT now() NULL,
	updated_at timestamptz NULL,
	CONSTRAINT enapo_ppas_distribution_invoice_pk PRIMARY KEY (id, customer_id)
);

CREATE TABLE enapo_ppas_distribution_invoice_device (
	device_serial_number varchar,	
	invoice_id varchar,	
	date_from date,
	date_to date,
	"type" varchar NULL,
	kind varchar,
	reading_type varchar,
	reading_from numeric NULL,
	reading_to numeric NULL,
	consumption numeric NULL,
	unit varchar NULL,
	meter_reading_type varchar NULL,
	gas_consumption_kwh numeric NULL,
	volume_coefficient float8 NULL,
	combustion_heat float8 NULL,
	normometers_nm3 float8 NULL,
	created_at timestamptz DEFAULT now() NULL,
	updated_at timestamptz NULL,
	CONSTRAINT enapo_ppas_distribution_invoice_device_pk PRIMARY KEY (device_serial_number, invoice_id, date_from, date_to, reading_type, kind) 
);

CREATE TABLE enapo_ppas_distribution_invoice_price (	
	invoice_id varchar,
	customer_id varchar,
	date_from date,
	date_to date,
	kind varchar,
	description varchar NULL,
	price_group varchar NULL,
	quantity numeric NULL,
	unit varchar NULL,
	unit_price numeric NULL,
	time_slot int4 NULL,
	price_netto numeric NULL,
	price_type varchar,
	currency varchar NULL,
	tax_rate numeric NULL,
	created_at timestamptz DEFAULT now() NULL,
	updated_at timestamptz NULL,
	CONSTRAINT enapo_ppas_distribution_invoice_price_pk PRIMARY KEY (invoice_id, customer_id, date_from, date_to, kind, price_type)
);

CREATE INDEX idx_ppas_invoice_eic ON enapo_ppas_distribution_invoice(eic);
CREATE INDEX idx_ppas_invoice_place_id ON enapo_ppas_distribution_invoice(place_id);