unit uDataModuleInfMargenArticulo;

interface

uses {vcl:} SysUtils, Classes, DB, DBClient,
     {RemObjects:} uDAClientDataModule, uDADataTable, uDABINAdapter,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uROWinInetHttpChannel, uDAScriptingProvider, uDACDSDataTable,
  uBizInfMargenArticulo, uIDataModuleInfMargenArticulo, uDADesigntimeCall;
  //uBizControlVentasTerminadas,

//const
//  CATEGORIA_VENTA_TERMINADA = 'VENTATERMINADA';

type
  TdmInfMargenArticulo = class(TDAClientDataModule, IDataModuleInfMargenArticulo)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_InfMargenArticulo2: TDACDSDataTable;
    tbl_InfMargenArticulo: TDACDSDataTable;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
  public
    function GetItems : IBizInfMargenArticulo;
    function GetNameColumns: String;
  end;

var
  dmInfMargenArticulo: TdmInfMargenArticulo;

implementation

{$R *.DFM}

uses
  Controls, Forms, uDAInterfaces, DataAbstract_Intf, uDataTableUtils,
  uROTypes,  Dialogs, uDataModuleConexion,
  uDataModuleBase, cxControls,
  schInfMargenArticuloClient_Intf; //, uEditorPreview, uDataModuleControles,  uDBSelectionList,


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

function TdmInfMargenArticulo.GetItems: IBizInfMargenArticulo;
var
  AInfMargenArticulo : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AInfMargenArticulo := _CloneDataTable(tbl_InfMargenArticulo2);
    AInfMargenArticulo.Fields.AssignFieldCollection(tbl_InfMargenArticulo.Fields);

    AInfMargenArticulo.BusinessRulesID := BIZ_InfMargenArticulo;

    //Importante inicializar columnas visibles
//    AInfMargenArticulo.FieldByName(fld_InfMargenArticuloID_FACTURA).Visible := False;
    AInfMargenArticulo.FieldByName(fld_InfMargenArticuloID_ARTICULO).Visible := False;
    AInfMargenArticulo.LoadSchema;
   (AInfMargenArticulo as IBizInfMargenArticulo).VisibleColumns := GetNameColumns;

    Result := (AInfMargenArticulo as IBizInfMargenArticulo);
  finally
    HideHourglassCursor;
  end;
end;

function TdmInfMargenArticulo.GetNameColumns: String;
var
  AList : TStringList;
  i : integer;
begin
  AList := TStringList.Create;
  try
    for i := 0 to tbl_InfMargenArticulo.FieldCount - 1 do
      if tbl_InfMargenArticulo.Fields[i].Visible then
        AList.Add(tbl_InfMargenArticulo.Fields[i].Name);
    Result := AList.CommaText;
  finally
    FreeAndNil(AList);
  end;
end;

function TdmInfMargenArticulo._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;

end.

