예제 #1
0
        public ActionResult Transaction(FormCollection f)
        {
            DateTime date    = DateTime.Now;
            string   info    = f["info"];
            int      acc_id  = ((account)Session["Account"]).acc_id;
            int      type_id = Convert.ToInt32(f["trans_type"]);
            int      cate_id = Convert.ToInt32(f["category"]);

            tran t = new tran
            {
                t_date  = date,
                t_info  = info,
                acc_id  = acc_id,
                type_id = type_id,
                c_id    = cate_id
            };

            //check quantity validity
            if (type_id == 2)
            {
                if (ModelState.IsValid)
                {
                    using (var db = new money_managementEntities())
                    {
                        List <base_money> bm = db.base_money.ToList();
                        foreach (base_money m in bm)
                        {
                            string name      = "Q_" + m.bm_value.ToString();
                            int    tquantity = Convert.ToInt32(f[name]);

                            if (tquantity <= 0)
                            {
                                continue;
                            }

                            int aquantity = Convert.ToInt32(db.account_money.Where(x => x.acc_id == acc_id).FirstOrDefault(x => x.bm_id == m.bm_id).quantity);
                            if (aquantity - tquantity < 0)
                            {
                                ViewBag.Alert = "You don't have enough money.";
                                return(View());
                            }
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                using (var db = new money_managementEntities())
                {
                    //trans
                    db.trans.Add(t);
                    db.SaveChanges();

                    //detail
                    List <base_money> bm = db.base_money.ToList();
                    foreach (base_money m in bm)
                    {
                        string name     = "Q_" + m.bm_value.ToString();
                        int    quantity = Convert.ToInt32(f[name]);

                        if (quantity <= 0)
                        {
                            continue;
                        }

                        trans_detail detail = new trans_detail
                        {
                            t_id       = t.t_id,
                            bm_id      = m.bm_id,
                            t_quantity = quantity
                        };

                        db.trans_detail.Add(detail);
                    }
                    db.SaveChanges();

                    //update quantity
                    db.P_UpdateQuantityAfterTrans();
                }
            }

            return(RedirectToAction("Index"));
        }
예제 #2
0
        public ActionResult Register(FormCollection form)
        {
            string username   = form["acc_username"];
            string password   = form["acc_password"];
            string repassword = form["re_password"];
            string email      = form["acc_email"];

            // Check password confirm
            if (!password.Equals(repassword))
            {
                ViewBag.Alert = "Password does not match.";
                return(View());
            }

            // Check username exists
            if (ModelState.IsValid)
            {
                using (var db = new money_managementEntities())
                {
                    account acc = db.accounts.FirstOrDefault(x => x.acc_username.Equals(username));
                    if (acc != null)
                    {
                        ViewBag.Alert = "Username already exists.";
                        return(View());
                    }
                }
            }

            // Check email exists
            if (ModelState.IsValid)
            {
                using (var db = new money_managementEntities())
                {
                    account acc = db.accounts.FirstOrDefault(x => x.acc_email.Equals(email));
                    if (acc != null)
                    {
                        ViewBag.Alert = "Email already exists.";
                        return(View());
                    }
                }
            }

            //Create account
            account new_acc = new account
            {
                acc_username    = username,
                acc_password    = MD5Cal.GetMd5Hash(password),
                acc_email       = email,
                acc_balance     = 0,
                acc_create_time = DateTime.Now,
                acc_verified    = false
            };

            if (ModelState.IsValid)
            {
                using (var db = new money_managementEntities())
                {
                    db.accounts.Add(new_acc);
                    db.SaveChanges();
                }
            }


            return(RedirectToAction("Login"));
        }