public JournalCollection AutoJournal(JournalCollection pAndLEntities)
        {
            JournalCollection _result = new JournalCollection();

            foreach (Journal _pAndLEntity in pAndLEntities)
            {
                _pAndLEntity.BaseAmount = _pAndLEntity.SGDAmount * _pAndLEntity.ExchangeRate;

                RelationCollection _relationCollection = EntityService.Instance.RelationEntityWandL(_pAndLEntity.EntityID);
                if (_relationCollection.Count == 0)
                {
                    continue;
                }

                Relation _allocate = _relationCollection.FirstOrDefault(relation => ((int)relation.Description)<4);
                IEnumerable<Relation> _positions = _relationCollection.Where(relation => (relation.Description == RelationDescription.Position) && !pAndLEntities.Any(journal=>journal.EntityID == relation.TargetEntity.EntityID) );
                //if the node was selected then it didn't need to be selected again.
                //!pAndLEntities.Any(journal=>journal.EntityID == relation.TargetEntity.EntityID

                if (_allocate == null)
                {
                    continue;
                }
                if (!pAndLEntities.Any(Journal => (Journal.EntityID == _allocate.TargetEntity.EntityID)))
                {
                    Journal _journalAllocate = new Journal()
                    {
                        RecordID = _pAndLEntity.RecordID,
                        EntityID = _allocate.TargetEntity.EntityID,
                        BaseCurrency = _allocate.TargetEntity.Currency.CurrencyID,
                        ExchangeRate = _allocate.TargetEntity.ExchangeRate,
                        SequenceNo = _pAndLEntity.SequenceNo,

                        //-600 = 550 / 0.55 * 0.6 * -1
                        //600 = -550 / 0.55 * 0.6 * -1
                        SGDAmount = (_pAndLEntity.SGDAmount / _allocate.Numeric * (_allocate.Numeric + _positions.Sum(_position => _position.Numeric)) * -1).ExtRound(),

                        EntryUser = _pAndLEntity.EntryUser,
                    };

                _journalAllocate.BaseAmount = (_journalAllocate.SGDAmount * _journalAllocate.ExchangeRate).ExtRound();
                _result.Add(_journalAllocate);

                }
                foreach (Relation _position in _positions)
                {
                    Journal _journalPosition = new Journal()
                    {
                        RecordID = _pAndLEntity.RecordID,
                        //EntityID = _position.TargetEntity.EntityID,
                        EntityID = _position.Entity.EntityID,
                        BaseCurrency = _position.TargetEntity.Currency.CurrencyID,
                        ExchangeRate = _position.TargetEntity.ExchangeRate,
                        SequenceNo = _pAndLEntity.SequenceNo,
                        //25 = 550 / 0.55 * 0.025
                        //-25 = -550 / 0.55 * 0.025
                        SGDAmount = (_pAndLEntity.SGDAmount / _allocate.Numeric * _position.Numeric).ExtRound(),
                        EntryUser = _pAndLEntity.EntryUser,
                    };

                    _journalPosition.BaseAmount = (_journalPosition.SGDAmount * _journalPosition.ExchangeRate).ExtRound();

                    _result.Add(_journalPosition);
                }
            }

            pAndLEntities.AddRange(_result);

            return pAndLEntities;
        }
        public int SaveRecord(Record record, JournalCollection jcollection)
        {
            //log record
            var _logRecord = LoadRecordByPeriodEntityID(record.EntityID, record.Period.ID);
            if (_logRecord.Any())
            {
                JournalCollection _delJournalCollection= new JournalCollection();
                JournalCollection _updJournalCollection = new JournalCollection();
                var _newJournal = jcollection;
                foreach (var _journal in _logRecord[0].JournalCollection)
                {
                    if (_newJournal.Any(x => _journal.EntityID == x.EntityID && _journal.BaseAmount == x.BaseAmount))
                        continue;
                    else if (_newJournal.Any(x => _journal.EntityID == x.EntityID))
                        _updJournalCollection.Add(_newJournal.First(x => x.EntityID == _journal.EntityID));
                    else
                        _delJournalCollection.Add(_journal);
                }

                #region "Update function"
                if (_updJournalCollection.Any())
                {
                    using (RecordAccessClient _recordAccessClient = new RecordAccessClient(EndpointName.RecordAccess))
                    {
                        try
                        {
                            _recordAccessClient.UpdateJournalCollection(_updJournalCollection.ToArray());
                        }
                        catch(Exception ex)
                        {
                            return -1;
                        }
                    }
                }
                #endregion

                #region "Delete function"
                if (_delJournalCollection.Any())
                {
                    using (RecordAccessClient _recordAccessClient = new RecordAccessClient(EndpointName.RecordAccess))
                    {
                        try
                        {
                            _recordAccessClient.InsertDeletionLog(_delJournalCollection.ToArray());
                        }
                        catch (Exception ex)
                        {
                            return -1;
                        }
                    }
                }
                #endregion

                return record.RecordID;
            }
            else
            {
                using (RecordAccessClient _recordAccessClient = new RecordAccessClient(EndpointName.RecordAccess))
                {
                    return _recordAccessClient.Insert(record, jcollection.ToArray());
                }
            }
        }