public bool SaveStockDetails(int userid ,int stockId, int SelectedStockTypeId, int quantity, decimal price) { try { using (var entityrepo = new UBSFundsDBEntities()) { using (var transaction = entityrepo.Database.BeginTransaction()) { try { if (stockId > 0) { var currentItem = entityrepo.tblStocks.Find(stockId); var selectedType = entityrepo.tblStockTypes.FirstOrDefault(s => s.Id == currentItem.StockType); if (currentItem != null) { currentItem.Price = price; currentItem.Quantity = quantity; currentItem.MarketValue = price * quantity; currentItem.TransactionCost = price * quantity * (selectedType.TransactionCharge ?? 0.0M); entityrepo.SaveChanges(); var latestEditItem = entityrepo.tblStocks.Find(currentItem.Id); if (latestEditItem != null) { var totalMarketValue = entityrepo.tblStocks.Sum(s => s.MarketValue) ?? 1; latestEditItem.StockWieght = ((latestEditItem.MarketValue / totalMarketValue)); entityrepo.SaveChanges(); } } } else { var count = entityrepo.tblStocks.Count(s => s.StockType == SelectedStockTypeId); var selectedType = entityrepo.tblStockTypes.FirstOrDefault(s => s.Id == SelectedStockTypeId); var StockName = string.Format("{0}{1}", selectedType != null ? selectedType.StockType : "Error", (count + 1)); var dbstockobj = new tblStock() { StockName = StockName, Price = price, UserId = userid, Quantity = quantity, StockType = selectedType.Id, MarketValue = price * quantity, TransactionCost = price * quantity * (selectedType.TransactionCharge ?? 0.0M) }; entityrepo.tblStocks.Add(dbstockobj); entityrepo.SaveChanges(); var latestEntryItem = entityrepo.tblStocks.Find(dbstockobj.Id); if (latestEntryItem != null) { var totalMarketValue = entityrepo.tblStocks.Sum(s => s.MarketValue) ?? 1; latestEntryItem.StockWieght = (latestEntryItem.MarketValue / (totalMarketValue!=0?totalMarketValue:1)); entityrepo.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } return true; } catch (Exception ex) { return false; } }
public bool DeleteStockDetails(StockDetail stock) { try { using (var entityrepo = new UBSFundsDBEntities()) { var delStock = entityrepo.tblStocks.Find(stock.StockId); if (stock != null) { entityrepo.tblStocks.Remove(delStock); entityrepo.SaveChanges(); return true; } return false; } } catch (Exception ex) { return false; } }