Example #1
0
        public static string SaveImgLocally(RATV3Entities db, string guid)
        {
            string path = "";

            try
            {
                //get file by GUID
                Models.File fileToRetrieve = db.Files.Where(x => (x.FileID.ToString() == guid)).FirstOrDefault();

                //if getting the file by ID did not work, we'll try to get it by its Title
                if (fileToRetrieve == null)
                {
                    fileToRetrieve = db.Files.Where(x => (x.FileNumber == guid)).FirstOrDefault();
                }
                if (fileToRetrieve != null)
                {
                    //file successfully retrieved
                    byte[] file = fileToRetrieve.ItemImage;
                    //generating the name under which we will save the file locally for the PDF Generation
                    string fileExtension   = fileToRetrieve.FileNumber.Split('.').Last();
                    string uniqueComponent = Guid.NewGuid().ToString();

                    string imgPath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/Temp/"),
                                                  uniqueComponent + "." + fileExtension).ToString();

                    using (var fileStream = new FileStream(imgPath, FileMode.Create, FileAccess.Write))
                    {
                        fileStream.Write(fileToRetrieve.ItemImage, 0, fileToRetrieve.ItemImage.Length);
                    }

                    path = imgPath;
                }
            }
            catch (Exception ex)
            {
                //could not save img locally
                //error handling
            }


            return(path);
        }
Example #2
0
        public static byte[] GenerateExcelReport(int ProjectID, int month, int year, RATV3Entities db)
        {
            byte[]  result  = new byte[0];
            Project project = db.Projects.Where(x => x.ProjectID == ProjectID).FirstOrDefault();

            if (project != null)
            {
                #region GenerateDocument
                MemoryStream memoryStream = new MemoryStream();
                using (SpreadsheetDocument document = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook))
                {
                    var workbookpart = document.AddWorkbookPart();
                    workbookpart.Workbook = new Workbook();
                    //add Style
                    WorkbookStylesPart stylePart = workbookpart.AddNewPart <WorkbookStylesPart>();
                    stylePart.Stylesheet = GenerateStylesheet();
                    stylePart.Stylesheet.Save();

                    //var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();


                    var sheets = document.WorkbookPart.Workbook.
                                 AppendChild <Sheets>(new Sheets());


                    CreateProjectSheet(document, workbookpart, sheets, ProjectID, month, year, db);



                    //save data
                    //worksheetPart.Worksheet.Save();
                    document.Close();
                }

                result = memoryStream.ToArray();
                #endregion
            }
            return(result);
        }
