unit uViewPeriodoFechas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxEdit, cxDropDownEdit, cxCalendar, StdCtrls,
  cxControls, cxContainer, cxTextEdit, cxMaskEdit, TBXDkPanels,
  cxEditRepositoryItems, dxLayoutControl, dxLayoutLookAndFeels, uViewBase,
  Mask, JvExMask, JvToolEdit, uViewInformeBaseParametros, TB2Dock;

type
  IViewPeriodoFechas = interface(IViewInformeBaseParametros)
    ['{793084E2-873E-4C57-8BD6-9087816CCF3A}']
  end;

  TfrViewPeriodoFechas = class(TfrViewInformeBaseParametros, IViewPeriodoFechas)
    cxRepository: TcxEditRepository;
    cxRepositoryPeriodos: TcxEditRepositoryComboBoxItem;
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutStandardLookAndFeel1: TdxLayoutStandardLookAndFeel;
    TBXDockablePanel1: TTBXDockablePanel;
    dxLayoutControl1: TdxLayoutControl;
    cbPeriodo: TcxComboBox;
    edtFechaIni: TcxDateEdit;
    edtFechaFin: TcxDateEdit;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutGroup2: TdxLayoutGroup;
    dxLayoutControl1Item2: TdxLayoutItem;
    dxLayoutControl1Item3: TdxLayoutItem;
    dxLayoutItem1: TdxLayoutItem;
    procedure cbPeriodoPropertiesChange(Sender: TObject);
    procedure edtFechaIni2PropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
    procedure edtFechaFinPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
  private
    function GetFechaFinal: TDateTime;
    function GetFechaInicial: TDateTime;
    procedure SetFechaFinal(const Value: TDateTime);
    procedure SetFechaInicial(const Value: TDateTime);
  public
    procedure Hoy;
    procedure Ayer;
    procedure EstaSemana;
    procedure EsteMes;
    procedure EsteAno;
    procedure Ultimos7dias;
    procedure Ultimos30dias;
    procedure Ultimos3meses;
    procedure MesAnterior;
    procedure AnoAnterior;
    procedure Personalizado;
    property FechaInicial : TDateTime read GetFechaInicial write SetFechaInicial;
    property FechaFinal : TDateTime read GetFechaFinal write SetFechaFinal;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

uses DateUtils;

{$R *.dfm}

procedure TfrViewPeriodoFechas.AnoAnterior;
begin
  edtFechaIni.Date := StartOfTheYear(IncYear(Today, -1));
  edtFechaFin.Date := EndOfTheYear(IncYear(Today, -1));
end;

procedure TfrViewPeriodoFechas.Ayer;
begin
  edtFechaIni.Date := Yesterday;
  edtFechaFin.Date := Yesterday;
end;

procedure TfrViewPeriodoFechas.EstaSemana;
begin
  edtFechaIni.Date := StartOfTheWeek(Today);
  edtFechaFin.Date := EndOfTheWeek(Today);
end;

procedure TfrViewPeriodoFechas.EsteAno;
begin
  edtFechaIni.Date := StartOfTheYear(Today);
  edtFechaFin.Date := EndOfTheYear(Today);
end;

procedure TfrViewPeriodoFechas.EsteMes;
begin
  edtFechaIni.Date := StartOfTheMonth(Today);
  edtFechaFin.Date := EndOfTheMonth(Today);
end;

function TfrViewPeriodoFechas.GetFechaFinal: TDateTime;
begin
  Result := DateOf(edtFechaFin.Date);
end;

function TfrViewPeriodoFechas.GetFechaInicial: TDateTime;
begin
  Result := DateOf(edtFechaIni.Date);
end;

procedure TfrViewPeriodoFechas.Hoy;
begin
  edtFechaIni.Date := Today;
  edtFechaFin.Date := Today;
end;

procedure TfrViewPeriodoFechas.MesAnterior;
begin
  edtFechaIni.Date := StartOfTheMonth(IncMonth(Today, -1));
  edtFechaFin.Date := EndOfTheMonth(IncMonth(Today, -1));
end;

procedure TfrViewPeriodoFechas.Personalizado;
begin
  edtFechaIni.SetFocus;
end;

procedure TfrViewPeriodoFechas.SetFechaFinal(const Value: TDateTime);
begin
  edtFechaFin.Date := Value;
end;

procedure TfrViewPeriodoFechas.SetFechaInicial(const Value: TDateTime);
begin
  edtFechaIni.Date := Value;
end;

procedure TfrViewPeriodoFechas.Ultimos30dias;
begin
  edtFechaIni.Date := IncDay(Today, -30);
  edtFechaFin.Date := Today
end;

procedure TfrViewPeriodoFechas.Ultimos3meses;
begin
  edtFechaIni.Date := IncMonth(Today, -3);
  edtFechaFin.Date := Today
end;

procedure TfrViewPeriodoFechas.Ultimos7dias;
begin
  edtFechaIni.Date := IncDay(Today, -7);
  edtFechaFin.Date := Today
end;

procedure TfrViewPeriodoFechas.cbPeriodoPropertiesChange(Sender: TObject);
begin
  case (Sender as TcxComboBox).ItemIndex of
    0   : Hoy;
    1   : Ayer;
    2   : EstaSemana;
    3   : EsteMes;
    4   : EsteAno;
    5   : Ultimos7dias;
    6   : Ultimos30dias;
    7   : Ultimos3meses;
    8   : MesAnterior;
    9   : AnoAnterior;
    else
      Personalizado;
  end;
end;

constructor TfrViewPeriodoFechas.Create(AOwner: TComponent);
begin
  inherited;
  cbPeriodo.ItemIndex := 3;
  EsteMes;
end;

procedure TfrViewPeriodoFechas.edtFechaIni2PropertiesValidate(
  Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption;
  var Error: Boolean);
begin
  inherited;
  if DisplayValue <> edtFechaIni.Date then
    cbPeriodo.ItemIndex := 12;
  if DisplayValue > edtFechaFin.Date then
  begin
    edtFechaFin.EditText := DisplayValue;
    edtFechaFin.ValidateEdit(True);
  end;
end;

procedure TfrViewPeriodoFechas.edtFechaFinPropertiesValidate(
  Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption;
  var Error: Boolean);
begin
  inherited;
  if DisplayValue <> edtFechaFin.Date then
    cbPeriodo.ItemIndex := 12;
  if DisplayValue < edtFechaIni.Date then
  begin
    edtFechaIni.EditText := DisplayValue;
    edtFechaIni.ValidateEdit(True);
  end;
end;

end.

