unit uBizOscOrderServer;

interface

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

const
  BIZ_SERVER_OSC_ORDER= 'Server.OSC.Order';

type
  TBizOscOrderServer = class(Tosc_OrdersBusinessProcessorRules)
  protected
    procedure Insert_Orders_Info(aChange: TDADeltaChange);
    procedure Update_Orders_Info(aChange: TDADeltaChange);
    procedure Delete_Orders_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;


{ TBizOscOrderServer }

procedure TBizOscOrderServer.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_Ordersorders_id] := ADataSet.FieldByName('last_id').AsInteger;
                Insert_Orders_Info(aChange);
              end;

    ctUpdate: Update_Orders_Info(aChange);
    ctDelete: Delete_Orders_Info(aChange);
  end;

  CanRemoveFromDelta := False;
end;

procedure TBizOscOrderServer.Delete_Orders_Info(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  // Borramos una fila por cada idioma que hay en OsCommerce y
  // por cada pedido que hay.
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

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

procedure TBizOscOrderServer.Insert_Orders_Info(aChange: TDADeltaChange);
begin
  //
end;

procedure TBizOscOrderServer.ProcessError(Sender: TDABusinessProcessor;
  aChangeType: TDAChangeType; aChange: TDADeltaChange;
  const aCommand: IDASQLCommand; var CanRemoveFromDelta: Boolean;
  Error: Exception);
begin
  inherited;

end;

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

  if aChange.OldValueByName[fld_osc_Ordersorders_status] <> aChange.NewValueByName[fld_osc_Ordersorders_status] then
  begin
    ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_osc_Orders_status_history');
    try
      with ACommand do
      begin
        ACommand.ParamByName('orders_id').Value := aChange.OldValueByName[fld_osc_Ordersorders_id];
        ACommand.ParamByName('orders_status_id').Value := aChange.NewValueByName[fld_osc_Ordersorders_status];
        ACommand.ParamByName('customer_notified').Value := 0;
        ACommand.ParamByName('comments').Value := aChange.NewValueByName[fld_osc_Orderscomments];
        ACommand.Execute;
      end;
    finally
      ACommand := NIL;
    end;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_OSC_ORDER, TBizOscOrderServer);


end.
