unit uDataModuleArticulos;

interface

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

type
  TDataModuleArticulos = class(TDataModuleBase, IDataModuleArticulos)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_Articulos: TDARemoteDataAdapter;
    tbl_Articulos: TDAMemDataTable;
    ds_Articulos: TDADataSource;
    tbl_ProveedoresConArticulos: TDAMemDataTable;
    ds_ProveedoresConArticulos: TDADataSource;
    tbl_Articulos_Proveedores: TDAMemDataTable;
    ds_Articulos_Proveedores: TDADataSource;
    tbl_ArticulosParaCompra: TDAMemDataTable;
    ds_ArticulosParaCompra: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetProveedores : IBizArticulosProveedores;

  protected
    procedure AsignarClaseNegocio(AArticulo: TDADataTable); virtual;

  public
    function GetItems (const Tipo: TEnumArticulos) : IBizArticulo;
    function GetItem(const ID : Integer) : IBizArticulo;
    function NewItem : IBizArticulo;
    function DarListaProveedoresConArticulos(const IDEmpresa: Integer) : TStringList;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls,
  schArticulosClient_Intf;

{ TdmArticulos }

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.DarListaProveedoresConArticulos(const IDEmpresa: Integer): TStringList;
begin
  Result := TStringList.Create;
  Result.LoadFromStream((RORemoteService as IsrvArticulos).DarListaProveedoresConArticulos(IDEmpresa));
end;

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

function TDataModuleArticulos._GetProveedores: IBizArticulosProveedores;
var
  AProveedores : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AProveedores := CloneDataTable(tbl_Articulos_Proveedores);
    with AProveedores do
    begin
      BusinessRulesID := BIZ_CLIENT_ARTICULOS_PROVEEDORES;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AProveedores as IBizArticulosProveedores);
  finally
    HideHourglassCursor;
  end;

end;

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

    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(const Tipo: TEnumArticulos): IBizArticulo;
var
  AArticulo : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    case Tipo of
      txArticulo: AArticulo := CloneDataTable(tbl_Articulos);
      txProveedor:  AArticulo := CloneDataTable(tbl_ArticulosParaCompra);
    end;

    AsignarClaseNegocio(AArticulo);

    case Tipo of
      txArticulo: TBizArticulo(AArticulo.BusinessEventsObj).Proveedores := _GetProveedores;
    end;

    Result := (AArticulo as IBizArticulo);

  finally
    HideHourglassCursor;
  end;
end;

end.
