예제 #1
0
        public async Task <ActionResult> Detail(int?id, bool partial = false)
        {
            UserModel user   = JwtTokenProvider.GetUserModel(Request.Cookies["token"].Value);
            var       audits = await _prepareService.GetAudits(id);

            if (!audits.Any())
            {
                return(HttpNotFound());
            }
            var audit = audits.First();

            if (!user.Roles.Any(r => (new[] { KnownRoles.Administrator, KnownRoles.Supervisor, KnownRoles.Documentalist }.Contains(r))) && audit.AuditorId != user.UserId && !audit.Inspection.InspectionSteps.Any(s => s.InspectorId == user.UserId))
            {
                return(RedirectToAction("Index", new { userId = user.UserId, partial = partial }));
            }
            var inspection = await _prepareService.GetInspection(audit.InspectionId);

            var inspectionSteps = InspectionController.GetInspectionStepAndChilds(inspection, inspection.Publication.PublishedActions.Distinct()
                                                                                  .OrderBy(a => a.WBSParts, new WBSPartsComparer()).ToList());
            var anomalies = await _prepareService.GetAnomalies(audit.InspectionId);

            var model = AuditMapper.ToAuditViewModel(audits.FirstOrDefault());

            model.Inspection = InspectionMapper.ToInspectionViewModel(inspection, inspectionSteps, anomalies.ToList());
            if (partial)
            {
                return(PartialView(model));
            }
            return(View(model));
        }
예제 #2
0
        public async Task ExportToExcel(string GridModel, string gridId, int id, string process, int?userId)
        {
            ExcelExport    exp = new ExcelExport();
            GridProperties obj = (GridProperties)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(GridProperties), GridModel);

            //Clear if there are any filter columns
            //syncfusion bug in exporting while in filter mode
            obj.FilterSettings.FilteredColumns.Clear();
            grid  = gridId;
            count = 0;

            if (gridId == "Audits")
            {
                var audits = await _prepareService.GetAudits();

                if (userId.HasValue)
                {
                    audits = audits.Where(a => a.AuditorId == userId.Value || a.Inspection.InspectionSteps.Any(s => s.InspectorId == userId.Value));
                }
                var auditManage = AuditMapper.ToAuditManageViewModel(audits);
                //var dataSource = auditManage.Audits.ToList();
                //auditViewModel = dataSource;
                //var currentDate = DateTime.Today.ToShortDateString().Replace("/", "-");
                //obj.ServerExcelQueryCellInfo = QueryCellInfo;
                //exp.Export(obj, dataSource, "Audits " + currentDate + ".xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron");

                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    // Set the default application version as Excel 2016.
                    excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016;


                    //var group1 = auditManage.Audits.ToList();
                    var allAudits = auditManage.Audits.ToList();
                    int i         = 0;
                    //Create a workbook with a worksheet.
                    IWorkbook workbook = excelEngine.Excel.Workbooks.Create(audits.Select(a => a.Survey).Distinct().Count());

                    foreach (var survey in audits.Select(a => a.Survey).Distinct())
                    {
                        IWorksheet worksheet = workbook.Worksheets[i];
                        DataTable  tbl       = new DataTable();
                        //fill columns
                        tbl.Columns.Add(LocalizedStrings.GetString("AuditDate"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("Schedule"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("Team"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("Auditor"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("Auditee"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("AuditStatus"), typeof(string));
                        tbl.Columns.Add(LocalizedStrings.GetString("StandardAuditee"), typeof(string));
                        foreach (var surveyItem in survey.SurveyItems)
                        {
                            tbl.Columns.Add(surveyItem.Query, typeof(int));
                        }
                        tbl.Columns.Add("Score", typeof(int));
                        tbl.Columns.Add(LocalizedStrings.GetString("Comment"), typeof(string));
                        //fill rows
                        foreach (var audit in auditManage.Audits.Where(a => a.SurveyId == survey.Id))
                        {
                            DataRow dr = null;
                            dr = tbl.NewRow();
                            dr[LocalizedStrings.GetString("AuditDate")]       = audit.EndDate.Value.ToShortDateString();
                            dr[LocalizedStrings.GetString("Schedule")]        = audit.EndDate.Value.ToShortTimeString().Replace(":", "H");
                            dr[LocalizedStrings.GetString("Team")]            = string.Join(",", audit.AuditorTeams);
                            dr[LocalizedStrings.GetString("Auditor")]         = audit.AuditorName;
                            dr[LocalizedStrings.GetString("Auditee")]         = audit.AuditeeName;
                            dr[LocalizedStrings.GetString("AuditStatus")]     = audits.Select(a => a.Auditor).FirstOrDefault(u => u.UserId == audit.AuditorId).Tenured.HasValue ? audits.Select(a => a.Auditor).FirstOrDefault(u => u.UserId == audit.AuditorId).Tenured.Value ? "Titulaire" : "Intérimaire" : "";
                            dr[LocalizedStrings.GetString("StandardAuditee")] = audit.ProcessName;
                            int scoreTemp = 0;
                            foreach (var surveyItem in survey.SurveyItems)
                            {
                                dr[surveyItem.Query] = audit.AuditItems.FirstOrDefault(item => item.Number == surveyItem.Number).IsOK.HasValue ? audit.AuditItems.FirstOrDefault(item => item.Number == surveyItem.Number).IsOK.Value ? 1 : 0 : -1;
                                if (audit.AuditItems.FirstOrDefault(item => item.Number == surveyItem.Number).IsOK.Value == true)
                                {
                                    scoreTemp++;
                                }
                            }
                            dr["Score"] = scoreTemp;
                            tbl.Rows.Add(dr);
                        }
                        worksheet.ImportDataTable(tbl, true, 2, 1);
                        worksheet.Name = survey.Name;
                        worksheet.UsedRange.WrapText = true;
                        worksheet.Columns[0].AutofitColumns();
                        worksheet.SetColumnWidth(1, 10);
                        IListObject table = worksheet.ListObjects.Create("tbl_" + survey.Name.Replace(" ", "_"), worksheet.UsedRange);
                        table.BuiltInTableStyle = TableBuiltInStyles.TableStyleLight9;
                        foreach (var surveyItem in survey.SurveyItems)
                        {
                            //Apply conditional formats for IsOK questionnaire items
                            IConditionalFormats condition  = worksheet.Columns[table.Columns.IndexOf(s => s.Name == surveyItem.Query)].ConditionalFormats;
                            IConditionalFormat  condition1 = condition.AddCondition();
                            condition1.FormatType   = ExcelCFType.CellValue;
                            condition1.Operator     = ExcelComparisonOperator.Equal;
                            condition1.FirstFormula = "1";
                            condition1.BackColor    = ExcelKnownColors.Green;
                            IConditionalFormat condition2 = condition.AddCondition();
                            condition2.FormatType   = ExcelCFType.CellValue;
                            condition2.Operator     = ExcelComparisonOperator.Equal;
                            condition2.FirstFormula = "0";
                            condition2.BackColor    = ExcelKnownColors.Red;
                            worksheet.Columns[table.Columns.IndexOf(s => s.Name == surveyItem.Query)].RowHeight = 80;
                            worksheet.AutofitColumn(table.Columns.IndexOf(s => s.Name == surveyItem.Query));
                        }
                        i++;
                    }
                    //worksheet.ImportData(group1, 2, 1, true);

                    var path = Server.MapPath("~/App_Data/" + LocalizedStrings.GetString("Audits") + ".xlsx");

                    workbook.SaveAs(path, HttpContext.ApplicationInstance.Response, ExcelDownloadType.PromptDialog, ExcelHttpContentType.Excel2010);
                    workbook.Close();
                    excelEngine.Dispose();
                }
            }
            if (gridId == "AuditItems")
            {
                var audits = await _prepareService.GetAudits(id);

                var audit      = AuditMapper.ToAuditViewModel(audits.FirstOrDefault());
                var dataSource = audit.AuditItems.ToList();
                auditItemViewModel = dataSource;
                var currentDate = DateTime.Today.ToShortDateString().Replace("/", "-");
                obj.ServerExcelQueryCellInfo = QueryCellInfo;
                exp.Export(obj, dataSource, LocalizedStrings.GetString("Audit") + " " + process + " " + currentDate + ".xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron");
            }
        }