Exemplo n.º 1
0
        private void dgvVentas_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (e.ColumnIndex == 9)
            {
                string          id  = dgvVentas.Rows[e.RowIndex].Cells["ID"].Value.ToString();
                frmDetalleVenta frm = new frmDetalleVenta(id);
                frm.ShowDialog();
            }

            if (e.ColumnIndex == 11)
            {
                string nro_doc = dgvVentas.Rows[e.RowIndex].Cells["DOC"].Value.ToString();
                string tipo    = dgvVentas.Rows[e.RowIndex].Cells["TIPO_VENTA"].Value.ToString();

                InvoicePDF pdf = new InvoicePDF();

                ent_venta                 = new Ent_Venta();
                ent_venta.nro_doc_str     = dgvVentas.Rows[e.RowIndex].Cells["NRO_DOC"].Value.ToString();
                ent_venta.tipo_venta      = dgvVentas.Rows[e.RowIndex].Cells["TIPO_VENTA"].Value.ToString();
                ent_venta.emision         = dgvVentas.Rows[e.RowIndex].Cells["FECHA"].Value.ToString();
                ent_venta.cliente         = dgvVentas.Rows[e.RowIndex].Cells["NOMBRES"].Value.ToString();
                ent_venta.cliente_doc     = dgvVentas.Rows[e.RowIndex].Cells["DNI"].Value.ToString();
                ent_venta.direccion       = dgvVentas.Rows[e.RowIndex].Cells["DIRECCION"].Value.ToString();
                ent_venta.monto_total     = Convert.ToDouble(dgvVentas.Rows[e.RowIndex].Cells["TOTAL"].Value.ToString());
                ent_venta.monto_subtotal  = Convert.ToDouble(ent_venta.monto_total / (ent_configuracion.IGV + 1));
                ent_venta.monto_igv       = (ent_venta.monto_total - Convert.ToDouble(ent_venta.monto_subtotal));
                ent_venta.monto_descuento = Convert.ToDouble(dgvVentas.Rows[e.RowIndex].Cells["DESCUENTO"].Value.ToString());;
                ent_venta.nro_doc         = Convert.ToInt32(dgvVentas.Rows[e.RowIndex].Cells["DOC"].Value.ToString());
                ent_venta.moneda          = dgvVentas.Rows[e.RowIndex].Cells["MONEDA"].Value.ToString();

                ent_venta.lstProductos = BL_Ventas.getDetalleVenta(nro_doc);

                if (tipo == "BO")
                {
                    pdf.createBoleta(ent_configuracion, ent_venta);
                }
                else
                {
                    pdf.createFactura(ent_configuracion, ent_venta);
                }

                /*String filename = "invoices\\" + (tipo == "BO" ? "boleta" : "factura") + "_" + nro_doc + ".pdf";
                 * if (File.Exists(filename))
                 * {
                 *  Process.Start(filename);
                 * } else
                 * {
                 *  MessageBox.Show((tipo == "BO" ? "Boleta" : "Factura") + " no encontrada.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 * }*/
            }
        }
