unit uDataModuleRemesasProveedor;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleRemesasProveedor, uIDataModuleRemesasProveedorReport, uBizRemesasProveedor;

type
  TDataModuleRemesasProveedor = class(TDAClientDataModule, IDataModuleRemesasProveedor, IDataModuleRemesasProveedorReport)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_RemesasProveedor: TDACDSDataTable;
    ds_RemesasProveedor: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

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

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

implementation

{$R *.DFM}

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

{ TdmRemesasProveedor }

procedure TDataModuleRemesasProveedor.AsignarClaseNegocio(var ARemesaProveedor: TDACDSDataTable);
begin
  ARemesaProveedor.BusinessRulesID := BIZ_CLIENT_REMESAPROVEEDOR;
end;

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

function TDataModuleRemesasProveedor.DarNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_RemesasProveedor then
    Result := 'GEN_REMESAS_Proveedor_ID';
end;

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

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

function TDataModuleRemesasProveedor.NewItem: IBizRemesaProveedor;
begin
  Result := GetItem(ID_NULO)
end;

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

function TDataModuleRemesasProveedor.GetItems: IBizRemesaProveedor;
var
  ARemesaProveedor : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ARemesaProveedor := _CloneDataTable(tbl_RemesasProveedor);
    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    ARemesaProveedor.FieldByName(fld_RemesasProveedorREFERENCIA).ServerAutoRefresh := TRUE;
    ARemesaProveedor.FieldByName(fld_RemesasProveedorIMPORTE_TOTAL).ServerAutoRefresh := TRUE;

    AsignarClaseNegocio(ARemesaProveedor);
    Result := (ARemesaProveedor as IBizRemesaProveedor);
  finally
    HideHourglassCursor;
  end;
end;


end.
