unit uEditorAlmacen;

interface

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

  uViewAlmacenes, uIEditorAlmacen, uAlmacenesController, JvExComCtrls,
  JvStatusBar, dxLayoutLookAndFeels, uDAInterfaces, cxGraphics, cxControls,
  cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit, cxLabel;

type
  TfEditorAlmacen = class(TfEditorDBItem, IEditorAlmacen)
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
  protected
    FController : IAlmacenesController;
    FAlmacen: IBizAlmacen;
    FViewAlmacen : IViewAlmacen;

    function GetController : IAlmacenesController;
    procedure SetController (const Value : IAlmacenesController); virtual;
    function GetAlmacen: IBizAlmacen; virtual;
    procedure SetAlmacen(const Value: IBizAlmacen); virtual;
    function GetViewAlmacen: IViewAlmacen;
    procedure SetViewAlmacen(const Value: IViewAlmacen);
    property ViewAlmacen: IViewAlmacen read GetViewAlmacen write SetViewAlmacen;

    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 Almacen: IBizAlmacen read GetAlmacen write SetAlmacen;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleAlmacenes, uDataModuleBase;

function ShowEditorAlmacen (ABizObject : TDADataTableRules): TModalResult;
var
  AEditor: TfEditorAlmacen;
begin
  AEditor := TfEditorAlmacen.Create(Application);
  try
    AEditor.Almacen := (ABizObject as IBizAlmacen);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

{
******************************* TfEditorAlmacen *******************************
}
function TfEditorAlmacen.GetAlmacen: IBizAlmacen;
begin
  Result := FAlmacen;
end;

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

function TfEditorAlmacen.GetViewAlmacen: IViewAlmacen;
begin
  Result := FViewAlmacen;
end;

procedure TfEditorAlmacen.GuardarInterno;
begin
  inherited;
  FController.Guardar(FAlmacen);
  Modified := False;
end;

procedure TfEditorAlmacen.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  if Assigned(Almacen) then
  begin
    if Almacen.EsNuevo then
      FTitulo := 'Nuevo almacén'
    else
      FTitulo := 'Almacén'  + ' - ' + Almacen.NOMBRE;
  end;

  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorAlmacen.SetAlmacen(const Value: IBizAlmacen);
begin
  FAlmacen := Value;
  dsDataTable.DataTable := FAlmacen.DataTable;

  if Assigned(FViewAlmacen) and Assigned(Almacen) then
    FViewAlmacen.Almacen := Almacen;
end;

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

procedure TfEditorAlmacen.SetViewAlmacen(const Value: IViewAlmacen);
begin
  FViewAlmacen := Value;

  if Assigned(FViewAlmacen) and Assigned(Almacen) then
    FViewAlmacen.Almacen := Almacen;
end;

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

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

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

  Almacen.DataTable.Active := True;
end;

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

procedure TfEditorAlmacen.AsignarVista;
var
  AViewAlmacen: TfrViewAlmacen;
begin
  AViewAlmacen := TfrViewAlmacen.create(Self);
  with AViewAlmacen do
  begin
    Parent := pagGeneral;
    Align := alClient;
    dxLayoutControlAlmacen.LookAndFeel := dmBase.dxLayoutSkinLookAndFeel;
  end;
  ViewAlmacen := AViewAlmacen;
end;

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

procedure TfEditorAlmacen.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;
  FViewAlmacen := NIL;
  FAlmacen := NIL;
end;

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

procedure TfEditorAlmacen.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar este almacen?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if not FController.Eliminar(FAlmacen) then
       actRefrescar.Execute;
  end;
end;

end.

