unit uViewContacto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, ExtCtrls, StdCtrls, Buttons, DB, uDADataTable,
  DBCtrls, Grids, DBGrids, uBizContactos, Mask, ComCtrls, uCustomView,
  JvComponent, JvFormAutoSize, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxDBEdit, dxLayoutControl, dxLayoutLookAndFeels, cxMemo, cxHyperLinkEdit,
  ImgList, ActnList, PngImageList, PngSpeedButton, uDAInterfaces, uBizEmpresasTiendas,
  cxGraphics, cxMaskEdit, cxDropDownEdit, uContactosController,
  cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox, uViewTienda;

type
  IViewContacto = interface(IViewBase)
    ['{5FCFD172-5F8A-4639-B479-1CF0061526B5}']
    function GetController : IContactosController;
    procedure SetController (const Value : IContactosController);
    property Controller : IContactosController read GetController
      write SetController;

    function GetContacto: IBizContacto;
    procedure SetContacto(const Value: IBizContacto);
    property Contacto: IBizContacto read GetContacto write SetContacto;
  end;

  TfrViewContacto = class(TfrViewBase, IViewContacto)
    dsContacto: TDADataSource;
    dxLayoutControlContactoGroup_Root: TdxLayoutGroup;
    dxLayoutControlContacto: TdxLayoutControl;
    dxLayoutControlContactoGroup1: TdxLayoutGroup;
    dxLayoutControlContactoGroup2: TdxLayoutGroup;
    dxLayoutControlContactoGroup3: TdxLayoutGroup;
    dxLayoutControlContactoGroup4: TdxLayoutGroup;
    dxLayoutControlContactoGroup5: TdxLayoutGroup;
    dxLayoutControlContactoGroup6: TdxLayoutGroup;
    dxLayoutControlContactoGroup7: TdxLayoutGroup;
    dxLayoutControlContactoGroup9: TdxLayoutGroup;
    dxLayoutControlContactoItem1: TdxLayoutItem;
    eCalle: TcxDBTextEdit;
    dxLayoutControlContactoItem2: TdxLayoutItem;
    cbProvincia: TcxDBComboBox;
    dxLayoutControlContactoItem3: TdxLayoutItem;
    cbPoblacion: TcxDBComboBox;
    dxLayoutControlContactoItem4: TdxLayoutItem;
    eCodigoPostal: TcxDBTextEdit;
    eObservaciones: TcxDBMemo;
    dxLayoutControlContactoItem8: TdxLayoutItem;
    dxLayoutControlContactoItem9: TdxLayoutItem;
    eTlfParticular: TcxDBTextEdit;
    dxLayoutControlContactoItem10: TdxLayoutItem;
    eTlfTrabajo: TcxDBTextEdit;
    dxLayoutControlContactoItem11: TdxLayoutItem;
    eTlfMovil: TcxDBTextEdit;
    dxLayoutControlContactoItem12: TdxLayoutItem;
    eFax: TcxDBTextEdit;
    dxLayoutControlContactoItem13: TdxLayoutItem;
    eNombre: TcxDBTextEdit;
    dxLayoutControlContactoItem14: TdxLayoutItem;
    eNIFCIF: TcxDBTextEdit;
    dxLayoutControlContactoItem7: TdxLayoutItem;
    eMailTrabajo: TcxDBHyperLinkEdit;
    ActionList1: TActionList;
    actMandarCorreoParticular: TAction;
    actMandarCorreoTrabajo: TAction;
    actVerPaginaWeb: TAction;
    dxLayoutControlContactoItem6: TdxLayoutItem;
    eMailParticular: TcxDBHyperLinkEdit;
    dxLayoutControlContactoGroup110: TdxLayoutGroup;
    dxLayoutControlContactoItem5: TdxLayoutItem;
    ePaginaWeb: TcxDBHyperLinkEdit;
    ImageList1: TPngImageList;
    PngSpeedButton1: TPngSpeedButton;
    dxLayoutControlContactoItem15: TdxLayoutItem;
    PngSpeedButton2: TPngSpeedButton;
    dxLayoutControlContactoItem17: TdxLayoutItem;
    PngSpeedButton3: TPngSpeedButton;
    dxLayoutControlContactoItem18: TdxLayoutItem;
    dxLayoutControlContactoGroup12: TdxLayoutGroup;
    dxLayoutControlContactoGroup13: TdxLayoutGroup;
    dxLayoutControlContactoGroup11: TdxLayoutGroup;
    dxLayoutControlContactoItem25: TdxLayoutItem;
    eReferencia: TcxDBTextEdit;
    dxLayoutControlContactoGroup16: TdxLayoutGroup;
    dxLayoutControlContactoItem167: TdxLayoutItem;
    ePersonaContacto: TcxDBTextEdit;
    dxLayoutControlContactoGroup100: TdxLayoutGroup;
    frViewTienda1: TfrViewTienda;
    dxLayoutControlContactoItem16: TdxLayoutItem;
    dxLayoutControlContactoGroup10: TdxLayoutGroup;
    procedure eMailTrabajoPropertiesEditValueChanged(Sender: TObject);
    procedure actMandarCorreoParticularExecute(Sender: TObject);
    procedure actMandarCorreoTrabajoExecute(Sender: TObject);
    procedure actVerPaginaWebExecute(Sender: TObject);
    procedure actMandarCorreoParticularUpdate(Sender: TObject);
    procedure actMandarCorreoTrabajoUpdate(Sender: TObject);
    procedure actVerPaginaWebUpdate(Sender: TObject);
    procedure eMailTrabajoPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbProvinciaPropertiesInitPopup(Sender: TObject);
    procedure cbPoblacionPropertiesInitPopup(Sender: TObject);
  private
    FProvincias : TStringList;
    FPoblaciones : TStringList;
    procedure CargarProvincias;
    procedure CargarPoblaciones;
  protected
    FContacto: IBizContacto;
    FController: IContactosController;
    function GetContacto: IBizContacto; virtual;
    procedure SetContacto(const Value: IBizContacto); virtual;
    function GetController : IContactosController;
    procedure SetController (const Value : IContactosController);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Contacto: IBizContacto read GetContacto write SetContacto;
    property Controller : IContactosController read GetController write SetController;
  end;

