Esempio n. 1
0
        public ActionResult CreateStockBuy([Bind(Include = "TradeType,TradeDate,UserName")] SummaryTable summaryTable, [Bind(Include = "stockID,stockPrice,stockAmount,stockFee,stockTax,stockTP,stockNetincome,stockNote,CashAccount")] StockHistory stockHistory, UsersData userdata)
        {
            if (Session["User"] == null)
            {
                return(RedirectToRoute("Default", new { Controller = "Home", Action = "Index" }));
            }

            if (ModelState.IsValid)
            {
                //存入總表獲得交易序號
                db.SummaryTable.Add(summaryTable);
                db.SaveChanges();
                var  id       = db.SummaryTable.Select(c => c.STId).ToList().LastOrDefault();
                bool cashflag = false;
                if (stockHistory.CashAccount)
                {
                    CashExpense expense = new CashExpense();
                    expense.OID        = id;
                    expense.UserName   = summaryTable.UserName;
                    expense.ExCashType = summaryTable.TradeType;
                    expense.ExAmount   = (stockHistory.stockNetincome) * (-1);
                    expense.ExDate     = summaryTable.TradeDate;
                    expense.ExNote     = "買進股票";
                    cashflag           = true;
                    db.CashExpense.Add(expense);
                    db.SaveChanges();
                }
                //存入股票交易紀錄表
                stockHistory.STId        = id;
                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);
                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       = (cashflag)? (ud[0].CashValue + Math.Abs((double)stockHistory.stockNetincome)): ud[0].CashValue;
                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"));
        }
Esempio n. 2
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"));
        }
Esempio n. 3
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"));
        }