unit uBizOscProductServer;

interface

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

const
  BIZ_SERVER_OSC_PRODUCT = 'Server.OSC.Product';

type
  TBizOscProductServer = class(Tosc_ProductsBusinessProcessorRules)
  protected
    procedure Insert_Products_Description(aChange: TDADeltaChange);
    procedure Update_Products_Description(aChange: TDADeltaChange);
    procedure Delete_Products_Description(aChange: TDADeltaChange);

    procedure Insert_Products_to_categories(aChange: TDADeltaChange);
    procedure Update_Products_to_categories(aChange: TDADeltaChange);
    procedure Delete_Products_to_categories(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;

const
  CATEGORIES_ID = 1;

{ TBizOscProductServer }

procedure TBizOscProductServer.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_Productsproducts_id] := ADataSet.FieldByName('last_id').AsInteger;
                Insert_Products_Description(aChange);
                Insert_Products_to_categories(aChange);
              end;

    ctUpdate: begin
                Update_Products_Description(aChange);
                Update_Products_to_categories(aChange);
              end;
    ctDelete: begin
                Delete_Products_Description(aChange);
                Delete_Products_to_categories(aChange);
              end;
  end;

  CanRemoveFromDelta := False;
end;

procedure TBizOscProductServer.Delete_Products_Description(
  aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
  AIdiomasDataSet : IDADataset;
  I: Integer;
begin
  // Borramos una fila por cada idioma que hay en OsCommerce y
  // por cada fabricante que hay.  
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  // Tabla osc_Products_description
  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_osc_Products_description');
  AIdiomasDataSet := ASchema.NewDataset(ACurrentConn, nme_osc_Languages);
  try
    AIdiomasDataSet.Open;
    for I := 0 to AIdiomasDataSet.RecordCount - 1 do
    begin
      with ACommand do
      begin
        ParamByName('OLD_products_id').Value := aChange.OldValueByName[fld_osc_Productsproducts_id];
        ParamByName('OLD_language_id').Value := AIdiomasDataSet.FieldByName(fld_osc_Languageslanguages_id).Value;
        Execute;
      end;
      AIdiomasDataSet.Next;
    end;
    AIdiomasDataSet.Close;
  finally
    ACommand := NIL;
    AIdiomasDataSet := NIL;
  end;
end;

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

  // Tabla osc_Products_description
  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_osc_Products_to_categories');
  try
    with ACommand do
    begin
      ParamByName('OLD_products_id').Value := aChange.OldValueByName[fld_osc_Productsproducts_id];
      ParamByName('OLD_categories_id').Value := CATEGORIES_ID;
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizOscProductServer.Insert_Products_Description(
  aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
  AIdiomasDataSet : IDADataset;
  I: Integer;
begin
  // Aquí insertamos una fila por cada idioma que hay en OsCommerce y
  // por cada producto que hay. Se hace para que no falle la tienda
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_osc_Products_description');
  AIdiomasDataSet := ASchema.NewDataset(ACurrentConn, nme_osc_Languages);
  try
    AIdiomasDataSet.Open;
    for I := 0 to AIdiomasDataSet.RecordCount - 1 do
    begin
      with ACommand do
      begin
        ParamByName('products_id').Value := aChange.NewValueByName[fld_osc_Productsproducts_id];
        ParamByName('language_id').Value := AIdiomasDataSet.FieldByName(fld_osc_Languageslanguages_id).Value;
        ParamByName('products_name').Value := aChange.NewValueByName[fld_osc_Productsproducts_name];
        ParamByName('products_description').Value := aChange.NewValueByName[fld_osc_Productsproducts_description];
        ParamByName('products_url').Value := null;
        ParamByName('products_viewed').Value := 0;
        Execute;
      end;
      AIdiomasDataSet.Next;
    end;
    AIdiomasDataSet.Close;
  finally
    ACommand := NIL;
    AIdiomasDataSet := NIL;
  end;
end;

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

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_osc_Products_to_categories');
  try
    with ACommand do
    begin
      ParamByName('products_id').Value := aChange.NewValueByName[fld_osc_Productsproducts_id];
      ParamByName('categories_id').Value := CATEGORIES_ID;
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizOscProductServer.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 TBizOscProductServer.Update_Products_Description(
  aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
  AIdiomasDataSet : IDADataset;
  I: Integer;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Update_osc_Products_description');
  AIdiomasDataSet := ASchema.NewDataset(ACurrentConn, nme_osc_Languages);
  try
    AIdiomasDataSet.Open;
    for I := 0 to AIdiomasDataSet.RecordCount - 1 do
    begin
      with ACommand do
      begin
        ParamByName('OLD_products_id').Value := aChange.OldValueByName[fld_osc_Productsproducts_id];
        ParamByName('OLD_language_id').Value := AIdiomasDataSet.FieldByName(fld_osc_Languageslanguages_id).Value;
        ParamByName('products_name').Value := aChange.NewValueByName[fld_osc_Productsproducts_name];
        ParamByName('products_description').Value := aChange.NewValueByName[fld_osc_Productsproducts_description];
        ParamByName('products_url').Value := null;
        ParamByName('products_viewed').Value := 0;
        Execute;
      end;
      AIdiomasDataSet.Next;
    end;
    AIdiomasDataSet.Close;
  finally
    ACommand := NIL;
    AIdiomasDataSet := NIL;
  end;
end;

procedure TBizOscProductServer.Update_Products_to_categories(
  aChange: TDADeltaChange);
begin
  // No se hace nada
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_OSC_PRODUCT, TBizOscProductServer);

end.
