unit uDataModuleEmpresas;

interface

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

type
  TDataModuleEmpresas = class(TDAClientDataModule, IDataModuleEmpresas)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    tbl_Empresas: TDACDSDataTable;
    ds_Empresas: TDADataSource;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROChannel: TROWinInetHTTPChannel;
    ROMessage: TROBinMessage;
    tbl_EmpresasDatosBanco: TDACDSDataTable;
    ds_EmpresasDatosBanco: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable;
    function _GetDatosBancarios : IBizEmpresasDatosBancarios;
  public
    function GetItem(const ID : Integer) : IBizEmpresa;
    function NewItem : IBizEmpresa;
    function GetItems : IBizEmpresa;
    function GetNextID(const DataSetName : String) : Integer;
  end;


implementation

{$R *.DFM}

uses
  uDataModuleConexion, uDataTableUtils, uDAInterfaces,
  FactuGES_Intf, schEmpresasClient_Intf, cxControls;

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

function TDataModuleEmpresas.GetItem(const ID: Integer): IBizEmpresa;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText('EMPRESAS.' + fld_EmpresasID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.GetItems: IBizEmpresa;
var
  AEmpresa : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AEmpresa := _CloneDataTable(tbl_Empresas);
    AEmpresa.BusinessRulesID := BIZ_CLIENT_EMPRESA;

    with TBizEmpresa(AEmpresa.BusinessEventsObj) do
    begin
      DatosBancarios := _GetDatosBancarios;
    end;

    Result := (AEmpresa as IBizEmpresa);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_Empresas then
    aGeneratorName := 'GEN_EMPRESAS_ID';

  if DataSetName = nme_EmpresasDatosBanco then
    aGeneratorName := 'GEN_EMPRESAS_DATOS_BANCO_ID';

  Result := (RORemoteService as IsrvEmpresas).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleEmpresas.NewItem: IBizEmpresa;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleEmpresas._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 TDataModuleEmpresas._GetDatosBancarios: IBizEmpresasDatosBancarios;
var
  ADatosBancarios : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ADatosBancarios := _CloneDataTable(tbl_EmpresasDatosBanco);
    with ADatosBancarios do
    begin
      BusinessRulesID := BIZ_CLIENT_EMPRESAS_DATOS_BANCARIOS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADatosBancarios as IBizEmpresasDatosBancarios);
  finally
    HideHourglassCursor;
  end;
end;

initialization

end.