示例#1
0
        private void GenerateReport()
        {
            try
            {
                var projName = SelectedProject.Code;
                var logoPath = System.IO.Directory.GetCurrentDirectory() + "\\logo.png";
                var path     = Path.GetDirectoryName(SelectedProject.Path);
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                //export images
                foreach (var item in SelectedProject.Units)
                {
                    if (item.Image != null)
                    {
                        ((System.Drawing.Image)item.Image).Save(path + "\\" + item.Code + ".png");                     //item.Image.ToImage().Save(path + "\\" + item.Code + ".png");
                    }
                }

                //initialize and detailed report
                var profiles    = new List <Profile>();
                var fillings    = new List <Domain.Models.Filling>();
                var accessories = new List <Accessory>();
                foreach (var item in SelectedProject.Units)
                {
                    if (item.Frame == null) //|| item.Frame.Length == 0)
                    {
                        continue;
                    }
                    byte[] frameByte = item.Frame;
                    if (frameByte == null || frameByte.Length == 0)
                    {
                        continue;
                    }

                    var formatter = new BinaryFormatter();
                    formatter.SurrogateSelector = PUtil.FrameworkSurrogateSelector;
                    var stream  = new MemoryStream(frameByte);
                    var pStream = new PStream(stream);
                    var frame   = (PVCFrame)pStream.ReadObjectTree(formatter);
                    stream.Close();

                    for (int i = 0; i < item.Quantity; i++)
                    {
                        profiles.AddRange(frame.Model.GetProfiles(true, true));
                        fillings.AddRange(frame.Model.GetFillings(true));
                        accessories.AddRange(frame.Model.GetAccessories());
                    }
                }

                var profileTables   = new List <PdfPTable>();
                var fillingTables   = new List <PdfPTable>();
                var accessoryTables = new List <PdfPTable>();
                var tables          = new List <PdfPTable>();

                if (profiles != null && profiles.Count > 0)
                {
                    profileTables = Reporter.ProfilesTable(profiles, includesPrice: true).ToList();
                }
                if (fillings != null && fillings.Count > 0)
                {
                    fillingTables = Reporter.FillingsTable(fillings, includesPrice: true).ToList();
                }
                if (accessories != null && fillings.Count > 0)
                {
                    accessoryTables = Reporter.AccessoriesTable(accessories, includesPrice: true).ToList();
                }

                if (profileTables != null && profileTables.Count > 0)
                {
                    tables.AddRange(profileTables);
                }
                if (fillingTables != null && fillingTables.Count > 0)
                {
                    tables.AddRange(fillingTables);
                }
                if (accessoryTables != null && accessoryTables.Count > 0)
                {
                    tables.AddRange(accessoryTables);
                }

                var fileName = path + "\\" + projName + "-Details.pdf";
                if (tables != null && tables.Count > 0)
                {
                    Reporter.ItemsReport(fileName, tables, projName, logoPath);
                }

                //total report
                tables = new List <PdfPTable>();
                if (profiles != null && profiles.Count > 0)
                {
                    profileTables = Reporter.ProfilesTable(profiles, includesPrice: true, onlyTotal: true).ToList();
                }
                if (fillings != null && fillings.Count > 0)
                {
                    fillingTables = Reporter.FillingsTable(fillings, includesPrice: true, onlyTotal: true).ToList();
                }
                if (accessories != null && fillings.Count > 0)
                {
                    accessoryTables = Reporter.AccessoriesTable(accessories, includesPrice: true, onlyTotal: true).ToList();
                }

                if (profileTables != null && profileTables.Count > 0)
                {
                    tables.AddRange(profileTables);
                }
                if (fillingTables != null && fillingTables.Count > 0)
                {
                    tables.AddRange(fillingTables);
                }
                if (accessoryTables != null && accessoryTables.Count > 0)
                {
                    tables.AddRange(accessoryTables);
                }

                fileName = path + "\\" + projName + "-Total.pdf";
                if (tables != null && tables.Count > 0)
                {
                    Reporter.ItemsReport(fileName, tables, projName, logoPath);
                }

                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
                {
                    FileName = path, UseShellExecute = true, Verb = "open"
                });
            }
            catch (IOException e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }