# For acl support, phpgw_acl is being used. The appname
# will be the bookkeeping_id
# preceeded by 'bookkeeping_'. The location will be empty if the whole
# bookkeeping account should be used. Else it will contain a number,
# representing a category_id. The user from acl_account has permissions
# given in acl_right on the given category_id (or bookkeeping_id).

# bookkeeping_id 0 is reserved for general bookkeeping configuration.
CREATE TABLE phpgw_bk_configuration (
             bookkeeping_id     int(11) NOT NULL,
             name               varchar(255) NOT NULL,
             value              text
);
CREATE INDEX phpgw_bk_configuration_ndx ON
             phpgw_bk_configuration (bookkeeping_id);

# bookkeeping_id 0 is reserved for general bookkeeping.
CREATE TABLE phpgw_bk_bookkeeping (
             bookkeeping_id     int(11) NOT NULL auto_increment,
             name               varchar(255) NOT NULL,
             PRIMARY KEY (bookkeeping_id)
);

# Cash, bank.
CREATE TABLE phpgw_bk_account_types (
             account_type_id    int(11) NOT NULL auto_increment,
             description        varchar(255) NOT NULL,
             PRIMARY KEY (account_type_id)
);

# An account is 'place' where money is kept. For every currency
# a cash account is being made when necessary.
CREATE TABLE phpgw_bk_accounts (
             account_id         int(11) NOT NULL auto_increment,
             account_type_id    int(11) NOT NULL,
             bookkeeping_id     int(11) NOT NULL,
             name               varchar(255) NOT NULL,
             currency_id        int(11) NOT NULL,
             PRIMARY KEY (account_id)
);

# Cateories can be entered as number or selected by name. A user can
# select from the categories he has access to.
CREATE TABLE phpgw_bk_categories (
             category_id        int(11) NOT NULL auto_increment,
             parent_category_id int(11),
             bookkeeping_id     int(11) NOT NULL,
             number             int(11),
             name               varchar(255),
             description        text,
             PRIMARY KEY (category_id)
);

CREATE TABLE phpgw_bk_currencies (
             currency_id        int(11) NOT NULL auto_increment,
             iso_currency_code  varchar(4) NOT NULL,
             description        varchar(255),
             PRIMARY KEY (currency_id)
);

# The registration of financial transactions
CREATE TABLE phpgw_bk_transactions (
             transaction_id     int(11) NOT NULL auto_increment,
             bookkeeping_id     int(11) NOT NULL,
             date_transaction   int(11) NOT NULL,
             category_id        int(11) NOT NULL,
             description        text,
             amount             decimal(20,2) NOT NULL,
             currency_id        int(11) NOT NULL,
             phpgw_account_id   int(11) NOT NULL,
             account_id         int(11) NOT NULL,
             PRIMARY KEY (transaction_id)
);

CREATE TABLE phpgw_bk_declarations (
             declaration_id     int(11) NOT NULL auto_increment,
             bookkeeping_id     int(11) NOT NULL,
             date_declaration   int(11) NOT NULL,
             category_id        int(11) NOT NULL,
             description        text,
             amount             decimal(20,2) NOT NULL,
             currency_id        int(11) NOT NULL,
             phpgw_account_id   int(11) NOT NULL,
             PRIMARY KEY (declaration_id)
);

INSERT INTO phpgw_bk_configuration VALUES (0,'admins',NULL);
INSERT INTO phpgw_bk_account_types (description) VALUES ('cash');
INSERT INTO phpgw_bk_account_types (description) VALUES ('bank');
