public bool AddInvoice(Invoice invoice, List<Product> products ) { try { using (TransactionScope ts = new TransactionScope()) { using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext()) { //Invoice newInvoice=new Invoice(){ InvoiceNo = invoice.InvoiceNo, CostPrice = invoice.CostPrice, Due = invoice.Due, InvoiceDate = invoice.InvoiceDate, TotalPrice = invoice.TotalPrice, vat = invoice.vat}; dataContext.Invoices.Add(invoice); dataContext.SaveChanges(); if (invoice.InvoiceID>0) { foreach (Product product in products) { SellProduct sellProduct=new SellProduct(){ InvoiceID = invoice.InvoiceID, Price = product.Price, ProductID = product.ProductID, Quantiry = product.Quantity, Size = product.Size}; dataContext.SellProducts.Add(sellProduct); dataContext.Database.ExecuteSqlCommand("Update Products set Quantity= Quantity-" + product.Quantity + "where ProductID="+product.ProductID); dataContext.SaveChanges(); } } } ts.Complete(); return true; } return false; } catch (Exception exception) { throw new Exception(exception.Message); } }
private void saveButton_Click(object sender, EventArgs e) { try { List<Product> productsList = GetAllProductONListView(); if (productsList.Count==0) { return; } int i = 1; // StreamWriter sw = null; //sw = new StreamWriter("D://Print//Summary.txt", false ); //sw. string invoiceNo = getInvoiceNo(); Invoice invoice = new Invoice() { InvoiceID = invoiceID, InvoiceNo = invoiceNo, vat = Convert.ToDouble(txtTaxPer.Text), CostPrice = Convert.ToDouble(txtSubTotal.Text), InvoiceDate = Convert.ToDateTime(DateTime.Now.ToString("dd-MM-yyyy")), Due = Convert.ToDouble(txtPaymentDue.Text), TotalPrice = Convert.ToDouble(txtTotal.Text)}; if (invoiceGateway.AddInvoice(invoice, productsList)) { //sw.Write("MONGOL BAROTA" + "\t" + invoice.InvoiceNo + Environment.NewLine + "COMILLA" + "\t\t" + DateTime.Now.ToString("dd-MM-yyyy") + Environment.NewLine + Environment.NewLine); //sw.Write("{0,2}{1,20}{2,5}{3,6}{4,7}{5,0}", "SL", "Name", "Qty","Size", "Price", Environment.NewLine); //sw.Write(Environment.NewLine+Environment.NewLine); //foreach (Product product1 in productsList) //{ // sw.Write("{0,2}{1,20}{2,5}{3,6}{4,7}{5,0}", i, product1.ProductName, product1.Quantity,product1.Size.ToString(), product1.Price.ToString(), Environment.NewLine); // i++; //} //sw.Write("{0,0}{1,0}{2,0}{3,24}{4,10}", Environment.NewLine, "-----------------------------------", Environment.NewLine, "Total", invoice.CostPrice); //sw.Write("{0,0}{1,24}{2,10}", Environment.NewLine, "Vat", invoice.vat); //sw.Write("{0,0}{1,24}{2,10}", Environment.NewLine, "Due", invoice.Due); //sw.Write("{0,0}{1,0}{2,0}{3,24}{4,10}", Environment.NewLine, "-----------------------------------", Environment.NewLine, "Total", invoice.TotalPrice); //sw.WriteLine(); //sw.Close(); //PrintPage(); CreatePDF(invoice,productsList); } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
private void CreatePDF(Invoice invoice, List<Product>productsList) { try { string shopName = "RICEMAN LUBNAN"; string address = "Shop#100, Comila Sadar, Comila, Dhaka, Bangladesh-1200"; iTextSharp.text.Font font10 = iTextSharp.text.FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.BOLD); iTextSharp.text.Font font8B = iTextSharp.text.FontFactory.GetFont("Arial", 08, iTextSharp.text.Font.BOLD); iTextSharp.text.Font font08 = iTextSharp.text.FontFactory.GetFont("Arial", 08, iTextSharp.text.Font.NORMAL); var pgSize = new iTextSharp.text.Rectangle(288, 700); var doc = new iTextSharp.text.Document(pgSize, 0, 0, 0, 0); //Document doc1 = new Document(iTextSharp.text.PageSize.A5, 10, 10, 0, 35); PdfWriter pdf = PdfWriter.GetInstance(doc, new FileStream(@"D:\abc.pdf", FileMode.OpenOrCreate)); doc.Open(); doc.Add(new Paragraph("\n\n")); Paragraph p = new Paragraph(shopName, font10) { SpacingBefore = 0 }; p.Alignment = Element.ALIGN_CENTER; doc.Add(p); doc.Add(new Paragraph(address, font08) { SpacingAfter = 0, IndentationLeft = 30, IndentationRight = 30, Alignment = 1, PaddingTop = 0 }); doc.Add(new Paragraph("------------------------------------------------------------------") { SpacingAfter = 0, IndentationLeft = 10, IndentationRight = 10, Alignment = 1, PaddingTop = 0 }); //1st table PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Phrase("Date : " + DateTime.Now.ToString("dd-MM-yyyy"), font08)) { HorizontalAlignment = 0, Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Time : " + DateTime.Now.ToString("HH:mm:ss tt"), font08)) { HorizontalAlignment = 2, Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Name :", font08)) { HorizontalAlignment = 0, Border = 0, Colspan = 2 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Invoice : "+ invoice.InvoiceNo, font08)) { HorizontalAlignment = 1, Border = 0, Colspan = 2 }; table.AddCell(cell); doc.Add(table); //2nd table int[] width = new int[] { 100, 20, 50, 25, 25, 60 }; table = new PdfPTable(6) { WidthPercentage = 96 }; table.SetWidths(width); cell = new PdfPCell(new Phrase("Itams", font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Qty", font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Price", font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Dis%", font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Vat%", font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); cell = new PdfPCell(new Phrase("Total", font8B)) { HorizontalAlignment = 2, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 1 }; table.AddCell(cell); doc.Add(table); table=new PdfPTable(6){WidthPercentage = 96}; table.SetWidths(width); foreach (Product product1 in productsList) { cell = new PdfPCell(new Phrase(product1.ProductName, font8B)) { HorizontalAlignment = 0, BorderWidthLeft = 0, BorderWidthTop = 0, BorderWidthRight = 0, BorderWidthBottom = 0 }; table.AddCell(cell); cell = new PdfPCell(new Phrase(product1.Quantity.ToString(), font8B)) { HorizontalAlignment = 0, Border = 0}; table.AddCell(cell); cell = new PdfPCell(new Phrase(product1.Price.ToString(), font8B)) { HorizontalAlignment = 0, Border = 0}; table.AddCell(cell); cell = new PdfPCell(new Phrase("Dis%", font8B)) { HorizontalAlignment = 0, Border = 0}; table.AddCell(cell); cell = new PdfPCell(new Phrase("Vat%", font8B)) { HorizontalAlignment = 0, Border = 0}; table.AddCell(cell); cell = new PdfPCell(new Phrase(product1.Price.ToString(), font8B)) { HorizontalAlignment = 2, Border = 0}; table.AddCell(cell); } doc.Add(table); doc.Add(new Paragraph("abcsa hhiokp hutyf giuy9urtf jguydrtjoj jgiuyf hiuyhyudt jgyufyuj[p dtydyhk fygy")); doc.Close(); } catch (Exception exception) { throw new Exception(exception.Message); } }