unit uDataModuleInfVentasArticulo;

interface

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

//const
//  CATEGORIA_VENTA_TERMINADA = 'VENTATERMINADA';

type
  TdmInfVentasArticulo = class(TDAClientDataModule, IDataModuleInfVentasArticulo)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_InfVentasArticulo2: TDACDSDataTable;
    tbl_InfVentasArticulo: TDACDSDataTable;
    procedure DAClientDataModuleCreate(Sender: TObject);

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

var
  dmInfVentasArticulo: TdmInfVentasArticulo;

implementation

{$R *.DFM}

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


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

function TdmInfVentasArticulo.GetItems: IBizInfVentasArticulo;
var
  AInfVentasArticulo : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AInfVentasArticulo := _CloneDataTable(tbl_InfVentasArticulo2);
    AInfVentasArticulo.Fields.AssignFieldCollection(tbl_InfVentasArticulo.Fields);

    AInfVentasArticulo.BusinessRulesID := BIZ_InfVentasArticulo;

    //Importante inicializar columnas visibles
    AInfVentasArticulo.FieldByName(fld_InfVentasArticuloID_FACTURA).Visible := False;
    AInfVentasArticulo.FieldByName(fld_InfVentasArticuloID_ARTICULO).Visible := False;
    AInfVentasArticulo.LoadSchema;
   (AInfVentasArticulo as IBizInfVentasArticulo).VisibleColumns := GetNameColumns;

    Result := (AInfVentasArticulo as IBizInfVentasArticulo);
  finally
    HideHourglassCursor;
  end;
end;

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

function TdmInfVentasArticulo._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.

