//[ValidateAntiForgeryToken] public ActionResult Create(FXtradeTable tradeTable, SummaryTable summaryTable, UsersData usersData) { if (ModelState.IsValid) { //存總表 db.SummaryTable.Add(summaryTable); db.SaveChanges(); //存外匯交易表 var uid = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault(); tradeTable.SummaryId = uid; db.FXtradeTable.Add(tradeTable); db.SaveChanges(); if (tradeTable.TradeClass == "賣出") { CashIncome cashincome = new CashIncome(); cashincome.UserName = Session["User"].ToString(); cashincome.OID = uid; cashincome.InCashType = summaryTable.TradeType; cashincome.InDate = summaryTable.TradeDate; cashincome.InNote = tradeTable.TradeClass; var change = tradeTable.NTD * -1; cashincome.InAmount = Convert.ToInt32(change); db.CashIncome.Add(cashincome); db.SaveChanges(); //更新個人資產表 TestHomeController th = new TestHomeController(); var name = summaryTable.UserName; var ud = th.GetUsersData(name); usersData.FXValue = th.SaveUserdata(name); usersData.UserName = ud[0].UserName; var cv = Math.Abs(tradeTable.NTD.Value); usersData.CashValue = ud[0].CashValue + tradeTable.NTD; usersData.InsuranceValue = ud[0].InsuranceValue; usersData.StockValue = ud[0].StockValue; usersData.FundValue = ud[0].FundValue; usersData.Email = ud[0].Email; usersData.Password = ud[0].Password; db.Entry(usersData).State = EntityState.Modified; db.SaveChanges(); } else { CashExpense cashexpense = new CashExpense(); cashexpense.UserName = Session["User"].ToString(); cashexpense.OID = uid; cashexpense.ExCashType = summaryTable.TradeType; cashexpense.ExDate = summaryTable.TradeDate; cashexpense.ExNote = tradeTable.TradeClass; if (tradeTable.TradeClass == "買入(不要連動新臺幣帳戶)") { cashexpense.ExAmount = 0; } else { cashexpense.ExAmount = Convert.ToInt32(tradeTable.NTD); } db.CashExpense.Add(cashexpense); db.SaveChanges(); //更新個人資產表 TestHomeController th = new TestHomeController(); var name = summaryTable.UserName; var ud = th.GetUsersData(name); usersData.FXValue = th.SaveUserdata(name); usersData.UserName = ud[0].UserName; var cv = Math.Abs(tradeTable.NTD.Value); if (tradeTable.TradeClass == "買入(不要連動新臺幣帳戶)") { usersData.CashValue = ud[0].CashValue; } else { usersData.CashValue = ud[0].CashValue - tradeTable.NTD; } usersData.InsuranceValue = ud[0].InsuranceValue; usersData.StockValue = ud[0].StockValue; usersData.FundValue = ud[0].FundValue; usersData.Email = ud[0].Email; usersData.Password = ud[0].Password; db.Entry(usersData).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Index")); } return(View(tradeTable)); }
public void Delete(int id) { FXtradeTable tt = dc.FXtradeTable.Find(id); if (tt == null) { return; } var name = Session["User"].ToString(); CashIncome cashincome = new CashIncome(); if (tt.TradeClass == "賣出") { CashExpense cashexpense = new CashExpense(); cashexpense.UserName = Session["User"].ToString(); var summary = dc.SummaryTable.Where(c => c.UserName == name).ToList(); cashexpense.OID = summary[0].STId; cashexpense.ExCashType = summary[0].TradeType; cashexpense.ExDate = summary[0].TradeDate; cashexpense.ExNote = "資料表刪除"; cashexpense.ExAmount = Convert.ToInt32(Math.Abs(tt.NTD.Value)); dc.CashExpense.Add(cashexpense); dc.FXtradeTable.Remove(tt); dc.SaveChanges(); UsersData usersData = new UsersData(); TestHomeController th = new TestHomeController(); var ud = th.GetUsersData(name); usersData.FXValue = th.SaveUserdata(name); usersData.UserName = ud[0].UserName; var cv = Math.Abs(tt.NTD.Value); usersData.CashValue = ud[0].CashValue - cv; usersData.InsuranceValue = ud[0].InsuranceValue; usersData.StockValue = ud[0].StockValue; usersData.FundValue = ud[0].FundValue; usersData.Email = ud[0].Email; usersData.Password = ud[0].Password; dc.Entry(usersData).State = EntityState.Modified; dc.SaveChanges(); } else { var summary = dc.SummaryTable.Where(c => c.UserName == name).ToList(); cashincome.UserName = name; cashincome.OID = summary[0].STId; cashincome.InCashType = summary[0].TradeType; cashincome.InDate = summary[0].TradeDate; cashincome.InNote = "資料表刪除"; var cv = Math.Abs(tt.NTD.Value); if (tt.TradeClass == "買入(不要連動新臺幣帳戶)") { cashincome.InAmount = 0; } else { cashincome.InAmount = Convert.ToInt32(cv); } dc.CashIncome.Add(cashincome); dc.FXtradeTable.Remove(tt); dc.SaveChanges(); UsersData usersData = new UsersData(); TestHomeController th = new TestHomeController(); var ud = th.GetUsersData(name); usersData.FXValue = th.SaveUserdata(name); usersData.UserName = ud[0].UserName; if (tt.TradeClass == "買入(不要連動新臺幣帳戶)") { usersData.CashValue = ud[0].CashValue; } else { usersData.CashValue = ud[0].CashValue + cv; } usersData.InsuranceValue = ud[0].InsuranceValue; usersData.StockValue = ud[0].StockValue; usersData.FundValue = ud[0].FundValue; usersData.Email = ud[0].Email; usersData.Password = ud[0].Password; dc.Entry(usersData).State = EntityState.Modified; dc.SaveChanges(); } }