unit uDataModuleRemesasCliente;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall, uDataModuleBase,
  uIDataModuleRemesasCliente, uIDataModuleRemesasClienteReport, uBizRemesasCliente,
  uDARemoteDataAdapter, uDADataStreamer, uDABin2DataStreamer, uDAInterfaces,
  uDAMemDataTable, uIntegerListUtils;

type
  TDataModuleRemesasCliente = class(TDataModuleBase, IDataModuleRemesasCliente, IDataModuleRemesasClienteReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_RemesasCliente: TDARemoteDataAdapter;
    tbl_RemesasCliente: TDAMemDataTable;
    ds_RemesasCliente: TDADataSource;
    tbl_ListaAnosRemesas: TDAMemDataTable;
    ds_ListaAnosRemesas: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    procedure AsignarClaseNegocio(var ARemesaCliente: TDAMemDataTable); virtual;

  public
    function GetItems : IBizRemesaCliente; overload;
    function GetItem(const ID : Integer) : IBizRemesaCliente;
    function NewItem : IBizRemesaCliente;

    // Report
    function GetReport(const ListaID: TIntegerList): Binary;

    function GetAnosItems : TStringList;    
  end;

implementation

{$R *.DFM}

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

{ TdmRemesasCliente }

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

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

function TDataModuleRemesasCliente.GetReport(const ListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := ListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvRemesasCliente).GenerateReport(AParam)
  finally
    FreeANDNIL(AParam);
  end;
end;

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

function TDataModuleRemesasCliente.GetAnosItems: TStringList;
var
  AListaAnos: TStringList;
begin
  AListaAnos := TStringList.Create;
  ShowHourglassCursor;
  try
    with tbl_ListaAnosRemesas do
    begin
      Open;
      First;
      while not eof do
      begin
        AListaAnos.Add(Format('%s=%s', [Fields[0].AsString, Fields[0].AsString]));
        Next;
      end;
      Close;
    end;
    Result := AListaAnos;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleRemesasCliente.GetItem(const ID: Integer): IBizRemesaCliente;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

    with Result.DataTable.DynamicWhere do
    begin
      // (ID = :ID)
      Condicion := NewBinaryExpression(NewField('', fld_RemesasClienteID), NewConstant(ID, datInteger), dboEqual);

      if IsEmpty then
         Expression := Condicion
      else
         Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleRemesasCliente.GetItems: IBizRemesaCliente;
var
  ARemesaCliente : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ARemesaCliente := CloneDataTable(tbl_RemesasCliente);
    AsignarClaseNegocio(ARemesaCliente);

    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    ARemesaCliente.FieldByName(fld_RemesasClienteREFERENCIA).ServerAutoRefresh := TRUE;
    ARemesaCliente.FieldByName(fld_RemesasClienteIMPORTE_TOTAL).ServerAutoRefresh := TRUE;

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


end.
