unit uDataModuleComisiones;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,

  uIDataModuleComisiones, uBizComisiones, uIDataModuleComisionesReport,
  uDADesigntimeCall,  uDataModuleBase,
  uDAInterfaces, uDAMemDataTable, uDADataStreamer, uDABin2DataStreamer,
  uDARemoteDataAdapter, uIntegerListUtils;

type
  TDataModuleComisiones = class(TDataModuleBase, IDataModuleComisiones, IDataModuleComisionesReport)
    RORemoteService: TRORemoteService;
    rda_Comisiones: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_ListaAnosComisiones: TDAMemDataTable;
    ds_ListaAnosComisiones: TDADataSource;
    tbl_Comisiones: TDAMemDataTable;
    ds_Comisiones: TDADataSource;
    tbl_FacturasComision: TDAMemDataTable;
    ds_FacturasComision: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetFacturasComision : IBizFacturasComision;
  public
    function GetAnosItems : TStringList;
    function GetItems : IBizComision;
    function GetItem(const ID : Integer) : IBizComision;
    function NewItem : IBizComision;

    // Report
    function GetRptComisiones(const AListaID: TIntegerList): Binary;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls,
  schComisionesClient_Intf, uBizContactos, Dialogs;

{ TdmComisiones }

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


function TDataModuleComisiones.GetRptComisiones(const AListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := AListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvComisiones).GenerarInforme(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

function TDataModuleComisiones.NewItem: IBizComision;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleComisiones._GetFacturasComision: IBizFacturasComision;
var
  AFacturasComision : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AFacturasComision := CloneDataTable(tbl_FacturasComision);
    with AFacturasComision do
    begin
      BusinessRulesID := BIZ_CLIENT_FACTURASCOMISION;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AFacturasComision as IBizFacturasComision);
  finally
    HideHourglassCursor;
  end;
end;

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

    with Result.DataTable.DynamicWhere do
    begin
      // (ID = :ID)
      Condicion := NewBinaryExpression(NewField('', fld_ComisionesID), NewConstant(ID, datInteger), dboEqual);

      if IsEmpty then
         Expression := Condicion
      else
         Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetAnosItems: TStringList;
var
  AListaAnos: TStringList;
begin
  AListaAnos := TStringList.Create;
  ShowHourglassCursor;
  try
    with tbl_ListaAnosComisiones do
    begin
      Open;
      First;
      while not eof do
      begin
        AListaAnos.Add(Format('%s=%s', [Fields[0].AsString, Fields[0].AsString]));
        Next;
      end;
      Close;
    end;
    Result := AListaAnos;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetItems: IBizComision;
var
  AComision : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AComision := CloneDataTable(tbl_Comisiones);

    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    AComision.FieldByName(fld_ComisionesREFERENCIA).ServerAutoRefresh := TRUE;

    AComision.BusinessRulesID := BIZ_CLIENT_COMISIONES;

    with TBizComision(AComision.BusinessEventsObj) do
    begin
      FacturasComision := _GetFacturasComision;
    end;

    Result := (AComision as IBizComision);
  finally
    HideHourglassCursor;
  end;
end;


end.
