unit uBizEmpresasServer;

interface

uses
  uDAInterfaces, uDADelta, uDABusinessProcessor,
  schEmpresasServer_Intf;

const
  BIZ_SERVER_EMPRESAS = 'Server.Empresas';

type
  TBizEmpresasServer = class(TEmpresasBusinessProcessorRules)
  protected
    procedure AfterProcessChange(Sender: TDABusinessProcessor; aChange: TDADeltaChange; Processed: Boolean;
      var CanRemoveFromDelta: Boolean); override;


  end;

implementation

uses
  Variants, uDAClasses, srvReferencias_Impl, uBusinessUtils, uROClasses,
  uDataModuleServer, schEmpresasClient_Intf, Dialogs;

{ TBizEmpresasServer }

procedure TBizEmpresasServer.AfterProcessChange(Sender: TDABusinessProcessor;
  aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean);
var
  AConn : IDAConnection;
  ParamTiempo: Variant;
  ParamMargen: Variant;

begin
  inherited;

  { Por defecto, mantenemos los deltas por si alguna tabla hija los necesita }
  CanRemoveFromDelta := False;

  case aChange.ChangeType of
    ctInsert, ctUpdate: begin
      ParamTiempo := Sender.CurrentChange.NewValueByName[fld_EmpresasPARAM_TIEMPO];
      ParamMargen := Sender.CurrentChange.NewValueByName[fld_EmpresasPARAM_MARGEN];
      if ((Sender.CurrentChange.OldValueByName[fld_EmpresasPARAM_TIEMPO] <> ParamTiempo)
      or (Sender.CurrentChange.OldValueByName[fld_EmpresasPARAM_MARGEN] <> ParamMargen)) then
      begin
        try
          //Recalculamos los PVP del catalogo
          AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);
          AConn.BeginTransaction;
          Sender.Schema.NewCommand(AConn, 'RecalcularPVPArticulos', ['ID_EMPRESA', 'PARAM_MARGEN', 'PARAM_TIEMPO'], [Sender.CurrentChange.OldValueByName[fld_EmpresasID], ParamMargen, ParamTiempo]);
          AConn.CommitTransaction;
        except
          AConn.RollbackTransaction;
          RaiseError('RecalcularPVPArticulos');
        end;

        AConn:= Nil;
      end;
    end;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_EMPRESAS, TBizEmpresasServer);

end.
