unit uDataModuleCobrosCliente;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleCobrosCliente, uBizCobrosCliente;

type
  TDataModuleCobrosCliente = class(TDAClientDataModule, IDataModuleCobrosCliente)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_CobrosCliente: TDACDSDataTable;
    ds_CobrosCliente: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
  protected
    procedure AsignarClaseNegocio(var ACobro: TDACDSDataTable); virtual;    
  public
    function GetItems : IBizCobroCliente;
    function GetItem(const ID : Integer) : IBizCobroCliente;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizCobroCliente;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, uDAInterfaces,
  schCobrosClienteClient_Intf, uBizContactos;

{ TdmPresupuestos }

procedure TDataModuleCobrosCliente.AsignarClaseNegocio(var ACobro: TDACDSDataTable);
begin
  ACobro.BusinessRulesID := BIZ_CLIENT_COBRO_CLIENTE;
end;

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

function TDataModuleCobrosCliente.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_CobrosCliente then
    aGeneratorName := 'GEN_COBROS_CLIENTE_ID';

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

function TDataModuleCobrosCliente.NewItem: IBizCobroCliente;
begin
  Result := GetItem(ID_NULO)
end;

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

function TDataModuleCobrosCliente.GetItems: IBizCobroCliente;
var
  ACobro : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ACobro := _CloneDataTable(tbl_CobrosCliente);

    AsignarClaseNegocio(ACobro);

    Result := (ACobro as IBizCobroCliente);
  finally
    HideHourglassCursor;
  end;
end;


end.
