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, TB2Dock, uViewParametrosInforme; type IViewPeriodoFechas = interface(IViewParametrosInforme) ['{793084E2-873E-4C57-8BD6-9087816CCF3A}'] end; TTipoFecha = (TFecha, TFechaVencimiento); TfrViewPeriodoFechas = class(TfrViewParametrosInforme, IViewPeriodoFechas) cxRepository: TcxEditRepository; cxRepositoryPeriodos: TcxEditRepositoryComboBoxItem; edtFechaIni: TcxDateEdit; edtFechaFin: TcxDateEdit; edtFechaVenFin: TcxDateEdit; edtFechaVenIni: TcxDateEdit; cbPeriodo: TcxComboBox; cbPeriodo2: TcxComboBox; dxLayoutControl1Group_Root: TdxLayoutGroup; dxLayoutControl1: TdxLayoutControl; dxLayoutControl1Group1: TdxLayoutGroup; dxLayoutControl1Group2: TdxLayoutGroup; dxLayoutControl1Item1: TdxLayoutItem; dxLayoutControl1Item2: TdxLayoutItem; dxLayoutControl1Item3: TdxLayoutItem; dxLayoutControl1Item4: TdxLayoutItem; dxLayoutControl1Item5: TdxLayoutItem; dxLayoutControl1Item6: TdxLayoutItem; dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList; dxLayoutOfficeLookAndFeel1: TdxLayoutOfficeLookAndFeel; dxLayoutControl1Group3: TdxLayoutGroup; 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); procedure cbPeriodo2PropertiesChange(Sender: TObject); procedure edtFechaVenIniPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); procedure edtFechaVenFinPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); private function GetFechaFinal: Variant; function GetFechaInicial: Variant; procedure SetFechaFinal(const Value: Variant); procedure SetFechaInicial(const Value: Variant); function GetFechaVenFinal: Variant; function GetFechaVenInicial: Variant; procedure SetFechaVenFinal(const Value: Variant); procedure SetFechaVenInicial(const Value: Variant); public procedure EsteTrimestre(const ATipoFecha: TTipoFecha); procedure TrimestreAnterior(const ATipoFecha: TTipoFecha); procedure PrimerTrimestre(const ATipoFecha: TTipoFecha); procedure SegundoTrimestre(const ATipoFecha: TTipoFecha); procedure TercerTrimestre(const ATipoFecha: TTipoFecha); procedure CuartoTrimestre(const ATipoFecha: TTipoFecha); procedure Hoy(const ATipoFecha: TTipoFecha); procedure Ayer(const ATipoFecha: TTipoFecha); procedure EstaSemana(const ATipoFecha: TTipoFecha); procedure EsteMes(const ATipoFecha: TTipoFecha); procedure EsteAno(const ATipoFecha: TTipoFecha); procedure Ultimos7dias(const ATipoFecha: TTipoFecha); procedure Ultimos30dias(const ATipoFecha: TTipoFecha); procedure Ultimos3meses(const ATipoFecha: TTipoFecha); procedure MesAnterior(const ATipoFecha: TTipoFecha); procedure AnoAnterior(const ATipoFecha: TTipoFecha); procedure Personalizado(const ATipoFecha: TTipoFecha); property FechaInicial : Variant read GetFechaInicial write SetFechaInicial; property FechaFinal : Variant read GetFechaFinal write SetFechaFinal; property FechaVenInicial : Variant read GetFechaVenInicial write SetFechaVenInicial; property FechaVenFinal : Variant read GetFechaVenFinal write SetFechaVenFinal; constructor Create(AOwner: TComponent); override; end; implementation uses DateUtils, Math; {$R *.dfm} procedure TfrViewPeriodoFechas.AnoAnterior(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := StartOfTheYear(IncYear(Today, -1)); edtFechaFin.Date := EndOfTheYear(IncYear(Today, -1)); end; TFechaVencimiento: begin edtFechaVenIni.Date := StartOfTheYear(IncYear(Today, -1)); edtFechaVenFin.Date := EndOfTheYear(IncYear(Today, -1)); end; end; end; procedure TfrViewPeriodoFechas.Ayer(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := Yesterday; edtFechaFin.Date := Yesterday; end; TFechaVencimiento: begin edtFechaVenIni.Date := Yesterday; edtFechaVenFin.Date := Yesterday; end; end; end; procedure TfrViewPeriodoFechas.EstaSemana(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := StartOfTheWeek(Today); edtFechaFin.Date := EndOfTheWeek(Today); end; TFechaVencimiento: begin edtFechaVenIni.Date := StartOfTheWeek(Today); edtFechaVenFin.Date := EndOfTheWeek(Today); end; end; end; procedure TfrViewPeriodoFechas.EsteAno(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := StartOfTheYear(Today); edtFechaFin.Date := EndOfTheYear(Today); end; TFechaVencimiento: begin edtFechaVenIni.Date := StartOfTheYear(Today); edtFechaVenFin.Date := EndOfTheYear(Today); end; end; end; procedure TfrViewPeriodoFechas.EsteMes(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := StartOfTheMonth(Today); edtFechaFin.Date := EndOfTheMonth(Today); end; TFechaVencimiento: begin edtFechaVenIni.Date := StartOfTheMonth(Today); edtFechaVenFin.Date := EndOfTheMonth(Today); end; end; end; procedure TfrViewPeriodoFechas.EsteTrimestre(const ATipoFecha: TTipoFecha); var ATrimestre : Integer; begin ATrimestre := Ceil(MonthOf(Today)/3); case ATrimestre of 1: PrimerTrimestre(ATipoFecha); 2: SegundoTrimestre(ATipoFecha); 3: TercerTrimestre(ATipoFecha); 4: CuartoTrimestre(ATipoFecha); end; end; function TfrViewPeriodoFechas.GetFechaFinal: Variant; begin Result := edtFechaFin.EditValue; end; function TfrViewPeriodoFechas.GetFechaInicial: Variant; begin Result := edtFechaIni.EditValue; end; function TfrViewPeriodoFechas.GetFechaVenFinal: Variant; begin Result := edtFechaVenFin.EditValue; end; function TfrViewPeriodoFechas.GetFechaVenInicial: Variant; begin Result := edtFechaVenIni.EditValue; end; procedure TfrViewPeriodoFechas.Hoy(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := Today; edtFechaFin.Date := Today; end; TFechaVencimiento: begin edtFechaVenIni.Date := Today; edtFechaVenFin.Date := Today; end; end; end; procedure TfrViewPeriodoFechas.MesAnterior(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := StartOfTheMonth(IncMonth(Today, -1)); edtFechaFin.Date := EndOfTheMonth(IncMonth(Today, -1)); end; TFechaVencimiento: begin edtFechaVenIni.Date := StartOfTheMonth(IncMonth(Today, -1)); edtFechaVenFin.Date := EndOfTheMonth(IncMonth(Today, -1)); end; end; end; procedure TfrViewPeriodoFechas.Personalizado(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.SetFocus; end; TFechaVencimiento: begin edtFechaVenIni.SetFocus; end; end; end; procedure TfrViewPeriodoFechas.PrimerTrimestre(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := EncodeDate(YearOf(Today), 1, 1); edtFechaFin.Date := EncodeDate(YearOf(Today), 3, 31); end; TFechaVencimiento: begin edtFechaVenIni.Date := EncodeDate(YearOf(Today), 1, 1); edtFechaVenFin.Date := EncodeDate(YearOf(Today), 3, 31);; end; end; end; procedure TfrViewPeriodoFechas.SegundoTrimestre(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := EncodeDate(YearOf(Today), 4, 1); edtFechaFin.Date := EncodeDate(YearOf(Today), 6, 30); end; TFechaVencimiento: begin edtFechaVenIni.Date := EncodeDate(YearOf(Today), 4, 1); edtFechaVenFin.Date := EncodeDate(YearOf(Today), 6, 30);; end; end; end; procedure TfrViewPeriodoFechas.SetFechaFinal(const Value: Variant); begin edtFechaFin.EditValue := Value; end; procedure TfrViewPeriodoFechas.SetFechaInicial(const Value: Variant); begin edtFechaIni.EditValue := Value; end; procedure TfrViewPeriodoFechas.SetFechaVenFinal(const Value: Variant); begin edtFechaVenFin.EditValue := Value; end; procedure TfrViewPeriodoFechas.SetFechaVenInicial(const Value: Variant); begin edtFechaVenIni.EditValue := Value; end; procedure TfrViewPeriodoFechas.TercerTrimestre(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := EncodeDate(YearOf(Today), 7, 1); edtFechaFin.Date := EncodeDate(YearOf(Today), 9, 30); end; TFechaVencimiento: begin edtFechaVenIni.Date := EncodeDate(YearOf(Today), 7, 1); edtFechaVenFin.Date := EncodeDate(YearOf(Today), 9, 30);; end; end; end; procedure TfrViewPeriodoFechas.TrimestreAnterior(const ATipoFecha: TTipoFecha); var ATrimestre : Integer; begin ATrimestre := Ceil(MonthOf(Today)/3)-1; case ATrimestre of 0: CuartoTrimestre(ATipoFecha); 1: PrimerTrimestre(ATipoFecha); 2: SegundoTrimestre(ATipoFecha); 3: TercerTrimestre(ATipoFecha); end; end; procedure TfrViewPeriodoFechas.Ultimos30dias(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := IncDay(Today, -30); edtFechaFin.Date := Today end; TFechaVencimiento: begin edtFechaVenIni.Date := IncDay(Today, -30); edtFechaVenFin.Date := Today end; end; end; procedure TfrViewPeriodoFechas.Ultimos3meses(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := IncMonth(Today, -3); edtFechaFin.Date := Today end; TFechaVencimiento: begin edtFechaVenIni.Date := IncMonth(Today, -3); edtFechaVenFin.Date := Today end; end; end; procedure TfrViewPeriodoFechas.Ultimos7dias(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := IncDay(Today, -7); edtFechaFin.Date := Today end; TFechaVencimiento: begin edtFechaVenIni.Date := IncDay(Today, -7); edtFechaVenFin.Date := Today end; end; end; procedure TfrViewPeriodoFechas.cbPeriodo2PropertiesChange(Sender: TObject); begin case (Sender as TcxComboBox).ItemIndex of 0 : Personalizado(TFechaVencimiento); 1 : EsteTrimestre(TFechaVencimiento); 2 : TrimestreAnterior(TFechaVencimiento); 3 : PrimerTrimestre(TFechaVencimiento); 4 : SegundoTrimestre(TFechaVencimiento); 5 : TercerTrimestre(TFechaVencimiento); 6 : CuartoTrimestre(TFechaVencimiento); 7 : EsteMes(TFechaVencimiento); 8 : MesAnterior(TFecha); 9 : EsteAno(TFechaVencimiento); 10 : AnoAnterior(TFechaVencimiento); else Personalizado(TFechaVencimiento); end; end; procedure TfrViewPeriodoFechas.cbPeriodoPropertiesChange(Sender: TObject); begin case (Sender as TcxComboBox).ItemIndex of 0 : Personalizado(TFecha); 1 : EsteTrimestre(TFecha); 2 : TrimestreAnterior(TFecha); 3 : PrimerTrimestre(TFecha); 4 : SegundoTrimestre(TFecha); 5 : TercerTrimestre(TFecha); 6 : CuartoTrimestre(TFecha); 7 : EsteMes(TFecha); 8 : MesAnterior(TFecha); 9 : EsteAno(TFecha); 10 : AnoAnterior(TFecha); else Personalizado(TFecha); end; end; constructor TfrViewPeriodoFechas.Create(AOwner: TComponent); begin inherited; cbPeriodo.ItemIndex := 1; cbPeriodo2.ItemIndex := -1; EsteTrimestre(TFecha); end; procedure TfrViewPeriodoFechas.CuartoTrimestre(const ATipoFecha: TTipoFecha); begin case ATipoFecha of TFecha: begin edtFechaIni.Date := EncodeDate(YearOf(Today), 10, 1); edtFechaFin.Date := EncodeDate(YearOf(Today), 12, 31); end; TFechaVencimiento: begin edtFechaVenIni.Date := EncodeDate(YearOf(Today), 10, 1); edtFechaVenFin.Date := EncodeDate(YearOf(Today), 12, 31);; end; end; end; procedure TfrViewPeriodoFechas.edtFechaIni2PropertiesValidate( Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin inherited; if Length(VarToStr(DisplayValue)) > 0 then begin if DisplayValue <> edtFechaIni.Date then cbPeriodo.ItemIndex := 12; if DisplayValue > edtFechaFin.Date then begin edtFechaFin.EditText := DisplayValue; edtFechaFin.ValidateEdit(True); end; end; end; procedure TfrViewPeriodoFechas.edtFechaVenFinPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin if Length(VarToStr(DisplayValue)) > 0 then begin if DisplayValue <> edtFechaVenFin.Date then cbPeriodo2.ItemIndex := 12; if DisplayValue < edtFechaVenIni.Date then begin edtFechaVenIni.EditText := DisplayValue; edtFechaVenIni.ValidateEdit(True); end; end; end; procedure TfrViewPeriodoFechas.edtFechaVenIniPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin inherited; if Length(VarToStr(DisplayValue)) > 0 then begin if DisplayValue <> edtFechaVenIni.Date then cbPeriodo2.ItemIndex := 12; if DisplayValue > edtFechaVenFin.Date then begin edtFechaVenFin.EditText := DisplayValue; edtFechaVenFin.ValidateEdit(True); end; end; end; procedure TfrViewPeriodoFechas.edtFechaFinPropertiesValidate( Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin inherited; if Length(VarToStr(DisplayValue)) > 0 then begin if DisplayValue <> edtFechaFin.Date then cbPeriodo.ItemIndex := 12; if DisplayValue < edtFechaIni.Date then begin edtFechaIni.EditText := DisplayValue; edtFechaIni.ValidateEdit(True); end; end; end; end.