INSERT INTO config (cfg_key, cfg_value) VALUES ('DATEFORMAT_PRINT', 'd.m.Y');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWPRJTITLELENGHT', '60');
INSERT INTO config (cfg_key, cfg_value) VALUES ('SHOWOPCATTITLELENGHT', '60');
INSERT INTO config (cfg_key, cfg_value) VALUES ('VERSION_DB', '0.3');
INSERT INTO config (cfg_key, cfg_value) values ('MAIN_SHOW_PRJSTAT', '');
INSERT INTO config (cfg_key, cfg_value) values ('GRAPHVIZ_ENABLED', 'false');
INSERT INTO config (cfg_key, cfg_value) values ('FIN_UNBILLED_GRPBY', '');
INSERT INTO config (cfg_key, cfg_value) values ('MAIN_PRJTAB_SORT', '');

alter table companies add column cpn_address2 varchar(250);

alter table persons add column pe_address2 varchar(250);
alter table persons add column pe_hourlyrate decimal(12,2) default NULL;

alter table users add column usr_perm varchar(250);

alter table operations add op_billabletype int default 0;
alter table operations add column op_amount decimal(12,2);

alter table projects add column prj_deadline date default null;
alter table projects add column prj_planned_manhours bigint;
alter table projects add column prj_planned_cost_manhours decimal(12,2);
alter table projects add column prj_planned_cost_material decimal(12,2);
alter table projects add prj_billabletype int default 0;

-- Uncomment the following line if all of your projects are billable, othwise
-- set the status for each project from the projects interface
--
-- update projects set prj_billabletype=0 where prj_billabletype is null;

alter table persons change column pecat_id pecat_id tinyint default null;
alter table billing_methods change column ctr_id ctr_id bigint default null;

alter table operationcat add column opcat_color varchar(20);

update users set usr_perm='1|2|3|4|5|6|7|8|9|10|11|12|14|15|13|16' where usr_priv=1;

CREATE TABLE log (
    id          INT NOT NULL,
    logtime     TIMESTAMP NOT NULL,
    ident       CHAR(16) NOT NULL,
    priority    INT NOT NULL,
    message     blob,
    PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE lineitems (
	lit_id bigint NOT NULL,
	bill_id bigint,
	quot_id bigint,
        op_id bigint,
        lit_prodcode varchar(18),
	lit_desc blob,
	lit_qty varchar(10),
        lit_netprice decimal(12,2),
	Primary Key (lit_id)
) ENGINE=InnoDB;

CREATE TABLE lineitems_lit_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;

DELETE FROM lineitems_lit_id_seq;
INSERT INTO lineitems_lit_id_seq (id) SELECT max(lit_id) + 1 FROM lineitems;

alter table bills add column bill_payterm varchar(30);
alter table quotations add column quot_payterm varchar(30);
alter table projects add column prj_priority tinyint;

alter table bills change column bill_desc bill_desc blob default null;
alter table quotations change column quot_desc quot_desc blob default null;
alter table billing_methods add column bmt_status tinyint;

alter table projects add column quot_id bigint;
alter table projects add column prj_manager_pe_id bigint;

alter table companies add column cpn_defaultpayterm varchar(30);

INSERT INTO config (cfg_key, cfg_value) values ('FIN_FISCYEAR_MONTH', 1);
INSERT INTO config (cfg_key, cfg_value) values ('FIN_FISCYEAR_DAY', 1);

CREATE TABLE files_file_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;


CREATE TABLE files (
	file_id bigint NOT NULL,
        file_added timestamp,
	pe_id bigint NOT NULL,
        file_original_name text not null,
        file_local_name varchar(100) not null,
        file_rel_type bigint not null,
        file_rel_id bigint not null,
        file_mimetype varchar(30),
	Primary Key (file_id)
) ENGINE=InnoDB;

DELETE FROM files_file_id_seq;
INSERT INTO files_file_id_seq (id) SELECT max(file_id) + 1 FROM files;


INSERT INTO config (cfg_key, cfg_value) VALUES ('DATETIMEFORMAT_LONG', '%A, %d. %B %Y %R');
INSERT INTO config (cfg_key, cfg_value) VALUES ('DATETIMEFORMAT_SHORT', '%d.%m.%Y (%a) %R');


CREATE TABLE productcat_prcat_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;


CREATE TABLE productcat (
       prcat_id bigint NOT NULL,
       prcat_title varchar(100) NOT NULL,
       Primary Key (prcat_id),
       Unique key prcat_title (prcat_title)
) ENGINE=InnoDB;

DELETE FROM productcat_prcat_id_seq;
INSERT INTO productcat_prcat_id_seq (id) SELECT max(prcat_id) + 1 FROM productcat;

CREATE TABLE products_prod_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;


CREATE TABLE products (
       prod_id bigint NOT NULL,
       prod_code varchar(18) NOT NULL,
       prod_name varchar(100) NOT NULL,
       prod_price decimal(12,2),
       prod_consumption decimal(12,4),
       prod_stock bigint,
       prcat_id bigint,
       prod_created timestamp NOT NULL,
       prod_lastmod timestamp,
       Primary key (prod_id),
       Unique Key prod_code (prod_code)
) ENGINE=InnoDB;


DELETE FROM products_prod_id_seq;
INSERT INTO products_prod_id_seq (id) SELECT max(prod_id) + 1 FROM products;


CREATE TABLE material_mat_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;

CREATE TABLE material (
	mat_id bigint NOT NULL,
        mat_prodcode varchar(100),
        prod_id bigint,
        mat_name varchar(100),
	mat_desc text,
        mat_qty integer,
	mat_price decimal(12,2) NOT NULL,
	curr_id integer NOT NULL,
	mat_added timestamp NOT NULL,
	prj_id bigint NOT NULL,
	pe_id bigint NOT NULL,
	bill_id bigint,
	Primary Key (mat_id)
) ENGINE=InnoDB;

DELETE FROM material_mat_id_seq;
INSERT INTO material_mat_id_seq (id) SELECT max(mat_id) + 1 FROM material;

alter table lineitems add column mat_id bigint;



alter table operationcat add column opcat_hourlyrate decimal(12,2);

CREATE TABLE projects_hourlyrates_phr_id_seq (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB;


CREATE TABLE projects_hourlyrates (
	phr_id bigint NOT NULL,
        phr_lastmod timestamp NOT NULL,
	pe_id_lastmod bigint NOT NULL,
	prj_id bigint NOT NULL,
        pe_id bigint,
	opcat_id bigint NOT NULL,
        phr_hourlyrate numeric(12,2) NOT NULL,
	Primary Key (phr_id)
) ENGINE=InnoDB;

INSERT INTO config (cfg_key, cfg_value) values ('SORT_PROJECTS', 18);
INSERT INTO config (cfg_key, cfg_value) values ('SORT_OPERATIONS', 2);
INSERT INTO config (cfg_key, cfg_value) values ('SORT_COMPANIES', 51);
INSERT INTO config (cfg_key, cfg_value) values ('SORT_PERSONS', 31);
INSERT INTO config (cfg_key, cfg_value) values ('RECORDS_PERPAGE', 20);
INSERT INTO config (cfg_key, cfg_value) values ('SITENAME', 'phpaga');

UPDATE config SET cfg_value = '0.9' WHERE cfg_key = 'VERSION_DB';

