예제 #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)
        {
            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 == "InspectionDetail")
            {
                var inspection = await _prepareService.GetInspection(id);

                // Retrieve all the action of the publication
                var actionSorted = inspection.Publication.PublishedActions.Distinct()
                                   .OrderBy(a => a.WBSParts, new WBSPartsComparer())
                                   .ToList();

                var inspectionSteps = GetInspectionStepAndChilds(inspection, actionSorted);
                var model           = InspectionMapper.ToInspectionViewModel(inspection, inspectionSteps, new List <Anomaly>());
                var dataSource      = model.Steps.ToList();
                inspectionStepViewModelData = dataSource;
                var currentDate = DateTime.Today.ToShortDateString().Replace("/", "-");
                obj.ServerExcelQueryCellInfo = QueryCellInfo;
                exp.Export(obj, dataSource, LocalizedStrings.GetString("Inspection") + " " + process + " " + currentDate + ".xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron");
            }
            if (gridId == "InspectionManage")
            {
                var inspections = await _prepareService.GetInspections();

                var model = await InspectionMapper.ToInspectionManageViewModel(inspections);

                var dataSource = model.Inspections.ToList();
                inspectionViewModelData = dataSource;
                var currentDate = DateTime.Today.ToShortDateString().Replace("/", "-");
                obj.ServerExcelQueryCellInfo = QueryCellInfo;
                exp.Export(obj, dataSource, LocalizedStrings.GetString("Inspection") + " " + currentDate + ".xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron");
            }
            if (gridId == "Anomaly")
            {
                var inspection = await _prepareService.GetInspection(id);

                var anomalies = await _prepareService.GetAnomalies(id);

                var model       = InspectionMapper.ToInspectionViewModel(inspection, new List <InspectionStep>(), anomalies);
                var dataSource  = model.Anomalies.ToList();
                var currentDate = DateTime.Today.ToShortDateString().Replace("/", "-");
                exp.Export(obj, dataSource, LocalizedStrings.GetString("Anomaly") + " " + process + " " + currentDate + ".xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron");
            }
        }
예제 #3
0
        public async Task <ActionResult> Detail(int id, bool partial = false)
        {
            ////check if user can create audit for inspection
            //var auditController = DependencyResolver.Current.GetService<AuditController>();
            //var allowCreateAudit = await auditController.allowUserCreateAudit();
            //ViewBag.AllowCreateAudit = allowCreateAudit;
            var inspection = await _prepareService.GetInspection(id);

            // Retrieve all the action of the publication
            var actionSorted = inspection.Publication.PublishedActions.Distinct()
                               .OrderBy(a => a.WBSParts, new WBSPartsComparer())
                               .ToList();

            var inspectionSteps = GetInspectionStepAndChilds(inspection, actionSorted);
            var anomalies       = await _prepareService.GetAnomalies(id);

            var model = InspectionMapper.ToInspectionViewModel(inspection, inspectionSteps, anomalies.ToList());

            if (partial)
            {
                return(PartialView(model));
            }
            return(View(model));
        }
