/// <summary>
 /// Deprecated Method for adding a new object to the Factura EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToFactura(Factura factura)
 {
     base.AddObject("Factura", factura);
 }
 /// <summary>
 /// Create a new Factura object.
 /// </summary>
 /// <param name="idEmpresa">Initial value of the IdEmpresa property.</param>
 /// <param name="idUsuario">Initial value of the idUsuario property.</param>
 /// <param name="idNumeroFactura">Initial value of the IdNumeroFactura property.</param>
 /// <param name="idFormaPago">Initial value of the IdFormaPago property.</param>
 /// <param name="idEstado">Initial value of the idEstado property.</param>
 public static Factura CreateFactura(global::System.Int32 idEmpresa, global::System.Int32 idUsuario, global::System.Int32 idNumeroFactura, global::System.Int32 idFormaPago, global::System.Int32 idEstado)
 {
     Factura factura = new Factura();
     factura.IdEmpresa = idEmpresa;
     factura.idUsuario = idUsuario;
     factura.IdNumeroFactura = idNumeroFactura;
     factura.IdFormaPago = idFormaPago;
     factura.idEstado = idEstado;
     return factura;
 }
        private void toolStripGenerar_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtNumeroCliente.Text == "")
                {
                    MessageBox.Show("Ingrese Cliente.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (this.txtNumSRI.Text == "")
                {
                    MessageBox.Show("Ingrese Numero SRI.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (this.txtNumeroCotizacion.Text == "")
                {
                    MessageBox.Show("Ingrese Cotizacion.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (this.txtNumeroPromocion.Text == "")
                {
                    MessageBox.Show("Ingrese Promocion.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (this.txtPromocion.Text == "")
                {
                    MessageBox.Show("Ingrese Promocion.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (cbxFormaPago.SelectedValue == "")
                {
                    MessageBox.Show("Seleccione Forma de Pago.", "Modulo de Facturacion",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (this.tbldetalle.Rows.Count == 0)
                {
                    MessageBox.Show("Favor ingrese un articulo para su factura.", "Modulo de Facturacion",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Factura obj = new Factura();
                obj.IdNumeroFactura = Int32.Parse(txtNumeroFactura.Text);
               // obj.cabecera_comprobante = Int32.Parse(txtNumSRI.Text);
                obj.IdNumeroCotizacion = Int32.Parse(txtNumeroCotizacion.Text);
                obj.IdPromocion = Int32.Parse(txtNumeroPromocion.Text);
                obj.IdFormaPago = Int32.Parse(cbxFormaPago.SelectedValue.ToString());
                obj.IdNumeroCliente = Int32.Parse(txtNumeroCliente.Text);
                obj.Fecha = this.dtpFecha.Value;
                obj.ValorEntrada = Decimal.Parse (txtValorEntrada.Text);
                obj.NumeroCuotaMensual = Int32.Parse(txtNumeroCuotaMensual.Text);
                obj.Subtotal = txtsubtotal.Value;
                obj.Iva = txtiva.Value;
                obj.Ice = txtice.Value;
                obj.Descuento = txtdescuento.Value;
                obj.TotalPagar = txtTotalPagar.Value;

                obj.IdEmpresa = Seguridad.empresa;
                obj.idUsuario = Seguridad.usuario;
                obj.idEstado = Int32.Parse(cmbestado.SelectedValue.ToString());

                da.AddToFactura(obj);
                int respuesta = da.SaveChanges();
                double totalcosto = 0;

                if (respuesta > 0)
                {
                    // registrar en contabilidad
                    clsDatoComprobante objcontabilidad = new clsDatoComprobante();
                    clsCabeceraComprobante objcontdet = new clsCabeceraComprobante();
                    //objcontdet.fecha =

                    objcontdet.fecha = DateTime.Now;
                    objcontdet.glosa = "Venta Modulo de Facturacion ";
                    objcontdet.IdEmpresa = Seguridad.empresa;

                    List<clsDetalleComprobante> Detalleconta = new List<clsDetalleComprobante>();

                    int i = 0;
                    while (i < tbldetalle.Rows.Count)
                    {
                        FacturaDet objdet = new FacturaDet ();
                        objdet.IdEmpresa = Seguridad.empresa;
                        objdet.idUsuario = Seguridad.usuario;
                        objdet.idEstado = Int32.Parse(cmbestado.SelectedValue.ToString());

                        objdet.Linea = i + 1;
                        objdet.IdNumeroFactura = Int32.Parse(txtNumeroFactura.Text);
                        objdet.IdArticulo = Int32.Parse(tbldetalle.Rows[i][0].ToString());
                        objdet.CuotaMensual = Decimal.Parse(tbldetalle.Rows[i][2].ToString());
                        objdet.FechaPago = DateTime.Parse(tbldetalle.Rows[i][3].ToString());
                        objdet.FechaMaximaPago = DateTime.Parse(tbldetalle.Rows[i][4].ToString());
                        objdet.precio = Decimal.Parse(tbldetalle.Rows[i][5].ToString());
                        objdet.cantidad = Int32.Parse(tbldetalle.Rows[i][6].ToString());
                        objdet.costo = Decimal.Parse(tbldetalle.Rows[i][7].ToString());
                        da.AddToFacturaDet (objdet);
                        da.SaveChanges();
                        i++;
                        totalcosto = totalcosto + Double.Parse(objdet.costo.ToString());
                    }

                    if (obj.IdFormaPago == 1)
                    {
                        clsDetalleComprobante obcontdet = new clsDetalleComprobante();
                        obcontdet.cuenta = "11101001"; // caja
                        obcontdet.debe = Decimal.Parse(obj.TotalPagar.ToString());
                        obcontdet.haber = 0;
                        Detalleconta.Add(obcontdet);

                        clsDetalleComprobante obcontdet1 = new clsDetalleComprobante();
                        obcontdet1.cuenta = "11401001"; // iva
                        obcontdet1.debe = 0;
                        obcontdet1.haber = Decimal.Parse(obj.Iva.ToString());
                        Detalleconta.Add(obcontdet1);

                        clsDetalleComprobante obcontdet2 = new clsDetalleComprobante();
                        obcontdet2.cuenta = "11401003"; // ice
                        obcontdet2.debe = 0;
                        obcontdet2.haber = Decimal.Parse(obj.Ice.ToString()); ;
                        Detalleconta.Add(obcontdet2);

                        clsDetalleComprobante obcontdet3 = new clsDetalleComprobante();
                        obcontdet3.cuenta = "41101001"; // ingreso por venta
                        obcontdet3.debe = 0;
                        obcontdet3.haber = Decimal.Parse(obj.Subtotal.ToString());
                        Detalleconta.Add(obcontdet3);

                        clsDetalleComprobante obcontdet4 = new clsDetalleComprobante();
                        obcontdet4.cuenta = "51101001"; // costo de venta
                        obcontdet4.debe = Decimal.Parse(totalcosto.ToString());
                        obcontdet4.haber = 0;
                        Detalleconta.Add(obcontdet4);

                        clsDetalleComprobante obcontdet5 = new clsDetalleComprobante();
                        obcontdet5.cuenta = "11301001"; // costo de venta
                        obcontdet5.debe = 0;
                        obcontdet5.haber = Decimal.Parse(totalcosto.ToString());
                        Detalleconta.Add(obcontdet5);

                    }
                    else {

                        clsDetalleComprobante obcontdet0 = new clsDetalleComprobante();
                        obcontdet0.cuenta = "11101001"; // caja
                        obcontdet0.debe = Decimal.Parse(txtValorEntrada.Value.ToString());
                        obcontdet0.haber = 0;
                        Detalleconta.Add(obcontdet0);

                        clsDetalleComprobante obcontdet = new clsDetalleComprobante();
                        obcontdet.cuenta = "11201001"; // credito
                        obcontdet.debe = Decimal.Parse((txtTotalPagar.Value - txtValorEntrada.Value).ToString());
                        obcontdet.haber = 0;
                        Detalleconta.Add(obcontdet);

                        clsDetalleComprobante obcontdet1 = new clsDetalleComprobante();
                        obcontdet1.cuenta = "11401001"; // iva
                        obcontdet1.debe = 0;
                        obcontdet1.haber = Decimal.Parse(obj.Iva.ToString());
                        Detalleconta.Add(obcontdet1);

                        clsDetalleComprobante obcontdet2 = new clsDetalleComprobante();
                        obcontdet2.cuenta = "11401003"; // ice
                        obcontdet2.debe = 0;
                        obcontdet2.haber = Decimal.Parse(obj.Ice.ToString()); ;
                        Detalleconta.Add(obcontdet2);

                        clsDetalleComprobante obcontdet3 = new clsDetalleComprobante();
                        obcontdet3.cuenta = "41101001"; // ingreso por venta
                        obcontdet3.debe = 0;
                        obcontdet3.haber = Decimal.Parse(obj.Subtotal.ToString());
                        Detalleconta.Add(obcontdet3);

                        clsDetalleComprobante obcontdet4 = new clsDetalleComprobante();
                        obcontdet4.cuenta = "51101001"; // costo de venta
                        obcontdet4.debe = Decimal.Parse(totalcosto.ToString());
                        obcontdet4.haber = 0;
                        Detalleconta.Add(obcontdet4);

                        clsDetalleComprobante obcontdet5 = new clsDetalleComprobante();
                        obcontdet5.cuenta = "11301001"; // costo de venta
                        obcontdet5.debe = 0;
                        obcontdet5.haber = Decimal.Parse(totalcosto.ToString());
                        Detalleconta.Add(obcontdet5);

                    }
                    objcontdet.Detalle = Detalleconta;

                    objcontabilidad.GuardarCabecera(ref objcontdet);

                    //guardar cuentas por cobrar
                   Int32 maxUnitsInStock=0;
                    try
                    {
                     maxUnitsInStock =
                    (from prod in da.Factura
                    select prod.IdNumeroFactura).Max() + 1;

                    ;
                    }
                    catch (Exception ex)
                    {
                    maxUnitsInStock= 1;
                    }

                    CuentaxCobrar objcxc = new CuentaxCobrar();
                    objcxc.idCuentaxCobrar  =  Int32.Parse(maxUnitsInStock.ToString());
                    //objcxc.numero_comprobante =
                    //objcxc.idTransaccion =
                    objcxc.idNumeroFactura =Int32.Parse(this.txtNumeroFactura.Text);
                    //objcxc.idCabeceraComprobante =
                    objcxc.TotalCuotas =Int32.Parse(txtNumeroCuotaMensual.Text);
                    objcxc.porcentaje_interes=Decimal.Parse(this.txtTasaFija.Text);
                    objcxc.Modulo =1;

                    objcxc.idEmpresa = Seguridad.empresa;
                    objcxc.idUsuario = Seguridad.usuario;
                    //objcxc.estado = Int32.Parse(cmbestado.SelectedValue.ToString());
                    objcxc.estado = cmbestado.SelectedValue.ToString();

                    da.AddToCuentaxCobrar(objcxc);
                     respuesta = da.SaveChanges();
                    if (respuesta > 0)
                    {
                         i = 0;
                        while (i < tblcutoas.Rows.Count)
                        {
                            CuentaxCobrarDet objdetcxc = new CuentaxCobrarDet ();
                            objdetcxc.idCuentaxCobrar = objcxc.idCuentaxCobrar;

                            //objdetcxc.idEmpresa = Seguridad.empresa;
                            //objdetcxc.idUsuario = Seguridad.usuario;
                            //objdetcxc.idEstado = Int32.Parse(cmbestado.SelectedValue.ToString());
                            objdetcxc.estado = cmbestado.SelectedValue.ToString();

                            objdetcxc.Numero = i + 1;
                            objdetcxc.numero_cuota = i + 1;

                            objdetcxc.valor_cuota = Decimal.Parse(tblcutoas.Rows[i][0].ToString());
                            objdetcxc.valor_interes = Decimal.Parse(tblcutoas.Rows[i][1].ToString());
                            objdetcxc.valor_mora = 0;
                            objdetcxc.fecha_cobro = DateTime.Parse(tblcutoas.Rows[i][2].ToString());
                            objdetcxc.fecha_vencimiento = DateTime.Parse(tblcutoas.Rows[i][3].ToString());
                            objdetcxc.FechaModificacion  =  DateTime.Parse(DateAndTime.Now.ToShortDateString());

                            da.AddToCuentaxCobrarDet (objdetcxc);
                            da.SaveChanges();
                            i++;

                        }
                    }

                    MessageBox.Show("Registro Ingresado con exito.", "Modulo de Facturacion",
             MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //cargarpromociones();
                    ImprimirReporte();

                    limpiar();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ingrese Correctamente los valores. " + ex.Message, "Modulo de Facturacion",
            MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }