unit CalendarioBasico;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RdxFrame, RdxBotones, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, ExtCtrls, RdxPaneles, RdxBarras, Menus, cxStyles,
  cxGraphics, cxScheduler, cxSchedulerStorage, cxSchedulerCustomControls,
  cxSchedulerCustomResourceView, cxSchedulerDayView,
  cxSchedulerDateNavigator, cxSchedulerWeekView, cxSchedulerDBStorage,
  cxCheckListBox, ActnList, IBQuery, DB, IBCustomDataSet, IBDatabase,
  IBTable, ComCtrls, ImgList, dxPSGlbl, dxPSUtl, dxPSEngn, dxPrnPg,
  dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPSContainerLnk, dxPrnDlg, dxPgsDlg,
  dxPScxCommon, cxSchedulerTimeGridView,
  cxSchedulerUtils, cxSchedulerYearView, dxPScxScheduler2Lnk;

type
  TfrCalendarioBasico = class(TRdxFrame)
    BarraAgenda: TRdxBarraSuperior;
    imgSombra: TImage;
    pnlExtra: TRdxBarraSuperior;
    bRefrescar: TRdxBoton;
    cxScheduler: TcxScheduler;
    cxSchedulerDBStorage: TcxSchedulerDBStorage;
    RdxBoton1: TRdxBoton;
    RdxBoton2: TRdxBoton;
    ActionList: TActionList;
    actDia: TAction;
    actSemana: TAction;
    actMes: TAction;
    RdxBoton3: TRdxBoton;
    bSeparador1: TShape;
    bConsultar: TRdxBotonSuperior;
    RdxBotonSuperior1: TRdxBotonSuperior;
    RdxBotonSuperior2: TRdxBotonSuperior;
    actVerTodos: TAction;
    actAnadir: TAction;
    actModificar: TAction;
    actEliminar: TAction;
    bImprimir: TRdxBotonSuperior;
    actRefrescar: TAction;
    Shape1: TShape;
    actSemana5: TAction;
    RdxBoton4: TRdxBoton;
    actImprimir: TAction;
    dxComponentPrinter1: TdxComponentPrinter;
    dxComponentPrinter1Link2: TcxSchedulerReportLink;
    dxPrintDialog1: TdxPrintDialog;
    procedure actDiaExecute(Sender: TObject); virtual;
    procedure actSemanaExecute(Sender: TObject);virtual;
    procedure actMesExecute(Sender: TObject);virtual;
    procedure actAnadirExecute(Sender: TObject);virtual;
    procedure actModificarExecute(Sender: TObject);virtual;
    procedure actModificarUpdate(Sender: TObject);virtual;
    procedure actEliminarExecute(Sender: TObject);virtual;
    procedure actEliminarUpdate(Sender: TObject);virtual;
    procedure cxSchedulerCustomDrawEvent(Sender: TObject;
      ACanvas: TcxCanvas; AViewInfo: TcxSchedulerEventCellViewInfo;
      var ADone: Boolean);virtual;
    procedure actRefrescarExecute(Sender: TObject);virtual;
    procedure cxSchedulerBeforeEditing(Sender: TcxCustomScheduler;
      AEvent: TcxSchedulerControlEvent; AInplace: Boolean;
      var Allow: Boolean);
    procedure actSemana5Execute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
  protected
    AnchorDate: TDateTime;
    procedure InicializarCampos; virtual;
    procedure InicializarEvento (var aEvent : TcxSchedulerControlEvent); virtual;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frCalendarioBasico: TfrCalendarioBasico;

implementation

{$R *.dfm}

{ TfrCalendarioMontajes }

uses
  Entidades, Configuracion, ResourcesEditor, cxSchedulerDialogs,
  dxOffice11, cxGeometry, cxLookAndFeelPainters, TablaCitas;

type
  TcxSchedulerTimeRulerCellViewInfoAccess = class(TcxSchedulerTimeRulerCellViewInfo);

constructor TfrCalendarioBasico.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  Entidad := entAgenda;
  ConfigurarFrame(Self, Self.Entidad);

  AnchorDate := Date;
  InicializarCampos;
end;

destructor TfrCalendarioBasico.Destroy;
begin
  inherited;
end;

procedure TfrCalendarioBasico.actDiaExecute(Sender: TObject);
begin
  if not cxScheduler.ViewWeek.Active then
    AnchorDate := cxScheduler.SelectedDays[0];
  cxScheduler.SelectDays([AnchorDate], True);

  cxScheduler.ViewDay.Active := True;
end;

procedure TfrCalendarioBasico.actSemanaExecute(Sender: TObject);
begin
  if not cxScheduler.ViewWeek.Active then
    AnchorDate := cxScheduler.SelectedDays[0];
  cxScheduler.SelectDays([AnchorDate], False);

  cxScheduler.ViewWeek.Active := True;
end;

procedure TfrCalendarioBasico.actMesExecute(Sender: TObject);
begin
  if not cxScheduler.ViewWeek.Active then
    AnchorDate := cxScheduler.SelectedDays[0];
  cxScheduler.SelectDays([AnchorDate], False);

  cxScheduler.GoToDate(cxScheduler.SelectedDays[0], vmMonth);
end;

procedure TfrCalendarioBasico.actAnadirExecute(Sender: TObject);
begin
  cxScheduler.CreateEventUsingDialog;
end;

procedure TfrCalendarioBasico.actModificarExecute(Sender: TObject);
begin
  if (cxScheduler.SelectedEventCount = 1) then
    cxScheduler.EditEventUsingDialog(cxScheduler.SelectedEvents[0]);
end;

procedure TfrCalendarioBasico.actModificarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (cxScheduler.SelectedEventCount = 1);
end;

procedure TfrCalendarioBasico.actEliminarExecute(Sender: TObject);
begin
  if (cxScheduler.SelectedEventCount > 0) then
    cxScheduler.DeleteSelectedEvents();
end;

procedure TfrCalendarioBasico.actEliminarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (cxScheduler.SelectedEventCount > 0);
end;

procedure TfrCalendarioBasico.cxSchedulerCustomDrawEvent(Sender: TObject;
  ACanvas: TcxCanvas; AViewInfo: TcxSchedulerEventCellViewInfo;
  var ADone: Boolean);
begin
{  if not AViewInfo.Selected then
  begin
    AViewInfo.Transparent := True;
    FillGradientRect(ACanvas, AViewInfo.Bounds, clWhite,
      AViewInfo.Color, False);
  end;
  ADone := False;}
end;

procedure TfrCalendarioBasico.actRefrescarExecute(Sender: TObject);
begin
  cxSchedulerDBStorage.FullRefresh;
end;

procedure TfrCalendarioBasico.InicializarEvento(
  var aEvent: TcxSchedulerControlEvent);
begin
  //
end;

procedure TfrCalendarioBasico.InicializarCampos;
var
  aField : TcxSchedulerDBStorageField;
begin
  inherited;
  with cxSchedulerDBStorage.CustomFields do
  begin
    try
      BeginUpdate;
      Clear;

      aField := Add;
      aField.FieldName := 'TIPOTAREA';

      aField := Add;
      aField.FieldName := 'CODIGOCONTRATO';

      aField := Add;
      aField.FieldName := 'NOMBRECLIENTE';

      aField := Add;
      aField.FieldName := 'PARTIDA';

      aField := Add;
      aField.FieldName := 'COMPLETADA';

      aField := Add;
      aField.FieldName := 'NOMBREPROVEEDOR';
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrCalendarioBasico.cxSchedulerBeforeEditing(
  Sender: TcxCustomScheduler; AEvent: TcxSchedulerControlEvent;
  AInplace: Boolean; var Allow: Boolean);
begin
  if VarIsNull(AEvent.ID) then
    InicializarEvento(AEvent);
  Allow := True;
end;

procedure TfrCalendarioBasico.actSemana5Execute(Sender: TObject);
begin
  AnchorDate := cxScheduler.SelectedDays[0];
  cxScheduler.SelectDays([AnchorDate], True);

  cxScheduler.SelectWorkDays(Date);
end;

procedure TfrCalendarioBasico.actImprimirExecute(Sender: TObject);
begin
  inherited;
  with dxComponentPrinter1Link2.PrintStyles do
  begin
    if cxScheduler.ViewDay.Active then
    begin
      Daily.Active := True;
      AnchorDate := cxScheduler.SelectedDays.Items[0];
    end
    else
      if cxScheduler.ViewWeek.Active then
        Weekly.Active := True
      else begin
        Monthly.Active := True;
        AnchorDate := cxScheduler.SelectedDays.Items[cxScheduler.SelectedDays.Count DIV 2];
      end;
  end;
  dxComponentPrinter1Link2.PrintRange.PrintFrom := AnchorDate;
  dxComponentPrinter1Link2.PrintRange.PrintTo := AnchorDate;
  dxComponentPrinter1.Print(True, nil, nil);
end;

end.
