unit uDataModuleVentasTerminadas;

interface

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

const
  CATEGORIA_VENTA_TERMINADA = 'VENTATERMINADA';

type
  TdmVentasTerminadas = class(TDAClientDataModule)
    RORemoteService: TRORemoteService;
    tbl_VentasTerminadas: TDACDSDataTable;
    tbl_VentasTerminadas2: TDACDSDataTable;
    tbl_VentasTerminadasCrossTab: TDACDSDataTable;
    ds_VentasTerminadasCrossTab: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function GetItems : IBizVentasTerminadas;
  public
    function GetNameColumns: String;
    function GetControl: IBizControlVentasTerminadas;
    function GetItemsCrossTab : IBizResumenVentas;
  end;

var
  dmVentasTerminadas: TdmVentasTerminadas;

implementation

{$R *.DFM}

uses
  Controls, Forms, uDAInterfaces, DataAbstract_Intf, uDataTableUtils,
  uROTypes, uEditorPreview, Dialogs, uDataModuleControles,
  uDataModuleBase, uDBSelectionList, schVentasTerminadasClient_Intf, cxControls;


procedure TdmVentasTerminadas.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmBase.Channel;
  RORemoteService.Message := dmBase.Message;
end;

function TdmVentasTerminadas.GetControl: IBizControlVentasTerminadas;
var
  aDataTable : TDACDSDataTable;
begin
  aDataTable := dmControles.GetItems(CATEGORIA_VENTA_TERMINADA);
  aDataTable.BusinessRulesID := BIZ_CONTROL_VENTASTERMINADAS;

  (aDataTable as IBizControlVentasTerminadas).Items := GetItems;
  Result := (aDataTable as IBizControlVentasTerminadas);
end;

function TdmVentasTerminadas.GetItems: IBizVentasTerminadas;
var
  dtVentas: TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    dtVentas := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_VentasTerminadas2, dtVentas);
    dtVentas.Fields.AssignFieldCollection(tbl_VentasTerminadas.Fields);
    dtVentas.BusinessRulesID := BIZ_VENTASTERMINADAS;

    dtVentas.FieldByName(fld_VentasTerminadasNUMINF).Visible := False;
    dtVentas.FieldByName(fld_VentasTerminadasCLAVE1).Visible := False;
    dtVentas.FieldByName(fld_VentasTerminadasCLAVE2).Visible := False;
    dtVentas.FieldByName(fld_VentasTerminadasCLAVE3).Visible := False;

    dtVentas.LoadSchema;
    Result := (dtVentas as IBizVentasTerminadas);
  finally
    HideHourglassCursor;
  end;
end;


function TdmVentasTerminadas.GetItemsCrossTab: IBizResumenVentas;
var
  dtVentas: TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    dtVentas := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_VentasTerminadasCrossTab, dtVentas);
    dtVentas.BusinessRulesID := BIZ_RESUMENVENTAS;
    Result := (dtVentas as IBizResumenVentas);
  finally
    HideHourglassCursor;
  end;
end;

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

initialization
  dmVentasTerminadas := TdmVentasTerminadas.Create(nil);

finalization
  FreeAndNil(dmVentasTerminadas);

end.

