unit uBizPagosProveedorServer;

interface

uses
  uDAInterfaces, uDADelta, uDABusinessProcessor,
  schRecibosProveedorServer_Intf;

const
  BIZ_SERVER_PAGOS_PROVEEDOR = 'Server.PagosProveedor';

type
  TBizPagosProveedorServer = class(TPagosProveedorBusinessProcessorRules)
  protected
    procedure Insert_Asiento_Pago(aChange: TDADeltaChange); virtual;
    procedure Update_Asiento_Pago(aChange: TDADeltaChange); virtual;
    procedure Delete_Asiento_Pago(aChange: TDADeltaChange); virtual;

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

implementation

uses
  Variants, uDAClasses, uReferenciasUtils, uBusinessUtils, uROClasses,  uDataModuleServer,
  schRecibosProveedorClient_Intf;

{ TBizPagosProveedorServer }

procedure TBizPagosProveedorServer.AfterProcessChange(Sender: TDABusinessProcessor; aChange: TDADeltaChange; Processed: Boolean;
  var CanRemoveFromDelta: Boolean);
begin
  case aChange.ChangeType of
    ctInsert: begin
                Insert_Asiento_Pago(aChange);
              end;
    ctUpdate: begin
                Update_Asiento_Pago(aChange);
              end;
    ctDelete: begin
                Delete_Asiento_Pago(aChange);
              end;
  end;

  // No hay que quitar los deltas para que los datos del contacto se
  // mantengan por si alguna tabla detalle lo necesita
  // (por ejemplo, DireccionesContacto)
  CanRemoveFromDelta := False;
end;

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

  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_AsientoPago');
  try
    with ACommand do
    begin
      ParamByName('IdPago').Value := aChange.OldValueByName[fld_PagosProveedorID];
      ParamByName('Tipo').Value := 'p';
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

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

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_AsientoPago');
  try
    with ACommand do
    begin
      ParamByName('IdPago').Value := aChange.NewValueByName[fld_PagosProveedorID];
      ParamByName('IdSubCuentaPago').Value := aChange.NewValueByName[fld_PagosProveedorCUENTA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

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

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_AsientoPago');
  try
    with ACommand do
    begin
      ParamByName('IdPago').Value := aChange.NewValueByName[fld_PagosProveedorID];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_PAGOS_PROVEEDOR, TBizPagosProveedorServer);

end.
