-- Adminer 4.8.1 PostgreSQL 16.2 (Debian 16.2-1.pgdg120+2) dump

DROP TABLE IF EXISTS "banko_ataskaita";
DROP SEQUENCE IF EXISTS banko_ataskaita_banko_ataskaitaid_seq;
CREATE SEQUENCE banko_ataskaita_banko_ataskaitaid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."banko_ataskaita" (
    "banko_ataskaitaid" integer DEFAULT nextval('banko_ataskaita_banko_ataskaitaid_seq') NOT NULL,
    "filename" character varying,
    "body" text NOT NULL,
    "tssaved" timestamp,
    "usaved" character varying,
    "saskaitosnr" character varying,
    "type" character varying NOT NULL,
    CONSTRAINT "banko_ataskaita_pkey" PRIMARY KEY ("banko_ataskaitaid")
) WITH (oids = false);


DROP TABLE IF EXISTS "banko_ataskaita_eilute";
DROP SEQUENCE IF EXISTS banko_ataskaita_eilute_banko_ataskaita_eiluteid_seq;
CREATE SEQUENCE banko_ataskaita_eilute_banko_ataskaita_eiluteid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."banko_ataskaita_eilute" (
    "banko_ataskaita_eiluteid" integer DEFAULT nextval('banko_ataskaita_eilute_banko_ataskaita_eiluteid_seq') NOT NULL,
    "banko_ataskaitaid" integer NOT NULL,
    "data" date,
    "opturinys" text,
    "suma" double precision,
    "mokejimasid" integer,
    "source_type" character varying,
    CONSTRAINT "banko_ataskaita_eilute_pkey" PRIMARY KEY ("banko_ataskaita_eiluteid")
) WITH (oids = false);


DROP VIEW IF EXISTS "banko_ataskaita_view";
CREATE TABLE "banko_ataskaita_view" ("banko_ataskaitaid" integer, "saskaitosnr" character varying, "filename" character varying, "body" text, "tssaved" timestamp, "usaved" character varying, "type" character varying, "unmatched" bigint, "total" bigint);


DROP TABLE IF EXISTS "bukle";
DROP SEQUENCE IF EXISTS bukle_bukleid_seq;
CREATE SEQUENCE bukle_bukleid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."bukle" (
    "bukleid" integer DEFAULT nextval('bukle_bukleid_seq') NOT NULL,
    "bukle" integer NOT NULL,
    "sutartisid" integer NOT NULL,
    "data" date NOT NULL,
    "tscreated" timestamp DEFAULT now(),
    "ucreated" character varying,
    "comments" text,
    CONSTRAINT "bukle_pkey" PRIMARY KEY ("bukleid")
) WITH (oids = false);

CREATE INDEX "bukle_bukle" ON "public"."bukle" USING btree ("bukle");

CREATE INDEX "bukle_data" ON "public"."bukle" USING btree ("data");


DROP VIEW IF EXISTS "bukle_view";
CREATE TABLE "bukle_view" ("bukleid" integer, "bukle" integer, "sutartisid" integer, "data" date, "tscreated" timestamp, "ucreated" character varying, "comments" text, "year" double precision, "month" double precision);


DROP TABLE IF EXISTS "cfgiptvlist";
DROP SEQUENCE IF EXISTS cfgiptvlist_cfgiptvlistid_seq;
CREATE SEQUENCE cfgiptvlist_cfgiptvlistid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."cfgiptvlist" (
    "cfgiptvlistid" integer DEFAULT nextval('cfgiptvlist_cfgiptvlistid_seq') NOT NULL,
    "cfgiptvlist" text,
    CONSTRAINT "cfgiptvlist_pkey" PRIMARY KEY ("cfgiptvlistid")
) WITH (oids = false);


DROP TABLE IF EXISTS "client_event_log";
DROP SEQUENCE IF EXISTS client_event_log_client_event_logid_seq;
CREATE SEQUENCE client_event_log_client_event_logid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."client_event_log" (
    "client_event_logid" integer DEFAULT nextval('client_event_log_client_event_logid_seq') NOT NULL,
    "ip" character varying,
    "mac" character varying,
    "type" integer,
    "description" text,
    "ts" timestamp DEFAULT now(),
    CONSTRAINT "client_event_log_pkey" PRIMARY KEY ("client_event_logid")
) WITH (oids = false);


DROP VIEW IF EXISTS "cross_bank_payments";
CREATE TABLE "cross_bank_payments" ("kodas" character varying, "vardas" character varying, "pavarde" character varying, "mokejimasid" integer, "type" character varying, "source_type" character varying, "klientasid" integer, "data" date, "suma" double precision);


DROP TABLE IF EXISTS "email_history";
DROP SEQUENCE IF EXISTS email_history_email_historyid_seq;
CREATE SEQUENCE email_history_email_historyid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."email_history" (
    "email_historyid" integer DEFAULT nextval('email_history_email_historyid_seq') NOT NULL,
    "klientasid" integer NOT NULL,
    "tscreated" timestamp DEFAULT now() NOT NULL,
    "ucreated" text NOT NULL,
    "email" text NOT NULL,
    CONSTRAINT "email_history_pkey" PRIMARY KEY ("email_historyid")
) WITH (oids = false);


DROP VIEW IF EXISTS "email_history_view";
CREATE TABLE "email_history_view" ("klientasid" integer, "email_historyid" integer, "vardas" character varying, "pavarde" character varying, "email" text, "tscreated" timestamp, "ucreated" text);


DROP TABLE IF EXISTS "flat";
DROP SEQUENCE IF EXISTS flat_flatid_seq;
CREATE SEQUENCE flat_flatid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."flat" (
    "flatid" integer DEFAULT nextval('flat_flatid_seq') NOT NULL,
    "objectid" integer NOT NULL,
    "had_contact" integer DEFAULT '0' NOT NULL,
    "number" character varying,
    "entrance" integer,
    "providerid" integer,
    "monthly_price" double precision,
    "contract_end_date" date,
    "contact" character varying,
    "comments" character varying,
    "ts_edited" timestamp,
    "sutartisid" integer,
    "tv_providerid" integer,
    "ysnphoneline" boolean DEFAULT false NOT NULL,
    CONSTRAINT "flat_pkey" PRIMARY KEY ("flatid")
) WITH (oids = false);


DROP TABLE IF EXISTS "gatve";
DROP SEQUENCE IF EXISTS gatve_gatveid_seq;
CREATE SEQUENCE gatve_gatveid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."gatve" (
    "gatveid" integer DEFAULT nextval('gatve_gatveid_seq') NOT NULL,
    "miestasid" integer NOT NULL,
    "pav" character varying NOT NULL,
    CONSTRAINT "gatve_pkey" PRIMARY KEY ("gatveid")
) WITH (oids = false);


DROP TABLE IF EXISTS "ip_address";
DROP SEQUENCE IF EXISTS ip_address_ip_addressid_seq;
CREATE SEQUENCE ip_address_ip_addressid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."ip_address" (
    "ip_addressid" integer DEFAULT nextval('ip_address_ip_addressid_seq') NOT NULL,
    "ip_poolid" integer NOT NULL,
    "ip" character varying NOT NULL,
    "ip_sort_key" bigint NOT NULL,
    CONSTRAINT "ip_address_ip_uniq_idx" UNIQUE ("ip"),
    CONSTRAINT "ip_address_pkey" PRIMARY KEY ("ip_addressid")
) WITH (oids = false);


DROP TABLE IF EXISTS "ip_pool";
DROP SEQUENCE IF EXISTS ip_pool_ip_poolid_seq;
CREATE SEQUENCE ip_pool_ip_poolid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."ip_pool" (
    "ip_poolid" integer DEFAULT nextval('ip_pool_ip_poolid_seq') NOT NULL,
    "description" character varying,
    "network_interfaceid" integer,
    "dhcp_server_name" character varying NOT NULL,
    "lease_time" character varying NOT NULL,
    CONSTRAINT "ip_pool_pkey" PRIMARY KEY ("ip_poolid")
) WITH (oids = false);


DROP VIEW IF EXISTS "ip_pool_view";
CREATE TABLE "ip_pool_view" ("ip_poolid" integer, "description" character varying, "network_interfaceid" integer, "dhcp_server_name" character varying, "lease_time" character varying, "free_ips" bigint, "taken_ips" bigint);


DROP TABLE IF EXISTS "ipmaclist";
DROP SEQUENCE IF EXISTS ipmaclist_ipmaclistid_seq;
CREATE SEQUENCE ipmaclist_ipmaclistid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."ipmaclist" (
    "ipmaclistid" integer DEFAULT nextval('ipmaclist_ipmaclistid_seq') NOT NULL,
    "ipmaclist" text,
    CONSTRAINT "ipmaclist_pkey" PRIMARY KEY ("ipmaclistid")
) WITH (oids = false);


