unit uDataModuleContactos;

interface

uses
  SysUtils, Classes, DB, DBClient,
  uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uBizContactos, uBizDatosBancarios, uBizCategoriasContacto,
  uIDataModuleContactos, uDADesigntimeCall;

type
  TDataModuleContactos = class(TDAClientDataModule, IDataModuleContactos)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    tbl_Contactos: TDACDSDataTable;
    ds_Contactos: TDADataSource;
    tbl_ContactosCategorias: TDACDSDataTable;
    ds_ContactosCategorias: TDADataSource;
    tbl_DatosBancarios: TDACDSDataTable;
    ds_DatosBancarios: TDADataSource;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function _InternalGetNextID(const AGeneratorName : String) : Integer; 
  public
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function _GetDatosBancarios : IBizDatosBancarios;
    function _GetCategorias : IBizCategoriasContacto;
    function _GetItems (IDCategoria : Integer): IBizContacto; virtual;
    function GetNextID(const DataSetName : String) : Integer; virtual;
  end;

implementation

{$R *.DFM}

uses
  uDataTableUtils, uDataModuleConexion, cxControls,
  FactuGES_Intf, uDataModuleUsuarios, schContactosClient_Intf, Dialogs,
  uDAInterfaces;

{ TdmContactos }

procedure TDataModuleContactos.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmConexion.Channel;
  RORemoteService.Message := dmConexion.Message;
end;

function TDataModuleContactos.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_Contactos then
    aGeneratorName := 'GEN_CONTACTOS_ID';

  if DataSetName = nme_DatosBancarios then
    aGeneratorName := 'GEN_CONTACTOS_DATOS_BANCO_ID';

  Result := _InternalGetNextID(aGeneratorName);
end;

function TDataModuleContactos._CloneDataTable(
  const ADataTable : TDACDSDataTable): TDACDSDataTable;
begin
  Result := NIL;

  if not Assigned(ADataTable) then
    raise Exception.Create ('No se ha asignado la tabla de origen (CloneDataTable)');

  Result := TDACDSDataTable.Create(NIL);
  CloneDataTable(ADataTable, Result);
end;

function TDataModuleContactos._GetCategorias: IBizCategoriasContacto;
var
  ACategorias : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ACategorias := _CloneDataTable(tbl_ContactosCategorias);
    with ACategorias do
    begin
      BusinessRulesID := BIZ_CLIENT_CATEGORIAS_CONTACTO;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ACategorias as IBizCategoriasContacto);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._GetDatosBancarios: IBizDatosBancarios;
var
  ADatosBancarios : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ADatosBancarios := _CloneDataTable(tbl_DatosBancarios);
    with ADatosBancarios do
    begin
      BusinessRulesID := BIZ_CLIENT_DATOS_BANCARIOS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADatosBancarios as IBizDatosBancarios);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._GetItems(IDCategoria: Integer): IBizContacto;
var
  AContacto : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AContacto := _CloneDataTable(tbl_Contactos);

    with AContacto do
      case IDCategoria of
        CATEGORIA_CLIENTE     : BusinessRulesID := BIZ_CLIENT_CLIENTE;
        CATEGORIA_PROVEEDOR   : BusinessRulesID := BIZ_CLIENT_PROVEEDOR;
        CATEGORIA_VENDEDOR    : BusinessRulesID := BIZ_CLIENT_VENDEDOR;
        CATEGORIA_INSTALADOR  : BusinessRulesID := BIZ_CLIENT_INSTALADOR;
      end;

    with TBizContacto(AContacto.BusinessEventsObj) do
    begin
      DatosBancarios := _GetDatosBancarios;
      _Categorias := _GetCategorias;
    end;

    Result := (AContacto as IBizContacto);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._InternalGetNextID(
  const AGeneratorName: String): Integer;
begin
  Result := (RORemoteService as IsrvContactos).GetNextAutoInc(AGeneratorName)
end;

end.
