public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; //Populate the sale detail model foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Obtain the information of the specific product var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of { detail.ProductId } could not be found in the database."); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } //Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //Save the sale model SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "RMData"); //Obtain the Sale Id for the detail models sale.Id = sql.LoadData <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "RMData").FirstOrDefault(); //Finish populating the sale detail models foreach (var item in details) { item.SaleId = sale.Id; //Save the detail models sql.SaveData("dbo.spSaleDetail_Insert", item, "RMData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: make this solid/dry //start filling in models to be saved to the database var details = new List <SaleDetailDBModel>(); var products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} could net be found in the database"); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } //create the sale model var sale = new SaleDBModel() { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId, }; sale.Total = sale.SubTotal + sale.Tax; //save the sale model SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "RRMData"); //get the id from the sale model sale.Id = sql.LoadData <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }, "RRMData").FirstOrDefault(); //finish filling it the sale model foreach (var item in details) { item.SaleId = sale.Id; //save the sale detail models sql.SaveData("dbo.spSaleDetail_Insert", item, "RRMData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Make this SOLID/DRY/BETTER List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; //fill in the available information foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel() { ProductId = item.ProductId, Quantity = item.Quantity }; //Get info about this product var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product id of {detail.ProductId} could not be found in the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //Create Sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //Save the sale model SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "TRMData"); //Get the ID from sale model sale.Id = sql.LoadData <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "TRMData").FirstOrDefault(); //Finish filling in the sale detail model foreach (var item in details) { item.SaleId = sale.Id; //Save the sale detail model sql.SaveData("dbo.spSaleDetail_Insert", item, "TRMData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; var productInfo = _productData.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database."); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; try { _sql.StartTransaction("KRMData"); _sql.SaveDataInTransaction("dbo.spSale_Insert", sale); sale.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); details.ForEach(item => { item.SaleId = sale.Id; _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); }); _sql.CommitTransaction(); } catch { _sql.RollbackTransaction(); throw; } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Make this SOLID / DRY / BETTER //Start filling in the models we sill save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() * (decimal)0.01; foreach (var item in saleInfo.SaleDetails) { var productInfo = _productData.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of { item.ProductId } could not be found in the database."); } var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, PurchasePrice = productInfo.RetailPrice * item.Quantity, }; detail.Tax = !productInfo.IsTaxable ? 0 : (productInfo.RetailPrice * item.Quantity * taxRate); details.Add(detail); } //Create the SaleModel SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //Save the SaleModel try { _sqlDataAccess.StartTransaction("TRMData"); _sqlDataAccess.SaveDataInTransaction("dbo.spSale_Insert", sale); sale.Id = _sqlDataAccess.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; _sqlDataAccess.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } _sqlDataAccess.CommitTransaction(); } catch { _sqlDataAccess.RollBackTransaction(); throw; } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: make this SOLID/DRY/better //Start filling the models with data which we will save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDeatils) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get the information for the product var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of { detail.ProductId } could not be found in the database"); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } //Crate the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //save the sale model SqlDataAccess data = new SqlDataAccess(); data.SaveData("dbo.spSale_Insert", sale, "BRMData"); //Get the SaleId sale.Id = data.LoadData <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "BRMData").FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; //save the sale detail model data.SaveData("dbo.spSaleDetail_Insert", item, "BRMData"); } }
public void SaveSale(SaleModel saleInfo, string userId) { //Fill in available sale details to save to the database List <SaleDetailDBModel> saleDetails = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; //Get product information var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product { item.ProductId } could not be fount in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } saleDetails.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = saleDetails.Sum(x => x.PurchasePrice), Tax = saleDetails.Sum(x => x.Tax), CashierId = userId }; sale.Total = sale.SubTotal + sale.Tax; SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSaleInsert", sale, "SpendiData"); //Get Id from SaleModel sale.Id = sql.LoadData <int, dynamic>("spSaleLookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }, "SpendiData").FirstOrDefault(); foreach (var item in saleDetails) { item.SaleId = sale.Id; sql.SaveData("dbo.spSaleDetailInsert", item, "SpendiData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get the info about product var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new System.Exception($"The product Id of {item.ProductId} is not found in DB."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { sql.StartTransaction("RMData"); sql.SaveDataInTransaction("dbo.spSale_Insert", sale); sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailsDBModel> details = new List <SaleDetailsDBModel>(); decimal taxRate = GetTaxRate(); foreach (var item in saleInfo.SaleDetails) { var saleDetails = new SaleDetailsDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; var productInfo = _prodcutData.GetProductById(item.ProductId); saleDetails.PurchasePrice = productInfo.RetailPrice * saleDetails.Quantity; if (productInfo.IsTaxable) { saleDetails.Tax = saleDetails.PurchasePrice * taxRate; } details.Add(saleDetails); } var saleToDb = new SaleDBModel() { CashierId = cashierId, SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax) }; saleToDb.Total = saleToDb.SubTotal + saleToDb.Tax; try { _sql.StartTransaction("RMData"); _sql.SaveDataInTransaction("dbo.spSale_Insert", saleToDb); saleToDb.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { saleToDb.CashierId, saleToDb.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = saleToDb.Id; _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } _sql.CommitTransaction(); } catch { _sql.RollbackTransaction(); throw; } }
private SaleDBModel PopulateSaleDBModel(List <SaleDetailDBModel> details, string cashierId) { SaleDBModel sale = new SaleDBModel { CashierId = cashierId, Subtotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax) }; sale.Total = sale.Subtotal + sale.Tax; return(sale); }
//public List<ProductModel> GetProducts() //{ // // var output = sql.LoadData<ProductModel, dynamic>("dbo.spProduct_GetAll", new { }, "BitComRMData"); // return output; //} public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTexRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = (new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }); var productInfo = products.GetById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; SqlDataAccess sql = new SqlDataAccess(); sql.SaveData <SaleDBModel>("dbo.spSale_Insert", sale, "BitComRMData"); sale.Id = sql.LoadData <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "BitComRMData").FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveData("dbo.spSaleDetail_Insert", item, "BitComRMData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { // TODO: Make this better List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData product = new ProductData(); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; var productInfo = product.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"ProductID {detail.ProductId} can't be found"); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * ConfigHelper.GetTaxRate(); } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "RMData"); sale.Id = sql.LoadData <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }, "RMData").FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveData("dbo.spSaleDetail_Insert", item, "RMData"); } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Improve - Make this SOLID/DRY // start filling in sale detailmodels we will save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //get info about product var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} come not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //create sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); //save sale model sql.SaveDataInTransaction("dbo.spSale_Insert", sale); //get id from sale model - assume we have it sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); //finish filling in the sale detail models foreach (var item in details) { item.SaleId = sale.Id; //save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } }; }
public void SaveSale(SaleModel saleInfo, string cashierId) { // Start filling in the sales detail models with save to database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get information about the product sold var productInfo = _productData.GetProductById(detail.ProductId); // Fill in the available information if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database."); } else { detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = productInfo.RetailPrice * detail.Quantity * taxRate; } else { detail.Tax = 0; } } details.Add(detail); } // Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; try { _sql.StartTransaction("PRMData"); // Save the sale model _sql.SaveDataInTransaction("[dbo].[spSale_Insert]", sale); // Get the Id from the sale Model sale.Id = _sql.LoadDataInTransaction <int, dynamic>("[dbo].[spSale_Lookup]", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); // Finish filling in the sale detail model foreach (var item in details) { item.SaleId = sale.Id; // save the sale detail model _sql.SaveDataInTransaction("[dbo].[spSaleDetail_Insert]", item); } _sql.CommitTransaction(); } catch { _sql.RollBackTransaction(); throw; } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: make a SOLID/DRY/better // start filling in the models which we need to save in the database. //fill in the available information List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get the information about this product var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); //Save the sale model sql.SaveDataInTransaction <SaleDBModel>("dbo.spSale_Insert", sale); //getting ID from the sale mode sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); //finish filling in the sale detail models. //for 1000s of calls. use advanced dapper. where you transfter a table . foreach (var item in details) { item.SaleId = sale.Id; //save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(_config); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //Wrap the code below in a transaction. So all of the process complete or none of it complete. //This is to avoid having incomplete/corrupt data in the SQL server incase part of the process //completes and another part fails. //Uses a C# transaction, meaning C# will handle the opening and closing of the transaction. //This is to be used sparingly. Most of the time you do not want to open transaction in C# //Instead transaction should be open in the SQL side. As you can leave a connection open //and forget to close it, making proformance to be reduced significantly. //using make it so all of the calls and made together. //The end of the using statement will close the connection and use the dispose method //However, we place the commitTransaction at the end anyways to help visability //if the transaction fail then we catch the failure and do a rollback. using (SqlDataAccess sql = new SqlDataAccess(_config)) { try { sql.StartTransaction("TSDatabase"); sql.SaveData("dbo.spSale_Insert", sale, "TSDatabase"); sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxtRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; var productInfo = _productData.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"{item.ProductId} brak w bazie"); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; try { _sqlDataAccess.StartTransaction("RMData"); _sqlDataAccess.SavedData <SaleDBModel>("dbo.sqlSale_Insert", sale, "RMData"); sale.Id = _sqlDataAccess.LoadDataInTransaction <int, dynamic>("sqlSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; _sqlDataAccess.SavedDataInTransaction("dbo.sqlSaleDetail_Insert", item); } _sqlDataAccess.ComitTransaction(); } catch { _sqlDataAccess.RollBackTransaction(); throw; } }
public async Task SaveSale(SaleModel saleInfo, string cashierId) { // TODO: Make it Solid/DRY/Better // Start filling in the models we will save to the database // Fill in the available info List <SaleDetailDBModel> saleDetails = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about this product var productInfo = await products.GetProductById(detail.ProductId).ConfigureAwait(false); if (productInfo == null) { throw new Exception($"the product id of {detail.ProductId} couldn't found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } saleDetails.Add(detail); } // Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = saleDetails.Sum(x => x.PurchasePrice), Tax = saleDetails.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); // Save the sale model await sql.SaveDataInTransaction("dbo.spSale_Insert", sale).ConfigureAwait(false); // Get the ID from the sale model sale.Id = (await sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).ConfigureAwait(false)) .FirstOrDefault(); // Finish filling in the sale detail models foreach (var item in saleDetails) { item.SaleId = sale.Id; // Save the sale detail models await sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item).ConfigureAwait(false); } sql.CommitTransaction(); } catch { sql.RollBackTransaction(); throw; } } }
//public List<ProductModel> GetProducts() //{ // SqlDataAccess sql = new SqlDataAccess(); // var output = sql.LoadData<ProductModel, dynamic>("dbo.spProduct_GetAll", new { }, "TRMDATA"); // return output; //} public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Make this SOLID/DRY/Better // Start filling int the models we will save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData product = new ProductData(); var taxRate = ConfigHelper.GetTaxRate(); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about this product var productInfo = product.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * (taxRate / 100)); } details.Add(detail); } // Fill in the available information // Create the Sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), UserId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; // NEW WAY WITH TRANSACTION: using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); // Save the Sale model sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // Get the ID from the Sale Model sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.UserId, sale.SaleDate }).FirstOrDefault(); // Finish filling in the Sale Detail Models foreach (var item in details) { item.SaleId = sale.Id; // Save the Sale Detail Models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } //// OLD WAY WITHOUT TRANSACTION: // Save the Sale model //SqlDataAccess sql = new SqlDataAccess(); //sql.SaveData("dbo.spSale_Insert", sale, "TRMData"); //// Get the ID from the Sale Model //sale.Id = sql.LoadData<int, dynamic>("spSale_Lookup", new { CashierId = sale.UserId, sale.SaleDate }, "TRMData").FirstOrDefault(); //// Finish filling in the Sale Detail Models //foreach (var item in details) //{ // item.SaleId = sale.Id; // // Save the Sale Detail Models // sql.SaveData("dbo.spSaleDetail_Insert", item, "TRMData"); //} }
/// <summary> /// Saves the sale model to database /// </summary> /// <param name="saleInfo"> is a SaleModel received from the API</param> /// public void SaveSale(SaleModel saleInfo, string cashierId) { // Note, We do not trust the frontend, therefore we don't get all the infos from there. // TODO: Make this SOLID/DRY/Better // Start filling in the Sale Detail models we will save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; //Get the info about this product var productInfo = _productData.GetProductById(detail.ProductId); // Fill in the available information (SaleId is not get yet.) if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // Create the Sale model (Consits of several items in the shopping cart.) SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; // Saving into sale and saledetail tables happen in one transaction try { _sql.StartTransaction("TRMData"); // Save the Sale model _sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // Get the Sale ID from the Sale model - dbo.spSale_Insert has an output variable Id, but "sale.id" do not get it. // Therefore an other query needs to be run. sale.Id = _sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // Finish filling the Sale detail models foreach (var item in details) { item.SaleId = sale.Id; // Save the sale detail models _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } _sql.CommitTransaction(); } catch { _sql.RollbackTransaction(); throw; // It throws the original exception, from the deep --> more info } }
public void SaveSale(SaleModel saleInfo, string cashierId) { // Todo: Make this S.O.L.I.D!!! // 1.) Create the models to be saved to the database and // fill in the available information. List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = (ConfigHelper.GetTaxRate() / 100); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about the products from the database var productInfo = products.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} could not be found in the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // 3.) Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("RMDatabase"); // 4. Save the sale model to the database sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // 5.) Getting the sale id sale.Id = sql.LoadDataInTransaction <int, dynamic>( "spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); // 6.) Finish filling in the SaleDetailModel and Save the sale detail models foreach (var item in details) { item.SaleId = sale.Id; // In cases where the database is going to be called thousands of times, // it's possible to use "table value parameters" here instead and save the whole table. sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleAPIInput, string cashierId) { System.Diagnostics.Debug.WriteLine("Start SaveSale"); // Filling in the sale deal models we'll save to the db List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData productDataAccess = new ProductData(config); foreach (var item in saleAPIInput.SaleDetails) { // Fill in the available info System.Diagnostics.Debug.WriteLine("item.ProductId " + item.ProductId); System.Diagnostics.Debug.WriteLine(" item.Quantity " + item.Quantity); System.Diagnostics.Debug.WriteLine(" cashierId " + cashierId); var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; var productInfo = productDataAccess.GetProductById(item.ProductId); System.Diagnostics.Debug.WriteLine("productInfo " + productInfo); System.Diagnostics.Debug.WriteLine("productInfo RetailPrice " + productInfo.RetailPrice); if (productInfo == null) { throw new Exception($"The product id of {detail.ProductId} is not found in db"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); details.Add(detail); } // create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = 0, CashierId = cashierId }; sale.Total = sale.SubTotal; // save the model using (SqlDataAccess sql = new SqlDataAccess(config)) { try { sql.StartTransaction("KelvinData"); sql.SaveDataInTransaction <SaleDBModel>("dbo.spSale_Insert", sale); // Get the id from sale model int saleId = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // Finish filling in sale detail models. foreach (var item in details) { item.SaleId = saleId; // save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { // ToDo: Make this SOLID/DRY/Better // Start filling in the models we will save in the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate(); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about this product var productInfo = products.GetProductById(detail.ProductId); if (productInfo is null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database."); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = detail.PurchasePrice * taxRate; } details.Add(detail); } // Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("RMData"); // Save the sale model sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // Get the Id from the sale model sale.Id = sql .LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }) .FirstOrDefault(); // Finish filling in the sale detail models details.ForEach(item => { item.SaleId = sale.Id; // Save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); }); sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { // TODO: make this SOLID/DRY/better // Start filling in the sale detail models we will save to the db List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = (new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }); // get the info about this product var productInfo = _productData.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product ID of {detail.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // fill in the available info // create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; // C# TRANSACTIONS ---- NOT SQL (use very rarely or not at all) // open transactions = bad for memory and more // prefferably SQL will handle the transaction try { _sql.StartTransaction("TRMData"); // save the sale model _sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // get the id from the sale model sale.Id = _sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // finish filling in the sale detail models foreach (var item in details) { item.SaleId = sale.Id; // save the sale detail models _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } _sql.CommitTransaction(); } catch { _sql.RollbackTransaction(); throw; } }
public void SaveSale(SaleModel saleInfo, string cashierId) { // TODO:: Make this SOLID/DRY/Better List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = GetTaxRate(); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get information about this product var productInfo = _productData.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found in the database"); } detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity; if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; try { _sql.StartTransaction("TRMData"); //Save the model _sql.SaveDataInTransaction <SaleDBModel>("dbo.spSale_Insert", sale); // Get the Id from the sale model sale.Id = _sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // Finish filling in the sale details models foreach (var item in details) { item.SaleId = sale.Id; // Save the sale details models _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } //Can explicitly call but it will implicitly close after using statment finished. _sql.CommitTransaction(); } catch { _sql.RollbackTransaction(); throw; } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Make this DRY/SOLID/Better ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; //Start filling the sale detail models we will save in the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get the information about this product //Question: Why are we getting productInfo from DB? //Answer: To validate calculations of taxes and purchace price at server side to //get protection from probable bad behaviour of some users var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The Product Id of {detail.ProductId} could not be found in the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //Fill in the available information //Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("PrimoData"); //Save the sale model sql.SaveDataInTransaction("dbo.spSale_Insert", sale); //Get the Id from the sale model sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); //Finish Filling in the sale detail models foreach (var item in details) { item.SaleId = sale.Id; //Save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollBackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: Make this SOLID/DRY/Better //Start filling in the models we will save to the database List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity, }; var productInfo = _productData.GetProductById(item.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {item.ProductId} could not be found in the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //Fill in the available information // Create the Sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; // Save the sale model try { _sql.StartTransaction("MRMData"); _sql.SaveDataInTransaction("dbo.spSale_Insert", sale); // Get the ID from the sale model sale.Id = _sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // Finish filling the sale detail model foreach (var item in details) { item.SaleId = sale.Id; // Save the sale detail models. _sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } _sql.CommitTransaction(); } catch { _sql.RollBackTransaction(); throw; } }
public void SaveSale(SaleModel sale, string userId) { // TODO: Make this SOLID/DRY/Better // Start in filling in the sale detail models we will save to the database List <SaleDetailDBModel> saleDetails = new List <SaleDetailDBModel>(); ProductData productData = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in sale.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about this product var productInfo = productData.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of {detail.ProductId} could not be found it the database."); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } saleDetails.Add(detail); } // Create the sale model SaleDBModel saleToSave = new SaleDBModel { SubTotal = saleDetails.Sum(x => x.PurchasePrice), Tax = saleDetails.Sum(x => x.Tax), CachierId = userId // Sale date is automaticaly added in db }; saleToSave.Total = saleToSave.SubTotal + saleToSave.Tax; #region Old - Replaced with transaction //// Save sale model //SqlDataAccess sql = new SqlDataAccess(); //sql.SaveData<SaleDBModel>("dbo.spSaleInsert", saleToSave, "TRMData"); ////Get the ID from sale model // i dont want to have id in model because of identity increment //var saleToSave_Id = sql.LoadData<int, dynamic>("spSaleLookup", new { CachierId = saleToSave.CachierId, SaleDate = saleToSave.SaleDate }, "TRMData").FirstOrDefault(); //// Finish filling in the sale detail models //foreach (var item in saleDetails) //{ // item.SaleId = saleToSave_Id; // // Save the sale detail model // sql.SaveData("dbo.spSaleDetailInsert", item, "TRMData"); //} #endregion using (SqlDataAccess sql = new SqlDataAccess()) // ok { try { sql.StartTransaction("TRMData"); // Save the sale model sql.SaveDataInTransaction("dbo.spSaleInsert", saleToSave); //Get the ID from sale model var saleToSave_Id = sql.LoadDataInTransaction <int, dynamic>("spSaleLookup", new { CachierId = saleToSave.CachierId, SaleDate = saleToSave.SaleDate }).FirstOrDefault(); // Finish filling in the sale detail models foreach (var item in saleDetails) { item.SaleId = saleToSave_Id; // Save the sale detail model sql.SaveDataInTransaction("dbo.spSaleDetailInsert", item); } sql.CommitTransaction(); } catch (Exception exc) { sql.RollbackTransaction(); throw exc; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData product = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //Get info about the product var productInfo = product.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product Id of { detail.ProductId } could nor be found in the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // Create the sale model SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), cashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); // Save the sale model sql.SaveDataInTransaction("dbo.spSale_Insert", sale); sale.Id = sql.LoadDataInTransaction <int, dynamic>("spSale_Lookup", new { CashierId = sale.cashierId, sale.SaleDate }).FirstOrDefault(); // Finish filling in the detail models foreach (var item in details) { item.SaleId = sale.Id; // Save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransaction(); } catch { sql.RollbackTransaction(); throw; } } }
public void SaveSale(SaleModel saleInfo, string cashierId) { //TODO: make this solid/dry/better //start filling in models we will save to db List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (var item in saleInfo.SaleDetails) { var detail = new SaleDetailDBModel { ProductId = item.ProductId, Quantity = item.Quantity }; //get the info about this product var productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"The product id of {detail.ProductId} could not be found in the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } //fill in available info //create salemodel SaleDBModel sale = new SaleDBModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId }; sale.Total = sale.SubTotal + sale.Tax; //save the sale model SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "DocuData"); //public List<ProductModel> Getproducts() //{ // SqlDataAccess sql = new SqlDataAccess(); // var output = sql.LoadData<ProductModel, dynamic>("dbo.spProductLookup_GetAll", new { }, "DocuData"); // return output; //} //get id from sale model sale.Id = sql.LoadData <int, dynamic>("dbo.spSale_LookUp", new { sale.CashierId, sale.SaleDate }, "DocuData").FirstOrDefault(); //finish filling in the sale detail models foreach (var item in details) { item.SaleId = sale.Id; //save the sale deatil model sql.SaveData("dbo.spSaleDetail_Insert", item, "DocuData"); } }