public ActionResult ProjectEventPaymentGenerator(int RepperID, int BudgetID)
        {
            if (RepperID > 0 && BudgetID > 0)
            {
                ReportPeriodListR repper = GetRepper(RepperID, BudgetID);

                ProjectEventService pes = new ProjectEventService();
                ProjectEvent        o   = new ProjectEvent();
                o.CompletedDate    = repper.PaymentDate;
                o.EventDescription = "Payment Request";
                o.ProjectID        = BudgetID;
                //o.EventType = GetPaymentEventType();
                o.EventTypeID    = GetPaymentEventType().EventTypeID;
                o.ReportPeriodID = RepperID;
                o.SSPOrGrantee   = true;
                o.EventStatus    = 0;
                pes.Insert(o);

                EventTemplate   eh = new EventTemplate();
                TemplateService ts = new TemplateService();

                TemplateDocument td1 = null;
                td1 = ts.GetTemplateDocument("Запрос на Оплату");
                if (td1 != null)
                {
                    eh.CreateFromTemplate(td1.TemplateDocsID, BudgetID, o.EventID, RepperID);
                }
            }

            return(RedirectToAction("Reppers", new { id = BudgetID }));
        }
        public ActionResult ProjectEventAcceptanceMemoGenerator(int RepperID, int BudgetID)
        {
            if (RepperID > 0 && BudgetID > 0)
            {
                ReportPeriodListR repper = GetRepper(RepperID, BudgetID);

                //Create Event.
                ProjectEventService pes = new ProjectEventService();
                ProjectEvent        o   = new ProjectEvent();
                o.CompletedDate    = repper.PaymentDate;
                o.EventDescription = "Letter to Grantee";
                o.ProjectID        = BudgetID;
                //o.EventType = GetPaymentEventType();
                o.EventTypeID    = GetPaymentEventType().EventTypeID;
                o.ReportPeriodID = RepperID;
                o.SSPOrGrantee   = true;
                o.EventStatus    = 0;

                pes.Insert(o);

                EventTemplate   eh = new EventTemplate();
                TemplateService ts = new TemplateService();

                //Generate Document.
                TemplateDocument td1 = null;
                td1 = ts.GetTemplateDocument("письмо о принятии отчета");
                if (td1 != null)
                {
                    eh.CreateFromTemplate(td1.TemplateDocsID, BudgetID, o.EventID, RepperID);
                }
            }

            return(RedirectToAction("Index", "Events", null));

            // return RedirectToAction("Reppers", new { id = BudgetID });
        }
        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 }));
        }