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

interface

uses {vcl:} SysUtils, Classes, DB, DBClient,
     {RemObjects:} uDAClientDataModule, uDADataTable, uDABINAdapter,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uROWinInetHttpChannel, uDAScriptingProvider, uDACDSDataTable,
  uBizContacto;

type
  TdmContactos = class(TDAClientDataModule)
    DABINAdapter: TDABINAdapter;
    ds_Categorias: TDADataSource;
    ds_Contactos: TDADataSource;
    RORemoteService: TRORemoteService;
    tbl_Categorias: TDACDSDataTable;
    tbl_Contactos: TDACDSDataTable;
    tbl_ContactosCategorias: TDACDSDataTable;
    ds_ContactosCategorias: TDADataSource;
    tbl_ContactosAsociados: TDACDSDataTable;
    ds_ContactosAsociados: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function GetContactosTable: TDACDSDataTable;
    function GetCategoriasTable: TDACDSDataTable;
  public
    function getCodigo: Integer;
    function GetContacto(Codigo : Integer): IBizContacto; overload;
    procedure GetContacto(AContacto : IBizContacto; const Codigo : Integer); overload;
    function GetContactos: IBizContacto;
    function GetCategorias: IBizCategoria;
    procedure Preview;
    function GetItemsSeleccionados(ASource : IBizContacto): IBizContacto;
  end;

var
  dmContactos: TdmContactos;

implementation

{$R *.DFM}

uses
  Controls, Forms, uDAInterfaces, DataAbstract_Intf, FactuGES_Intf,
  uDataTableUtils, uROTypes, uEditorPreview,
  Dialogs, schContactosClient_Intf, uDataModuleBase, uDBSelectionList,
  cxControls;


{
********************************* TdmContactos *********************************
}

procedure TdmContactos.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmBase.Channel;
  RORemoteService.Message := dmBase.Message;
end;

function TdmContactos.GetContactosTable: TDACDSDataTable;
var
  dtContactos: TDACDSDataTable;
  dtCategorias: TDACDSDataTable;
  dtAsociados: TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    dtContactos := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_Contactos, dtContactos);
    dtContactos.BusinessRulesID := BIZ_CONTACTO;
    dtContactos.FieldByName(fld_ContactosNOMBRE).BusinessRulesID := BIZ_NOMBRECONTACTO;

    dtCategorias := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_ContactosCategorias, dtCategorias);
    dtCategorias.BusinessRulesID := BIZ_CATEGORIACONTACTO;
    (dtContactos as IBizContacto).Categorias := (dtCategorias as IBizCategoriaContacto);

    dtAsociados := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_ContactosAsociados, dtAsociados);
    dtAsociados.BusinessRulesID := BIZ_CONTACTOASOCIADO;
    (dtContactos as IBizContacto).Asociados := (dtAsociados as IBizContactoAsociado);

    Result := dtContactos;
  finally
    HideHourglassCursor;
  end;
end;

procedure TdmContactos.Preview;
var
  AStream: TMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AStream := Binary.Create;
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    AStream := (RemoteService as ISrvContactos).GenerateReport;
    AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    AEditorPreview.ShowModal;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

function TdmContactos.GetContactos: IBizContacto;
var
  AContacto: IBizContacto;
begin
  AContacto := (GetContactosTable as IBizContacto);
  Result := AContacto;
end;

function TdmContactos.GetCategoriasTable: TDACDSDataTable;
var
  dtCategorias: TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    dtCategorias := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_Categorias, dtCategorias);
    dtCategorias.BusinessRulesID := BIZ_CATEGORIA;
    Result := dtCategorias;
  finally
    HideHourglassCursor;
  end;
end;

function TdmContactos.GetCategorias: IBizCategoria;
var
  ACategoria: IBizCategoria;
begin
  ACategoria := (GetCategoriasTable as IBizCategoria);
  Result := ACategoria;
end;

function TdmContactos.GetContacto(Codigo: Integer): IBizContacto;
var
  AContacto: IBizContacto;
begin
  AContacto := GetContactos;
  ShowHourglassCursor;
  try
    with AContacto.DataTable do
    begin
      if Active then
        Active := False;
      Where.Clear;
      Where.AddCondition(fld_ContactosCODIGO, cEqual, Codigo);
      Active := True;
    end;
    Result := AContacto;
  finally
    HideHourglassCursor;
  end;
end;

function TdmContactos.GetItemsSeleccionados(ASource: IBizContacto): IBizContacto;
var
  aObj : ISelectedRowList;
  dtContacto : IBizContacto;
  i : integer;
  Cadena: Variant;
begin
  if Supports(ASource, ISelectedRowList, aObj) then
  begin
    if (aObj.SelectedRows.Count = 1) then
    begin
      ASource.DataTable.GotoBookmark(aObj.SelectedRows[0]);
      Result := GetContacto(ASource.CODIGO);
      Exit;
    end
    else
      dtContacto := GetContactos;
  end
  else
    raise Exception.Create('Interface no soportada (GetItemsSeleccionados)');

  ShowHourglassCursor;
  try
    //Si esta abierta cerramos para poner el where
    if dtContacto.DataTable.Active then
       dtContacto.DataTable.Active := False;
    with dtContacto.DataTable.Where do
    begin
      Cadena := '';
      for i := 0 to aObj.SelectedRows.Count - 1 do
      begin
        ASource.DataTable.GotoBookmark(aObj.SelectedRows[i]);
        if Cadena <> '' then Cadena:= Cadena + ', ';
        Cadena:= Cadena + IntToStr(ASource.CODIGO);
      end;
      Clear;
      Cadena := '(' + Cadena + ')';
      AddCondition(fld_ContactosCODIGO, cIn, Cadena);
    end;

    Result := dtContacto;
  finally
    HideHourglassCursor;
  end;
end;

procedure TdmContactos.GetContacto(AContacto: IBizContacto; const Codigo: Integer);
begin
  with AContacto.DataTable do
  begin
    if Active then
      Active := False;
    Where.Clear;
    Where.AddCondition(fld_ContactosCODIGO, cEqual, Codigo);
  end;
end;

function TdmContactos.getCodigo: Integer;
begin
  Result := (RORemoteService as IsrvContactos).GetCodigo('GEN_CONTACTOS');
end;

initialization
  dmContactos := TdmContactos.Create(nil);

finalization
  FreeAndNil(dmContactos);

end.
