예제 #1
0
        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"));
        }