Example #3
0
        private static Sheet CreateProjectSheet(SpreadsheetDocument document,
                                                WorkbookPart workbookpart,
                                                Sheets sheets,
                                                int ProjectID,
                                                int month,
                                                int year,
                                                RATV3Entities db,
                                                uint sheetID = 1)
        {
            #region GetData
            //getting the department
            Project    project    = db.Projects.Where(x => x.ProjectID == ProjectID).FirstOrDefault();
            Department department = db.Departments.Where(x => x.DepartmentID == project.DepartmentID).FirstOrDefault();

            //setting the date interval for our Sprints
            DateTime firstDayOfMonth = new DateTime(year, month, 1);
            DateTime lastDayOfMonth  = firstDayOfMonth.AddMonths(1).AddTicks(-1);

            while (firstDayOfMonth.DayOfWeek != DayOfWeek.Monday)
            {
                firstDayOfMonth = firstDayOfMonth.AddDays(-1);
            }
            while (lastDayOfMonth.DayOfWeek != DayOfWeek.Sunday)
            {
                lastDayOfMonth = lastDayOfMonth.AddDays(1);
            }

            //GETTING SPRINTS + SPRINT IDS
            List <Sprint> sprints = db.Sprints.Where(x => x.ProjectID == ProjectID &&
                                                     (DbFunctions.TruncateTime(x.StartDate) >= firstDayOfMonth.Date &&
                                                      DbFunctions.TruncateTime(x.EndDate) <= lastDayOfMonth.Date))
                                    .OrderBy(x => x.StartDate).ToList();
            int[] sprintIds = new int[] { 0 };
            if (sprints != null && sprints.Count > 0)
            {
                sprintIds = sprints.Select(x => x.SprintID).ToArray();
            }

            //Getting Employees and Tasks
            List <Employee> employees = db.Employees.Where(x => x.DepartmentID == department.DepartmentID).ToList();
            List <Task>     tasks     = db.Tasks.Where(x => x.SprintID.HasValue && sprintIds.Contains(x.SprintID.Value)).ToList();
            #endregion

            WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart <WorksheetPart>();

            string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart);
            var    sheet          = new Sheet()
            {
                Id      = relationshipId,
                SheetId = sheetID,
                Name    = project.Title
            };
            sheets.AppendChild(sheet);

            var sheetData = new SheetData();



            newWorksheetPart.Worksheet = new Worksheet(sheetData);

            //create header rows
            DateTime monthDate = new DateTime(year, month, 1);

            List <string> MergeableRows = new List <string>();
            Row           projectRow    = CreateProjectRow(firstDayOfMonth, lastDayOfMonth, project, MergeableRows, monthDate, 1);
            Row           sprintRow     = CreateSprintRow(firstDayOfMonth, lastDayOfMonth, sprints, MergeableRows, 2);
            Row           headerRow     = CreateDatesRow(firstDayOfMonth, lastDayOfMonth, 3);

            sheetData.AppendChild(projectRow);
            sheetData.AppendChild(sprintRow);
            sheetData.AppendChild(headerRow);


            //Merging cells
            MergeCells mergeCells = new MergeCells();
            for (int i = 0; i < MergeableRows.Count; i += 2)
            {
                mergeCells.Append(new MergeCell()
                {
                    Reference = new StringValue(MergeableRows[i] + ":" + MergeableRows[i + 1])
                });
            }

            //creating content rows
            int colIndex = 4;
            foreach (Employee emp in employees)
            {
                //creating row for this employee
                List <Task> empTasks = tasks.Where(x => x.EmployeeID == emp.EmployeeID).ToList();
                Row         row      = CreateEmployeeRow(firstDayOfMonth, lastDayOfMonth, colIndex, emp, empTasks);
                sheetData.AppendChild(row);

                colIndex++;
            }


            //set col width
            Columns columns = new Columns();
            columns.Append(new Column()
            {
                Min = 1, Max = 1, Width = 20, CustomWidth = true
            });
            columns.Append(new Column()
            {
                Min = 2, Max = 100, Width = 12, CustomWidth = true
            });
            newWorksheetPart.Worksheet.Append(columns);

            //merging cells
            newWorksheetPart.Worksheet.InsertAfter(mergeCells, newWorksheetPart.Worksheet.Elements <SheetData>().First());

            newWorksheetPart.Worksheet.Save();
            return(sheet);
        }
