public Transfer Subtotal(int entryUserID, Entity entity)
        {
            User _user = (new UserAccessClient(EndpointName.UserAccess)).QueryuserID(entryUserID)[0];

            Dictionary<Entity, WeeklySummary> _pair = new Dictionary<Entity, WeeklySummary>();

            HasWeeklySummary(entity, _pair);

            Record _record = RecordHelper.GenerateTempRecord();

            _record.Type = RecordType.WinAndLoss;

            WeeklySummary _weeklySummary = _pair[entity];

            //Transfer
            Transfer _transfer = new Transfer()
            {
                RecordID = _record.RecordID,
                ToEntity = entity,
            };

            if (_weeklySummary == null)
            {
                using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
                {
                    _weeklySummaryAccessClient.Insert1(_weeklySummary = new WeeklySummary(PeriodService.Instance.GetCurrentPeriod()[0], entity));
                }
            }

            _transfer.ExchangeRate = _weeklySummary.ExchangeRate;
            _transfer.BaseBefore = _weeklySummary.BaseBalance;
            _transfer.SGDBefore = _weeklySummary.SGDBalance;
            _transfer.Currency.CurrencyID = _weeklySummary.BaseCurrency;
            foreach (Entity _entity in _pair.Keys)
            {
                if (_entity == entity)
                {
                    continue;
                }
                else if ((_weeklySummary = _pair[_entity]) == null)
                {
                    using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
                    {
                        _weeklySummaryAccessClient.Insert1(_weeklySummary = new WeeklySummary(PeriodService.Instance.GetCurrentPeriod()[0], _entity));
                    }
                }

                TransferDetail _transferDetail = new TransferDetail()
                {
                    RecordID = _record.RecordID,
                    Entity = _entity,
                    BaseCurrency = _weeklySummary.BaseCurrency,
                    ExchangeRate = _weeklySummary.ExchangeRate,
                    BaseBefore = _weeklySummary.BaseBalance,
                    SGDBefore = _weeklySummary.SGDBalance,
                };

                _transferDetail.BaseTransfer = _transferDetail.BaseBefore;
                _transferDetail.SGDTransfer = _transferDetail.SGDBefore;
                _transferDetail.ProfitAndLoss = 0;

                _transferDetail.BaseResult = 0;
                _transferDetail.SGDResult = 0;

                _transfer.TransferDetailCollection.Add(_transferDetail);
            }

            _transfer.SGDResult = _transfer.TransferDetailCollection.Sum(TransferDetail => TransferDetail.SGDBefore).ExtRound();
            _transfer.BaseResult = (_transfer.SGDResult * _transfer.ExchangeRate).ExtRound();

            //Record
            Journal _journal = new Journal()
            {
                RecordID = _record.RecordID,
                EntityID = entity.EntityID,
                BaseCurrency = entity.Currency.CurrencyID,
                ExchangeRate = _transfer.ExchangeRate,
                BaseAmount = _transfer.TransferDetailCollection.Sum(TransferDetail => TransferDetail.BaseTransfer),
                SGDAmount = _transfer.TransferDetailCollection.Sum(TransferDetail => TransferDetail.SGDTransfer),
                EntryUser = _user,
            };

            _record.JournalCollection.Add(_journal);

            foreach (Entity _entity in _pair.Keys)
            {
                if (_entity == entity)
                {
                    continue;
                }
                else if ((_weeklySummary = _pair[_entity]) == null)
                {
                    continue;
                }

                _journal = new Journal()
                {
                    RecordID = _record.RecordID,
                    EntityID = _entity.EntityID,
                    BaseCurrency = _weeklySummary.BaseCurrency,
                    ExchangeRate = _weeklySummary.ExchangeRate,
                    BaseAmount = _weeklySummary.BaseBalance * -1,
                    SGDAmount = _weeklySummary.SGDBalance * -1,
                    EntryUser = _user,
                };

                _record.JournalCollection.Add(_journal);
            }

            _transfer.RecordNotInDB = _record;

            return _transfer;
        }
        private void HasWeeklySummary(int entityID)
        {
            WeeklySummaryCollection _wsa;
            using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
            {
                var _period = PeriodService.Instance.GetCurrentPeriod()[0];
                _wsa = new WeeklySummaryCollection(_weeklySummaryAccessClient.Query(_period.ID, entityID));
                if (!_wsa.Any())
                {
                    using (EntityAccessClient _entityAccessClient = new EntityAccessClient(EndpointName.EntityAccess))
                    {
                        var _entity = _entityAccessClient.Query2(entityID)[0];
                        if (_entity.SumType != SumType.Not)
                        {
                            WeeklySummary _newWeeklySummary = new WeeklySummary
                            {
                                Entity = _entity,
                                ExchangeRate = _entity.ExchangeRate,
                                Period = _period,
                                BaseCurrency = _entity.Currency.CurrencyID,
                            };
                            _weeklySummaryAccessClient.Insert1(_newWeeklySummary);
                        }
                    }
                }

            }
        }
        private EntityCollection SetTransaction(EntityCollection entityCollection, WeeklySummaryCollection weeklySummaryCollection)
        {
            for (int i = 0; i < entityCollection.Count; i++)
            {
                if (entityCollection[i].SumType == SumType.Transaction)
                {
                    entityCollection[i].SubEntities = SetSubtotal(entityCollection[i].SubEntities, weeklySummaryCollection);

                    WeeklySummaryCollection _weeklySummaryCollection = CalculateService.Instance.GetWeeklySummary(entityCollection[i].EntityID);

                    if (_weeklySummaryCollection.Count == 0)
                    {
                        WeeklySummary _weeklySummary = new WeeklySummary(PeriodService.Instance.GetCurrentPeriod()[0], entityCollection[i]);

                        using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
                        {
                            _weeklySummaryAccessClient.Insert1(_weeklySummary);
                        }

                        weeklySummaryCollection.Add(_weeklySummary);
                    }
                    else
                    {
                        weeklySummaryCollection.AddRange(_weeklySummaryCollection);
                    }

                    continue;
                }

                EntityCollection _subEntities = entityCollection[i].SubEntities;

                entityCollection.RemoveAt(i);

                if (_subEntities.Count > 0)
                {
                    entityCollection.InsertRange(i, _subEntities);
                }

                i--;
            }

            return entityCollection;
        }