unit uBizComisiones; interface uses uDAInterfaces, uDADataTable, schComisionesClient_Intf, Classes, DBGrids, uDBSelectionList, DB, uExceptions, Controls; const BIZ_SELECCION = 'Client.Field.Seleccion'; BIZ_ALBARANESCOMISION = 'BizAlbaranesComision'; BIZ_VENDEDORESCOMISION = 'BizVendedoresComision'; type IBizSeleccion = interface(IDAStronglyTypedDataTable) ['{E4CB36C8-4A41-4DDB-92DB-211517EB60DB}'] end; IBizVendedoresComision = interface(IVendedoresComision) ['{4095FB35-E5FE-4C04-929A-480C29FC9436}'] procedure calcularComisiones(Total: Double); end; IBizAlbaranesComision = interface(IAlbaranesComision) ['{1CC63E18-6230-4421-96E8-26C011993F5C}'] function getVendedoresComision: IBizVendedoresComision; procedure setVendedoresComision(const Value: IBizVendedoresComision); property VendedoresComision: IBizVendedoresComision read getVendedoresComision write setVendedoresComision; function getTotal: Double; procedure setTotal(const Value: Double); property Total: Double read getTotal write setTotal; function GetOnTotalChanged : TNotifyEvent; procedure SetOnTotalChanged (const Value : TNotifyEvent); property OnTotalChanged : TNotifyEvent read GetOnTotalChanged write SetOnTotalChanged; end; TBizSeleccionFieldRules = class(TDAFieldRules) protected procedure OnChange(Sender: TDACustomField); override; end; TBizAlbaranesComisionDataTableRules = class(TAlbaranesComisionDataTableRules, IBizAlbaranesComision) private FVendedoresComision: IBizVendedoresComision; FTotal: Double; FOnTotalChanged : TNotifyEvent; function getTotal: Double; procedure setTotal(const Value: Double); function GetOnTotalChanged: TNotifyEvent; procedure SetOnTotalChanged(const Value: TNotifyEvent); function getVendedoresComision: IBizVendedoresComision; procedure setVendedoresComision(const Value: IBizVendedoresComision); protected procedure AfterOpen(Sender: TDADataTable); override; public property VendedoresComision: IBizVendedoresComision read getVendedoresComision write setVendedoresComision; property Total: Double read getTotal write setTotal; property OnTotalChanged : TNotifyEvent read GetOnTotalChanged write SetOnTotalChanged; constructor Create(aDataTable: TDADataTable); override; destructor Destroy; override; end; TBizVendedoresComisionDataTableRules = class(TVendedoresComisionDataTableRules, IBizVendedoresComision) public procedure calcularComisiones(Total: Double); end; implementation uses Windows, Dialogs, uDACDSDataTable, SysUtils, uDataModuleBase, uEditorUtils, uDataModuleComisiones, Variants; { TBizAlbaranesComisionDataTableRules } { ************************** TBizAlbaranesComisionDataTableRules ************************** } procedure TBizAlbaranesComisionDataTableRules.AfterOpen(Sender: TDADataTable); var ldTotal : Double; begin inherited; with (Sender as IBizAlbaranesComision) do begin DataTable.DisableControls; ldTotal := 0; First; while not DataTable.Eof do begin ldTotal := ldTotal + IMPORTETOTAL; Next; end; First; Total := ldTotal; DataTable.EnableControls; end; end; constructor TBizAlbaranesComisionDataTableRules.Create(aDataTable: TDADataTable); begin inherited; FVendedoresComision := Nil; aDataTable.AfterOpen := AfterOpen; end; destructor TBizAlbaranesComisionDataTableRules.Destroy; begin FVendedoresComision := Nil; inherited; end; function TBizAlbaranesComisionDataTableRules.GetOnTotalChanged: TNotifyEvent; begin Result := FOnTotalChanged; end; function TBizAlbaranesComisionDataTableRules.getTotal: Double; begin Result := FTotal; end; function TBizAlbaranesComisionDataTableRules.getVendedoresComision: IBizVendedoresComision; begin Result := FVendedoresComision; end; procedure TBizAlbaranesComisionDataTableRules.SetOnTotalChanged(const Value: TNotifyEvent); begin FOnTotalChanged := Value; end; procedure TBizAlbaranesComisionDataTableRules.setTotal(const Value: Double); begin FTotal := Value; if Assigned(FOnTotalChanged) then FOnTotalChanged(Self); FVendedoresComision.CalcularComisiones(FTotal); end; { TBizSeleccionFieldRules } procedure TBizSeleccionFieldRules.OnChange(Sender: TDACustomField); var AAlbaranesComision : IBizAlbaranesComision; begin if Supports(DataTable, IBizAlbaranesComision, AAlbaranesComision) then begin if AAlbaranesComision.SELECCION = 1 then AAlbaranesComision.Total := AAlbaranesComision.Total + AAlbaranesComision.IMPORTETOTAL else AAlbaranesComision.Total := AAlbaranesComision.Total - AAlbaranesComision.IMPORTETOTAL; end; end; procedure TBizAlbaranesComisionDataTableRules.setVendedoresComision(const Value: IBizVendedoresComision); begin if Assigned(FVendedoresComision) then FVendedoresComision := Nil; FVendedoresComision := Value; end; { TBizVendedoresComisionDataTableRules } procedure TBizVendedoresComisionDataTableRules.calcularComisiones(Total: Double); begin with (Self as IBizVendedoresComision) do begin DataTable.DisableControls; First; while not DataTable.Eof do begin Edit; COMISION := (Total * PORCENTAJE) / 100; Post; Next; end; First; DataTable.EnableControls; end; end; initialization RegisterFieldRules(BIZ_SELECCION, TBizSeleccionFieldRules); RegisterDataTableRules(BIZ_ALBARANESCOMISION, TBizAlbaranesComisionDataTableRules); RegisterDataTableRules(BIZ_VENDEDORESCOMISION, TBizVendedoresComisionDataTableRules); finalization end.