Пример #1
0
        public static void InsertarDetalleFactura(DetalleFactura df, SqlTransaction tran, SqlConnection con)
        {
            try
            {
                string sql = "INSERT INTO DetallesFacturas (idRepuesto, cantidad, subtotal, idFactura) VALUES (@idRepuesto, @cantidad, @subtotal, @idFactura)";
                sql += "; UPDATE Repuestos SET stock = stock - @c WHERE idRepuesto = @id";
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = con;
                cmd.Transaction = tran;

                cmd.Parameters.AddWithValue("@idRepuesto", df.repuesto.idRepuesto);
                cmd.Parameters.AddWithValue("@id", df.repuesto.idRepuesto);
                cmd.Parameters.AddWithValue("@cantidad", df.cantidad);
                cmd.Parameters.AddWithValue("@subtotal", df.subtotal);
                cmd.Parameters.AddWithValue("@c", df.cantidad);
                cmd.Parameters.AddWithValue("@idFactura", df.factura.idFactura);
                cmd.ExecuteNonQuery();

            }
            catch (SqlException e)
            {
                throw new ApplicationException("Error al insertar detalle factura: " + e.Message);
            }
        }
Пример #2
0
    protected void btnAgregarRepuestos_Click(object sender, EventArgs e)
    {
        if(ddlRepuestos.SelectedIndex != 0 && !string.IsNullOrWhiteSpace(txtCantidadRepuestos.Text))
            {
            DetalleFactura df = new DetalleFactura();
            Repuesto r = new Repuesto();
            int idRepuesto = int.Parse(ddlRepuestos.SelectedValue.ToString());
            int cantidad = int.Parse(txtCantidadRepuestos.Text.ToString());
            Boolean found = false;

            if (ViewState["detallesFactura"] == null) ViewState["detallesFactura"] = new List<DetalleFactura>();
            List<DetalleFactura> listaDetalles = (List<DetalleFactura>)ViewState["detallesFactura"];

            foreach (var item in listaDetalles)
            {
                if (item.repuesto.idRepuesto == idRepuesto)
                {
                    found = true;

                    if (item.repuesto.stock >= item.cantidad + cantidad)
                    {
                        item.cantidad += cantidad;
                        item.subtotal = item.cantidad * item.repuesto.precio;
                        break;
                    }
                    else
                    {
                        alertaStockInsuficiente.Visible = true;
                        break;
                    }
                }

            }

            if (!found)
            {
                r = GestorRepuestos.ObtenerPorId(idRepuesto);
                if (r.stock >= cantidad)
                {
                    df.cantidad = cantidad;
                    df.repuesto = r;
                    df.subtotal = cantidad * r.precio;
                    listaDetalles.Add(df);
                    alertaStockInsuficiente.Visible = false;

                }
                else
                {
                    alertaStockInsuficiente.Visible = true;
                }

            }

            actualizarDatosFactura();
            CargarGrillaRepuestos();
            ResetP2();
        }
    }