-- DROP SCHEMA Agents;

CREATE SCHEMA Agents AUTHORIZATION user11;

-- DROP SEQUENCE agent_id_seq;

CREATE SEQUENCE agent_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE agentpriorityhistory_id_seq;

CREATE SEQUENCE agentpriorityhistory_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE agenttype_id_seq;

CREATE SEQUENCE agenttype_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE material_id_seq;

CREATE SEQUENCE material_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE materialcounthistory_id_seq;

CREATE SEQUENCE materialcounthistory_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE materialtype_id_seq;

CREATE SEQUENCE materialtype_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE product_id_seq;

CREATE SEQUENCE product_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE productcosthistory_id_seq;

CREATE SEQUENCE productcosthistory_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE productsale_id_seq;

CREATE SEQUENCE productsale_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE producttype_id_seq;

CREATE SEQUENCE producttype_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE shop_id_seq;

CREATE SEQUENCE shop_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;
-- DROP SEQUENCE supplier_id_seq;

CREATE SEQUENCE supplier_id_seq
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 2147483647
	START 1
	CACHE 1
	NO CYCLE;-- Agents.agenttype определение

-- Drop table

-- DROP TABLE agenttype;

CREATE TABLE agenttype (
	id serial4 NOT NULL,
	title varchar(50) NOT NULL,
	image varchar(100) NULL,
	CONSTRAINT agenttype_pkey PRIMARY KEY (id)
);


-- Agents.materialtype определение

-- Drop table

-- DROP TABLE materialtype;

CREATE TABLE materialtype (
	id serial4 NOT NULL,
	title varchar(50) NOT NULL,
	defectedpercent float8 NOT NULL,
	CONSTRAINT materialtype_pkey PRIMARY KEY (id)
);


-- Agents.producttype определение

-- Drop table

-- DROP TABLE producttype;

CREATE TABLE producttype (
	id serial4 NOT NULL,
	title varchar(50) NOT NULL,
	defectedpercent float8 NOT NULL,
	CONSTRAINT producttype_pkey PRIMARY KEY (id)
);


-- Agents.supplier определение

-- Drop table

-- DROP TABLE supplier;

CREATE TABLE supplier (
	id serial4 NOT NULL,
	title varchar(150) NOT NULL,
	inn varchar(12) NOT NULL,
	startdate date NOT NULL,
	qualityrating int4 NULL,
	suppliertype varchar(20) NULL,
	CONSTRAINT supplier_pkey PRIMARY KEY (id)
);


-- Agents.agent определение

-- Drop table

-- DROP TABLE agent;

CREATE TABLE agent (
	id serial4 NOT NULL,
	title varchar(150) NOT NULL,
	agenttypeid int4 NOT NULL,
	address varchar(300) NULL,
	inn varchar(12) NOT NULL,
	kpp varchar(9) NULL,
	directorname varchar(100) NULL,
	phone varchar(20) NOT NULL,
	email varchar(255) NULL,
	logo varchar(100) NULL,
	priority int4 NOT NULL,
	CONSTRAINT agent_pkey PRIMARY KEY (id),
	CONSTRAINT agent_agenttypeid_fkey FOREIGN KEY (agenttypeid) REFERENCES agenttype(id)
);


-- Agents.agentpriorityhistory определение

-- Drop table

-- DROP TABLE agentpriorityhistory;

CREATE TABLE agentpriorityhistory (
	id serial4 NOT NULL,
	agentid int4 NOT NULL,
	changedate timestamp(6) NOT NULL,
	priorityvalue int4 NOT NULL,
	CONSTRAINT agentpriorityhistory_pkey PRIMARY KEY (id),
	CONSTRAINT agentpriorityhistory_agentid_fkey FOREIGN KEY (agentid) REFERENCES agent(id)
);


-- Agents.material определение

-- Drop table

-- DROP TABLE material;

