#1 2015-06-18 08:08:57

willo
Member
From: Cape Town, South Africa
Registered: 2014-11-15
Posts: 67
Website

Modeling the multi-legged Accounting Transaction pattern using ORM

Hi guys,

I've been wondering how to implement the multi-legged Accounting Transaction using the ORM as described by Fowler here: http://martinfowler.com/eaaDev/Accounti … ction.html

The ORM might not be the best tool to model this, but it would make for an interesting use case nonetheless.

What I have so far:

  TFinancialEvent = class;

  TTransaction = class( TSQLRecord )
  private
    fAmount: TCurrency;
    fAccount: TLegacyAccount;
    fTransactionDate: TDateTime;
    fStockEvent: TStockEvent;
  published
    property FinancialEvent : TFinancialEvent read fFinancialEvent write fFinancialEvent;
    property TransactionDate : TDateTime read fTransactionDate write fTransactionDate;
    property Account : TLegacyAccount read fAccount write fAccount;
    property Amount : TCurrency read fAmount write fAmount;
  end;

  TFinancialEvent = class( TSQLRecord )
  private
    fEventDate: TDateTime;
  published
    property EventDate : TDateTime read fEventDate;
  end;

Now, a lot of guys would now derive a new class from TFinancialEvent to handle specific events. Let's look at a Stock Procurement event, for instance:

  TStockProcurementEvent = class(TFinancialEvent)
  public
    constructor Create( aStockItem : TLegacyStockItem; aQuantity: Double; aAmount : TCurrency;
      aSupplier : TLegacySupplier; aReceivingLocation : TLegacyAccount; aDate : TDateTime ); reintroduce;
  end;

...

implementation

{ TStockProcurementEvent }

constructor TStockProcurementEvent.Create( aStockItem : TLegacyStockItem; aQuantity: Double; aAmount : TCurrency;
      aSupplier : TLegacySupplier; aReceivingLocation : TLegacyAccount; aDate : TDateTime );
var
  aTransaction : TTransaction;
begin
  inherited Create;
  fEventDate := aDate;

  //First add the Supplier leg
  aTransaction := Self.Add;
  aTransaction.Account := aSupplier;
  aTransaction.Amount := -aAmount;
  aTransaction.TransactionDate := aDate;

  //Then add the Receiver leg
  aTransaction := Self.Add;
  aTransaction.Account := aReceivingLocation;
  aTransaction.Amount := Amount;
  aTransaction.TransactionDate := aDate;
end;

There are some assumptions and the code is far from complete. The one solution I played with was to make TFinancialEvent a descendant from TCollection of TTransaction, which provides a clean separation from the Framework, but then there is no ORM involved.

So then I started playing with keeping a dynamic array of transactions in the Event object, but I haven't found an easy way to of adding or loading slave records using the ORM.

Any suggestions?

Offline

Board footer

Powered by FluxBB