unit uEditorCalendarioBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, dxLayoutLookAndFeels, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, pngimage, ExtCtrls, JvExControls, JvComponent,
  JvNavigationPane, uBizCitas, uViewCalendarioBase, cxSchedulerStorage,
  cxSchedulerDBStorage, DB, uDADataTable,


  cxScheduler, cxSchedulerCustomControls, cxSchedulerCustomResourceView,
  cxSchedulerDayView, cxSchedulerDateNavigator, cxSchedulerTimeGridView,
  cxSchedulerUtils, cxSchedulerWeekView, cxSchedulerYearView, dxPSGlbl,
  dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider,
  dxPSFillPatterns, dxPSEdgePatterns, dxPrnDlg, dxPSCore, dxPScxCommon,
  dxPScxScheduler2Lnk;

type
  IEditorCalendarioBase = interface(IEditorBase)
  ['{FC69C1D8-B566-4CBF-9F87-16728114D161}']
    function GetCitas: IBizCitas;
    procedure SetCitas(const Value: IBizCitas);
    property Citas: IBizCitas read GetCitas write SetCitas;
  end;

  TfEditorCalendarioBase = class(TfEditorBase, IEditorCalendarioBase)
    TBXItem7: TTBXItem;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem33: TTBXItem;
    TBXItem34: TTBXItem;
    actDia: TAction;
    actSemana: TAction;
    actMes: TAction;
    cxSchedulerDBStorage: TcxSchedulerDBStorage;
    DADataSource: TDADataSource;
    actSemana5: TAction;
    dxComponentPrinter1: TdxComponentPrinter;
    dxComponentPrinter1Link2: TcxSchedulerReportLink;
    dxPrintDialog1: TdxPrintDialog;

    procedure FormShow(Sender: TObject);
    procedure actDiaExecute(Sender: TObject);
    procedure actSemanaExecute(Sender: TObject);
    procedure actMesExecute(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actNuevoExecute(Sender: TObject);
    procedure actModificarExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actSemana5Execute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);

    procedure cxSchedulerBeforeEditing(Sender: TcxCustomScheduler;
      AEvent: TcxSchedulerControlEvent; AInplace: Boolean;
      var Allow: Boolean);
    procedure actPrevisualizarExecute(Sender: TObject);

  private
    FCitas: IBizCitas;
    FViewCitas: IViewCalendarioBase;

  protected
    function GetViewCitas: IViewCalendarioBase;
    procedure SetViewCitas(const Value: IViewCalendarioBase);
    function GetCitas: IBizCitas;
    procedure SetCitas(const Value: IBizCitas);

    procedure InicializarCampos; virtual;
    procedure InicializarEvento (var aEvent : TcxSchedulerControlEvent); virtual;

  public
    property Citas: IBizCitas read GetCitas write SetCitas;
    property ViewCitas: IViewCalendarioBase read GetViewCitas write SetViewCitas;
    destructor Destroy; override;
  end;

var
  fEditorCalendarioBase: TfEditorCalendarioBase;

implementation
{$R *.dfm}

{ TfEditorCalendarioBase }

uses schCitasClient_Intf;

function TfEditorCalendarioBase.GetCitas: IBizCitas;
begin
  Result := FCitas;
end;

function TfEditorCalendarioBase.GetViewCitas: IViewCalendarioBase;
begin
  Result := FViewCitas;
end;

procedure TfEditorCalendarioBase.SetCitas(const Value: IBizCitas);
begin
  FCitas := Value;

  if Assigned(FCitas) then
  begin
    DADataSource.DataTable := FCitas.DataTable;

    if not DADataSource.DataTable.Active then
       DADataSource.DataTable.Active := True;
  end
  else
    DADataSource.DataTable := NIL;
end;

procedure TfEditorCalendarioBase.SetViewCitas(const Value: IViewCalendarioBase);
begin
  FViewCitas := Value;

  if Assigned(FViewCitas) then
  begin
     FViewCitas.Calendario.OnBeforeEditing := cxSchedulerBeforeEditing;
     dxComponentPrinter1Link2.Component := FViewCitas.Calendario;
  end
  else
    dxComponentPrinter1Link2.Component := NIL;
end;

procedure TfEditorCalendarioBase.FormShow(Sender: TObject);
begin
  inherited;

  InicializarCampos;

  if not Assigned(FViewCitas) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(Citas) then
    raise Exception.Create('No hay ninguna cita asignada');


end;

destructor TfEditorCalendarioBase.Destroy;
begin
  FViewCitas := NIL;
  FCitas := NIL;
  inherited;
end;

procedure TfEditorCalendarioBase.actDiaExecute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    if not ViewWeek.Active then
      FViewCitas.pAnchorDate := SelectedDays[0];
    SelectDays([FViewCitas.pAnchorDate], True);

    ViewDay.Active := True;
  end;
end;

procedure TfEditorCalendarioBase.actSemanaExecute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    if not ViewWeek.Active then
      FViewCitas.pAnchorDate := SelectedDays[0];
    SelectDays([FViewCitas.pAnchorDate], False);

    ViewWeek.Active := True;
  end;
end;

procedure TfEditorCalendarioBase.actMesExecute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    if not ViewWeek.Active then
      FViewCitas.pAnchorDate := SelectedDays[0];
    SelectDays([FViewCitas.pAnchorDate], False);

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

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

procedure TfEditorCalendarioBase.actNuevoExecute(Sender: TObject);
begin
  FViewCitas.Calendario.CreateEventUsingDialog;
end;

procedure TfEditorCalendarioBase.actModificarExecute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    if (SelectedEventCount = 1) then
      EditEventUsingDialog(SelectedEvents[0]);
  end;
end;

procedure TfEditorCalendarioBase.actEliminarExecute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    if (SelectedEventCount > 0) then
      DeleteSelectedEvents();
  end;
end;

procedure TfEditorCalendarioBase.actModificarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FViewCitas.Calendario.SelectedEventCount = 1);
end;

procedure TfEditorCalendarioBase.actEliminarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FViewCitas.Calendario.SelectedEventCount > 0);
end;

procedure TfEditorCalendarioBase.actSemana5Execute(Sender: TObject);
begin
  with FViewCitas.Calendario do
  begin
    FViewCitas.pAnchorDate := SelectedDays[0];
    SelectDays([FViewCitas.pAnchorDate], True);

    SelectWorkDays(Date);
  end;
end;

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

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

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

procedure TfEditorCalendarioBase.InicializarCampos;
var
  aField : TcxSchedulerDBStorageField;
begin
  inherited;

  with cxSchedulerDBStorage.CustomFields do
  begin
    try
      BeginUpdate;
      Clear;

      aField := Add;
      aField.FieldName := fld_CITASTIPOTAREA;

      aField := Add;
      aField.FieldName := fld_CITASCODIGOCONTRATO;

      aField := Add;
      aField.FieldName := fld_CITASNOMBRECLIENTE;

      aField := Add;
      aField.FieldName := fld_CITASPARTIDA;

      aField := Add;
      aField.FieldName := fld_CITASCOMPLETADA;

      aField := Add;
      aField.FieldName := fld_CITASNOMBREPROVEEDOR;

    finally
      EndUpdate;
    end;
  end;
end;

procedure TfEditorCalendarioBase.actPrevisualizarExecute(Sender: TObject);
begin
  inherited;
  with FViewCitas.Calendario do
  begin
    with dxComponentPrinter1Link2.PrintStyles do
    begin
      if ViewDay.Active then
      begin
        Daily.Active := True;
        FViewCitas.pAnchorDate := SelectedDays.Items[0];
      end
      else
        if ViewWeek.Active then
          Weekly.Active := True
        else begin
          Monthly.Active := True;
          FViewCitas.pAnchorDate := SelectedDays.Items[SelectedDays.Count DIV 2];
        end;
    end;
    dxComponentPrinter1Link2.PrintRange.PrintFrom := FViewCitas.pAnchorDate;
    dxComponentPrinter1Link2.PrintRange.PrintTo := FViewCitas.pAnchorDate;
    dxComponentPrinter1.Preview(True, nil);
  end;
end;

end.
