
CREATE FUNCTION "year" (timestamp with time zone) RETURNS integer
AS '
  BEGIN
   RETURN date_part(''year'', $1);
  END;
'
LANGUAGE plpgsql;

CREATE FUNCTION "year" (timestamp without time zone) RETURNS integer
AS '
  BEGIN
   RETURN date_part(''year'', $1);
  END;
'
LANGUAGE plpgsql;

CREATE SEQUENCE "config_cfg_id_seq";

CREATE SEQUENCE "expenses_exp_id_seq";
ALTER TABLE bills ADD COLUMN "bill_footer" character varying(255);

CREATE TABLE "config" (
    "cfg_id" integer DEFAULT nextval('config_cfg_id_seq'::text) NOT NULL,
    "cfg_key" character varying(60) NOT NULL,
    "cfg_value" character varying(200) NOT NULL
);

CREATE UNIQUE INDEX config_pkey ON config USING btree (cfg_id);
CREATE UNIQUE INDEX cfg_key_config_index ON config USING btree (cfg_key);
CREATE INDEX cfg_id_config_index ON config USING btree (cfg_id);

CREATE TABLE "expenses" (
    "exp_id" bigint DEFAULT nextval('expenses_exp_id_seq'::text) NOT NULL,
    "pe_id" bigint NOT NULL,
    "prj_id" bigint NOT NULL,
    "exp_desc" text NOT NULL,
    "exp_sum" numeric(12,2) NOT NULL,
    "curr_id" smallint NOT NULL,
    "exp_sum_foreign" numeric(12,2),
    "curr_id_foreign" smallint,
    "exp_date" date NOT NULL,
    "exp_exchange_rate" numeric(12,5),
    "bill_id" bigint
);

CREATE UNIQUE INDEX expenses_pkey ON expenses USING btree (exp_id);
CREATE INDEX bill_id_expenses_index ON expenses USING btree (bill_id);
CREATE INDEX curr_id_expenses_index ON expenses USING btree (curr_id);
CREATE INDEX curr_id_foreign_expenses_index ON expenses USING btree (curr_id_foreign);
CREATE INDEX pe_id_expenses_index ON expenses USING btree (pe_id);
CREATE INDEX prj_id_expenses_index ON expenses USING btree (prj_id);
ALTER TABLE project_members ADD COLUMN "prjmem_hourlyrate" numeric(12,2);
ALTER TABLE projects ADD COLUMN "cpn_id_issuebill" bigint;
ALTER TABLE projects ADD COLUMN "prj_prj_id" bigint;
CREATE INDEX cpn_id_issuebill_projects_index ON projects USING btree (cpn_id_issuebill);
ALTER TABLE quotations ADD COLUMN "quot_footer" character varying(255);

CREATE TABLE "userconfig" (
    "cfg_id" integer NOT NULL,
    "usr_id" bigint NOT NULL,
    "cfg_value" character varying(200) NOT NULL
);

