{
===============================================================================
  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 uViewContactos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewGrid, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPScxGridLnk, ActnList,
  uDADataTable, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  uDAScriptingProvider, uDACDSDataTable, uBizContacto, StdCtrls, ImgList,
  PngImageList;

type
  TViewType = (vtAll, vtCat);

  IViewContactos = interface(IViewGrid)
    ['{5FCFD172-5F8A-4639-B479-1CF0061526B5}']
    function GetContactos: IBizContacto;
    procedure SetContactos(const Value: IBizContacto);
    property Contactos: IBizContacto read GetContactos write SetContactos;
    function GetViewType: TViewType;
    procedure SetViewType(const Value: TViewType);
  end;

  TfrViewContactos = class(TfrViewGrid, IViewContactos)
    FContactosMem: TDACDSDataTable;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewCODIGOEMPRESA: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewFECHAALTA: TcxGridDBColumn;
    cxGridViewUSUARIO: TcxGridDBColumn;
    cxGridViewNIFCIF: TcxGridDBColumn;
    cxGridViewNOMBRE: TcxGridDBColumn;
    cxGridViewCALLE: TcxGridDBColumn;
    cxGridViewPROVINCIA: TcxGridDBColumn;
    cxGridViewCODIGOPOSTAL: TcxGridDBColumn;
    cxGridViewPOBLACION: TcxGridDBColumn;
    cxGridViewTELEFONO1: TcxGridDBColumn;
    cxGridViewTELEFONO2: TcxGridDBColumn;
    cxGridViewMOVIL: TcxGridDBColumn;
    cxGridViewFAX: TcxGridDBColumn;
    cxGridViewCORREO1: TcxGridDBColumn;
    cxGridViewCORREO2: TcxGridDBColumn;
    cxGridViewPAGINAWEB: TcxGridDBColumn;
    cxGridViewPUESTO: TcxGridDBColumn;
    cxGridViewTRABAJO: TcxGridDBColumn;
    cxGridViewLISTACATEGORIAS: TcxGridDBColumn;
    cxGridViewCATEGORIAS: TcxGridDBColumn;
    actVistaTodos: TAction;
    actVistaCategorias: TAction;
    PngImageList: TPngImageList;
    procedure actVistaTodosExecute(Sender: TObject);
    procedure actVistaCategoriasExecute(Sender: TObject);
    procedure cxGridViewCODIGOCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    FViewType: TViewType;
    procedure deshabilitarVistaCategorias;
    procedure habilitarVistaCategorias;
    procedure darListaCategorias(const Cadena: String; var Lista: TStringList);
    procedure rellenarContactosMem;
  protected
    FContactos: IBizContacto;
    function GetContactos: IBizContacto; virtual;
    procedure SetContactos(const Value: IBizContacto); virtual;
    function GetViewType: TViewType;
    procedure SetViewType(const Value: TViewType);
  public
    property DataSet: IBizContacto read GetContactos write SetContactos;
    property ViewType: TViewType read GetViewType write SetViewType;

    procedure SyncFocusedRecordsFromDataSet; override;
    procedure SyncFocusedRecordsGrid;
    procedure SyncFocusedRecordsFromGrid; override;
    procedure RefreshGrid; override;

    procedure Preview; override;
    procedure Print; override;
  end;

var
  frViewContactos: TfrViewContactos;

implementation
{$R *.dfm}
{ TfrViewContactos }

uses uDataModuleContactos, uDataTableUtils, uDAInterfaces,
   uDBSelectionList, uUtils, schContactosClient_Intf;


procedure TfrViewContactos.actVistaCategoriasExecute(Sender: TObject);
begin
  cxGridViewCATEGORIAS.GroupIndex := 0;
  rellenarContactosMem;
end;

procedure TfrViewContactos.actVistaTodosExecute(Sender: TObject);
begin
  deshabilitarVistaCategorias;
  DADataSource.DataTable := FContactos.DataTable;
  cxGridViewCATEGORIAS.GroupIndex := -1;
end;

procedure TfrViewContactos.darListaCategorias(const Cadena: String; var Lista: TStringList);
var
  CadeaAux: String;
begin
    Lista.Clear;
    CadeaAux := Cadena;

    repeat
      if Pos(';', CadeaAux) = 0 then
      begin
        Lista.Add(CadeaAux);
        CadeaAux := ''
      end
      else
      begin
        Lista.Add(Copy(CadeaAux, 0, Pos(';', CadeaAux)-1));
        CadeaAux := Copy(CadeaAux, Pos(';', CadeaAux)+1, Length(CadeaAux));
      end;
    until (Length(CadeaAux) = 0);
end;

procedure TfrViewContactos.deshabilitarVistaCategorias;
begin
  cxGridView.OnFocusedRecordChanged := Nil;
  DADataSource.DataTable := Nil;
  FContactosMem.Close;
end;

function TfrViewContactos.GetContactos: IBizContacto;
begin
  Result := FContactos;
end;

procedure TfrViewContactos.habilitarVistaCategorias;
begin
    DADataSource.DataTable := FContactosMem;
end;

procedure TfrViewContactos.rellenarContactosMem;
var
  i,j: Integer;
  Lista: TStringList;
  ListaCamposClave: TStringList;
  ListaValoresClave: TStringList;
  CadenaAux: String;
  AValue: Variant;
begin
  try
    ListaCamposClave := TStringList.Create;
    ListaValoresClave := TStringList.Create;
    Lista := TStringList.Create;

    deshabilitarVistaCategorias;
    FContactosMem.Open;
    with FContactos do
    begin
      getKeyListAndValueList(DataTable, ListaCamposClave, ListaValoresClave);
      DataTable.Refresh;
      DataTable.DisableControls;
      First;
      while not EOF do
      begin
        //Insertamos tantas tuplas iguales como categorias tenga cada contacto
        j:=0;
        darListaCategorias(LISTACATEGORIAS, Lista);
        repeat
          FContactosMem.Insert;
          for i:=0 to DataTable.Fields.Count-1 do
              FContactosMem.Fields[i].Value := DataTable.Fields[i].Value;

          if Lista.Count <> 0 then
             FContactosMem.Fields[i].Value := Lista.Strings[j];

          FContactosMem.Post;
          Inc(j);
        until (j > Lista.Count - 1);
        Next;
      end;
    end;
  finally
    habilitarVistaCategorias;
    CadenaAux := getStrList(ListaCamposClave,';');
    AValue := getArrList(ListaValoresClave);
    FContactos.DataTable.First;
    FContactos.DataTable.Locate(CadenaAux, AValue, []);
    FContactos.DataTable.EnableControls;
    FreeAndNil(Lista);
    FreeAndNil(ListaCamposClave);
    FreeAndNil(ListaValoresClave);
  end;
end;

procedure TfrViewContactos.SetContactos(const Value: IBizContacto);
begin
  FContactos := Value;
  if Assigned(FContactos) then
  begin
     DADataSource.DataTable := FContactos.DataTable;
  end
  else
     DADataSource.DataTable := Nil;
end;

function TfrViewContactos.GetViewType: TViewType;
begin
  Result:= FViewType;
end;

procedure TfrViewContactos.SetViewType(const Value: TViewType);
begin
  FViewType := Value;
  Case FViewType of
    vtAll: actVistaTodos.Execute;
    vtCat: actVistaCategorias.Execute;
  end;
end;

procedure TfrViewContactos.SyncFocusedRecordsGrid;
var
  AValue : Variant;

begin
  if ViewType = vtCat then
  begin
     rellenarContactosMem;
     AValue := FContactos.CODIGO;
     FContactosMem.DisableEventHandlers;
     FContactosMem.DisableControls;
     FContactosMem.Locate(fld_CONTACTOSCODIGO, AValue, []);
     FContactosMem.EnableEventHandlers;
     FContactosMem.EnableControls;
  end;

  inherited;
end;

procedure TfrViewContactos.cxGridViewCODIGOCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  R : TRect;
begin
  inherited;
  R := AViewInfo.ContentBounds;
  ACanvas.FillRect(R);
  ACanvas.DrawImage(PngImageList, R.Left, R.Top, 0);
  ADone := True;
end;

procedure TfrViewContactos.Preview;
begin
  cxGridViewCODIGO.Visible := False;
  inherited;
  cxGridViewCODIGO.Visible := True;
end;

procedure TfrViewContactos.Print;
begin
  cxGridViewCODIGO.Visible := False;
  inherited;
  cxGridViewCODIGO.Visible := True;
end;

procedure TfrViewContactos.RefreshGrid;
var
  AValue : Variant;

begin
  if ViewType = vtCat then
  begin
     rellenarContactosMem;
     AValue := FContactos.CODIGO;
     FContactosMem.DisableEventHandlers;
     FContactosMem.DisableControls;
     FContactosMem.Locate(fld_CONTACTOSCODIGO, AValue, []);
     FContactosMem.EnableEventHandlers;
     FContactosMem.EnableControls;
  end
  else
    inherited;
end;

procedure TfrViewContactos.SyncFocusedRecordsFromDataSet;
var
  AValue: Variant;
begin
  AValue := FContactos.CODIGO;
  cxGridView.DataController.GotoFirst;
  cxGridView.DataController.LocateByKey(AValue);
end;

procedure TfrViewContactos.SyncFocusedRecordsFromGrid;
var
  AValue : Variant;
  i: Integer;
  Clave : String;
begin
  if ViewType = vtCat then
  begin
    Clave := cxGridView.DataController.KeyFieldNames;
    if Length(Clave) = 0 then
       raise Exception.Create('Falta indicar los campos clave de la tabla en la vista del grid (SyncFocusedRecords)');

    FContactos.DataTable.DisableEventHandlers;
    FContactos.DataTable.DisableControls;
    try
      for i := 0 to cxGridView.Controller.SelectedRecordCount - 1 do
      begin
          AValue := cxGridView.Controller.SelectedRows[i].Values[cxGridView.GetColumnByFieldName(Clave).Index];
          FContactos.DataTable.First;
          FContactos.DataTable.Locate(Clave, AValue, []);
      end;
    finally
       FContactos.DataTable.EnableEventHandlers;
       FContactos.DataTable.EnableControls;
    end;
  end
  else
    inherited;
end;

end.
