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);
                        }
                    }
                }

            }
        }
        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 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;
        }
 //TODO:
 public WeeklySummaryCollection GetWeeklySummary(int entityID)
 {
     using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
     {
         HasWeeklySummary(entityID);
         return new WeeklySummaryCollection(_weeklySummaryAccessClient.Query(PeriodService.Instance.GetCurrentPeriod()[0].ID, entityID));
     }
 }
        public WeeklySummaryCollection Confirm(int confirmUserID, int entityID)
        {
            User _confirmUser = (new UserAccessClient(EndpointName.UserAccess)).QueryuserID(confirmUserID)[0];

            WeeklySummaryCollection _confirm = new WeeklySummaryCollection();

            Entity _entity = EntityService.Instance.LoadEntity(entityID)[0];

            if ((_entity.SumType != SumType.Transaction) && (_entity.SumType != SumType.Subtotal))
            {
                return _confirm;
            }

            _entity.SubEntities = ((_entity.SumType == SumType.Transaction) ? SetSubtotal(_entity.SubEntities, new WeeklySummaryCollection()) : SetLastLevel(_entity.SubEntities, new WeeklySummaryCollection()));

            ConfirmTransfer(new EntityCollection(new Entity[] { _entity }), _confirm);

            if (_confirm.Count <= 0)
            {
                return _confirm;
            }

            foreach (WeeklySummary _weeklySummary in _confirm)
            {
                _weeklySummary.Status = WeeklySummaryStatus.Confirm;
                _weeklySummary.ConfirmUser = _confirmUser;
            }

            using (WeeklySummaryAccessClient _weeklySummaryAccessClient = new WeeklySummaryAccessClient(EndpointName.WeeklySummaryAccess))
            {
                _weeklySummaryAccessClient.Update2(_confirm.ToArray());
            }

            //<EntityID, RecordID>
            Dictionary<int, int> _pairs = new Dictionary<int, int>();

            foreach (WeeklySummary _weeklySummary in _confirm)
            {
                int _recordID = -1;

                using (RecordAccessClient _recordAccessClient = new RecordAccessClient(EndpointName.RecordAccess))
                {
                    _pairs[_weeklySummary.Entity.EntityID] = (_recordID = _recordAccessClient.QueryRecordID(_weeklySummary.Entity.EntityID, PeriodService.Instance.GetCurrentPeriod()[0].ID));
                }

                if (_recordID > 0)
                {
                    if (_pairs.Values.Count(RecordID => (RecordID == _recordID)) == 1)
                    {
                        using (RecordAccessClient _recordAccessClient = new RecordAccessClient(EndpointName.RecordAccess))
                        {
                            _recordAccessClient.ChangeStatus(_recordID, RecordStatus.Confirm);
                        }
                    }
                }
            }

            foreach (KeyValuePair<int, int> _pair in _pairs)
            {
                if (_pair.Value > 0)
                {
                    continue;
                }

                Entity _subtotal = null;

                if (_entity.SumType == SumType.Transaction)
                {
                    if ((_subtotal = _entity.SubEntities.FirstOrDefault(Entity => (_pair.Key == Entity.EntityID))) == null)
                    {
                        continue;
                    }
                }
                else if (_pair.Key == _entity.EntityID)
                {
                    _subtotal = _entity;
                }
                else
                {
                    continue;
                }

                EntityCollection _entityCollection = new EntityCollection() { _subtotal };

                _entityCollection.AddRange(_subtotal.SubEntities);

                decimal[] _baseTransfer = new decimal[_entityCollection.Count];

                for (int i = 1; i < _entityCollection.Count; i++)
                {
                    _baseTransfer[i] = _confirm.FirstOrDefault(WeeklySummary => (WeeklySummary.Entity.EntityID == _entityCollection[i].EntityID)).BaseTransfer;
                }

                Transfer _transfer = CalculateService.Instance.Transfer(confirmUserID, _entityCollection, _baseTransfer).Item1;

                DataEntryService.Instance.InsertTransfer(_transfer.RecordNotInDB, _transfer);
            }

            return _confirm;
        }