unit uEditorAlmacenes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewAlmacenes, uBizAlmacenes, 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, uIEditorAlmacenes, uEditorGridBase,
  JvComponentBase, uViewGridBase, uAlmacenesController, JvExComCtrls,
  JvStatusBar, uDAInterfaces;

type
  TfEditorAlmacenes = class(TfEditorGridBase, IEditorAlmacenes)
    frViewAlmacenes1: TfrViewAlmacenes;
    procedure FormShow(Sender: TObject);
  private
    FAlmacenes: IBizAlmacen;
    FController : IAlmacenesController;
  protected
    function GetAlmacenes: IBizAlmacen;
    procedure SetAlmacenes(const Value: IBizAlmacen);
    function GetController : IAlmacenesController; virtual;
    procedure SetController (const Value : IAlmacenesController); virtual;

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

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;  
    property Almacenes: IBizAlmacen read GetAlmacenes write SetAlmacenes;
    property Controller : IAlmacenesController read GetController write SetController;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uCustomEditor, uDataModuleAlmacenes, uDataModuleUsuarios,
  uEditorAlmacen, uEditorDBBase, uFactuGES_App,
  cxGrid, cxGridCustomTableView; //, uListaAlmacenes;

{$R *.dfm}

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

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

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

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

function TfEditorAlmacenes.GetAlmacenes: IBizAlmacen;
begin
  Result := FAlmacenes;
end;

function TfEditorAlmacenes.GetController: IAlmacenesController;
begin
  Result := FController;
end;

procedure TfEditorAlmacenes.ModificarInterno;
begin
  inherited;
  FController.Ver(Almacenes);
end;

procedure TfEditorAlmacenes.NuevoInterno;
begin
  inherited;
  FController.Anadir(Almacenes);
  FController.Ver(Almacenes);
end;

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

procedure TfEditorAlmacenes.SetAlmacenes(const Value: IBizAlmacen);
begin
  FAlmacenes := Value;
  dsDataTable.DataTable := FAlmacenes.DataTable;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewAlmacenes).Almacenes := Almacenes;
end;

procedure TfEditorAlmacenes.SetController(const Value: IAlmacenesController);
begin
  FController := Value;
end;

destructor TfEditorAlmacenes.Destroy;
begin
  FAlmacenes := NIL;
  inherited;
end;

procedure TfEditorAlmacenes.DuplicarInterno;
{var
  AAlmacen : IBizAlmacen;}
begin
{
  inherited;
  AAlmacen := FController.Duplicar(Almacenes);
  try
    FController.Ver(AAlmacen);
  finally
    actRefrescar.Execute;
  end;
}  
end;

procedure TfEditorAlmacenes.EliminarInterno;
begin

  if (Application.MessageBox('¿Desea borrar este almacén?', '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(Almacenes) then
       actRefrescar.Execute;
  end;
end;

constructor TfEditorAlmacenes.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewAlmacenes1; //CreateView(TfrViewAlmacenes) as IViewAlmacenes;
end;

end.

  