public static StaffLeave ToModel(this leaves_allocated row) { return(new StaffLeave() { id = row.id.ToString(), name = row.leave.name, annualTotal = row.annualTotal, remaining = row.remaining, taken = row.leaves_takens.Where(x => x.status == (byte)LeaveStatus.APPROVED).Sum(x => x.days) }); }
public ActionResult SaveStaff(long?id, int?total, decimal?left, int type, long staffid) { var alloc = new leaves_allocated(); if (id.HasValue) { alloc = db.leaves_allocateds.Single(x => x.id == id.Value); } else { alloc.type = type; alloc.staffid = staffid; // check that leave type has not been allocated var exist = db.leaves_allocateds.Any(x => x.staffid == staffid && x.type == type); if (exist) { return(Json("Leave type has already been allocated to this staff.".ToJsonFail())); } } if (total.HasValue && !left.HasValue) { return(Json("You must specify leave remaining if an annual total is specified.".ToJsonFail())); } if (!total.HasValue && left.HasValue) { return(Json("You must specify an annual total if you want to specify remaining leave.".ToJsonFail())); } alloc.annualTotal = total; alloc.remaining = left; if (!id.HasValue) { db.leaves_allocateds.InsertOnSubmit(alloc); } try { repository.Save(); } catch (Exception ex) { return(SendJsonErrorResponse(ex)); } var viewmodel = "Entry saved successfully.".ToJsonOKMessage(); viewmodel.data = this.RenderViewToString("StaffRows", new[] { alloc }.ToModel()); return(Json(viewmodel)); }