Example #1
0
        /// <summary>
        /// Handles the Click event of the FinishButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void FinishButton_Click(object sender, EventArgs e)
        {
            try
            {
                var txt = new SpoolMx();
                txt.SetEmisorRet(tbRfcEmi.Text, tbNomEmi.Text, tbCURPE.Text);
                if (chkRecNacional.Checked)
                {
                    txt.SetReceptorNacRet(tbRfcRec.Text, tbNomRec.Text, tbCURPR.Text);
                }
                else
                {
                    txt.SetReceptorExtRet(tbRfcRec.Text, tbNomRec.Text);
                }
                txt.SetPeriodoRet(tbMesIni.Text, tbMesFin.Text, tbEjerc.Text);
                txt.SetTotalesRet(tbTotOperacion.Text, tbTotGrav.Text, tbTotExent.Text, tbTotRet.Text);
                var sql = "SELECT impuesto, base, importe, tipoPago FROM DAT_MX_ImpuestosRetencionesTemp WHERE (id_Empleado = @idUser)";
                _db.Conectar();
                _db.CrearComando(sql);
                _db.AsignarParametroCadena("@idUser", _idUser);
                var dr = _db.EjecutarConsulta();
                while (dr.Read())
                {
                    txt.AgregaImpuestoRet(dr["base"].ToString(), dr["impuesto"].ToString(), dr["importe"].ToString(), dr["tipoPago"].ToString());
                }
                _db.Desconectar();
                var fecha = Localization.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
                var cve   = ddlCveRet.SelectedValue;
                var desc  = ddlCveRet.SelectedItem.Text;
                if (chkPagos.Checked)
                {
                    txt.SetPagosExtranjeros(chkBeneficiario.Checked ? "SI" : "NO");
                    if (chkBeneficiario.Checked)
                    {
                        txt.SetPagosExtranjerosBeneficiario(tbRFCB.Text, tbCURPB.Text, tbRazonB.Text, ddlConceptoPagoB.SelectedValue, tbDescConceptoB.Text);
                    }
                    if (chkNoBeneficiario.Checked)
                    {
                        txt.SetPagosExtranjerosNoBeneficiario(ddlPaisExt.SelectedValue, ddlConceptoPagoNB.SelectedValue, tbDescConceptoNB.Text);
                    }
                    cve  = "18";
                    desc = "Pagos realizados a favor de residentes en el extranjero.";
                }
                else if (chkDividOutil.Checked)
                {
                    txt.SetDividendosRetencion(ddlTipoDivOUtil.SelectedValue, tbMontISRAcredRetMexico.Text, tbMontISRAcredRetExtranjero.Text, tbMontRetExtDivExt.Text, ddlTipoSocDistrDiv.SelectedItem.Text, tbMontISRAcredNal.Text, tbMontDivAcumNal.Text, tbMontDivAcumExt.Text, tbProporcionRem.Text);
                    cve  = "14";
                    desc = "Dividendos o utilidades distribuidas.";
                }

                var folioR = 0;
                var sqlR   = "select top 1 folio from Dat_General where serie=@serie and codDoc='07' ORDER BY folio DESC";
                _db.Conectar();
                _db.CrearComando(sqlR);
                _db.AsignarParametroCadena("@serie", ddlSerie.SelectedItem.Text);
                var drR = _db.EjecutarConsulta();
                if (drR.Read())
                {
                    folioR = Convert.ToInt32(drR["folio"].ToString()) + 1;
                }
                _db.Desconectar();


                txt.SetComprobanteRet(folioR.ToString(), fecha, cve, desc, ddlSerie.SelectedItem.Text);



                // txt.SetComprobanteRet("", fecha, cve, desc, ddlSerie.SelectedItem.Text);



                var ambiente = false;
                switch (hfambiente.Value)
                {
                case "1":
                    ambiente = false;
                    break;

                case "2":
                    ambiente = true;
                    break;

                default:
                    break;
                }
                var txtInvoice = txt.ConstruyeTxtRetenciones();
                var randomMs   = new Random().Next(1000, 5000);
                System.Threading.Thread.Sleep(randomMs);
                var coreMx = new wsEmision.WsEmision {
                    Timeout = (1800 * 1000)
                };
                var result = coreMx.RecibeInfoTxt(txtInvoice, _idUser, Session["IDENTEMI"].ToString(), ambiente, "07", false, true, "", "");
                if (result != null)
                {
                    var xDoc = new XmlDocument();
                    xDoc.LoadXml(result.OuterXml);
                    Session["uuidCreado"] = GetAtributte(xDoc, "UUID", "tfd:TimbreFiscalDigital");
                    (Master as SiteMaster).MostrarAlerta(this, "El comprobante se ha generado satisfactoriamente", 2, null);
                    Response.Redirect("~/Documentos.aspx", false);
                }
                else
                {
                    (Master as SiteMaster).MostrarAlerta(this, "El comprobante no se creó correctamente<br/>" + coreMx.ObtenerMensaje(), 4, null);
                }
            }
            catch (Exception ex)
            {
                (Master as SiteMaster).MostrarAlerta(this, "El comprobante no se creó correctamente<br/>" + ex.Message, 4, null);
            }
        }