unit uViewObraRelacionada;

interface

uses uViewBase, DB, uDADataTable, ImgList, Controls, PngImageList,
  Classes, ActnList, dxLayoutControl, ComCtrls, ToolWin, cxContainer,
  cxEdit, cxTextEdit, cxDBEdit, cxControls, uBizObraFinal;

type
  IViewObraRelacionada = interface(IViewBase)
  ['{B2AC5AD9-5F73-4FF4-8F8E-B2C8CDBF1B89}']
    function GetObra: IBizObraFinal;
    procedure SetObra(const Value: IBizObraFinal);
    property Obra: IBizObraFinal read GetObra write SetObra;

    function GetOnObraChanged : TNotifyEvent;
    procedure SetOnObraChanged (const Value : TNotifyEvent);
    property OnObraChanged : TNotifyEvent read GetOnObraChanged write SetOnObraChanged;
  end;

  TfrViewObraRelacionada = class(TfrViewBase, IViewObraRelacionada)
    ActionList1: TActionList;
    actElegirObra: TAction;
    actVerObra: TAction;
    PngImageList: TPngImageList;
    DADataSource: TDADataSource;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Item2: TdxLayoutItem;
    edtlNombre: TcxDBTextEdit;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Item1: TdxLayoutItem;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton2: TToolButton;
    procedure actVerObraExecute(Sender: TObject);
    procedure actVerObraUpdate(Sender: TObject);
    procedure actElegirObraExecute(Sender: TObject);
  private
    FObra : IBizObraFinal;
    FOnObraChanged : TNotifyEvent;
  protected
    function GetObra: IBizObraFinal;
    procedure SetObra(const Value: IBizObraFinal);
    function GetOnObraChanged : TNotifyEvent;
    procedure SetOnObraChanged (const Value : TNotifyEvent);
  public
    property Obra: IBizObraFinal read GetObra write SetObra;
    property OnObraChanged : TNotifyEvent read GetOnObraChanged write SetOnObraChanged;
  end;

implementation
{$R *.dfm}

uses Windows, uDataModuleObrasFinal;


{ TfrViewObraRelacionada }

procedure TfrViewObraRelacionada.actVerObraExecute(Sender: TObject);
begin
  FObra.Show;
end;

procedure TfrViewObraRelacionada.actVerObraUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(FObra);
end;

function TfrViewObraRelacionada.GetObra: IBizObraFinal;
begin
  Result := FObra;
end;

procedure TfrViewObraRelacionada.SetObra(const Value: IBizObraFinal);
begin
  FObra := Value;
  DADataSource.DataTable := FObra.DataTable;

  if not FObra.DataTable.Active then
    FObra.DataTable.Active := True;

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


function TfrViewObraRelacionada.GetOnObraChanged: TNotifyEvent;
begin
  Result := FOnObraChanged;
end;

procedure TfrViewObraRelacionada.SetOnObraChanged(const Value: TNotifyEvent);
begin
  FOnObraChanged := Value;
end;

procedure TfrViewObraRelacionada.actElegirObraExecute(Sender: TObject);
var
  AObra : IBizObraFinal;

begin
  inherited;

  if Length(edtlNombre.Text) > 0 then
    if (MessageBox(0, 'Esta factura ya tiene asociada una obra. '+#13+#10+
      'Si elige otra obra, se cambiará en esta factura el cliente '+#13+#10+
      'para hacerlos coincidir con la obra elegida. '+#13+#10+
      '¿Desea continuar?', 'Confirmación', MB_ICONQUESTION or MB_YESNO) = idNo) then
      Exit;

  AObra := dmObrasFinal.GetObrasItems;

  try
    if AObra.ShowForSelect = mrOK then
    begin
      Obra := dmObrasFinal.GetItemsSeleccionados(AObra);
    end;
  finally
    AObra := NIL;
  end;
end;

end.
