{
===============================================================================
  Copyright (©) 2006. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 22-05-2006
  Versión actual: 1.0.0
  Fecha versión actual: 22-05-2006
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit uEditorContacto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewContacto, uBizContacto, JvNavigationPane, ActnList,
  uEditorBase, StdActns, TB2Dock, TB2Toolbar, TBX, ImgList, PngImageList,
  TB2Item, uEditorDBItem, DB, uDADataTable, uCustomView, uViewBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, dxLayoutLookAndFeels,
  uViewDatosFacturacion, uViewObservaciones, uViewDatosBancarios, ExtCtrls,
  JvComponentBase, StdCtrls;

type
  IEditorContacto = interface(IEditorDBItem)
    ['{A9E35279-DF14-4571-A3EB-DCFF7EDC8473}']
    function GetContacto: IBizContacto;
    procedure SetContacto(const Value: IBizContacto);
    property Contacto: IBizContacto read GetContacto write SetContacto;
  end;

  TfEditorContacto = class(TfEditorDBItem, IEditorContacto)
    frViewContacto1: TfrViewContacto;
    pagDetalles: TTabSheet;
    frViewDatosBancarios1: TfrViewDatosBancarios;
    frViewDatosFacturacion1: TfrViewDatosFacturacion;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);

    procedure actEliminarExecute(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actGuardarExecute(Sender: TObject);
  private
    FContacto: IBizContacto;
    FViewContacto: IViewContacto;
  protected
    function GetContacto: IBizContacto; virtual;
    procedure SetContacto(const Value: IBizContacto); virtual;
    function GetViewContacto: IViewContacto;
    procedure SetViewContacto(const Value: IViewContacto);
    property ViewContacto: IViewContacto read GetViewContacto write
            SetViewContacto;
  public
    property Contacto: IBizContacto read GetContacto write SetContacto;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleContactos, uDataModuleBase,
  uEditorUtils, LiteralesContactos, uEditorDBBase;

function ShowEditorContacto (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorContacto;
begin
  AEditor := TfEditorContacto.Create(Application);
  try
    AEditor.Contacto := (ABizObject as IBizContacto);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

{
******************************* TfEditorContacto *******************************
}

procedure TfEditorContacto.actEliminarExecute(Sender: TObject);
begin
  if (MessageDlg(msgLitBorrar, mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
     inherited;
end;

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

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

  if Assigned(FViewContacto) and Assigned(Contacto) then
  begin
    dsDataTable.DataTable := FContacto.DataTable;
    FViewContacto.Contacto := Contacto;
  end
  else
    dsDataTable.DataTable := Nil;

  frViewDatosBancarios1.DADataSource.DataTable := dsDataTable.DataTable;
  frViewDatosFacturacion1.DADataSource.DataTable := dsDataTable.DataTable;
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');

  if not Contacto.DataTable.Active then
  begin
     Contacto.DataTable.Active := True;
     Contacto.Categorias.DataTable.Active := True;
     Contacto.Asociados.DataTable.Active := True;
  end;
     
  FViewContacto.SetFocus;
end;

constructor TfEditorContacto.Create(AOwner: TComponent);
begin
  inherited;
  ViewContacto := frViewContacto1;
end;

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

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

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

procedure TfEditorContacto.dsDataTableDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if Assigned(FContacto) and (not (FContacto.DataTable.Fetching) or
    not (FContacto.DataTable.Opening) or not (FContacto.DataTable.Closing)) then
  begin
    if Length(FContacto.NOMBRE) = 0 then
      JvNavPanelHeader.Caption := msgNuevoContacto
    else
      JvNavPanelHeader.Caption := msgContacto + FContacto.NOMBRE;
    Self.Caption := JvNavPanelHeader.Caption;
  end;
end;

procedure TfEditorContacto.actRefrescarExecute(Sender: TObject);
begin
  inherited;
  ViewContacto.Refrescar;
end;

procedure TfEditorContacto.actGuardarExecute(Sender: TObject);
begin
  inherited;
  ViewContacto.Refrescar;
end;

destructor TfEditorContacto.Destroy;
begin
  ViewContacto := Nil;
  inherited;
end;

initialization
  RegisterEditor(IBizContacto, ShowEditorContacto, etItem);

end.

