/**
  * Process a new transaction 
  * 
  * The param $oReversedLedgerTrans is only required if creating a reversal.
  * 
  * @param   LedgerTransaction   $oLedgerTrans           The new transaction to make 
  * @param   array               $aLedgerEntries         Array of Ledger Entries (account movements) to save
  * @param   LedgerTransaction   $oAdjustedLedgerTrans   The transaction that is to be reversed by this new transaction
  */
 public function process(LedgerTransaction $oLedgerTrans, array $aLedgerEntries, LedgerTransaction $oAdjustedLedgerTrans = null)
 {
     if (count($aLedgerEntries) === 0) {
         throw new LedgerException('Unable to process a new transaction a transaction must have at least one entry');
     }
     $bSuccess = true;
     $sErrorMsg = sprintf('Unable to save ledger transacrtion');
     # save the header any database errors will be recorded in the log by the entity
     if (true === ($bSuccess = $oLedgerTrans->save())) {
         $iTransactionID = $oLedgerTrans->iTransactionID;
         # assign new id to each entry and attempt to save it to the database
         foreach ($aLedgerEntries as $oEntry) {
             $oEntry->iTransactionID = $iTransactionID;
             if (false === ($bSuccess = $oEntry->save())) {
                 $sErrorMsg = sprintf('Unable to save ledger entry at account %s ', $oEntry->sAccountNumber);
                 $sErrorMsg = $this->buildResultString($sErrorMsg, $oEntry);
                 break;
             }
         }
         # assign the new reversal adjustment transction id to the source transaction
         if (true === $bSuccess && $oAdjustedLedgerTrans instanceof LedgerTransaction) {
             $oAdjustedLedgerTrans->iAdjustmentID = $iTransactionID;
             if (false === ($bSuccess = $oAdjustedLedgerTrans->save())) {
                 $sErrorMsg = sprintf('Unable to link source transaction at id %s to the new adjustment at id %s', $oAdjustedLedgerTrans->iTransactionID, $iTransactionID);
                 $sErrorMsg = $this->buildResultString($sErrorMsg, $oAdjustedLedgerTrans);
             }
         }
     } else {
         $sErrorMsg = $this->buildResultString($sErrorMsg, $oLedgerTrans);
     }
     if ($bSuccess === false) {
         $this->getLogger()->error($sErrorMsg);
         throw new LedgerException($sErrorMsg);
     }
     return $bSuccess;
 }
 public function testEntityLedgerTransaction()
 {
     $oContainer = $this->getContainer();
     $oLogger = $oContainer->getAppLogger();
     $oGateway = $oContainer->getGatewayCollection()->getGateway('ledger_transaction');
     # Create
     $oEntity = new LedgerTransaction($oGateway, $oLogger);
     $oEntity->iOrgUnitID = 1;
     $oEntity->oProcessingDate = new DateTime('now');
     $oEntity->oOccuredDate = new DateTime('now - 10 day');
     $oEntity->sVoucherNumber = '10004';
     $oEntity->iAdjustmentID = null;
     $oEntity->iUserID = 1;
     $oEntity->iJournalTypeID = 1;
     $bResult = $oEntity->save();
     $this->assertTrue($bResult);
     $this->assertNotEmpty($oEntity->iTransactionID);
     # test update (Adjustment id is set)
     $oEntity = new LedgerTransaction($oGateway, $oLogger);
     $oEntity->iTransactionID = 2;
     $oEntity->iOrgUnitID = 1;
     $oEntity->oProcessingDate = new DateTime('now');
     $oEntity->oOccuredDate = new DateTime('now - 5 day');
     $oEntity->sVoucherNumber = '1002';
     $oEntity->iAdjustmentID = 3;
     $oEntity->iUserID = 1;
     $oEntity->iJournalTypeID = 1;
     $oEntity->save();
     $this->assertTrue($bResult);
 }