Está en la página 1de 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Scripting.Hosting;
using System.Windows.Forms;
using System.IO;
using IronPython.Hosting;

namespace SistemaInventario
{
public partial class F4_Venta : Form
{
public F4_Venta()
{
InitializeComponent();
}

private void pb_atras_Click(object sender, EventArgs e)


{
F2_Menu men = new F2_Menu();
men.Show();
this.Hide();
}

private void F4_Venta_Load(object sender, EventArgs e)


{

private void pb_venderProd_Click(object sender, EventArgs e)


{
string content = "";

List<string> codigo = new List<string>();


List<string> nombre = new List<string>();
List<string> precio = new List<string>();
List<string> cantidad = new List<string>();

// Verificación de casilleros vacíos

if (string.IsNullOrEmpty(txb_precVenta.Text) ||
string.IsNullOrEmpty(txb_cant.Text) || string.IsNullOrEmpty(txb_codprod.Text))
{
MessageBox.Show("Inválido. Revise que todas las casillas se
encuentren con información (no vacías)", "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
try
{
StreamReader leer = new StreamReader("productos.txt");
// StreamWriter escribir = new StreamWriter("productos.txt");
string cod = txb_codprod.Text;
string[] arreglos = new string[3]; // Crea arreglos (secciones)
char[] separador = { '-' }; // Crea el separador
string cadena = leer.ReadLine();
bool autorizado = false;

while (cadena != null && autorizado == false)


{
arreglos = cadena.Split(separador);

codigo.Add(arreglos[0].Trim());
nombre.Add(arreglos[1].Trim());
precio.Add(arreglos[2].Trim());

if (arreglos[0].Trim().Equals(cod))
{
// Ejecutar venta
//leer.Close();
autorizado = true;

// Quitar los que se ha vendido

// CONTROLAR QUE LOS PRODUCTOS NO SEAN NEGATIVOS AL


VENDER
int quitar = int.Parse(txb_cant.Text);
int antes = int.Parse(arreglos[3].Trim());
int ahora = antes - quitar;
// 1. Ahora convertir a string y guardar
// 2. arreglos[3] = string de 1

// Unir Python y C# para sobreescribir


if (ahora < 0)
{
MessageBox.Show("No existe la cantidad de productos
suficientes para ejecutar esa
venta","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
break;
}
else
{
// Ejecución de venta

// Reporte en txt
string precV = txb_precVenta.Text;
string fecha = DateTime.UtcNow.ToString("dd-MM-
yyyy");
StreamWriter escr = new
StreamWriter("fechas.txt",true);
// dia-mes-año-nombreProducto-PrecioVenta-
CantidadVendida
escr.WriteLine(fecha+"-"+arreglos[1].Trim()
+"-"+precV+"-"+quitar);

string a0 = arreglos[0].Trim(); // Código


string a1 = arreglos[1].Trim(); // Nombre
string a2 = arreglos[2].Trim(); // Costo
// Cantidad

cantidad.Add(Convert.ToString(ahora));
cadena = leer.ReadLine();
//StreamWriter esc = new
StreamWriter("productos.txt",false);
//esc.WriteLine(a0+"-"+a1+"-"+a2+"-"+ahora);
////esc.WriteLine(content);
//esc.Close();
//MessageBox.Show("Venta hecha");

//StreamWriter esct = new


StreamWriter("productos.txt", true);
//esct.WriteLine(content);
//esct.Close();
////MessageBox.Show(content);
//MessageBox.Show("Hecho.");
}
}
else
{
cantidad.Add(arreglos[3].Trim());
//content += cadena+ "\n";
cadena = leer.ReadLine();
}
}

if (autorizado == false)
{
MessageBox.Show("Error. No se encontró el producto",
"CÓDIGO INCORRECTO", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
leer.Close();
StreamWriter esc = new StreamWriter("productos.txt",false);

for (int i = 0; i < codigo.Count(); i++)


{
for (i = 0; i < nombre.Count(); i++)
{
for (i = 0; i < precio.Count(); i++)
{
for (i = 0; i < cantidad.Count(); i++)
{
esc.WriteLine(codigo[i]+"-"+nombre[i]
+"-"+precio[i]+"-"+cantidad[i]+ "\n");
MessageBox.Show(codigo[i] + "-" + nombre[i]
+ "-" + precio[i] + "-" + cantidad[i] + "\n");
}
}
}
}
esc.Close();
MessageBox.Show("Ya");
}
leer.Close();
}
catch (Exception error)
{
MessageBox.Show("Error: " + error);
}
}
}

private void pb_verProd_Click(object sender, EventArgs e)


{
// Ver Producto con Mbox
StreamReader leer = new StreamReader("productos.txt");
string todo = leer.ReadToEnd();

MessageBox.Show(todo,"PRODUCTOS",MessageBoxButtons.OK,MessageBoxIcon.Information);
leer.Close();
}

}
}

También podría gustarte