public static int CreateStock(StockCreateViewModel model, string username) { int result = 0; DateTime current = DateTime.Now; Entities entities = new Entities(); //Create stock Assets stock = new Assets(); stock.AssetName = model.Name; stock.Note = model.Note; stock.CreatedDate = current; stock.CreatedBy = Constants.Constants.USER; stock.AssetType = (int)Constants.Constants.ASSET_TYPE.STOCK; stock.ObtainedBy = (int)Constants.Constants.OBTAIN_BY.CREATE; stock.Username = username; StockTransactions transaction = new StockTransactions(); transaction.Name = "Tạo cổ phiếu " + stock.AssetName; transaction.NumberOfShares = model.NumberOfStock.Value; transaction.SpotPrice = model.SpotRice.Value; transaction.Value = model.StockValue.Value; transaction.ExpectedDividend = model.ExpectedDividend.Value; transaction.Username = username; transaction.TransactionDate = current; transaction.TransactionType = (int)Constants.Constants.TRANSACTION_TYPE.CREATE; transaction.CreatedDate = current; transaction.CreatedBy = Constants.Constants.USER; if (model.IsInDebt) { if (model.Liabilities != null && model.Liabilities.Liabilities.Count > 0) { foreach (var liabilityViewModel in model.Liabilities.Liabilities) { Liabilities liability = new Liabilities(); liability.Name = liabilityViewModel.Source; liability.Value = liabilityViewModel.Value.Value; liability.InterestType = liabilityViewModel.InterestType; liability.InterestRate = liabilityViewModel.InterestRate.Value; liability.InterestRatePerX = liabilityViewModel.InterestRatePerX; liability.StartDate = liabilityViewModel.StartDate.Value; liability.EndDate = liabilityViewModel.EndDate.Value; liability.LiabilityType = (int)Constants.Constants.LIABILITY_TYPE.STOCK; liability.CreatedDate = current; liability.CreatedBy = Constants.Constants.USER; liability.Username = username; transaction.Liabilities.Add(liability); stock.Liabilities.Add(liability); } } } stock.StockTransactions.Add(transaction); entities.Assets.Add(stock); result = entities.SaveChanges(); return(result); }
public StockTransaction PostStockTransaction(StockTransaction stockTransaction) { var item = StockTransactions.FirstOrDefault(c => c.StockTransactionId == stockTransaction.StockTransactionId); var accountTransaction = GetAccountTransactionByStockTransactionId(stockTransaction.StockTransactionId); if (item != null) { item = stockTransaction; } else { stockTransaction.StockTransactionId = GenerateStockTransactionId(); StockTransactions.Add(stockTransaction); } if (accountTransaction == null) { accountTransaction = PostAccountTransaction(new AccountTransaction { Amount = stockTransaction.Amount * stockTransaction.UnitPrice * (stockTransaction.TransactionType == TransactionType.Buy ? -1 : 1), Date = stockTransaction.Date, StockTransactionId = stockTransaction.StockTransactionId, StockTransaction = stockTransaction, AccountId = Session.DefaultAccount.AccountId }); } else { accountTransaction.Amount = stockTransaction.Amount * stockTransaction.UnitPrice * (stockTransaction.TransactionType == TransactionType.Buy ? -1 : 1); } stockTransaction.AccountTransactionId = accountTransaction.AccountTransactionId; return(stockTransaction); }
public int GenerateStockTransactionId() { if (StockTransactions.Count > 0) { return(StockTransactions.Max(c => c.StockTransactionId) + 1); } return(1); }
public void DeleteStockTransaction(int stockTransactionId) { var stockTransaction = GetStockTransaction(stockTransactionId); if (AccountTransactions.Any(c => c.AccountTransactionId == stockTransaction.AccountTransactionId)) { AccountTransactions.Remove(AccountTransactions.First(c => c.AccountTransactionId == stockTransaction.AccountTransactionId)); } StockTransactions.Remove(stockTransaction); Save(); }
public StockTransaction GetStockTransaction(int stockTransactionId) { if (StockTransactions.Any(c => c.StockTransactionId == stockTransactionId)) { var result = StockTransactions.First(c => c.StockTransactionId == stockTransactionId); if (result.AccountTransaction == null) { result.AccountTransaction = AccountTransactions.FirstOrDefault(c => c.AccountTransactionId == result.AccountTransactionId); } if (result.Stock == null) { result.Stock = GetStock(result.StockCode); } return(result); } return(null); }
public AccountTransaction GetAccountTransaction(int accountTransactionId) { if (AccountTransactions.Any(c => c.AccountTransactionId == accountTransactionId)) { var result = AccountTransactions.First(c => c.AccountTransactionId == accountTransactionId); if (result.Account == null) { result.Account = GetAccount(result.AccountId); } if (result.StockTransaction == null && result.StockTransactionId.HasValue) { result.StockTransaction = StockTransactions.FirstOrDefault(c => c.StockTransactionId == result.StockTransactionId.Value); } return(result); } return(null); }
public StockTransaction GetStockTransactionByAccountTransactionId(int accountTransactionId) { if (StockTransactions.Any(c => c.AccountTransactionId == accountTransactionId)) { var result = StockTransactions.First(c => c.AccountTransactionId == accountTransactionId); if (result.AccountTransaction == null) { result.AccountTransaction = GetAccountTransaction(result.AccountTransactionId); } if (result.Stock == null) { result.Stock = GetStock(result.StockCode); } return(result); } return(null); }
public static StockTransactionViewModel CreateViewModel(StockTransactions transaction) { Entities entities = new Entities(); StockTransactionViewModel model = new StockTransactionViewModel(); model.NumberOfStock = transaction.NumberOfShares; model.SpotRice = transaction.SpotPrice; model.StockValue = transaction.Value; model.ExpectedDividend = transaction.ExpectedDividend / 100; var liabilities = entities.Liabilities.Where(x => x.TransactionId == transaction.Id); foreach (var liability in liabilities.Where(x => !x.DisabledDate.HasValue)) { StockLiabilityViewModel liabilityViewModel = StockLiabilityQueries.CreateViewModel(liability); model.Liabilities.Liabilities.Add(liabilityViewModel); } return(model); }
public static int UpdateStock(StockUpdateViewModel model) { Entities entities = new Entities(); var stock = entities.Assets.Where(x => x.Id == model.Id).FirstOrDefault(); stock.AssetName = model.Name; stock.Note = model.Note; entities.Assets.Attach(stock); entities.Entry(stock).State = System.Data.Entity.EntityState.Modified; StockTransactions transaction = entities.StockTransactions.Where(x => x.AssetId == model.Id).FirstOrDefault(); transaction.Name = "Tạo cổ phiếu " + stock.AssetName; transaction.NumberOfShares = model.NumberOfStock.Value; transaction.SpotPrice = model.SpotRice.Value; transaction.Value = model.StockValue.Value; transaction.ExpectedDividend = model.ExpectedDividend.Value; entities.StockTransactions.Attach(transaction); entities.Entry(transaction).State = System.Data.Entity.EntityState.Modified; return(entities.SaveChanges()); }