Exemplo n.º 1
0
        public static void Create(InvoiceEntity invoice)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
            {
                conn.Open();
                //
                // Creacion de la Factura
                //
                string sqlFactura = @"INSERT INTO Invoice (CustomerId, InvoiceDate, BillingAddress, Total) VALUES (@customerId, @date, @address, @total)
                           SELECT SCOPE_IDENTITY()";

                using (SqlCommand cmd = new SqlCommand(sqlFactura, conn))
                {

                    cmd.Parameters.AddWithValue("@customerId", invoice.CustomerId);
                    cmd.Parameters.AddWithValue("@date", invoice.InvoiceDate);
                    cmd.Parameters.AddWithValue("@address", invoice.BillingAddress);
                    cmd.Parameters.AddWithValue("@total", 0);

                    invoice.InvoiceId = Convert.ToInt32(cmd.ExecuteScalar());
                }


                string sqlLineaFactura = @"INSERT INTO InvoiceLine (InvoiceId, TrackId, UnitPrice, Quantity) 
                                            VALUES (@invoiceid, @trackid, @unitprice, @quantity)
                                            SELECT SCOPE_IDENTITY()";

                using (SqlCommand cmd = new SqlCommand(sqlLineaFactura, conn))
                {

                    foreach (InvoiceLinesEntity invoiceLine in invoice.Lineas)
                    {
                        //
                        // como se reutiliza el mismo objeto SqlCommand es necesario limpiar los parametros
                        // de la operacion previa, sino estos se iran agregando en la coleccion, generando un fallo
                        //
                        cmd.Parameters.Clear();

                        cmd.Parameters.AddWithValue("@invoiceid", invoice.InvoiceId);
                        cmd.Parameters.AddWithValue("@trackid", invoiceLine.TrackId);
                        cmd.Parameters.AddWithValue("@unitprice", invoiceLine.UnitPrice);
                        cmd.Parameters.AddWithValue("@quantity", invoiceLine.Quantity);

                        //
                        // Si bien obtenermos el id de linea de factura, este no es usado
                        // en la aplicacion
                        //
                        invoiceLine.InvoiceLineId = Convert.ToInt32(cmd.ExecuteScalar());
                    }

                }

            }

            

        }
Exemplo n.º 2
0
        public static void RegistrarFacturacion(InvoiceEntity invoice)
        {
            //
            // inicializo la transacciones
            //
            using (TransactionScope scope = new TransactionScope())
            {
                //
                // Creo la factura y sus lineas
                //
                InvoiceDAL.Create(invoice);

                //
                // Actualizo el total
                //
                InvoiceDAL.UpdateTotal(invoice.InvoiceId, invoice.Total);
                
                scope.Complete();
            }

        }
Exemplo n.º 3
0
        private void btnConfirmar_Click(object sender, EventArgs e)
        {
            if (!Validaciones())
                return;


            #region Creo\Actualizo la informacion del cliente

            //
            // si el cliente se ha seleccionado lo actualizo, sino se crea uno nuevo
            //
            if (cliente == null)
                cliente = new CustomerEntity();

            cliente.FirstName = txtNombre.Text;
            cliente.LastName = txtApellido.Text;
            cliente.Company = txtCompañia.Text;
            cliente.Address = txtDireccion.Text;
            cliente.Email = txtEmail.Text;

            cliente = CustomerBO.Save(cliente);

            #endregion

            #region Creo el Encabezado\Linea de la Factura

            InvoiceEntity invoice = new InvoiceEntity();

            invoice.CustomerId = cliente.CustomerId;
            invoice.InvoiceDate = DateTime.Now.Date;
            invoice.BillingAddress = txtDireccion.Text;


            foreach (DataGridViewRow row in dgvLineaCompra.Rows)
            {
                InvoiceLinesEntity invoiceLine = new InvoiceLinesEntity();

                invoiceLine.TrackId = Convert.ToInt32(row.Cells["Track"].Value);
                invoiceLine.UnitPrice = Convert.ToDecimal(row.Cells["PrecioUnitario"].Value);
                invoiceLine.Quantity = Convert.ToInt32(row.Cells["Cantidad"].Value);

                invoice.Lineas.Add(invoiceLine);
            }

            InvoiceBO.RegistrarFacturacion(invoice);

            #endregion
                
   
            InicializarControles();

        }