using CKUERA; using iTextSharp.text.pdf; using iTextSharp.text; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Image = System.Drawing.Image; using ImageI = iTextSharp.text.Image; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using iTextSharp.text.pdf.parser; namespace ECV { public partial class Rep_Cliente_Prod : Form { public Rep_Cliente_Prod() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; // CREAR PDF string pdfgenerate = "\\DOC_PDF\\REPORTE.pdf"; Document oDoc; PdfWriter pdfw; PdfContentByte cb; float top1; //PREPARA EL PDF oDoc = new Document(PageSize.A4, 30, 20, 20, 20); pdfw = PdfWriter.GetInstance(oDoc, new FileStream(pdfgenerate, FileMode.Create, FileAccess.Write, FileShare.None)); pdfw.CompressionLevel = PdfStream.BEST_COMPRESSION; pdfw.SetFullCompression(); oDoc.Open(); oDoc.NewPage(); top1 = oDoc.PageSize.Top; try { //ENCABEZADO LOGO (usado en reportes) ImageI logo = ImageI.GetInstance("logo.png", true); logo.CompressionLevel = PdfStream.NO_COMPRESSION; //logo.SetAbsolutePosition(60, top1 - 130); oDoc.Add(logo); } catch (Exception) { } cb = pdfw.DirectContent;//DEBE IR ANTES DE LAS LÍNEAS DE TEXTO //EL TÍTULO Paragraph title = new Paragraph(); title.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 10, BaseColor.BLACK); if (combo_producto.Text == "")//SI ES SÓLO POR CLIENTE { title.Add("REPORTE DE VENTAS POR CLIENTE: " + combo_cliente.Text + "." + "\nDESDE: " + Fecha_desde.Text + ", HASTA: " + Fecha_hasta.Text + "\nGenerado por " + Global.USUARIO_SIST + " - Terminal " + Global.NUM_caja + " - Fecha: " + DateTime.Now.ToString()); } else//SI POR CLIENTE Y PRODUCTO { title.Add("REPORTE DE CLIENTE POR PRODUCTO: " + combo_producto.Text + "." + "\nDESDE: " + Fecha_desde.Text + ", HASTA: " + Fecha_hasta.Text + "\nGenerado por " + Global.USUARIO_SIST + " - Terminal " + Global.NUM_caja + " - Fecha: " + DateTime.Now.ToString()); } title.Alignment = Element.ALIGN_LEFT; oDoc.Add(title); PdfPTable table = new PdfPTable(7);//TABLA CON 7 CELDAS //TEXTO QUE IRÁ DENTRO DE LA TABLA DEL ENCABEZADO Paragraph TEXTO_T0 = new Paragraph(); TEXTO_T0.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9f, BaseColor.BLACK); TEXTO_T0.Add("FECHA"); Paragraph TEXTO_T1 = new Paragraph(); TEXTO_T1.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9f, BaseColor.BLACK); TEXTO_T1.Add("HORA"); Paragraph TEXTO_T2 = new Paragraph(); TEXTO_T2.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9f, BaseColor.BLACK); TEXTO_T2.Add("CODIGO"); TEXTO_T2.Alignment = Element.ALIGN_RIGHT; Paragraph TEXTO_T3 = new Paragraph(); TEXTO_T3.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9f, BaseColor.BLACK); TEXTO_T3.Add("DESCRIPCION"); Paragraph TEXTO_VEN = new Paragraph(); TEXTO_VEN.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9f, BaseColor.BLACK); TEXTO_VEN.Add("CANT"); Phrase Titulo4 = new Phrase("PRECIO", FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK)); PdfPCell TEXTO_T4 = new PdfPCell(Titulo4); TEXTO_T4.HorizontalAlignment = Element.ALIGN_LEFT; TEXTO_T4.BackgroundColor = new BaseColor(224, 255, 255); Phrase Titulo5 = new Phrase("SUBTOTAL", FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK)); PdfPCell TEXTO_T5 = new PdfPCell(Titulo5); TEXTO_T5.HorizontalAlignment = Element.ALIGN_RIGHT; TEXTO_T5.BackgroundColor = new BaseColor(224, 255, 255); //table.HorizontalAlignment = 0; table.TotalWidth = 545f;//MEDIDA TOTAL DE LA TABLA table.LockedWidth = true; float[] widths = new float[] { 55f, 40f, 72f, 235f, 30f, 55f, 55f };//MEDIDAS DE COLUMNAS table.SetWidths(widths); //table.WidthPercentage = 100; table.DefaultCell.BackgroundColor = new BaseColor(224, 255, 255); table.DefaultCell.BorderColor = new BaseColor(0, 0, 0);//125,125,255 table.AddCell(TEXTO_T0); table.AddCell(TEXTO_T1); table.AddCell(TEXTO_T2); table.AddCell(TEXTO_T3); table.AddCell(TEXTO_VEN); table.AddCell(TEXTO_T4); table.AddCell(TEXTO_T5); table.DefaultCell.BackgroundColor = new BaseColor(255, 255, 255); string devuelve = ""; if (combo_producto.Text == "") { string id_cliente = Global.ID_cliente.ToString().Trim(); string fecha_dede = DateTime.Parse(Fecha_desde.Text).ToString("yyyy-MM-dd"); string fecha_hasta = DateTime.Parse(Fecha_hasta.Text).ToString("yyyy-MM-dd"); HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("http://" + Global._host + "/ecv/rep_cliente_prod.php"); rq.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(string.Format("nu={0}▼{1}▼{2}▼{3}▼", id_cliente, "", fecha_dede, fecha_hasta)); rq.ContentType = "application/x-www-form-urlencoded"; rq.ContentLength = byteArray.Length; Stream stream = rq.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); // WebResponse response = rq.GetResponse(); stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); devuelve = reader.ReadToEnd(); reader.Close(); stream.Close(); response.Close(); if (devuelve.ToUpper().Contains("WARNING") | devuelve.ToUpper().Contains("ERROR")) { MessageBox.Show(devuelve); this.Cursor = Cursors.Default; return; } } else { string id_cliente = Global.ID_cliente.ToString(); string fecha_dede = DateTime.Parse(Fecha_desde.Text).ToString("yyyy-MM-dd"); string fecha_hasta = DateTime.Parse(Fecha_hasta.Text).ToString("yyyy-MM-dd"); HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("http://" + Global._host + "/ecv/rep_cliente_prod.php");//id_cliente, códigoProd, Desde, Hasta rq.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(string.Format("nu={0}▼{1}▼{2}▼{3}▼", id_cliente, Global.CodigoProd, fecha_dede, fecha_hasta)); rq.ContentType = "application/x-www-form-urlencoded"; rq.ContentLength = byteArray.Length; Stream stream = rq.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); // WebResponse response = rq.GetResponse(); stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); devuelve = reader.ReadToEnd(); reader.Close(); stream.Close(); response.Close(); if (devuelve.ToUpper().Contains("WARNING") | devuelve.ToUpper().Contains("ERROR")) { MessageBox.Show(devuelve); Application.Exit(); } } //TEXTO QUE IRÁ DENTRO DE LA TABLA DEL CUERPO if (devuelve != "") { foreach (var line in devuelve.Split('↔')) { var valores = line.Split('▲'); if (valores[0] != "") { Paragraph fecha = new Paragraph(); fecha.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK); fecha.Add(DateTime.Parse(valores[0]).ToString("dd-MM-yyyy")); Paragraph hora = new Paragraph(); hora.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK); hora.Add(DateTime.Parse(valores[1]).ToString("HH:mm:ss")); Paragraph codigo = new Paragraph(); codigo.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK); codigo.Add(valores[2]); Paragraph descrip = new Paragraph(); descrip.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK); descrip.Add(valores[3]); string cant= double.Parse(valores[4]).ToString("#,##0"); Paragraph cantidad = new Paragraph(); cantidad.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK); cantidad.Add(cant); PdfPCell cantidadx = new PdfPCell(cantidad); cantidadx.HorizontalAlignment = Element.ALIGN_CENTER; cantidadx.BackgroundColor = new BaseColor(255, 255, 255); cantidadx.BorderColor = new BaseColor(211, 211, 211);//125,125,255 string Montox = double.Parse(valores[5]).ToString("#,##0.##"); Phrase precio = new Phrase(string.Format("{0:#,##0.##}", Montox), FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK)); PdfPCell monto = new PdfPCell(precio); monto.HorizontalAlignment = Element.ALIGN_RIGHT; monto.BackgroundColor = new BaseColor(255, 255, 255); monto.BorderColor = new BaseColor(211, 211, 211);//125,125,255 string subt = (double.Parse(valores[5]) * double.Parse(valores[4])).ToString("#,##0.##"); Phrase sub = new Phrase(string.Format("{0:#,##0.##}", subt), FontFactory.GetFont(FontFactory.DefaultEncoding, 9, BaseColor.BLACK)); PdfPCell subtotal = new PdfPCell(sub); subtotal.HorizontalAlignment = Element.ALIGN_RIGHT; subtotal.BackgroundColor = new BaseColor(255, 255, 255); subtotal.BorderColor = new BaseColor(211, 211, 211);//125,125,255 //table.WidthPercentage = 100; table.DefaultCell.BorderColor = new BaseColor(211, 211, 211);//125,125,255 table.AddCell(fecha); table.AddCell(hora); table.AddCell(codigo); table.AddCell(descrip); table.AddCell(cantidadx); table.AddCell(monto); table.AddCell(subtotal); Global.Total_Rep_Prod = Global.Total_Rep_Prod + double.Parse(subt); } } } //TOTAL DE VENTA QUE SALE EN LA CABECERA Paragraph title2 = new Paragraph(); title2.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 14, BaseColor.BLUE); title2.Add("Valor Total " + Global.SIMB_ + " " + string.Format("{0:#,##0.##}", Global.Total_Rep_Prod)); title2.Alignment = Element.ALIGN_RIGHT; oDoc.Add(title2); //PARRAFO VACÍO COMO SEPARADOR Paragraph title3 = new Paragraph(); title3.Font = FontFactory.GetFont(FontFactory.DefaultEncoding, 2, BaseColor.BLACK); title3.Add("."); title3.Alignment = Element.ALIGN_LEFT; oDoc.Add(title3); oDoc.Add(table); this.Cursor = Cursors.Default; pdfw.Flush(); oDoc.Close(); System.Diagnostics.Process.Start(@pdfgenerate);//MUESTRA EL REPORTE EN PDF //return (null); } private void Rep_Cliente_Prod_Load(object sender, EventArgs e) { Fecha_desde.Text = DateTime.Now.ToString(); Fecha_hasta.Text = DateTime.Now.ToString(); Button1.BackColor= Global.ColorSistema; } private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e) { } private void combo_cliente_TextChanged(object sender, EventArgs e) { DataG3.Top = 132; if (combo_cliente.Text == "") { DataG3.Rows.Clear(); DataG3.Visible = false; return; } DataG3.Rows.Clear(); HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("http://" + Global._host + "/ecv/rep_cliente_prod_cargar_razon_s.php"); rq.Method = "POST"; //WebRequest.Create("http://" + Global._host + " byte[] byteArray = Encoding.UTF8.GetBytes(string.Format("nu={0}", combo_cliente.Text)); rq.ContentType = "application/x-www-form-urlencoded"; rq.ContentLength = byteArray.Length; Stream stream = rq.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); // WebResponse response = rq.GetResponse(); stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string devuelve = reader.ReadToEnd(); reader.Close(); stream.Close(); response.Close(); string resultado = devuelve.ToUpper(); bool re; re = resultado.Contains("ERROR"); if (re == true) { MessageBox.Show(devuelve); return; } if (devuelve != "") { foreach (var line in devuelve.Split('↔')) { if (line == "")//si la última línea está vacía, continua al siguiente proceso { continue; } var v = line.Split('▲'); DataG3.Visible = true; DataG3.Rows.Add(v[0].ToUpper(), v[1].ToUpper(), v[2].ToUpper()); DataG3.Columns[0].Width = 465; DataG3.Columns[1].Width = 90; DataG3.Columns[2].Width = 0; } } } private void DataG3_Click(object sender, EventArgs e) { if (DataG3.CurrentRow == null)//SI NO HAY DATOS... { return; } else { if (DataG3.Top == 132)//CLIENTES { combo_cliente.Text = DataG3.CurrentRow.Cells[0].Value.ToString() + ", RUC: " + DataG3.CurrentRow.Cells[1].Value.ToString() + ", ID|" + DataG3.CurrentRow.Cells[2].Value.ToString();//RAZÓN SOCIAL var v = combo_cliente.Text.Split('|'); Global.ID_cliente = int.Parse(v[1]); DataG3.Visible = false; return; } if (DataG3.Top == 186)//PRODUCTOS { Global.CodigoProd = DataG3.CurrentRow.Cells[1].Value.ToString().Trim(); combo_producto.Text = DataG3.CurrentRow.Cells[0].Value.ToString().Trim() + ", CÓDIGO: " + DataG3.CurrentRow.Cells[1].Value.ToString().Trim(); DataG3.Visible = false; return; } } } private void combo_producto_TextChanged(object sender, EventArgs e) { DataG3.Top = 186; if (combo_producto.Text == "") { DataG3.Rows.Clear(); DataG3.Visible=false; return; } DataG3.Rows.Clear(); HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("http://" + Global._host + "/ecv/consultar_precios.php"); rq.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(string.Format("nu={0}▼{1}▼{2}", combo_producto.Text, "", "")); rq.ContentType = "application/x-www-form-urlencoded"; rq.ContentLength = byteArray.Length; Stream stream = rq.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); // WebResponse response = rq.GetResponse(); stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string devuelve = reader.ReadToEnd(); reader.Close(); stream.Close(); response.Close(); string resultado = devuelve.ToUpper(); bool re; re = resultado.Contains("ERROR"); if (re == true) { MessageBox.Show(devuelve); return; } if (devuelve != "") { foreach (var line in devuelve.Split('↔')) { if (line == "")//si la última línea está vacía, continua al siguiente proceso { continue; } var v = line.Split('▲'); DataG3.Visible = true; DataG3.Rows.Add(v[1].ToUpper(), v[2].ToUpper()); DataG3.Columns[0].Width = 455; DataG3.Columns[1].Width = 100; } } } private void combo_producto_SelectedIndexChanged(object sender, EventArgs e) { } private void DataG3_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }