Files

244 lines
8.8 KiB
C#

using compabetov2.database;
using compabetov2.database.modelos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace compabetov2.Ventas
{
public partial class datosEnvio : Form
{
public string Cliente => txtCliente.Text;
public string Direccion => txtDireccion.Text;
public string Telefono => txtTelefono.Text;
private Envio envio;
List<DetalleEnvioModel> detallesEnvio;
List<DetalleImporte> importes;
vender ventanaPadre;
public datosEnvio(Envio envio, List<DetalleEnvioModel> detallesEnvio, List<DetalleImporte> importes, vender ventanaPadre)
{
InitializeComponent();
this.envio = envio;
this.detallesEnvio = detallesEnvio;
this.ventanaPadre = ventanaPadre;
this.importes = importes;
txtTelefono.KeyPress += new KeyPressEventHandler(txtTelefono_KeyPress);
}
private void btnAceptar_Click(object sender, EventArgs e)
{
envio.cliente = txtCliente.Text;
envio.direccion = txtDireccion.Text;
envio.telefono = txtTelefono.Text;
if (service.crearEnvio(envio, detallesEnvio, importes))
{
MessageBox.Show("Envio hecho");
ImprimirTicketEnvio();
ventanaPadre.eliminarProductosTocket();
ventanaPadre.llenarLayout();
ventanaPadre.total = 0;
ventanaPadre.importes.Clear();
this.Close();
}
}
private string crearContenidoImprimir()
{
int contador_saltar_2_vueltas = 1;
string resultado = "";
foreach (var item in ventanaPadre.flowLayoutPanel2.Controls)
{
if (contador_saltar_2_vueltas <= 3)
{
contador_saltar_2_vueltas++;
continue;
}
TableLayoutPanel panelProducto = item as TableLayoutPanel;
TableLayoutPanel panelInfo = panelProducto.Controls[1] as TableLayoutPanel;
Label lbnombre = panelInfo.Controls[0] as Label;
Label lbPrecio = panelInfo.Controls[1] as Label;
Label Lbcantidad = panelInfo.Controls[2] as Label;
Label Lbtotal = panelInfo.Controls[3] as Label;
//nombre producto
string textoLbnombre = lbnombre.Text;
string[] cadenas = textoLbnombre.Split(':');
string productoNombre = cadenas[1].Remove(0, 1);
//precio
string textoLbPrecio = lbPrecio.Text;
cadenas = textoLbPrecio.Split('$');
string precioProducto = cadenas[1];
//cantidad
string textoLbCantidad = Lbcantidad.Text;
cadenas = textoLbCantidad.Split(':');
string cantidadProducto = cadenas[1].Remove(0, 1);
//total
string textoLbTotal = Lbtotal.Text;
cadenas = textoLbTotal.Split('$');
string totalProducto = cadenas[1];
resultado += $"{(productoNombre.Length > 10 ? $"{productoNombre}\n" : $"{productoNombre}")} | ${precioProducto} | {cantidadProducto}\n";
}
return resultado;
}
private void ImprimirContenidoEnvio(PrintPageEventArgs e, int paginasImpresas)
{
string responsable = envio.responsable;
// Contenido que deseas imprimir
string contenido = " Mini Súper\n" +
" Compa BETO\n" +
" Saturnino Peña #44\n" +
" Lomas de la Laguna\n" +
" 311 169 2829\n" +
$" Fecha: {DateTime.Now.ToString("dd/MM/yyyy")}\n" +
$" Hora: {DateTime.Now.ToString("hh:mm:ss")}\n" +
$" Atendió:\n " +
$" {responsable}\n" +
$" ENVIO:\n " +
$" EN CAMINO:\n " +
"Producto | Precio | Cantidad\n" +
"------------------------------------\n";
contenido += crearContenidoImprimir();
contenido += $"------------------------------------\n" +
$" Total: ${envio.total + 10}\n" +
"------------------------------------\n" +
" Mini Súper\n" +
" Compa BETO\n" +
" ¡Gracias por su compra!\n" +
"------------------------------------\n" +
" Detalle envío:\n" +
$" Cliente: {txtCliente.Text}\n" +
$" Dirección: {AjustarDireccion(txtDireccion.Text)}\n" +//tengo duda aquí
$" Teléfono: {txtTelefono.Text}";
// Configuración de la fuente y del área de impresión
Font fuente = new Font("Arial", 10); // Ajustar el tamaño de fuente según sea necesario
PointF posicion = new PointF(0, e.MarginBounds.Top);
// Imprimir el contenido
e.Graphics.DrawString(contenido, fuente, Brushes.Black, posicion);
}
private string AjustarDireccion(string direccion)
{
const int maxCaracteresPorLinea = 8;
// Verifica la longitud de la dirección
if (direccion.Length > maxCaracteresPorLinea)
{
StringBuilder direccionFormateada = new StringBuilder();
for (int i = 0; i < direccion.Length; i += maxCaracteresPorLinea)
{
int caracteresRestantes = direccion.Length - i;
int caracteresEnEstaLinea = Math.Min(caracteresRestantes, maxCaracteresPorLinea);
direccionFormateada.AppendLine(direccion.Substring(i, caracteresEnEstaLinea));
}
return direccionFormateada.ToString();
}
return direccion; // Devuelve la dirección sin cambios si no es necesario ajustarla
}
private void ImprimirTicketEnvio()
{
using (PrintDocument documentoImpresion = new PrintDocument())
{
int paginasImpresas = 0;
documentoImpresion.PrintPage += (sender, e) =>
{
ImprimirContenidoEnvio(e, paginasImpresas);
paginasImpresas++;
e.HasMorePages = paginasImpresas < 2; // Indica que hay más páginas por imprimir
};
using (PrintDialog printDialog = new PrintDialog())
{
printDialog.Document = documentoImpresion;
if (printDialog.ShowDialog() == DialogResult.OK)
{
documentoImpresion.Print();
}
}
}
}
private void btnCancelar_Click(object sender, EventArgs e)
{
Close();
}
private void datosEnvio_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Close();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
envio.cliente = txtCliente.Text;
envio.direccion = txtDireccion.Text;
envio.telefono = txtTelefono.Text;
if (service.crearEnvio(envio, detallesEnvio, importes))
{
MessageBox.Show("Envio hecho");
ImprimirTicketEnvio();
ventanaPadre.eliminarProductosTocket();
ventanaPadre.llenarLayout();
ventanaPadre.total = 0;
this.Close();
}
}
private void txtTelefono_TextChanged(object sender, EventArgs e)
{
}
private void txtTelefono_KeyPress(object sender, KeyPressEventArgs e)
{
// Verificar si la tecla presionada no es un número o la tecla de retroceso
if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
{
e.Handled = true; // Ignorar la tecla presionada
// Mostrar un mensaje indicando que solo se permiten números
MessageBox.Show("Por favor, ingrese solo números en el teléfono.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}