unit uDataModuleInventario;

interface

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

type
  TDataModuleInventario = class(TDAClientDataModule, IDataModuleInventario)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_INVENTARIO: TDACDSDataTable;
    ds_INVENTARIO: TDADataSource;
    tbl_DetalleReservas: TDACDSDataTable;
    dsDetalleReservas: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function darNombreGenerador(DataSetName: String): String; virtual;
  public
    function GetItems : IBizInventario; overload;
    function GetItems(const ID_ALMACEN : Integer) : IBizInventario; overload;
    function GetNextID(const DataSetName : String) : Integer;
    function GetDetalleReservas: IBizDetalleReservas;
  end;

implementation

{$R *.DFM}

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

{ TdmArticulos }

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

function TDataModuleInventario.darNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_Inventario then
     Result := 'GEN_MOVIMIENTOS_ID';
end;

function TDataModuleInventario.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  //Se usará para dar nuevos ids a la tabla de movimientos (Regularizaciones)
  aGeneratorName := darNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvInventario).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleInventario._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 TDataModuleInventario.GetDetalleReservas: IBizDetalleReservas;
var
  ADetalleReservas : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalleReservas := _CloneDataTable(tbl_DetalleReservas);
    ADetalleReservas.BusinessRulesID := BIZ_CLIENT_DETALLE_RESERVAS_INV;
    Result := (ADetalleReservas as IBizDetalleReservas);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleInventario.GetItems(const ID_ALMACEN: Integer): IBizInventario;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);

      OpenBraket;
      AddText(fld_INVENTARIOID_ALMACEN + ' = ' + IntToStr(ID_ALMACEN));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleInventario.GetItems: IBizInventario;
var
  AInventario : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AInventario := _CloneDataTable(tbl_Inventario);
    AInventario.BusinessRulesID := BIZ_CLIENT_INVENTARIO;
    Result := (AInventario as IBizInventario);
  finally
    HideHourglassCursor;
  end;
end;


end.
