{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uBizReferenciaGenericaServer;

interface

uses
  Classes, SysUtils,
  uDADataTable,
  uDAInterfaces, schReferenciaGenericaServer_Intf,
  uDABusinessProcessor;

const
  BIZ_SERVER_REFERENCIA_GENERICA = 'Server.ReferenciaGenerica';

type
  { TBizReferenciaGenericaServerRules }
  TBizReferenciaGenericaServerRules = class(TReferenciaGenericaBusinessProcessorRules)
  private
    function GetConnection: IDAConnection;
    function GetSchema: IDASchema;
    function GetDataSet(const AName : String) : IDADataset;
    function GetCommand(const AName : String) : IDASQLCommand;
  protected
    function ExisteFila: Boolean;
    function InsertarFila: Boolean;
    procedure BeforeProcessChange(Sender: TDABusinessProcessor;
      aChangeType: TDAChangeType; aChange: TDADeltaChange;
      var ProcessChange: Boolean); override;
  end;

implementation

uses
  Dialogs, Variants, uDAClasses;


{ TBizReferenciaGenericaServerRules }

procedure TBizReferenciaGenericaServerRules.BeforeProcessChange(
  Sender: TDABusinessProcessor; aChangeType: TDAChangeType;
  aChange: TDADeltaChange; var ProcessChange: Boolean);
begin
  inherited;
  if aChangeType = ctUpdate then
  begin
    if not ExisteFila then
      InsertarFila;
  end;
end;

function TBizReferenciaGenericaServerRules.ExisteFila: Boolean;
var
  Connection : IDAConnection;
  ADataSet: IDADataSet;
begin
  Result := True;
  Connection := GetConnection;
  ADataSet := GetDataSet('_ExisteFilaReferenciaGenerica');
  with ADataSet do
  begin
    ParamByName('NUMINF').AsString := NUMINF;
    ParamByName('CLAVE1').AsFloat := CLAVE1;
    ParamByName('CLAVE2').AsInteger := CLAVE2;
    ParamByName('CLAVE3').AsInteger := CLAVE3;
    ParamByName('NUMCON').AsInteger := NUMCON;
  end;
  Connection.BeginTransaction;
  try
    ADataSet.Open;
    Result := not ADataSet.IsEmpty;
    ADataSet.Close;
  finally
    Connection.RollbackTransaction;
    ADataSet := nil;
    Connection := nil;
  end;
end;

function TBizReferenciaGenericaServerRules.GetCommand(
  const AName: String): IDASQLCommand;
begin
  Result := GetSchema.NewCommand(GetConnection, AName);
end;

function TBizReferenciaGenericaServerRules.GetConnection: IDAConnection;
begin
  Result := BusinessProcessor.Schema.ConnectionManager.NewConnection(BusinessProcessor.Schema.ConnectionManager.GetDefaultConnectionName);
end;

function TBizReferenciaGenericaServerRules.GetDataSet(
  const AName: String): IDADataset;
begin
  Result := GetSchema.NewDataset(GetConnection, AName);
end;

function TBizReferenciaGenericaServerRules.GetSchema: IDASchema;
begin
  Result := BusinessProcessor.Schema;
end;

function TBizReferenciaGenericaServerRules.InsertarFila: Boolean;
var
  Connection : IDAConnection;
  ACommand: IDASQLCommand;
begin
  Result := False;
  Connection := GetConnection;
  ACommand := GetCommand(BusinessProcessor.InsertCommandName);
  with ACommand do
  begin
    ParamByName('NUMINF').AsString := NUMINF;
    ParamByName('CLAVE1').AsFloat := CLAVE1;
    ParamByName('CLAVE2').AsInteger := CLAVE2;
    ParamByName('CLAVE3').AsInteger := CLAVE3;
    ParamByName('FECHACAMBIO').AsDateTime := Now;
    ParamByName('ESTADO').AsString := 'P';  // Pendiente por defecto
    ParamByName('VTATERM').AsVariant := Null;
    ParamByName('VTAPROCESO').AsVariant := Null;
    ParamByName('VTAFINAL').AsVariant := Null;
    ParamByName('VALTOT').AsVariant := Null;
    ParamByName('CODIGO_BARRA').AsVariant := Null;
  end;

  Connection.BeginTransaction;
  try
    ACommand.Execute;
    Connection.CommitTransaction;
    Result := True;
  except
    Connection.RollbackTransaction;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_REFERENCIA_GENERICA, TBizReferenciaGenericaServerRules);

end.

