/// <summary> /// Gets the ID of the dependent to delete via the form's viewstate, as the /// delete button's ID is generated also with the dependent's ID. From there, /// the employee's cost is updated, along with their other resulting totals. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_DeleteDependent_Click(object sender, EventArgs e) { string[] buttonIDComponents = new string[0]; int dependentToDelete_ID = 0; for (int i = 0; i < Request.Form.Count; i++) { if (Request.Form.AllKeys[i].Contains("Delete")) { string buttonNumber = Request.Form.AllKeys[i]; buttonIDComponents = buttonNumber.Split('_'); dependentToDelete_ID = int.Parse(buttonIDComponents[1]); } } using (var db = new BenefitsContext()) { Dependent toDelete = db.Dependents.FirstOrDefault(p => p.id == dependentToDelete_ID); Employee toUpdate = db.Employees.First(p => p.employeeID == incomingEmployeeID); foreach (Dependent d in dependentBelongingToEmployee) { if (d.id == toDelete.id) { dependentBelongingToEmployee.Remove(d); break; } } if (dependentBelongingToEmployee.Count < 1) { toUpdate.hasDependent = false; } toUpdate.cost -= toDelete.cost; toUpdate.deductionsPerPaycheck = toUpdate.cost / 26; toUpdate.paycheckAfterDeductions = toUpdate.paycheckBeforeDeductions - toUpdate.deductionsPerPaycheck; db.Dependents.Remove(toDelete); db.SaveChanges(); } // reload the page Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri); }
/// <summary> /// Method to change the 10% discount for dependents, if necessary /// </summary> /// <param name="toSubmit"></param> /// <param name="firstInitialBeforeChanges"></param> /// <param name="firstInitialAfterChanges"></param> private void alterDependentDiscount(Dependent toSubmit, char firstInitialBeforeChanges, char firstInitialAfterChanges) { // if their name didn't start with a, give them a discount if (!firstInitialBeforeChanges.Equals('a') && firstInitialAfterChanges.Equals('a')) { toSubmit.Employee.cost -= toSubmit.cost; toSubmit.cost -= toSubmit.cost * .10; toSubmit.Employee.cost += toSubmit.cost; toSubmit.Employee.deductionsPerPaycheck = toSubmit.Employee.cost / 26; toSubmit.Employee.paycheckAfterDeductions = toSubmit.Employee.paycheckBeforeDeductions - toSubmit.Employee.deductionsPerPaycheck; } // if their name started with a and is changed to a non a name, remove discount else if (firstInitialBeforeChanges.Equals('a') && !firstInitialAfterChanges.Equals('a')) { toSubmit.Employee.cost -= toSubmit.cost; toSubmit.cost = 500; // 500 is base dependent cost toSubmit.Employee.cost += toSubmit.cost; toSubmit.Employee.deductionsPerPaycheck = toSubmit.Employee.cost / 26; toSubmit.Employee.paycheckAfterDeductions = toSubmit.Employee.paycheckBeforeDeductions - toSubmit.Employee.deductionsPerPaycheck; } }
/// <summary> /// Create the employee based off of their text boxes being filled; /// then cycle through the form's viewstate to collect information /// from the generated text boxes. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button_SubmitWithDependents_Click(object sender, EventArgs e) { /// PART 1: CREATE THE EMPLOYEE AND ADD THEM TO DB string employee_fname = TextBox_EmployeeFirstName.Text; string employee_lname = TextBox_EmployeeLastName.Text; Employee newEmployee = createStandardEmployeeWithDependents(employee_fname, employee_lname); using (var db = new BenefitsContext()) { db.Employees.Add(newEmployee); db.SaveChanges(); } /// PART 2: CYCLE THROUGH ALL YOUR FIELDS AND ADD DEPENDENTS BASED ON THE EXISTING /// FIELDS for (int i = 0; i < Request.Form.Count; i++) { if (Request.Form.AllKeys[i].Contains("dep_FirstName")) // indicates that the text boxes for names exist { Dependent newDependent = new Dependent(); newDependent.cost = 500; int ParamStartPoint = Request.Form.AllKeys[i].IndexOf("dep_First"); int ParamNameLength = Request.Form.AllKeys[i].Length - ParamStartPoint - 1; string[] ControlName = Request.Form.AllKeys[i].Substring(ParamStartPoint, ParamNameLength).Split('$'); if (ControlName[0] == "dep_FirstName") { newDependent.firstName = Request.Form[i]; } i++; if (Request.Form.AllKeys[i].Contains("dep_LastName")) // indicates that the text boxes exist { int ParamStartPoint2 = Request.Form.AllKeys[i].IndexOf("dep_Last"); int ParamNameLength2 = Request.Form.AllKeys[i].Length - ParamStartPoint2 - 1; string[] ControlName2 = Request.Form.AllKeys[i].Substring(ParamStartPoint2, ParamNameLength2).Split('$'); if (ControlName2[0] == "dep_LastName") { newDependent.lastName = Request.Form[i]; } } apply10PercentOffIfApplicable(newDependent); newEmployee.cost += newDependent.cost; newDependent.employeeID = newEmployee.employeeID; // we have the info to add the dependent! using (var db = new BenefitsContext()) { db.Dependents.Add(newDependent); Employee employeeToUpdate = db.Employees.Find(newEmployee.employeeID); employeeToUpdate.cost = newEmployee.cost; employeeToUpdate.deductionsPerPaycheck = employeeToUpdate.cost / 26; employeeToUpdate.paycheckBeforeDeductions = 2000; employeeToUpdate.paycheckAfterDeductions = employeeToUpdate.paycheckBeforeDeductions - employeeToUpdate.deductionsPerPaycheck; db.SaveChanges(); } } } Response.Redirect("~/ViewEmployees.aspx"); }
/// <summary> /// Helper to cycle through many dependent fields, taking into account the two /// different cases. 1) Adding a brand new employee with dependents, and 2) /// Adding brand new dependents to an existing employee. /// </summary> /// <param name="newEmployee"></param> private void createDependentsFromFields(Employee newEmployee) { for (int i = 0; i < Request.Form.Count; i++) { Dependent newDependent = new Dependent(); if (Request.Form.AllKeys[i].Contains("dep_firstname")) { int dep_firstNameIndex = Request.Form.AllKeys[i].IndexOf("dep_firstname"); int dep_firstNameLength = Request.Form.AllKeys[i].Length - dep_firstNameIndex; string[] dep_firstNameValue = Request.Form.AllKeys[i].Substring(dep_firstNameIndex, dep_firstNameLength).Split('$'); if (dep_firstNameValue[0].Contains("dep_firstname")) { if (passedFirstDependentField) // multiple dependents { string[] firstnames = Request.Form[i].Split(','); if (firstnames[0].Equals("")) { break; } i++; string[] lastNames = Request.Form[i].Split(','); for (int extraDependentCount = 0; extraDependentCount < firstnames.Length; extraDependentCount++) { newDependent = new Dependent(); newDependent.employeeID = newEmployee.employeeID; if (!firstnames[extraDependentCount].Equals("") && !lastNames[extraDependentCount].Equals("")) { newDependent.firstName = firstnames[extraDependentCount]; newDependent.lastName = lastNames[extraDependentCount]; newDependent.cost = 500; apply10PercentOffIfApplicable(newDependent); using (var db = new BenefitsContext()) { Employee toUpdate = db.Employees.FirstOrDefault(p => p.employeeID == newEmployee.employeeID); toUpdate.cost += newDependent.cost; toUpdate.deductionsPerPaycheck = toUpdate.cost / 26; toUpdate.paycheckAfterDeductions = toUpdate.paycheckBeforeDeductions - toUpdate.deductionsPerPaycheck; db.Dependents.Add(newDependent); db.SaveChanges(); } } } } string firstName = Request.Form[i]; i++; string lastName = Request.Form[i]; newDependent = createInitialDependent(newEmployee.employeeID, firstName, lastName); if (addingDependentsFromEmployee) { newDependent.employeeID = incomingEmployeeID; } else { newDependent.employeeID = newEmployee.employeeID; } } if (!passedFirstDependentField) { using (var db = new BenefitsContext()) { if (!addingDependentsFromEmployee) { Employee toUpdate = db.Employees.FirstOrDefault(p => p.employeeID == newEmployee.employeeID); toUpdate.cost += newDependent.cost; toUpdate.deductionsPerPaycheck = toUpdate.cost / 26; toUpdate.paycheckAfterDeductions = toUpdate.paycheckBeforeDeductions - toUpdate.deductionsPerPaycheck; } else { Employee toUpdate = db.Employees.FirstOrDefault(p => p.employeeID == incomingEmployeeID); toUpdate.cost += newDependent.cost; toUpdate.deductionsPerPaycheck = toUpdate.cost / 26; toUpdate.paycheckAfterDeductions = toUpdate.paycheckBeforeDeductions - toUpdate.deductionsPerPaycheck; } db.Dependents.Add(newDependent); db.SaveChanges(); } passedFirstDependentField = true; } } } }