/// <summary> /// Método que guarda una factura /// </summary> /// <param name="pFactura">Factura que se guardará</param> /// <returns>Retorna el Encabezado de factura que se guardó</returns> public EncabezadoFactura GuardarFactura(EncabezadoFactura pFactura) { EncabezadoFactura oFacturaEncabezado = new EncabezadoFactura(); SqlDataAdapter sda = new SqlDataAdapter(); IConexion conexion = new Conexion(); DataTable dt = new DataTable(); using (SqlConnection conn = conexion.conexion()) { try { string query = @"PA_INSERT_EncFactura"; SqlCommand comm = new SqlCommand(query, conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@IDEncFactura", pFactura.IDEncFactura); comm.Parameters.AddWithValue("@Fecha", pFactura.Fecha); comm.Parameters.AddWithValue("@IDCliente", pFactura.oCliente.IDCliente); comm.Parameters.AddWithValue("@IDMensajero", pFactura.oMensajero.IDMensajero); comm.Parameters.AddWithValue("@XML", pFactura.GenerarXML()); comm.Parameters.AddWithValue("@IDTarjeta", pFactura.oTarjeta.IDTarjeta); comm.ExecuteNonQuery(); //while (reader.Read()) //{ // client = pFactura.IDEncFactura; //} } catch (SqlException sqlError) { StringBuilder msg = new StringBuilder(); msg.AppendFormat(Utilitarios.CreateSQLExceptionsErrorDetails(sqlError)); _MyLogControlEventos.ErrorFormat("Error {0}", msg.ToString()); throw; } catch (Exception er) { StringBuilder msg = new StringBuilder(); msg.AppendFormat(Utilitarios.CreateGenericErrorExceptionDetail(er)); _MyLogControlEventos.ErrorFormat("Error {0}", msg.ToString()); throw; } finally { conn.Close(); } } using (SqlConnection conn = conexion.conexion()) { try { string queryDetalle = @"PA_INSERT_DetFactura"; foreach (DetFactura item in pFactura._ListDetFactura) { SqlCommand comm2 = new SqlCommand(queryDetalle, conn); comm2.Parameters.AddWithValue("@NoFactura", item.NoFactura); comm2.Parameters.AddWithValue("@Secuancial", item.Secuencial); comm2.Parameters.AddWithValue("@Total", item.Total); comm2.Parameters.AddWithValue("@Cantidad", item.CantidadPaquetes); comm2.Parameters.AddWithValue("@Kilometros", item.CantidadKilometros); comm2.Parameters.AddWithValue("@DescripcionRuta", item.DescripcionRuta); comm2.Parameters.AddWithValue("@Impuesto", item.Impuesto); comm2.Parameters.AddWithValue("@DescripcionPaquete", item.DescripcionPaquete); comm2.Parameters.AddWithValue("@TipoEnvio", item.TipoEnvio.TipoEnvio); comm2.CommandType = CommandType.StoredProcedure; comm2.ExecuteNonQuery(); //while (reader.Read()) //{ // client = item.NoFactura; //} } oFacturaEncabezado = ObtenerFactura(pFactura.IDEncFactura); return(oFacturaEncabezado); } catch (SqlException sqlError) { StringBuilder msg = new StringBuilder(); msg.AppendFormat(Utilitarios.CreateSQLExceptionsErrorDetails(sqlError)); _MyLogControlEventos.ErrorFormat("Error {0}", msg.ToString()); throw; } catch (Exception er) { StringBuilder msg = new StringBuilder(); msg.AppendFormat(Utilitarios.CreateGenericErrorExceptionDetail(er)); _MyLogControlEventos.ErrorFormat("Error {0}", msg.ToString()); throw; } finally { conn.Close(); } } }
/// <summary> /// Método que guarda el detalle de factura en el encabezado /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGuardar_Click(object sender, EventArgs e) { IBLLImpuesto _BLLImpuesto = new BLLImpuesto(); IBLLPrecioEnvio _BLLPrecioEnvio = new BLLPrecioEnvio(); IBLLFactura _BLLFactura = new BLLFactura(); DetFactura oDetFactura = new DetFactura(); string descripcionPaquete = ""; string descripcionruta = ""; try { erpErrores.Clear(); if (_EncabezadoFactura == null) { MessageBox.Show("Debe agregar los datos del encabezado de la factura para continuar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { if (string.IsNullOrEmpty(txtIDMensajero.Text)) { erpErrores.SetError(txtIDMensajero, "Debe ingresar la identificación del mensajero"); return; } if (string.IsNullOrEmpty(txtNombreMensajero.Text)) { erpErrores.SetError(txtNombreMensajero, "Debe ingresar el nombre del mensajero"); return; } if (string.IsNullOrEmpty(mskNoTarjeta.Text)) { erpErrores.SetError(mskNoTarjeta, "Debe ingresar el número de tarjeta"); return; } int cantidad = 0; foreach (var item in mskNoTarjeta.Text) { cantidad++; } if (cantidad < 16) { erpErrores.SetError(mskNoTarjeta, "Debe ingresar un número válido"); return; } if (Convert.ToInt32(txtCantidadPaquetes.Text) == 0 || string.IsNullOrEmpty(txtCantidadPaquetes.Text)) { erpErrores.SetError(txtCantidadPaquetes, "Debe ingresar un paquete"); return; } if ((Convert.ToDouble(txtKilometros.Text) == 0 || string.IsNullOrEmpty(txtKilometros.Text))) { erpErrores.SetError(txtKilometros, "Debe ingresar una ruta válida"); return; } if (lstUbicaciones.Items.Count <= 1) { erpErrores.SetError(lstUbicaciones, "Debe ingresar una ruta válida"); return; } oDetFactura.CantidadKilometros = (int)Math.Round(Convert.ToDouble(txtKilometros.Text)); for (int i = 0; i < lstPaquetes.Items.Count; i++) { descripcionPaquete += lstPaquetes.Items[i].ToString() + "\n"; } oDetFactura.DescripcionPaquete = descripcionPaquete; for (int i = 0; i < lstUbicaciones.Items.Count; i++) { descripcionruta += lstUbicaciones.Items[i].ToString() + "\n"; } oDetFactura.DescripcionRuta = descripcionruta; oDetFactura.Impuesto = Convert.ToDouble(txtImpuesto.Text); oDetFactura.NoFactura = _EncabezadoFactura.IDEncFactura; oDetFactura.PrecioKilometro = _BLLPrecioEnvio.MostrarXID(cboTipoEnvio.SelectedItem.ToString()).PrecioRango; oDetFactura.Secuencial = _BLLFactura.GetNextNumeroFactura(); oDetFactura.CantidadPaquetes = Convert.ToInt32(txtCantidadPaquetes.Text); oDetFactura.TipoEnvio = _BLLPrecioEnvio.MostrarXID(cboTipoEnvio.SelectedItem.ToString()); _EncabezadoFactura.AgregrarDetalle(oDetFactura); txtSubTotal.Text = _EncabezadoFactura.SubTotal() + ""; txtTotal.Text = _EncabezadoFactura.SubTotal() + ((_EncabezadoFactura.SubTotal() * _EncabezadoFactura.Impuesto())) + ""; oDetFactura.Total = _EncabezadoFactura.SubTotal() + ((_EncabezadoFactura.SubTotal() * _EncabezadoFactura.Impuesto())); _EncabezadoFactura.XML = _EncabezadoFactura.GenerarXML(); File.WriteAllText(@"c:\temp\proyecto.xml", _EncabezadoFactura.GenerarXML()); } } catch (Exception er) { StringBuilder msg = new StringBuilder(); msg.AppendFormat("Message {0}\n", er.Message); msg.AppendFormat("Source {0}\n", er.Source); msg.AppendFormat("InnerException {0}\n", er.InnerException); msg.AppendFormat("StackTrace {0}\n", er.StackTrace); msg.AppendFormat("TargetSite {0}\n", er.TargetSite); // Log error _MyLogControlEventos.ErrorFormat("Error {0}", msg.ToString()); // Mensaje de Error MessageBox.Show("Se ha producido el siguiente error " + er.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }