unit uEditorInformeBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane,
  uBizInformes, schInformesClient_Intf, uViewInformeBase, JvExComCtrls,
  JvStatusBar, pngimage, TB2ExtItems, TBXExtItems;

type
  IEditorInformeBase = interface(IEditorDBItem)
  ['{E8DB0818-75F8-4575-A30E-25B6599A757F}']
    function GetInforme: IBizInforme;
    procedure SetInforme(const Value: IBizInforme);
    property Informe: IBizInforme read GetInforme write SetInforme;
  end;


  TfEditorInformeBase = class(TfEditorDBItem, IEditorInformeBase)
    actPropiedades: TAction;
    TBXSeparatorItem14: TTBXSeparatorItem;
    tbxiPropiedades: TTBXItem;
    tbxiPropiedades2: TTBXItem;
    tbxFiltro: TTBXToolbar;
    TBXLabelItem1: TTBXLabelItem;
    tbxEditFiltro: TTBXEditItem;
    TBXItem34: TTBXItem;
    actQuitarFiltro: TAction;
    procedure actPropiedadesExecute(Sender: TObject);
    procedure actGuardarCerrarUpdate(Sender: TObject);
    procedure actGuardarUpdate(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actQuitarFiltroExecute(Sender: TObject);
    procedure tbxEditFiltroChange(Sender: TObject; const Text: string);

  private
    procedure ComprobarPrivilegios;

  protected
    FInforme : IBizInforme;
    FViewInforme : IViewInformeBase;
    function GetInforme: IBizInforme; virtual;
    procedure SetInforme(const Value: IBizInforme); virtual;
//    function GetModified: Boolean; override;
    procedure RellenarCabeceraInforme; virtual;
    function GetModified: Boolean; override;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    property Informe: IBizInforme read GetInforme write SetInforme;

  end;

implementation

uses uEditorDBBase, uEditorBase, uDataModuleUsuarios, uFormPropiedadesInforme,
uViewGridBase;

{$R *.dfm}

{ TfEditorControlBase }

function TfEditorInformeBase.GetInforme: IBizInforme;
begin
  Result := FInforme;
end;

function TfEditorInformeBase.GetModified: Boolean;
begin
  if not dmUsuarios.EsAdministrador then
     Result := False
  else
     Result := inherited GetModified;
end;

procedure TfEditorInformeBase.SetInforme(const Value: IBizInforme);
begin
  FInforme := Value;
  dsDataTable.DataTable := FInforme.DataTable;

  ComprobarPrivilegios;
  
  if Assigned(FViewInforme) then
     FViewInforme.Informe := FInforme;
end;

procedure TfEditorInformeBase.tbxEditFiltroChange(Sender: TObject; const Text: string);
begin
  inherited;
  if not Assigned(FViewInforme) then
     Exit;
  
  if Length(Text) > 0 then
     FViewInforme.Filter := Text
  else
     actQuitarFiltro.Execute;
end;

procedure TfEditorInformeBase.actPropiedadesExecute(Sender: TObject);
begin
  inherited;

  with TfrPropiedadesInforme.Create(NIL) do
  try
    Nombre := FInforme.NOMBRE;
    Descripcion := FInforme.DESCRIPCION;
    Modificable := True; //FInforme.EsModificable;
    Icono := FInforme.ICONO;
    if (ShowModal = mrOK) then
    begin
      FInforme.DataTable.Edit;
      FInforme.NOMBRE := Nombre;
      FInforme.DESCRIPCION := Descripcion;
      FInforme.ICONO := Icono;
      if Modificable then
        FInforme.MODIFICABLE := 'S'
      else
        FInforme.MODIFICABLE := 'N';
    end;
  finally
    Free;
    ActualizarEstadoEditor;    
//    FViewInforme.Modificable := FInforme.EsModificable;
  end;
end;

procedure TfEditorInformeBase.actQuitarFiltroExecute(Sender: TObject);
begin
  inherited;
  if Assigned(FViewInforme) then
  begin
    tbxEditFiltro.Text := '';
    FViewInforme.Filter := '';
  end;
end;

procedure TfEditorInformeBase.ComprobarPrivilegios;
begin
  if not dmUsuarios.EsAdministrador then
  begin
     actGuardarCerrar.Enabled := False;
     actGuardarCerrar.Visible := False;
     actGuardar.Enabled := False;
     actGuardar.Visible := False;
     actEliminar.Enabled := False;
     actEliminar.Visible := False;
     actPropiedades.Enabled := False;
     actPropiedades.Visible := False;
  end;
end;

{
function TfEditorInformeBase.GetModified: Boolean;
begin
  Result := inherited GetModified and
    (Assigned(FInforme) and FInforme.EsModificable);
end;
}

procedure TfEditorInformeBase.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := ATitulo;
  if (FTitulo = '') and Assigned(FInforme) then
  begin
    if FInforme.EsNuevo then
      FTitulo := 'Nuevo informe' + ' - ' + FInforme.NOMBRE
    else
      FTitulo := 'Informe ' + ' - ' + FInforme.NOMBRE;
  end;

  inherited PonerTitulos(FTitulo);

  Self.Caption := FTitulo + ' (' + dmUsuarios.EmpresaActual.NOMBRE + ')';
end;

procedure TfEditorInformeBase.actGuardarCerrarUpdate(Sender: TObject);
begin
  inherited;
//  (Sender as TAction).Enabled := Assigned(FInforme) and (FInforme.EsModificable);
end;

procedure TfEditorInformeBase.actGuardarUpdate(Sender: TObject);
begin
  inherited;
//  (Sender as TAction).Enabled := Assigned(FInforme) and (FInforme.EsModificable);
end;

procedure TfEditorInformeBase.actPrevisualizarExecute(Sender: TObject);
begin
  RellenarCabeceraInforme;
  inherited;
end;

procedure TfEditorInformeBase.RellenarCabeceraInforme;
begin
  inherited;
  //
end;

procedure TfEditorInformeBase.actImprimirExecute(Sender: TObject);
begin
  RellenarCabeceraInforme;
  inherited;
end;


procedure TfEditorInformeBase.actEliminarExecute(Sender: TObject);
begin
{
  if not FInforme.EsModificable then
    ShowMessage('Este control no se puede eliminar')
  else
    inherited
}
end;

end.
