unit uBizVentasTerminadas;

interface

uses
  Classes, DB, SysUtils, uROClasses, uDADataTable, uDAInterfaces,
  DataAbstract_Intf, uROTypes, schVentasTerminadasClient_Intf;

const
  BIZ_VENTASTERMINADAS = 'Client.VentasTerminadas';
  BIZ_RESUMENVENTAS = 'Client.ResumenVentas';

type
  IBizVentasTerminadas = interface(IVentasTerminadas)
    ['{A8E22BA0-970A-461D-8C69-E3C37F099F76}']
    function GetVisibleColumns: String;
    procedure SetVisibleColumns(const Value: String);
    property VisibleColumns : String read GetVisibleColumns write SetVisibleColumns;
  end;

  IBizResumenVentas = interface(IVentasTerminadasCrossTab)
  ['{F7536368-285D-45B4-B0F0-7DBE724982A3}']
  end;

  TBizResumenVentasDataTableRules = class(TVentasTerminadasCrossTabDataTableRules, IBizResumenVentas)
  protected
    procedure OnBeforeDataRequestCall(DataTable: TDADataTable; Request: TDARemoteRequest);
  public
    constructor Create(aDataTable: TDADataTable); override;
  end;

  TBizVentasTerminadasDataTableRules = class(TVentasTerminadasDataTableRules, IBizVentasTerminadas)
  private
    FVisibleColumns : String;
    FDateParams : TDADatasetParamArray;
  protected
    function GetVisibleColumns: String;
    procedure SetVisibleColumns(const Value: String);
    procedure OnBeforeDataRequestCall(DataTable: TDADataTable;
      Request: TDARemoteRequest);
    procedure OnBeforeSchemaCall(DataTable: TDADataTable;
      Request: TDARemoteRequest);
  public
    constructor Create(aDataTable: TDADataTable); override;
    property VisibleColumns : String read GetVisibleColumns write SetVisibleColumns;
    destructor Destroy; override;
  end;


implementation

{ TBIZ_VENTASDataTableRules }

uses
  Dialogs, uEditorUtils, uDataModuleVentasTerminadas;

constructor TBizVentasTerminadasDataTableRules.Create(aDataTable: TDADataTable);
begin
  inherited;
  FDateParams := TDADatasetParamArray.Create;
  if Assigned(DataTable) then
  begin
    DataTable.OnBeforeDataRequestCall := OnBeforeDataRequestCall;
    DataTable.OnBeforeSchemaCall := OnBeforeSchemaCall;
  end;
end;

destructor TBizVentasTerminadasDataTableRules.Destroy;
begin
  FreeAndNIL(FDateParams);
  inherited;
end;

function TBizVentasTerminadasDataTableRules.GetVisibleColumns: String;
begin
  if Length(FVisibleColumns) = 0 then
    FVisibleColumns := dmVentasTerminadas.GetNameColumns;
  Result := FVisibleColumns
end;

procedure TBizVentasTerminadasDataTableRules.OnBeforeDataRequestCall(
  DataTable: TDADataTable; Request: TDARemoteRequest);
begin
  Request.ParamByName('Columns').AsString := VisibleColumns;
  FDateParams.Clear;
  NewDatasetParam(FDateParams, 'FECHAINI', DataTable.ParamByName('FECHAINI').AsDateTime);
  NewDatasetParam(FDateParams, 'FECHAFIN', DataTable.ParamByName('FECHAFIN').AsDateTime);
  Request.ParamByName('Params').AsComplexType := FDateParams;
end;

procedure TBizVentasTerminadasDataTableRules.OnBeforeSchemaCall(
  DataTable: TDADataTable; Request: TDARemoteRequest);
begin
  Request.ParamByName('Columns').AsString := VisibleColumns;
end;

procedure TBizVentasTerminadasDataTableRules.SetVisibleColumns(const Value: String);
begin
  if Length(Value) > 0 then
    FVisibleColumns := Value
  else
    FVisibleColumns := dmVentasTerminadas.GetNameColumns;
end;


{ TBizResumenVentasDataTableRules }

constructor TBizResumenVentasDataTableRules.Create(aDataTable: TDADataTable);
begin
  inherited;
  if Assigned(DataTable) then
     DataTable.OnBeforeDataRequestCall := OnBeforeDataRequestCall;
end;

procedure TBizResumenVentasDataTableRules.OnBeforeDataRequestCall(DataTable: TDADataTable; Request: TDARemoteRequest);
begin
  Request.ParamByName('FechaIni').AsString := DataTable.ParamByName('FechaIni').AsString;
  Request.ParamByName('FechaFin').AsString := DataTable.ParamByName('FechaFin').AsString;
end;

initialization
  RegisterDataTableRules(BIZ_VENTASTERMINADAS, TBizVentasTerminadasDataTableRules);
  RegisterDataTableRules(BIZ_RESUMENVENTAS, TBizResumenVentasDataTableRules);

finalization

end.

