Пример #1
0
        public async Task ExportNotificationsToPDFAsync(IEnumerable <Notification> notifications)
        {
            try
            {
                PDFGenerator pdf     = new PDFGenerator(Phrases.GlobalStockAlerts, Phrases.StockAlertsListOf);
                Section      section = pdf.CreateDocumentSection();

                // Set title
                pdf.AddParagraph(Phrases.GlobalStockAlerts, true, false, 16);
                pdf.AddParagraph($"{Phrases.GlobalDate}: {DateTime.Now.ShortDate()}", false, true, null, 1);

                // Create table and table columns
                Table table = pdf.CreateTable();
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Right);
                pdf.AddTableColumn(table, ParagraphAlignment.Right);

                // Create table header
                Row row = pdf.CreateTableHeaderRow(table);
                pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, Phrases.GlobalDate, true);
                pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, Phrases.GlobalReference, true);
                pdf.AddTableRowCell(row, 2, ParagraphAlignment.Left, Phrases.GlobalProduct, true);
                pdf.AddTableRowCell(row, 3, ParagraphAlignment.Left, Phrases.GlobalLocation, true);
                pdf.AddTableRowCell(row, 4, ParagraphAlignment.Right, Phrases.StockMovementMinStock, true);
                pdf.AddTableRowCell(row, 5, ParagraphAlignment.Right, Phrases.StockMovementsStock, true);

                // Populate the table rows
                notifications.ToList().ForEach((notification) => {
                    row = table.AddRow();
                    pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, notification.CreatedAt.ShortDateWithTime());
                    pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, notification.ProductLocation.Product.Reference);
                    pdf.AddTableRowCell(row, 2, ParagraphAlignment.Left, notification.ProductLocation.Product.Name);
                    pdf.AddTableRowCell(row, 3, ParagraphAlignment.Left, notification.ProductLocation.Location.Name);
                    pdf.AddTableRowCell(row, 4, ParagraphAlignment.Right, notification.ProductLocation.Stock.ToString());
                    pdf.AddTableRowCell(row, 5, ParagraphAlignment.Right, notification.ProductLocation.MinStock.ToString());
                });

                // Add the table to the section
                pdf.AddTableToLastSection(table);

                // Rendering the document
                await pdf.GenerateAsync();
            }
            catch
            {
                OperationErrorsList errorsList = new OperationErrorsList();
                errorsList.AddError("export-notifications-error", Phrases.GlobalErrorOperationDB);
                throw new ServiceErrorException(errorsList);
            }
        }
        public async Task ExportProductLocationsFromLocationToPDFAsync(IEnumerable <ProductLocation> productLocations)
        {
            try
            {
                string       locationName = productLocations.ElementAt(0).Location.Name;
                PDFGenerator pdf          = new PDFGenerator($"{Phrases.GlobalProducts} {locationName}", Phrases.ProductsListOf);
                Section      section      = pdf.CreateDocumentSection();

                // Set title
                pdf.AddParagraph(Phrases.GlobalProducts, true, false, 16);
                pdf.AddParagraph($"{Phrases.GlobalDate}: {DateTime.Now.ShortDate()}", false, true);
                pdf.AddParagraph($"{Phrases.GlobalLocation}: {locationName}", false, true, null, 1);

                // Create table and table columns
                Table table = pdf.CreateTable();
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Right);

                // Create table header
                Row row = pdf.CreateTableHeaderRow(table);
                pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, Phrases.GlobalReference, true);
                pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, Phrases.GlobalName, true);
                pdf.AddTableRowCell(row, 2, ParagraphAlignment.Right, Phrases.StockMovementsStock, true);

                // Populate the table rows
                productLocations.ToList().ForEach((productLocation) => {
                    row = table.AddRow();
                    pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, productLocation.Product.Reference);
                    pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, productLocation.Product.Name);
                    pdf.AddTableRowCell(row, 2, ParagraphAlignment.Right, productLocation.Stock.ToString());
                });

                // Add the table to the section
                pdf.AddTableToLastSection(table);

                // Rendering the document
                await pdf.GenerateAsync();
            }
            catch
            {
                OperationErrorsList errorsList = new OperationErrorsList();
                errorsList.AddError("export-location-products-error", Phrases.GlobalErrorOperationDB);
                throw new ServiceErrorException(errorsList);
            }
        }
        public async Task ExportStockMovementsToPDFAsync(ExportData <IEnumerable <StockMovement>, StockMovementOptions> data)
        {
            try
            {
                IEnumerable <StockMovement> movements = data.Data;
                StockMovementOptions        options   = data?.Options;

                PDFGenerator pdf     = new PDFGenerator(Phrases.StockMovementsLabel, Phrases.StockMovementsListOf);
                Section      section = pdf.CreateDocumentSection();

                // Set title
                pdf.AddParagraph(Phrases.StockMovementsLabel, true, false, 16);

                if (options != null)
                {
                    string startDate = (options?.StartDate != default)
                        ? $"{options.StartDate.ShortDate()} - "
                        : "";

                    string endDate = (options?.EndDate != default)
                        ? options.EndDate.ShortDate()
                        : "";

                    pdf.AddParagraph($"{Phrases.GlobalDate}: {startDate}{endDate}", false, true);

                    if (!string.IsNullOrEmpty(options.SearchValue))
                    {
                        Product product = movements.ElementAt(0).Product;
                        pdf.AddParagraph($"{Phrases.GlobalProduct}: {product.Reference} {product.Name}", false, true);
                    }

                    if (options.LocationId != null)
                    {
                        Location location = await AppServices.LocationService.GetByIdAsync(( int )options.LocationId);

                        pdf.AddParagraph($"{Phrases.GlobalLocation}: {location.Name}", false, true);
                    }


                    if (options.UserId != null)
                    {
                        User user = await AppServices.UserService.GetByIdAsync(( int )options.UserId);

                        pdf.AddParagraph($"{Phrases.GlobalUser}: {user.Username}", false, true);
                    }

                    pdf.AddParagraph("", false, false, null, 1);
                }

                // Create table and table columns
                Table table = pdf.CreateTable();
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Left);
                pdf.AddTableColumn(table, ParagraphAlignment.Right);
                pdf.AddTableColumn(table, ParagraphAlignment.Right);

                // Create table header
                Row row = pdf.CreateTableHeaderRow(table);
                pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, Phrases.GlobalDate, true);
                pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, Phrases.GlobalUser, true);
                pdf.AddTableRowCell(row, 2, ParagraphAlignment.Left, Phrases.GlobalReference, true);
                pdf.AddTableRowCell(row, 3, ParagraphAlignment.Left, Phrases.GlobalName, true);
                pdf.AddTableRowCell(row, 4, ParagraphAlignment.Left, Phrases.GlobalMovement, true);
                pdf.AddTableRowCell(row, 5, ParagraphAlignment.Right, Phrases.StockMovementQty, true);
                pdf.AddTableRowCell(row, 6, ParagraphAlignment.Right, Phrases.StockMovementStockAcc, true);

                // Populate the table rows
                movements.ToList().ForEach((movement) => {
                    row = table.AddRow();
                    pdf.AddTableRowCell(row, 0, ParagraphAlignment.Left, movement.CreatedAt.ShortDateWithTime());
                    pdf.AddTableRowCell(row, 1, ParagraphAlignment.Left, movement.User.Username);
                    pdf.AddTableRowCell(row, 2, ParagraphAlignment.Left, movement.Product.Reference);
                    pdf.AddTableRowCell(row, 3, ParagraphAlignment.Left, movement.Product.Name);
                    pdf.AddTableRowCell(row, 4, ParagraphAlignment.Left, movement.ConcatMovementString());
                    pdf.AddTableRowCell(row, 5, ParagraphAlignment.Right, movement.Qty.ToString());
                    pdf.AddTableRowCell(row, 6, ParagraphAlignment.Right, movement.Stock.ToString());
                });

                // Add the table to the section
                pdf.AddTableToLastSection(table);

                // Rendering the document
                await pdf.GenerateAsync();
            }
            catch
            {
                OperationErrorsList errorsList = new OperationErrorsList();
                errorsList.AddError("export-stock-movements-error", Phrases.GlobalErrorOperationDB);
                throw new ServiceErrorException(errorsList);
            }
        }