unit uViewDatosYSeleccionProveedor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, StdCtrls, cxControls, cxContainer, cxEdit, cxLabel,
  cxDBLabel, ExtCtrls, DB, uDADataTable, uBizContactos,
  ComCtrls, ToolWin, ActnList, ImgList, PngImageList, cxTextEdit, cxDBEdit,
  pngimage, JvExControls, JvComponent, JvButton, JvTransparentButton, Mask,
  DBCtrls,
  uProveedoresController, dxLayoutControl, Buttons;

type
  IViewDatosYSeleccionProveedor = interface(IViewBase)
    ['{EE8C51BD-618D-4211-A247-6FA9BD8EBFD9}']
    function GetController: IProveedoresController;
    procedure SetController(Value: IProveedoresController);
    property Controller: IProveedoresController read GetController write SetController;

    function GetProveedor: IBizProveedor;
    procedure SetProveedor(Value: IBizProveedor);
    property Proveedor: IBizProveedor read GetProveedor write SetProveedor;

    function GetOnProveedorChanged : TNotifyEvent;
    procedure SetOnProveedorChanged (const Value : TNotifyEvent);
    property OnProveedorChanged : TNotifyEvent read GetOnProveedorChanged
      write SetOnProveedorChanged;

    function GetMsgElegirProveedor: String;
    procedure SetMsgElegirProveedor(const Value: String);
    property MsgElegirProveedor : String read GetMsgElegirProveedor
      write SetMsgElegirProveedor;
  end;

  TfrViewDatosYSeleccionProveedor = class(TfrViewBase, IViewDatosYSeleccionProveedor)
    DADataSource: TDADataSource;
    ActionList1: TActionList;
    actElegirContacto: TAction;
    actAnadirContacto: TAction;
    actVerContacto: TAction;
    PngImageList: TPngImageList;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Item1: TdxLayoutItem;
    edtlNombre: TcxDBTextEdit;
    dxLayoutControl1Item2: TdxLayoutItem;
    edtNIFCIF: TcxDBTextEdit;
    dxLayoutControl1Item3: TdxLayoutItem;
    edtCalle: TcxDBTextEdit;
    dxLayoutControl1Item4: TdxLayoutItem;
    edtPoblacion: TcxDBTextEdit;
    dxLayoutControl1Item5: TdxLayoutItem;
    edtProvincia: TcxDBTextEdit;
    dxLayoutControl1Item6: TdxLayoutItem;
    edtCodigoPostal: TcxDBTextEdit;
    Button1: TBitBtn;
    dxLayoutControl1Item7: TdxLayoutItem;
    Button2: TBitBtn;
    dxLayoutControl1Item8: TdxLayoutItem;
    Button3: TBitBtn;
    dxLayoutControl1Item9: TdxLayoutItem;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Group2: TdxLayoutGroup;
    procedure actElegirContactoExecute(Sender: TObject);
    procedure actAnadirContactoExecute(Sender: TObject);
    procedure actVerContactoExecute(Sender: TObject);
    procedure actVerContactoUpdate(Sender: TObject);
  private
    FController : IProveedoresController;
    FProveedor : IBizProveedor;
    FOnProveedorChanged : TNotifyEvent;
    FMsgElegirProveedor : String;
  protected
    function GetController: IProveedoresController;
    procedure SetController(Value: IProveedoresController);
    function GetProveedor: IBizProveedor;
    procedure SetProveedor(Value: IBizProveedor);
    function GetOnProveedorChanged : TNotifyEvent;
    procedure SetOnProveedorChanged (const Value : TNotifyEvent);
    function GetMsgElegirProveedor: String;
    procedure SetMsgElegirProveedor(const Value: String);
    procedure SetReadOnly(Value: Boolean); override;

  public
    constructor Create(AOwner: TComponent); override;
    property MsgElegirProveedor : String read GetMsgElegirProveedor write SetMsgElegirProveedor;
    property Controller: IProveedoresController read GetController write SetController;
    property Proveedor: IBizProveedor read GetProveedor write SetProveedor;
    property OnProveedorChanged : TNotifyEvent read GetOnProveedorChanged write SetOnProveedorChanged;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleProveedores, Math;

procedure TfrViewDatosYSeleccionProveedor.actElegirContactoExecute(Sender: TObject);
var
  AProveedor : IBizProveedor;
begin
  inherited;
  AProveedor := (Controller.ElegirContacto(Controller.BuscarTodos,
    FMsgElegirProveedor, False) as IBizProveedor);

  if Assigned(AProveedor) then
  begin
    Self.Update;
    ShowHourglassCursor;
    try
      Proveedor := AProveedor;
    finally
      HideHourglassCursor;
    end;
  end;
end;

procedure TfrViewDatosYSeleccionProveedor.actAnadirContactoExecute(
  Sender: TObject);
var
  AProveedor : IBizProveedor;
begin
  inherited;
  AProveedor := (FController.Nuevo as IBizProveedor);
  FController.Ver(AProveedor);
  Proveedor := AProveedor;
end;

procedure TfrViewDatosYSeleccionProveedor.actVerContactoExecute(Sender: TObject);
begin
  inherited;
  FController.Ver(Proveedor);
end;

procedure TfrViewDatosYSeleccionProveedor.actVerContactoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(edtlNombre.Text) > 0);
end;

constructor TfrViewDatosYSeleccionProveedor.Create(AOwner: TComponent);
begin
  inherited;
  FMsgElegirProveedor := '';
end;

function TfrViewDatosYSeleccionProveedor.GetProveedor: IBizProveedor;
begin
  Result := FProveedor;
end;

function TfrViewDatosYSeleccionProveedor.GetController: IProveedoresController;
begin
  Result := FController;
end;

function TfrViewDatosYSeleccionProveedor.GetMsgElegirProveedor: String;
begin
  Result := FMsgElegirProveedor;
end;

procedure TfrViewDatosYSeleccionProveedor.SetProveedor(Value: IBizProveedor);
begin
  FProveedor := Value;
  if Assigned(FProveedor) then
  begin
    DADataSource.DataTable := FProveedor.DataTable;
    if not FProveedor.DataTable.Active then
      FProveedor.DataTable.Active := True;
  end
  else
    DADataSource.DataTable := NIL;

  if Assigned(FOnProveedorChanged) then
    FOnProveedorChanged(Self);
end;

procedure TfrViewDatosYSeleccionProveedor.SetReadOnly(Value: Boolean);
begin
  inherited;

  if ReadOnly then
  begin
    actAnadirContacto.Enabled := False;
    actElegirContacto.Enabled := False;
  end;
end;

procedure TfrViewDatosYSeleccionProveedor.SetController(Value: IProveedoresController);
begin
  FController := Value;
end;

procedure TfrViewDatosYSeleccionProveedor.SetMsgElegirProveedor(
  const Value: String);
begin
  FMsgElegirProveedor := Value;
end;

function TfrViewDatosYSeleccionProveedor.GetOnProveedorChanged: TNotifyEvent;
begin
  Result := FOnProveedorChanged;
end;

procedure TfrViewDatosYSeleccionProveedor.SetOnProveedorChanged(
  const Value: TNotifyEvent);
begin
  FOnProveedorChanged := Value;
end;

end.
