private void DetallarFactura_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'tablas.DetallePro' table. You can move, or remove it, as needed. this.detalleProTableAdapter.Fill(this.tablas.DetallePro); try { FacturasCajTableAdapter adaperFac = new FacturasCajTableAdapter(); FacturasCajDataTable dataFac = adaperFac.BuscarFacPorIdConCajero(idFactura); dgvFactura.DataSource = dataFac; foreach (FacturasCajRow item in dataFac) { dgvFactura[2, 0].Value = item.Cajero; } DetallesTableAdapter adapterDet = new DetallesTableAdapter(); dgvDetalles.DataSource = adapterDet.BuscarDetPorIdFactura(idFactura); } catch (Exception error) { log.Error($"Error: {error.Message}", error); MessageBox.Show($"Error: {error.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnFacturar_Click(object sender, EventArgs e) { try { bool estadoCliente = false; if (cboNombreCliente.Text == "" && cbxCliente.Checked == false) { estadoCliente = true; } else if ((cboNombreCliente.Text != "" && cbxCliente.Checked == true) && idCliente != 0) { estadoCliente = true; } if (estadoCliente && dgvProductos.Rows.Count > 0 && txtPago.Text != "") { decimal devuelta = Convert.ToDecimal(txtPago.Text) - Convert.ToDecimal(txtTotal.Text); txtDevuelta.Text = devuelta.ToString(); if (devuelta < 0 && cbxCliente.Checked == false) { auxBtnFactura = true; DialogResult resultado = MessageBox.Show("Debe introducir un cliente para procesar la factura que genera deuda\n¿Desea agregar un cliente?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (resultado == DialogResult.Yes) { cbxCliente.Checked = true; BloquearControles(); } else { btnFacturar.Enabled = false; txtPago.Focus(); } } else { using (TransactionScope transaction = new TransactionScope()) { if (devuelta < 0) { devuelta -= clienteDebe; adapterCli.ActualizarMontoC(idCliente, devuelta * -1); } FacturaTableAdapter adapterFac = new FacturaTableAdapter(); DetallesTableAdapter adapterDet = new DetallesTableAdapter(); adapterFac.InsertarF(DateTime.Now, idCliente, usuario.Id, Convert.ToDecimal(txtPago.Text), totalITBIS, Convert.ToDecimal(txtDescuento.Text), Convert.ToDecimal(txtTotal.Text)); int idFactura = Convert.ToInt32(adapterFac.IdMasGrandeF()); foreach (DataGridViewRow item in dgvProductos.Rows) { decimal numExis = Convert.ToDecimal(item.Cells[7].Value.ToString()) - Convert.ToDecimal(item.Cells[2].Value.ToString()); decimal descuento = Convert.ToDecimal(item.Cells[4].Value.ToString()); adapterPro.ActualizarExistenciasP(Convert.ToInt32(item.Cells[6].Value.ToString()), numExis); adapterDet.InsertarFD(idFactura, Convert.ToInt32(item.Cells[6].Value.ToString()), Convert.ToDecimal(item.Cells[1].Value.ToString()), Convert.ToInt32(item.Cells[2].Value.ToString()), Convert.ToDecimal(item.Cells[3].Value.ToString()), descuento); log.Info($"Se facturó una cantidad de {Convert.ToInt32(item.Cells[2].Value.ToString())} para el producto con el ID {Convert.ToInt32(item.Cells[6].Value.ToString())} con un precio de {Convert.ToDecimal(item.Cells[1].Value.ToString())} por el usuario {usuario.Nombre} {usuario.Apellidos} para la factura {idFactura}"); } log.Info($"{usuario.Nombre} {usuario.Apellidos} realizó la factura con el ID: {idFactura} para el cliente {idCliente}"); MessageBox.Show("Factura realizada correctamente", "Facturación"); _menu.CargarDashboard(); LimpiarTodo(); cboNombreCliente.Focus(); transaction.Complete(); } } } else { string mensaje = null; if (!estadoCliente) { mensaje = "Debe chequear el cliente\n"; } if (dgvProductos.Rows.Count <= 0) { mensaje += "Debe agregar productos\n"; } if (txtPago.Text == "") { mensaje += "Debe ingresar el monto a pagar"; } MessageBox.Show(mensaje, "Error al facturar"); } } catch (Exception error) { log.Error($"Error: {error.Message}", error); MessageBox.Show($"Error: {error.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }