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_InfMargen: TDACDSDataTable;
    tbl_InfMargenPorArticulo1: TDACDSDataTable;
    ds_InfMargenPorArticulo1: TDADataSource;
    tbl_InfMargenPorCliente1: TDACDSDataTable;
    ds_InfMargenPorCliente1: TDADataSource;
    tbl_InfMargenPorFactura1: TDACDSDataTable;
    ds_InfMargenPorFactura1: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
  public
    function GetItems(Agrupado: TEnumAgrupaciones) : IBizInfMargenArticulo;
    function GetNameColumns(Agrupado: TEnumAgrupaciones): 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(Agrupado: TEnumAgrupaciones): IBizInfMargenArticulo;
var
  AInfMargenArticulo : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try

    Case Agrupado of
    tCliente: begin
                tbl_InfMargen.LogicalName := 'InfMargenPorCliente';
                tbl_InfMargen.SchemaCall.ParamByName('DatasetName').AsString := 'InfMargenPorCliente';
                tbl_InfMargen.DataRequestCall.ParamByName('DatasetName').AsString := 'InfMargenPorCliente';
              end;
    tArticulo:begin
                tbl_InfMargen.LogicalName := 'InfMargenPorArticulo';
                tbl_InfMargen.SchemaCall.ParamByName('DatasetName').AsString := 'InfMargenPorArticulo';
                tbl_InfMargen.DataRequestCall.ParamByName('DatasetName').AsString := 'InfMargenPorArticulo';
              end;
    tFactura: begin
                tbl_InfMargen.LogicalName := 'InfMargenPorFactura';
                tbl_InfMargen.SchemaCall.ParamByName('DatasetName').AsString := 'InfMargenPorFactura';
                tbl_InfMargen.DataRequestCall.ParamByName('DatasetName').AsString := 'InfMargenPorFactura';
              end;
    End;

    AInfMargenArticulo := _CloneDataTable(tbl_InfMargen);
    AInfMargenArticulo.Fields.AssignFieldCollection(tbl_InfMargenPorCliente1.Fields); //Siempre por cliente
    AInfMargenArticulo.BusinessRulesID := BIZ_InfMargenArticulo;

    //Importante inicializar columnas visibles
//    AInfMargenArticulo.FieldByName(fld_InfMargenArticuloID_FACTURA).Visible := False;
//    AInfMargenArticulo.FieldByName(fld_InfMargenPorClienteID_ARTICULO).Visible := False;

    AInfMargenArticulo.LoadSchema;
   (AInfMargenArticulo as IBizInfMargenArticulo).VisibleColumns := GetNameColumns(Agrupado);

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

function TdmInfMargenArticulo.GetNameColumns(Agrupado: TEnumAgrupaciones): String;
var
  AList : TStringList;
  i : integer;
begin
  AList := TStringList.Create;
  try
    case Agrupado of
    tCliente: begin
                for i := 0 to tbl_InfMargenPorCliente1.FieldCount - 1 do
                  if tbl_InfMargenPorCliente1.Fields[i].Visible then
                    AList.Add(tbl_InfMargenPorCliente1.Fields[i].Name);
              end;
    tArticulo:begin
                for i := 0 to tbl_InfMargenPorArticulo1.FieldCount - 1 do
                  if tbl_InfMargenPorArticulo1.Fields[i].Visible then
                    AList.Add(tbl_InfMargenPorArticulo1.Fields[i].Name);
              end;
    tFactura: begin
                for i := 0 to tbl_InfMargenPorFactura1.FieldCount - 1 do
                  if tbl_InfMargenPorFactura1.Fields[i].Visible then
                    AList.Add(tbl_InfMargenPorFactura1.Fields[i].Name);
              end;
    end;
    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.

