public Tuple<Transfer, Entity> Transfer(int entryUserID, EntityCollection entityCollection, decimal[] baseTransfer)
        {
            User _user = (new UserAccessClient(EndpointName.UserAccess)).QueryuserID(entryUserID)[0];

            Entity _exchangeDiffEntity = EntityService.Instance.LoadEntity(int.Parse(PropertiesService.Instance.GetPropertyValue(SpecialProperty.ExchangeDiff)[0].PropertyValue))[0];

            WeeklySummaryCollection _weeklySummaryCollection = new WeeklySummaryCollection();

            foreach (Entity _entity in entityCollection)
            {
                _weeklySummaryCollection.AddRange(GetWeeklySummary(_entity.EntityID));
            }

            Record _record = RecordHelper.GenerateTempRecord();

            _record.Type = RecordType.Transfer;

            int _index = 0;

            //Transfer
            Transfer _transfer = new Transfer()
            {
                RecordID = _record.RecordID,
                ToEntity = entityCollection[_index],
                Currency = new Currency() { CurrencyID = _weeklySummaryCollection[_index].BaseCurrency },
                ExchangeRate = _weeklySummaryCollection[_index].ExchangeRate,
                BaseBefore = _weeklySummaryCollection[_index].BaseBalance,
                SGDBefore = _weeklySummaryCollection[_index].SGDBalance,
            };

            foreach (Entity _entity in entityCollection)
            {
                if (_index == 0)
                {
                    _index++;

                    continue;
                }

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

                _transferDetail.BaseTransfer = baseTransfer[_index];
                _transferDetail.SGDTransfer = (_transferDetail.BaseTransfer / _transfer.ExchangeRate).ExtRound();
                _transferDetail.ProfitAndLoss = (_transferDetail.BaseTransfer / _transferDetail.ExchangeRate).ExtRound() - _transferDetail.SGDTransfer;

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

                _transfer.TransferDetailCollection.Add(_transferDetail);

                _index++;
            }

            _transfer.BaseResult = _transfer.TransferDetailCollection.Sum(x=>x.BaseTransfer)+_transfer.BaseBefore;
            _transfer.SGDResult = (_transfer.BaseResult / _transfer.ExchangeRate).ExtRound();

            //Record
            _index = 0;

            Journal _journal = new Journal()
            {
                RecordID = _record.RecordID,
                EntityID = entityCollection[_index].EntityID,
                BaseCurrency = _transfer.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 entityCollection)
            {
                if (_index == 0)
                {
                    _index++;

                    continue;
                }

                _journal = new Journal()
                {
                    RecordID = _record.RecordID,
                    EntityID = _entity.EntityID,
                    BaseCurrency = _transfer.TransferDetailCollection[_index - 1].BaseCurrency,
                    ExchangeRate = _transfer.TransferDetailCollection[_index - 1].ExchangeRate,
                    BaseAmount = _transfer.TransferDetailCollection[_index - 1].BaseTransfer * -1,
                    SGDAmount = (_transfer.TransferDetailCollection[_index - 1].SGDTransfer + _transfer.TransferDetailCollection[_index - 1].ProfitAndLoss) * -1,
                    EntryUser = _user,
                };

                _record.JournalCollection.Add(_journal);

                _index++;
            }

            _journal = new Journal()
            {
                RecordID = _record.RecordID,
                EntityID = _exchangeDiffEntity.EntityID,
                SGDAmount = _transfer.TransferDetailCollection.Sum(TransferDetail => TransferDetail.ProfitAndLoss)
            };

            _record.JournalCollection.Add(_journal);

            _transfer.RecordNotInDB = _record;

            return new Tuple<Transfer, Entity>(_transfer, _exchangeDiffEntity);
        }
        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;
        }
        private WeeklySummaryCollection ExcelConfirmTransfer(EntityCollection entityCollection, WeeklySummaryCollection forSave)
        {
            WeeklySummaryCollection _return = new WeeklySummaryCollection();

            foreach (Entity _entity in entityCollection)
            {
                if (_entity.IsLastLevel == 1)
                {
                    WeeklySummaryCollection _lastLevel = forSave;

                    if (_lastLevel.Count == 0)
                    {
                        continue;
                    }

                    forSave.AddRange(_lastLevel);
                    _return.AddRange(_lastLevel);
                }
                else if (_entity.SumType == SumType.Subtotal)
                {
                    WeeklySummaryCollection _lastLevel = ConfirmTransfer(_entity.SubEntities, forSave);

                    WeeklySummary _subtotal = CalculateService.Instance.GetWeeklySummary(_entity.EntityID)[0];

                    if (_subtotal.Status == WeeklySummaryStatus.Confirm)
                    {
                        forSave.Add(_subtotal);
                        _return.Add(_subtotal);

                        continue;
                    }

                    _subtotal.BaseTransfer = _lastLevel.Sum(WeeklySummary => WeeklySummary.BaseWinAndLoss);
                    _subtotal.SGDTransfer = _lastLevel.Sum(WeeklySummary => WeeklySummary.SGDWinAndLoss);

                    _lastLevel.All(lastLevel =>
                    {
                        lastLevel.BaseTransfer = lastLevel.BaseWinAndLoss * -1;
                        lastLevel.SGDTransfer = lastLevel.SGDWinAndLoss * -1;

                        lastLevel.BaseBalance += lastLevel.BasePrevBalance + lastLevel.BaseWinAndLoss + lastLevel.BaseTransfer;
                        lastLevel.SGDBalance += lastLevel.SGDPrevBalance + lastLevel.SGDWinAndLoss + lastLevel.SGDTransfer;

                        return true;
                    });

                    _subtotal.BaseBalance += _subtotal.BasePrevBalance + _subtotal.BaseWinAndLoss + _subtotal.BaseTransfer;
                    _subtotal.SGDBalance += _subtotal.SGDPrevBalance + _subtotal.SGDWinAndLoss + _subtotal.SGDTransfer;

                    forSave.Add(_subtotal);
                    _return.Add(_subtotal);
                }
                else if (_entity.SumType == SumType.Transaction)
                {
                    WeeklySummaryCollection _subtotal = ConfirmTransfer(_entity.SubEntities, forSave);

                    WeeklySummary _transfer = CalculateService.Instance.GetWeeklySummary(_entity.EntityID)[0];

                    if (_transfer.Status == WeeklySummaryStatus.Confirm)
                    {
                        forSave.Add(_transfer);
                        _return.Add(_transfer);

                        continue;
                    }

                    _transfer.BaseTransfer = _subtotal.Sum(WeeklySummary => WeeklySummary.BaseTransfer);
                    _transfer.SGDTransfer = _subtotal.Sum(WeeklySummary => WeeklySummary.SGDTransfer);

                    _subtotal.All(subtotal =>
                    {
                        subtotal.BaseBalance += subtotal.BasePrevBalance + subtotal.BaseWinAndLoss + subtotal.BaseTransfer;
                        subtotal.SGDBalance += subtotal.SGDPrevBalance + subtotal.SGDWinAndLoss + subtotal.SGDTransfer;

                        return true;
                    });

                    _transfer.BaseBalance += _transfer.BasePrevBalance + _transfer.BaseWinAndLoss + _transfer.BaseTransfer;
                    _transfer.SGDBalance += _transfer.SGDPrevBalance + _transfer.SGDWinAndLoss + _transfer.SGDTransfer;

                    forSave.Add(_transfer);
                    _return.Add(_transfer);
                }

                continue;
            }

            return _return;
        }
        private EntityCollection SetLastLevel(EntityCollection entityCollection, WeeklySummaryCollection weeklySummaryCollection)
        {
            for (int i = 0; i < entityCollection.Count; i++)
            {
                if (entityCollection[i].IsLastLevel == 1)
                {
                    WeeklySummaryCollection _weeklySummaryCollection = CalculateService.Instance.GetWeeklySummary(entityCollection[i].EntityID);

                    if (_weeklySummaryCollection.Count == 0)
                    {
                        entityCollection.RemoveAt(i--);
                    }
                    else
                    {
                        weeklySummaryCollection.AddRange(_weeklySummaryCollection);
                    }

                    continue;
                }

                EntityCollection _subEntities = entityCollection[i].SubEntities;

                entityCollection.RemoveAt(i);

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

                i--;
            }

            return entityCollection;
        }
        private WeeklySummaryCollection ConfirmTransfer(EntityCollection entityCollection, WeeklySummaryCollection forSave)
        {
            WeeklySummaryCollection _return = new WeeklySummaryCollection();

            foreach (Entity _entity in entityCollection)
            {
                if (_entity.IsLastLevel == 1)
                {
                    WeeklySummaryCollection _lastLevel = CalculateService.Instance.GetWeeklySummary(_entity.EntityID);

                    if (_lastLevel.Count == 0)
                    {
                        continue;
                    }

                    _lastLevel.All(lastLevel =>
                    {
                        List<decimal> _winAndLoss = DataEntryService.Instance.GetjournalSum(PeriodService.Instance.GetCurrentPeriod()[0].ID, (int)SumType.Subtotal, lastLevel.Entity.EntityID);

                        if (_winAndLoss.Count < 2)
                        {
                            return true;
                        }

                        lastLevel.BaseWinAndLoss = _winAndLoss[0];
                        lastLevel.SGDWinAndLoss = _winAndLoss[1];

                        return true;
                    });

                    forSave.AddRange(_lastLevel);
                    _return.AddRange(_lastLevel);
                }
                else if (_entity.SumType == SumType.Subtotal)
                {
                    WeeklySummaryCollection _lastLevel = ConfirmTransfer(_entity.SubEntities, forSave);

                    WeeklySummary _subtotal = CalculateService.Instance.GetWeeklySummary(_entity.EntityID)[0];

                    _subtotal.BaseWinAndLoss = _lastLevel.Sum(WeeklySummary => WeeklySummary.BaseWinAndLoss);
                    _subtotal.SGDWinAndLoss = _lastLevel.Sum(WeeklySummary => WeeklySummary.SGDWinAndLoss);

                    List<decimal> _transfer = DataEntryService.Instance.GetjournalSum(PeriodService.Instance.GetCurrentPeriod()[0].ID, (int)SumType.Transaction, _subtotal.Entity.EntityID);

                    if (_transfer.Count >= 2)
                    {
                        _subtotal.BaseTransfer = _transfer[0];
                        _subtotal.SGDTransfer = _transfer[1];
                    }

                    _lastLevel.All(lastLevel =>
                        {
                            lastLevel.BaseBalance = lastLevel.BasePrevBalance + lastLevel.BaseWinAndLoss + lastLevel.BaseTransfer;
                            lastLevel.SGDBalance = lastLevel.SGDPrevBalance + lastLevel.SGDWinAndLoss + lastLevel.SGDTransfer;

                            return true;
                        });

                    _subtotal.BaseBalance = _subtotal.BasePrevBalance + _subtotal.BaseWinAndLoss + _subtotal.BaseTransfer;
                    _subtotal.SGDBalance = _subtotal.SGDPrevBalance + _subtotal.SGDWinAndLoss + _subtotal.SGDTransfer;

                    forSave.Add(_subtotal);
                    _return.Add(_subtotal);
                }
                else if (_entity.SumType == SumType.Transaction)
                {
                    WeeklySummaryCollection _subtotal = ConfirmTransfer(_entity.SubEntities, forSave);

                    WeeklySummary _transfer = CalculateService.Instance.GetWeeklySummary(_entity.EntityID)[0];

                    _transfer.BaseWinAndLoss = _subtotal.Sum(WeeklySummary => WeeklySummary.BaseBalance);
                    _transfer.SGDWinAndLoss = _subtotal.Sum(WeeklySummary => WeeklySummary.SGDBalance);

                    _subtotal.All(subtotal =>
                    {
                        //subtotal.BaseBalance = subtotal.BasePrevBalance + subtotal.BaseWinAndLoss + subtotal.BaseTransfer;
                        //subtotal.SGDBalance = subtotal.SGDPrevBalance + subtotal.SGDWinAndLoss + subtotal.SGDTransfer;

                        return true;
                    });

                    _transfer.BaseBalance = _transfer.BasePrevBalance + _transfer.BaseWinAndLoss + _transfer.BaseTransfer;
                    _transfer.SGDBalance = _transfer.SGDPrevBalance + _transfer.SGDWinAndLoss + _transfer.SGDTransfer;

                    forSave.Add(_transfer);
                    _return.Add(_transfer);
                }

                continue;
            }

            return _return;
        }