unit uDataModuleArticulos;

interface

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

type
  TDataModuleArticulos = class(TDAClientDataModule, IDataModuleArticulos)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_Articulos: TDACDSDataTable;
    ds_Articulos: TDADataSource;
    tbl_ArticulosParaCliente: TDACDSDataTable;
    ds_ArticulosParaCliente: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    procedure AsignarClaseNegocio(var AArticulo: TDACDSDataTable); virtual;
    function darNombreGenerador(DataSetName: String): String; virtual;
  public
    function GetItems : IBizArticulo; overload;
    function GetItems (IDCliente: Integer) : IBizArticulo; overload;
    function GetItem(const ID : Integer; AClienteID: Integer = -1) : IBizArticulo;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizArticulo;
  end;

implementation

{$R *.DFM}

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

{ TdmArticulos }

procedure TDataModuleArticulos.AsignarClaseNegocio(var AArticulo: TDACDSDataTable);
begin
  AArticulo.BusinessRulesID := BIZ_CLIENT_ARTICULO;
end;

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

function TDataModuleArticulos.darNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_Articulos then
    Result := 'GEN_ARTICULOS_ID';
end;

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

function TDataModuleArticulos.NewItem: IBizArticulo;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleArticulos._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 TDataModuleArticulos.GetItem(const ID: Integer; AClienteID: Integer = -1): IBizArticulo;
begin
  ShowHourglassCursor;
  try
    if (AClienteID = -1) then
      Result := Self.GetItems
    else
      Result := Self.GetItems(AClienteID);

    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText('V_ARTICULOS.' + fld_ArticulosID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleArticulos.GetItems(IDCliente: Integer): IBizArticulo;
var
  AArticulo : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AArticulo := _CloneDataTable(tbl_ArticulosParaCliente);

    AsignarClaseNegocio(AArticulo);

    AArticulo.ParamByName('ID_CLIENTE').AsInteger := IdCliente;
    Result := (AArticulo as IBizArticulo);

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleArticulos.GetItems: IBizArticulo;
var
  AArticulo : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AArticulo := _CloneDataTable(tbl_Articulos);

    AsignarClaseNegocio(AArticulo);

    Result := (AArticulo as IBizArticulo);

  finally
    HideHourglassCursor;
  end;
end;


end.
