unit uEditorObras;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewObras, uBizObras, ActnList, DBActns, uViewGrid,
  Menus, uDataModuleBase, ImgList, PngImageList, TB2Dock, TB2Toolbar, TBX,
  TB2Item, StdActns, TB2ExtItems, TBXExtItems, TB2MRU, DB, uDADataTable,
  JvFormAutoSize, uDAScriptingProvider, uDACDSDataTable, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, ExtCtrls, uCustomView, uViewBase,
  uViewBarraSeleccion, pngimage, uIEditorObras, uEditorGridBase,
  JvComponentBase, uViewGridBase, uObrasController, JvExComCtrls,
  JvStatusBar, uDAInterfaces, dxGDIPlusClasses;

type
  TfEditorObras = class(TfEditorGridBase, IEditorObras)
    frViewObras1: TfrViewObras;
    procedure FormShow(Sender: TObject);
  private
    FObras: IBizObra;
    FController : IObrasController;
  protected
    function GetObras: IBizObra;
    procedure SetObras(const Value: IBizObra);
    function GetController : IObrasController; virtual;
    procedure SetController (const Value : IObrasController); virtual;

    procedure NuevoInterno; override;
    procedure EliminarInterno; override;
    procedure ModificarInterno; override;
    procedure DuplicarInterno; override;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;  
    property Obras: IBizObra read GetObras write SetObras;
    property Controller : IObrasController read GetController write SetController;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uCustomEditor, uDataModuleObras, uDataModuleUsuarios,
  uEditorObra, uEditorDBBase, uFactuGES_App,
  cxGrid, cxGridCustomTableView; //, uListaObras;

{$R *.dfm}

{
****************************** TfEditorObras *******************************
}
procedure TfEditorObras.FormShow(Sender: TObject);
begin
  inherited;

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

  if not Assigned(Obras) then
    raise Exception.Create('No hay ningún Obra asignado');

  Obras.DataTable.Active := True;
  ViewGrid.GotoFirst;
end;

function TfEditorObras.GetObras: IBizObra;
begin
  Result := FObras;
end;

function TfEditorObras.GetController: IObrasController;
begin
  Result := FController;
end;

procedure TfEditorObras.ModificarInterno;
begin
  inherited;
  FController.Ver(Obras);
end;

procedure TfEditorObras.NuevoInterno;
begin
  inherited;
  FController.Anadir(Obras);
  FController.Ver(Obras);
end;

procedure TfEditorObras.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := 'Lista de obras - ' + AppFactuGES.EmpresaActiva.NOMBRE;
  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorObras.SetObras(const Value: IBizObra);
begin
  FObras := Value;
  dsDataTable.DataTable := FObras.DataTable;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewObras).Obras := Obras;
end;

procedure TfEditorObras.SetController(const Value: IObrasController);
begin
  FController := Value;
end;

destructor TfEditorObras.Destroy;
begin
  FObras := NIL;
  inherited;
end;

procedure TfEditorObras.DuplicarInterno;
var
  AObra : IBizObra;
begin
  inherited;
  AObra := FController.Duplicar(Obras);
  try
    FController.Ver(AObra);
  finally
    actRefrescar.Execute;
  end;
end;

procedure TfEditorObras.EliminarInterno;
begin

  if (Application.MessageBox('¿Desea borrar esta obra?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    //Para que en el caso de no poderse realizar la operación se refresquen
    //los datos y no nos permita eliminar un registro a la segunda
    if not FController.Eliminar(Obras) then
       actRefrescar.Execute;
  end;
end;

constructor TfEditorObras.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewObras1; //CreateView(TfrViewObras) as IViewObras;
end;

end.
