public ActionResult GetTaskResources(Guid?DepartmentID, Guid?ProjectID, int?page, string sortBy, ResourceFilterVM filterBy)
        {
            IEnumerable <Project> projects;

            if (DepartmentID == null && ProjectID == null)
            {
                projects = ProjectRepo.GetCollections();
            }
            else if (ProjectID != null)
            {
                projects = ProjectRepo.GetCollections().Where(p => p.ProjectGUID == ProjectID);
            }
            else
            {
                projects = ProjectRepo.GetCollections().Where(p => p.RequiredDeptGUID == DepartmentID);
            }

            var q = from p in projects
                    join t in TaskRepo.GetCollections() on p.ProjectGUID equals t.ProjectGUID
                    join tr in ResourceRepo.GetCollections() on t.TaskGUID equals tr.TaskGUID
                    join c in ResourceCatRepo.GetCollections() on tr.CategoryID equals c.CategoryID
                    select new ProjectResourceVM
            {
                ProjectGUID  = p.ProjectGUID,
                ProjectName  = p.ProjectName,
                TaskGUID     = t.TaskGUID,
                TaskName     = t.TaskName,
                ResourceGUID = tr.ResourceGUID,
                ResourceID   = tr.ResourceID,
                ResourceName = tr.ResourceName,
                CategoryID   = c.CategoryID,
                CategoryName = c.CategoryName,
                Quantity     = tr.Quantity,
                Unit         = tr.Unit,
                UnitPrice    = tr.UnitPrice,
                SubTotal     = (tr.UnitPrice * tr.Quantity),
                Date         = tr.Date,
                Description  = tr.Description
            };

            var ProjectResourceList = q.ToList().AsQueryable().Sort(sortBy).Filter(filterBy);

            Response.Cookies["sortBy"].Value = sortBy;
            ViewBag.Count              = ProjectResourceList.Count();
            ViewBag.sortByDate         = string.IsNullOrEmpty(sortBy) ? "DateDesc" : "";
            ViewBag.sortByProjectName  = sortBy == "ProjectName" ? "ProjectNameDesc" : "ProjectName";
            ViewBag.sortByTaskName     = sortBy == "TaskName" ? "TaskNameDesc" : "TaskName";
            ViewBag.sortByResourceName = sortBy == "ResourceName" ? "ResourceNameDesc" : "ResourceName";
            ViewBag.sortByResourceCat  = sortBy == "ResourceCat" ? "ResourceCatDesc" : "ResourceCat";
            ViewBag.sortByQuantity     = sortBy == "Quantity" ? "QuantityDesc" : "Quantity";
            ViewBag.sortByUnitPrice    = sortBy == "UnitPrice" ? "UnitPriceDesc" : "UnitPrice";
            ViewBag.sortBySubtotal     = sortBy == "SubTotal" ? "SubTotalDesc" : "SubTotal";

            return(PartialView(ProjectResourceList.ToPagedList(page ?? 1, 8)));
        }
Ejemplo n.º 2
0
        public ActionResult ExportAsExcel(FormCollection form, Guid?DepartmentID, Guid?ProjectID, string sortBy, ResourceFilterVM filterBy)
        {
            IEnumerable <Project> projects;

            if (DepartmentID == null && ProjectID == null)
            {
                projects = ProjectRepo.GetCollections();
            }
            else if (ProjectID != null)
            {
                projects = ProjectRepo.GetCollections().Where(p => p.ProjectGUID == ProjectID);
            }
            else
            {
                projects = ProjectRepo.GetCollections().Where(p => p.RequiredDeptGUID == DepartmentID);
            }

            var q = from p in projects
                    join t in TaskRepo.GetCollections() on p.ProjectGUID equals t.ProjectGUID
                    join tr in ResourceRepo.GetCollections() on t.TaskGUID equals tr.TaskGUID
                    join c in ResourceCatRepo.GetCollections() on tr.CategoryID equals c.CategoryID
                    select new ProjectResourceVM
            {
                ProjectGUID  = p.ProjectGUID,
                ProjectName  = p.ProjectName,
                TaskGUID     = t.TaskGUID,
                TaskName     = t.TaskName,
                ResourceGUID = tr.ResourceGUID,
                ResourceID   = tr.ResourceID,
                ResourceName = tr.ResourceName,
                CategoryID   = c.CategoryID,
                CategoryName = c.CategoryName,
                Quantity     = tr.Quantity,
                Unit         = tr.Unit,
                UnitPrice    = tr.UnitPrice,
                SubTotal     = (tr.UnitPrice * tr.Quantity),
                Date         = tr.Date,
                Description  = tr.Description
            };

            var ProjectResourceList = q.AsQueryable().Sort(sortBy).Filter(filterBy).Select(p => new
            {
                費用發生日期 = p.Date.ToShortDateString(),
                專案名稱   = p.ProjectName,
                工作名稱   = p.TaskName,
                費用名稱   = p.ResourceName,
                類別名稱   = p.CategoryName,
                數量     = p.Quantity,
                單價     = p.UnitPrice,
                小計     = p.SubTotal,
                備註     = p.Description
            });

            var gv = new GridView();

            gv.DataSource = ProjectResourceList.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename = [" + DateTime.Now.ToShortDateString() + "]費用清單.xls");
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter   objStringWriter   = new StringWriter();
            HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);

            gv.RenderControl(objHtmlTextWriter);
            Response.Output.Write(objStringWriter.ToString());
            Response.Flush();
            Response.End();
            return(Content("Complete!", "text/plain"));
        }