Subtract product stock when making a Sale

2

I am working on a desktop application in Windows Forms and MySQL as a database manager.

How do you rest in inventories when you make a Sale?

That's the code I use for the save sales button. The data is added to a sale table, and the fields of the tables. The code works and saves me, but I want to subtract from the inventory.

Save button code:

private void button8_Click(object sender, EventArgs e)
{
     Login.Venta_Tienda.VentaR pCliente = new Login.Venta_Tienda.VentaR();

     pCliente.Idproducto_V = textBox1.Text.Trim();
     pCliente.Producto_V = comboBox1.Text.Trim();
     pCliente.Cantidad_V = textBox2.Text.Trim(); 

     pCliente.Preciocxu_V = textBox3.Text.Trim(); 

     pCliente.Total_V = textBox5.Text.Trim();

     pCliente.Fechafac_V = dateTimePicker1.Value.Year
       + "/" + dateTimePicker1.Value.Month + "/" + dateTimePicker1.Value.Day;

     int resultado = Login.Venta_Tienda.MetodosVenta.Agregarp(pCliente);
     if (resultado > 0)
     {
         MessageBox.Show(
           "Venta Guardado Con Exito!!", "Guardado", MessageBoxButtons.OK,
           MessageBoxIcon.Information
         );
     }
     else
     {
         MessageBox.Show(
           "No se pudo guardar el cliente", "Fallo!!", MessageBoxButtons.OK,
           MessageBoxIcon.Exclamation
         );
     }
}

Table fields ventatienda :

Idventa Int,
Idproducto VARCHAR,
Producto VARCHAR,
Cantidad  VARCHAR,
Preciocxu  VARCHAR,
Total  VARCHAR,
Fechafac  VARCHAR

Table fields inventariomarket :

campos  INT,
IdProducto  VARCHAR,
Producto  VARCHAR,
Existencias  VARCHAR,
Precioxunidad  VARCHAR,
Proveedor  VARCHAR,
Fecha_Recibido  Date
    
asked by sunohara 16.11.2016 в 16:34
source

2 answers

1

Responding to your question, you should have a relational model similar to this, which I do not intend to be a solution, but a scope to the design of your database.

When you complete the Sale process, it should impact your Inventory table by subtracting the product stock. For that you must do at the time of registering the sale.

UPDATE inventariomarket
    SET stock = stock - @cantidad 
    WHERE IdAlmacen = @IdAlmacen AND IdProducto = @IdProducto

You could also create a Store Prodecure and do something like this:

ALTER PROCEDURE [dbo].[xspAfectaDetalleBodegaArticulo] 
(
    @idBodega VARCHAR(2),
    @idArticulo VARCHAR(6),
    @idMovimiento INT,
    @cantidad INT
)
AS
BEGIN

    SET NOCOUNT ON;

        INSERT INTO DetalleBodegaArticulo(idBodega, idArticulo, idMovimiento, cantidad)
    VALUES(@idBodega, @idArticulo, @idMovimiento, @cantidad)

    IF  @IdMovimiento = 2 
    BEGIN
        IF EXISTS( SELECT idBodega FROM BodegaArticulos WHERE idBodega = @idBodega AND idArticulo = @idArticulo ) 
            UPDATE BodegaArticulos
            SET stock = stock + @cantidad 
            WHERE idBodega = @idBodega AND idArticulo = @idArticulo
        ELSE
            INSERT INTO BodegaArticulos
            VALUES ( @idBodega, @idArticulo , @cantidad)
    END
    ELSE
    BEGIN 
        IF EXISTS( SELECT idBodega FROM BodegaArticulos WHERE idBodega = @idBodega AND idArticulo = @idArticulo ) 
            UPDATE BodegaArticulos
            SET stock = stock - @cantidad 
            WHERE idBodega = @idBodega AND idArticulo = @idArticulo
        ELSE
            PRINT('NO PUEDES REALIZAR LA VENTA, DEBIDO A QUE NO HAY PRODUCTOS EN STOCK')
    END
END 

You have to adapt it to your needs.

    
answered by 16.11.2016 / 18:01
source
1

You must rescue the data from the table after that, subtracting the amount that the client carries and the new data to re-write it in the database. Since you do not leave a code, I can not help you to see if you have a specific error or something.

Here are some links on how to connect Mysql and C #:

Link 1: link

Link 2: link

    
answered by 16.11.2016 в 16:38