unit uDataModuleComisiones;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall, uIDataModuleComisiones, uBizComisiones, uDataModuleBase,
  uIDataModuleComisionesReport, uDARemoteDataAdapter, uDADataStreamer,
  uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable;

type
  TDataModuleComisiones = class(TDataModuleBase, IDataModuleComisiones, IDataModuleComisionesReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_Comisiones: TDARemoteDataAdapter;
    tbl_Comisiones: TDAMemDataTable;
    ds_Comisiones: TDADataSource;
    tbl_Facturas_Comision: TDAMemDataTable;
    ds_Facturas_Comision: TDADataSource;
    tbl_Vendedores_Comision: TDAMemDataTable;
    ds_Vendedores_Comision: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function _GetFacturas : IBizFacturasComision;
    function _GetVendedores : IBizVendedoresComision;
  public
    function GetItems : IBizComisiones; overload;
    function GetFacturasSinComision : IBizFacturasComision;
    function GetItem(const ID : Integer) : IBizComisiones;
    function NewItem : IBizComisiones;

    // Report
    function GetReport(const AComisionID: String): Binary;
  end;

implementation
{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, 
  schComisionesClient_Intf;

{ TdmComisiones }

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

function TDataModuleComisiones.GetItems: IBizComisiones;
var
  AComisiones : TDAMemDataTable;
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;
      Vendedores := _GetVendedores;      
    end;

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

function TDataModuleComisiones._GetFacturas: IBizFacturasComision;
var
  AFacturas : TDAMemDataTable;
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._GetVendedores: IBizVendedoresComision;
var
  AVendedores : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AVendedores := CloneDataTable(tbl_Vendedores_Comision);
    with AVendedores do
    begin
      BusinessRulesID := BIZ_CLIENT_VENDEDORES_COMISION;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AVendedores as IBizVendedoresComision);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetFacturasSinComision: IBizFacturasComision;
var
  AFacturasSinComision : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
//    AFacturasSinComision := CloneDataTable(tbl_V_Detalles_Facturas_Comision);
//    AFacturasSinComision.BusinessRulesID := BIZ_CLIENT_FACTURAS_COMISION;
    Result := (AFacturasSinComision as IBizFacturasComision);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleComisiones.GetItem(const ID: Integer): IBizComisiones;
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.GetReport(const AComisionID: String): Binary;
begin
  Result := (RORemoteService as IsrvComisiones).GenerateReport(AComisionID);
end;

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

end.
