Esempio n. 1
0
        public static List <eFactura> getClienteById(string pConnection)
        {
            List <eFactura> eFactura = new List <eFactura>();

            using (SqlConnection lConnection = new SqlConnection(pConnection))
            {
                using (SqlDataAdapter lDataAdapter = new SqlDataAdapter("SELECT f.id, c.nombre, c.apellido, f.fecha, f.num, f.serie FROM tb_facturas as f INNER JOIN tb_clientes as c ON f.id_cliente =  c.id", lConnection))
                {
                    lDataAdapter.SelectCommand.CommandType = CommandType.Text;
                    DataTable lDataTable = new DataTable();
                    lDataAdapter.Fill(lDataTable);
                    if (lDataTable != null && lDataTable.Rows.Count > 0)
                    {
                        foreach (DataRow lRow in lDataTable.Rows)
                        {
                            eFactura lFactura = new eFactura();
                            lFactura.id               = Convert.ToString(lRow["id"]);
                            lFactura.nombre_cliente   = Convert.ToString(lRow["nombre"]);
                            lFactura.apellido_cliente = Convert.ToString(lRow["apellido"]);
                            lFactura.fecha            = Convert.ToDateTime(lRow["fecha"]);
                            lFactura.num              = Convert.ToInt32(lRow["num"]);
                            lFactura.serie            = Convert.ToString(lRow["serie"]);
                            eFactura.Add(lFactura);
                        }
                    }
                }
            }
            return(eFactura);
        }
Esempio n. 2
0
        public static void GuardarFactura(string Conection, eFactura pFactura)
        {
            try
            {
                FacturasBO lFacturas = new FacturasBO();
                lFacturas.id         = Guid.NewGuid().ToString();
                lFacturas.num        = FacturasBO.GetNum(Conection, pFactura.serie);
                lFacturas.serie      = pFactura.serie;
                lFacturas.fecha      = DateTime.Now;
                lFacturas.id_cliente = pFactura.id_cliente;

                List <DetallesFacturaBO> detalles  = new List <DetallesFacturaBO>();
                List <ProductosBO>       productos = new List <ProductosBO>();

                foreach (var oDetalle in pFactura.Detalles)
                {
                    DetallesFacturaBO lDetalle  = new DetallesFacturaBO();
                    ProductosBO       lProducto = new ProductosBO(Conection, oDetalle.id_productos);
                    if (lProducto.cantidad < oDetalle.cantidad_productos)
                    {
                        throw new Exception("No hay existencia del producto " + lProducto.nombre);
                    }
                    lProducto.cantidad = lProducto.cantidad - oDetalle.cantidad_productos;
                    productos.Add(lProducto);
                    lDetalle.total = oDetalle.cantidad_productos * lProducto.precio;
                    lDetalle.cantidad_productos = oDetalle.cantidad_productos;
                    lDetalle.id_productos       = oDetalle.id_productos;
                    lDetalle.id_factura         = Guid.Parse((string)lFacturas.id);
                    detalles.Add(lDetalle);
                }
                SqlConnection  lConnection = new SqlConnection(Conection);
                SqlTransaction lTransaction;

                lConnection.Open();
                lTransaction = lConnection.BeginTransaction();

                try
                {
                    lFacturas.SaveTransaction(lConnection, lTransaction);
                    foreach (var iDetalle in detalles)
                    {
                        iDetalle.SaveTransaction(lConnection, lTransaction);
                    }
                    foreach (var iProductos in productos)
                    {
                        iProductos.SaveTransaction(lConnection, lTransaction);
                    }

                    lTransaction.Commit();
                }
                catch
                {
                    lTransaction.Rollback();
                    throw;
                }
                finally
                {
                    lConnection.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
 public IHttpActionResult PostFactura(eFactura pFactura)
 {
     FacturasBL.GuardarFactura(pConnection, pFactura);
     return(Ok());
 }