{
===============================================================================
  Copyright (©) 2002. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 25-12-2002
  Versión actual: 1.0.0
  Fecha versión actual: 25-12-2002
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit InformeTrimestral;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  RdxFrameInformeTrimestral, Grids, DBGrids, ComCtrls, RdxBotones, ToolWin, ExtCtrls,
  Menus, RXDBCtrl, Configuracion, RdxFrame,
  Db, RdxPaneles, RdxBarras, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  am2000menuitem, am2000popupmenu, am2000, cxContainer, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, StdCtrls, ActnList, StrUtils,

  Entidades, InformeTrimestralVentas, InformeTrimestralCompras, vistaPrevia,
  RdxTitulos, ImgList,

  RdxEmpresaActiva, am2000utils, cxDataStorage, cxTL, cxInplaceContainer,
  cxTLData, cxDBTL;

type
  TfrInformeTrimestral = class(TRdxFrameInformeTrimestral)
    dsFacturas: TDataSource;
    pnlCuerpo: TRdxPanel;
    pnlContenido: TRdxPanel;
    pnlExtra: TRdxBarraSuperior;
    Acciones: TActionList;
    actImprimir: TAction;
    actAceptar: TAction;
    dsTrimestres: TDataSource;
    pnlGrid: TRdxPanel;
    pnlListaTrimestres: TRdxPanel;
    gridFacturas: TcxGrid;
    gridFacturasDBTableView1: TcxGridDBTableView;
    gridFacturasLevel1: TcxGridLevel;
    bAnular: TRdxBoton;
    bAceptar: TRdxBoton;
    actAsignarTri: TAction;
    Splitter1: TSplitter;
    pnlTitulo: TRdxPanelTituloOperacion;
    brSalir: TRdxBarraInferior;
    bSalir: TRdxBoton;
    ImageList1: TImageList;
    MenuTrimestres: TPopupMenu2000;
    mnuAnadir: TMenuItem2000;
    mnuModificar: TMenuItem2000;
    actAbrirTrimestre: TAction;
    actCerrarTrimestre: TAction;
    RdxBoton1: TRdxBoton;
    RdxBoton2: TRdxBoton;
    bSeparador1: TShape;
    cbxTipos: TcxComboBox;
    RdxPanel1: TRdxPanel;
    eTrimestre: TLabel;
    tlTrimestres: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn1: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn3: TcxDBTreeListColumn;
    cxDBTreeList1cxDBTreeListColumn4: TcxDBTreeListColumn;
    bCambioTrimestre: TRdxBoton;
    actCambioTrimestre: TAction;
    procedure gridFacturasDBTableView1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure gridFacturasDBTableView1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

    procedure actImprimirExecute(Sender: TObject);
    procedure actAceptarExecute(Sender: TObject);
    procedure actAsignarTriExecute(Sender: TObject);
    procedure RdxFrameInformeTrimestralShow(Sender: TObject);
    procedure gridFacturasDBTableView1DblClick(Sender: TObject);
    procedure tlTrimestresGetLevelColor(Sender: TObject; ALevel: Integer;
      var AColor: TColor);
    procedure actAbrirTrimestreUpdate(Sender: TObject);
    procedure actAbrirTrimestreExecute(Sender: TObject);
    procedure actCerrarTrimestreUpdate(Sender: TObject);
    procedure actCerrarTrimestreExecute(Sender: TObject);
    procedure gridFacturasDBTableView1StartDrag(Sender: TObject;
      var DragObject: TDragObject);
    procedure gridFacturasDBTableView1EndDrag(Sender, Target: TObject; X,
      Y: Integer);
    procedure tlTrimestresEndDrag(Sender, Target: TObject; X, Y: Integer);
    procedure actAsignarTriUpdate(Sender: TObject);
    procedure cbxTiposPropertiesChange(Sender: TObject);
    procedure tlTrimestresGetNodeImageIndex(Sender: TObject;
      ANode: TcxTreeListNode; AIndexType: TcxTreeListImageIndexType;
      var AIndex: TImageIndex);
    procedure tlTrimestresCustomDrawCell(Sender: TObject;
      ACanvas: TcxCanvas; AViewInfo: TcxTreeListEditCellViewInfo;
      var ADone: Boolean);
    procedure tlTrimestresFocusedNodeChanged(Sender: TObject;
      APrevFocusedNode, AFocusedNode: TcxTreeListNode);
    procedure tlTrimestresDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure tlTrimestresDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure actCambioTrimestreExecute(Sender: TObject);
  private
    FCodigoTipFacLista : TStrings;
    FCodigoTipFac : Variant;
    FCodigoFactura : String;
    FInformeTrimestralVentas : TdmInformeTrimestralVentas;
    FInformeTrimestralCompras : TdmInformeTrimestralCompras;
    procedure ActualizarBotones;
    procedure Refrescar;
    procedure AplicarFiltro;
    function AsignarTrimestre(CodigoFactura, CodigoTrimestre: String) : Boolean;
    procedure setCodigoTipFac(const Value: variant);
  protected
    property CodigoTipFac: variant read FCodigoTipFac write setCodigoTipFac;
    function CambiarEntidad(EntidadAnterior, Entidad : TRdxEntidad): Boolean; override;
  public
    constructor Create (AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frInformeTrimestral: TfrInformeTrimestral;

implementation

{$R *.DFM}

uses
  BaseDatos, TablaFacturasCliente, TablaFacturasProveedor, IBDatabase, FacturaCliente,
  Literales, IBCustomDataSet, Mensajes, Variants, ImprimirFacturasCliente,
  Constantes, TablaTrimestres, cxVariants, AsignarTrimestre, FacturaProveedor,
  ImprimirInformeTrimestral, StrFunc, TablaEmpresas, TrimestrePredeterminado;

var
  FArrastrando : Boolean;
  FTriInicioArrastre : String;

function GetDragSourceGridView(Source: TObject): TcxCustomGridView;
begin
  if (TDragControlObject(Source).Control is TcxGridSite) then
    Result := TcxGridSite(TDragControlObject(Source).Control).GridView
  else
    Result := nil;
end;

constructor TfrInformeTrimestral.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  BaseDatos := dmBaseDatos.BD;
  Transaccion := dmBaseDatos.Transaccion;

  TablaFacturas := TIBDataSet.Create(Self);
  dsFacturas.DataSet := TablaFacturas;

  TablaTrimestres := TIBDataSet.Create(Self);
  dsTrimestres.DataSet := TablaTrimestres;
end;

destructor TfrInformeTrimestral.Destroy;
begin
  TablaFacturas.Close;
  TablaFacturas.UnPrepare;
  TablaFacturas.Free;

  TablaTrimestres.Close;
  TablaTrimestres.UnPrepare;
  TablaTrimestres.Free;

  FreeAndNil(FCodigoTipFacLista);
  inherited;
end;

procedure TfrInformeTrimestral.ActualizarBotones;
begin
  if BaseDatos.IsReadOnly then
  begin
    actImprimir.Enabled := True;
    Exit;
  end;

  if TablaFacturas.RecordCount = 0 then
  begin
    actImprimir.Enabled := False;
  end
  else begin
    actImprimir.Enabled := True;
  end;
end;

procedure TfrInformeTrimestral.actImprimirExecute(Sender: TObject);
begin
  Contenido := TfrImprimirInformeTrimestral.Create(Self);
  (Contenido as TfrImprimirInformeTrimestral).CodigoTrimestre := TablaTrimestres.FieldByName('CODIGO').AsString;
  Contenido.Entidad := Entidad;
  Contenido.Modo := Imprimir;
end;

procedure TfrInformeTrimestral.actAceptarExecute(Sender: TObject);
begin
  CloseFrame;
end;

procedure TfrInformeTrimestral.gridFacturasDBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  IndiceCol : Integer;
begin
  if not AViewInfo.Selected then
  begin
    IndiceCol := (Sender as TcxGridDBTableView).GetColumnByFieldName('CLASEFACTURA').Index;
    if UpperCase(AViewInfo.GridRecord.DisplayTexts[IndiceCol]) = CTE_CF_ABONO then
      ACanvas.Canvas.Font.Color := $000000CC;

    if (TablaFacturas.FieldByName('SITUACIONTRIMESTRE').AsString = CTE_CERRADO) then
      ACanvas.Canvas.Font.Color := clGray;
  end;
end;

function TfrInformeTrimestral.CambiarEntidad(EntidadAnterior, Entidad: TRdxEntidad): Boolean;
begin
  Result := inherited CambiarEntidad(EntidadAnterior, Entidad);

  with TablaTrimestres do
  begin
    DisableControls;
    Close;
    Database := BaseDatos;
    Transaction := Transaccion;
    SelectSQL.Assign(dmTablaTrimestres.sqlConsultarGridTrimestres);
    ParamByName('CODIGOEMPRESA').AsInteger := EmpresaActiva.Codigo;
    Prepare;
    Open;
    EnableControls;
  end;

  with TablaFacturas do
  begin
    DisableControls;
    Close;
    Database := BaseDatos;
    Transaction := Transaccion;
    case Entidad of
      entInformeTrimestralVentas : SelectSQL.Assign(dmTablaFacturasCliente.sqlConsultarGridFacturasTrimestre);
      entInformeTrimestralCompras: SelectSQL.Assign(dmTablaFacturasProveedor.sqlConsultarGridFacturasTrimestre);
    else
      //LOG
    end;
    ParamByName('CODIGOEMPRESA').AsInteger := EmpresaActiva.Codigo;
    Prepare;
    Open;
    EnableControls;
    ActualizarBotones;
  end;

  case Entidad of
  entInformeTrimestralVentas : begin
                                dmTablaFacturasCliente.InicializarTablaFacturas(@TablaFacturas);
                                dmTablaFacturasCliente.InicializarGridFacturasTrimestre(gridFacturas);
                                //Inicializamos lista de tipos de facturas
                                cbxTipos.Properties.Items := dmTablaFacturasCliente.DarTiposOpDesTrimestral(FCodigoTipFacLista);
                                cbxTipos.ItemIndex := 0;
                               end;
  entInformeTrimestralCompras: begin
                                dmTablaFacturasProveedor.InicializarTablaFacturas(@TablaFacturas);
                                dmTablaFacturasProveedor.InicializarGridFacturasTrimestre(gridFacturas);
                                cbxTipos.Properties.Items := dmTablaFacturasProveedor.DarTiposOpDesTrimestral(FCodigoTipFacLista);
                                cbxTipos.ItemIndex := 0;
                               end;
  else
    //LOG
  end;

  //Expandimos la lista
  tlTrimestres.FullExpand;
  TablaTrimestres.Locate('CODIGO', EmpresaActiva.CodigoTrimestre, []);

  //Colocamos el puntero en el primer registro
  gridFacturasDBTableView1.Controller.GoToFirst;
end;

function TfrInformeTrimestral.AsignarTrimestre(CodigoFactura, CodigoTrimestre: String) : Boolean;
begin
  //Validamos
  if (length(CodigoTrimestre) = 2) then
    raise Exception.Create(msgTriCodigoErroneo);

  case Entidad of
    entInformeTrimestralVentas :
    begin
        if not dmTablaTrimestres.EstaTrimestreAbierto(CodigoTrimestre) then
          raise Exception.Create(msgTriCerrado);
        if not dmTablaFacturasCliente.ComprobarTrimestreFactura(CodigoFactura) then
          raise Exception.Create(msgTriFacTriCer);
    end;

    entInformeTrimestralCompras:
    begin
        if not dmTablaTrimestres.EstaTrimestreAbierto(CodigoTrimestre) then
          raise Exception.Create(msgTriCerrado);
        if not dmTablaFacturasProveedor.ComprobarTrimestreFactura(CodigoFactura) then
          raise Exception.Create(msgTriFacTriCer);
    end;
  end;

  //Realizamos la operacion
  if (Entidad = entInformeTrimestralVentas) then
    Result := dmTablaFacturasCliente.AsignarTrimestre(CodigoFactura, CodigoTrimestre)
  else if (Entidad = entInformeTrimestralCompras) then
    Result := dmTablaFacturasProveedor.AsignarTrimestre(CodigoFactura, CodigoTrimestre)
end;

procedure TfrInformeTrimestral.actAsignarTriExecute(Sender: TObject);
var
  iCounter: integer;
  Index: integer;
  Columna : TcxGridDBColumn;
begin
  fAsignarTrimestre := TfAsignarTrimestre.Create(Self);
  try
    fAsignarTrimestre.ShowModal;
    if fAsignarTrimestre.ModalResult = mrOK then
    begin
      Columna := gridFacturasDBTableView1.GetColumnByFieldName('CODIGO');
      try
        for iCounter := 0 to gridFacturasDBTableView1.ViewData.DataController.GetSelectedCount - 1 do
        begin
          Index := gridFacturasDBTableView1.ViewData.DataController.GetSelectedRowIndex(iCounter);
          FCodigoFactura := gridFacturasDBTableView1.ViewData.Records[Index].Values[Columna.Index];

          if not EsCadenaVacia(FCodigoFactura) then
            AsignarTrimestre(FCodigoFactura, fAsignarTrimestre.CodigoTrimestre)
        end;
      except
        Rollback;
        raise;
      end;
    end;
  finally
    fAsignarTrimestre.Free
  end;
  AplicarFiltro;
  Refrescar;
end;

procedure TfrInformeTrimestral.RdxFrameInformeTrimestralShow(Sender: TObject);
begin
  FArrastrando := False;
  gridFacturas.SetFocus;
end;

procedure TfrInformeTrimestral.AplicarFiltro;
var
  lsCodigoTrimestre : String;
begin
  lsCodigoTrimestre := TablaTrimestres.FieldByName('CODIGO').AsString;
  //Filtro de año
  if (Length(lsCodigoTrimestre) = 2) then
  begin
    lsCodigoTrimestre := lsCodigoTrimestre + '%';
    with gridFacturas.ActiveView.DataController.Filter do
    begin
      BeginUpdate;
      try
        Root.Clear;
        Root.AddItem((gridFacturas.ActiveView as TcxGridDBTableView).GetColumnByFieldName('CODIGOTRIMESTRE'), foLike, lsCodigoTrimestre, 'Trimestre');
        Active := True;
      finally
        EndUpdate;
      end;
    end;
  end
  else //Filtro de trimestre
  begin
    with gridFacturas.ActiveView.DataController.Filter do
    begin
      BeginUpdate;
      try
        Root.Clear;
        Root.AddItem((gridFacturas.ActiveView as TcxGridDBTableView).GetColumnByFieldName('CODIGOTRIMESTRE'), foEqual, lsCodigoTrimestre, 'Trimestre');
        Active := True;
      finally
        EndUpdate;
      end;
    end;
  end;

  if not VarIsNull(FCodigoTipFac) then
  begin
    with gridFacturas.ActiveView.DataController.Filter do
    begin
      BeginUpdate;
      try
        if AnsiPos('>',FCodigoTipFac) <> 0 then
           Root.AddItem((gridFacturas.ActiveView as TcxGridDBTableView).GetColumnByFieldName('TIPOFACTURA'), foGreater, AnsiRightStr(FCodigoTipFac, length(FCodigoTipFac)-1), 'Tipo factura')
        else if AnsiPos('<',FCodigoTipFac) <> 0 then
           Root.AddItem((gridFacturas.ActiveView as TcxGridDBTableView).GetColumnByFieldName('TIPOFACTURA'), foLess, AnsiRightStr(FCodigoTipFac, length(FCodigoTipFac)-1), 'Tipo factura')
        else
           Root.AddItem((gridFacturas.ActiveView as TcxGridDBTableView).GetColumnByFieldName('TIPOFACTURA'), foEqual, FCodigoTipFac, 'Tipo factura');

        Active := True;
      finally
        EndUpdate;
      end;
    end;
  end;

  //Posicionamos en el primer registro
  gridFacturasDBTableView1.Controller.GoToFirst;
end;

procedure TfrInformeTrimestral.gridFacturasDBTableView1MouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  if Button = mbLeft then
  begin
    with TcxGridSite(Sender) do
    begin
      if ViewInfo.GetHitTest(X, Y).HitTestCode = htCell then
        FCodigoFactura := TablaFacturas.FieldByName('CODIGO').AsString;
    end;
  end;
end;

procedure TfrInformeTrimestral.Refrescar;
var
  BookmarkAux : String;
begin
  tlTrimestres.BeginUpdate;
  try
    with TablaTrimestres do
    begin
      BookmarkAux := Bookmark;
      DisableControls;
      Close;
      Open;
      Bookmark := BookmarkAux;
      EnableControls;
    end;

    with TablaFacturas do
    begin
      DisableControls;
      Close;
      Open;
      EnableControls;
    end;
    dmTablaFacturasCliente.InicializarTablaFacturas(@TablaFacturas);

    //Colocamos el puntero en el primer registro
    gridFacturasDBTableView1.Controller.GoToFirst;
  finally
    tlTrimestres.FullRefresh;
    tlTrimestres.EndUpdate;
    //Expandimos la lista
    tlTrimestres.FullExpand;
  end;
end;

procedure TfrInformeTrimestral.gridFacturasDBTableView1DblClick(
  Sender: TObject);
begin
  if Entidad = entInformeTrimestralVentas then
  begin
    Contenido := TfrFacturaCliente.Create(Self);
    (Contenido as TfrFacturaCliente).CodigoFactura := FCodigoFactura;
    Contenido.Modo := Consultar;
  end;
  if Entidad = entInformeTrimestralCompras then
  begin
    Contenido := TfrFacturaProveedor.Create(Self);
    (Contenido as TfrFacturaProveedor).CodigoFactura := FCodigoFactura;
    Contenido.Modo := Consultar;
  end;
end;

procedure TfrInformeTrimestral.tlTrimestresGetLevelColor(Sender: TObject;
  ALevel: Integer; var AColor: TColor);
begin
  if ALevel = 0 then
    AColor := $0093CAB1;
end;

procedure TfrInformeTrimestral.actAbrirTrimestreUpdate(Sender: TObject);
begin
  if (UpperCase(TablaTrimestres.FieldByName('SITUACION').AsString) = CTE_CERRADO) and
     (Length(TablaTrimestres.FieldByName('CODIGO').AsString) > 2)  then
    (Sender as TAction).Enabled := True
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfrInformeTrimestral.actAbrirTrimestreExecute(Sender: TObject);
begin
  dmTablaTrimestres.AbrirTrimestre(TablaTrimestres.FieldByName('CODIGO').AsString);
  Commit;
  Refrescar;  
end;

procedure TfrInformeTrimestral.actCerrarTrimestreUpdate(Sender: TObject);
begin
  if (UpperCase(TablaTrimestres.FieldByName('SITUACION').AsString) = CTE_ABIERTO) and
     (Length(TablaTrimestres.FieldByName('CODIGO').AsString) > 2)  then
    (Sender as TAction).Enabled := True
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfrInformeTrimestral.actCerrarTrimestreExecute(Sender: TObject);
begin
  if (EmpresaActiva.CodigoTrimestre = TablaTrimestres.FieldByName('CODIGO').AsString) then
    verMensaje(msgTriCerrarPredeterminado)
  else
  begin
    dmTablaTrimestres.CerrarTrimestre(TablaTrimestres.FieldByName('CODIGO').AsString);
    Commit;
    Refrescar;
  end;
end;

procedure TfrInformeTrimestral.gridFacturasDBTableView1StartDrag(
  Sender: TObject; var DragObject: TDragObject);
begin
  FArrastrando := True;
  FTriInicioArrastre := tlTrimestres.Columns[0].DataBinding.Field.AsString;
end;

procedure TfrInformeTrimestral.gridFacturasDBTableView1EndDrag(Sender,
  Target: TObject; X, Y: Integer);
begin
  FArrastrando := False;
  FTriInicioArrastre := '';
end;

procedure TfrInformeTrimestral.tlTrimestresEndDrag(Sender, Target: TObject;
  X, Y: Integer);
begin
  FArrastrando := False;
  FTriInicioArrastre := '';
end;

procedure TfrInformeTrimestral.actAsignarTriUpdate(Sender: TObject);
begin
  if (Length(TablaTrimestres.FieldByName('CODIGO').AsString) > 2)  then
    (Sender as TAction).Enabled := True
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfrInformeTrimestral.cbxTiposPropertiesChange(Sender: TObject);
var
  Index : Integer;

begin
  Index := (Sender as TcxComboBox).Properties.Items.Count - FCodigoTipFacLista.Count;
  Index := (Sender as TcxComboBox).ItemIndex - Index;

  //Casos especiales
  if (Index < 0) then
    case (Sender as TcxComboBox).ItemIndex of
    0: CodigoTipFac := Null;
    1: CodigoTipFac := '<50';
    2: CodigoTipFac := '>49';
    end

  //Filtro normal
  else CodigoTipFac := FCodigoTipFacLista[Index];
end;

procedure TfrInformeTrimestral.setCodigoTipFac(const Value: variant);
begin
  FCodigoTipFac := Value;
  AplicarFiltro;
end;

procedure TfrInformeTrimestral.tlTrimestresGetNodeImageIndex(
  Sender: TObject; ANode: TcxTreeListNode;
  AIndexType: TcxTreeListImageIndexType; var AIndex: TImageIndex);
begin
  case ANode.Level of
    0 : if ANode.Values[3] = CTE_CERRADO then
          AIndex := 3
        else
          AIndex := 2;
    1 : if ANode.Values[3] = CTE_CERRADO then
          AIndex := 1
        else
          AIndex := 0;
  end;
end;

procedure TfrInformeTrimestral.tlTrimestresCustomDrawCell(Sender: TObject;
  ACanvas: TcxCanvas; AViewInfo: TcxTreeListEditCellViewInfo;
  var ADone: Boolean);
begin
  if AViewInfo.Node.Level = 0 then
    ACanvas.Font.Style := [fsBold];

  if AViewInfo.Node.Texts[3] = CTE_CERRADO then
    ACanvas.Font.Color := clGray;

  if AViewInfo.Node.Selected then
    ACanvas.Font.Style := [fsBold];
end;

procedure TfrInformeTrimestral.tlTrimestresFocusedNodeChanged(
  Sender: TObject; APrevFocusedNode, AFocusedNode: TcxTreeListNode);
begin
  if not FArrastrando then
    AplicarFiltro;
end;

procedure TfrInformeTrimestral.tlTrimestresDragOver(Sender,
  Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := False;
  with TcxGridSite(TDragControlObject(Source).Control) do
  begin
    if (GridView = gridFacturasDBTableView1) and Assigned(tlTrimestres.HitTest.HitNode) then
      Accept := (tlTrimestres.HitTest.HitNode.Texts[3] = CTE_ABIERTO);
  end;
end;

procedure TfrInformeTrimestral.tlTrimestresDragDrop(Sender,
  Source: TObject; X, Y: Integer);
var
  lsCodigoTrimestre : String;
  AGridView: TcxGridDBTableView;
  iCounter: integer;
  Index: integer;
  Columna : TcxGridDBColumn;
  FNodo: TcxTreeListNode;
begin
  lsCodigoTrimestre := tlTrimestres.HitTest.HitNode.Texts[0];

  if not (Source is TcxDragControlObject) then
    Exit;
  AGridView := GetDragSourceGridView(Source) as TcxGridDBTableView;
  if AGridView = nil then
    Exit;
  Columna := AGridView.GetColumnByFieldName('CODIGO');
  try
    for iCounter := 0 to AGridView.ViewData.DataController.GetSelectedCount - 1 do
    begin
      Index := AGridView.ViewData.DataController.GetSelectedRowIndex(iCounter);
      FCodigoFactura := AGridView.ViewData.Records[Index].Values[Columna.Index];

      if not EsCadenaVacia(FCodigoFactura) then
        AsignarTrimestre(FCodigoFactura, lsCodigoTrimestre)
    end;
  except
    Rollback;
    raise;
  end;
  FNodo := tlTrimestres.FindNodeByKeyValue(FTriInicioArrastre, tlTrimestres.Columns[2]);
  if FNodo <> NIL then
    FNodo.Focused := True;
  AplicarFiltro;
  Refrescar;
end;

procedure TfrInformeTrimestral.actCambioTrimestreExecute(Sender: TObject);
var
  fTrimestrePredeterminado : TfTrimestrePredeterminado;

begin
  fTrimestrePredeterminado := TfTrimestrePredeterminado.Create(Self);
  fTrimestrePredeterminado.Entidad := entInformes;
  try
    fTrimestrePredeterminado.ShowModal;
  finally
    fTrimestrePredeterminado.Free;
  end;
end;

end.
