unit uViewControlVentasTerminadas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewControlBase, ActnList, ImgList, PngImageList, uViewGrid,
  uViewControlGrid, uViewSumarios, TBXDkPanels, StdCtrls,
  uViewAgrupaciones, uCustomView, uViewBase, uViewColumnas, ComCtrls,
  TB2Dock, uViewParametrosControlGrid, uBizControlesBase, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  cxTextEdit, uViewFiltros,
  uViewControGridlVentasTerminadas, uViewPeriodoFechas;

type
  IViewControlVentasTerminadas = interface(IViewControlBase)
    ['{B8B9105B-E833-4E7D-BA36-EB9A7EBDCE69}']
  end;

  TfrViewControlVentasTerminadas = class(TfrViewControlBase, IViewControlVentasTerminadas)
    frViewPeriodoFechas1: TfrViewPeriodoFechas;
    actPeriodoFechas: TAction;
    frViewControlGridVentasTerminadas1: TfrViewControlGridVentasTerminadas;
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPeriodoFechasExecute(Sender: TObject);
  protected
    FModificado : Boolean;
    procedure OnViewControlGridChanged(Sender : TObject);
    procedure OnFilterChanged(Sender : TObject);
    procedure ActivarTarea(const ATarea: Integer); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Refresh; override;
  end;

implementation

uses
  uDataModuleVentasTerminadas, uBizControlVentasTerminadas, uBizVentasTerminadas;

const
  PERIODO = 0;

{$R *.dfm}

{ TfrViewControlVentasTerminadas }

constructor TfrViewControlVentasTerminadas.Create(AOwner: TComponent);
begin
  inherited;
  frViewControlGridVentasTerminadas1.OnViewChanged := OnViewControlGridChanged;
  frViewControlGridVentasTerminadas1.OnFilterChanged := OnFilterChanged;
  frViewColumnas1.ViewControlGrid := frViewControlGridVentasTerminadas1;
  frViewAgrupaciones1.ViewControlGrid := frViewControlGridVentasTerminadas1;
  frViewSumarios1.ViewControlGrid := frViewControlGridVentasTerminadas1;
  frViewFiltros1.ViewControlGrid := frViewControlGridVentasTerminadas1;
  pcParametros.ActivePage := TabSheet5;
end;

procedure TfrViewControlVentasTerminadas.Refresh;
var
  AColumns : TStringList;
begin
  inherited;
  ShowHourglassCursor;
  actRefrescar.Enabled := False;

  frViewControlGridVentasTerminadas1.OnViewChanged := nil;
  frViewControlGridVentasTerminadas1.OnFilterChanged := nil;

  try
    // Calcular las columnas a pedir al servidor
    AColumns := frViewColumnas1.GetCheckedColumns;
    AColumns.Duplicates := dupIgnore;
    AColumns.AddStrings(frViewAgrupaciones1.GetAgrupaciones);

    (Control as IBizControlVentasTerminadas).Items.VisibleColumns := AColumns.CommaText;
    with (Control as IBizControlVentasTerminadas).Items.DataTable do
    begin
      DisableControls;
      try
        Active := False;
        LoadSchema;
        ParamByName('FECHAINI').AsDateTime := frViewPeriodoFechas1.FechaInicial;
        ParamByName('FECHAFIN').AsDateTime := frViewPeriodoFechas1.FechaFinal;
        Active := True;

        frViewColumnas1.Execute;
        frViewAgrupaciones1.Execute;
        frViewSumarios1.Refresh;
        frViewFiltros1.Refresh;
        pnlTareas.Visible := True;

        if Assigned(FOnViewChanged) then
          FOnViewChanged(Self);
      finally
        EnableControls;
        frViewControlGridVentasTerminadas1.Refresh;
        frViewControlGridVentasTerminadas1.ExpandirTodo;
        frViewControlGridVentasTerminadas1.GotoFirst;
      end;
    end;
  finally
    FreeAndNil(AColumns);
    frViewControlGridVentasTerminadas1.OnViewChanged := OnViewControlGridChanged;
    frViewControlGridVentasTerminadas1.OnFilterChanged := OnFilterChanged;
    actRefrescar.Enabled := True;
    HideHourglassCursor;
  end;
end;

procedure TfrViewControlVentasTerminadas.actRefrescarExecute(Sender: TObject);
begin
  inherited;
  Refresh;
end;

procedure TfrViewControlVentasTerminadas.OnViewControlGridChanged(
  Sender: TObject);
begin
  if Assigned(Self.OnViewChanged) then
    Self.OnViewChanged(Self);
end;

procedure TfrViewControlVentasTerminadas.OnFilterChanged(Sender: TObject);
begin
  frViewFiltros1.Refresh;
end;

procedure TfrViewControlVentasTerminadas.actPeriodoFechasExecute(
  Sender: TObject);
begin
  inherited;
  ActivarTarea(PERIODO);
end;

procedure TfrViewControlVentasTerminadas.ActivarTarea(
  const ATarea: Integer);
begin
  inherited;
  case ATarea of
    PERIODO    : pcParametros.ActivePage := TabSheet5;
  end;
end;

end.
