public int Add(int FACTURAId, int PRESTACIONId, int MONTO_TOTAL, int MONTO_COBRADO)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _repositorioFACTURA = new RepositorioFACTURA(context);
                    FACTURA _objFACTURA = _repositorioFACTURA.GetById(FACTURAId);
                    if(Equals(_objFACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",FACTURAId.ToString()));
                    }

                    RepositorioPRESTACION _repositorioPRESTACION = new RepositorioPRESTACION(context);
                    PRESTACION _objPRESTACION = _repositorioPRESTACION.GetById(PRESTACIONId);
                    if(Equals(_objPRESTACION,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado PRESTACION con Id =",PRESTACIONId.ToString()));
                    }

                    FACTURA_DETALLE _FACTURA_DETALLE = new FACTURA_DETALLE();

                    //properties

                    _FACTURA_DETALLE.MONTO_TOTAL = MONTO_TOTAL;
                    _FACTURA_DETALLE.MONTO_COBRADO = MONTO_COBRADO;
                    _FACTURA_DETALLE.ACTIVO = true;

                    //parents

                    _FACTURA_DETALLE.FACTURA = _objFACTURA;
                    _FACTURA_DETALLE.PRESTACION = _objPRESTACION;

                    context.AddObject("FACTURA_DETALLE",_FACTURA_DETALLE);
                    context.SaveChanges();

                    return _FACTURA_DETALLE.ID;
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public int Add(int NOTA_COBROId, int FACTURAId, int MONTO_PENDIENTE)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioNOTA_COBRO _repositorioNOTA_COBRO = new RepositorioNOTA_COBRO(context);
                    NOTA_COBRO _objNOTA_COBRO = _repositorioNOTA_COBRO.GetById(NOTA_COBROId);
                    if(Equals(_objNOTA_COBRO,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado NOTA_COBRO con Id =",NOTA_COBROId.ToString()));
                    }

                    RepositorioFACTURA _repositorioFACTURA = new RepositorioFACTURA(context);
                    FACTURA _objFACTURA = _repositorioFACTURA.GetById(FACTURAId);
                    if(Equals(_objFACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",FACTURAId.ToString()));
                    }

                    NOTA_COBRO_DETALLE _NOTA_COBRO_DETALLE = new NOTA_COBRO_DETALLE();

                    //properties

                    _NOTA_COBRO_DETALLE.MONTO_PENDIENTE = MONTO_PENDIENTE;
                    _NOTA_COBRO_DETALLE.ACTIVO = true;

                    //parents

                    _NOTA_COBRO_DETALLE.NOTA_COBRO = _objNOTA_COBRO;
                    _NOTA_COBRO_DETALLE.FACTURA = _objFACTURA;

                    context.AddObject("NOTA_COBRO_DETALLE",_NOTA_COBRO_DETALLE);
                    context.SaveChanges();

                    return _NOTA_COBRO_DETALLE.ID;
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public void AnularFacturas(List<int> LISTA_ID_FACTURA)
        {
            Init();
            //ListaDetalleFactura = new List<DTO_REPORTE_DETALLEFACTURA_PRESTACION>();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    foreach (int ID_FACTURA in LISTA_ID_FACTURA)
                    {
                        FACTURA _FACTURA = _RepositorioFACTURA.GetByIdWithReferences(ID_FACTURA);
                        if (_FACTURA == null)
                            throw new Exception("No se encuentra informacion de la factura");

                        if (_FACTURA.NUMERO_FACTURA.HasValue)
                            throw new Exception("La factura ya ha sido numerada");

                        if (_FACTURA.NOTA_CREDITO.Any(nc => nc.ACTIVO))
                            throw new Exception("La factura tiene notas de créditos asociadas");

                        _FACTURA.ACTIVO = false;
                        foreach (FACTURA_DETALLE _FACTURA_DETALLE in _FACTURA.FACTURA_DETALLE.Where(fd => fd.ACTIVO))
                        {
                            _FACTURA_DETALLE.ACTIVO = false;
                            context.ApplyPropertyChanges("FACTURA_DETALLE", _FACTURA_DETALLE);
                        }
                        context.ApplyPropertyChanges("FACTURA", _FACTURA);

                        context.SaveChanges();

                    }

                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public int Add(int FACTURAId, System.DateTime FECHA_EMISION, int NUMERO_NOTA_CREDITO, bool CORRECCION_TOTAL_PARCIAL)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _repositorioFACTURA = new RepositorioFACTURA(context);
                    FACTURA _objFACTURA = _repositorioFACTURA.GetById(FACTURAId);
                    if(Equals(_objFACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",FACTURAId.ToString()));
                    }

                    NOTA_CREDITO _NOTA_CREDITO = new NOTA_CREDITO();

                    //properties

                    _NOTA_CREDITO.FECHA_EMISION = FECHA_EMISION;
                    _NOTA_CREDITO.NUMERO_NOTA_CREDITO = NUMERO_NOTA_CREDITO;
                    _NOTA_CREDITO.CORRECCION_TOTAL_PARCIAL = CORRECCION_TOTAL_PARCIAL;
                    _NOTA_CREDITO.ACTIVO = true;

                    //parents

                    _NOTA_CREDITO.FACTURA = _objFACTURA;

                    context.AddObject("NOTA_CREDITO",_NOTA_CREDITO);
                    context.SaveChanges();

                    return _NOTA_CREDITO.ID;
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
Example #5
0
        public void Update(int Id, int CLIENTEId, int FACTURACIONId, int TIPO_FACTURAId, int CORRELATIVO, string RUT_LABORATORIO, int NETO, int IVA, int TOTAL, string NOMBRE_CLIENTE = "", string RUT_CLIENTE = "", string DIRECCION = "", string NOMBRE_COMUNA = "", int? NUMERO_FACTURA = null, int? DESCUENTO = null, string FONO = "", string GIRO = "", string DETALLE = "", bool? PAGADA = null)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA repositorio = new RepositorioFACTURA(context);
                    FACTURA _FACTURA = repositorio.GetById(Id);
                    if(Equals(_FACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",Id.ToString()));
                    }

                    RepositorioCLIENTE _repositorioCLIENTE = new RepositorioCLIENTE(context);
                    CLIENTE _objCLIENTE = _repositorioCLIENTE.GetById(CLIENTEId);
                    if(Equals(_objCLIENTE,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado CLIENTE con Id =",CLIENTEId.ToString()));
                    }

                    RepositorioFACTURACION _repositorioFACTURACION = new RepositorioFACTURACION(context);
                    FACTURACION _objFACTURACION = _repositorioFACTURACION.GetById(FACTURACIONId);
                    if(Equals(_objFACTURACION,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURACION con Id =",FACTURACIONId.ToString()));
                    }

                    RepositorioTIPO_FACTURA _repositorioTIPO_FACTURA = new RepositorioTIPO_FACTURA(context);
                    TIPO_FACTURA _objTIPO_FACTURA = _repositorioTIPO_FACTURA.GetById(TIPO_FACTURAId);
                    if(Equals(_objTIPO_FACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado TIPO_FACTURA con Id =",TIPO_FACTURAId.ToString()));
                    }

                    //properties

                        _FACTURA.CORRELATIVO = CORRELATIVO;
                    if (!string.IsNullOrEmpty(NOMBRE_CLIENTE))
                    {
                        _FACTURA.NOMBRE_CLIENTE = NOMBRE_CLIENTE;
                    }
                    if (!string.IsNullOrEmpty(RUT_CLIENTE))
                    {
                        _FACTURA.RUT_CLIENTE = RUT_CLIENTE;
                    }
                    if (!string.IsNullOrEmpty(DIRECCION))
                    {
                        _FACTURA.DIRECCION = DIRECCION;
                    }
                    if (!string.IsNullOrEmpty(NOMBRE_COMUNA))
                    {
                        _FACTURA.NOMBRE_COMUNA = NOMBRE_COMUNA;
                    }
                    if (NUMERO_FACTURA.HasValue)
                    {
                        _FACTURA.NUMERO_FACTURA = NUMERO_FACTURA.Value;
                    }
                    if (!string.IsNullOrEmpty(RUT_LABORATORIO))
                    {
                        _FACTURA.RUT_LABORATORIO = RUT_LABORATORIO;
                    }
                    if (DESCUENTO.HasValue)
                    {
                        _FACTURA.DESCUENTO = DESCUENTO.Value;
                    }
                    if (!string.IsNullOrEmpty(FONO))
                    {
                        _FACTURA.FONO = FONO;
                    }
                    if (!string.IsNullOrEmpty(GIRO))
                    {
                        _FACTURA.GIRO = GIRO;
                    }
                    if (!string.IsNullOrEmpty(DETALLE))
                    {
                        _FACTURA.DETALLE = DETALLE;
                    }
                        _FACTURA.NETO = NETO;
                        _FACTURA.IVA = IVA;
                        _FACTURA.TOTAL = TOTAL;
                    if (PAGADA.HasValue)
                    {
                        _FACTURA.PAGADA = PAGADA.Value;
                    }

                    //parents

                    _FACTURA.CLIENTE = _objCLIENTE;
                    _FACTURA.FACTURACION = _objFACTURACION;
                    _FACTURA.TIPO_FACTURA = _objTIPO_FACTURA;

                    context.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                 throw ex;
            }
        }
Example #6
0
 public FACTURA GetByIdWithReferences(int ID)
 {
     Init();
     try
     {
         using (LQCEEntities context = new LQCEEntities())
         {
             RepositorioFACTURA repositorio = new RepositorioFACTURA(context);
             return repositorio.GetByIdWithReferences(ID);
         }
     }
     catch (Exception ex)
     {
          ISException.RegisterExcepcion(ex);
         Error = ex.Message;
         throw ex;
     }
 }
Example #7
0
 public List<FACTURA> GetByFilterWithReferences(int? CLIENTEId = null, int? FACTURACIONId = null, int? TIPO_FACTURAId = null, int? CORRELATIVO = null, string RUT_LABORATORIO = "", int? NETO = null, int? IVA = null, int? TOTAL = null, string NOMBRE_CLIENTE = "", string RUT_CLIENTE = "", string DIRECCION = "", string NOMBRE_COMUNA = "", int? NUMERO_FACTURA = null, int? DESCUENTO = null, string FONO = "", string GIRO = "", string DETALLE = "", bool? PAGADA = null)
 {
     Init();
     try
     {
          using (LQCEEntities context = new LQCEEntities())
         {
             RepositorioFACTURA repositorio = new RepositorioFACTURA(context);
             return repositorio.GetByFilterWithReferences(CLIENTEId, FACTURACIONId, TIPO_FACTURAId, CORRELATIVO, RUT_LABORATORIO, NETO, IVA, TOTAL, NOMBRE_CLIENTE, RUT_CLIENTE, DIRECCION, NOMBRE_COMUNA, NUMERO_FACTURA, DESCUENTO, FONO, GIRO, DETALLE, PAGADA).OrderBy(i => i.ID).ToList();
         }
     }
     catch (Exception ex)
     {
          ISException.RegisterExcepcion(ex);
         Error = ex.Message;
         throw ex;
     }
 }
Example #8
0
 public List<FACTURA> GetAllWithReferences()
 {
     Init();
     try
     {
         using (LQCEEntities context = new LQCEEntities())
         {
             RepositorioFACTURA repositorio = new RepositorioFACTURA(context);
                                 return repositorio.GetAllWithReferences().OrderBy(i => i.ID).ToList();
                             }
     }
     catch (Exception ex)
        {
          ISException.RegisterExcepcion(ex);
         Error = ex.Message;
         throw ex;
     }
 }
Example #9
0
        public void Delete(int Id)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA repositorio = new RepositorioFACTURA(context);
                    FACTURA _FACTURA = repositorio.GetById(Id);

                    if(Equals(_FACTURA ,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",Id.ToString()));
                    }

                    _FACTURA.ACTIVO = false;

                    context.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                 throw ex;
            }
        }
        public void NumerarFacturas(int ID_FACTURACION, int ID_TIPO_FACTURA, bool NUMERAR_TODAS,
            int? CORRELATIVO_DESDE, int? CORRELATIVO_HASTA, int NUMERO_FACTURA_INICIAL)
        {
            Init();
            ListaDetalleFactura = new List<DTO_REPORTE_DETALLEFACTURA_PRESTACION>();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    var q = _RepositorioFACTURA.GetByFilterWithReferences(null, ID_FACTURACION, ID_TIPO_FACTURA,
                        null, "", null, null, null, "", "", "", "", null, null, "", "", "", null);

                    if (!NUMERAR_TODAS)
                    {
                        if (!CORRELATIVO_DESDE.HasValue)
                            throw new Exception("Debe señalar factura inicial a facturar");
                        if (!CORRELATIVO_HASTA.HasValue)
                            throw new Exception("Debe señalar factura final a facturar");
                        if (CORRELATIVO_DESDE.Value > CORRELATIVO_HASTA.Value)
                            throw new Exception("El rango de facturas está mal definido, el valor inicial es mayor al valor final");

                        q = q.Where(f => f.CORRELATIVO >= CORRELATIVO_DESDE.Value && f.CORRELATIVO <= CORRELATIVO_HASTA.Value);
                    }

                    if (q.Any(f => f.NUMERO_FACTURA.HasValue))
                        throw new Exception("Ya existen facturas numeradas en el rango seleccionado");

                    int NUMERO_FACTURA_FINAL = NUMERO_FACTURA_INICIAL + q.Count() - 1;

                    var q2 = _RepositorioFACTURA.GetByFilterWithReferences(null, null, ID_TIPO_FACTURA,
                        null, "", null, null, null, null, null, "", "", null, null, "", "", "", null);
                    if (q2.Any(f => f.NUMERO_FACTURA.HasValue
                        && f.NUMERO_FACTURA >= NUMERO_FACTURA_INICIAL
                        && f.NUMERO_FACTURA <= NUMERO_FACTURA_FINAL))
                        throw new Exception("Ya existen facturas numeradas con los numeros de facturas indicados");

                    int NUEVO_NUMERO_FACTURA = NUMERO_FACTURA_INICIAL;
                    foreach (var _FACTURA in q.OrderBy(f => f.CORRELATIVO).ToList())
                    {
                        if (_FACTURA.NUMERO_FACTURA.HasValue)
                            throw new Exception("Factura ya está numerada");

                        _FACTURA.NUMERO_FACTURA = NUEVO_NUMERO_FACTURA;
                        context.ApplyPropertyChanges("FACTURA", _FACTURA);

                        NUEVO_NUMERO_FACTURA++;
                    }
                    context.SaveChanges();

                    try
                    {
                        foreach (var _FACTURA in q.OrderBy(f => f.CORRELATIVO).ToList())
                        {
                            // PENDIENTE: Generar PDFs
                            var LISTA_DTO_REPORTE_FACTURA = GetReporteFacturaByID_FACTURA(_FACTURA.ID);

                            ListaDetalleFactura = GetReporteDetalleFacturaByID_FACTURACION(_FACTURA.ID);

                            string deviceInfo =
                                          "<DeviceInfo>" +
                                          "  <OutputFormat>PDF</OutputFormat>" +
                                         "  <PageWidth>8.2in</PageWidth>" +
                                      "  <PageHeight>11.7in</PageHeight>" +
                                      "  <MarginTop>0in</MarginTop>" +
                                      "  <MarginLeft>0in</MarginLeft>" +
                                      "  <MarginRight>0in</MarginRight>" +
                                      "  <MarginBottom>0in</MarginBottom>" +
                                          "</DeviceInfo>";
                            Warning[] warnings;
                            m_streams_matriz = new List<Stream>();
                            m_streams_DetalleFactura = new List<Stream>();

                            // m_streams_individual = new List<Stream>();

                            // Documento 1: Un archivo por factura con fondo
                            var tf = from f in LISTA_DTO_REPORTE_FACTURA
                                     group f by f.NOMBRE_REPORTE_FACTURA_INDIVIDUAL into g
                                     select g;

                            foreach (var facturas in tf)
                            {
                                Hashtable propiedades = new Hashtable();
                                propiedades.Add("Fecha de Documento", _FACTURA.FACTURACION.FECHA_FACTURACION);
                                propiedades.Add("Tipo de Documento", "Factura " + facturas.FirstOrDefault().NOMBRE_TIPO_FACTURA);
                                propiedades.Add("Formato", "Individual");
                                propiedades.Add("RUT Cliente", facturas.FirstOrDefault().RUT_CLIENTE);
                                propiedades.Add("Nombre Cliente", facturas.FirstOrDefault().NOMBRE_CLIENTE);

                                ReportViewer _ReportViewer = new ReportViewer();
                                _ReportViewer.ProcessingMode = ProcessingMode.Local;
                                _ReportViewer.LocalReport.ShowDetailedSubreportMessages = true;
                                _ReportViewer.LocalReport.DataSources.Clear();
                                _ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", facturas));

                                _ReportViewer.LocalReport.ReportEmbeddedResource = "LQCE.Transaccion.Reporte." + facturas.Key;

                                _ReportViewer.LocalReport.Render("PDF", deviceInfo, CreateStream, out warnings);
                                foreach (Stream stream in m_streams_matriz)
                                    stream.Position = 0;

                                using (SPWeb spWeb = new SPSite(Settings.Default.SP_WEB).OpenWeb())
                                {
                                    SPList spList = spWeb.GetList(Settings.Default.SP_LIBRERIA_FACTURAS);
                                    string strNombreFactura = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + _FACTURA.NUMERO_FACTURA.Value.ToString() + "_" + facturas.Key + ".pdf";
                                    spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + strNombreFactura, m_streams_matriz[0], propiedades, true);
                                    spList.Update();
                                }
                            }

                            // Documento 2: Un archivo por cada detalles de facturas
                            List<DTO_REPORTE_DETALLEFACTURA_FACTURA> LISTA_DTO_REPORTE_DETALLEFACTURA_FACTURA =
                                (from df in ListaDetalleFactura
                                 group df by df.ID_FACTURA into g
                                 select new DTO_REPORTE_DETALLEFACTURA_FACTURA
                                 {
                                     ID_FACTURA = g.Key,
                                     ID_CLIENTE = g.FirstOrDefault().ID_CLIENTE,
                                     NOMBRE_CLIENTE = g.FirstOrDefault().NOMBRE_CLIENTE,
                                     RUT_CLIENTE = g.FirstOrDefault().RUT_CLIENTE,
                                     DETALLE = g.FirstOrDefault().DETALLE,
                                     SUMA_PENDIENTE = g.Where(p => p.ESTADO_PENDIENTE == "INPAGO" || p.ESTADO_PREVISION == "INPAGO").Sum(p => p.MONTO_TOTAL)
                                 }).ToList();

                            Hashtable propiedadesDetalle = new Hashtable();
                            propiedadesDetalle.Add("Fecha de Documento", _FACTURA.FACTURACION.FECHA_FACTURACION);
                            propiedadesDetalle.Add("Tipo de Documento", "Detalle de Factura");
                            propiedadesDetalle.Add("Formato", "Individual");
                            propiedadesDetalle.Add("RUT Cliente", _FACTURA.RUT_CLIENTE);
                            propiedadesDetalle.Add("Nombre Cliente", _FACTURA.NOMBRE_CLIENTE);

                            ReportViewer _ReportViewerDetalle = new ReportViewer();
                            _ReportViewerDetalle.ProcessingMode = ProcessingMode.Local;
                            _ReportViewerDetalle.LocalReport.ShowDetailedSubreportMessages = true;
                            _ReportViewerDetalle.LocalReport.DataSources.Clear();
                            _ReportViewerDetalle.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LISTA_DTO_REPORTE_DETALLEFACTURA_FACTURA));
                            _ReportViewerDetalle.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(ReporteDetalleFactura_SubreportProcessingEventHandler);
                            _ReportViewerDetalle.LocalReport.ReportEmbeddedResource = "LQCE.Transaccion.Reporte.DetalleFactura.rdlc";

                            _ReportViewerDetalle.LocalReport.Render("PDF", deviceInfo, CreateStreamDetalleFactura, out warnings);
                            foreach (Stream stream in m_streams_DetalleFactura)
                                stream.Position = 0;

                            using (SPWeb spWeb = new SPSite(Settings.Default.SP_WEB).OpenWeb())
                            {
                                SPList spList = spWeb.GetList(Settings.Default.SP_LIBRERIA_FACTURAS);
                                string strNombreFactura = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + _FACTURA.NUMERO_FACTURA.Value.ToString() + "_DetalleFactura.pdf";
                                spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + strNombreFactura, m_streams_DetalleFactura[0], propiedadesDetalle, true);
                                spList.Update();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // En caso de error, al generar los PDF se eliminan los registros de las facturas
                        foreach (var _FACTURA in q.OrderBy(f => f.CORRELATIVO).ToList())
                        {

                            _FACTURA.NUMERO_FACTURA = null;
                            context.ApplyPropertyChanges("FACTURA", _FACTURA);
                        }
                        context.SaveChanges();
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        /// <summary>
        /// Retorna todas las facturaciones realizadas y que tienen facturas por numerar
        /// </summary>
        /// <returns></returns>
        public List<DTO_RESUMEN_FACTURACION> GetResumenFacturacionPorNumerar()
        {
            Init();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    return (from f in _RepositorioFACTURA.GetAllWithReferences()
                            where f.ACTIVO && f.FACTURACION.ACTIVO
                            && f.FACTURACION.FACTURA.Any(fd => fd.ACTIVO && fd.NUMERO_FACTURA == null)
                            group f by new { ID_FACTURACION = f.FACTURACION.ID, ID_TIPO_FACTURA = f.TIPO_FACTURA.ID } into g
                            select new DTO_RESUMEN_FACTURACION
                            {
                                ID_FACTURACION = g.Key.ID_FACTURACION,
                                ID_TIPO_FACTURA = g.Key.ID_TIPO_FACTURA,
                                NOMBRE_TIPO_FACTURA = g.FirstOrDefault().TIPO_FACTURA.NOMBRE_FACTURA,
                                FECHA_FACTURACION = g.FirstOrDefault().FACTURACION.FECHA_FACTURACION,
                                TOTAL_FACTURAS = g.Count(),
                                TOTAL_FACTURAS_POR_NUMERAR = g.Count(fa => fa.NUMERO_FACTURA == null)
                            }).ToList();
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public void EmitirNotasCobros(DateTime FechaFacturacionDesde, DateTime FechaFacturacionHasta,
            int IdTipoCobro, int? IdCliente)
        {
            Init();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURACION _RepositorioFACTURACION = new RepositorioFACTURACION(context);
                    RepositorioTIPO_COBRO _RepositorioTIPO_COBRO = new RepositorioTIPO_COBRO(context);
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);
                    RepositorioCLIENTE _RepositorioCLIENTE = new RepositorioCLIENTE(context);
                    RepositorioVISTA_REPORTE_FACTURA _RepositorioVISTA_REPORTE_FACTURA = new RepositorioVISTA_REPORTE_FACTURA(context);

                    TIPO_COBRO _TIPO_COBRO = _RepositorioTIPO_COBRO.GetById(IdTipoCobro);
                    if (_TIPO_COBRO == null)
                        throw new Exception("No se encuentra información del tipo de cobro");

                    COBRO _COBRO = new COBRO();
                    _COBRO.FECHA_COBRO = DateTime.Now;
                    _COBRO.TIPO_COBRO = _TIPO_COBRO;
                    _COBRO.ACTIVO = true;
                    context.AddToCOBRO(_COBRO);

                    var cliente_facturas = (from f in _RepositorioFACTURACION.GetFacturasPorNotificar(FechaFacturacionDesde,
                        FechaFacturacionHasta, IdTipoCobro, IdCliente)
                                            group f by f.ID_CLIENTE into g
                                            select new
                                            {
                                                IdCliente = g.Key,
                                                Facturas = g
                                            }).ToList();

                    if (!cliente_facturas.Any())
                        throw new Exception("No hay facturas que notificar");

                    int correlativo = 1;
                    foreach (var cf in cliente_facturas)
                    {
                        CLIENTE _CLIENTE = _RepositorioCLIENTE.GetById(cf.IdCliente);
                        if (_CLIENTE == null)
                            throw new Exception("No se encuentra información del cliente");

                        NOTA_COBRO _NOTA_COBRO = new NOTA_COBRO();
                        _NOTA_COBRO.COBRO = _COBRO;
                        _NOTA_COBRO.CORRELATIVO = correlativo;
                        _NOTA_COBRO.CLIENTE = _CLIENTE;
                        _NOTA_COBRO.ACTIVO = true;
                        context.AddToNOTA_COBRO(_NOTA_COBRO);

                        foreach (var f in cf.Facturas)
                        {
                            FACTURA _FACTURA = _RepositorioFACTURA.GetById(f.ID);
                            if (_FACTURA == null)
                                throw new Exception("No se encuentra información de la factura ");

                            VISTA_REPORTE_FACTURA _VISTA_REPORTE_FACTURA = _RepositorioVISTA_REPORTE_FACTURA.GetById(f.ID);
                            if (_VISTA_REPORTE_FACTURA == null)
                                throw new Exception("No se encuentra información de la factura ");

                            NOTA_COBRO_DETALLE _NOTA_COBRO_DETALLE = new NOTA_COBRO_DETALLE();
                            _NOTA_COBRO_DETALLE.NOTA_COBRO = _NOTA_COBRO;
                            _NOTA_COBRO_DETALLE.FACTURA = _FACTURA;
                            if (_FACTURA.PAGADA.HasValue && _FACTURA.PAGADA.Value == true)
                            {
                                _NOTA_COBRO_DETALLE.MONTO_PENDIENTE = 0;
                            }
                            else
                            {
                                _NOTA_COBRO_DETALLE.MONTO_PENDIENTE = _VISTA_REPORTE_FACTURA.SALDO_DEUDOR ?? 0;
                            }
                            _NOTA_COBRO_DETALLE.ACTIVO = true;
                            context.AddToNOTA_COBRO_DETALLE(_NOTA_COBRO_DETALLE);
                        }

                        correlativo++;
                    }

                    context.SaveChanges();

                    try
                    {
                        ListaNotaCobro = GetReporteNotaCobroByID_COBRO(context, _COBRO.ID);

                        string deviceInfo =
                                      "<DeviceInfo>" +
                                      "  <OutputFormat>PDF</OutputFormat>" +
                                      "  <PageWidth>8.2in</PageWidth>" +
                                      "  <PageHeight>11.7in</PageHeight>" +
                                      "  <MarginTop>0in</MarginTop>" +
                                      "  <MarginLeft>0in</MarginLeft>" +
                                      "  <MarginRight>0in</MarginRight>" +
                                      "  <MarginBottom>0in</MarginBottom>" +
                                      "</DeviceInfo>";
                        Warning[] warnings;
                        m_streams_NotaCobro = new List<Stream>();

                        List<DTO_REPORTE_NOTA_COBRO> ListaNotaCobroEncabezado = (from nc in ListaNotaCobro
                                                                                 group nc by nc.CORRELATIVO into g
                                                                                 select new DTO_REPORTE_NOTA_COBRO
                                                                                 {
                                                                                     ID_COBRO = g.FirstOrDefault().ID_COBRO,
                                                                                     ID_CLIENTE = g.FirstOrDefault().ID_CLIENTE,
                                                                                     CORRELATIVO = g.Key,
                                                                                     NOMBRE_CLIENTE = g.FirstOrDefault().NOMBRE_CLIENTE,
                                                                                     RUT_CLIENTE = g.FirstOrDefault().RUT_CLIENTE,
                                                                                     NOMBRE_REPORTE = g.FirstOrDefault().NOMBRE_REPORTE,
                                                                                 }).ToList();

                        // Documento 1: Un archivo con todas las notas de cobro emitidas
                        Hashtable propiedades = new Hashtable();
                        propiedades.Add("Fecha de Documento", _COBRO.FECHA_COBRO);
                        propiedades.Add("Tipo de Documento", "Nota de Cobro " + ListaNotaCobroEncabezado.FirstOrDefault().NOMBRE_REPORTE);
                        propiedades.Add("Formato", "Consolidado");

                        ReportViewer _ReportViewer = new ReportViewer();
                        _ReportViewer.ProcessingMode = ProcessingMode.Local;
                        _ReportViewer.LocalReport.ShowDetailedSubreportMessages = true;
                        _ReportViewer.LocalReport.DataSources.Clear();
                        _ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ListaNotaCobroEncabezado));
                        _ReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(ReporteNotaCobro_SubreportProcessingEventHandler);
                        _ReportViewer.LocalReport.ReportEmbeddedResource = "LQCE.Transaccion.Reporte." + ListaNotaCobroEncabezado.FirstOrDefault().NOMBRE_REPORTE;

                        _ReportViewer.LocalReport.Render("PDF", deviceInfo, CreateStreamNotaCobro, out warnings);
                        foreach (Stream stream in m_streams_NotaCobro)
                            stream.Position = 0;

                        using (SPWeb spWeb = new SPSite(Settings.Default.SP_WEB).OpenWeb())
                        {
                            SPList spList = spWeb.GetList(Settings.Default.SP_LIBRERIA_FACTURAS);
                            string strNombreFactura = DateTime.Now.ToString("yyyyMMddhhmmss") + "_NotaCobro_" + _COBRO.TIPO_COBRO.NOMBRE + ".pdf";
                            spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + strNombreFactura, m_streams_NotaCobro[0], propiedades, true);
                            spList.Update();
                        }

                        foreach (var item in ListaNotaCobroEncabezado)
                        {
                            m_streams_NotaCobroIndividual = new List<Stream>();
                            // Documento 2: Un archivo por cada detalles de facturas
                            List<DTO_REPORTE_NOTA_COBRO> LISTA_DTO_REPORTE_NOTA_COBRO2 = new List<DTO_REPORTE_NOTA_COBRO>();
                            LISTA_DTO_REPORTE_NOTA_COBRO2.Add(item);

                            Hashtable propiedadesDetalle = new Hashtable();
                            propiedadesDetalle.Add("Fecha de Documento", _COBRO.FECHA_COBRO);
                            propiedadesDetalle.Add("Tipo de Documento", "Nota de Cobro " + item.NOMBRE_REPORTE);
                            propiedadesDetalle.Add("Formato", "Individual");
                            propiedadesDetalle.Add("RUT Cliente", item.RUT_CLIENTE);
                            propiedadesDetalle.Add("Nombre Cliente", item.NOMBRE_CLIENTE);

                            ReportViewer _ReportViewerDetalle = new ReportViewer();
                            _ReportViewerDetalle.ProcessingMode = ProcessingMode.Local;
                            _ReportViewerDetalle.LocalReport.ShowDetailedSubreportMessages = true;
                            _ReportViewerDetalle.LocalReport.DataSources.Clear();
                            _ReportViewerDetalle.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LISTA_DTO_REPORTE_NOTA_COBRO2));
                            _ReportViewerDetalle.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(ReporteNotaCobro_SubreportProcessingEventHandler);
                            _ReportViewerDetalle.LocalReport.ReportEmbeddedResource = "LQCE.Transaccion.Reporte." + item.NOMBRE_REPORTE;

                            _ReportViewerDetalle.LocalReport.Render("PDF", deviceInfo, CreateStreamNotaCobroIndividual, out warnings);
                            foreach (Stream stream in m_streams_NotaCobroIndividual)
                                stream.Position = 0;

                            using (SPWeb spWeb = new SPSite(Settings.Default.SP_WEB).OpenWeb())
                            {
                                SPList spList = spWeb.GetList(Settings.Default.SP_LIBRERIA_FACTURAS);
                                string strNombreFactura = DateTime.Now.ToString("yyyyMMddhhmmss") + "_NotaCobroIndividual_" + _COBRO.TIPO_COBRO.NOMBRE + "_" + item.NOMBRE_CLIENTE + ".pdf";
                                spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + strNombreFactura, m_streams_NotaCobroIndividual[0], propiedadesDetalle, true);
                                spList.Update();
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        RepositorioCOBRO _RepositorioCOBRO = new RepositorioCOBRO(context);
                        COBRO _NUEVO_COBRO = _RepositorioCOBRO.GetByIdWithReferences(_COBRO.ID);

                        _NUEVO_COBRO.ACTIVO = false;
                        foreach (var _NOTA_COBRO in _NUEVO_COBRO.NOTA_COBRO)
                        {
                            _NOTA_COBRO.ACTIVO = false;
                            foreach (var _NOTA_COBRO_DETALLE in _NOTA_COBRO.NOTA_COBRO_DETALLE)
                            {
                                _NOTA_COBRO_DETALLE.ACTIVO = false;
                                context.ApplyPropertyChanges("NOTA_COBRO_DETALLE", _NOTA_COBRO_DETALLE);
                            }
                            context.ApplyPropertyChanges("NOTA_COBRO", _NOTA_COBRO);
                        }
                        context.ApplyPropertyChanges("COBRO", _NUEVO_COBRO);
                        context.SaveChanges();
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public void EmitirNotaCredito(int IdFactura, int NumeroNotaCredito, bool CorreccionTotal)
        {
            Init();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    FACTURA _FACTURA = _RepositorioFACTURA.GetByIdWithReferences(IdFactura);
                    if (_FACTURA == null)
                        throw new Exception("No se encuentra informacion de la factura");

                    if (!_FACTURA.NUMERO_FACTURA.HasValue)
                        throw new Exception("La factura no ha sido numerada");

                    if (_FACTURA.PAGADA.HasValue && _FACTURA.PAGADA.Value == true)
                        throw new Exception("La factura ya ha sido pagada");

                    NOTA_CREDITO _NOTA_CREDITO = new NOTA_CREDITO();
                    _NOTA_CREDITO.FACTURA = _FACTURA;
                    _NOTA_CREDITO.FECHA_EMISION = DateTime.Now;
                    _NOTA_CREDITO.NUMERO_NOTA_CREDITO = NumeroNotaCredito;
                    _NOTA_CREDITO.CORRECCION_TOTAL_PARCIAL = CorreccionTotal;
                    _NOTA_CREDITO.ACTIVO = true;
                    context.AddToNOTA_CREDITO(_NOTA_CREDITO);

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        protected List<DTO_REPORTE_FACTURA> GetReporteFacturaByID_FACTURA(int IdFactura)
        {
            Init();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    FACTURA _FACTURA = _RepositorioFACTURA.GetByIdWithReferences(IdFactura);
                    if (_FACTURA == null)
                        throw new Exception("No se encuentra información de la factura");

                    DTO_REPORTE_FACTURA _DTO_REPORTE_FACTURA = new DTO_REPORTE_FACTURA();

                    _DTO_REPORTE_FACTURA.NOMBRE_REPORTE_FACTURA = _FACTURA.TIPO_FACTURA.NOMBRE_REPORTE_FACTURA;
                    _DTO_REPORTE_FACTURA.NOMBRE_REPORTE_FACTURA_INDIVIDUAL = _FACTURA.TIPO_FACTURA.NOMBRE_REPORTE_FACTURA_INDIVIDUAL;
                    _DTO_REPORTE_FACTURA.DIA = _FACTURA.FACTURACION.FECHA_FACTURACION.Day;
                    _DTO_REPORTE_FACTURA.MES = _FACTURA.FACTURACION.FECHA_FACTURACION.ToString("MMMM");
                    _DTO_REPORTE_FACTURA.AÑO = _FACTURA.FACTURACION.FECHA_FACTURACION.Year;
                    _DTO_REPORTE_FACTURA.NOMBRE_CLIENTE = _FACTURA.NOMBRE_CLIENTE;
                    _DTO_REPORTE_FACTURA.RUT_CLIENTE = _FACTURA.RUT_CLIENTE;
                    _DTO_REPORTE_FACTURA.DIRECCION = _FACTURA.DIRECCION;
                    _DTO_REPORTE_FACTURA.COMUNA = _FACTURA.NOMBRE_COMUNA;
                    _DTO_REPORTE_FACTURA.FONO = _FACTURA.FONO;
                    _DTO_REPORTE_FACTURA.GIRO = _FACTURA.GIRO;
                    _DTO_REPORTE_FACTURA.DETALLE = _FACTURA.DETALLE;
                    _DTO_REPORTE_FACTURA.NETO = _FACTURA.NETO;
                    _DTO_REPORTE_FACTURA.IVA = _FACTURA.IVA;
                    _DTO_REPORTE_FACTURA.TOTAL = _FACTURA.TOTAL;
                    _DTO_REPORTE_FACTURA.NUMERO_FACTURA = _FACTURA.NUMERO_FACTURA;
                    _DTO_REPORTE_FACTURA.NOMBRE_TIPO_FACTURA = _FACTURA.TIPO_FACTURA.NOMBRE_FACTURA;

                    List<DTO_REPORTE_FACTURA> lista = new List<DTO_REPORTE_FACTURA>();
                    lista.Add(_DTO_REPORTE_FACTURA);
                    return lista;
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
        public void BorrarNumeracionFacturas(int ID_FACTURACION, int ID_TIPO_FACTURA, int CORRELATIVO_DESDE, int CORRELATIVO_HASTA)
        {
            Init();
            //ListaDetalleFactura = new List<DTO_REPORTE_DETALLEFACTURA_PRESTACION>();
            try
            {
                using (LQCEEntities context = new LQCEEntities())
                {
                    if (CORRELATIVO_DESDE > CORRELATIVO_HASTA)
                        throw new Exception("El rango de facturas está mal definido, el valor inicial es mayor al valor final");

                    RepositorioFACTURA _RepositorioFACTURA = new RepositorioFACTURA(context);

                    var q = _RepositorioFACTURA.GetByFilterWithReferences(null, ID_FACTURACION, ID_TIPO_FACTURA,
                        null, "", null, null, null, "", "", "", "", null, null, "", "", "", null);
                    q = q.Where(f => f.CORRELATIVO >= CORRELATIVO_DESDE && f.CORRELATIVO <= CORRELATIVO_HASTA);

                    if (q.Any(f => f.NOTA_COBRO_DETALLE.Any(n => n.ACTIVO)))
                        throw new Exception("Ya existen facturas cobradas al cliente en el rango señalado");

                    if (q.Any(f => f.PAGADA.HasValue && f.PAGADA.Value == true))
                        throw new Exception("Ya existen facturas pagadas en el rango señalado");

                    if (q.Any(f => f.NOTA_CREDITO.Any(nc => nc.ACTIVO)))
                        throw new Exception("Existen facturas con notas de crédito en el rango señalado");

                    foreach (var _FACTURA in q.OrderBy(f => f.CORRELATIVO).ToList())
                    {
                        if (_FACTURA.NUMERO_FACTURA.HasValue)
                        {
                            _FACTURA.NUMERO_FACTURA = null;
                            context.ApplyPropertyChanges("FACTURA", _FACTURA);
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                throw ex;
            }
        }
Example #16
0
        public void Update(int Id, int FACTURAId, System.DateTime FECHA_EMISION, int NUMERO_NOTA_CREDITO, bool CORRECCION_TOTAL_PARCIAL)
        {
            Init();
            try
            {
                 using (LQCEEntities context = new LQCEEntities())
                {
                    RepositorioNOTA_CREDITO repositorio = new RepositorioNOTA_CREDITO(context);
                    NOTA_CREDITO _NOTA_CREDITO = repositorio.GetById(Id);
                    if(Equals(_NOTA_CREDITO,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado NOTA_CREDITO con Id =",Id.ToString()));
                    }

                    RepositorioFACTURA _repositorioFACTURA = new RepositorioFACTURA(context);
                    FACTURA _objFACTURA = _repositorioFACTURA.GetById(FACTURAId);
                    if(Equals(_objFACTURA,null))
                    {
                        throw new Exception(String.Concat("No se ha encontrado FACTURA con Id =",FACTURAId.ToString()));
                    }

                    //properties

                        _NOTA_CREDITO.FECHA_EMISION = FECHA_EMISION;
                        _NOTA_CREDITO.NUMERO_NOTA_CREDITO = NUMERO_NOTA_CREDITO;
                        _NOTA_CREDITO.CORRECCION_TOTAL_PARCIAL = CORRECCION_TOTAL_PARCIAL;

                    //parents

                    _NOTA_CREDITO.FACTURA = _objFACTURA;

                    context.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                 ISException.RegisterExcepcion(ex);
                Error = ex.Message;
                 throw ex;
            }
        }