CREATE UNIQUE INDEX userconfig_pkey ON userconfig USING btree (cfg_id, usr_id);
CREATE INDEX cfg_id_userconfig_index ON userconfig USING btree (cfg_id);
CREATE INDEX usr_id_userconfig_index ON userconfig USING btree (usr_id);
ALTER TABLE billing_methods ADD CONSTRAINT fk_billing_methods_ctr_id FOREIGN KEY (ctr_id) REFERENCES countries(ctr_id) MATCH FULL;
ALTER TABLE bills ADD CONSTRAINT fk_bills_bmt_id FOREIGN KEY (bmt_id) REFERENCES billing_methods(bmt_id) MATCH FULL;
ALTER TABLE bills ADD CONSTRAINT fk_bills_curr_id FOREIGN KEY (curr_id) REFERENCES currencies(curr_id) MATCH FULL;
ALTER TABLE bills ADD CONSTRAINT fk_bills_cpn_id FOREIGN KEY (cpn_id) REFERENCES companies(cpn_id) MATCH FULL;
ALTER TABLE companies ADD CONSTRAINT fk_companies_ctr_id FOREIGN KEY (ctr_id) REFERENCES countries(ctr_id) MATCH FULL;
ALTER TABLE companies ADD CONSTRAINT fk_companies_ccat_id FOREIGN KEY (ccat_id) REFERENCES companycat(ccat_id) MATCH FULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_pe_id FOREIGN KEY (pe_id) REFERENCES persons(pe_id) MATCH FULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_prj_id FOREIGN KEY (prj_id) REFERENCES projects(prj_id) MATCH FULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_curr_id FOREIGN KEY (curr_id) REFERENCES currencies(curr_id) MATCH FULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_curr_id_foreign FOREIGN KEY (curr_id_foreign) REFERENCES currencies(curr_id) MATCH FULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_bill_id FOREIGN KEY (bill_id) REFERENCES bills(bill_id) MATCH FULL;
ALTER TABLE operations ADD CONSTRAINT fk_operations_pe_id FOREIGN KEY (pe_id) REFERENCES persons(pe_id) MATCH FULL;
ALTER TABLE operations ADD CONSTRAINT fk_operations_prj_id FOREIGN KEY (prj_id) REFERENCES projects(prj_id) MATCH FULL;
ALTER TABLE operations ADD CONSTRAINT fk_operations_opcat_id FOREIGN KEY (opcat_id) REFERENCES operationcat(opcat_id) MATCH FULL;
ALTER TABLE operations ADD CONSTRAINT fk_operations_bill_id FOREIGN KEY (bill_id) REFERENCES bills(bill_id) MATCH FULL;
ALTER TABLE persons ADD CONSTRAINT fk_persons_ctr_id FOREIGN KEY (ctr_id) REFERENCES countries(ctr_id) MATCH FULL;
ALTER TABLE persons ADD CONSTRAINT fk_persons_pecat_id FOREIGN KEY (pecat_id) REFERENCES personcat(pecat_id) MATCH FULL;
ALTER TABLE persons ADD CONSTRAINT fk_persons_cpn_id FOREIGN KEY (cpn_id) REFERENCES companies(cpn_id) MATCH FULL;
ALTER TABLE persons ADD CONSTRAINT fk_persons_jcat_id FOREIGN KEY (jcat_id) REFERENCES jobcat(jcat_id) MATCH FULL;
ALTER TABLE project_members ADD CONSTRAINT fk_project_members_prj_id FOREIGN KEY (prj_id) REFERENCES projects(prj_id) MATCH FULL;
ALTER TABLE project_members ADD CONSTRAINT fk_project_members_jcat_id FOREIGN KEY (jcat_id) REFERENCES jobcat(jcat_id) MATCH FULL;
ALTER TABLE project_members ADD CONSTRAINT fk_project_members_pe_id FOREIGN KEY (pe_id) REFERENCES persons(pe_id) MATCH FULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_cpn_id FOREIGN KEY (cpn_id) REFERENCES companies(cpn_id) MATCH FULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_jcat_id FOREIGN KEY (prjcat_id) REFERENCES projectcat(prjcat_id) MATCH FULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_prjstat_id FOREIGN KEY (prj_status) REFERENCES projectstatus(prjstat_id) MATCH FULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_cpn_id_issuebill FOREIGN KEY (cpn_id_issuebill) REFERENCES companies(cpn_id) MATCH FULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_prj_prj_id FOREIGN KEY (prj_prj_id) REFERENCES projects(prj_id) MATCH FULL;
ALTER TABLE quotations ADD CONSTRAINT fk_quotations_bmt_id FOREIGN KEY (bmt_id) REFERENCES billing_methods(bmt_id) MATCH FULL;
ALTER TABLE quotations ADD CONSTRAINT fk_quotations_curr_id FOREIGN KEY (curr_id) REFERENCES currencies(curr_id) MATCH FULL;
ALTER TABLE quotations ADD CONSTRAINT fk_quotations_cpn_id FOREIGN KEY (cpn_id) REFERENCES companies(cpn_id) MATCH FULL;
ALTER TABLE userconfig ADD CONSTRAINT fk_userconfig_cfg_id FOREIGN KEY (cfg_id) REFERENCES config(cfg_id) MATCH FULL;
ALTER TABLE userconfig ADD CONSTRAINT fk_userconfig_usr_id FOREIGN KEY (usr_id) REFERENCES users(usr_id) MATCH FULL;
ALTER TABLE users ADD CONSTRAINT fk_users_pe_id FOREIGN KEY (pe_id) REFERENCES persons(pe_id) MATCH FULL;
ALTER TABLE users ADD CONSTRAINT fk_users_lng_id FOREIGN KEY (lng_id) REFERENCES languages(lng_id) MATCH FULL;

