Пример #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("¿De verdad desea emitir esta nota de credito?", "Confirmacion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                if ((comboBox2.SelectedIndex != -1) && (comboBox3.SelectedIndex != -1) && (comboBox1.SelectedIndex != -1))
                {
                    if (dataGridView1.CurrentRow.Cells["Folio"].Value.ToString() == "")
                    {
                        DAO.FelWebServiceDAO felweb = new EquimarFac.DAO.FelWebServiceDAO();
                        cargacliente();
                        foreach (DataGridViewRow row in dataGridView2.Rows)
                        {
                            felweb.NombreCliente        = row.Cells[1].Value.ToString();
                            felweb.Contacto             = row.Cells[6].Value.ToString();
                            felweb.Telefono             = row.Cells[4].Value.ToString();
                            felweb.Email                = row.Cells[18].Value.ToString();
                            felweb.rfcReceptor          = row.Cells[5].Value.ToString();
                            felweb.nombreReceptor       = row.Cells[1].Value.ToString();
                            felweb.calleReceptor        = row.Cells[10].Value.ToString();
                            felweb.noExteriorReceptor   = row.Cells[11].Value.ToString();
                            felweb.noInteriorReceptor   = row.Cells[12].Value.ToString();
                            felweb.coloniaReceptor      = row.Cells[13].Value.ToString();
                            felweb.localidadReceptor    = row.Cells[14].Value.ToString();
                            felweb.referenciaReceptor   = row.Cells[15].Value.ToString();
                            felweb.municipioReceptor    = row.Cells[3].Value.ToString();
                            felweb.estadoReceptor       = row.Cells[16].Value.ToString();
                            felweb.paisReceptor         = row.Cells[17].Value.ToString();
                            felweb.codigoPostalReceptor = row.Cells[19].Value.ToString();
                        }

                        felweb.ClaveCFDI = "CRE";

                        DAO.FacturasDAO facturasdao1 = new EquimarFac.DAO.FacturasDAO();
                        facturasdao1.Nombre      = comboBox2.Text;
                        dataGridView2.DataSource = facturasdao1.devuelvedatospacpornombre();
                        foreach (DataGridViewRow row in dataGridView2.Rows)
                        {
                            felweb.CuentaFEL   = row.Cells[1].Value.ToString();
                            felweb.emisorRFC   = row.Cells[0].Value.ToString();
                            felweb.PasswordFEL = row.Cells[2].Value.ToString();
                        }
                        felweb.formaDePago       = "Pago en una sola exhibición";
                        felweb.parcialidades     = "";
                        felweb.condicionesDePago = "";
                        felweb.metodoDePago      = comboBox1.Text;

                        decimal iva      = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["Iva"].Value),
                                total    = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["Total"].Value),
                                subtotal = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["Subtotal"].Value);
                        decimal tipodecambio;
                        string  tipodecambiostring;
                        //            NotaCredito.Folio,
                        //NotaCredito.Fecha,
                        //NotaCredito.Concepto,
                        //NotaCredito.Subtotal,
                        //NotaCredito.Iva,
                        //NotaCredito.Total,
                        //NotaCredito.ConceptoT as 'Monto en letras',
                        //NotaCredito.Descuento,
                        //NotaCredito.MotivoDescuento,
                        //NotaCredito.PorcentajeDescuento,
                        //NotaCredito.Moneda,
                        //NotaCredito.TipoDeCambio,
                        //NotaCredito.FechaTipoCambio,
                        //NotaCredito.UUID
                        if ((dataGridView1.CurrentRow.Cells["TipoDeCambio"].Value.ToString() == "") | (dataGridView1.CurrentRow.Cells["TipoDeCambio"].Value.ToString() == "0.00"))
                        {
                            tipodecambiostring     = "";
                            felweb.fechaTipoCambio = "";
                        }
                        else
                        {
                            tipodecambio           = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["TipoDeCambio"].Value);
                            tipodecambiostring     = tipodecambio.ToString();
                            felweb.fechaTipoCambio = Convert.ToDateTime(dataGridView1.CurrentRow.Cells["FechaTipoCambio"].Value).ToShortDateString();
                        }


                        string ivastring      = iva.ToString();
                        string totalstring    = total.ToString();
                        string subtotalstring = subtotal.ToString();



                        felweb.moneda     = dataGridView1.CurrentRow.Cells["Moneda"].Value.ToString();
                        felweb.tipoCambio = tipodecambiostring;

                        felweb.totalImpuestosRetenidos   = "0.000000";
                        felweb.totalImpuestosTrasladados = ivastring;
                        felweb.subTotal        = subtotalstring;
                        felweb.total           = totalstring;
                        felweb.importeConLetra = dataGridView1.CurrentRow.Cells["Monto en letras"].Value.ToString();
                        felweb.LugarExpedicion = "Progreso, Yucatan";
                        //felweb.NumCuentaPago = "";
                        felweb.FolioFiscalOrig      = "";
                        felweb.SerieFolioFiscalOrig = "";
                        felweb.FechaFolioFiscalOrig = "";
                        felweb.MontoFolioFiscalOrig = "";
                        //Datos Varios
                        felweb.datosEtiquetas1 = "|Factura folio|" + textBox8.Text + "|";

                        if (lblAgencia.Text == "")
                        {
                            //felweb.datosEtiquetas2 = "|Viaje|" + dataGridView2.Rows[0].Cells["Viaje"].Value.ToString() + "|";
                            if (lblNumCuenta.Text == "")
                            {
                                felweb.NumCuentaPago = "";
                                cargacliente();
                                foreach (DataGridViewRow row in dataGridView2.Rows)
                                {
                                    if ((row.Cells[16].Value.ToString() == "Estado Extranjero") && (row.Cells[17].Value.ToString() == "País Extranjero"))
                                    {
                                        felweb.datosEtiquetas2 = "|País|" + row.Cells["PaisExtranjero"].Value.ToString() + "|";
                                        felweb.datosEtiquetas3 = "|Estado|" + row.Cells["EstadoExtranjero"].Value.ToString() + "|";
                                    }
                                }
                            }
                            else
                            {
                                felweb.NumCuentaPago = lblNumCuenta.Text;
                                //felweb.datosEtiquetas3 = "|NumCuenta|" + lblNumCuenta.Text + "|";
                                cargacliente();
                                foreach (DataGridViewRow row in dataGridView2.Rows)
                                {
                                    if ((row.Cells[16].Value.ToString() == "Estado Extranjero") && (row.Cells[17].Value.ToString() == "País Extranjero"))
                                    {
                                        felweb.datosEtiquetas2 = "|País|" + row.Cells["PaisExtranjero"].Value.ToString() + "|";
                                        felweb.datosEtiquetas3 = "|Estado|" + row.Cells["EstadoExtranjero"].Value.ToString() + "|";
                                    }
                                }
                            }
                        }
                        else
                        {
                            felweb.datosEtiquetas2 = "|Agencia|" + lblAgencia.Text + "|";
                            //felweb.datosEtiquetas3 = "|Viaje|" + dataGridView2.Rows[0].Cells["Viaje"].Value.ToString() + "|";

                            if (lblNumCuenta.Text == "")
                            {
                                felweb.NumCuentaPago = "";
                                cargacliente();
                                foreach (DataGridViewRow row in dataGridView2.Rows)
                                {
                                    if ((row.Cells[16].Value.ToString() == "Estado Extranjero") && (row.Cells[17].Value.ToString() == "País Extranjero"))
                                    {
                                        felweb.datosEtiquetas3 = "|País|" + row.Cells["PaisExtranjero"].Value.ToString() + "|";
                                        felweb.datosEtiquetas4 = "|Estado|" + row.Cells["EstadoExtranjero"].Value.ToString() + "|";
                                    }
                                }
                            }
                            else
                            {
                                felweb.NumCuentaPago = lblNumCuenta.Text;
                                //felweb.datosEtiquetas4 = "|NumCuenta|" + lblNumCuenta.Text + "|";
                                cargacliente();
                                foreach (DataGridViewRow row in dataGridView2.Rows)
                                {
                                    if ((row.Cells[16].Value.ToString() == "Estado Extranjero") && (row.Cells[17].Value.ToString() == "País Extranjero"))
                                    {
                                        felweb.datosEtiquetas3 = "|País|" + row.Cells["PaisExtranjero"].Value.ToString() + "|";
                                        felweb.datosEtiquetas4 = "|Estado|" + row.Cells["EstadoExtranjero"].Value.ToString() + "|";
                                    }
                                }
                            }
                        }



                        //decimal descuentodecimal;

                        //if (dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString() != "")
                        //{
                        //    descuentodecimal = decimal.Parse(dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString());
                        //}
                        //else
                        //{
                        //    descuentodecimal = 0;
                        //}

                        felweb.importeConLetra = dataGridView1.CurrentRow.Cells["Monto en letras"].Value.ToString();
                        List <string> datosConceptos = new List <string>(), datosInfoAduanera = new List <string>();
                        ////cargaconceptos();
                        ////foreach (DataGridViewRow row in dataGridView2.Rows)
                        ////{
                        ////El esquema, para incluir los conceptos sera el siguiente: | Cantidad | Unidad | noIdentificacion | Descripcion | valorUnitario | Importe |
                        ////Haciendo uso del caracter "|" pipe, para separar cada uno de los valores correspondientes. Ejemplo: |1|mtro.||alambre 1/2 pulgada|1.0|1.0|
                        ////|cantidad|unidad|noIdentificacion|descripcion|valorUnitario|importe|
                        decimal valorconcepto = 0;
                        if ((dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString() == "") | (dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString() == "0.00"))
                        {
                            valorconcepto = (decimal.Parse(dataGridView1.CurrentRow.Cells["Subtotal"].Value.ToString()));
                        }
                        else
                        {
                            valorconcepto = (decimal.Parse(dataGridView1.CurrentRow.Cells["Subtotal"].Value.ToString())) - (decimal.Parse(dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString()));
                        }

                        datosConceptos.Add("|1|1||" + dataGridView1.CurrentRow.Cells["Concepto"].Value.ToString() + "|" + valorconcepto.ToString() + "|" + valorconcepto.ToString() + "|");
                        datosInfoAduanera.Add("");
                        // }
                        string descuentostring           = "";
                        string porcentajedescuentostring = "";
                        if ((dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString() == "") | (dataGridView1.CurrentRow.Cells["Descuento"].Value.ToString() == "0.00"))
                        {
                            felweb.descuento           = "0";
                            felweb.porcentajeDescuento = "0";
                            felweb.motivoDescuento     = "";
                        }
                        else
                        {
                            decimal descuento           = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["Descuento"].Value),
                                    porcentajedescuento = Convert.ToDecimal(dataGridView1.CurrentRow.Cells["PorcentajeDescuento"].Value);
                            descuentostring            = descuento.ToString();
                            porcentajedescuentostring  = porcentajedescuento.ToString();
                            felweb.descuento           = "0";
                            felweb.porcentajeDescuento = "0";
                            felweb.motivoDescuento     = "";
                            datosConceptos.Add("|1|1||" + dataGridView1.CurrentRow.Cells["MotivoDescuento"].Value.ToString() + "|" + descuentostring + "|" + descuentostring + "|");
                            datosInfoAduanera.Add("");
                        }
                        felweb.conceptos    = datosConceptos;
                        felweb.infoaduanera = datosInfoAduanera;
                        //felweb.datosConceptos = dataGridView1.CurrentRow.Cells["Cliente"].Value.ToString();
                        //felweb.datosInfoAduanera = dataGridView1.CurrentRow.Cells["Cliente"].Value.ToString();
                        felweb.datosRetenidosIVA      = "";
                        felweb.datosRetenidosISR      = "";
                        felweb.datosTraslados1        = "|IVA (IVA 16.00%)|IVA|16.00|" + dataGridView1.CurrentRow.Cells["Iva"].Value.ToString() + "|";
                        felweb.datosRetenidosLocales1 = "";
                        felweb.datosRetenidosLocales2 = "";
                        felweb.datosTrasladosLocales1 = "";
                        string[] respuesta = new string[5];
                        respuesta = felweb.GenerarCDFI();
                        if (respuesta[0] == "True")
                        {
                            XmlDocument xmldoc     = new XmlDocument();
                            string      resultado1 = respuesta[3].ToString();
                            //XmlWriterSettings settings;

                            ////settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, CheckCharacters = false };

                            //XmlReader reader = XmlReader.Create(resultado1, settings.Encoding.EncodingName = UTF8Encoding);
                            XmlReaderSettings rs = new XmlReaderSettings();
                            rs.CheckCharacters = false;

                            //// XmlXapResolver is the default resolver.
                            //using (XmlReader reader = XmlReader.Create(resultado1, rs))
                            //{
                            //    xmldoc.in
                            //    xmldoc.Load(reader);
                            //}

                            xmldoc.LoadXml(resultado1);

                            //xmldoc.Save("C:\\Facturas CFDI\\FacturasXML\\FacturasMiXML_Timbrado.xml");
                            DAO.FacturasDAO facturasdao = new EquimarFac.DAO.FacturasDAO();
                            facturasdao.IDNotaCredito = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
                            //// Move to an element.
                            //XmlElement root = doc.DocumentElement;

                            //// Get an attribute.
                            //XmlAttribute attr = root.GetAttributeNode("ISBN");

                            //// Display the value of the attribute.
                            //String attrValue = attr.InnerXml;
                            //Console.WriteLine(attrValue);
                            XmlElement elementoxml = xmldoc.DocumentElement;
                            string     lstVideos   = elementoxml.ChildNodes.Count.ToString();
                            //XmlAttribute atributeelement = elementoxml.get
                            string                 atributouuid = lstVideos;
                            XmlNodeList            nodelist     = elementoxml.ChildNodes;
                            XmlNodeList            nodelist2    = nodelist.Item(4).ChildNodes;
                            XmlNodeList            nodelist3    = nodelist2.Item(0).ChildNodes;
                            XmlAttributeCollection elemento2    = nodelist2.Item(0).Attributes;
                            //XmlAttribute atrib = elemento2.GetNamedItem("UUID");
                            string nodotexto = elemento2.GetNamedItem("UUID").InnerText;

                            XmlAttributeCollection elemento3 = elementoxml.Attributes;
                            string nodotexto2 = elemento3.GetNamedItem("folio").InnerText;
                            facturasdao.Folio = nodotexto2;
                            xmldoc.Save("C:\\Facturas CFDI\\FacturasXML\\NC " + nodotexto2 + ".xml");
                            Image imagen = Base64ToImage(respuesta[4]);
                            imagen.Save("C:\\Facturas CFDI\\FacturasCBD\\NC " + nodotexto2 + ".png");
                            facturasdao.UUID = nodotexto;

                            string resultado = facturasdao.actualizanotascreditocfdi();
                            MessageBox.Show("Correcto");
                            carganotasgrid();
                        }
                        else
                        {
                            MessageBox.Show("Error de generacion del CFDI " + respuesta[1] + " " + respuesta[2]);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Para volver a emitir una Nota es necesario volver a crearla");
                    }
                }
                else
                {
                    MessageBox.Show("Es necesario elegir una cuenta, la moneda y el tipo de pago");
                }
            }
        }