Beispiel #1
0
        private iva CargaDatos()
        {
            iva iva = new iva();

            try
            {
                progressBar1.Value = 40;

                cp_codigo_SRI_Bus         dat_ti             = new cp_codigo_SRI_Bus();
                List <cp_codigo_SRI_Info> ListCodigoSRI      = new List <cp_codigo_SRI_Info>();
                List <cp_codigo_SRI_Info> ListCodigoSustento = new List <cp_codigo_SRI_Info>();
                ListCodigoSRI = dat_ti.Get_List_codigo_SRI_(param.IdEmpresa);

                ListCodigoSustento = ListCodigoSRI.FindAll(c => c.co_estado == "A" && c.IdTipoSRI == "COD_IDCREDITO");

                List <cp_TipoDocumento_Info> LstTipDoc = new List <cp_TipoDocumento_Info>();
                cp_TipoDocumento_Bus         TipDoc_B  = new cp_TipoDocumento_Bus();
                LstTipDoc = TipDoc_B.Get_List_TipoDocumento();


                cp_orden_giro_pagos_sri_Bus   formasPago_B   = new cp_orden_giro_pagos_sri_Bus();
                cp_orden_giro_Bus             OGB            = new cp_orden_giro_Bus();
                cp_proveedor_Autorizacion_Bus Autorizacion_B = new cp_proveedor_Autorizacion_Bus();
                cp_proveedor_Bus Proveedor_B = new cp_proveedor_Bus();
                tb_persona_bus   Persona_B   = new tb_persona_bus();

                List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras> lstDetalleCompras = new List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras>();
                List <cp_orden_giro_Info> listaOG = new List <cp_orden_giro_Info>();
                listaOG = OGB.Get_List_orden_giro_SRI(param.IdEmpresa, Convert.ToInt32(cmb_anio.Text), Convert.ToInt32(cmb_periodo.SelectedValue));



                iva.IdInformante = Empresa_I.em_ruc;
                iva.razonSocial  = Empresa_I.RazonSocial.Replace(".", " ").Replace("ñ", "n").Replace("Ñ", "N");
                iva.Anio         = cmb_anio.Text;
                iva.Mes          = (cmb_periodo.SelectedValue.ToString().Length == 1) ? "0" + cmb_periodo.SelectedValue.ToString() : cmb_periodo.SelectedValue.ToString();

                tb_Sucursal_Bus bus_sucursal       = new tb_Sucursal_Bus();
                int             numEstablecimiento = bus_sucursal.Get_numEstablecimiento_x_empresa_SRI(param.IdEmpresa);
                iva.numEstabRuc = numEstablecimiento.ToString("000");


                iva.TipoIDInformante = ivaTypeTipoIDInformante.R;
                iva.codigoOperativo  = codigoOperativoType.IVA;
                iva.totalVentas      = 0;



                foreach (cp_orden_giro_Info item in listaOG)
                {
                    try
                    {
                        Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras itemDetalleCompras = new Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras();
                        if (item.IdCbteCble_Ogiro == 9)
                        {
                        }
                        info_Retencion_x_RF   = Retencion_B.Get_Info_retencion_X_Retecion_FT(param.IdEmpresa, item.IdCbteCble_Ogiro, item.IdTipoCbte_Ogiro);
                        info_Retencion_x_RIVA = Retencion_B.Get_Info_retencion_X_Retecion_RTIVA(param.IdEmpresa, item.IdCbteCble_Ogiro, item.IdTipoCbte_Ogiro);

                        string secuenci_numReten = "";
                        List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir> LstdetalleAir = new List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir>();
                        string fecRet = info_Retencion_x_RF.fecha.ToString("dd/MM/yyyy");


                        if (info_Retencion_x_RF.ListDetalle.Count != 0)
                        {
                            foreach (var item2 in info_Retencion_x_RF.ListDetalle)
                            {
                                Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir detalleAir = new Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir();
                                detalleAir.codRetAir = item2.CodigoSRI.Trim();

                                detalleAir.porcentajeAir = Convert.ToDecimal(item2.re_Porcen_retencion).ToString();
                                detalleAir.baseImpAir    = Convert.ToDecimal(item2.re_baseRetencion).ToString();

                                fecRet = info_Retencion_x_RF.fecha.ToString("dd/MM/yyyy");
                                detalleAir.valRetAir = Convert.ToDecimal(item2.re_valor_retencion).ToString();
                                LstdetalleAir.Add(detalleAir);
                            }
                        }


                        itemDetalleCompras.valRetBien10      = "0";
                        itemDetalleCompras.valRetServ20      = "0";
                        itemDetalleCompras.valRetServ50      = "0";
                        itemDetalleCompras.valRetServ100     = "0";
                        itemDetalleCompras.valorRetBienes    = "0";
                        itemDetalleCompras.valorRetServicios = "0";
                        itemDetalleCompras.totbasesImpReemb  = "0";


                        if (info_Retencion_x_RIVA.ListDetalle.Count != 0)
                        {
                            foreach (var item_iva in info_Retencion_x_RIVA.ListDetalle)
                            {
                                if (item_iva.re_Porcen_retencion == 10)
                                {
                                    itemDetalleCompras.valRetBien10 = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }

                                if (item_iva.re_Porcen_retencion == 20)
                                {
                                    itemDetalleCompras.valRetServ20 = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }

                                if (item_iva.re_Porcen_retencion == 30)
                                {
                                    itemDetalleCompras.valorRetBienes = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }

                                if (item_iva.re_Porcen_retencion == 50)
                                {
                                    itemDetalleCompras.valRetServ50 = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }

                                if (item_iva.re_Porcen_retencion == 70)
                                {
                                    itemDetalleCompras.valorRetServicios = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }


                                if (item_iva.re_Porcen_retencion == 100)
                                {
                                    itemDetalleCompras.valRetServ100 = Convert.ToDecimal(item_iva.re_valor_retencion).ToString();
                                }
                            }
                        }



                        if (item.IdIden_credito != 0)
                        {
                            var sustento = ListCodigoSustento.First(c => c.IdCodigo_SRI == item.IdIden_credito);
                            itemDetalleCompras.codSustento = sustento.codigoSRI;
                        }


                        if (!String.IsNullOrEmpty(item.co_serie))
                        {
                            string[] serie = Convert.ToString(item.co_serie).Split('-');

                            itemDetalleCompras.establecimiento = serie[0];
                            itemDetalleCompras.puntoEmision    = serie[1];
                        }


                        itemDetalleCompras.secuencial = item.co_factura;

                        //if (item.co_factura == "000002133") { MessageBox.Show(""); }



                        itemDetalleCompras.fechaRegistro = Convert.ToDateTime(item.co_FechaContabilizacion).ToString("dd/MM/yyyy");
                        itemDetalleCompras.idProv        = item.InfoProveedor.Persona_Info.pe_cedulaRuc.Trim();
                        itemDetalleCompras.tpIdProv      = (item.InfoProveedor.Persona_Info.IdTipoDocumento.Trim() == "RUC") ? "01" : ((item.InfoProveedor.Persona_Info.IdTipoDocumento.Trim() == "CED") ? "02" : "03");
                        itemDetalleCompras.parteRel      = (item.InfoProveedor.es_empresa_relacionada == true) ? parteRelType.SI : parteRelType.NO;
                        itemDetalleCompras.autorizacion  = (item.Num_Autorizacion == null) ? "" : item.Num_Autorizacion;



                        if (itemDetalleCompras.tpIdProv == "03")
                        {
                            itemDetalleCompras.tipoProv = (item.InfoProveedor.Persona_Info.pe_Naturaleza == "NATUR") ? "01" : "02";

                            if (item.InfoProveedor.es_empresa_relacionada == true)
                            {
                                itemDetalleCompras.parteRel = parteRelType.SI;
                            }
                            else
                            {
                                itemDetalleCompras.parteRel = parteRelType.NO;
                            }
                        }


                        var tipD = LstTipDoc.First(c => c.CodTipoDocumento == item.IdOrden_giro_Tipo);
                        itemDetalleCompras.tipoComprobante = tipD.CodSRI;
                        itemDetalleCompras.fechaEmision    = item.co_FechaFactura.ToString("dd/MM/yyyy");
                        itemDetalleCompras.autorizacion    = item.Num_Autorizacion;

                        itemDetalleCompras.baseNoGraIva  = "0";                                                                                                 //base NO objeto de IVA
                        itemDetalleCompras.baseImponible = Math.Round(Convert.ToDecimal(item.co_subtotal_siniva), 2, MidpointRounding.AwayFromZero).ToString(); // sin iva
                        itemDetalleCompras.baseImpGrav   = Math.Round(Convert.ToDecimal(item.co_subtotal_iva), 2, MidpointRounding.AwayFromZero).ToString();    //con iva
                        itemDetalleCompras.baseImpExe    = "0";

                        itemDetalleCompras.montoIce = Math.Round(Convert.ToDecimal(item.co_Ice_valor), 2, MidpointRounding.AwayFromZero).ToString();
                        itemDetalleCompras.montoIva = Math.Round(Convert.ToDecimal(item.co_valoriva), 2, MidpointRounding.AwayFromZero).ToString();

                        itemDetalleCompras.valRetBien10Specified     = true;
                        itemDetalleCompras.valRetServ20Specified     = true;
                        itemDetalleCompras.valRetServ50Specified     = true;
                        itemDetalleCompras.totbasesImpReembSpecified = true;

                        if (tipD.CodSRI == "05")
                        {
                            itemDetalleCompras.docModificado    = item.Tipodoc_a_Modificar;
                            itemDetalleCompras.estabModificado  = item.estable_a_Modificar;
                            itemDetalleCompras.ptoEmiModificado = item.ptoEmi_a_Modificar;
                            itemDetalleCompras.secModificado    = item.num_docu_Modificar;
                            itemDetalleCompras.autModificado    = item.aut_doc_Modificar;
                        }

                        if (LstdetalleAir.Count > 0)
                        {
                            itemDetalleCompras.air = LstdetalleAir;     //AREGLAR
                        }
                        if (!string.IsNullOrEmpty(info_Retencion_x_RF.NAutorizacion))
                        {
                            secuenci_numReten = info_Retencion_x_RF.NumRetencion.ToString();
                            string sSerieRetencion = "";
                            sSerieRetencion = info_Retencion_x_RF.serie1 + '-' + info_Retencion_x_RF.serie2;
                            if (!string.IsNullOrEmpty(sSerieRetencion))
                            {
                                itemDetalleCompras.estabRetencion1  = info_Retencion_x_RF.serie1;
                                itemDetalleCompras.ptoEmiRetencion1 = info_Retencion_x_RF.serie2;
                            }
                            itemDetalleCompras.secRetencion1 = secuenci_numReten;
                            itemDetalleCompras.autRetencion1 = info_Retencion_x_RF.NAutorizacion;
                            itemDetalleCompras.fechaEmiRet1  = fecRet;
                        }

                        lstDetalleCompras.Add(itemDetalleCompras);

                        pagoExterior Item_pagoExterior = new pagoExterior();

                        if (item.PagoLocExt == "LOC")
                        {
                            Item_pagoExterior.pagoLocExt = pagoLocExtType.Item01;
                        }
                        else
                        {
                            Item_pagoExterior.pagoLocExt = pagoLocExtType.Item02;
                        }


                        Item_pagoExterior.paisEfecPago = (Item_pagoExterior.pagoLocExt == pagoLocExtType.Item01) ? "NA" : (item.PaisPago != null || item.PaisPago != "") ? item.PaisPago : "NA";

                        Item_pagoExterior.aplicConvDobTrib   = (item.ConvenioTributacion == "S") ? aplicConvDobTribType.SI : (item.ConvenioTributacion == "N") ? aplicConvDobTribType.NO :aplicConvDobTribType.NA;
                        Item_pagoExterior.pagExtSujRetNorLeg = (item.PagoSujetoRetencion == "S") ? aplicConvDobTribType.SI : (item.PagoSujetoRetencion == "N") ? aplicConvDobTribType.NO : aplicConvDobTribType.NA;



                        itemDetalleCompras.pagoExterior = Item_pagoExterior;



                        List <cp_orden_giro_pagos_sri_Info> formasDePagoOG = new List <cp_orden_giro_pagos_sri_Info>();
                        formasDePagoOG = formasPago_B.Get_List_orden_giro_pagos_sri(item.IdEmpresa, item.IdCbteCble_Ogiro, item.IdTipoCbte_Ogiro);

                        string  cadena = "";
                        string  coma   = "";
                        Boolean Hay_registros_de_pagos = false;


                        if (formasDePagoOG.Count > 0)
                        {
                            foreach (var itemfp in formasDePagoOG)
                            {
                                if (itemfp.check == true)
                                {
                                    cadena = cadena + coma + itemfp.codigo_pago_sri;
                                    coma   = ",";
                                    Hay_registros_de_pagos = true;
                                }
                            }
                            if (Hay_registros_de_pagos)
                            {
                                itemDetalleCompras.formasDePago = cadena.Split(',');
                            }
                        }


                        if (Hay_registros_de_pagos == false)
                        {
                            decimal Total = 0;
                            Total = Convert.ToDecimal(itemDetalleCompras.baseNoGraIva) + Convert.ToDecimal(itemDetalleCompras.baseImpGrav);

                            if (Total >= 1000)
                            {
                                string[] arr1 = new string[] { "01" };
                                itemDetalleCompras.formasDePago = arr1;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

// Para las notas de debito y credito cxp
//*************************************

                cp_nota_DebCre_Bus         nota_B      = new cp_nota_DebCre_Bus();
                List <cp_nota_DebCre_Info> listaNotaDC = new List <cp_nota_DebCre_Info>();
                listaNotaDC = nota_B.Get_List_nota_DebCre(param.IdEmpresa, Convert.ToInt32(cmb_anio.Text), Convert.ToInt32(cmb_periodo.SelectedValue));



                progressBar1.Value = 70;
                foreach (cp_nota_DebCre_Info item in listaNotaDC)
                {
                    Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras itemDetalleCompras = new Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleCompras();



                    List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir> LstdetalleAir = new List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAir>();

                    cp_proveedor_Autorizacion_Info Autorizacion_I = new cp_proveedor_Autorizacion_Info();

                    var sustento = ListCodigoSustento.First(c => c.IdCodigo_SRI == item.IdIden_credito);
                    itemDetalleCompras.codSustento = sustento.codigoSRI;

                    itemDetalleCompras.establecimiento = item.cn_serie1;
                    itemDetalleCompras.puntoEmision    = item.cn_serie2;
                    itemDetalleCompras.secuencial      = item.cn_Nota;
                    itemDetalleCompras.autorizacion    = item.cn_Autorizacion;



                    //if ("000002133" == item.cn_Nota) { MessageBox.Show(""); }

                    if (item.Fecha_contable != null)
                    {
                        itemDetalleCompras.fechaRegistro = Convert.ToDateTime(item.Fecha_contable).ToString("dd/MM/yyyy");
                        itemDetalleCompras.fechaEmision  = Convert.ToDateTime(item.Fecha_contable).ToString("dd/MM/yyyy");
                    }

                    if (String.IsNullOrEmpty(itemDetalleCompras.fechaRegistro))
                    {
                        itemDetalleCompras.fechaRegistro = Convert.ToDateTime(item.cn_fecha).ToString("dd/MM/yyyy");
                        itemDetalleCompras.fechaEmision  = Convert.ToDateTime(item.cn_fecha).ToString("dd/MM/yyyy");
                    }



                    itemDetalleCompras.idProv   = item.InfoProveedor.Persona_Info.pe_cedulaRuc.Trim();
                    itemDetalleCompras.tpIdProv = (item.InfoProveedor.Persona_Info.IdTipoDocumento.Trim() == "RUC") ? "01" : ((item.InfoProveedor.Persona_Info.IdTipoDocumento.Trim() == "CED") ? "02" : "03");

                    if (itemDetalleCompras.tpIdProv == "03")
                    {
                        itemDetalleCompras.tipoProv = (item.InfoProveedor.Persona_Info.pe_Naturaleza == "NATUR") ? "01" : "02";
                        if (item.InfoProveedor.es_empresa_relacionada == true)
                        {
                            itemDetalleCompras.parteRel = parteRelType.SI;
                        }
                        else
                        {
                            itemDetalleCompras.parteRel = parteRelType.NO;
                        }
                    }



                    itemDetalleCompras.tipoComprobante = (item.DebCre == "C" || item.DebCre == "Credito") ? "04" : "05";
                    if (item.Fecha_contable != null)
                    {
                        itemDetalleCompras.fechaEmision = Convert.ToString(item.Fecha_contable);
                    }

                    itemDetalleCompras.baseNoGraIva  = "0";
                    itemDetalleCompras.baseImponible = Math.Round(Convert.ToDecimal(item.cn_subtotal_siniva), 2, MidpointRounding.AwayFromZero).ToString();    //sin iva
                    itemDetalleCompras.baseImpGrav   = Math.Round(Convert.ToDecimal(item.cn_subtotal_iva), 2, MidpointRounding.AwayFromZero).ToString();
                    itemDetalleCompras.baseImpExe    = "0";


                    itemDetalleCompras.montoIce = Math.Round(Convert.ToDecimal(item.cn_Ice_valor), 2, MidpointRounding.AwayFromZero).ToString();
                    itemDetalleCompras.montoIva = Math.Round(Convert.ToDecimal(item.cn_valoriva), 2, MidpointRounding.AwayFromZero).ToString();


                    itemDetalleCompras.valRetBien10      = "0.00";
                    itemDetalleCompras.valRetServ20      = "0.00";
                    itemDetalleCompras.valorRetBienes    = "0.00";
                    itemDetalleCompras.valRetServ50      = "0.00";
                    itemDetalleCompras.valorRetServicios = "0.00";
                    itemDetalleCompras.valRetServ100     = "0.00";
                    itemDetalleCompras.totbasesImpReemb  = "0.00";

                    itemDetalleCompras.valRetBien10Specified     = true;
                    itemDetalleCompras.valRetServ20Specified     = true;
                    itemDetalleCompras.valRetServ50Specified     = true;
                    itemDetalleCompras.totbasesImpReembSpecified = true;


                    if (LstdetalleAir.Count > 0)
                    {
                        itemDetalleCompras.air = LstdetalleAir;
                    }


                    itemDetalleCompras.docModificado    = item.docModificado;
                    itemDetalleCompras.estabModificado  = item.estabModificado;
                    itemDetalleCompras.ptoEmiModificado = item.ptoEmiModificado;
                    itemDetalleCompras.secModificado    = item.secModificado;
                    itemDetalleCompras.autModificado    = item.autModificado;

                    lstDetalleCompras.Add(itemDetalleCompras);

                    pagoExterior Item_pagoExterior = new pagoExterior();
                    Item_pagoExterior.pagoLocExt   = (item.PagoLocExt == "LOC") ? pagoLocExtType.Item01 : pagoLocExtType.Item02;
                    Item_pagoExterior.paisEfecPago = (Item_pagoExterior.pagoLocExt == pagoLocExtType.Item01) ? "NA"  : (item.PaisPago != null || item.PaisPago != "") ? item.PaisPago : "NA";

                    Item_pagoExterior.aplicConvDobTrib   = (item.ConvenioTributacion == "S") ? aplicConvDobTribType.SI : (item.ConvenioTributacion == "N") ? aplicConvDobTribType.NO :aplicConvDobTribType.NA;
                    Item_pagoExterior.pagExtSujRetNorLeg = (item.PagoSujetoRetencion == "S") ? aplicConvDobTribType.SI : (item.PagoSujetoRetencion == "N") ? aplicConvDobTribType.NO : aplicConvDobTribType.NA;

                    itemDetalleCompras.pagoExterior = Item_pagoExterior;
                }

                iva.compras        = lstDetalleCompras;
                progressBar1.Value = 85;

//*****ANULADOS*******
//********************

                tb_sis_Documento_Tipo_x_Empresa_Anulados_Bus         DocAnu_B  = new tb_sis_Documento_Tipo_x_Empresa_Anulados_Bus();
                List <tb_sis_Documento_Tipo_x_Empresa_Anulados_Info> LstDocAnu = new List <tb_sis_Documento_Tipo_x_Empresa_Anulados_Info>();

                LstDocAnu = DocAnu_B.ConsultaPorMesAnio(param.IdEmpresa, Convert.ToInt32(cmb_anio.Text), Convert.ToInt32(cmb_periodo.SelectedValue));

                List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAnulados> lstDetalleDocAnu = new List <Core.Erp.Info.CuentasxPagar.xmlATS_V_1_1_4.detalleAnulados>();
                foreach (var itemDA in LstDocAnu)
                {
                    if (itemDA.Autorizacion != null)
                    {
                        detalleAnulados info = new detalleAnulados();
                        info.autorizacion     = itemDA.Autorizacion;
                        info.establecimiento  = itemDA.Serie1;
                        info.puntoEmision     = itemDA.Serie2;
                        info.secuencialInicio = itemDA.Documento;
                        info.secuencialFin    = itemDA.DocumentoFin;
                        var tipD = LstTipDoc.First(c => c.CodTipoDocumento == itemDA.codDocumentoTipo);
                        info.tipoComprobante = tipD.CodSRI;
                        lstDetalleDocAnu.Add(info);
                    }
                }

                if (lstDetalleDocAnu.Count > 0)
                {
                    iva.anulados = lstDetalleDocAnu;
                }

                //// FACTURAS

                fa_factura_Bus fac_B = new fa_factura_Bus();

                List <detalleVentas> LstDV = new List <detalleVentas>();
                LstDV = fac_B.Get_List_VentasParaATS(param.IdEmpresa, Convert.ToInt32(cmb_anio.Text), Convert.ToInt32(cmb_periodo.SelectedValue));

                int IdPeriodo = (Convert.ToInt32(iva.Anio) * 100) + Convert.ToInt32(iva.Mes);

                foreach (var item in LstDV)
                {
                    item.montoIce          = 0;
                    item.montoIceSpecified = true;

                    #region Personalizacion de cliente Grafinpren

                    /// el cliente nos pide q enceremos el valor de la retencion de estos meses junio y julio 2016
                    if (param.IdCliente_Ven_x_Default == Cl_Enumeradores.eCliente_Vzen.GRAFINPRENT)
                    {
                        if (IdPeriodo >= 201606 && IdPeriodo <= 201611)
                        {
                            item.valorRetIva   = "0.00";
                            item.valorRetRenta = "0.00";
                        }
                    }
                    #endregion
                }



                if (LstDV.Count() > 0)
                {
                    iva.ventas = LstDV;
                }

                List <ventaEst> LstVenSum = new List <ventaEst>();
                LstVenSum = fac_B.Get_List_VentasXEstablecimientoParaATS(param.IdEmpresa, Convert.ToInt32(cmb_anio.Text), Convert.ToInt32(cmb_periodo.SelectedValue));

                foreach (var item in LstVenSum)
                {
                }
                if (LstVenSum.Count > 0)
                {
                    iva.ventasEstablecimiento = LstVenSum;
                    iva.totalVentas           = Math.Round(Math.Abs(LstVenSum.Sum(c => c.ventasEstab)), 2, MidpointRounding.AwayFromZero);
                }
                else
                {
                    iva.totalVentas = 0;
                }


                iva.totalVentasSpecified = true;

                return(iva);
            }
            catch (Exception ex)
            {
                Log_Error_bus.Log_Error(ex.ToString());
                MessageBox.Show(ex.ToString());
                return(iva);
            }
        }
Beispiel #2
0
        public iva get_ats(int IdEmpresa, int IdPeriodo, int IdSucursal, int[] IntArray)
        {
            try
            {
                string registro = "";
                iva    ats      = new iva();
                info_periodo = data_periodo.get_info(IdEmpresa, IdPeriodo);
                info_empresa = data_empresa.get_info(IdEmpresa);
                ats_Info info_ats = get_info(IdEmpresa, IdPeriodo, IdSucursal, IntArray);

                #region cabecera del xml
                ats.IdInformante = info_empresa.em_ruc;
                ats.razonSocial  = info_empresa.RazonSocial.Replace(".", " ").Replace("ñ", "n").Replace("Ñ", "N");
                ats.Anio         = info_periodo.IdanioFiscal.ToString();
                ats.Mes          = info_periodo.pe_mes.ToString().PadLeft(2, '0');

                ats.TipoIDInformante = ivaTypeTipoIDInformante.R;
                ats.codigoOperativo  = codigoOperativoType.IVA;
                ats.totalVentas      = info_ats.lst_ventas.Sum(v => v.ventasEstab);
                #endregion

                #region listado de compras
                if (info_ats.lst_compras != null)
                {
                    if (info_ats.lst_compras.Count() > 0)
                    {
                        ats.compras = new List <detalleCompras>();
                    }
                    info_ats.lst_compras.ForEach(
                        comp =>
                    {
                        detalleCompras comp_det = new detalleCompras();

                        registro                           = comp.denopr + " " + comp.secuencial;
                        comp.denopr                        = cl_funciones.QuitartildesEspaciosPuntos(comp.denopr);
                        comp_det.codSustento               = comp.codSustento;
                        comp_det.tpIdProv                  = comp.tpIdProv;
                        comp_det.idProv                    = comp.idProv;
                        comp_det.tipoComprobante           = comp.tipoComprobante;
                        comp_det.parteRel                  = parteRelType.NO;
                        comp_det.fechaRegistro             = comp.fechaRegistro.ToString().Substring(0, 10);
                        comp_det.establecimiento           = comp.establecimiento;
                        comp_det.puntoEmision              = comp.puntoEmision;
                        comp_det.secuencial                = comp.secuencial;
                        comp_det.fechaEmision              = comp.fechaEmision.ToString().Substring(0, 10);
                        comp_det.autorizacion              = comp.autorizacion;
                        comp_det.baseNoGraIva              = comp.baseNoGraIva.ToString("n2");
                        comp_det.baseImponible             = comp.baseImponible.ToString("n2");
                        comp_det.baseImpGrav               = comp.baseImpGrav.ToString("n2");
                        comp_det.baseImpExe                = comp.baseImpExe.ToString("n2");
                        comp_det.montoIce                  = comp.montoIce.ToString("n2");
                        comp_det.montoIva                  = comp.montoIva.ToString("n2");
                        comp_det.valRetBien10              = "0.00";
                        comp_det.valRetServ20              = "0.00";
                        comp_det.valorRetBienes            = "0.00";
                        comp_det.valRetServ50              = "0.00";
                        comp_det.valorRetServicios         = "0.00";
                        comp_det.valRetServ100             = "0.00";
                        comp_det.totbasesImpReemb          = "0.00";
                        comp_det.valRetBien10Specified     = true;
                        comp_det.valRetServ20Specified     = true;
                        comp_det.valRetServ50Specified     = true;
                        comp_det.totbasesImpReembSpecified = true;
                        pagoExterior item_pago             = new pagoExterior();
                        item_pago.pagoLocExt               = (comp.pagoLocExt == "LOC") ? pagoLocExtType.Item01 : pagoLocExtType.Item02;
                        item_pago.paisEfecPago             = (item_pago.pagoLocExt == pagoLocExtType.Item01) ? "NA" : (comp.pagoLocExt != null || comp.pagoLocExt != "") ? comp.pagoLocExt : "NA";
                        item_pago.aplicConvDobTrib         = aplicConvDobTribType.NA;
                        item_pago.pagExtSujRetNorLeg       = aplicConvDobTribType.NA;
                        comp_det.pagoExterior              = item_pago;
                        if (comp_det.idProv == "0601895618001")
                        {
                        }
                        if ((Convert.ToDecimal(comp.baseImponible) + Convert.ToDecimal(comp.baseImpGrav) + Convert.ToDecimal(comp.montoIva)) >= 1000)
                        {
                            comp_det.formasDePago = null;
                            string[] AFormaPago   = { "20" };
                            comp_det.formasDePago = AFormaPago;
                        }
                        #region Reembolso
                        if (comp.codSustento == "41")
                        {
                            comp_det.codSustento               = "01";
                            comp_det.reembolsos                = new List <reembolso>();
                            reembolso reem                     = new reembolso();
                            reem.tipoComprobanteReemb          = "01";
                            reem.tpIdProvReemb                 = comp.tpIdProv;
                            reem.idProvReemb                   = comp.idProv;
                            reem.establecimientoReemb          = comp.establecimiento;
                            reem.puntoEmisionReemb             = comp.puntoEmision;
                            reem.secuencialReemb               = comp.secuencial;
                            reem.fechaEmisionReemb             = comp.fechaEmision.ToString().Substring(0, 10);
                            reem.autorizacionReemb             = comp.autorizacion;
                            reem.baseImponibleReemb            = comp.baseImponible;
                            reem.baseImpGravReemb              = comp.baseImpGrav;
                            reem.baseImpExeReemb               = comp.baseImpExe;
                            reem.montoIceRemb                  = comp.montoIce;
                            reem.montoIvaRemb                  = comp.montoIva;
                            comp_det.totbasesImpReembSpecified = true;
                            comp_det.totbasesImpReemb          = Convert.ToString(Convert.ToDecimal(comp.baseImponible) + Convert.ToDecimal(comp.baseImpGrav));
                            comp_det.reembolsos.Add(reem);
                        }

                        #endregion

                        #region retencion por facturas
                        if (info_ats.lst_retenciones != null)
                        {
                            if (info_ats.lst_retenciones.Count() > 0)
                            {
                                var lstret_x_fac = info_ats.lst_retenciones.Where(r => r.Cedula_ruc == comp.idProv & r.co_serie == comp.establecimiento + "-" + comp.puntoEmision & comp.secuencial == r.co_factura);
                                if (lstret_x_fac != null)
                                {
                                    if (lstret_x_fac.Count() > 0)
                                    {
                                        comp_det.air = new List <detalleAir>();
                                        foreach (var item in lstret_x_fac)
                                        {
                                            if (item.re_tipo_Ret == "RTF")
                                            {
                                                comp_det.air.Add(new detalleAir
                                                {
                                                    codRetAir     = item.codRetAir.ToString(),
                                                    baseImpAir    = item.baseImpAir.ToString(),
                                                    porcentajeAir = item.porcentajeAir.ToString(),
                                                    valRetAir     = item.valRetAir.ToString(),
                                                });
                                            }
                                            else
                                            {
                                                if (item.porcentajeAir == "10")
                                                {
                                                    comp_det.valRetBien10 = item.valRetAir.ToString();
                                                }
                                                if (item.porcentajeAir == "20")
                                                {
                                                    comp_det.valRetServ20 = item.valRetAir.ToString();
                                                }
                                                if (item.porcentajeAir == "30")
                                                {
                                                    comp_det.valorRetBienes = item.valRetAir.ToString();
                                                }
                                                if (item.porcentajeAir == "50")
                                                {
                                                    comp_det.valRetServ50 = item.valRetAir.ToString();
                                                }
                                                if (item.porcentajeAir == "70")
                                                {
                                                    comp_det.valorRetServicios = item.valRetAir.ToString();
                                                }
                                                if (item.porcentajeAir == "100")
                                                {
                                                    comp_det.valRetServ100 = item.valRetAir.ToString();
                                                }
                                            }
                                        }
                                    }
                                    else   // si no tiene retencion
                                    {
                                        comp_det.air = new List <detalleAir>();
                                        comp_det.air.Add(new detalleAir
                                        {
                                            codRetAir     = "332",
                                            baseImpAir    = Convert.ToDecimal(comp.baseImpGrav + comp.baseImponible).ToString(),
                                            porcentajeAir = "0",
                                            valRetAir     = "0.00",
                                        });
                                    }
                                }
                            }
                        }
                        #endregion
                        ats.compras.Add(comp_det);
                    });
                }
                #endregion

                #region Ventas
                if (info_ats.lst_ventas != null)
                {
                    #region Agrupando clintes



                    var lst_ventas_x_cliente = (from q in info_ats.lst_ventas

                                                group q by new
                    {
                        q.idCliente,
                        q.DenoCli,
                        q.tipoCliente,
                        q.tipoComprobante,
                        q.tpIdCliente
                    }
                                                into g
                                                select new ventas_Info
                    {
                        idCliente = g.Key.idCliente,
                        DenoCli = g.Key.DenoCli,
                        tipoCliente = g.Key.tipoCliente,
                        tpIdCliente = g.Key.tpIdCliente,
                        tipoComprobante = g.Key.tipoComprobante,
                        baseNoGraIva = g.Sum(y => y.baseNoGraIva),
                        baseImponible = g.Sum(y => y.baseImponible),
                        baseImpGrav = g.Sum(y => y.baseImpGrav),
                        montoIva = g.Sum(y => y.montoIva),
                        montoIce = g.Sum(y => y.montoIce),
                        valorRetIva = g.Sum(y => y.valorRetIva),
                        valorRetRenta = g.Sum(y => y.valorRetRenta),
                        numeroComprobantes = info_ats.lst_ventas.Count()
                    }).ToList();


                    if (lst_ventas_x_cliente.Count > 0)
                    {
                        ats.ventas = new List <detalleVentas>();
                        lst_ventas_x_cliente.ForEach(
                            vent =>
                        {
                            detalleVentas det_ventas         = new detalleVentas();
                            det_ventas.tpIdCliente           = vent.tpIdCliente;
                            det_ventas.idCliente             = vent.idCliente;
                            det_ventas.parteRelVtas          = parteRelType.NO;
                            det_ventas.parteRelVtasSpecified = true;

                            if (vent.tipoCliente == " 01" && vent.idCliente != "9999999999999")
                            {
                                det_ventas.tipoCliente = vent.tipoCliente;
                            }

                            if (det_ventas.idCliente == "9999999999999")
                            {
                                det_ventas.tpIdCliente           = "07";
                                det_ventas.parteRelVtasSpecified = false;
                            }
                            if (det_ventas.tpIdCliente == "06")
                            {
                                det_ventas.tipoCliente = vent.tipoCliente;
                                det_ventas.denoCli     = vent.DenoCli;
                            }

                            vent.DenoCli = cl_funciones.QuitartildesEspaciosPuntos(vent.DenoCli);
                            det_ventas.tipoComprobante    = "18";
                            det_ventas.tipoEmision        = tipoEmisionType.F;
                            det_ventas.numeroComprobantes = vent.numeroComprobantes.ToString();
                            det_ventas.baseNoGraIva       = vent.baseNoGraIva;
                            det_ventas.baseImponible      = vent.baseImponible;
                            det_ventas.baseImpGrav        = vent.baseImpGrav;
                            det_ventas.montoIva           = vent.montoIva;
                            det_ventas.montoIce           = vent.montoIce;
                            det_ventas.valorRetIva        = vent.valorRetIva.ToString("n2");
                            det_ventas.valorRetRenta      = vent.valorRetRenta.ToString("n2");
                            det_ventas.montoIceSpecified  = true;
                            det_ventas.formasDePago       = null;
                            string[] AFormaPago           = { "20" };
                            det_ventas.formasDePago       = AFormaPago;

                            ats.ventas.Add(det_ventas);
                        }
                            );
                    }
                }



                #endregion



                #endregion

                #region ventas por establecimientos
                if (info_ats.lst_ventas != null)
                {
                    if (info_ats.lst_ventas.Count() > 0)
                    {
                        ats.ventasEstablecimiento = new List <ventaEst>();

                        var vtas = info_ats.lst_ventas.GroupBy(x => x.codEstab)
                                   .Select(x => new
                        {
                            codEstab    = x.Key,
                            ventasEstab = x.Sum(y => y.ventasEstab)
                        }).ToList();

                        foreach (var item in vtas)
                        {
                            ventaEst vtas_esta = new ventaEst();
                            vtas_esta.codEstab    = item.codEstab;
                            vtas_esta.ventasEstab = item.ventasEstab;
                            vtas_esta.ivaComp     = Convert.ToDecimal("0.00");
                            ats.ventasEstablecimiento.Add(vtas_esta);
                        }
                    }
                }
                #endregion

                #region exportaciones
                if (info_ats.lst_exportaciones != null)
                {
                    if (info_ats.lst_exportaciones.Count() > 0)
                    {
                        ats.exportaciones = new List <detalleExportacionesType>();
                        info_ats.lst_exportaciones.ForEach
                        (
                            exp =>
                        {
                            detalleExportacionesType exp_det = new detalleExportacionesType();
                            exp_det.tpIdClienteEx            = exp.tpIdClienteEx;
                            exp_det.idClienteEx         = exp.idClienteEx;
                            exp_det.parteRelExp         = parteRelType.NO;
                            exp_det.tipoCli             = "02";
                            exp_det.denoExpCli          = exp.denoExpCli = cl_funciones.QuitartildesEspaciosPuntos(exp.denoExpCli);
                            exp_det.tipoRegi            = tipoRegiType.Item01;
                            exp_det.paisEfecPagoGen     = exp.paisEfecPagoGen;
                            exp_det.paisEfecExp         = exp.paisEfecExp;
                            exp_det.exportacionDe       = exp.exportacionDe;
                            exp_det.tipoComprobante     = exp.tipoComprobante;
                            exp_det.fechaEmbarque       = exp.fechaEmbarque.ToString().Substring(0, 10);
                            exp_det.valorFOB            = (exp.valorFOB) == null?Convert.ToDecimal(0.00):Convert.ToDecimal(exp.valorFOB);
                            exp_det.valorFOBComprobante = (exp.valorFOB) == null ? Convert.ToDecimal(0.00) : Convert.ToDecimal(exp.valorFOBComprobante);
                            exp_det.establecimiento     = exp.establecimiento;
                            exp_det.puntoEmision        = exp.puntoEmision;
                            exp_det.secuencial          = exp.secuencial;
                            exp_det.autorizacion        = (exp.autorizacion) == null?"34345454656453":exp.autorizacion;
                            exp_det.fechaEmision        = exp.fechaEmision.ToString().Substring(0, 10);
                            //exp_det.pagoRegFisSpecified = true;
                            exp_det.parteRelExpSpecified = true;
                            exp_det.tipoRegiSpecified    = true;
                            //exp_det.impuestoOtroPaisSpecified = true;
                            //exp_det.ingExtGravOtroPaisSpecified = true;
                            ats.exportaciones.Add(exp_det);
                        }
                        );
                    }
                }
                #endregion

                #region Anulados
                if (info_ats.lst_anulados != null)
                {
                    if (info_ats.lst_anulados.Count() > 0)
                    {
                        ats.anulados = new List <detalleAnulados>();
                        info_ats.lst_anulados.ForEach
                        (
                            anu =>
                        {
                            detalleAnulados anula  = new detalleAnulados();
                            anula.tipoComprobante  = anu.tipoComprobante;
                            anula.establecimiento  = anu.Establecimiento;
                            anula.puntoEmision     = anu.puntoEmision;
                            anula.secuencialInicio = anu.secuencialInicio;
                            anula.secuencialFin    = anu.secuencialFin;
                            anula.autorizacion     = anu.Autorizacion;
                            ats.anulados.Add(anula);
                        }
                        );
                    }
                }
                #endregion


                if (info_ats.lst_ventas.Count == 0)
                {
                    ats.totalVentasSpecified = false;
                }
                else
                {
                    ats.numEstabRuc = info_ats.lst_ventas.GroupBy(x => x.codEstab)
                                      .Select(x => new
                    {
                        codEstab    = x.Key,
                        ventasEstab = x.Sum(y => y.ventasEstab)
                    }).Count().ToString().PadLeft(3, '0');
                    ats.totalVentasSpecified = true;
                }
                return(ats);
            }
            catch (Exception)
            {
                throw;
            }
        }