using RMDataManagerLibrary.Internal.DataAccess; using RMDataManagerLibrary.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RMDataManagerLibrary.DataAcccess { public class SaleData { //TODO: Make this SOLID/DRY/BETTER public void SaveSale(SaleModel saleInfo, string cashierId) { List details = new List(); ProductData products = new ProductData(); decimal 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($"Product with Id {item.ProductId} could not be 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; //save SqlDataAccess sql = new SqlDataAccess(); sql.SaveData("dbo.spSaleInsert", sale, "RMData"); // get id from saleModel sale.Id = sql.LoadData("spSaleLookUp",new { sale.CashierId, sale.SaleDate }, "RMData").FirstOrDefault(); // finish filling up sale details foreach (var item in details) { item.SaleId = sale.Id; // Save Details sql.SaveData("dbo.spSaleDetailInsert", item, "RMData"); } } } }