2064 lines
88 KiB
C#
2064 lines
88 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data.SQLite;
|
|
using System.Diagnostics.Contracts;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using compabetov2.admin;
|
|
using compabetov2.database;
|
|
using compabetov2.database.modelos;
|
|
|
|
namespace compabetov2.database
|
|
{
|
|
internal class DAO
|
|
{
|
|
public static bool iniciarSesionDAO(String usuario, String contra)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT usuario, contra FROM Login WHERE usuario = @param0";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue($"@param{0}", usuario);
|
|
SQLiteDataReader reader = command.ExecuteReader();
|
|
|
|
if (reader.HasRows)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
if (reader["contra"].Equals(contra))
|
|
{
|
|
conexion.Close();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
}
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
catch (SQLiteException ex)
|
|
{
|
|
if (ex.Message.Contains("no such table"))
|
|
{
|
|
crearDB();
|
|
MessageBox.Show("La base de datos fue creada vuelva a intentar");
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirInicioSesionDAO(string usuario)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT usuario, tipo FROM Login WHERE usuario = @param0";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue($"@param{0}", usuario);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool verificarExistenciaLoginDAO(LoginModel loginModel)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Login WHERE usuario = @usuario";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@usuario", loginModel.usuario);
|
|
reader = command.ExecuteReader();
|
|
|
|
if (reader.Read())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static bool crearLoginDAO(LoginModel login, int idempleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlLogin = "INSERT INTO Login (tipo, usuario, contra) VALUES(@tipo, @usuario, @contra);";
|
|
string sqlEmpleado = "UPDATE Empleado SET fk_idLogin = @fklogin WHERE idempleado = @idempleado";
|
|
string sqlConsulta = "SELECT idlogin FROM Login ORDER BY idlogin DESC LIMIT 1";
|
|
|
|
|
|
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
|
|
commandLogin.Parameters.AddWithValue("@tipo", login.tipo);
|
|
commandLogin.Parameters.AddWithValue("@usuario", login.usuario);
|
|
commandLogin.Parameters.AddWithValue("@contra", login.contra);
|
|
commandLogin.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlConsulta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long fkLogin = -1;
|
|
while (reader.Read())
|
|
{
|
|
fkLogin = reader.GetInt64(0);
|
|
}
|
|
|
|
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlEmpleado, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@fklogin", fkLogin);
|
|
commandEmpleado.Parameters.AddWithValue("@idempleado", idempleado);
|
|
commandEmpleado.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;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarLoginDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sql = "UPDATE Empleado SET fk_idLogin = -1 WHERE idempleado = @idempleado";
|
|
string sqlLogin = "DELETE FROM Login WHERE idlogin = @idlogin";
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
|
|
|
|
|
|
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
|
|
commandLogin.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
|
|
|
|
command.ExecuteNonQuery();
|
|
commandLogin.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;
|
|
}
|
|
}
|
|
|
|
public static bool modificarContraDAO(Empleado empleado, string contra)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE Login SET contra = @contra WHERE idlogin = @idlogin";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@contra", contra);
|
|
command.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool insertarEmpleadoDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Empleado (nombre, apellidos, calle, fecha_contratacion, estado, colonia, cp, no_ext, no_int, referencia, fecha_nac, telefono,img,fk_idLogin) " +
|
|
"VALUES (@nombre, @apellidos, @calle, @fecha_contratacion, @estado, @colonia, @cp, @no_ext, @no_int, @referencia, @fecha_nac, @telefono, @img, @fk_idLogin)";
|
|
|
|
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", empleado.nombre);
|
|
command.Parameters.AddWithValue("@apellidos", empleado.apellidos);
|
|
command.Parameters.AddWithValue("@calle", empleado.calle);
|
|
command.Parameters.AddWithValue("@fecha_contratacion", empleado.fecha_contratacion);
|
|
command.Parameters.AddWithValue("@estado", empleado.estado);
|
|
command.Parameters.AddWithValue("@colonia", empleado.colonia);
|
|
command.Parameters.AddWithValue("@cp", empleado.cp);
|
|
command.Parameters.AddWithValue("@no_ext", empleado.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", empleado.no_int);
|
|
command.Parameters.AddWithValue("@referencia", empleado.referencia);
|
|
command.Parameters.AddWithValue("@fecha_nac", empleado.fecha_nac);
|
|
command.Parameters.AddWithValue("@telefono", empleado.telefono);
|
|
command.Parameters.AddWithValue("@img", empleado.img);
|
|
command.Parameters.AddWithValue("@fk_idLogin", empleado.fkIdLogin);
|
|
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirEmpleadosDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Empleado WHERE idempleado > 1";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool actualizarEmpleadoDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE Empleado SET nombre = @nombre, apellidos = @apellidos, calle = @calle, fecha_contratacion = @fecha_contratacion, " +
|
|
"estado = @estado, colonia = @colonia, cp = @cp, no_ext = @no_ext, no_int = @no_int, referencia = @referencia, " +
|
|
"fecha_nac = @fecha_nac, telefono = @telefono WHERE idempleado = @idempleado;";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", empleado.nombre);
|
|
command.Parameters.AddWithValue("@apellidos", empleado.apellidos);
|
|
command.Parameters.AddWithValue("@calle", empleado.calle);
|
|
command.Parameters.AddWithValue("@fecha_contratacion", empleado.fecha_contratacion);
|
|
command.Parameters.AddWithValue("@estado", empleado.estado);
|
|
command.Parameters.AddWithValue("@colonia", empleado.colonia);
|
|
command.Parameters.AddWithValue("@cp", empleado.cp);
|
|
command.Parameters.AddWithValue("@no_ext", empleado.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", empleado.no_int);
|
|
command.Parameters.AddWithValue("@referencia", empleado.referencia);
|
|
command.Parameters.AddWithValue("@fecha_nac", empleado.fecha_nac);
|
|
command.Parameters.AddWithValue("@telefono", empleado.telefono);
|
|
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
|
|
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarEmpleadoDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sql = "DELETE FROM Empleado WHERE idempleado = @idempleado";
|
|
string sqlLogin = "DELETE FROM Login WHERE idlogin = @idlogin";
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
|
|
|
|
|
|
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
|
|
commandLogin.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
|
|
|
|
command.ExecuteNonQuery();
|
|
commandLogin.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;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirProductosDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Producto";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool insertarProductoDAO(Producto producto)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Producto (nombre, descripcion, precio,img, stock, fk_idcategoria) " +
|
|
"VALUES (@nombre, @descripcion, @precio,@img , @stock, @idcategoria)";
|
|
|
|
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", producto.nombre);
|
|
command.Parameters.AddWithValue("@descripcion", producto.descripcion);
|
|
command.Parameters.AddWithValue("@precio", producto.precion);
|
|
command.Parameters.AddWithValue("@img", producto.img);
|
|
command.Parameters.AddWithValue("@stock", producto.stock);
|
|
command.Parameters.AddWithValue("@idcategoria", 1);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool editarProductoDAO(Producto producto)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE Producto SET nombre = @nombre, precio = @precio, img = @img WHERE idproducto = @idproducto;";
|
|
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", producto.nombre);
|
|
command.Parameters.AddWithValue("@precio", producto.precion);
|
|
command.Parameters.AddWithValue("@img", producto.img);
|
|
command.Parameters.AddWithValue("@idproducto", producto.idProducto);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarProductoDAO(Producto producto)
|
|
{
|
|
|
|
var db_conexion = new Conexion();
|
|
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sql = "DELETE FROM Producto WHERE idproducto = @idproducto";
|
|
string sqlVariante = "DELETE FROM variantes WHERE fk_producto=@fk_producto;";
|
|
|
|
SQLiteCommand commandProducto = new SQLiteCommand(sql, conexion);
|
|
commandProducto.Parameters.AddWithValue("@idproducto", producto.idProducto);
|
|
int filasAfectadas = commandProducto.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandVariante = new SQLiteCommand(sqlVariante, conexion);
|
|
commandVariante.Parameters.AddWithValue("@fk_producto", producto.idProducto);
|
|
commandVariante.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;
|
|
}
|
|
}
|
|
|
|
public static bool aumentarStockDAO(List<Producto> stockNuevo)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in stockNuevo)
|
|
{
|
|
string sql = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto;";
|
|
string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto";
|
|
|
|
SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion);
|
|
commandProdcutoStock.Parameters.AddWithValue("@idproducto", item.idProducto);
|
|
SQLiteDataReader stockReader = commandProdcutoStock.ExecuteReader();
|
|
|
|
int stockActual = -1;
|
|
while (stockReader.Read())
|
|
{
|
|
stockActual = int.Parse(stockReader["stock"].ToString());
|
|
|
|
}
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idproducto", item.idProducto);
|
|
command.Parameters.AddWithValue("@stock", stockActual + item.stock);
|
|
command.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;
|
|
}
|
|
}
|
|
|
|
|
|
public static bool insertarClienteDAO(Cliente cliente)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Cliente (nombres, apellidos, calle, colonia, cp, no_ext, no_int, referencia, telefono, telefono_extra) " +
|
|
"VALUES (@nombres, @apellidos, @calle, @colonia, @cp, @no_ext, @no_int, @referencia, @telefono, @telefono_extra)";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", cliente.nombres);
|
|
command.Parameters.AddWithValue("@apellidios", cliente.apellidos);
|
|
command.Parameters.AddWithValue("@calle", cliente.calle);
|
|
command.Parameters.AddWithValue("@colonia", cliente.colonia);
|
|
command.Parameters.AddWithValue("@cp", cliente.cp);
|
|
command.Parameters.AddWithValue("@no_ext", cliente.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", cliente.no_int);
|
|
command.Parameters.AddWithValue("@referencia", cliente.referencia);
|
|
command.Parameters.AddWithValue("@telefono", cliente.telefono);
|
|
command.Parameters.AddWithValue("@telefono_extra", cliente.telefono_extra);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarClienteDAO(Cliente cliente)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "DELETE FROM Cliente WHERE idCliente = @idCliente";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@idCliente", cliente.idCliente);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirClientesDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Cliente";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
private static string guardarImg(String rutaImagen)
|
|
{
|
|
//consigue la carpeta del proyectro
|
|
string carpetaImagenes = Path.Combine(Application.StartupPath, "produtos_img");
|
|
|
|
//crear el directorio sino existe
|
|
if (!Directory.Exists(carpetaImagenes))
|
|
{
|
|
Directory.CreateDirectory(carpetaImagenes);
|
|
}
|
|
|
|
string nombreArchivo = Path.GetFileName(rutaImagen);
|
|
string rutaDestino = Path.Combine(carpetaImagenes, nombreArchivo);
|
|
File.Copy(rutaImagen, rutaDestino, true);
|
|
|
|
return rutaDestino;
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirMermaDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Merma";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool insertarMermaDAO(Merma merma)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Merma (fecha, responsable, razon) VALUES(@fecha, @responsable, @razon);";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@fecha", merma.fecha);
|
|
command.Parameters.AddWithValue("@responsable", merma.responsable);
|
|
command.Parameters.AddWithValue("@razon", merma.razon);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool crearVentaDAO(VentaModel venta, List<DetalleVentaModel> detalleVentas, List<DetalleImporte> importes)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlVenta = "INSERT INTO venta (fecha, total,responzable) VALUES(@fecha, @total,@responzable);";
|
|
string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_variante, cantidad,precioCompra,precioVenta) VALUES(@idVenta, @fk_variante, @cantidada,@precioCompra,@precioVenta);";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto";
|
|
string sqlProductoUpdate = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto";
|
|
string sqlImporte = "INSERT INTO detalle_importe (fk_importe,cantidad,precio,fk_venta) VALUES (@fk_importe,@cantidad,@precio,@fk_venta)";
|
|
|
|
// CREACION DE LA VENTA
|
|
SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion);
|
|
commandVenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString());
|
|
commandVenta.Parameters.AddWithValue("@total", venta.total);
|
|
commandVenta.Parameters.AddWithValue("@responzable", venta.responzable);
|
|
commandVenta.ExecuteNonQuery();
|
|
|
|
//CONSEGUIR EL ID DE LA VENTA PARA RELACIONARLO CON DETALLE VENTA
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdVenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idVenta = -1;
|
|
while (reader.Read())
|
|
{
|
|
idVenta = reader.GetInt64(0);
|
|
}
|
|
|
|
// CREACION DE DETALLE VENTA
|
|
foreach (DetalleVentaModel detalleVenta in detalleVentas)
|
|
{
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta);
|
|
commandEmpleado.Parameters.AddWithValue("@fk_variante", detalleVenta.producto.idVariante);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleVenta.cantidad);
|
|
commandEmpleado.Parameters.AddWithValue("@precioCompra", detalleVenta.producto.precioCompra);
|
|
commandEmpleado.Parameters.AddWithValue("@precioVenta", detalleVenta.producto.precio);
|
|
commandEmpleado.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion);
|
|
commandProdcutoStock.Parameters.AddWithValue("@idproducto", detalleVenta.producto.fkProducto);
|
|
SQLiteDataReader stockReader = commandProdcutoStock.ExecuteReader();
|
|
|
|
int stockActual = -1;
|
|
while (stockReader.Read())
|
|
{
|
|
stockActual = int.Parse(stockReader["stock"].ToString());
|
|
|
|
}
|
|
|
|
//ACTUALIZACION DE STOCK
|
|
SQLiteCommand commandProdcutoUpdate = new SQLiteCommand(sqlProductoUpdate, conexion);
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@stock", stockActual - (detalleVenta.cantidad * detalleVenta.producto.cantidad));
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleVenta.producto.fkProducto);
|
|
commandProdcutoUpdate.ExecuteNonQuery();
|
|
|
|
|
|
|
|
}
|
|
//creacion del importe
|
|
foreach (DetalleImporte importe in importes)
|
|
{
|
|
if (importe.cantidad != 0)
|
|
{
|
|
SQLiteCommand commandDetalleImporte = new SQLiteCommand(sqlImporte, conexion);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandDetalleImporte.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_venta", idVenta);
|
|
commandDetalleImporte.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;
|
|
}
|
|
}
|
|
|
|
public static bool devolverImporteDAO(VentaModel venta, List<DetalleImporte> importes)
|
|
{
|
|
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlVenta = "INSERT INTO venta (fecha, total,responzable) VALUES(@fecha, @total,@responzable);";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
string sqlImporte = "INSERT INTO detalle_importe (fk_importe,cantidad,precio,fk_venta) VALUES (@fk_importe,@cantidad,@precio,@fk_venta)";
|
|
|
|
// CREACION DE LA VENTA
|
|
SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion);
|
|
commandVenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString());
|
|
commandVenta.Parameters.AddWithValue("@total", venta.total);
|
|
commandVenta.Parameters.AddWithValue("@responzable", venta.responzable);
|
|
commandVenta.ExecuteNonQuery();
|
|
|
|
//CONSEGUIR EL ID DE LA VENTA PARA RELACIONARLO CON DETALLE VENTA
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdVenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idVenta = -1;
|
|
while (reader.Read())
|
|
{
|
|
idVenta = reader.GetInt64(0);
|
|
}
|
|
|
|
|
|
//creacion del importe
|
|
foreach (DetalleImporte importe in importes)
|
|
{
|
|
if (importe.cantidad != 0)
|
|
{
|
|
SQLiteCommand commandDetalleImporte = new SQLiteCommand(sqlImporte, conexion);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandDetalleImporte.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_venta", idVenta);
|
|
commandDetalleImporte.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;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarMermaDAO(Merma merma)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "DELETE FROM Merma WHERE idmerma = @idmerma;";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@idmerma", merma.idMerma);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool actualizarMermaDAO(Merma merma)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE Merma SET fecha = @fecha, responsable = @responsable, razon = @razon WHERE idmerma = @idmerma; ";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@fecha", merma.fecha);
|
|
command.Parameters.AddWithValue("@responsable", merma.responsable);
|
|
command.Parameters.AddWithValue("@razon", merma.razon);
|
|
command.Parameters.AddWithValue("@idmerma", merma.idMerma);
|
|
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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 ORDER BY idCuenta DESC";
|
|
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, List<DetalleImporte> importes)
|
|
{
|
|
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,cliente) VALUES(@total, @fecha, 'ACTIVO', @descripcion,@cliente);";
|
|
string sqlDetalle = "INSERT INTO detalle_cuenta (fk_cuenta, fk_variante, cantidad) VALUES(@fk_cuenta, @fk_variante, @cantidad);";
|
|
string sqlIdCuenta = "SELECT idCuenta FROM cuenta ORDER BY idCuenta DESC LIMIT 1";
|
|
string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto";
|
|
string sqlProductoUpdate = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto";
|
|
string sqlImporte = "INSERT INTO detalle_importe_cuenta (fk_importe, cantidad, precio, fk_cuenta) VALUES(@fk_importe, @cantidad, @precio, @fk_cuenta);";
|
|
|
|
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.Parameters.AddWithValue("@cliente", cuenta.cliente);
|
|
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_variante", detalleCuenta.producto.idVariante);
|
|
commandDetalle.Parameters.AddWithValue("@cantidad", detalleCuenta.cantidad);
|
|
commandDetalle.ExecuteNonQuery();
|
|
|
|
//modificar stock
|
|
SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion);
|
|
commandProdcutoStock.Parameters.AddWithValue("@idproducto", detalleCuenta.producto.fkProducto);
|
|
SQLiteDataReader stockReader = commandProdcutoStock.ExecuteReader();
|
|
|
|
int stockActual = -1;
|
|
while (stockReader.Read())
|
|
{
|
|
stockActual = int.Parse(stockReader["stock"].ToString());
|
|
|
|
}
|
|
|
|
SQLiteCommand commandProdcutoUpdate = new SQLiteCommand(sqlProductoUpdate, conexion);
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@stock", stockActual - (detalleCuenta.cantidad * detalleCuenta.producto.cantidad));
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleCuenta.producto.fkProducto);
|
|
commandProdcutoUpdate.ExecuteNonQuery();
|
|
}
|
|
|
|
foreach(DetalleImporte importe in importes)
|
|
{
|
|
if(importe.cantidad != 0) {
|
|
SQLiteCommand commandImportes = new SQLiteCommand(sqlImporte, conexion);
|
|
commandImportes.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandImportes.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandImportes.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandImportes.Parameters.AddWithValue("@fk_cuenta", idCuenta);
|
|
commandImportes.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;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirDetalleCuanteDAO(int idCuenta)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "Select v.idVariante , v.nombre ,v.cantidad ,v.fk_producto , dc.cantidad from detalle_cuenta dc, variantes v " +
|
|
"WHERE dc.fk_variante = v.idVariante and dc.fk_cuenta = @fk_cuenta;";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fk_cuenta", idCuenta);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirImportesCuentaDAO(int idCuenta)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM detalle_importe_cuenta WHERE fk_cuenta = @fk_cuenta;";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fk_cuenta", idCuenta);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool cancelarCuentaDAO(int idCuenta)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlCuenta = "UPDATE cuenta SET estado ='CANCELADO' WHERE idCuenta = @idCuenta;";
|
|
|
|
SQLiteCommand commandCuenta = new SQLiteCommand(sqlCuenta, conexion);
|
|
commandCuenta.Parameters.AddWithValue("@idCuenta", idCuenta);
|
|
commandCuenta.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;
|
|
}
|
|
}
|
|
|
|
public static bool saldarCuentaDAO(Cuenta cuenta, List<DetalleCuentaModel> detallesCuentas, List<DetalleImporteCuenta> importes)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlVenta = "INSERT INTO venta (fecha, total) VALUES(@fecha, @total);";
|
|
string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_variante, cantidad) VALUES(@idVenta, @fk_variante, @cantidada);";
|
|
string sqlCuenta = "UPDATE cuenta SET estado ='SALDADO' WHERE idCuenta = @idCuenta;";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
string sqlImporte = "INSERT INTO detalle_importe (fk_importe,cantidad,precio,fk_venta) VALUES (@fk_importe,@cantidad,@precio,@fk_venta)";
|
|
|
|
|
|
|
|
SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion);
|
|
commandVenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString());
|
|
commandVenta.Parameters.AddWithValue("@total", cuenta.total);
|
|
commandVenta.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandCuenta = new SQLiteCommand(sqlCuenta, conexion);
|
|
commandCuenta.Parameters.AddWithValue("@idCuenta", cuenta.id);
|
|
commandCuenta.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdVenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idVenta = -1;
|
|
while (reader.Read())
|
|
{
|
|
idVenta = reader.GetInt64(0);
|
|
}
|
|
foreach (DetalleCuentaModel detalleCuenta in detallesCuentas)
|
|
{
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta);
|
|
commandEmpleado.Parameters.AddWithValue("@fk_variante", detalleCuenta.producto.idVariante);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleCuenta.cantidad);
|
|
commandEmpleado.ExecuteNonQuery();
|
|
}
|
|
|
|
foreach (DetalleImporteCuenta importe in importes)
|
|
{
|
|
if (importe.cantidad != 0)
|
|
{
|
|
SQLiteCommand commandDetalleImporte = new SQLiteCommand(sqlImporte, conexion);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandDetalleImporte.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandDetalleImporte.Parameters.AddWithValue("@fk_venta", idVenta);
|
|
commandDetalleImporte.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;
|
|
}
|
|
}
|
|
|
|
|
|
public static SQLiteDataReader conseguirVentasMesDAO(DateTime fecha)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM venta WHERE fecha LIKE @fecha";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fecha", $"%{fecha.Month}/{fecha.Year}%");
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirEnvioMesDAO(DateTime fecha)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM envios WHERE fecha LIKE @fecha and estado = 'CONCLUIDO'";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fecha", $"%{fecha.Month}/{fecha.Year}%");
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirDetalleVenta(VentaModel venta)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "select * from detalle_venta dv, variantes v WHERE dv.fk_variante = v.idVariante and dv.fk_venta = @idVenta";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idVenta", venta.id);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool crearEnvio(Envio envio, List<DetalleEnvioModel> detallesEnvio, List<DetalleImporte> importes)
|
|
{
|
|
return DAO.crearEnvioDAO(envio, detallesEnvio, importes);
|
|
}
|
|
|
|
public static bool crearEnvioDAO(Envio envio, List<DetalleEnvioModel> detallesEnvio, List<DetalleImporte> importes)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlEnvio = "INSERT INTO envios (fecha, direccion, telefono,responzable,total,cliente) VALUES(@fecha,@direccion , @telefono, @responzable, @total,@cliente);";
|
|
string sqlDetalle = "INSERT INTO detalle_envios (fk_envios, fk_variante, cantidad,precioVenta,precioCompra) VALUES(@fk_envios, @fk_variante, @cantidad, @precioVenta,@precioCompra);";
|
|
string sqlIdCuenta = "SELECT idEnvios FROM envios ORDER BY idEnvios DESC LIMIT 1";
|
|
string sqlImporte = "INSERT INTO detalle_importe_envio (fk_importe, cantidad, precio, fk_envio) VALUES(@fk_importe, @cantidad, @precio, @fk_envio);";
|
|
|
|
SQLiteCommand commandCuenta = new SQLiteCommand(sqlEnvio, conexion);
|
|
commandCuenta.Parameters.AddWithValue("@fecha", envio.fecha);
|
|
commandCuenta.Parameters.AddWithValue("@direccion", envio.direccion);
|
|
commandCuenta.Parameters.AddWithValue("@telefono", envio.telefono);
|
|
commandCuenta.Parameters.AddWithValue("@cliente", envio.cliente);
|
|
commandCuenta.Parameters.AddWithValue("@responzable", envio.responsable);
|
|
commandCuenta.Parameters.AddWithValue("@total", envio.total);
|
|
commandCuenta.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdCuenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idEnvio = -1;
|
|
while (reader.Read())
|
|
{
|
|
idEnvio = reader.GetInt64(0);
|
|
}
|
|
|
|
foreach (DetalleEnvioModel detalleEnvio in detallesEnvio)
|
|
{
|
|
SQLiteCommand commandDetalle = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandDetalle.Parameters.AddWithValue("@fk_envios", idEnvio);
|
|
commandDetalle.Parameters.AddWithValue("@fk_variante", detalleEnvio.producto.idVariante);
|
|
commandDetalle.Parameters.AddWithValue("@cantidad", detalleEnvio.cantidad);
|
|
commandDetalle.Parameters.AddWithValue("@precioVenta", detalleEnvio.producto.precio);
|
|
commandDetalle.Parameters.AddWithValue("@precioCompra", detalleEnvio.producto.precioCompra);
|
|
commandDetalle.ExecuteNonQuery();
|
|
}
|
|
|
|
//creacion de importes
|
|
foreach(DetalleImporte importe in importes)
|
|
{
|
|
if(importe.cantidad != 0) {
|
|
SQLiteCommand commandImportes = new SQLiteCommand(sqlImporte, conexion);
|
|
commandImportes.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandImportes.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandImportes.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandImportes.Parameters.AddWithValue("@fk_envio", idEnvio);
|
|
commandImportes.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;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirEnvioDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM envios ORDER BY idEnvios DESC limit 30";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirImporteEnvio(int idEnvio)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM detalle_importe_envio where fk_envio = @idEnvio";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idEnvio", idEnvio);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirDetalleEnvioDAO(int idEnvio)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "Select v.idVariante, v.nombre, v.cantidad, v.fk_producto, de.cantidad from detalle_envios de,variantes v " +
|
|
"WHERE de.fk_variante = v.idVariante and de.fk_envios = @fk_envios;";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fk_envios", idEnvio);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool editarEnvioDAO(Envio envio)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlCuenta = "UPDATE envios SET direccion = @direccion, telefono = @telefono, cliente = @cliente WHERE idEnvios = @idEnvios;";
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sqlCuenta, conexion);
|
|
command.Parameters.AddWithValue("@idEnvios", envio.id);
|
|
command.Parameters.AddWithValue("@direccion", envio.direccion);
|
|
command.Parameters.AddWithValue("@telefono", envio.telefono);
|
|
command.Parameters.AddWithValue("@cliente", envio.cliente);
|
|
command.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;
|
|
}
|
|
}
|
|
|
|
public static bool cancelarEnvioDAO(Envio envio, List<DetalleEnvioModel> detalles)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlCuenta = "UPDATE envios SET estado = @estado WHERE idEnvios = @idEnvios;";
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sqlCuenta, conexion);
|
|
command.Parameters.AddWithValue("@idEnvios", envio.id);
|
|
command.Parameters.AddWithValue("@estado", "CANCELADO");
|
|
command.ExecuteNonQuery();
|
|
|
|
/* foreach (DetalleEnvioModel detalleEnvio in detalles)
|
|
{
|
|
//actualizar stock
|
|
SQLiteCommand commandStock = new SQLiteCommand(sqlStock, conexion);
|
|
commandStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto);
|
|
SQLiteDataReader readerStock = commandStock.ExecuteReader();
|
|
long stock = 0;
|
|
while (readerStock.Read()) stock = readerStock.GetInt64(0);
|
|
|
|
SQLiteCommand commandActualizarStock = new SQLiteCommand(sqlActualizarStock, conexion);
|
|
commandActualizarStock.Parameters.AddWithValue("@stock", stock - (detalleEnvio.cantidad * detalleEnvio.producto.cantidad));
|
|
commandActualizarStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto);
|
|
commandActualizarStock.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;
|
|
}
|
|
}
|
|
public static bool llenarTablaVariantes()
|
|
{
|
|
List<Producto> productos = service.conseguirProductos();
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sql = "INSERT INTO variantes(idVariante, nombre, precio, cantidad, img, fk_producto)" +
|
|
"VALUES(@idVariante, @nombre, @precio, @cantidad, @img, @fk_producto);";
|
|
|
|
for(int i = 0; i<productos.Count; i++)
|
|
{
|
|
SQLiteCommand commandStock = new SQLiteCommand(sql, conexion);
|
|
commandStock.Parameters.AddWithValue("@idVariante", i+1);
|
|
commandStock.Parameters.AddWithValue("@nombre", productos[i].nombre);
|
|
commandStock.Parameters.AddWithValue("@precio", productos[i].precion);
|
|
commandStock.Parameters.AddWithValue("@cantidad", 1);
|
|
commandStock.Parameters.AddWithValue("@img", productos[i].img);
|
|
commandStock.Parameters.AddWithValue("@fk_producto", productos[i].idProducto);
|
|
commandStock.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;
|
|
}
|
|
|
|
}
|
|
|
|
public static bool concluirEnvioDAO(Envio envio, List<DetalleEnvioModel> detallesEnvio, List<DetalleImporteEnvio> importes)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlVenta = "INSERT INTO venta (fecha, total,responzable) VALUES(@fecha, @total,@responzable);";
|
|
string sqlDetalle = "INSERT INTO detalle_venta (fk_venta, fk_variante, cantidad) VALUES(@idVenta, @fk_variante, @cantidada);";
|
|
string sqlEnvio = "UPDATE envios SET estado ='CONCLUIDO' WHERE idEnvios = @idEnvios;";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
string sqlStock = "select stock from Producto p where idproducto = @idProducto";
|
|
string sqlActualizarStock = "UPDATE Producto set stock = @stock WHERE idproducto = @idProducto";
|
|
string sqlImporte = "INSERT INTO detalle_importe (fk_importe, cantidad, precio, fk_venta) VALUES(@fk_importe, @cantidad, @precio, @fk_venta);";
|
|
|
|
SQLiteCommand commandVenta = new SQLiteCommand(sqlVenta, conexion);
|
|
commandVenta.Parameters.AddWithValue("@fecha", DateTime.Now.ToString());
|
|
commandVenta.Parameters.AddWithValue("@total", envio.total + 10);
|
|
commandVenta.Parameters.AddWithValue("@responzable", envio.responsable);
|
|
commandVenta.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandEnvio = new SQLiteCommand(sqlEnvio, conexion);
|
|
commandEnvio.Parameters.AddWithValue("@idEnvios", envio.id);
|
|
commandEnvio.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdVenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idVenta = -1;
|
|
while (reader.Read()) idVenta = reader.GetInt64(0);
|
|
|
|
foreach (DetalleEnvioModel detalleEnvio in detallesEnvio)
|
|
{
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta);
|
|
commandEmpleado.Parameters.AddWithValue("@fk_variante", detalleEnvio.producto.idVariante);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleEnvio.cantidad);
|
|
commandEmpleado.ExecuteNonQuery();
|
|
|
|
//actualizar stock
|
|
SQLiteCommand commandStock = new SQLiteCommand(sqlStock, conexion);
|
|
commandStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto);
|
|
SQLiteDataReader readerStock = commandStock.ExecuteReader();
|
|
long stock = 0;
|
|
while (readerStock.Read()) stock = readerStock.GetInt64(0);
|
|
|
|
SQLiteCommand commandActualizarStock = new SQLiteCommand(sqlActualizarStock, conexion);
|
|
commandActualizarStock.Parameters.AddWithValue("@stock", stock - (detalleEnvio.cantidad * detalleEnvio.producto.cantidad));
|
|
commandActualizarStock.Parameters.AddWithValue("@idProducto", detalleEnvio.producto.fkProducto);
|
|
commandActualizarStock.ExecuteNonQuery();
|
|
}
|
|
|
|
foreach(DetalleImporteEnvio importe in importes)
|
|
{
|
|
if (importe.cantidad != 0)
|
|
{
|
|
SQLiteCommand commandImporte = new SQLiteCommand(sqlImporte, conexion);
|
|
commandImporte.Parameters.AddWithValue("@fk_importe", importe.fkImporte);
|
|
commandImporte.Parameters.AddWithValue("@cantidad", importe.cantidad);
|
|
commandImporte.Parameters.AddWithValue("@precio", importe.precioImporte);
|
|
commandImporte.Parameters.AddWithValue("@fk_venta", idVenta);
|
|
commandImporte.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;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirProductosFiltradosDAO(string nombreProducto)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM variantes WHERE nombre LIKE @nombre";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@nombre", $"%{nombreProducto}%");
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirVariantesDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM variantes";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirVarianteDAO(int idProducto)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM variantes WHERE fk_producto = @fk_producto";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fk_producto", idProducto);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool insertarVarianteDAO(Variante variante)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO variantes (nombre, precio, cantidad, img, fk_producto, precioCompra) " +
|
|
"VALUES(@nombre, @precio, @cantidad, @img, @fk_producto, @precioCompra);";
|
|
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@nombre", variante.nombre);
|
|
command.Parameters.AddWithValue("@precio", variante.precio);
|
|
command.Parameters.AddWithValue("@cantidad", variante.cantidad);
|
|
command.Parameters.AddWithValue("@img", variante.img);
|
|
command.Parameters.AddWithValue("@fk_producto", variante.fkProducto);
|
|
command.Parameters.AddWithValue("@precioCompra", variante.precioCompra);
|
|
command.ExecuteNonQuery();
|
|
|
|
return true;
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarVarianteDAO(int idVariante)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
conexion.Open();
|
|
|
|
string sql = "DELETE FROM variantes WHERE idVariante = @idVariante;";
|
|
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idVariante", idVariante);
|
|
command.ExecuteNonQuery();
|
|
|
|
return true;
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool editarVarianteDAO(Variante variante)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE variantes SET nombre = @nombre, precio = @precio, cantidad = @cantidad, img = @img, " +
|
|
"fk_producto = @fk_producto, precioCompra = @precioCompra WHERE idVariante = @idVariante; ";
|
|
|
|
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@nombre", variante.nombre);
|
|
command.Parameters.AddWithValue("@precio", variante.precio);
|
|
command.Parameters.AddWithValue("@cantidad", variante.cantidad);
|
|
command.Parameters.AddWithValue("@img", variante.img);
|
|
command.Parameters.AddWithValue("@fk_producto", variante.fkProducto);
|
|
command.Parameters.AddWithValue("@precioCompra", variante.precioCompra);
|
|
command.Parameters.AddWithValue("@idVariante", variante.idVariante);
|
|
command.ExecuteNonQuery();
|
|
|
|
return true;
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirImportesDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM importe";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
|
|
public static SQLiteDataReader conseguirImportesVentaDAO(VentaModel venta)
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM detalle_importe WHERE fk_venta = @fk_venta;";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@fk_venta", venta.id);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
private static void crearDB()
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
conexion.Open();
|
|
string[] consultas = new string[]
|
|
{
|
|
"DROP TABLE IF EXISTS Empleado",
|
|
"DROP TABLE IF EXISTS Login",
|
|
"DROP TABLE IF EXISTS detalle_venta",
|
|
"DROP TABLE IF EXISTS Producto",
|
|
"DROP TABLE IF EXISTS Categoria",
|
|
"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",
|
|
"DROP TABLE IF EXISTS detalle_envios",
|
|
"DROP TABLE IF EXISTS envios",
|
|
"DROP TABLE IF EXISTS variantes",
|
|
|
|
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT NOT NULL, usuario TEXT NOT NULL, "+
|
|
"contra TEXT NOT NULL)",
|
|
|
|
"CREATE TABLE Empleado (idempleado INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, "+
|
|
"apellidos TEXT NOT NULL, calle TEXT NOT NULL, fecha_contratacion TEXT NOT NULL,estado TEXT NOT NULL, colonia TEXT NOT NULL, cp TEXT NOT NULL, no_ext TEXT NOT NULL," +
|
|
"no_int TEXT NOT NULL, referencia TEXT NOT NULL, fecha_nac TEXT NOT NULL, telefono TEXT NOT NULL,img TEXT DEFAULT '', fk_idLogin INTEGER," +
|
|
"FOREIGN KEY (fk_idLogin) REFERENCES Login(idlogin))",
|
|
|
|
"CREATE TABLE Categoria (idcategoria INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL)",
|
|
|
|
"CREATE TABLE Producto (idproducto INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL, "+
|
|
"precio DECIMAL(10, 2) NOT NULL, img TEXT NOT NULL, stock INTEGER NOT NULL, " +
|
|
"fk_idcategoria INTEGER NOT NULL, FOREIGN KEY (fk_idcategoria) REFERENCES Categoria (idcategoria))",
|
|
|
|
"CREATE TABLE variantes (idVariante INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT, precio DECIMAL(10,2), cantidad INTEGER DEFAULT 1, " +
|
|
"img TEXT,fk_producto INTEGER, FOREIGN KEY (fk_producto) REFERENCES Producto(idproducto))",
|
|
|
|
"CREATE TABLE Cliente (idcliente INTEGER PRIMARY KEY AUTOINCREMENT, nombres TEXT NOT NULL, apellidos TEXT NOT NULL, " +
|
|
"calle TEXT NOT NULL, colonia TEXT NOT NULL, cp TEXT NOT NULL, no_ext TEXT NOT NULL, no_int TEXT NOT NULL, referencia TEXT NOT NULL, " +
|
|
"telefono TEXT NOT NULL, telefono_extra TEXT NOT NULL)",
|
|
|
|
"CREATE TABLE Merma (idmerma INTEGER PRIMARY KEY AUTOINCREMENT, fecha TEXT, responsable TEXT, razon TEXT)",
|
|
|
|
"CREATE TABLE venta (idVenta INTEGER PRIMARY KEY AUTOINCREMENT,fecha TEXT, total DECIMAL(10, 2),responzable TEXT DEFAULT '');",
|
|
|
|
"CREATE TABLE detalle_venta (fk_venta INTEGER, fk_variante INTEGER,cantidad INTEGER, PRIMARY KEY (fk_venta, fk_variante)," +
|
|
"FOREIGN KEY (fk_venta) REFERENCES venta(idVenta),FOREIGN KEY (fk_variante) REFERENCES variantes(idVariante))",
|
|
|
|
"CREATE TABLE cuenta (idCuenta INTEGER PRIMARY KEY AUTOINCREMENT, total DECIMAL(10,2), fecha TEXT, estado TEXT DEFAULT 'ACTIVO', descripcion TEXT, cliente TEXT DEFAULT '')",
|
|
|
|
"CREATE TABLE detalle_cuenta (fk_cuenta INTEGER, fk_variante INTEGER,cantidad INTEGER, PRIMARY KEY (fk_cuenta, fk_variante), " +
|
|
"FOREIGN KEY (fk_cuenta) REFERENCES Cuenta(idCuenta),FOREIGN KEY (fk_variante) REFERENCES variantes(idVariante))",
|
|
|
|
"CREATE TABLE envios (idEnvios INTEGER PRIMARY KEY AUTOINCREMENT, fecha TEXT, direccion TEXT, telefono TEXT,responzable TEXT DEFAULT '',estado TEXT DEFAULT 'EN PROCESO', total DECIMAL(10,2) DEFAULT 0,cliente TEXT)",
|
|
|
|
"CREATE TABLE detalle_envios (fk_envios INTEGER, fk_variante INTEGER,cantidad INTEGER, PRIMARY KEY (fk_envios, fk_variante)," +
|
|
"FOREIGN KEY (fk_envios) REFERENCES envios(idEnvios),FOREIGN KEY (fk_variante) REFERENCES variantes(idVariante))",
|
|
|
|
"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);",
|
|
|
|
};
|
|
|
|
foreach (string consulta in consultas)
|
|
{
|
|
using (var comando = new SQLiteCommand(consulta, conexion))
|
|
{
|
|
comando.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |