public void Update(Budget budget)
        {
            BudgetRepository budgetRepo = new BudgetRepository(_db);

            budget.NewModifyTimeStamp();
            budgetRepo.Update(budget);
            budgetRepo.Save();
        }
 public ActionResult DeleteBudget(string id)
 {
     try
     {
         using (var budgetRepo = new BudgetRepository())
         {
             budgetRepo.Delete(id);
             budgetRepo.Save();
         }
         returnobj.SetSuccess("delete success");
     }
     catch (Exception ex)
     {
         returnobj.SetError(ex.Message);
     }
     return(Content(returnobj.ToJson(), "application/json"));
 }
        public ActionResult UploadBudget_Old(List <BudgetFileModel> filedata)
        {
            List <Account> accounts = new List <Account>();
            List <Budget>  budgets  = new List <Budget>();

            try
            {
                // 1. Validate POST parameter
                if (filedata == null)
                {
                    throw new ArgumentNullException("filedata");
                }

                // 2. Read each row and process data.
                filedata = filedata.OrderBy(f => f.CostCenterID).ThenBy(f => f.AccountID).ToList();
                foreach (var row in filedata)
                {
                    // 2.1 Check existing and add to Accounts.
                    var account = accounts.Where(a => a.AccountID == row.AccountID).FirstOrDefault();
                    if (account == null)
                    {
                        account = new Account(row);
                        accounts.Add(account);
                    }
                    //accounts.Add(account);

                    // 2.2 Check existing and add to Budgets Header.
                    //TODO: Check existing in db
                    var budget = budgets.Where(b =>
                                               b.AccountID == row.AccountID &&
                                               b.CostCenterID == row.CostCenterID &&
                                               b.Year == row.Year
                                               ).FirstOrDefault();

                    if (budget == null)
                    {
                        // Add new budget
                        budget = new Budget(row);
                        budgets.Add(budget);
                    }
                    else
                    {
                        // Sum amount to exist budget
                        int index = budgets.IndexOf(budget);
                        budgets[index].BudgetAmount += row.Amount;
                        //budgets[index].RemainAmount += row.Amount;
                    }
                }

                //// Write to file
                //StringBuilder csvFile = new StringBuilder();
                //foreach (var budget in budgets)
                //{
                //    csvFile.Append(budget.AccountID + "," + budget.CostCenterID + "," + budget.BudgetAmount);
                //    csvFile.Append(Environment.NewLine);
                //}

                //System.IO.File.WriteAllText(@"C:/budget.txt", csvFile.ToString());

                //3.Save to database
                using (var context = new BudgetContext())
                {
                    using (var db_transaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            var accRepo    = new AccountRepository(context);
                            var budgetRepo = new BudgetRepository(context);

                            accounts.ForEach(a => accRepo.AddOrUpdate(a));
                            accRepo.Save();

                            budgets.ForEach(b => budgetRepo.AddOrUpdate(b));
                            budgetRepo.Save();

                            db_transaction.Commit();
                            returnobj.SetSuccess(filedata);
                        }
                        catch (Exception ex)
                        {
                            db_transaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                returnobj.SetError(ex.Message);
                throw;
            }
            return(Content(returnobj.ToJson(), "application/json"));
        }
        public ActionResult Budget(CreateBudgetModel form)
        {
            try
            {
                // 1. Validate account.
                Account account;
                using (var accRepo = new AccountRepository())
                {
                    account = accRepo.GetById(form.AccountID);
                    // if not then, Add to database
                    if (account == null)
                    {
                        account = new Account()
                        {
                            AccountID   = form.AccountID,
                            AccountName = form.AccountName,
                            Status      = RecordStatus.Active
                        };
                        accRepo.Add(account);
                        accRepo.Save();
                    }
                }

                // 2. Check Budget.
                Budget budget;
                using (var budgetRepo = new BudgetRepository())
                {
                    // 2.1 check budget is already in database
                    budget = budgetRepo.Get().Where(
                        b =>
                        b.Year == form.Year &&
                        b.AccountID == form.AccountID &&
                        b.CostCenterID == form.CostCenterID &&
                        b.Status == BudgetStatus.Active
                        ).FirstOrDefault();

                    if (budget != null)
                    {
                        throw new Exception("งบประมาณนี้มีอยู่แล้วในระบบ");
                    }
                    else // Add new budget
                    {
                        budget = new Budget()
                        {
                            BudgetID       = Guid.NewGuid(),
                            AccountID      = form.AccountID,
                            CostCenterID   = form.CostCenterID,
                            Sequence       = 0,
                            Year           = form.Year,
                            BudgetAmount   = form.Amount,
                            WithdrawAmount = 0,
                            RemainAmount   = 0,
                            Status         = BudgetStatus.Active
                        };

                        budgetRepo.Add(budget);
                        budgetRepo.Save();
                    }
                }

                // 3. Set return value
                returnobj.SetSuccess(budget);
            }
            catch (Exception ex)
            {
                returnobj.SetError(ex.Message);
            }

            return(Content(returnobj.ToJson(), "application/json"));
        }