public DataTable GetByIdFacturaDT(int idFactura)
        {
            try
            {
                int controlImpagas = 0;

                using (cooperativaEntities bd = new cooperativaEntities())
                {

                    var Listar = (from f in bd.facturas

                                  join e in bd.cod_estados_pago on f.id_estadoPago equals e.id_estado_pago
                                  join m in bd.socios_mediciones on f.id_medicion equals m.id_medicion
                                  join pr in bd.periodos on f.id_periodo equals pr.id_periodo
                                  #region
                                  join detalle1 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 23  //IVA
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe,
                                           importea = d != null ? (int?)d.importe : null
                                       }) on f.id_factura equals detalle1.id_factura
                                  into temp
                                  from i in temp.DefaultIfEmpty()

                                  join detalle2 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 18  //Recargo
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle2.id_factura
                                  into temp2
                                  from r in temp2.DefaultIfEmpty()

                                  join detalle3 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 20  //IVARECARGO
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle3.id_factura
                                  into temp3
                                  from ir in temp3.DefaultIfEmpty()

                                  join detalle4 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 21  //DESCUENTO
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle4.id_factura
                                  into temp4
                                  from desc in temp4.DefaultIfEmpty()

                                  join detalle5 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 22   //IVADESCUENTO
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle5.id_factura
                                  into temp5
                                  from idesc in temp5.DefaultIfEmpty()

                                  join detalle6 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 5   //CONVENIOS
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle6.id_factura
                                  into temp6
                                  from fconv in temp6.DefaultIfEmpty()

                                  join detalle7 in
                                      (from d in bd.facturas_detalles
                                       where d.id_concepto == 4   //ACCIONES
                                       select new
                                       {
                                           d.id_factura,
                                           d.importe
                                       }) on f.id_factura equals detalle7.id_factura
                                 into temp7
                                  from facc in temp7.DefaultIfEmpty()

                                  join yr in
                                      (from fx in bd.facturas
                                       join mx in bd.socios_mediciones on fx.id_medicion equals mx.id_medicion

                                       where fx.id_socio == idFactura
                                       orderby fx.id_factura descending
                                       select new
                                       {
                                           fx.id_factura,
                                           mx.lectura,
                                           fx.id_socio
                                       }) on idFactura equals yr.id_factura
                                    into tempo
                                  from df in tempo.Take(1).DefaultIfEmpty()

                                  #endregion
                                  where f.id_factura == idFactura
                                  select new
                                  {
                                      Periodo = f.id_periodo,
                                      Factura = f.id_factura,
                                      FechaFact = f.fechaPago,
                                      FechaVenc = pr.fecha_primer_venc,
                                      idEstado = e.id_estado_pago,
                                      Estado = e.estado_pago,
                                      Total = f.importeTotal,
                                      Neto = f.importeNeto,
                                      IVA = (Decimal?)i.importe,

                                      Recargo = (Decimal?)r.importe,

                                      IVARecargo = (Decimal?)ir.importe,

                                      Descuento = (Decimal?)desc.importe,

                                      IVADescuento = (Decimal?)idesc.importe,

                                      Cobrado = f.cobrado,
                                      Consumo = m.consumo,
                                      Lectura = m.lectura,

                                      LecturaAnt = (Int32?)df.lectura,
                                      DiasVenc = 0,

                                      // Status = aa == null ? (bool?)null : aa.Online;
                                      Convenio = (Decimal?)fconv.importe==null? 0 :(Decimal?)fconv.importe,
                                      Acciones = (Decimal?)facc.importe == null ? 0 : (Decimal?)facc.importe

                                  }).ToList();

                    Commons oCommons = new Commons();
                    DataTable FacturasDetalles = oCommons.convertToTable(Listar);

                    foreach (DataRow rowFactDet in FacturasDetalles.Rows)
                    {
                        CalculosFactura oCalculosFactura = new CalculosFactura();

                        if (int.Parse(rowFactDet["idEstado"].ToString()) == 3)
                        {
                            //paga muestro lo de la fact. como lo hice al principio

                        }
                        else
                        {
                            rowFactDet["Recargo"] = oCalculosFactura.CalcularRecargo(idFactura);
                            rowFactDet["IVARecargo"] = oCalculosFactura.CalcularIVARecargo(idFactura);
                            rowFactDet["Total"] = decimal.Parse(rowFactDet["Total"].ToString()) + decimal.Parse(rowFactDet["Recargo"].ToString()) + decimal.Parse(rowFactDet["IVARecargo"].ToString());

                            rowFactDet["Descuento"] = oCalculosFactura.CalcularDescuento(idFactura);
                            rowFactDet["IVADescuento"] = oCalculosFactura.CalcularIVADescuento(idFactura);
                        }

                        rowFactDet["LecturaAnt"] = oCalculosFactura.ObtenerLecturaAnterior(idFactura);

                        rowFactDet["FechaVenc"] = oCalculosFactura.ObtenerFechaVenc(idFactura);

                        rowFactDet["DiasVenc"] = oCalculosFactura.ObtenerDiasDeVencimiento(idFactura);
                    }
                    return FacturasDetalles;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }
        public Decimal? GetTotalIVARecargoBySocio(int idSocio, int mostrarImpagas)
        {
            decimal totalIVARecargo = 0;
            Commons oCommons = new Commons();
            int controlImpagas = 0;
            if (mostrarImpagas == 1)
            {
                controlImpagas = 3;
            }
            using (cooperativaEntities bd = new cooperativaEntities())
            {
                var detallesByFact = (from fac in bd.facturas

                                      where fac.id_socio == idSocio && (fac.id_estadoPago < controlImpagas | controlImpagas == 0)
                                      select new
                                      {
                                          fac.id_factura
                                      }).ToList();

                DataTable detallesCalc = oCommons.convertToTable(detallesByFact);
                CalculosFactura oCalculosFactura = new CalculosFactura();
                foreach (DataRow rowDet in detallesCalc.Rows)
                {
                    totalIVARecargo = totalIVARecargo + oCalculosFactura.CalcularIVARecargo(int.Parse(rowDet["id_factura"].ToString()));
                }

                return totalIVARecargo;

            }
        }
