/// <summary> /// Add a contract in our database, no logical conditions /// We will check conditions to sign it and to calculate the salary /// </summary> /// <param name="c"></param> public void AddContract(Contract c) { Child cc = GetAllChild(x => x.ID == c.ChildID).FirstOrDefault(); Nanny n = GetAllNanny(x => x.ID == c.NannyID).FirstOrDefault(); Mother m = GetAllMother(x => x.ID == cc.MotherID).FirstOrDefault(); // create the distance if (m.Request.SearchAddress != "") { c.Distance = (new DistanceClass(m.Request.SearchAddress, n.Address)).Distance; } else { c.Distance = (new DistanceClass(m.Request.Address, n.Address)).Distance; } if (c.Distance < m.Request.DistanceAccepted) { SignContract(c); } else { throw new Exception("Your contract cannot be signed because the distance isn't acceptable."); } dal.AddContract(c); }
/// <summary> /// add the contract /// </summary> /// <param name="c"></param> public void AddContract(Contract c) { Child chi = GetChild(c.ChildId); Nanny nan = GetNanny(c.NannyId); Mother mom = GetMother(c.MotherId); int CalculatesSalary = 0; //if the nanny have more children from the mother they are rebate if ((DateTime.Today.Year - chi.DateBirth.Year) * 12 + (DateTime.Today.Month - chi.DateBirth.Month) < nan.MinAgeMonth) //checking { throw new Exception("the nanny can't get the age of the child"); } if ((DateTime.Today.Year - chi.DateBirth.Year) * 12 + (DateTime.Today.Month - chi.DateBirth.Month) > nan.MaxAgeMonth)//checking { throw new Exception("the nanny can't get the age of the child"); } if (nan.ListIdContract.Count == nan.MaxChildren)//checking { throw new Exception("the nanny take care of max child that she can"); } //calculates Salary foreach (int idc in nan.ListIdContract) { Contract con = GetContract(idc); if (con.MotherId == mom.Id) { CalculatesSalary++; } } if (c.HorM1 == false)//hour { if (nan.PerHour == false) { throw new Exception("the nanny dont agree to get a hour rate"); } c.PayHours = c.PayHours - ((nan.PayHour * CalculatesSalary * 2) / 100); c.PayMonth = c.PayHours * (float)(nan.HowMuchHourNanWork1.TotalHours) * 4; } else//month { c.PayMonth = nan.PayMonth - ((nan.PayMonth * CalculatesSalary * 2) / 100); c.PayHours = nan.PayHour - ((nan.PayHour * CalculatesSalary * 2) / 100); } dal.AddContract(c); }
/* contract functions */ /// <summary> /// add contract to contract's DB /// </summary> /// <param name="contract">the contract to add to ContractList</param> /// <remarks> /// accept contract, update number of nanny's children, that the child has nanny, /// the final payment and cal dal.AddContract to add the contract /// check that - nanny, mother and child exsist /// check also that there this contract dosn't already exsist /// throw exceptions accordingly /// </remarks> public void AddContract(Contract contract) { Mother mother = FindMother(contract.MotherID); Nanny nanny = FindNanny(contract.NannyID); Child child = FindChild(contract.ChildID); if (mother == null) { throw new BLException("mother with ID: " + contract.MotherID + " dosn't exsist", "add contract"); } if (nanny == null) { throw new BLException("nanny with ID: " + contract.NannyID + " dosn't exsist", "add contract"); } if (child == null) { throw new BLException("child with ID: " + contract.ChildID + " dosn't exsist", "add contract"); } // if child age is under 3 month throw exception if (child.AgeInMonth < 3) { throw new BLException(child.FirstName + " is under 3 month", "add contrsct"); } if (IsChildInNannyAge(nanny, child.ID) == false) { throw new BLException(child.FirstName + " is not on nanny's age range", "add contrsct"); } if (child.HaveNanny == true) { throw new BLException(child.FirstName + " already has a nanny ", "add contrsct"); } // if nanny has more then his max childre throw exception if (nanny.Children >= nanny.MaxChildren) { throw new BLException(nanny.FullName() + " already has " + nanny.MaxChildren + " children", "Add contract"); } try { // calculate the final payment CalculatePayment(contract); // update number of nanny's children and that the child has nanny UpdateNannyChildren(nanny, 1); UpdateHaveNanny(child, true); dal.AddContract(contract.Clone()); } catch (BLException ex) { // if fail at UpdateHaveNanny need to reduce back nanny children if (ex.sender == "Update Have Nanny") { UpdateNannyChildren(nanny, -1); } throw; } catch (DALException ex) { // if fail at UpdateHaveNanny need to reduce back nanny children, // and if fail at AddContract need to reduce back nanny children // and change back the have nanny feild of child. if (ex.sender == "Update Have Nanny") { UpdateNannyChildren(nanny, -1); } if (ex.sender == "Add contract") { UpdateNannyChildren(nanny, -1); } UpdateHaveNanny(child, false); throw new BLException(ex.Message, ex.sender); } }