unit uDataModuleRemesasCliente;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleRemesasCliente, uIDataModuleRemesasClienteReport, uBizRemesasCliente;

type
  TDataModuleRemesasCliente = class(TDAClientDataModule, IDataModuleRemesasCliente, IDataModuleRemesasClienteReport)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_RemesasCliente: TDACDSDataTable;
    ds_RemesasCliente: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    procedure AsignarClaseNegocio(var ARemesaCliente: TDACDSDataTable); virtual;
    function darNombreGenerador(DataSetName: String): String; virtual;
  public
    function GetItems : IBizRemesaCliente; overload;
    function GetItem(const ID : Integer) : IBizRemesaCliente;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizRemesaCliente;

    // Report
    function GetReport(const ID: String): Binary;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, uDAInterfaces,
  schRemesasClienteClient_Intf, Dialogs;

{ TdmRemesasCliente }

procedure TDataModuleRemesasCliente.AsignarClaseNegocio(var ARemesaCliente: TDACDSDataTable);
begin
  ARemesaCliente.BusinessRulesID := BIZ_CLIENT_REMESACLIENTE;
end;

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

function TDataModuleRemesasCliente.DarNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_RemesasCliente then
    Result := 'GEN_REMESAS_CLIENTE_ID';
end;

function TDataModuleRemesasCliente.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  aGeneratorName := DarNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvRemesasCliente).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleRemesasCliente.GetReport(const ID: String): Binary;
begin
  Result := (RORemoteService as IsrvRemesasCliente).GenerateReport(ID);
end;

function TDataModuleRemesasCliente.NewItem: IBizRemesaCliente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleRemesasCliente._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 TDataModuleRemesasCliente.GetItem(const ID: Integer): IBizRemesaCliente;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_RemesasClienteID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleRemesasCliente.GetItems: IBizRemesaCliente;
var
  ARemesaCliente : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ARemesaCliente := _CloneDataTable(tbl_RemesasCliente);
    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    ARemesaCliente.FieldByName(fld_RemesasClienteREFERENCIA).ServerAutoRefresh := TRUE;
    ARemesaCliente.FieldByName(fld_RemesasClienteIMPORTE_TOTAL).ServerAutoRefresh := TRUE;

    AsignarClaseNegocio(ARemesaCliente);
    Result := (ARemesaCliente as IBizRemesaCliente);
  finally
    HideHourglassCursor;
  end;
end;


end.
