Пример #1
0
        public static Classes.Sale GetSale(int SaleID)
        {
            Classes.Sale sale = new Classes.Sale();

            string headerQuery = string.Format("SELECT * FROM VentasCabecera WHERE NoVenta = {0}", SaleID);
            string detailQuery = string.Format("SELECT * FROM VentasDetalle WHERE NoVenta = {0}", SaleID);

            DataTable dtCab = new DataTable();
            DataTable dtLin = new DataTable();

            dtCab.Columns.Add("NoVenta", typeof(int));
            dtCab.Columns.Add("Cliente", typeof(int));
            dtCab.Columns.Add("MontoTotal", typeof(decimal));
            dtCab.Columns.Add("Productos", typeof(int));
            dtCab.Columns.Add("Fecha", typeof(DateTime));

            dtLin.Columns.Add("NoLinea", typeof(int));
            dtLin.Columns.Add("NoVenta", typeof(int));
            dtLin.Columns.Add("SKU", typeof(string));
            dtLin.Columns.Add("Cantidad", typeof(int));
            dtLin.Columns.Add("Monto", typeof(decimal));

            OpenConnection();
            try
            {
                com = new SqlCommand(headerQuery, con);
                rdr = com.ExecuteReader();
                while (rdr.Read())
                {
                    dtCab.Rows.Add(int.Parse(rdr[0].ToString()), int.Parse(rdr[1].ToString()), decimal.Parse(rdr[2].ToString()), int.Parse(rdr[3].ToString()), Convert.ToDateTime(rdr[4].ToString()));
                }
                rdr.Close();
                com = new SqlCommand(detailQuery, con);
                rdr = com.ExecuteReader();
                while (rdr.Read())
                {
                    dtLin.Rows.Add(int.Parse(rdr[0].ToString()), int.Parse(rdr[1].ToString()), rdr[2].ToString(), int.Parse(rdr[3].ToString()), decimal.Parse(rdr[4].ToString()));
                }

                sale.cliente    = (int)dtCab.Rows[0]["Cliente"];
                sale.fecha      = (DateTime)dtCab.Rows[0]["Fecha"];
                sale.folio      = (int)dtCab.Rows[0]["NoVenta"];
                sale.montoTotal = (decimal)dtCab.Rows[0]["MontoTotal"];
                sale.productos  = (int)dtCab.Rows[0]["Productos"];

                foreach (DataRow row in dtLin.Rows)
                {
                    Classes.Producto producto = new DataAccess.ProductsDataHandler().FetchProduct(row[2].ToString());
                    sale.AddItem(new Classes.ItemEntry(producto, int.Parse(row["cantidad"].ToString())));
                }
            } catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("Hubo un error al extraer los datos de la venta de la base de datos, intente nuevamente.", "Error al consultar venta", //System.Windows.Forms.MessageBoxButtons.OK, //System.Windows.Forms.MessageBoxIcon.Error);
                return(new Classes.Sale());
            }
            return(sale);
        }
Пример #2
0
        public static bool AddSale(Classes.Sale sale)
        {
            OpenConnection();
            string query = String.Format("INSERT INTO VentasCabecera(Cliente, MontoTotal, Productos) VALUES({0}, {1}, {2})", sale.cliente, sale.montoTotal, sale.productos);

            try
            {
                com = new SqlCommand(query, con);
                com.ExecuteNonQuery();
                foreach (Classes.ItemEntry entry in sale.Detalle)
                {
                    query = String.Format("INSERT INTO VentasDetalle(NoVenta, SKU, Cantidad, Monto) VALUES({0}, '{1}', {2}, {3})", sale.folio, entry.producto.SKU, entry.cantidad, entry.monto);
                    com   = new SqlCommand(query, con);
                    com.ExecuteNonQuery();
                    new DataAccess.InventoryDataHandler().UpdateStocks(entry.producto, new DataAccess.InventoryDataHandler().RetrieveStocks(entry.producto) - entry.cantidad);
                }
                CloseConnection();
                return(true);
            } catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("Ocurrió un error al ingresar los datos de la venta en la base de datos: " + ex.Message, "Error inesperado", //System.Windows.Forms.MessageBoxButtons.OK, //System.Windows.Forms.MessageBoxIcon.Error);
                return(false);
            }
        }