INSERT INTO config (cfg_key, cfg_value) VALUES ('BILL_PDF_FOOTER', 'Payable within 30 days');
INSERT INTO config (cfg_key, cfg_value) VALUES ('BILL_PDF_HEADER', 'Invoice %s / %s');
INSERT INTO config (cfg_key, cfg_value) VALUES ('BILL_PDF_TOTALHOURS', 'Total number of hours billed: %s h between %s and %s');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DATEFORMAT_LONG', '%A, %d. %B %Y');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DATEFORMAT_SHORT', '%d.%m.%Y (%a)');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULTBMT', '3');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULTCOUNTRY', '1');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULTCURRENCY', '2');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULTLANGUAGE', '3');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULTPHOTO', 'nofoto.jpg');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DEFAULT_SKIN', 'decent');
INSERT INTO config (cfg_key, cfg_value) VALUES ('FINGRAPH_HEIGHT', '200');
INSERT INTO config (cfg_key, cfg_value) VALUES ('FINGRAPH_WIDTH', '420');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPHSHOWCPNNAMELENGHT', '25');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_HEIGHT', '250');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_OPCAT_ANGLE', '55');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_OPCAT_STYLE', '3');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_PDF_HEIGHT', '600');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_PDF_WIDTH', '1000');
INSERT INTO config (cfg_key, cfg_value) VALUES ('GRAPH_WIDTH', '600');
INSERT INTO config (cfg_key, cfg_value) VALUES ('IMAGETYPE', 'png');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_CITY', 'Milano');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_EMAIL', 'donkey@phpaga.sourceforge.net');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_FULLLOCATION', '99099 - Milano (MI), Italy');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_NAME', 'phpaga Enterprises');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_PHONE', 'Tel. +39 0000 00000000');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_STREET', 'Viale Camilleri 2002');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_COMPANY_TAXNR', 'P.IVA 00110022003');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_FOOTER', 'KINKY BANK di Milano, ABI 0123 - CAB 98976 - c/c 12340987');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_LOGO_FILE', '/data/webs/phpaga/logos/lanthaler-it.jpg');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LETTERHEAD_OWNER_PERSONALTAXNR', 'Cod. fisc. ABCZYX00A11B123Q');
INSERT INTO config (cfg_key, cfg_value) VALUES ('LOCATION_FORMAT', '%ZIP - %CITY (%REGION), %COUNTRY');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_HEIGHT', '120');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_SMALL_HEIGHT', '75');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_SMALL_WIDTH', '550');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_WIDTH', '600');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MIN_LOGIN_LEN', '1');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MIN_PASSWD_LEN', '2');
INSERT INTO config (cfg_key, cfg_value) VALUES ('OPDESC_OVERLIB', 'true');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDFSHOWBILLDESCLENGHT', '80');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDFSHOWOPDESCLENGHT', '120');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDFSHOWQUOTATIONDESCLENGHT', '80');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_BORDER_LEFT', '50');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_BORDER_TOP', '50');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_DOCFORMAT', 'a4');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_FONT_SIZE_HEADER', '14.0');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_FONT_SIZE_NORMAL', '12.0');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_FONT_SIZE_SMALL', '8.0');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_LOGOWIDTH', '150');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PDF_PRINT_LETTERHEAD_DEFAULT', 'true');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PENAME_FORMAT', '%LASTNAME, %FIRSTNAME');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_BACKGROUND', 'white');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_BAR', 'black');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_CLOSED', 'darkblue');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_HEADERBAR_BACKGROUND', '#14996e');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_HEADERBAR_DIVIDER', '#666666');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_HEADERBAR_TEXT', 'white');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_OPEN', 'darkgreen');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_OVERDUE', 'darkred');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_COLOR_UNDEFINED', 'white');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_FRAME', 'false');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_WIDTH', '800');
INSERT INTO config (cfg_key, cfg_value) VALUES ('PRHGRAPH_GANTT_WIDTH_HEADERBAR_DIVIDER', '2');
INSERT INTO config (cfg_key, cfg_value) VALUES ('QUOTATION_PDF_FOOTER', 'Expires 30 days from issue.');
INSERT INTO config (cfg_key, cfg_value) VALUES ('QUOTATION_PDF_HEADER', 'Quotation %s / %s');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SEPARATOR_DECIMALS', ',');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SEPARATOR_THOUSANDS', '.');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHORTLOCATION_FORMAT', '%CITY, %REGION');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWBPP', '19');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWCPNPP', '25');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWLASTOPERATIONS', '8');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWOPDESCLENGHT', '60');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWOPPP', '19');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWPEPP', '25');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWPRJPP', '25');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWUPP', '19');
INSERT INTO config (cfg_key, cfg_value) VALUES ('TEMPLATESET', 'phpaga');
INSERT INTO config (cfg_key, cfg_value) VALUES ('OPDESC_OVERLIB_BGCOLOR', '#000000');
INSERT INTO config (cfg_key, cfg_value) VALUES ('OPDESC_OVERLIB_FGCOLOR', '#ffcc00');
INSERT INTO config (cfg_key, cfg_value) VALUES ('OPDESC_OVERLIB_TEXTCOLOR', '#000000');
INSERT INTO config (cfg_key, cfg_value) VALUES ('OPDESC_OVERLIB_TEXTFONT', 'helvetica, sans-serif');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_WEEKLY_AVG_HOURS', '40');
INSERT INTO config (cfg_key, cfg_value) VALUES ('MHRSGRAPH_WEEKLY_AVG_LINECOLOR', '#888888');

