public void SaveSale(SaleModel sale, string cashierId) { ProductData productData = new ProductData(); List <SaleDetailDbModel> details = new List <SaleDetailDbModel>(); //Create a model for insertion in DB for each sale detail model that was posted to the api foreach (SaleDetailModel item in sale.saleDetails) { ProductModel productInfo = productData.GetProductById(item.Id); if (productInfo == null) { throw new System.Exception($"The product with id {item.Id} could not be found in database"); } decimal tax = 0; decimal purchasePrice = productInfo.RetailPrice * item.Quantity; if (productInfo.IsTaxable) { tax = purchasePrice * Convert.ToDecimal(ConfigHelper.GetTaxRate()); } details.Add(new SaleDetailDbModel { ProductId = item.Id, Quantity = item.Quantity, PurchasePrice = purchasePrice, Tax = tax, }); } //create the sale model SaleDbModel saleDbModel = new SaleDbModel { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = cashierId, }; saleDbModel.Total = saleDbModel.SubTotal + saleDbModel.Tax; using (SqlDataAccess dataAccess = new SqlDataAccess()) { try { dataAccess.StartTransaction("TRMData"); dataAccess.SaveDataInTransaction("dbo.spSale_Insert", saleDbModel); saleDbModel.Id = dataAccess.LoadDataInTransaction <int, dynamic>("dbo.spLookUp", new { CashierId = cashierId, saleDbModel.SaleDate }).FirstOrDefault(); //Finish filling in the sale detail models foreach (SaleDetailDbModel item in details) { item.SaleId = saleDbModel.Id; dataAccess.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } ; dataAccess.CommitTransaction(); } catch (Exception e) { dataAccess.RollBackTransaction(); throw; } } }
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(); ProductData products = new ProductData(); 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; SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSale_Insert", sale, "TRMData"); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveData("dbo.SaleDetail_Insert", item, "TRMData"); } }
//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"); //} }
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 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) { //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(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; using (SqlDataAccess sql = new SqlDataAccess(_config)) { try { sql.StartTransaction("TRMData"); sql.SaveDataInTranscation("dbo.spSale_Insert", sale); sale.Id = sql.LoadDataInTranscation <int, dynamic>("dbo.spSale_Lookup", new { sale.CashierId, sale.SaleDate }).FirstOrDefault(); foreach (var item in details) { item.SaleId = sale.Id; sql.SaveDataInTranscation("dbo.spSaleDetail_Insert", item); } sql.CommitTranscation(); } catch { sql.RollbackTranscation(); 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 sale, string userId) { List <SaleDetailDBModel> details = new List <SaleDetailDBModel>(); ProductData products = new ProductData(); var taxRate = ConfigHelper.GetTaxRate() / 100; foreach (SaleDetailModel item in sale.SaleDetails) { SaleDetailDBModel detail = new SaleDetailDBModel() { ProductId = item.ProductId, Quantity = item.Quantity }; // Get the information about this product ProductModel productInfo = products.GetProductById(detail.ProductId); if (productInfo == null) { throw new ArgumentNullException($"The ProductId of {detail.ProductId} could not be found on the database"); } detail.PurchasePrice = (productInfo.RetailPrice * detail.Quantity); if (productInfo.IsTaxable) { detail.Tax = (detail.PurchasePrice * taxRate); } details.Add(detail); } // Create the Sale model SaleDBModel saleDB = new SaleDBModel() { SubTotal = details.Sum(x => x.PurchasePrice), Tax = details.Sum(x => x.Tax), CashierId = userId }; saleDB.Total = saleDB.SubTotal + saleDB.Tax; using (SqlDataAccess sql = new SqlDataAccess()) { try { sql.StartTransaction("TRMData"); // Save the sale model sql.SaveDataInTransaction <SaleDBModel>("dbo.spSale_Insert", saleDB); // Get the ID from the detail model saleDB.Id = sql.LoadDataInTransaction <int, dynamic>("dbo.spSale_Lookup", new { saleDB.CashierId, saleDB.SaleDate }).FirstOrDefault(); // Finish filling in the sale detai models foreach (var item in details) { item.SaleId = saleDB.Id; // Save the sale detail models sql.SaveDataInTransaction("dbo.spSaleDetail_Insert", item); } sql.CommitTransation(); } catch { sql.RollbackTransaction(); throw; } } }
/// <summary> /// Saves the sale model to database /// </summary> /// <param name="saleInfo">sale 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>(); 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); // 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 using (SqlDataAccess sql = new SqlDataAccess()) { 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 userId) { // TODO: Make this SOLID/DRY/Btter // Start filling in the sale detail models we will save to the database List <SaleDetailDbModel> details = new List <SaleDetailDbModel>(); ProductData product = new ProductData(config); 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 = product.GetProductById(detail.ProductId); if (productInfo == null) { throw new Exception($"El producto con Id { detail.ProductId } no se encuentra en la base de datos."); } 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 = userId }; sale.Total = sale.SubTotal + sale.Tax; using (SqlDataAccess sql = new SqlDataAccess(config)) { 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>("dbo.spSale_Lookup", new { CashierId = sale.CashierId, SaleDate = sale.SaleDate }).FirstOrDefault(); // Finish filling in the sale details 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; } } }