DROP TABLE IF EXISTS "klientas";
DROP SEQUENCE IF EXISTS klientas_klientasid_seq;
CREATE SEQUENCE klientas_klientasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."klientas" (
    "klientasid" integer DEFAULT nextval('klientas_klientasid_seq') NOT NULL,
    "kodas" character varying NOT NULL,
    "vardas" character varying NOT NULL,
    "pavarde" character varying NOT NULL,
    "rajonasid" integer,
    "miestasid" integer,
    "gatveid" integer,
    "namas" character varying,
    "mob_telefonas" character varying,
    "telefonas" character varying,
    "email" character varying,
    "tscreated" timestamp DEFAULT now(),
    "ucreated" character varying,
    "tipas" integer DEFAULT '0' NOT NULL,
    "im_kodas" character varying,
    "pvm_kodas" character varying,
    "wants_paper_invoice" integer DEFAULT '1' NOT NULL,
    "butas" character varying,
    "ak" character varying,
    "docnr" character varying,
    "ignore_debt_until" date,
    CONSTRAINT "klientas_kodas_uniq_idx" UNIQUE ("kodas"),
    CONSTRAINT "klientas_pkey" PRIMARY KEY ("klientasid")
) WITH (oids = false);


DROP TABLE IF EXISTS "klientas_contact";
DROP SEQUENCE IF EXISTS klientas_contact_klientas_contactid_seq;
CREATE SEQUENCE klientas_contact_klientas_contactid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."klientas_contact" (
    "klientas_contactid" integer DEFAULT nextval('klientas_contact_klientas_contactid_seq') NOT NULL,
    "klientasid" integer NOT NULL,
    "tssent" timestamp,
    "media" integer NOT NULL,
    "message" text NOT NULL,
    "sent_to" character varying,
    CONSTRAINT "klientas_contact_pkey" PRIMARY KEY ("klientas_contactid")
) WITH (oids = false);

CREATE INDEX "klientas_contact_klientasid" ON "public"."klientas_contact" USING btree ("klientasid");


DROP TABLE IF EXISTS "klientas_history";
DROP SEQUENCE IF EXISTS klientas_history_klientas_historyid_seq;
CREATE SEQUENCE klientas_history_klientas_historyid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."klientas_history" (
    "klientas_historyid" integer DEFAULT nextval('klientas_history_klientas_historyid_seq') NOT NULL,
    "klientasid" integer NOT NULL,
    "pdf" text,
    "tscreated" timestamp DEFAULT now() NOT NULL,
    "tsemailsent" timestamp,
    "info" text,
    CONSTRAINT "klientas_history_pkey" PRIMARY KEY ("klientas_historyid")
) WITH (oids = false);


DROP VIEW IF EXISTS "klientas_saskaita_view";
CREATE TABLE "klientas_saskaita_view" ("saskaitaid" integer, "sutartisid_deprecated" integer, "ext_source_dok_nr" character varying, "metai" integer, "menuo" integer, "suma_deprecated" double precision, "autogenerated" boolean, "source" character varying, "saskaita_html" text, "saskaita_pdf" text, "tsemailsent" timestamp, "saskaitanr" integer, "tsexported" timestamp, "serija" character varying, "tscreated" timestamp, "sort_key" integer, "date_created" date, "vat" double precision, "klientasid" integer, "suma" numeric, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "mob_telefonas" character varying, "telefonas" character varying, "email" character varying, "tipas" integer, "im_kodas" character varying, "pvm_kodas" character varying, "wants_paper_invoice" integer, "butas" character varying, "ak" character varying, "docnr" character varying, "ignore_debt_until" date);


DROP VIEW IF EXISTS "klientas_view";
CREATE TABLE "klientas_view" ("klientasid" integer, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "mob_telefonas" character varying, "telefonas" character varying, "email" character varying, "tscreated" timestamp, "ucreated" character varying, "tipas" integer, "im_kodas" character varying, "pvm_kodas" character varying, "wants_paper_invoice" integer, "butas" character varying, "ak" character varying, "docnr" character varying, "ignore_debt_until" date, "balansas" double precision);


DROP VIEW IF EXISTS "kliento_balansas";
CREATE TABLE "kliento_balansas" ("klientasid" integer, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "sask" double precision, "mok" double precision, "balansas" double precision);


DROP VIEW IF EXISTS "kliento_balansas_v2";
CREATE TABLE "kliento_balansas_v2" ("klientasid" integer, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "sask" double precision, "mok" double precision, "balansas" double precision);


DROP TABLE IF EXISTS "kliento_komp";
DROP SEQUENCE IF EXISTS kliento_komp_kliento_kompid_seq;
CREATE SEQUENCE kliento_komp_kliento_kompid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."kliento_komp" (
    "kliento_kompid" integer DEFAULT nextval('kliento_komp_kliento_kompid_seq') NOT NULL,
    "ts_registered" timestamp DEFAULT now() NOT NULL,
    "sutartisid" integer NOT NULL,
    "mac" character varying NOT NULL,
    "ip_addressid" integer NOT NULL,
    "comment" character varying,
    "type" integer NOT NULL,
    "in_debtor_list" boolean DEFAULT false NOT NULL,
    CONSTRAINT "kliento_komp_pkey" PRIMARY KEY ("kliento_kompid")
) WITH (oids = false);

CREATE INDEX "kliento_komp_ip_addressid" ON "public"."kliento_komp" USING btree ("ip_addressid");

CREATE INDEX "kliento_komp_sutartisid" ON "public"."kliento_komp" USING btree ("sutartisid");


DROP TABLE IF EXISTS "kliento_komp_log";
DROP SEQUENCE IF EXISTS kliento_komp_log_kliento_komp_logid_seq;
CREATE SEQUENCE kliento_komp_log_kliento_komp_logid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."kliento_komp_log" (
    "kliento_komp_logid" integer DEFAULT nextval('kliento_komp_log_kliento_komp_logid_seq') NOT NULL,
    "sutartisid" integer NOT NULL,
    "ip" character varying NOT NULL,
    "mac" character varying NOT NULL,
    "type" integer NOT NULL,
    "action" character varying NOT NULL,
    "ts" timestamp DEFAULT now() NOT NULL,
    CONSTRAINT "kliento_komp_log_pkey" PRIMARY KEY ("kliento_komp_logid")
) WITH (oids = false);


DROP VIEW IF EXISTS "kliento_komp_view";
CREATE TABLE "kliento_komp_view" ("kliento_kompid" integer, "ts_registered" timestamp, "sutartisid" integer, "mac" character varying, "ip_addressid" integer, "comment" character varying, "type" integer, "in_debtor_list" boolean, "ip" character varying, "ip_poolid" integer, "network_interfaceid" integer, "routerid" integer);


DROP TABLE IF EXISTS "log";
DROP SEQUENCE IF EXISTS log_logid_seq;
CREATE SEQUENCE log_logid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."log" (
    "logid" integer DEFAULT nextval('log_logid_seq') NOT NULL,
    "objectname" character varying NOT NULL,
    "objectid" character varying NOT NULL,
    "action" character varying NOT NULL,
    "field" character varying,
    "label" character varying,
    "value" character varying,
    "ucreated" character varying NOT NULL,
    "tscreated" timestamp DEFAULT now() NOT NULL,
    CONSTRAINT "log_pkey" PRIMARY KEY ("logid")
) WITH (oids = false);


DROP TABLE IF EXISTS "menesine_kaina";
DROP SEQUENCE IF EXISTS menesine_kaina_menesine_kainaid_seq;
CREATE SEQUENCE menesine_kaina_menesine_kainaid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."menesine_kaina" (
    "menesine_kainaid" integer DEFAULT nextval('menesine_kaina_menesine_kainaid_seq') NOT NULL,
    "metai" integer NOT NULL,
    "menuo" integer NOT NULL,
    "kaina" double precision NOT NULL,
    "sutartisid" integer NOT NULL,
    "comments" text,
    CONSTRAINT "menesine_kaina_pkey" PRIMARY KEY ("menesine_kainaid")
) WITH (oids = false);

CREATE INDEX "menesine_kaina_menuo" ON "public"."menesine_kaina" USING btree ("menuo");

CREATE INDEX "menesine_kaina_metai" ON "public"."menesine_kaina" USING btree ("metai");


DROP TABLE IF EXISTS "miestas";
DROP SEQUENCE IF EXISTS miestas_miestasid_seq;
CREATE SEQUENCE miestas_miestasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."miestas" (
    "miestasid" integer DEFAULT nextval('miestas_miestasid_seq') NOT NULL,
    "rajonasid" integer NOT NULL,
    "pav" character varying NOT NULL,
    CONSTRAINT "miestas_pkey" PRIMARY KEY ("miestasid")
) WITH (oids = false);


DROP TABLE IF EXISTS "mokejimas";
DROP SEQUENCE IF EXISTS mokejimas_mokejimasid_seq;
CREATE SEQUENCE mokejimas_mokejimasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."mokejimas" (
    "mokejimasid" integer DEFAULT nextval('mokejimas_mokejimasid_seq') NOT NULL,
    "upriskyre" character varying,
    "saltinis" character varying NOT NULL,
    "tspriskyre" character varying,
    "klientasid" integer NOT NULL,
    "opturinys" text,
    "data" date NOT NULL,
    "suma" double precision NOT NULL,
    "saskaitosnr" character varying,
    "ext_source_dok_nr" character varying,
    "tsexported" timestamp,
    CONSTRAINT "mokejimas_pkey" PRIMARY KEY ("mokejimasid")
) WITH (oids = false);

CREATE INDEX "mokejimas_data" ON "public"."mokejimas" USING btree ("data");

CREATE INDEX "mokejimas_klientasid" ON "public"."mokejimas" USING btree ("klientasid");


