unit uEditorComisionesReport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorPreview, frxExportImage, frxExportPDF, frxDCtrl, frxClass,
  frxDMPExport, frxGradient, frxChBox, frxCross, frxRich, frxChart, frxOLE,
  frxBarcode, JvAppStorage, JvAppRegistryStorage, JvComponentBase,
  JvFormPlacement, ImgList, PngImageList, StdActns, ActnList, ComCtrls,
  TB2ExtItems, TBXExtItems, TBX, TB2Item, TB2Dock, TB2Toolbar, pngimage,
  ExtCtrls, JvExControls, JvComponent, JvNavigationPane, uIEditorComisionesPreview,
  JvExComCtrls, JvStatusBar, StdCtrls, Buttons, cxRadioGroup, cxMaskEdit,
  cxDropDownEdit, cxCalendar, cxControls, cxContainer, cxEdit, cxTextEdit,
  TBXDkPanels, cxGraphics, DB, uDADataTable, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, uIDataModuleComisionesReport;

type
  TfEditorComisionesPreview = class(TfEditorPreview, IEditorComisionesPreview)
    pnlFiltrar: TTBXDockablePanel;
    Label1: TLabel;
    edtFechaFiltro1: TcxDateEdit;
    edtFechaFiltro2: TcxDateEdit;
    Button1: TBitBtn;
    Label2: TLabel;
    Label3: TLabel;
    cbAgentes: TcxLookupComboBox;
    dsAgentes: TDADataSource;
    procedure edtFechaFiltro1PropertiesEditValueChanged(Sender: TObject);
    procedure OnAgentesChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  protected
    FDataModule : IDataModuleComisionesReport;
    function GetDataModule : IDataModuleComisionesReport;
    procedure SetDataModule (const Value : IDataModuleComisionesReport);

    procedure RefrescarInterno; override;
  public
    property DataModule : IDataModuleComisionesReport read GetDataModule write SetDataModule;
  end;


implementation
{$R *.dfm}

uses uROTypes, uDialogUtils, uBizContactos, uAgentesController;

procedure TfEditorComisionesPreview.Button1Click(Sender: TObject);
begin
  cbAgentes.Properties.OnEditValueChanged := Nil;
  cbAgentes.Clear;
  cbAgentes.Properties.OnEditValueChanged := OnAgentesChange;

  edtFechaFiltro1.Properties.OnEditValueChanged := Nil;
  edtFechaFiltro2.Properties.OnEditValueChanged := Nil;
  edtFechaFiltro1.Clear;
  edtFechaFiltro2.Clear;
  edtFechaFiltro1.Properties.OnEditValueChanged := edtFechaFiltro1PropertiesEditValueChanged;
  edtFechaFiltro2.Properties.OnEditValueChanged := edtFechaFiltro1PropertiesEditValueChanged;

  RefrescarInterno;
end;

procedure TfEditorComisionesPreview.OnAgentesChange(Sender: TObject);
begin
  inherited;
  RefrescarInterno;
end;

procedure TfEditorComisionesPreview.RefrescarInterno;
var
  AStream: Binary;
begin
  ShowHourglassCursor;
  try
  AStream := FDataModule.GetReport(Null, False, edtFechaFiltro1.Text, edtFechaFiltro2.Text, cbAgentes.EditValue);
    try
      Report.PreviewPages.Clear;
      LoadFromStream(AStream);
      Report.Preview.Refresh;
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TfEditorComisionesPreview.SetDataModule(const Value: IDataModuleComisionesReport);
begin
  FDataModule := Value;
end;

procedure TfEditorComisionesPreview.edtFechaFiltro1PropertiesEditValueChanged(Sender: TObject);
begin
  if not VarIsNull(edtFechaFiltro1.EditValue) and not VarIsNull(edtFechaFiltro2.EditValue) then
  begin
    if (edtFechaFiltro1.EditValue > edtFechaFiltro2.EditValue) then
    begin
       ShowWarningMessage('La fecha de inicio debe ser anterior a la fecha final');
       edtFechaFiltro1.SetFocus;
    end
    else
       RefrescarInterno;
  end;
end;

procedure TfEditorComisionesPreview.FormShow(Sender: TObject);
var
  AAgentesController: IAgentesController;
  AAgentes: IBizAgente;
begin
  inherited;
  try
    AAgentesController := TAgentesController.Create;
    AAgentes := (AAgentesController.BuscarTodos as IBizAgente);
    AAgentes.DataTable.Active := True;
    dsAgentes.DataTable := AAgentes.DataTable;
  finally
    AAgentesController := Nil;
    AAgentes := Nil;
  end;
end;

function TfEditorComisionesPreview.GetDataModule: IDataModuleComisionesReport;
begin
  Result := FDataModule; 
end;

end.
