unit uDataModuleComisiones;

interface

uses
  SysUtils, Classes, DB, uIDataModuleComisiones, uIDataModuleComisionesReport,
  uBizComisiones, uDAInterfaces, uDADataTable, uROTypes,
  uDAScriptingProvider, uDAMemDataTable, uDADataStreamer, uDABin2DataStreamer,
  uDARemoteDataAdapter, uROClient, uRORemoteService, uDataModuleBase;

type
  TDataModuleComisiones = class(TDataModuleBase, IDataModuleComisiones, IDataModuleComisionesReport)
    RORemoteService: TRORemoteService;
    rda_Comisiones: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_Comisiones: TDAMemDataTable;
    ds_Comisiones: TDADataSource;
    tbl_Facturas_Comision: TDAMemDataTable;
    ds_Facturas_Comision: TDADataSource;
    tbl_V_Detalles_Facturas_Comision: TDAMemDataTable;
    ds_V_Detalles_Facturas_Comision: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function _GetFacturas : IBizFacturasComision;
    procedure AsignarClaseNegocio(AComisiones: TDADataTable);

  public
    function GetItems : IBizComisiones; overload;
    function GetFacturasDesglosadas : IBizFacturasComision;
    function GetItem(const ID : Integer) : IBizComisiones;
    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,
  schComisionesClient_Intf;

{ TdmComisiones }

procedure TDataModuleComisiones.AsignarClaseNegocio(AComisiones: TDADataTable);
begin
  AComisiones.BusinessRulesID := BIZ_CLIENT_COMISIONES;
end;

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

function TDataModuleComisiones.GetFacturasDesglosadas: IBizFacturasComision;
var
  AFacturasDesglosadas : TDAMemDataTable;
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;
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.GetItems: IBizComisiones;
var
  AComisiones : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AComisiones := CloneDataTable(tbl_Comisiones);
    AsignarClaseNegocio(AComisiones);

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

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


function TDataModuleComisiones.GetReport(const AComisionID: Variant;
  const DesglosadoProv: Boolean; FechaIni, 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;


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;

end.
