inserta cuenta

This commit is contained in:
carlos
2024-01-22 00:57:40 -06:00
17 changed files with 328 additions and 73 deletions
+91
View File
@@ -905,6 +905,90 @@ namespace compabetov2.database
}
}
public static SQLiteDataReader conseguirCuentaDAO()
{
SQLiteDataReader reader = null;
var db_conexion = new Conexion();
try
{
SQLiteConnection conexion = db_conexion.get_connection();
conexion.Open();
string sql = "SELECT * FROM cuenta";
SQLiteCommand command = new SQLiteCommand(sql, conexion);
reader = command.ExecuteReader();
return reader;
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
return reader;
}
}
public static bool crearCuentaDAO(Cuenta cuenta, List<DetalleCuentaModel> detalleCuentas)
{
var db_conexion = new Conexion();
try
{
using (SQLiteConnection conexion = db_conexion.get_connection())
{
conexion.Open();
using (var transaccion = conexion.BeginTransaction())
{
try
{
string sqlCuenta = "INSERT INTO cuenta (total, fecha, estado, descripcion) VALUES(@total, @fecha, 'ACTIVO', @descripcion);";
string sqlDetalle = "INSERT INTO detalle_cuenta (fk_cuenta, fk_producto, cantidad) VALUES(@fk_cuenta, @fk_producto, @cantidad);";
string sqlIdCuenta = "SELECT idCuenta FROM cuenta ORDER BY idCuenta DESC LIMIT 1";
SQLiteCommand commandCuenta = new SQLiteCommand(sqlCuenta, conexion);
commandCuenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString());
commandCuenta.Parameters.AddWithValue("@total", cuenta.total);
commandCuenta.Parameters.AddWithValue("@descripcion", cuenta.descripcion);
commandCuenta.ExecuteNonQuery();
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdCuenta, conexion);
SQLiteDataReader reader = commandConsulta.ExecuteReader();
long idCuenta = -1;
while (reader.Read())
{
idCuenta = reader.GetInt64(0);
}
foreach (DetalleCuentaModel detalleCuenta in detalleCuentas)
{
SQLiteCommand commandDetalle = new SQLiteCommand(sqlDetalle, conexion);
commandDetalle.Parameters.AddWithValue("@fk_cuenta", idCuenta);
commandDetalle.Parameters.AddWithValue("@fk_producto", detalleCuenta.producto.idProducto);
commandDetalle.Parameters.AddWithValue("@cantidad", detalleCuenta.cantidad);
commandDetalle.ExecuteNonQuery();
}
transaccion.Commit();
return true;
}
catch (SQLiteException ex)
{
transaccion.Rollback();
MessageBox.Show(ex.Message);
return false;
}
}
}
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
private static void crearDB()
{
@@ -923,6 +1007,8 @@ namespace compabetov2.database
"DROP TABLE IF EXISTS Cliente",
"DROP TABLE IF EXISTS Merma",
"DROP TABLE IF EXISTS venta",
"DROP TABLE IF EXISTS detalle_cuenta",
"DROP TABLE IF EXISTS cuenta",
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT NOT NULL, usuario TEXT NOT NULL, "+
"contra TEXT NOT NULL)",
@@ -949,6 +1035,11 @@ namespace compabetov2.database
"CREATE TABLE detalle_venta (fk_venta INTEGER, fk_producto INTEGER, PRIMARY KEY (fk_venta, fk_producto), " +
"FOREIGN KEY (fk_venta) REFERENCES venta(idVenta),FOREIGN KEY (fk_producto) REFERENCES producto(idproducto));",
"CREATE TABLE cuenta (idCuenta INTEGER PRIMARY KEY AUTOINCREMENT, total DECIMAL(10,2), fecha TEXT, estado TEXT DEFAULT 'ACTIVO', descripcion TEXT)",
"CREATE TABLE detalle_cuenta (fk_cuenta INTEGER, fk_producto INTEGER,cantidad INTEGER, PRIMARY KEY (fk_cuenta, fk_producto), " +
"FOREIGN KEY (fk_cuenta) REFERENCES Cuenta(idCuenta),FOREIGN KEY (fk_producto) REFERENCES producto(idproducto))",
"INSERT INTO Login ( tipo, usuario, contra) VALUES('admin', 'admin', '12345');",
"INSERT INTO Empleado ( nombre, apellidos, calle, fecha_contratacion,estado, colonia, cp, no_ext,no_int, referencia, fecha_nac, telefono,fk_idLogin) VALUES('jefe', '', '', '', '', '', '', '', '', '', '', '',1);",
+26
View File
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace compabetov2.database.modelos
{
public class Cuenta
{
public int id;
public decimal total;
public string fecha;
public string estado;
public string descripcion;
public Cuenta(decimal total = 0, string fecha = "", string estado = "", string descripcion = "", int id = -1) {
this.id = id;
this.total = total;
this.fecha = fecha;
this.estado = estado;
this.descripcion = descripcion;
}
}
}
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace compabetov2.database.modelos
{
public class DetalleCuentaModel
{
public int idCuenta;
public Producto producto;
public int cantidad;
public DetalleCuentaModel(Producto producto, int cantidad, int idCuenta =-1)
{
this.idCuenta = idCuenta;
this.producto = producto;
this.cantidad = cantidad;
}
}
}
+30
View File
@@ -222,5 +222,35 @@ namespace compabetov2.database
{
return DAO.actualizarMermaDAO(merma);
}
public static bool crearCuenta(Cuenta cuenta, List<DetalleCuentaModel> detalleCuentas)
{
return DAO.crearCuentaDAO(cuenta, detalleCuentas);
}
public static List<Cuenta> conseguirCuentaDAO()
{
List<Cuenta> cuentas = new List<Cuenta>();
SQLiteDataReader reader = DAO.conseguirCuentaDAO();
while(reader.Read())
{
long idCuenta = reader.GetInt64(0);
long total = reader.GetInt64(1);
string fecha = reader["fecha"].ToString();
string estado = reader["estado"].ToString();
string descripcion = reader["descripcion"].ToString();
Cuenta cuenta = new Cuenta(
(int) total,
fecha,
estado,
descripcion,
(int)idCuenta
);
cuentas.Add(cuenta);
}
return cuentas;
}
}
}