unit uBizOscCustomerServer;

interface

uses
  SysUtils, schTiendaWebServer_Intf, uDAInterfaces,
  uDADataTable, uDABusinessProcessor, uBusinessUtils;

const
  BIZ_SERVER_OSC_CUSTOMER = 'Server.OSC.Customer';

type
  TBizOscCustomerServer = class(Tosc_CustomersBusinessProcessorRules)
  protected
    procedure Insert_Customers_Info(aChange: TDADeltaChange);
    procedure Update_Customers_Info(aChange: TDADeltaChange);
    procedure Delete_Customers_Info(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

uses
  Dialogs, Variants, uDataModuleServer, uDAClasses, DARemoteService_Impl,
  schTiendaWebClient_Intf, uROClasses, srvTiendaWeb_Impl, Windows;

{ TBizOscCustomerServer }

procedure TBizOscCustomerServer.AfterProcessChange(Sender: TDABusinessProcessor;
  aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ADataset : IDADataset;
begin
  ASchema := Sender.Schema;
  ACurrentConn := GetBusinessProcessorConnection(Sender);

  inherited;

  case aChange.ChangeType of
    ctInsert: begin
                ADataSet := ASchema.NewDataset(ACurrentConn, 'GetLastID', [], [], True);
                aChange.NewValueByName[fld_osc_Customerscustomers_id] := ADataSet.FieldByName('last_id').AsInteger;
                Insert_Customers_Info(aChange);
              end;

    ctUpdate: Update_Customers_Info(aChange);
    ctDelete: Delete_Customers_Info(aChange);
  end;

  CanRemoveFromDelta := False;
end;

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

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

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

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_Customers_info');
  try
    with ACommand do
    begin
      ParamByName('customers_info_id').Value := aChange.NewValueByName[fld_osc_Customerscustomers_id];
      ParamByName('rdx_customers_info_id_local').Value := aChange.NewValueByName[fld_osc_Customersrdx_customers_id_local];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizOscCustomerServer.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 TBizOscCustomerServer.Update_Customers_Info(aChange: TDADeltaChange);
begin
  // NO HACE FALTA HACER NADA
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_OSC_CUSTOMER, TBizOscCustomerServer);

end.
