public bool CreateFromTemplate(int TemplateDocsID, int ProjectID, int EventID, int?ReportPeriodID)
        {
            bool result = true;

            try
            {
                TemplateService ts      = new TemplateService();
                string          dir     = ConfigurationManager.AppSettings["PhysicalPath"];
                string          WordDoc = ts.GenerateDocumentFromTemplateName(TemplateDocsID, EventID, ProjectID, dir, ReportPeriodID);

                TemplateFile tfile = ts.GetTemplateDocument(TemplateDocsID).TemplateFile;

                string virtdir = "files/" + ProjectID.ToString() + "/"; // "files/A" + DateTime.Now.Year.ToString().Substring(2) + ProjectID.ToString() + "/";
                dir += virtdir.Replace("/", "\\");

                if (!(Directory.Exists(dir)))
                {
                    Directory.CreateDirectory(dir);
                }
                string fileformat = EventID.ToString() + "_" + ProjectID.ToString() + "_";
                int    i;
                for (i = 1; System.IO.File.Exists(dir + fileformat + i.ToString() + "." + tfile.fileextension); i++)
                {
                }
                System.IO.File.WriteAllText(dir + fileformat + i.ToString() + "." + tfile.fileextension, WordDoc);
                ProjectEventDocument docItem = new ProjectEventDocument();
                docItem.Author               = session.CurrentUser.FirstName + " " + session.CurrentUser.LastName + " " + session.CurrentUser.MiddleName;;
                docItem.CreatedDate          = DateTime.Now;
                docItem.fileextension        = tfile.fileextension;
                docItem.FileName             = fileformat + i.ToString() + "." + tfile.fileextension;
                docItem.PhysicalAbsolutePath = dir + fileformat + i.ToString() + "." + tfile.fileextension;
                docItem.PhysicalVirtualPath  = "\\" + virtdir.Replace("/", "\\") + fileformat + i.ToString() + "." + tfile.fileextension;
                docItem.UpdatedDate          = docItem.CreatedDate;
                docItem.WebVirtualPath       = "/" + virtdir + fileformat + i.ToString() + "." + tfile.fileextension;
                //docItem.
                ProjectEventService pes = new ProjectEventService();
                pes.InsertDocument(docItem, EventID);

                TemplateDocument tdoc = ts.GetTemplateDocument(TemplateDocsID);
                if (tdoc != null)
                {
                    ProjectService projservice = new ProjectService();

                    //acknowledgement letter - completed project
                    if (tdoc.TemplateDocName.ToLower().Contains("letter") && tdoc.TemplateDocName.ToLower().Contains("acknowledgement"))
                    {
                        AppDropDownsService apservice = new AppDropDownsService();
                        ProposalStatusList  psitem    = apservice.ProposalStatusListGetItem("Completed");

                        //if we have needed status in DB then
                        if (psitem != null)
                        {
                            Project        _project = projservice.GetProposalInfo(ProjectID);
                            ProposalStatus _prop    = _project.ProposalStatus;
                            _prop.PropStatusID = psitem.ProposalStatusID;
                            projservice.ProposalStatusUpdate(_prop);
                        }
                    }

                    //award letter - Active project
                    if (tdoc.TemplateDocName.ToLower().Contains("letter") && tdoc.TemplateDocName.ToLower().Contains("award"))
                    {
                        AppDropDownsService apservice = new AppDropDownsService();
                        ProposalStatusList  psitem    = apservice.ProposalStatusListGetItem("Active");

                        //if we have needed status in DB then
                        if (psitem != null)
                        {
                            Project        _project = projservice.GetProposalInfo(ProjectID);
                            ProposalStatus _prop    = _project.ProposalStatus;
                            _prop.PropStatusID = psitem.ProposalStatusID;
                            projservice.ProposalStatusUpdate(_prop);
                        }
                    }

                    //close out letter - Closed project
                    if (tdoc.TemplateDocName.ToLower().Contains("letter") && tdoc.TemplateDocName.ToLower().Contains("close") && tdoc.TemplateDocName.ToLower().Contains("out"))
                    {
                        AppDropDownsService apservice = new AppDropDownsService();
                        ProposalStatusList  psitem    = apservice.ProposalStatusListGetItem("Closed");

                        //if we have needed status in DB then
                        if (psitem != null)
                        {
                            Project        _project = projservice.GetProposalInfo(ProjectID);
                            ProposalStatus _prop    = _project.ProposalStatus;
                            _prop.PropStatusID = psitem.ProposalStatusID;
                            //  projservice.ProposalStatusUpdate(_prop);
                        }
                    }

                    //suspended letter - Active project
                    if (tdoc.TemplateDocName.ToLower().Contains("letter") && tdoc.TemplateDocName.ToLower().Contains("suspended"))
                    {
                        AppDropDownsService apservice = new AppDropDownsService();
                        ProposalStatusList  psitem    = apservice.ProposalStatusListGetItem("Suspended");

                        //if we have needed status in DB then
                        if (psitem != null)
                        {
                            Project        _project = projservice.GetProposalInfo(ProjectID);
                            ProposalStatus _prop    = _project.ProposalStatus;
                            _prop.PropStatusID = psitem.ProposalStatusID;
                            projservice.ProposalStatusUpdate(_prop);
                        }
                    }

                    //reject letter - Rejected project
                    if (tdoc.TemplateDocName.ToLower().Contains("letter") && tdoc.TemplateDocName.ToLower().Contains("reject"))
                    {
                        AppDropDownsService apservice = new AppDropDownsService();
                        ProposalStatusList  psitem    = apservice.ProposalStatusListGetItem("Rejected");

                        //if we have needed status in DB then
                        if (psitem != null)
                        {
                            Project        _project = projservice.GetProposalInfo(ProjectID);
                            ProposalStatus _prop    = _project.ProposalStatus;
                            _prop.PropStatusID = psitem.ProposalStatusID;
                            projservice.ProposalStatusUpdate(_prop);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return(result);
        }
        public ActionResult UserAction(string useraction, ProjectEvent o, string SSPOrGrantee)
        {
            if (Session["ProposalID"] == null)
            {
                return(RedirectToAction("Search", "ProposalInfo"));
            }

            ProjectEventService pes = new ProjectEventService();

            switch (useraction.ToLower())
            {
            case "generate file from template":
                if (o.EventID > 0)
                {
                    pes.Update(o);
                }
                return(RedirectToAction("SelectTemplate", new { ProjectID = o.ProjectID, EventID = o.EventID, EventTypeID = o.EventTypeID, ReportPeriodID = o.ReportPeriodID }));

            case "insert":
                // if (o.EventID > 0)
                pes.Insert(o);
                break;
                return(RedirectToAction("SelectTemplate", new { ProjectID = o.ProjectID, EventID = o.EventID, o.ReportPeriodID }));

            case "update":
                if (o.EventID > 0)       //there must be eventID and user must be logged in.
                {
                    pes.Update(o);
                    if (Request.Files.Count != 0)
                    {
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            if (!String.IsNullOrEmpty(Request.Files[i].FileName))
                            {
                                string FileKey;
                                FileKey = Request.Files.GetKey(i);
                                string FileID;
                                FileID = FileKey.Replace("files_", "");

                                string dir = ConfigurationManager.AppSettings["PhysicalPath"];

                                //string MyString="";
                                //int FileLen;
                                //System.IO.Stream MyStream;

                                //FileLen = Request.Files[i].ContentLength;
                                //byte[] input = new byte[FileLen];

                                //// Initialize the stream.
                                //MyStream = Request.Files[i].InputStream;

                                //// Read the file into the byte array.
                                //MyStream.Read(input, 0, FileLen);

                                //// Copy the byte array into a string.
                                //for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
                                //    MyString = MyString + input[Loop1].ToString();
                                string fileOriginalName = Request.Files[i].FileName;
                                string ExtenSion        = Path.GetExtension(fileOriginalName);

                                if (FileID == "0")
                                {
                                    #region InsertNewDocument


                                    ProjectEventDocument myDoc = new ProjectEventDocument();
                                    string virtdir             = "files/" + o.ProjectID.ToString() + "/";//"files/A" + DateTime.Now.Year.ToString().Substring(2) + o.ProjectID.ToString() + "/";
                                    dir += virtdir.Replace("/", "\\");
                                    if (!(Directory.Exists(dir)))
                                    {
                                        Directory.CreateDirectory(dir);
                                    }
                                    string fileformat = o.EventID.ToString() + "_" + o.ProjectID.ToString() + "_";
                                    int    fi;
                                    //checking other file names to come up with new file number.
                                    for (fi = 1; System.IO.File.Exists(dir + fileformat + fi.ToString() + ExtenSion); fi++)
                                    {
                                    }



                                    Request.Files[i].SaveAs(dir + fileformat + fi.ToString() + ExtenSion);

                                    myDoc.Author               = session.CurrentUser.FirstName + " " + session.CurrentUser.LastName + " " + session.CurrentUser.MiddleName;
                                    myDoc.CreatedDate          = DateTime.Now;
                                    myDoc.fileextension        = ExtenSion.Replace(".", ""); //needs "doc"
                                    myDoc.FileName             = fileformat + fi.ToString() + ExtenSion;
                                    myDoc.PhysicalAbsolutePath = dir + fileformat + fi.ToString() + ExtenSion;
                                    myDoc.PhysicalVirtualPath  = "\\" + virtdir.Replace("/", "\\") + fileformat + fi.ToString() + ExtenSion;
                                    myDoc.UpdatedDate          = myDoc.CreatedDate;
                                    myDoc.WebVirtualPath       = "/" + virtdir + fileformat + fi.ToString() + ExtenSion;
                                    pes.InsertDocument(myDoc, o.EventID);
                                    #endregion
                                }
                                else
                                {
                                    #region Update Document      //with different file extension it won't work.
                                    //get object by fileid
                                    //update object

                                    ProjectEventDocument myDoc = pes.GetProjectEventDocument(int.Parse(FileID));
                                    //System.IO.File.WriteAllText(dir + myDoc.PhysicalVirtualPath, MyString);

                                    if (myDoc.fileextension == ExtenSion.Replace(".", ""))      //if extensions are the same then Update allowed.
                                    {
                                        Request.Files[i].SaveAs(dir + myDoc.PhysicalVirtualPath);
                                        myDoc.Author      = session.CurrentUser.FirstName + " " + session.CurrentUser.LastName + " " + session.CurrentUser.MiddleName;;
                                        myDoc.UpdatedDate = DateTime.Now;
                                        pes.UpdateProjectEventDocument(myDoc);
                                    }
                                    //get object by fileid
                                    //update object
                                    #endregion
                                }
                            }
                        }
                    }
                }
                break;

            case "delete":
                string mydir = Request.MapPath("~");
                foreach (var item in o.ProjectEventDocs)
                {
                    try
                    {
                        System.IO.File.Delete(mydir + item.ProjectEventDocument.PhysicalVirtualPath);
                    }
                    catch (Exception ex) { }
                    DeleteMyFile(item.ProjectEventDocumentID, o.EventID);
                }
                pes.Delete(o.EventID);
                break;
            }

            return(RedirectToAction("Index", new { id = o.EventID }));
        }