public void SaveCostProfitCenter(CostProfitCentersViewModel CostProfitCenterVM)
        {
            DateTime stamp = DateTime.UtcNow;

            if (CostProfitCenterVM.CostCenterID == 0)
            {
                CostProfitCenter c = new CostProfitCenter();
                c.LastModified = stamp;
                c.LastModifiedBy = "System";
                c.RecordAdded = stamp;
                c.RecordAddedBy = "System";
                c.Description = CostProfitCenterVM.Description;
                c.IsCostCenter = CostProfitCenterVM.IsCostCenter;
                c.IsDeleted= false;
                context.CostProfitCenters.Add(c);
                context.SaveChanges();
                Audit(c, stamp);
            }
            else
            {
                CostProfitCenter dbEntry = context.CostProfitCenters.Find(CostProfitCenterVM.CostCenterID);
                if (dbEntry != null)
                {
                    Audit(dbEntry, CostProfitCenterVM, stamp);
                    dbEntry.LastModified = stamp;
                    dbEntry.LastModifiedBy = "System";
                    dbEntry.Description = CostProfitCenterVM.Description;
                    dbEntry.IsCostCenter = CostProfitCenterVM.IsCostCenter;
                    context.SaveChanges();
                }
            }
        }
 public ActionResult Edit(CostProfitCentersViewModel costCenter)
 {
     if (ModelState.IsValid)
     {
         if (costCenter.CostCenterID == 0)
         {
             repository.SaveCostProfitCenter(costCenter);
             return RedirectToAction("Index");
         }
         else
         {
             repository.SaveCostProfitCenter(costCenter);
             TempData["message"] = string.Format("{0} has been saved", costCenter.Description);
             return RedirectToAction("Index");
         }
     }
     else
     {
         //there is something wrong with the data values
         return View(costCenter);
     }
 }
        private void Audit(CostProfitCenter InDB, CostProfitCentersViewModel NewData, DateTime stamp)
        {
            var query = from p in context.ApplicationTableFields
                        where p.IsDeleted == false && p.ApplicationTableID == 11 && p.IsAudited == true
                        orderby p.Description
                        select p;

            foreach (var item in query)
            {
                ApplicationTableField f = context.ApplicationTableFields.Find(item.ApplicationTableFieldID);

                ChangeLog cl = new ChangeLog();
                cl.ApplicationTableFieldID = item.ApplicationTableFieldID;
                cl.RecordID = InDB.CostProfitCenterID;
                cl.ChangeMade = stamp;
                cl.UserName = "******";

                switch (f.Description)
                {
                    case "Description":
                        cl.BeforeValue = InDB.Description;
                        cl.AfterValue = NewData.Description;
                        break;
                    case "IsCostCenter":
                        cl.BeforeValue = InDB.IsCostCenter.ToString();
                        cl.AfterValue = NewData.IsCostCenter.ToString();
                        break;
                }
                IChangeLogRepository repo = new EFChangeLogRepository();
                repo.SaveChangeLog(cl);
            }
        }