예제 #1
0
        public ActionResult CreateNewCustomer(CustomerModel customerModel)
        {
            var createCustomerContext = new YNTCTermContext();
            int? categoryModel = (int?)TempData["categoryModel"];
            int? termId = (int?)TempData["termId"];

            var currentTerm = createCustomerContext.Term.Find(termId);

            CategoryModel modelOfCategory = createCustomerContext.CategoryModels.Find(categoryModel);
            modelOfCategory.Customers.Add(customerModel);

            if (ModelState.IsValid)
            {
                createCustomerContext.Entry(customerModel).State = EntityState.Added;
                createCustomerContext.SaveChanges();
                return RedirectToAction("MonthOverview", currentTerm);
            }
            return View("CreateNewCustomerView");
        }
예제 #2
0
        private TermModel GetNewTerm()
        {
            TermModel term = new TermModel();
            using (var db = new YNTCTermContext())
            {
                TermModel latestTerm = db.Term.OrderByDescending(e => e.Id).FirstOrDefault();

                term.PrevId = latestTerm.Id;
                //term.NextId = term.Id;
                db.SaveChanges();
                term.StartDate = latestTerm.StartDate.AddMonths(1);
                term.ProjectedGoal = latestTerm.ProjectedGoal;
                term.Categories = new List<CategoryModel>();

                foreach(CategoryModel catm in latestTerm.Categories)//perform deep copy
                {
                    CategoryModel new_catm = new CategoryModel { NameOfCategory = catm.NameOfCategory,
                    Customers = new List<CustomerModel>()
                    };

                    foreach(CustomerModel custm in catm.Customers)
                    {
                        CustomerModel new_custm = new CustomerModel
                        {
                            NameOfCompany = custm.NameOfCompany,
                            BudgetActualCustomer = new BudgetActualModel { Budget = 0, Actual = 0, Difference = 0 }
                        };
                        new_catm.Customers.Add(new_custm);
                    }
                    term.Categories.Add(new_catm);
                }
            }
            return term;
        }