unit uViewCliente;

interface

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

type
  IViewCliente = interface(IViewContacto)
  ['{C06F4653-1745-4B7E-A3DC-102B2F23238E}']
  end;

  TfrViewCliente = class(TfrViewContacto, IViewCliente)
    dxLayoutControlContactoItem24: TdxLayoutItem;
    eNombreComercial: TcxDBTextEdit;
    dxLayoutControlContactoItem26: TdxLayoutItem;
    cbClienteBloqueado: TcxDBCheckBox;
    dxLayoutControlContactoGroup18: TdxLayoutGroup;
    dxLayoutControlContactoItem19: TdxLayoutItem;
    cbGrupoCliente: TcxDBComboBox;
    dxLayoutControlContactoItem16: TdxLayoutItem;
    eBloqueo: TcxDBTextEdit;
    actBloqueoCliente: TAction;
    dxLayoutControlContactoItem22: TdxLayoutItem;
    eCodigoAsignado: TcxDBTextEdit;
    dxLayoutControlContactoGroup8: TdxLayoutGroup;
    dxLayoutControlContactoItem20: TdxLayoutItem;
    cbFelicitacion: TcxDBCheckBox;
    dxLayoutControlContactoItem29: TdxLayoutItem;
    dxLayoutControlContactoItem48: TdxLayoutItem;
    dxLayoutControlContactoGroup30: TdxLayoutGroup;
    cxDBCheckBox1: TcxDBCheckBox;
    Label1: TLabel;
    cbRapel: TcxDBCheckBox;
    Label2: TLabel;
    eMailAdministracion: TcxDBHyperLinkEdit;
    PngSpeedButton4: TPngSpeedButton;
    actMandarCorreoAdministracion: TAction;


    procedure cbGrupoClientePropertiesInitPopup(Sender: TObject);
    procedure cbGrupoClientePropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure actBloqueoClienteExecute(Sender: TObject);
    procedure actMandarCorreoAdministracionExecute(Sender: TObject);
    procedure eMailAdministracionPropertiesEditValueChanged(Sender: TObject);
    procedure eMailAdministracionPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure actMandarCorreoAdministracionUpdate(Sender: TObject);
  protected
    FGrupoController: IGruposClienteController;
    procedure SetContacto(const Value: IBizContacto); override;
  end;

implementation

{$R *.dfm}

uses
  uBizGruposCliente;

type
  THackcxDBHyperLinkEdit = class(TcxDBHyperLinkEdit);
  
{ TfrViewCliente }

procedure TfrViewCliente.actBloqueoClienteExecute(Sender: TObject);
begin
  inherited;
  eBloqueo.Enabled := cbClienteBloqueado.Checked;
end;

procedure TfrViewCliente.actMandarCorreoAdministracionExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMailAdministracion).DoStart;
end;

procedure TfrViewCliente.actMandarCorreoAdministracionUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMailAdministracion.Text) > 0)
end;

procedure TfrViewCliente.cbGrupoClientePropertiesInitPopup(Sender: TObject);
begin
  inherited;
  with cbGrupoCliente.Properties.Items do
  begin
    BeginUpdate;
    try
      Clear;
      AddStrings(FGrupoController.DarListaGruposCliente);
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrViewCliente.cbGrupoClientePropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AGrupo : IBizGrupoCliente;
begin
  inherited;
  with (Sender as TcxDBComboBox) do
  begin
    if (Length(DisplayValue) > 0) and (DisplayValue <> (Contacto as IBizCliente).GRUPO_CLIENTE) then
      if Properties.LookupItems.IndexOf(DisplayValue) < 0 then
      begin
        Properties.LookupItems.BeginUpdate;
        AGrupo := FGrupoController.BuscarTodos;
        AGrupo.DataTable.Active := True;
        try
          FGrupoController.Anadir(AGrupo);
          AGrupo.DESCRIPCION := DisplayValue;
          FGrupoController.Guardar(AGrupo);
          Properties.LookupItems.Add(DisplayValue);
        finally
          Properties.LookupItems.EndUpdate;
          AGrupo := NIL;
        end;
      end;
  end;
end;

procedure TfrViewCliente.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FGrupoController := TGruposClienteController.Create;
end;

procedure TfrViewCliente.CustomViewDestroy(Sender: TObject);
begin
  FGrupoController := NIL;
  inherited;
end;

procedure TfrViewCliente.eMailAdministracionPropertiesEditValueChanged(
  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 TfrViewCliente.eMailAdministracionPropertiesValidate(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;

procedure TfrViewCliente.SetContacto(const Value: IBizContacto);
begin
  inherited;
  if Assigned(FContacto) then
    eBloqueo.Enabled := cbClienteBloqueado.Checked;
end;

end.
