--
-- Database 'property_manager'

-- REVISIT <AP>: 20090511
--  Perhaps "Rent Master" instead of "Rent Manager" ?
--  Perhaps "Property Master" ?
--  Perhaps better to market under several names:
--    "Self Storage Master", "Apartment Master", etc.


-- AP:
-- Originally authored for MySQL.
-- However, requirements to consider / keep in mind:
--    PostgreSQL: Don't want to be tied to ONLY MySQL.
--    SQLite: Will likely require a standalone option.

-- REVISIT <AP>: 20090511
--  I would like timestamps to note when things change, but
--  I don't think we can rely on the magic of MySQL to update
--  them, since we may be required to use other engines.
--  At the moment, I'll keep the MySQL statements, but we
--  may have to move this logic into the application :-/


DROP DATABASE IF EXISTS `property_manager`;
CREATE DATABASE `property_manager`;
USE `property_manager`;


-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## SOFTWARE CONFIGURATION
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_config_system

DROP TABLE IF EXISTS `pmgr_config_system`;
CREATE TABLE `pmgr_config_system` (
  `config_version_id`    INT(10) UNSIGNED NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


LOCK TABLES `pmgr_config_system` WRITE;
INSERT INTO `pmgr_config_system` (`config_version_id`)
  VALUES
  (1);
UNLOCK TABLES;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_config_versions

DROP TABLE IF EXISTS `pmgr_config_versions`;
CREATE TABLE `pmgr_config_versions` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `schema`               INT UNSIGNED NOT NULL DEFAULT 1, -- DB REVISION
  `major`                INT UNSIGNED NOT NULL,
  `minor`                INT UNSIGNED NOT NULL,
  `bugfix`               INT UNSIGNED NOT NULL DEFAULT 0,

  -- REVISIT <AP>: 20090511
  --   Should we just use the 'notes' table instead?
  `comment`              VARCHAR(255) DEFAULT NULL,
  `stamp`                TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


LOCK TABLES `pmgr_config_versions` WRITE;
INSERT INTO `pmgr_config_versions` (`id`, `schema`, `major`, `minor`, `bugfix`, `stamp`, `comment`)
  VALUES
  (1, 1, 0, 1, 0, '2009-05-11 06:00', 'First revision');
UNLOCK TABLES;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_config_options

DROP TABLE IF EXISTS `pmgr_config_options`;
CREATE TABLE `pmgr_config_options` (
  `name`                 VARCHAR(50)  NOT NULL,
  `value`                VARCHAR(255) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;




-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## CONTACTS (PEOPLE and COMPANIES)
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contacts

DROP TABLE IF EXISTS `pmgr_contacts`;
CREATE TABLE `pmgr_contacts` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `first_name`           VARCHAR(255) DEFAULT NULL,
  `middle_name`          VARCHAR(255) DEFAULT NULL,
  `last_name`            VARCHAR(255) DEFAULT NULL,
  `company_name`         VARCHAR(255) DEFAULT NULL,
  `display_name`         VARCHAR(512) DEFAULT NULL,

  -- FEDERAL ID, e.g. SSN or EIN
  `id_federal`           VARCHAR(16)  DEFAULT NULL,

  -- LOCAL ID, e.g. drivers license
  `id_local`             VARCHAR(16)  DEFAULT NULL,
  `id_local_state`       CHAR(2)      DEFAULT NULL,
  `id_local_exp`         DATE         DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contact_addresses

DROP TABLE IF EXISTS `pmgr_contact_addresses`;
CREATE TABLE `pmgr_contact_addresses` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `address`              VARCHAR(255) DEFAULT NULL,
  `city`                 VARCHAR(255) DEFAULT NULL,
  `state`                CHAR(2)      DEFAULT NULL, -- REVISIT <AP>: Convert to ENUM
  `postcode`             VARCHAR(12)  DEFAULT NULL,
  `country`              VARCHAR(128) DEFAULT NULL, -- REVISIT <AP>: Convert to ENUM
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contact_phones

DROP TABLE IF EXISTS `pmgr_contact_phones`;
CREATE TABLE `pmgr_contact_phones` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `type`                 ENUM('LANDLINE',
                              'MOBILE',
                              'VIRTUAL',
                              'PAGER',
                              'FAX')
                           NOT NULL DEFAULT 'LANDLINE',
  `phone`                VARCHAR(18)  NOT NULL,
  `ext`                  VARCHAR(6)   DEFAULT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  UNIQUE KEY `number_key` (`phone`, `ext`),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contact_emails

DROP TABLE IF EXISTS `pmgr_contact_emails`;
CREATE TABLE `pmgr_contact_emails` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `email`                VARCHAR(128) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  UNIQUE KEY `email_key` (`email`),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contacts_methods

DROP TABLE IF EXISTS `pmgr_contacts_methods`;
CREATE TABLE `pmgr_contacts_methods` (
  `contact_id`           INT(10) UNSIGNED NOT NULL,
  `method`               ENUM('ADDRESS',
                              'PHONE',
                              'EMAIL')
                           NOT NULL,
  `method_id`            INT(10) UNSIGNED NOT NULL,
  `type`                 ENUM('MAIN',
                              'HOME',
                              'BUSINESS',
                              'OTHER')
                           NOT NULL DEFAULT 'MAIN',
  `preference`           ENUM('PRIMARY',
                              'WORK',
                              'ALTERNATE',
                              'EMERGENCY')
                           NOT NULL DEFAULT 'PRIMARY',
--  `position`   TINYINT UNSIGNED NOT NULL DEFAULT 1,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`contact_id`, `method`, `method_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## GROUPS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_groups

DROP TABLE IF EXISTS `pmgr_groups`;
CREATE TABLE `pmgr_groups` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  -- REVISIT <AP>: 20090511
  --   code may not be userful
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_group_options

DROP TABLE IF EXISTS `pmgr_group_options`;
CREATE TABLE `pmgr_group_options` (
  `group_id`             INT(10) UNSIGNED NOT NULL,
  `name`                 VARCHAR(50)  NOT NULL,
  `value`                VARCHAR(255) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`group_id`, `name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_group_permissions

DROP TABLE IF EXISTS `pmgr_group_permissions`;
CREATE TABLE `pmgr_group_permissions` (
  `group_id`             INT(10) UNSIGNED NOT NULL,
  `name`                 CHAR(30) NOT NULL,
  `access`               ENUM('ALLOWED',
                              'DENIED',
                              'FORCED')
                           NOT NULL DEFAULT 'ALLOWED',
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`group_id`, `name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## USERS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_users

DROP TABLE IF EXISTS `pmgr_users`;
CREATE TABLE `pmgr_users` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"

  -- Login details.  Passwords are not yet used (and so NULL).
  `login`                VARCHAR(30)  NOT NULL,
  `salt`                 CHAR(12)     DEFAULT NULL,
  `passhash`             VARCHAR(255) DEFAULT NULL,

  -- Contact information for this user
  `contact_id`           INT(10) UNSIGNED NOT NULL,

  -- Specific comments
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_user_options

DROP TABLE IF EXISTS `pmgr_user_options`;
CREATE TABLE `pmgr_user_options` (
  `user_id`              INT(10) UNSIGNED NOT NULL,
  `name`                 VARCHAR(50)  NOT NULL,
  `value`                VARCHAR(255) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`user_id`, `name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## SITES
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_sites

DROP TABLE IF EXISTS `pmgr_sites`;
CREATE TABLE `pmgr_sites` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  UNIQUE KEY `site_code` (`code`),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_site_options

DROP TABLE IF EXISTS `pmgr_site_options`;
CREATE TABLE `pmgr_site_options` (
  `site_id`              INT(10) UNSIGNED NOT NULL,
  `name`                 VARCHAR(50)  NOT NULL,
  `value`                VARCHAR(255) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`site_id`, `name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_site_memberships
--
-- Which users are allowed to access which sites,
-- and under which set of group permissions (possibly multiple)

-- SELECT U.id, P.name, MAX(P.access)
--  FROM pmgr_users U
--    LEFT JOIN pmgr_site_membership   M ON M.user_id = U.id
--    LEFT JOIN pmgr_groups            G ON G.id = M.group_id
--    LEFT JOIN pmgr_group_permissions P ON P.group_id = G.id
-- GROUP BY U.id, P.name

DROP TABLE IF EXISTS `pmgr_site_memberships`;
CREATE TABLE `pmgr_site_memberships` (
  `site_id`              INT(10) UNSIGNED NOT NULL,
  `user_id`              INT(10) UNSIGNED NOT NULL,
  `group_id`             INT(10) UNSIGNED NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`site_id`, `user_id`, `group_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_site_areas

DROP TABLE IF EXISTS `pmgr_site_areas`;
CREATE TABLE `pmgr_site_areas` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `site_id`              INT(10) UNSIGNED NOT NULL,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## UNITS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_units


DROP TABLE IF EXISTS `pmgr_units`;
CREATE TABLE `pmgr_units` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `unit_size_id`         INT(10) UNSIGNED NOT NULL,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,

  `status`               ENUM('DELETED',
                              'DAMAGED',
                              'COMPANY',
                              'UNAVAILABLE',
                              'RESERVED',
                              'DIRTY',
                              'VACANT',
                              'OCCUPIED',
                              'LATE', -- NOT SURE
                              'LOCKED',
                              'LIENED')
                           NOT NULL DEFAULT 'VACANT',

  `current_lease_id`     INT(10) UNSIGNED DEFAULT NULL,

  `sort_order`           MEDIUMINT UNSIGNED NOT NULL,
  `walk_order`           MEDIUMINT UNSIGNED NOT NULL,

  `deposit`              FLOAT(12,2) DEFAULT NULL,
  `rent`                 FLOAT(12,2) DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_unit_types

DROP TABLE IF EXISTS `pmgr_unit_types`;
CREATE TABLE `pmgr_unit_types` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_unit_sizes

DROP TABLE IF EXISTS `pmgr_unit_sizes`;
CREATE TABLE `pmgr_unit_sizes` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `unit_type_id`         INT(10) UNSIGNED NOT NULL,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `width`                SMALLINT UNSIGNED NOT NULL,     -- inches
  `depth`                SMALLINT UNSIGNED NOT NULL,     -- inches
  `height`               SMALLINT UNSIGNED DEFAULT NULL, -- inches
  `comment`              VARCHAR(255) DEFAULT NULL,

  `deposit`              FLOAT(12,2) DEFAULT NULL,
  `rent`                 FLOAT(12,2) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## ACTIONS
-- ##

-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_actions

DROP TABLE IF EXISTS `pmgr_actions`;
CREATE TABLE `pmgr_actions` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `action`               ENUM('LETTER',
                              'GATELOCK',
                              'OVERLOCK',
                              'LIEN',
                              'AUCTION')
                           NOT NULL,

  -- Which letter, or other specific action?
  -- REVISIT <AP>: Are there other specific actions?
  `item_id`              INT(10) UNSIGNED DEFAULT NULL,

  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_late_schedules

DROP TABLE IF EXISTS `pmgr_late_schedules`;
CREATE TABLE `pmgr_late_schedules` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"

  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_actions_late_schedules

DROP TABLE IF EXISTS `pmgr_actions_late_schedules`;
CREATE TABLE `pmgr_actions_late_schedules` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `late_schedule_id`     INT(10) UNSIGNED DEFAULT NULL,
  `days_past_due`        SMALLINT NOT NULL,
  `recurring`            TINYINT DEFAULT 0,
  `action_id`            INT(10) UNSIGNED DEFAULT NULL,
-- REVISIT <AP>: 20090513
--  Should we have a specific fee list, or just use account
--  `fee_id`               INT(10) UNSIGNED DEFAULT NULL,
  `account_id`           INT(10) UNSIGNED DEFAULT NULL,
  `amount`               FLOAT(12,2) DEFAULT NULL,
  `tax`                  FLOAT(12,2) DEFAULT NULL,
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## CUSTOMERS
-- ##

-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_customers

DROP TABLE IF EXISTS `pmgr_customers`;
CREATE TABLE `pmgr_customers` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  -- Customer name may be the same as the primary contact
  -- or it may entirely independent of that person.
  `name`                 VARCHAR(80) NOT NULL,

  -- Primary Contact... every customer must have one
  -- (and presumably, most customers will _be_ one).
  -- REVISIT <AP> 20090612:
  --   Does this contact also get added to the
  --   contacts_customers table?
  `primary_contact_id`   INT(10) UNSIGNED NOT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_contacts_customers

DROP TABLE IF EXISTS `pmgr_contacts_customers`;
CREATE TABLE `pmgr_contacts_customers` (
  `contact_id`           INT(10) UNSIGNED NOT NULL,
  `customer_id`          INT(10) UNSIGNED NOT NULL,

  -- What type of contact is this for the lease?
  `type`                 ENUM('TENANT',    -- TENANT
                              'ALTERNATE') -- ALTERNATE CONTACT ONLY
                           NOT NULL DEFAULT 'TENANT',

  -- If the tenant is active as part of the lease
  `active`               TINYINT DEFAULT 1,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`customer_id`, `contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## LEASES
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_leases

DROP TABLE IF EXISTS `pmgr_leases`;
CREATE TABLE `pmgr_leases` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

   -- Allow user to specify their own lease numbers
   -- If NULL, `id` will be used
  `number`               VARCHAR(20) DEFAULT NULL,

  `lease_type_id`        INT(10) UNSIGNED NOT NULL,
  `unit_id`              INT(10) UNSIGNED NOT NULL,
  `customer_id`          INT(10) UNSIGNED NOT NULL,
  `late_schedule_id`     INT(10) UNSIGNED DEFAULT NULL,

  `lease_date`           DATE NOT NULL,
  `movein_planned_date`  DATE DEFAULT NULL,
  `movein_date`          DATE DEFAULT NULL,
  `moveout_date`         DATE DEFAULT NULL,
  `moveout_planned_date` DATE DEFAULT NULL,
  `notice_given_date`    DATE DEFAULT NULL,
  `notice_received_date` DATE DEFAULT NULL,
  `close_date`           DATE DEFAULT NULL,

  `deposit`              FLOAT(12,2) DEFAULT NULL,
  `rent`                 FLOAT(12,2) DEFAULT NULL,

  `next_rent`            FLOAT(12,2) DEFAULT NULL,
  `next_rent_date`       DATE DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_lease_types

DROP TABLE IF EXISTS `pmgr_lease_types`;
CREATE TABLE `pmgr_lease_types` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`                 VARCHAR(12) NOT NULL, -- User style "id"
  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## RESERVATIONS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_reservations

DROP TABLE IF EXISTS `pmgr_reservations`;
CREATE TABLE `pmgr_reservations` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

   -- Allow user to specify their own reservation numbers
   -- If NULL, `id` will be used
  `number`               VARCHAR(20) DEFAULT NULL,

  `customer_id`          INT(10) UNSIGNED NOT NULL,
  `deposit`              FLOAT(12,2) DEFAULT NULL,

  `reservation_date`     DATE NOT NULL,
  `expiration_date`      DATE DEFAULT NULL,
  `fulfilled_date`       DATE DEFAULT NULL,
  `lease_id`             INT(10) UNSIGNED NOT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_reservations_units

DROP TABLE IF EXISTS `pmgr_reservations_units`;
CREATE TABLE `pmgr_reservations_units` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `reservation_id`       INT(10) UNSIGNED NOT NULL,

  `requested_id`         INT(10) UNSIGNED DEFAULT NULL,
  `type`                 ENUM('UNIT',
                              'UNIT_SIZE',
                              'UNIT_TYPE')
                           NOT NULL DEFAULT 'UNIT',

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;




-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## FINANCIAL ACCOUNTS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_accounts

DROP TABLE IF EXISTS `pmgr_accounts`;
CREATE TABLE `pmgr_accounts` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `type`                 ENUM('ASSET',
                              'LIABILITY',
                              'EQUITY',
                              'INCOME',
                              'EXPENSE')
                           NOT NULL DEFAULT 'ASSET',

  -- Accounts Receivable / Accounts Payable
  -- If this is a trackable account, any credit posted
  -- to an ASSET OR EXPENSE account should have an
  -- entry posted to the reconciliations table (unless
  -- it is a "pre-payment" of sorts, in which case the
  -- reconciliation will have to wait for the debit).
  -- For LIABILITY, EQUITY, and INCOME, the opposite
  -- is true, with reconciliations posted, under
  -- normal circumstances, when a debit occurs.
  `trackable`            TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
  `tillable`             TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, -- Does manager collect by hand?
  `depositable`          TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, -- Does this account receive deposits?
  `chargeable`           TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, -- Can be used for charges?
  `payable`              TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, -- Can be used for payments?
			   			   
  -- Security Level
  `level`                INT UNSIGNED DEFAULT 1,

  `name`                 VARCHAR(80) NOT NULL,
  `external_account`     INT(10) UNSIGNED DEFAULT NULL,
  `external_name`        VARCHAR(80) DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

LOCK TABLES `pmgr_accounts` WRITE;
INSERT INTO `pmgr_accounts` (`type`, `name`, `tillable`, `depositable`, `chargeable`, `payable`)
  VALUES
  ('ASSET', 'A/R', 0, 0, 0, 0),
  ('ASSET', 'Invoice', 0, 0, 0, 0),
  ('ASSET', 'Receipt', 0, 0, 0, 0),
  ('LIABILITY', 'A/P', 0, 0, 0, 0),
  ('LIABILITY', 'Tax', 0, 0, 1, 0),
  ('LIABILITY', 'Customer Credit', 0, 0, 1, 1),
  ('ASSET', 'Petty Cash', 0, 0, 0, 0),
  ('ASSET', 'Bank', 0, 1, 0, 0),
  ('ASSET', 'Cash', 1, 0, 0, 0),
  ('ASSET', 'Check', 1, 0, 0, 0),
  ('ASSET', 'Money Order', 1, 0, 0, 0),
  ('LIABILITY', 'Security Deposit', 0, 0, 1, 1),
  ('INCOME', 'Rent', 0, 0, 1, 0),
  ('INCOME', 'Late Charge', 0, 0, 1, 0),
  ('EXPENSE', 'Concession', 0, 0, 0, 1),
  ('EXPENSE', 'Bad Debt', 0, 0, 0, 1);
UNLOCK TABLES;



-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## LEDGERS
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_ledgers
--
-- REVISIT <AP>: 20090605
--   We may not really need a ledgers table.
--   It's not clear to me though, as we very
--   possibly need to close out certain
--   ledgers every so often, and just carry
--   the balance forward (year end, etc).

DROP TABLE IF EXISTS `pmgr_ledgers`;
CREATE TABLE `pmgr_ledgers` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `account_id`           INT(10) UNSIGNED NOT NULL,
  `sequence`             INT(10) UNSIGNED DEFAULT 1,

  `prior_ledger_id`      INT(10) UNSIGNED DEFAULT NULL,
  `close_id`             INT(10) UNSIGNED DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_closes

DROP TABLE IF EXISTS `pmgr_closes`;
CREATE TABLE `pmgr_closes` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `stamp`                TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_transactions

DROP TABLE IF EXISTS `pmgr_transactions`;
CREATE TABLE `pmgr_transactions` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

--   `type`                 ENUM('INVOICE',
--                               'RECEIPT')
--                            NOT NULL,

  `stamp`                TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `due_date`             DATE DEFAULT NULL,

  -- REVISIT <AP>: 20090604
  --   How should we track which charges have been paid?
  -- `related_transaction_id` INT(10) UNSIGNED NOT NULL,
  -- `related_entry_id` INT(10) UNSIGNED NOT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_ledger_entries

DROP TABLE IF EXISTS `pmgr_ledger_entries`;
CREATE TABLE `pmgr_ledger_entries` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  -- Effective date may be used for a variety of entries
  -- charges & payments are not always effective at the
  -- time of transaction.  Through date, on the other hand,
  -- will probably only be relevant for charges, such as
  -- rent, which is effective for a range of dates.
  `effective_date`       DATE DEFAULT NULL, -- first day
  `through_date`         DATE DEFAULT NULL, -- last day

  `monetary_source_id`   INT(10) UNSIGNED DEFAULT NULL, -- NULL if internal transfer
  `transaction_id`       INT(10) UNSIGNED NOT NULL,
  `customer_id`          INT(10) UNSIGNED DEFAULT NULL,
  `lease_id`             INT(10) UNSIGNED DEFAULT NULL,
  `amount`               FLOAT(12,2) NOT NULL,

  -- REVISIT <AP>: 20090707
  --  Experimental.  Considering automatically hooking
  --  charges to their invoice.  This may help ease the
  --  ongoing accounting dilema that we've been having.
  --  It might allow us to keep the underlying invoice
  --  ledgers without having to expose the user to them.
  `root_transaction_id`  INT(10) UNSIGNED DEFAULT NULL,

  `debit_ledger_id`      INT(10) UNSIGNED NOT NULL,
  `credit_ledger_id`     INT(10) UNSIGNED NOT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_reconciliations

DROP TABLE IF EXISTS `pmgr_reconciliations`;
CREATE TABLE `pmgr_reconciliations` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `debit_ledger_entry_id`    INT(10) UNSIGNED NOT NULL,
  `credit_ledger_entry_id`   INT(10) UNSIGNED NOT NULL,
  `amount`                   FLOAT(12,2) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_monetary_sources

DROP TABLE IF EXISTS `pmgr_monetary_sources`;
CREATE TABLE `pmgr_monetary_sources` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `name`                 VARCHAR(80) DEFAULT NULL,
  monetary_type_id       INT(10) UNSIGNED NOT NULL,

  -- REVISIT <AP>: 20090605
  --   Check Number;
  --   Routing Number, Account Number;
  --   Card Number, Expiration Date; CVV2 Code
  --   etc.
  --     REVISIT <AP> 20090630
  --     I _think_ that CVV2 is NEVER supposed to
  --     be stored ANYWHERE.  Merchants agree to
  --     use it only to verify the transaction and
  --     then leave no record of it, so that even
  --     if their security is compromised, no one
  --     will know the CVV2 code unless they are
  --     in physical possession of the card.
  `data1`                VARCHAR(80) DEFAULT NULL,
  `data2`                VARCHAR(80) DEFAULT NULL,
  `data3`                VARCHAR(80) DEFAULT NULL,
  `data4`                VARCHAR(80) DEFAULT NULL,

  `comment`              VARCHAR(255) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_monetary_types

DROP TABLE IF EXISTS `pmgr_monetary_types`;
CREATE TABLE `pmgr_monetary_types` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `name`                 VARCHAR(80) NOT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,
  `tillable`             TINYINT(1) NOT NULL DEFAULT 1, -- Does manager collect by hand?

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

LOCK TABLES `pmgr_monetary_types` WRITE;
INSERT INTO `pmgr_monetary_types` (`id`, `name`, `tillable`)
  VALUES
  -- (1, 'Transfer', 0),
  (2, 'Cash', 1),
  (3, 'Check', 1),
  (4, 'Money Order', 1),
  (5, 'ACH', 0),
  (6, 'Debit Card', 0),
  (7, 'Credit Card', 0),
  (8, 'Other Tillable', 1),
  (9, 'Other Non-Tillable', 0);
UNLOCK TABLES;




-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## SITE MAPS
-- ##

-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_maps


DROP TABLE IF EXISTS `pmgr_maps`;
CREATE TABLE `pmgr_maps` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `site_area_id`         INT(10) UNSIGNED NOT NULL,
  `name`                 VARCHAR(80)  DEFAULT NULL,
  `comment`              VARCHAR(255) DEFAULT NULL,

  `width`                SMALLINT UNSIGNED NOT NULL,     -- inches
  `depth`                SMALLINT UNSIGNED NOT NULL,     -- inches

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_maps_units


DROP TABLE IF EXISTS `pmgr_maps_units`;
CREATE TABLE `pmgr_maps_units` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `map_id`               INT(10) UNSIGNED NOT NULL,
  `unit_id`              INT(10) UNSIGNED NOT NULL,
  `pt_top`               SMALLINT UNSIGNED NOT NULL,     -- inches
  `pt_left`              SMALLINT UNSIGNED NOT NULL,     -- inches
--  `pt_bottom` SMALLINT UNSIGNED NOT NULL,     -- inches
--  `pt_right`  SMALLINT UNSIGNED NOT NULL,     -- inches
  `transpose`            SMALLINT UNSIGNED NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;




-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ######################################################################
-- ##
-- ## NOTES
-- ##


-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_notes

DROP TABLE IF EXISTS `pmgr_notes`;
CREATE TABLE `pmgr_notes` (
  `id`                   INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  -- HISTORY is a note created by the system, when the
  --   user changes a setting or field that has been
  --   flagged for tracking.
  -- USER is a manually entered note.
  `type`                 ENUM('HISTORY',
                              'USER')
                           NOT NULL DEFAULT 'USER',

  -- `item` references the object type, and `item_id` denotes
  -- the specific object to which this note is attached
  `item_id`              INT(10) UNSIGNED NOT NULL,
  `item`                 ENUM('OPTION',
                              'UNIT_TYPE',
                              'UNIT_SIZE',
                              'UNIT',
                              'CUSTOMER',
                              'SITE',
                              'SITE_AREA',
                              'USER') -- etc, etc
                           NOT NULL,

  -- Text of the recorded note
  `note`                 TEXT,

  -- User that made this note, or if HISTORY, made the change
  `user_id`              INT(10) UNSIGNED NOT NULL,
  `stamp`                TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;