Esempio n. 3
0
        private void CargarDatos()
        {
            if (txtNroFactura.Text.Length > 0) {

                facturas oFacturas = new facturas();
                socios oSocio = new socios();
                SocioImplement oSocioImplement = new SocioImplement();
                FacturasImplement oFacturasImplement = new FacturasImplement();
                PeriodosImplement oPeriodosImplement = new PeriodosImplement();
                FacturasDetallesImplement oFacturasDetallesImplement = new FacturasDetallesImplement();
                _idFactura = int.Parse(txtNroFactura.Text);
                if (oFacturasImplement.ExisteFactura(_idFactura))
                {
                    oFacturas = oFacturasImplement.Get(int.Parse(txtNroFactura.Text));
                    oSocio = oSocioImplement.Get((int)oFacturas.id_socio);
                    _idSocio = oSocio.id_socio;
                    txtCodigoSubCodSocio.Text = oSocio.codigo_socio + "/" + oSocio.subcodigo_socio;
                    txtNombreSocio.Text = oSocio.nombre;
                    txtVencimiento.Text = ((DateTime)oPeriodosImplement.Get(oFacturas.id_periodo).fecha_primer_venc).ToShortDateString();
                    //facturado = neto + iva
                    DataTable detallesFacturaDT = oFacturasImplement.GetByIdFacturaDT(oFacturas.id_factura);
                    decimal _totalFacturado = 0;
                    decimal _importeTotal = 0;
                    decimal _convenio = 0;
                    decimal _acciones = 0;
                    foreach (DataRow dr in detallesFacturaDT.Rows)
                    {
                        //IVA = decimal.Parse(dr["IVA"].ToString());
                        _totalFacturado = _totalFacturado + decimal.Parse(dr["Neto"].ToString()) + decimal.Parse(dr["IVA"].ToString());
                        _importeTotal = _importeTotal + decimal.Parse(dr["Total"].ToString());
                        _convenio = _convenio + decimal.Parse(dr["Convenio"].ToString());
                        _acciones = _acciones + decimal.Parse(dr["Acciones"].ToString());
                    }

                    txtFacturado.Text = _totalFacturado.ToString();
                    txtCobrado.Text = oFacturas.cobrado.ToString();
                    txtResto.Text = (_totalFacturado - (decimal.Parse(oFacturas.cobrado.ToString()))).ToString();
                    txtDescuento.Text = "0";
                    CalculosFactura oCalculosFacturas = new CalculosFactura();
                    txtRecargo.Text = (oCalculosFacturas.CalcularRecargo(oFacturas.id_factura) + oCalculosFacturas.CalcularIVARecargo(oFacturas.id_factura)).ToString();
                    txtTotal.Text = (_importeTotal - (decimal)oFacturas.cobrado - decimal.Parse(txtDescuento.Text)).ToString();
                    txtConvenio.Text = _convenio.ToString();
                    txtAcciones.Text = _acciones.ToString();

                    CobranzasImplement oCobranzasImplement = new CobranzasImplement();

                    txtFactIngresadas.Text = oCobranzasImplement.GetFacturas(dtpFechaCaja.Value).ToString();

                    txtTotalCobrado.Text = _importeTotal.ToString();
                    txtTotalFactura.Text = _importeTotal.ToString(); ;
                    txtDiferencia.Text = (decimal.Parse(txtTotalFactura.Text) - decimal.Parse(txtTotalCobrado.Text)).ToString();
                }
                else {
                    //si esta con cobranza entonces seria para cambiar la caja
                    if (oFacturasImplement.ExisteCobranza(_idFactura))
                    {
                        _existeCobranza = true;
                    }
                    else
                    {
                        _existeCobranza = false;
                    }
                }
            }
        }