unit uDataModuleAlmacenes;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleAlmacenes, uBizAlmacenes, uDataModuleBase, uDARemoteDataAdapter,
  uDADataStreamer, uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable,
  uRODynamicRequest;


type
  TDataModuleAlmacenes = class(TDataModuleBase, IDataModuleAlmacenes)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_Almacenes: TDARemoteDataAdapter;
    tbl_Almacenes: TDAMemDataTable;
    ds_Almacenes: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    procedure AsignarClaseNegocio(AAlmacen: TDADataTable); virtual;
  public
    function GetItems : IBizAlmacen;
    function GetItem(const ID : Integer) : IBizAlmacen;
    function NewItem : IBizAlmacen;
    function DarListaAlmacenes(const IDEmpresa: Integer): TStringList;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls,
  schAlmacenesClient_Intf;

{ TdmAlmacens }

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

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

function TDataModuleAlmacenes.DarListaAlmacenes(const IDEmpresa: Integer): TStringList;
var
  ABinary : Binary;
begin
  ABinary := (RORemoteService as IsrvAlmacenes).DarListaAlmacenes(IDEmpresa);
  Result := TStringList.Create;
  Result.LoadFromStream(ABinary);
end;

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

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

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

    AsignarClaseNegocio(AAlmacen);

    Result := (AAlmacen as IBizAlmacen);

  finally
    HideHourglassCursor;
  end;
end;


end.
