unit uEditorContactos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, uEditorGridBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewContactos, uBizContactos, ActnList, DBActns, uViewGridBase,
  Menus, uDMBase, ImgList, PngImageList, TB2Dock, TB2Toolbar, TBX,
  TB2Item, StdActns, TB2ExtItems, TBXExtItems, TB2MRU, DB, uDADataTable,
  JvFormAutoSize, uDAScriptingProvider, uDACDSDataTable, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, ExtCtrls, uCustomView, uViewBase,
  uViewBarraSeleccion, JvComponentBase, uContactosController,

  uIEditorContactos, pngimage, TBXStatusBars, JvExComCtrls, JvStatusBar,
  uDAInterfaces;

type
  TfEditorContactos = class(TfEditorGridBase, IEditorContactos)
    TBXSubmenuItemAcciones: TTBXSubmenuItem;
    procedure FormShow(Sender: TObject);
  protected
    FContactos: IBizContacto;
    FController : IContactosController;

    function GetContactos: IBizContacto; virtual;
    procedure SetContactos(const Value: IBizContacto); virtual;
    procedure SetViewGrid(const Value: IViewGridBase); override;
    function GetController : IContactosController; virtual;
    procedure SetController (const Value : IContactosController); virtual;
    procedure NuevoInterno; override;
    procedure ModificarInterno; override;
    procedure EliminarInterno; override;
    procedure DuplicarInterno; override;
    procedure ImprimirInterno; override;
    procedure PrevisualizarInterno; override;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    property Controller : IContactosController read GetController
      write SetController;
    property Contactos: IBizContacto read GetContactos write SetContactos;
    destructor Destroy; override;
  end;

implementation

uses
  uCustomEditor, uDataModuleContactos, uFactuGES_App,
  uEditorDBBase, cxGrid, cxGridCustomTableView,
  uDBSelectionListUtils;

{$R *.dfm}

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

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

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

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

function TfEditorContactos.GetContactos: IBizContacto;
begin
  Result := FContactos;
end;

function TfEditorContactos.GetController: IContactosController;
begin
  Result := FController;
end;

procedure TfEditorContactos.ImprimirInterno;
begin
  inherited;
end;

procedure TfEditorContactos.ModificarInterno;
begin
  inherited;
  FController.Ver(Contactos);
end;

procedure TfEditorContactos.NuevoInterno;
begin
  inherited;
  FController.Anadir(Contactos);
  FController.Ver(Contactos);
end;

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

procedure TfEditorContactos.PrevisualizarInterno;
begin
  inherited;
end;

procedure TfEditorContactos.SetContactos(const Value: IBizContacto);
begin
  FContactos := Value;

  if Assigned(FContactos) and Assigned(FContactos.DataTable) then
  begin
    dsDataTable.DataTable := FContactos.DataTable;
    if Assigned(ViewGrid) then
      (ViewGrid as IViewContactos).Contactos := FContactos;
  end;
end;

procedure TfEditorContactos.SetController(const Value: IContactosController);
begin
  FController := Value;
end;

procedure TfEditorContactos.SetViewGrid(const Value: IViewGridBase);
begin
  inherited;
  if Assigned(ViewGrid) and Assigned(Contactos) then
    (ViewGrid as IViewContactos).Contactos := Contactos;
end;

destructor TfEditorContactos.Destroy;
begin
  FContactos := NIL;
  FController := NIL;
  inherited;
end;

procedure TfEditorContactos.DuplicarInterno;
var
  AContacto : IBizContacto;
begin
  inherited;
  AContacto := FController.Duplicar(Contactos);
  try
    FController.Ver(AContacto);
  finally
    actRefrescar.Execute;
  end;
end;

procedure TfEditorContactos.EliminarInterno;
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(Contactos) then
     actRefrescar.Execute;
end;

end.

