예제 #1
0
 public ActionResult Create()
 {
     TimesheetViewModel timeSheetViewModel = new TimesheetViewModel();
     timeSheetViewModel.ObjectState = ObjectState.Added;
     timeSheetViewModel.EmployeeName = User.Identity.Name;
     return View(timeSheetViewModel);
 }
예제 #2
0
        public static TimesheetViewModel CreateTimesheetViewModelFromTimesheet(Timesheet timeSheet)
        {
            TimesheetViewModel timeSheetViewModel = new TimesheetViewModel();
            timeSheetViewModel.TimesheetId = timeSheet.TimesheetId;
            timeSheetViewModel.EmployeeName = timeSheet.EmployeeName;
            timeSheetViewModel.PONumber = timeSheet.PONumber;
            timeSheetViewModel.ObjectState = ObjectState.Unchanged;
            timeSheetViewModel.RowVersion = timeSheet.RowVersion;

            foreach (TimesheetItem timeSheetItem in timeSheet.TimesheetItems)
            {
                TimesheetItemViewModel timeSheetItemViewModel = new TimesheetItemViewModel();
                timeSheetItemViewModel.TimesheetItemId = timeSheetItem.TimesheetItemId;
                timeSheetItemViewModel.ProductCode = timeSheetItem.ProductCode;
                timeSheetItemViewModel.Quantity = timeSheetItem.Quantity;
                timeSheetItemViewModel.UnitPrice = timeSheetItem.UnitPrice;

                timeSheetItemViewModel.ObjectState = ObjectState.Unchanged;
                timeSheetItemViewModel.RowVersion = timeSheetItem.RowVersion;

                timeSheetItemViewModel.TimesheetId = timeSheet.TimesheetId;

                timeSheetViewModel.TimesheetItems.Add(timeSheetItemViewModel);
            }

            return timeSheetViewModel;
        }
예제 #3
0
        public static Timesheet CreateTimesheetFromTimesheetViewModel(TimesheetViewModel timeSheetViewModel)
        {
            Timesheet timeSheet = new Timesheet();
            timeSheet.TimesheetId = timeSheetViewModel.TimesheetId;
            timeSheet.EmployeeName = timeSheetViewModel.EmployeeName;
            timeSheet.PONumber = timeSheetViewModel.PONumber;
            timeSheet.ObjectState = timeSheetViewModel.ObjectState;
            timeSheet.RowVersion = timeSheetViewModel.RowVersion;

            int temporaryTimesheetItemId = -1;

            foreach (TimesheetItemViewModel timeSheetItemViewModel in timeSheetViewModel.TimesheetItems)
            {
                TimesheetItem timeSheetItem = new TimesheetItem();
                timeSheetItem.ProductCode = timeSheetItemViewModel.ProductCode;
                timeSheetItem.Quantity = timeSheetItemViewModel.Quantity;
                timeSheetItem.UnitPrice = timeSheetItemViewModel.UnitPrice;

                timeSheetItem.ObjectState = timeSheetItemViewModel.ObjectState;
                timeSheetItem.RowVersion = timeSheetItemViewModel.RowVersion;

                if (timeSheetItemViewModel.ObjectState != ObjectState.Added)
                    timeSheetItem.TimesheetItemId = timeSheetItemViewModel.TimesheetItemId;
                else
                {
                    timeSheetItem.TimesheetItemId = temporaryTimesheetItemId;
                    temporaryTimesheetItemId--;
                }

                timeSheetItem.TimesheetId = timeSheetViewModel.TimesheetId;

                timeSheet.TimesheetItems.Add(timeSheetItem);
            }

            return timeSheet;
        }
예제 #4
0
        public JsonResult Save(TimesheetViewModel timeSheetViewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelStateException(ModelState);
            }

            Timesheet timeSheet = ViewModels.Helpers.CreateTimesheetFromTimesheetViewModel(timeSheetViewModel);

            _timesheetContext.Timesheets.Attach(timeSheet);

            if (timeSheet.ObjectState == ObjectState.Deleted)
            {
                foreach (TimesheetItemViewModel timeSheetItemViewModel in timeSheetViewModel.TimesheetItems)
                {
                    TimesheetItem timeSheetItem = _timesheetContext.TimesheetItems.Find(timeSheetItemViewModel.TimesheetItemId);
                    if (timeSheetItem != null)
                        timeSheetItem.ObjectState = ObjectState.Deleted;
                }
            }
            else
            {
                foreach (int timeSheetItemId in timeSheetViewModel.TimesheetItemsToDelete)
                {
                    TimesheetItem timeSheetItem = _timesheetContext.TimesheetItems.Find(timeSheetItemId);
                    if (timeSheetItem != null)
                        timeSheetItem.ObjectState = ObjectState.Deleted;
                }
            }

            _timesheetContext.ApplyStateChanges();
            string messageToClient = string.Empty;

            try
            {
                _timesheetContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                messageToClient = "Someone else have modified this timesheet since you retrieved it.  Your changes have not been applied.  What you see now are the current values in the database.";
            }
            catch (Exception ex)
            {
                throw new ModelStateException(ex);
            }

            if (timeSheet.ObjectState == ObjectState.Deleted)
                return Json(new { newLocation = "/Timesheet/Index/" });

            if (messageToClient.Trim().Length == 0)
                //messageToClient = ViewModels.Helpers.GetMessageToClient(timeSheetViewModel.ObjectState, timeSheet.EmployeeName);

                timeSheetViewModel.TimesheetId = timeSheet.TimesheetId;
            _timesheetContext.Dispose();
            _timesheetContext = new TimesheetContext();
            timeSheet = _timesheetContext.Timesheets.Find(timeSheetViewModel.TimesheetId);

            timeSheetViewModel = ViewModels.Helpers.CreateTimesheetViewModelFromTimesheet(timeSheet);
            timeSheetViewModel.MessageToClient = messageToClient;

            return Json(new { timeSheetViewModel = timeSheetViewModel });
        }