unit uDataModuleAlmacenes;

interface

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

type
  TDataModuleAlmacenes = class(TDAClientDataModule, IDataModuleAlmacenes)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_Almacenes: TDACDSDataTable;
    ds_Almacenes: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

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

implementation

{$R *.DFM}

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

{ TdmAlmacens }

procedure TDataModuleAlmacenes.AsignarClaseNegocio(var AAlmacen: TDACDSDataTable);
begin
  AAlmacen.BusinessRulesID := BIZ_CLIENT_ALMACEN;
end;

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

function TDataModuleAlmacenes.darNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_Almacenes then
    Result := 'GEN_ALMACENES_ID';
end;

function TDataModuleAlmacenes.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  aGeneratorName := darNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvAlmacenes).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleAlmacenes.NewItem: IBizAlmacen;
begin
  Result := GetItem(ID_NULO)
end;

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

function TDataModuleAlmacenes.GetItems: IBizAlmacen;
var
  AAlmacen : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AAlmacen := _CloneDataTable(tbl_Almacenes);

    AsignarClaseNegocio(AAlmacen);

    Result := (AAlmacen as IBizAlmacen);

  finally
    HideHourglassCursor;
  end;
end;


end.
