unit uDataModuleArticulos;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleArticulos, uBizArticulos, uDataModuleBase, uDARemoteDataAdapter,
  uDADataStreamer, uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable;

type
  TDataModuleArticulos = class(TDataModuleBase, IDataModuleArticulos)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_Articulos: TDARemoteDataAdapter;
    tbl_Articulos: TDAMemDataTable;
    ds_Articulos: TDADataSource;
    tbl_ArticulosParaCliente: TDAMemDataTable;
    ds_ArticulosParaCliente: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    procedure AsignarClaseNegocio(AArticulo: TDADataTable); virtual;

  public
    function GetItems : IBizArticulo; overload;
    function GetItems (IDCliente: Integer) : IBizArticulo; overload;
    function GetItem(const ID : Integer; AClienteID: Integer = -1) : IBizArticulo;
    function NewItem : IBizArticulo;
    function EliminarImagen(const ID: Integer): Boolean;
    function AnadirImagen(const ID: Integer; const AFileName: String): Boolean;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls,
  schArticulosClient_Intf;

{ TdmArticulos }

function TDataModuleArticulos.AnadirImagen(const ID: Integer; const AFileName: String): Boolean;
var
  AImagen: Binary;
begin
  AImagen := Binary.Create;
  AImagen.LoadFromFile(AFileName);
  Result := (RORemoteService as IsrvArticulos).AnadirImagen(ID, AImagen);
end;

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

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

function TDataModuleArticulos.EliminarImagen(const ID: Integer): Boolean;
begin
  Result := (RORemoteService as IsrvArticulos).EliminarImagen(ID);
end;

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

function TDataModuleArticulos.GetItem(const ID: Integer; AClienteID: Integer = -1): IBizArticulo;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    if (AClienteID = -1) then
      Result := Self.GetItems
    else
      Result := Self.GetItems(AClienteID);

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleArticulos.GetItems(IDCliente: Integer): IBizArticulo;
var
  AArticulo : TDAMemDataTable;
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 : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AArticulo := CloneDataTable(tbl_Articulos);

    AsignarClaseNegocio(AArticulo);

    Result := (AArticulo as IBizArticulo);

  finally
    HideHourglassCursor;
  end;
end;


end.
