Esempio n. 1
0
        public bool PostChanges(WorkModel model)
        {
            WorksheetEntities entities = new WorksheetEntities();

            try
            {
                Employee MyProfile = (from ce in entities.Employees where (ce.Active == true) && (ce.FirstName + " " + ce.LastName == model.FirstName + " " + model.LastName) select ce).FirstOrDefault();
                if (MyProfile == null)
                {
                    return(false);
                }
                int      employeeId = MyProfile.Id_Employee;
                Employee existing   = (from e in entities.Employees where (e.Id_Employee == employeeId) && (e.Active == true) select e).FirstOrDefault();
                if (existing != null && model.Picture != null && model.Operation == "Save")
                {
                    existing.FirstName       = model.FirstName;
                    existing.LastName        = model.LastName;
                    existing.PhoneNumber     = model.PhoneNumber.ToString();
                    existing.EmailAddress    = model.Email;
                    existing.LastModifiedAt  = DateTime.Now;
                    existing.EmployeePicture = model.Picture;
                }
                else if (existing != null && model.Picture == null && model.Operation == "Save")
                {
                    existing.FirstName      = model.FirstName;
                    existing.LastName       = model.LastName;
                    existing.PhoneNumber    = model.PhoneNumber.ToString();
                    existing.EmailAddress   = model.Email;
                    existing.LastModifiedAt = DateTime.Now;
                }
                else if (existing != null && model.Operation == "SavePW")
                {
                    existing.Password = model.Password;
                }

                entities.SaveChanges();
            }
            catch
            {
                return(false);
            }
            finally
            {
                entities.Dispose();
            }

            return(true);
        }
Esempio n. 2
0
        public bool PostWork(WorkModel model)
        {
            WorksheetEntities entities = new WorksheetEntities();

            Customer customer = (from cu in entities.Customers where (cu.Active == true) && (cu.CustomerName == model.CustomerName) select cu).FirstOrDefault();

            try
            {
                // Save chosen work
                if (model.Operation == "Save")
                {
                    WorkAssignment newEntry = new WorkAssignment()
                    {
                        Id_Customer = customer.Id_Customer,
                        Title       = model.WorkTitle,
                        Description = model.Description,
                        Deadline    = model.Deadline,
                        InProgress  = true,
                        CreatedAt   = DateTime.Now,
                        Active      = true
                    };

                    entities.WorkAssignments.Add(newEntry);
                }
                // Modify chosen work
                else if (model.Operation == "Modify")
                {
                    WorkAssignment existing = (from wa in entities.WorkAssignments where (wa.Id_WorkAssignment == model.WorkID) && (wa.Active == true) select wa).FirstOrDefault();
                    if (existing != null)
                    {
                        existing.Title          = model.WorkTitle;
                        existing.Description    = model.Description;
                        existing.Deadline       = model.Deadline;
                        existing.LastModifiedAt = DateTime.Now;
                    }
                    else
                    {
                        return(false);
                    }
                }
                // Delete chosen work
                else if (model.Operation == "Delete")
                {
                    WorkAssignment existing = (from wa in entities.WorkAssignments where (wa.Id_WorkAssignment == model.WorkID) select wa).FirstOrDefault();
                    if (existing != null)
                    {
                        entities.WorkAssignments.Remove(existing);
                    }
                    else
                    {
                        return(false);
                    }
                }
                // Assign chosen work to an employee
                else if (model.Operation == "Assign")
                {
                    WorkAssignment assignment = (from wa in entities.WorkAssignments where (wa.Id_WorkAssignment == model.WorkID) && (wa.Active == true) && (wa.InProgress == true) select wa).FirstOrDefault();
                    if (assignment == null)
                    {
                        return(false);
                    }

                    Employee emp = (from e in entities.Employees where (e.Id_Employee == model.EmployeeId) select e).FirstOrDefault();
                    if (emp == null)
                    {
                        return(false);
                    }

                    int workId     = assignment.Id_WorkAssignment;
                    int customerId = assignment.Id_Customer.Value;

                    assignment.InProgressAt = DateTime.Now;

                    Timesheet newEntry = new Timesheet()
                    {
                        Id_Customer       = customerId,
                        Id_Contractor     = emp.Id_Contractor,
                        Id_Employee       = emp.Id_Employee,
                        Id_WorkAssignment = workId,
                        StartTime         = DateTime.Now,
                        CreatedAt         = DateTime.Now,
                        Active            = true,
                        WorkComplete      = false
                    };

                    entities.Timesheets.Add(newEntry);
                }

                else if (model.Operation == "MarkComplete")
                {
                    WorkAssignment assignment = (from wa in entities.WorkAssignments where (wa.Id_WorkAssignment == model.WorkID) && (wa.Active == true) && (wa.InProgress == true) && (wa.InProgressAt != null) select wa).FirstOrDefault();
                    if (assignment == null)
                    {
                        return(false);
                    }

                    int workId     = assignment.Id_WorkAssignment;
                    int customerId = assignment.Id_Customer.Value;

                    assignment.CompletedAt = DateTime.Now;
                    assignment.Completed   = true;
                    assignment.InProgress  = false;

                    Timesheet existing = (from e in entities.Timesheets where (e.Id_WorkAssignment == workId) && (e.Id_Customer == customerId) select e).FirstOrDefault();
                    if (existing != null)
                    {
                        existing.WorkComplete   = true;
                        existing.StopTime       = DateTime.Now;
                        existing.LastModifiedAt = DateTime.Now;
                        existing.Comments       = "Work set to complete by Admin";
                    }
                }

                entities.SaveChanges();
            }
            catch
            {
                return(false);
            }
            finally
            {
                entities.Dispose();
            }

            return(true);
        }
Esempio n. 3
0
        // POST: Customers
        public bool PostCustomer(WorkModel model)
        {
            WorksheetEntities entities = new WorksheetEntities();

            try
            {
                if (model.CustOperation == "Save")
                {
                    Customer newEntry = new Customer()
                    {
                        CustomerName  = model.CustomerName,
                        ContactPerson = model.ContactPerson,
                        PhoneNumber   = model.CustomerPhoneNumber,
                        EmailAddress  = model.CustomerEmail,
                        CreatedAt     = DateTime.Now,
                        Active        = true
                    };

                    entities.Customers.Add(newEntry);
                }

                // Modify chosen customer
                else if (model.CustOperation == "Modify")
                {
                    Customer existing = (from c in entities.Customers where (c.Id_Customer == model.CustomerId) && (c.Active == true) select c).FirstOrDefault();
                    if (existing != null)
                    {
                        existing.CustomerName   = model.CustomerName;
                        existing.ContactPerson  = model.ContactPerson;
                        existing.PhoneNumber    = model.CustomerPhoneNumber;
                        existing.EmailAddress   = model.CustomerEmail;
                        existing.LastModifiedAt = DateTime.Now;
                    }
                    else
                    {
                        return(false);
                    }
                }


                // Delete chosen customer
                else if (model.CustOperation == "Delete")
                {
                    Customer chosenCustomer = (from cc in entities.Customers where (cc.CustomerName == model.CustomerName) select cc).FirstOrDefault();
                    if (chosenCustomer == null)
                    {
                        return(false);
                    }
                    int      customerId = chosenCustomer.Id_Customer;
                    Customer existing   = (from e in entities.Customers where (e.Id_Customer == customerId) select e).FirstOrDefault();
                    if (existing != null)
                    {
                        entities.Customers.Remove(existing);
                    }
                    else
                    {
                        return(false);
                    }
                }

                entities.SaveChanges();
            }
            catch
            {
                return(false);
            }
            finally
            {
                entities.Dispose();
            }

            return(true);
        }
        // POST: Contractors
        public bool PostContractor(WorkModel model)
        {
            WorksheetEntities entities = new WorksheetEntities();

            try
            {
                if (model.ContOperation == "Save")
                {
                    Contractor newEntry = new Contractor()
                    {
                        CompanyName   = model.ContractorName,
                        ContactPerson = model.ContractorContactPerson,
                        PhoneNumber   = model.ContractorPhoneNumber,
                        EmailAddress  = model.ContractorEmail,
                        VatId         = model.VatId,
                        HourlyRate    = int.Parse(model.HourlyRate),
                        Active        = true,
                        CreatedAt     = DateTime.Now
                    };

                    entities.Contractors.Add(newEntry);
                }

                // Modify chosen contractor
                else if (model.ContOperation == "Modify")
                {
                    Contractor existing = (from co in entities.Contractors where (co.Id_Contractor == model.ContractorId) && (co.Active == true) select co).FirstOrDefault();
                    if (existing != null)
                    {
                        existing.CompanyName    = model.ContractorName;
                        existing.ContactPerson  = model.ContractorContactPerson;
                        existing.PhoneNumber    = model.ContractorPhoneNumber;
                        existing.EmailAddress   = model.ContractorEmail;
                        existing.VatId          = model.VatId;
                        existing.HourlyRate     = int.Parse(model.HourlyRate);
                        existing.LastModifiedAt = DateTime.Now;
                    }
                    else
                    {
                        return(false);
                    }
                }


                // Delete chosen contractor
                else if (model.ContOperation == "Delete")
                {
                    Contractor existing = (from e in entities.Contractors where (e.Id_Contractor == model.ContractorId) select e).FirstOrDefault();
                    if (existing != null)
                    {
                        entities.Contractors.Remove(existing);
                    }
                    else
                    {
                        return(false);
                    }
                }

                entities.SaveChanges();
            }
            catch
            {
                return(false);
            }
            finally
            {
                entities.Dispose();
            }

            return(true);
        }
        public bool PostEmployee(WorkModel model)
        {
            WorksheetEntities entities = new WorksheetEntities();

            try
            {
                if (model.EmpOperation == "Save")
                {
                    Contractor contractor = (from c in entities.Contractors where (c.CompanyName == model.ContractorName) select c).FirstOrDefault();
                    Employee   newEntry   = new Employee()
                    {
                        Id_Contractor = contractor.Id_Contractor,
                        Username      = model.UserName,
                        Password      = model.Password,
                        FirstName     = model.FirstName,
                        LastName      = model.LastName,
                        PhoneNumber   = model.PhoneNumber.ToString(),
                        EmailAddress  = model.Email,
                        CreatedAt     = DateTime.Now,
                        Active        = true,
                    };

                    entities.Employees.Add(newEntry);
                }

                // Modify chosen employee
                else if (model.EmpOperation == "Modify")
                {
                    Employee existing = (from e in entities.Employees where (e.Id_Employee == model.EmployeeId) && (e.Active == true) select e).FirstOrDefault();
                    if (existing != null)
                    {
                        existing.FirstName       = model.FirstName;
                        existing.LastName        = model.LastName;
                        existing.PhoneNumber     = model.PhoneNumber.ToString();
                        existing.EmailAddress    = model.Email;
                        existing.LastModifiedAt  = DateTime.Now;
                        existing.EmployeePicture = model.Picture;
                    }
                    else
                    {
                        return(false);
                    }
                }

                // Delete chosen work
                else if (model.EmpOperation == "Delete")
                {
                    Employee existing = (from e in entities.Employees where (e.Id_Employee == model.EmployeeId) select e).FirstOrDefault();
                    if (existing != null)
                    {
                        entities.Employees.Remove(existing);
                    }
                    else
                    {
                        return(false);
                    }
                }

                entities.SaveChanges();
            }
            catch
            {
                return(false);
            }
            finally
            {
                entities.Dispose();
            }

            return(true);
        }