Updated the SQL script to hold Invoice Numbers as text as this may cause issues using int in c#. Finished the debug Access to SQL code form. Pushing this purely to have the code for reference at another time.

This commit is contained in:
2017-03-22 22:36:51 -05:00
parent f554d37fb1
commit 36fe75d761
2 changed files with 329 additions and 228 deletions
@@ -1,4 +1,4 @@
--File Version: 1.2.0.0 for SQL Server
--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:
@@ -9,6 +9,7 @@
--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
@@ -111,7 +112,7 @@ 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 INTEGER NOT NULL,
InvoiceNumber VARCHAR(256) NOT NULL,
InvoiceNetAmountAtCost DECIMAL(19,2),
InvoiceNetAmount DECIMAL(19,2),
InvoiceNote VARCHAR(256),
@@ -165,9 +166,8 @@ 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),
--Allows percentages between %1000.00 (10,000 percent) and %0.1235. 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(6,6), --The percentage of the manager's salary that was made on this week's sale items.
--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)
+324 -223
View File
@@ -4,11 +4,13 @@ using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows.Forms;
namespace AdvertsingProfitControl
@@ -100,253 +102,331 @@ namespace AdvertsingProfitControl
var actualSales = ReturnActualSales(oldDateId);
var invoices = ReturnInvoiceTable(oldDateId);
var weeklySales = ReturnWeeklySalesFromDateId(oldDateId);
var dbR = new DatabaseReader();
var taxable = dbR.ReturnTaxableFromDateId(oldDateId, _connectionString);
var comment = GetComments(oldDateId);
MassiveWriteFunction(date.ToString(), projectionsTable, inventoryTable, actualSales, invoices, weeklySales, comment);
var costs = dbR.ReturnCostAnalysis(oldDateId, _connectionString);
MassiveWriteFunction(date.ToString(), projectionsTable, inventoryTable, actualSales, invoices, weeklySales, taxable, costs, comment);
}
}
private void MassiveWriteFunction(string dateString, DataTable projections, DataTable inventory, DataTable actualSales, DataTable invoice, DataTable weeklySales, string comments)
private void MassiveWriteFunction(string dateString, DataTable projections, DataTable inventory, DataTable actualSales, DataTable invoice, DataTable weeklySales, DataTable taxableTable, DataTable costOfSales, string comments)
{
var dbT = new DatabaseTracker();
//Get a new ID number for the date supplied.
var oleDbConnection = new OleDbConnection(dbT.DatabaseConnectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
//var dbT = new DatabaseTracker();
////Get a new ID number for the date supplied.
//var oleDbConnection = new OleDbConnection(dbT.DatabaseConnectionString);
//var oleDbCommand = new OleDbCommand
//{
// Connection = oleDbConnection
//};
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
//Get the date ID for the date string
oleDbCommand.CommandText = "INSERT INTO WeekEnding (EndOfWeekDate) VALUES (?)";
oleDbCommand.Parameters.AddWithValue("DateString", dateString);
var rowsEffected = oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
if (rowsEffected == 0)
using (var transaction = new TransactionScope())
{
oleDbTransaction.Rollback();
MessageBox.Show(@"Failed on date");
return;
}
oleDbCommand.CommandText = "SELECT WeekEnding.ID FROM WeekEnding WHERE EndOfWeekDate = ?";
oleDbCommand.Parameters.AddWithValue("Date", dateString);
var reader = oleDbCommand.ExecuteReader();
oleDbCommand.Parameters.Clear();
var dateId = 0;
while (reader != null && reader.Read())
{
dateId = int.Parse(reader[0].ToString());
}
reader?.Close();
int adspecialId = 0;
for (var rowIndex = 0; rowIndex < projections.Rows.Count; rowIndex++)
{
//Get the ID number of the ad item.
var adItemId = int.Parse(RetrieveAdItemId(projections.Rows[rowIndex][0].ToString()));
//Get the group name of the ad special its in.
adspecialId = GetAdSpecialId(int.Parse(projections.Rows[rowIndex][8].ToString()));
oleDbCommand.CommandText =
"INSERT INTO Projections (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC
oleDbCommand.Parameters.AddWithValue("Sold", projections.Rows[rowIndex][1]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", projections.Rows[rowIndex][2]);
oleDbCommand.Parameters.AddWithValue("TotalSales", projections.Rows[rowIndex][3]);
oleDbCommand.Parameters.AddWithValue("Cost", projections.Rows[rowIndex][4]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", projections.Rows[rowIndex][5]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", projections.Rows[rowIndex][6]);
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("RowAttribute",
int.Parse(projections.Rows[rowIndex][7].ToString()));
oleDbCommand.Parameters.AddWithValue("adSpecialID", adspecialId);
oleDbCommand.Parameters.AddWithValue("RowPosition", (rowIndex + 1));
oleDbCommand.Parameters.AddWithValue("dateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
}
//inventory
for (var i = 0; i < inventory.Rows.Count; i++)
{
var adItemId = int.Parse(RetrieveAdItemId(inventory.Rows[i][0].ToString()));
adspecialId = GetAdSpecialId(int.Parse(inventory.Rows[i][6].ToString()));
oleDbCommand.CommandText =
"INSERT INTO Inventory (BeginningInventory, Received, TotalInventory, EndingInventory, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
//"SELECT AdItem.AdItem, APC.BeginingInventory, APC.Received, APC.TotalInventory, APC.EndingInventory, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
oleDbCommand.Parameters.AddWithValue("BeginningInventory", inventory.Rows[i][1].ToString());
oleDbCommand.Parameters.AddWithValue("Received", inventory.Rows[i][2].ToString());
oleDbCommand.Parameters.AddWithValue("TotalInventory", inventory.Rows[i][3].ToString());
oleDbCommand.Parameters.AddWithValue("EndingInventory", inventory.Rows[i][4].ToString());
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("RowAttribute", int.Parse(inventory.Rows[i][5].ToString()));
oleDbCommand.Parameters.AddWithValue("FK_AdSpecialGroup", adspecialId);
oleDbCommand.Parameters.AddWithValue("RowPosition", (i + 1));
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
}
var db = new AdvertisingProfitControlModel();
var date = new WeekEndingDate {EndingDate = DateTime.Parse(dateString)};
if (db.WeekEndingDates.Any(x => x.EndingDate == date.EndingDate))
{
date = db.WeekEndingDates.First(x => x.EndingDate == date.EndingDate);
}
else
{
db.WeekEndingDates.Add(date);
}
//oleDbConnection.Open();
//oleDbTransaction = oleDbConnection.BeginTransaction();
//oleDbCommand.Transaction = oleDbTransaction;
////Get the date ID for the date string
//oleDbCommand.CommandText = "INSERT INTO WeekEnding (EndOfWeekDate) VALUES (?)";
//oleDbCommand.Parameters.AddWithValue("DateString", dateString);
//var rowsEffected = oleDbCommand.ExecuteNonQuery();
//oleDbCommand.Parameters.Clear();
//if (rowsEffected == 0)
//{
// oleDbTransaction.Rollback();
// MessageBox.Show(@"Failed on date");
// return;
//}
//oleDbCommand.CommandText = "SELECT WeekEnding.ID FROM WeekEnding WHERE EndOfWeekDate = ?";
//oleDbCommand.Parameters.AddWithValue("Date", dateString);
//var reader = oleDbCommand.ExecuteReader();
//oleDbCommand.Parameters.Clear();
//var dateId = 0;
//while (reader != null && reader.Read())
//{
// dateId = int.Parse(reader[0].ToString());
//}
//reader?.Close();
for (var rowIndex = 0; rowIndex < projections.Rows.Count; rowIndex++)
{
var projectionedSale = new Projection();
var adItem = new AdItem();
var adItemId = 0;
//var id = int.Parse(projections.Rows[rowIndex][0].ToString());
var spam = projections.Rows[rowIndex][0].ToString();
if (db.AdItems.Any(x => x.Name == spam))
{
//Exists
adItem = db.AdItems.First(x => x.Name == spam);
adItemId = adItem.Id;
}
else
{
//Doesn't
adItem.Name = projections.Rows[rowIndex][0].ToString();
db.AdItems.Add(adItem);
adItemId = adItem.Id;
//actual sales
for (var rowIndex = 0; rowIndex < actualSales.Rows.Count; rowIndex++)
{
//Get the ID number of the ad item.
var adItemId = int.Parse(RetrieveAdItemId(actualSales.Rows[rowIndex][0].ToString()));
adspecialId = GetAdSpecialId(int.Parse(actualSales.Rows[rowIndex][8].ToString()));
oleDbCommand.CommandText =
"INSERT INTO ActualSales (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC
oleDbCommand.Parameters.AddWithValue("Sold", actualSales.Rows[rowIndex][1]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", actualSales.Rows[rowIndex][2]);
oleDbCommand.Parameters.AddWithValue("TotalSales", actualSales.Rows[rowIndex][3]);
oleDbCommand.Parameters.AddWithValue("Cost", actualSales.Rows[rowIndex][4]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", actualSales.Rows[rowIndex][5]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", actualSales.Rows[rowIndex][6]);
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("RowAttribute",
int.Parse(projections.Rows[rowIndex][7].ToString()));
oleDbCommand.Parameters.AddWithValue("adSpecialID", adspecialId);
oleDbCommand.Parameters.AddWithValue("RowPosition", (rowIndex + 1));
oleDbCommand.Parameters.AddWithValue("dateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
}
}
projectionedSale.Sold = projections.Rows[rowIndex][1].ToString();
projectionedSale.SalePrice = projections.Rows[rowIndex][2].ToString();
projectionedSale.TotalSales = decimal.Parse(projections.Rows[rowIndex][3].ToString());
projectionedSale.Cost = decimal.Parse(projections.Rows[rowIndex][4].ToString());
projectionedSale.ProfitReturn = decimal.Parse(projections.Rows[rowIndex][5].ToString());
projectionedSale.TotalProfitReturn = decimal.Parse(projections.Rows[rowIndex][6].ToString());
projectionedSale.FkAdItemId = db.AdItems.First(x => x.Id == adItemId).Id;
projectionedSale.RowAttribute = int.Parse(projections.Rows[rowIndex][7].ToString());
projectionedSale.FkAdSpecialId = (int) projections.Rows[rowIndex][8];
projectionedSale.RowPosition = rowIndex + 1;
projectionedSale.FkDateId = date.Id;
db.Projections.Add(projectionedSale);
//Get the ID number of the ad item.
// //Get the ID number of the ad item.
// var adItemId = int.Parse(RetrieveAdItemId(projections.Rows[rowIndex][0].ToString()));
// //Get the group name of the ad special its in.
// adspecialId = GetAdSpecialId(int.Parse(projections.Rows[rowIndex][8].ToString()));
// oleDbCommand.CommandText =
// "INSERT INTO Projections (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// //SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC
// oleDbCommand.Parameters.AddWithValue("Sold", projections.Rows[rowIndex][1]);
// oleDbCommand.Parameters.AddWithValue("SalesPrice", projections.Rows[rowIndex][2]);
// oleDbCommand.Parameters.AddWithValue("TotalSales", projections.Rows[rowIndex][3]);
// oleDbCommand.Parameters.AddWithValue("Cost", projections.Rows[rowIndex][4]);
// oleDbCommand.Parameters.AddWithValue("ProfitReturn", projections.Rows[rowIndex][5]);
// oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", projections.Rows[rowIndex][6]);
// oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
// oleDbCommand.Parameters.AddWithValue("RowAttribute",
// int.Parse(projections.Rows[rowIndex][7].ToString()));
// oleDbCommand.Parameters.AddWithValue("adSpecialID", adspecialId);
// oleDbCommand.Parameters.AddWithValue("RowPosition", (rowIndex + 1));
// oleDbCommand.Parameters.AddWithValue("dateID", dateId);
// oleDbCommand.ExecuteNonQuery();
// oleDbCommand.Parameters.Clear();
}
//inventory
for (var i = 0; i < inventory.Rows.Count; i++)
{
var inventoryObject = new Inventory();
AdItem adItem;
var adItemId = 0;
var spam = actualSales.Rows[i][0].ToString();
if (db.AdItems.Any(x => x.Name == spam))
{
//Exists
adItem = db.AdItems.First(x => x.Name == spam);
adItemId = adItem.Id;
}
inventoryObject.BeginningInventory = inventory.Rows[i][1].ToString();
inventoryObject.Recieved = inventory.Rows[i][2].ToString();
inventoryObject.TotalInventory = inventory.Rows[i][3].ToString();
inventoryObject.EndingInventory = inventory.Rows[i][4].ToString();
inventoryObject.FkAdItemId = adItemId;
inventoryObject.RowAttribute = int.Parse(inventory.Rows[i][5].ToString());
inventoryObject.FkAdSpecialId = int.Parse(inventory.Rows[i][6].ToString());
inventoryObject.RowPosition = i + 1;
inventoryObject.FkDateId = date.Id;
db.Inventories.Add(inventoryObject);
// var adItemId = int.Parse(RetrieveAdItemId(inventory.Rows[i][0].ToString()));
// adspecialId = GetAdSpecialId(int.Parse(inventory.Rows[i][6].ToString()));
// oleDbCommand.CommandText =
// "INSERT INTO Inventory (BeginningInventory, Received, TotalInventory, EndingInventory, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
// //"SELECT AdItem.AdItem, APC.BeginingInventory, APC.Received, APC.TotalInventory, APC.EndingInventory, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
// oleDbCommand.Parameters.AddWithValue("BeginningInventory", inventory.Rows[i][1].ToString());
// oleDbCommand.Parameters.AddWithValue("Received", inventory.Rows[i][2].ToString());
// oleDbCommand.Parameters.AddWithValue("TotalInventory", inventory.Rows[i][3].ToString());
// oleDbCommand.Parameters.AddWithValue("EndingInventory", inventory.Rows[i][4].ToString());
// oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
// oleDbCommand.Parameters.AddWithValue("RowAttribute", int.Parse(inventory.Rows[i][5].ToString()));
// oleDbCommand.Parameters.AddWithValue("FK_AdSpecialGroup", adspecialId);
// oleDbCommand.Parameters.AddWithValue("RowPosition", (i + 1));
// oleDbCommand.Parameters.AddWithValue("DateID", dateId);
// oleDbCommand.ExecuteNonQuery();
// oleDbCommand.Parameters.Clear();
}
for (var i = 0; i < invoice.Rows.Count; i++)
{
var supplierId = GetSupplierId(invoice.Rows[i][1].ToString());
//SELECT Invoice.InvoiceDate, Supplier.SupplierName, Invoice.InvoiceNumber, Invoice.InvoiceNetAmountAtCost, Invoice.InvoiceNetAmount, Invoice.InvoiceNote FROM (Supplier INNER JOIN Invoice ON Supplier.ID = Invoice.FK_Supplier) WHERE FK_DateID = ?
oleDbCommand.CommandText =
"INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES(?,?,?,?,?,?,?)";
oleDbCommand.Parameters.AddWithValue("InvoiceDate", invoice.Rows[i][0].ToString());
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoice.Rows[i][2].ToString());
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmountAtCost",
invoice.Rows[i][3].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][3].ToString()));
oleDbCommand.Parameters.AddWithValue("InvoiceNetAmount",
invoice.Rows[i][4].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][4].ToString()));
oleDbCommand.Parameters.AddWithValue("InvoiceNote", invoice.Rows[i][5].ToString());
oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplierId);
oleDbCommand.Parameters.AddWithValue("FK_DateID", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
}
//"SELECT WeeklySales.Sunday, WeeklySales.Monday, WeeklySales.Tuesday, WeeklySales.Wednesday, WeeklySales.Thursday, WeeklySales.Friday, WeeklySales.Saturday, WeeklySales.TotalSales FROM WeeklySales WHERE FK_DateID = ?"
oleDbCommand.CommandText =
"INSERT INTO WeeklySales (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, TotalSales, FK_DateID) VALUES (?,?,?,?,?,?,?,?,?)";
oleDbCommand.Parameters.AddWithValue("Sunday", weeklySales.Rows[0][0]);
oleDbCommand.Parameters.AddWithValue("Monday", weeklySales.Rows[0][1]);
oleDbCommand.Parameters.AddWithValue("Tuesday", weeklySales.Rows[0][2]);
oleDbCommand.Parameters.AddWithValue("Wednesday", weeklySales.Rows[0][3]);
oleDbCommand.Parameters.AddWithValue("Thursday", weeklySales.Rows[0][4]);
oleDbCommand.Parameters.AddWithValue("Friday", weeklySales.Rows[0][5]);
oleDbCommand.Parameters.AddWithValue("Saturday", weeklySales.Rows[0][6]);
oleDbCommand.Parameters.AddWithValue("TotalSales", weeklySales.Rows[0][7]);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
//actual sales
for (var rowIndex = 0; rowIndex < actualSales.Rows.Count; rowIndex++)
{
var actualSale = new ActualSale();
//Get the ID number of the ad item.
AdItem adItem;
var adItemId = 0;
var spam = actualSales.Rows[rowIndex][0].ToString();
if (db.AdItems.Any(x => x.Name == spam))
{
//Exists
adItem = db.AdItems.First(x => x.Name == spam);
adItemId = adItem.Id;
}
//adspecialId = GetAdSpecialId(int.Parse(actualSales.Rows[rowIndex][8].ToString()));
//oleDbCommand.CommandText =
// "INSERT INTO ActualSales (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC
oleDbCommand.Parameters.Clear();
oleDbCommand.CommandText = "INSERT INTO Comment (Comment, FK_DateID) VALUES (?, ?)";
oleDbCommand.Parameters.AddWithValue("Com", comments);
oleDbCommand.Parameters.AddWithValue("FK", dateId);
oleDbCommand.ExecuteNonQuery();
oleDbTransaction.Commit();
//SELECT Projections.ID, AdItem.AdItem, Projections.Sold, Projections.SalePrice, Projections.TotalSales, Projections.Cost, Projections.ProfitReturn, Projections.TotalProfitReturn, Projections.RowAttribute, Projections.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Projections ON AdItem.ID = Projections.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC
actualSale.Sold = actualSales.Rows[rowIndex][1].ToString();
actualSale.SalePrice = actualSales.Rows[rowIndex][2].ToString();
actualSale.TotalSales = decimal.Parse(actualSales.Rows[rowIndex][3].ToString());
actualSale.Cost = decimal.Parse(actualSales.Rows[rowIndex][4].ToString());
actualSale.ProfitReturn = decimal.Parse(actualSales.Rows[rowIndex][5].ToString());
actualSale.TotalProfitReturn = decimal.Parse(actualSales.Rows[rowIndex][6].ToString());
actualSale.FkAdItemId = adItemId;
actualSale.RowAttribute = int.Parse(actualSales.Rows[rowIndex][7].ToString());
actualSale.FkAdSpecialId = (int) actualSales.Rows[rowIndex][8];
actualSale.RowPosition = rowIndex + 1;
actualSale.FkDateId = date.Id; //db.WeekEndingDates.First(x => x.Id == dateId);
//oleDbCommand.Parameters.AddWithValue("Sold", actualSales.Rows[rowIndex][1]);
//oleDbCommand.Parameters.AddWithValue("SalesPrice", actualSales.Rows[rowIndex][2]);
//oleDbCommand.Parameters.AddWithValue("TotalSales", actualSales.Rows[rowIndex][3]);
//oleDbCommand.Parameters.AddWithValue("Cost", actualSales.Rows[rowIndex][4]);
//oleDbCommand.Parameters.AddWithValue("ProfitReturn", actualSales.Rows[rowIndex][5]);
//oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", actualSales.Rows[rowIndex][6]);
//oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
//oleDbCommand.Parameters.AddWithValue("RowAttribute",
// int.Parse(projections.Rows[rowIndex][7].ToString()));
//oleDbCommand.Parameters.AddWithValue("adSpecialID", adspecialId);
//oleDbCommand.Parameters.AddWithValue("RowPosition", (rowIndex + 1));
//oleDbCommand.Parameters.AddWithValue("dateID", dateId);
//oleDbCommand.ExecuteNonQuery();
//oleDbCommand.Parameters.Clear();
db.ActualSales.Add(actualSale);
}
for (var i = 0; i < invoice.Rows.Count; i++)
{
var invoiceObject = new Invoice();
var supplier = new Supplier();
var spam = invoice.Rows[i][1].ToString();
if (db.Suppliers.Any(x => x.Name == spam))
{
supplier = db.Suppliers.First(x => x.Name == spam);
}
else
{
supplier.Name = invoice.Rows[i][1].ToString();
db.Suppliers.Add(supplier);
}
invoiceObject.InvoiceDate = DateTime.Parse(invoice.Rows[i][0].ToString());
invoiceObject.InvoiceNumber = int.Parse(invoice.Rows[i][2].ToString());
invoiceObject.InvoiceNetAmountAtCost = decimal.Parse(invoice.Rows[i][3].ToString());
invoiceObject.InvoiceNetAmount = decimal.Parse(invoice.Rows[i][4].ToString());
invoiceObject.InvoiceNote = invoice.Rows[i][5].ToString();
invoiceObject.FkSupplierId = supplier.Id;
invoiceObject.FkDateId = date.Id;
db.Invoices.Add(invoiceObject);
// //SELECT Invoice.InvoiceDate, Supplier.SupplierName, Invoice.InvoiceNumber, Invoice.InvoiceNetAmountAtCost, Invoice.InvoiceNetAmount, Invoice.InvoiceNote FROM (Supplier INNER JOIN Invoice ON Supplier.ID = Invoice.FK_Supplier) WHERE FK_DateID = ?
// oleDbCommand.CommandText =
// "INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES(?,?,?,?,?,?,?)";
// oleDbCommand.Parameters.AddWithValue("InvoiceDate", invoice.Rows[i][0].ToString());
// oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoice.Rows[i][2].ToString());
// oleDbCommand.Parameters.AddWithValue("InvoiceNetAmountAtCost",
// invoice.Rows[i][3].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][3].ToString()));
// oleDbCommand.Parameters.AddWithValue("InvoiceNetAmount",
// invoice.Rows[i][4].ToString() == "" ? 0 : double.Parse(invoice.Rows[i][4].ToString()));
// oleDbCommand.Parameters.AddWithValue("InvoiceNote", invoice.Rows[i][5].ToString());
// oleDbCommand.Parameters.AddWithValue("FK_Supplier", supplier.Id);
// oleDbCommand.Parameters.AddWithValue("FK_DateID", dateId);
// oleDbCommand.ExecuteNonQuery();
// oleDbCommand.Parameters.Clear();
}
var weeklySale = new WeeklySale
{
Sunday = decimal.Parse(weeklySales.Rows[0][0].ToString()),
Monday = decimal.Parse(weeklySales.Rows[0][1].ToString()),
Tuesday = decimal.Parse(weeklySales.Rows[0][2].ToString()),
Wednesday = decimal.Parse(weeklySales.Rows[0][3].ToString()),
Thursday = decimal.Parse(weeklySales.Rows[0][4].ToString()),
Friday = decimal.Parse(weeklySales.Rows[0][5].ToString()),
Saturday = decimal.Parse(weeklySales.Rows[0][6].ToString()),
TotalSales = decimal.Parse(weeklySales.Rows[0][7].ToString()),
FkDateId = date.Id
};
db.WeeklySales.Add(weeklySale);
//Comes from DatabaseReader class, so offset by one because zero is the ID number.
var taxable = new Taxable
{
Sunday = decimal.Parse(taxableTable.Rows[0][1].ToString()),
Monday = decimal.Parse(taxableTable.Rows[0][2].ToString()),
Tuesday = decimal.Parse(taxableTable.Rows[0][3].ToString()),
Wednesday = decimal.Parse(taxableTable.Rows[0][4].ToString()),
Thursday = decimal.Parse(taxableTable.Rows[0][5].ToString()),
Friday = decimal.Parse(taxableTable.Rows[0][6].ToString()),
Saturday = decimal.Parse(taxableTable.Rows[0][7].ToString()),
Total = decimal.Parse(taxableTable.Rows[0][8].ToString()),
FkDateId = date.Id
};
db.Taxables.Add(taxable);
//SELECT CostOfSalesAnalysis.ID, CostOfSalesAnalysis.SalesPerManHour, CostOfSalesAnalysis.SalaryPercentage, CostOfSalesAnalysis.SalaryDollars, CostOfSalesAnalysis.Supplies FROM CostOfSalesAnalysis WHERE FK_DateID = ?
var cost = new CostAnalysi
{
SalesPerManHour = decimal.Parse(costOfSales.Rows[0][1].ToString()),
SalaryPercentage = decimal.Parse(costOfSales.Rows[0][2].ToString()),
SalaryDollar = decimal.Parse(costOfSales.Rows[0][3].ToString()),
Supplies = decimal.Parse(costOfSales.Rows[0][4].ToString()),
FkDateId = date.Id
};
db.CostAnalysis.Add(cost);
if (!string.IsNullOrEmpty(comments))
{
var comment = new Note
{
Remark = comments,
FkDateId = date.Id
};
db.Notes.Add(comment);
}
//oleDbCommand.Parameters.Clear();
//oleDbCommand.CommandText = "INSERT INTO Comment (Comment, FK_DateID) VALUES (?, ?)";
//oleDbCommand.Parameters.AddWithValue("Com", comments);
//oleDbCommand.Parameters.AddWithValue("FK", dateId);
//oleDbCommand.ExecuteNonQuery();
//oleDbTransaction.Commit();
db.SaveChanges();
transaction.Complete();
}
}
catch (OleDbException e)
{
MessageBox.Show(e.Message);
oleDbTransaction?.Rollback();
//oleDbTransaction?.Rollback();
}
finally
{
oleDbConnection.Close();
//oleDbConnection.Close();
}
}
public int GetSupplierId(string supplierName)
{
var dbT = new DatabaseTracker();
var supplierId = 0;
var oleDbConnection = new OleDbConnection(dbT.DatabaseConnectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection,
CommandText = "SELECT Supplier.ID FROM Supplier WHERE SupplierName = ?"
};
oleDbCommand.Parameters.AddWithValue("eh", supplierName);
var supplier = new Supplier();
var db = new AdvertisingProfitControlModel();
try
{
oleDbConnection.Open();
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
if (db.Suppliers.Any(x => x.Name == supplierName))
{
supplierId = int.Parse(reader[0].ToString());
return db.Suppliers.First(x => x.Name == supplierName).Id;
}
//No match
supplier.Name = supplierName;
db.Suppliers.Add(supplier);
db.SaveChanges();
return supplier.Id;
}
catch (OleDbException e)
{
MessageBox.Show(e.Message);
}
finally
{
oleDbConnection.Close();
}
return supplierId;
}
public int GetAdSpecialId(int adSpecialId)
{
var id = 0;
var dbT = new DatabaseTracker();
var dbR = new DatabaseReader();
//Get a new ID number for the date supplied.
var oleDbConnection = new OleDbConnection(_connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection,
CommandText = "SELECT GroupCategory.GroupDescription FROM GroupCategory WHERE GroupCategory.ID = ?"
};
oleDbCommand.Parameters.AddWithValue("eh", adSpecialId);
try
{
oleDbConnection.Open();
var reader = oleDbCommand.ExecuteReader();
string adSpecialText = "";
while (reader != null && reader.Read())
{
adSpecialText = reader[0].ToString();
}
oleDbCommand.Parameters.Clear();
oleDbCommand.Connection.Close();
var spam = new OleDbConnection(dbT.DatabaseConnectionString);
var newSpam = new OleDbCommand
{
Connection = spam,
CommandText =
"SELECT AdSpecialName.ID FROM AdSpecialName WHERE AdSpecialName.AdSpecialName = ?"
};
spam.Open();
newSpam.Parameters.AddWithValue("text", adSpecialText);
reader?.Close();
reader = newSpam.ExecuteReader();
while (reader != null && reader.Read())
{
id = int.Parse(reader[0].ToString());
}
reader?.Close();
spam.Close();
}
catch (OleDbException e)
{
MessageBox.Show(e.Message);
throw;
}
finally
{
oleDbConnection.Close();
}
return id;
return 0;
}
private string GetComments(int dateId)
@@ -375,12 +455,33 @@ namespace AdvertsingProfitControl
return comments;
}
public string RetrieveAdItemId(string adItemName)
public string RetrieveAdItemName(int id)
{
var dbT = new DatabaseTracker();
var dbR = new DatabaseReader();
return dbR.RetrieveAdItemId(adItemName, dbT.DatabaseConnectionString);
var oleDbCommand = new OleDbCommand()
{
CommandText = "SELECT AdItem.AdItem FROM AdItem WHERE AdItem.ID = ?"
};
oleDbCommand.Parameters.AddWithValue("ding", id);
var connection = new OleDbConnection(_connectionString);
oleDbCommand.Connection = connection;
var adItemName = "";
using (connection)
{
using (oleDbCommand)
{
connection.Open();
using (var reader = oleDbCommand.ExecuteReader())
{
while (reader != null && reader.Read())
{
adItemName = reader[0].ToString();
}
}
}
connection.Close();
}
return adItemName;
}
private int GetDateIdByDateString(string dateString)
@@ -418,7 +519,7 @@ namespace AdvertsingProfitControl
var dataTable = new DataTable();
var oleDbCommand = new OleDbCommand
{
CommandText = "SELECT AdItem.AdItem, APC.ProjectionSold, APC.ProjectionSalePrice, APC.ProjectionTotalSales, APC.ProjectionCost, APC.ProjectionProfitReturn, APC.ProjectionTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
CommandText = "SELECT AdItem.AdItem, Projections.Sold, Projections.SalePrice, Projections.TotalSales, Projections.Cost, Projections.ProfitReturn, Projections.TotalProfitReturn, Projections.RowAttribute, Projections.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Projections ON AdItem.ID = Projections.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
};
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
var connection = new OleDbConnection(_connectionString);
@@ -444,7 +545,7 @@ namespace AdvertsingProfitControl
var dataTable = new DataTable();
var oleDbCommand = new OleDbCommand()
{
CommandText = "SELECT AdItem.AdItem, APC.BeginingInventory, APC.Received, APC.TotalInventory, APC.EndingInventory, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
CommandText = "SELECT AdItem.AdItem, Inventory.BeginningInventory, Inventory.Received, Inventory.TotalInventory, Inventory.EndingInventory, Inventory.RowAttribute, Inventory.FK_AdSpecialGroupName FROM (AdItem INNER JOIN Inventory ON AdItem.ID = Inventory.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
};
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
var connection = new OleDbConnection(_connectionString);
@@ -470,7 +571,7 @@ namespace AdvertsingProfitControl
var dataTable = new DataTable();
var oleDbCommand = new OleDbCommand()
{
CommandText = "SELECT AdItem.AdItem, APC.ActualSold, APC.ActualSalePrice, APC.ActualTotalSales, APC.ActualCost, APC.ActualProfitReturn, APC.ActualTotalProfitReturn, APC.RowAttribute, APC.FK_GroupID FROM (AdItem INNER JOIN APC ON AdItem.ID = APC.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
CommandText = "SELECT AdItem.AdItem, ActualSales.Sold, ActualSales.SalePrice, ActualSales.TotalSales, ActualSales.Cost, ActualSales.ProfitReturn, ActualSales.TotalProfitReturn, ActualSales.RowAttribute, ActualSales.FK_AdSpecialGroupName FROM (AdItem INNER JOIN ActualSales ON AdItem.ID = ActualSales.FK_AdItemID) WHERE FK_DateID = ? ORDER BY RowPosition ASC"
};
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
var connection = new OleDbConnection(_connectionString);