-- noinspection SqlNoDataSourceInspectionForFile

-- Schema: users
-- Brief: Control user access into POS
-- Created by: amerolli
-- Table schema_version: must be defined for database update through the Persistence Component

CREATE TABLE schema_version AS
SELECT "$Author: rrosauro $" AS Author,
       "$Date: 2009-11-27 11:37:18 -0200 (Fri, 27 Nov 2009) $" AS LastModifiedAt,
       "$Revision: e6dc09949464b693e78023978cc8ba0f10c0cd1d$" AS Revision;

-- Table Users:
--   This table keeps basic user information for identification and authentication 
--   purposes.
--   +------------------+--------------------------------------+-----+------+---------+
--   | Field Name       | Field description                    | Key | NULL | Default |
--   +------------------+--------------------------------------+-----+------+---------+
--   | UserId           | The user unique identification, used |  P  |  No  |   N/D   |
--   |                  | for numeric login.                   |     |  No  |   N/D   |
--   | UserName         | Unique user name to be used in       |     |  No  |   N/D   |
--   |                  | alternate login procedure.           |     |  No  |   N/D   |
--   | LongName         | Complete name of user.               |     |  No  |   N/D   |
--   | Password         | HMAC digest of password (key) and    |     |  No  |   N/D   |
--   |                  | username (message).                  |     |  No  |   N/D   |
--   | PasswordValidity | Date limit to current password; if   |     | Yes  |   N/D   |
--   |                  | null there is no limit.              |     |  No  |   N/D   |
--   | Level            | User Security Level                  |     |  No  |   0     |
--   | Status           | 0->active, 1->inactive,              |     |  No  |   N/D   |
--   |                  | 2->licensed, 3->vacation,            |     |  No  |   N/D   |
--   |                  | 99->terminated                       |     |  No  |   N/D   |
--   | AdmissionDate    | Admission date                       |     |  No  |   N/D   |
--   | TerminationDate  | Termination date                     |     | Yes  |   N/D   |
--   | PayRate          | Pay rate                             |     | Yes  |   N/D   |
--   +------------------+--------------------------------------+-----+------+---------+
CREATE TABLE Users (
	UserId				    INTEGER	PRIMARY KEY AUTOINCREMENT,
	UserName			    VARCHAR(15) NOT NULL,
	LongName			    VARCHAR(50) NOT NULL,
	Password			    VARCHAR(32) NOT NULL,
	PasswordValidity	DATETIME,
	Level            INTEGER NOT NULL DEFAULT 0,
	Status	           INTEGER NOT NULL,
	AdmissionDate    DATE NOT NULL,
	TerminationDate  DATETIME,
	PayRate          VARCHAR(30),
	BadgeNumber      TEXT,
	FingerPrintMinutiae BLOB
);

-- Table Shift:
--   This table describes all possible shifts combinations.
--   +------------------+--------------------------------------+-----+------+---------+
--   | Field Name       | Field description                    | Key | NULL | Default |
--   +------------------+--------------------------------------+-----+------+---------+
--   | ShiftId          | Shift unique identification.         |  P  |  No  |   N/D   |
--   | Name             | Description.                         |     |  No  |   N/D   |
--   | WorkDays         | Indicates the workdays starting at   |     |  No  | 0111110 |
--   |                  | monday.                              |     |  No  |         |
--   | StartTime        | Shift start time.                    |     |  No  | 08:00   |
--   | EndTime          | Shift end time.                      |     |  No  | 18:00   |
--   | NumBreaks        | Number of allowed breaks.            |     |  No  |    1    |
--   | BreakDuration    | Time in minutes for each break.      |     |  No  |   60    |
--   +------------------+--------------------------------------+-----+------+---------+
CREATE TABLE Shift (
	ShiftId				INTEGER NOT NULL,
	Name				VARCHAR(20),
	WorkDays			INTEGER NOT NULL DEFAULT 62, -- '0111110'
	StartTime			VARCHAR(5) NOT NULL DEFAULT '08:00',
	EndTime				VARCHAR(5) NOT NULL DEFAULT '18:00',
	
	-- TODO: create a break table (we may have different break times into the same shift)
	
	NumBreaks			INTEGER	NOT NULL DEFAULT 1,
	BreakDuration		INTEGER NOT NULL DEFAULT 60,
	PRIMARY KEY(ShiftId)
);

