unit uBizObrasServer;

interface

uses
  SysUtils, schObrasServer_Intf, uDAInterfaces,
  uDADataTable, uDABusinessProcessor, uDADelta;

const
  BIZ_SERVER_OBRA = 'Server.Obra';

type
  TBizObrasServer = class(TObrasBusinessProcessorRules)
  protected
    procedure Insert_Datos_Obra(aChange: TDADeltaChange);
    procedure Update_Datos_Obra(aChange: TDADeltaChange);
    procedure Delete_Datos_Obra(aChange: TDADeltaChange);

    procedure AfterProcessChange(Sender: TDABusinessProcessor;
      aChange: TDADeltaChange; Processed: Boolean;
      var CanRemoveFromDelta: Boolean); override;

    procedure ProcessError(Sender: TDABusinessProcessor;
      aChangeType: TDAChangeType; aChange: TDADeltaChange;
      const aCommand: IDASQLCommand; var CanRemoveFromDelta: Boolean;
      Error: Exception); override;
  end;


implementation

{ TBizObrasServer }

uses
  uDataModuleServer, uDAClasses, Variants,
  schObrasClient_Intf, uBusinessUtils;
  

procedure TBizObrasServer.AfterProcessChange(Sender: TDABusinessProcessor;
  aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean);
begin
  inherited;
  { Por defecto, mantenemos los deltas por si alguna tabla hija los necesita }
  CanRemoveFromDelta := False;

  case aChange.ChangeType of
    ctInsert: begin
                Insert_Datos_Obra(aChange);
              end;
    ctUpdate: begin
                Update_Datos_Obra(aChange);
              end;
    ctDelete: begin
                Delete_Datos_Obra(aChange);
              end;
  end;
end;

procedure TBizObrasServer.Delete_Datos_Obra(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_OBRAS_DATOS');
  try
    with ACommand do
    begin
      ParamByName('OLD_ID_ALMACEN').Value := aChange.OldValueByName[fld_ObrasID];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizObrasServer.Insert_Datos_Obra(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_OBRAS_DATOS');
  try
    with ACommand do
    begin
      ParamByName('ID_ALMACEN').Value := aChange.NewValueByName[fld_ObrasID];
      ParamByName('ID_CLIENTE').Value := aChange.NewValueByName[fld_ObrasID_CLIENTE];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizObrasServer.ProcessError(Sender: TDABusinessProcessor;
  aChangeType: TDAChangeType; aChange: TDADeltaChange;
  const aCommand: IDASQLCommand; var CanRemoveFromDelta: Boolean;
  Error: Exception);
begin
  inherited;
  //IMPORTANTE ESTO HACE QUE EL CLIENTE SE ENTERE DEL ERROR Y LOS BP ASOCIADOS EN EL
  //SCHEMA HAGAN ROLLBACK TAMBIEN
//  CanRemoveFromDelta := True;
//  raise Exception.Create(Error.Message);
end;

procedure TBizObrasServer.Update_Datos_Obra(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Update_OBRAS_DATOS');
  try
    with ACommand do
    begin
      ParamByName('OLD_ID_ALMACEN').Value := aChange.NewValueByName[fld_ObrasID];
      ParamByName('ID_CLIENTE').Value := aChange.NewValueByName[fld_ObrasID_CLIENTE];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_OBRA, TBizObrasServer);

end.

