コード例 #1
0
        public MemoryStream DownloadOrderReporting(List <Order> orders, string from, string to, int[] offices, int[] products)
        {
            var pdfData   = new MemoryStream();
            var document  = new Document(PageSize.A4, 35, 35, 25, 50);
            var pdfWriter = PdfWriter.GetInstance(document, pdfData);

            pdfWriter.ViewerPreferences = PdfWriter.PageLayoutOneColumn;

            // Our custom Header and Footer is done using Event Handler
            var pageEventHandler = new TwoColumnHeaderFooter();

            pdfWriter.PageEvent = pageEventHandler;

            // Define the page header
            //pageEventHandler.Title = title;
            //pageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10, Font.BOLD);
            //pageEventHandler.HeaderLeft = "Group";
            //pageEventHandler.HeaderRight = "1";
            var      filePath    = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "Content\\Fonts", "times.ttf");
            BaseFont baseFont    = BaseFont.CreateFont("C:\\Windows\\Fonts\\times.ttf", BaseFont.IDENTITY_H, true);
            var      fontBold    = new Font(baseFont, 11, Font.BOLD, BaseColor.BLACK);
            Font     defaultFont = new Font(baseFont, 12, Font.NORMAL, BaseColor.BLACK);

            document.Open();

            //Add header
            //var myCompany = new Chunk("VPDD CTY TNHH TM VC TRUNG THÀNH", defaultFont);
            //document.Add(myCompany);
            //document.Add(new Phrase("\n"));
            //var myCompanyAddress = new Chunk("Đ/C: SỐ 17 NGÁCH 43 NGÕ 64 NG.LƯƠNG BẰNG - ĐỐNG ĐA - HN", defaultFont);
            //document.Add(myCompanyAddress);
            //document.Add(new Phrase("\n"));
            //var myCompanyContact = new Chunk("SĐT: 04.3514.9969          FAX: 04.3573.9119", defaultFont);
            //document.Add(myCompanyContact);
            //document.Add(new Paragraph("\r\n"));
            //var myTitle = new Paragraph("DANH SÁCH ĐƠN HÀNG CẦN GIAO", new Font(baseFont, 22, Font.BOLD, BaseColor.BLACK));
            //myTitle.Alignment = Element.ALIGN_CENTER;
            //document.Add(myTitle);
            document.Add(new Paragraph("\r\n"));

            //From - To
            Rectangle pageSize = document.PageSize;

            var headerTable = new PdfPTable(2)
            {
                WidthPercentage = 100
            };

            headerTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            headerTable.SetWidths(new Single[] { 1, 1 });
            if (!string.IsNullOrEmpty(from))
            {
                var headerLeftCell = new PdfPCell(new Phrase(0, "TỪ NGÀY: " + from, defaultFont))
                {
                    BorderWidth = 0
                };
                headerTable.AddCell(headerLeftCell);
            }
            if (!string.IsNullOrEmpty(to))
            {
                var headerRightCell = new PdfPCell(new Phrase(8, "ĐẾN NGÀY: " + to, defaultFont))
                {
                    HorizontalAlignment = Element.ALIGN_RIGHT,
                    PaddingRight        = 5,
                    BorderWidth         = 0,
                };
                headerTable.AddCell(headerRightCell);
            }
            document.Add(headerTable);

            //Report date
            if (offices.Count() > 1)
            {
                var officeNames    = "";
                var officeEntities = _myOfficeRepository.GetAll().Where(p => products.Contains(p.MyOfficeId)).ToList();
                foreach (var item in officeEntities)
                {
                    officeNames += item.OfficeName + " ;";
                }
                var officeReport = new Chunk(" CHI NHÁNH: " + officeNames, defaultFont);
                document.Add(officeReport);
            }

            if (products.Count() > 1)
            {
                var productNames    = "";
                var productEntities = _productRepository.GetAll().Where(p => products.Contains(p.ProductId)).ToList();
                foreach (var item in productEntities)
                {
                    productNames += item.ProductName + " ;";
                }
                var productReport = new Chunk(" SẢN PHẨM: " + productNames, defaultFont);
                document.Add(productReport);
            }
            document.Add(new Paragraph("\r\n"));

            //Add columns
            var tableColumn = new PdfPTable(6)
            {
                WidthPercentage = 100
            };
            var colWidthPercentages = new Single[] { 7, 9, 31, 23, 15, 15 };

            tableColumn.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            tableColumn.SetWidths(colWidthPercentages);
            tableColumn.AddCell(new PdfPCell(new Phrase("STT", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            tableColumn.AddCell(new PdfPCell(new Phrase("ID", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            tableColumn.AddCell(new PdfPCell(new Phrase("KHÁCH HÀNG", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            tableColumn.AddCell(new PdfPCell(new Phrase("CHI NHÁNH", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            tableColumn.AddCell(new PdfPCell(new Phrase("NGÀY TẠO", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            tableColumn.AddCell(new PdfPCell(new Phrase("THÀNH TIÊN", new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK)))
            {
                BorderWidth = 2f, Padding = 3, HorizontalAlignment = Element.ALIGN_CENTER
            });
            document.Add(tableColumn);

            //Add rows
            var no = 1;

            foreach (var item in orders)
            {
                var tableRow = new PdfPTable(6)
                {
                    WidthPercentage = 100
                };
                tableRow.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
                tableRow.SetWidths(colWidthPercentages);
                tableRow.AddCell(new PdfPCell(new Phrase(no.ToString(), defaultFont)));
                tableRow.AddCell(new PdfPCell(new Phrase((item.OrderId + 1000).ToString(), defaultFont)));
                tableRow.AddCell(new PdfPCell(new Phrase(item.Customer.CustomerName, defaultFont)));
                tableRow.AddCell(new PdfPCell(new Phrase(item.MyOffice.OfficeName, defaultFont)));
                tableRow.AddCell(new PdfPCell(new Phrase(item.CreateDate.ToString("dd/MM/yyyy"), defaultFont)));
                tableRow.AddCell(new PdfPCell(new Phrase(String.Format("{0:0,0}", item.TotalCost), defaultFont)));
                document.Add(tableRow);
                no++;
            }

            document.Close();

            return(pdfData);
        }
コード例 #2
0
        public ActionResult GridModel(ReportFilterModel filterModel)
        {
            //Default get all
            var fromDate      = DateTime.Today;
            var isAllFromDate = true;
            var toDate        = DateTime.Today;
            var isAllToDate   = true;

            //Filter by date
            if (filterModel.FromDate.HasValue)
            {
                fromDate      = filterModel.FromDate.Value.Date;
                isAllFromDate = false;
            }
            if (filterModel.ToDate.HasValue)
            {
                toDate      = filterModel.ToDate.Value.Date.AddDays(1);
                isAllToDate = false;
            }
            var fromListCustomers     = new int[1];
            var isGetAllFromCustomers = true;

            //Filter by deliver to
            if (filterModel.ListCustomers != null && filterModel.ListCustomers.Count() > 0)
            {
                fromListCustomers     = filterModel.ListCustomers;
                isGetAllFromCustomers = false;
            }
            var listOffices         = new int[1];
            var isGetAllListOffices = true;

            //Filter by deliver to
            if (filterModel.ListOffices != null && filterModel.ListOffices.Count() > 0)
            {
                listOffices         = filterModel.ListOffices;
                isGetAllListOffices = false;
            }
            var listProducts         = new int[1];
            var isGetAllListProducts = true;

            //Filter by deliver to
            if (filterModel.ListProducts != null && filterModel.ListProducts.Count() > 0)
            {
                listProducts         = filterModel.ListProducts;
                isGetAllListProducts = false;
            }

            var model = from x in Repository.Search(filterModel.search).Where(p => (listOffices.Contains(p.MyOfficeId) || (isGetAllListOffices && WorkContext.MyOffices.Contains(p.MyOfficeId))) &&
                                                                              (fromListCustomers.Contains(p.Order.CustomerId) || (isGetAllFromCustomers)) &&
                                                                              (p.ExportDetails.Any(k => listProducts.Contains(k.ProductId)) || (isGetAllListProducts)) &&
                                                                              ((p.CreateDate >= fromDate || isAllFromDate) && (p.CreateDate <= toDate || isAllToDate)))
                        join m in _myOfficeRepository.GetAll() on x.MyOfficeId equals m.MyOfficeId into m1
                        from m2 in m1.DefaultIfEmpty()
                        join s in _customerRepository.GetAll() on x.Order.CustomerId equals s.CustomerId into s1
                        from s2 in s1.DefaultIfEmpty()
                        select new ExportTrackModel
            {
                ExportTrackId = x.ExportTrackId,
                CreateDate    = x.CreateDate,
                Note          = x.Note,
                MyOfficeName  = (m2 == null ? String.Empty : m2.OfficeName),
                CustomerName  = (s2 == null ? String.Empty : s2.CustomerName)
            };

            var gridModel = new GridModel <ExportTrackModel>
            {
                Data = model
            };

            return(new JsonResult
            {
                Data = gridModel
            });
        }