예제 #4
0
        public async Task ExportToExcelAnomaly(string gridColumns, string gridHeaders, int id, string process)
        {
            try
            {
                List <Column>        columnsInfos    = (List <Column>)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(List <Column>), gridColumns);
                JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                var columnHeaders = JsonConvert.DeserializeObject <List <GridHeader> >(gridHeaders);

                var inspection = await _prepareService.GetInspection(id);

                var anomalies = await _prepareService.GetAnomalies(id);

                var model      = InspectionMapper.ToInspectionViewModel(inspection, new List <InspectionStep>(), anomalies);
                var dataSource = model.Anomalies.ToList();

                var fileName = LocalizedStrings.GetString("Anomaly") + " " + process + " " + ".xlsx";

                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    IApplication application = excelEngine.Excel;
                    //application.DefaultVersion = ExcelVersion.Excel2013;
                    IWorkbook  workbook  = application.Workbooks.Create(1);
                    IWorksheet worksheet = workbook.Worksheets[0];

                    int rowCounter     = 1;
                    int columnCounter  = 1;
                    var headers        = columnsInfos.Where(_ => _.Visible);
                    var shapes         = new List <IPictureShape>();
                    var rangeLocations = new List <RangeLocation>();
                    var shapeCounter   = 1;

                    // Adding headers
                    foreach (var header in headers)
                    {
                        worksheet.Range[rowCounter, columnCounter].VerticalAlignment   = ExcelVAlign.VAlignCenter;
                        worksheet.Range[rowCounter, columnCounter].HorizontalAlignment = ExcelHAlign.HAlignCenter;
                        worksheet.Range[rowCounter, columnCounter++].Text = columnHeaders.FirstOrDefault(h => h.Field == header.Field).HeaderText;
                    }

                    foreach (var data in dataSource)
                    {
                        rowCounter++;
                        columnCounter = 1;
                        foreach (var header in headers)
                        {
                            if (header.Field != "Photo" && (data.GetType().GetProperty(header.Field) == null || (data.GetType().GetProperty(header.Field).GetValue(data) == null && header.Field != "Description")))
                            {
                                continue;
                            }
                            var range = worksheet.Range[rowCounter, columnCounter++];
                            switch (header.Field)
                            {
                            case "Photo":
                                if (data.HasPhoto)
                                {
                                    range.VerticalAlignment = ExcelVAlign.VAlignTop;
                                    try
                                    {
                                        range.Value = "";
                                        var memoryStream = new MemoryStream(Convert.FromBase64String(data.RawPhoto));

                                        var imageFile = Image.FromStream(memoryStream);

                                        int    maxPixelSize    = 500;
                                        double maxWidthPoints  = maxPixelSize.PixelsToWidthPoints();
                                        double maxHeightPoints = maxPixelSize.PixelsToHeightPoints();
                                        var    widthScale      = Convert.ToInt32(maxWidthPoints / imageFile.Width.PixelsToWidthPoints() * 100.0);
                                        var    heightScale     = Convert.ToInt32(maxHeightPoints / imageFile.Height.PixelsToHeightPoints() * 100.0);

                                        var imageScaleInPercent = Math.Min(widthScale, heightScale);
                                        range.RowHeight   = (imageFile.Height >= imageFile.Width ? maxHeightPoints : (imageFile.Height * imageScaleInPercent / 100.0).PixelsToHeightPoints());
                                        range.ColumnWidth = maxWidthPoints;
                                        var shape = range.Worksheet.Pictures.AddPicture(range.Row, range.Column, imageFile, imageScaleInPercent, imageScaleInPercent);
                                        shape.Name = "shape" + shapeCounter;
                                        shapes.Add(shape);
                                        rangeLocations.Add(new RangeLocation {
                                            ShapeName = shape.Name, Row = range.Row, Column = range.Column
                                        });
                                        shapeCounter++;
                                    }
                                    catch (Exception ex)
                                    {
                                        _traceManager.TraceError(ex, $"Error on {nameof(ExportToExcelAnomaly)}");
                                        range.Value = "";
                                    }
                                }
                                break;

                            default:
                                range.Value = data.GetType().GetProperty(header.Field).GetValue(data) == null ? "" : data.GetType().GetProperty(header.Field).GetValue(data).ToString();
                                break;
                            }
                        }
                    }

                    foreach (var shape in shapes)
                    {
                        //Rearrange cell height
                        var range = worksheet.Range[rangeLocations.FirstOrDefault(r => r.ShapeName == shape.Name).Row, rangeLocations.FirstOrDefault(r => r.ShapeName == shape.Name).Column];
                        range.RowHeight = shape.Height.PixelsToHeightPoints();
                    }

                    // Saving the workbook to disk in XLSX format
                    workbook.SaveAs(fileName,
                                    ExcelSaveType.SaveAsXLS,
                                    System.Web.HttpContext.Current.Response,
                                    ExcelDownloadType.Open);
                }
            }
            catch (Exception e)
            {
                _traceManager.TraceError(e, $"Error on {nameof(ExportToExcelAnomaly)}");
            }
        }
예제 #5
0
        public async Task ExportToExcelInspectionDetail(string gridColumns, string gridHeaders, int id, string process)
        {
            try
            {
                List <Column>        columnsInfos    = (List <Column>)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(List <Column>), gridColumns);
                JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                var columnHeaders = JsonConvert.DeserializeObject <List <GridHeader> >(gridHeaders);

                var inspection = await _prepareService.GetInspection(id);

                // Retrieve all the action of the publication
                var actionSorted = inspection.Publication.PublishedActions.Distinct()
                                   .OrderBy(a => a.WBSParts, new WBSPartsComparer())
                                   .ToList();
                var inspectionSteps = GetInspectionStepAndChilds(inspection, actionSorted);
                var model           = InspectionMapper.ToInspectionViewModel(inspection, inspectionSteps, new List <Anomaly>());
                var dataSource      = model.Steps.ToList();

                var fileName = LocalizedStrings.GetString("Inspection") + " " + process + " " + ".xlsx";

                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    IApplication application = excelEngine.Excel;
                    //application.DefaultVersion = ExcelVersion.Excel2013;
                    IWorkbook  workbook  = application.Workbooks.Create(1);
                    IWorksheet worksheet = workbook.Worksheets[0];

                    int rowCounter     = 1;
                    int columnCounter  = 1;
                    var headers        = columnsInfos.Where(_ => _.Visible);
                    var shapes         = new List <IPictureShape>();
                    var rangeLocations = new List <RangeLocation>();
                    var shapeCounter   = 1;

                    // Adding headers
                    foreach (var header in headers)
                    {
                        worksheet.Range[rowCounter, columnCounter].VerticalAlignment   = ExcelVAlign.VAlignCenter;
                        worksheet.Range[rowCounter, columnCounter].HorizontalAlignment = ExcelHAlign.HAlignCenter;
                        worksheet.Range[rowCounter, columnCounter++].Text = columnHeaders.FirstOrDefault(h => h.Field == header.Field).HeaderText;
                    }

                    foreach (var data in dataSource)
                    {
                        rowCounter++;
                        columnCounter = 1;
                        foreach (var header in headers)
                        {
                            if (header.Field != "Photo" && (data.GetType().GetProperty(header.Field) == null || (data.GetType().GetProperty(header.Field).GetValue(data) == null && header.Field != "Description")))
                            {
                                continue;
                            }
                            var range = worksheet.Range[rowCounter, columnCounter++];
                            switch (header.Field)
                            {
                            case "ActionLabel":
                                if (data.Level > 0)
                                {
                                    range.IndentLevel = 2;
                                }
                                range.Value = data.GetType().GetProperty(header.Field).GetValue(data).ToString();
                                break;

                            case "IsOK":
                                if (data.IsOK == true)
                                {
                                    range.Value                = "OK";
                                    range.CellStyle.Color      = Color.Green;
                                    range.CellStyle.Font.Color = ExcelKnownColors.White;
                                }
                                else
                                {
                                    range.Value                = "NOK";
                                    range.CellStyle.Color      = Color.Red;
                                    range.CellStyle.Font.Color = ExcelKnownColors.White;
                                }
                                break;

                            case "Photo":
                                if (data.HasThumbnail)
                                {
                                    range.VerticalAlignment = ExcelVAlign.VAlignTop;
                                    var fileServer = ConfigurationManager.AppSettings["FileServerUri"];
                                    var filePath   = data.ThumbnailHash + data.ThumbnailExt;
                                    try
                                    {
                                        range.Value = "";
                                        var downloadPath = Path.Combine(fileServer, "GetFile", filePath);
                                        downloadPath = downloadPath.Replace("\\", "/");

                                        var webClient    = new WebClient();
                                        var imageBytes   = webClient.DownloadData(downloadPath);
                                        var memoryStream = new MemoryStream(imageBytes);

                                        var imageFile = Image.FromStream(memoryStream);

                                        int    maxPixelSize    = 500;
                                        double maxWidthPoints  = maxPixelSize.PixelsToWidthPoints();
                                        double maxHeightPoints = maxPixelSize.PixelsToHeightPoints();
                                        var    widthScale      = Convert.ToInt32(maxWidthPoints / imageFile.Width.PixelsToWidthPoints() * 100.0);
                                        var    heightScale     = Convert.ToInt32(maxHeightPoints / imageFile.Height.PixelsToHeightPoints() * 100.0);

                                        var imageScaleInPercent = Math.Min(widthScale, heightScale);

                                        range.RowHeight   = (imageFile.Height >= imageFile.Width ? maxHeightPoints : (imageFile.Height * imageScaleInPercent / 100.0).PixelsToHeightPoints()) + 61;
                                        range.ColumnWidth = range.ColumnWidth >= maxWidthPoints + 22.86 ? range.ColumnWidth : maxWidthPoints + 22.86;
                                        var shape = range.Worksheet.Pictures.AddPicture(range.Row, range.Column, imageFile, imageScaleInPercent, imageScaleInPercent);
                                        shape.Name = "shape" + shapeCounter;
                                        shapes.Add(shape);
                                        rangeLocations.Add(new RangeLocation {
                                            ShapeName = shape.Name, Row = range.Row, Column = range.Column
                                        });
                                        shapeCounter++;
                                    }
                                    catch (Exception ex)
                                    {
                                        _traceManager.TraceError(ex, $"Error on {nameof(ExportToExcelInspectionDetail)}");
                                        range.Value = "";
                                    }
                                }
                                break;

                            default:
                                range.Value = data.GetType().GetProperty(header.Field).GetValue(data) == null ? "" : data.GetType().GetProperty(header.Field).GetValue(data).ToString();
                                break;
                            }
                        }
                    }

                    foreach (var shape in shapes)
                    {
                        //Rearrange cell height
                        var range = worksheet.Range[rangeLocations.FirstOrDefault(r => r.ShapeName == shape.Name).Row, rangeLocations.FirstOrDefault(r => r.ShapeName == shape.Name).Column];
                        range.RowHeight = shape.Height.PixelsToHeightPoints();
                    }

                    // Saving the workbook to disk in XLSX format
                    workbook.SaveAs(fileName,
                                    ExcelSaveType.SaveAsXLS,
                                    System.Web.HttpContext.Current.Response,
                                    ExcelDownloadType.Open);
                }
            }
            catch (Exception e)
            {
                _traceManager.TraceError(e, $"Error on {nameof(ExportToExcelInspectionDetail)}");
            }
        }