--File Version: 1.0.0.0 for SQL Server
--Purpose: Creates an empty database template in SQL for the Advertising Profit Control software; master template.
--Notes: This version does not implement any table relationships, its just flat data tables.
--	Currency will be represented with DECIMAL(19,2) as the standard. From stackoverflow:
--	http://stackoverflow.com/questions/628637/best-data-type-for-storing-currency-values-in-a-mysql-database

--Create the database called "AdvertisingProfitControl".	
CREATE DATABASE AdvertisingProfitControl

--Begin creating the tables for the database.
CREATE TABLE WeekEndingDates
(
Id INTEGER NOT NULL PRIMARY KEY,
WeekEndingDate DATE NOT NULL --A date is required to be entered.
)

CREATE TABLE AdItems
(
Id INTEGER NOT NULL PRIMARY KEY,
AdItem VARCHAR(255) NOT NULL,
Description VARCHAR(255) --Optional field may be used later one.
)

CREATE TABLE AdSpecials
(
Id INTEGER NOT NULL PRIMARY KEY,
AdSpecial VARCHAR(255) NOT NULL,
Description VARCHAR(255) --Optional field may be used later one, most likely in a tool tip.
)

CREATE TABLE Projections
(
Id INTEGER NOT NULL PRIMARY KEY,
--Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting respectively. 
Sold VARCHAR(128),
SalePrice VARCHAR(128),
--Might be over kill to store money like this but better safe then sorry.
TotalSales DECIMAL(19,2),
Cost DECIMAL(19,2),
ProfitReturn DECIMAL(19,2),
TotalProfitReturn DECIMAL(19,2),
--The attributes of the row can not be null, and neither can the foreign keys.
RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id),
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Inventory
(
Id INTEGER NOT NULL PRIMARY KEY,
--All values minus the primary key and foreign keys are set as strings to allow
--specifying the number of bins of product are present.
BeginningInventory VARCHAR(255),
Recieved VARCHAR(255),
TotalInventory VARCHAR(255),
EndingInventory VARCHAR(255),
--The attributes of the row can not be null, and neither can the foreign keys.
RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id),
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE ActualSales
(
Id INTEGER NOT NULL PRIMARY KEY,
--Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting respectively. 
Sold VARCHAR(128),
SalePrice VARCHAR(128),
--Might be over kill to store money like this but better safe then sorry.
TotalSales DECIMAL(19,2),
Cost DECIMAL(19,2),
ProfitReturn DECIMAL(19,2),
TotalProfitReturn DECIMAL(19,2),
--The attributes of the row can not be null, and neither can the foreign keys.
RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id),
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Suppliers
(
Id INTEGER NOT NULL PRIMARY KEY,
Supplier VARCHAR(255),
Description VARCHAR(255) --Optional field may be used later one.
)

CREATE TABLE Invoices
(
Id INTEGER NOT NULL PRIMARY KEY,
InvoiceDate DATE NOT NULL,
InvoiceNumber INTEGER NOT NULL,
InvoiceNetAmountAtCost DECIMAL(19,2),
InvoiceNetAmount DECIMAL(19,2),
InvoiceNote VARCHAR(255),
--Keys are never allowed to be null.
FkSupplierId INTEGER FOREIGN KEY REFERENCES Suppliers(Id),
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Notes --Comment is a reserved word so the table and column had to be renamed to "Note" instead.
(
Id INTEGER NOT NULL PRIMARY KEY,
--The current version (1.0.0.0) only allows the user to enter a max of 256 characters into the comments box.
Note VARCHAR(256) NOT NULL,
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE WeeklySales
(
Id INTEGER NOT NULL PRIMARY KEY,
Sunday DECIMAL(19,2),
Monday DECIMAL(19,2),
Tuesday DECIMAL(19,2),
Wednesday DECIMAL(19,2),
Thursday DECIMAL(19,2),
Friday DECIMAL(19,2),
Saturday DECIMAL(19,2),
TotalSales DECIMAL(19,2),
--Keys are never allowed to be null.
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Taxable
(
Id INTEGER NOT NULL PRIMARY KEY,
Sunday DECIMAL(19,2),
Monday DECIMAL(19,2),
Tuesday DECIMAL(19,2),
Wednesday DECIMAL(19,2),
Thursday DECIMAL(19,2),
Friday DECIMAL(19,2),
Saturday DECIMAL(19,2),
Total DECIMAL(19,2),
--Keys are never allowed to be null.
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

--This table contains items that didn't fit in the other tables.
--Just little things that were on the back of the physical 
--advertising profit control sheet (the Weekly Inventory Control).
CREATE TABLE CostAnalysis
(
Id INTEGER NOT NULL PRIMARY KEY,
SalesPerManHour DECIMAL(19,2),
--Allows percentages between %1000.00 (10,000 percent) and %0.1235.
SalaryPercentage DECIMAL(6,2), --The percentage of the manager's salary that was made on this week's sale items.
SalaryDollar DECIMAL(19,2), --
Supplies DECIMAL(19,2), --Total of costs from invoices (?).
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Version
(
Id INTEGER NOT NULL PRIMARY KEY,
VersionNumber VARCHAR(32) --32 characters should be more then enough to store a version number.
)