implementation
{$R *.dfm}

uses
  uFactuGES_App, uProvinciasPoblacionesController, uStringsUtils;

type
  THackcxDBHyperLinkEdit = class(TcxDBHyperLinkEdit);

{ TfrViewContactos }

{
******************************* TfrViewContacto ********************************
}
procedure TfrViewContacto.actMandarCorreoParticularExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMailParticular).DoStart;
end;

procedure TfrViewContacto.actMandarCorreoParticularUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMailParticular.Text) > 0)
end;

procedure TfrViewContacto.actMandarCorreoTrabajoExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMailTrabajo).DoStart;
end;

procedure TfrViewContacto.actMandarCorreoTrabajoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMailTrabajo.Text) > 0)
end;

procedure TfrViewContacto.actVerPaginaWebExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(ePaginaWeb).DoStart;
end;

procedure TfrViewContacto.actVerPaginaWebUpdate(Sender: TObject);
begin
  inherited;
 (Sender as TAction).Enabled := (Length(ePaginaWeb.Text) > 0)
end;

procedure TfrViewContacto.CargarPoblaciones;
var 
  i : integer;
   AID : Integer;
begin
  AID := StrToInt(FProvincias.Values[cbProvincia.Text]);
  with TProvinciasPoblacionesController.Create do
  begin
    try
      FPoblaciones := DarListaPoblaciones(AID);
      with cbPoblacion.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FPoblaciones.Count - 1 do
            Add(FPoblaciones.Names[i]);
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
  end;
end;

procedure TfrViewContacto.CargarProvincias;
var
  i : integer;
begin
  with TProvinciasPoblacionesController.Create do
  begin
    try
      FProvincias := DarListaProvincias;

      with cbProvincia.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FProvincias.Count - 1 do
            Add(FProvincias.Names[i]);
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
  end;
end;

procedure TfrViewContacto.cbPoblacionPropertiesInitPopup(Sender: TObject);
begin
  ShowHourglassCursor;
  try
    FreeANDNIL(FPoblaciones);
    if not Assigned(FProvincias) then
      CargarProvincias;
    if not EsCadenaVacia(cbProvincia.Text) and (FProvincias.IndexOfName(cbProvincia.Text) <> -1) then
      CargarPoblaciones
  finally
    HideHourglassCursor;
  end;
end;

procedure TfrViewContacto.cbProvinciaPropertiesInitPopup(Sender: TObject);
begin
  ShowHourglassCursor; 
  try
    if not Assigned(FProvincias) then
      CargarProvincias;
  finally
    HideHourglassCursor;
  end;
end;

constructor TfrViewContacto.Create(AOwner: TComponent);
begin
  inherited;
  FProvincias := NIL;
  FPoblaciones := NIL;
end;

destructor TfrViewContacto.Destroy;
begin
  if Assigned(FProvincias) then 
    FreeANDNIL(FProvincias);

  if Assigned(FPoblaciones) then
    FreeANDNIL(FPoblaciones);

  inherited;
end;

procedure TfrViewContacto.eMailTrabajoPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  if not VarIsNull((Sender as TcxDBHyperLinkEdit).EditValue) then
     (Sender as TcxDBHyperLinkEdit).EditValue := StringReplace((Sender as TcxDBHyperLinkEdit).EditValue, (Sender as TcxDBHyperLinkEdit).Properties.Prefix, '', []);
end;

procedure TfrViewContacto.eMailTrabajoPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  inherited;
  if not VarIsNull(DisplayValue) then
     DisplayValue := StringReplace(DisplayValue, (Sender as TcxDBHyperLinkEdit).Properties.Prefix, '', []);
end;

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

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

procedure TfrViewContacto.SetContacto(const Value: IBizContacto);
begin
  FContacto := Value;
  if Assigned(FContacto) then
  begin
    dsContacto.DataTable := FContacto.DataTable;
    frViewTienda1.DataItem := FContacto.DataTable;
  end
  else begin
    dsContacto.DataTable := Nil;
  end;
end;

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

initialization
  RegisterClass(TfrViewContacto);

finalization
  UnRegisterClass(TfrViewContacto);

end.