DROP TABLE IF EXISTS "net_inlet";
DROP SEQUENCE IF EXISTS net_inlet_net_inletid_seq;
CREATE SEQUENCE net_inlet_net_inletid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."net_inlet" (
    "net_inletid" integer DEFAULT nextval('net_inlet_net_inletid_seq') NOT NULL,
    "objectid" integer NOT NULL,
    "location" character varying NOT NULL,
    "type" integer NOT NULL,
    "net_inlet_tech_typeid" integer NOT NULL,
    CONSTRAINT "net_inlet_pkey" PRIMARY KEY ("net_inletid")
) WITH (oids = false);


DROP TABLE IF EXISTS "net_inlet_tech_type";
DROP SEQUENCE IF EXISTS net_inlet_tech_type_net_inlet_tech_typeid_seq;
CREATE SEQUENCE net_inlet_tech_type_net_inlet_tech_typeid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."net_inlet_tech_type" (
    "net_inlet_tech_typeid" integer DEFAULT nextval('net_inlet_tech_type_net_inlet_tech_typeid_seq') NOT NULL,
    "name" character varying NOT NULL,
    CONSTRAINT "net_inlet_tech_type_pkey" PRIMARY KEY ("net_inlet_tech_typeid")
) WITH (oids = false);


DROP TABLE IF EXISTS "network_interface";
DROP SEQUENCE IF EXISTS network_interface_network_interfaceid_seq;
CREATE SEQUENCE network_interface_network_interfaceid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."network_interface" (
    "network_interfaceid" integer DEFAULT nextval('network_interface_network_interfaceid_seq') NOT NULL,
    "name" character varying NOT NULL,
    "routerid" integer,
    "subnet" character varying NOT NULL,
    CONSTRAINT "network_interface_pkey" PRIMARY KEY ("network_interfaceid")
) WITH (oids = false);


DROP VIEW IF EXISTS "network_interface_view";
CREATE TABLE "network_interface_view" ("network_interfaceid" integer, "name" character varying, "routerid" integer, "subnet" character varying, "free_ips" bigint, "taken_ips" bigint);


DROP TABLE IF EXISTS "object";
DROP SEQUENCE IF EXISTS object_objectid_seq;
CREATE SEQUENCE object_objectid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."object" (
    "objectid" integer DEFAULT nextval('object_objectid_seq') NOT NULL,
    "rajonasid" integer NOT NULL,
    "miestasid" integer NOT NULL,
    "gatveid" integer NOT NULL,
    "namas" character varying NOT NULL,
    "supervisor_name" character varying,
    "supervisor_lastname" character varying,
    "supervisor_address" character varying,
    "supervisor_phone" character varying,
    "supervisor_email" character varying,
    "number_of_flats" integer DEFAULT '0' NOT NULL,
    "routerid" integer,
    "type" integer,
    "postindex" character varying,
    CONSTRAINT "object_pkey" PRIMARY KEY ("objectid")
) WITH (oids = false);


