unit uDataModuleComisiones;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall, uIDataModuleComisiones, uBizComisiones,
  uIDataModuleComisionesReport;

type
  TDataModuleComisiones = class(TDAClientDataModule, IDataModuleComisiones, IDataModuleComisionesReport)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_Comisiones: TDACDSDataTable;
    ds_Comisiones: TDADataSource;
    tbl_Facturas_Comision: TDACDSDataTable;
    ds_Facturas_Comision: TDADataSource;
    tbl_V_Detalles_Facturas_Comision: TDACDSDataTable;
    ds_V_Detalles_Facturas_Comision: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function _GetFacturas : IBizFacturasComision;
    function darNombreGenerador(DataSetName: String): String; virtual;
  public
    function GetItems : IBizComisiones; overload;
    function GetFacturasDesglosadas : IBizFacturasComision;
    function GetItem(const ID : Integer) : IBizComisiones;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizComisiones;

    // Report
    function GetReport(const AComisionID: Variant; const DesglosadoProv: Boolean; FechaIni: String; FechaFin: String; AAgenteID: Variant): Binary;
  end;

implementation
{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, uDAInterfaces,
  schComisionesClient_Intf;

{ TdmComisiones }

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

function TDataModuleComisiones.darNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_Comisiones then
    Result := 'GEN_COMISIONES_LIQUID_ID';
end;

function TDataModuleComisiones.GetItems: IBizComisiones;
var
  AComisiones : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AComisiones := _CloneDataTable(tbl_Comisiones);

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

    AComisiones.BusinessRulesID := BIZ_CLIENT_COMISIONES;

    with TBizComisiones(AComisiones.BusinessEventsObj) do
    begin
      Facturas := _GetFacturas;
    end;

    Result := (AComisiones as IBizComisiones);
  finally
    HideHourglassCursor;
  end;
end;

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

function TDataModuleComisiones._GetFacturas: IBizFacturasComision;
var
  AFacturas : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AFacturas := _CloneDataTable(tbl_Facturas_Comision);
    with AFacturas do
    begin
      BusinessRulesID := BIZ_CLIENT_FACTURAS_COMISION;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AFacturas as IBizFacturasComision);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetFacturasDesglosadas: IBizFacturasComision;
var
  AFacturasDesglosadas : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AFacturasDesglosadas := _CloneDataTable(tbl_V_Detalles_Facturas_Comision);
    AFacturasDesglosadas.BusinessRulesID := BIZ_CLIENT_FACTURAS_COMISION;
    Result := (AFacturasDesglosadas as IBizFacturasComision);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetItem(const ID: Integer): IBizComisiones;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_ComisionesID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;


function TDataModuleComisiones.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  aGeneratorName := DarNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvComisiones).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleComisiones.GetReport(const AComisionID: Variant; const DesglosadoProv: Boolean; FechaIni: String; FechaFin: String; AAgenteID: Variant): Binary;
begin
  Result := (RORemoteService as IsrvComisiones).GenerateReport(AComisionID, DesglosadoProv, FechaIni, FechaFin, AAgenteID);
end;

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

end.
