public void UpdateInfo(StockTrx newInfo) { Name = newInfo.Name; Emiten = newInfo.Emiten; Lot = newInfo.Lot; BuyPrice = newInfo.BuyPrice; }
public void AddStock(StockTrx trx, int lot, int price) { bool addSelf = false; if (trx.StockTrxAdditions == null) { addSelf = true; trx.StockTrxAdditions = new List <StockTrxAddition>(2); } else if (trx.StockTrxAdditions.Count == 0) { addSelf = true; } if (addSelf) { trx.StockTrxAdditions.Add(new StockTrxAddition { StockTrxId = trx.Id, Lot = trx.Lot, BuyPrice = trx.BuyPrice }); } trx.StockTrxAdditions.Add(new StockTrxAddition { StockTrxId = trx.Id, Lot = lot, BuyPrice = price }); trx.Lot += lot; trx.BuyPrice = trx.StockTrxAdditions.Sum(x => x.BuyPrice * x.Lot) / trx.StockTrxAdditions.Sum(x => x.Lot); }
public async Task <IActionResult> Create([Bind("Id,Name,State,Lot,BuyPrice,SellPrice,SoldAt,LessonLearned,Emiten")] StockTrx stockTrx) { if (ModelState.IsValid) { _context.Add(stockTrx); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(stockTrx)); }
public async Task <IActionResult> Add(long id, [Bind("Id,Lot,BuyPrice")] StockTrx stockTrx) { if (id != stockTrx.Id) { return(NotFound()); } long currentUserId = _userService.GetCurrentUserId(); var stockTrxDb = await _context.StockTrxs .Include(x => x.StockTrxAdditions) .FirstOrDefaultAsync(m => m.Id == id && m.CreatedBy == currentUserId); if (stockTrxDb.CreatedBy != currentUserId) { return(NotFound()); } if (ModelState.IsValid) { try { _stockService.AddStock(stockTrxDb, stockTrx.Lot, stockTrx.BuyPrice); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StockTrxExists(stockTrx.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(stockTrx)); }