Exemplo n.º 1
0
        //invoice
        private void btnGenerateInvoice_Click(object sender, RoutedEventArgs e)
        {
            SelectedSalesOrder = lvSalesOrders.SelectedItem as SalesOrder;

            if (SelectedSalesOrder != null)
            {
                Invoice invoice = new Invoice()
                {
                    SalesOrder = SelectedSalesOrder,
                    Date       = DateTime.Now,
                    Paid       = SelectedSalesOrder.Paid
                };


                //create pdf invoice
                using (PdfDocument document = new PdfDocument())
                {
                    PdfPage page = document.Pages.Add();

                    PdfGraphics graphics = page.Graphics;

                    PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14);

                    PdfImage image = PdfImage.FromFile("../../Images/coolblue_banner.jpg");

                    graphics.DrawImage(image, 0, 0, 300, 100);

                    graphics.DrawString("Order Invoice", font, PdfBrushes.Black, new PointF(0, 100));

                    graphics.DrawString("Reference Number: " + SelectedSalesOrder.SalesOrderId.ToString(), font, PdfBrushes.Black, new PointF(0, 120));

                    graphics.DrawString("Date: " + SelectedSalesOrder.OrderDate.ToString(), font, PdfBrushes.Black, new PointF(0, 140));

                    graphics.DrawString("Total: £" + SelectedSalesOrder.TotalPrice.ToString(), font, PdfBrushes.Black, new PointF(0, 160));
                    graphics.DrawString("BTW: £" + UtilityClass.BTW(SelectedSalesOrder.TotalPrice).ToString(), font, PdfBrushes.Black, new PointF(100, 160));
                    graphics.DrawString("Total (inc BTW): £" + UtilityClass.PriceWithBTW(SelectedSalesOrder.TotalPrice).ToString(), font, PdfBrushes.Black, new PointF(200, 160));

                    PdfGrid pdfGrid = new PdfGrid();

                    DataTable datatable = new DataTable();

                    datatable.Columns.Add("Product");
                    datatable.Columns.Add("Description");
                    datatable.Columns.Add("Quantity");
                    datatable.Columns.Add("Price");


                    //grouped products for invoice
                    var groupedProducts = SelectedSalesOrder.SalesOrderProducts.GroupBy(product => product.Product.Name);

                    //create 1 row for each group of products
                    foreach (var group in groupedProducts)
                    {
                        datatable.Rows.Add(new Object[] { group.Key, group.First().Product.Description, group.Count().ToString(), (group.First().Product.Price *group.Count()).ToString() });
                    }

                    pdfGrid.DataSource = datatable;
                    pdfGrid.Draw(page, new PointF(0, 200));



                    //allow user to select where to save document-  savefiledialog
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter       = "Files(*.pdf)|*.pdf";
                    saveFileDialog.AddExtension = true;
                    saveFileDialog.DefaultExt   = ".pdf";

                    if (saveFileDialog.ShowDialog() == true && saveFileDialog.CheckPathExists)
                    {
                        document.Save(saveFileDialog.FileName);
                        document.Close();

                        MessageBoxResult result = MessageBox.Show("Do you want to view the invoice?", "Invoice Created", MessageBoxButton.YesNo, MessageBoxImage.Information);
                        if (result == MessageBoxResult.Yes)
                        {
                            Process process = new Process();
                            process.StartInfo.FileName = saveFileDialog.FileName;
                            process.Start();
                        }
                    }
                }
            }
            else
            {
                MessageNotSelected();
            }
        }