Esempio n. 1
0
        public ActionResult CreateStockSell([Bind(Include = "TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote")] StockHistory stockHistory)
        {
            //([Bind(Include = "TradeType,date,userID")]
            //[Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote")]
            if (ModelState.IsValid)
            {
                //存入總表取得交易序號
                db.SummaryTable.Add(summaryTable);
                db.SaveChanges();
                var id = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();
                //存入現金帳戶
                CashIncome cashincome = new CashIncome();
                cashincome.OID        = id;
                cashincome.UserName   = summaryTable.UserName;
                cashincome.InCashType = summaryTable.TradeType;
                cashincome.InAmount   = (stockHistory.stockNetincome);
                cashincome.InDate     = summaryTable.TradeDate;
                cashincome.InNote     = "賣出股票";
                db.CashIncome.Add(cashincome);
                db.SaveChanges();


                //存入股票交易記錄表

                stockHistory.stockAmount = stockHistory.stockAmount * 1000;
                stockHistory.STId        = id;
                db.StockHistory.Add(stockHistory);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        //Edit Income
        public ActionResult EditIncome([Bind(Include = "InCashID,UserName,InCashType,InAmount,InDate,InNote")] CashIncome cashIncome)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }
            var username = Convert.ToString(Session["User"]);

            if (ModelState.IsValid)
            {
                //var CI = db.CashIncome.Where(c => c.UserName == username && c.InCashID == cashIncome.InCashID).Select(c => c).SingleOrDefault();
                //var stData = db.SummaryTable.Where(c => c.UserName == username && c.STId == CI.OID).Select(c => c).SingleOrDefault();
                //stData.TradeDate = cashIncome.InDate;
                //db.Entry(stData).State = EntityState.Modified;
                //var id = GetData(cashIncome.InCashID);
                //cashIncome.OID = id[0].OID;
                //cashIncome.InCashType = id[0].InCashType;
                //cashIncome.InDate = id[0].InDate;


                //更新userdata的cashvalue
                var Esum     = db.CashExpense.Sum(c => c.ExAmount).HasValue ? db.CashExpense.Sum(c => c.ExAmount):0;
                var Isum     = db.CashIncome.Sum(c => c.InAmount);
                var net      = (decimal)Isum - (decimal)Esum;
                var userdata = db.UsersData.Where(c => c.UserName == username).Select(c => c).SingleOrDefault();
                userdata.CashValue       = (double)net;
                db.Entry(userdata).State = EntityState.Modified;
                //更新收入表資料
                db.Entry(cashIncome).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(cashIncome));
        }
Esempio n. 3
0
        public ActionResult CreateStockSell([Bind(Include = "TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote")] StockHistory stockHistory, UsersData userdata)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }
            //([Bind(Include = "TradeType,date,userID")]
            //[Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote")]
            if (ModelState.IsValid)
            {
                //存入總表取得交易序號
                db.SummaryTable.Add(summaryTable);
                db.SaveChanges();
                var id = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();

                //存入現金帳戶
                CashIncome cashincome = new CashIncome();
                cashincome.OID        = id;
                cashincome.UserName   = summaryTable.UserName;
                cashincome.InCashType = summaryTable.TradeType;
                cashincome.InAmount   = (stockHistory.stockNetincome);
                cashincome.InDate     = summaryTable.TradeDate;
                cashincome.InNote     = "賣出股票";
                db.CashIncome.Add(cashincome);
                db.SaveChanges();
                //存入股票交易記錄表
                stockHistory.stockAmount = stockHistory.stockAmount * (-1000);
                AVGCalculator calculator = new AVGCalculator();
                string        _username  = Session["User"].ToString();
                string        _searchid  = stockHistory.stockID.ToString();
                int           _invchange = (int)stockHistory.stockAmount;
                int           _cashflow  = (int)stockHistory.stockNetincome;
                stockHistory.stockLastAVG = calculator.GetAvg(_username, _searchid, _invchange, _cashflow);
                stockHistory.STId         = id;
                stockHistory.stockTP      = 0;
                db.StockHistory.Add(stockHistory);
                db.SaveChanges();

                //計算與登錄股票現值
                var     name  = summaryTable.UserName;
                decimal pvsum = calculator.getPVsum(name);
                var     ud    = calculator.GetUserdata(name);

                userdata.StockValue      = (double)pvsum;
                userdata.UserName        = ud[0].UserName;
                userdata.CashValue       = ud[0].CashValue + Math.Abs((double)stockHistory.stockNetincome);
                userdata.Password        = ud[0].Password;
                userdata.Email           = ud[0].Email;
                userdata.FXValue         = ud[0].FXValue;
                userdata.InsuranceValue  = ud[0].InsuranceValue;
                userdata.FundValue       = ud[0].FundValue;
                db.Entry(userdata).State = EntityState.Modified;
                db.SaveChanges();


                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Index"));
        }
        // GET: CashIncomes/Create
        public IActionResult Create()
        {
            CreateInitial(0);
            CashIncome cashIncome = new CashIncome();

            cashIncome.IncomeDate = DateTime.Today;
            return(View(cashIncome));
        }
 //Edit Income
 public ActionResult EditIncome([Bind(Include = "InCashID,UserName,InCashType,InAmount,InDate,InNote")] CashIncome cashIncome)
 {
     if (ModelState.IsValid)
     {
         db.Entry(cashIncome).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(cashIncome));
 }
        //Delete
        public ActionResult DeleteIncome(int?id)
        {
            CashIncome obj      = db.CashIncome.Find(id);
            var        username = db.SummaryTable.Where(c => c.STId == obj.OID).Select(c => c).SingleOrDefault();

            db.SummaryTable.Remove(username);
            db.CashIncome.Remove(obj);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Esempio n. 7
0
        public ActionResult CreateEXD([Bind(Include = "TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote")] StockHistory stockHistory)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            //([Bind(Include = "TradeType,date,userID")]
            //[Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockNetincome,stockNote"])
            if (ModelState.IsValid)
            {
                db.SummaryTable.Add(summaryTable);
                db.SaveChanges();

                var id = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();
                //存入現金帳戶
                CashIncome cashincome = new CashIncome();
                cashincome.OID        = id;
                cashincome.UserName   = summaryTable.UserName;
                cashincome.InCashType = summaryTable.TradeType;
                cashincome.InAmount   = (stockHistory.stockNetincome);
                cashincome.InDate     = summaryTable.TradeDate;
                cashincome.InNote     = "除息收入";
                db.CashIncome.Add(cashincome);
                db.SaveChanges();

                //存入股票交易紀錄表
                stockHistory.STId = id;
                AVGCalculator calculator = new AVGCalculator();
                string        _username  = Session["User"].ToString();
                string        _searchid  = stockHistory.stockID.ToString();
                int           _invchange = (int)stockHistory.stockAmount;
                int           _cashflow  = (int)stockHistory.stockNetincome;
                stockHistory.stockLastAVG = calculator.GetAvg(_username, _searchid, _invchange, _cashflow);
                db.StockHistory.Add(stockHistory);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }


            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Create([Bind("CashIncomeId,ProductId,Description,Amount,IncomeDate")] CashIncome cashIncome)
        {
            if (ModelState.IsValid)
            {
                cashIncome.Product = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == cashIncome.ProductId);

                //product.Balance = product.Balance + cashIncome.Amount;
                cashIncome.Product.Balance = cashIncome.Product.Balance + cashIncome.Amount;

                cashIncome.StatusIncome = Models.Enum.StatusCashFlow.Activo;
                cashIncome.CreatedDate  = DateTime.Now;
                _context.Add(cashIncome);
                //_context.Update(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(cashIncome.ProductId);
            return(View(cashIncome));
        }
        public async Task <IActionResult> Edit(int id, [Bind("CashIncomeId,ProductId,Description,Amount,IncomeDate,CreatedDate,StatusIncome")] CashIncome cashIncome)
        {
            if (id != cashIncome.CashIncomeId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var cashIncomeOld = await _context.CashIncome.SingleOrDefaultAsync(c => c.CashIncomeId == cashIncome.CashIncomeId);

                    cashIncome.Product = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == cashIncome.ProductId);

                    if (cashIncome.Amount != cashIncomeOld.Amount)
                    {
                        var _amount = cashIncome.Amount - cashIncomeOld.Amount;
                        cashIncome.Product.Balance = cashIncome.Product.Balance + _amount;
                        //_context.Update(product);
                    }
                    _context.Entry(cashIncomeOld).State = EntityState.Detached; //detach old to update the new
                    _context.Update(cashIncome);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CashIncomeExists(cashIncome.CashIncomeId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(cashIncome.ProductId);
            return(View(cashIncome));
        }
Esempio n. 10
0
        //Delete Income
        public ActionResult DeleteIncome(int?id)
        {
            CashIncome obj    = db.CashIncome.Find(id);
            var        stData = db.SummaryTable.Where(c => c.STId == obj.OID).Select(c => c).SingleOrDefault();

            //刪除總表資料
            db.SummaryTable.Remove(stData);
            //刪除收入表資料
            db.CashIncome.Remove(obj);

            //更新userdata的cashvalue
            var username = Convert.ToString(Session["User"]);
            var Esum     = db.CashExpense.Sum(c => c.ExAmount).HasValue? db.CashExpense.Sum(c => c.ExAmount):0;
            var Isum     = db.CashIncome.Sum(c => c.InAmount).HasValue? db.CashIncome.Sum(c => c.InAmount):0;
            var net      = Isum - Esum;
            var userdata = db.UsersData.Where(c => c.UserName == username).Select(c => c).SingleOrDefault();

            userdata.CashValue       = (double)net;
            db.Entry(userdata).State = EntityState.Modified;

            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Esempio n. 11
0
        //[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));
        }
Esempio n. 12
0
        //Insert Income
        public ActionResult InsertIncome([Bind(Include = "STId,TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "InCashID,UserName,InCashType,InAmount,InDate,InNote")] CashIncome cashIncome)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            summaryTable.TradeType = cashIncome.InCashType;
            summaryTable.TradeDate = cashIncome.InDate;
            summaryTable.UserName  = cashIncome.UserName;
            db.SummaryTable.Add(summaryTable);
            db.SaveChanges();

            cashIncome.OID = summaryTable.STId;
            if (ModelState.IsValid)
            {
                db.CashIncome.Add(cashIncome);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cashIncome));
        }
Esempio n. 13
0
        public ActionResult Withdraw(Insurances insurances)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            if (ModelState.IsValid)
            {
                SummaryTable ST = new SummaryTable
                {
                    TradeType = "保險",
                    TradeDate = insurances.WithdrawDate,
                    UserName  = Session["User"].ToString()
                };



                Insurances olddata = db.Insurances.Find(insurances.SerialNumber);
                olddata.Withdrawed = true;
                db.SaveChanges();

                insurances.PurchaseOrWithdraw = false;
                insurances.Withdrawed         = true;

                insurances.UserID = Session["User"].ToString();
                if (insurances.RelateCash == true)
                {
                    insurances.CashFlow = insurances.Withdrawal;
                    db.SummaryTable.Add(ST);
                    db.SaveChanges();

                    var id = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();
                    insurances.STID = id;
                    db.Insurances.Add(insurances);
                    db.SaveChanges();

                    var User = db.UsersData.Where(U => U.UserName == insurances.UserID).Select(U => U).SingleOrDefault();
                    User.InsuranceValue -= insurances.Withdrawal;
                    User.CashValue      += insurances.Withdrawal;
                    db.SaveChanges();

                    CashIncome CI = new CashIncome
                    {
                        OID        = id,
                        UserName   = insurances.UserID,
                        InCashType = "保險",
                        InAmount   = insurances.CashFlow,
                        InDate     = insurances.WithdrawDate,
                        InNote     = insurances.InsuranceName + "解約金"
                    };

                    db.CashIncome.Add(CI);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    insurances.CashFlow = 0;
                    db.SummaryTable.Add(ST);
                    db.SaveChanges();

                    var id = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();
                    insurances.STID = id;
                    db.Insurances.Add(insurances);
                    db.SaveChanges();

                    var User = db.UsersData.Where(U => U.UserName == insurances.UserID).Select(U => U).SingleOrDefault();
                    User.InsuranceValue -= insurances.Withdrawal;
                    User.CashValue      += insurances.Withdrawal;
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(insurances));
        }
Esempio n. 14
0
        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();
            }
        }
Esempio n. 15
0
        //Insert Income
        public ActionResult InsertIncome([Bind(Include = "STId,TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "InCashID,UserName,InCashType,InAmount,InDate,InNote")] CashIncome cashIncome)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            if (ModelState.IsValid)
            {
                var username = Convert.ToString(Session["User"]);
                var Esum     = db.CashExpense.Sum(c => c.ExAmount).HasValue? db.CashExpense.Sum(c => c.ExAmount):0;
                var Isum     = (db.CashIncome.Sum(c => c.InAmount) + Convert.ToInt32(cashIncome.InAmount)).HasValue? db.CashIncome.Sum(c => c.InAmount) + Convert.ToInt32(cashIncome.InAmount):0;
                var net      = Isum - Esum;
                //算出現金淨值,更新至userdata的cashvalue
                var userdata = db.UsersData.Where(c => c.UserName == username).Select(c => c).SingleOrDefault();
                userdata.CashValue       = (double)net;
                db.Entry(userdata).State = EntityState.Modified;

                //新增總表資料
                summaryTable.TradeType = cashIncome.InCashType;
                summaryTable.TradeDate = cashIncome.InDate;
                summaryTable.UserName  = cashIncome.UserName;
                db.SummaryTable.Add(summaryTable);
                db.SaveChanges();

                //新增現金資產
                cashIncome.OID = summaryTable.STId;
                db.CashIncome.Add(cashIncome);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cashIncome));
        }
        //Insert Income
        public ActionResult InsertIncome([Bind(Include = "STId,TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "InCashID,UserName,InCashType,InAmount,InDate,InNote")] CashIncome cashIncome)
        {
            summaryTable.TradeType = cashIncome.InCashType;
            summaryTable.TradeDate = cashIncome.InDate;
            summaryTable.UserName  = cashIncome.UserName;
            db.SummaryTable.Add(summaryTable);
            db.SaveChanges();

            cashIncome.OID = summaryTable.STId;
            if (ModelState.IsValid)
            {
                db.CashIncome.Add(cashIncome);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cashIncome));
        }
Esempio n. 17
0
        public ActionResult Sell(Fund fund)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            if (ModelState.IsValid)
            {
                Fund olddata = db.Fund.Find(fund.SerialNumber);
                olddata.Units = olddata.Units - fund.Units;
                db.SaveChanges();

                fund.BuyOrSell = false;

                SummaryTable ST = new SummaryTable
                {
                    UserName  = Session["User"].ToString(),
                    TradeDate = fund.Date,
                    TradeType = "基金"
                };

                if (fund.RelateCash == true)
                {
                    fund.CashFlow = (fund.SellNAV * fund.Units) * (1 + fund.Fee / 100);
                    db.SummaryTable.Add(ST);
                    db.SaveChanges();

                    fund.SerialNumber = 0;
                    fund.UserID       = Session["User"].ToString();
                    fund.STID         = db.SummaryTable.Select(s => s.STId).ToList().LastOrDefault();
                    db.Fund.Add(fund);
                    db.SaveChanges();

                    CashIncome CI = new CashIncome
                    {
                        UserName   = fund.UserID,
                        InCashType = "基金",
                        OID        = fund.STID,
                        InAmount   = Convert.ToInt32(fund.CashFlow),
                        InDate     = fund.Date,
                        InNote     = "基金贖回"
                    };
                    db.CashIncome.Add(CI);
                    db.SaveChanges();

                    string value = db.fund_data.Where(D => D.FundName == fund.FundName).Select(D => D.FundNAV).SingleOrDefault();
                    var    User  = db.UsersData.Where(U => U.UserName == fund.UserID).Select(U => U).SingleOrDefault();
                    User.FundValue -= fund.Units * double.Parse(value);
                    User.CashValue += fund.SellNAV * fund.Units;
                }
                else
                {
                    fund.CashFlow = 0;
                    db.SummaryTable.Add(ST);
                    db.SaveChanges();

                    fund.SerialNumber = 0;
                    fund.UserID       = Session["User"].ToString();
                    fund.STID         = db.SummaryTable.Select(s => s.STId).ToList().LastOrDefault();
                    db.Fund.Add(fund);
                    db.SaveChanges();

                    string value = db.fund_data.Where(D => D.FundName == fund.FundName).Select(D => D.FundNAV).SingleOrDefault();
                    var    User  = db.UsersData.Where(U => U.UserName == fund.UserID).Select(U => U).SingleOrDefault();
                    User.FundValue -= fund.Units * double.Parse(value);
                }
                return(RedirectToAction("Index"));
            }
            return(View(fund));
        }
Esempio n. 18
0
        //Delete資料

        public ActionResult DeleteStock(int?id)
        {
            StockHistory obj        = db.StockHistory.Find(id);
            CashExpense  objCashExp = db.CashExpense.Find(id);
            CashIncome   objCashIn  = db.CashIncome.Find(id);
            var          stData     = db.SummaryTable.Where(c => c.STId == obj.STId).Select(c => c).SingleOrDefault();
            var          netincome  = obj.stockNetincome; //負值=買進紀錄、正值=賣出紀錄
            double       cashchange = 0;

            if (netincome > 0)
            {
                //cashchange = (double)netincome;
                ////刪除總表資料
                db.SummaryTable.Remove(stData);
                //刪除股票表資料
                db.StockHistory.Remove(obj);
                //刪除現金表資料
                db.CashIncome.Remove(objCashIn);
            }
            else if (netincome < 0)    //買進紀錄
            {
                bool flagcash = obj.CashAccount;
                if (flagcash)    //現金支出要回沖
                {
                    cashchange = (double)netincome;
                    //////刪除總表資料
                    //db.SummaryTable.Remove(stData);
                    //刪除股票表資料
                    db.StockHistory.Remove(obj);
                    //刪除現金支出資料
                    db.CashExpense.Remove(objCashExp);
                }
                else
                {
                    cashchange = 0;
                    ////刪除總表資料
                    //db.SummaryTable.Remove(stData);
                    //刪除股票表資料
                    db.StockHistory.Remove(obj);
                }
            }
            else //netincom=0
            {
                cashchange = 0;
                ////刪除總表資料
                //db.SummaryTable.Remove(stData);
                //刪除股票表資料
                db.StockHistory.Remove(obj);
            }
            db.SaveChanges();

            //計算與登錄股票現值
            AVGCalculator calculator = new AVGCalculator();
            var           name       = Convert.ToString(Session["User"]);
            decimal       pvsum      = calculator.getPVsum(name);
            var           ud         = calculator.GetUserdata(name);
            UsersData     userdata   = new UsersData();

            userdata.StockValue      = (double)pvsum;
            userdata.UserName        = ud[0].UserName;
            userdata.CashValue       = ud[0].CashValue + (cashchange * (-1));
            userdata.Password        = ud[0].Password;
            userdata.Email           = ud[0].Email;
            userdata.FXValue         = ud[0].FXValue;
            userdata.InsuranceValue  = ud[0].InsuranceValue;
            userdata.FundValue       = ud[0].FundValue;
            db.Entry(userdata).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }