public void CreateHourCosts(Project project, Project similarProject)
        {
            using (var context = new ScrumFactoryEntities(this.connectionString)) {

                // get similar costs
                RoleHourCost[] similarCosts = null;
                if(similarProject!=null)
                    similarCosts = GetHourCosts(similarProject.ProjectUId);

                // fore each role at this project
                foreach (Role r in project.Roles) {

                    // checks if there is a role with the same shortname at the similar project, and if so, uses its costs
                    Role similarRole = null;
                    string roleName = r.RoleShortName.ToLower();
                    if(similarProject!=null)
                       similarRole = similarProject.Roles.FirstOrDefault(sr => sr.RoleShortName.ToLower() == roleName);

                    RoleHourCost similarHourCost = null;
                    if(similarRole!=null && similarCosts!=null)
                        similarHourCost = similarCosts.FirstOrDefault(c => c.RoleUId == similarRole.RoleUId);
                    if(similarHourCost==null)
                        similarHourCost = new RoleHourCost() { Price = 0, Cost = 0 };

                    // only if role is new
                    RoleHourCost oldCost = context.RoleHourCosts.SingleOrDefault(h => h.RoleUId == r.RoleUId);
                    if (oldCost == null) {
                        RoleHourCost cost = new RoleHourCost() { RoleUId = r.RoleUId, ProjectUId = project.ProjectUId, Cost = similarHourCost.Cost, Price = similarHourCost.Price };
                        context.RoleHourCosts.AddObject(cost);
                    }

                }
                context.SaveChanges();
            }
        }
        public bool IsBacklogItemFirstPlan(Project project, string backlogItemUId)
        {
            using (var context = new ScrumFactoryEntities(this.connectionString)) {

                int? minPlanNumber = context.PlannedHours.Where(p => p.BacklogItemUId == backlogItemUId && p.SprintNumber!=null).Min(p =>  (int?)p.PlanningNumber);
                if (minPlanNumber == null)
                    return true;

                if (project.CurrentPlanningNumber != (int)minPlanNumber)
                    return false;

                return true;

            }
        }
 /// <summary>
 /// Create a new Project object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 public static Project CreateProject(global::System.String id, global::System.String name)
 {
     Project project = new Project();
     project.Id = id;
     project.Name = name;
     return project;
 }
        public Project GetLastSimilarProject(Project project, bool onlyWithProposals = false)
        {
            using (var context = new ScrumFactoryEntities(this.connectionString)) {

                // first tries to get the last project from same client
                var projectQuery = context.Projects
                    .Include("Roles").Include("Sprints").Include("Memberships")
                    .Where(p => p.ClientName == project.ClientName && p.ProjectUId != project.ProjectUId);

                if(onlyWithProposals)
                    projectQuery = projectQuery.Where(p => context.RoleHourCosts.Any(o => o.ProjectUId == p.ProjectUId && o.Price>0));

                Project similar = projectQuery.OrderByDescending(p => p.CreateDate).FirstOrDefault();

                if (similar != null)
                    return similar;

                // id there is no project from this client before, gets the last one of any client made by me
                projectQuery = context.Projects
                    .Include("Roles").Include("Sprints").Include("Memberships")
                    .Where(p => p.ProjectUId!=project.ProjectUId && p.CreateBy==project.CreateBy);

                if (onlyWithProposals)
                    projectQuery = projectQuery.Where(p => context.RoleHourCosts.Any(o => o.ProjectUId == p.ProjectUId && o.Price > 0));

                similar = projectQuery.OrderByDescending(p => p.CreateDate).FirstOrDefault();

                if (similar != null)
                    return similar;

                // id there is no project from this client before, gets the last one of any client
                projectQuery = context.Projects
                    .Include("Roles").Include("Sprints").Include("Memberships")
                    .Where(p => p.ProjectUId != project.ProjectUId);

                if (onlyWithProposals)
                    projectQuery = projectQuery.Where(p => context.RoleHourCosts.Any(o => o.ProjectUId == p.ProjectUId && o.Price > 0));

                similar = projectQuery.OrderByDescending(p => p.CreateDate).FirstOrDefault();

                return similar;
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Projects EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToProjects(Project project)
 {
     base.AddObject("Projects", project);
 }