DROP VIEW IF EXISTS "object_stats_view";
CREATE TABLE "object_stats_view" ("objectid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "number_of_flats" integer, "type" integer, "total_flats" bigint, "net_flats" bigint, "rubinet_flats" bigint, "other_provider_flats" bigint, "known_flats" bigint, "explored_flats" bigint, "unknown_flats" bigint, "percent_of_net_flats" double precision, "percent_of_total_flats" double precision, "percent_net_flats" double precision, "percent_info_complete" double precision, "invoice_sum" double precision);


DROP TABLE IF EXISTS "object_vadybininkas";
DROP SEQUENCE IF EXISTS object_vadybininkas_object_vadybininkasid_seq;
CREATE SEQUENCE object_vadybininkas_object_vadybininkasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."object_vadybininkas" (
    "object_vadybininkasid" integer DEFAULT nextval('object_vadybininkas_object_vadybininkasid_seq') NOT NULL,
    "objectid" integer NOT NULL,
    "vadybininkasid" integer NOT NULL,
    CONSTRAINT "object_vadybininkas_pkey" PRIMARY KEY ("object_vadybininkasid")
) WITH (oids = false);


DROP TABLE IF EXISTS "power_inlet";
DROP SEQUENCE IF EXISTS power_inlet_power_inletid_seq;
CREATE SEQUENCE power_inlet_power_inletid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."power_inlet" (
    "power_inletid" integer DEFAULT nextval('power_inlet_power_inletid_seq') NOT NULL,
    "objectid" integer NOT NULL,
    "location" character varying NOT NULL,
    "power_inlet_typeid" integer NOT NULL,
    CONSTRAINT "power_inlet_pkey" PRIMARY KEY ("power_inletid")
) WITH (oids = false);


DROP TABLE IF EXISTS "power_inlet_type";
DROP SEQUENCE IF EXISTS power_inlet_type_power_inlet_typeid_seq;
CREATE SEQUENCE power_inlet_type_power_inlet_typeid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."power_inlet_type" (
    "power_inlet_typeid" integer DEFAULT nextval('power_inlet_type_power_inlet_typeid_seq') NOT NULL,
    "name" character varying NOT NULL,
    CONSTRAINT "power_inlet_type_pkey" PRIMARY KEY ("power_inlet_typeid")
) WITH (oids = false);


DROP TABLE IF EXISTS "provider";
DROP SEQUENCE IF EXISTS provider_providerid_seq;
CREATE SEQUENCE provider_providerid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."provider" (
    "providerid" integer DEFAULT nextval('provider_providerid_seq') NOT NULL,
    "name" character varying NOT NULL,
    CONSTRAINT "provider_pkey" PRIMARY KEY ("providerid")
) WITH (oids = false);


DROP TABLE IF EXISTS "rajonas";
DROP SEQUENCE IF EXISTS rajonas_rajonasid_seq;
CREATE SEQUENCE rajonas_rajonasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."rajonas" (
    "rajonasid" integer DEFAULT nextval('rajonas_rajonasid_seq') NOT NULL,
    "pav" character varying NOT NULL,
    CONSTRAINT "rajonas_pkey" PRIMARY KEY ("rajonasid")
) WITH (oids = false);


DROP TABLE IF EXISTS "rivile_saskaitos";
DROP SEQUENCE IF EXISTS rivile_saskaitos_rivile_saskaitosid_seq;
CREATE SEQUENCE rivile_saskaitos_rivile_saskaitosid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."rivile_saskaitos" (
    "rivile_saskaitosid" integer DEFAULT nextval('rivile_saskaitos_rivile_saskaitosid_seq') NOT NULL,
    "saskaitosnr" character varying(20),
    "saskaitoskodas" character varying,
    CONSTRAINT "rivile_saskaitos_pkey" PRIMARY KEY ("rivile_saskaitosid")
) WITH (oids = false);


DROP TABLE IF EXISTS "router";
DROP SEQUENCE IF EXISTS router_routerid_seq;
CREATE SEQUENCE router_routerid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."router" (
    "routerid" integer DEFAULT nextval('router_routerid_seq') NOT NULL,
    "name" character varying NOT NULL,
    "ip" character varying NOT NULL,
    "username" character varying NOT NULL,
    "password" character varying NOT NULL,
    "versionid" integer DEFAULT '7' NOT NULL,
    "port" integer DEFAULT '22' NOT NULL,
    CONSTRAINT "router_pkey" PRIMARY KEY ("routerid"),
    CONSTRAINT "router_uniq_up_idx" UNIQUE ("ip")
) WITH (oids = false);


DROP VIEW IF EXISTS "router_view";
CREATE TABLE "router_view" ("routerid" integer, "name" character varying, "ip" character varying, "username" character varying, "password" character varying, "versionid" integer, "port" integer, "free_ips" bigint, "taken_ips" bigint);


DROP TABLE IF EXISTS "saskaita";
DROP SEQUENCE IF EXISTS saskaita_saskaitaid_seq;
CREATE SEQUENCE saskaita_saskaitaid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."saskaita" (
    "saskaitaid" integer DEFAULT nextval('saskaita_saskaitaid_seq') NOT NULL,
    "sutartisid_deprecated" integer,
    "ext_source_dok_nr" character varying,
    "metai" integer NOT NULL,
    "menuo" integer NOT NULL,
    "suma_deprecated" double precision,
    "autogenerated" boolean,
    "source" character varying,
    "saskaita_html" text,
    "saskaita_pdf" text,
    "tsemailsent" timestamp,
    "saskaitanr" integer,
    "tsexported" timestamp,
    "serija" character varying,
    "tscreated" timestamp DEFAULT now(),
    "sort_key" integer,
    "date_created" date DEFAULT date(now()) NOT NULL,
    "vat" double precision NOT NULL,
    "klientasid" integer,
    "viena_saskaitaid" integer,
    CONSTRAINT "saskaita_pkey" PRIMARY KEY ("saskaitaid")
) WITH (oids = false);

CREATE INDEX "saskaita_klientasid" ON "public"."saskaita" USING btree ("klientasid");


DROP TABLE IF EXISTS "saskaita_eilute";
DROP SEQUENCE IF EXISTS saskaita_eilute_saskaita_eiluteid_seq;
CREATE SEQUENCE saskaita_eilute_saskaita_eiluteid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."saskaita_eilute" (
    "saskaita_eiluteid" integer DEFAULT nextval('saskaita_eilute_saskaita_eiluteid_seq') NOT NULL,
    "saskaitaid" integer NOT NULL,
    "sutartisid" integer NOT NULL,
    "suma" numeric,
    CONSTRAINT "saskaita_eilute_pkey" PRIMARY KEY ("saskaita_eiluteid")
) WITH (oids = false);

CREATE INDEX "saskaita_eilute_saskaitaid" ON "public"."saskaita_eilute" USING btree ("saskaitaid");

CREATE INDEX "saskaita_eilute_sutartisid" ON "public"."saskaita_eilute" USING btree ("sutartisid");


DROP VIEW IF EXISTS "saskaita_eilute_view";
CREATE TABLE "saskaita_eilute_view" ("saskaitaid" integer, "saskaita_eiluteid" integer, "sutartisid" integer, "suma" numeric, "sutartisid_deprecated" integer, "ext_source_dok_nr" character varying, "metai" integer, "menuo" integer, "suma_deprecated" double precision, "autogenerated" boolean, "source" character varying, "saskaita_html" text, "saskaita_pdf" text, "tsemailsent" timestamp, "saskaitanr" integer, "tsexported" timestamp, "serija" character varying, "tscreated" timestamp, "sort_key" integer, "date_created" date, "vat" double precision, "klientasid" integer);


DROP VIEW IF EXISTS "saskaita_view";
CREATE TABLE "saskaita_view" ("saskaitaid" integer, "sutartisid_deprecated" integer, "ext_source_dok_nr" character varying, "metai" integer, "menuo" integer, "suma_deprecated" double precision, "autogenerated" boolean, "source" character varying, "saskaita_html" text, "saskaita_pdf" text, "tsemailsent" timestamp, "saskaitanr" integer, "tsexported" timestamp, "serija" character varying, "tscreated" timestamp, "sort_key" integer, "date_created" date, "vat" double precision, "klientasid" integer, "suma" numeric, "viena_saskaitaid" integer, "in_viena_saskaita" boolean);


DROP TABLE IF EXISTS "skolininkas";
DROP SEQUENCE IF EXISTS skolininkas_skolininkasid_seq;
CREATE SEQUENCE skolininkas_skolininkasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."skolininkas" (
    "skolininkasid" integer DEFAULT nextval('skolininkas_skolininkasid_seq') NOT NULL,
    "klientasid" integer NOT NULL,
    "year" integer NOT NULL,
    "month" integer NOT NULL,
    "block" boolean DEFAULT false NOT NULL,
    CONSTRAINT "skolininkas_pkey" PRIMARY KEY ("skolininkasid")
) WITH (oids = false);


DROP VIEW IF EXISTS "skolininkas_list_view";
CREATE TABLE "skolininkas_list_view" ("klientasid" integer, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "mob_telefonas" character varying, "telefonas" character varying, "email" character varying, "tscreated" timestamp, "ucreated" character varying, "tipas" integer, "im_kodas" character varying, "pvm_kodas" character varying, "wants_paper_invoice" integer, "butas" character varying, "ak" character varying, "docnr" character varying, "ignore_debt_until" date, "year" integer, "month" integer, "skolininkasid" integer, "block" boolean);


DROP VIEW IF EXISTS "skolininkas_view";
CREATE TABLE "skolininkas_view" ("klientasid" integer, "kodas" character varying, "vardas" character varying, "pavarde" character varying, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "mob_telefonas" character varying, "telefonas" character varying, "email" character varying, "tscreated" timestamp, "ucreated" character varying, "tipas" integer, "im_kodas" character varying, "pvm_kodas" character varying, "wants_paper_invoice" integer, "butas" character varying, "ak" character varying, "docnr" character varying, "debt" double precision, "mokdata" date, "kontaktodata" timestamp, "kaina" double precision, "moksuma" double precision);


DROP TABLE IF EXISTS "sutartis";
DROP SEQUENCE IF EXISTS sutartis_sutartisid_seq;
CREATE SEQUENCE sutartis_sutartisid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."sutartis" (
    "sutartisid" integer DEFAULT nextval('sutartis_sutartisid_seq') NOT NULL,
    "sutartiesid" character varying NOT NULL,
    "klientasid" integer,
    "password" character varying NOT NULL,
    "vadybininkasid" integer,
    "rajonasid" integer,
    "miestasid" integer,
    "gatveid" integer,
    "namas" character varying,
    "data" date,
    "paslaugos_pradzia" date,
    "greitaveika_lt" double precision,
    "greitaveika_lt_vnt" integer DEFAULT '0' NOT NULL,
    "greitaveika_uzsienyje" double precision,
    "greitaveika_uzsienyje_vnt" integer DEFAULT '0' NOT NULL,
    "buhaltere_patvirtino" boolean DEFAULT false NOT NULL,
    "pajungimo_mokestis" double precision DEFAULT '0',
    "ts_vadyb_priskyrimas" timestamp,
    "u_vadyb_priskyrimas" character varying,
    "ts_p_prisijungimas" timestamp,
    "ts_buh_patvirtino" timestamp,
    "u_buh_patvirtino" character varying,
    "skola" double precision DEFAULT '0' NOT NULL,
    "menesine_kaina" double precision DEFAULT '0',
    "mac_address" character varying,
    "sutartis_typeid" integer,
    "max_computers" integer DEFAULT '1' NOT NULL,
    "butas" character varying,
    "filepath" character varying,
    CONSTRAINT "sutartis_pkey" PRIMARY KEY ("sutartisid"),
    CONSTRAINT "sutartis_sutartiesid_idx" UNIQUE ("sutartiesid")
) WITH (oids = false);


DROP VIEW IF EXISTS "sutartis_list_view";
CREATE TABLE "sutartis_list_view" ("sutartisid" integer, "sutartiesid" character varying, "klientasid" integer, "password" character varying, "vadybininkasid" integer, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "data" date, "paslaugos_pradzia" date, "greitaveika_lt" double precision, "greitaveika_lt_vnt" integer, "greitaveika_uzsienyje" double precision, "greitaveika_uzsienyje_vnt" integer, "buhaltere_patvirtino" boolean, "pajungimo_mokestis" double precision, "ts_vadyb_priskyrimas" timestamp, "u_vadyb_priskyrimas" character varying, "ts_p_prisijungimas" timestamp, "ts_buh_patvirtino" timestamp, "u_buh_patvirtino" character varying, "skola" double precision, "menesine_kaina" double precision, "mac_address" character varying, "sutartis_typeid" integer, "max_computers" integer, "butas" character varying, "filepath" character varying, "bukle" integer, "vardas" character varying, "pavarde" character varying, "mob_telefonas" character varying, "telefonas" character varying, "email" character varying, "kaina" double precision, "klientokodas" character varying, "ak" character varying);


DROP TABLE IF EXISTS "sutartis_type";
DROP SEQUENCE IF EXISTS sutartis_type_sutartis_typeid_seq;
CREATE SEQUENCE sutartis_type_sutartis_typeid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."sutartis_type" (
    "sutartis_typeid" integer DEFAULT nextval('sutartis_type_sutartis_typeid_seq') NOT NULL,
    "pav" character varying,
    CONSTRAINT "sutartis_type_pkey" PRIMARY KEY ("sutartis_typeid")
) WITH (oids = false);


DROP VIEW IF EXISTS "sutartis_view";
CREATE TABLE "sutartis_view" ("sutartisid" integer, "sutartiesid" character varying, "klientasid" integer, "password" character varying, "vadybininkasid" integer, "rajonasid" integer, "miestasid" integer, "gatveid" integer, "namas" character varying, "data" date, "paslaugos_pradzia" date, "greitaveika_lt" double precision, "greitaveika_lt_vnt" integer, "greitaveika_uzsienyje" double precision, "greitaveika_uzsienyje_vnt" integer, "buhaltere_patvirtino" boolean, "pajungimo_mokestis" double precision, "ts_vadyb_priskyrimas" timestamp, "u_vadyb_priskyrimas" character varying, "ts_p_prisijungimas" timestamp, "ts_buh_patvirtino" timestamp, "u_buh_patvirtino" character varying, "skola" double precision, "menesine_kaina" double precision, "mac_address" character varying, "sutartis_typeid" integer, "max_computers" integer, "butas" character varying, "filepath" character varying, "bukle" integer, "kaina" double precision);


DROP TABLE IF EXISTS "tv_provider";
DROP SEQUENCE IF EXISTS tv_provider_tv_providerid_seq;
CREATE SEQUENCE tv_provider_tv_providerid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."tv_provider" (
    "tv_providerid" integer DEFAULT nextval('tv_provider_tv_providerid_seq') NOT NULL,
    "name" character varying NOT NULL,
    CONSTRAINT "tv_provider_pkey" PRIMARY KEY ("tv_providerid")
) WITH (oids = false);


DROP TABLE IF EXISTS "vadybininkas";
DROP SEQUENCE IF EXISTS vadybininkas_vadybininkasid_seq;
CREATE SEQUENCE vadybininkas_vadybininkasid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."vadybininkas" (
    "vadybininkasid" integer DEFAULT nextval('vadybininkas_vadybininkasid_seq') NOT NULL,
    "vardas" character varying NOT NULL,
    "pavarde" character varying NOT NULL,
    "telefonas" character varying,
    "username" character varying,
    CONSTRAINT "vadybininkas_pkey" PRIMARY KEY ("vadybininkasid")
) WITH (oids = false);


DROP TABLE IF EXISTS "viena_saskaita";
DROP SEQUENCE IF EXISTS viena_saskaita_viena_saskaitaid_seq;
CREATE SEQUENCE viena_saskaita_viena_saskaitaid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."viena_saskaita" (
    "viena_saskaitaid" integer DEFAULT nextval('viena_saskaita_viena_saskaitaid_seq') NOT NULL,
    "tscreated" timestamp DEFAULT now() NOT NULL,
    "year" integer NOT NULL,
    "month" integer NOT NULL,
    "code_file" text NOT NULL,
    "result_xml" text,
    "unknown_codes_file" text,
    "ts_result_xml_emailed" timestamp,
    "ts_unknown_codes_file_emailed" timestamp,
    CONSTRAINT "viena_saskaita_pkey" PRIMARY KEY ("viena_saskaitaid")
) WITH (oids = false);


DROP TABLE IF EXISTS "viena_saskaita_row";
DROP SEQUENCE IF EXISTS viena_saskaita_row_viena_saskaita_rowid_seq;
CREATE SEQUENCE viena_saskaita_row_viena_saskaita_rowid_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;

CREATE TABLE "public"."viena_saskaita_row" (
    "viena_saskaita_rowid" integer DEFAULT nextval('viena_saskaita_row_viena_saskaita_rowid_seq') NOT NULL,
    "viena_saskaitaid" integer NOT NULL,
    "code" text NOT NULL,
    "klientasid" integer,
    CONSTRAINT "viena_saskaita_row_pkey" PRIMARY KEY ("viena_saskaita_rowid")
) WITH (oids = false);


DROP VIEW IF EXISTS "viena_saskaita_view";
CREATE TABLE "viena_saskaita_view" ("viena_saskaitaid" integer, "tscreated" timestamp, "year" integer, "month" integer, "code_file" text, "result_xml" text, "unknown_codes_file" text, "ts_result_xml_emailed" timestamp, "ts_unknown_codes_file_emailed" timestamp, "billcnt" bigint);


ALTER TABLE ONLY "public"."banko_ataskaita_eilute" ADD CONSTRAINT "banko_ataskaita_eilute_banko_ataskaita_fkey" FOREIGN KEY (banko_ataskaitaid) REFERENCES banko_ataskaita(banko_ataskaitaid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."banko_ataskaita_eilute" ADD CONSTRAINT "banko_ataskaita_mokejimas_fkey" FOREIGN KEY (mokejimasid) REFERENCES mokejimas(mokejimasid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;

ALTER TABLE ONLY "public"."bukle" ADD CONSTRAINT "bukle_sutartis_fkey" FOREIGN KEY (sutartisid) REFERENCES sutartis(sutartisid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."email_history" ADD CONSTRAINT "email_history_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."flat" ADD CONSTRAINT "flat_object_fkey" FOREIGN KEY (objectid) REFERENCES object(objectid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."flat" ADD CONSTRAINT "flat_provider_fkey" FOREIGN KEY (providerid) REFERENCES provider(providerid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;
ALTER TABLE ONLY "public"."flat" ADD CONSTRAINT "flat_sutartis_fkey" FOREIGN KEY (sutartisid) REFERENCES sutartis(sutartisid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;
ALTER TABLE ONLY "public"."flat" ADD CONSTRAINT "flat_tv_provider_fkey" FOREIGN KEY (tv_providerid) REFERENCES tv_provider(tv_providerid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;

ALTER TABLE ONLY "public"."gatve" ADD CONSTRAINT "gatve_miestas_fkey" FOREIGN KEY (miestasid) REFERENCES miestas(miestasid) NOT DEFERRABLE;

ALTER TABLE ONLY "public"."ip_address" ADD CONSTRAINT "io_address_ip_pool_fkey" FOREIGN KEY (ip_poolid) REFERENCES ip_pool(ip_poolid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."ip_pool" ADD CONSTRAINT "ip_pool_network_interface_fkey" FOREIGN KEY (network_interfaceid) REFERENCES network_interface(network_interfaceid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."klientas" ADD CONSTRAINT "klientas_miestas_fkey" FOREIGN KEY (miestasid) REFERENCES miestas(miestasid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;
ALTER TABLE ONLY "public"."klientas" ADD CONSTRAINT "klientas_rajonas_fkey" FOREIGN KEY (rajonasid) REFERENCES rajonas(rajonasid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;
ALTER TABLE ONLY "public"."klientas" ADD CONSTRAINT "klietnas_gatve_fkey" FOREIGN KEY (gatveid) REFERENCES gatve(gatveid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;

ALTER TABLE ONLY "public"."klientas_contact" ADD CONSTRAINT "klientas_contact_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."klientas_history" ADD CONSTRAINT "klientas_history_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."kliento_komp" ADD CONSTRAINT "kliento_komp_sutartis_fkey" FOREIGN KEY (sutartisid) REFERENCES sutartis(sutartisid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."kliento_komp" ADD CONSTRAINT "kliento_kompo_ip_addressid_fkey" FOREIGN KEY (ip_addressid) REFERENCES ip_address(ip_addressid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;

ALTER TABLE ONLY "public"."menesine_kaina" ADD CONSTRAINT "menesine_kaina_sutartis_fkey" FOREIGN KEY (sutartisid) REFERENCES sutartis(sutartisid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."miestas" ADD CONSTRAINT "miestas_rajonas_fkey" FOREIGN KEY (rajonasid) REFERENCES rajonas(rajonasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."mokejimas" ADD CONSTRAINT "mokejimas_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."net_inlet" ADD CONSTRAINT "net_inlet_net_inlet_tech_type_fkey" FOREIGN KEY (net_inlet_tech_typeid) REFERENCES net_inlet_tech_type(net_inlet_tech_typeid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."net_inlet" ADD CONSTRAINT "net_inlet_object_fkey" FOREIGN KEY (objectid) REFERENCES object(objectid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."network_interface" ADD CONSTRAINT "network_interface_router_fkey" FOREIGN KEY (routerid) REFERENCES router(routerid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."object" ADD CONSTRAINT "object_gatve_fkey" FOREIGN KEY (gatveid) REFERENCES gatve(gatveid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."object" ADD CONSTRAINT "object_miestas_fkey" FOREIGN KEY (miestasid) REFERENCES miestas(miestasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."object" ADD CONSTRAINT "object_rajonas_fkey" FOREIGN KEY (rajonasid) REFERENCES rajonas(rajonasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."object" ADD CONSTRAINT "object_router_fkey" FOREIGN KEY (routerid) REFERENCES router(routerid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;

ALTER TABLE ONLY "public"."object_vadybininkas" ADD CONSTRAINT "object_vadybininkas_object_fkey" FOREIGN KEY (objectid) REFERENCES object(objectid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."object_vadybininkas" ADD CONSTRAINT "object_vadybininkas_vadybininkas_fkey" FOREIGN KEY (vadybininkasid) REFERENCES vadybininkas(vadybininkasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."power_inlet" ADD CONSTRAINT "power_inlet_object_fkey" FOREIGN KEY (objectid) REFERENCES object(objectid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."power_inlet" ADD CONSTRAINT "power_inlet_power_inlet_type_fkey" FOREIGN KEY (power_inlet_typeid) REFERENCES power_inlet_type(power_inlet_typeid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."saskaita" ADD CONSTRAINT "saskaita_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."saskaita" ADD CONSTRAINT "saskaita_viena_saskaita_fkey" FOREIGN KEY (viena_saskaitaid) REFERENCES viena_saskaita(viena_saskaitaid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;

ALTER TABLE ONLY "public"."saskaita_eilute" ADD CONSTRAINT "saskaita_eilute_saskaita_fkey" FOREIGN KEY (saskaitaid) REFERENCES saskaita(saskaitaid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."saskaita_eilute" ADD CONSTRAINT "saskaita_eilute_sutartisid" FOREIGN KEY (sutartisid) REFERENCES sutartis(sutartisid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."skolininkas" ADD CONSTRAINT "skolininkas_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "klientas_miestas_fkey" FOREIGN KEY (miestasid) REFERENCES miestas(miestasid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;
ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "klientas_rajonas_fkey" FOREIGN KEY (rajonasid) REFERENCES rajonas(rajonasid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;
ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "klietnas_gatve_fkey" FOREIGN KEY (gatveid) REFERENCES gatve(gatveid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;
ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "sutartis_klientas_klientasid_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "sutartis_sutartis_type_fkey" FOREIGN KEY (sutartis_typeid) REFERENCES sutartis_type(sutartis_typeid) NOT DEFERRABLE;
ALTER TABLE ONLY "public"."sutartis" ADD CONSTRAINT "sutartis_vadybininkas_fkey" FOREIGN KEY (vadybininkasid) REFERENCES vadybininkas(vadybininkasid) ON UPDATE CASCADE ON DELETE RESTRICT NOT DEFERRABLE;

ALTER TABLE ONLY "public"."viena_saskaita_row" ADD CONSTRAINT "viena_saskaita_row_klientas_fkey" FOREIGN KEY (klientasid) REFERENCES klientas(klientasid) ON UPDATE CASCADE ON DELETE SET NULL NOT DEFERRABLE;
ALTER TABLE ONLY "public"."viena_saskaita_row" ADD CONSTRAINT "viena_saskaita_vs_fkey" FOREIGN KEY (viena_saskaitaid) REFERENCES viena_saskaita(viena_saskaitaid) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

DROP TABLE IF EXISTS "banko_ataskaita_view";
CREATE VIEW "banko_ataskaita_view" AS SELECT banko_ataskaita.banko_ataskaitaid,
    banko_ataskaita.saskaitosnr,
    banko_ataskaita.filename,
    banko_ataskaita.body,
    banko_ataskaita.tssaved,
    banko_ataskaita.usaved,
    banko_ataskaita.type,
    COALESCE(unmatched.count, (0)::bigint) AS unmatched,
    matched.count AS total
   FROM ((banko_ataskaita
     LEFT JOIN ( SELECT banko_ataskaita_eilute.banko_ataskaitaid,
            count(*) AS count
           FROM banko_ataskaita_eilute
          WHERE (banko_ataskaita_eilute.mokejimasid IS NULL)
          GROUP BY banko_ataskaita_eilute.banko_ataskaitaid) unmatched USING (banko_ataskaitaid))
     LEFT JOIN ( SELECT banko_ataskaita_eilute.banko_ataskaitaid,
            count(*) AS count
           FROM banko_ataskaita_eilute
          GROUP BY banko_ataskaita_eilute.banko_ataskaitaid) matched USING (banko_ataskaitaid));

DROP TABLE IF EXISTS "bukle_view";
CREATE VIEW "bukle_view" AS SELECT bukleid,
    bukle,
    sutartisid,
    data,
    tscreated,
    ucreated,
    comments,
    date_part('year'::text, data) AS year,
    date_part('month'::text, data) AS month
   FROM bukle;

DROP TABLE IF EXISTS "cross_bank_payments";
CREATE VIEW "cross_bank_payments" AS SELECT klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    mokejimas.mokejimasid,
    banko_ataskaita.type,
    banko_ataskaita_eilute.source_type,
    mokejimas.klientasid,
    mokejimas.data,
    mokejimas.suma
   FROM (((mokejimas
     JOIN klientas USING (klientasid))
     JOIN banko_ataskaita_eilute USING (mokejimasid))
     JOIN banko_ataskaita USING (banko_ataskaitaid))
  WHERE ((banko_ataskaita.type)::text <> (banko_ataskaita_eilute.source_type)::text);

DROP TABLE IF EXISTS "email_history_view";
CREATE VIEW "email_history_view" AS SELECT k.klientasid,
    eh.email_historyid,
    k.vardas,
    k.pavarde,
    eh.email,
    eh.tscreated,
    eh.ucreated
   FROM (klientas k
     JOIN email_history eh USING (klientasid));

DROP TABLE IF EXISTS "ip_pool_view";
CREATE VIEW "ip_pool_view" AS SELECT ip_pool.ip_poolid,
    ip_pool.description,
    ip_pool.network_interfaceid,
    ip_pool.dhcp_server_name,
    ip_pool.lease_time,
    COALESCE(a.free_ips, (0)::bigint) AS free_ips,
    COALESCE(b.taken_ips, (0)::bigint) AS taken_ips
   FROM ((ip_pool
     LEFT JOIN ( SELECT ip_address.ip_poolid,
            count(*) AS free_ips
           FROM ip_address
          WHERE (NOT (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid))))
          GROUP BY ip_address.ip_poolid) a USING (ip_poolid))
     LEFT JOIN ( SELECT ip_address.ip_poolid,
            count(*) AS taken_ips
           FROM ip_address
          WHERE (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid)))
          GROUP BY ip_address.ip_poolid) b USING (ip_poolid));

DROP TABLE IF EXISTS "klientas_saskaita_view";
CREATE VIEW "klientas_saskaita_view" AS SELECT saskaita_view.saskaitaid,
    saskaita_view.sutartisid_deprecated,
    saskaita_view.ext_source_dok_nr,
    saskaita_view.metai,
    saskaita_view.menuo,
    saskaita_view.suma_deprecated,
    saskaita_view.autogenerated,
    saskaita_view.source,
    saskaita_view.saskaita_html,
    saskaita_view.saskaita_pdf,
    saskaita_view.tsemailsent,
    saskaita_view.saskaitanr,
    saskaita_view.tsexported,
    saskaita_view.serija,
    saskaita_view.tscreated,
    saskaita_view.sort_key,
    saskaita_view.date_created,
    saskaita_view.vat,
    saskaita_view.klientasid,
    saskaita_view.suma,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    klientas.rajonasid,
    klientas.miestasid,
    klientas.gatveid,
    klientas.namas,
    klientas.mob_telefonas,
    klientas.telefonas,
    klientas.email,
    klientas.tipas,
    klientas.im_kodas,
    klientas.pvm_kodas,
    klientas.wants_paper_invoice,
    klientas.butas,
    klientas.ak,
    klientas.docnr,
    klientas.ignore_debt_until
   FROM (saskaita_view
     LEFT JOIN klientas USING (klientasid));

DROP TABLE IF EXISTS "klientas_view";
CREATE VIEW "klientas_view" AS SELECT klientas.klientasid,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    klientas.rajonasid,
    klientas.miestasid,
    klientas.gatveid,
    klientas.namas,
    klientas.mob_telefonas,
    klientas.telefonas,
    klientas.email,
    klientas.tscreated,
    klientas.ucreated,
    klientas.tipas,
    klientas.im_kodas,
    klientas.pvm_kodas,
    klientas.wants_paper_invoice,
    klientas.butas,
    klientas.ak,
    klientas.docnr,
    klientas.ignore_debt_until,
    kliento_balansas.balansas
   FROM (klientas
     JOIN kliento_balansas USING (klientasid));

DROP TABLE IF EXISTS "kliento_balansas";
CREATE VIEW "kliento_balansas" AS SELECT klientas.klientasid,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    COALESCE((saskaitos.sask)::double precision, (0)::double precision) AS sask,
    COALESCE(mokejimai.mok, (0)::double precision) AS mok,
    (COALESCE(mokejimai.mok, (0)::double precision) - COALESCE((saskaitos.sask)::double precision, (0)::double precision)) AS balansas
   FROM ((klientas
     LEFT JOIN ( SELECT sum(saskaita_eilute.suma) AS sask,
            saskaita.klientasid
           FROM (saskaita
             JOIN saskaita_eilute USING (saskaitaid))
          GROUP BY saskaita.klientasid) saskaitos USING (klientasid))
     LEFT JOIN ( SELECT sum(mokejimas.suma) AS mok,
            mokejimas.klientasid
           FROM mokejimas
          GROUP BY mokejimas.klientasid) mokejimai USING (klientasid));

DROP TABLE IF EXISTS "kliento_balansas_v2";
CREATE VIEW "kliento_balansas_v2" AS SELECT klientas.klientasid,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    COALESCE(((saskaitos.sask)::double precision / (100)::double precision), (0)::double precision) AS sask,
    COALESCE(((mokejimai.mok)::double precision / (100)::double precision), (0)::double precision) AS mok,
    ((COALESCE((mokejimai.mok)::double precision, (0)::double precision) - COALESCE((saskaitos.sask)::double precision, (0)::double precision)) / (100)::double precision) AS balansas
   FROM ((klientas
     LEFT JOIN ( SELECT (sum(((saskaita_eilute.suma)::double precision * (100)::double precision)))::integer AS sask,
            saskaita.klientasid
           FROM (saskaita
             JOIN saskaita_eilute USING (saskaitaid))
          GROUP BY saskaita.klientasid) saskaitos USING (klientasid))
     LEFT JOIN ( SELECT (sum((mokejimas.suma * (100)::double precision)))::integer AS mok,
            mokejimas.klientasid
           FROM mokejimas
          GROUP BY mokejimas.klientasid) mokejimai USING (klientasid));

DROP TABLE IF EXISTS "kliento_komp_view";
CREATE VIEW "kliento_komp_view" AS SELECT kliento_komp.kliento_kompid,
    kliento_komp.ts_registered,
    kliento_komp.sutartisid,
    kliento_komp.mac,
    kliento_komp.ip_addressid,
    kliento_komp.comment,
    kliento_komp.type,
    kliento_komp.in_debtor_list,
    ip_address.ip,
    ip_address.ip_poolid,
    ip_pool.network_interfaceid,
    network_interface.routerid
   FROM ((((kliento_komp
     JOIN ip_address USING (ip_addressid))
     LEFT JOIN ip_pool USING (ip_poolid))
     LEFT JOIN network_interface USING (network_interfaceid))
     LEFT JOIN router USING (routerid));

DROP TABLE IF EXISTS "network_interface_view";
CREATE VIEW "network_interface_view" AS SELECT network_interface.network_interfaceid,
    network_interface.name,
    network_interface.routerid,
    network_interface.subnet,
    COALESCE(a.free_ips, (0)::bigint) AS free_ips,
    COALESCE(b.taken_ips, (0)::bigint) AS taken_ips
   FROM ((network_interface
     LEFT JOIN ( SELECT ip_pool.network_interfaceid,
            count(*) AS free_ips
           FROM (ip_pool
             JOIN ip_address USING (ip_poolid))
          WHERE (NOT (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid))))
          GROUP BY ip_pool.network_interfaceid) a USING (network_interfaceid))
     LEFT JOIN ( SELECT ip_pool.network_interfaceid,
            count(*) AS taken_ips
           FROM (ip_pool
             JOIN ip_address USING (ip_poolid))
          WHERE (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid)))
          GROUP BY ip_pool.network_interfaceid) b USING (network_interfaceid));

DROP TABLE IF EXISTS "object_stats_view";
CREATE VIEW "object_stats_view" AS SELECT object.objectid,
    object.miestasid,
    object.gatveid,
    object.namas,
    object.number_of_flats,
    object.type,
    COALESCE(tft.total_flats, (0)::bigint) AS total_flats,
    COALESCE(nft.net_flats, (0)::bigint) AS net_flats,
    COALESCE(rft.rubinet_flats, (0)::bigint) AS rubinet_flats,
    COALESCE(opft.other_provider_flats, (0)::bigint) AS other_provider_flats,
    COALESCE(kft.known_flats, (0)::bigint) AS known_flats,
    (COALESCE(kft.known_flats, (0)::bigint) - COALESCE(rft.rubinet_flats, (0)::bigint)) AS explored_flats,
    ((COALESCE(tft.total_flats, (0)::bigint) + COALESCE(uf.uninhabited_flats, (0)::bigint)) - COALESCE(kft.known_flats, (0)::bigint)) AS unknown_flats,
        CASE
            WHEN (nft.net_flats > 0) THEN (((100 * COALESCE(rft.rubinet_flats, (0)::bigint)))::double precision / (nft.net_flats)::double precision)
            ELSE (0)::double precision
        END AS percent_of_net_flats,
        CASE
            WHEN (tft.total_flats > 0) THEN (((100 * COALESCE(rft.rubinet_flats, (0)::bigint)))::double precision / (tft.total_flats)::double precision)
            ELSE (0)::double precision
        END AS percent_of_total_flats,
        CASE
            WHEN (tft.total_flats > 0) THEN (((100 * COALESCE(nft.net_flats, (0)::bigint)))::double precision / (tft.total_flats)::double precision)
            ELSE (0)::double precision
        END AS percent_net_flats,
        CASE
            WHEN (tft.total_flats > 0) THEN (((100 * COALESCE(kft.known_flats, (0)::bigint)))::double precision / ((tft.total_flats)::double precision + (uf.uninhabited_flats)::double precision))
            ELSE (0)::double precision
        END AS percent_info_complete,
    COALESCE((isumt.invoice_sum)::double precision, (0)::double precision) AS invoice_sum
   FROM (((((((object
     LEFT JOIN ( SELECT count(*) AS total_flats,
            flat.objectid
           FROM flat
          WHERE ((flat.providerid <> '-2'::integer) OR (flat.providerid IS NULL))
          GROUP BY flat.objectid) tft USING (objectid))
     LEFT JOIN ( SELECT count(*) AS uninhabited_flats,
            flat.objectid
           FROM flat
          WHERE (flat.providerid = '-2'::integer)
          GROUP BY flat.objectid) uf USING (objectid))
     LEFT JOIN ( SELECT count(*) AS net_flats,
            flat.objectid
           FROM flat
          WHERE ((flat.providerid <> ALL (ARRAY['-2'::integer, '-1'::integer])) AND (flat.providerid IS NOT NULL))
          GROUP BY flat.objectid) nft USING (objectid))
     LEFT JOIN ( SELECT count(*) AS rubinet_flats,
            flat.objectid
           FROM flat
          WHERE (flat.providerid = '-3'::integer)
          GROUP BY flat.objectid) rft USING (objectid))
     LEFT JOIN ( SELECT count(*) AS other_provider_flats,
            flat.objectid
           FROM flat
          WHERE ((flat.providerid >= 0) AND (flat.providerid IS NOT NULL))
          GROUP BY flat.objectid) opft USING (objectid))
     LEFT JOIN ( SELECT count(*) AS known_flats,
            flat.objectid
           FROM flat
          WHERE (flat.providerid IS NOT NULL)
          GROUP BY flat.objectid) kft USING (objectid))
     LEFT JOIN ( SELECT sum(saskaita_eilute.suma) AS invoice_sum,
            sutartis.gatveid,
            sutartis.namas
           FROM ((saskaita
             JOIN saskaita_eilute USING (saskaitaid))
             JOIN sutartis USING (sutartisid))
          WHERE (((saskaita.metai)::double precision = date_part('year'::text, (date(now()) - '1 mon'::interval))) AND ((saskaita.menuo)::double precision = date_part('month'::text, (date(now()) - '1 mon'::interval))))
          GROUP BY sutartis.gatveid, sutartis.namas) isumt USING (gatveid, namas));

DROP TABLE IF EXISTS "router_view";
CREATE VIEW "router_view" AS SELECT router.routerid,
    router.name,
    router.ip,
    router.username,
    router.password,
    router.versionid,
    router.port,
    COALESCE(a.free_ips, (0)::bigint) AS free_ips,
    COALESCE(b.taken_ips, (0)::bigint) AS taken_ips
   FROM ((router
     LEFT JOIN ( SELECT network_interface.routerid,
            count(*) AS free_ips
           FROM ((network_interface
             JOIN ip_pool USING (network_interfaceid))
             JOIN ip_address USING (ip_poolid))
          WHERE (NOT (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid))))
          GROUP BY network_interface.routerid) a USING (routerid))
     LEFT JOIN ( SELECT network_interface.routerid,
            count(*) AS taken_ips
           FROM ((network_interface
             JOIN ip_pool USING (network_interfaceid))
             JOIN ip_address USING (ip_poolid))
          WHERE (EXISTS ( SELECT kliento_komp.kliento_kompid,
                    kliento_komp.ts_registered,
                    kliento_komp.sutartisid,
                    kliento_komp.mac,
                    kliento_komp.ip_addressid,
                    kliento_komp.comment,
                    kliento_komp.type
                   FROM kliento_komp
                  WHERE (kliento_komp.ip_addressid = ip_address.ip_addressid)))
          GROUP BY network_interface.routerid) b USING (routerid));

DROP TABLE IF EXISTS "saskaita_eilute_view";
CREATE VIEW "saskaita_eilute_view" AS SELECT saskaita_eilute.saskaitaid,
    saskaita_eilute.saskaita_eiluteid,
    saskaita_eilute.sutartisid,
    saskaita_eilute.suma,
    saskaita.sutartisid_deprecated,
    saskaita.ext_source_dok_nr,
    saskaita.metai,
    saskaita.menuo,
    saskaita.suma_deprecated,
    saskaita.autogenerated,
    saskaita.source,
    saskaita.saskaita_html,
    saskaita.saskaita_pdf,
    saskaita.tsemailsent,
    saskaita.saskaitanr,
    saskaita.tsexported,
    saskaita.serija,
    saskaita.tscreated,
    saskaita.sort_key,
    saskaita.date_created,
    saskaita.vat,
    saskaita.klientasid
   FROM (saskaita_eilute
     JOIN saskaita USING (saskaitaid));

DROP TABLE IF EXISTS "saskaita_view";
CREATE VIEW "saskaita_view" AS SELECT saskaita.saskaitaid,
    saskaita.sutartisid_deprecated,
    saskaita.ext_source_dok_nr,
    saskaita.metai,
    saskaita.menuo,
    saskaita.suma_deprecated,
    saskaita.autogenerated,
    saskaita.source,
    saskaita.saskaita_html,
    saskaita.saskaita_pdf,
    saskaita.tsemailsent,
    saskaita.saskaitanr,
    saskaita.tsexported,
    saskaita.serija,
    saskaita.tscreated,
    saskaita.sort_key,
    saskaita.date_created,
    saskaita.vat,
    saskaita.klientasid,
    tblsum.suma,
    saskaita.viena_saskaitaid,
        CASE
            WHEN (saskaita.viena_saskaitaid IS NOT NULL) THEN true
            ELSE false
        END AS in_viena_saskaita
   FROM (saskaita
     JOIN ( SELECT saskaita_eilute.saskaitaid,
            COALESCE(sum(saskaita_eilute.suma), (0)::numeric) AS suma
           FROM saskaita_eilute
          GROUP BY saskaita_eilute.saskaitaid) tblsum USING (saskaitaid));

DROP TABLE IF EXISTS "skolininkas_list_view";
CREATE VIEW "skolininkas_list_view" AS SELECT klientas.klientasid,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    klientas.rajonasid,
    klientas.miestasid,
    klientas.gatveid,
    klientas.namas,
    klientas.mob_telefonas,
    klientas.telefonas,
    klientas.email,
    klientas.tscreated,
    klientas.ucreated,
    klientas.tipas,
    klientas.im_kodas,
    klientas.pvm_kodas,
    klientas.wants_paper_invoice,
    klientas.butas,
    klientas.ak,
    klientas.docnr,
    klientas.ignore_debt_until,
    skolininkas.year,
    skolininkas.month,
    skolininkas.skolininkasid,
    skolininkas.block
   FROM (skolininkas
     JOIN klientas USING (klientasid));

DROP TABLE IF EXISTS "skolininkas_view";
CREATE VIEW "skolininkas_view" AS SELECT klientas.klientasid,
    klientas.kodas,
    klientas.vardas,
    klientas.pavarde,
    klientas.rajonasid,
    klientas.miestasid,
    klientas.gatveid,
    klientas.namas,
    klientas.mob_telefonas,
    klientas.telefonas,
    klientas.email,
    klientas.tscreated,
    klientas.ucreated,
    klientas.tipas,
    klientas.im_kodas,
    klientas.pvm_kodas,
    klientas.wants_paper_invoice,
    klientas.butas,
    klientas.ak,
    klientas.docnr,
    (COALESCE((saskaitos.sask)::double precision, (0)::double precision) - COALESCE(mokejimai.mok, (0)::double precision)) AS debt,
    maxmokdata.mokdata,
    kkc.kontaktodata,
    COALESCE(sv.kaina, (0)::double precision) AS kaina,
    maxmokdata.moksuma
   FROM (((((klientas
     LEFT JOIN ( SELECT sum(sutartis_view.kaina) AS kaina,
            sutartis_view.klientasid
           FROM sutartis_view
          WHERE (sutartis_view.bukle = ANY (ARRAY[2, 3]))
          GROUP BY sutartis_view.klientasid) sv USING (klientasid))
     LEFT JOIN ( SELECT sum(saskaita_eilute.suma) AS sask,
            saskaita.klientasid
           FROM (saskaita
             JOIN saskaita_eilute USING (saskaitaid))
          GROUP BY saskaita.klientasid) saskaitos USING (klientasid))
     LEFT JOIN ( SELECT sum(mokejimas.suma) AS mok,
            mokejimas.klientasid
           FROM mokejimas
          GROUP BY mokejimas.klientasid) mokejimai USING (klientasid))
     LEFT JOIN ( SELECT mokejimas.data AS mokdata,
            mokejimas.suma AS moksuma,
            mokejimas.klientasid
           FROM (mokejimas
             LEFT JOIN mokejimas tmp ON (((mokejimas.klientasid = tmp.klientasid) AND (mokejimas.data < tmp.data))))
          WHERE (tmp.mokejimasid IS NULL)
          GROUP BY mokejimas.klientasid, mokejimas.data, mokejimas.suma) maxmokdata USING (klientasid))
     LEFT JOIN ( SELECT max(klientas_contact.tssent) AS kontaktodata,
            klientas_contact.klientasid
           FROM klientas_contact
          GROUP BY klientas_contact.klientasid) kkc USING (klientasid))
  WHERE ((COALESCE((saskaitos.sask)::double precision, (0)::double precision) - COALESCE(mokejimai.mok, (0)::double precision)) > (0.009)::double precision)
  ORDER BY klientas.klientasid;

DROP TABLE IF EXISTS "sutartis_list_view";
CREATE VIEW "sutartis_list_view" AS SELECT sutartis.sutartisid,
    sutartis.sutartiesid,
    sutartis.klientasid,
    sutartis.password,
    sutartis.vadybininkasid,
    sutartis.rajonasid,
    sutartis.miestasid,
    sutartis.gatveid,
    sutartis.namas,
    sutartis.data,
    sutartis.paslaugos_pradzia,
    sutartis.greitaveika_lt,
    sutartis.greitaveika_lt_vnt,
    sutartis.greitaveika_uzsienyje,
    sutartis.greitaveika_uzsienyje_vnt,
    sutartis.buhaltere_patvirtino,
    sutartis.pajungimo_mokestis,
    sutartis.ts_vadyb_priskyrimas,
    sutartis.u_vadyb_priskyrimas,
    sutartis.ts_p_prisijungimas,
    sutartis.ts_buh_patvirtino,
    sutartis.u_buh_patvirtino,
    sutartis.skola,
    sutartis.menesine_kaina,
    sutartis.mac_address,
    sutartis.sutartis_typeid,
    sutartis.max_computers,
    sutartis.butas,
    sutartis.filepath,
    bukle.bukle,
    klientas.vardas,
    klientas.pavarde,
    klientas.mob_telefonas,
    klientas.telefonas,
    klientas.email,
    aa.kaina,
    klientas.kodas AS klientokodas,
    klientas.ak
   FROM (((sutartis
     LEFT JOIN klientas USING (klientasid))
     LEFT JOIN ( SELECT bukle_1.bukle,
            bukle_1.sutartisid
           FROM (bukle bukle_1
             LEFT JOIN bukle tmp ON (((bukle_1.sutartisid = tmp.sutartisid) AND (bukle_1.data < tmp.data) AND (tmp.data <= date(now())))))
          WHERE ((tmp.sutartisid IS NULL) AND (bukle_1.data <= date(now())))) bukle USING (sutartisid))
     LEFT JOIN ( SELECT max(menesine_kaina.kaina) AS kaina,
            menesine_kaina.sutartisid
           FROM (menesine_kaina
             LEFT JOIN menesine_kaina mtmp ON (((menesine_kaina.sutartisid = mtmp.sutartisid) AND (((menesine_kaina.metai * 12) + menesine_kaina.menuo) < ((mtmp.metai * 12) + mtmp.menuo)) AND ((((mtmp.metai)::double precision = date_part('year'::text, date(now()))) AND ((mtmp.menuo)::double precision <= date_part('month'::text, date(now())))) OR ((mtmp.metai)::double precision < date_part('year'::text, date(now())))))))
          WHERE (((((menesine_kaina.metai)::double precision = date_part('year'::text, date(now()))) AND ((menesine_kaina.menuo)::double precision <= date_part('month'::text, date(now())))) OR ((menesine_kaina.metai)::double precision < date_part('year'::text, date(now())))) AND (mtmp.sutartisid IS NULL))
          GROUP BY menesine_kaina.sutartisid) aa USING (sutartisid))
  ORDER BY sutartis.sutartisid;

DROP TABLE IF EXISTS "sutartis_view";
CREATE VIEW "sutartis_view" AS SELECT sutartis.sutartisid,
    sutartis.sutartiesid,
    sutartis.klientasid,
    sutartis.password,
    sutartis.vadybininkasid,
    sutartis.rajonasid,
    sutartis.miestasid,
    sutartis.gatveid,
    sutartis.namas,
    sutartis.data,
    sutartis.paslaugos_pradzia,
    sutartis.greitaveika_lt,
    sutartis.greitaveika_lt_vnt,
    sutartis.greitaveika_uzsienyje,
    sutartis.greitaveika_uzsienyje_vnt,
    sutartis.buhaltere_patvirtino,
    sutartis.pajungimo_mokestis,
    sutartis.ts_vadyb_priskyrimas,
    sutartis.u_vadyb_priskyrimas,
    sutartis.ts_p_prisijungimas,
    sutartis.ts_buh_patvirtino,
    sutartis.u_buh_patvirtino,
    sutartis.skola,
    sutartis.menesine_kaina,
    sutartis.mac_address,
    sutartis.sutartis_typeid,
    sutartis.max_computers,
    sutartis.butas,
    sutartis.filepath,
    COALESCE(a.bukle, 0) AS bukle,
    COALESCE(aa.kaina, (0)::double precision) AS kaina
   FROM ((sutartis
     LEFT JOIN ( SELECT max(bukle.bukle) AS bukle,
            bukle.sutartisid
           FROM (bukle
             LEFT JOIN bukle tmp ON (((bukle.sutartisid = tmp.sutartisid) AND (bukle.data < tmp.data) AND (tmp.data <= date(now())))))
          WHERE ((bukle.data <= date(now())) AND (tmp.bukleid IS NULL))
          GROUP BY bukle.sutartisid) a USING (sutartisid))
     LEFT JOIN ( SELECT max(m.kaina) AS kaina,
            m.sutartisid
           FROM (menesine_kaina m
             LEFT JOIN menesine_kaina tmp ON (((m.sutartisid = tmp.sutartisid) AND (((m.metai * 12) + m.menuo) < ((tmp.metai * 12) + tmp.menuo)) AND ((((tmp.metai)::double precision = date_part('year'::text, date(now()))) AND ((tmp.menuo)::double precision <= date_part('month'::text, date(now())))) OR ((tmp.metai)::double precision < date_part('year'::text, date(now())))))))
          WHERE (((((m.metai)::double precision = date_part('year'::text, date(now()))) AND ((m.menuo)::double precision <= date_part('month'::text, date(now())))) OR ((m.metai)::double precision < date_part('year'::text, date(now())))) AND (tmp.menesine_kainaid IS NULL))
          GROUP BY m.sutartisid) aa USING (sutartisid));

DROP TABLE IF EXISTS "viena_saskaita_view";
CREATE VIEW "viena_saskaita_view" AS SELECT viena_saskaita.viena_saskaitaid,
    viena_saskaita.tscreated,
    viena_saskaita.year,
    viena_saskaita.month,
    viena_saskaita.code_file,
    viena_saskaita.result_xml,
    viena_saskaita.unknown_codes_file,
    viena_saskaita.ts_result_xml_emailed,
    viena_saskaita.ts_unknown_codes_file_emailed,
    billcounter.billcnt
   FROM (viena_saskaita
     LEFT JOIN ( SELECT count(*) AS billcnt,
            saskaita.viena_saskaitaid
           FROM saskaita
          WHERE (saskaita.viena_saskaitaid IS NOT NULL)
          GROUP BY saskaita.viena_saskaitaid) billcounter USING (viena_saskaitaid));

-- 2025-11-10 13:15:45.076625+00
