/// <summary>
 /// saves Report Instance to the database under the Reports table
 /// </summary>
 /// <param name="d"></param>
 public void createReport(Report r)
 {
     using (var context = new PetoEntities())
     {
         context.Reports.AddObject(r);
         context.SaveChanges();
     }
 }
 /// <summary>
 /// saves Document Instance to the database under the Documents table
 /// </summary>
 /// <param name="d"></param>
 public void createDocument(Document d)
 {
     using (var context = new PetoEntities())
     {
         context.Documents.AddObject(d);
         context.SaveChanges();
     }
 }
 /// <summary>
 /// get branches
 /// </summary>
 /// <returns></returns>
 public List<string> getBranches()
 {
     using (var context = new PetoEntities())
     {
         return (from i in context.Branches
                select i.name).ToList();
     }
 }
 /// <summary>
 /// creates a user and saves it to the Users table in the database
 /// </summary>
 /// <param name="user"></param>
 public void createUser(User user)
 {
     using (var context = new PetoEntities())
     {
         context.Users.AddObject(user);
         context.SaveChanges();
     }
 }
 /// <summary>
 /// get all the report category names
 /// </summary>
 /// <returns></returns>
 public List<string> getCategories()
 {
     using (var context = new PetoEntities())
     {
         return (from i in context.ReportCategories
                 orderby i.name
                 select i.name).ToList();
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     TimesheetManager tm = new TimesheetManager();
        using(var context = new PetoEntities())
     {
        string name = Membership.GetUser().UserName;
        User user = context.Users.FirstOrDefault(x => x.username == name);
        HiddenField1.Value = user.UserId.ToString();
     }
 }
 /// <summary>
 /// This method gets search data for predictive search datasource based on role
 /// </summary>
 /// <param name="role"></param>
 /// <returns></returns>
 public string[] getData(string role)
 {
     using (var context = new PetoEntities())
     {
         // get an array of names which will be in format: [<Name> MMM-YYYY]
         return (from x in context.Reports
                 where x.Role == role
                 select x.Name).Distinct().ToArray();
     }
 }
    /// <summary>
    /// delete a Report
    /// </summary>
    /// <param name="id"></param>
    public void deleteReport(int ReportId)
    {
        using (var context = new PetoEntities())
        {
            Report d = context.Reports.FirstOrDefault(x => x.ReportId == ReportId);

            if (d != null)
            {
                context.Reports.DeleteObject(d);
                context.SaveChanges();
            }
        }
    }
    /// <summary>
    /// delete a document
    /// </summary>
    /// <param name="id"></param>
    public void deleteDocument(int DocumentId)
    {
        using (var context = new PetoEntities())
        {
            Document d = context.Documents.FirstOrDefault(x => x.DocumentId == DocumentId);

            if (d != null)
            {
                context.Documents.DeleteObject(d);
                context.SaveChanges();
            }
        }
    }
    protected void searchBtn_Click(object sender, EventArgs e)
    {
        if (searchTB.Text != string.Empty)
        {
            List<Document> searchResults = new List<Document>();

            //SqlConnection conn = new SqlConnection("data source=208.124.173.18;initial catalog=petomaccallum.ca;user id=sa;password=PMLsa1234;multipleactiveresultsets=True");
            SqlConnection conn = new SqlConnection("data source=User;initial catalog=petomaccallum.ca;user id=sa;password=PMLtest;multipleactiveresultsets=True");
            conn.Open();

            SqlCommand stmt = conn.CreateCommand();

            //get projnum, name, client
            if (searchTB.Text.Contains(' '))
            {
                stmt.CommandText = "SELECT DocumentId FROM Documents WHERE FREETEXT(fileContent,@searchText)";
            }
            else {
                stmt.CommandText = "SELECT DocumentId FROM Documents WHERE CONTAINS(fileContent,@searchText)";
            }

            stmt.Parameters.AddWithValue("@searchText", searchTB.Text);
            TimesheetManager tm = new TimesheetManager();

            SqlDataReader projectReader = stmt.ExecuteReader();
            using (var context = new PetoEntities())
            {
                if (projectReader.HasRows)
                {
                    while (projectReader.Read())
                    {
                        int id = projectReader.GetInt32(0);
                        searchResults.Add(context.Documents.FirstOrDefault(x => x.DocumentId == id));
                    }
                }
                GridView1.DataSource = searchResults;
                GridView1.DataBind();
            }
        }
        else {

             FileManager fm = new FileManager();

            GridView1.DataSource = fm.getDocuments("");
            GridView1.DataBind();
        }
    }
    protected void uploadBtn_Click(object sender, EventArgs e)
    {
        if (fu.HasFile && fu.FileContent.Length != 0)
        {

            using (var context = new PetoEntities())
            {
                try
                {
                    // create new instance of a document
                    Document d = new Document();

                    // populate this instance
                    d.name = fu.FileName;
                    // divide by 1000 to convert bytes to kilobytes
                    d.size = fu.PostedFile.ContentLength / 1000;
                    d.MIMEType = fu.PostedFile.ContentType;
                    d.DateCreated = DateTime.Now;
                    d.DateModified = DateTime.Now;
                    d.fileContent = fu.FileBytes;
                    d.extension = Path.GetExtension(fu.PostedFile.FileName).Substring(1);
                    d.Role = permissionDDL.SelectedItem.Value;
                    // d.Category = typeDDL.SelectedValue;

                    // save changes to the database
                    FileManager fm = new FileManager();
                    fm.createDocument(d);
                    // show success
                    statusLbl.ForeColor = System.Drawing.Color.Green;
                    statusLbl.Text = "File Successfully Uploaded";

                }
                catch (Exception ex)
                {
                    // shoe error message
                    statusLbl.ForeColor = System.Drawing.Color.Red;
                    statusLbl.Text = "Error: File was not uploaded due to: " + ex.Message;
                    throw;
                }
            }
        }
        else
        {
            statusLbl.ForeColor = System.Drawing.Color.Red;
            statusLbl.Text = "Error: No File Chosen";
        }
    }
    /// <summary>
    /// copy chargeable record into a manager chargeable record
    /// </summary>
    /// <param name="j"></param>
    public void copyToManagerChargeable(ChargeableJob j)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                ManagerChargeable m = new ManagerChargeable();

                m.BillingHours = j.EmpHours;
                m.PayRollHours = j.EmpHours;

                m.BillingAccomodation = j.Accomodations;
                m.PayRollAccomodation = j.Accomodations;

                m.BillingTravelDistance = j.TravelDistance;
                m.PayRollTravelDistance = j.TravelDistance;

                m.TruckDistance = j.TruckDistance;

                m.TimeSheetId = j.TimeSheetId;
                m.ProjectId = j.ProjectId.Value;

                m.Classification = j.Classification;
                m.Activity = j.Activity;
                m.Day = j.Day;

                m.Remarks = j.Remarks;

                context.ManagerChargeables.AddObject(m);
                context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    /// <summary>
    /// delete a membership user
    /// </summary>
    /// <param name="user"></param>
    public void deleteUser(string UserName)
    {
        try
        {
            // delete the user from the API (asp_Users) table
            Membership.DeleteUser(UserName, true);
            using (var context = new PetoEntities())
            {
                User user = context.Users.FirstOrDefault(x=> x.username == UserName);
                // if a user is returned with the specified username delete that record from the Users table
                if (user != null) {
                    context.Users.DeleteObject(user);
                    context.SaveChanges();

                }
            }

        }
        catch (Exception )
        {

            throw;
        }
    }
 /// <summary>
 /// get chargeable records for a project
 /// </summary>
 /// <param name="projectID"></param>
 /// <returns></returns>
 public List<ChargeableJob> getChargeableForProjectID(int projectID)
 {
     using (var context = new PetoEntities())
     {
         return (from x in context.ChargeableJobs
                 where x.ProjectId == projectID
                 select x).ToList();
     }
 }
 /// <summary>
 /// get branch names
 /// </summary>
 /// <returns></returns>
 public string[] getBranches()
 {
     using (var context = new PetoEntities())
     {
         return (from x in context.Branches
                 orderby x.name
                 select x.name).ToArray();
     }
 }
 /// <summary>
 /// get activities for the classification name
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public string[] getActivityForClassification(string name)
 {
     using (var context = new PetoEntities())
     {
         return (from x in context.ClassificationActivities
                 orderby x.ActivityName
                 where x.ClassificationName == name
                 select x.ActivityName).Distinct().ToArray();
     }
 }
    /// <summary>
    /// delete a Summary record of Employee entered chargeable and nonchargeable jobs
    /// </summary>
    /// <param name="NonChargeableId"></param>
    /// <param name="ChargeableId"></param>
    /// <param name="id"></param>
    public void deleteSummary(int NonChargeableId, int ChargeableId, int id, int labID, bool manager)
    {
        using (var context = new PetoEntities())
        {
            TimeSheet t = context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == id);
            // Lab test deletion
            if (labID > 0)
            {
                if (manager)
                {
                    // subtract hours from total hours in timesheet
                    ManagerTest m = context.ManagerTests.FirstOrDefault(x => x.TestId == labID);
                    if (m != null)
                    {
                        if (m.LabTest.HasValue)
                        {
                            t.TotalHours -= m.LabTest.Value;
                        }
                        context.ManagerTests.DeleteObject(m);
                        context.SaveChanges();
                    }
                }
                else
                {
                    EmpTest e = context.EmpTests.FirstOrDefault(x => x.TestId == labID);
                    if (e != null)
                    {
                        // subtract hours from total hours in timesheet
                        if (e.LabTest.HasValue)
                        {
                            t.TotalHours -= e.LabTest.Value;
                        }
                        context.EmpTests.DeleteObject(e);
                        context.SaveChanges();
                    }
                }
            }

            if (NonChargeableId > 0)
            {
                if (!manager)
                {
                    NonChargeable m = context.NonChargeables.FirstOrDefault(x => x.NonChargeId == NonChargeableId);
                    if (m != null)
                    {
                        // subtract the values from the times heet before deleting
                        if (t != null)
                        {
                            if (m.Hours > 0)
                            {
                                t.TotalHours -= m.Hours;
                            }
                            if (m.Distance > 0)
                            {
                                t.TotalDistance -= m.Distance;
                            }
                            if (m.Accomodation > 0)
                            {
                                t.TotalExpenses -= m.Accomodation;
                            }
                            if (m.Misc > 0)
                            {
                                t.TotalExpenses -= m.Misc;
                            }
                        }
                        // save changes to db
                        context.NonChargeables.DeleteObject(m);
                        context.SaveChanges();
                    }
                }
                else
                {
                    ManagerNonChargeable m = context.ManagerNonChargeables.FirstOrDefault(x => x.ManagerNonChargeableId == NonChargeableId);
                    if (m != null)
                    {
                        // subtract the values from the timesheet before deleting
                        if (t != null)
                        {
                            if (m.Hours > 0)
                            {
                                t.TotalHours -= m.Hours;
                            }
                            if (m.Distance > 0)
                            {
                                t.TotalDistance -= m.Distance;
                            }
                            if (m.TruckDistance > 0)
                            {
                                t.TotalTruck -= m.TruckDistance;
                            }
                            if (m.Accomodations > 0 || m.Misc > 0)
                            {
                                t.TotalExpenses -= m.Accomodations - m.Misc;
                            }
                        }
                        // save changes to db
                        context.ManagerNonChargeables.DeleteObject(m);
                        context.SaveChanges();
                    }
                }
            }

            if (ChargeableId > 0)
            {
                if (!manager)
                {
                    ChargeableJob c = context.ChargeableJobs.FirstOrDefault(x => x.ChargeableId == ChargeableId);

                    if (c != null)
                    {
                        if (t != null)
                        {
                            // hours
                            if (c.EmpHours > 0)
                            {
                                t.TotalHours -= c.EmpHours;
                            }

                            // distance
                            if (c.TravelDistance > 0)
                            {
                                t.TotalDistance -= c.TravelDistance;
                            }

                            // truck distance
                            if (c.TruckDistance > 0)
                            {
                                t.TotalTruck -= c.TruckDistance;
                            }
                            // Expenses
                            if (c.Accomodations > 0)
                            {
                                t.TotalExpenses -= c.Accomodations;
                            }
                            if (c.Misc > 0)
                            {
                                t.TotalExpenses -= c.Misc;
                            }
                        }
                        // save changes to db
                        context.ChargeableJobs.DeleteObject(c);
                        context.SaveChanges();
                    }
                }
                else
                {
                    ManagerChargeable c = context.ManagerChargeables.FirstOrDefault(x => x.ManagerChargeableId == ChargeableId);

                    if (c != null)
                    {
                        if (t != null)
                        {
                            // hours
                            if (c.BillingHours > 0)
                            {
                                t.TotalHours -= c.BillingHours;
                            }

                            // distance
                            if (c.BillingTravelDistance > 0)
                            {
                                t.TotalDistance -= c.BillingTravelDistance;
                            }

                            // truck distance
                            if (c.TruckDistance > 0)
                            {
                                t.TotalTruck -= c.TruckDistance;
                            }

                            // Expenses
                            if (c.BillingAccomodation > 0 || c.BillingMisc > 0)
                            {
                                t.TotalExpenses -= c.BillingAccomodation - c.BillingMisc;
                            }
                        }
                        // save changes to db
                        context.ManagerChargeables.DeleteObject(c);
                        context.SaveChanges();
                    }
                }
            }
        }
    }
    /// <summary>
    /// delete a nonchargeable job in the timesheet
    /// </summary>
    /// <param name="timesheetID"></param>
    public void deleteNonChargeableForID(int NonChargeId)
    {
        using (var context = new PetoEntities())
        {
            NonChargeable n = context.NonChargeables.FirstOrDefault(x => x.NonChargeId == NonChargeId);
            if (n != null)
            {
                // subtract the values from the timesheet before deleting
                TimeSheet t = context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == n.TimeSheetId);

                if (t != null)
                {
                    if (n.Hours > 0)
                    {
                        t.TotalHours -= n.Hours;
                    }
                    if (n.Distance > 0)
                    {
                        t.TotalDistance -= n.Distance;
                    }

                    if (n.Accomodation > 0 || n.Misc > 0)
                    {
                        t.TotalExpenses -= n.Accomodation - n.Misc;
                    }
                }
                context.NonChargeables.DeleteObject(n);
                context.SaveChanges();
            }
        }
    }
    /// <summary>
    /// update manager chargeable expense record
    /// </summary>
    /// <param name="hours"></param>
    /// <param name="accom"></param>
    /// <param name="distance"></param>
    /// <param name="misc"></param>
    /// <param name="day"></param>
    /// <param name="projectNo"></param>
    /// <param name="classification"></param>
    /// <param name="activity"></param>
    /// <param name="timesheetID"></param>
    /// <returns></returns>
    public bool updateManagerChargeable(string bHours, string bAccom, string bDist, string truckDist, string bMisc,
        string day, int projectNo, string classification, string activity, int timesheetID, string remarks, ref decimal totalHours, ref int totalDist, ref int totalTruck, ref decimal totalExpense)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                TimesheetManager tm = new TimesheetManager();
                ManagerChargeable cJob = context.ManagerChargeables.FirstOrDefault(x => x.ProjectId == projectNo && x.TimeSheetId == timesheetID && x.Day == day && x.Classification == classification && x.Activity == activity);

                // update fields
                if (cJob != null)
                {
                    // Hours
                    if (bHours != string.Empty)
                    {
                        if (cJob.BillingHours.HasValue)
                        {
                            totalHours -= cJob.BillingHours.Value;
                        }
                        cJob.BillingHours = Convert.ToDecimal(bHours.Trim());
                        // totalize hours
                        totalHours += cJob.BillingHours.Value;
                    }

                    // Misc
                    if (bMisc != string.Empty)
                    {
                        if (cJob.BillingMisc.HasValue)
                        {
                            totalExpense -= cJob.BillingMisc.Value;
                        }
                        cJob.BillingMisc = Convert.ToDecimal(bMisc.Trim());
                        totalExpense += cJob.BillingMisc.Value;
                    }
                    if (projectNo > 0)
                    {
                        cJob.ProjectId = projectNo;
                    }

                    if (remarks != string.Empty)
                    {
                        cJob.Remarks = remarks;
                    }
                    if (classification != "Select")
                    {
                        cJob.Classification = classification;
                    }
                    if (activity != "Select")
                    {
                        cJob.Activity = activity;
                    }
                    // Distance
                    if (bDist != string.Empty)
                    {
                        if (cJob.BillingTravelDistance.HasValue)
                        {
                            totalDist -= cJob.BillingTravelDistance.Value;
                        }
                        // assign inputted value
                        cJob.BillingTravelDistance = Convert.ToInt32(bDist.Trim());
                        // totalize distance
                        totalDist += cJob.BillingTravelDistance.Value;
                    }
                    // truck distance
                    if (truckDist != string.Empty)
                    {
                        // subtract the value if it already exists
                        if (cJob.TruckDistance.HasValue)
                        {
                            totalTruck -= cJob.TruckDistance.Value;
                        }
                        cJob.TruckDistance = Convert.ToInt32(truckDist);
                        totalTruck += cJob.TruckDistance.Value;
                    }
                    // Accom
                    if (bAccom != string.Empty)
                    {
                        if (cJob.BillingAccomodation.HasValue)
                        {
                            totalExpense -= cJob.BillingAccomodation.Value;
                        }
                        cJob.BillingAccomodation = Convert.ToDecimal(bAccom.Trim());
                        // totalize expense
                        totalExpense += cJob.BillingAccomodation.Value;
                    }
                    context.SaveChanges();

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
    /// <summary>
    /// copy nonchargeable record to manager nonchargeable record
    /// </summary>
    /// <param name="n"></param>
    public void copyToManagerNonChargeable(NonChargeable n)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                ManagerNonChargeable mNCh = new ManagerNonChargeable();

                mNCh.Hours = n.Hours;
                mNCh.Accomodations = n.Accomodation;
                mNCh.Misc = n.Misc;
                mNCh.Distance = n.Distance;

                mNCh.Remarks = n.Remarks;
                mNCh.TimeSheetId = n.TimeSheetId;
                mNCh.TypeHours = n.TypeHours;
                mNCh.TypeExpense = n.TypeExpense;
                mNCh.Day = n.Day;

                context.ManagerNonChargeables.AddObject(mNCh);
                context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
 /// <summary>
 /// create timesheet instance singleton for weekending. returns timesheetID
 /// </summary>
 /// <param name="t"></param>
 public int createTimesheet(TimeSheet t)
 {
     using (var context = new PetoEntities())
     {
         try
         {
             if (context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == t.TimeSheetId) == null)
             {
                 context.TimeSheets.AddObject(t);
                 context.SaveChanges();
                 return t.TimeSheetId;
             }
             else
             {
                 return context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == t.TimeSheetId).TimeSheetId;
             }
         }
         catch (Exception)
         {
             throw;
         }
     }
 }
    /// <summary>
    /// create a NonChargeable Record
    /// </summary>
    /// <param name="n"></param>
    public string createNonChargeable(string hours, string accom, string distance, string misc, string day, string expense, string hourType, int timesheetID, string remarks,
        ref decimal hoursLBL, ref int distLBL, ref decimal expenseLBL)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                // to ensure only not null values are saved
                if (timesheetID > 0 && (hours != "0" && hours != string.Empty) || (accom != "0" && accom != string.Empty) || (distance != "0" && distance != string.Empty) || (misc != "0" && misc != string.Empty))
                {
                    NonChargeable nonCh = new NonChargeable();
                    if (hours != "0" && hours != string.Empty)
                    {
                        nonCh.Hours = Convert.ToDecimal(hours);
                        // assign type of hour
                        if (hourType != "Select")
                        {
                            nonCh.TypeHours = hourType;
                        }
                        hoursLBL += nonCh.Hours;
                    }
                    if (accom != "0" && accom != string.Empty)
                    {
                        nonCh.Accomodation = Convert.ToDecimal(accom);
                        expenseLBL += Convert.ToDecimal(accom);
                    }
                    if (distance != "0" && distance != string.Empty)
                    {
                        nonCh.Distance = Convert.ToInt32(distance);
                        distLBL += Convert.ToInt32(distance);
                    }
                    if (remarks != string.Empty)
                    {
                        nonCh.Remarks = remarks;
                    }
                    if (misc != "0" && misc != string.Empty)
                    {
                        nonCh.Misc = Convert.ToDecimal(misc);
                        expenseLBL += Convert.ToDecimal(misc);
                    }
                    // assign expenses
                    if ((misc != "0" && misc != string.Empty) || (distance != "0" && distance != string.Empty) || (accom != "0" && accom != string.Empty))
                    {
                        if (expense != "Select")
                        {
                            nonCh.TypeExpense = expense;
                        }
                    }
                    if (misc != "0" && misc != string.Empty || distance != "0" && distance != string.Empty || accom != "0" && accom != string.Empty || hours != "0" && hours != string.Empty)
                    {
                        nonCh.TimeSheetId = timesheetID;
                    }
                    nonCh.Day = day;

                    // one of these must be selected in order to save
                    if (expense != "Select" || hours != "Select")
                    {
                        context.NonChargeables.AddObject(nonCh);
                        context.SaveChanges();
                    }
                }

                return "Data Saved Successfully";
            }
            catch (Exception ex)
            {
                return "Data was not saved due to: " + ex.Message;
            }
        }
    }
    /// <summary>
    /// update an existing timesheet
    /// </summary>
    /// <param name="id"></param>
    /// <param name="weekending"></param>
    /// <param name="status"></param>
    /// <param name="empComment"></param>
    /// <param name="mComment"></param>
    /// <param name="hours"></param>
    /// <param name="distance"></param>
    /// <param name="expenses"></param>
    public void updateTimeSheet(int id, decimal hours, int distance, int truckDist, decimal expenses, string status, string empComment, string mComment)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                TimeSheet t = context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == id);
                // determine who is trying to update the timesheet
                string name = Membership.GetUser().UserName;
                User user = context.Users.FirstOrDefault(x => x.username == name);

                // ensure timesheet exists and has not already been approved or completed
                if (t != null)
                {
                    if (status != string.Empty)
                    {
                        t.Status = status;
                    }

                    if (empComment != string.Empty)
                    {
                        t.EmployeeComments = empComment;
                    }
                    if (mComment != string.Empty)
                    {
                        t.ManagerComments = mComment;
                    }
                    t.ModifiedBy = user.username;
                    t.DateModified = DateTime.Now;

                    if (expenses > 0)
                    {
                        t.TotalExpenses = expenses;
                    }
                    else if (expenses == 0)
                    {
                        t.TotalExpenses = 0;
                    }
                    if (distance > 0)
                    {
                        t.TotalDistance = distance;
                    }
                    else if (distance == 0)
                    {
                        t.TotalDistance = 0;
                    }

                    // truck distance
                    if (truckDist > 0)
                    {
                        t.TotalTruck = truckDist;
                    }
                    else if (distance == 0)
                    {
                        t.TotalTruck = 0;
                    }

                    if (hours > 0)
                    {
                        t.TotalHours = hours;
                    }
                    else if (hours == 0)
                    {
                        t.TotalHours = 0;
                    }

                    // if timesheet is rejected copy manager comments into employee comments
                    if (t.Status == "Rejected")
                    {
                        t.EmployeeComments = t.EmployeeComments + "\n MANAGER COMMENT: \n " + mComment;
                    }

                    // only a manager can update an already approved timesheet
                    if (Roles.GetRolesForUser().Contains("Manager") && t.Status == "Approved")
                    {
                        t.ApprovedBy = user.username;
                        t.DateApproved = DateTime.Now;
                        t.ManagerComments = mComment;

                        context.SaveChanges();
                    }
                    else if (t.Status != "Approved")
                    {
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    /// <summary>
    /// update a non-chargeable record
    /// </summary>
    /// <param name="hours"></param>
    /// <param name="accom"></param>
    /// <param name="distance"></param>
    /// <param name="misc"></param>
    /// <param name="day"></param>
    /// <param name="density"></param>
    /// <param name="expense"></param>
    /// <param name="hourType"></param>
    /// <param name="timesheetID"></param>
    /// <returns></returns>
    public bool updateNonChargeable(string hours, string accom, string distance, string misc, string day, string expense, string hourType, int timesheetID, string remarks,
        ref decimal totalHours, ref int totalDist, ref decimal totalExpense)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                NonChargeable nCh = context.NonChargeables.FirstOrDefault(x => x.Day == day && x.TimeSheetId == timesheetID && (x.TypeExpense == expense || x.TypeHours == hourType));

                if (nCh != null)
                {
                    if (hours != string.Empty)
                    {
                        totalHours -= nCh.Hours;
                        nCh.Hours = Convert.ToDecimal(hours);
                        totalHours += nCh.Hours;
                    }
                    if (misc != string.Empty)
                    {
                        if (nCh.Misc.HasValue)
                        {
                            totalExpense -= nCh.Misc.Value;
                        }
                        nCh.Misc = Convert.ToDecimal(misc.Trim());
                        totalExpense += nCh.Misc.Value;
                    }

                    if (remarks != string.Empty)
                    {
                        nCh.Remarks = remarks;
                    }
                    if (expense != "Select")
                    {
                        nCh.TypeExpense = expense;
                    }
                    if (hourType != "Select")
                    {
                        nCh.TypeHours = hourType;
                    }
                    if (distance != string.Empty)
                    {
                        if (nCh.Distance.HasValue)
                        {
                            totalDist -= nCh.Distance.Value;
                        }
                        // assign distance
                        nCh.Distance = Convert.ToInt32(distance.Trim());
                    }
                    if (accom != string.Empty)
                    {
                        if (nCh.Accomodation.HasValue)
                        {
                            totalExpense -= nCh.Accomodation.Value;
                        }
                        nCh.Accomodation = Convert.ToDecimal(accom.Trim());
                        totalExpense += nCh.Accomodation.Value;
                    }

                    // one drop down list value cannot be Select
                    if (expense != "Select" || hourType != "Select")
                    {
                        context.SaveChanges();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
 /// <summary>
 /// get chargeablejobs for the timesheet
 /// </summary>
 /// <param name="timesheetID"></param>
 /// <returns></returns>
 public List<ChargeableJob> getChargeableJobsForTimesheetID(int timesheetID)
 {
     using (var context = new PetoEntities())
     {
         return (from x in context.ChargeableJobs
                 where x.TimeSheetId == timesheetID
                 select x).ToList();
     }
 }
 /// <summary>
 /// delete a chargeable in the timesheet
 /// </summary>
 /// <param name="timesheetID"></param>
 public void deleteChargeableForID(int ChargeableId)
 {
     using (var context = new PetoEntities())
     {
         ChargeableJob c = context.ChargeableJobs.FirstOrDefault(x => x.ChargeableId == ChargeableId);
         if (c != null)
         {
             // subtract the values from the timesheet
             TimeSheet t = context.TimeSheets.FirstOrDefault(x => x.TimeSheetId == c.TimeSheetId);
             if (t != null)
             {
                 if (c.EmpHours > 0)
                 {
                     t.TotalHours -= c.EmpHours;
                 }
                 if (c.TravelDistance > 0)
                 {
                     t.TotalDistance -= c.TravelDistance;
                 }
                 if (c.TruckDistance > 0)
                 {
                     t.TotalTruck -= c.TruckDistance;
                 }
                 if (c.Accomodations > 0 || c.Misc > 0)
                 {
                     t.TotalExpenses -= c.Accomodations - c.Misc;
                 }
             }
             context.ChargeableJobs.DeleteObject(c);
             context.SaveChanges();
         }
     }
 }
    /// <summary>
    /// get the totals for timesheetID
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public decimal[] getChargeableTotalsForID(int id)
    {
        decimal[] result = new decimal[8];

        using (var context = new PetoEntities())
        {
            // get manager chargeable
            List<ManagerChargeable> mCh = (from x in context.ManagerChargeables
                                           where x.TimeSheetId == id
                                           select x).ToList();
            foreach (ManagerChargeable item in mCh)
            {
                switch (item.Day)
                {
                    case "Sunday":
                        if (item.BillingHours.HasValue)
                        {
                            result[0] += item.BillingHours.Value;
                        }
                        break;

                    case "Monday":
                        if (item.BillingHours.HasValue)
                        {
                            result[1] += item.BillingHours.Value;
                        }
                        break;

                    case "Tuesday":
                        if (item.BillingHours.HasValue)
                        {
                            result[2] += item.BillingHours.Value;
                        }
                        break;

                    case "Wednesday":
                        if (item.BillingHours.HasValue)
                        {
                            result[3] += item.BillingHours.Value;
                        }
                        break;

                    case "Thursday":
                        if (item.BillingHours.HasValue)
                        {
                            result[4] += item.BillingHours.Value;
                        }
                        break;

                    case "Friday":
                        if (item.BillingHours.HasValue)
                        {
                            result[5] += item.BillingHours.Value;
                        }
                        break;

                    case "Saturday":
                        if (item.BillingHours.HasValue)
                        {
                            result[6] += item.BillingHours.Value;
                        }
                        break;
                    default:
                        break;
                }
            }
            return result;
        }
    }
 /// <summary>
 /// delete lab test
 /// </summary>
 /// <param name="timesheet"></param>
 /// <param name="day"></param>
 /// <param name="manager"></param>
 public void deleteLabTest(int id, int time, bool manager)
 {
     using (var context = new PetoEntities())
     {
         TimeSheet ts = context.TimeSheets.FirstOrDefault(t => t.TimeSheetId == time);
         if (manager)
         {
             // subtract hours from total hours in timesheet
             ManagerTest t = context.ManagerTests.FirstOrDefault(x => x.TestId == id);
             if (t != null)
             {
                 if (t.LabTest > 0)
                 {
                     ts.TotalHours -= t.LabTest;
                 }
                 context.ManagerTests.DeleteObject(t);
                 context.SaveChanges();
             }
         }
         else
         {
             EmpTest t = context.EmpTests.FirstOrDefault(x => x.TestId == id);
             if (t != null)
             {
                 // subtract hours from total hours in timesheet
                 if (t.LabTest > 0)
                 {
                     ts.TotalHours -= t.LabTest;
                 }
                 context.EmpTests.DeleteObject(t);
                 context.SaveChanges();
             }
         }
     }
 }
    /// <summary>
    /// create manager chargeable job in db
    /// </summary>
    /// <param name="j"></param>
    public string createManagerChargeable(string bHours, string bAccom, string bDist, string truckDist, string bMisc,
        string day, string projectNo, string classification, string activity, int timesheetID, string remarks, ref decimal hoursLBL, ref int distLBL, ref int truckLBL, ref decimal expenseLBL)
    {
        using (var context = new PetoEntities())
        {
            try
            {
                if (timesheetID > 0 && bHours != "0" && bHours != string.Empty || bAccom != "0" && bAccom != string.Empty || bDist != "0" && bDist != string.Empty || bMisc != "0" && bMisc != string.Empty || truckDist != "0" && truckDist != string.Empty)
                {
                    ManagerChargeable c = new ManagerChargeable();
                    // Hours

                    if (bHours != "0" && bHours != string.Empty)
                    {
                        c.BillingHours = Convert.ToDecimal(bHours);
                        if (c.BillingHours.HasValue)
                        {
                            hoursLBL += c.BillingHours.Value;
                        }
                    }
                    // Accomodation

                    if (bAccom != "0" && bAccom != string.Empty)
                    {
                        c.BillingAccomodation = Convert.ToDecimal(bAccom);
                        expenseLBL += Convert.ToDecimal(c.BillingAccomodation);
                    }
                    // Distance

                    if (bDist != "0" && bDist != string.Empty)
                    {
                        c.BillingTravelDistance = Convert.ToInt32(bDist);
                        distLBL += Convert.ToInt32(bDist);
                    }
                    // truck distance
                    if (truckDist != "0" && truckDist != string.Empty)
                    {
                        c.TruckDistance = Convert.ToInt32(truckDist);
                        truckLBL += Convert.ToInt32(truckDist);
                    }

                    // Misc
                    if (bMisc != "0" && bMisc != string.Empty)
                    {
                        c.BillingMisc = Convert.ToDecimal(bMisc);
                        expenseLBL += Convert.ToDecimal(c.BillingMisc);
                    }

                    if (remarks != string.Empty)
                    {
                        c.Remarks = remarks;
                    }
                    if (c.BillingHours > 0 || c.BillingTravelDistance > 0 || c.BillingAccomodation > 0 || c.BillingMisc > 0 || c.TruckDistance > 0)
                    {
                        c.Day = day;
                        c.ProjectId = idForProjectNo(projectNo);

                        if (classification != "Select")
                        {
                            c.Classification = classification;
                        }

                        c.Activity = activity;
                        c.TimeSheetId = Convert.ToInt32(timesheetID);

                        context.ManagerChargeables.AddObject(c);
                        context.SaveChanges();
                        return "Data was successfully saved";
                    }
                    else
                    {
                        return "Data was not Entered in input fields.";
                    }
                }
                else
                {
                    return "Input values are empty";
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
 /// <summary>
 /// update lab test
 /// </summary>
 /// <param name="day"></param>
 /// <param name="tests"></param>
 /// <param name="timesheet"></param>
 /// <param name="manager"></param>
 public bool updateLabTest(string day, string density, string lab, int timesheet, bool manager, ref decimal totalLab)
 {
     using (var context = new PetoEntities())
     {
         try
         {
             if (manager)
             {
                 ManagerTest t = context.ManagerTests.FirstOrDefault(x => x.TimeSheetId == timesheet && x.Day == day);
                 t.Day = day;
                 if (density != string.Empty)
                 {
                     t.DensityTest = Convert.ToInt32(density);
                 }
                 if (lab != string.Empty)
                 {
                     totalLab -= Convert.ToInt32(lab);
                     t.LabTest = Convert.ToInt32(lab);
                     totalLab += t.LabTest.Value;
                 }
                 context.SaveChanges();
                 return true;
             }
             else
             {
                 EmpTest t = context.EmpTests.FirstOrDefault(x => x.TimeSheetId == timesheet && x.Day == day);
                 if (density != string.Empty)
                 {
                     t.DensityTest = Convert.ToInt32(density);
                 }
                 if (lab != string.Empty)
                 {
                     totalLab -= Convert.ToInt32(lab);
                     t.LabTest = Convert.ToInt32(lab);
                     totalLab += t.LabTest.Value;
                 }
                 context.SaveChanges();
                 return true;
             }
         }
         catch (Exception)
         {
             return false;
         }
     }
 }