unit uBizApuntes;

interface

uses
  uDAInterfaces, uDADataTable, schContabilidadClient_Intf;

const
  CTE_PUNTEADO = 1;
  CTE_NO_PUNTEADO = -1;
  BIZ_CLIENT_Apunte = 'Client.Apunte';

type
  IBizApunte = interface(IApuntes)
  ['{D26061DB-FFF4-456F-B442-A14A19501DD0}']
    function EsNuevo : Boolean;
    procedure SetNumOrden;
  end;

  TBizApunte = class(TApuntesDataTableRules, IBizApunte)
  private
    FNumOrden: Integer;

  protected
    procedure OnNewRecord(Sender: TDADataTable); override;

  public
    procedure SetNumOrden;
    procedure IniciarValoresApunteNueva;
    function EsNuevo : Boolean;

    constructor Create(aDataTable: TDADataTable); override;
    destructor Destroy; override;
  end;

implementation

{ TBizApunte }

uses
  DB, SysUtils, uDataTableUtils, uFactuGES_App;

constructor TBizApunte.Create(aDataTable: TDADataTable);
begin
  inherited;
  FNumOrden := 1;
end;

destructor TBizApunte.Destroy;
begin
  FNumOrden := 0;
  inherited;
end;

function TBizApunte.EsNuevo: Boolean;
begin
  Result := (ID < 0);
end;

procedure TBizApunte.IniciarValoresApunteNueva;
begin
  NUM_ORDEN := FNumOrden;
  Inc(FNumOrden);
  PUNTEADO := CTE_NO_PUNTEADO;
end;

procedure TBizApunte.OnNewRecord(Sender: TDADataTable);
begin
  inherited;
  IniciarValoresApunteNueva;
end;

procedure TBizApunte.SetNumOrden;
var
  ABookmark : TBookmark;
  NumOrdenMax : Integer;
begin
  ABookmark := NIL;
  
  with Self.DataTable do
  begin
    if not Opening then
       Open;

    try
      ABookmark := GetBookMark;
      DisableControls;
      DisableEventHandlers;

      NumOrdenMax := 0;
      First;
      while not Eof do
      begin
        if NUM_ORDEN > NumOrdenMax then
           NumOrdenMax := NUM_ORDEN;
        Next;
      end;

      Inc(NumOrdenMax);
      FNumOrden := NumOrdenMax;
      
    finally
      EnableEventHandlers;
      GotoBookmark(ABookmark);
      FreeBookmark(ABookmark);
      EnableControls;
    end;
  end;
end;

initialization
  RegisterDataTableRules(BIZ_CLIENT_Apunte, TBizApunte);

finalization

end.

