--File Version: 1.2.1.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
--	Percentages will be stored as DECIMAL(6,6) to allow a max of six characters with 5 characters following the decimal point.
--	Reference for descision: http://stackoverflow.com/questions/7205697/sql-which-data-type-represents-percentages-well
--	Also a good one to view: http://stackoverflow.com/questions/2762302/appropriate-datatype-for-holding-percent-values
--Versions:
--	1.0.0.0: Initial script version.
--	1.2.0.0: Changed column names that produced duplicate names when C# code is generated by Entity Framework (i.e. Aditem becomes AdItem1).
--  1.2.1.0: Changed Invoice Number data type to VARCHAR(256) to reduce the risk of to large of number being used for int32 in C#.

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

--Begin creating the tables for the database.
CREATE TABLE WeekEndingDates
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
EndingDate DATE NOT NULL --A date is required to be entered.
)

CREATE TABLE AdItems
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Name VARCHAR(256) NOT NULL,
Description VARCHAR(256) --Optional field may be used later one.
)

CREATE TABLE AdSpecials
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Name VARCHAR(256) NOT NULL,
Description VARCHAR(256) --Optional field may be used later one, most likely in a tool tip.
)

CREATE TABLE Projections
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting. 
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 NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Inventory
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--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(256),
Recieved VARCHAR(256),
TotalInventory VARCHAR(256),
EndingInventory VARCHAR(256),
--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 NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE ActualSales
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting. 
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 NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Suppliers
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Name VARCHAR(256),
Description VARCHAR(256) --Optional field may be used later one.
)

CREATE TABLE Invoices
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
InvoiceDate DATE NOT NULL,
InvoiceNumber VARCHAR(256) NOT NULL,
InvoiceNetAmountAtCost DECIMAL(19,2),
InvoiceNetAmount DECIMAL(19,2),
InvoiceNote VARCHAR(256),
--Keys are never allowed to be null.
FkSupplierId INTEGER NOT NULL FOREIGN KEY REFERENCES Suppliers(Id),
FkDateId INTEGER NOT NULL 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 IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--As of version 1.0.0.0, the user can only enter a max of 256 characters into the comments box.
Remark VARCHAR(256) NOT NULL,
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE WeeklySales
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
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 NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Taxable
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
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 NOT NULL 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 IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
SalesPerManHour DECIMAL(19,2),
--DECIMAL(x,y) x is the total number of digits you want to represent and y is the number of digits to display after the decimal point.
SalaryPercentage DECIMAL(19, 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 NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
)

CREATE TABLE Version
(
Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
VersionNumber VARCHAR(32) --32 characters should be more then enough to store a version number.
)