Example #4
0
        /// <summary>
        ///  //we'll get the email addresses and notify the attendants the attendants
        /// </summary>
        public void SendMeetingRequest(RATV3Entities db,
                                       Event ev,
                                       List <Employee> attendantEmployees,
                                       Employee creator,
                                       ControllerContext ControllerContext,
                                       int update = 0)
        {
            try
            {
                //getting the reviewed employee
                Attendant at       = ev.Attendants.FirstOrDefault();
                Employee  reviewed = db.Employees.Where(x => x.EmployeeID == at.EmployeeID).FirstOrDefault();
                reviewed.SkillLevelsList = reviewed.SkillLevels.ToList();

                //getting message content
                EventTypeInfo currentEventType = Const.EventTypesinfo.Where(x => x.EventType == ev.EventType).FirstOrDefault();
                string        embededHtml      = "<html><head></head><body>" + "<p>Test</p>" + "<br/><br/>" + "<p>Test Embeded</p>" + "<br/>" + "<p>Test Ending</p>" + "<br/></body></html>";

                if (currentEventType != null && currentEventType.EventType == "Performance Review")
                {
                    string generatednotificationHTML = ViewRenderer.RenderView("~/Views/Notifications/PerformanceReview.cshtml", reviewed,
                                                                               ControllerContext);

                    if (!String.IsNullOrEmpty(generatednotificationHTML))
                    {
                        embededHtml = generatednotificationHTML;
                    }
                }



                //preping the email message
                MailMessage email = new MailMessage();
                email.From = new MailAddress(creator.Email, creator.FirstName + ' ' + creator.LastName);
                //adding recipients
                foreach (Employee attendant in attendantEmployees)
                {
                    email.To.Add(new MailAddress(attendant.Email, attendant.FirstName + ' ' + attendant.LastName));
                }

                email.IsBodyHtml = true;
                email.Subject    = ev.EventType;


                //preparing email content
                //"text/html" - this view will have all the content
                System.Net.Mime.ContentType htmlMimeContent = new System.Net.Mime.ContentType("text/html");
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(embededHtml, htmlMimeContent);
                htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
                if (ev.EventType == "Performance Review" && ev.Attendants != null && ev.Attendants.Count > 0 && reviewed != null)
                {
                    //we'll attach the pdf to the email

                    Stream pdfDocument = GenerateUserCV(reviewed, ControllerContext);
                    if (pdfDocument != null)
                    {
                        LinkedResource resource = new LinkedResource(pdfDocument);
                        resource.ContentType.Name = reviewed.FirstName + " " + reviewed.LastName + " " + "Report.pdf";
                        htmlView.LinkedResources.Add(resource);
                    }
                }
                else if (ev.EventType == "Department Monthly Meeting" && reviewed.DepartmentID.HasValue)
                {
                    //we'll attach the excel report to the email

                    byte[] array = ExcelReportGenerator.GenerateExcelReportForDepartment(reviewed.DepartmentID.Value,
                                                                                         ev.StartTime.Month, ev.StartTime.Year, db);
                    Stream excelDocument = new MemoryStream(array);
                    if (excelDocument != null)
                    {
                        LinkedResource resource = new LinkedResource(excelDocument);
                        resource.ContentType.Name = "Department Report.xlsx";
                        htmlView.LinkedResources.Add(resource);
                    }
                }



                //preparing calendar meeting view
                DateTime endTime;
                if (ev.EndTime.HasValue)
                {
                    endTime = ev.EndTime.Value;
                }
                else
                {
                    endTime = ev.StartTime;
                }

                //this is the guid of the meeting request
                Guid requestGUID;

                if (ev.IcsGuid.HasValue)
                {
                    requestGUID = ev.IcsGuid.Value;
                }
                else
                {
                    requestGUID = Guid.NewGuid();
                }
                AlternateView avCal = CreateICSView(email, ev.StartTime, endTime, ev, requestGUID, update);
                //email.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
                email.AlternateViews.Add(htmlView);
                email.AlternateViews.Add(avCal);

                //finally we send the mail
                client.Send(email);

                if (!ev.IcsGuid.HasValue)
                {
                    ev.IcsGuid         = requestGUID;
                    db.Entry(ev).State = EntityState.Modified;
                }
                if (update > 0)
                {
                    //we want to be able to store the update number too

                    //ev.UpdateNo = update;
                    db.Entry(ev).State = EntityState.Modified;
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                //handle Exception
            }
        }
Example #5
0
        /// <summary>
        ///  //we'll get the email addresses and notify the attendants about the event being canceled
        /// </summary>
        public void CancelMeetingRequest(RATV3Entities db,
                                         Event ev,
                                         List <Employee> attendantEmployees,
                                         Employee creator,
                                         ControllerContext ControllerContext)
        {
            //getting the reviewed employee
            Attendant at       = ev.Attendants.FirstOrDefault();
            Employee  reviewed = db.Employees.Where(x => x.EmployeeID == at.EmployeeID).FirstOrDefault();

            reviewed.SkillLevelsList = reviewed.SkillLevels.ToList();


            string embededHtml = ViewRenderer.RenderView("~/Views/Notifications/CanceledEvent.cshtml", reviewed,
                                                         ControllerContext);

            if (String.IsNullOrEmpty(embededHtml))
            {
                embededHtml = " This Event has been canceled";
            }
            //preping the email message
            var email = new MailMessage();

            email.From = new MailAddress(creator.Email, creator.FirstName + ' ' + creator.LastName);
            //adding recipients
            foreach (Employee attendant in attendantEmployees)
            {
                email.To.Add(new MailAddress(attendant.Email, attendant.FirstName + ' ' + attendant.LastName));
            }

            email.IsBodyHtml = true;
            email.Subject    = ev.EventType;


            //preparing email content
            //"text/html" - this view will have all the content
            System.Net.Mime.ContentType htmlMimeContent = new System.Net.Mime.ContentType("text/html");
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(embededHtml, htmlMimeContent);

            htmlView.ContentType.CharSet = Encoding.UTF8.WebName;

            //preparing calendar meeting view
            DateTime endTime;

            if (ev.EndTime.HasValue)
            {
                endTime = ev.EndTime.Value;
            }
            else
            {
                endTime = ev.StartTime;
            }

            //this is the guid of the meeting request
            Guid requestGUID;

            if (ev.IcsGuid.HasValue)
            {
                //if we have
                requestGUID = ev.IcsGuid.Value;
                AlternateView avCal = CreateCancelationICSView(email, ev.StartTime, endTime, ev, requestGUID);
                //email.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
                email.AlternateViews.Add(htmlView);
                email.AlternateViews.Add(avCal);

                //finally we send the mail
                client.Send(email);
            }
        }