private void btnCostoDia_Click(object sender, RoutedEventArgs e) { if (!lfechareporte.Content.Equals("")) { if (!lventasDia.Content.Equals("0")) { DateTime date = Convert.ToDateTime(lfechareporte.Content); ventasFacade vfac = new ventasFacade(); List<MVentas> listVentasDia = vfac.getVentasByFechaDia(date); if (listVentasDia.Count > 0) { SaveFileDialog exportSaveFileDialog = new SaveFileDialog(); exportSaveFileDialog.Title = "Guardar reporte de costos productos vendidos en el dia"; exportSaveFileDialog.Filter = "PDF(*.pdf)|*.pdf"; exportSaveFileDialog.FileName = "ReportecostoProdvendidosDia"; exportSaveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (exportSaveFileDialog.ShowDialog() == true) { // Creamos el documento con el tamaño de página tradicional Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50); PdfWriter writer = null; // Indicamos donde vamos a guardar el documento try { writer = PdfWriter.GetInstance(doc, new FileStream(exportSaveFileDialog.FileName, FileMode.Create)); doc.AddCreator("Magnolia"); doc.Open(); iTextSharp.text.Image imagen = iTextSharp.text.Image.GetInstance(urlLogo, System.Drawing.Imaging.ImageFormat.Png); imagen.Alignment = Element.ALIGN_CENTER; imagen.ScaleToFit(120f, 120f); doc.Add(imagen); doc.Add(Chunk.NEWLINE); iTextSharp.text.Font _fontTitulo = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 25, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK); iTextSharp.text.Paragraph titulo = new iTextSharp.text.Paragraph("Reporte de costo ventas diaria"); titulo.Alignment = Element.ALIGN_CENTER; titulo.Font = _fontTitulo; doc.Add(titulo); doc.Add(Chunk.NEWLINE); iTextSharp.text.Paragraph dgeneral = new iTextSharp.text.Paragraph("Detalles general"); dgeneral.Alignment = Element.ALIGN_LEFT; //doc.AddTitle("Reporte de MVentas"); iTextSharp.text.Font _standardFont = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK); ////////////////////////////////////// ///////Tabla especificos ////////// ///////////////////////////////////// iTextSharp.text.Paragraph despecifico = new iTextSharp.text.Paragraph("Detalles Especificos"); despecifico.Alignment = Element.ALIGN_LEFT; // doc.Add(Chunk.NEWLINE); categoriaFacade catFacEsp = new categoriaFacade(); ProductoFacade prodFacEsp = new ProductoFacade(); ventasFacade ventasFacEsp = new ventasFacade(); PdfPTable tblEspecifico = new PdfPTable(5); string tempIdpro = ""; int dineroTotalbyCat = 0; List<MVentas> listProductosinCatniInfoProd = new List<MVentas>(); List<MVentas> listProductoConCategoria = new List<MVentas>(); foreach (var item in listVentasDia) { if (!tempIdpro.Equals(item.idProducto)) { string IDCategoria = prodFacEsp.getIdCatbyidProd(item.idProducto); if (string.IsNullOrEmpty(IDCategoria)) { //MessageBox.Show(item.idProducto + ":Sin categoria"); //Agregar en celda distinta para obtener el total, no tienen categoria porque posiblemente se borra una categoria durante el mes o tiempo de uso. //Sin nombre de producto ni categoria List<MVentas> listventaAgrupadabyFecha = ventasFacEsp.getCostoVentasbyIdProdSinNombreGroupByFechaDia(item.idProducto, date); foreach (var i in listventaAgrupadabyFecha) { MVentas sinInfo = new MVentas(i.fecha, i.idProducto, i.nombreProducto, i.precioReal, i.cantidad, i.total, i.idCategoria); bool exists = listProductosinCatniInfoProd.Any(x => x.fecha == sinInfo.fecha && x.idProducto == sinInfo.idProducto); if (!exists) { listProductosinCatniInfoProd.Add(sinInfo); } } } else { //Con nombre de producto pero sin categoria string NombreCat = catFacEsp.getNombreCategoriaById(IDCategoria); if (string.IsNullOrEmpty(NombreCat)) { List<MVentas> listventaAgrupadabyFecha = ventasFacEsp.getCostosVentasbyIdProdGroupByFechaDia(item.idProducto, date); foreach (var i in listventaAgrupadabyFecha) { MVentas sinInfo = new MVentas(i.fecha, i.idProducto, i.nombreProducto, i.precioReal, i.cantidad, i.total, i.idCategoria); listProductosinCatniInfoProd.Add(sinInfo); } } else { /*Agrupar por categoria los idproducto Obtener todas las id categoria para obtener idprodudcto y buscar por idproducto en ventas * si no encuentra idproducto en ventas categoria no ha tenido ventas * */ //Obtiene el cantidadTotal , dineroTotal recaudado para producto agrupado por fecha List<MVentas> listVentaPorIdprod = ventasFacEsp.getCostosVentasbyIdProdGroupByFechaDia(item.idProducto, date); foreach (var v in listVentaPorIdprod) { //idcategoria que sera igual para distinto idproducto MVentas ConInfo = new MVentas(v.fecha, v.idProducto, v.nombreProducto, v.precioReal, v.cantidad, v.total, v.idCategoria); bool exists = listProductoConCategoria.Any(x => x.fecha == ConInfo.fecha && x.idProducto == ConInfo.idProducto && x.idCategoria == ConInfo.idCategoria); if (!exists) { listProductoConCategoria.Add(ConInfo); } } } } } } //LLenar tabla especifico PdfPCell clFechaEsp = new PdfPCell(); PdfPCell clidprod = new PdfPCell(); PdfPCell clnombreprod = new PdfPCell(); PdfPCell clPrecioReal = new PdfPCell(); PdfPCell clCant = new PdfPCell(); PdfPCell cltotalEsp = new PdfPCell(); listProductoConCategoria = listProductoConCategoria.OrderByDescending(i => i.idCategoria).ToList(); var q = from x in listProductoConCategoria group x.idCategoria by x.idCategoria into g let count = g.Count() /*orderby count descending*/ select new { Value = g.Key, Count = count }; int posList = 0; foreach (var x in q) { tblEspecifico.WidthPercentage = 100; tblEspecifico.HorizontalAlignment = Element.ALIGN_LEFT; PdfPCell clnombreCat = new PdfPCell(new Phrase(catFacEsp.getNombreCategoriaById(x.Value.ToString()), _standardFont)); clnombreCat.Colspan = 2; PdfPCell h1 = new PdfPCell(new Phrase("")); PdfPCell h2 = new PdfPCell(new Phrase("")); PdfPCell h3 = new PdfPCell(new Phrase("")); h1.Border = 0; h2.Border = 0; h3.Border = 0; tblEspecifico.AddCell(clnombreCat); tblEspecifico.AddCell(h1); tblEspecifico.AddCell(h2); tblEspecifico.AddCell(h3); clFechaEsp = new PdfPCell(new Phrase("Fecha", _standardFont)); clidprod = new PdfPCell(new Phrase("IDProducto", _standardFont)); clnombreprod = new PdfPCell(new Phrase("Nombre", _standardFont)); clPrecioReal = new PdfPCell(new Phrase("Precio Compra", _standardFont)); clCant = new PdfPCell(new Phrase("Cantidad", _standardFont)); //PdfPCell clTipoPag = new PdfPCell(new Phrase("Tipo Pago", _standardFont)); cltotalEsp = new PdfPCell(new Phrase("Total", _standardFont)); tblEspecifico.AddCell(clFechaEsp); //tblEspecifico.AddCell(clidprod); tblEspecifico.AddCell(clnombreprod); tblEspecifico.AddCell(clPrecioReal); tblEspecifico.AddCell(clCant); //tblEspecifico.AddCell(clTipoPag); tblEspecifico.AddCell(cltotalEsp); for (int i = 0; i < x.Count; i++) { var v = listProductoConCategoria[posList]; clFechaEsp = new PdfPCell(new Phrase(v.fecha.ToString("d"), _standardFont)); //clidprod = new PdfPCell(new Phrase(x.Value.ToString(), _standardFont)); clidprod = new PdfPCell(new Phrase(v.idProducto.ToString(), _standardFont)); //clnombreprod = new PdfPCell(new Phrase(v.idCategoria.ToString() + ":" + v.nombreProducto, _standardFont)); clnombreprod = new PdfPCell(new Phrase(v.nombreProducto.ToString(), _standardFont)); clPrecioReal = new PdfPCell(new Phrase(v.precioReal.ToString(), _standardFont)); //clnombreprod= new PdfPCell(new Phrase(prodFacEsp.getnombreProdbyidProd(v.idProducto), _standardFont)); clCant = new PdfPCell(new Phrase(v.cantidad.ToString(), _standardFont)); //clTipoPag = new PdfPCell(new Phrase("", _standardFont)); cltotalEsp = new PdfPCell(new Phrase(v.total.ToString(), _standardFont)); tblEspecifico.AddCell(clFechaEsp); //tblEspecifico.AddCell(clidprod); tblEspecifico.AddCell(clnombreprod); tblEspecifico.AddCell(clPrecioReal); tblEspecifico.AddCell(clCant); //tblEspecifico.AddCell(clTipoPag); tblEspecifico.AddCell(cltotalEsp); dineroTotalbyCat = dineroTotalbyCat + Convert.ToInt32(v.total); tempIdpro = v.idProducto; posList = posList + 1; } //################################# //en cada categoria //################################# clFechaEsp = new PdfPCell(new Phrase("", _standardFont)); clidprod = new PdfPCell(new Phrase("", _standardFont)); clnombreprod = new PdfPCell(new Phrase("", _standardFont)); clPrecioReal = new PdfPCell(new Phrase("", _standardFont)); clCant = new PdfPCell(new Phrase("Subtotal($)", _standardFont)); //clTipoPag = new PdfPCell(new Phrase("-----------", _standardFont)); cltotalEsp = new PdfPCell(new Phrase(dineroTotalbyCat.ToString(), _standardFont)); dineroTotalbyCat = 0; clFechaEsp.Border = 0; clidprod.Border = 0; clnombreprod.Border = 0; clPrecioReal.Border = 0; clCant.Border = 1; cltotalEsp.Border = 1; tblEspecifico.AddCell(clFechaEsp); //tblEspecifico.AddCell(clidprod); tblEspecifico.AddCell(clnombreprod); tblEspecifico.AddCell(clPrecioReal); tblEspecifico.AddCell(clCant); //tblEspecifico.AddCell(clTipoPag); tblEspecifico.AddCell(cltotalEsp); ///////////////////////////////////////////// clFechaEsp = new PdfPCell(new Phrase("", _standardFont)); clidprod = new PdfPCell(new Phrase("", _standardFont)); clnombreprod = new PdfPCell(new Phrase("", _standardFont)); clPrecioReal = new PdfPCell(new Phrase("", _standardFont)); clCant = new PdfPCell(new Phrase("", _standardFont)); //clTipoPag = new PdfPCell(new Phrase("-----------", _standardFont)); cltotalEsp = new PdfPCell(new Phrase("", _standardFont)); clFechaEsp.Border = 0; clidprod.Border = 0; clnombreprod.Border = 0; clPrecioReal.Border = 0; clCant.Border = 0; cltotalEsp.Border = 0; cltotalEsp.FixedHeight = 30f; clFechaEsp.FixedHeight = 30f; clidprod.FixedHeight = 30f; clnombreprod.FixedHeight = 30f; clPrecioReal.FixedHeight = 30f; clCant.FixedHeight = 30f; cltotalEsp.FixedHeight = 30f; tblEspecifico.AddCell(clFechaEsp); //tblEspecifico.AddCell(clidprod); tblEspecifico.AddCell(clnombreprod); tblEspecifico.AddCell(clPrecioReal); tblEspecifico.AddCell(clCant); //tblEspecifico.AddCell(clTipoPag); tblEspecifico.AddCell(cltotalEsp); //} // catActual = prodFac.getIdCatbyidProd(v.idProducto); //MessageBox.Show(catActual); } //####################################################### //###### PRODUCTOS QUE SE HALLAN ELIMINADO ############ //####################################################### //Tabla para tabla otros que no tengan una categoria asociada pero que igualmente deben estar en reportes de ventas. bool otros = false; PdfPTable tblOtros = new PdfPTable(5); int totalOtros = 0; if (listProductosinCatniInfoProd.Count > 0) { otros = true; tblOtros.WidthPercentage = 100; tblOtros.HorizontalAlignment = Element.ALIGN_LEFT; PdfPCell clnombreCat_otros = new PdfPCell(new Phrase("Otros", _standardFont)); clnombreCat_otros.Colspan = 2; PdfPCell h11 = new PdfPCell(new Phrase("")); PdfPCell h22 = new PdfPCell(new Phrase("")); PdfPCell h33 = new PdfPCell(new Phrase("")); h11.Border = 0; h22.Border = 0; h33.Border = 0; tblOtros.AddCell(clnombreCat_otros); tblOtros.AddCell(h11); tblOtros.AddCell(h22); tblOtros.AddCell(h33); PdfPCell clFechaEsp_otros = new PdfPCell(new Phrase("Fecha", _standardFont)); PdfPCell clidprod_otros = new PdfPCell(new Phrase("IDProducto", _standardFont)); PdfPCell clnombreprod_otros = new PdfPCell(new Phrase("Nombre", _standardFont)); PdfPCell clPrecioReal_otros = new PdfPCell(new Phrase("Precio Compra", _standardFont)); PdfPCell clCant_otros = new PdfPCell(new Phrase("Cantidad", _standardFont)); //PdfPCell clTipoPag = new PdfPCell(new Phrase("Tipo Pago", _standardFont)); PdfPCell cltotalEsp_otros = new PdfPCell(new Phrase("Total", _standardFont)); tblOtros.AddCell(clFechaEsp_otros); //tblOtros.AddCell(clidprod_otros); tblOtros.AddCell(clnombreprod_otros); tblOtros.AddCell(clPrecioReal_otros); tblOtros.AddCell(clCant_otros); //tblEspecifico.AddCell(clTipoPag); tblOtros.AddCell(cltotalEsp_otros); foreach (var item in listProductosinCatniInfoProd) { clFechaEsp_otros = new PdfPCell(new Phrase(item.fecha.ToString("d"), _standardFont)); clidprod_otros = new PdfPCell(new Phrase(item.idProducto, _standardFont)); clnombreprod_otros = new PdfPCell(new Phrase(item.nombreProducto, _standardFont)); clPrecioReal_otros = new PdfPCell(new Phrase(item.precioReal, _standardFont)); //clnombreprod= new PdfPCell(new Phrase(prodFacEsp.getnombreProdbyidProd(v.idProducto), _standardFont)); clCant_otros = new PdfPCell(new Phrase(item.cantidad.ToString(), _standardFont)); //clTipoPag = new PdfPCell(new Phrase("", _standardFont)); cltotalEsp_otros = new PdfPCell(new Phrase(item.total.ToString(), _standardFont)); tblOtros.AddCell(clFechaEsp_otros); //tblOtros.AddCell(clidprod_otros); tblOtros.AddCell(clnombreprod_otros); tblOtros.AddCell(clPrecioReal_otros); tblOtros.AddCell(clCant_otros); //tblEspecifico.AddCell(clTipoPag); tblOtros.AddCell(cltotalEsp_otros); totalOtros = totalOtros + Convert.ToInt32(item.total); } clFechaEsp_otros = new PdfPCell(new Phrase("", _standardFont)); clidprod_otros = new PdfPCell(new Phrase("", _standardFont)); clnombreprod_otros = new PdfPCell(new Phrase("", _standardFont)); clPrecioReal_otros = new PdfPCell(new Phrase("", _standardFont)); clCant_otros = new PdfPCell(new Phrase("Subtotal($)", _standardFont)); //clTipoPag = new PdfPCell(new Phrase("-----------", _standardFont)); cltotalEsp_otros = new PdfPCell(new Phrase(totalOtros.ToString(), _standardFont)); clFechaEsp_otros.Border = 0; clidprod_otros.Border = 0; clnombreprod_otros.Border = 0; clPrecioReal_otros.Border = 0; clCant_otros.Border = 1; cltotalEsp_otros.Border = 1; tblOtros.AddCell(clFechaEsp_otros); //tblOtros.AddCell(clidprod_otros); tblOtros.AddCell(clnombreprod_otros); tblOtros.AddCell(clPrecioReal_otros); tblOtros.AddCell(clCant_otros); //tblEspecifico.AddCell(clTipoPag); tblOtros.AddCell(cltotalEsp_otros); clFechaEsp_otros = new PdfPCell(new Phrase("", _standardFont)); clidprod_otros = new PdfPCell(new Phrase("", _standardFont)); clnombreprod_otros = new PdfPCell(new Phrase("", _standardFont)); clPrecioReal_otros = new PdfPCell(new Phrase("", _standardFont)); clCant_otros = new PdfPCell(new Phrase("", _standardFont)); //clTipoPag = new PdfPCell(new Phrase("-----------", _standardFont)); cltotalEsp_otros = new PdfPCell(new Phrase("", _standardFont)); clFechaEsp_otros.Border = 0; clidprod_otros.Border = 0; clnombreprod_otros.Border = 0; clPrecioReal_otros.Border = 0; clCant_otros.Border = 0; cltotalEsp_otros.Border = 0; cltotalEsp_otros.FixedHeight = 50f; clFechaEsp_otros.FixedHeight = 50f; clidprod_otros.FixedHeight = 50f; clnombreprod_otros.FixedHeight = 50f; clPrecioReal_otros.FixedHeight = 50f; clCant_otros.FixedHeight = 50f; cltotalEsp_otros.FixedHeight = 50f; tblOtros.AddCell(clFechaEsp_otros); //tblOtros.AddCell(clidprod_otros); tblOtros.AddCell(clnombreprod_otros); tblOtros.AddCell(clPrecioReal_otros); tblOtros.AddCell(clCant_otros); //tblEspecifico.AddCell(clTipoPag); tblOtros.AddCell(cltotalEsp_otros); } //####################################################### //###### DETALLES GENERALES ############ //####################################################### PdfPTable tblGenerales = new PdfPTable(3); //doc.SetMargins(0f, 0f, 0f, 0f); tblGenerales.HorizontalAlignment = Element.ALIGN_LEFT; // Configuramos el título de las columnas de la tabla PdfPCell clFecha = new PdfPCell(new Phrase("Dia", _standardFont)); PdfPCell clCat = new PdfPCell(new Phrase("Categoria", _standardFont)); PdfPCell clSubtotal = new PdfPCell(new Phrase("Subtotal", _standardFont)); tblGenerales.AddCell(clFecha); tblGenerales.AddCell(clCat); tblGenerales.AddCell(clSubtotal); categoriaFacade catFac = new categoriaFacade(); ProductoFacade prodFac = new ProductoFacade(); int subtotal = 0; int total = 0; int posListG = 0; //Agrupar por categoria los idproducto List<MVentas> ListcatGeneral = new List<MVentas>(); foreach (var x in q) { for (int i = 0; i < x.Count; i++) { var v = listProductoConCategoria[posListG]; subtotal = subtotal + Convert.ToInt32(v.total); posListG = posListG + 1; } total = total + subtotal; clFecha = new PdfPCell(new Phrase(date.ToString("D"), _standardFont)); clCat = new PdfPCell(new Phrase(catFac.getNombreCategoriaById(x.Value.ToString()), _standardFont)); clSubtotal = new PdfPCell(new Phrase(subtotal.ToString(), _standardFont)); tblGenerales.AddCell(clFecha); tblGenerales.AddCell(clCat); tblGenerales.AddCell(clSubtotal); subtotal = 0; } if (otros) { clFecha = new PdfPCell(new Phrase(date.ToString("D"), _standardFont)); clCat = new PdfPCell(new Phrase("Otros", _standardFont)); clSubtotal = new PdfPCell(new Phrase(totalOtros.ToString(), _standardFont)); tblGenerales.AddCell(clFecha); tblGenerales.AddCell(clCat); tblGenerales.AddCell(clSubtotal); total = total + totalOtros; } clFecha = new PdfPCell(new Phrase("", _standardFont)); clCat = new PdfPCell(new Phrase("Total Costo Ventas Producto", _standardFont)); clSubtotal = new PdfPCell(new Phrase(total.ToString(), _standardFont)); clFecha.Border = 0; tblGenerales.AddCell(clFecha); tblGenerales.AddCell(clCat); tblGenerales.AddCell(clSubtotal); int vtotales = listVentasDia.Count; iTextSharp.text.Font _fontDe = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK); iTextSharp.text.Paragraph ventasTotal = new iTextSharp.text.Paragraph("Total Ventas:" + vtotales.ToString(), _fontDe); int efectivo = ventasFacEsp.getVentasByFechaDiaPagoEfectivo(date); int cuenta = ventasFacEsp.getVentasByFechaDiaPagoCuenta(date); int debito = ventasFacEsp.getVentasByFechaDiaPagoDebito(date); int cheque = ventasFacEsp.getVentasByFechaDiaPagoCheque(date); iTextSharp.text.Paragraph pago = new iTextSharp.text.Paragraph("Pago: Efectivo:" + efectivo.ToString() + " Cuenta:" + cuenta.ToString() + " Debito:" + debito.ToString() + " Cheque:" + cheque.ToString(), _fontDe); ventasTotal.Alignment = Element.ALIGN_LEFT; if (otros) { doc.Add(dgeneral); doc.Add(Chunk.NEWLINE); doc.Add(tblGenerales); doc.Add(ventasTotal); doc.Add(pago); doc.Add(Chunk.NEWLINE); doc.Add(despecifico); doc.Add(new Chunk("\n")); doc.Add(tblEspecifico); doc.Add(new Chunk("\n")); doc.Add(tblOtros); } else { doc.Add(dgeneral); doc.Add(Chunk.NEWLINE); doc.Add(tblGenerales); doc.Add(ventasTotal); doc.Add(pago); doc.Add(Chunk.NEWLINE); doc.Add(despecifico); doc.Add(new Chunk("\n")); doc.Add(tblEspecifico); doc.Add(new Chunk("\n")); } int page = writer.PageNumber; //iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph(page.ToString(), _standardFont); //doc.Add(p); doc.Close(); writer.Close(); //MessageBox.Show("Pdf Creado!"); } // } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { doc.Close(); writer.Close(); } System.Diagnostics.Process.Start(exportSaveFileDialog.FileName); } } else { MessageBox.Show("Dia no ha tenido ventas", "Magnolia", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Dia no ha tenido ventas", "Magnolia", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Seleccionar fecha", "Magnolia", MessageBoxButton.OK, MessageBoxImage.Warning); } }