-- Table Allocation:
--   This table allocates employees to shifts.
--   +------------------+--------------------------------------+-----+------+---------+
--   | Field Name       | Field description                    | Key | NULL | Default |
--   +------------------+--------------------------------------+-----+------+---------+
--   | Id               | Shift/User relation unique id (auto).|  P  |  No  |   N/D   |
--   | ShiftId          | Shift unique identification.         |     |  No  |   N/D   |
--   | UserId           | The user unique identification.      |     |  No  |   N/D   |
--   | ShiftTolerance   | Time in minutes of tolerance.        |     |  No  |    0    |
--   +------------------+--------------------------------------+-----+------+---------+
CREATE TABLE Allocation (
	Id					INTEGER PRIMARY KEY AUTOINCREMENT,
	ShiftId				INTEGER NOT NULL,
	UserId				INTEGER NOT NULL,
	ShiftTolerance		INTEGER NOT NULL DEFAULT 0
);


--
-- Default user (1/1)
--
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(6626, 'Suporte', 'Suporte', '2a68fba8d921e4bd3e5b03ba401bf6eb', 30, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(31415, 'Delivery', 'Delivery', '7c3d32402e4509c78b5c4ed6552ef756', 21, 0, '2019-01-01');

INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(2718, 'Totem', 'Totem', 'b1eac4329ffd24ef085940fe70904e00', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180001, 'Totem01', 'Totem01', '1022a6963c76b9aa97a85b207865cb95', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180002, 'Totem02', 'Totem02', '6475a8ec4860e1b4f386d3f4377c43ff', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180003, 'Totem03', 'Totem03', 'e505b8dd99fba45eb6a2a4c96e9bbc2c', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180004, 'Totem04', 'Totem04', 'c7e75f616546de4ecb69a3abf0cd0cab', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180005, 'Totem05', 'Totem05', '80a5dcace2be6c290836e5a5567d8350', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180006, 'Totem06', 'Totem06', '05cbba396983ac8964deec69cc8d2131', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180007, 'Totem07', 'Totem07', '889c017e54b2d6285ebad484575e1f63', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180008, 'Totem08', 'Totem08', '198968f40e0cfb1d3e9e98dad96a096a', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180009, 'Totem09', 'Totem09', '4eb6c2cff967297cd8e2ac60f9470548', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180010, 'Totem10', 'Totem10', 'e0ca929d9c6a2c92a4a79043818584cb', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180011, 'Totem11', 'Totem11', '31e579759e50f1425df519d16ca15b24', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180012, 'Totem12', 'Totem12', 'b23cfaf980885d82a63605cb8212f60b', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180013, 'Totem13', 'Totem13', 'f54da54aee4b8f1647b95fad79db0bc5', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180014, 'Totem14', 'Totem14', '4567e5cc6b36ed40b06323e846c06d66', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180015, 'Totem15', 'Totem15', 'd445a24aebf7cae16f9952cfd02bbdf6', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180016, 'Totem16', 'Totem16', 'b1810a04c0f0c571de0fc65cb54eeef2', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180017, 'Totem17', 'Totem17', '518195c33e4a757ab9205af8ca230963', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180018, 'Totem18', 'Totem18', '5605fd3a0b9783b5c2c991761b5720d3', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180019, 'Totem19', 'Totem19', '4f5899b71631d09c117515ac79328eda', 21, 0, '2019-01-01');
INSERT INTO Users(UserId, UserName, LongName, Password, Level, Status, AdmissionDate) VALUES(27180020, 'Totem20', 'Totem20', '275e70d6c26154ca2bb010b00743031e', 21, 0, '2019-01-01');
