unit uEditorContacto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewContacto, uBizContactos, 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,

  uIEditorContacto, uContactosController, TBXStatusBars, JvExComCtrls,
  JvStatusBar, uViewContactoDatosBancarios, dxLayoutLookAndFeels, JvLabel,
  uDAInterfaces, uViewDetallesGenerico, uViewContactoListaDatosBancarios,
  uViewPersonalContacto, cxControls, cxContainer, cxEdit, cxLabel;

type
  TfEditorContacto = class(TfEditorDBItem, IEditorContacto)
    pagDatosBancarios: TTabSheet;
    dxLayoutLookAndFeel: TdxLayoutLookAndFeelList;
    dxLayoutOfficeLookAndFeel: TdxLayoutOfficeLookAndFeel;
    frViewListaDatosBancarios: TfrViewListaDatosBancarios;
    pagPersonal: TTabSheet;
    frViewPersonalContacto1: TfrViewPersonalContacto;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
  protected
    FController : IContactosController;
    FContacto: IBizContacto;
    FViewContacto : IViewContacto;
    function GetContacto: IBizContacto; virtual;
    procedure SetContacto(const Value: IBizContacto); virtual;

    function GetViewContacto: IViewContacto;
    procedure SetViewContacto(const Value: IViewContacto);
    procedure GuardarInterno; override;
    procedure EliminarInterno; override;
    property ViewContacto: IViewContacto read GetViewContacto write
            SetViewContacto;
    function GetController : IContactosController; virtual;
    procedure SetController (const Value : IContactosController); virtual;
  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    constructor Create(AOwner: TComponent); override;
    property Controller : IContactosController read GetController
      write SetController;
    property Contacto: IBizContacto read GetContacto write SetContacto;
    destructor Destroy; override;
  end;
  
implementation

{$R *.dfm}

uses
  uFactuGES_App, uCustomEditor, uDataModuleContactos,
  uDMBase, uDataModuleUsuarios;

{
******************************* TfEditorContacto *******************************
}
function TfEditorContacto.GetContacto: IBizContacto;
begin
  Result := FContacto;
end;

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

function TfEditorContacto.GetViewContacto: IViewContacto;
begin
  Result := FViewContacto;
end;

procedure TfEditorContacto.GuardarInterno;
begin
  inherited;
  FController.Guardar(FContacto);
  Modified := False;
end;

procedure TfEditorContacto.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := ATitulo;

  if Assigned(Contacto) then
  begin
    if (FTitulo = '') then
    begin
      if Contacto.EsNuevo then
        FTitulo := 'Nuevo contacto'
      else
        FTitulo := 'Contacto';
    end;

    if Length(Contacto.NOMBRE) > 0 then
      FTitulo := FTitulo + ' - ' + Contacto.NOMBRE;
  end;

  inherited PonerTitulos(FTitulo);

  Self.Caption := FTitulo + ' (' + AppFactuGES.EmpresaActiva.NOMBRE + ')';
end;

procedure TfEditorContacto.SetContacto(const Value: IBizContacto);
begin
  FContacto := Value;
  dsDataTable.DataTable := FContacto.DataTable;

  if Assigned(FViewContacto) and Assigned(Contacto) then
  begin
    FViewContacto.Contacto := Contacto;
    frViewListaDatosBancarios.dsDetalles.DataTable := FContacto.DatosBancarios.DataTable;

    if Assigned(FContacto.Personal) then
      frViewPersonalContacto1.dsDetalles.DataTable := FContacto.Personal.DataTable
    else
      frViewPersonalContacto1.dsDetalles.DataTable := NIL;
  end
  else begin
    frViewListaDatosBancarios.dsDetalles.DataTable := NIL;
    frViewPersonalContacto1.dsDetalles.DataTable := NIL;    
  end;
end;

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

  if Assigned(FViewContacto) then
    FViewContacto.Controller := FController;
end;

procedure TfEditorContacto.SetViewContacto(const Value: IViewContacto);
begin
  FViewContacto := Value;

  if Assigned(FViewContacto) and Assigned(Contacto) then
    FViewContacto.Contacto := Contacto;
end;

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

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

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

  Contacto.DataTable.Active := True;
//  FViewContacto.ShowEmbedded(pagGeneral);
//  FViewContacto.SetFocus;
end;

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

destructor TfEditorContacto.Destroy;
begin
  FViewContacto := NIL;
  FContacto := NIL;
  inherited;
end;

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

procedure TfEditorContacto.EliminarInterno;
begin
  inherited;
  if not FController.Eliminar(FContacto) then
     actRefrescar.Execute;
end;

end.