CREATE TABLE material (
	id serial4 NOT NULL,
	title varchar(100) NOT NULL,
	countinpack int4 NOT NULL,
	unit varchar(10) NOT NULL,
	countinstock float8 NULL,
	mincount float8 NOT NULL,
	description text NULL,
	cost numeric(10, 2) NOT NULL,
	image varchar(100) NULL,
	materialtypeid int4 NULL,
	CONSTRAINT material_pkey PRIMARY KEY (id),
	CONSTRAINT material_materialtypeid_fkey FOREIGN KEY (materialtypeid) REFERENCES materialtype(id)
);


-- Agents.materialcounthistory определение

-- Drop table

-- DROP TABLE materialcounthistory;

CREATE TABLE materialcounthistory (
	id serial4 NOT NULL,
	materialid int4 NOT NULL,
	changedate timestamp(6) NOT NULL,
	countvalue float8 NOT NULL,
	CONSTRAINT materialcounthistory_pkey PRIMARY KEY (id),
	CONSTRAINT materialcounthistory_materialid_fkey FOREIGN KEY (materialid) REFERENCES material(id)
);


-- Agents.materialsupplier определение

-- Drop table

-- DROP TABLE materialsupplier;

CREATE TABLE materialsupplier (
	materialid int4 NOT NULL,
	supplierid int4 NOT NULL,
	CONSTRAINT materialsupplier_pkey PRIMARY KEY (materialid, supplierid),
	CONSTRAINT fk_materialsupplier_material FOREIGN KEY (materialid) REFERENCES material(id),
	CONSTRAINT fk_materialsupplier_supplier FOREIGN KEY (supplierid) REFERENCES supplier(id)
);


-- Agents.product определение

-- Drop table

-- DROP TABLE product;

CREATE TABLE product (
	id serial4 NOT NULL,
	title varchar(100) NOT NULL,
	producttypeid int4 NULL,
	articlenumber varchar(10) NOT NULL,
	description text NULL,
	image varchar(100) NULL,
	productionpersoncount int4 NULL,
	productionworkshopnumber int4 NULL,
	mincostforagent numeric(10, 2) NOT NULL,
	CONSTRAINT product_pkey PRIMARY KEY (id),
	CONSTRAINT product_producttypeid_fkey FOREIGN KEY (producttypeid) REFERENCES producttype(id)
);


-- Agents.productcosthistory определение

-- Drop table

-- DROP TABLE productcosthistory;

CREATE TABLE productcosthistory (
	id serial4 NOT NULL,
	productid int4 NOT NULL,
	changedate timestamp(6) NOT NULL,
	costvalue numeric(10, 2) NOT NULL,
	CONSTRAINT productcosthistory_pkey PRIMARY KEY (id),
	CONSTRAINT productcosthistory_productid_fkey FOREIGN KEY (productid) REFERENCES product(id)
);


-- Agents.productmaterial определение

-- Drop table

-- DROP TABLE productmaterial;

CREATE TABLE productmaterial (
	productid int4 NOT NULL,
	materialid int4 NOT NULL,
	count float8 NULL,
	CONSTRAINT productmaterial_pkey PRIMARY KEY (productid, materialid),
	CONSTRAINT fk_productmaterial_material FOREIGN KEY (materialid) REFERENCES material(id),
	CONSTRAINT fk_productmaterial_product FOREIGN KEY (productid) REFERENCES product(id)
);


-- Agents.productsale определение

-- Drop table

-- DROP TABLE productsale;

CREATE TABLE productsale (
	id serial4 NOT NULL,
	agentid int4 NOT NULL,
	productid int4 NOT NULL,
	saledate date NOT NULL,
	productcount int4 NOT NULL,
	CONSTRAINT productsale_pkey PRIMARY KEY (id),
	CONSTRAINT productsale_agentid_fkey FOREIGN KEY (agentid) REFERENCES agent(id),
	CONSTRAINT productsale_productid_fkey FOREIGN KEY (productid) REFERENCES product(id)
);


-- Agents.shop определение

-- Drop table

-- DROP TABLE shop;

CREATE TABLE shop (
	id serial4 NOT NULL,
	title varchar(150) NOT NULL,
	address varchar(300) NULL,
	agentid int4 NOT NULL,
	CONSTRAINT shop_pkey PRIMARY KEY (id),
	CONSTRAINT shop_agentid_fkey FOREIGN KEY (agentid) REFERENCES agent(id)
);