Exemplo n.º 2
0
        private void procesarCompra()
        {
            forma_pago = cboFormaPago.SelectedValue.ToString();

            Ent_Venta venta = new Ent_Venta();

            venta.nro_doc         = int.Parse(correlativo);
            venta.nro_doc_str     = lblSerie.Text;
            venta.cod_tienda      = cod_tienda;
            venta.tipo_venta      = tipo_venta;
            venta.forma_pago      = forma_pago;
            venta.cantidad        = sumarCantidad();
            venta.monto_total     = total;
            venta.monto_subtotal  = double.Parse(txtSubTotal.Text);
            venta.monto_igv       = double.Parse(txtIGV.Text);
            venta.monto_recibido  = double.Parse(txtRecibido.Text);
            venta.monto_vuelto    = double.Parse(txtVuelto.Text);
            venta.monto_descuento = double.Parse(txtDescuento.Text);
            venta.cliente_doc     = txtDNI.Text;
            venta.cliente         = txtNombres.Text + " " + txtApellidos.Text;
            venta.direccion       = txtDireccion.Text;
            venta.usuario         = ent_usuario.username;

            venta.fecha_inicio = dtpFechaInicio.Value.ToShortDateString();
            venta.fecha_fin    = dtpFechaEntrega.Value.ToShortDateString();

            venta.moneda = cboMoneda.SelectedValue.ToString();

            bool existe_cliente = BL_Clientes.existeCliente(venta.cliente_doc);

            //save customer if doesnt exists
            if (!existe_cliente)
            {
                log.Info("Cliente " + txtDNI.Text + " no existe en la base de datos.", System.Reflection.MethodBase.GetCurrentMethod().Name);

                var confirm = MessageBox.Show("¿Desea guardar el cliente en el sistema?", "Atención", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirm == DialogResult.Yes)
                {
                    Ent_Clientes nuevo_cliente = new Ent_Clientes();
                    nuevo_cliente.dni       = txtDNI.Text;
                    nuevo_cliente.nombres   = txtNombres.Text;
                    nuevo_cliente.apellidos = txtApellidos.Text;
                    nuevo_cliente.direccion = txtDireccion.Text;
                    nuevo_cliente.telefono  = txtTelefono.Text;
                    nuevo_cliente.email     = txtEmail.Text;
                    nuevo_cliente.tipo      = (tipo_venta == "FA") ? "E" : "N";
                    nuevo_cliente.posible   = "1";

                    try
                    {
                        string _result = BL_Clientes.insertarCliente(nuevo_cliente);

                        if (_result == "1")
                        {
                            log.Info("Cliente " + nuevo_cliente.dni + " grabado con éxito.", System.Reflection.MethodBase.GetCurrentMethod().Name);
                        }
                        else
                        {
                            log.Error("Error al grabar cliente: " + _result, System.Reflection.MethodBase.GetCurrentMethod().Name);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ocurrió un error al grabar al cliente, sin embargo, el proceso de compra continuará.\n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        log.Error("Error al grabar cliente: " + ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name);
                    }
                }
                else
                {
                    log.Info("Cliente " + txtDNI.Text + " no se guardará en la base de datos.", System.Reflection.MethodBase.GetCurrentMethod().Name);
                }
            }

            foreach (DataGridViewRow row in dgvProductos.Rows)
            {
                Ent_Productos prd = new Ent_Productos();
                prd.id       = int.Parse(row.Cells["ID"].Value.ToString());
                prd.nombre   = row.Cells["DESCRIPCION"].Value.ToString();
                prd.cantidad = int.Parse(row.Cells["CANTIDAD"].Value.ToString());
                prd.precio   = float.Parse(row.Cells["PU"].Value.ToString());

                venta.lstProductos.Add(prd);
            }

            try
            {
                string result = BL_Ventas.procesarVenta(venta);

                if (result == "1")
                {
                    MessageBox.Show("Venta Realizada con Éxito!.", "Venta", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    InvoicePDF pdf = new InvoicePDF();
                    if (venta.tipo_venta == "BO")
                    {
                        pdf.createBoleta(ent_configuracion, venta);
                    }
                    else
                    {
                        pdf.createFactura(ent_configuracion, venta);
                    }

                    log.Info("Venta " + lblSerie.Text + " realizada con éxito.", System.Reflection.MethodBase.GetCurrentMethod().Name);
                    string resumen =
                        "\n-----------------------------------\n" +
                        "Resumen Venta:\n" +
                        "Serie: " + lblSerie.Text + "\n" +
                        "Tipo Venta: " + tipo_venta + "\n" +
                        "Forma de Pago: " + cboFormaPago.Text + "\n" +
                        "Cliente: " + txtDNI.Text + "\n" +
                        "Productos:\n";
                    foreach (DataGridViewRow item in dgvProductos.Rows)
                    {
                        resumen += "  - " + item.Cells["CODIGO"].Value.ToString() + " | " +
                                   item.Cells["DESCRIPCION"].Value.ToString() + " | " +
                                   item.Cells["CANTIDAD"].Value.ToString() + " | " +
                                   item.Cells["PU"].Value.ToString() + " | " +
                                   item.Cells["IMPORTE"].Value.ToString() + "\n";
                    }
                    resumen +=
                        "Subtotal: " + txtSubTotal.Text + "\n" +
                        "IGV: " + txtIGV.Text + "\n" +
                        "TOTAL: " + txtTotal.Text + "\n" +
                        "Recibido: " + txtRecibido.Text + "\n" +
                        "Vuelto: " + txtVuelto.Text + "\n" +
                        "-----------------------------------\n\n";
                    log.Info(resumen, System.Reflection.MethodBase.GetCurrentMethod().Name);
                    reiniciarVenta();
                }
                else
                {
                    MessageBox.Show(result, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error en el proceso de compra.\n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                log.Error("Error en el proceso de compra: " + ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }