unit uEditorEpigrafe;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewEpigrafe, uBizEpigrafes, JvNavigationPane, ActnList,
  uEditorBase, StdActns, TB2Dock, TB2Toolbar, TBX, ImgList, PngImageList,
  TB2Item, uEditorItem, DB, uDADataTable, uEditorDBBase, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, StdCtrls, pngimage, ExtCtrls,
  TBXDkPanels, JvButton, AppEvnts, uCustomView, uViewBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, JvComponentBase,

  uViewEpigrafes, uIEditorEpigrafe, uEpigrafesController, JvExComCtrls,
  JvStatusBar, dxLayoutLookAndFeels, uDAInterfaces, dxGDIPlusClasses;

type
  TfEditorEpigrafe = class(TfEditorDBItem, IEditorEpigrafe)
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutOfficeLookAndFeel1: TdxLayoutOfficeLookAndFeel;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
  protected
    FController : IEpigrafesController;
    FEpigrafe: IBizEpigrafe;
    FViewEpigrafe : IViewEpigrafe;

    function GetController : IEpigrafesController;
    procedure SetController (const Value : IEpigrafesController); virtual;
    function GetEpigrafe: IBizEpigrafe; virtual;
    procedure SetEpigrafe(const Value: IBizEpigrafe); virtual;
    function GetViewEpigrafe: IViewEpigrafe;
    procedure SetViewEpigrafe(const Value: IViewEpigrafe);
    property ViewEpigrafe: IViewEpigrafe read GetViewEpigrafe write SetViewEpigrafe;

    procedure GuardarInterno; override;
    procedure EliminarInterno; override;

    procedure PonerTitulos(const ATitulo: string = ''); override;

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;

  public
    property Epigrafe: IBizEpigrafe read GetEpigrafe write SetEpigrafe;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleBase, uFactuGES_App;

function ShowEditorEpigrafe (ABizObject : TDADataTableRules): TModalResult;
var
  AEditor: TfEditorEpigrafe;
begin
  AEditor := TfEditorEpigrafe.Create(Application);
  try
    AEditor.Epigrafe := (ABizObject as IBizEpigrafe);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

{
******************************* TfEditorEpigrafe *******************************
}
function TfEditorEpigrafe.GetEpigrafe: IBizEpigrafe;
begin
  Result := FEpigrafe;
end;

function TfEditorEpigrafe.GetController: IEpigrafesController;
begin
  Result := FController;
end;

function TfEditorEpigrafe.GetViewEpigrafe: IViewEpigrafe;
begin
  Result := FViewEpigrafe;
end;

procedure TfEditorEpigrafe.GuardarInterno;
begin
  inherited;
  FController.Guardar(FEpigrafe);
  Modified := False;
end;

procedure TfEditorEpigrafe.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  if Assigned(Epigrafe) then
  begin
    if Epigrafe.EsNuevo then
      FTitulo := 'Nuevo epígrafe - ' + AppFactuGES.EjercicioActivo.NOMBRE
    else
      FTitulo := 'Epígrafe'  + ' - ' + AppFactuGES.EjercicioActivo.NOMBRE + ' - ' + Epigrafe.DESCRIPCION;
  end;

  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorEpigrafe.SetEpigrafe(const Value: IBizEpigrafe);
begin
  FEpigrafe := Value;
  dsDataTable.DataTable := FEpigrafe.DataTable;

  if Assigned(FViewEpigrafe) and Assigned(Epigrafe) then
    FViewEpigrafe.Epigrafe := Epigrafe;
end;

procedure TfEditorEpigrafe.SetController(const Value: IEpigrafesController);
begin
  FController := Value;
  if Assigned(ViewEpigrafe) then
     ViewEpigrafe.Controller := FController;
end;

procedure TfEditorEpigrafe.SetViewEpigrafe(const Value: IViewEpigrafe);
begin
  FViewEpigrafe := Value;

  if Assigned(FViewEpigrafe) and Assigned(Epigrafe) then
    FViewEpigrafe.Epigrafe := Epigrafe;
end;

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

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

  if not Assigned(Epigrafe) then
    raise Exception.Create('No hay ningún almacén asignado');

  Epigrafe.DataTable.Active := True;
end;

destructor TfEditorEpigrafe.Destroy;
begin
  // Utilizar mejor OnClose;
  inherited;
end;

procedure TfEditorEpigrafe.AsignarVista;
var
  AViewEpigrafe: TfrViewEpigrafe;
begin
  AViewEpigrafe := TfrViewEpigrafe.create(Self);
  with AViewEpigrafe do
  begin
    Parent := pagGeneral;
    Align := alClient;
//    dxLayoutControlEpigrafe.LookAndFeel := dxLayoutOfficeLookAndFeel1;
  end;
  ViewEpigrafe := AViewEpigrafe;
end;

constructor TfEditorEpigrafe.Create(AOwner: TComponent);
begin
  inherited;
  pgPaginas.ActivePageIndex := 0;
  AsignarVista;
end;

procedure TfEditorEpigrafe.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;
  FViewEpigrafe := NIL;
  FEpigrafe := NIL;
end;

procedure TfEditorEpigrafe.dsDataTableDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if Assigned(FEpigrafe) and (not (FEpigrafe.DataTable.Fetching) or
    not (FEpigrafe.DataTable.Opening) or not (FEpigrafe.DataTable.Closing)) then
    PonerTitulos;
end;

procedure TfEditorEpigrafe.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar este Epígrafe?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if not FController.Eliminar(FEpigrafe) then
       actRefrescar.Execute;
  end;
end;

end.

