1587 lines
66 KiB
C#
1587 lines
66 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.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 INNER JOIN Categoria ON Producto.fk_idcategoria = Categoria.idcategoria";
|
|
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)";
|
|
|
|
if (!producto.img.Equals(""))
|
|
{
|
|
producto.img = guardarImg(producto.img);
|
|
}
|
|
|
|
|
|
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", producto.categoria.idCategoria);
|
|
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();
|
|
|
|
string sql = "DELETE FROM Producto WHERE idproducto = @idproducto";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
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 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 insertarCategoriaDAO(Categoria categoria)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Categoria (nombre, descripcion) " +
|
|
"VALUES (@nombre, @descripcion)";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", categoria.nombre);
|
|
command.Parameters.AddWithValue("@descripcion", categoria.descripcion);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { 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)
|
|
{
|
|
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_producto, cantidad) VALUES(@idVenta, @idProducto, @cantidada);";
|
|
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";
|
|
|
|
|
|
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();
|
|
|
|
SQLiteCommand commandConsulta = new SQLiteCommand(sqlIdVenta, conexion);
|
|
SQLiteDataReader reader = commandConsulta.ExecuteReader();
|
|
|
|
long idVenta = -1;
|
|
while (reader.Read())
|
|
{
|
|
idVenta = reader.GetInt64(0);
|
|
}
|
|
|
|
foreach (DetalleVentaModel detalleVenta in detalleVentas)
|
|
{
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta);
|
|
commandEmpleado.Parameters.AddWithValue("@idProducto", detalleVenta.producto.idProducto);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleVenta.cantidad);
|
|
commandEmpleado.ExecuteNonQuery();
|
|
|
|
SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion);
|
|
commandProdcutoStock.Parameters.AddWithValue("@idproducto", detalleVenta.producto.idProducto);
|
|
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 - detalleVenta.cantidad);
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleVenta.producto.idProducto);
|
|
commandProdcutoUpdate.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)
|
|
{
|
|
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,'');";
|
|
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";
|
|
string sqlProductoStock = "SELECT stock FROM Producto WHERE idproducto = @idproducto";
|
|
string sqlProductoUpdate = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto";
|
|
|
|
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();
|
|
|
|
//modificar stock
|
|
SQLiteCommand commandProdcutoStock = new SQLiteCommand(sqlProductoStock, conexion);
|
|
commandProdcutoStock.Parameters.AddWithValue("@idproducto", detalleCuenta.producto.idProducto);
|
|
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);
|
|
commandProdcutoUpdate.Parameters.AddWithValue("@idproducto", detalleCuenta.producto.idProducto);
|
|
commandProdcutoUpdate.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 p.idproducto, p.nombre , dc.cantidad from detalle_cuenta dc, Producto p WHERE dc.fk_producto = p.idproducto 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 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)
|
|
{
|
|
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_producto, cantidad) VALUES(@idVenta, @idProducto, @cantidada);";
|
|
string sqlCuenta = "UPDATE cuenta SET estado ='SALDADO' WHERE idCuenta = @idCuenta;";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
|
|
|
|
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)
|
|
{
|
|
MessageBox.Show(detalleCuenta.producto.idProducto.ToString());
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlDetalle, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@idVenta", idVenta);
|
|
commandEmpleado.Parameters.AddWithValue("@idProducto", detalleCuenta.producto.idProducto);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleCuenta.cantidad);
|
|
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 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 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, Producto p WHERE dv.fk_producto = p.idproducto 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 SQLiteDataReader filtrarVentaFechaMes(DateTime fecha)
|
|
{
|
|
Dictionary<string, dynamic> estadisticas = new Dictionary<string, dynamic>();
|
|
SQLiteDataReader reader = DAO.filtrarVentaFechaMesDAO(fecha);
|
|
string formatoFecha = "dd/MM/yyyy";
|
|
decimal totalDia = 0;
|
|
decimal totalMes = 0;
|
|
List<VentaModel> ventas = new List<VentaModel>();
|
|
while (reader.Read())
|
|
{
|
|
VentaModel venta = new VentaModel(
|
|
(decimal)reader["total"],
|
|
(int)reader.GetInt64(0),
|
|
reader.GetString(1)
|
|
);
|
|
string[] cadenasFech = venta.fecha.Split(' ');
|
|
DateTime.TryParseExact(cadenasFech[0], formatoFecha, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime fechaVenta);
|
|
if (fecha.Day == fechaVenta.Day)
|
|
{
|
|
totalDia += venta.total;
|
|
}
|
|
totalMes += venta.total;
|
|
ventas.Add(venta);
|
|
}
|
|
|
|
estadisticas.Add("ventasDia", totalDia);
|
|
estadisticas.Add("ventasMes", totalMes);
|
|
|
|
return estadisticas;
|
|
}*/
|
|
|
|
public static bool crearEnvio(Envio envio, List<DetalleEnvioModel> detallesEnvio)
|
|
{
|
|
return DAO.crearEnvioDAO(envio, detallesEnvio);
|
|
}
|
|
|
|
public static bool crearEnvioDAO(Envio envio, List<DetalleEnvioModel> detallesEnvio)
|
|
{
|
|
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) VALUES(@fecha,@direccion , @telefono, @responzable, @total);";
|
|
string sqlDetalle = "INSERT INTO detalle_envios (fk_envios, fk_producto, cantidad) VALUES(@fk_envios, @fk_producto, @cantidad);";
|
|
string sqlIdCuenta = "SELECT idEnvios FROM envios ORDER BY idEnvios DESC LIMIT 1";
|
|
|
|
SQLiteCommand commandCuenta = new SQLiteCommand(sqlEnvio, conexion);
|
|
commandCuenta.Parameters.AddWithValue("@fecha", envio.fecha);
|
|
commandCuenta.Parameters.AddWithValue("@direccion", "");
|
|
commandCuenta.Parameters.AddWithValue("@telefono", "");
|
|
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_producto", detalleEnvio.producto.idProducto);
|
|
commandDetalle.Parameters.AddWithValue("@cantidad", detalleEnvio.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;
|
|
}
|
|
}
|
|
|
|
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";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
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 p.idproducto, p.nombre , de.cantidad from detalle_envios de, Producto p WHERE de.fk_producto = p.idproducto 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(int idEnvio)
|
|
{
|
|
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", idEnvio);
|
|
command.Parameters.AddWithValue("@estado", "CANCELADO");
|
|
|
|
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 concluirEnvioDAO(Envio envio, List<DetalleEnvioModel> detallesEnvio)
|
|
{
|
|
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_producto, cantidad) VALUES(@idVenta, @idProducto, @cantidada);";
|
|
string sqlEnvio = "UPDATE envios SET estado ='CONCLUIDO' WHERE idEnvios = @idEnvios;";
|
|
string sqlIdVenta = "SELECT idVenta FROM venta ORDER BY idVenta DESC LIMIT 1";
|
|
|
|
|
|
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("@idProducto", detalleEnvio.producto.idProducto);
|
|
commandEmpleado.Parameters.AddWithValue("@cantidada", detalleEnvio.cantidad);
|
|
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 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 Producto 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;
|
|
}
|
|
}
|
|
|
|
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",
|
|
|
|
"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 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_producto INTEGER,cantidad INTEGER DEFAULT 0, 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, cliente TEXT DEFAULT '')",
|
|
|
|
"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))",
|
|
|
|
"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_producto INTEGER,cantidad INTEGER, PRIMARY KEY (fk_envios, fk_producto), " +
|
|
"FOREIGN KEY (fk_envios) REFERENCES envios(idEnvios),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);",
|
|
|
|
};
|
|
|
|
foreach (string consulta in consultas)
|
|
{
|
|
using (var comando = new SQLiteCommand(consulta, conexion))
|
|
{
|
|
comando.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |