unit uBizPagosClienteServer;

interface

uses
  uDAInterfaces, uDADelta, uDABusinessProcessor,
  schRecibosClienteServer_Intf;

const
  BIZ_SERVER_PAGOS_CLIENTE = 'Server.PagosCliente';
  CTE_PAGO = 'Pago';
  CTE_DEVOLUCION = 'Devolución';


type
  TBizPagosClienteServer = class(TPagosClienteBusinessProcessorRules)
  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,
  schRecibosClienteClient_Intf;

{ TBizFacturasClienteServer }

procedure TBizPagosClienteServer.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 TBizPagosClienteServer.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').AsInteger := aChange.OldValueByName[fld_PagosClienteID];
      ParamByName('Tipo').AsString := 'c';
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizPagosClienteServer.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').AsInteger := aChange.NewValueByName[fld_PagosClienteID];
      ParamByName('IdSubCuentaPago').AsInteger := aChange.NewValueByName[fld_PagosClienteCUENTA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizPagosClienteServer.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').AsInteger := aChange.NewValueByName[fld_PagosClienteID];
      ParamByName('IdSubCuentaPago').AsInteger := aChange.NewValueByName[fld_PagosClienteCUENTA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_PAGOS_CLIENTE, TBizPagosClienteServer);

end.
